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