util.c
Go to the documentation of this file.
1 /*
2  * util.c
3  *
4  * some general memory functions
5  *
6  * a Net::DNS like library for C
7  *
8  * (c) NLnet Labs, 2004-2006
9  *
10  * See the file LICENSE for the license
11  */
12 
13 #include <ldns/config.h>
14 
15 #include <ldns/rdata.h>
16 #include <ldns/rr.h>
17 #include <ldns/util.h>
18 #include <strings.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <sys/time.h>
22 #include <time.h>
23 #include <ctype.h>
24 
25 #ifdef HAVE_SSL
26 #include <openssl/rand.h>
27 #endif
28 
30 ldns_lookup_by_name(ldns_lookup_table *table, const char *name)
31 {
32  while (table->name != NULL) {
33  if (strcasecmp(name, table->name) == 0)
34  return table;
35  table++;
36  }
37  return NULL;
38 }
39 
42 {
43  while (table->name != NULL) {
44  if (table->id == id)
45  return table;
46  table++;
47  }
48  return NULL;
49 }
50 
51 int
52 ldns_get_bit(uint8_t bits[], size_t index)
53 {
54  /*
55  * The bits are counted from left to right, so bit #0 is the
56  * left most bit.
57  */
58  return (int) (bits[index / 8] & (1 << (7 - index % 8)));
59 }
60 
61 int
62 ldns_get_bit_r(uint8_t bits[], size_t index)
63 {
64  /*
65  * The bits are counted from right to left, so bit #0 is the
66  * right most bit.
67  */
68  return (int) bits[index / 8] & (1 << (index % 8));
69 }
70 
71 void
72 ldns_set_bit(uint8_t *byte, int bit_nr, bool value)
73 {
74  /*
75  * The bits are counted from right to left, so bit #0 is the
76  * right most bit.
77  */
78  if (bit_nr >= 0 && bit_nr < 8) {
79  if (value) {
80  *byte = *byte | (0x01 << bit_nr);
81  } else {
82  *byte = *byte & ~(0x01 << bit_nr);
83  }
84  }
85 }
86 
87 int
89 {
90  switch (ch) {
91  case '0': return 0;
92  case '1': return 1;
93  case '2': return 2;
94  case '3': return 3;
95  case '4': return 4;
96  case '5': return 5;
97  case '6': return 6;
98  case '7': return 7;
99  case '8': return 8;
100  case '9': return 9;
101  case 'a': case 'A': return 10;
102  case 'b': case 'B': return 11;
103  case 'c': case 'C': return 12;
104  case 'd': case 'D': return 13;
105  case 'e': case 'E': return 14;
106  case 'f': case 'F': return 15;
107  default:
108  return -1;
109  }
110 }
111 
112 char
114 {
115  switch (i) {
116  case 0: return '0';
117  case 1: return '1';
118  case 2: return '2';
119  case 3: return '3';
120  case 4: return '4';
121  case 5: return '5';
122  case 6: return '6';
123  case 7: return '7';
124  case 8: return '8';
125  case 9: return '9';
126  case 10: return 'a';
127  case 11: return 'b';
128  case 12: return 'c';
129  case 13: return 'd';
130  case 14: return 'e';
131  case 15: return 'f';
132  default:
133  abort();
134  }
135 }
136 
137 int
138 ldns_hexstring_to_data(uint8_t *data, const char *str)
139 {
140  size_t i;
141 
142  if (!str || !data) {
143  return -1;
144  }
145 
146  if (strlen(str) % 2 != 0) {
147  return -2;
148  }
149 
150  for (i = 0; i < strlen(str) / 2; i++) {
151  data[i] =
152  16 * (uint8_t) ldns_hexdigit_to_int(str[i*2]) +
153  (uint8_t) ldns_hexdigit_to_int(str[i*2 + 1]);
154  }
155 
156  return (int) i;
157 }
158 
159 const char *
161 {
162  return (char*)LDNS_VERSION;
163 }
164 
165 /* Number of days per month (except for February in leap years). */
166 static const int mdays[] = {
167  31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
168 };
169 
170 #define LDNS_MOD(x,y) (((x) % (y) < 0) ? ((x) % (y) + (y)) : ((x) % (y)))
171 #define LDNS_DIV(x,y) (((x) % (y) < 0) ? ((x) / (y) - 1 ) : ((x) / (y)))
172 
173 static int
174 is_leap_year(int year)
175 {
176  return LDNS_MOD(year, 4) == 0 && (LDNS_MOD(year, 100) != 0
177  || LDNS_MOD(year, 400) == 0);
178 }
179 
180 static int
181 leap_days(int y1, int y2)
182 {
183  --y1;
184  --y2;
185  return (LDNS_DIV(y2, 4) - LDNS_DIV(y1, 4)) -
186  (LDNS_DIV(y2, 100) - LDNS_DIV(y1, 100)) +
187  (LDNS_DIV(y2, 400) - LDNS_DIV(y1, 400));
188 }
189 
190 /*
191  * Code adapted from Python 2.4.1 sources (Lib/calendar.py).
192  */
193 time_t
194 ldns_mktime_from_utc(const struct tm *tm)
195 {
196  int year = 1900 + tm->tm_year;
197  time_t days = 365 * ((time_t) year - 1970) + leap_days(1970, year);
198  time_t hours;
199  time_t minutes;
200  time_t seconds;
201  int i;
202 
203  for (i = 0; i < tm->tm_mon; ++i) {
204  days += mdays[i];
205  }
206  if (tm->tm_mon > 1 && is_leap_year(year)) {
207  ++days;
208  }
209  days += tm->tm_mday - 1;
210 
211  hours = days * 24 + tm->tm_hour;
212  minutes = hours * 60 + tm->tm_min;
213  seconds = minutes * 60 + tm->tm_sec;
214 
215  return seconds;
216 }
217 
218 time_t
219 mktime_from_utc(const struct tm *tm)
220 {
221  return ldns_mktime_from_utc(tm);
222 }
223 
224 #if SIZEOF_TIME_T <= 4
225 
226 static void
227 ldns_year_and_yday_from_days_since_epoch(int64_t days, struct tm *result)
228 {
229  int year = 1970;
230  int new_year;
231 
232  while (days < 0 || days >= (int64_t) (is_leap_year(year) ? 366 : 365)) {
233  new_year = year + (int) LDNS_DIV(days, 365);
234  days -= (new_year - year) * 365;
235  days -= leap_days(year, new_year);
236  year = new_year;
237  }
238  result->tm_year = year;
239  result->tm_yday = (int) days;
240 }
241 
242 /* Number of days per month in a leap year. */
243 static const int leap_year_mdays[] = {
244  31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
245 };
246 
247 static void
248 ldns_mon_and_mday_from_year_and_yday(struct tm *result)
249 {
250  int idays = result->tm_yday;
251  const int *mon_lengths = is_leap_year(result->tm_year) ?
252  leap_year_mdays : mdays;
253 
254  result->tm_mon = 0;
255  while (idays >= mon_lengths[result->tm_mon]) {
256  idays -= mon_lengths[result->tm_mon++];
257  }
258  result->tm_mday = idays + 1;
259 }
260 
261 static void
262 ldns_wday_from_year_and_yday(struct tm *result)
263 {
264  result->tm_wday = 4 /* 1-1-1970 was a thursday */
265  + LDNS_MOD((result->tm_year - 1970), 7) * LDNS_MOD(365, 7)
266  + leap_days(1970, result->tm_year)
267  + result->tm_yday;
268  result->tm_wday = LDNS_MOD(result->tm_wday, 7);
269  if (result->tm_wday < 0) {
270  result->tm_wday += 7;
271  }
272 }
273 
274 static struct tm *
275 ldns_gmtime64_r(int64_t clock, struct tm *result)
276 {
277  result->tm_isdst = 0;
278  result->tm_sec = (int) LDNS_MOD(clock, 60);
279  clock = LDNS_DIV(clock, 60);
280  result->tm_min = (int) LDNS_MOD(clock, 60);
281  clock = LDNS_DIV(clock, 60);
282  result->tm_hour = (int) LDNS_MOD(clock, 24);
283  clock = LDNS_DIV(clock, 24);
284 
285  ldns_year_and_yday_from_days_since_epoch(clock, result);
286  ldns_mon_and_mday_from_year_and_yday(result);
287  ldns_wday_from_year_and_yday(result);
288  result->tm_year -= 1900;
289 
290  return result;
291 }
292 
293 #endif /* SIZEOF_TIME_T <= 4 */
294 
295 static int64_t
296 ldns_serial_arithmetics_time(int32_t time, time_t now)
297 {
298  /* Casting due to https://github.com/NLnetLabs/ldns/issues/71 */
299  int32_t offset = (int32_t) ((uint32_t) time - (uint32_t) now);
300  return (int64_t) now + offset;
301 }
302 
303 struct tm *
304 ldns_serial_arithmetics_gmtime_r(int32_t time, time_t now, struct tm *result)
305 {
306 #if SIZEOF_TIME_T <= 4
307  int64_t secs_since_epoch = ldns_serial_arithmetics_time(time, now);
308  return ldns_gmtime64_r(secs_since_epoch, result);
309 #else
310  time_t secs_since_epoch = ldns_serial_arithmetics_time(time, now);
311  return gmtime_r(&secs_since_epoch, result);
312 #endif
313 }
314 
315 #ifdef ldns_serial_arithmitics_gmtime_r
316 #undef ldns_serial_arithmitics_gmtime_r
317 #endif
318 /* alias function because of previously used wrong spelling */
319 struct tm *
320 ldns_serial_arithmitics_gmtime_r(int32_t time, time_t now, struct tm *result);
321 struct tm *
322 ldns_serial_arithmitics_gmtime_r(int32_t time, time_t now, struct tm *result)
323 {
324  return ldns_serial_arithmetics_gmtime_r(time, now, result);
325 }
326 
338 int
339 ldns_init_random(FILE *fd, unsigned int size)
340 {
341  /* if fp is given, seed srandom with data from file
342  otherwise use /dev/urandom */
343  FILE *rand_f;
344  uint8_t *seed;
345  size_t read = 0;
346  unsigned int seed_i;
347  struct timeval tv;
348 
349  /* we'll need at least sizeof(unsigned int) bytes for the
350  standard prng seed */
351  if (size < (unsigned int) sizeof(seed_i)){
352  size = (unsigned int) sizeof(seed_i);
353  }
354 
355  seed = LDNS_XMALLOC(uint8_t, size);
356  if(!seed) {
357  return 1;
358  }
359 
360  if (!fd) {
361  if ((rand_f = fopen("/dev/urandom", "r")) == NULL) {
362  /* no readable /dev/urandom, try /dev/random */
363  if ((rand_f = fopen("/dev/random", "r")) == NULL) {
364  /* no readable /dev/random either, and no entropy
365  source given. we'll have to improvise */
366  for (read = 0; read < size; read++) {
367  gettimeofday(&tv, NULL);
368  seed[read] = (uint8_t) (tv.tv_usec % 256);
369  }
370  } else {
371  read = fread(seed, 1, size, rand_f);
372  }
373  } else {
374  read = fread(seed, 1, size, rand_f);
375  }
376  } else {
377  rand_f = fd;
378  read = fread(seed, 1, size, rand_f);
379  }
380 
381  if (read < size) {
382  LDNS_FREE(seed);
383  if (!fd) fclose(rand_f);
384  return 1;
385  } else {
386 #ifdef HAVE_SSL
387  /* Seed the OpenSSL prng (most systems have it seeded
388  automatically, in that case this call just adds entropy */
389  RAND_seed(seed, (int) size);
390 #else
391  /* Seed the standard prng, only uses the first
392  * unsigned sizeof(unsigned int) bytes found in the entropy pool
393  */
394  memcpy(&seed_i, seed, sizeof(seed_i));
395  srandom(seed_i);
396 #endif
397  LDNS_FREE(seed);
398  }
399 
400  if (!fd) {
401  if (rand_f) fclose(rand_f);
402  }
403 
404  return 0;
405 }
406 
411 uint16_t
413 {
414  uint16_t rid = 0;
415 #ifdef HAVE_SSL
416  if (RAND_bytes((unsigned char*)&rid, 2) != 1) {
417  rid = (uint16_t) random();
418  }
419 #else
420  rid = (uint16_t) random();
421 #endif
422  return rid;
423 }
424 
425 /*
426  * BubbleBabble code taken from OpenSSH
427  * Copyright (c) 2001 Carsten Raskgaard. All rights reserved.
428  */
429 char *
430 ldns_bubblebabble(uint8_t *data, size_t len)
431 {
432  char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
433  char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
434  'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
435  size_t i, j = 0, rounds, seed = 1;
436  char *retval;
437 
438  rounds = (len / 2) + 1;
439  retval = LDNS_XMALLOC(char, rounds * 6);
440  if(!retval) return NULL;
441  retval[j++] = 'x';
442  for (i = 0; i < rounds; i++) {
443  size_t idx0, idx1, idx2, idx3, idx4;
444  if ((i + 1 < rounds) || (len % 2 != 0)) {
445  idx0 = (((((size_t)(data[2 * i])) >> 6) & 3) +
446  seed) % 6;
447  idx1 = (((size_t)(data[2 * i])) >> 2) & 15;
448  idx2 = ((((size_t)(data[2 * i])) & 3) +
449  (seed / 6)) % 6;
450  retval[j++] = vowels[idx0];
451  retval[j++] = consonants[idx1];
452  retval[j++] = vowels[idx2];
453  if ((i + 1) < rounds) {
454  idx3 = (((size_t)(data[(2 * i) + 1])) >> 4) & 15;
455  idx4 = (((size_t)(data[(2 * i) + 1]))) & 15;
456  retval[j++] = consonants[idx3];
457  retval[j++] = '-';
458  retval[j++] = consonants[idx4];
459  seed = ((seed * 5) +
460  ((((size_t)(data[2 * i])) * 7) +
461  ((size_t)(data[(2 * i) + 1])))) % 36;
462  }
463  } else {
464  idx0 = seed % 6;
465  idx1 = 16;
466  idx2 = seed / 6;
467  retval[j++] = vowels[idx0];
468  retval[j++] = consonants[idx1];
469  retval[j++] = vowels[idx2];
470  }
471  }
472  retval[j++] = 'x';
473  retval[j++] = '\0';
474  return retval;
475 }
476 
477 /*
478  * For backwards compatibility, because we have always exported this symbol.
479  */
480 #ifdef HAVE_B64_NTOP
481 int ldns_b64_ntop(const uint8_t* src, size_t srclength,
482  char *target, size_t targsize);
483 {
484  return b64_ntop(src, srclength, target, targsize);
485 }
486 #endif
487 
488 /*
489  * For backwards compatibility, because we have always exported this symbol.
490  */
491 #ifdef HAVE_B64_PTON
492 int ldns_b64_pton(const char* src, uint8_t *target, size_t targsize)
493 {
494  return b64_pton(src, target, targsize);
495 }
496 #endif
497 
498 
499 static int
500 ldns_b32_ntop_base(const uint8_t* src, size_t src_sz,
501  char* dst, size_t dst_sz,
502  bool extended_hex, bool add_padding)
503 {
504  size_t ret_sz;
505  const char* b32 = extended_hex ? "0123456789abcdefghijklmnopqrstuv"
506  : "abcdefghijklmnopqrstuvwxyz234567";
507 
508  size_t c = 0; /* c is used to carry partial base32 character over
509  * byte boundaries for sizes with a remainder.
510  * (i.e. src_sz % 5 != 0)
511  */
512 
513  ret_sz = add_padding ? ldns_b32_ntop_calculate_size(src_sz)
514  : ldns_b32_ntop_calculate_size_no_padding(src_sz);
515 
516  /* Do we have enough space? */
517  if (dst_sz < ret_sz + 1)
518  return -1;
519 
520  /* We know the size; terminate the string */
521  dst[ret_sz] = '\0';
522 
523  /* First process all chunks of five */
524  while (src_sz >= 5) {
525  /* 00000... ........ ........ ........ ........ */
526  dst[0] = b32[(src[0] ) >> 3];
527 
528  /* .....111 11...... ........ ........ ........ */
529  dst[1] = b32[(src[0] & 0x07) << 2 | src[1] >> 6];
530 
531  /* ........ ..22222. ........ ........ ........ */
532  dst[2] = b32[(src[1] & 0x3e) >> 1];
533 
534  /* ........ .......3 3333.... ........ ........ */
535  dst[3] = b32[(src[1] & 0x01) << 4 | src[2] >> 4];
536 
537  /* ........ ........ ....4444 4....... ........ */
538  dst[4] = b32[(src[2] & 0x0f) << 1 | src[3] >> 7];
539 
540  /* ........ ........ ........ .55555.. ........ */
541  dst[5] = b32[(src[3] & 0x7c) >> 2];
542 
543  /* ........ ........ ........ ......66 666..... */
544  dst[6] = b32[(src[3] & 0x03) << 3 | src[4] >> 5];
545 
546  /* ........ ........ ........ ........ ...77777 */
547  dst[7] = b32[(src[4] & 0x1f) ];
548 
549  src_sz -= 5;
550  src += 5;
551  dst += 8;
552  }
553  /* Process what remains */
554  switch (src_sz) {
555  case 4: /* ........ ........ ........ ......66 666..... */
556  dst[6] = b32[(src[3] & 0x03) << 3];
557 
558  /* ........ ........ ........ .55555.. ........ */
559  dst[5] = b32[(src[3] & 0x7c) >> 2];
560 
561  /* ........ ........ ....4444 4....... ........ */
562  c = src[3] >> 7 ;
563  /* fallthrough */
564  case 3: dst[4] = b32[(src[2] & 0x0f) << 1 | c];
565 
566  /* ........ .......3 3333.... ........ ........ */
567  c = src[2] >> 4 ;
568  /* fallthrough */
569  case 2: dst[3] = b32[(src[1] & 0x01) << 4 | c];
570 
571  /* ........ ..22222. ........ ........ ........ */
572  dst[2] = b32[(src[1] & 0x3e) >> 1];
573 
574  /* .....111 11...... ........ ........ ........ */
575  c = src[1] >> 6 ;
576  /* fallthrough */
577  case 1: dst[1] = b32[(src[0] & 0x07) << 2 | c];
578 
579  /* 00000... ........ ........ ........ ........ */
580  dst[0] = b32[ src[0] >> 3];
581  }
582  /* Add padding */
583  if (add_padding) {
584  switch (src_sz) {
585  case 1: dst[2] = '=';
586  dst[3] = '=';
587  /* fallthrough */
588  case 2: dst[4] = '=';
589  /* fallthrough */
590  case 3: dst[5] = '=';
591  dst[6] = '=';
592  /* fallthrough */
593  case 4: dst[7] = '=';
594  }
595  }
596  return (int)ret_sz;
597 }
598 
599 int
600 ldns_b32_ntop(const uint8_t* src, size_t src_sz, char* dst, size_t dst_sz)
601 {
602  return ldns_b32_ntop_base(src, src_sz, dst, dst_sz, false, true);
603 }
604 
605 int
606 ldns_b32_ntop_extended_hex(const uint8_t* src, size_t src_sz,
607  char* dst, size_t dst_sz)
608 {
609  return ldns_b32_ntop_base(src, src_sz, dst, dst_sz, true, true);
610 }
611 
612 #ifndef HAVE_B32_NTOP
613 
614 int
615 b32_ntop(const uint8_t* src, size_t src_sz, char* dst, size_t dst_sz)
616 {
617  return ldns_b32_ntop_base(src, src_sz, dst, dst_sz, false, true);
618 }
619 
620 int
621 b32_ntop_extended_hex(const uint8_t* src, size_t src_sz,
622  char* dst, size_t dst_sz)
623 {
624  return ldns_b32_ntop_base(src, src_sz, dst, dst_sz, true, true);
625 }
626 
627 #endif /* ! HAVE_B32_NTOP */
628 
629 static int
630 ldns_b32_pton_base(const char* src, size_t src_sz,
631  uint8_t* dst, size_t dst_sz,
632  bool extended_hex, bool check_padding)
633 {
634  size_t i = 0;
635  char ch = '\0';
636  uint8_t buf[8];
637  uint8_t* start = dst;
638 
639  while (src_sz) {
640  /* Collect 8 characters in buf (if possible) */
641  for (i = 0; i < 8; i++) {
642 
643  do {
644  ch = *src++;
645  --src_sz;
646 
647  } while (isspace((unsigned char)ch) && src_sz > 0);
648 
649  if (ch == '=' || ch == '\0')
650  break;
651 
652  else if (extended_hex)
653 
654  if (ch >= '0' && ch <= '9')
655  buf[i] = (uint8_t)ch - '0';
656  else if (ch >= 'a' && ch <= 'v')
657  buf[i] = (uint8_t)ch - 'a' + 10;
658  else if (ch >= 'A' && ch <= 'V')
659  buf[i] = (uint8_t)ch - 'A' + 10;
660  else
661  return -1;
662 
663  else if (ch >= 'a' && ch <= 'z')
664  buf[i] = (uint8_t)ch - 'a';
665  else if (ch >= 'A' && ch <= 'Z')
666  buf[i] = (uint8_t)ch - 'A';
667  else if (ch >= '2' && ch <= '7')
668  buf[i] = (uint8_t)ch - '2' + 26;
669  else
670  return -1;
671  }
672  /* Less that 8 characters. We're done. */
673  if (i < 8)
674  break;
675 
676  /* Enough space available at the destination? */
677  if (dst_sz < 5)
678  return -1;
679 
680  /* 00000... ........ ........ ........ ........ */
681  /* .....111 11...... ........ ........ ........ */
682  dst[0] = buf[0] << 3 | buf[1] >> 2;
683 
684  /* .....111 11...... ........ ........ ........ */
685  /* ........ ..22222. ........ ........ ........ */
686  /* ........ .......3 3333.... ........ ........ */
687  dst[1] = buf[1] << 6 | buf[2] << 1 | buf[3] >> 4;
688 
689  /* ........ .......3 3333.... ........ ........ */
690  /* ........ ........ ....4444 4....... ........ */
691  dst[2] = buf[3] << 4 | buf[4] >> 1;
692 
693  /* ........ ........ ....4444 4....... ........ */
694  /* ........ ........ ........ .55555.. ........ */
695  /* ........ ........ ........ ......66 666..... */
696  dst[3] = buf[4] << 7 | buf[5] << 2 | buf[6] >> 3;
697 
698  /* ........ ........ ........ ......66 666..... */
699  /* ........ ........ ........ ........ ...77777 */
700  dst[4] = buf[6] << 5 | buf[7];
701 
702  dst += 5;
703  dst_sz -= 5;
704  }
705  /* Not ending on a eight byte boundary? */
706  if (i > 0 && i < 8) {
707 
708  /* Enough space available at the destination? */
709  if (dst_sz < (i + 1) / 2)
710  return -1;
711 
712  switch (i) {
713  case 7: /* ........ ........ ........ ......66 666..... */
714  /* ........ ........ ........ .55555.. ........ */
715  /* ........ ........ ....4444 4....... ........ */
716  dst[3] = buf[4] << 7 | buf[5] << 2 | buf[6] >> 3;
717  /* fallthrough */
718 
719  case 5: /* ........ ........ ....4444 4....... ........ */
720  /* ........ .......3 3333.... ........ ........ */
721  dst[2] = buf[3] << 4 | buf[4] >> 1;
722  /* fallthrough */
723 
724  case 4: /* ........ .......3 3333.... ........ ........ */
725  /* ........ ..22222. ........ ........ ........ */
726  /* .....111 11...... ........ ........ ........ */
727  dst[1] = buf[1] << 6 | buf[2] << 1 | buf[3] >> 4;
728  /* fallthrough */
729 
730  case 2: /* .....111 11...... ........ ........ ........ */
731  /* 00000... ........ ........ ........ ........ */
732  dst[0] = buf[0] << 3 | buf[1] >> 2;
733 
734  break;
735 
736  default:
737  return -1;
738  }
739  dst += (i + 1) / 2;
740 
741  if (check_padding) {
742  /* Check remaining padding characters */
743  if (ch != '=')
744  return -1;
745 
746  /* One down, 8 - i - 1 more to come... */
747  for (i = 8 - i - 1; i > 0; i--) {
748 
749  do {
750  if (src_sz == 0)
751  return -1;
752  ch = *src++;
753  src_sz--;
754 
755  } while (isspace((unsigned char)ch));
756 
757  if (ch != '=')
758  return -1;
759  }
760  }
761  }
762  return dst - start;
763 }
764 
765 int
766 ldns_b32_pton(const char* src, size_t src_sz, uint8_t* dst, size_t dst_sz)
767 {
768  return ldns_b32_pton_base(src, src_sz, dst, dst_sz, false, true);
769 }
770 
771 int
772 ldns_b32_pton_extended_hex(const char* src, size_t src_sz,
773  uint8_t* dst, size_t dst_sz)
774 {
775  return ldns_b32_pton_base(src, src_sz, dst, dst_sz, true, true);
776 }
777 
778 #ifndef HAVE_B32_PTON
779 
780 int
781 b32_pton(const char* src, size_t src_sz, uint8_t* dst, size_t dst_sz)
782 {
783  return ldns_b32_pton_base(src, src_sz, dst, dst_sz, false, true);
784 }
785 
786 int
787 b32_pton_extended_hex(const char* src, size_t src_sz,
788  uint8_t* dst, size_t dst_sz)
789 {
790  return ldns_b32_pton_base(src, src_sz, dst, dst_sz, true, true);
791 }
792 
793 #endif /* ! HAVE_B32_PTON */
794 
int ldns_b64_ntop(uint8_t const *src, size_t srclength, char *target, size_t targsize)
int ldns_b64_pton(char const *src, uint8_t *target, size_t targsize)
Defines ldns_rdf and functions to manipulate those.
Contains the definition of ldns_rr and functions to manipulate those.
A general purpose lookup table.
Definition: util.h:156
const char * name
Definition: util.h:158
int ldns_hexdigit_to_int(char ch)
Returns the int value of the given (hex) digit.
Definition: util.c:88
int ldns_b32_pton(const char *src, size_t src_sz, uint8_t *dst, size_t dst_sz)
Definition: util.c:766
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_extended_hex(const uint8_t *src, size_t src_sz, char *dst, size_t dst_sz)
Definition: util.c:606
int b32_ntop_extended_hex(const uint8_t *src, size_t src_sz, char *dst, size_t dst_sz)
Definition: util.c:621
int b32_ntop(const uint8_t *src, size_t src_sz, char *dst, size_t dst_sz)
Definition: util.c:615
int b32_pton_extended_hex(const char *src, size_t src_sz, uint8_t *dst, size_t dst_sz)
Definition: util.c:787
ldns_lookup_table * ldns_lookup_by_id(ldns_lookup_table *table, int id)
Definition: util.c:41
int ldns_b32_ntop(const uint8_t *src, size_t src_sz, char *dst, size_t dst_sz)
Definition: util.c:600
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
int b32_pton(const char *src, size_t src_sz, uint8_t *dst, size_t dst_sz)
Definition: util.c:781
int ldns_b32_pton_extended_hex(const char *src, size_t src_sz, uint8_t *dst, size_t dst_sz)
Definition: util.c:772
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
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
#define LDNS_MOD(x, y)
Definition: util.c:170
const char * ldns_version(void)
Show the internal library version.
Definition: util.c:160
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:412
char * ldns_bubblebabble(uint8_t *data, size_t len)
Encode data as BubbleBabble.
Definition: util.c:430
#define LDNS_DIV(x, y)
Definition: util.c:171
struct tm * ldns_serial_arithmitics_gmtime_r(int32_t time, time_t now, struct tm *result)
Definition: util.c:322
int ldns_init_random(FILE *fd, unsigned int size)
Init the random source applications should call this if they need entropy data within ldns If openSSL...
Definition: util.c:339
char ldns_int_to_hexdigit(int i)
Returns the char (hex) representation of the given int.
Definition: util.c:113
ldns_lookup_table * ldns_lookup_by_name(ldns_lookup_table *table, const char *name)
Definition: util.c:30
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
#define LDNS_FREE(ptr)
Definition: util.h:60
#define LDNS_VERSION
Definition: util.h:30
#define LDNS_XMALLOC(type, count)
Definition: util.h:51