Changeset View
Changeset View
Standalone View
Standalone View
contrib/ldns/util.c
Show First 20 Lines • Show All 287 Lines • ▼ Show 20 Lines | ldns_gmtime64_r(int64_t clock, struct tm *result) | ||||
result->tm_year -= 1900; | result->tm_year -= 1900; | ||||
return result; | return result; | ||||
} | } | ||||
#endif /* SIZEOF_TIME_T <= 4 */ | #endif /* SIZEOF_TIME_T <= 4 */ | ||||
static int64_t | static int64_t | ||||
ldns_serial_arithmitics_time(int32_t time, time_t now) | ldns_serial_arithmetics_time(int32_t time, time_t now) | ||||
{ | { | ||||
int32_t offset = time - (int32_t) now; | /* Casting due to https://github.com/NLnetLabs/ldns/issues/71 */ | ||||
int32_t offset = (int32_t) ((uint32_t) time - (uint32_t) now); | |||||
return (int64_t) now + offset; | return (int64_t) now + offset; | ||||
} | } | ||||
struct tm * | struct tm * | ||||
ldns_serial_arithmitics_gmtime_r(int32_t time, time_t now, struct tm *result) | ldns_serial_arithmetics_gmtime_r(int32_t time, time_t now, struct tm *result) | ||||
{ | { | ||||
#if SIZEOF_TIME_T <= 4 | #if SIZEOF_TIME_T <= 4 | ||||
int64_t secs_since_epoch = ldns_serial_arithmitics_time(time, now); | int64_t secs_since_epoch = ldns_serial_arithmetics_time(time, now); | ||||
return ldns_gmtime64_r(secs_since_epoch, result); | return ldns_gmtime64_r(secs_since_epoch, result); | ||||
#else | #else | ||||
time_t secs_since_epoch = ldns_serial_arithmitics_time(time, now); | time_t secs_since_epoch = ldns_serial_arithmetics_time(time, now); | ||||
return gmtime_r(&secs_since_epoch, result); | return gmtime_r(&secs_since_epoch, result); | ||||
#endif | #endif | ||||
} | } | ||||
#ifdef ldns_serial_arithmitics_gmtime_r | |||||
#undef ldns_serial_arithmitics_gmtime_r | |||||
#endif | |||||
/* alias function because of previously used wrong spelling */ | |||||
struct tm *ldns_serial_arithmitics_gmtime_r(int32_t, time_t, struct tm *); | |||||
struct tm * | |||||
ldns_serial_arithmitics_gmtime_r(int32_t time, time_t now, struct tm *result) | |||||
{ | |||||
return ldns_serial_arithmetics_gmtime_r(time, now, result); | |||||
} | |||||
/** | /** | ||||
* Init the random source | * Init the random source | ||||
* applications should call this if they need entropy data within ldns | * applications should call this if they need entropy data within ldns | ||||
* If openSSL is available, it is automatically seeded from /dev/urandom | * If openSSL is available, it is automatically seeded from /dev/urandom | ||||
* or /dev/random | * or /dev/random | ||||
* | * | ||||
* If you need more entropy, or have no openssl available, this function | * If you need more entropy, or have no openssl available, this function | ||||
* MUST be called at the start of the program | * MUST be called at the start of the program | ||||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | if (read < size) { | ||||
return 1; | return 1; | ||||
} else { | } else { | ||||
#ifdef HAVE_SSL | #ifdef HAVE_SSL | ||||
/* Seed the OpenSSL prng (most systems have it seeded | /* Seed the OpenSSL prng (most systems have it seeded | ||||
automatically, in that case this call just adds entropy */ | automatically, in that case this call just adds entropy */ | ||||
RAND_seed(seed, (int) size); | RAND_seed(seed, (int) size); | ||||
#else | #else | ||||
/* Seed the standard prng, only uses the first | /* Seed the standard prng, only uses the first | ||||
* unsigned sizeof(unsiged int) bytes found in the entropy pool | * unsigned sizeof(unsigned int) bytes found in the entropy pool | ||||
*/ | */ | ||||
memcpy(&seed_i, seed, sizeof(seed_i)); | memcpy(&seed_i, seed, sizeof(seed_i)); | ||||
srandom(seed_i); | srandom(seed_i); | ||||
#endif | #endif | ||||
LDNS_FREE(seed); | LDNS_FREE(seed); | ||||
} | } | ||||
if (!fd) { | if (!fd) { | ||||
▲ Show 20 Lines • Show All 154 Lines • ▼ Show 20 Lines | ldns_b32_ntop_base(const uint8_t* src, size_t src_sz, | ||||
case 4: /* ........ ........ ........ ......66 666..... */ | case 4: /* ........ ........ ........ ......66 666..... */ | ||||
dst[6] = b32[(src[3] & 0x03) << 3]; | dst[6] = b32[(src[3] & 0x03) << 3]; | ||||
/* ........ ........ ........ .55555.. ........ */ | /* ........ ........ ........ .55555.. ........ */ | ||||
dst[5] = b32[(src[3] & 0x7c) >> 2]; | dst[5] = b32[(src[3] & 0x7c) >> 2]; | ||||
/* ........ ........ ....4444 4....... ........ */ | /* ........ ........ ....4444 4....... ........ */ | ||||
c = src[3] >> 7 ; | c = src[3] >> 7 ; | ||||
/* fallthrough */ | |||||
case 3: dst[4] = b32[(src[2] & 0x0f) << 1 | c]; | case 3: dst[4] = b32[(src[2] & 0x0f) << 1 | c]; | ||||
/* ........ .......3 3333.... ........ ........ */ | /* ........ .......3 3333.... ........ ........ */ | ||||
c = src[2] >> 4 ; | c = src[2] >> 4 ; | ||||
/* fallthrough */ | |||||
case 2: dst[3] = b32[(src[1] & 0x01) << 4 | c]; | case 2: dst[3] = b32[(src[1] & 0x01) << 4 | c]; | ||||
/* ........ ..22222. ........ ........ ........ */ | /* ........ ..22222. ........ ........ ........ */ | ||||
dst[2] = b32[(src[1] & 0x3e) >> 1]; | dst[2] = b32[(src[1] & 0x3e) >> 1]; | ||||
/* .....111 11...... ........ ........ ........ */ | /* .....111 11...... ........ ........ ........ */ | ||||
c = src[1] >> 6 ; | c = src[1] >> 6 ; | ||||
/* fallthrough */ | |||||
case 1: dst[1] = b32[(src[0] & 0x07) << 2 | c]; | case 1: dst[1] = b32[(src[0] & 0x07) << 2 | c]; | ||||
/* 00000... ........ ........ ........ ........ */ | /* 00000... ........ ........ ........ ........ */ | ||||
dst[0] = b32[ src[0] >> 3]; | dst[0] = b32[ src[0] >> 3]; | ||||
} | } | ||||
/* Add padding */ | /* Add padding */ | ||||
if (add_padding) { | if (add_padding) { | ||||
switch (src_sz) { | switch (src_sz) { | ||||
case 1: dst[2] = '='; | case 1: dst[2] = '='; | ||||
dst[3] = '='; | dst[3] = '='; | ||||
/* fallthrough */ | |||||
case 2: dst[4] = '='; | case 2: dst[4] = '='; | ||||
/* fallthrough */ | |||||
case 3: dst[5] = '='; | case 3: dst[5] = '='; | ||||
dst[6] = '='; | dst[6] = '='; | ||||
/* fallthrough */ | |||||
case 4: dst[7] = '='; | case 4: dst[7] = '='; | ||||
} | } | ||||
} | } | ||||
return (int)ret_sz; | return (int)ret_sz; | ||||
} | } | ||||
int | int | ||||
ldns_b32_ntop(const uint8_t* src, size_t src_sz, char* dst, size_t dst_sz) | ldns_b32_ntop(const uint8_t* src, size_t src_sz, char* dst, size_t dst_sz) | ||||
▲ Show 20 Lines • Show All 108 Lines • ▼ Show 20 Lines | if (i > 0 && i < 8) { | ||||
if (dst_sz < (i + 1) / 2) | if (dst_sz < (i + 1) / 2) | ||||
return -1; | return -1; | ||||
switch (i) { | switch (i) { | ||||
case 7: /* ........ ........ ........ ......66 666..... */ | case 7: /* ........ ........ ........ ......66 666..... */ | ||||
/* ........ ........ ........ .55555.. ........ */ | /* ........ ........ ........ .55555.. ........ */ | ||||
/* ........ ........ ....4444 4....... ........ */ | /* ........ ........ ....4444 4....... ........ */ | ||||
dst[3] = buf[4] << 7 | buf[5] << 2 | buf[6] >> 3; | dst[3] = buf[4] << 7 | buf[5] << 2 | buf[6] >> 3; | ||||
/* fallthrough */ | |||||
case 5: /* ........ ........ ....4444 4....... ........ */ | case 5: /* ........ ........ ....4444 4....... ........ */ | ||||
/* ........ .......3 3333.... ........ ........ */ | /* ........ .......3 3333.... ........ ........ */ | ||||
dst[2] = buf[3] << 4 | buf[4] >> 1; | dst[2] = buf[3] << 4 | buf[4] >> 1; | ||||
/* fallthrough */ | |||||
case 4: /* ........ .......3 3333.... ........ ........ */ | case 4: /* ........ .......3 3333.... ........ ........ */ | ||||
/* ........ ..22222. ........ ........ ........ */ | /* ........ ..22222. ........ ........ ........ */ | ||||
/* .....111 11...... ........ ........ ........ */ | /* .....111 11...... ........ ........ ........ */ | ||||
dst[1] = buf[1] << 6 | buf[2] << 1 | buf[3] >> 4; | dst[1] = buf[1] << 6 | buf[2] << 1 | buf[3] >> 4; | ||||
/* fallthrough */ | |||||
case 2: /* .....111 11...... ........ ........ ........ */ | case 2: /* .....111 11...... ........ ........ ........ */ | ||||
/* 00000... ........ ........ ........ ........ */ | /* 00000... ........ ........ ........ ........ */ | ||||
dst[0] = buf[0] << 3 | buf[1] >> 2; | dst[0] = buf[0] << 3 | buf[1] >> 2; | ||||
break; | break; | ||||
default: | default: | ||||
▲ Show 20 Lines • Show All 58 Lines • Show Last 20 Lines |