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