13b6c3722Schristos /* 23b6c3722Schristos * parseutil.h - parse utilities for string and wire conversion 33b6c3722Schristos * 43b6c3722Schristos * (c) NLnet Labs, 2004 53b6c3722Schristos * 63b6c3722Schristos * See the file LICENSE for the license 73b6c3722Schristos */ 83b6c3722Schristos /** 93b6c3722Schristos * \file 103b6c3722Schristos * 113b6c3722Schristos * Utility functions for parsing, base32(DNS variant) and base64 encoding 123b6c3722Schristos * and decoding, Hex, Time units, Escape codes. 133b6c3722Schristos */ 143b6c3722Schristos 153b6c3722Schristos #ifndef LDNS_PARSEUTIL_H 163b6c3722Schristos #define LDNS_PARSEUTIL_H 173b6c3722Schristos struct tm; 183b6c3722Schristos 193b6c3722Schristos /** 203b6c3722Schristos * A general purpose lookup table 213b6c3722Schristos * 223b6c3722Schristos * Lookup tables are arrays of (id, name) pairs, 233b6c3722Schristos * So you can for instance lookup the RCODE 3, which is "NXDOMAIN", 243b6c3722Schristos * and vice versa. The lookup tables themselves are defined wherever needed, 253b6c3722Schristos * for instance in host2str.c 263b6c3722Schristos */ 273b6c3722Schristos struct sldns_struct_lookup_table { 283b6c3722Schristos int id; 293b6c3722Schristos const char *name; 303b6c3722Schristos }; 313b6c3722Schristos typedef struct sldns_struct_lookup_table sldns_lookup_table; 323b6c3722Schristos 333b6c3722Schristos /** 343b6c3722Schristos * Looks up the table entry by name, returns NULL if not found. 353b6c3722Schristos * \param[in] table the lookup table to search in 363b6c3722Schristos * \param[in] name what to search for 373b6c3722Schristos * \return the item found 383b6c3722Schristos */ 393b6c3722Schristos sldns_lookup_table *sldns_lookup_by_name(sldns_lookup_table table[], 403b6c3722Schristos const char *name); 413b6c3722Schristos /** 423b6c3722Schristos * Looks up the table entry by id, returns NULL if not found. 433b6c3722Schristos * \param[in] table the lookup table to search in 443b6c3722Schristos * \param[in] id what to search for 453b6c3722Schristos * \return the item found 463b6c3722Schristos */ 473b6c3722Schristos sldns_lookup_table *sldns_lookup_by_id(sldns_lookup_table table[], int id); 483b6c3722Schristos 493b6c3722Schristos /** 503b6c3722Schristos * Convert TM to seconds since epoch (midnight, January 1st, 1970). 513b6c3722Schristos * Like timegm(3), which is not always available. 523b6c3722Schristos * \param[in] tm a struct tm* with the date 533b6c3722Schristos * \return the seconds since epoch 543b6c3722Schristos */ 553b6c3722Schristos time_t sldns_mktime_from_utc(const struct tm *tm); 563b6c3722Schristos 573b6c3722Schristos /** 583b6c3722Schristos * The function interprets time as the number of seconds since epoch 593b6c3722Schristos * with respect to now using serial arithmetics (rfc1982). 603b6c3722Schristos * That number of seconds is then converted to broken-out time information. 61*7a540f2bSchristos * This is especially useful when converting the inception and expiration 623b6c3722Schristos * fields of RRSIG records. 633b6c3722Schristos * 643b6c3722Schristos * \param[in] time number of seconds since epoch (midnight, January 1st, 1970) 650cd9f4ecSchristos * to be interpreted as a serial arithmetics number relative to now. 663b6c3722Schristos * \param[in] now number of seconds since epoch (midnight, January 1st, 1970) 673b6c3722Schristos * to which the time value is compared to determine the final value. 683b6c3722Schristos * \param[out] result the struct with the broken-out time information 693b6c3722Schristos * \return result on success or NULL on error 703b6c3722Schristos */ 710cd9f4ecSchristos struct tm * sldns_serial_arithmetics_gmtime_r(int32_t time, time_t now, struct tm *result); 723b6c3722Schristos 733b6c3722Schristos /** 743b6c3722Schristos * converts a ttl value (like 5d2h) to a long. 753b6c3722Schristos * \param[in] nptr the start of the string 763b6c3722Schristos * \param[out] endptr points to the last char in case of error 77*7a540f2bSchristos * \param[out] overflow returns if the string causes integer overflow error, 78*7a540f2bSchristos * the number is too big, string of digits too long. 793b6c3722Schristos * \return the convert duration value 803b6c3722Schristos */ 81*7a540f2bSchristos uint32_t sldns_str2period(const char *nptr, const char **endptr, int* overflow); 823b6c3722Schristos 833b6c3722Schristos /** 843b6c3722Schristos * Returns the int value of the given (hex) digit 853b6c3722Schristos * \param[in] ch the hex char to convert 863b6c3722Schristos * \return the converted decimal value 873b6c3722Schristos */ 883b6c3722Schristos int sldns_hexdigit_to_int(char ch); 893b6c3722Schristos 903b6c3722Schristos /** 913b6c3722Schristos * calculates the size needed to store the result of b64_ntop 923b6c3722Schristos */ 933b6c3722Schristos size_t sldns_b64_ntop_calculate_size(size_t srcsize); 943b6c3722Schristos 953b6c3722Schristos int sldns_b64_ntop(uint8_t const *src, size_t srclength, 963b6c3722Schristos char *target, size_t targsize); 97d0eba39bSchristos int sldns_b64url_ntop(uint8_t const *src, size_t srclength, char *target, 98d0eba39bSchristos size_t targsize); 993b6c3722Schristos 1003b6c3722Schristos /** 1013b6c3722Schristos * calculates the size needed to store the result of sldns_b64_pton 1023b6c3722Schristos */ 1033b6c3722Schristos size_t sldns_b64_pton_calculate_size(size_t srcsize); 1043b6c3722Schristos int sldns_b64_pton(char const *src, uint8_t *target, size_t targsize); 105d0eba39bSchristos int sldns_b64url_pton(char const *src, size_t srcsize, uint8_t *target, 106d0eba39bSchristos size_t targsize); 107*7a540f2bSchristos int sldns_b64_contains_nonurl(char const *src, size_t srcsize); 1083b6c3722Schristos 1093b6c3722Schristos /** 1103b6c3722Schristos * calculates the size needed to store the result of b32_ntop 1113b6c3722Schristos */ 1123b6c3722Schristos size_t sldns_b32_ntop_calculate_size(size_t src_data_length); 1133b6c3722Schristos 1143b6c3722Schristos size_t sldns_b32_ntop_calculate_size_no_padding(size_t src_data_length); 1153b6c3722Schristos 1163b6c3722Schristos int sldns_b32_ntop(const uint8_t* src_data, size_t src_data_length, 1173b6c3722Schristos char* target_text_buffer, size_t target_text_buffer_size); 1183b6c3722Schristos 1193b6c3722Schristos int sldns_b32_ntop_extended_hex(const uint8_t* src_data, size_t src_data_length, 1203b6c3722Schristos char* target_text_buffer, size_t target_text_buffer_size); 1213b6c3722Schristos 1223b6c3722Schristos /** 1233b6c3722Schristos * calculates the size needed to store the result of b32_pton 1243b6c3722Schristos */ 1253b6c3722Schristos size_t sldns_b32_pton_calculate_size(size_t src_text_length); 1263b6c3722Schristos 1273b6c3722Schristos int sldns_b32_pton(const char* src_text, size_t src_text_length, 1283b6c3722Schristos uint8_t* target_data_buffer, size_t target_data_buffer_size); 1293b6c3722Schristos 1303b6c3722Schristos int sldns_b32_pton_extended_hex(const char* src_text, size_t src_text_length, 1313b6c3722Schristos uint8_t* target_data_buffer, size_t target_data_buffer_size); 1323b6c3722Schristos 1333b6c3722Schristos /* 1343b6c3722Schristos * Checks whether the escaped value at **s is an octal value or 1353b6c3722Schristos * a 'normally' escaped character (and not eos) 1363b6c3722Schristos * 1373b6c3722Schristos * @param ch_p: the parsed character 1383b6c3722Schristos * @param str_p: the string. moved along for characters read. 1393b6c3722Schristos * The string pointer at *s is increased by either 0 (on error), 1 (on 1403b6c3722Schristos * normal escapes), or 3 (on octals) 1413b6c3722Schristos * 1423b6c3722Schristos * @return 0 on error 1433b6c3722Schristos */ 1443b6c3722Schristos int sldns_parse_escape(uint8_t *ch_p, const char** str_p); 1453b6c3722Schristos 1463b6c3722Schristos /** 1473b6c3722Schristos * Parse one character, with escape codes, 1483b6c3722Schristos * @param ch_p: the parsed character 1493b6c3722Schristos * @param str_p: the string. moved along for characters read. 1503b6c3722Schristos * @return 0 on error 1513b6c3722Schristos */ 1523b6c3722Schristos int sldns_parse_char(uint8_t *ch_p, const char** str_p); 1533b6c3722Schristos 1543b6c3722Schristos #endif /* LDNS_PARSEUTIL_H */ 155