26 #include <openssl/rand.h>
32 while (table->
name != NULL) {
33 if (strcasecmp(name, table->
name) == 0)
43 while (table->
name != NULL) {
58 return (
int) (bits[index / 8] & (1 << (7 - index % 8)));
68 return (
int) bits[index / 8] & (1 << (index % 8));
78 if (bit_nr >= 0 && bit_nr < 8) {
80 *
byte = *
byte | (0x01 << bit_nr);
82 *
byte = *
byte & ~(0x01 << bit_nr);
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;
146 if (strlen(str) % 2 != 0) {
150 for (i = 0; i < strlen(str) / 2; i++) {
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)
181 leap_days(
int y1,
int y2)
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;
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;
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)
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);
280 result->tm_min = (int)
LDNS_MOD(clock, 60);
282 result->tm_hour = (int)
LDNS_MOD(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;
296 ldns_serial_arithmetics_time(int32_t time, time_t now)
299 int32_t offset = (int32_t) ((uint32_t) time - (uint32_t) now);
300 return (int64_t) now + offset;
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
351 if (size < (
unsigned int)
sizeof(seed_i)){
352 size = (
unsigned int)
sizeof(seed_i);
361 if ((rand_f = fopen(
"/dev/urandom",
"r")) == NULL) {
363 if ((rand_f = fopen(
"/dev/random",
"r")) == NULL) {
366 for (read = 0; read < size; read++) {
367 gettimeofday(&tv, NULL);
368 seed[read] = (uint8_t) (tv.tv_usec % 256);
371 read = fread(seed, 1, size, rand_f);
374 read = fread(seed, 1, size, rand_f);
378 read = fread(seed, 1, size, rand_f);
383 if (!fd) fclose(rand_f);
389 RAND_seed(seed, (
int) size);
394 memcpy(&seed_i, seed,
sizeof(seed_i));
401 if (rand_f) fclose(rand_f);
416 if (RAND_bytes((
unsigned char*)&rid, 2) != 1) {
417 rid = (uint16_t) random();
420 rid = (uint16_t) random();
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;
438 rounds = (len / 2) + 1;
440 if(!retval)
return NULL;
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) +
447 idx1 = (((size_t)(data[2 * i])) >> 2) & 15;
448 idx2 = ((((size_t)(data[2 * i])) & 3) +
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];
458 retval[j++] = consonants[idx4];
460 ((((
size_t)(data[2 * i])) * 7) +
461 ((
size_t)(data[(2 * i) + 1])))) % 36;
467 retval[j++] = vowels[idx0];
468 retval[j++] = consonants[idx1];
469 retval[j++] = vowels[idx2];
482 char *target,
size_t targsize);
484 return b64_ntop(src, srclength, target, targsize);
492 int ldns_b64_pton(
const char* src, uint8_t *target,
size_t targsize)
494 return b64_pton(src, target, targsize);
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)
505 const char* b32 = extended_hex ?
"0123456789abcdefghijklmnopqrstuv"
506 :
"abcdefghijklmnopqrstuvwxyz234567";
513 ret_sz = add_padding ? ldns_b32_ntop_calculate_size(src_sz)
514 : ldns_b32_ntop_calculate_size_no_padding(src_sz);
517 if (dst_sz < ret_sz + 1)
524 while (src_sz >= 5) {
526 dst[0] = b32[(src[0] ) >> 3];
529 dst[1] = b32[(src[0] & 0x07) << 2 | src[1] >> 6];
532 dst[2] = b32[(src[1] & 0x3e) >> 1];
535 dst[3] = b32[(src[1] & 0x01) << 4 | src[2] >> 4];
538 dst[4] = b32[(src[2] & 0x0f) << 1 | src[3] >> 7];
541 dst[5] = b32[(src[3] & 0x7c) >> 2];
544 dst[6] = b32[(src[3] & 0x03) << 3 | src[4] >> 5];
547 dst[7] = b32[(src[4] & 0x1f) ];
556 dst[6] = b32[(src[3] & 0x03) << 3];
559 dst[5] = b32[(src[3] & 0x7c) >> 2];
564 case 3: dst[4] = b32[(src[2] & 0x0f) << 1 | c];
569 case 2: dst[3] = b32[(src[1] & 0x01) << 4 | c];
572 dst[2] = b32[(src[1] & 0x3e) >> 1];
577 case 1: dst[1] = b32[(src[0] & 0x07) << 2 | c];
580 dst[0] = b32[ src[0] >> 3];
585 case 1: dst[2] =
'=';
588 case 2: dst[4] =
'=';
590 case 3: dst[5] =
'=';
593 case 4: dst[7] =
'=';
602 return ldns_b32_ntop_base(src, src_sz, dst, dst_sz,
false,
true);
607 char* dst,
size_t dst_sz)
609 return ldns_b32_ntop_base(src, src_sz, dst, dst_sz,
true,
true);
612 #ifndef HAVE_B32_NTOP
615 b32_ntop(
const uint8_t* src,
size_t src_sz,
char* dst,
size_t dst_sz)
617 return ldns_b32_ntop_base(src, src_sz, dst, dst_sz,
false,
true);
622 char* dst,
size_t dst_sz)
624 return ldns_b32_ntop_base(src, src_sz, dst, dst_sz,
true,
true);
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)
637 uint8_t* start = dst;
641 for (i = 0; i < 8; i++) {
647 }
while (isspace((
unsigned char)ch) && src_sz > 0);
649 if (ch ==
'=' || ch ==
'\0')
652 else if (extended_hex)
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;
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;
682 dst[0] = buf[0] << 3 | buf[1] >> 2;
687 dst[1] = buf[1] << 6 | buf[2] << 1 | buf[3] >> 4;
691 dst[2] = buf[3] << 4 | buf[4] >> 1;
696 dst[3] = buf[4] << 7 | buf[5] << 2 | buf[6] >> 3;
700 dst[4] = buf[6] << 5 | buf[7];
706 if (i > 0 && i < 8) {
709 if (dst_sz < (i + 1) / 2)
716 dst[3] = buf[4] << 7 | buf[5] << 2 | buf[6] >> 3;
721 dst[2] = buf[3] << 4 | buf[4] >> 1;
727 dst[1] = buf[1] << 6 | buf[2] << 1 | buf[3] >> 4;
732 dst[0] = buf[0] << 3 | buf[1] >> 2;
747 for (i = 8 - i - 1; i > 0; i--) {
755 }
while (isspace((
unsigned char)ch));
768 return ldns_b32_pton_base(src, src_sz, dst, dst_sz,
false,
true);
773 uint8_t* dst,
size_t dst_sz)
775 return ldns_b32_pton_base(src, src_sz, dst, dst_sz,
true,
true);
778 #ifndef HAVE_B32_PTON
781 b32_pton(
const char* src,
size_t src_sz, uint8_t* dst,
size_t dst_sz)
783 return ldns_b32_pton_base(src, src_sz, dst, dst_sz,
false,
true);
788 uint8_t* dst,
size_t dst_sz)
790 return ldns_b32_pton_base(src, src_sz, dst, dst_sz,
true,
true);
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.
int ldns_hexdigit_to_int(char ch)
Returns the int value of the given (hex) digit.
int ldns_b32_pton(const char *src, size_t src_sz, uint8_t *dst, size_t dst_sz)
int ldns_hexstring_to_data(uint8_t *data, const char *str)
Converts a hex string to binary data.
time_t ldns_mktime_from_utc(const struct tm *tm)
Convert TM to seconds since epoch (midnight, January 1st, 1970).
int ldns_b32_ntop_extended_hex(const uint8_t *src, size_t src_sz, char *dst, size_t dst_sz)
int b32_ntop_extended_hex(const uint8_t *src, size_t src_sz, char *dst, size_t dst_sz)
int b32_ntop(const uint8_t *src, size_t src_sz, char *dst, size_t dst_sz)
int b32_pton_extended_hex(const char *src, size_t src_sz, uint8_t *dst, size_t dst_sz)
ldns_lookup_table * ldns_lookup_by_id(ldns_lookup_table *table, int id)
int ldns_b32_ntop(const uint8_t *src, size_t src_sz, char *dst, size_t dst_sz)
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...
int b32_pton(const char *src, size_t src_sz, uint8_t *dst, size_t dst_sz)
int ldns_b32_pton_extended_hex(const char *src, size_t src_sz, uint8_t *dst, size_t dst_sz)
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...
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...
const char * ldns_version(void)
Show the internal library version.
time_t mktime_from_utc(const struct tm *tm)
uint16_t ldns_get_random(void)
Get random number.
char * ldns_bubblebabble(uint8_t *data, size_t len)
Encode data as BubbleBabble.
struct tm * ldns_serial_arithmitics_gmtime_r(int32_t time, time_t now, struct tm *result)
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...
char ldns_int_to_hexdigit(int i)
Returns the char (hex) representation of the given int.
ldns_lookup_table * ldns_lookup_by_name(ldns_lookup_table *table, const char *name)
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...
#define LDNS_XMALLOC(type, count)