1266077Sdes/*
2266077Sdes * parseutil.h - parse utilities for string and wire conversion
3266077Sdes *
4266077Sdes * (c) NLnet Labs, 2004
5266077Sdes *
6266077Sdes * See the file LICENSE for the license
7266077Sdes */
8266077Sdes/**
9266077Sdes * \file
10266077Sdes *
11266077Sdes * Utility functions for parsing, base32(DNS variant) and base64 encoding
12266077Sdes * and decoding, Hex, Time units, Escape codes.
13266077Sdes */
14266077Sdes
15266077Sdes#ifndef LDNS_PARSEUTIL_H
16266077Sdes#define LDNS_PARSEUTIL_H
17266077Sdesstruct tm;
18266077Sdes
19266077Sdes/**
20266077Sdes *  A general purpose lookup table
21266077Sdes *
22266077Sdes *  Lookup tables are arrays of (id, name) pairs,
23266077Sdes *  So you can for instance lookup the RCODE 3, which is "NXDOMAIN",
24266077Sdes *  and vice versa. The lookup tables themselves are defined wherever needed,
25266077Sdes *  for instance in host2str.c
26266077Sdes */
27266077Sdesstruct sldns_struct_lookup_table {
28266077Sdes        int id;
29266077Sdes        const char *name;
30266077Sdes};
31266077Sdestypedef struct sldns_struct_lookup_table sldns_lookup_table;
32266077Sdes
33266077Sdes/**
34266077Sdes * Looks up the table entry by name, returns NULL if not found.
35266077Sdes * \param[in] table the lookup table to search in
36266077Sdes * \param[in] name what to search for
37266077Sdes * \return the item found
38266077Sdes */
39266077Sdessldns_lookup_table *sldns_lookup_by_name(sldns_lookup_table table[],
40266077Sdes                                       const char *name);
41266077Sdes/**
42266077Sdes * Looks up the table entry by id, returns NULL if not found.
43266077Sdes * \param[in] table the lookup table to search in
44266077Sdes * \param[in] id what to search for
45266077Sdes * \return the item found
46266077Sdes */
47266077Sdessldns_lookup_table *sldns_lookup_by_id(sldns_lookup_table table[], int id);
48266077Sdes
49266077Sdes/**
50266077Sdes * Convert TM to seconds since epoch (midnight, January 1st, 1970).
51266077Sdes * Like timegm(3), which is not always available.
52266077Sdes * \param[in] tm a struct tm* with the date
53266077Sdes * \return the seconds since epoch
54266077Sdes */
55266077Sdestime_t sldns_mktime_from_utc(const struct tm *tm);
56266077Sdes
57266077Sdes/**
58266077Sdes * The function interprets time as the number of seconds since epoch
59294190Sdes * with respect to now using serial arithmetics (rfc1982).
60266077Sdes * That number of seconds is then converted to broken-out time information.
61266077Sdes * This is especially usefull when converting the inception and expiration
62266077Sdes * fields of RRSIG records.
63266077Sdes *
64266077Sdes * \param[in] time number of seconds since epoch (midnight, January 1st, 1970)
65294190Sdes *            to be intepreted as a serial arithmetics number relative to now.
66266077Sdes * \param[in] now number of seconds since epoch (midnight, January 1st, 1970)
67266077Sdes *            to which the time value is compared to determine the final value.
68266077Sdes * \param[out] result the struct with the broken-out time information
69266077Sdes * \return result on success or NULL on error
70266077Sdes */
71266077Sdesstruct tm * sldns_serial_arithmitics_gmtime_r(int32_t time, time_t now, struct tm *result);
72266077Sdes
73266077Sdes/**
74266077Sdes * converts a ttl value (like 5d2h) to a long.
75266077Sdes * \param[in] nptr the start of the string
76266077Sdes * \param[out] endptr points to the last char in case of error
77266077Sdes * \return the convert duration value
78266077Sdes */
79266077Sdesuint32_t sldns_str2period(const char *nptr, const char **endptr);
80266077Sdes
81266077Sdes/**
82266077Sdes * Returns the int value of the given (hex) digit
83266077Sdes * \param[in] ch the hex char to convert
84266077Sdes * \return the converted decimal value
85266077Sdes */
86266077Sdesint sldns_hexdigit_to_int(char ch);
87266077Sdes
88266077Sdes/**
89266077Sdes * calculates the size needed to store the result of b64_ntop
90266077Sdes */
91266077Sdessize_t sldns_b64_ntop_calculate_size(size_t srcsize);
92266077Sdes
93266077Sdesint sldns_b64_ntop(uint8_t const *src, size_t srclength,
94266077Sdes	char *target, size_t targsize);
95266077Sdes
96266077Sdes/**
97266077Sdes * calculates the size needed to store the result of sldns_b64_pton
98266077Sdes */
99266077Sdessize_t sldns_b64_pton_calculate_size(size_t srcsize);
100266077Sdes
101266077Sdesint sldns_b64_pton(char const *src, uint8_t *target, size_t targsize);
102266077Sdes
103266077Sdes/**
104266077Sdes * calculates the size needed to store the result of b32_ntop
105266077Sdes */
106266077Sdessize_t sldns_b32_ntop_calculate_size(size_t src_data_length);
107266077Sdes
108266077Sdessize_t sldns_b32_ntop_calculate_size_no_padding(size_t src_data_length);
109266077Sdes
110266077Sdesint sldns_b32_ntop(const uint8_t* src_data, size_t src_data_length,
111266077Sdes	char* target_text_buffer, size_t target_text_buffer_size);
112266077Sdes
113266077Sdesint sldns_b32_ntop_extended_hex(const uint8_t* src_data, size_t src_data_length,
114266077Sdes	char* target_text_buffer, size_t target_text_buffer_size);
115266077Sdes
116266077Sdes/**
117266077Sdes * calculates the size needed to store the result of b32_pton
118266077Sdes */
119266077Sdessize_t sldns_b32_pton_calculate_size(size_t src_text_length);
120266077Sdes
121266077Sdesint sldns_b32_pton(const char* src_text, size_t src_text_length,
122266077Sdes	uint8_t* target_data_buffer, size_t target_data_buffer_size);
123266077Sdes
124266077Sdesint sldns_b32_pton_extended_hex(const char* src_text, size_t src_text_length,
125266077Sdes	uint8_t* target_data_buffer, size_t target_data_buffer_size);
126266077Sdes
127266077Sdes/*
128266077Sdes * Checks whether the escaped value at **s is an octal value or
129266077Sdes * a 'normally' escaped character (and not eos)
130266077Sdes *
131266077Sdes * @param ch_p: the parsed character
132266077Sdes * @param str_p: the string. moved along for characters read.
133266077Sdes * The string pointer at *s is increased by either 0 (on error), 1 (on
134266077Sdes * normal escapes), or 3 (on octals)
135266077Sdes *
136266077Sdes * @return 0 on error
137266077Sdes */
138266077Sdesint sldns_parse_escape(uint8_t *ch_p, const char** str_p);
139266077Sdes
140266077Sdes/**
141266077Sdes * Parse one character, with escape codes,
142266077Sdes * @param ch_p: the parsed character
143266077Sdes * @param str_p: the string. moved along for characters read.
144266077Sdes * @return 0 on error
145266077Sdes */
146266077Sdesint sldns_parse_char(uint8_t *ch_p, const char** str_p);
147266077Sdes
148266077Sdes#endif /* LDNS_PARSEUTIL_H */
149