2 * parseutil.c - parse utilities for string and wire conversion
4 * (c) NLnet Labs, 2004-2006
6 * See the file LICENSE for the license
11 * Utility functions for parsing, base32(DNS variant) and base64 encoding
12 * and decoding, Hex, Time units, Escape codes.
16 #include "sldns/parseutil.h"
22 sldns_lookup_by_name(sldns_lookup_table *table, const char *name)
24 while (table->name != NULL) {
25 if (strcasecmp(name, table->name) == 0)
33 sldns_lookup_by_id(sldns_lookup_table *table, int id)
35 while (table->name != NULL) {
43 /* Number of days per month (except for February in leap years). */
44 static const int mdays[] = {
45 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
48 #define LDNS_MOD(x,y) (((x) % (y) < 0) ? ((x) % (y) + (y)) : ((x) % (y)))
49 #define LDNS_DIV(x,y) (((x) % (y) < 0) ? ((x) / (y) - 1 ) : ((x) / (y)))
52 is_leap_year(int year)
54 return LDNS_MOD(year, 4) == 0 && (LDNS_MOD(year, 100) != 0
55 || LDNS_MOD(year, 400) == 0);
59 leap_days(int y1, int y2)
63 return (LDNS_DIV(y2, 4) - LDNS_DIV(y1, 4)) -
64 (LDNS_DIV(y2, 100) - LDNS_DIV(y1, 100)) +
65 (LDNS_DIV(y2, 400) - LDNS_DIV(y1, 400));
69 * Code adapted from Python 2.4.1 sources (Lib/calendar.py).
72 sldns_mktime_from_utc(const struct tm *tm)
74 int year = 1900 + tm->tm_year;
75 time_t days = 365 * ((time_t) year - 1970) + leap_days(1970, year);
81 for (i = 0; i < tm->tm_mon; ++i) {
84 if (tm->tm_mon > 1 && is_leap_year(year)) {
87 days += tm->tm_mday - 1;
89 hours = days * 24 + tm->tm_hour;
90 minutes = hours * 60 + tm->tm_min;
91 seconds = minutes * 60 + tm->tm_sec;
96 #if SIZEOF_TIME_T <= 4
99 sldns_year_and_yday_from_days_since_epoch(int64_t days, struct tm *result)
104 while (days < 0 || days >= (int64_t) (is_leap_year(year) ? 366 : 365)) {
105 new_year = year + (int) LDNS_DIV(days, 365);
106 days -= (new_year - year) * 365;
107 days -= leap_days(year, new_year);
110 result->tm_year = year;
111 result->tm_yday = (int) days;
114 /* Number of days per month in a leap year. */
115 static const int leap_year_mdays[] = {
116 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
120 sldns_mon_and_mday_from_year_and_yday(struct tm *result)
122 int idays = result->tm_yday;
123 const int *mon_lengths = is_leap_year(result->tm_year) ?
124 leap_year_mdays : mdays;
127 while (idays >= mon_lengths[result->tm_mon]) {
128 idays -= mon_lengths[result->tm_mon++];
130 result->tm_mday = idays + 1;
134 sldns_wday_from_year_and_yday(struct tm *result)
136 result->tm_wday = 4 /* 1-1-1970 was a thursday */
137 + LDNS_MOD((result->tm_year - 1970), 7) * LDNS_MOD(365, 7)
138 + leap_days(1970, result->tm_year)
140 result->tm_wday = LDNS_MOD(result->tm_wday, 7);
141 if (result->tm_wday < 0) {
142 result->tm_wday += 7;
147 sldns_gmtime64_r(int64_t clock, struct tm *result)
149 result->tm_isdst = 0;
150 result->tm_sec = (int) LDNS_MOD(clock, 60);
151 clock = LDNS_DIV(clock, 60);
152 result->tm_min = (int) LDNS_MOD(clock, 60);
153 clock = LDNS_DIV(clock, 60);
154 result->tm_hour = (int) LDNS_MOD(clock, 24);
155 clock = LDNS_DIV(clock, 24);
157 sldns_year_and_yday_from_days_since_epoch(clock, result);
158 sldns_mon_and_mday_from_year_and_yday(result);
159 sldns_wday_from_year_and_yday(result);
160 result->tm_year -= 1900;
165 #endif /* SIZEOF_TIME_T <= 4 */
168 sldns_serial_arithmitics_time(int32_t time, time_t now)
170 int32_t offset = time - (int32_t) now;
171 return (int64_t) now + offset;
175 sldns_serial_arithmitics_gmtime_r(int32_t time, time_t now, struct tm *result)
177 #if SIZEOF_TIME_T <= 4
178 int64_t secs_since_epoch = sldns_serial_arithmitics_time(time, now);
179 return sldns_gmtime64_r(secs_since_epoch, result);
181 time_t secs_since_epoch = sldns_serial_arithmitics_time(time, now);
182 return gmtime_r(&secs_since_epoch, result);
187 sldns_hexdigit_to_int(char ch)
200 case 'a': case 'A': return 10;
201 case 'b': case 'B': return 11;
202 case 'c': case 'C': return 12;
203 case 'd': case 'D': return 13;
204 case 'e': case 'E': return 14;
205 case 'f': case 'F': return 15;
212 sldns_str2period(const char *nptr, const char **endptr)
216 uint32_t seconds = 0;
218 for(*endptr = nptr; **endptr; (*endptr)++) {
249 seconds += i * 60 * 60;
254 seconds += i * 60 * 60 * 24;
259 seconds += i * 60 * 60 * 24 * 7;
273 i += (**endptr - '0');
277 /* disregard signedness */
282 /* disregard signedness */
287 sldns_parse_escape(uint8_t *ch_p, const char** str_p)
291 if ((*str_p)[0] && isdigit((unsigned char)(*str_p)[0]) &&
292 (*str_p)[1] && isdigit((unsigned char)(*str_p)[1]) &&
293 (*str_p)[2] && isdigit((unsigned char)(*str_p)[2])) {
295 val = (uint16_t)(((*str_p)[0] - '0') * 100 +
296 ((*str_p)[1] - '0') * 10 +
297 ((*str_p)[2] - '0'));
302 *ch_p = (uint8_t)val;
306 } else if ((*str_p)[0] && !isdigit((unsigned char)(*str_p)[0])) {
308 *ch_p = (uint8_t)*(*str_p)++;
313 return 0; /* LDNS_WIREPARSE_ERR_SYNTAX_BAD_ESCAPE */
316 /** parse one character, with escape codes */
318 sldns_parse_char(uint8_t *ch_p, const char** str_p)
324 case '\\': *str_p += 1;
325 return sldns_parse_escape(ch_p, str_p);
327 default: *ch_p = (uint8_t)*(*str_p)++;
332 size_t sldns_b32_ntop_calculate_size(size_t src_data_length)
334 return src_data_length == 0 ? 0 : ((src_data_length - 1) / 5 + 1) * 8;
337 size_t sldns_b32_ntop_calculate_size_no_padding(size_t src_data_length)
339 return ((src_data_length + 3) * 8 / 5) - 4;
343 sldns_b32_ntop_base(const uint8_t* src, size_t src_sz, char* dst, size_t dst_sz,
344 int extended_hex, int add_padding)
347 const char* b32 = extended_hex ? "0123456789abcdefghijklmnopqrstuv"
348 : "abcdefghijklmnopqrstuvwxyz234567";
350 size_t c = 0; /* c is used to carry partial base32 character over
351 * byte boundaries for sizes with a remainder.
352 * (i.e. src_sz % 5 != 0)
355 ret_sz = add_padding ? sldns_b32_ntop_calculate_size(src_sz)
356 : sldns_b32_ntop_calculate_size_no_padding(src_sz);
358 /* Do we have enough space? */
359 if (dst_sz < ret_sz + 1)
362 /* We know the size; terminate the string */
365 /* First process all chunks of five */
366 while (src_sz >= 5) {
367 /* 00000... ........ ........ ........ ........ */
368 dst[0] = b32[(src[0] ) >> 3];
370 /* .....111 11...... ........ ........ ........ */
371 dst[1] = b32[(src[0] & 0x07) << 2 | src[1] >> 6];
373 /* ........ ..22222. ........ ........ ........ */
374 dst[2] = b32[(src[1] & 0x3e) >> 1];
376 /* ........ .......3 3333.... ........ ........ */
377 dst[3] = b32[(src[1] & 0x01) << 4 | src[2] >> 4];
379 /* ........ ........ ....4444 4....... ........ */
380 dst[4] = b32[(src[2] & 0x0f) << 1 | src[3] >> 7];
382 /* ........ ........ ........ .55555.. ........ */
383 dst[5] = b32[(src[3] & 0x7c) >> 2];
385 /* ........ ........ ........ ......66 666..... */
386 dst[6] = b32[(src[3] & 0x03) << 3 | src[4] >> 5];
388 /* ........ ........ ........ ........ ...77777 */
389 dst[7] = b32[(src[4] & 0x1f) ];
395 /* Process what remains */
397 case 4: /* ........ ........ ........ ......66 666..... */
398 dst[6] = b32[(src[3] & 0x03) << 3];
400 /* ........ ........ ........ .55555.. ........ */
401 dst[5] = b32[(src[3] & 0x7c) >> 2];
403 /* ........ ........ ....4444 4....... ........ */
405 case 3: dst[4] = b32[(src[2] & 0x0f) << 1 | c];
407 /* ........ .......3 3333.... ........ ........ */
409 case 2: dst[3] = b32[(src[1] & 0x01) << 4 | c];
411 /* ........ ..22222. ........ ........ ........ */
412 dst[2] = b32[(src[1] & 0x3e) >> 1];
414 /* .....111 11...... ........ ........ ........ */
416 case 1: dst[1] = b32[(src[0] & 0x07) << 2 | c];
418 /* 00000... ........ ........ ........ ........ */
419 dst[0] = b32[ src[0] >> 3];
424 case 1: dst[2] = '=';
426 case 2: dst[4] = '=';
427 case 3: dst[5] = '=';
429 case 4: dst[7] = '=';
436 sldns_b32_ntop(const uint8_t* src, size_t src_sz, char* dst, size_t dst_sz)
438 return sldns_b32_ntop_base(src, src_sz, dst, dst_sz, 0, 1);
442 sldns_b32_ntop_extended_hex(const uint8_t* src, size_t src_sz,
443 char* dst, size_t dst_sz)
445 return sldns_b32_ntop_base(src, src_sz, dst, dst_sz, 1, 1);
448 size_t sldns_b32_pton_calculate_size(size_t src_text_length)
450 return src_text_length * 5 / 8;
454 sldns_b32_pton_base(const char* src, size_t src_sz, uint8_t* dst, size_t dst_sz,
455 int extended_hex, int check_padding)
460 uint8_t* start = dst;
463 /* Collect 8 characters in buf (if possible) */
464 for (i = 0; i < 8; i++) {
470 } while (isspace((unsigned char)ch) && src_sz > 0);
472 if (ch == '=' || ch == '\0')
475 else if (extended_hex)
477 if (ch >= '0' && ch <= '9')
478 buf[i] = (uint8_t)ch - '0';
479 else if (ch >= 'a' && ch <= 'v')
480 buf[i] = (uint8_t)ch - 'a' + 10;
481 else if (ch >= 'A' && ch <= 'V')
482 buf[i] = (uint8_t)ch - 'A' + 10;
486 else if (ch >= 'a' && ch <= 'z')
487 buf[i] = (uint8_t)ch - 'a';
488 else if (ch >= 'A' && ch <= 'Z')
489 buf[i] = (uint8_t)ch - 'A';
490 else if (ch >= '2' && ch <= '7')
491 buf[i] = (uint8_t)ch - '2' + 26;
495 /* Less that 8 characters. We're done. */
499 /* Enough space available at the destination? */
503 /* 00000... ........ ........ ........ ........ */
504 /* .....111 11...... ........ ........ ........ */
505 dst[0] = buf[0] << 3 | buf[1] >> 2;
507 /* .....111 11...... ........ ........ ........ */
508 /* ........ ..22222. ........ ........ ........ */
509 /* ........ .......3 3333.... ........ ........ */
510 dst[1] = buf[1] << 6 | buf[2] << 1 | buf[3] >> 4;
512 /* ........ .......3 3333.... ........ ........ */
513 /* ........ ........ ....4444 4....... ........ */
514 dst[2] = buf[3] << 4 | buf[4] >> 1;
516 /* ........ ........ ....4444 4....... ........ */
517 /* ........ ........ ........ .55555.. ........ */
518 /* ........ ........ ........ ......66 666..... */
519 dst[3] = buf[4] << 7 | buf[5] << 2 | buf[6] >> 3;
521 /* ........ ........ ........ ......66 666..... */
522 /* ........ ........ ........ ........ ...77777 */
523 dst[4] = buf[6] << 5 | buf[7];
528 /* Not ending on a eight byte boundary? */
529 if (i > 0 && i < 8) {
531 /* Enough space available at the destination? */
532 if (dst_sz < (i + 1) / 2)
536 case 7: /* ........ ........ ........ ......66 666..... */
537 /* ........ ........ ........ .55555.. ........ */
538 /* ........ ........ ....4444 4....... ........ */
539 dst[3] = buf[4] << 7 | buf[5] << 2 | buf[6] >> 3;
541 case 5: /* ........ ........ ....4444 4....... ........ */
542 /* ........ .......3 3333.... ........ ........ */
543 dst[2] = buf[3] << 4 | buf[4] >> 1;
545 case 4: /* ........ .......3 3333.... ........ ........ */
546 /* ........ ..22222. ........ ........ ........ */
547 /* .....111 11...... ........ ........ ........ */
548 dst[1] = buf[1] << 6 | buf[2] << 1 | buf[3] >> 4;
550 case 2: /* .....111 11...... ........ ........ ........ */
551 /* 00000... ........ ........ ........ ........ */
552 dst[0] = buf[0] << 3 | buf[1] >> 2;
562 /* Check remaining padding characters */
566 /* One down, 8 - i - 1 more to come... */
567 for (i = 8 - i - 1; i > 0; i--) {
575 } while (isspace((unsigned char)ch));
586 sldns_b32_pton(const char* src, size_t src_sz, uint8_t* dst, size_t dst_sz)
588 return sldns_b32_pton_base(src, src_sz, dst, dst_sz, 0, 1);
592 sldns_b32_pton_extended_hex(const char* src, size_t src_sz,
593 uint8_t* dst, size_t dst_sz)
595 return sldns_b32_pton_base(src, src_sz, dst, dst_sz, 1, 1);
598 size_t sldns_b64_ntop_calculate_size(size_t srcsize)
600 return ((((srcsize + 2) / 3) * 4) + 1);
603 /* RFC 1521, section 5.2.
605 * The encoding process represents 24-bit groups of input bits as output
606 * strings of 4 encoded characters. Proceeding from left to right, a
607 * 24-bit input group is formed by concatenating 3 8-bit input groups.
608 * These 24 bits are then treated as 4 concatenated 6-bit groups, each
609 * of which is translated into a single digit in the base64 alphabet.
611 * This routine does not insert spaces or linebreaks after 76 characters.
613 int sldns_b64_ntop(uint8_t const *src, size_t srclength,
614 char *target, size_t targsize)
617 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
618 const char pad64 = '=';
620 if(targsize < sldns_b64_ntop_calculate_size(srclength))
622 /* whole chunks: xxxxxxyy yyyyzzzz zzwwwwww */
623 while(i+3 <= srclength) {
624 if(o+4 > targsize) return -1;
625 target[o] = b64[src[i] >> 2];
626 target[o+1] = b64[ ((src[i]&0x03)<<4) | (src[i+1]>>4) ];
627 target[o+2] = b64[ ((src[i+1]&0x0f)<<2) | (src[i+2]>>6) ];
628 target[o+3] = b64[ (src[i+2]&0x3f) ];
633 switch(srclength - i) {
635 /* two at end, converted into A B C = */
636 target[o] = b64[src[i] >> 2];
637 target[o+1] = b64[ ((src[i]&0x03)<<4) | (src[i+1]>>4) ];
638 target[o+2] = b64[ ((src[i+1]&0x0f)<<2) ];
644 /* one at end, converted into A B = = */
645 target[o] = b64[src[i] >> 2];
646 target[o+1] = b64[ ((src[i]&0x03)<<4) ];
657 /* assert: i == srclength */
658 if(o+1 > targsize) return -1;
663 size_t sldns_b64_pton_calculate_size(size_t srcsize)
665 return (((((srcsize + 3) / 4) * 3)) + 1);
668 int sldns_b64_pton(char const *src, uint8_t *target, size_t targsize)
670 const uint8_t pad64 = 64; /* is 64th in the b64 array */
673 size_t o = 0, incount = 0;
676 /* skip any character that is not base64 */
677 /* conceptually we do:
678 const char* b64 = pad'=' is appended to array
679 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
680 const char* d = strchr(b64, *s++);
684 if(d <= 'Z' && d >= 'A')
686 else if(d <= 'z' && d >= 'a')
688 else if(d <= '9' && d >= '0')
697 in[incount++] = (uint8_t)d;
700 /* process whole block of 4 characters into 3 output bytes */
701 if(in[3] == pad64 && in[2] == pad64) { /* A B = = */
704 target[o] = (in[0]<<2) | ((in[1]&0x30)>>4);
706 break; /* we are done */
707 } else if(in[3] == pad64) { /* A B C = */
710 target[o] = (in[0]<<2) | ((in[1]&0x30)>>4);
711 target[o+1]= ((in[1]&0x0f)<<4) | ((in[2]&0x3c)>>2);
713 break; /* we are done */
717 /* write xxxxxxyy yyyyzzzz zzwwwwww */
718 target[o] = (in[0]<<2) | ((in[1]&0x30)>>4);
719 target[o+1]= ((in[1]&0x0f)<<4) | ((in[2]&0x3c)>>2);
720 target[o+2]= ((in[2]&0x03)<<6) | in[3];