1ae8c6e27Sflorian /**
2ae8c6e27Sflorian  * str2wire.c - read txt presentation of RRs
3ae8c6e27Sflorian  *
4ae8c6e27Sflorian  * (c) NLnet Labs, 2005-2006
5ae8c6e27Sflorian  *
6ae8c6e27Sflorian  * See the file LICENSE for the license
7ae8c6e27Sflorian  */
8ae8c6e27Sflorian 
9ae8c6e27Sflorian /**
10ae8c6e27Sflorian  * \file
11ae8c6e27Sflorian  *
12ae8c6e27Sflorian  * Parses text to wireformat.
13ae8c6e27Sflorian  */
14ae8c6e27Sflorian #include "config.h"
15ae8c6e27Sflorian #include "sldns/str2wire.h"
16ae8c6e27Sflorian #include "sldns/wire2str.h"
17ae8c6e27Sflorian #include "sldns/sbuffer.h"
18ae8c6e27Sflorian #include "sldns/parse.h"
19ae8c6e27Sflorian #include "sldns/parseutil.h"
20ae8c6e27Sflorian #include <ctype.h>
21ae8c6e27Sflorian #ifdef HAVE_TIME_H
22ae8c6e27Sflorian #include <time.h>
23ae8c6e27Sflorian #endif
24ae8c6e27Sflorian #ifdef HAVE_NETDB_H
25ae8c6e27Sflorian #include <netdb.h>
26ae8c6e27Sflorian #endif
27ae8c6e27Sflorian 
28ae8c6e27Sflorian /** return an error */
29ae8c6e27Sflorian #define RET_ERR(e, off) ((int)((e)|((off)<<LDNS_WIREPARSE_SHIFT)))
30ae8c6e27Sflorian /** Move parse error but keep its ID */
31ae8c6e27Sflorian #define RET_ERR_SHIFT(e, move) RET_ERR(LDNS_WIREPARSE_ERROR(e), LDNS_WIREPARSE_OFFSET(e)+(move));
32ae8c6e27Sflorian 
33ae8c6e27Sflorian /*
34ae8c6e27Sflorian  * No special care is taken, all dots are translated into
35ae8c6e27Sflorian  * label separators.
36ae8c6e27Sflorian  * @param rel: true if the domain is not absolute (not terminated in .).
37ae8c6e27Sflorian  * 	The output is then still terminated with a '0' rootlabel.
38ae8c6e27Sflorian  */
39ae8c6e27Sflorian static int sldns_str2wire_dname_buf_rel(const char* str, uint8_t* buf,
40ae8c6e27Sflorian 	size_t* olen, int* rel)
41ae8c6e27Sflorian {
42ae8c6e27Sflorian 	size_t len;
43ae8c6e27Sflorian 
44ae8c6e27Sflorian 	const char *s;
45ae8c6e27Sflorian 	uint8_t *q, *pq, label_len;
46ae8c6e27Sflorian 
47ae8c6e27Sflorian 	if(rel) *rel = 0;
48ae8c6e27Sflorian 	len = strlen((char*)str);
49ae8c6e27Sflorian 	/* octet representation can make strings a lot longer than actual length */
50ae8c6e27Sflorian 	if (len > LDNS_MAX_DOMAINLEN * 4) {
51ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_DOMAINNAME_OVERFLOW, 0);
52ae8c6e27Sflorian 	}
53ae8c6e27Sflorian 	if (0 == len) {
54ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_DOMAINNAME_UNDERFLOW, 0);
55ae8c6e27Sflorian 	}
56ae8c6e27Sflorian 
57ae8c6e27Sflorian 	/* root label */
58ae8c6e27Sflorian 	if (1 == len && *str == '.') {
59ae8c6e27Sflorian 		if(*olen < 1)
60ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, 0);
61ae8c6e27Sflorian 		buf[0] = 0;
62ae8c6e27Sflorian 		*olen = 1;
63ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_OK;
64ae8c6e27Sflorian 	}
65ae8c6e27Sflorian 
66ae8c6e27Sflorian 	/* get on with the rest */
67ae8c6e27Sflorian 
68ae8c6e27Sflorian 	/* s is on the current character in the string
69ae8c6e27Sflorian          * pq points to where the labellength is going to go
70ae8c6e27Sflorian          * label_len keeps track of the current label's length
71ae8c6e27Sflorian 	 * q builds the dname inside the buf array
72ae8c6e27Sflorian 	 */
73ae8c6e27Sflorian 	len = 0;
74ae8c6e27Sflorian 	if(*olen < 1)
75ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, 0);
76ae8c6e27Sflorian 	q = buf+1;
77ae8c6e27Sflorian 	pq = buf;
78ae8c6e27Sflorian 	label_len = 0;
79ae8c6e27Sflorian 	for (s = str; *s; s++, q++) {
80ae8c6e27Sflorian 		if (q >= buf + *olen)
81ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, q-buf);
82d32eb43cSflorian 		if (q >= buf + LDNS_MAX_DOMAINLEN)
83ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_DOMAINNAME_OVERFLOW, q-buf);
84ae8c6e27Sflorian 		switch (*s) {
85ae8c6e27Sflorian 		case '.':
86ae8c6e27Sflorian 			if (label_len > LDNS_MAX_LABELLEN) {
87ae8c6e27Sflorian 				return RET_ERR(LDNS_WIREPARSE_ERR_LABEL_OVERFLOW, q-buf);
88ae8c6e27Sflorian 			}
89ae8c6e27Sflorian 			if (label_len == 0) {
90ae8c6e27Sflorian 				return RET_ERR(LDNS_WIREPARSE_ERR_EMPTY_LABEL, q-buf);
91ae8c6e27Sflorian 			}
92ae8c6e27Sflorian 			len += label_len + 1;
93ae8c6e27Sflorian 			*q = 0;
94ae8c6e27Sflorian 			*pq = label_len;
95ae8c6e27Sflorian 			label_len = 0;
96ae8c6e27Sflorian 			pq = q;
97ae8c6e27Sflorian 			break;
98ae8c6e27Sflorian 		case '\\':
99ae8c6e27Sflorian 			/* octet value or literal char */
100ae8c6e27Sflorian 			s += 1;
101ae8c6e27Sflorian 			if (!sldns_parse_escape(q, &s)) {
102ae8c6e27Sflorian 				*q = 0;
103ae8c6e27Sflorian 				return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_BAD_ESCAPE, q-buf);
104ae8c6e27Sflorian 			}
105ae8c6e27Sflorian 			s -= 1;
106ae8c6e27Sflorian 			label_len++;
107ae8c6e27Sflorian 			break;
108ae8c6e27Sflorian 		default:
109ae8c6e27Sflorian 			*q = (uint8_t)*s;
110ae8c6e27Sflorian 			label_len++;
111ae8c6e27Sflorian 		}
112ae8c6e27Sflorian 	}
113ae8c6e27Sflorian 
114ae8c6e27Sflorian 	/* add root label if last char was not '.' */
115ae8c6e27Sflorian 	if(label_len != 0) {
116ae8c6e27Sflorian 		if(rel) *rel = 1;
117ae8c6e27Sflorian 		if (q >= buf + *olen)
118ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, q-buf);
119d32eb43cSflorian 		if (q >= buf + LDNS_MAX_DOMAINLEN) {
120ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_DOMAINNAME_OVERFLOW, q-buf);
121ae8c6e27Sflorian 		}
122ae8c6e27Sflorian                 if (label_len > LDNS_MAX_LABELLEN) {
123ae8c6e27Sflorian                         return RET_ERR(LDNS_WIREPARSE_ERR_LABEL_OVERFLOW, q-buf);
124ae8c6e27Sflorian                 }
125ae8c6e27Sflorian                 if (label_len == 0) { /* label_len 0 but not . at end? */
126ae8c6e27Sflorian                         return RET_ERR(LDNS_WIREPARSE_ERR_EMPTY_LABEL, q-buf);
127ae8c6e27Sflorian                 }
128ae8c6e27Sflorian 		len += label_len + 1;
129ae8c6e27Sflorian 		*pq = label_len;
130ae8c6e27Sflorian 		*q = 0;
131ae8c6e27Sflorian 	}
132ae8c6e27Sflorian 	len++;
133ae8c6e27Sflorian 	*olen = len;
134ae8c6e27Sflorian 
135ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
136ae8c6e27Sflorian }
137ae8c6e27Sflorian 
138ae8c6e27Sflorian int sldns_str2wire_dname_buf(const char* str, uint8_t* buf, size_t* len)
139ae8c6e27Sflorian {
140ae8c6e27Sflorian 	return sldns_str2wire_dname_buf_rel(str, buf, len, NULL);
141ae8c6e27Sflorian }
142ae8c6e27Sflorian 
143ae8c6e27Sflorian int sldns_str2wire_dname_buf_origin(const char* str, uint8_t* buf, size_t* len,
144ae8c6e27Sflorian 	uint8_t* origin, size_t origin_len)
145ae8c6e27Sflorian {
146ae8c6e27Sflorian 	size_t dlen = *len;
147ae8c6e27Sflorian 	int rel = 0;
148ae8c6e27Sflorian 	int s = sldns_str2wire_dname_buf_rel(str, buf, &dlen, &rel);
149ae8c6e27Sflorian 	if(s) return s;
150ae8c6e27Sflorian 
151ae8c6e27Sflorian 	if(rel && origin && dlen > 0) {
15257403691Sflorian 		if((unsigned)dlen >= 0x00ffffffU ||
15357403691Sflorian 			(unsigned)origin_len >= 0x00ffffffU)
15457403691Sflorian 			/* guard against integer overflow in addition */
15557403691Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_GENERAL, *len);
156ae8c6e27Sflorian 		if(dlen + origin_len - 1 > LDNS_MAX_DOMAINLEN)
157ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_DOMAINNAME_OVERFLOW,
158ae8c6e27Sflorian 				LDNS_MAX_DOMAINLEN);
159ae8c6e27Sflorian 		if(dlen + origin_len - 1 > *len)
160ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
161ae8c6e27Sflorian 				*len);
162ae8c6e27Sflorian 		memmove(buf+dlen-1, origin, origin_len);
163ae8c6e27Sflorian 		*len = dlen + origin_len - 1;
164ae8c6e27Sflorian 	} else
165ae8c6e27Sflorian 		*len = dlen;
166ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
167ae8c6e27Sflorian }
168ae8c6e27Sflorian 
169ae8c6e27Sflorian uint8_t* sldns_str2wire_dname(const char* str, size_t* len)
170ae8c6e27Sflorian {
171ae8c6e27Sflorian 	uint8_t dname[LDNS_MAX_DOMAINLEN+1];
172ae8c6e27Sflorian 	*len = sizeof(dname);
173ae8c6e27Sflorian 	if(sldns_str2wire_dname_buf(str, dname, len) == 0) {
17457403691Sflorian 		uint8_t* r;
17557403691Sflorian 		if(*len > sizeof(dname)) return NULL;
17657403691Sflorian 		r = (uint8_t*)malloc(*len);
177ae8c6e27Sflorian 		if(r) return memcpy(r, dname, *len);
178ae8c6e27Sflorian 	}
179ae8c6e27Sflorian 	*len = 0;
180ae8c6e27Sflorian 	return NULL;
181ae8c6e27Sflorian }
182ae8c6e27Sflorian 
183ae8c6e27Sflorian /** read owner name */
184ae8c6e27Sflorian static int
185ae8c6e27Sflorian rrinternal_get_owner(sldns_buffer* strbuf, uint8_t* rr, size_t* len,
186ae8c6e27Sflorian 	size_t* dname_len, uint8_t* origin, size_t origin_len, uint8_t* prev,
187ae8c6e27Sflorian 	size_t prev_len, char* token, size_t token_len)
188ae8c6e27Sflorian {
189ae8c6e27Sflorian 	/* split the rr in its parts -1 signals trouble */
190ae8c6e27Sflorian 	if(sldns_bget_token(strbuf, token, "\t\n ", token_len) == -1) {
191ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX,
192ae8c6e27Sflorian 			sldns_buffer_position(strbuf));
193ae8c6e27Sflorian 	}
194ae8c6e27Sflorian 
19557403691Sflorian 	if(token_len < 2) /* make sure there is space to read "@" or "" */
19657403691Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
19757403691Sflorian 			sldns_buffer_position(strbuf));
198da8c8390Sflorian 	if(token[0]=='@' && token[1]=='\0') {
199ae8c6e27Sflorian 		uint8_t* tocopy;
200ae8c6e27Sflorian 		if (origin) {
201ae8c6e27Sflorian 			*dname_len = origin_len;
202ae8c6e27Sflorian 			tocopy = origin;
203ae8c6e27Sflorian 		} else if (prev) {
204ae8c6e27Sflorian 			*dname_len = prev_len;
205ae8c6e27Sflorian 			tocopy = prev;
206ae8c6e27Sflorian 		} else {
207ae8c6e27Sflorian 			/* default to root */
208ae8c6e27Sflorian 			*dname_len = 1;
209ae8c6e27Sflorian 			tocopy = (uint8_t*)"\0";
210ae8c6e27Sflorian 		}
211ae8c6e27Sflorian 		if(*len < *dname_len)
212ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
213ae8c6e27Sflorian 				sldns_buffer_position(strbuf));
214ae8c6e27Sflorian 		memmove(rr, tocopy, *dname_len);
215ae8c6e27Sflorian 	} else if(*token == '\0') {
216ae8c6e27Sflorian 		/* no ownername was given, try prev, if that fails
217ae8c6e27Sflorian 		 * origin, else default to root */
218ae8c6e27Sflorian 		uint8_t* tocopy;
219ae8c6e27Sflorian 		if(prev) {
220ae8c6e27Sflorian 			*dname_len = prev_len;
221ae8c6e27Sflorian 			tocopy = prev;
222ae8c6e27Sflorian 		} else if(origin) {
223ae8c6e27Sflorian 			*dname_len = origin_len;
224ae8c6e27Sflorian 			tocopy = origin;
225ae8c6e27Sflorian 		} else {
226ae8c6e27Sflorian 			*dname_len = 1;
227ae8c6e27Sflorian 			tocopy = (uint8_t*)"\0";
228ae8c6e27Sflorian 		}
229ae8c6e27Sflorian 		if(*len < *dname_len)
230ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
231ae8c6e27Sflorian 				sldns_buffer_position(strbuf));
232ae8c6e27Sflorian 		memmove(rr, tocopy, *dname_len);
233ae8c6e27Sflorian 	} else {
234ae8c6e27Sflorian 		size_t dlen = *len;
235ae8c6e27Sflorian 		int s = sldns_str2wire_dname_buf_origin(token, rr, &dlen,
236ae8c6e27Sflorian 			origin, origin_len);
237ae8c6e27Sflorian 		if(s) return RET_ERR_SHIFT(s,
238ae8c6e27Sflorian 			sldns_buffer_position(strbuf)-strlen(token));
239ae8c6e27Sflorian 		*dname_len = dlen;
240ae8c6e27Sflorian 	}
241ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
242ae8c6e27Sflorian }
243ae8c6e27Sflorian 
244ae8c6e27Sflorian /** read ttl */
245ae8c6e27Sflorian static int
246ae8c6e27Sflorian rrinternal_get_ttl(sldns_buffer* strbuf, char* token, size_t token_len,
247ae8c6e27Sflorian 	int* not_there, uint32_t* ttl, uint32_t default_ttl)
248ae8c6e27Sflorian {
249ae8c6e27Sflorian 	const char* endptr;
250ae8c6e27Sflorian 	if(sldns_bget_token(strbuf, token, "\t\n ", token_len) == -1) {
251ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TTL,
252ae8c6e27Sflorian 			sldns_buffer_position(strbuf));
253ae8c6e27Sflorian 	}
254ae8c6e27Sflorian 	*ttl = (uint32_t) sldns_str2period(token, &endptr);
255ae8c6e27Sflorian 
256ae8c6e27Sflorian 	if (strlen(token) > 0 && !isdigit((unsigned char)token[0])) {
257ae8c6e27Sflorian 		*not_there = 1;
258ae8c6e27Sflorian 		/* ah, it's not there or something */
259ae8c6e27Sflorian 		if (default_ttl == 0) {
260ae8c6e27Sflorian 			*ttl = LDNS_DEFAULT_TTL;
261ae8c6e27Sflorian 		} else {
262ae8c6e27Sflorian 			*ttl = default_ttl;
263ae8c6e27Sflorian 		}
264ae8c6e27Sflorian 	}
265ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
266ae8c6e27Sflorian }
267ae8c6e27Sflorian 
268ae8c6e27Sflorian /** read class */
269ae8c6e27Sflorian static int
270ae8c6e27Sflorian rrinternal_get_class(sldns_buffer* strbuf, char* token, size_t token_len,
271ae8c6e27Sflorian 	int* not_there, uint16_t* cl)
272ae8c6e27Sflorian {
273ae8c6e27Sflorian 	/* if 'not_there' then we got token from previous parse routine */
274ae8c6e27Sflorian 	if(!*not_there) {
275ae8c6e27Sflorian 		/* parse new token for class */
276ae8c6e27Sflorian 		if(sldns_bget_token(strbuf, token, "\t\n ", token_len) == -1) {
277ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_CLASS,
278ae8c6e27Sflorian 				sldns_buffer_position(strbuf));
279ae8c6e27Sflorian 		}
280ae8c6e27Sflorian 	} else *not_there = 0;
281ae8c6e27Sflorian 	*cl = sldns_get_rr_class_by_name(token);
282ae8c6e27Sflorian 	/* class can be left out too, assume IN, current token must be type */
283ae8c6e27Sflorian 	if(*cl == 0 && strcmp(token, "CLASS0") != 0) {
284ae8c6e27Sflorian 		*not_there = 1;
285ae8c6e27Sflorian 		*cl = LDNS_RR_CLASS_IN;
286ae8c6e27Sflorian 	}
287ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
288ae8c6e27Sflorian }
289ae8c6e27Sflorian 
290ae8c6e27Sflorian /** read type */
291ae8c6e27Sflorian static int
292ae8c6e27Sflorian rrinternal_get_type(sldns_buffer* strbuf, char* token, size_t token_len,
293ae8c6e27Sflorian 	int* not_there, uint16_t* tp)
294ae8c6e27Sflorian {
295ae8c6e27Sflorian 	/* if 'not_there' then we got token from previous parse routine */
296ae8c6e27Sflorian 	if(!*not_there) {
297ae8c6e27Sflorian 		/* parse new token for type */
298ae8c6e27Sflorian 		if(sldns_bget_token(strbuf, token, "\t\n ", token_len) == -1) {
299ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TYPE,
300ae8c6e27Sflorian 				sldns_buffer_position(strbuf));
301ae8c6e27Sflorian 		}
302ae8c6e27Sflorian 	}
303ae8c6e27Sflorian 	*tp = sldns_get_rr_type_by_name(token);
304ae8c6e27Sflorian 	if(*tp == 0 && strcmp(token, "TYPE0") != 0) {
305ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TYPE,
306ae8c6e27Sflorian 			sldns_buffer_position(strbuf));
307ae8c6e27Sflorian 	}
308ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
309ae8c6e27Sflorian }
310ae8c6e27Sflorian 
311ae8c6e27Sflorian /** put type, class, ttl into rr buffer */
312ae8c6e27Sflorian static int
313ae8c6e27Sflorian rrinternal_write_typeclassttl(sldns_buffer* strbuf, uint8_t* rr, size_t len,
314ae8c6e27Sflorian 	size_t dname_len, uint16_t tp, uint16_t cl, uint32_t ttl, int question)
315ae8c6e27Sflorian {
316ae8c6e27Sflorian 	if(question) {
317ae8c6e27Sflorian 		/* question is : name, type, class */
318ae8c6e27Sflorian 		if(dname_len + 4 > len)
319ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
320ae8c6e27Sflorian 				sldns_buffer_position(strbuf));
321ae8c6e27Sflorian 		sldns_write_uint16(rr+dname_len, tp);
322ae8c6e27Sflorian 		sldns_write_uint16(rr+dname_len+2, cl);
323ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_OK;
324ae8c6e27Sflorian 	}
325ae8c6e27Sflorian 
326ae8c6e27Sflorian 	/* type(2), class(2), ttl(4), rdatalen(2 (later)) = 10 */
327ae8c6e27Sflorian 	if(dname_len + 10 > len)
328ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
329ae8c6e27Sflorian 			sldns_buffer_position(strbuf));
330ae8c6e27Sflorian 	sldns_write_uint16(rr+dname_len, tp);
331ae8c6e27Sflorian 	sldns_write_uint16(rr+dname_len+2, cl);
332ae8c6e27Sflorian 	sldns_write_uint32(rr+dname_len+4, ttl);
333ae8c6e27Sflorian 	sldns_write_uint16(rr+dname_len+8, 0); /* rdatalen placeholder */
334ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
335ae8c6e27Sflorian }
336ae8c6e27Sflorian 
337ae8c6e27Sflorian /** find delimiters for type */
338ae8c6e27Sflorian static const char*
339ae8c6e27Sflorian rrinternal_get_delims(sldns_rdf_type rdftype, size_t r_cnt, size_t r_max)
340ae8c6e27Sflorian {
341ae8c6e27Sflorian 	switch(rdftype) {
342ae8c6e27Sflorian 	case LDNS_RDF_TYPE_B64        :
343ae8c6e27Sflorian 	case LDNS_RDF_TYPE_HEX        : /* These rdf types may con- */
344ae8c6e27Sflorian 	case LDNS_RDF_TYPE_LOC        : /* tain whitespace, only if */
345ae8c6e27Sflorian 	case LDNS_RDF_TYPE_WKS        : /* it is the last rd field. */
346ae8c6e27Sflorian 	case LDNS_RDF_TYPE_IPSECKEY   :
347ae8c6e27Sflorian 	case LDNS_RDF_TYPE_NSEC       :	if (r_cnt == r_max - 1) {
348ae8c6e27Sflorian 						return "\n";
349ae8c6e27Sflorian 					}
350ae8c6e27Sflorian 					break;
351ae8c6e27Sflorian 	default                       :	break;
352ae8c6e27Sflorian 	}
353ae8c6e27Sflorian 	return "\n\t ";
354ae8c6e27Sflorian }
355ae8c6e27Sflorian 
356ae8c6e27Sflorian /* Syntactic sugar for sldns_rr_new_frm_str_internal */
357ae8c6e27Sflorian static int
358ae8c6e27Sflorian sldns_rdf_type_maybe_quoted(sldns_rdf_type rdf_type)
359ae8c6e27Sflorian {
360ae8c6e27Sflorian 	return  rdf_type == LDNS_RDF_TYPE_STR ||
361ae8c6e27Sflorian 		rdf_type == LDNS_RDF_TYPE_LONG_STR;
362ae8c6e27Sflorian }
363ae8c6e27Sflorian 
364ae8c6e27Sflorian /** see if rdata is quoted */
365ae8c6e27Sflorian static int
366ae8c6e27Sflorian rrinternal_get_quoted(sldns_buffer* strbuf, const char** delimiters,
367ae8c6e27Sflorian 	sldns_rdf_type rdftype)
368ae8c6e27Sflorian {
369ae8c6e27Sflorian 	if(sldns_rdf_type_maybe_quoted(rdftype) &&
370ae8c6e27Sflorian 		sldns_buffer_remaining(strbuf) > 0) {
371ae8c6e27Sflorian 
372ae8c6e27Sflorian 		/* skip spaces */
373ae8c6e27Sflorian 		while(sldns_buffer_remaining(strbuf) > 0 &&
374ae8c6e27Sflorian 			*(sldns_buffer_current(strbuf)) == ' ') {
375ae8c6e27Sflorian 			sldns_buffer_skip(strbuf, 1);
376ae8c6e27Sflorian 		}
377ae8c6e27Sflorian 
378ae8c6e27Sflorian 		if(sldns_buffer_remaining(strbuf) > 0 &&
379ae8c6e27Sflorian 			*(sldns_buffer_current(strbuf)) == '\"') {
380ae8c6e27Sflorian 			*delimiters = "\"\0";
381ae8c6e27Sflorian 			sldns_buffer_skip(strbuf, 1);
382ae8c6e27Sflorian 			return 1;
383ae8c6e27Sflorian 		}
384ae8c6e27Sflorian 	}
385ae8c6e27Sflorian 	return 0;
386ae8c6e27Sflorian }
387ae8c6e27Sflorian 
388ae8c6e27Sflorian /** spool hex data into rdata */
389ae8c6e27Sflorian static int
390ae8c6e27Sflorian rrinternal_spool_hex(char* token, uint8_t* rr, size_t rr_len,
391ae8c6e27Sflorian 	size_t rr_cur_len, size_t* cur_hex_data_size, size_t hex_data_size)
392ae8c6e27Sflorian {
393ae8c6e27Sflorian 	char* p = token;
394ae8c6e27Sflorian 	while(*p) {
395ae8c6e27Sflorian 		if(isspace((unsigned char)*p)) {
396ae8c6e27Sflorian 			p++;
397ae8c6e27Sflorian 			continue;
398ae8c6e27Sflorian 		}
399ae8c6e27Sflorian 		if(!isxdigit((unsigned char)*p))
400ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_RDATA,
401ae8c6e27Sflorian 				p-token);
402ae8c6e27Sflorian 		if(*cur_hex_data_size >= hex_data_size)
403ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_RDATA,
404ae8c6e27Sflorian 				p-token);
405ae8c6e27Sflorian 		/* extra robust check */
406ae8c6e27Sflorian 		if(rr_cur_len+(*cur_hex_data_size)/2 >= rr_len)
407ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
408ae8c6e27Sflorian 				p-token);
409ae8c6e27Sflorian 		/* see if 16s or 1s */
410ae8c6e27Sflorian 		if( ((*cur_hex_data_size)&1) == 0) {
411ae8c6e27Sflorian 			rr[rr_cur_len+(*cur_hex_data_size)/2] =
412ae8c6e27Sflorian 				(uint8_t)sldns_hexdigit_to_int(*p)*16;
413ae8c6e27Sflorian 		} else {
414ae8c6e27Sflorian 			rr[rr_cur_len+(*cur_hex_data_size)/2] +=
415ae8c6e27Sflorian 				(uint8_t)sldns_hexdigit_to_int(*p);
416ae8c6e27Sflorian 		}
417ae8c6e27Sflorian 		p++;
418ae8c6e27Sflorian 		(*cur_hex_data_size)++;
419ae8c6e27Sflorian 	}
420ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
421ae8c6e27Sflorian }
422ae8c6e27Sflorian 
423ae8c6e27Sflorian /** read unknown rr type format */
424ae8c6e27Sflorian static int
425ae8c6e27Sflorian rrinternal_parse_unknown(sldns_buffer* strbuf, char* token, size_t token_len,
426ae8c6e27Sflorian         uint8_t* rr, size_t* rr_len, size_t* rr_cur_len, size_t pre_data_pos)
427ae8c6e27Sflorian {
428ae8c6e27Sflorian 	const char* delim = "\n\t ";
429ae8c6e27Sflorian 	size_t hex_data_size, cur_hex_data_size;
430ae8c6e27Sflorian 	/* go back to before \#
431ae8c6e27Sflorian 	 * and skip it while setting delimiters better
432ae8c6e27Sflorian 	 */
433ae8c6e27Sflorian 	sldns_buffer_set_position(strbuf, pre_data_pos);
434ae8c6e27Sflorian 	if(sldns_bget_token(strbuf, token, delim, token_len) == -1)
435ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_GENERAL; /* should not fail */
436ae8c6e27Sflorian 	/* read rdata octet length */
437ae8c6e27Sflorian 	if(sldns_bget_token(strbuf, token, delim, token_len) == -1) {
438ae8c6e27Sflorian 		/* something goes very wrong here */
439ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_RDATA,
440ae8c6e27Sflorian 			sldns_buffer_position(strbuf));
441ae8c6e27Sflorian 	}
442ae8c6e27Sflorian 	hex_data_size = (size_t)atoi(token);
443ae8c6e27Sflorian 	if(hex_data_size > LDNS_MAX_RDFLEN ||
444ae8c6e27Sflorian 		*rr_cur_len + hex_data_size > *rr_len) {
445ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
446ae8c6e27Sflorian 			sldns_buffer_position(strbuf));
447ae8c6e27Sflorian 	}
448ae8c6e27Sflorian 	/* copy hex chars into hex str (2 chars per byte) */
449ae8c6e27Sflorian 	hex_data_size *= 2;
450ae8c6e27Sflorian 	cur_hex_data_size = 0;
451ae8c6e27Sflorian 	while(cur_hex_data_size < hex_data_size) {
452ae8c6e27Sflorian 		int status;
453ae8c6e27Sflorian 		ssize_t c = sldns_bget_token(strbuf, token, delim, token_len);
454ae8c6e27Sflorian 		if((status = rrinternal_spool_hex(token, rr, *rr_len,
455ae8c6e27Sflorian 			*rr_cur_len, &cur_hex_data_size, hex_data_size)) != 0)
456ae8c6e27Sflorian 			return RET_ERR_SHIFT(status,
457ae8c6e27Sflorian 				sldns_buffer_position(strbuf)-strlen(token));
458ae8c6e27Sflorian 		if(c == -1) {
459ae8c6e27Sflorian 			if(cur_hex_data_size != hex_data_size)
460ae8c6e27Sflorian 				return RET_ERR(
461ae8c6e27Sflorian 					LDNS_WIREPARSE_ERR_SYNTAX_RDATA,
462ae8c6e27Sflorian 					sldns_buffer_position(strbuf));
463ae8c6e27Sflorian 			break;
464ae8c6e27Sflorian 		}
465ae8c6e27Sflorian 	}
466ae8c6e27Sflorian 	*rr_cur_len += hex_data_size/2;
467ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
468ae8c6e27Sflorian }
469ae8c6e27Sflorian 
470ae8c6e27Sflorian /** parse normal RR rdata element */
471ae8c6e27Sflorian static int
472ae8c6e27Sflorian rrinternal_parse_rdf(sldns_buffer* strbuf, char* token, size_t token_len,
473ae8c6e27Sflorian 	uint8_t* rr, size_t rr_len, size_t* rr_cur_len, sldns_rdf_type rdftype,
474ae8c6e27Sflorian 	uint16_t rr_type, size_t r_cnt, size_t r_max, size_t dname_len,
475ae8c6e27Sflorian 	uint8_t* origin, size_t origin_len)
476ae8c6e27Sflorian {
477ae8c6e27Sflorian 	size_t len;
478ae8c6e27Sflorian 	int status;
479ae8c6e27Sflorian 
480ae8c6e27Sflorian 	switch(rdftype) {
481ae8c6e27Sflorian 	case LDNS_RDF_TYPE_DNAME:
482ae8c6e27Sflorian 		/* check if the origin should be used or concatenated */
483ae8c6e27Sflorian 		if(strcmp(token, "@") == 0) {
484ae8c6e27Sflorian 			uint8_t* tocopy;
485ae8c6e27Sflorian 			size_t copylen;
486ae8c6e27Sflorian 			if(origin) {
487ae8c6e27Sflorian 				copylen = origin_len;
488ae8c6e27Sflorian 				tocopy = origin;
489ae8c6e27Sflorian 			} else if(rr_type == LDNS_RR_TYPE_SOA) {
490ae8c6e27Sflorian 				copylen = dname_len;
491ae8c6e27Sflorian 				tocopy = rr; /* copy rr owner name */
492ae8c6e27Sflorian 			} else {
493ae8c6e27Sflorian 				copylen = 1;
494ae8c6e27Sflorian 				tocopy = (uint8_t*)"\0";
495ae8c6e27Sflorian 			}
496ae8c6e27Sflorian 			if((*rr_cur_len) + copylen > rr_len)
497ae8c6e27Sflorian 				return RET_ERR(
498ae8c6e27Sflorian 					LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
499ae8c6e27Sflorian 					sldns_buffer_position(strbuf));
500ae8c6e27Sflorian 			memmove(rr+*rr_cur_len, tocopy, copylen);
501ae8c6e27Sflorian 			(*rr_cur_len) += copylen;
502ae8c6e27Sflorian 		} else {
503ae8c6e27Sflorian 			size_t dlen = rr_len - (*rr_cur_len);
504ae8c6e27Sflorian 			int s = sldns_str2wire_dname_buf_origin(token,
505ae8c6e27Sflorian 				rr+*rr_cur_len, &dlen, origin, origin_len);
506ae8c6e27Sflorian 			if(s) return RET_ERR_SHIFT(s,
507ae8c6e27Sflorian 				sldns_buffer_position(strbuf)-strlen(token));
508ae8c6e27Sflorian 			(*rr_cur_len) += dlen;
509ae8c6e27Sflorian 		}
510ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_OK;
511ae8c6e27Sflorian 
512ae8c6e27Sflorian 	case LDNS_RDF_TYPE_HEX:
513ae8c6e27Sflorian 	case LDNS_RDF_TYPE_B64:
514ae8c6e27Sflorian 		/* When this is the last rdata field, then the
515ae8c6e27Sflorian 		 * rest should be read in (cause then these
516ae8c6e27Sflorian 		 * rdf types may contain spaces). */
517ae8c6e27Sflorian 		if(r_cnt == r_max - 1) {
518ae8c6e27Sflorian 			size_t tlen = strlen(token);
519ae8c6e27Sflorian 			(void)sldns_bget_token(strbuf, token+tlen, "\n",
520ae8c6e27Sflorian 				token_len - tlen);
521ae8c6e27Sflorian 		}
522ae8c6e27Sflorian 		break;
523ae8c6e27Sflorian 	default:
524ae8c6e27Sflorian 		break;
525ae8c6e27Sflorian 	}
526ae8c6e27Sflorian 
527ae8c6e27Sflorian 	len = rr_len - (*rr_cur_len);
528ae8c6e27Sflorian 	if((status=sldns_str2wire_rdf_buf(token, rr+(*rr_cur_len), &len,
529ae8c6e27Sflorian 		rdftype)) != 0)
530ae8c6e27Sflorian 		return RET_ERR_SHIFT(status,
531ae8c6e27Sflorian 			sldns_buffer_position(strbuf)-strlen(token));
532ae8c6e27Sflorian 	*rr_cur_len += len;
533ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
534ae8c6e27Sflorian }
535ae8c6e27Sflorian 
536ae8c6e27Sflorian /**
537ae8c6e27Sflorian  * Parse one rdf token.  Takes care of quotes and parenthesis.
538ae8c6e27Sflorian  */
539ae8c6e27Sflorian static int
540ae8c6e27Sflorian sldns_parse_rdf_token(sldns_buffer* strbuf, char* token, size_t token_len,
541ae8c6e27Sflorian 	int* quoted, int* parens, size_t* pre_data_pos,
542ae8c6e27Sflorian 	const char* delimiters, sldns_rdf_type rdftype, size_t* token_strlen)
543ae8c6e27Sflorian {
544ae8c6e27Sflorian 	size_t slen;
545ae8c6e27Sflorian 
546ae8c6e27Sflorian 	/* skip spaces */
547ae8c6e27Sflorian 	while(sldns_buffer_remaining(strbuf) > 0 && !*quoted &&
548ae8c6e27Sflorian 		*(sldns_buffer_current(strbuf)) == ' ') {
549ae8c6e27Sflorian 		sldns_buffer_skip(strbuf, 1);
550ae8c6e27Sflorian 	}
551ae8c6e27Sflorian 
552ae8c6e27Sflorian 	*pre_data_pos = sldns_buffer_position(strbuf);
553ae8c6e27Sflorian 	if(sldns_bget_token_par(strbuf, token, (*quoted)?"\"":delimiters,
554ae8c6e27Sflorian 		token_len, parens, (*quoted)?NULL:" \t") == -1) {
555ae8c6e27Sflorian 		return 0;
556ae8c6e27Sflorian 	}
557ae8c6e27Sflorian 	slen = strlen(token);
558ae8c6e27Sflorian 	/* check if not quoted yet, and we have encountered quotes */
559ae8c6e27Sflorian 	if(!*quoted && sldns_rdf_type_maybe_quoted(rdftype) &&
560ae8c6e27Sflorian 		slen >= 2 &&
561ae8c6e27Sflorian 		(token[0] == '"' || token[0] == '\'') &&
562ae8c6e27Sflorian 		(token[slen-1] == '"' || token[slen-1] == '\'')) {
563ae8c6e27Sflorian 		/* move token two smaller (quotes) with endnull */
564ae8c6e27Sflorian 		memmove(token, token+1, slen-2);
565ae8c6e27Sflorian 		token[slen-2] = 0;
566ae8c6e27Sflorian 		slen -= 2;
567ae8c6e27Sflorian 		*quoted = 1;
568ae8c6e27Sflorian 	} else if(!*quoted && sldns_rdf_type_maybe_quoted(rdftype) &&
569ae8c6e27Sflorian 		slen >= 2 &&
570ae8c6e27Sflorian 		(token[0] == '"' || token[0] == '\'')) {
571ae8c6e27Sflorian 		/* got the start quote (remove it) but read remainder
572ae8c6e27Sflorian 		 * of quoted string as well into remainder of token */
573ae8c6e27Sflorian 		memmove(token, token+1, slen-1);
574ae8c6e27Sflorian 		token[slen-1] = 0;
575ae8c6e27Sflorian 		slen -= 1;
576ae8c6e27Sflorian 		*quoted = 1;
577ae8c6e27Sflorian 		/* rewind buffer over skipped whitespace */
578ae8c6e27Sflorian 		while(sldns_buffer_position(strbuf) > 0 &&
579ae8c6e27Sflorian 			(sldns_buffer_current(strbuf)[-1] == ' ' ||
580ae8c6e27Sflorian 			sldns_buffer_current(strbuf)[-1] == '\t')) {
581ae8c6e27Sflorian 			sldns_buffer_skip(strbuf, -1);
582ae8c6e27Sflorian 		}
583ae8c6e27Sflorian 		if(sldns_bget_token_par(strbuf, token+slen,
584ae8c6e27Sflorian 			"\"", token_len-slen,
585ae8c6e27Sflorian 			parens, NULL) == -1) {
586ae8c6e27Sflorian 			return 0;
587ae8c6e27Sflorian 		}
588ae8c6e27Sflorian 		slen = strlen(token);
589ae8c6e27Sflorian 	}
590ae8c6e27Sflorian 	*token_strlen = slen;
591ae8c6e27Sflorian 	return 1;
592ae8c6e27Sflorian }
593ae8c6e27Sflorian 
594ae8c6e27Sflorian /** Add space and one more rdf token onto the existing token string. */
595ae8c6e27Sflorian static int
596ae8c6e27Sflorian sldns_affix_token(sldns_buffer* strbuf, char* token, size_t* token_len,
597ae8c6e27Sflorian 	int* quoted, int* parens, size_t* pre_data_pos,
598ae8c6e27Sflorian 	const char* delimiters, sldns_rdf_type rdftype, size_t* token_strlen)
599ae8c6e27Sflorian {
600ae8c6e27Sflorian 	size_t addlen = *token_len - *token_strlen;
601ae8c6e27Sflorian 	size_t addstrlen = 0;
602ae8c6e27Sflorian 
603ae8c6e27Sflorian 	/* add space */
604ae8c6e27Sflorian 	if(addlen < 1) return 0;
605ae8c6e27Sflorian 	token[*token_strlen] = ' ';
606ae8c6e27Sflorian 	token[++(*token_strlen)] = 0;
607ae8c6e27Sflorian 
608ae8c6e27Sflorian 	/* read another token */
609ae8c6e27Sflorian 	addlen = *token_len - *token_strlen;
610ae8c6e27Sflorian 	if(!sldns_parse_rdf_token(strbuf, token+*token_strlen, addlen, quoted,
611ae8c6e27Sflorian 		parens, pre_data_pos, delimiters, rdftype, &addstrlen))
612ae8c6e27Sflorian 		return 0;
613ae8c6e27Sflorian 	(*token_strlen) += addstrlen;
614ae8c6e27Sflorian 	return 1;
615ae8c6e27Sflorian }
616ae8c6e27Sflorian 
617411c5950Sflorian static int sldns_str2wire_svcparam_key_cmp(const void *a, const void *b)
618411c5950Sflorian {
619411c5950Sflorian 	return sldns_read_uint16(*(uint8_t**) a)
620411c5950Sflorian 	     - sldns_read_uint16(*(uint8_t**) b);
621411c5950Sflorian }
622411c5950Sflorian 
623411c5950Sflorian /**
624411c5950Sflorian  * Add constraints to the SVCB RRs which involve the whole set
625411c5950Sflorian  */
626411c5950Sflorian static int sldns_str2wire_check_svcbparams(uint8_t* rdata, uint16_t rdata_len)
627411c5950Sflorian {
628411c5950Sflorian 	size_t   nparams = 0, i;
629411c5950Sflorian 	uint8_t  new_rdata[LDNS_MAX_RDFLEN];
630411c5950Sflorian 	uint8_t* new_rdata_ptr = new_rdata;
631411c5950Sflorian 	uint8_t* svcparams[MAX_NUMBER_OF_SVCPARAMS];
632411c5950Sflorian 	uint8_t* rdata_ptr = rdata;
633411c5950Sflorian 	uint16_t rdata_remaining = rdata_len;
634411c5950Sflorian 
635411c5950Sflorian 	/* find the SvcParams */
636411c5950Sflorian 	while (rdata_remaining) {
637411c5950Sflorian 		uint16_t svcbparam_len;
638411c5950Sflorian 
639411c5950Sflorian 		svcparams[nparams] = rdata_ptr;
640411c5950Sflorian 		if (rdata_remaining < 4)
641411c5950Sflorian 			return LDNS_WIREPARSE_ERR_SVCPARAM_BROKEN_RDATA;
642411c5950Sflorian 		svcbparam_len = sldns_read_uint16(rdata_ptr + 2);
643411c5950Sflorian 		rdata_remaining -= 4;
644411c5950Sflorian 		rdata_ptr += 4;
645411c5950Sflorian 
646411c5950Sflorian 		if (rdata_remaining < svcbparam_len)
647411c5950Sflorian 			return LDNS_WIREPARSE_ERR_SVCPARAM_BROKEN_RDATA;
648411c5950Sflorian 		rdata_remaining -= svcbparam_len;
649411c5950Sflorian 		rdata_ptr += svcbparam_len;
650411c5950Sflorian 
651411c5950Sflorian 		nparams += 1;
652411c5950Sflorian 		if (nparams >= MAX_NUMBER_OF_SVCPARAMS)
653411c5950Sflorian 			return LDNS_WIREPARSE_ERR_SVCB_TOO_MANY_PARAMS;
654411c5950Sflorian 	}
655411c5950Sflorian 
656411c5950Sflorian 	/* In draft-ietf-dnsop-svcb-https-06 Section 7:
657411c5950Sflorian 	 *
658411c5950Sflorian 	 *     In wire format, the keys are represented by their numeric
659411c5950Sflorian 	 *     values in network byte order, concatenated in ascending order.
660411c5950Sflorian 	 */
661411c5950Sflorian 	qsort((void *)svcparams
662411c5950Sflorian 	     ,nparams
663411c5950Sflorian 	     ,sizeof(uint8_t*)
664411c5950Sflorian 	     ,sldns_str2wire_svcparam_key_cmp);
665411c5950Sflorian 
666411c5950Sflorian 
667411c5950Sflorian 	/* The code below revolves around sematic errors in the SVCParam set.
668411c5950Sflorian 	 * So long as we do not distinguish between running Unbound as a primary
669411c5950Sflorian 	 * or as a secondary, we default to secondary behavior and we ignore the
670411c5950Sflorian 	 * sematic errors. */
671411c5950Sflorian 
672411c5950Sflorian #ifdef SVCB_SEMANTIC_ERRORS
673411c5950Sflorian 	{
674411c5950Sflorian 		uint8_t* mandatory = NULL;
675411c5950Sflorian 		/* In draft-ietf-dnsop-svcb-https-06 Section 7:
676411c5950Sflorian 		 *
677411c5950Sflorian 		 *     Keys (...) MUST NOT appear more than once.
678411c5950Sflorian 		 *
679411c5950Sflorian 		 * If they key has already been seen, we have a duplicate
680411c5950Sflorian 		 */
681411c5950Sflorian 		for(i=0; i < nparams; i++) {
682411c5950Sflorian 			uint16_t key = sldns_read_uint16(svcparams[i]);
683411c5950Sflorian 			if(i + 1 < nparams && key == sldns_read_uint16(svcparams[i+1]))
684411c5950Sflorian 				return LDNS_WIREPARSE_ERR_SVCB_DUPLICATE_KEYS;
685411c5950Sflorian 			if(key == SVCB_KEY_MANDATORY)
686411c5950Sflorian 				mandatory = svcparams[i];
687411c5950Sflorian 		}
688411c5950Sflorian 
689411c5950Sflorian 		/* 4. verify that all the SvcParamKeys in mandatory are present */
690411c5950Sflorian 		if(mandatory) {
691411c5950Sflorian 			/* Divide by sizeof(uint16_t)*/
692411c5950Sflorian 			uint16_t mandatory_nkeys = sldns_read_uint16(mandatory + 2) / sizeof(uint16_t);
693411c5950Sflorian 
694411c5950Sflorian 			/* Guaranteed by sldns_str2wire_svcparam_key_value */
695411c5950Sflorian 			assert(mandatory_nkeys > 0);
696411c5950Sflorian 
697411c5950Sflorian 			for(i=0; i < mandatory_nkeys; i++) {
698411c5950Sflorian 				uint16_t mandatory_key = sldns_read_uint16(
699411c5950Sflorian 					mandatory
700411c5950Sflorian 					+ 2 * sizeof(uint16_t)
701411c5950Sflorian 					+ i * sizeof(uint16_t));
702411c5950Sflorian 				uint8_t found = 0;
703411c5950Sflorian 				size_t j;
704411c5950Sflorian 
705411c5950Sflorian 				for(j=0; j < nparams; j++) {
706411c5950Sflorian 					if(mandatory_key == sldns_read_uint16(svcparams[j])) {
707411c5950Sflorian 						found = 1;
708411c5950Sflorian 						break;
709411c5950Sflorian 					}
710411c5950Sflorian 				}
711411c5950Sflorian 
712411c5950Sflorian 				if(!found)
713411c5950Sflorian 					return LDNS_WIREPARSE_ERR_SVCB_MANDATORY_MISSING_PARAM;
714411c5950Sflorian 			}
715411c5950Sflorian 		}
716411c5950Sflorian 	}
717411c5950Sflorian #endif
718411c5950Sflorian 	/* Write rdata in correct order */
719411c5950Sflorian 	for (i = 0; i < nparams; i++) {
720411c5950Sflorian 		uint16_t svcparam_len = sldns_read_uint16(svcparams[i] + 2)
721411c5950Sflorian 		                      + 2 * sizeof(uint16_t);
722411c5950Sflorian 
723411c5950Sflorian 		if ((unsigned)(new_rdata_ptr - new_rdata) + svcparam_len > sizeof(new_rdata))
724411c5950Sflorian 			return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
725411c5950Sflorian 
726411c5950Sflorian 		memcpy(new_rdata_ptr, svcparams[i], svcparam_len);
727411c5950Sflorian 		new_rdata_ptr += svcparam_len;
728411c5950Sflorian 	}
729411c5950Sflorian 	memcpy(rdata, new_rdata, rdata_len);
730411c5950Sflorian 	return LDNS_WIREPARSE_ERR_OK;
731411c5950Sflorian }
732411c5950Sflorian 
733ae8c6e27Sflorian /** parse rdata from string into rr buffer(-remainder after dname). */
734ae8c6e27Sflorian static int
735ae8c6e27Sflorian rrinternal_parse_rdata(sldns_buffer* strbuf, char* token, size_t token_len,
736ae8c6e27Sflorian 	uint8_t* rr, size_t* rr_len, size_t dname_len, uint16_t rr_type,
737ae8c6e27Sflorian 	uint8_t* origin, size_t origin_len)
738ae8c6e27Sflorian {
739ae8c6e27Sflorian 	const sldns_rr_descriptor *desc = sldns_rr_descript((uint16_t)rr_type);
740ae8c6e27Sflorian 	size_t r_cnt, r_min, r_max;
741ae8c6e27Sflorian 	size_t rr_cur_len = dname_len + 10, pre_data_pos, token_strlen;
742ae8c6e27Sflorian 	int was_unknown_rr_format = 0, parens = 0, status, quoted;
743ae8c6e27Sflorian 	const char* delimiters;
744ae8c6e27Sflorian 	sldns_rdf_type rdftype;
745ae8c6e27Sflorian 	/* a desc is always returned */
746ae8c6e27Sflorian 	if(!desc) return LDNS_WIREPARSE_ERR_GENERAL;
747ae8c6e27Sflorian 	r_max = sldns_rr_descriptor_maximum(desc);
748ae8c6e27Sflorian 	r_min = sldns_rr_descriptor_minimum(desc);
749ae8c6e27Sflorian 	/* robust check */
750ae8c6e27Sflorian 	if(rr_cur_len > *rr_len)
751ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
752ae8c6e27Sflorian 			sldns_buffer_position(strbuf));
753ae8c6e27Sflorian 
754ae8c6e27Sflorian 	/* because number of fields can be variable, we can't rely on
755ae8c6e27Sflorian 	 * _maximum() only */
756ae8c6e27Sflorian 	for(r_cnt=0; r_cnt < r_max; r_cnt++) {
757ae8c6e27Sflorian 		rdftype = sldns_rr_descriptor_field_type(desc, r_cnt);
758ae8c6e27Sflorian 		delimiters = rrinternal_get_delims(rdftype, r_cnt, r_max);
759ae8c6e27Sflorian 		quoted = rrinternal_get_quoted(strbuf, &delimiters, rdftype);
760ae8c6e27Sflorian 
761ae8c6e27Sflorian 		if(!sldns_parse_rdf_token(strbuf, token, token_len, &quoted,
762ae8c6e27Sflorian 			&parens, &pre_data_pos, delimiters, rdftype,
763ae8c6e27Sflorian 			&token_strlen))
764ae8c6e27Sflorian 			break;
765ae8c6e27Sflorian 
766ae8c6e27Sflorian 		/* rfc3597 specifies that any type can be represented
767ae8c6e27Sflorian 		 * with \# method, which can contain spaces...
768ae8c6e27Sflorian 		 * it does specify size though... */
769ae8c6e27Sflorian 
770ae8c6e27Sflorian 		/* unknown RR data */
771ae8c6e27Sflorian 		if(token_strlen>=2 && strncmp(token, "\\#", 2) == 0 &&
772ae8c6e27Sflorian 			!quoted && (token_strlen == 2 || token[2]==' ')) {
773ae8c6e27Sflorian 			was_unknown_rr_format = 1;
774ae8c6e27Sflorian 			if((status=rrinternal_parse_unknown(strbuf, token,
775ae8c6e27Sflorian 				token_len, rr, rr_len, &rr_cur_len,
776ae8c6e27Sflorian 				pre_data_pos)) != 0)
777ae8c6e27Sflorian 				return status;
778ae8c6e27Sflorian 		} else if(token_strlen > 0 || quoted) {
779ae8c6e27Sflorian 			if(rdftype == LDNS_RDF_TYPE_HIP) {
780ae8c6e27Sflorian 				/* affix the HIT and PK fields, with a space */
781ae8c6e27Sflorian 				if(!sldns_affix_token(strbuf, token,
782ae8c6e27Sflorian 					&token_len, &quoted, &parens,
783ae8c6e27Sflorian 					&pre_data_pos, delimiters,
784ae8c6e27Sflorian 					rdftype, &token_strlen))
785ae8c6e27Sflorian 					break;
786ae8c6e27Sflorian 				if(!sldns_affix_token(strbuf, token,
787ae8c6e27Sflorian 					&token_len, &quoted, &parens,
788ae8c6e27Sflorian 					&pre_data_pos, delimiters,
789ae8c6e27Sflorian 					rdftype, &token_strlen))
790ae8c6e27Sflorian 					break;
791ae8c6e27Sflorian 			} else if(rdftype == LDNS_RDF_TYPE_INT16_DATA &&
792ae8c6e27Sflorian 				strcmp(token, "0")!=0) {
793ae8c6e27Sflorian 				/* affix len and b64 fields */
794ae8c6e27Sflorian 				if(!sldns_affix_token(strbuf, token,
795ae8c6e27Sflorian 					&token_len, &quoted, &parens,
796ae8c6e27Sflorian 					&pre_data_pos, delimiters,
797ae8c6e27Sflorian 					rdftype, &token_strlen))
798ae8c6e27Sflorian 					break;
799ae8c6e27Sflorian 			}
800ae8c6e27Sflorian 
801ae8c6e27Sflorian 			/* normal RR */
802ae8c6e27Sflorian 			if((status=rrinternal_parse_rdf(strbuf, token,
803ae8c6e27Sflorian 				token_len, rr, *rr_len, &rr_cur_len, rdftype,
804ae8c6e27Sflorian 				rr_type, r_cnt, r_max, dname_len, origin,
805ae8c6e27Sflorian 				origin_len)) != 0) {
806ae8c6e27Sflorian 				return status;
807ae8c6e27Sflorian 			}
808ae8c6e27Sflorian 		}
809ae8c6e27Sflorian 	}
810ae8c6e27Sflorian 	if(!was_unknown_rr_format && r_cnt+1 < r_min) {
811ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_MISSING_VALUE,
812ae8c6e27Sflorian 			sldns_buffer_position(strbuf));
813ae8c6e27Sflorian 	}
814ae8c6e27Sflorian 	while(parens != 0) {
815ae8c6e27Sflorian 		/* read remainder, must be "" */
816ae8c6e27Sflorian 		if(sldns_bget_token_par(strbuf, token, "\n", token_len,
817ae8c6e27Sflorian 			&parens, " \t") == -1) {
818ae8c6e27Sflorian 			if(parens != 0)
819ae8c6e27Sflorian 				return RET_ERR(LDNS_WIREPARSE_ERR_PARENTHESIS,
820ae8c6e27Sflorian 					sldns_buffer_position(strbuf));
821ae8c6e27Sflorian 			break;
822ae8c6e27Sflorian 		}
823ae8c6e27Sflorian 		if(strcmp(token, "") != 0)
824ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_PARENTHESIS,
825ae8c6e27Sflorian 				sldns_buffer_position(strbuf));
826ae8c6e27Sflorian 	}
827ae8c6e27Sflorian 	/* write rdata length */
828ae8c6e27Sflorian 	sldns_write_uint16(rr+dname_len+8, (uint16_t)(rr_cur_len-dname_len-10));
829ae8c6e27Sflorian 	*rr_len = rr_cur_len;
830411c5950Sflorian 	/* SVCB/HTTPS handling  */
831411c5950Sflorian 	if (rr_type == LDNS_RR_TYPE_SVCB || rr_type == LDNS_RR_TYPE_HTTPS) {
832411c5950Sflorian 		size_t rdata_len = rr_cur_len - dname_len - 10;
833411c5950Sflorian 		uint8_t *rdata = rr+dname_len + 10;
834411c5950Sflorian 
835411c5950Sflorian 		/* skip 1st rdata field SvcPriority (uint16_t) */
836411c5950Sflorian 		if (rdata_len < sizeof(uint16_t))
837411c5950Sflorian 			return LDNS_WIREPARSE_ERR_OK;
838411c5950Sflorian 
839411c5950Sflorian 		rdata_len -= sizeof(uint16_t);
840411c5950Sflorian 		rdata += sizeof(uint16_t);
841411c5950Sflorian 
842411c5950Sflorian 		/* skip 2nd rdata field dname */
843411c5950Sflorian 		while (rdata_len && *rdata != 0) {
844411c5950Sflorian 			uint8_t label_len;
845411c5950Sflorian 
846411c5950Sflorian 			if (*rdata & 0xC0)
847411c5950Sflorian 				return LDNS_WIREPARSE_ERR_OK;
848411c5950Sflorian 
849411c5950Sflorian 			label_len = *rdata + 1;
850411c5950Sflorian 			if (rdata_len < label_len)
851411c5950Sflorian 				return LDNS_WIREPARSE_ERR_OK;
852411c5950Sflorian 
853411c5950Sflorian 			rdata_len -= label_len;
854411c5950Sflorian 			rdata += label_len;
855411c5950Sflorian 		}
856411c5950Sflorian 		/* The root label is one more character, so smaller
857411c5950Sflorian 		 * than 1 + 1 means no Svcparam Keys */
858411c5950Sflorian 		if (rdata_len < 2 || *rdata != 0)
859411c5950Sflorian 			return LDNS_WIREPARSE_ERR_OK;
860411c5950Sflorian 
861411c5950Sflorian 		rdata_len -= 1;
862411c5950Sflorian 		rdata += 1;
863411c5950Sflorian 		return sldns_str2wire_check_svcbparams(rdata, rdata_len);
864411c5950Sflorian 
865411c5950Sflorian 	}
866ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
867ae8c6e27Sflorian }
868ae8c6e27Sflorian 
869ae8c6e27Sflorian /*
870ae8c6e27Sflorian  * trailing spaces are allowed
871ae8c6e27Sflorian  * leading spaces are not allowed
872ae8c6e27Sflorian  * allow ttl to be optional
873ae8c6e27Sflorian  * class is optional too
874ae8c6e27Sflorian  * if ttl is missing, and default_ttl is 0, use DEF_TTL
875ae8c6e27Sflorian  * allow ttl to be written as 1d3h
876ae8c6e27Sflorian  * So the RR should look like. e.g.
877ae8c6e27Sflorian  * miek.nl. 3600 IN MX 10 elektron.atoom.net
878ae8c6e27Sflorian  * or
879ae8c6e27Sflorian  * miek.nl. 1h IN MX 10 elektron.atoom.net
880ae8c6e27Sflorian  * or
881ae8c6e27Sflorian  * miek.nl. IN MX 10 elektron.atoom.net
882ae8c6e27Sflorian  */
883ae8c6e27Sflorian static int
884ae8c6e27Sflorian sldns_str2wire_rr_buf_internal(const char* str, uint8_t* rr, size_t* len,
885ae8c6e27Sflorian 	size_t* dname_len, uint32_t default_ttl, uint8_t* origin,
886ae8c6e27Sflorian 	size_t origin_len, uint8_t* prev, size_t prev_len, int question)
887ae8c6e27Sflorian {
888ae8c6e27Sflorian 	int status;
889ae8c6e27Sflorian 	int not_there = 0;
890ae8c6e27Sflorian 	char token[LDNS_MAX_RDFLEN+1];
891ae8c6e27Sflorian 	uint32_t ttl = 0;
892ae8c6e27Sflorian 	uint16_t tp = 0, cl = 0;
893ae8c6e27Sflorian 	size_t ddlen = 0;
894ae8c6e27Sflorian 
895ae8c6e27Sflorian 	/* string in buffer */
896ae8c6e27Sflorian 	sldns_buffer strbuf;
897ae8c6e27Sflorian 	sldns_buffer_init_frm_data(&strbuf, (uint8_t*)str, strlen(str));
898ae8c6e27Sflorian 	if(!dname_len) dname_len = &ddlen;
899ae8c6e27Sflorian 
900ae8c6e27Sflorian 	/* parse the owner */
901ae8c6e27Sflorian 	if((status=rrinternal_get_owner(&strbuf, rr, len, dname_len, origin,
902ae8c6e27Sflorian 		origin_len, prev, prev_len, token, sizeof(token))) != 0)
903ae8c6e27Sflorian 		return status;
904ae8c6e27Sflorian 
905ae8c6e27Sflorian 	/* parse the [ttl] [class] <type> */
906ae8c6e27Sflorian 	if((status=rrinternal_get_ttl(&strbuf, token, sizeof(token),
907ae8c6e27Sflorian 		&not_there, &ttl, default_ttl)) != 0)
908ae8c6e27Sflorian 		return status;
909ae8c6e27Sflorian 	if((status=rrinternal_get_class(&strbuf, token, sizeof(token),
910ae8c6e27Sflorian 		&not_there, &cl)) != 0)
911ae8c6e27Sflorian 		return status;
912ae8c6e27Sflorian 	if((status=rrinternal_get_type(&strbuf, token, sizeof(token),
913ae8c6e27Sflorian 		&not_there, &tp)) != 0)
914ae8c6e27Sflorian 		return status;
915ae8c6e27Sflorian 	/* put ttl, class, type into the rr result */
916ae8c6e27Sflorian 	if((status=rrinternal_write_typeclassttl(&strbuf, rr, *len, *dname_len, tp, cl,
917ae8c6e27Sflorian 		ttl, question)) != 0)
918ae8c6e27Sflorian 		return status;
919ae8c6e27Sflorian 	/* for a question-RR we are done, no rdata */
920ae8c6e27Sflorian 	if(question) {
921ae8c6e27Sflorian 		*len = *dname_len + 4;
922ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_OK;
923ae8c6e27Sflorian 	}
924ae8c6e27Sflorian 
925ae8c6e27Sflorian 	/* rdata */
926ae8c6e27Sflorian 	if((status=rrinternal_parse_rdata(&strbuf, token, sizeof(token),
927ae8c6e27Sflorian 		rr, len, *dname_len, tp, origin, origin_len)) != 0)
928ae8c6e27Sflorian 		return status;
929ae8c6e27Sflorian 
930ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
931ae8c6e27Sflorian }
932ae8c6e27Sflorian 
933ae8c6e27Sflorian int sldns_str2wire_rr_buf(const char* str, uint8_t* rr, size_t* len,
934ae8c6e27Sflorian 	size_t* dname_len, uint32_t default_ttl, uint8_t* origin,
935ae8c6e27Sflorian 	size_t origin_len, uint8_t* prev, size_t prev_len)
936ae8c6e27Sflorian {
937ae8c6e27Sflorian 	return sldns_str2wire_rr_buf_internal(str, rr, len, dname_len,
938ae8c6e27Sflorian 		default_ttl, origin, origin_len, prev, prev_len, 0);
939ae8c6e27Sflorian }
940ae8c6e27Sflorian 
941ae8c6e27Sflorian int sldns_str2wire_rr_question_buf(const char* str, uint8_t* rr, size_t* len,
942ae8c6e27Sflorian 	size_t* dname_len, uint8_t* origin, size_t origin_len, uint8_t* prev,
943ae8c6e27Sflorian 	size_t prev_len)
944ae8c6e27Sflorian {
945ae8c6e27Sflorian 	return sldns_str2wire_rr_buf_internal(str, rr, len, dname_len,
946ae8c6e27Sflorian 		0, origin, origin_len, prev, prev_len, 1);
947ae8c6e27Sflorian }
948ae8c6e27Sflorian 
949ae8c6e27Sflorian uint16_t sldns_wirerr_get_type(uint8_t* rr, size_t len, size_t dname_len)
950ae8c6e27Sflorian {
951ae8c6e27Sflorian 	if(len < dname_len+2)
952ae8c6e27Sflorian 		return 0;
953ae8c6e27Sflorian 	return sldns_read_uint16(rr+dname_len);
954ae8c6e27Sflorian }
955ae8c6e27Sflorian 
956ae8c6e27Sflorian uint16_t sldns_wirerr_get_class(uint8_t* rr, size_t len, size_t dname_len)
957ae8c6e27Sflorian {
958ae8c6e27Sflorian 	if(len < dname_len+4)
959ae8c6e27Sflorian 		return 0;
960ae8c6e27Sflorian 	return sldns_read_uint16(rr+dname_len+2);
961ae8c6e27Sflorian }
962ae8c6e27Sflorian 
963ae8c6e27Sflorian uint32_t sldns_wirerr_get_ttl(uint8_t* rr, size_t len, size_t dname_len)
964ae8c6e27Sflorian {
965ae8c6e27Sflorian 	if(len < dname_len+8)
966ae8c6e27Sflorian 		return 0;
967ae8c6e27Sflorian 	return sldns_read_uint32(rr+dname_len+4);
968ae8c6e27Sflorian }
969ae8c6e27Sflorian 
970ae8c6e27Sflorian uint16_t sldns_wirerr_get_rdatalen(uint8_t* rr, size_t len, size_t dname_len)
971ae8c6e27Sflorian {
972ae8c6e27Sflorian 	if(len < dname_len+10)
973ae8c6e27Sflorian 		return 0;
974ae8c6e27Sflorian 	return sldns_read_uint16(rr+dname_len+8);
975ae8c6e27Sflorian }
976ae8c6e27Sflorian 
977ae8c6e27Sflorian uint8_t* sldns_wirerr_get_rdata(uint8_t* rr, size_t len, size_t dname_len)
978ae8c6e27Sflorian {
979ae8c6e27Sflorian 	if(len < dname_len+10)
980ae8c6e27Sflorian 		return NULL;
981ae8c6e27Sflorian 	return rr+dname_len+10;
982ae8c6e27Sflorian }
983ae8c6e27Sflorian 
984ae8c6e27Sflorian uint8_t* sldns_wirerr_get_rdatawl(uint8_t* rr, size_t len, size_t dname_len)
985ae8c6e27Sflorian {
986ae8c6e27Sflorian 	if(len < dname_len+10)
987ae8c6e27Sflorian 		return NULL;
988ae8c6e27Sflorian 	return rr+dname_len+8;
989ae8c6e27Sflorian }
990ae8c6e27Sflorian 
991ae8c6e27Sflorian const char* sldns_get_errorstr_parse(int e)
992ae8c6e27Sflorian {
993ae8c6e27Sflorian 	sldns_lookup_table *lt;
994ae8c6e27Sflorian 	lt = sldns_lookup_by_id(sldns_wireparse_errors, LDNS_WIREPARSE_ERROR(e));
995ae8c6e27Sflorian 	return lt?lt->name:"unknown error";
996ae8c6e27Sflorian }
997ae8c6e27Sflorian 
998ae8c6e27Sflorian /* Strip whitespace from the start and the end of <line>.  */
999ae8c6e27Sflorian char *
1000ae8c6e27Sflorian sldns_strip_ws(char *line)
1001ae8c6e27Sflorian {
1002ae8c6e27Sflorian         char *s = line, *e;
1003ae8c6e27Sflorian 
1004ae8c6e27Sflorian         for (s = line; *s && isspace((unsigned char)*s); s++)
1005ae8c6e27Sflorian                 ;
1006ae8c6e27Sflorian         for (e = strchr(s, 0); e > s+2 && isspace((unsigned char)e[-1]) && e[-2] != '\\'; e--)
1007ae8c6e27Sflorian                 ;
1008ae8c6e27Sflorian         *e = 0;
1009ae8c6e27Sflorian         return s;
1010ae8c6e27Sflorian }
1011ae8c6e27Sflorian 
1012ae8c6e27Sflorian int sldns_fp2wire_rr_buf(FILE* in, uint8_t* rr, size_t* len, size_t* dname_len,
1013ae8c6e27Sflorian 	struct sldns_file_parse_state* parse_state)
1014ae8c6e27Sflorian {
1015ae8c6e27Sflorian 	char line[LDNS_RR_BUF_SIZE+1];
1016ae8c6e27Sflorian 	ssize_t size;
1017ae8c6e27Sflorian 
1018ae8c6e27Sflorian 	/* read an entire line in from the file */
1019ae8c6e27Sflorian 	if((size = sldns_fget_token_l(in, line, LDNS_PARSE_SKIP_SPACE,
1020ae8c6e27Sflorian 		LDNS_RR_BUF_SIZE, parse_state?&parse_state->lineno:NULL))
1021ae8c6e27Sflorian 		== -1) {
1022ae8c6e27Sflorian 		/* if last line was empty, we are now at feof, which is not
1023ae8c6e27Sflorian 		 * always a parse error (happens when for instance last line
1024ae8c6e27Sflorian 		 * was a comment)
1025ae8c6e27Sflorian 		 */
1026ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX;
1027ae8c6e27Sflorian 	}
1028ae8c6e27Sflorian 
1029ae8c6e27Sflorian 	/* we can have the situation, where we've read ok, but still got
1030ae8c6e27Sflorian 	 * no bytes to play with, in this case size is 0 */
1031ae8c6e27Sflorian 	if(size == 0) {
1032ae8c6e27Sflorian 		if(*len > 0)
1033ae8c6e27Sflorian 			rr[0] = 0;
1034ae8c6e27Sflorian 		*len = 0;
1035ae8c6e27Sflorian 		*dname_len = 0;
1036ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_OK;
1037ae8c6e27Sflorian 	}
1038ae8c6e27Sflorian 
1039ae8c6e27Sflorian 	if(strncmp(line, "$ORIGIN", 7) == 0 && isspace((unsigned char)line[7])) {
1040ae8c6e27Sflorian 		int s;
1041ae8c6e27Sflorian 		strlcpy((char*)rr, line, *len);
1042ae8c6e27Sflorian 		*len = 0;
1043ae8c6e27Sflorian 		*dname_len = 0;
1044ae8c6e27Sflorian 		if(!parse_state) return LDNS_WIREPARSE_ERR_OK;
1045ae8c6e27Sflorian 		parse_state->origin_len = sizeof(parse_state->origin);
1046ae8c6e27Sflorian 		s = sldns_str2wire_dname_buf(sldns_strip_ws(line+8),
1047ae8c6e27Sflorian 			parse_state->origin, &parse_state->origin_len);
1048ae8c6e27Sflorian 		if(s) parse_state->origin_len = 0;
1049ae8c6e27Sflorian 		return s;
1050ae8c6e27Sflorian 	} else if(strncmp(line, "$TTL", 4) == 0 && isspace((unsigned char)line[4])) {
1051ae8c6e27Sflorian 		const char* end = NULL;
1052ae8c6e27Sflorian 		strlcpy((char*)rr, line, *len);
1053ae8c6e27Sflorian 		*len = 0;
1054ae8c6e27Sflorian 		*dname_len = 0;
1055ae8c6e27Sflorian 		if(!parse_state) return LDNS_WIREPARSE_ERR_OK;
1056ae8c6e27Sflorian 		parse_state->default_ttl = sldns_str2period(
1057ae8c6e27Sflorian 			sldns_strip_ws(line+5), &end);
1058ae8c6e27Sflorian 	} else if (strncmp(line, "$INCLUDE", 8) == 0) {
1059ae8c6e27Sflorian 		strlcpy((char*)rr, line, *len);
1060ae8c6e27Sflorian 		*len = 0;
1061ae8c6e27Sflorian 		*dname_len = 0;
1062ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_INCLUDE;
1063ae8c6e27Sflorian 	} else if (strncmp(line, "$", 1) == 0) {
1064ae8c6e27Sflorian 		strlcpy((char*)rr, line, *len);
1065ae8c6e27Sflorian 		*len = 0;
1066ae8c6e27Sflorian 		*dname_len = 0;
1067ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_INCLUDE;
1068ae8c6e27Sflorian 	} else {
1069ae8c6e27Sflorian 		int r = sldns_str2wire_rr_buf(line, rr, len, dname_len,
1070ae8c6e27Sflorian 			parse_state?parse_state->default_ttl:0,
1071ae8c6e27Sflorian 			(parse_state&&parse_state->origin_len)?
1072ae8c6e27Sflorian 				parse_state->origin:NULL,
1073ae8c6e27Sflorian 			parse_state?parse_state->origin_len:0,
1074ae8c6e27Sflorian 			(parse_state&&parse_state->prev_rr_len)?
1075ae8c6e27Sflorian 				parse_state->prev_rr:NULL,
1076ae8c6e27Sflorian 			parse_state?parse_state->prev_rr_len:0);
1077ae8c6e27Sflorian 		if(r == LDNS_WIREPARSE_ERR_OK && (*dname_len) != 0 &&
1078ae8c6e27Sflorian 			parse_state &&
1079ae8c6e27Sflorian 			(*dname_len) <= sizeof(parse_state->prev_rr)) {
1080ae8c6e27Sflorian 			memmove(parse_state->prev_rr, rr, *dname_len);
1081ae8c6e27Sflorian 			parse_state->prev_rr_len = (*dname_len);
1082ae8c6e27Sflorian 		}
1083411c5950Sflorian 		if(r == LDNS_WIREPARSE_ERR_OK && parse_state) {
1084411c5950Sflorian 			parse_state->default_ttl = sldns_wirerr_get_ttl(
1085411c5950Sflorian 				rr, *len, *dname_len);
1086411c5950Sflorian 		}
1087ae8c6e27Sflorian 		return r;
1088ae8c6e27Sflorian 	}
1089ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1090ae8c6e27Sflorian }
1091ae8c6e27Sflorian 
1092411c5950Sflorian static int
1093411c5950Sflorian sldns_str2wire_svcparam_key_lookup(const char *key, size_t key_len)
1094411c5950Sflorian {
1095411c5950Sflorian 	char buf[64];
1096411c5950Sflorian 	char *endptr;
1097411c5950Sflorian 	unsigned long int key_value;
1098411c5950Sflorian 
1099411c5950Sflorian 	if (key_len >= 4  && key_len <= 8 && !strncmp(key, "key", 3)) {
1100411c5950Sflorian 		memcpy(buf, key + 3, key_len - 3);
1101411c5950Sflorian 		buf[key_len - 3] = 0;
1102411c5950Sflorian 		key_value = strtoul(buf, &endptr, 10);
1103411c5950Sflorian 
1104411c5950Sflorian 		if (endptr > buf	/* digits seen */
1105411c5950Sflorian 		&& *endptr == 0		/* no non-digit chars after digits */
1106411c5950Sflorian 		&&  key_value <= 65535)	/* no overflow */
1107411c5950Sflorian 			return key_value;
1108411c5950Sflorian 
1109411c5950Sflorian 	} else switch (key_len) {
1110411c5950Sflorian 	case sizeof("mandatory")-1:
1111411c5950Sflorian 		if (!strncmp(key, "mandatory", sizeof("mandatory")-1))
1112411c5950Sflorian 			return SVCB_KEY_MANDATORY;
1113411c5950Sflorian 		if (!strncmp(key, "echconfig", sizeof("echconfig")-1))
1114411c5950Sflorian 			return SVCB_KEY_ECH; /* allow "echconfig as well as "ech" */
1115411c5950Sflorian 		break;
1116411c5950Sflorian 
1117411c5950Sflorian 	case sizeof("alpn")-1:
1118411c5950Sflorian 		if (!strncmp(key, "alpn", sizeof("alpn")-1))
1119411c5950Sflorian 			return SVCB_KEY_ALPN;
1120411c5950Sflorian 		if (!strncmp(key, "port", sizeof("port")-1))
1121411c5950Sflorian 			return SVCB_KEY_PORT;
1122411c5950Sflorian 		break;
1123411c5950Sflorian 
1124411c5950Sflorian 	case sizeof("no-default-alpn")-1:
1125411c5950Sflorian 		if (!strncmp( key  , "no-default-alpn"
1126411c5950Sflorian 		            , sizeof("no-default-alpn")-1))
1127411c5950Sflorian 			return SVCB_KEY_NO_DEFAULT_ALPN;
1128411c5950Sflorian 		break;
1129411c5950Sflorian 
1130411c5950Sflorian 	case sizeof("ipv4hint")-1:
1131411c5950Sflorian 		if (!strncmp(key, "ipv4hint", sizeof("ipv4hint")-1))
1132411c5950Sflorian 			return SVCB_KEY_IPV4HINT;
1133411c5950Sflorian 		if (!strncmp(key, "ipv6hint", sizeof("ipv6hint")-1))
1134411c5950Sflorian 			return SVCB_KEY_IPV6HINT;
1135411c5950Sflorian 		break;
1136411c5950Sflorian 
1137411c5950Sflorian 	case sizeof("ech")-1:
1138411c5950Sflorian 		if (!strncmp(key, "ech", sizeof("ech")-1))
1139411c5950Sflorian 			return SVCB_KEY_ECH;
1140411c5950Sflorian 		break;
1141411c5950Sflorian 
1142411c5950Sflorian 	default:
1143411c5950Sflorian 		break;
1144411c5950Sflorian 	}
1145411c5950Sflorian 
1146411c5950Sflorian 	/* Although the returned value might be used by the caller,
1147411c5950Sflorian 	 * the parser has erred, so the zone will not be loaded.
1148411c5950Sflorian 	 */
1149411c5950Sflorian 	return -1;
1150411c5950Sflorian }
1151411c5950Sflorian 
1152411c5950Sflorian static int
1153411c5950Sflorian sldns_str2wire_svcparam_port(const char* val, uint8_t* rd, size_t* rd_len)
1154411c5950Sflorian {
1155411c5950Sflorian 	unsigned long int port;
1156411c5950Sflorian 	char *endptr;
1157411c5950Sflorian 
1158411c5950Sflorian 	if (*rd_len < 6)
1159411c5950Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1160411c5950Sflorian 
1161411c5950Sflorian 	port = strtoul(val, &endptr, 10);
1162411c5950Sflorian 
1163411c5950Sflorian 	if (endptr > val	/* digits seen */
1164411c5950Sflorian 	&& *endptr == 0		/* no non-digit chars after digits */
1165411c5950Sflorian 	&&  port <= 65535) {	/* no overflow */
1166411c5950Sflorian 
1167411c5950Sflorian 		sldns_write_uint16(rd, SVCB_KEY_PORT);
1168411c5950Sflorian 		sldns_write_uint16(rd + 2, sizeof(uint16_t));
1169411c5950Sflorian 		sldns_write_uint16(rd + 4, port);
1170411c5950Sflorian 		*rd_len = 6;
1171411c5950Sflorian 
1172411c5950Sflorian 		return LDNS_WIREPARSE_ERR_OK;
1173411c5950Sflorian 	}
1174411c5950Sflorian 
1175411c5950Sflorian 	return LDNS_WIREPARSE_ERR_SVCB_PORT_VALUE_SYNTAX;
1176411c5950Sflorian }
1177411c5950Sflorian 
1178411c5950Sflorian static int
1179411c5950Sflorian sldns_str2wire_svcbparam_ipv4hint(const char* val, uint8_t* rd, size_t* rd_len)
1180411c5950Sflorian {
1181411c5950Sflorian 	size_t count;
1182411c5950Sflorian 	char ip_str[INET_ADDRSTRLEN+1];
1183411c5950Sflorian 	char *next_ip_str;
1184411c5950Sflorian 	size_t i;
1185411c5950Sflorian 
1186411c5950Sflorian 	for (i = 0, count = 1; val[i]; i++) {
1187411c5950Sflorian 		if (val[i] == ',')
1188411c5950Sflorian 			count += 1;
1189411c5950Sflorian 		if (count > SVCB_MAX_COMMA_SEPARATED_VALUES) {
1190411c5950Sflorian 			return LDNS_WIREPARSE_ERR_SVCB_IPV4_TOO_MANY_ADDRESSES;
1191411c5950Sflorian 		}
1192411c5950Sflorian 	}
1193411c5950Sflorian 
1194411c5950Sflorian 	if (*rd_len < (LDNS_IP4ADDRLEN * count) + 4)
1195411c5950Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1196411c5950Sflorian 
1197411c5950Sflorian 	/* count is number of comma's in val + 1; so the actual number of IPv4
1198411c5950Sflorian 	 * addresses in val
1199411c5950Sflorian 	 */
1200411c5950Sflorian 	sldns_write_uint16(rd, SVCB_KEY_IPV4HINT);
1201411c5950Sflorian 	sldns_write_uint16(rd + 2, LDNS_IP4ADDRLEN * count);
1202411c5950Sflorian 	*rd_len = 4;
1203411c5950Sflorian 
1204411c5950Sflorian 	while (count) {
1205411c5950Sflorian 		if (!(next_ip_str = strchr(val, ','))) {
1206411c5950Sflorian 			if (inet_pton(AF_INET, val, rd + *rd_len) != 1)
1207411c5950Sflorian 				break;
1208411c5950Sflorian 			*rd_len += LDNS_IP4ADDRLEN;
1209411c5950Sflorian 
1210411c5950Sflorian 			assert(count == 1);
1211411c5950Sflorian 
1212411c5950Sflorian 		} else if (next_ip_str - val >= (int)sizeof(ip_str))
1213411c5950Sflorian 			break;
1214411c5950Sflorian 
1215411c5950Sflorian 		else {
1216411c5950Sflorian 			memcpy(ip_str, val, next_ip_str - val);
1217411c5950Sflorian 			ip_str[next_ip_str - val] = 0;
1218411c5950Sflorian 			if (inet_pton(AF_INET, ip_str, rd + *rd_len) != 1) {
1219411c5950Sflorian 				break;
1220411c5950Sflorian 			}
1221411c5950Sflorian 			*rd_len += LDNS_IP4ADDRLEN;
1222411c5950Sflorian 
1223411c5950Sflorian 			val = next_ip_str + 1;
1224411c5950Sflorian 		}
1225411c5950Sflorian 		count--;
1226411c5950Sflorian 	}
1227411c5950Sflorian 	if (count) /* verify that we parsed all values */
1228411c5950Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_IP4;
1229411c5950Sflorian 
1230411c5950Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1231411c5950Sflorian }
1232411c5950Sflorian 
1233411c5950Sflorian static int
1234411c5950Sflorian sldns_str2wire_svcbparam_ipv6hint(const char* val, uint8_t* rd, size_t* rd_len)
1235411c5950Sflorian {
1236411c5950Sflorian 	size_t count;
1237411c5950Sflorian 	char ip_str[INET6_ADDRSTRLEN+1];
1238411c5950Sflorian 	char *next_ip_str;
1239411c5950Sflorian 	size_t i;
1240411c5950Sflorian 
1241411c5950Sflorian 	for (i = 0, count = 1; val[i]; i++) {
1242411c5950Sflorian 		if (val[i] == ',')
1243411c5950Sflorian 			count += 1;
1244411c5950Sflorian 		if (count > SVCB_MAX_COMMA_SEPARATED_VALUES) {
1245411c5950Sflorian 			return LDNS_WIREPARSE_ERR_SVCB_IPV6_TOO_MANY_ADDRESSES;
1246411c5950Sflorian 		}
1247411c5950Sflorian 	}
1248411c5950Sflorian 
1249411c5950Sflorian 	if (*rd_len < (LDNS_IP6ADDRLEN * count) + 4)
1250411c5950Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1251411c5950Sflorian 
1252411c5950Sflorian 	/* count is number of comma's in val + 1; so the actual number of IPv6
1253411c5950Sflorian 	 * addresses in val
1254411c5950Sflorian 	 */
1255411c5950Sflorian 	sldns_write_uint16(rd, SVCB_KEY_IPV6HINT);
1256411c5950Sflorian 	sldns_write_uint16(rd + 2, LDNS_IP6ADDRLEN * count);
1257411c5950Sflorian 	*rd_len = 4;
1258411c5950Sflorian 
1259411c5950Sflorian 	while (count) {
1260411c5950Sflorian 		if (!(next_ip_str = strchr(val, ','))) {
1261411c5950Sflorian 			if (inet_pton(AF_INET6, val, rd + *rd_len) != 1)
1262411c5950Sflorian 				break;
1263411c5950Sflorian 			*rd_len += LDNS_IP6ADDRLEN;
1264411c5950Sflorian 
1265411c5950Sflorian 			assert(count == 1);
1266411c5950Sflorian 
1267411c5950Sflorian 		} else if (next_ip_str - val >= (int)sizeof(ip_str))
1268411c5950Sflorian 			break;
1269411c5950Sflorian 
1270411c5950Sflorian 		else {
1271411c5950Sflorian 			memcpy(ip_str, val, next_ip_str - val);
1272411c5950Sflorian 			ip_str[next_ip_str - val] = 0;
1273411c5950Sflorian 			if (inet_pton(AF_INET6, ip_str, rd + *rd_len) != 1) {
1274411c5950Sflorian 				break;
1275411c5950Sflorian 			}
1276411c5950Sflorian 			*rd_len += LDNS_IP6ADDRLEN;
1277411c5950Sflorian 
1278411c5950Sflorian 			val = next_ip_str + 1;
1279411c5950Sflorian 		}
1280411c5950Sflorian 		count--;
1281411c5950Sflorian 	}
1282411c5950Sflorian 	if (count) /* verify that we parsed all values */
1283411c5950Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_IP6;
1284411c5950Sflorian 
1285411c5950Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1286411c5950Sflorian }
1287411c5950Sflorian 
1288411c5950Sflorian /* compare function used for sorting uint16_t's */
1289411c5950Sflorian static int
1290411c5950Sflorian sldns_network_uint16_cmp(const void *a, const void *b)
1291411c5950Sflorian {
1292411c5950Sflorian 	return ((int)sldns_read_uint16(a)) - ((int)sldns_read_uint16(b));
1293411c5950Sflorian }
1294411c5950Sflorian 
1295411c5950Sflorian static int
1296411c5950Sflorian sldns_str2wire_svcbparam_mandatory(const char* val, uint8_t* rd, size_t* rd_len)
1297411c5950Sflorian {
1298411c5950Sflorian 	size_t i, count, val_len;
1299411c5950Sflorian 	char* next_key;
1300411c5950Sflorian 
1301411c5950Sflorian 	val_len = strlen(val);
1302411c5950Sflorian 
1303411c5950Sflorian 	for (i = 0, count = 1; val[i]; i++) {
1304411c5950Sflorian 		if (val[i] == ',')
1305411c5950Sflorian 			count += 1;
1306411c5950Sflorian 		if (count > SVCB_MAX_COMMA_SEPARATED_VALUES) {
1307411c5950Sflorian 			return LDNS_WIREPARSE_ERR_SVCB_MANDATORY_TOO_MANY_KEYS;
1308411c5950Sflorian 		}
1309411c5950Sflorian 	}
1310411c5950Sflorian 	if (sizeof(uint16_t) * (count + 2) > *rd_len)
1311411c5950Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1312411c5950Sflorian 
1313411c5950Sflorian 	sldns_write_uint16(rd, SVCB_KEY_MANDATORY);
1314411c5950Sflorian 	sldns_write_uint16(rd + 2, sizeof(uint16_t) * count);
1315411c5950Sflorian 	*rd_len = 4;
1316411c5950Sflorian 
1317411c5950Sflorian 	while (1) {
1318411c5950Sflorian 		int svcparamkey;
1319411c5950Sflorian 
1320411c5950Sflorian 		if (!(next_key = strchr(val, ','))) {
1321411c5950Sflorian 			svcparamkey = sldns_str2wire_svcparam_key_lookup(val, val_len);
1322411c5950Sflorian 
1323411c5950Sflorian 			if (svcparamkey < 0) {
1324411c5950Sflorian 				return LDNS_WIREPARSE_ERR_SVCB_UNKNOWN_KEY;
1325411c5950Sflorian 			}
1326411c5950Sflorian 
1327411c5950Sflorian 			sldns_write_uint16(rd + *rd_len, svcparamkey);
1328411c5950Sflorian 			*rd_len += 2;
1329411c5950Sflorian 			break;
1330411c5950Sflorian 		} else {
1331411c5950Sflorian 			svcparamkey = sldns_str2wire_svcparam_key_lookup(val, next_key - val);
1332411c5950Sflorian 
1333411c5950Sflorian 			if (svcparamkey < 0) {
1334411c5950Sflorian 				return LDNS_WIREPARSE_ERR_SVCB_UNKNOWN_KEY;
1335411c5950Sflorian 			}
1336411c5950Sflorian 
1337411c5950Sflorian 			sldns_write_uint16(rd + *rd_len,
1338411c5950Sflorian 				svcparamkey);
1339411c5950Sflorian 			*rd_len += 2;
1340411c5950Sflorian 		}
1341411c5950Sflorian 
1342411c5950Sflorian 		val_len -= next_key - val + 1;
1343411c5950Sflorian 		val = next_key + 1; /* skip the comma */
1344411c5950Sflorian 	}
1345411c5950Sflorian 
1346411c5950Sflorian 	/* In draft-ietf-dnsop-svcb-https-06 Section 7:
1347411c5950Sflorian 	 *
1348411c5950Sflorian 	 *    "In wire format, the keys are represented by their numeric
1349411c5950Sflorian 	 *     values in network byte order, concatenated in ascending order."
1350411c5950Sflorian 	 */
1351411c5950Sflorian 	qsort((void *)(rd + 4), count, sizeof(uint16_t), sldns_network_uint16_cmp);
1352411c5950Sflorian 
1353411c5950Sflorian 	/* The code below revolves around sematic errors in the SVCParam set.
1354411c5950Sflorian 	 * So long as we do not distinguish between running Unbound as a primary
1355411c5950Sflorian 	 * or as a secondary, we default to secondary behavior and we ignore the
1356411c5950Sflorian 	 * semantic errors. */
1357411c5950Sflorian #ifdef SVCB_SEMANTIC_ERRORS
1358411c5950Sflorian 	/* In draft-ietf-dnsop-svcb-https-06 Section 8
1359411c5950Sflorian 	 * automatically mandatory MUST NOT appear in its own value-list
1360411c5950Sflorian 	 */
1361411c5950Sflorian 	if (sldns_read_uint16(rd + 4) == SVCB_KEY_MANDATORY)
1362411c5950Sflorian 		return LDNS_WIREPARSE_ERR_SVCB_MANDATORY_IN_MANDATORY;
1363411c5950Sflorian 
1364411c5950Sflorian 	/* Guarantee key uniqueness. After the sort we only need to
1365411c5950Sflorian 	 * compare neighbouring keys */
1366411c5950Sflorian 	if (count > 1) {
1367411c5950Sflorian 		for (i = 0; i < count - 1; i++) {
1368411c5950Sflorian 			uint8_t* current_pos = (rd + 4 + (sizeof(uint16_t) * i));
1369411c5950Sflorian 			uint16_t key = sldns_read_uint16(current_pos);
1370411c5950Sflorian 
1371411c5950Sflorian 			if (key == sldns_read_uint16(current_pos + 2)) {
1372411c5950Sflorian 				return LDNS_WIREPARSE_ERR_SVCB_MANDATORY_DUPLICATE_KEY;
1373411c5950Sflorian 			}
1374411c5950Sflorian 		}
1375411c5950Sflorian 	}
1376411c5950Sflorian #endif
1377411c5950Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1378411c5950Sflorian }
1379411c5950Sflorian 
1380411c5950Sflorian static int
1381411c5950Sflorian sldns_str2wire_svcbparam_ech_value(const char* val, uint8_t* rd, size_t* rd_len)
1382411c5950Sflorian {
1383411c5950Sflorian 	uint8_t buffer[LDNS_MAX_RDFLEN];
1384411c5950Sflorian 	int wire_len;
1385411c5950Sflorian 
1386411c5950Sflorian 	/* single 0 represents empty buffer */
1387411c5950Sflorian 	if(strcmp(val, "0") == 0) {
1388411c5950Sflorian 		if (*rd_len < 4)
1389411c5950Sflorian 			return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1390411c5950Sflorian 		sldns_write_uint16(rd, SVCB_KEY_ECH);
1391411c5950Sflorian 		sldns_write_uint16(rd + 2, 0);
1392411c5950Sflorian 
1393411c5950Sflorian 		return LDNS_WIREPARSE_ERR_OK;
1394411c5950Sflorian 	}
1395411c5950Sflorian 
1396411c5950Sflorian 	wire_len = sldns_b64_pton(val, buffer, LDNS_MAX_RDFLEN);
1397411c5950Sflorian 
1398411c5950Sflorian 	if (wire_len <= 0) {
1399411c5950Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_B64;
1400411c5950Sflorian 	} else if ((unsigned)wire_len + 4 > *rd_len) {
1401411c5950Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1402411c5950Sflorian 	} else {
1403411c5950Sflorian 		sldns_write_uint16(rd, SVCB_KEY_ECH);
1404411c5950Sflorian 		sldns_write_uint16(rd + 2, wire_len);
1405411c5950Sflorian 		memcpy(rd + 4, buffer, wire_len);
1406411c5950Sflorian 		*rd_len = 4 + wire_len;
1407411c5950Sflorian 
1408411c5950Sflorian 		return LDNS_WIREPARSE_ERR_OK;
1409411c5950Sflorian 	}
1410411c5950Sflorian }
1411411c5950Sflorian 
1412411c5950Sflorian static const char*
1413411c5950Sflorian sldns_str2wire_svcbparam_parse_next_unescaped_comma(const char *val)
1414411c5950Sflorian {
1415411c5950Sflorian 	while (*val) {
1416411c5950Sflorian 		/* Only return when the comma is not escaped*/
1417411c5950Sflorian 		if (*val == '\\'){
1418411c5950Sflorian 			++val;
1419411c5950Sflorian 			if (!*val)
1420411c5950Sflorian 				break;
1421411c5950Sflorian 		} else if (*val == ',')
1422411c5950Sflorian 				return val;
1423411c5950Sflorian 
1424411c5950Sflorian 		val++;
1425411c5950Sflorian 	}
1426411c5950Sflorian 	return NULL;
1427411c5950Sflorian }
1428411c5950Sflorian 
1429411c5950Sflorian /* The source is already properly unescaped, this double unescaping is purely to allow for
1430411c5950Sflorian  * comma's in comma seperated alpn lists.
1431411c5950Sflorian  *
1432411c5950Sflorian  * In draft-ietf-dnsop-svcb-https-06 Section 7:
1433411c5950Sflorian  * To enable simpler parsing, this SvcParamValue MUST NOT contain escape sequences.
1434411c5950Sflorian  */
1435411c5950Sflorian static size_t
1436411c5950Sflorian sldns_str2wire_svcbparam_parse_copy_unescaped(uint8_t *dst,
1437411c5950Sflorian 	const char *src, size_t len)
1438411c5950Sflorian {
1439411c5950Sflorian 	uint8_t *orig_dst = dst;
1440411c5950Sflorian 
1441411c5950Sflorian 	while (len) {
1442411c5950Sflorian 		if (*src == '\\') {
1443411c5950Sflorian 			src++;
1444411c5950Sflorian 			len--;
1445411c5950Sflorian 			if (!len)
1446411c5950Sflorian 				break;
1447411c5950Sflorian 		}
1448411c5950Sflorian 		*dst++ = *src++;
1449411c5950Sflorian 		len--;
1450411c5950Sflorian 	}
1451411c5950Sflorian 	return (size_t)(dst - orig_dst);
1452411c5950Sflorian }
1453411c5950Sflorian 
1454411c5950Sflorian static int
1455411c5950Sflorian sldns_str2wire_svcbparam_alpn_value(const char* val,
1456411c5950Sflorian 	uint8_t* rd, size_t* rd_len)
1457411c5950Sflorian {
1458411c5950Sflorian 	uint8_t     unescaped_dst[LDNS_MAX_RDFLEN];
1459411c5950Sflorian 	uint8_t    *dst = unescaped_dst;
1460411c5950Sflorian 	const char *next_str;
1461411c5950Sflorian 	size_t      str_len;
1462411c5950Sflorian 	size_t      dst_len;
1463411c5950Sflorian 	size_t      val_len;
1464411c5950Sflorian 
1465411c5950Sflorian 	val_len = strlen(val);
1466411c5950Sflorian 
1467411c5950Sflorian 	if (val_len > sizeof(unescaped_dst)) {
1468411c5950Sflorian 		return LDNS_WIREPARSE_ERR_SVCB_ALPN_KEY_TOO_LARGE;
1469411c5950Sflorian 	}
1470411c5950Sflorian 	while (val_len) {
1471411c5950Sflorian 		size_t key_len;
1472411c5950Sflorian 
1473411c5950Sflorian 		str_len = (next_str = sldns_str2wire_svcbparam_parse_next_unescaped_comma(val))
1474411c5950Sflorian 		        ? (size_t)(next_str - val) : val_len;
1475411c5950Sflorian 
1476411c5950Sflorian 		if (str_len > 255) {
1477411c5950Sflorian 			return LDNS_WIREPARSE_ERR_SVCB_ALPN_KEY_TOO_LARGE;
1478411c5950Sflorian 		}
1479411c5950Sflorian 
1480411c5950Sflorian 		key_len = sldns_str2wire_svcbparam_parse_copy_unescaped(dst + 1, val, str_len);
1481411c5950Sflorian 		*dst++ = key_len;
1482411c5950Sflorian 		 dst  += key_len;
1483411c5950Sflorian 
1484411c5950Sflorian 		if (!next_str)
1485411c5950Sflorian 			break;
1486411c5950Sflorian 
1487411c5950Sflorian 		/* skip the comma in the next iteration */
1488411c5950Sflorian 		val_len -= next_str - val + 1;
1489411c5950Sflorian 		val = next_str + 1;
1490411c5950Sflorian 	}
1491411c5950Sflorian 	dst_len = dst - unescaped_dst;
1492411c5950Sflorian 	if (*rd_len < 4 + dst_len)
1493411c5950Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1494411c5950Sflorian 	sldns_write_uint16(rd, SVCB_KEY_ALPN);
1495411c5950Sflorian 	sldns_write_uint16(rd + 2, dst_len);
1496411c5950Sflorian 	memcpy(rd + 4, unescaped_dst, dst_len);
1497411c5950Sflorian 	*rd_len = 4 + dst_len;
1498411c5950Sflorian 
1499411c5950Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1500411c5950Sflorian }
1501411c5950Sflorian 
1502411c5950Sflorian static int
1503411c5950Sflorian sldns_str2wire_svcparam_value(const char *key, size_t key_len,
1504411c5950Sflorian 	const char *val, uint8_t* rd, size_t* rd_len)
1505411c5950Sflorian {
1506411c5950Sflorian 	size_t str_len;
1507411c5950Sflorian 	int svcparamkey = sldns_str2wire_svcparam_key_lookup(key, key_len);
1508411c5950Sflorian 
1509411c5950Sflorian 	if (svcparamkey < 0) {
1510411c5950Sflorian 		return LDNS_WIREPARSE_ERR_SVCB_UNKNOWN_KEY;
1511411c5950Sflorian 	}
1512411c5950Sflorian 
1513411c5950Sflorian 	/* key without value */
1514411c5950Sflorian 	if (val == NULL) {
1515411c5950Sflorian 		switch (svcparamkey) {
1516411c5950Sflorian #ifdef SVCB_SEMANTIC_ERRORS
1517411c5950Sflorian 		case SVCB_KEY_MANDATORY:
1518411c5950Sflorian 		case SVCB_KEY_ALPN:
1519411c5950Sflorian 		case SVCB_KEY_PORT:
1520411c5950Sflorian 		case SVCB_KEY_IPV4HINT:
1521411c5950Sflorian 		case SVCB_KEY_IPV6HINT:
1522411c5950Sflorian 			return LDNS_WIREPARSE_ERR_SVCB_MISSING_PARAM;
1523411c5950Sflorian #endif
1524411c5950Sflorian 		default:
1525411c5950Sflorian 			if (*rd_len < 4)
1526411c5950Sflorian 				return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1527411c5950Sflorian 			sldns_write_uint16(rd, svcparamkey);
1528411c5950Sflorian 			sldns_write_uint16(rd + 2, 0);
1529411c5950Sflorian 			*rd_len = 4;
1530411c5950Sflorian 
1531411c5950Sflorian 			return LDNS_WIREPARSE_ERR_OK;
1532411c5950Sflorian 		}
1533411c5950Sflorian 	}
1534411c5950Sflorian 
1535411c5950Sflorian 	/* value is non-empty */
1536411c5950Sflorian 	switch (svcparamkey) {
1537411c5950Sflorian 	case SVCB_KEY_PORT:
1538411c5950Sflorian 		return sldns_str2wire_svcparam_port(val, rd, rd_len);
1539411c5950Sflorian 	case SVCB_KEY_IPV4HINT:
1540411c5950Sflorian 		return sldns_str2wire_svcbparam_ipv4hint(val, rd, rd_len);
1541411c5950Sflorian 	case SVCB_KEY_IPV6HINT:
1542411c5950Sflorian 		return sldns_str2wire_svcbparam_ipv6hint(val, rd, rd_len);
1543411c5950Sflorian 	case SVCB_KEY_MANDATORY:
1544411c5950Sflorian 		return sldns_str2wire_svcbparam_mandatory(val, rd, rd_len);
1545411c5950Sflorian #ifdef SVCB_SEMANTIC_ERRORS
1546411c5950Sflorian 	case SVCB_KEY_NO_DEFAULT_ALPN:
1547411c5950Sflorian 		return LDNS_WIREPARSE_ERR_SVCB_NO_DEFAULT_ALPN_VALUE;
1548411c5950Sflorian #endif
1549411c5950Sflorian 	case SVCB_KEY_ECH:
1550411c5950Sflorian 		return sldns_str2wire_svcbparam_ech_value(val, rd, rd_len);
1551411c5950Sflorian 	case SVCB_KEY_ALPN:
1552411c5950Sflorian 		return sldns_str2wire_svcbparam_alpn_value(val, rd, rd_len);
1553411c5950Sflorian 	default:
1554411c5950Sflorian 		str_len = strlen(val);
1555411c5950Sflorian 		if (*rd_len < 4 + str_len)
1556411c5950Sflorian 			return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1557411c5950Sflorian 		sldns_write_uint16(rd, svcparamkey);
1558411c5950Sflorian 		sldns_write_uint16(rd + 2, str_len);
1559411c5950Sflorian 		memcpy(rd + 4, val, str_len);
1560411c5950Sflorian 		*rd_len = 4 + str_len;
1561411c5950Sflorian 
1562411c5950Sflorian 		return LDNS_WIREPARSE_ERR_OK;
1563411c5950Sflorian 	}
1564411c5950Sflorian 
1565411c5950Sflorian 	return LDNS_WIREPARSE_ERR_GENERAL;
1566411c5950Sflorian }
1567411c5950Sflorian 
1568*6d5a1597Sflorian static int sldns_str2wire_svcparam_buf(const char* str, uint8_t* rd, size_t* rd_len)
1569411c5950Sflorian {
1570411c5950Sflorian 	const char* eq_pos;
1571411c5950Sflorian 	char unescaped_val[LDNS_MAX_RDFLEN];
1572411c5950Sflorian 	char* val_out = unescaped_val;
1573411c5950Sflorian 	const char* val_in;
1574411c5950Sflorian 
1575411c5950Sflorian 	eq_pos = strchr(str, '=');
1576411c5950Sflorian 
1577411c5950Sflorian 	/* case: key=value */
1578411c5950Sflorian 	if (eq_pos != NULL && eq_pos[1]) {
1579411c5950Sflorian 		val_in = eq_pos + 1;
1580411c5950Sflorian 
1581411c5950Sflorian 		/* unescape characters and "" blocks */
1582411c5950Sflorian 		if (*val_in == '"') {
1583411c5950Sflorian 			val_in++;
1584411c5950Sflorian 			while (*val_in != '"'
1585411c5950Sflorian 			&& (unsigned)(val_out - unescaped_val + 1) < sizeof(unescaped_val)
1586411c5950Sflorian 			&& sldns_parse_char( (uint8_t*) val_out, &val_in)) {
1587411c5950Sflorian 				val_out++;
1588411c5950Sflorian 			}
1589411c5950Sflorian 		} else {
1590411c5950Sflorian 			while ((unsigned)(val_out - unescaped_val + 1) < sizeof(unescaped_val)
1591411c5950Sflorian 			&& sldns_parse_char( (uint8_t*) val_out, &val_in)) {
1592411c5950Sflorian 				val_out++;
1593411c5950Sflorian 			}
1594411c5950Sflorian 		}
1595411c5950Sflorian 		*val_out = 0;
1596411c5950Sflorian 
1597411c5950Sflorian 		return sldns_str2wire_svcparam_value(str, eq_pos - str,
1598411c5950Sflorian 		                                         unescaped_val[0] ? unescaped_val : NULL, rd, rd_len);
1599411c5950Sflorian 	}
1600411c5950Sflorian 	/* case: key= */
1601411c5950Sflorian 	else if (eq_pos != NULL && !(eq_pos[1])) {
1602411c5950Sflorian 		return sldns_str2wire_svcparam_value(str, eq_pos - str, NULL, rd, rd_len);
1603411c5950Sflorian 	}
1604411c5950Sflorian 	/* case: key */
1605411c5950Sflorian 	else {
1606411c5950Sflorian 		return sldns_str2wire_svcparam_value(str, strlen(str), NULL, rd, rd_len);
1607411c5950Sflorian 	}
1608411c5950Sflorian }
1609411c5950Sflorian 
1610ae8c6e27Sflorian int sldns_str2wire_rdf_buf(const char* str, uint8_t* rd, size_t* len,
1611ae8c6e27Sflorian 	sldns_rdf_type rdftype)
1612ae8c6e27Sflorian {
1613ae8c6e27Sflorian 	switch (rdftype) {
1614ae8c6e27Sflorian 	case LDNS_RDF_TYPE_DNAME:
1615ae8c6e27Sflorian 		return sldns_str2wire_dname_buf(str, rd, len);
1616ae8c6e27Sflorian 	case LDNS_RDF_TYPE_INT8:
1617ae8c6e27Sflorian 		return sldns_str2wire_int8_buf(str, rd, len);
1618ae8c6e27Sflorian 	case LDNS_RDF_TYPE_INT16:
1619ae8c6e27Sflorian 		return sldns_str2wire_int16_buf(str, rd, len);
1620ae8c6e27Sflorian 	case LDNS_RDF_TYPE_INT32:
1621ae8c6e27Sflorian 		return sldns_str2wire_int32_buf(str, rd, len);
1622ae8c6e27Sflorian 	case LDNS_RDF_TYPE_A:
1623ae8c6e27Sflorian 		return sldns_str2wire_a_buf(str, rd, len);
1624ae8c6e27Sflorian 	case LDNS_RDF_TYPE_AAAA:
1625ae8c6e27Sflorian 		return sldns_str2wire_aaaa_buf(str, rd, len);
1626ae8c6e27Sflorian 	case LDNS_RDF_TYPE_STR:
1627ae8c6e27Sflorian 		return sldns_str2wire_str_buf(str, rd, len);
1628ae8c6e27Sflorian 	case LDNS_RDF_TYPE_APL:
1629ae8c6e27Sflorian 		return sldns_str2wire_apl_buf(str, rd, len);
1630ae8c6e27Sflorian 	case LDNS_RDF_TYPE_B64:
1631ae8c6e27Sflorian 		return sldns_str2wire_b64_buf(str, rd, len);
1632ae8c6e27Sflorian 	case LDNS_RDF_TYPE_B32_EXT:
1633ae8c6e27Sflorian 		return sldns_str2wire_b32_ext_buf(str, rd, len);
1634ae8c6e27Sflorian 	case LDNS_RDF_TYPE_HEX:
1635ae8c6e27Sflorian 		return sldns_str2wire_hex_buf(str, rd, len);
1636ae8c6e27Sflorian 	case LDNS_RDF_TYPE_NSEC:
1637ae8c6e27Sflorian 		return sldns_str2wire_nsec_buf(str, rd, len);
1638ae8c6e27Sflorian 	case LDNS_RDF_TYPE_TYPE:
1639ae8c6e27Sflorian 		return sldns_str2wire_type_buf(str, rd, len);
1640ae8c6e27Sflorian 	case LDNS_RDF_TYPE_CLASS:
1641ae8c6e27Sflorian 		return sldns_str2wire_class_buf(str, rd, len);
1642ae8c6e27Sflorian 	case LDNS_RDF_TYPE_CERT_ALG:
1643ae8c6e27Sflorian 		return sldns_str2wire_cert_alg_buf(str, rd, len);
1644ae8c6e27Sflorian 	case LDNS_RDF_TYPE_ALG:
1645ae8c6e27Sflorian 		return sldns_str2wire_alg_buf(str, rd, len);
1646ae8c6e27Sflorian 	case LDNS_RDF_TYPE_TIME:
1647ae8c6e27Sflorian 		return sldns_str2wire_time_buf(str, rd, len);
1648ae8c6e27Sflorian 	case LDNS_RDF_TYPE_PERIOD:
1649ae8c6e27Sflorian 		return sldns_str2wire_period_buf(str, rd, len);
1650ae8c6e27Sflorian 	case LDNS_RDF_TYPE_TSIGTIME:
1651ae8c6e27Sflorian 		return sldns_str2wire_tsigtime_buf(str, rd, len);
1652ae8c6e27Sflorian 	case LDNS_RDF_TYPE_LOC:
1653ae8c6e27Sflorian 		return sldns_str2wire_loc_buf(str, rd, len);
1654ae8c6e27Sflorian 	case LDNS_RDF_TYPE_WKS:
1655ae8c6e27Sflorian 		return sldns_str2wire_wks_buf(str, rd, len);
1656ae8c6e27Sflorian 	case LDNS_RDF_TYPE_NSAP:
1657ae8c6e27Sflorian 		return sldns_str2wire_nsap_buf(str, rd, len);
1658ae8c6e27Sflorian 	case LDNS_RDF_TYPE_ATMA:
1659ae8c6e27Sflorian 		return sldns_str2wire_atma_buf(str, rd, len);
1660ae8c6e27Sflorian 	case LDNS_RDF_TYPE_IPSECKEY:
1661ae8c6e27Sflorian 		return sldns_str2wire_ipseckey_buf(str, rd, len);
1662ae8c6e27Sflorian 	case LDNS_RDF_TYPE_NSEC3_SALT:
1663ae8c6e27Sflorian 		return sldns_str2wire_nsec3_salt_buf(str, rd, len);
1664ae8c6e27Sflorian 	case LDNS_RDF_TYPE_NSEC3_NEXT_OWNER:
1665ae8c6e27Sflorian 		return sldns_str2wire_b32_ext_buf(str, rd, len);
1666ae8c6e27Sflorian 	case LDNS_RDF_TYPE_ILNP64:
1667ae8c6e27Sflorian 		return sldns_str2wire_ilnp64_buf(str, rd, len);
1668ae8c6e27Sflorian 	case LDNS_RDF_TYPE_EUI48:
1669ae8c6e27Sflorian 		return sldns_str2wire_eui48_buf(str, rd, len);
1670ae8c6e27Sflorian 	case LDNS_RDF_TYPE_EUI64:
1671ae8c6e27Sflorian 		return sldns_str2wire_eui64_buf(str, rd, len);
1672ae8c6e27Sflorian 	case LDNS_RDF_TYPE_TAG:
1673ae8c6e27Sflorian 		return sldns_str2wire_tag_buf(str, rd, len);
1674ae8c6e27Sflorian 	case LDNS_RDF_TYPE_LONG_STR:
1675ae8c6e27Sflorian 		return sldns_str2wire_long_str_buf(str, rd, len);
1676ae8c6e27Sflorian 	case LDNS_RDF_TYPE_TSIGERROR:
1677ae8c6e27Sflorian 		return sldns_str2wire_tsigerror_buf(str, rd, len);
1678ae8c6e27Sflorian 	case LDNS_RDF_TYPE_HIP:
1679ae8c6e27Sflorian 		return sldns_str2wire_hip_buf(str, rd, len);
1680ae8c6e27Sflorian 	case LDNS_RDF_TYPE_INT16_DATA:
1681ae8c6e27Sflorian 		return sldns_str2wire_int16_data_buf(str, rd, len);
1682411c5950Sflorian 	case LDNS_RDF_TYPE_SVCPARAM:
1683411c5950Sflorian 		return sldns_str2wire_svcparam_buf(str, rd, len);
1684ae8c6e27Sflorian 	case LDNS_RDF_TYPE_UNKNOWN:
1685ae8c6e27Sflorian 	case LDNS_RDF_TYPE_SERVICE:
1686ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_NOT_IMPL;
1687ae8c6e27Sflorian 	case LDNS_RDF_TYPE_NONE:
1688ae8c6e27Sflorian 	default:
1689ae8c6e27Sflorian 		break;
1690ae8c6e27Sflorian 	}
1691ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_GENERAL;
1692ae8c6e27Sflorian }
1693ae8c6e27Sflorian 
1694ae8c6e27Sflorian int sldns_str2wire_int8_buf(const char* str, uint8_t* rd, size_t* len)
1695ae8c6e27Sflorian {
1696ae8c6e27Sflorian 	char* end;
1697ae8c6e27Sflorian 	uint8_t r = (uint8_t)strtol((char*)str, &end, 10);
1698ae8c6e27Sflorian 	if(*end != 0)
1699ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_INT, end-(char*)str);
1700ae8c6e27Sflorian 	if(*len < 1)
1701ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1702ae8c6e27Sflorian 	rd[0] = r;
1703ae8c6e27Sflorian 	*len = 1;
1704ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1705ae8c6e27Sflorian }
1706ae8c6e27Sflorian 
1707ae8c6e27Sflorian int sldns_str2wire_int16_buf(const char* str, uint8_t* rd, size_t* len)
1708ae8c6e27Sflorian {
1709ae8c6e27Sflorian 	char* end;
1710ae8c6e27Sflorian 	uint16_t r = (uint16_t)strtol((char*)str, &end, 10);
1711ae8c6e27Sflorian 	if(*end != 0)
1712ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_INT, end-(char*)str);
1713ae8c6e27Sflorian 	if(*len < 2)
1714ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1715ae8c6e27Sflorian 	sldns_write_uint16(rd, r);
1716ae8c6e27Sflorian 	*len = 2;
1717ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1718ae8c6e27Sflorian }
1719ae8c6e27Sflorian 
1720ae8c6e27Sflorian int sldns_str2wire_int32_buf(const char* str, uint8_t* rd, size_t* len)
1721ae8c6e27Sflorian {
1722ae8c6e27Sflorian 	char* end;
1723ae8c6e27Sflorian 	uint32_t r;
1724ae8c6e27Sflorian 	errno = 0; /* must set to zero before call,
1725ae8c6e27Sflorian 			note race condition on errno */
1726ae8c6e27Sflorian 	if(*str == '-')
1727ae8c6e27Sflorian 		r = (uint32_t)strtol((char*)str, &end, 10);
1728ae8c6e27Sflorian 	else	r = (uint32_t)strtoul((char*)str, &end, 10);
1729ae8c6e27Sflorian 	if(*end != 0)
1730ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_INT, end-(char*)str);
1731ae8c6e27Sflorian 	if(errno == ERANGE)
1732ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_INTEGER_OVERFLOW;
1733ae8c6e27Sflorian 	if(*len < 4)
1734ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1735ae8c6e27Sflorian 	sldns_write_uint32(rd, r);
1736ae8c6e27Sflorian 	*len = 4;
1737ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1738ae8c6e27Sflorian }
1739ae8c6e27Sflorian 
1740ae8c6e27Sflorian int sldns_str2wire_a_buf(const char* str, uint8_t* rd, size_t* len)
1741ae8c6e27Sflorian {
1742ae8c6e27Sflorian 	struct in_addr address;
1743ae8c6e27Sflorian 	if(inet_pton(AF_INET, (char*)str, &address) != 1)
1744ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_IP4;
1745ae8c6e27Sflorian 	if(*len < sizeof(address))
1746ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1747ae8c6e27Sflorian 	memmove(rd, &address, sizeof(address));
1748ae8c6e27Sflorian 	*len = sizeof(address);
1749ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1750ae8c6e27Sflorian }
1751ae8c6e27Sflorian 
1752ae8c6e27Sflorian int sldns_str2wire_aaaa_buf(const char* str, uint8_t* rd, size_t* len)
1753ae8c6e27Sflorian {
1754ae8c6e27Sflorian #ifdef AF_INET6
1755ae8c6e27Sflorian 	uint8_t address[LDNS_IP6ADDRLEN + 1];
1756ae8c6e27Sflorian 	if(inet_pton(AF_INET6, (char*)str, address) != 1)
1757ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_IP6;
1758ae8c6e27Sflorian 	if(*len < LDNS_IP6ADDRLEN)
1759ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1760ae8c6e27Sflorian 	memmove(rd, address, LDNS_IP6ADDRLEN);
1761ae8c6e27Sflorian 	*len = LDNS_IP6ADDRLEN;
1762ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1763ae8c6e27Sflorian #else
1764ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_NOT_IMPL;
1765ae8c6e27Sflorian #endif
1766ae8c6e27Sflorian }
1767ae8c6e27Sflorian 
1768ae8c6e27Sflorian int sldns_str2wire_str_buf(const char* str, uint8_t* rd, size_t* len)
1769ae8c6e27Sflorian {
1770ae8c6e27Sflorian 	uint8_t ch = 0;
1771ae8c6e27Sflorian 	size_t sl = 0;
1772ae8c6e27Sflorian 	const char* s = str;
1773ae8c6e27Sflorian 	/* skip length byte */
1774ae8c6e27Sflorian 	if(*len < 1)
1775ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1776ae8c6e27Sflorian 
1777ae8c6e27Sflorian 	/* read characters */
1778ae8c6e27Sflorian 	while(sldns_parse_char(&ch, &s)) {
1779ae8c6e27Sflorian 		if(sl >= 255)
1780ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR, s-str);
178157403691Sflorian 		if(*len < sl+2)
1782ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
1783ae8c6e27Sflorian 				s-str);
1784ae8c6e27Sflorian 		rd[++sl] = ch;
1785ae8c6e27Sflorian 	}
1786ae8c6e27Sflorian 	if(!s)
1787ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_BAD_ESCAPE;
1788ae8c6e27Sflorian 	rd[0] = (uint8_t)sl;
1789ae8c6e27Sflorian 	*len = sl+1;
1790ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1791ae8c6e27Sflorian }
1792ae8c6e27Sflorian 
1793ae8c6e27Sflorian int sldns_str2wire_apl_buf(const char* str, uint8_t* rd, size_t* len)
1794ae8c6e27Sflorian {
1795ae8c6e27Sflorian 	const char *my_str = str;
1796ae8c6e27Sflorian 
1797ae8c6e27Sflorian 	char my_ip_str[64];
1798ae8c6e27Sflorian 	size_t ip_str_len;
1799ae8c6e27Sflorian 
1800ae8c6e27Sflorian 	uint16_t family;
1801ae8c6e27Sflorian 	int negation;
1802ae8c6e27Sflorian 	size_t adflength = 0;
1803ae8c6e27Sflorian 	uint8_t data[16+4];
1804ae8c6e27Sflorian 	uint8_t prefix;
1805ae8c6e27Sflorian 	size_t i;
1806ae8c6e27Sflorian 
1807ae8c6e27Sflorian 	if(*my_str == '\0') {
1808ae8c6e27Sflorian 		/* empty APL element, no data, no string */
1809ae8c6e27Sflorian 		*len = 0;
1810ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_OK;
1811ae8c6e27Sflorian 	}
1812ae8c6e27Sflorian 
1813ae8c6e27Sflorian 	/* [!]afi:address/prefix */
1814ae8c6e27Sflorian 	if (strlen(my_str) < 2
1815ae8c6e27Sflorian 			|| strchr(my_str, ':') == NULL
1816ae8c6e27Sflorian 			|| strchr(my_str, '/') == NULL
1817ae8c6e27Sflorian 			|| strchr(my_str, ':') > strchr(my_str, '/')) {
1818ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_INVALID_STR;
1819ae8c6e27Sflorian 	}
1820ae8c6e27Sflorian 
1821ae8c6e27Sflorian 	if (my_str[0] == '!') {
1822ae8c6e27Sflorian 		negation = 1;
1823ae8c6e27Sflorian 		my_str += 1;
1824ae8c6e27Sflorian 	} else {
1825ae8c6e27Sflorian 		negation = 0;
1826ae8c6e27Sflorian 	}
1827ae8c6e27Sflorian 
1828ae8c6e27Sflorian 	family = (uint16_t) atoi(my_str);
1829ae8c6e27Sflorian 
1830ae8c6e27Sflorian 	my_str = strchr(my_str, ':') + 1;
1831ae8c6e27Sflorian 
1832ae8c6e27Sflorian 	/* need ip addr and only ip addr for inet_pton */
1833ae8c6e27Sflorian 	ip_str_len = (size_t) (strchr(my_str, '/') - my_str);
1834ae8c6e27Sflorian 	if(ip_str_len+1 > sizeof(my_ip_str))
1835ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_INVALID_STR;
1836ae8c6e27Sflorian 	(void)strlcpy(my_ip_str, my_str, sizeof(my_ip_str));
1837ae8c6e27Sflorian 	my_ip_str[ip_str_len] = 0;
1838ae8c6e27Sflorian 
1839ae8c6e27Sflorian 	if (family == 1) {
1840ae8c6e27Sflorian 		/* ipv4 */
1841ae8c6e27Sflorian 		if(inet_pton(AF_INET, my_ip_str, data+4) == 0)
1842ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_INVALID_STR;
1843ae8c6e27Sflorian 		for (i = 0; i < 4; i++) {
1844ae8c6e27Sflorian 			if (data[i+4] != 0) {
1845ae8c6e27Sflorian 				adflength = i + 1;
1846ae8c6e27Sflorian 			}
1847ae8c6e27Sflorian 		}
1848ae8c6e27Sflorian 	} else if (family == 2) {
1849ae8c6e27Sflorian 		/* ipv6 */
1850ae8c6e27Sflorian 		if (inet_pton(AF_INET6, my_ip_str, data+4) == 0)
1851ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_INVALID_STR;
1852ae8c6e27Sflorian 		for (i = 0; i < 16; i++) {
1853ae8c6e27Sflorian 			if (data[i+4] != 0) {
1854ae8c6e27Sflorian 				adflength = i + 1;
1855ae8c6e27Sflorian 			}
1856ae8c6e27Sflorian 		}
1857ae8c6e27Sflorian 	} else {
1858ae8c6e27Sflorian 		/* unknown family */
1859ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_INVALID_STR;
1860ae8c6e27Sflorian 	}
1861ae8c6e27Sflorian 
1862ae8c6e27Sflorian 	my_str = strchr(my_str, '/') + 1;
1863ae8c6e27Sflorian 	prefix = (uint8_t) atoi(my_str);
1864ae8c6e27Sflorian 
1865ae8c6e27Sflorian 	sldns_write_uint16(data, family);
1866ae8c6e27Sflorian 	data[2] = prefix;
1867ae8c6e27Sflorian 	data[3] = (uint8_t)adflength;
1868ae8c6e27Sflorian 	if (negation) {
1869ae8c6e27Sflorian 		/* set bit 1 of byte 3 */
1870ae8c6e27Sflorian 		data[3] = data[3] | 0x80;
1871ae8c6e27Sflorian 	}
1872ae8c6e27Sflorian 
1873ae8c6e27Sflorian 	if(*len < 4+adflength)
1874ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1875ae8c6e27Sflorian 	memmove(rd, data, 4+adflength);
1876ae8c6e27Sflorian 	*len = 4+adflength;
1877ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1878ae8c6e27Sflorian }
1879ae8c6e27Sflorian 
1880ae8c6e27Sflorian int sldns_str2wire_b64_buf(const char* str, uint8_t* rd, size_t* len)
1881ae8c6e27Sflorian {
1882ae8c6e27Sflorian 	size_t sz = sldns_b64_pton_calculate_size(strlen(str));
1883ae8c6e27Sflorian 	int n;
1884ae8c6e27Sflorian 	if(strcmp(str, "0") == 0) {
1885ae8c6e27Sflorian 		*len = 0;
1886ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_OK;
1887ae8c6e27Sflorian 	}
1888ae8c6e27Sflorian 	if(*len < sz)
1889ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1890ae8c6e27Sflorian 	n = sldns_b64_pton(str, rd, *len);
1891ae8c6e27Sflorian 	if(n < 0)
1892ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_B64;
1893ae8c6e27Sflorian 	*len = (size_t)n;
1894ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1895ae8c6e27Sflorian }
1896ae8c6e27Sflorian 
1897ae8c6e27Sflorian int sldns_str2wire_b32_ext_buf(const char* str, uint8_t* rd, size_t* len)
1898ae8c6e27Sflorian {
1899ae8c6e27Sflorian 	size_t slen = strlen(str);
1900ae8c6e27Sflorian 	size_t sz = sldns_b32_pton_calculate_size(slen);
1901ae8c6e27Sflorian 	int n;
1902ae8c6e27Sflorian 	if(*len < 1+sz)
1903ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1904ae8c6e27Sflorian 	rd[0] = (uint8_t)sz;
1905ae8c6e27Sflorian 	n = sldns_b32_pton_extended_hex(str, slen, rd+1, *len-1);
1906ae8c6e27Sflorian 	if(n < 0)
1907ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_B32_EXT;
1908ae8c6e27Sflorian 	*len = (size_t)n+1;
1909ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1910ae8c6e27Sflorian }
1911ae8c6e27Sflorian 
1912ae8c6e27Sflorian /** see if the string ends, or ends in whitespace */
1913ae8c6e27Sflorian static int
1914ae8c6e27Sflorian sldns_is_last_of_string(const char* str)
1915ae8c6e27Sflorian {
1916ae8c6e27Sflorian 	if(*str == 0) return 1;
1917ae8c6e27Sflorian 	while(isspace((unsigned char)*str))
1918ae8c6e27Sflorian 		str++;
1919ae8c6e27Sflorian 	if(*str == 0) return 1;
1920ae8c6e27Sflorian 	return 0;
1921ae8c6e27Sflorian }
1922ae8c6e27Sflorian 
1923ae8c6e27Sflorian int sldns_str2wire_hex_buf(const char* str, uint8_t* rd, size_t* len)
1924ae8c6e27Sflorian {
1925ae8c6e27Sflorian 	const char* s = str;
1926ae8c6e27Sflorian 	size_t dlen = 0; /* number of hexdigits parsed */
1927ae8c6e27Sflorian 	while(*s) {
1928ae8c6e27Sflorian 		if(isspace((unsigned char)*s)) {
1929ae8c6e27Sflorian 			s++;
1930ae8c6e27Sflorian 			continue;
1931ae8c6e27Sflorian 		}
1932ae8c6e27Sflorian 		if(dlen == 0 && *s == '0' && sldns_is_last_of_string(s+1)) {
1933ae8c6e27Sflorian 			*len = 0;
1934ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_OK;
1935ae8c6e27Sflorian 		}
1936ae8c6e27Sflorian 		if(!isxdigit((unsigned char)*s))
1937ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str);
1938ae8c6e27Sflorian 		if(*len < dlen/2 + 1)
1939ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
1940ae8c6e27Sflorian 				s-str);
1941ae8c6e27Sflorian 		if((dlen&1)==0)
1942ae8c6e27Sflorian 			rd[dlen/2] = (uint8_t)sldns_hexdigit_to_int(*s++) * 16;
1943ae8c6e27Sflorian 		else	rd[dlen/2] += (uint8_t)sldns_hexdigit_to_int(*s++);
1944ae8c6e27Sflorian 		dlen++;
1945ae8c6e27Sflorian 	}
1946ae8c6e27Sflorian 	if((dlen&1)!=0)
1947ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str);
1948ae8c6e27Sflorian 	*len = dlen/2;
1949ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1950ae8c6e27Sflorian }
1951ae8c6e27Sflorian 
1952ae8c6e27Sflorian int sldns_str2wire_nsec_buf(const char* str, uint8_t* rd, size_t* len)
1953ae8c6e27Sflorian {
1954ae8c6e27Sflorian 	const char *delim = "\n\t ";
1955ae8c6e27Sflorian 	char token[64]; /* for a type name */
1956ae8c6e27Sflorian 	size_t type_count = 0;
1957ae8c6e27Sflorian 	int block;
1958ae8c6e27Sflorian 	size_t used = 0;
1959ae8c6e27Sflorian 	uint16_t maxtype = 0;
1960ae8c6e27Sflorian 	uint8_t typebits[8192]; /* 65536 bits */
1961ae8c6e27Sflorian 	uint8_t window_in_use[256];
1962ae8c6e27Sflorian 
1963ae8c6e27Sflorian 	/* string in buffer */
1964ae8c6e27Sflorian 	sldns_buffer strbuf;
1965ae8c6e27Sflorian 	sldns_buffer_init_frm_data(&strbuf, (uint8_t*)str, strlen(str));
1966ae8c6e27Sflorian 
1967ae8c6e27Sflorian 	/* parse the types */
1968ae8c6e27Sflorian 	memset(typebits, 0, sizeof(typebits));
1969ae8c6e27Sflorian 	memset(window_in_use, 0, sizeof(window_in_use));
1970ae8c6e27Sflorian 	while(sldns_buffer_remaining(&strbuf) > 0 &&
1971ae8c6e27Sflorian 		sldns_bget_token(&strbuf, token, delim, sizeof(token)) != -1) {
1972ae8c6e27Sflorian 		uint16_t t = sldns_get_rr_type_by_name(token);
1973ae8c6e27Sflorian 		if(token[0] == 0)
1974ae8c6e27Sflorian 			continue;
1975ae8c6e27Sflorian 		if(t == 0 && strcmp(token, "TYPE0") != 0)
1976ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TYPE,
1977ae8c6e27Sflorian 				sldns_buffer_position(&strbuf));
1978ae8c6e27Sflorian 		typebits[t/8] |= (0x80>>(t%8));
1979ae8c6e27Sflorian 		window_in_use[t/256] = 1;
1980ae8c6e27Sflorian 		type_count++;
1981ae8c6e27Sflorian 		if(t > maxtype) maxtype = t;
1982ae8c6e27Sflorian 	}
1983ae8c6e27Sflorian 
1984ae8c6e27Sflorian 	/* empty NSEC bitmap */
1985ae8c6e27Sflorian 	if(type_count == 0) {
1986ae8c6e27Sflorian 		*len = 0;
1987ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_OK;
1988ae8c6e27Sflorian 	}
1989ae8c6e27Sflorian 
1990ae8c6e27Sflorian 	/* encode windows {u8 windowblock, u8 bitmaplength, 0-32u8 bitmap},
1991ae8c6e27Sflorian 	 * block is 0-255 upper octet of types, length if 0-32. */
1992ae8c6e27Sflorian 	for(block = 0; block <= (int)maxtype/256; block++) {
1993ae8c6e27Sflorian 		int i, blocklen = 0;
1994ae8c6e27Sflorian 		if(!window_in_use[block])
1995ae8c6e27Sflorian 			continue;
1996ae8c6e27Sflorian 		for(i=0; i<32; i++) {
1997ae8c6e27Sflorian 			if(typebits[block*32+i] != 0)
1998ae8c6e27Sflorian 				blocklen = i+1;
1999ae8c6e27Sflorian 		}
2000ae8c6e27Sflorian 		if(blocklen == 0)
2001ae8c6e27Sflorian 			continue; /* empty window should have been !in_use */
2002ae8c6e27Sflorian 		if(used+blocklen+2 > *len)
2003ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2004ae8c6e27Sflorian 		rd[used+0] = (uint8_t)block;
2005ae8c6e27Sflorian 		rd[used+1] = (uint8_t)blocklen;
2006ae8c6e27Sflorian 		for(i=0; i<blocklen; i++) {
2007ae8c6e27Sflorian 			rd[used+2+i] = typebits[block*32+i];
2008ae8c6e27Sflorian 		}
2009ae8c6e27Sflorian 		used += blocklen+2;
2010ae8c6e27Sflorian 	}
2011ae8c6e27Sflorian 	*len = used;
2012ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2013ae8c6e27Sflorian }
2014ae8c6e27Sflorian 
2015ae8c6e27Sflorian int sldns_str2wire_type_buf(const char* str, uint8_t* rd, size_t* len)
2016ae8c6e27Sflorian {
2017ae8c6e27Sflorian 	uint16_t t = sldns_get_rr_type_by_name(str);
2018ae8c6e27Sflorian 	if(t == 0 && strcmp(str, "TYPE0") != 0)
2019ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_TYPE;
2020ae8c6e27Sflorian 	if(*len < 2)
2021ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2022ae8c6e27Sflorian 	sldns_write_uint16(rd, t);
2023ae8c6e27Sflorian 	*len = 2;
2024ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2025ae8c6e27Sflorian }
2026ae8c6e27Sflorian 
2027ae8c6e27Sflorian int sldns_str2wire_class_buf(const char* str, uint8_t* rd, size_t* len)
2028ae8c6e27Sflorian {
2029ae8c6e27Sflorian 	uint16_t c = sldns_get_rr_class_by_name(str);
2030ae8c6e27Sflorian 	if(c == 0 && strcmp(str, "CLASS0") != 0)
2031ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_CLASS;
2032ae8c6e27Sflorian 	if(*len < 2)
2033ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2034ae8c6e27Sflorian 	sldns_write_uint16(rd, c);
2035ae8c6e27Sflorian 	*len = 2;
2036ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2037ae8c6e27Sflorian }
2038ae8c6e27Sflorian 
2039ae8c6e27Sflorian /* An certificate alg field can either be specified as a 8 bits number
2040ae8c6e27Sflorian  * or by its symbolic name. Handle both */
2041ae8c6e27Sflorian int sldns_str2wire_cert_alg_buf(const char* str, uint8_t* rd, size_t* len)
2042ae8c6e27Sflorian {
2043ae8c6e27Sflorian 	sldns_lookup_table *lt = sldns_lookup_by_name(sldns_cert_algorithms,
2044ae8c6e27Sflorian 		str);
2045ae8c6e27Sflorian 	if(*len < 2)
2046ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2047ae8c6e27Sflorian 	if(lt) {
2048ae8c6e27Sflorian 		sldns_write_uint16(rd, (uint16_t)lt->id);
2049ae8c6e27Sflorian 	} else {
2050ae8c6e27Sflorian 		int s = sldns_str2wire_int16_buf(str, rd, len);
2051ae8c6e27Sflorian 		if(s) return s;
2052ae8c6e27Sflorian 		if(sldns_read_uint16(rd) == 0)
2053ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_CERT_BAD_ALGORITHM;
2054ae8c6e27Sflorian 	}
2055ae8c6e27Sflorian 	*len = 2;
2056ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2057ae8c6e27Sflorian }
2058ae8c6e27Sflorian 
2059ae8c6e27Sflorian /* An alg field can either be specified as a 8 bits number
2060ae8c6e27Sflorian  * or by its symbolic name. Handle both */
2061ae8c6e27Sflorian int sldns_str2wire_alg_buf(const char* str, uint8_t* rd, size_t* len)
2062ae8c6e27Sflorian {
2063ae8c6e27Sflorian 	sldns_lookup_table *lt = sldns_lookup_by_name(sldns_algorithms, str);
2064ae8c6e27Sflorian 	if(*len < 1)
2065ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2066ae8c6e27Sflorian 	if(lt) {
2067ae8c6e27Sflorian 		rd[0] = (uint8_t)lt->id;
2068ae8c6e27Sflorian 		*len = 1;
2069ae8c6e27Sflorian 	} else {
2070ae8c6e27Sflorian 		/* try as-is (a number) */
2071ae8c6e27Sflorian 		return sldns_str2wire_int8_buf(str, rd, len);
2072ae8c6e27Sflorian 	}
2073ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2074ae8c6e27Sflorian }
2075ae8c6e27Sflorian 
2076ae8c6e27Sflorian int sldns_str2wire_tsigerror_buf(const char* str, uint8_t* rd, size_t* len)
2077ae8c6e27Sflorian {
2078ae8c6e27Sflorian 	sldns_lookup_table *lt = sldns_lookup_by_name(sldns_tsig_errors, str);
2079ae8c6e27Sflorian 	if(*len < 2)
2080ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2081ae8c6e27Sflorian 	if(lt) {
2082ae8c6e27Sflorian 		sldns_write_uint16(rd, (uint16_t)lt->id);
2083ae8c6e27Sflorian 		*len = 2;
2084ae8c6e27Sflorian 	} else {
2085ae8c6e27Sflorian 		/* try as-is (a number) */
2086ae8c6e27Sflorian 		return sldns_str2wire_int16_buf(str, rd, len);
2087ae8c6e27Sflorian 	}
2088ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2089ae8c6e27Sflorian }
2090ae8c6e27Sflorian 
2091ae8c6e27Sflorian int sldns_str2wire_time_buf(const char* str, uint8_t* rd, size_t* len)
2092ae8c6e27Sflorian {
2093ae8c6e27Sflorian 	/* convert a time YYYYDDMMHHMMSS to wireformat */
2094ae8c6e27Sflorian 	struct tm tm;
2095ae8c6e27Sflorian 	if(*len < 4)
2096ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2097ae8c6e27Sflorian 
2098ae8c6e27Sflorian 	/* Try to scan the time... */
2099ae8c6e27Sflorian 	memset(&tm, 0, sizeof(tm));
2100ae8c6e27Sflorian 	if (strlen(str) == 14 && sscanf(str, "%4d%2d%2d%2d%2d%2d",
2101ae8c6e27Sflorian 		&tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour,
2102ae8c6e27Sflorian 		&tm.tm_min, &tm.tm_sec) == 6) {
2103ae8c6e27Sflorian 	   	tm.tm_year -= 1900;
2104ae8c6e27Sflorian 	   	tm.tm_mon--;
2105ae8c6e27Sflorian 	   	/* Check values */
2106ae8c6e27Sflorian 		if (tm.tm_year < 70)
2107ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_SYNTAX_TIME;
2108ae8c6e27Sflorian 		if (tm.tm_mon < 0 || tm.tm_mon > 11)
2109ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_SYNTAX_TIME;
2110ae8c6e27Sflorian 		if (tm.tm_mday < 1 || tm.tm_mday > 31)
2111ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_SYNTAX_TIME;
2112ae8c6e27Sflorian 		if (tm.tm_hour < 0 || tm.tm_hour > 23)
2113ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_SYNTAX_TIME;
2114ae8c6e27Sflorian 		if (tm.tm_min < 0 || tm.tm_min > 59)
2115ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_SYNTAX_TIME;
2116ae8c6e27Sflorian 		if (tm.tm_sec < 0 || tm.tm_sec > 59)
2117ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_SYNTAX_TIME;
2118ae8c6e27Sflorian 
2119ae8c6e27Sflorian 		sldns_write_uint32(rd, (uint32_t)sldns_mktime_from_utc(&tm));
2120ae8c6e27Sflorian 	} else {
2121ae8c6e27Sflorian 		/* handle it as 32 bits timestamp */
2122ae8c6e27Sflorian 		char *end;
2123ae8c6e27Sflorian 		uint32_t l = (uint32_t)strtol((char*)str, &end, 10);
2124ae8c6e27Sflorian 		if(*end != 0)
2125ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TIME,
2126ae8c6e27Sflorian 				end-(char*)str);
2127ae8c6e27Sflorian 		sldns_write_uint32(rd, l);
2128ae8c6e27Sflorian 	}
2129ae8c6e27Sflorian 	*len = 4;
2130ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2131ae8c6e27Sflorian }
2132ae8c6e27Sflorian 
2133ae8c6e27Sflorian int sldns_str2wire_tsigtime_buf(const char* str, uint8_t* rd, size_t* len)
2134ae8c6e27Sflorian {
2135ae8c6e27Sflorian 	char* end;
2136ae8c6e27Sflorian 	uint64_t t = (uint64_t)strtol((char*)str, &end, 10);
2137ae8c6e27Sflorian 	uint16_t high;
2138ae8c6e27Sflorian 	uint32_t low;
2139ae8c6e27Sflorian 	if(*end != 0)
2140ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TIME, end-str);
2141ae8c6e27Sflorian 	if(*len < 6)
2142ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2143ae8c6e27Sflorian 	high = (uint16_t)(t>>32);
2144ae8c6e27Sflorian 	low = (uint32_t)(t);
2145ae8c6e27Sflorian 	sldns_write_uint16(rd, high);
2146ae8c6e27Sflorian 	sldns_write_uint32(rd+2, low);
2147ae8c6e27Sflorian 	*len = 6;
2148ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2149ae8c6e27Sflorian }
2150ae8c6e27Sflorian 
2151ae8c6e27Sflorian int sldns_str2wire_period_buf(const char* str, uint8_t* rd, size_t* len)
2152ae8c6e27Sflorian {
2153ae8c6e27Sflorian 	const char* end;
2154ae8c6e27Sflorian 	uint32_t p = sldns_str2period(str, &end);
2155ae8c6e27Sflorian 	if(*end != 0)
2156ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_PERIOD, end-str);
2157ae8c6e27Sflorian 	if(*len < 4)
2158ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2159ae8c6e27Sflorian 	sldns_write_uint32(rd, p);
2160ae8c6e27Sflorian 	*len = 4;
2161ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2162ae8c6e27Sflorian }
2163ae8c6e27Sflorian 
2164ae8c6e27Sflorian /** read "<digits>[.<digits>][mM]" into mantissa exponent format for LOC type */
2165ae8c6e27Sflorian static int
2166ae8c6e27Sflorian loc_parse_cm(char* my_str, char** endstr, uint8_t* m, uint8_t* e)
2167ae8c6e27Sflorian {
2168ae8c6e27Sflorian 	uint32_t meters = 0, cm = 0, val;
2169411c5950Sflorian 	char* cm_endstr;
2170ae8c6e27Sflorian 	while (isblank((unsigned char)*my_str)) {
2171ae8c6e27Sflorian 		my_str++;
2172ae8c6e27Sflorian 	}
2173ae8c6e27Sflorian 	meters = (uint32_t)strtol(my_str, &my_str, 10);
2174ae8c6e27Sflorian 	if (*my_str == '.') {
2175ae8c6e27Sflorian 		my_str++;
2176411c5950Sflorian 		cm = (uint32_t)strtol(my_str, &cm_endstr, 10);
2177411c5950Sflorian 		if(cm_endstr == my_str + 1)
2178411c5950Sflorian 			cm *= 10;
2179411c5950Sflorian 		my_str = cm_endstr;
2180ae8c6e27Sflorian 	}
2181ae8c6e27Sflorian 	if (meters >= 1) {
2182ae8c6e27Sflorian 		*e = 2;
2183ae8c6e27Sflorian 		val = meters;
2184ae8c6e27Sflorian 	} else	{
2185ae8c6e27Sflorian 		*e = 0;
2186ae8c6e27Sflorian 		val = cm;
2187ae8c6e27Sflorian 	}
2188ae8c6e27Sflorian 	while(val >= 10) {
2189ae8c6e27Sflorian 		(*e)++;
2190ae8c6e27Sflorian 		val /= 10;
2191ae8c6e27Sflorian 	}
2192ae8c6e27Sflorian 	*m = (uint8_t)val;
2193ae8c6e27Sflorian 
2194ae8c6e27Sflorian 	if (*e > 9)
2195ae8c6e27Sflorian 		return 0;
2196ae8c6e27Sflorian 	if (*my_str == 'm' || *my_str == 'M') {
2197ae8c6e27Sflorian 		my_str++;
2198ae8c6e27Sflorian 	}
2199ae8c6e27Sflorian 	*endstr = my_str;
2200ae8c6e27Sflorian 	return 1;
2201ae8c6e27Sflorian }
2202ae8c6e27Sflorian 
2203ae8c6e27Sflorian int sldns_str2wire_loc_buf(const char* str, uint8_t* rd, size_t* len)
2204ae8c6e27Sflorian {
2205ae8c6e27Sflorian 	uint32_t latitude = 0;
2206ae8c6e27Sflorian 	uint32_t longitude = 0;
2207ae8c6e27Sflorian 	uint32_t altitude = 0;
2208ae8c6e27Sflorian 
2209ae8c6e27Sflorian 	uint32_t equator = (uint32_t)1<<31; /* 2**31 */
2210ae8c6e27Sflorian 
2211ae8c6e27Sflorian 	/* only support version 0 */
2212ae8c6e27Sflorian 	uint32_t h = 0;
2213ae8c6e27Sflorian 	uint32_t m = 0;
2214ae8c6e27Sflorian 	uint8_t size_b = 1, size_e = 2;
2215ae8c6e27Sflorian 	uint8_t horiz_pre_b = 1, horiz_pre_e = 6;
2216ae8c6e27Sflorian 	uint8_t vert_pre_b = 1, vert_pre_e = 3;
2217ae8c6e27Sflorian 
2218ae8c6e27Sflorian 	double s = 0.0;
2219ae8c6e27Sflorian 	int northerness;
2220ae8c6e27Sflorian 	int easterness;
2221ae8c6e27Sflorian 
2222ae8c6e27Sflorian 	char *my_str = (char *) str;
2223ae8c6e27Sflorian 
2224ae8c6e27Sflorian 	if (isdigit((unsigned char) *my_str)) {
2225ae8c6e27Sflorian 		h = (uint32_t) strtol(my_str, &my_str, 10);
2226ae8c6e27Sflorian 	} else {
2227ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_INVALID_STR;
2228ae8c6e27Sflorian 	}
2229ae8c6e27Sflorian 
2230ae8c6e27Sflorian 	while (isblank((unsigned char) *my_str)) {
2231ae8c6e27Sflorian 		my_str++;
2232ae8c6e27Sflorian 	}
2233ae8c6e27Sflorian 
2234ae8c6e27Sflorian 	if (isdigit((unsigned char) *my_str)) {
2235ae8c6e27Sflorian 		m = (uint32_t) strtol(my_str, &my_str, 10);
2236ae8c6e27Sflorian 	} else if (*my_str == 'N' || *my_str == 'S') {
2237ae8c6e27Sflorian 		goto north;
2238ae8c6e27Sflorian 	} else {
2239ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_INVALID_STR;
2240ae8c6e27Sflorian 	}
2241ae8c6e27Sflorian 
2242ae8c6e27Sflorian 	while (isblank((unsigned char) *my_str)) {
2243ae8c6e27Sflorian 		my_str++;
2244ae8c6e27Sflorian 	}
2245ae8c6e27Sflorian 
2246ae8c6e27Sflorian 	if (isdigit((unsigned char) *my_str)) {
2247ae8c6e27Sflorian 		s = strtod(my_str, &my_str);
2248ae8c6e27Sflorian 	}
2249ae8c6e27Sflorian 
2250ae8c6e27Sflorian 	/* skip blanks before northerness */
2251ae8c6e27Sflorian 	while (isblank((unsigned char) *my_str)) {
2252ae8c6e27Sflorian 		my_str++;
2253ae8c6e27Sflorian 	}
2254ae8c6e27Sflorian 
2255ae8c6e27Sflorian north:
2256ae8c6e27Sflorian 	if (*my_str == 'N') {
2257ae8c6e27Sflorian 		northerness = 1;
2258ae8c6e27Sflorian 	} else if (*my_str == 'S') {
2259ae8c6e27Sflorian 		northerness = 0;
2260ae8c6e27Sflorian 	} else {
2261ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_INVALID_STR;
2262ae8c6e27Sflorian 	}
2263ae8c6e27Sflorian 
2264ae8c6e27Sflorian 	my_str++;
2265ae8c6e27Sflorian 
2266ae8c6e27Sflorian 	/* store number */
2267ae8c6e27Sflorian 	s = 1000.0 * s;
2268ae8c6e27Sflorian 	/* add a little to make floor in conversion a round */
2269ae8c6e27Sflorian 	s += 0.0005;
2270ae8c6e27Sflorian 	latitude = (uint32_t) s;
2271ae8c6e27Sflorian 	latitude += 1000 * 60 * m;
2272ae8c6e27Sflorian 	latitude += 1000 * 60 * 60 * h;
2273ae8c6e27Sflorian 	if (northerness) {
2274ae8c6e27Sflorian 		latitude = equator + latitude;
2275ae8c6e27Sflorian 	} else {
2276ae8c6e27Sflorian 		latitude = equator - latitude;
2277ae8c6e27Sflorian 	}
2278ae8c6e27Sflorian 	while (isblank((unsigned char)*my_str)) {
2279ae8c6e27Sflorian 		my_str++;
2280ae8c6e27Sflorian 	}
2281ae8c6e27Sflorian 
2282ae8c6e27Sflorian 	if (isdigit((unsigned char) *my_str)) {
2283ae8c6e27Sflorian 		h = (uint32_t) strtol(my_str, &my_str, 10);
2284ae8c6e27Sflorian 	} else {
2285ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_INVALID_STR;
2286ae8c6e27Sflorian 	}
2287ae8c6e27Sflorian 
2288ae8c6e27Sflorian 	while (isblank((unsigned char) *my_str)) {
2289ae8c6e27Sflorian 		my_str++;
2290ae8c6e27Sflorian 	}
2291ae8c6e27Sflorian 
2292ae8c6e27Sflorian 	if (isdigit((unsigned char) *my_str)) {
2293ae8c6e27Sflorian 		m = (uint32_t) strtol(my_str, &my_str, 10);
2294ae8c6e27Sflorian 	} else if (*my_str == 'E' || *my_str == 'W') {
2295ae8c6e27Sflorian 		goto east;
2296ae8c6e27Sflorian 	} else {
2297ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_INVALID_STR;
2298ae8c6e27Sflorian 	}
2299ae8c6e27Sflorian 
2300ae8c6e27Sflorian 	while (isblank((unsigned char)*my_str)) {
2301ae8c6e27Sflorian 		my_str++;
2302ae8c6e27Sflorian 	}
2303ae8c6e27Sflorian 
2304ae8c6e27Sflorian 	if (isdigit((unsigned char) *my_str)) {
2305ae8c6e27Sflorian 		s = strtod(my_str, &my_str);
2306ae8c6e27Sflorian 	}
2307ae8c6e27Sflorian 
2308ae8c6e27Sflorian 	/* skip blanks before easterness */
2309ae8c6e27Sflorian 	while (isblank((unsigned char)*my_str)) {
2310ae8c6e27Sflorian 		my_str++;
2311ae8c6e27Sflorian 	}
2312ae8c6e27Sflorian 
2313ae8c6e27Sflorian east:
2314ae8c6e27Sflorian 	if (*my_str == 'E') {
2315ae8c6e27Sflorian 		easterness = 1;
2316ae8c6e27Sflorian 	} else if (*my_str == 'W') {
2317ae8c6e27Sflorian 		easterness = 0;
2318ae8c6e27Sflorian 	} else {
2319ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_INVALID_STR;
2320ae8c6e27Sflorian 	}
2321ae8c6e27Sflorian 
2322ae8c6e27Sflorian 	my_str++;
2323ae8c6e27Sflorian 
2324ae8c6e27Sflorian 	/* store number */
2325ae8c6e27Sflorian 	s *= 1000.0;
2326ae8c6e27Sflorian 	/* add a little to make floor in conversion a round */
2327ae8c6e27Sflorian 	s += 0.0005;
2328ae8c6e27Sflorian 	longitude = (uint32_t) s;
2329ae8c6e27Sflorian 	longitude += 1000 * 60 * m;
2330ae8c6e27Sflorian 	longitude += 1000 * 60 * 60 * h;
2331ae8c6e27Sflorian 
2332ae8c6e27Sflorian 	if (easterness) {
2333ae8c6e27Sflorian 		longitude += equator;
2334ae8c6e27Sflorian 	} else {
2335ae8c6e27Sflorian 		longitude = equator - longitude;
2336ae8c6e27Sflorian 	}
2337ae8c6e27Sflorian 
2338ae8c6e27Sflorian 	altitude = (uint32_t)(strtod(my_str, &my_str)*100.0 +
2339ae8c6e27Sflorian 		10000000.0 + 0.5);
2340ae8c6e27Sflorian 	if (*my_str == 'm' || *my_str == 'M') {
2341ae8c6e27Sflorian 		my_str++;
2342ae8c6e27Sflorian 	}
2343ae8c6e27Sflorian 
2344ae8c6e27Sflorian 	if (strlen(my_str) > 0) {
2345ae8c6e27Sflorian 		if(!loc_parse_cm(my_str, &my_str, &size_b, &size_e))
2346ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_INVALID_STR;
2347ae8c6e27Sflorian 	}
2348ae8c6e27Sflorian 
2349ae8c6e27Sflorian 	if (strlen(my_str) > 0) {
2350ae8c6e27Sflorian 		if(!loc_parse_cm(my_str, &my_str, &horiz_pre_b, &horiz_pre_e))
2351ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_INVALID_STR;
2352ae8c6e27Sflorian 	}
2353ae8c6e27Sflorian 
2354ae8c6e27Sflorian 	if (strlen(my_str) > 0) {
2355ae8c6e27Sflorian 		if(!loc_parse_cm(my_str, &my_str, &vert_pre_b, &vert_pre_e))
2356ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_INVALID_STR;
2357ae8c6e27Sflorian 	}
2358ae8c6e27Sflorian 
2359ae8c6e27Sflorian 	if(*len < 16)
2360ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2361ae8c6e27Sflorian 	rd[0] = 0;
2362ae8c6e27Sflorian 	rd[1] = ((size_b << 4) & 0xf0) | (size_e & 0x0f);
2363ae8c6e27Sflorian 	rd[2] = ((horiz_pre_b << 4) & 0xf0) | (horiz_pre_e & 0x0f);
2364ae8c6e27Sflorian 	rd[3] = ((vert_pre_b << 4) & 0xf0) | (vert_pre_e & 0x0f);
2365ae8c6e27Sflorian 	sldns_write_uint32(rd + 4, latitude);
2366ae8c6e27Sflorian 	sldns_write_uint32(rd + 8, longitude);
2367ae8c6e27Sflorian 	sldns_write_uint32(rd + 12, altitude);
2368ae8c6e27Sflorian 	*len = 16;
2369ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2370ae8c6e27Sflorian }
2371ae8c6e27Sflorian 
2372ae8c6e27Sflorian static void
2373ae8c6e27Sflorian ldns_tolower_str(char* s)
2374ae8c6e27Sflorian {
2375ae8c6e27Sflorian 	if(s) {
2376ae8c6e27Sflorian 		while(*s) {
2377ae8c6e27Sflorian 			*s = (char)tolower((unsigned char)*s);
2378ae8c6e27Sflorian 			s++;
2379ae8c6e27Sflorian 		}
2380ae8c6e27Sflorian 	}
2381ae8c6e27Sflorian }
2382ae8c6e27Sflorian 
2383ae8c6e27Sflorian int sldns_str2wire_wks_buf(const char* str, uint8_t* rd, size_t* len)
2384ae8c6e27Sflorian {
2385ae8c6e27Sflorian 	int rd_len = 1;
2386ae8c6e27Sflorian 	int have_proto = 0;
2387ae8c6e27Sflorian 	char token[50], proto_str[50];
2388ae8c6e27Sflorian 	sldns_buffer strbuf;
2389ae8c6e27Sflorian 	sldns_buffer_init_frm_data(&strbuf, (uint8_t*)str, strlen(str));
2390ae8c6e27Sflorian 	proto_str[0]=0;
2391ae8c6e27Sflorian 
2392ae8c6e27Sflorian 	/* check we have one byte for proto */
2393ae8c6e27Sflorian 	if(*len < 1)
2394ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2395ae8c6e27Sflorian 
2396ae8c6e27Sflorian 	while(sldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) > 0) {
2397ae8c6e27Sflorian 		ldns_tolower_str(token);
2398ae8c6e27Sflorian 		if(!have_proto) {
2399ae8c6e27Sflorian 			struct protoent *p = getprotobyname(token);
2400ae8c6e27Sflorian 			have_proto = 1;
2401ae8c6e27Sflorian 			if(p) rd[0] = (uint8_t)p->p_proto;
2402ae8c6e27Sflorian 			else if(strcasecmp(token, "tcp")==0) rd[0]=6;
2403ae8c6e27Sflorian 			else if(strcasecmp(token, "udp")==0) rd[0]=17;
2404ae8c6e27Sflorian 			else rd[0] = (uint8_t)atoi(token);
2405ae8c6e27Sflorian 			(void)strlcpy(proto_str, token, sizeof(proto_str));
2406ae8c6e27Sflorian 		} else {
2407ae8c6e27Sflorian 			int serv_port;
2408ae8c6e27Sflorian 			struct servent *serv = getservbyname(token, proto_str);
2409ae8c6e27Sflorian 			if(serv) serv_port=(int)ntohs((uint16_t)serv->s_port);
2410ae8c6e27Sflorian 			else if(strcasecmp(token, "domain")==0) serv_port=53;
2411ae8c6e27Sflorian 			else {
2412ae8c6e27Sflorian 				serv_port = atoi(token);
2413ae8c6e27Sflorian 				if(serv_port == 0 && strcmp(token, "0") != 0) {
2414ae8c6e27Sflorian #ifdef HAVE_ENDSERVENT
2415ae8c6e27Sflorian 					endservent();
2416ae8c6e27Sflorian #endif
2417ae8c6e27Sflorian #ifdef HAVE_ENDPROTOENT
2418ae8c6e27Sflorian 					endprotoent();
2419ae8c6e27Sflorian #endif
2420ae8c6e27Sflorian 					return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX,
2421ae8c6e27Sflorian 						sldns_buffer_position(&strbuf));
2422ae8c6e27Sflorian 				}
2423ae8c6e27Sflorian 				if(serv_port < 0 || serv_port > 65535) {
2424ae8c6e27Sflorian #ifdef HAVE_ENDSERVENT
2425ae8c6e27Sflorian 					endservent();
2426ae8c6e27Sflorian #endif
2427ae8c6e27Sflorian #ifdef HAVE_ENDPROTOENT
2428ae8c6e27Sflorian 					endprotoent();
2429ae8c6e27Sflorian #endif
2430ae8c6e27Sflorian 					return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX,
2431ae8c6e27Sflorian 						sldns_buffer_position(&strbuf));
2432ae8c6e27Sflorian 				}
2433ae8c6e27Sflorian 			}
2434ae8c6e27Sflorian 			if(rd_len < 1+serv_port/8+1) {
2435ae8c6e27Sflorian 				/* bitmap is larger, init new bytes at 0 */
2436ae8c6e27Sflorian 				if(*len < 1+(size_t)serv_port/8+1) {
2437ae8c6e27Sflorian #ifdef HAVE_ENDSERVENT
2438ae8c6e27Sflorian 					endservent();
2439ae8c6e27Sflorian #endif
2440ae8c6e27Sflorian #ifdef HAVE_ENDPROTOENT
2441ae8c6e27Sflorian 					endprotoent();
2442ae8c6e27Sflorian #endif
2443ae8c6e27Sflorian 					return RET_ERR(
2444ae8c6e27Sflorian 					LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
2445ae8c6e27Sflorian 					sldns_buffer_position(&strbuf));
2446ae8c6e27Sflorian 				}
2447ae8c6e27Sflorian 				memset(rd+rd_len, 0, 1+(size_t)serv_port/8+1-rd_len);
2448ae8c6e27Sflorian 				rd_len = 1+serv_port/8+1;
2449ae8c6e27Sflorian 			}
2450ae8c6e27Sflorian 			rd[1+ serv_port/8] |= (1 << (7 - serv_port % 8));
2451ae8c6e27Sflorian 		}
2452ae8c6e27Sflorian 	}
2453ae8c6e27Sflorian 	*len = (size_t)rd_len;
2454ae8c6e27Sflorian 
2455ae8c6e27Sflorian #ifdef HAVE_ENDSERVENT
2456ae8c6e27Sflorian 	endservent();
2457ae8c6e27Sflorian #endif
2458ae8c6e27Sflorian #ifdef HAVE_ENDPROTOENT
2459ae8c6e27Sflorian 	endprotoent();
2460ae8c6e27Sflorian #endif
2461ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2462ae8c6e27Sflorian }
2463ae8c6e27Sflorian 
2464ae8c6e27Sflorian int sldns_str2wire_nsap_buf(const char* str, uint8_t* rd, size_t* len)
2465ae8c6e27Sflorian {
2466ae8c6e27Sflorian 	const char* s = str;
2467ae8c6e27Sflorian 	size_t slen;
2468ae8c6e27Sflorian 	size_t dlen = 0; /* number of hexdigits parsed */
2469ae8c6e27Sflorian 
2470ae8c6e27Sflorian 	/* just a hex string with optional dots? */
2471ae8c6e27Sflorian 	if (s[0] != '0' || s[1] != 'x')
2472ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_INVALID_STR;
2473ae8c6e27Sflorian 	s += 2;
2474ae8c6e27Sflorian 	slen = strlen(s);
2475ae8c6e27Sflorian 	if(slen > LDNS_MAX_RDFLEN*2)
2476ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_LABEL_OVERFLOW;
2477ae8c6e27Sflorian 	while(*s) {
2478ae8c6e27Sflorian 		if(isspace((unsigned char)*s) || *s == '.') {
2479ae8c6e27Sflorian 			s++;
2480ae8c6e27Sflorian 			continue;
2481ae8c6e27Sflorian 		}
2482ae8c6e27Sflorian 		if(!isxdigit((unsigned char)*s))
2483ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str);
2484ae8c6e27Sflorian 		if(*len < dlen/2 + 1)
2485ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
2486ae8c6e27Sflorian 				s-str);
2487ae8c6e27Sflorian 		if((dlen&1)==0)
2488ae8c6e27Sflorian 			rd[dlen/2] = (uint8_t)sldns_hexdigit_to_int(*s++) * 16;
2489ae8c6e27Sflorian 		else	rd[dlen/2] += sldns_hexdigit_to_int(*s++);
2490ae8c6e27Sflorian 		dlen++;
2491ae8c6e27Sflorian 	}
2492ae8c6e27Sflorian 	if((dlen&1)!=0)
2493ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str);
2494ae8c6e27Sflorian 	*len = dlen/2;
2495ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2496ae8c6e27Sflorian }
2497ae8c6e27Sflorian 
2498ae8c6e27Sflorian int sldns_str2wire_atma_buf(const char* str, uint8_t* rd, size_t* len)
2499ae8c6e27Sflorian {
2500ae8c6e27Sflorian 	const char* s = str;
2501ae8c6e27Sflorian 	size_t slen = strlen(str);
2502ae8c6e27Sflorian 	size_t dlen = 0; /* number of hexdigits parsed */
2503ae8c6e27Sflorian 
2504ae8c6e27Sflorian 	/* just a hex string with optional dots? */
2505ae8c6e27Sflorian 	/* notimpl e.164 format */
2506ae8c6e27Sflorian 	if(slen > LDNS_MAX_RDFLEN*2)
2507ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_LABEL_OVERFLOW;
2508ae8c6e27Sflorian 	while(*s) {
2509ae8c6e27Sflorian 		if(isspace((unsigned char)*s) || *s == '.') {
2510ae8c6e27Sflorian 			s++;
2511ae8c6e27Sflorian 			continue;
2512ae8c6e27Sflorian 		}
2513ae8c6e27Sflorian 		if(!isxdigit((unsigned char)*s))
2514ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str);
2515ae8c6e27Sflorian 		if(*len < dlen/2 + 1)
2516ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
2517ae8c6e27Sflorian 				s-str);
2518ae8c6e27Sflorian 		if((dlen&1)==0)
2519ae8c6e27Sflorian 			rd[dlen/2] = (uint8_t)sldns_hexdigit_to_int(*s++) * 16;
2520ae8c6e27Sflorian 		else	rd[dlen/2] += sldns_hexdigit_to_int(*s++);
2521ae8c6e27Sflorian 		dlen++;
2522ae8c6e27Sflorian 	}
2523ae8c6e27Sflorian 	if((dlen&1)!=0)
2524ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str);
2525ae8c6e27Sflorian 	*len = dlen/2;
2526ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2527ae8c6e27Sflorian }
2528ae8c6e27Sflorian 
2529ae8c6e27Sflorian int sldns_str2wire_ipseckey_buf(const char* str, uint8_t* rd, size_t* len)
2530ae8c6e27Sflorian {
2531ae8c6e27Sflorian 	size_t gwlen = 0, keylen = 0;
2532ae8c6e27Sflorian 	int s;
2533ae8c6e27Sflorian 	uint8_t gwtype;
2534ae8c6e27Sflorian 	char token[512];
2535ae8c6e27Sflorian 	sldns_buffer strbuf;
2536ae8c6e27Sflorian 	sldns_buffer_init_frm_data(&strbuf, (uint8_t*)str, strlen(str));
2537ae8c6e27Sflorian 
2538ae8c6e27Sflorian 	if(*len < 3)
2539ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2540ae8c6e27Sflorian 	/* precedence */
2541ae8c6e27Sflorian 	if(sldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) <= 0)
2542ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR,
2543ae8c6e27Sflorian 			sldns_buffer_position(&strbuf));
2544ae8c6e27Sflorian 	rd[0] = (uint8_t)atoi(token);
2545ae8c6e27Sflorian 	/* gateway_type */
2546ae8c6e27Sflorian 	if(sldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) <= 0)
2547ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR,
2548ae8c6e27Sflorian 			sldns_buffer_position(&strbuf));
2549ae8c6e27Sflorian 	rd[1] = (uint8_t)atoi(token);
2550ae8c6e27Sflorian 	gwtype = rd[1];
2551ae8c6e27Sflorian 	/* algorithm */
2552ae8c6e27Sflorian 	if(sldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) <= 0)
2553ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR,
2554ae8c6e27Sflorian 			sldns_buffer_position(&strbuf));
2555ae8c6e27Sflorian 	rd[2] = (uint8_t)atoi(token);
2556ae8c6e27Sflorian 
2557ae8c6e27Sflorian 	/* gateway */
2558ae8c6e27Sflorian 	if(sldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) <= 0)
2559ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR,
2560ae8c6e27Sflorian 			sldns_buffer_position(&strbuf));
2561ae8c6e27Sflorian 	if(gwtype == 0) {
2562ae8c6e27Sflorian 		/* NOGATEWAY */
2563ae8c6e27Sflorian 		if(strcmp(token, ".") != 0)
2564ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR,
2565ae8c6e27Sflorian 				sldns_buffer_position(&strbuf));
2566ae8c6e27Sflorian 		gwlen = 0;
2567ae8c6e27Sflorian 	} else if(gwtype == 1) {
2568ae8c6e27Sflorian 		/* IP4 */
2569ae8c6e27Sflorian 		gwlen = *len - 3;
2570ae8c6e27Sflorian 		s = sldns_str2wire_a_buf(token, rd+3, &gwlen);
2571ae8c6e27Sflorian 		if(s) return RET_ERR_SHIFT(s, sldns_buffer_position(&strbuf));
2572ae8c6e27Sflorian 	} else if(gwtype == 2) {
2573ae8c6e27Sflorian 		/* IP6 */
2574ae8c6e27Sflorian 		gwlen = *len - 3;
2575ae8c6e27Sflorian 		s = sldns_str2wire_aaaa_buf(token, rd+3, &gwlen);
2576ae8c6e27Sflorian 		if(s) return RET_ERR_SHIFT(s, sldns_buffer_position(&strbuf));
2577ae8c6e27Sflorian 	} else if(gwtype == 3) {
2578ae8c6e27Sflorian 		/* DNAME */
2579ae8c6e27Sflorian 		gwlen = *len - 3;
2580ae8c6e27Sflorian 		s = sldns_str2wire_dname_buf(token, rd+3, &gwlen);
2581ae8c6e27Sflorian 		if(s) return RET_ERR_SHIFT(s, sldns_buffer_position(&strbuf));
2582ae8c6e27Sflorian 	} else {
2583ae8c6e27Sflorian 		/* unknown gateway type */
2584ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR,
2585ae8c6e27Sflorian 			sldns_buffer_position(&strbuf));
2586ae8c6e27Sflorian 	}
2587ae8c6e27Sflorian 	/* double check for size */
2588ae8c6e27Sflorian 	if(*len < 3 + gwlen)
2589ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
2590ae8c6e27Sflorian 			sldns_buffer_position(&strbuf));
2591ae8c6e27Sflorian 
2592ae8c6e27Sflorian 	/* publickey in remainder of strbuf */
2593ae8c6e27Sflorian 	keylen = *len - 3 - gwlen;
2594ae8c6e27Sflorian 	s = sldns_str2wire_b64_buf((const char*)sldns_buffer_current(&strbuf),
2595ae8c6e27Sflorian 		rd+3+gwlen, &keylen);
2596ae8c6e27Sflorian 	if(s) return RET_ERR_SHIFT(s, sldns_buffer_position(&strbuf));
2597ae8c6e27Sflorian 
2598ae8c6e27Sflorian 	*len = 3 + gwlen + keylen;
2599ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2600ae8c6e27Sflorian }
2601ae8c6e27Sflorian 
2602ae8c6e27Sflorian int sldns_str2wire_nsec3_salt_buf(const char* str, uint8_t* rd, size_t* len)
2603ae8c6e27Sflorian {
2604ae8c6e27Sflorian 	int i, salt_length_str = (int)strlen(str);
2605ae8c6e27Sflorian 	if (salt_length_str == 1 && str[0] == '-') {
2606ae8c6e27Sflorian 		salt_length_str = 0;
2607ae8c6e27Sflorian 	} else if (salt_length_str % 2 != 0) {
2608ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_HEX;
2609ae8c6e27Sflorian 	}
2610ae8c6e27Sflorian 	if (salt_length_str > 512)
2611ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_HEX;
2612ae8c6e27Sflorian 	if(*len < 1+(size_t)salt_length_str / 2)
2613ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2614ae8c6e27Sflorian 	rd[0] = (uint8_t) (salt_length_str / 2);
2615ae8c6e27Sflorian 	for (i = 0; i < salt_length_str; i += 2) {
2616ae8c6e27Sflorian 		if (isxdigit((unsigned char)str[i]) &&
2617ae8c6e27Sflorian 			isxdigit((unsigned char)str[i+1])) {
2618ae8c6e27Sflorian 			rd[1+i/2] = (uint8_t)(sldns_hexdigit_to_int(str[i])*16
2619ae8c6e27Sflorian 				+ sldns_hexdigit_to_int(str[i+1]));
2620ae8c6e27Sflorian 		} else {
2621ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_HEX, i);
2622ae8c6e27Sflorian 		}
2623ae8c6e27Sflorian 	}
2624ae8c6e27Sflorian 	*len = 1 + (size_t)rd[0];
2625ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2626ae8c6e27Sflorian }
2627ae8c6e27Sflorian 
2628ae8c6e27Sflorian int sldns_str2wire_ilnp64_buf(const char* str, uint8_t* rd, size_t* len)
2629ae8c6e27Sflorian {
2630ae8c6e27Sflorian 	unsigned int a, b, c, d;
2631ae8c6e27Sflorian 	uint16_t shorts[4];
2632ae8c6e27Sflorian 	int l;
2633ae8c6e27Sflorian 	if(*len < sizeof(shorts))
2634ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2635ae8c6e27Sflorian 
2636ae8c6e27Sflorian 	if (sscanf(str, "%4x:%4x:%4x:%4x%n", &a, &b, &c, &d, &l) != 4 ||
2637ae8c6e27Sflorian 			l != (int)strlen(str) || /* more data to read */
2638ae8c6e27Sflorian 			strpbrk(str, "+-")       /* signed hexes */
2639ae8c6e27Sflorian 			)
2640ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_ILNP64;
2641ae8c6e27Sflorian 	shorts[0] = htons(a);
2642ae8c6e27Sflorian 	shorts[1] = htons(b);
2643ae8c6e27Sflorian 	shorts[2] = htons(c);
2644ae8c6e27Sflorian 	shorts[3] = htons(d);
2645ae8c6e27Sflorian 	memmove(rd, &shorts, sizeof(shorts));
2646ae8c6e27Sflorian 	*len = sizeof(shorts);
2647ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2648ae8c6e27Sflorian }
2649ae8c6e27Sflorian 
2650ae8c6e27Sflorian int sldns_str2wire_eui48_buf(const char* str, uint8_t* rd, size_t* len)
2651ae8c6e27Sflorian {
2652ae8c6e27Sflorian 	unsigned int a, b, c, d, e, f;
2653ae8c6e27Sflorian 	int l;
2654ae8c6e27Sflorian 
2655ae8c6e27Sflorian 	if(*len < 6)
2656ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2657ae8c6e27Sflorian 	if (sscanf(str, "%2x-%2x-%2x-%2x-%2x-%2x%n",
2658ae8c6e27Sflorian 			&a, &b, &c, &d, &e, &f, &l) != 6 ||
2659ae8c6e27Sflorian 			l != (int)strlen(str))
2660ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_EUI48;
2661ae8c6e27Sflorian 	rd[0] = a;
2662ae8c6e27Sflorian 	rd[1] = b;
2663ae8c6e27Sflorian 	rd[2] = c;
2664ae8c6e27Sflorian 	rd[3] = d;
2665ae8c6e27Sflorian 	rd[4] = e;
2666ae8c6e27Sflorian 	rd[5] = f;
2667ae8c6e27Sflorian 	*len = 6;
2668ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2669ae8c6e27Sflorian }
2670ae8c6e27Sflorian 
2671ae8c6e27Sflorian int sldns_str2wire_eui64_buf(const char* str, uint8_t* rd, size_t* len)
2672ae8c6e27Sflorian {
2673ae8c6e27Sflorian 	unsigned int a, b, c, d, e, f, g, h;
2674ae8c6e27Sflorian 	int l;
2675ae8c6e27Sflorian 
2676ae8c6e27Sflorian 	if(*len < 8)
2677ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2678ae8c6e27Sflorian 	if (sscanf(str, "%2x-%2x-%2x-%2x-%2x-%2x-%2x-%2x%n",
2679ae8c6e27Sflorian 			&a, &b, &c, &d, &e, &f, &g, &h, &l) != 8 ||
2680ae8c6e27Sflorian 			l != (int)strlen(str))
2681ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_EUI64;
2682ae8c6e27Sflorian 	rd[0] = a;
2683ae8c6e27Sflorian 	rd[1] = b;
2684ae8c6e27Sflorian 	rd[2] = c;
2685ae8c6e27Sflorian 	rd[3] = d;
2686ae8c6e27Sflorian 	rd[4] = e;
2687ae8c6e27Sflorian 	rd[5] = f;
2688ae8c6e27Sflorian 	rd[6] = g;
2689ae8c6e27Sflorian 	rd[7] = h;
2690ae8c6e27Sflorian 	*len = 8;
2691ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2692ae8c6e27Sflorian }
2693ae8c6e27Sflorian 
2694ae8c6e27Sflorian int sldns_str2wire_tag_buf(const char* str, uint8_t* rd, size_t* len)
2695ae8c6e27Sflorian {
2696ae8c6e27Sflorian 	size_t slen = strlen(str);
2697ae8c6e27Sflorian 	const char* ptr;
2698ae8c6e27Sflorian 
2699ae8c6e27Sflorian 	if (slen > 255)
2700ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_TAG;
2701ae8c6e27Sflorian 	if(*len < slen+1)
2702ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2703ae8c6e27Sflorian 	for (ptr = str; *ptr; ptr++) {
2704ae8c6e27Sflorian 		if(!isalnum((unsigned char)*ptr))
2705ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TAG, ptr-str);
2706ae8c6e27Sflorian 	}
2707ae8c6e27Sflorian 	rd[0] = (uint8_t)slen;
2708ae8c6e27Sflorian 	memmove(rd+1, str, slen);
2709ae8c6e27Sflorian 	*len = slen+1;
2710ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2711ae8c6e27Sflorian }
2712ae8c6e27Sflorian 
2713ae8c6e27Sflorian int sldns_str2wire_long_str_buf(const char* str, uint8_t* rd, size_t* len)
2714ae8c6e27Sflorian {
2715ae8c6e27Sflorian 	uint8_t ch = 0;
2716ae8c6e27Sflorian 	const char* pstr = str;
2717ae8c6e27Sflorian 	size_t length = 0;
2718ae8c6e27Sflorian 
2719ae8c6e27Sflorian 	/* Fill data with parsed bytes */
2720ae8c6e27Sflorian 	while (sldns_parse_char(&ch, &pstr)) {
2721ae8c6e27Sflorian 		if(*len < length+1)
2722ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2723ae8c6e27Sflorian 		rd[length++] = ch;
2724ae8c6e27Sflorian 	}
2725ae8c6e27Sflorian 	if(!pstr)
2726ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_BAD_ESCAPE;
2727ae8c6e27Sflorian 	*len = length;
2728ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2729ae8c6e27Sflorian }
2730ae8c6e27Sflorian 
2731ae8c6e27Sflorian int sldns_str2wire_hip_buf(const char* str, uint8_t* rd, size_t* len)
2732ae8c6e27Sflorian {
2733ae8c6e27Sflorian 	char* s, *end;
2734ae8c6e27Sflorian 	int e;
2735ae8c6e27Sflorian 	size_t hitlen, pklen = 0;
2736ae8c6e27Sflorian 	/* presentation format:
2737ae8c6e27Sflorian 	 * 	pk-algo HIThex pubkeybase64
2738ae8c6e27Sflorian 	 * wireformat:
2739ae8c6e27Sflorian 	 * 	hitlen[1byte] pkalgo[1byte] pubkeylen[2byte] [hit] [pubkey] */
2740ae8c6e27Sflorian 	if(*len < 4)
2741ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2742ae8c6e27Sflorian 
2743ae8c6e27Sflorian 	/* read PK algorithm */
2744ae8c6e27Sflorian 	rd[1] = (uint8_t)strtol((char*)str, &s, 10);
2745ae8c6e27Sflorian 	if(*s != ' ')
2746ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_INT, s-(char*)str);
2747ae8c6e27Sflorian 	s++;
2748ae8c6e27Sflorian 	while(*s == ' ')
2749ae8c6e27Sflorian 		s++;
2750ae8c6e27Sflorian 
2751ae8c6e27Sflorian 	/* read HIT hex tag */
2752ae8c6e27Sflorian 	/* zero terminate the tag (replace later) */
2753ae8c6e27Sflorian 	end = strchr(s, ' ');
2754ae8c6e27Sflorian 	if(!end) return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX, s-(char*)str);
2755ae8c6e27Sflorian 	*end = 0;
2756ae8c6e27Sflorian 	hitlen = *len - 4;
2757ae8c6e27Sflorian 	if((e = sldns_str2wire_hex_buf(s, rd+4, &hitlen)) != 0) {
2758ae8c6e27Sflorian 		*end = ' ';
2759ae8c6e27Sflorian 		return RET_ERR_SHIFT(e, s-(char*)str);
2760ae8c6e27Sflorian 	}
2761ae8c6e27Sflorian 	if(hitlen > 255) {
2762ae8c6e27Sflorian 		*end = ' ';
2763ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_LABEL_OVERFLOW, s-(char*)str+255*2);
2764ae8c6e27Sflorian 	}
2765ae8c6e27Sflorian 	rd[0] = (uint8_t)hitlen;
2766ae8c6e27Sflorian 	*end = ' ';
2767ae8c6e27Sflorian 	s = end+1;
2768ae8c6e27Sflorian 
2769ae8c6e27Sflorian 	/* read pubkey base64 sequence */
2770ae8c6e27Sflorian 	pklen = *len - 4 - hitlen;
2771ae8c6e27Sflorian 	if((e = sldns_str2wire_b64_buf(s, rd+4+hitlen, &pklen)) != 0)
2772ae8c6e27Sflorian 		return RET_ERR_SHIFT(e, s-(char*)str);
2773ae8c6e27Sflorian 	if(pklen > 65535)
2774ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_LABEL_OVERFLOW, s-(char*)str+65535);
2775ae8c6e27Sflorian 	sldns_write_uint16(rd+2, (uint16_t)pklen);
2776ae8c6e27Sflorian 
2777ae8c6e27Sflorian 	*len = 4 + hitlen + pklen;
2778ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2779ae8c6e27Sflorian }
2780ae8c6e27Sflorian 
2781ae8c6e27Sflorian int sldns_str2wire_int16_data_buf(const char* str, uint8_t* rd, size_t* len)
2782ae8c6e27Sflorian {
2783ae8c6e27Sflorian 	char* s;
2784ae8c6e27Sflorian 	int n;
2785ae8c6e27Sflorian 	n = strtol(str, &s, 10);
278657403691Sflorian 	if(n < 0) /* negative number not allowed */
278757403691Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX;
2788ae8c6e27Sflorian 	if(*len < ((size_t)n)+2)
2789ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2790ae8c6e27Sflorian 	if(n > 65535)
2791ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_LABEL_OVERFLOW;
2792ae8c6e27Sflorian 
2793ae8c6e27Sflorian 	if(n == 0) {
2794ae8c6e27Sflorian 		sldns_write_uint16(rd, 0);
2795ae8c6e27Sflorian 		*len = 2;
2796ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_OK;
2797ae8c6e27Sflorian 	}
2798ae8c6e27Sflorian 	if(*s != ' ')
2799ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_INT, s-(char*)str);
2800ae8c6e27Sflorian 	s++;
2801ae8c6e27Sflorian 	while(*s == ' ')
2802ae8c6e27Sflorian 		s++;
2803ae8c6e27Sflorian 
2804ae8c6e27Sflorian 	n = sldns_b64_pton(s, rd+2, (*len)-2);
2805ae8c6e27Sflorian 	if(n < 0)
2806ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_B64;
2807ae8c6e27Sflorian 	sldns_write_uint16(rd, (uint16_t)n);
2808ae8c6e27Sflorian 	*len = ((size_t)n)+2;
2809ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2810ae8c6e27Sflorian }
2811