parseutil.h revision 294190
150476Speter/*
229585Sjulian * parseutil.h - parse utilities for string and wire conversion
329585Sjulian *
429585Sjulian * (c) NLnet Labs, 2004
529585Sjulian *
6174990Sache * See the file LICENSE for the license
7174990Sache */
8199179Sume/**
9199179Sume * \file
10199179Sume *
11199179Sume * Utility functions for parsing, base32(DNS variant) and base64 encoding
12199179Sume * and decoding, Hex, Time units, Escape codes.
13199179Sume */
14199179Sume
15199179Sume#ifndef LDNS_PARSEUTIL_H
16199179Sume#define LDNS_PARSEUTIL_H
17199179Sumestruct tm;
18199179Sume
19199179Sume/**
2029585Sjulian *  A general purpose lookup table
21174990Sache *
2229585Sjulian *  Lookup tables are arrays of (id, name) pairs,
2329594Sjulian *  So you can for instance lookup the RCODE 3, which is "NXDOMAIN",
2429594Sjulian *  and vice versa. The lookup tables themselves are defined wherever needed,
2529594Sjulian *  for instance in host2str.c
2629594Sjulian */
2729594Sjulianstruct sldns_struct_lookup_table {
2829594Sjulian        int id;
2929594Sjulian        const char *name;
3029594Sjulian};
3129594Sjuliantypedef struct sldns_struct_lookup_table sldns_lookup_table;
3229594Sjulian
3329594Sjulian/**
3429594Sjulian * Looks up the table entry by name, returns NULL if not found.
3529585Sjulian * \param[in] table the lookup table to search in
3629585Sjulian * \param[in] name what to search for
3729585Sjulian * \return the item found
3874606Sache */
3974606Sachesldns_lookup_table *sldns_lookup_by_name(sldns_lookup_table table[],
4074606Sache                                       const char *name);
4174606Sache/**
4274606Sache * Looks up the table entry by id, returns NULL if not found.
4374606Sache * \param[in] table the lookup table to search in
4474606Sache * \param[in] id what to search for
4529585Sjulian * \return the item found
4629585Sjulian */
4729585Sjuliansldns_lookup_table *sldns_lookup_by_id(sldns_lookup_table table[], int id);
4829594Sjulian
4929594Sjulian/**
5029594Sjulian * Convert TM to seconds since epoch (midnight, January 1st, 1970).
5129594Sjulian * Like timegm(3), which is not always available.
5229594Sjulian * \param[in] tm a struct tm* with the date
5329594Sjulian * \return the seconds since epoch
5429594Sjulian */
5529585Sjuliantime_t sldns_mktime_from_utc(const struct tm *tm);
5629585Sjulian
5729585Sjulian/**
5829594Sjulian * The function interprets time as the number of seconds since epoch
5929585Sjulian * with respect to now using serial arithmetics (rfc1982).
6029585Sjulian * That number of seconds is then converted to broken-out time information.
6129585Sjulian * This is especially usefull when converting the inception and expiration
6274570Sache * fields of RRSIG records.
6329585Sjulian *
6429585Sjulian * \param[in] time number of seconds since epoch (midnight, January 1st, 1970)
6529585Sjulian *            to be intepreted as a serial arithmetics number relative to now.
6629585Sjulian * \param[in] now number of seconds since epoch (midnight, January 1st, 1970)
6729585Sjulian *            to which the time value is compared to determine the final value.
68199271Sume * \param[out] result the struct with the broken-out time information
6929585Sjulian * \return result on success or NULL on error
7029585Sjulian */
7129585Sjulianstruct tm * sldns_serial_arithmitics_gmtime_r(int32_t time, time_t now, struct tm *result);
72193869Sedwin
7329585Sjulian/**
7429585Sjulian * converts a ttl value (like 5d2h) to a long.
7529585Sjulian * \param[in] nptr the start of the string
76193869Sedwin * \param[out] endptr points to the last char in case of error
7729585Sjulian * \return the convert duration value
7829585Sjulian */
7929585Sjulianuint32_t sldns_str2period(const char *nptr, const char **endptr);
8029594Sjulian
8153943Sache/**
82174990Sache * Returns the int value of the given (hex) digit
8353943Sache * \param[in] ch the hex char to convert
8453943Sache * \return the converted decimal value
8553943Sache */
8653943Sacheint sldns_hexdigit_to_int(char ch);
8753943Sache
8853943Sache/**
8953943Sache * calculates the size needed to store the result of b64_ntop
9053943Sache */
9153943Sachesize_t sldns_b64_ntop_calculate_size(size_t srcsize);
9253943Sache
9353943Sacheint sldns_b64_ntop(uint8_t const *src, size_t srclength,
9453943Sache	char *target, size_t targsize);
9553943Sache
9653943Sache/**
9774413Sache * calculates the size needed to store the result of sldns_b64_pton
9853943Sache */
9974413Sachesize_t sldns_b64_pton_calculate_size(size_t srcsize);
10053961Sache
10174413Sacheint sldns_b64_pton(char const *src, uint8_t *target, size_t targsize);
10253961Sache
10374413Sache/**
10474413Sache * calculates the size needed to store the result of b32_ntop
105 */
106size_t sldns_b32_ntop_calculate_size(size_t src_data_length);
107
108size_t sldns_b32_ntop_calculate_size_no_padding(size_t src_data_length);
109
110int sldns_b32_ntop(const uint8_t* src_data, size_t src_data_length,
111	char* target_text_buffer, size_t target_text_buffer_size);
112
113int sldns_b32_ntop_extended_hex(const uint8_t* src_data, size_t src_data_length,
114	char* target_text_buffer, size_t target_text_buffer_size);
115
116/**
117 * calculates the size needed to store the result of b32_pton
118 */
119size_t sldns_b32_pton_calculate_size(size_t src_text_length);
120
121int sldns_b32_pton(const char* src_text, size_t src_text_length,
122	uint8_t* target_data_buffer, size_t target_data_buffer_size);
123
124int sldns_b32_pton_extended_hex(const char* src_text, size_t src_text_length,
125	uint8_t* target_data_buffer, size_t target_data_buffer_size);
126
127/*
128 * Checks whether the escaped value at **s is an octal value or
129 * a 'normally' escaped character (and not eos)
130 *
131 * @param ch_p: the parsed character
132 * @param str_p: the string. moved along for characters read.
133 * The string pointer at *s is increased by either 0 (on error), 1 (on
134 * normal escapes), or 3 (on octals)
135 *
136 * @return 0 on error
137 */
138int sldns_parse_escape(uint8_t *ch_p, const char** str_p);
139
140/**
141 * Parse one character, with escape codes,
142 * @param ch_p: the parsed character
143 * @param str_p: the string. moved along for characters read.
144 * @return 0 on error
145 */
146int sldns_parse_char(uint8_t *ch_p, const char** str_p);
147
148#endif /* LDNS_PARSEUTIL_H */
149