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