Home | History | Annotate | Line # | Download | only in sldns
parseutil.h revision 1.1
      1  1.1  christos /*
      2  1.1  christos  * parseutil.h - parse utilities for string and wire conversion
      3  1.1  christos  *
      4  1.1  christos  * (c) NLnet Labs, 2004
      5  1.1  christos  *
      6  1.1  christos  * See the file LICENSE for the license
      7  1.1  christos  */
      8  1.1  christos /**
      9  1.1  christos  * \file
     10  1.1  christos  *
     11  1.1  christos  * Utility functions for parsing, base32(DNS variant) and base64 encoding
     12  1.1  christos  * and decoding, Hex, Time units, Escape codes.
     13  1.1  christos  */
     14  1.1  christos 
     15  1.1  christos #ifndef LDNS_PARSEUTIL_H
     16  1.1  christos #define LDNS_PARSEUTIL_H
     17  1.1  christos struct tm;
     18  1.1  christos 
     19  1.1  christos /**
     20  1.1  christos  *  A general purpose lookup table
     21  1.1  christos  *
     22  1.1  christos  *  Lookup tables are arrays of (id, name) pairs,
     23  1.1  christos  *  So you can for instance lookup the RCODE 3, which is "NXDOMAIN",
     24  1.1  christos  *  and vice versa. The lookup tables themselves are defined wherever needed,
     25  1.1  christos  *  for instance in host2str.c
     26  1.1  christos  */
     27  1.1  christos struct sldns_struct_lookup_table {
     28  1.1  christos         int id;
     29  1.1  christos         const char *name;
     30  1.1  christos };
     31  1.1  christos typedef struct sldns_struct_lookup_table sldns_lookup_table;
     32  1.1  christos 
     33  1.1  christos /**
     34  1.1  christos  * Looks up the table entry by name, returns NULL if not found.
     35  1.1  christos  * \param[in] table the lookup table to search in
     36  1.1  christos  * \param[in] name what to search for
     37  1.1  christos  * \return the item found
     38  1.1  christos  */
     39  1.1  christos sldns_lookup_table *sldns_lookup_by_name(sldns_lookup_table table[],
     40  1.1  christos                                        const char *name);
     41  1.1  christos /**
     42  1.1  christos  * Looks up the table entry by id, returns NULL if not found.
     43  1.1  christos  * \param[in] table the lookup table to search in
     44  1.1  christos  * \param[in] id what to search for
     45  1.1  christos  * \return the item found
     46  1.1  christos  */
     47  1.1  christos sldns_lookup_table *sldns_lookup_by_id(sldns_lookup_table table[], int id);
     48  1.1  christos 
     49  1.1  christos /**
     50  1.1  christos  * Convert TM to seconds since epoch (midnight, January 1st, 1970).
     51  1.1  christos  * Like timegm(3), which is not always available.
     52  1.1  christos  * \param[in] tm a struct tm* with the date
     53  1.1  christos  * \return the seconds since epoch
     54  1.1  christos  */
     55  1.1  christos time_t sldns_mktime_from_utc(const struct tm *tm);
     56  1.1  christos 
     57  1.1  christos /**
     58  1.1  christos  * The function interprets time as the number of seconds since epoch
     59  1.1  christos  * with respect to now using serial arithmetics (rfc1982).
     60  1.1  christos  * That number of seconds is then converted to broken-out time information.
     61  1.1  christos  * This is especially usefull when converting the inception and expiration
     62  1.1  christos  * fields of RRSIG records.
     63  1.1  christos  *
     64  1.1  christos  * \param[in] time number of seconds since epoch (midnight, January 1st, 1970)
     65  1.1  christos  *            to be intepreted as a serial arithmetics number relative to now.
     66  1.1  christos  * \param[in] now number of seconds since epoch (midnight, January 1st, 1970)
     67  1.1  christos  *            to which the time value is compared to determine the final value.
     68  1.1  christos  * \param[out] result the struct with the broken-out time information
     69  1.1  christos  * \return result on success or NULL on error
     70  1.1  christos  */
     71  1.1  christos struct tm * sldns_serial_arithmitics_gmtime_r(int32_t time, time_t now, struct tm *result);
     72  1.1  christos 
     73  1.1  christos /**
     74  1.1  christos  * converts a ttl value (like 5d2h) to a long.
     75  1.1  christos  * \param[in] nptr the start of the string
     76  1.1  christos  * \param[out] endptr points to the last char in case of error
     77  1.1  christos  * \return the convert duration value
     78  1.1  christos  */
     79  1.1  christos uint32_t sldns_str2period(const char *nptr, const char **endptr);
     80  1.1  christos 
     81  1.1  christos /**
     82  1.1  christos  * Returns the int value of the given (hex) digit
     83  1.1  christos  * \param[in] ch the hex char to convert
     84  1.1  christos  * \return the converted decimal value
     85  1.1  christos  */
     86  1.1  christos int sldns_hexdigit_to_int(char ch);
     87  1.1  christos 
     88  1.1  christos /**
     89  1.1  christos  * calculates the size needed to store the result of b64_ntop
     90  1.1  christos  */
     91  1.1  christos size_t sldns_b64_ntop_calculate_size(size_t srcsize);
     92  1.1  christos 
     93  1.1  christos int sldns_b64_ntop(uint8_t const *src, size_t srclength,
     94  1.1  christos 	char *target, size_t targsize);
     95  1.1  christos 
     96  1.1  christos /**
     97  1.1  christos  * calculates the size needed to store the result of sldns_b64_pton
     98  1.1  christos  */
     99  1.1  christos size_t sldns_b64_pton_calculate_size(size_t srcsize);
    100  1.1  christos 
    101  1.1  christos int sldns_b64_pton(char const *src, uint8_t *target, size_t targsize);
    102  1.1  christos 
    103  1.1  christos /**
    104  1.1  christos  * calculates the size needed to store the result of b32_ntop
    105  1.1  christos  */
    106  1.1  christos size_t sldns_b32_ntop_calculate_size(size_t src_data_length);
    107  1.1  christos 
    108  1.1  christos size_t sldns_b32_ntop_calculate_size_no_padding(size_t src_data_length);
    109  1.1  christos 
    110  1.1  christos int sldns_b32_ntop(const uint8_t* src_data, size_t src_data_length,
    111  1.1  christos 	char* target_text_buffer, size_t target_text_buffer_size);
    112  1.1  christos 
    113  1.1  christos int sldns_b32_ntop_extended_hex(const uint8_t* src_data, size_t src_data_length,
    114  1.1  christos 	char* target_text_buffer, size_t target_text_buffer_size);
    115  1.1  christos 
    116  1.1  christos /**
    117  1.1  christos  * calculates the size needed to store the result of b32_pton
    118  1.1  christos  */
    119  1.1  christos size_t sldns_b32_pton_calculate_size(size_t src_text_length);
    120  1.1  christos 
    121  1.1  christos int sldns_b32_pton(const char* src_text, size_t src_text_length,
    122  1.1  christos 	uint8_t* target_data_buffer, size_t target_data_buffer_size);
    123  1.1  christos 
    124  1.1  christos int sldns_b32_pton_extended_hex(const char* src_text, size_t src_text_length,
    125  1.1  christos 	uint8_t* target_data_buffer, size_t target_data_buffer_size);
    126  1.1  christos 
    127  1.1  christos /*
    128  1.1  christos  * Checks whether the escaped value at **s is an octal value or
    129  1.1  christos  * a 'normally' escaped character (and not eos)
    130  1.1  christos  *
    131  1.1  christos  * @param ch_p: the parsed character
    132  1.1  christos  * @param str_p: the string. moved along for characters read.
    133  1.1  christos  * The string pointer at *s is increased by either 0 (on error), 1 (on
    134  1.1  christos  * normal escapes), or 3 (on octals)
    135  1.1  christos  *
    136  1.1  christos  * @return 0 on error
    137  1.1  christos  */
    138  1.1  christos int sldns_parse_escape(uint8_t *ch_p, const char** str_p);
    139  1.1  christos 
    140  1.1  christos /**
    141  1.1  christos  * Parse one character, with escape codes,
    142  1.1  christos  * @param ch_p: the parsed character
    143  1.1  christos  * @param str_p: the string. moved along for characters read.
    144  1.1  christos  * @return 0 on error
    145  1.1  christos  */
    146  1.1  christos int sldns_parse_char(uint8_t *ch_p, const char** str_p);
    147  1.1  christos 
    148  1.1  christos #endif /* LDNS_PARSEUTIL_H */
    149