Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
[dragonfly.git] / lib / libldns / ldns / util.h
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.6.7"
31 #define LDNS_REVISION ((1<<16)|(6<<8)|(7))
32
33 /**
34  * splint static inline workaround
35  */
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
46 /**
47  * Memory management macros
48  */
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_REALLOC(ptr, type)         LDNS_XREALLOC((ptr), type, 1)
54
55 #define LDNS_XREALLOC(ptr, type, count)                         \
56         ((type *) realloc((ptr), (count) * sizeof(type)))
57
58 #define LDNS_FREE(ptr) \
59         do { free((ptr)); (ptr) = NULL; } while (0)
60
61 #define LDNS_DEP     printf("DEPRECATED FUNCTION!\n");
62
63 /*
64  * Copy data allowing for unaligned accesses in network byte order
65  * (big endian).
66  */
67 INLINE uint16_t
68 ldns_read_uint16(const void *src)
69 {
70 #ifdef ALLOW_UNALIGNED_ACCESSES
71         return ntohs(*(uint16_t *) src);
72 #else
73         uint8_t *p = (uint8_t *) src;
74         return ((uint16_t) p[0] << 8) | (uint16_t) p[1];
75 #endif
76 }
77
78 INLINE uint32_t
79 ldns_read_uint32(const void *src)
80 {
81 #ifdef ALLOW_UNALIGNED_ACCESSES
82         return ntohl(*(uint32_t *) src);
83 #else
84         uint8_t *p = (uint8_t *) src;
85         return (  ((uint32_t) p[0] << 24)
86                 | ((uint32_t) p[1] << 16)
87                 | ((uint32_t) p[2] << 8)
88                 |  (uint32_t) p[3]);
89 #endif
90 }
91
92 /*
93  * Copy data allowing for unaligned accesses in network byte order
94  * (big endian).
95  */
96 INLINE void
97 ldns_write_uint16(void *dst, uint16_t data)
98 {
99 #ifdef ALLOW_UNALIGNED_ACCESSES
100         * (uint16_t *) dst = htons(data);
101 #else
102         uint8_t *p = (uint8_t *) dst;
103         p[0] = (uint8_t) ((data >> 8) & 0xff);
104         p[1] = (uint8_t) (data & 0xff);
105 #endif
106 }
107
108 INLINE void
109 ldns_write_uint32(void *dst, uint32_t data)
110 {
111 #ifdef ALLOW_UNALIGNED_ACCESSES
112         * (uint32_t *) dst = htonl(data);
113 #else
114         uint8_t *p = (uint8_t *) dst;
115         p[0] = (uint8_t) ((data >> 24) & 0xff);
116         p[1] = (uint8_t) ((data >> 16) & 0xff);
117         p[2] = (uint8_t) ((data >> 8) & 0xff);
118         p[3] = (uint8_t) (data & 0xff);
119 #endif
120 }
121
122 /* warning. */
123 INLINE void
124 ldns_write_uint64_as_uint48(void *dst, uint64_t data)
125 {
126         uint8_t *p = (uint8_t *) dst;
127         p[0] = (uint8_t) ((data >> 40) & 0xff);
128         p[1] = (uint8_t) ((data >> 32) & 0xff);
129         p[2] = (uint8_t) ((data >> 24) & 0xff);
130         p[3] = (uint8_t) ((data >> 16) & 0xff);
131         p[4] = (uint8_t) ((data >> 8) & 0xff);
132         p[5] = (uint8_t) (data & 0xff);
133 }
134
135
136 /**
137  * Structure to do a Schwartzian-like transformation, for instance when
138  * sorting. If you need a transformation on the objects that are sorted,
139  * you can sue this to store the transformed values, so you do not
140  * need to do the transformation again for each comparison
141  */
142 struct ldns_schwartzian_compare_struct {
143         void *original_object;
144         void *transformed_object;
145 };
146
147 /** A general purpose lookup table
148  *  
149  *  Lookup tables are arrays of (id, name) pairs,
150  *  So you can for instance lookup the RCODE 3, which is "NXDOMAIN",
151  *  and vice versa. The lookup tables themselves are defined wherever needed,
152  *  for instance in \ref host2str.c
153  */
154 struct ldns_struct_lookup_table {
155         int id;
156         const char *name;
157 };
158 typedef struct ldns_struct_lookup_table ldns_lookup_table;
159   
160 /**
161  * Looks up the table entry by name, returns NULL if not found.
162  * \param[in] table the lookup table to search in
163  * \param[in] name what to search for
164  * \return the item found
165  */
166 ldns_lookup_table *ldns_lookup_by_name(ldns_lookup_table table[],
167                                        const char *name);
168
169 /**
170  * Looks up the table entry by id, returns NULL if not found.
171  * \param[in] table the lookup table to search in
172  * \param[in] id what to search for
173  * \return the item found
174  */
175 ldns_lookup_table *ldns_lookup_by_id(ldns_lookup_table table[], int id);
176
177 /**
178  * Returns the value of the specified bit
179  * The bits are counted from left to right, so bit #0 is the
180  * left most bit.
181  * \param[in] bits array holding the bits
182  * \param[in] index to the wanted bit
183  * \return 
184  */
185 int ldns_get_bit(uint8_t bits[], size_t index);
186
187
188 /**
189  * Returns the value of the specified bit
190  * The bits are counted from right to left, so bit #0 is the
191  * right most bit.
192  * \param[in] bits array holding the bits
193  * \param[in] index to the wanted bit
194  * \return 1 or 0 depending no the bit state
195  */
196 int ldns_get_bit_r(uint8_t bits[], size_t index);
197
198 /**
199  * sets the specified bit in the specified byte to
200  * 1 if value is true, 0 if false
201  * The bits are counted from right to left, so bit #0 is the
202  * right most bit.
203  * \param[in] byte the bit to set the bit in
204  * \param[in] bit_nr the bit to set (0 <= n <= 7)
205  * \param[in] value whether to set the bit to 1 or 0
206  * \return 1 or 0 depending no the bit state
207  */
208 void ldns_set_bit(uint8_t *byte, int bit_nr, bool value);
209
210 /**
211  * Returns the value of a to the power of b
212  * (or 1 of b < 1)
213  */
214 /*@unused@*/
215 INLINE long
216 ldns_power(long a, long b) {
217         long result = 1;
218         while (b > 0) {
219                 if (b & 1) {
220                         result *= a;
221                         if (b == 1) {
222                                 return result;
223                         }
224                 }
225                 a *= a;
226                 b /= 2;
227         }
228         return result;
229 }
230
231 /**
232  * Returns the int value of the given (hex) digit
233  * \param[in] ch the hex char to convert
234  * \return the converted decimal value
235  */
236 int ldns_hexdigit_to_int(char ch);
237
238 /**
239  * Returns the char (hex) representation of the given int
240  * \param[in] ch the int to convert
241  * \return the converted hex char
242  */
243 char ldns_int_to_hexdigit(int ch);
244
245 /**
246  * Converts a hex string to binary data
247  *
248  * \param[out] data The binary result is placed here.
249  * At least strlen(str)/2 bytes should be allocated
250  * \param[in] str The hex string to convert.
251  * This string should not contain spaces
252  * \return The number of bytes of converted data, or -1 if one of the arguments * is NULL, or -2 if the string length is not an even number
253  */
254 int
255 ldns_hexstring_to_data(uint8_t *data, const char *str);
256
257 /**
258  * Show the internal library version
259  * \return a string with the version in it
260  */
261 const char * ldns_version(void);
262
263 /**
264  * Convert TM to seconds since epoch (midnight, January 1st, 1970).
265  * Like timegm(3), which is not always available.
266  * \param[in] tm a struct tm* with the date
267  * \return the seconds since epoch
268  */
269 time_t mktime_from_utc(const struct tm *tm);
270
271 /**
272  * Seed the random function.
273  * If the file descriptor is specified, the random generator is seeded with
274  * data from that file. If not, /dev/urandom is used.
275  *
276  * applications should call this if they need entropy data within ldns
277  * If openSSL is available, it is automatically seeded from /dev/urandom
278  * or /dev/random.
279  *
280  * If you need more entropy, or have no openssl available, this function
281  * MUST be called at the start of the program
282  *
283  * If openssl *is* available, this function just adds more entropy
284  *
285  * \param[in] fd a file providing entropy data for the seed
286  * \param[in] size the number of bytes to use as entropy data. If this is 0,
287  *            only the minimal amount is taken (usually 4 bytes)
288  * \return 0 if seeding succeeds, 1 if it fails
289  */
290 int ldns_init_random(FILE *fd, unsigned int size);
291
292 /**
293  * Get random number.
294  * \return random number.
295  *
296  */
297 uint16_t ldns_get_random(void);
298
299 /**
300  * Encode data as BubbleBabble
301  *
302  * \param[in] data a pointer to data to be encoded
303  * \param[in] len size the number of bytes of data
304  * \return a string of BubbleBabble
305  */
306 char *ldns_bubblebabble(uint8_t *data, size_t len);
307
308 #ifndef B32_NTOP
309 int ldns_b32_ntop(uint8_t const *src, size_t srclength,
310              char *target, size_t targsize);
311 int b32_ntop(uint8_t const *src, size_t srclength,
312              char *target, size_t targsize);
313 int ldns_b32_ntop_extended_hex(uint8_t const *src, size_t srclength,
314              char *target, size_t targsize);
315 int b32_ntop_extended_hex(uint8_t const *src, size_t srclength,
316              char *target, size_t targsize);
317 /**
318  * calculates the size needed to store the result of b32_ntop
319  */
320 /*@unused@*/
321 INLINE size_t ldns_b32_ntop_calculate_size(size_t srcsize)
322 {
323         size_t result = ((((srcsize / 5) * 8) - 2) + 2);
324         return result;
325 }
326 #endif /* !B32_NTOP */
327 #ifndef B32_PTON
328 int ldns_b32_pton(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize);
329 int b32_pton(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize);
330 int ldns_b32_pton_extended_hex(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize);
331 int b32_pton_extended_hex(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize);
332 /**
333  * calculates the size needed to store the result of b32_pton
334  */
335 /*@unused@*/
336 INLINE size_t ldns_b32_pton_calculate_size(size_t srcsize)
337 {
338         size_t result = ((((srcsize) / 8) * 5));
339         return result;
340 }
341 #endif /* !B32_PTON */
342
343 #ifdef __cplusplus
344 }
345 #endif
346
347 #endif /* !_UTIL_H */