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