ldns  1.7.0
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
24 extern "C" {
25 #endif
26 
27 #define dprintf(X,Y) fprintf(stderr, (X), (Y))
28 /* #define dprintf(X, Y) */
29 
30 #define LDNS_VERSION "1.7.0"
31 #define LDNS_REVISION ((1<<16)|(7<<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  */
69 INLINE uint16_t
70 ldns_read_uint16(const void *src)
71 {
72 #ifdef ALLOW_UNALIGNED_ACCESSES
73  return ntohs(*(const uint16_t *) src);
74 #else
75  const uint8_t *p = (const uint8_t *) src;
76  return ((uint16_t) p[0] << 8) | (uint16_t) p[1];
77 #endif
78 }
79 
80 INLINE uint32_t
81 ldns_read_uint32(const void *src)
82 {
83 #ifdef ALLOW_UNALIGNED_ACCESSES
84  return ntohl(*(const uint32_t *) src);
85 #else
86  const uint8_t *p = (const uint8_t *) src;
87  return ( ((uint32_t) p[0] << 24)
88  | ((uint32_t) p[1] << 16)
89  | ((uint32_t) p[2] << 8)
90  | (uint32_t) p[3]);
91 #endif
92 }
93 
94 /*
95  * Copy data allowing for unaligned accesses in network byte order
96  * (big endian).
97  */
98 INLINE void
99 ldns_write_uint16(void *dst, uint16_t data)
100 {
101 #ifdef ALLOW_UNALIGNED_ACCESSES
102  * (uint16_t *) dst = htons(data);
103 #else
104  uint8_t *p = (uint8_t *) dst;
105  p[0] = (uint8_t) ((data >> 8) & 0xff);
106  p[1] = (uint8_t) (data & 0xff);
107 #endif
108 }
109 
110 INLINE void
111 ldns_write_uint32(void *dst, uint32_t data)
112 {
113 #ifdef ALLOW_UNALIGNED_ACCESSES
114  * (uint32_t *) dst = htonl(data);
115 #else
116  uint8_t *p = (uint8_t *) dst;
117  p[0] = (uint8_t) ((data >> 24) & 0xff);
118  p[1] = (uint8_t) ((data >> 16) & 0xff);
119  p[2] = (uint8_t) ((data >> 8) & 0xff);
120  p[3] = (uint8_t) (data & 0xff);
121 #endif
122 }
123 
124 /* warning. */
125 INLINE void
126 ldns_write_uint64_as_uint48(void *dst, uint64_t data)
127 {
128  uint8_t *p = (uint8_t *) dst;
129  p[0] = (uint8_t) ((data >> 40) & 0xff);
130  p[1] = (uint8_t) ((data >> 32) & 0xff);
131  p[2] = (uint8_t) ((data >> 24) & 0xff);
132  p[3] = (uint8_t) ((data >> 16) & 0xff);
133  p[4] = (uint8_t) ((data >> 8) & 0xff);
134  p[5] = (uint8_t) (data & 0xff);
135 }
136 
137 
147 };
148 
157  int id;
158  const char *name;
159 };
161 
169  const char *name);
170 
178 
187 int ldns_get_bit(uint8_t bits[], size_t index);
188 
189 
198 int ldns_get_bit_r(uint8_t bits[], size_t index);
199 
210 void ldns_set_bit(uint8_t *byte, int bit_nr, bool value);
211 
216 /*@unused@*/
217 INLINE long
218 ldns_power(long a, long b) {
219  long result = 1;
220  while (b > 0) {
221  if (b & 1) {
222  result *= a;
223  if (b == 1) {
224  return result;
225  }
226  }
227  a *= a;
228  b /= 2;
229  }
230  return result;
231 }
232 
238 int ldns_hexdigit_to_int(char ch);
239 
245 char ldns_int_to_hexdigit(int ch);
246 
256 int
257 ldns_hexstring_to_data(uint8_t *data, const char *str);
258 
263 const char * ldns_version(void);
264 
271 time_t ldns_mktime_from_utc(const struct tm *tm);
272 
273 time_t mktime_from_utc(const struct tm *tm);
274 
289 struct tm * ldns_serial_arithmitics_gmtime_r(int32_t time, time_t now, struct tm *result);
290 
310 int ldns_init_random(FILE *fd, unsigned int size);
311 
317 uint16_t ldns_get_random(void);
318 
326 char *ldns_bubblebabble(uint8_t *data, size_t len);
327 
328 
329 INLINE time_t ldns_time(time_t *t) { return time(t); }
330 
331 
335 /*@unused@*/
336 INLINE size_t ldns_b32_ntop_calculate_size(size_t src_data_length)
337 {
338  return src_data_length == 0 ? 0 : ((src_data_length - 1) / 5 + 1) * 8;
339 }
340 
341 INLINE size_t ldns_b32_ntop_calculate_size_no_padding(size_t src_data_length)
342 {
343  return ((src_data_length + 3) * 8 / 5) - 4;
344 }
345 
346 int ldns_b32_ntop(const uint8_t* src_data, size_t src_data_length,
347  char* target_text_buffer, size_t target_text_buffer_size);
348 
349 int ldns_b32_ntop_extended_hex(const uint8_t* src_data, size_t src_data_length,
350  char* target_text_buffer, size_t target_text_buffer_size);
351 
352 #if ! LDNS_BUILD_CONFIG_HAVE_B32_NTOP
353 
354 int b32_ntop(const uint8_t* src_data, size_t src_data_length,
355  char* target_text_buffer, size_t target_text_buffer_size);
356 
357 int b32_ntop_extended_hex(const uint8_t* src_data, size_t src_data_length,
358  char* target_text_buffer, size_t target_text_buffer_size);
359 
360 #endif /* ! LDNS_BUILD_CONFIG_HAVE_B32_NTOP */
361 
362 
366 /*@unused@*/
367 INLINE size_t ldns_b32_pton_calculate_size(size_t src_text_length)
368 {
369  return src_text_length * 5 / 8;
370 }
371 
372 int ldns_b32_pton(const char* src_text, size_t src_text_length,
373  uint8_t* target_data_buffer, size_t target_data_buffer_size);
374 
375 int ldns_b32_pton_extended_hex(const char* src_text, size_t src_text_length,
376  uint8_t* target_data_buffer, size_t target_data_buffer_size);
377 
378 #if ! LDNS_BUILD_CONFIG_HAVE_B32_PTON
379 
380 int b32_pton(const char* src_text, size_t src_text_length,
381  uint8_t* target_data_buffer, size_t target_data_buffer_size);
382 
383 int b32_pton_extended_hex(const char* src_text, size_t src_text_length,
384  uint8_t* target_data_buffer, size_t target_data_buffer_size);
385 
386 #endif /* ! LDNS_BUILD_CONFIG_HAVE_B32_PTON */
387 
388 
389 #ifdef __cplusplus
390 }
391 #endif
392 
393 #endif /* !_UTIL_H */
ldns_schwartzian_compare_struct::transformed_object
void * transformed_object
Definition: util.h:146
ldns_schwartzian_compare_struct::original_object
void * original_object
Definition: util.h:145
ldns_serial_arithmitics_gmtime_r
struct tm * ldns_serial_arithmitics_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
ldns_lookup_by_name
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.
ldns_b32_pton_extended_hex
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:751
ldns_struct_lookup_table::id
int id
Definition: util.h:157
b32_ntop
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:597
ldns_hexstring_to_data
int ldns_hexstring_to_data(uint8_t *data, const char *str)
Converts a hex string to binary data.
Definition: util.c:138
ldns_b32_pton
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:745
ldns_set_bit
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...
ldns_get_bit
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
ldns_b32_ntop
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:582
ldns_bubblebabble
char * ldns_bubblebabble(uint8_t *data, size_t len)
Encode data as BubbleBabble.
Definition: util.c:418
ldns_version
const char * ldns_version(void)
Show the internal library version.
Definition: util.c:160
ldns_schwartzian_compare_struct
Structure to do a Schwartzian-like transformation, for instance when sorting.
Definition: util.h:144
ldns_lookup_by_id
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.
ldns_mktime_from_utc
time_t ldns_mktime_from_utc(const struct tm *tm)
Convert TM to seconds since epoch (midnight, January 1st, 1970).
Definition: util.c:194
ldns_struct_lookup_table::name
const char * name
Definition: util.h:158
b32_ntop_extended_hex
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:603
ldns_init_random
int ldns_init_random(FILE *fd, unsigned int size)
Seed the random function.
Definition: util.c:327
ldns_get_bit_r
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
INLINE
#define INLINE
splint static inline workaround
Definition: util.h:42
ldns_struct_lookup_table
A general purpose lookup table.
Definition: util.h:156
ldns_int_to_hexdigit
char ldns_int_to_hexdigit(int ch)
Returns the char (hex) representation of the given int.
Definition: util.c:113
ldns_get_random
uint16_t ldns_get_random(void)
Get random number.
Definition: util.c:400
b32_pton_extended_hex
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:766
common.h
ldns_b32_ntop_extended_hex
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:588
mktime_from_utc
time_t mktime_from_utc(const struct tm *tm)
Definition: util.c:219
ldns_hexdigit_to_int
int ldns_hexdigit_to_int(char ch)
Returns the int value of the given (hex) digit.
Definition: util.c:88
b32_pton
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:760