util.h
Go to the documentation of this file.
1/*
2 * util.h
3 *
4 * helper function header file
5 *
6 * a Net::DNS like library for C
7 *
8 * (c) NLnet Labs, 2004
9 *
10 * See the file LICENSE for the license
11 */
12
13#ifndef _UTIL_H
14#define _UTIL_H
15
16#include <inttypes.h>
17#include <sys/types.h>
18#include <unistd.h>
19#include <ldns/common.h>
20#include <time.h>
21#include <stdio.h>
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27#define dprintf(X,Y) fprintf(stderr, (X), (Y))
28/* #define dprintf(X, Y) */
29
30#define LDNS_VERSION "1.9.0"
31#define LDNS_REVISION ((1<<16)|(9<<8)|(0))
32
36#ifdef S_SPLINT_S
37# define INLINE
38#else
39# ifdef SWIG
40# define INLINE static
41# else
42# define INLINE static inline
43# endif
44#endif
45
49#define LDNS_MALLOC(type) LDNS_XMALLOC(type, 1)
50
51#define LDNS_XMALLOC(type, count) ((type *) malloc((count) * sizeof(type)))
52
53#define LDNS_CALLOC(type, count) ((type *) calloc((count), sizeof(type)))
54
55#define LDNS_REALLOC(ptr, type) LDNS_XREALLOC((ptr), type, 1)
56
57#define LDNS_XREALLOC(ptr, type, count) \
58 ((type *) realloc((ptr), (count) * sizeof(type)))
59
60#define LDNS_FREE(ptr) \
61 do { free((ptr)); (ptr) = NULL; } while (0)
62
63#define LDNS_DEP printf("DEPRECATED FUNCTION!\n");
64
65/*
66 * Copy data allowing for unaligned accesses in network byte order
67 * (big endian).
68 */
69INLINE uint16_t
70ldns_read_uint16(const void *src)
71{
72#ifdef ALLOW_UNALIGNED_ACCESSES
73 return ntohs(*(const uint16_t *) src);
74#else
75# ifndef __clang_analyzer__
76 const uint8_t *p = (const uint8_t *) src;
77 return ((uint16_t) p[0] << 8) | (uint16_t) p[1];
78# endif
79#endif
80}
81
82INLINE uint32_t
83ldns_read_uint32(const void *src)
84{
85#ifdef ALLOW_UNALIGNED_ACCESSES
86 return ntohl(*(const uint32_t *) src);
87#else
88 const uint8_t *p = (const uint8_t *) src;
89 return ( ((uint32_t) p[0] << 24)
90 | ((uint32_t) p[1] << 16)
91 | ((uint32_t) p[2] << 8)
92 | (uint32_t) p[3]);
93#endif
94}
95
96INLINE uint64_t
97ldns_read_uint64(const void *src)
98{
99#ifdef ALLOW_UNALIGNED_ACCESSES
100 const uint32_t *p = (const uint32_t *) src;
101 return ( ((uint64_t) ntohl(src[0]) << 32)
102 | (uint64_t) ntohl(src[1]));
103#else
104 const uint8_t *p = (const uint8_t *) src;
105 return ( ((uint64_t) p[0] << 56)
106 | ((uint64_t) p[1] << 48)
107 | ((uint64_t) p[2] << 40)
108 | ((uint64_t) p[3] << 32)
109 | ((uint64_t) p[4] << 24)
110 | ((uint64_t) p[5] << 16)
111 | ((uint64_t) p[6] << 8)
112 | (uint64_t) p[7]);
113#endif
114}
115
116/*
117 * Copy data allowing for unaligned accesses in network byte order
118 * (big endian).
119 */
120INLINE void
121ldns_write_uint16(void *dst, uint16_t data)
122{
123#ifdef ALLOW_UNALIGNED_ACCESSES
124 * (uint16_t *) dst = htons(data);
125#else
126 uint8_t *p = (uint8_t *) dst;
127 p[0] = (uint8_t) ((data >> 8) & 0xff);
128 p[1] = (uint8_t) (data & 0xff);
129#endif
130}
131
132INLINE void
133ldns_write_uint32(void *dst, uint32_t data)
134{
135#ifdef ALLOW_UNALIGNED_ACCESSES
136 * (uint32_t *) dst = htonl(data);
137#else
138 uint8_t *p = (uint8_t *) dst;
139 p[0] = (uint8_t) ((data >> 24) & 0xff);
140 p[1] = (uint8_t) ((data >> 16) & 0xff);
141 p[2] = (uint8_t) ((data >> 8) & 0xff);
142 p[3] = (uint8_t) (data & 0xff);
143#endif
144}
145
146/* warning. */
147INLINE void
148ldns_write_uint64_as_uint48(void *dst, uint64_t data)
149{
150 uint8_t *p = (uint8_t *) dst;
151 p[0] = (uint8_t) ((data >> 40) & 0xff);
152 p[1] = (uint8_t) ((data >> 32) & 0xff);
153 p[2] = (uint8_t) ((data >> 24) & 0xff);
154 p[3] = (uint8_t) ((data >> 16) & 0xff);
155 p[4] = (uint8_t) ((data >> 8) & 0xff);
156 p[5] = (uint8_t) (data & 0xff);
157}
158
159
170
179 int id;
180 const char *name;
181};
183
191 const char *name);
192
200
209int ldns_get_bit(uint8_t bits[], size_t index);
210
211
220int ldns_get_bit_r(uint8_t bits[], size_t index);
221
232void ldns_set_bit(uint8_t *byte, int bit_nr, bool value);
233
238/*@unused@*/
239INLINE long
240ldns_power(long a, long b) {
241 long result = 1;
242 while (b > 0) {
243 if (b & 1) {
244 result *= a;
245 if (b == 1) {
246 return result;
247 }
248 }
249 a *= a;
250 b /= 2;
251 }
252 return result;
253}
254
260int ldns_hexdigit_to_int(char ch);
261
267char ldns_int_to_hexdigit(int ch);
268
278int
279ldns_hexstring_to_data(uint8_t *data, const char *str);
280
285const char * ldns_version(void);
286
293time_t ldns_mktime_from_utc(const struct tm *tm);
294
295time_t mktime_from_utc(const struct tm *tm);
296
311struct tm * ldns_serial_arithmetics_gmtime_r(int32_t time, time_t now, struct tm *result);
312
313/* previously used wrong spelling */
314#define ldns_serial_arithmitics_gmtime_r ldns_serial_arithmetics_gmtime_r
315
335int ldns_init_random(FILE *fd, unsigned int size);
336
342uint16_t ldns_get_random(void);
343
351char *ldns_bubblebabble(uint8_t *data, size_t len);
352
353
354INLINE time_t ldns_time(time_t *t) { return time(t); }
355
356
360/*@unused@*/
361INLINE size_t ldns_b32_ntop_calculate_size(size_t src_data_length)
362{
363 return src_data_length == 0 ? 0 : ((src_data_length - 1) / 5 + 1) * 8;
364}
365
366INLINE size_t ldns_b32_ntop_calculate_size_no_padding(size_t src_data_length)
367{
368 return ((src_data_length + 3) * 8 / 5) - 4;
369}
370
371int ldns_b32_ntop(const uint8_t* src_data, size_t src_data_length,
372 char* target_text_buffer, size_t target_text_buffer_size);
373
374int ldns_b32_ntop_extended_hex(const uint8_t* src_data, size_t src_data_length,
375 char* target_text_buffer, size_t target_text_buffer_size);
376
377#if ! LDNS_BUILD_CONFIG_HAVE_B32_NTOP
378
379int b32_ntop(const uint8_t* src_data, size_t src_data_length,
380 char* target_text_buffer, size_t target_text_buffer_size);
381
382int b32_ntop_extended_hex(const uint8_t* src_data, size_t src_data_length,
383 char* target_text_buffer, size_t target_text_buffer_size);
384
385#endif /* ! LDNS_BUILD_CONFIG_HAVE_B32_NTOP */
386
387
391/*@unused@*/
392INLINE size_t ldns_b32_pton_calculate_size(size_t src_text_length)
393{
394 return src_text_length * 5 / 8;
395}
396
397int ldns_b32_pton(const char* src_text, size_t src_text_length,
398 uint8_t* target_data_buffer, size_t target_data_buffer_size);
399
400int ldns_b32_pton_extended_hex(const char* src_text, size_t src_text_length,
401 uint8_t* target_data_buffer, size_t target_data_buffer_size);
402
403#if ! LDNS_BUILD_CONFIG_HAVE_B32_PTON
404
405int b32_pton(const char* src_text, size_t src_text_length,
406 uint8_t* target_data_buffer, size_t target_data_buffer_size);
407
408int b32_pton_extended_hex(const char* src_text, size_t src_text_length,
409 uint8_t* target_data_buffer, size_t target_data_buffer_size);
410
411#endif /* ! LDNS_BUILD_CONFIG_HAVE_B32_PTON */
412
413
414#ifdef __cplusplus
415}
416#endif
417
418#endif /* !_UTIL_H */
Common definitions for LDNS.
Structure to do a Schwartzian-like transformation, for instance when sorting.
Definition util.h:166
A general purpose lookup table.
Definition util.h:178
const char * name
Definition util.h:180
int ldns_hexdigit_to_int(char ch)
Returns the int value of the given (hex) digit.
Definition util.c:88
int ldns_b32_pton_extended_hex(const char *src_text, size_t src_text_length, uint8_t *target_data_buffer, size_t target_data_buffer_size)
Definition util.c:777
int ldns_hexstring_to_data(uint8_t *data, const char *str)
Converts a hex string to binary data.
Definition util.c:138
time_t ldns_mktime_from_utc(const struct tm *tm)
Convert TM to seconds since epoch (midnight, January 1st, 1970).
Definition util.c:194
int ldns_b32_ntop(const uint8_t *src_data, size_t src_data_length, char *target_text_buffer, size_t target_text_buffer_size)
Definition util.c:605
ldns_lookup_table * ldns_lookup_by_name(ldns_lookup_table table[], const char *name)
Looks up the table entry by name, returns NULL if not found.
#define INLINE
splint static inline workaround
Definition util.h:42
struct tm * ldns_serial_arithmetics_gmtime_r(int32_t time, time_t now, struct tm *result)
The function interprets time as the number of seconds since epoch with respect to now using serial ar...
Definition util.c:304
int ldns_b32_ntop_extended_hex(const uint8_t *src_data, size_t src_data_length, char *target_text_buffer, size_t target_text_buffer_size)
Definition util.c:611
int b32_ntop_extended_hex(const uint8_t *src_data, size_t src_data_length, char *target_text_buffer, size_t target_text_buffer_size)
Definition util.c:626
int b32_pton(const char *src_text, size_t src_text_length, uint8_t *target_data_buffer, size_t target_data_buffer_size)
Definition util.c:786
const char * ldns_version(void)
Show the internal library version.
Definition util.c:160
int ldns_get_bit_r(uint8_t bits[], size_t index)
Returns the value of the specified bit The bits are counted from right to left, so bit #0 is the righ...
Definition util.c:62
ldns_lookup_table * ldns_lookup_by_id(ldns_lookup_table table[], int id)
Looks up the table entry by id, returns NULL if not found.
char * ldns_bubblebabble(uint8_t *data, size_t len)
Encode data as BubbleBabble.
Definition util.c:435
void ldns_set_bit(uint8_t *byte, int bit_nr, signed char value)
sets the specified bit in the specified byte to 1 if value is true, 0 if false The bits are counted f...
Definition util.c:72
char ldns_int_to_hexdigit(int ch)
Returns the char (hex) representation of the given int.
Definition util.c:113
int ldns_get_bit(uint8_t bits[], size_t index)
Returns the value of the specified bit The bits are counted from left to right, so bit #0 is the left...
Definition util.c:52
int b32_ntop(const uint8_t *src_data, size_t src_data_length, char *target_text_buffer, size_t target_text_buffer_size)
Definition util.c:620
time_t mktime_from_utc(const struct tm *tm)
Definition util.c:219
uint16_t ldns_get_random(void)
Get random number.
Definition util.c:417
int ldns_b32_pton(const char *src_text, size_t src_text_length, uint8_t *target_data_buffer, size_t target_data_buffer_size)
Definition util.c:771
int ldns_init_random(FILE *fd, unsigned int size)
Seed the random function.
Definition util.c:339
int b32_pton_extended_hex(const char *src_text, size_t src_text_length, uint8_t *target_data_buffer, size_t target_data_buffer_size)
Definition util.c:792