4 * some general memory functions
6 * a Net::DNS like library for C
8 * (c) NLnet Labs, 2004-2006
10 * See the file LICENSE for the license
13 #include <ldns/config.h>
15 #include <ldns/rdata.h>
17 #include <ldns/util.h>
26 #include <openssl/rand.h>
30 ldns_lookup_by_name(ldns_lookup_table *table, const char *name)
32 while (table->name != NULL) {
33 if (strcasecmp(name, table->name) == 0)
41 ldns_lookup_by_id(ldns_lookup_table *table, int id)
43 while (table->name != NULL) {
52 ldns_get_bit(uint8_t bits[], size_t index)
55 * The bits are counted from left to right, so bit #0 is the
58 return (int) (bits[index / 8] & (1 << (7 - index % 8)));
62 ldns_get_bit_r(uint8_t bits[], size_t index)
65 * The bits are counted from right to left, so bit #0 is the
68 return (int) bits[index / 8] & (1 << (index % 8));
72 ldns_set_bit(uint8_t *byte, int bit_nr, bool value)
75 * The bits are counted from right to left, so bit #0 is the
78 if (bit_nr >= 0 && bit_nr < 8) {
80 *byte = *byte | (0x01 << bit_nr);
82 *byte = *byte & ~(0x01 << bit_nr);
88 ldns_hexdigit_to_int(char ch)
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;
113 ldns_int_to_hexdigit(int i)
138 ldns_hexstring_to_data(uint8_t *data, const char *str)
146 if (strlen(str) % 2 != 0) {
150 for (i = 0; i < strlen(str) / 2; i++) {
152 16 * (uint8_t) ldns_hexdigit_to_int(str[i*2]) +
153 (uint8_t) ldns_hexdigit_to_int(str[i*2 + 1]);
162 return (char*)LDNS_VERSION;
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
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)))
174 is_leap_year(int year)
176 return LDNS_MOD(year, 4) == 0 && (LDNS_MOD(year, 100) != 0
177 || LDNS_MOD(year, 400) == 0);
181 leap_days(int y1, int 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));
191 * Code adapted from Python 2.4.1 sources (Lib/calendar.py).
194 ldns_mktime_from_utc(const struct tm *tm)
196 int year = 1900 + tm->tm_year;
197 time_t days = 365 * ((time_t) year - 1970) + leap_days(1970, year);
203 for (i = 0; i < tm->tm_mon; ++i) {
206 if (tm->tm_mon > 1 && is_leap_year(year)) {
209 days += tm->tm_mday - 1;
211 hours = days * 24 + tm->tm_hour;
212 minutes = hours * 60 + tm->tm_min;
213 seconds = minutes * 60 + tm->tm_sec;
219 mktime_from_utc(const struct tm *tm)
221 return ldns_mktime_from_utc(tm);
224 #if SIZEOF_TIME_T <= 4
227 ldns_year_and_yday_from_days_since_epoch(int64_t days, struct tm *result)
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);
238 result->tm_year = year;
239 result->tm_yday = (int) days;
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
248 ldns_mon_and_mday_from_year_and_yday(struct tm *result)
250 int idays = result->tm_yday;
251 const int *mon_lengths = is_leap_year(result->tm_year) ?
252 leap_year_mdays : mdays;
255 while (idays >= mon_lengths[result->tm_mon]) {
256 idays -= mon_lengths[result->tm_mon++];
258 result->tm_mday = idays + 1;
262 ldns_wday_from_year_and_yday(struct tm *result)
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)
268 result->tm_wday = LDNS_MOD(result->tm_wday, 7);
269 if (result->tm_wday < 0) {
270 result->tm_wday += 7;
275 ldns_gmtime64_r(int64_t clock, struct tm *result)
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);
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;
293 #endif /* SIZEOF_TIME_T <= 4 */
296 ldns_serial_arithmetics_time(int32_t time, time_t now)
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;
304 ldns_serial_arithmetics_gmtime_r(int32_t time, time_t now, struct tm *result)
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);
310 time_t secs_since_epoch = ldns_serial_arithmetics_time(time, now);
311 return gmtime_r(&secs_since_epoch, result);
315 #ifdef ldns_serial_arithmitics_gmtime_r
316 #undef ldns_serial_arithmitics_gmtime_r
318 /* alias function because of previously used wrong spelling */
319 struct tm *ldns_serial_arithmitics_gmtime_r(int32_t, time_t, struct tm *);
321 ldns_serial_arithmitics_gmtime_r(int32_t time, time_t now, struct tm *result)
323 return ldns_serial_arithmetics_gmtime_r(time, now, result);
327 * Init the random source
328 * applications should call this if they need entropy data within ldns
329 * If openSSL is available, it is automatically seeded from /dev/urandom
332 * If you need more entropy, or have no openssl available, this function
333 * MUST be called at the start of the program
335 * If openssl *is* available, this function just adds more entropy
338 ldns_init_random(FILE *fd, unsigned int size)
340 /* if fp is given, seed srandom with data from file
341 otherwise use /dev/urandom */
348 /* we'll need at least sizeof(unsigned int) bytes for the
349 standard prng seed */
350 if (size < (unsigned int) sizeof(seed_i)){
351 size = (unsigned int) sizeof(seed_i);
354 seed = LDNS_XMALLOC(uint8_t, size);
360 if ((rand_f = fopen("/dev/urandom", "r")) == NULL) {
361 /* no readable /dev/urandom, try /dev/random */
362 if ((rand_f = fopen("/dev/random", "r")) == NULL) {
363 /* no readable /dev/random either, and no entropy
364 source given. we'll have to improvise */
365 for (read = 0; read < size; read++) {
366 gettimeofday(&tv, NULL);
367 seed[read] = (uint8_t) (tv.tv_usec % 256);
370 read = fread(seed, 1, size, rand_f);
373 read = fread(seed, 1, size, rand_f);
377 read = fread(seed, 1, size, rand_f);
382 if (!fd) fclose(rand_f);
386 /* Seed the OpenSSL prng (most systems have it seeded
387 automatically, in that case this call just adds entropy */
388 RAND_seed(seed, (int) size);
390 /* Seed the standard prng, only uses the first
391 * unsigned sizeof(unsigned int) bytes found in the entropy pool
393 memcpy(&seed_i, seed, sizeof(seed_i));
400 if (rand_f) fclose(rand_f);
411 ldns_get_random(void)
415 if (RAND_bytes((unsigned char*)&rid, 2) != 1) {
416 rid = (uint16_t) random();
419 rid = (uint16_t) random();
425 * BubbleBabble code taken from OpenSSH
426 * Copyright (c) 2001 Carsten Raskgaard. All rights reserved.
429 ldns_bubblebabble(uint8_t *data, size_t len)
431 char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
432 char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
433 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
434 size_t i, j = 0, rounds, seed = 1;
437 rounds = (len / 2) + 1;
438 retval = LDNS_XMALLOC(char, rounds * 6);
439 if(!retval) return NULL;
441 for (i = 0; i < rounds; i++) {
442 size_t idx0, idx1, idx2, idx3, idx4;
443 if ((i + 1 < rounds) || (len % 2 != 0)) {
444 idx0 = (((((size_t)(data[2 * i])) >> 6) & 3) +
446 idx1 = (((size_t)(data[2 * i])) >> 2) & 15;
447 idx2 = ((((size_t)(data[2 * i])) & 3) +
449 retval[j++] = vowels[idx0];
450 retval[j++] = consonants[idx1];
451 retval[j++] = vowels[idx2];
452 if ((i + 1) < rounds) {
453 idx3 = (((size_t)(data[(2 * i) + 1])) >> 4) & 15;
454 idx4 = (((size_t)(data[(2 * i) + 1]))) & 15;
455 retval[j++] = consonants[idx3];
457 retval[j++] = consonants[idx4];
459 ((((size_t)(data[2 * i])) * 7) +
460 ((size_t)(data[(2 * i) + 1])))) % 36;
466 retval[j++] = vowels[idx0];
467 retval[j++] = consonants[idx1];
468 retval[j++] = vowels[idx2];
477 * For backwards compatibility, because we have always exported this symbol.
480 int ldns_b64_ntop(const uint8_t* src, size_t srclength,
481 char *target, size_t targsize);
483 return b64_ntop(src, srclength, target, targsize);
488 * For backwards compatibility, because we have always exported this symbol.
491 int ldns_b64_pton(const char* src, uint8_t *target, size_t targsize)
493 return b64_pton(src, target, targsize);
499 ldns_b32_ntop_base(const uint8_t* src, size_t src_sz,
500 char* dst, size_t dst_sz,
501 bool extended_hex, bool add_padding)
504 const char* b32 = extended_hex ? "0123456789abcdefghijklmnopqrstuv"
505 : "abcdefghijklmnopqrstuvwxyz234567";
507 size_t c = 0; /* c is used to carry partial base32 character over
508 * byte boundaries for sizes with a remainder.
509 * (i.e. src_sz % 5 != 0)
512 ret_sz = add_padding ? ldns_b32_ntop_calculate_size(src_sz)
513 : ldns_b32_ntop_calculate_size_no_padding(src_sz);
515 /* Do we have enough space? */
516 if (dst_sz < ret_sz + 1)
519 /* We know the size; terminate the string */
522 /* First process all chunks of five */
523 while (src_sz >= 5) {
524 /* 00000... ........ ........ ........ ........ */
525 dst[0] = b32[(src[0] ) >> 3];
527 /* .....111 11...... ........ ........ ........ */
528 dst[1] = b32[(src[0] & 0x07) << 2 | src[1] >> 6];
530 /* ........ ..22222. ........ ........ ........ */
531 dst[2] = b32[(src[1] & 0x3e) >> 1];
533 /* ........ .......3 3333.... ........ ........ */
534 dst[3] = b32[(src[1] & 0x01) << 4 | src[2] >> 4];
536 /* ........ ........ ....4444 4....... ........ */
537 dst[4] = b32[(src[2] & 0x0f) << 1 | src[3] >> 7];
539 /* ........ ........ ........ .55555.. ........ */
540 dst[5] = b32[(src[3] & 0x7c) >> 2];
542 /* ........ ........ ........ ......66 666..... */
543 dst[6] = b32[(src[3] & 0x03) << 3 | src[4] >> 5];
545 /* ........ ........ ........ ........ ...77777 */
546 dst[7] = b32[(src[4] & 0x1f) ];
552 /* Process what remains */
554 case 4: /* ........ ........ ........ ......66 666..... */
555 dst[6] = b32[(src[3] & 0x03) << 3];
557 /* ........ ........ ........ .55555.. ........ */
558 dst[5] = b32[(src[3] & 0x7c) >> 2];
560 /* ........ ........ ....4444 4....... ........ */
563 case 3: dst[4] = b32[(src[2] & 0x0f) << 1 | c];
565 /* ........ .......3 3333.... ........ ........ */
568 case 2: dst[3] = b32[(src[1] & 0x01) << 4 | c];
570 /* ........ ..22222. ........ ........ ........ */
571 dst[2] = b32[(src[1] & 0x3e) >> 1];
573 /* .....111 11...... ........ ........ ........ */
576 case 1: dst[1] = b32[(src[0] & 0x07) << 2 | c];
578 /* 00000... ........ ........ ........ ........ */
579 dst[0] = b32[ src[0] >> 3];
584 case 1: dst[2] = '=';
587 case 2: dst[4] = '=';
589 case 3: dst[5] = '=';
592 case 4: dst[7] = '=';
599 ldns_b32_ntop(const uint8_t* src, size_t src_sz, char* dst, size_t dst_sz)
601 return ldns_b32_ntop_base(src, src_sz, dst, dst_sz, false, true);
605 ldns_b32_ntop_extended_hex(const uint8_t* src, size_t src_sz,
606 char* dst, size_t dst_sz)
608 return ldns_b32_ntop_base(src, src_sz, dst, dst_sz, true, true);
611 #ifndef HAVE_B32_NTOP
614 b32_ntop(const uint8_t* src, size_t src_sz, char* dst, size_t dst_sz)
616 return ldns_b32_ntop_base(src, src_sz, dst, dst_sz, false, true);
620 b32_ntop_extended_hex(const uint8_t* src, size_t src_sz,
621 char* dst, size_t dst_sz)
623 return ldns_b32_ntop_base(src, src_sz, dst, dst_sz, true, true);
626 #endif /* ! HAVE_B32_NTOP */
629 ldns_b32_pton_base(const char* src, size_t src_sz,
630 uint8_t* dst, size_t dst_sz,
631 bool extended_hex, bool check_padding)
636 uint8_t* start = dst;
639 /* Collect 8 characters in buf (if possible) */
640 for (i = 0; i < 8; i++) {
646 } while (isspace((unsigned char)ch) && src_sz > 0);
648 if (ch == '=' || ch == '\0')
651 else if (extended_hex)
653 if (ch >= '0' && ch <= '9')
654 buf[i] = (uint8_t)ch - '0';
655 else if (ch >= 'a' && ch <= 'v')
656 buf[i] = (uint8_t)ch - 'a' + 10;
657 else if (ch >= 'A' && ch <= 'V')
658 buf[i] = (uint8_t)ch - 'A' + 10;
662 else if (ch >= 'a' && ch <= 'z')
663 buf[i] = (uint8_t)ch - 'a';
664 else if (ch >= 'A' && ch <= 'Z')
665 buf[i] = (uint8_t)ch - 'A';
666 else if (ch >= '2' && ch <= '7')
667 buf[i] = (uint8_t)ch - '2' + 26;
671 /* Less that 8 characters. We're done. */
675 /* Enough space available at the destination? */
679 /* 00000... ........ ........ ........ ........ */
680 /* .....111 11...... ........ ........ ........ */
681 dst[0] = buf[0] << 3 | buf[1] >> 2;
683 /* .....111 11...... ........ ........ ........ */
684 /* ........ ..22222. ........ ........ ........ */
685 /* ........ .......3 3333.... ........ ........ */
686 dst[1] = buf[1] << 6 | buf[2] << 1 | buf[3] >> 4;
688 /* ........ .......3 3333.... ........ ........ */
689 /* ........ ........ ....4444 4....... ........ */
690 dst[2] = buf[3] << 4 | buf[4] >> 1;
692 /* ........ ........ ....4444 4....... ........ */
693 /* ........ ........ ........ .55555.. ........ */
694 /* ........ ........ ........ ......66 666..... */
695 dst[3] = buf[4] << 7 | buf[5] << 2 | buf[6] >> 3;
697 /* ........ ........ ........ ......66 666..... */
698 /* ........ ........ ........ ........ ...77777 */
699 dst[4] = buf[6] << 5 | buf[7];
704 /* Not ending on a eight byte boundary? */
705 if (i > 0 && i < 8) {
707 /* Enough space available at the destination? */
708 if (dst_sz < (i + 1) / 2)
712 case 7: /* ........ ........ ........ ......66 666..... */
713 /* ........ ........ ........ .55555.. ........ */
714 /* ........ ........ ....4444 4....... ........ */
715 dst[3] = buf[4] << 7 | buf[5] << 2 | buf[6] >> 3;
718 case 5: /* ........ ........ ....4444 4....... ........ */
719 /* ........ .......3 3333.... ........ ........ */
720 dst[2] = buf[3] << 4 | buf[4] >> 1;
723 case 4: /* ........ .......3 3333.... ........ ........ */
724 /* ........ ..22222. ........ ........ ........ */
725 /* .....111 11...... ........ ........ ........ */
726 dst[1] = buf[1] << 6 | buf[2] << 1 | buf[3] >> 4;
729 case 2: /* .....111 11...... ........ ........ ........ */
730 /* 00000... ........ ........ ........ ........ */
731 dst[0] = buf[0] << 3 | buf[1] >> 2;
741 /* Check remaining padding characters */
745 /* One down, 8 - i - 1 more to come... */
746 for (i = 8 - i - 1; i > 0; i--) {
754 } while (isspace((unsigned char)ch));
765 ldns_b32_pton(const char* src, size_t src_sz, uint8_t* dst, size_t dst_sz)
767 return ldns_b32_pton_base(src, src_sz, dst, dst_sz, false, true);
771 ldns_b32_pton_extended_hex(const char* src, size_t src_sz,
772 uint8_t* dst, size_t dst_sz)
774 return ldns_b32_pton_base(src, src_sz, dst, dst_sz, true, true);
777 #ifndef HAVE_B32_PTON
780 b32_pton(const char* src, size_t src_sz, uint8_t* dst, size_t dst_sz)
782 return ldns_b32_pton_base(src, src_sz, dst, dst_sz, false, true);
786 b32_pton_extended_hex(const char* src, size_t src_sz,
787 uint8_t* dst, size_t dst_sz)
789 return ldns_b32_pton_base(src, src_sz, dst, dst_sz, true, true);
792 #endif /* ! HAVE_B32_PTON */