xref: /freebsd/contrib/unbound/sldns/str2wire.c (revision 9d54812421274e490dc5f0fe4722ab8d35d9b258)
1 /**
2  * str2wire.c - read txt presentation of RRs
3  *
4  * (c) NLnet Labs, 2005-2006
5  *
6  * See the file LICENSE for the license
7  */
8 
9 /**
10  * \file
11  *
12  * Parses text to wireformat.
13  */
14 #include "config.h"
15 #include "sldns/str2wire.h"
16 #include "sldns/wire2str.h"
17 #include "sldns/sbuffer.h"
18 #include "sldns/parse.h"
19 #include "sldns/parseutil.h"
20 #include <ctype.h>
21 #ifdef HAVE_TIME_H
22 #include <time.h>
23 #endif
24 #ifdef HAVE_NETDB_H
25 #include <netdb.h>
26 #endif
27 
28 /** bits for the offset */
29 #define RET_OFFSET_MASK (((unsigned)(~LDNS_WIREPARSE_MASK))>>LDNS_WIREPARSE_SHIFT)
30 /** return an error */
31 #define RET_ERR(e, off) ((int)(((e)&LDNS_WIREPARSE_MASK)|(((off)&RET_OFFSET_MASK)<<LDNS_WIREPARSE_SHIFT)))
32 /** Move parse error but keep its ID */
33 #define RET_ERR_SHIFT(e, move) RET_ERR(LDNS_WIREPARSE_ERROR(e), LDNS_WIREPARSE_OFFSET(e)+(move));
34 
35 /*
36  * No special care is taken, all dots are translated into
37  * label separators.
38  * @param rel: true if the domain is not absolute (not terminated in .).
39  * 	The output is then still terminated with a '0' rootlabel.
40  */
41 static int sldns_str2wire_dname_buf_rel(const char* str, uint8_t* buf,
42 	size_t* olen, int* rel)
43 {
44 	size_t len;
45 
46 	const char *s;
47 	uint8_t *q, *pq, label_len;
48 
49 	if(rel) *rel = 0;
50 	len = strlen((char*)str);
51 	/* octet representation can make strings a lot longer than actual length */
52 	if (len > LDNS_MAX_DOMAINLEN * 4) {
53 		return RET_ERR(LDNS_WIREPARSE_ERR_DOMAINNAME_OVERFLOW, 0);
54 	}
55 	if (0 == len) {
56 		return RET_ERR(LDNS_WIREPARSE_ERR_DOMAINNAME_UNDERFLOW, 0);
57 	}
58 
59 	/* root label */
60 	if (1 == len && *str == '.') {
61 		if(*olen < 1)
62 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, 0);
63 		buf[0] = 0;
64 		*olen = 1;
65 		return LDNS_WIREPARSE_ERR_OK;
66 	}
67 
68 	/* get on with the rest */
69 
70 	/* s is on the current character in the string
71          * pq points to where the labellength is going to go
72          * label_len keeps track of the current label's length
73 	 * q builds the dname inside the buf array
74 	 */
75 	len = 0;
76 	if(*olen < 1)
77 		return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, 0);
78 	q = buf+1;
79 	pq = buf;
80 	label_len = 0;
81 	for (s = str; *s; s++, q++) {
82 		if (q >= buf + *olen)
83 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, q-buf);
84 		if (q >= buf + LDNS_MAX_DOMAINLEN)
85 			return RET_ERR(LDNS_WIREPARSE_ERR_DOMAINNAME_OVERFLOW, q-buf);
86 		switch (*s) {
87 		case '.':
88 			if (label_len > LDNS_MAX_LABELLEN) {
89 				return RET_ERR(LDNS_WIREPARSE_ERR_LABEL_OVERFLOW, q-buf);
90 			}
91 			if (label_len == 0) {
92 				return RET_ERR(LDNS_WIREPARSE_ERR_EMPTY_LABEL, q-buf);
93 			}
94 			len += label_len + 1;
95 			*q = 0;
96 			*pq = label_len;
97 			label_len = 0;
98 			pq = q;
99 			break;
100 		case '\\':
101 			/* octet value or literal char */
102 			s += 1;
103 			if (!sldns_parse_escape(q, &s)) {
104 				*q = 0;
105 				return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_BAD_ESCAPE, q-buf);
106 			}
107 			s -= 1;
108 			label_len++;
109 			break;
110 		default:
111 			*q = (uint8_t)*s;
112 			label_len++;
113 		}
114 	}
115 
116 	/* add root label if last char was not '.' */
117 	if(label_len != 0) {
118 		if(rel) *rel = 1;
119 		if (q >= buf + *olen)
120 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, q-buf);
121 		if (q >= buf + LDNS_MAX_DOMAINLEN) {
122 			return RET_ERR(LDNS_WIREPARSE_ERR_DOMAINNAME_OVERFLOW, q-buf);
123 		}
124                 if (label_len > LDNS_MAX_LABELLEN) {
125                         return RET_ERR(LDNS_WIREPARSE_ERR_LABEL_OVERFLOW, q-buf);
126                 }
127                 if (label_len == 0) { /* label_len 0 but not . at end? */
128                         return RET_ERR(LDNS_WIREPARSE_ERR_EMPTY_LABEL, q-buf);
129                 }
130 		len += label_len + 1;
131 		*pq = label_len;
132 		*q = 0;
133 	}
134 	len++;
135 	*olen = len;
136 
137 	return LDNS_WIREPARSE_ERR_OK;
138 }
139 
140 int sldns_str2wire_dname_buf(const char* str, uint8_t* buf, size_t* len)
141 {
142 	return sldns_str2wire_dname_buf_rel(str, buf, len, NULL);
143 }
144 
145 int sldns_str2wire_dname_buf_origin(const char* str, uint8_t* buf, size_t* len,
146 	uint8_t* origin, size_t origin_len)
147 {
148 	size_t dlen = *len;
149 	int rel = 0;
150 	int s = sldns_str2wire_dname_buf_rel(str, buf, &dlen, &rel);
151 	if(s) return s;
152 
153 	if(rel && origin && dlen > 0) {
154 		if((unsigned)dlen >= 0x00ffffffU ||
155 			(unsigned)origin_len >= 0x00ffffffU)
156 			/* guard against integer overflow in addition */
157 			return RET_ERR(LDNS_WIREPARSE_ERR_GENERAL, *len);
158 		if(dlen + origin_len - 1 > LDNS_MAX_DOMAINLEN)
159 			return RET_ERR(LDNS_WIREPARSE_ERR_DOMAINNAME_OVERFLOW,
160 				LDNS_MAX_DOMAINLEN);
161 		if(dlen + origin_len - 1 > *len)
162 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
163 				*len);
164 		memmove(buf+dlen-1, origin, origin_len);
165 		*len = dlen + origin_len - 1;
166 	} else
167 		*len = dlen;
168 	return LDNS_WIREPARSE_ERR_OK;
169 }
170 
171 uint8_t* sldns_str2wire_dname(const char* str, size_t* len)
172 {
173 	uint8_t dname[LDNS_MAX_DOMAINLEN+1];
174 	*len = sizeof(dname);
175 	if(sldns_str2wire_dname_buf(str, dname, len) == 0) {
176 		uint8_t* r;
177 		if(*len > sizeof(dname)) return NULL;
178 		r = (uint8_t*)malloc(*len);
179 		if(r) return memcpy(r, dname, *len);
180 	}
181 	*len = 0;
182 	return NULL;
183 }
184 
185 /** read owner name */
186 static int
187 rrinternal_get_owner(sldns_buffer* strbuf, uint8_t* rr, size_t* len,
188 	size_t* dname_len, uint8_t* origin, size_t origin_len, uint8_t* prev,
189 	size_t prev_len, char* token, size_t token_len)
190 {
191 	/* split the rr in its parts -1 signals trouble */
192 	if(sldns_bget_token(strbuf, token, "\t\n ", token_len) == -1) {
193 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX,
194 			sldns_buffer_position(strbuf));
195 	}
196 
197 	if(token_len < 2) /* make sure there is space to read "@" or "" */
198 		return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
199 			sldns_buffer_position(strbuf));
200 	if(token[0]=='@' && token[1]=='\0') {
201 		uint8_t* tocopy;
202 		if (origin) {
203 			*dname_len = origin_len;
204 			tocopy = origin;
205 		} else if (prev) {
206 			*dname_len = prev_len;
207 			tocopy = prev;
208 		} else {
209 			/* default to root */
210 			*dname_len = 1;
211 			tocopy = (uint8_t*)"\0";
212 		}
213 		if(*len < *dname_len)
214 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
215 				sldns_buffer_position(strbuf));
216 		memmove(rr, tocopy, *dname_len);
217 	} else if(*token == '\0') {
218 		/* no ownername was given, try prev, if that fails
219 		 * origin, else default to root */
220 		uint8_t* tocopy;
221 		if(prev) {
222 			*dname_len = prev_len;
223 			tocopy = prev;
224 		} else if(origin) {
225 			*dname_len = origin_len;
226 			tocopy = origin;
227 		} else {
228 			*dname_len = 1;
229 			tocopy = (uint8_t*)"\0";
230 		}
231 		if(*len < *dname_len)
232 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
233 				sldns_buffer_position(strbuf));
234 		memmove(rr, tocopy, *dname_len);
235 	} else {
236 		size_t dlen = *len;
237 		int s = sldns_str2wire_dname_buf_origin(token, rr, &dlen,
238 			origin, origin_len);
239 		if(s) return RET_ERR_SHIFT(s,
240 			sldns_buffer_position(strbuf)-strlen(token));
241 		*dname_len = dlen;
242 	}
243 	return LDNS_WIREPARSE_ERR_OK;
244 }
245 
246 /** read ttl */
247 static int
248 rrinternal_get_ttl(sldns_buffer* strbuf, char* token, size_t token_len,
249 	int* not_there, uint32_t* ttl, uint32_t default_ttl)
250 {
251 	const char* endptr;
252 	int overflow;
253 	if(sldns_bget_token(strbuf, token, "\t\n ", token_len) == -1) {
254 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TTL,
255 			sldns_buffer_position(strbuf));
256 	}
257 	*ttl = (uint32_t) sldns_str2period(token, &endptr, &overflow);
258 	if(overflow) {
259 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_INTEGER_OVERFLOW,
260 			sldns_buffer_position(strbuf));
261 	}
262 
263 	if (strlen(token) > 0 && !isdigit((unsigned char)token[0])) {
264 		*not_there = 1;
265 		/* ah, it's not there or something */
266 		if (default_ttl == 0) {
267 			*ttl = LDNS_DEFAULT_TTL;
268 		} else {
269 			*ttl = default_ttl;
270 		}
271 	}
272 	return LDNS_WIREPARSE_ERR_OK;
273 }
274 
275 /** read class */
276 static int
277 rrinternal_get_class(sldns_buffer* strbuf, char* token, size_t token_len,
278 	int* not_there, uint16_t* cl)
279 {
280 	/* if 'not_there' then we got token from previous parse routine */
281 	if(!*not_there) {
282 		/* parse new token for class */
283 		if(sldns_bget_token(strbuf, token, "\t\n ", token_len) == -1) {
284 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_CLASS,
285 				sldns_buffer_position(strbuf));
286 		}
287 	} else *not_there = 0;
288 	*cl = sldns_get_rr_class_by_name(token);
289 	/* class can be left out too, assume IN, current token must be type */
290 	if(*cl == 0 && strcmp(token, "CLASS0") != 0) {
291 		*not_there = 1;
292 		*cl = LDNS_RR_CLASS_IN;
293 	}
294 	return LDNS_WIREPARSE_ERR_OK;
295 }
296 
297 /** read type */
298 static int
299 rrinternal_get_type(sldns_buffer* strbuf, char* token, size_t token_len,
300 	int* not_there, uint16_t* tp)
301 {
302 	/* if 'not_there' then we got token from previous parse routine */
303 	if(!*not_there) {
304 		/* parse new token for type */
305 		if(sldns_bget_token(strbuf, token, "\t\n ", token_len) == -1) {
306 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TYPE,
307 				sldns_buffer_position(strbuf));
308 		}
309 	}
310 	*tp = sldns_get_rr_type_by_name(token);
311 	if(*tp == 0 && strcmp(token, "TYPE0") != 0) {
312 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TYPE,
313 			sldns_buffer_position(strbuf));
314 	}
315 	return LDNS_WIREPARSE_ERR_OK;
316 }
317 
318 /** put type, class, ttl into rr buffer */
319 static int
320 rrinternal_write_typeclassttl(sldns_buffer* strbuf, uint8_t* rr, size_t len,
321 	size_t dname_len, uint16_t tp, uint16_t cl, uint32_t ttl, int question)
322 {
323 	if(question) {
324 		/* question is : name, type, class */
325 		if(dname_len + 4 > len)
326 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
327 				sldns_buffer_position(strbuf));
328 		sldns_write_uint16(rr+dname_len, tp);
329 		sldns_write_uint16(rr+dname_len+2, cl);
330 		return LDNS_WIREPARSE_ERR_OK;
331 	}
332 
333 	/* type(2), class(2), ttl(4), rdatalen(2 (later)) = 10 */
334 	if(dname_len + 10 > len)
335 		return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
336 			sldns_buffer_position(strbuf));
337 	sldns_write_uint16(rr+dname_len, tp);
338 	sldns_write_uint16(rr+dname_len+2, cl);
339 	sldns_write_uint32(rr+dname_len+4, ttl);
340 	sldns_write_uint16(rr+dname_len+8, 0); /* rdatalen placeholder */
341 	return LDNS_WIREPARSE_ERR_OK;
342 }
343 
344 /** find delimiters for type */
345 static const char*
346 rrinternal_get_delims(sldns_rdf_type rdftype, size_t r_cnt, size_t r_max)
347 {
348 	switch(rdftype) {
349 	case LDNS_RDF_TYPE_B64        :
350 	case LDNS_RDF_TYPE_HEX        : /* These rdf types may con- */
351 	case LDNS_RDF_TYPE_LOC        : /* tain whitespace, only if */
352 	case LDNS_RDF_TYPE_WKS        : /* it is the last rd field. */
353 	case LDNS_RDF_TYPE_IPSECKEY   :
354 	case LDNS_RDF_TYPE_NSEC       :	if (r_cnt == r_max - 1) {
355 						return "\n";
356 					}
357 					break;
358 	default                       :	break;
359 	}
360 	return "\n\t ";
361 }
362 
363 /* Syntactic sugar for sldns_rr_new_frm_str_internal */
364 static int
365 sldns_rdf_type_maybe_quoted(sldns_rdf_type rdf_type)
366 {
367 	return  rdf_type == LDNS_RDF_TYPE_STR ||
368 		rdf_type == LDNS_RDF_TYPE_LONG_STR;
369 }
370 
371 /** see if rdata is quoted */
372 static int
373 rrinternal_get_quoted(sldns_buffer* strbuf, const char** delimiters,
374 	sldns_rdf_type rdftype)
375 {
376 	if(sldns_rdf_type_maybe_quoted(rdftype) &&
377 		sldns_buffer_remaining(strbuf) > 0) {
378 
379 		/* skip spaces */
380 		while(sldns_buffer_remaining(strbuf) > 0 &&
381 			(*(sldns_buffer_current(strbuf)) == ' ' ||
382 			*(sldns_buffer_current(strbuf)) == '\t')) {
383 			sldns_buffer_skip(strbuf, 1);
384 		}
385 
386 		if(sldns_buffer_remaining(strbuf) > 0 &&
387 			*(sldns_buffer_current(strbuf)) == '\"') {
388 			*delimiters = "\"\0";
389 			sldns_buffer_skip(strbuf, 1);
390 			return 1;
391 		}
392 	}
393 	return 0;
394 }
395 
396 /** spool hex data into rdata */
397 static int
398 rrinternal_spool_hex(char* token, uint8_t* rr, size_t rr_len,
399 	size_t rr_cur_len, size_t* cur_hex_data_size, size_t hex_data_size)
400 {
401 	char* p = token;
402 	while(*p) {
403 		if(isspace((unsigned char)*p)) {
404 			p++;
405 			continue;
406 		}
407 		if(!isxdigit((unsigned char)*p))
408 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_RDATA,
409 				p-token);
410 		if(*cur_hex_data_size >= hex_data_size)
411 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_RDATA,
412 				p-token);
413 		/* extra robust check */
414 		if(rr_cur_len+(*cur_hex_data_size)/2 >= rr_len)
415 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
416 				p-token);
417 		/* see if 16s or 1s */
418 		if( ((*cur_hex_data_size)&1) == 0) {
419 			rr[rr_cur_len+(*cur_hex_data_size)/2] =
420 				(uint8_t)sldns_hexdigit_to_int(*p)*16;
421 		} else {
422 			rr[rr_cur_len+(*cur_hex_data_size)/2] +=
423 				(uint8_t)sldns_hexdigit_to_int(*p);
424 		}
425 		p++;
426 		(*cur_hex_data_size)++;
427 	}
428 	return LDNS_WIREPARSE_ERR_OK;
429 }
430 
431 /** read unknown rr type format */
432 static int
433 rrinternal_parse_unknown(sldns_buffer* strbuf, char* token, size_t token_len,
434         uint8_t* rr, size_t* rr_len, size_t* rr_cur_len, size_t pre_data_pos)
435 {
436 	const char* delim = "\n\t ";
437 	size_t hex_data_size, cur_hex_data_size;
438 	/* go back to before \#
439 	 * and skip it while setting delimiters better
440 	 */
441 	sldns_buffer_set_position(strbuf, pre_data_pos);
442 	if(sldns_bget_token(strbuf, token, delim, token_len) == -1)
443 		return LDNS_WIREPARSE_ERR_GENERAL; /* should not fail */
444 	/* read rdata octet length */
445 	if(sldns_bget_token(strbuf, token, delim, token_len) == -1) {
446 		/* something goes very wrong here */
447 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_RDATA,
448 			sldns_buffer_position(strbuf));
449 	}
450 	hex_data_size = (size_t)atoi(token);
451 	if(hex_data_size > LDNS_MAX_RDFLEN ||
452 		*rr_cur_len + hex_data_size > *rr_len) {
453 		return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
454 			sldns_buffer_position(strbuf));
455 	}
456 	/* copy hex chars into hex str (2 chars per byte) */
457 	hex_data_size *= 2;
458 	cur_hex_data_size = 0;
459 	while(cur_hex_data_size < hex_data_size) {
460 		int status;
461 		ssize_t c = sldns_bget_token(strbuf, token, delim, token_len);
462 		if((status = rrinternal_spool_hex(token, rr, *rr_len,
463 			*rr_cur_len, &cur_hex_data_size, hex_data_size)) != 0)
464 			return RET_ERR_SHIFT(status,
465 				sldns_buffer_position(strbuf)-strlen(token));
466 		if(c == -1) {
467 			if(cur_hex_data_size != hex_data_size)
468 				return RET_ERR(
469 					LDNS_WIREPARSE_ERR_SYNTAX_RDATA,
470 					sldns_buffer_position(strbuf));
471 			break;
472 		}
473 	}
474 	*rr_cur_len += hex_data_size/2;
475 	return LDNS_WIREPARSE_ERR_OK;
476 }
477 
478 /** parse normal RR rdata element */
479 static int
480 rrinternal_parse_rdf(sldns_buffer* strbuf, char* token, size_t token_len,
481 	uint8_t* rr, size_t rr_len, size_t* rr_cur_len, sldns_rdf_type rdftype,
482 	uint16_t rr_type, size_t r_cnt, size_t r_max, size_t dname_len,
483 	uint8_t* origin, size_t origin_len)
484 {
485 	size_t len;
486 	int status;
487 
488 	switch(rdftype) {
489 	case LDNS_RDF_TYPE_DNAME:
490 		/* check if the origin should be used or concatenated */
491 		if(strcmp(token, "@") == 0) {
492 			uint8_t* tocopy;
493 			size_t copylen;
494 			if(origin) {
495 				copylen = origin_len;
496 				tocopy = origin;
497 			} else if(rr_type == LDNS_RR_TYPE_SOA) {
498 				copylen = dname_len;
499 				tocopy = rr; /* copy rr owner name */
500 			} else {
501 				copylen = 1;
502 				tocopy = (uint8_t*)"\0";
503 			}
504 			if((*rr_cur_len) + copylen > rr_len)
505 				return RET_ERR(
506 					LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
507 					sldns_buffer_position(strbuf));
508 			memmove(rr+*rr_cur_len, tocopy, copylen);
509 			(*rr_cur_len) += copylen;
510 		} else {
511 			size_t dlen = rr_len - (*rr_cur_len);
512 			int s = sldns_str2wire_dname_buf_origin(token,
513 				rr+*rr_cur_len, &dlen, origin, origin_len);
514 			if(s) return RET_ERR_SHIFT(s,
515 				sldns_buffer_position(strbuf)-strlen(token));
516 			(*rr_cur_len) += dlen;
517 		}
518 		return LDNS_WIREPARSE_ERR_OK;
519 
520 	case LDNS_RDF_TYPE_HEX:
521 	case LDNS_RDF_TYPE_B64:
522 		/* When this is the last rdata field, then the
523 		 * rest should be read in (cause then these
524 		 * rdf types may contain spaces). */
525 		if(r_cnt == r_max - 1) {
526 			size_t tlen = strlen(token);
527 			(void)sldns_bget_token(strbuf, token+tlen, "\n",
528 				token_len - tlen);
529 		}
530 		break;
531 	default:
532 		break;
533 	}
534 
535 	len = rr_len - (*rr_cur_len);
536 	if((status=sldns_str2wire_rdf_buf(token, rr+(*rr_cur_len), &len,
537 		rdftype)) != 0)
538 		return RET_ERR_SHIFT(status,
539 			sldns_buffer_position(strbuf)-strlen(token));
540 	*rr_cur_len += len;
541 	return LDNS_WIREPARSE_ERR_OK;
542 }
543 
544 /**
545  * Parse one rdf token.  Takes care of quotes and parenthesis.
546  */
547 static int
548 sldns_parse_rdf_token(sldns_buffer* strbuf, char* token, size_t token_len,
549 	int* quoted, int* parens, size_t* pre_data_pos,
550 	const char* delimiters, sldns_rdf_type rdftype, size_t* token_strlen)
551 {
552 	size_t slen;
553 
554 	/* skip spaces and tabs */
555 	while(sldns_buffer_remaining(strbuf) > 0 && !*quoted &&
556 		(*(sldns_buffer_current(strbuf)) == ' ' ||
557 		*(sldns_buffer_current(strbuf)) == '\t')) {
558 		sldns_buffer_skip(strbuf, 1);
559 	}
560 
561 	*pre_data_pos = sldns_buffer_position(strbuf);
562 	if(sldns_bget_token_par(strbuf, token, (*quoted)?"\"":delimiters,
563 		token_len, parens, (*quoted)?NULL:" \t") == -1) {
564 		return 0;
565 	}
566 	slen = strlen(token);
567 	/* check if not quoted yet, and we have encountered quotes */
568 	if(!*quoted && sldns_rdf_type_maybe_quoted(rdftype) &&
569 		slen >= 2 &&
570 		(token[0] == '"' || token[0] == '\'') &&
571 		(token[slen-1] == '"' || token[slen-1] == '\'')) {
572 		/* move token two smaller (quotes) with endnull */
573 		memmove(token, token+1, slen-2);
574 		token[slen-2] = 0;
575 		slen -= 2;
576 		*quoted = 1;
577 	} else if(!*quoted && sldns_rdf_type_maybe_quoted(rdftype) &&
578 		slen >= 2 &&
579 		(token[0] == '"' || token[0] == '\'')) {
580 		/* got the start quote (remove it) but read remainder
581 		 * of quoted string as well into remainder of token */
582 		memmove(token, token+1, slen-1);
583 		token[slen-1] = 0;
584 		slen -= 1;
585 		*quoted = 1;
586 		/* rewind buffer over skipped whitespace */
587 		while(sldns_buffer_position(strbuf) > 0 &&
588 			(sldns_buffer_current(strbuf)[-1] == ' ' ||
589 			sldns_buffer_current(strbuf)[-1] == '\t')) {
590 			sldns_buffer_skip(strbuf, -1);
591 		}
592 		if(sldns_bget_token_par(strbuf, token+slen,
593 			"\"", token_len-slen,
594 			parens, NULL) == -1) {
595 			return 0;
596 		}
597 		slen = strlen(token);
598 	}
599 	*token_strlen = slen;
600 	return 1;
601 }
602 
603 /** Add space and one more rdf token onto the existing token string. */
604 static int
605 sldns_affix_token(sldns_buffer* strbuf, char* token, size_t* token_len,
606 	int* quoted, int* parens, size_t* pre_data_pos,
607 	const char* delimiters, sldns_rdf_type rdftype, size_t* token_strlen)
608 {
609 	size_t addlen = *token_len - *token_strlen;
610 	size_t addstrlen = 0;
611 
612 	/* add space */
613 	/* when addlen < 2, the token buffer is full considering the NULL byte
614 	 * from strlen and will lead to buffer overflow with the second
615 	 * assignment below. */
616 	if(addlen < 2) return 0;
617 	token[*token_strlen] = ' ';
618 	token[++(*token_strlen)] = 0;
619 
620 	/* read another token */
621 	addlen = *token_len - *token_strlen;
622 	if(!sldns_parse_rdf_token(strbuf, token+*token_strlen, addlen, quoted,
623 		parens, pre_data_pos, delimiters, rdftype, &addstrlen))
624 		return 0;
625 	(*token_strlen) += addstrlen;
626 	return 1;
627 }
628 
629 static int sldns_str2wire_svcparam_key_cmp(const void *a, const void *b)
630 {
631 	return sldns_read_uint16(*(uint8_t**) a)
632 	     - sldns_read_uint16(*(uint8_t**) b);
633 }
634 
635 /**
636  * Add constraints to the SVCB RRs which involve the whole set
637  */
638 static int sldns_str2wire_check_svcbparams(uint8_t* rdata, uint16_t rdata_len)
639 {
640 	size_t   nparams = 0, i;
641 	uint8_t  new_rdata[LDNS_MAX_RDFLEN];
642 	uint8_t* new_rdata_ptr = new_rdata;
643 	uint8_t* svcparams[MAX_NUMBER_OF_SVCPARAMS];
644 	uint8_t* rdata_ptr = rdata;
645 	uint16_t rdata_remaining = rdata_len;
646 
647 	/* find the SvcParams */
648 	while (rdata_remaining) {
649 		uint16_t svcbparam_len;
650 
651 		svcparams[nparams] = rdata_ptr;
652 		if (rdata_remaining < 4)
653 			return LDNS_WIREPARSE_ERR_SVCPARAM_BROKEN_RDATA;
654 		svcbparam_len = sldns_read_uint16(rdata_ptr + 2);
655 		rdata_remaining -= 4;
656 		rdata_ptr += 4;
657 
658 		if (rdata_remaining < svcbparam_len)
659 			return LDNS_WIREPARSE_ERR_SVCPARAM_BROKEN_RDATA;
660 		rdata_remaining -= svcbparam_len;
661 		rdata_ptr += svcbparam_len;
662 
663 		nparams += 1;
664 		if (nparams >= MAX_NUMBER_OF_SVCPARAMS)
665 			return LDNS_WIREPARSE_ERR_SVCB_TOO_MANY_PARAMS;
666 	}
667 
668 	/* In draft-ietf-dnsop-svcb-https-06 Section 7:
669 	 *
670 	 *     In wire format, the keys are represented by their numeric
671 	 *     values in network byte order, concatenated in ascending order.
672 	 */
673 	qsort((void *)svcparams
674 	     ,nparams
675 	     ,sizeof(uint8_t*)
676 	     ,sldns_str2wire_svcparam_key_cmp);
677 
678 
679 	/* The code below revolves around semantic errors in the SVCParam set.
680 	 * So long as we do not distinguish between running Unbound as a primary
681 	 * or as a secondary, we default to secondary behavior and we ignore the
682 	 * semantic errors. */
683 
684 #ifdef SVCB_SEMANTIC_ERRORS
685 	{
686 		uint8_t* mandatory = NULL;
687 		/* In draft-ietf-dnsop-svcb-https-06 Section 7:
688 		 *
689 		 *     Keys (...) MUST NOT appear more than once.
690 		 *
691 		 * If they key has already been seen, we have a duplicate
692 		 */
693 		for(i=0; i < nparams; i++) {
694 			uint16_t key = sldns_read_uint16(svcparams[i]);
695 			if(i + 1 < nparams && key == sldns_read_uint16(svcparams[i+1]))
696 				return LDNS_WIREPARSE_ERR_SVCB_DUPLICATE_KEYS;
697 			if(key == SVCB_KEY_MANDATORY)
698 				mandatory = svcparams[i];
699 		}
700 
701 		/* 4. verify that all the SvcParamKeys in mandatory are present */
702 		if(mandatory) {
703 			/* Divide by sizeof(uint16_t)*/
704 			uint16_t mandatory_nkeys = sldns_read_uint16(mandatory + 2) / sizeof(uint16_t);
705 
706 			/* Guaranteed by sldns_str2wire_svcparam_key_value */
707 			assert(mandatory_nkeys > 0);
708 
709 			for(i=0; i < mandatory_nkeys; i++) {
710 				uint16_t mandatory_key = sldns_read_uint16(
711 					mandatory
712 					+ 2 * sizeof(uint16_t)
713 					+ i * sizeof(uint16_t));
714 				uint8_t found = 0;
715 				size_t j;
716 
717 				for(j=0; j < nparams; j++) {
718 					if(mandatory_key == sldns_read_uint16(svcparams[j])) {
719 						found = 1;
720 						break;
721 					}
722 				}
723 
724 				if(!found)
725 					return LDNS_WIREPARSE_ERR_SVCB_MANDATORY_MISSING_PARAM;
726 			}
727 		}
728 	}
729 #endif
730 	/* Write rdata in correct order */
731 	for (i = 0; i < nparams; i++) {
732 		uint16_t svcparam_len = sldns_read_uint16(svcparams[i] + 2)
733 		                      + 2 * sizeof(uint16_t);
734 
735 		if ((unsigned)(new_rdata_ptr - new_rdata) + svcparam_len > sizeof(new_rdata))
736 			return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
737 
738 		memcpy(new_rdata_ptr, svcparams[i], svcparam_len);
739 		new_rdata_ptr += svcparam_len;
740 	}
741 	memcpy(rdata, new_rdata, rdata_len);
742 	return LDNS_WIREPARSE_ERR_OK;
743 }
744 
745 /** parse rdata from string into rr buffer(-remainder after dname). */
746 static int
747 rrinternal_parse_rdata(sldns_buffer* strbuf, char* token, size_t token_len,
748 	uint8_t* rr, size_t* rr_len, size_t dname_len, uint16_t rr_type,
749 	uint8_t* origin, size_t origin_len)
750 {
751 	const sldns_rr_descriptor *desc = sldns_rr_descript((uint16_t)rr_type);
752 	size_t r_cnt, r_min, r_max;
753 	size_t rr_cur_len = dname_len + 10, pre_data_pos, token_strlen;
754 	int was_unknown_rr_format = 0, parens = 0, status, quoted;
755 	const char* delimiters;
756 	sldns_rdf_type rdftype;
757 	/* a desc is always returned */
758 	if(!desc) return LDNS_WIREPARSE_ERR_GENERAL;
759 	r_max = sldns_rr_descriptor_maximum(desc);
760 	r_min = sldns_rr_descriptor_minimum(desc);
761 	/* robust check */
762 	if(rr_cur_len > *rr_len)
763 		return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
764 			sldns_buffer_position(strbuf));
765 
766 	/* because number of fields can be variable, we can't rely on
767 	 * _maximum() only */
768 	for(r_cnt=0; r_cnt < r_max; r_cnt++) {
769 		rdftype = sldns_rr_descriptor_field_type(desc, r_cnt);
770 		delimiters = rrinternal_get_delims(rdftype, r_cnt, r_max);
771 		quoted = rrinternal_get_quoted(strbuf, &delimiters, rdftype);
772 
773 		if(!sldns_parse_rdf_token(strbuf, token, token_len, &quoted,
774 			&parens, &pre_data_pos, delimiters, rdftype,
775 			&token_strlen))
776 			break;
777 
778 		/* rfc3597 specifies that any type can be represented
779 		 * with \# method, which can contain spaces...
780 		 * it does specify size though... */
781 
782 		/* unknown RR data */
783 		if(token_strlen>=2 && strncmp(token, "\\#", 2) == 0 &&
784 			!quoted && (token_strlen == 2 || token[2]==' ' ||
785 			token[2]=='\t')) {
786 			was_unknown_rr_format = 1;
787 			if((status=rrinternal_parse_unknown(strbuf, token,
788 				token_len, rr, rr_len, &rr_cur_len,
789 				pre_data_pos)) != 0)
790 				return status;
791 		} else if(token_strlen > 0 || quoted) {
792 			if(rdftype == LDNS_RDF_TYPE_HIP) {
793 				/* affix the HIT and PK fields, with a space */
794 				if(!sldns_affix_token(strbuf, token,
795 					&token_len, &quoted, &parens,
796 					&pre_data_pos, delimiters,
797 					rdftype, &token_strlen))
798 					break;
799 				if(!sldns_affix_token(strbuf, token,
800 					&token_len, &quoted, &parens,
801 					&pre_data_pos, delimiters,
802 					rdftype, &token_strlen))
803 					break;
804 			} else if(rdftype == LDNS_RDF_TYPE_INT16_DATA &&
805 				strcmp(token, "0")!=0) {
806 				/* affix len and b64 fields */
807 				if(!sldns_affix_token(strbuf, token,
808 					&token_len, &quoted, &parens,
809 					&pre_data_pos, delimiters,
810 					rdftype, &token_strlen))
811 					break;
812 			}
813 
814 			/* normal RR */
815 			if((status=rrinternal_parse_rdf(strbuf, token,
816 				token_len, rr, *rr_len, &rr_cur_len, rdftype,
817 				rr_type, r_cnt, r_max, dname_len, origin,
818 				origin_len)) != 0) {
819 				return status;
820 			}
821 		}
822 	}
823 	if(!was_unknown_rr_format && r_cnt+1 < r_min) {
824 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_MISSING_VALUE,
825 			sldns_buffer_position(strbuf));
826 	}
827 	while(parens != 0) {
828 		/* read remainder, must be "" */
829 		if(sldns_bget_token_par(strbuf, token, "\n", token_len,
830 			&parens, " \t") == -1) {
831 			if(parens != 0)
832 				return RET_ERR(LDNS_WIREPARSE_ERR_PARENTHESIS,
833 					sldns_buffer_position(strbuf));
834 			break;
835 		}
836 		if(strcmp(token, "") != 0)
837 			return RET_ERR(LDNS_WIREPARSE_ERR_PARENTHESIS,
838 				sldns_buffer_position(strbuf));
839 	}
840 	/* write rdata length */
841 	sldns_write_uint16(rr+dname_len+8, (uint16_t)(rr_cur_len-dname_len-10));
842 	*rr_len = rr_cur_len;
843 	/* SVCB/HTTPS handling  */
844 	if (rr_type == LDNS_RR_TYPE_SVCB || rr_type == LDNS_RR_TYPE_HTTPS) {
845 		size_t rdata_len = rr_cur_len - dname_len - 10;
846 		uint8_t *rdata = rr+dname_len + 10;
847 
848 		/* skip 1st rdata field SvcPriority (uint16_t) */
849 		if (rdata_len < sizeof(uint16_t))
850 			return LDNS_WIREPARSE_ERR_OK;
851 
852 		rdata_len -= sizeof(uint16_t);
853 		rdata += sizeof(uint16_t);
854 
855 		/* skip 2nd rdata field dname */
856 		while (rdata_len && *rdata != 0) {
857 			uint8_t label_len;
858 
859 			if (*rdata & 0xC0)
860 				return LDNS_WIREPARSE_ERR_OK;
861 
862 			label_len = *rdata + 1;
863 			if (rdata_len < label_len)
864 				return LDNS_WIREPARSE_ERR_OK;
865 
866 			rdata_len -= label_len;
867 			rdata += label_len;
868 		}
869 		/* The root label is one more character, so smaller
870 		 * than 1 + 1 means no Svcparam Keys */
871 		if (rdata_len < 2 || *rdata != 0)
872 			return LDNS_WIREPARSE_ERR_OK;
873 
874 		rdata_len -= 1;
875 		rdata += 1;
876 		return sldns_str2wire_check_svcbparams(rdata, rdata_len);
877 
878 	}
879 	return LDNS_WIREPARSE_ERR_OK;
880 }
881 
882 /*
883  * trailing spaces are allowed
884  * leading spaces are not allowed
885  * allow ttl to be optional
886  * class is optional too
887  * if ttl is missing, and default_ttl is 0, use DEF_TTL
888  * allow ttl to be written as 1d3h
889  * So the RR should look like. e.g.
890  * miek.nl. 3600 IN MX 10 elektron.atoom.net
891  * or
892  * miek.nl. 1h IN MX 10 elektron.atoom.net
893  * or
894  * miek.nl. IN MX 10 elektron.atoom.net
895  */
896 static int
897 sldns_str2wire_rr_buf_internal(const char* str, uint8_t* rr, size_t* len,
898 	size_t* dname_len, uint32_t default_ttl, uint8_t* origin,
899 	size_t origin_len, uint8_t* prev, size_t prev_len, int question)
900 {
901 	int status;
902 	int not_there = 0;
903 	char token[LDNS_MAX_RDFLEN+1];
904 	uint32_t ttl = 0;
905 	uint16_t tp = 0, cl = 0;
906 	size_t ddlen = 0;
907 
908 	/* string in buffer */
909 	sldns_buffer strbuf;
910 	sldns_buffer_init_frm_data(&strbuf, (uint8_t*)str, strlen(str));
911 	if(!dname_len) dname_len = &ddlen;
912 
913 	/* parse the owner */
914 	if((status=rrinternal_get_owner(&strbuf, rr, len, dname_len, origin,
915 		origin_len, prev, prev_len, token, sizeof(token))) != 0)
916 		return status;
917 
918 	/* parse the [ttl] [class] <type> */
919 	if((status=rrinternal_get_ttl(&strbuf, token, sizeof(token),
920 		&not_there, &ttl, default_ttl)) != 0)
921 		return status;
922 	if((status=rrinternal_get_class(&strbuf, token, sizeof(token),
923 		&not_there, &cl)) != 0)
924 		return status;
925 	if((status=rrinternal_get_type(&strbuf, token, sizeof(token),
926 		&not_there, &tp)) != 0)
927 		return status;
928 	/* put ttl, class, type into the rr result */
929 	if((status=rrinternal_write_typeclassttl(&strbuf, rr, *len, *dname_len, tp, cl,
930 		ttl, question)) != 0)
931 		return status;
932 	/* for a question-RR we are done, no rdata */
933 	if(question) {
934 		*len = *dname_len + 4;
935 		return LDNS_WIREPARSE_ERR_OK;
936 	}
937 
938 	/* rdata */
939 	if((status=rrinternal_parse_rdata(&strbuf, token, sizeof(token),
940 		rr, len, *dname_len, tp, origin, origin_len)) != 0)
941 		return status;
942 
943 	return LDNS_WIREPARSE_ERR_OK;
944 }
945 
946 int sldns_str2wire_rr_buf(const char* str, uint8_t* rr, size_t* len,
947 	size_t* dname_len, uint32_t default_ttl, uint8_t* origin,
948 	size_t origin_len, uint8_t* prev, size_t prev_len)
949 {
950 	return sldns_str2wire_rr_buf_internal(str, rr, len, dname_len,
951 		default_ttl, origin, origin_len, prev, prev_len, 0);
952 }
953 
954 int sldns_str2wire_rr_question_buf(const char* str, uint8_t* rr, size_t* len,
955 	size_t* dname_len, uint8_t* origin, size_t origin_len, uint8_t* prev,
956 	size_t prev_len)
957 {
958 	return sldns_str2wire_rr_buf_internal(str, rr, len, dname_len,
959 		0, origin, origin_len, prev, prev_len, 1);
960 }
961 
962 uint16_t sldns_wirerr_get_type(uint8_t* rr, size_t len, size_t dname_len)
963 {
964 	if(len < dname_len+2)
965 		return 0;
966 	return sldns_read_uint16(rr+dname_len);
967 }
968 
969 uint16_t sldns_wirerr_get_class(uint8_t* rr, size_t len, size_t dname_len)
970 {
971 	if(len < dname_len+4)
972 		return 0;
973 	return sldns_read_uint16(rr+dname_len+2);
974 }
975 
976 uint32_t sldns_wirerr_get_ttl(uint8_t* rr, size_t len, size_t dname_len)
977 {
978 	if(len < dname_len+8)
979 		return 0;
980 	return sldns_read_uint32(rr+dname_len+4);
981 }
982 
983 uint16_t sldns_wirerr_get_rdatalen(uint8_t* rr, size_t len, size_t dname_len)
984 {
985 	if(len < dname_len+10)
986 		return 0;
987 	return sldns_read_uint16(rr+dname_len+8);
988 }
989 
990 uint8_t* sldns_wirerr_get_rdata(uint8_t* rr, size_t len, size_t dname_len)
991 {
992 	if(len < dname_len+10)
993 		return NULL;
994 	return rr+dname_len+10;
995 }
996 
997 uint8_t* sldns_wirerr_get_rdatawl(uint8_t* rr, size_t len, size_t dname_len)
998 {
999 	if(len < dname_len+10)
1000 		return NULL;
1001 	return rr+dname_len+8;
1002 }
1003 
1004 const char* sldns_get_errorstr_parse(int e)
1005 {
1006 	sldns_lookup_table *lt;
1007 	lt = sldns_lookup_by_id(sldns_wireparse_errors, LDNS_WIREPARSE_ERROR(e));
1008 	return lt?lt->name:"unknown error";
1009 }
1010 
1011 /* Strip whitespace from the start and the end of <line>.  */
1012 char *
1013 sldns_strip_ws(char *line)
1014 {
1015         char *s = line, *e;
1016 
1017         for (s = line; *s && isspace((unsigned char)*s); s++)
1018                 ;
1019         for (e = strchr(s, 0); e > s+2 && isspace((unsigned char)e[-1]) && e[-2] != '\\'; e--)
1020                 ;
1021         *e = 0;
1022         return s;
1023 }
1024 
1025 int sldns_fp2wire_rr_buf(FILE* in, uint8_t* rr, size_t* len, size_t* dname_len,
1026 	struct sldns_file_parse_state* parse_state)
1027 {
1028 	char line[LDNS_RR_BUF_SIZE+1];
1029 	ssize_t size;
1030 
1031 	/* read an entire line in from the file */
1032 	if((size = sldns_fget_token_l(in, line, LDNS_PARSE_SKIP_SPACE,
1033 		LDNS_RR_BUF_SIZE, parse_state?&parse_state->lineno:NULL))
1034 		== -1) {
1035 		/* if last line was empty, we are now at feof, which is not
1036 		 * always a parse error (happens when for instance last line
1037 		 * was a comment)
1038 		 */
1039 		return LDNS_WIREPARSE_ERR_SYNTAX;
1040 	}
1041 
1042 	/* we can have the situation, where we've read ok, but still got
1043 	 * no bytes to play with, in this case size is 0 */
1044 	if(size == 0) {
1045 		if(*len > 0)
1046 			rr[0] = 0;
1047 		*len = 0;
1048 		*dname_len = 0;
1049 		return LDNS_WIREPARSE_ERR_OK;
1050 	}
1051 
1052 	if(strncmp(line, "$ORIGIN", 7) == 0 && isspace((unsigned char)line[7])) {
1053 		int s;
1054 		strlcpy((char*)rr, line, *len);
1055 		*len = 0;
1056 		*dname_len = 0;
1057 		if(!parse_state) return LDNS_WIREPARSE_ERR_OK;
1058 		parse_state->origin_len = sizeof(parse_state->origin);
1059 		s = sldns_str2wire_dname_buf(sldns_strip_ws(line+8),
1060 			parse_state->origin, &parse_state->origin_len);
1061 		if(s) parse_state->origin_len = 0;
1062 		return s;
1063 	} else if(strncmp(line, "$TTL", 4) == 0 && isspace((unsigned char)line[4])) {
1064 		const char* end = NULL;
1065 		int overflow = 0;
1066 		strlcpy((char*)rr, line, *len);
1067 		*len = 0;
1068 		*dname_len = 0;
1069 		if(!parse_state) return LDNS_WIREPARSE_ERR_OK;
1070 		parse_state->default_ttl = sldns_str2period(
1071 			sldns_strip_ws(line+5), &end, &overflow);
1072 		if(overflow)
1073 			return LDNS_WIREPARSE_ERR_SYNTAX_INTEGER_OVERFLOW;
1074 	} else if (strncmp(line, "$INCLUDE", 8) == 0) {
1075 		strlcpy((char*)rr, line, *len);
1076 		*len = 0;
1077 		*dname_len = 0;
1078 		return LDNS_WIREPARSE_ERR_INCLUDE;
1079 	} else if (strncmp(line, "$", 1) == 0) {
1080 		strlcpy((char*)rr, line, *len);
1081 		*len = 0;
1082 		*dname_len = 0;
1083 		return LDNS_WIREPARSE_ERR_INCLUDE;
1084 	} else {
1085 		int r = sldns_str2wire_rr_buf(line, rr, len, dname_len,
1086 			parse_state?parse_state->default_ttl:0,
1087 			(parse_state&&parse_state->origin_len)?
1088 				parse_state->origin:NULL,
1089 			parse_state?parse_state->origin_len:0,
1090 			(parse_state&&parse_state->prev_rr_len)?
1091 				parse_state->prev_rr:NULL,
1092 			parse_state?parse_state->prev_rr_len:0);
1093 		if(r == LDNS_WIREPARSE_ERR_OK && (*dname_len) != 0 &&
1094 			parse_state &&
1095 			(*dname_len) <= sizeof(parse_state->prev_rr)) {
1096 			memmove(parse_state->prev_rr, rr, *dname_len);
1097 			parse_state->prev_rr_len = (*dname_len);
1098 		}
1099 		if(r == LDNS_WIREPARSE_ERR_OK && parse_state) {
1100 			parse_state->default_ttl = sldns_wirerr_get_ttl(
1101 				rr, *len, *dname_len);
1102 		}
1103 		return r;
1104 	}
1105 	return LDNS_WIREPARSE_ERR_OK;
1106 }
1107 
1108 static int
1109 sldns_str2wire_svcparam_key_lookup(const char *key, size_t key_len)
1110 {
1111 	char buf[64];
1112 	char *endptr;
1113 	unsigned long int key_value;
1114 
1115 	if (key_len >= 4  && key_len <= 8 && !strncmp(key, "key", 3)) {
1116 		memcpy(buf, key + 3, key_len - 3);
1117 		buf[key_len - 3] = 0;
1118 		key_value = strtoul(buf, &endptr, 10);
1119 
1120 		if (endptr > buf	/* digits seen */
1121 		&& *endptr == 0		/* no non-digit chars after digits */
1122 		&&  key_value <= 65535)	/* no overflow */
1123 			return key_value;
1124 
1125 	} else switch (key_len) {
1126 	case sizeof("mandatory")-1:
1127 		if (!strncmp(key, "mandatory", sizeof("mandatory")-1))
1128 			return SVCB_KEY_MANDATORY;
1129 		if (!strncmp(key, "echconfig", sizeof("echconfig")-1))
1130 			return SVCB_KEY_ECH; /* allow "echconfig" as well as "ech" */
1131 		break;
1132 
1133 	case sizeof("alpn")-1:
1134 		if (!strncmp(key, "alpn", sizeof("alpn")-1))
1135 			return SVCB_KEY_ALPN;
1136 		if (!strncmp(key, "port", sizeof("port")-1))
1137 			return SVCB_KEY_PORT;
1138 		break;
1139 
1140 	case sizeof("no-default-alpn")-1:
1141 		if (!strncmp( key  , "no-default-alpn"
1142 		            , sizeof("no-default-alpn")-1))
1143 			return SVCB_KEY_NO_DEFAULT_ALPN;
1144 		break;
1145 
1146 	case sizeof("ipv4hint")-1:
1147 		if (!strncmp(key, "ipv4hint", sizeof("ipv4hint")-1))
1148 			return SVCB_KEY_IPV4HINT;
1149 		if (!strncmp(key, "ipv6hint", sizeof("ipv6hint")-1))
1150 			return SVCB_KEY_IPV6HINT;
1151 		break;
1152 
1153 	case sizeof("ech")-1:
1154 		if (!strncmp(key, "ech", sizeof("ech")-1))
1155 			return SVCB_KEY_ECH;
1156 		break;
1157 
1158 	default:
1159 		break;
1160 	}
1161 
1162 	/* Although the returned value might be used by the caller,
1163 	 * the parser has erred, so the zone will not be loaded.
1164 	 */
1165 	return -1;
1166 }
1167 
1168 static int
1169 sldns_str2wire_svcparam_port(const char* val, uint8_t* rd, size_t* rd_len)
1170 {
1171 	unsigned long int port;
1172 	char *endptr;
1173 
1174 	if (*rd_len < 6)
1175 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1176 
1177 	port = strtoul(val, &endptr, 10);
1178 
1179 	if (endptr > val	/* digits seen */
1180 	&& *endptr == 0		/* no non-digit chars after digits */
1181 	&&  port <= 65535) {	/* no overflow */
1182 
1183 		sldns_write_uint16(rd, SVCB_KEY_PORT);
1184 		sldns_write_uint16(rd + 2, sizeof(uint16_t));
1185 		sldns_write_uint16(rd + 4, port);
1186 		*rd_len = 6;
1187 
1188 		return LDNS_WIREPARSE_ERR_OK;
1189 	}
1190 
1191 	return LDNS_WIREPARSE_ERR_SVCB_PORT_VALUE_SYNTAX;
1192 }
1193 
1194 static int
1195 sldns_str2wire_svcbparam_ipv4hint(const char* val, uint8_t* rd, size_t* rd_len)
1196 {
1197 	size_t count;
1198 	char ip_str[INET_ADDRSTRLEN+1];
1199 	char *next_ip_str;
1200 	size_t i;
1201 
1202 	for (i = 0, count = 1; val[i]; i++) {
1203 		if (val[i] == ',')
1204 			count += 1;
1205 		if (count > SVCB_MAX_COMMA_SEPARATED_VALUES) {
1206 			return LDNS_WIREPARSE_ERR_SVCB_IPV4_TOO_MANY_ADDRESSES;
1207 		}
1208 	}
1209 
1210 	if (*rd_len < (LDNS_IP4ADDRLEN * count) + 4)
1211 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1212 
1213 	/* count is number of comma's in val + 1; so the actual number of IPv4
1214 	 * addresses in val
1215 	 */
1216 	sldns_write_uint16(rd, SVCB_KEY_IPV4HINT);
1217 	sldns_write_uint16(rd + 2, LDNS_IP4ADDRLEN * count);
1218 	*rd_len = 4;
1219 
1220 	while (count) {
1221 		if (!(next_ip_str = strchr(val, ','))) {
1222 			if (inet_pton(AF_INET, val, rd + *rd_len) != 1)
1223 				break;
1224 			*rd_len += LDNS_IP4ADDRLEN;
1225 
1226 			assert(count == 1);
1227 
1228 		} else if (next_ip_str - val >= (int)sizeof(ip_str))
1229 			break;
1230 
1231 		else {
1232 			memcpy(ip_str, val, next_ip_str - val);
1233 			ip_str[next_ip_str - val] = 0;
1234 			if (inet_pton(AF_INET, ip_str, rd + *rd_len) != 1) {
1235 				break;
1236 			}
1237 			*rd_len += LDNS_IP4ADDRLEN;
1238 
1239 			val = next_ip_str + 1;
1240 		}
1241 		count--;
1242 	}
1243 	if (count) /* verify that we parsed all values */
1244 		return LDNS_WIREPARSE_ERR_SYNTAX_IP4;
1245 
1246 	return LDNS_WIREPARSE_ERR_OK;
1247 }
1248 
1249 static int
1250 sldns_str2wire_svcbparam_ipv6hint(const char* val, uint8_t* rd, size_t* rd_len)
1251 {
1252 	size_t count;
1253 	char ip_str[INET6_ADDRSTRLEN+1];
1254 	char *next_ip_str;
1255 	size_t i;
1256 
1257 	for (i = 0, count = 1; val[i]; i++) {
1258 		if (val[i] == ',')
1259 			count += 1;
1260 		if (count > SVCB_MAX_COMMA_SEPARATED_VALUES) {
1261 			return LDNS_WIREPARSE_ERR_SVCB_IPV6_TOO_MANY_ADDRESSES;
1262 		}
1263 	}
1264 
1265 	if (*rd_len < (LDNS_IP6ADDRLEN * count) + 4)
1266 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1267 
1268 	/* count is number of comma's in val + 1; so the actual number of IPv6
1269 	 * addresses in val
1270 	 */
1271 	sldns_write_uint16(rd, SVCB_KEY_IPV6HINT);
1272 	sldns_write_uint16(rd + 2, LDNS_IP6ADDRLEN * count);
1273 	*rd_len = 4;
1274 
1275 	while (count) {
1276 		if (!(next_ip_str = strchr(val, ','))) {
1277 			if (inet_pton(AF_INET6, val, rd + *rd_len) != 1)
1278 				break;
1279 			*rd_len += LDNS_IP6ADDRLEN;
1280 
1281 			assert(count == 1);
1282 
1283 		} else if (next_ip_str - val >= (int)sizeof(ip_str))
1284 			break;
1285 
1286 		else {
1287 			memcpy(ip_str, val, next_ip_str - val);
1288 			ip_str[next_ip_str - val] = 0;
1289 			if (inet_pton(AF_INET6, ip_str, rd + *rd_len) != 1) {
1290 				break;
1291 			}
1292 			*rd_len += LDNS_IP6ADDRLEN;
1293 
1294 			val = next_ip_str + 1;
1295 		}
1296 		count--;
1297 	}
1298 	if (count) /* verify that we parsed all values */
1299 		return LDNS_WIREPARSE_ERR_SYNTAX_IP6;
1300 
1301 	return LDNS_WIREPARSE_ERR_OK;
1302 }
1303 
1304 /* compare function used for sorting uint16_t's */
1305 static int
1306 sldns_network_uint16_cmp(const void *a, const void *b)
1307 {
1308 	return ((int)sldns_read_uint16(a)) - ((int)sldns_read_uint16(b));
1309 }
1310 
1311 static int
1312 sldns_str2wire_svcbparam_mandatory(const char* val, uint8_t* rd, size_t* rd_len)
1313 {
1314 	size_t i, count, val_len;
1315 	char* next_key;
1316 
1317 	val_len = strlen(val);
1318 
1319 	for (i = 0, count = 1; val[i]; i++) {
1320 		if (val[i] == ',')
1321 			count += 1;
1322 		if (count > SVCB_MAX_COMMA_SEPARATED_VALUES) {
1323 			return LDNS_WIREPARSE_ERR_SVCB_MANDATORY_TOO_MANY_KEYS;
1324 		}
1325 	}
1326 	if (sizeof(uint16_t) * (count + 2) > *rd_len)
1327 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1328 
1329 	sldns_write_uint16(rd, SVCB_KEY_MANDATORY);
1330 	sldns_write_uint16(rd + 2, sizeof(uint16_t) * count);
1331 	*rd_len = 4;
1332 
1333 	while (1) {
1334 		int svcparamkey;
1335 
1336 		if (!(next_key = strchr(val, ','))) {
1337 			svcparamkey = sldns_str2wire_svcparam_key_lookup(val, val_len);
1338 
1339 			if (svcparamkey < 0) {
1340 				return LDNS_WIREPARSE_ERR_SVCB_UNKNOWN_KEY;
1341 			}
1342 
1343 			sldns_write_uint16(rd + *rd_len, svcparamkey);
1344 			*rd_len += 2;
1345 			break;
1346 		} else {
1347 			svcparamkey = sldns_str2wire_svcparam_key_lookup(val, next_key - val);
1348 
1349 			if (svcparamkey < 0) {
1350 				return LDNS_WIREPARSE_ERR_SVCB_UNKNOWN_KEY;
1351 			}
1352 
1353 			sldns_write_uint16(rd + *rd_len,
1354 				svcparamkey);
1355 			*rd_len += 2;
1356 		}
1357 
1358 		val_len -= next_key - val + 1;
1359 		val = next_key + 1; /* skip the comma */
1360 	}
1361 
1362 	/* In draft-ietf-dnsop-svcb-https-06 Section 7:
1363 	 *
1364 	 *    "In wire format, the keys are represented by their numeric
1365 	 *     values in network byte order, concatenated in ascending order."
1366 	 */
1367 	qsort((void *)(rd + 4), count, sizeof(uint16_t), sldns_network_uint16_cmp);
1368 
1369 	/* The code below revolves around semantic errors in the SVCParam set.
1370 	 * So long as we do not distinguish between running Unbound as a primary
1371 	 * or as a secondary, we default to secondary behavior and we ignore the
1372 	 * semantic errors. */
1373 #ifdef SVCB_SEMANTIC_ERRORS
1374 	/* In draft-ietf-dnsop-svcb-https-06 Section 8
1375 	 * automatically mandatory MUST NOT appear in its own value-list
1376 	 */
1377 	if (sldns_read_uint16(rd + 4) == SVCB_KEY_MANDATORY)
1378 		return LDNS_WIREPARSE_ERR_SVCB_MANDATORY_IN_MANDATORY;
1379 
1380 	/* Guarantee key uniqueness. After the sort we only need to
1381 	 * compare neighbouring keys */
1382 	if (count > 1) {
1383 		for (i = 0; i < count - 1; i++) {
1384 			uint8_t* current_pos = (rd + 4 + (sizeof(uint16_t) * i));
1385 			uint16_t key = sldns_read_uint16(current_pos);
1386 
1387 			if (key == sldns_read_uint16(current_pos + 2)) {
1388 				return LDNS_WIREPARSE_ERR_SVCB_MANDATORY_DUPLICATE_KEY;
1389 			}
1390 		}
1391 	}
1392 #endif
1393 	return LDNS_WIREPARSE_ERR_OK;
1394 }
1395 
1396 static int
1397 sldns_str2wire_svcbparam_ech_value(const char* val, uint8_t* rd, size_t* rd_len)
1398 {
1399 	uint8_t buffer[LDNS_MAX_RDFLEN];
1400 	int wire_len;
1401 
1402 	/* single 0 represents empty buffer */
1403 	if(strcmp(val, "0") == 0) {
1404 		if (*rd_len < 4)
1405 			return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1406 		sldns_write_uint16(rd, SVCB_KEY_ECH);
1407 		sldns_write_uint16(rd + 2, 0);
1408 
1409 		return LDNS_WIREPARSE_ERR_OK;
1410 	}
1411 
1412 	wire_len = sldns_b64_pton(val, buffer, LDNS_MAX_RDFLEN);
1413 
1414 	if (wire_len <= 0) {
1415 		return LDNS_WIREPARSE_ERR_SYNTAX_B64;
1416 	} else if ((unsigned)wire_len + 4 > *rd_len) {
1417 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1418 	} else {
1419 		sldns_write_uint16(rd, SVCB_KEY_ECH);
1420 		sldns_write_uint16(rd + 2, wire_len);
1421 		memcpy(rd + 4, buffer, wire_len);
1422 		*rd_len = 4 + wire_len;
1423 
1424 		return LDNS_WIREPARSE_ERR_OK;
1425 	}
1426 }
1427 
1428 static const char*
1429 sldns_str2wire_svcbparam_parse_next_unescaped_comma(const char *val)
1430 {
1431 	while (*val) {
1432 		/* Only return when the comma is not escaped*/
1433 		if (*val == '\\'){
1434 			++val;
1435 			if (!*val)
1436 				break;
1437 		} else if (*val == ',')
1438 				return val;
1439 
1440 		val++;
1441 	}
1442 	return NULL;
1443 }
1444 
1445 /* The source is already properly unescaped, this double unescaping is purely to allow for
1446  * comma's in comma separated alpn lists.
1447  *
1448  * In draft-ietf-dnsop-svcb-https-06 Section 7:
1449  * To enable simpler parsing, this SvcParamValue MUST NOT contain escape sequences.
1450  */
1451 static size_t
1452 sldns_str2wire_svcbparam_parse_copy_unescaped(uint8_t *dst,
1453 	const char *src, size_t len)
1454 {
1455 	uint8_t *orig_dst = dst;
1456 
1457 	while (len) {
1458 		if (*src == '\\') {
1459 			src++;
1460 			len--;
1461 			if (!len)
1462 				break;
1463 		}
1464 		*dst++ = *src++;
1465 		len--;
1466 	}
1467 	return (size_t)(dst - orig_dst);
1468 }
1469 
1470 static int
1471 sldns_str2wire_svcbparam_alpn_value(const char* val,
1472 	uint8_t* rd, size_t* rd_len)
1473 {
1474 	uint8_t     unescaped_dst[LDNS_MAX_RDFLEN];
1475 	uint8_t    *dst = unescaped_dst;
1476 	const char *next_str;
1477 	size_t      str_len;
1478 	size_t      dst_len;
1479 	size_t      val_len;
1480 
1481 	val_len = strlen(val);
1482 
1483 	if (val_len > sizeof(unescaped_dst)) {
1484 		return LDNS_WIREPARSE_ERR_SVCB_ALPN_KEY_TOO_LARGE;
1485 	}
1486 	while (val_len) {
1487 		size_t key_len;
1488 
1489 		str_len = (next_str = sldns_str2wire_svcbparam_parse_next_unescaped_comma(val))
1490 		        ? (size_t)(next_str - val) : val_len;
1491 
1492 		if (str_len > 255) {
1493 			return LDNS_WIREPARSE_ERR_SVCB_ALPN_KEY_TOO_LARGE;
1494 		}
1495 
1496 		key_len = sldns_str2wire_svcbparam_parse_copy_unescaped(dst + 1, val, str_len);
1497 		*dst++ = key_len;
1498 		 dst  += key_len;
1499 
1500 		if (!next_str)
1501 			break;
1502 
1503 		/* skip the comma in the next iteration */
1504 		val_len -= next_str - val + 1;
1505 		val = next_str + 1;
1506 	}
1507 	dst_len = dst - unescaped_dst;
1508 	if (*rd_len < 4 + dst_len)
1509 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1510 	sldns_write_uint16(rd, SVCB_KEY_ALPN);
1511 	sldns_write_uint16(rd + 2, dst_len);
1512 	memcpy(rd + 4, unescaped_dst, dst_len);
1513 	*rd_len = 4 + dst_len;
1514 
1515 	return LDNS_WIREPARSE_ERR_OK;
1516 }
1517 
1518 static int
1519 sldns_str2wire_svcparam_value(const char *key, size_t key_len,
1520 	const char *val, uint8_t* rd, size_t* rd_len)
1521 {
1522 	size_t str_len;
1523 	int svcparamkey = sldns_str2wire_svcparam_key_lookup(key, key_len);
1524 
1525 	if (svcparamkey < 0) {
1526 		return LDNS_WIREPARSE_ERR_SVCB_UNKNOWN_KEY;
1527 	}
1528 
1529 	/* key without value */
1530 	if (val == NULL) {
1531 		switch (svcparamkey) {
1532 #ifdef SVCB_SEMANTIC_ERRORS
1533 		case SVCB_KEY_MANDATORY:
1534 		case SVCB_KEY_ALPN:
1535 		case SVCB_KEY_PORT:
1536 		case SVCB_KEY_IPV4HINT:
1537 		case SVCB_KEY_IPV6HINT:
1538 			return LDNS_WIREPARSE_ERR_SVCB_MISSING_PARAM;
1539 #endif
1540 		default:
1541 			if (*rd_len < 4)
1542 				return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1543 			sldns_write_uint16(rd, svcparamkey);
1544 			sldns_write_uint16(rd + 2, 0);
1545 			*rd_len = 4;
1546 
1547 			return LDNS_WIREPARSE_ERR_OK;
1548 		}
1549 	}
1550 
1551 	/* value is non-empty */
1552 	switch (svcparamkey) {
1553 	case SVCB_KEY_PORT:
1554 		return sldns_str2wire_svcparam_port(val, rd, rd_len);
1555 	case SVCB_KEY_IPV4HINT:
1556 		return sldns_str2wire_svcbparam_ipv4hint(val, rd, rd_len);
1557 	case SVCB_KEY_IPV6HINT:
1558 		return sldns_str2wire_svcbparam_ipv6hint(val, rd, rd_len);
1559 	case SVCB_KEY_MANDATORY:
1560 		return sldns_str2wire_svcbparam_mandatory(val, rd, rd_len);
1561 #ifdef SVCB_SEMANTIC_ERRORS
1562 	case SVCB_KEY_NO_DEFAULT_ALPN:
1563 		return LDNS_WIREPARSE_ERR_SVCB_NO_DEFAULT_ALPN_VALUE;
1564 #endif
1565 	case SVCB_KEY_ECH:
1566 		return sldns_str2wire_svcbparam_ech_value(val, rd, rd_len);
1567 	case SVCB_KEY_ALPN:
1568 		return sldns_str2wire_svcbparam_alpn_value(val, rd, rd_len);
1569 	default:
1570 		str_len = strlen(val);
1571 		if (*rd_len < 4 + str_len)
1572 			return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1573 		sldns_write_uint16(rd, svcparamkey);
1574 		sldns_write_uint16(rd + 2, str_len);
1575 		memcpy(rd + 4, val, str_len);
1576 		*rd_len = 4 + str_len;
1577 
1578 		return LDNS_WIREPARSE_ERR_OK;
1579 	}
1580 
1581 	return LDNS_WIREPARSE_ERR_GENERAL;
1582 }
1583 
1584 static int sldns_str2wire_svcparam_buf(const char* str, uint8_t* rd, size_t* rd_len)
1585 {
1586 	const char* eq_pos;
1587 	char unescaped_val[LDNS_MAX_RDFLEN];
1588 	char* val_out = unescaped_val;
1589 	const char* val_in;
1590 
1591 	eq_pos = strchr(str, '=');
1592 
1593 	/* case: key=value */
1594 	if (eq_pos != NULL && eq_pos[1]) {
1595 		val_in = eq_pos + 1;
1596 
1597 		/* unescape characters and "" blocks */
1598 		if (*val_in == '"') {
1599 			val_in++;
1600 			while (*val_in != '"'
1601 			&& (size_t)(val_out - unescaped_val + 1) < sizeof(unescaped_val)
1602 			&& sldns_parse_char( (uint8_t*) val_out, &val_in)) {
1603 				val_out++;
1604 			}
1605 		} else {
1606 			while ((size_t)(val_out - unescaped_val + 1) < sizeof(unescaped_val)
1607 			&& sldns_parse_char( (uint8_t*) val_out, &val_in)) {
1608 				val_out++;
1609 			}
1610 		}
1611 		*val_out = 0;
1612 
1613 		return sldns_str2wire_svcparam_value(str, eq_pos - str,
1614 		                                         unescaped_val[0] ? unescaped_val : NULL, rd, rd_len);
1615 	}
1616 	/* case: key= */
1617 	else if (eq_pos != NULL && !(eq_pos[1])) {
1618 		return sldns_str2wire_svcparam_value(str, eq_pos - str, NULL, rd, rd_len);
1619 	}
1620 	/* case: key */
1621 	else {
1622 		return sldns_str2wire_svcparam_value(str, strlen(str), NULL, rd, rd_len);
1623 	}
1624 }
1625 
1626 int sldns_str2wire_rdf_buf(const char* str, uint8_t* rd, size_t* len,
1627 	sldns_rdf_type rdftype)
1628 {
1629 	switch (rdftype) {
1630 	case LDNS_RDF_TYPE_DNAME:
1631 		return sldns_str2wire_dname_buf(str, rd, len);
1632 	case LDNS_RDF_TYPE_INT8:
1633 		return sldns_str2wire_int8_buf(str, rd, len);
1634 	case LDNS_RDF_TYPE_INT16:
1635 		return sldns_str2wire_int16_buf(str, rd, len);
1636 	case LDNS_RDF_TYPE_INT32:
1637 		return sldns_str2wire_int32_buf(str, rd, len);
1638 	case LDNS_RDF_TYPE_A:
1639 		return sldns_str2wire_a_buf(str, rd, len);
1640 	case LDNS_RDF_TYPE_AAAA:
1641 		return sldns_str2wire_aaaa_buf(str, rd, len);
1642 	case LDNS_RDF_TYPE_STR:
1643 		return sldns_str2wire_str_buf(str, rd, len);
1644 	case LDNS_RDF_TYPE_APL:
1645 		return sldns_str2wire_apl_buf(str, rd, len);
1646 	case LDNS_RDF_TYPE_B64:
1647 		return sldns_str2wire_b64_buf(str, rd, len);
1648 	case LDNS_RDF_TYPE_B32_EXT:
1649 		return sldns_str2wire_b32_ext_buf(str, rd, len);
1650 	case LDNS_RDF_TYPE_HEX:
1651 		return sldns_str2wire_hex_buf(str, rd, len);
1652 	case LDNS_RDF_TYPE_NSEC:
1653 		return sldns_str2wire_nsec_buf(str, rd, len);
1654 	case LDNS_RDF_TYPE_TYPE:
1655 		return sldns_str2wire_type_buf(str, rd, len);
1656 	case LDNS_RDF_TYPE_CLASS:
1657 		return sldns_str2wire_class_buf(str, rd, len);
1658 	case LDNS_RDF_TYPE_CERT_ALG:
1659 		return sldns_str2wire_cert_alg_buf(str, rd, len);
1660 	case LDNS_RDF_TYPE_ALG:
1661 		return sldns_str2wire_alg_buf(str, rd, len);
1662 	case LDNS_RDF_TYPE_TIME:
1663 		return sldns_str2wire_time_buf(str, rd, len);
1664 	case LDNS_RDF_TYPE_PERIOD:
1665 		return sldns_str2wire_period_buf(str, rd, len);
1666 	case LDNS_RDF_TYPE_TSIGTIME:
1667 		return sldns_str2wire_tsigtime_buf(str, rd, len);
1668 	case LDNS_RDF_TYPE_LOC:
1669 		return sldns_str2wire_loc_buf(str, rd, len);
1670 	case LDNS_RDF_TYPE_WKS:
1671 		return sldns_str2wire_wks_buf(str, rd, len);
1672 	case LDNS_RDF_TYPE_NSAP:
1673 		return sldns_str2wire_nsap_buf(str, rd, len);
1674 	case LDNS_RDF_TYPE_ATMA:
1675 		return sldns_str2wire_atma_buf(str, rd, len);
1676 	case LDNS_RDF_TYPE_IPSECKEY:
1677 		return sldns_str2wire_ipseckey_buf(str, rd, len);
1678 	case LDNS_RDF_TYPE_NSEC3_SALT:
1679 		return sldns_str2wire_nsec3_salt_buf(str, rd, len);
1680 	case LDNS_RDF_TYPE_NSEC3_NEXT_OWNER:
1681 		return sldns_str2wire_b32_ext_buf(str, rd, len);
1682 	case LDNS_RDF_TYPE_ILNP64:
1683 		return sldns_str2wire_ilnp64_buf(str, rd, len);
1684 	case LDNS_RDF_TYPE_EUI48:
1685 		return sldns_str2wire_eui48_buf(str, rd, len);
1686 	case LDNS_RDF_TYPE_EUI64:
1687 		return sldns_str2wire_eui64_buf(str, rd, len);
1688 	case LDNS_RDF_TYPE_TAG:
1689 		return sldns_str2wire_tag_buf(str, rd, len);
1690 	case LDNS_RDF_TYPE_LONG_STR:
1691 		return sldns_str2wire_long_str_buf(str, rd, len);
1692 	case LDNS_RDF_TYPE_TSIGERROR:
1693 		return sldns_str2wire_tsigerror_buf(str, rd, len);
1694 	case LDNS_RDF_TYPE_HIP:
1695 		return sldns_str2wire_hip_buf(str, rd, len);
1696 	case LDNS_RDF_TYPE_INT16_DATA:
1697 		return sldns_str2wire_int16_data_buf(str, rd, len);
1698 	case LDNS_RDF_TYPE_SVCPARAM:
1699 		return sldns_str2wire_svcparam_buf(str, rd, len);
1700 	case LDNS_RDF_TYPE_UNKNOWN:
1701 	case LDNS_RDF_TYPE_SERVICE:
1702 		return LDNS_WIREPARSE_ERR_NOT_IMPL;
1703 	case LDNS_RDF_TYPE_NONE:
1704 	default:
1705 		break;
1706 	}
1707 	return LDNS_WIREPARSE_ERR_GENERAL;
1708 }
1709 
1710 int sldns_str2wire_int8_buf(const char* str, uint8_t* rd, size_t* len)
1711 {
1712 	char* end;
1713 	uint8_t r = (uint8_t)strtol((char*)str, &end, 10);
1714 	if(*end != 0)
1715 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_INT, end-(char*)str);
1716 	if(*len < 1)
1717 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1718 	rd[0] = r;
1719 	*len = 1;
1720 	return LDNS_WIREPARSE_ERR_OK;
1721 }
1722 
1723 int sldns_str2wire_int16_buf(const char* str, uint8_t* rd, size_t* len)
1724 {
1725 	char* end;
1726 	uint16_t r = (uint16_t)strtol((char*)str, &end, 10);
1727 	if(*end != 0)
1728 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_INT, end-(char*)str);
1729 	if(*len < 2)
1730 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1731 	sldns_write_uint16(rd, r);
1732 	*len = 2;
1733 	return LDNS_WIREPARSE_ERR_OK;
1734 }
1735 
1736 int sldns_str2wire_int32_buf(const char* str, uint8_t* rd, size_t* len)
1737 {
1738 	char* end;
1739 	uint32_t r;
1740 	errno = 0; /* must set to zero before call,
1741 			note race condition on errno */
1742 	if(*str == '-')
1743 		r = (uint32_t)strtol((char*)str, &end, 10);
1744 	else	r = (uint32_t)strtoul((char*)str, &end, 10);
1745 	if(*end != 0)
1746 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_INT, end-(char*)str);
1747 	if(errno == ERANGE)
1748 		return LDNS_WIREPARSE_ERR_SYNTAX_INTEGER_OVERFLOW;
1749 	if(*len < 4)
1750 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1751 	sldns_write_uint32(rd, r);
1752 	*len = 4;
1753 	return LDNS_WIREPARSE_ERR_OK;
1754 }
1755 
1756 int sldns_str2wire_a_buf(const char* str, uint8_t* rd, size_t* len)
1757 {
1758 	struct in_addr address;
1759 	if(inet_pton(AF_INET, (char*)str, &address) != 1)
1760 		return LDNS_WIREPARSE_ERR_SYNTAX_IP4;
1761 	if(*len < sizeof(address))
1762 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1763 	memmove(rd, &address, sizeof(address));
1764 	*len = sizeof(address);
1765 	return LDNS_WIREPARSE_ERR_OK;
1766 }
1767 
1768 int sldns_str2wire_aaaa_buf(const char* str, uint8_t* rd, size_t* len)
1769 {
1770 #ifdef AF_INET6
1771 	uint8_t address[LDNS_IP6ADDRLEN + 1];
1772 	if(inet_pton(AF_INET6, (char*)str, address) != 1)
1773 		return LDNS_WIREPARSE_ERR_SYNTAX_IP6;
1774 	if(*len < LDNS_IP6ADDRLEN)
1775 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1776 	memmove(rd, address, LDNS_IP6ADDRLEN);
1777 	*len = LDNS_IP6ADDRLEN;
1778 	return LDNS_WIREPARSE_ERR_OK;
1779 #else
1780 	return LDNS_WIREPARSE_ERR_NOT_IMPL;
1781 #endif
1782 }
1783 
1784 int sldns_str2wire_str_buf(const char* str, uint8_t* rd, size_t* len)
1785 {
1786 	uint8_t ch = 0;
1787 	size_t sl = 0;
1788 	const char* s = str;
1789 	/* skip length byte */
1790 	if(*len < 1)
1791 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1792 
1793 	/* read characters */
1794 	while(sldns_parse_char(&ch, &s)) {
1795 		if(sl >= 255)
1796 			return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR, s-str);
1797 		if(*len < sl+2)
1798 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
1799 				s-str);
1800 		rd[++sl] = ch;
1801 	}
1802 	if(!s)
1803 		return LDNS_WIREPARSE_ERR_SYNTAX_BAD_ESCAPE;
1804 	rd[0] = (uint8_t)sl;
1805 	*len = sl+1;
1806 	return LDNS_WIREPARSE_ERR_OK;
1807 }
1808 
1809 int sldns_str2wire_apl_buf(const char* str, uint8_t* rd, size_t* len)
1810 {
1811 	const char *my_str = str;
1812 
1813 	char my_ip_str[64];
1814 	size_t ip_str_len;
1815 
1816 	uint16_t family;
1817 	int negation;
1818 	size_t adflength = 0;
1819 	uint8_t data[16+4];
1820 	uint8_t prefix;
1821 	size_t i;
1822 
1823 	if(*my_str == '\0') {
1824 		/* empty APL element, no data, no string */
1825 		*len = 0;
1826 		return LDNS_WIREPARSE_ERR_OK;
1827 	}
1828 
1829 	/* [!]afi:address/prefix */
1830 	if (strlen(my_str) < 2
1831 			|| strchr(my_str, ':') == NULL
1832 			|| strchr(my_str, '/') == NULL
1833 			|| strchr(my_str, ':') > strchr(my_str, '/')) {
1834 		return LDNS_WIREPARSE_ERR_INVALID_STR;
1835 	}
1836 
1837 	if (my_str[0] == '!') {
1838 		negation = 1;
1839 		my_str += 1;
1840 	} else {
1841 		negation = 0;
1842 	}
1843 
1844 	family = (uint16_t) atoi(my_str);
1845 
1846 	my_str = strchr(my_str, ':') + 1;
1847 
1848 	/* need ip addr and only ip addr for inet_pton */
1849 	ip_str_len = (size_t) (strchr(my_str, '/') - my_str);
1850 	if(ip_str_len+1 > sizeof(my_ip_str))
1851 		return LDNS_WIREPARSE_ERR_INVALID_STR;
1852 	(void)strlcpy(my_ip_str, my_str, sizeof(my_ip_str));
1853 	my_ip_str[ip_str_len] = 0;
1854 
1855 	if (family == 1) {
1856 		/* ipv4 */
1857 		if(inet_pton(AF_INET, my_ip_str, data+4) == 0)
1858 			return LDNS_WIREPARSE_ERR_INVALID_STR;
1859 		for (i = 0; i < 4; i++) {
1860 			if (data[i+4] != 0) {
1861 				adflength = i + 1;
1862 			}
1863 		}
1864 	} else if (family == 2) {
1865 		/* ipv6 */
1866 		if (inet_pton(AF_INET6, my_ip_str, data+4) == 0)
1867 			return LDNS_WIREPARSE_ERR_INVALID_STR;
1868 		for (i = 0; i < 16; i++) {
1869 			if (data[i+4] != 0) {
1870 				adflength = i + 1;
1871 			}
1872 		}
1873 	} else {
1874 		/* unknown family */
1875 		return LDNS_WIREPARSE_ERR_INVALID_STR;
1876 	}
1877 
1878 	my_str = strchr(my_str, '/') + 1;
1879 	prefix = (uint8_t) atoi(my_str);
1880 
1881 	sldns_write_uint16(data, family);
1882 	data[2] = prefix;
1883 	data[3] = (uint8_t)adflength;
1884 	if (negation) {
1885 		/* set bit 1 of byte 3 */
1886 		data[3] = data[3] | 0x80;
1887 	}
1888 
1889 	if(*len < 4+adflength)
1890 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1891 	memmove(rd, data, 4+adflength);
1892 	*len = 4+adflength;
1893 	return LDNS_WIREPARSE_ERR_OK;
1894 }
1895 
1896 int sldns_str2wire_b64_buf(const char* str, uint8_t* rd, size_t* len)
1897 {
1898 	size_t sz = sldns_b64_pton_calculate_size(strlen(str));
1899 	int n;
1900 	if(strcmp(str, "0") == 0) {
1901 		*len = 0;
1902 		return LDNS_WIREPARSE_ERR_OK;
1903 	}
1904 	if(*len < sz)
1905 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1906 	n = sldns_b64_pton(str, rd, *len);
1907 	if(n < 0)
1908 		return LDNS_WIREPARSE_ERR_SYNTAX_B64;
1909 	*len = (size_t)n;
1910 	return LDNS_WIREPARSE_ERR_OK;
1911 }
1912 
1913 int sldns_str2wire_b32_ext_buf(const char* str, uint8_t* rd, size_t* len)
1914 {
1915 	size_t slen = strlen(str);
1916 	size_t sz = sldns_b32_pton_calculate_size(slen);
1917 	int n;
1918 	if(*len < 1+sz)
1919 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1920 	rd[0] = (uint8_t)sz;
1921 	n = sldns_b32_pton_extended_hex(str, slen, rd+1, *len-1);
1922 	if(n < 0)
1923 		return LDNS_WIREPARSE_ERR_SYNTAX_B32_EXT;
1924 	*len = (size_t)n+1;
1925 	return LDNS_WIREPARSE_ERR_OK;
1926 }
1927 
1928 /** see if the string ends, or ends in whitespace */
1929 static int
1930 sldns_is_last_of_string(const char* str)
1931 {
1932 	if(*str == 0) return 1;
1933 	while(isspace((unsigned char)*str))
1934 		str++;
1935 	if(*str == 0) return 1;
1936 	return 0;
1937 }
1938 
1939 int sldns_str2wire_hex_buf(const char* str, uint8_t* rd, size_t* len)
1940 {
1941 	const char* s = str;
1942 	size_t dlen = 0; /* number of hexdigits parsed */
1943 	while(*s) {
1944 		if(isspace((unsigned char)*s)) {
1945 			s++;
1946 			continue;
1947 		}
1948 		if(dlen == 0 && *s == '0' && sldns_is_last_of_string(s+1)) {
1949 			*len = 0;
1950 			return LDNS_WIREPARSE_ERR_OK;
1951 		}
1952 		if(!isxdigit((unsigned char)*s))
1953 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str);
1954 		if(*len < dlen/2 + 1)
1955 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
1956 				s-str);
1957 		if((dlen&1)==0)
1958 			rd[dlen/2] = (uint8_t)sldns_hexdigit_to_int(*s++) * 16;
1959 		else	rd[dlen/2] += (uint8_t)sldns_hexdigit_to_int(*s++);
1960 		dlen++;
1961 	}
1962 	if((dlen&1)!=0)
1963 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str);
1964 	*len = dlen/2;
1965 	return LDNS_WIREPARSE_ERR_OK;
1966 }
1967 
1968 int sldns_str2wire_nsec_buf(const char* str, uint8_t* rd, size_t* len)
1969 {
1970 	const char *delim = "\n\t ";
1971 	char token[64]; /* for a type name */
1972 	size_t type_count = 0;
1973 	int block;
1974 	size_t used = 0;
1975 	uint16_t maxtype = 0;
1976 	uint8_t typebits[8192]; /* 65536 bits */
1977 	uint8_t window_in_use[256];
1978 
1979 	/* string in buffer */
1980 	sldns_buffer strbuf;
1981 	sldns_buffer_init_frm_data(&strbuf, (uint8_t*)str, strlen(str));
1982 
1983 	/* parse the types */
1984 	memset(typebits, 0, sizeof(typebits));
1985 	memset(window_in_use, 0, sizeof(window_in_use));
1986 	while(sldns_buffer_remaining(&strbuf) > 0 &&
1987 		sldns_bget_token(&strbuf, token, delim, sizeof(token)) != -1) {
1988 		uint16_t t = sldns_get_rr_type_by_name(token);
1989 		if(token[0] == 0)
1990 			continue;
1991 		if(t == 0 && strcmp(token, "TYPE0") != 0)
1992 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TYPE,
1993 				sldns_buffer_position(&strbuf));
1994 		typebits[t/8] |= (0x80>>(t%8));
1995 		window_in_use[t/256] = 1;
1996 		type_count++;
1997 		if(t > maxtype) maxtype = t;
1998 	}
1999 
2000 	/* empty NSEC bitmap */
2001 	if(type_count == 0) {
2002 		*len = 0;
2003 		return LDNS_WIREPARSE_ERR_OK;
2004 	}
2005 
2006 	/* encode windows {u8 windowblock, u8 bitmaplength, 0-32u8 bitmap},
2007 	 * block is 0-255 upper octet of types, length if 0-32. */
2008 	for(block = 0; block <= (int)maxtype/256; block++) {
2009 		int i, blocklen = 0;
2010 		if(!window_in_use[block])
2011 			continue;
2012 		for(i=0; i<32; i++) {
2013 			if(typebits[block*32+i] != 0)
2014 				blocklen = i+1;
2015 		}
2016 		if(blocklen == 0)
2017 			continue; /* empty window should have been !in_use */
2018 		if(used+blocklen+2 > *len)
2019 			return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2020 		rd[used+0] = (uint8_t)block;
2021 		rd[used+1] = (uint8_t)blocklen;
2022 		for(i=0; i<blocklen; i++) {
2023 			rd[used+2+i] = typebits[block*32+i];
2024 		}
2025 		used += blocklen+2;
2026 	}
2027 	*len = used;
2028 	return LDNS_WIREPARSE_ERR_OK;
2029 }
2030 
2031 int sldns_str2wire_type_buf(const char* str, uint8_t* rd, size_t* len)
2032 {
2033 	uint16_t t = sldns_get_rr_type_by_name(str);
2034 	if(t == 0 && strcmp(str, "TYPE0") != 0)
2035 		return LDNS_WIREPARSE_ERR_SYNTAX_TYPE;
2036 	if(*len < 2)
2037 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2038 	sldns_write_uint16(rd, t);
2039 	*len = 2;
2040 	return LDNS_WIREPARSE_ERR_OK;
2041 }
2042 
2043 int sldns_str2wire_class_buf(const char* str, uint8_t* rd, size_t* len)
2044 {
2045 	uint16_t c = sldns_get_rr_class_by_name(str);
2046 	if(c == 0 && strcmp(str, "CLASS0") != 0)
2047 		return LDNS_WIREPARSE_ERR_SYNTAX_CLASS;
2048 	if(*len < 2)
2049 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2050 	sldns_write_uint16(rd, c);
2051 	*len = 2;
2052 	return LDNS_WIREPARSE_ERR_OK;
2053 }
2054 
2055 /* An certificate alg field can either be specified as a 8 bits number
2056  * or by its symbolic name. Handle both */
2057 int sldns_str2wire_cert_alg_buf(const char* str, uint8_t* rd, size_t* len)
2058 {
2059 	sldns_lookup_table *lt = sldns_lookup_by_name(sldns_cert_algorithms,
2060 		str);
2061 	if(*len < 2)
2062 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2063 	if(lt) {
2064 		sldns_write_uint16(rd, (uint16_t)lt->id);
2065 	} else {
2066 		int s = sldns_str2wire_int16_buf(str, rd, len);
2067 		if(s) return s;
2068 		if(sldns_read_uint16(rd) == 0)
2069 			return LDNS_WIREPARSE_ERR_CERT_BAD_ALGORITHM;
2070 	}
2071 	*len = 2;
2072 	return LDNS_WIREPARSE_ERR_OK;
2073 }
2074 
2075 /* An alg field can either be specified as a 8 bits number
2076  * or by its symbolic name. Handle both */
2077 int sldns_str2wire_alg_buf(const char* str, uint8_t* rd, size_t* len)
2078 {
2079 	sldns_lookup_table *lt = sldns_lookup_by_name(sldns_algorithms, str);
2080 	if(*len < 1)
2081 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2082 	if(lt) {
2083 		rd[0] = (uint8_t)lt->id;
2084 		*len = 1;
2085 	} else {
2086 		/* try as-is (a number) */
2087 		return sldns_str2wire_int8_buf(str, rd, len);
2088 	}
2089 	return LDNS_WIREPARSE_ERR_OK;
2090 }
2091 
2092 int sldns_str2wire_tsigerror_buf(const char* str, uint8_t* rd, size_t* len)
2093 {
2094 	sldns_lookup_table *lt = sldns_lookup_by_name(sldns_tsig_errors, str);
2095 	if(*len < 2)
2096 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2097 	if(lt) {
2098 		sldns_write_uint16(rd, (uint16_t)lt->id);
2099 		*len = 2;
2100 	} else {
2101 		/* try as-is (a number) */
2102 		return sldns_str2wire_int16_buf(str, rd, len);
2103 	}
2104 	return LDNS_WIREPARSE_ERR_OK;
2105 }
2106 
2107 int sldns_str2wire_time_buf(const char* str, uint8_t* rd, size_t* len)
2108 {
2109 	/* convert a time YYYYDDMMHHMMSS to wireformat */
2110 	struct tm tm;
2111 	if(*len < 4)
2112 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2113 
2114 	/* Try to scan the time... */
2115 	memset(&tm, 0, sizeof(tm));
2116 	if (strlen(str) == 14 && sscanf(str, "%4d%2d%2d%2d%2d%2d",
2117 		&tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour,
2118 		&tm.tm_min, &tm.tm_sec) == 6) {
2119 	   	tm.tm_year -= 1900;
2120 	   	tm.tm_mon--;
2121 	   	/* Check values */
2122 		if (tm.tm_year < 70)
2123 			return LDNS_WIREPARSE_ERR_SYNTAX_TIME;
2124 		if (tm.tm_mon < 0 || tm.tm_mon > 11)
2125 			return LDNS_WIREPARSE_ERR_SYNTAX_TIME;
2126 		if (tm.tm_mday < 1 || tm.tm_mday > 31)
2127 			return LDNS_WIREPARSE_ERR_SYNTAX_TIME;
2128 		if (tm.tm_hour < 0 || tm.tm_hour > 23)
2129 			return LDNS_WIREPARSE_ERR_SYNTAX_TIME;
2130 		if (tm.tm_min < 0 || tm.tm_min > 59)
2131 			return LDNS_WIREPARSE_ERR_SYNTAX_TIME;
2132 		if (tm.tm_sec < 0 || tm.tm_sec > 59)
2133 			return LDNS_WIREPARSE_ERR_SYNTAX_TIME;
2134 
2135 		sldns_write_uint32(rd, (uint32_t)sldns_mktime_from_utc(&tm));
2136 	} else {
2137 		/* handle it as 32 bits timestamp */
2138 		char *end;
2139 		uint32_t l = (uint32_t)strtol((char*)str, &end, 10);
2140 		if(*end != 0)
2141 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TIME,
2142 				end-(char*)str);
2143 		sldns_write_uint32(rd, l);
2144 	}
2145 	*len = 4;
2146 	return LDNS_WIREPARSE_ERR_OK;
2147 }
2148 
2149 int sldns_str2wire_tsigtime_buf(const char* str, uint8_t* rd, size_t* len)
2150 {
2151 	char* end;
2152 	uint64_t t = (uint64_t)strtol((char*)str, &end, 10);
2153 	uint16_t high;
2154 	uint32_t low;
2155 	if(*end != 0)
2156 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TIME, end-str);
2157 	if(*len < 6)
2158 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2159 	high = (uint16_t)(t>>32);
2160 	low = (uint32_t)(t);
2161 	sldns_write_uint16(rd, high);
2162 	sldns_write_uint32(rd+2, low);
2163 	*len = 6;
2164 	return LDNS_WIREPARSE_ERR_OK;
2165 }
2166 
2167 int sldns_str2wire_period_buf(const char* str, uint8_t* rd, size_t* len)
2168 {
2169 	const char* end;
2170 	int overflow;
2171 	uint32_t p = sldns_str2period(str, &end, &overflow);
2172 	if(*end != 0)
2173 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_PERIOD, end-str);
2174 	if(overflow)
2175 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_INTEGER_OVERFLOW,
2176 			end-str);
2177 	if(*len < 4)
2178 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2179 	sldns_write_uint32(rd, p);
2180 	*len = 4;
2181 	return LDNS_WIREPARSE_ERR_OK;
2182 }
2183 
2184 /** read "<digits>[.<digits>][mM]" into mantissa exponent format for LOC type */
2185 static int
2186 loc_parse_cm(char* my_str, char** endstr, uint8_t* m, uint8_t* e)
2187 {
2188 	uint32_t meters = 0, cm = 0, val;
2189 	char* cm_endstr;
2190 	while (isblank((unsigned char)*my_str)) {
2191 		my_str++;
2192 	}
2193 	meters = (uint32_t)strtol(my_str, &my_str, 10);
2194 	if (*my_str == '.') {
2195 		my_str++;
2196 		cm = (uint32_t)strtol(my_str, &cm_endstr, 10);
2197 		if(cm_endstr == my_str + 1)
2198 			cm *= 10;
2199 		my_str = cm_endstr;
2200 	}
2201 	if (meters >= 1) {
2202 		*e = 2;
2203 		val = meters;
2204 	} else	{
2205 		*e = 0;
2206 		val = cm;
2207 	}
2208 	while(val >= 10) {
2209 		(*e)++;
2210 		val /= 10;
2211 	}
2212 	*m = (uint8_t)val;
2213 
2214 	if (*e > 9)
2215 		return 0;
2216 	if (*my_str == 'm' || *my_str == 'M') {
2217 		my_str++;
2218 	}
2219 	*endstr = my_str;
2220 	return 1;
2221 }
2222 
2223 int sldns_str2wire_loc_buf(const char* str, uint8_t* rd, size_t* len)
2224 {
2225 	uint32_t latitude = 0;
2226 	uint32_t longitude = 0;
2227 	uint32_t altitude = 0;
2228 
2229 	uint32_t equator = (uint32_t)1<<31; /* 2**31 */
2230 
2231 	/* only support version 0 */
2232 	uint32_t h = 0;
2233 	uint32_t m = 0;
2234 	uint8_t size_b = 1, size_e = 2;
2235 	uint8_t horiz_pre_b = 1, horiz_pre_e = 6;
2236 	uint8_t vert_pre_b = 1, vert_pre_e = 3;
2237 
2238 	double s = 0.0;
2239 	int northerness;
2240 	int easterness;
2241 
2242 	char *my_str = (char *) str;
2243 
2244 	if (isdigit((unsigned char) *my_str)) {
2245 		h = (uint32_t) strtol(my_str, &my_str, 10);
2246 	} else {
2247 		return LDNS_WIREPARSE_ERR_INVALID_STR;
2248 	}
2249 
2250 	while (isblank((unsigned char) *my_str)) {
2251 		my_str++;
2252 	}
2253 
2254 	if (isdigit((unsigned char) *my_str)) {
2255 		m = (uint32_t) strtol(my_str, &my_str, 10);
2256 	} else if (*my_str == 'N' || *my_str == 'S') {
2257 		goto north;
2258 	} else {
2259 		return LDNS_WIREPARSE_ERR_INVALID_STR;
2260 	}
2261 
2262 	while (isblank((unsigned char) *my_str)) {
2263 		my_str++;
2264 	}
2265 
2266 	if (isdigit((unsigned char) *my_str)) {
2267 		s = strtod(my_str, &my_str);
2268 	}
2269 
2270 	/* skip blanks before northerness */
2271 	while (isblank((unsigned char) *my_str)) {
2272 		my_str++;
2273 	}
2274 
2275 north:
2276 	if (*my_str == 'N') {
2277 		northerness = 1;
2278 	} else if (*my_str == 'S') {
2279 		northerness = 0;
2280 	} else {
2281 		return LDNS_WIREPARSE_ERR_INVALID_STR;
2282 	}
2283 
2284 	my_str++;
2285 
2286 	/* store number */
2287 	s = 1000.0 * s;
2288 	/* add a little to make floor in conversion a round */
2289 	s += 0.0005;
2290 	latitude = (uint32_t) s;
2291 	latitude += 1000 * 60 * m;
2292 	latitude += 1000 * 60 * 60 * h;
2293 	if (northerness) {
2294 		latitude = equator + latitude;
2295 	} else {
2296 		latitude = equator - latitude;
2297 	}
2298 	while (isblank((unsigned char)*my_str)) {
2299 		my_str++;
2300 	}
2301 
2302 	if (isdigit((unsigned char) *my_str)) {
2303 		h = (uint32_t) strtol(my_str, &my_str, 10);
2304 	} else {
2305 		return LDNS_WIREPARSE_ERR_INVALID_STR;
2306 	}
2307 
2308 	while (isblank((unsigned char) *my_str)) {
2309 		my_str++;
2310 	}
2311 
2312 	if (isdigit((unsigned char) *my_str)) {
2313 		m = (uint32_t) strtol(my_str, &my_str, 10);
2314 	} else if (*my_str == 'E' || *my_str == 'W') {
2315 		goto east;
2316 	} else {
2317 		return LDNS_WIREPARSE_ERR_INVALID_STR;
2318 	}
2319 
2320 	while (isblank((unsigned char)*my_str)) {
2321 		my_str++;
2322 	}
2323 
2324 	if (isdigit((unsigned char) *my_str)) {
2325 		s = strtod(my_str, &my_str);
2326 	}
2327 
2328 	/* skip blanks before easterness */
2329 	while (isblank((unsigned char)*my_str)) {
2330 		my_str++;
2331 	}
2332 
2333 east:
2334 	if (*my_str == 'E') {
2335 		easterness = 1;
2336 	} else if (*my_str == 'W') {
2337 		easterness = 0;
2338 	} else {
2339 		return LDNS_WIREPARSE_ERR_INVALID_STR;
2340 	}
2341 
2342 	my_str++;
2343 
2344 	/* store number */
2345 	s *= 1000.0;
2346 	/* add a little to make floor in conversion a round */
2347 	s += 0.0005;
2348 	longitude = (uint32_t) s;
2349 	longitude += 1000 * 60 * m;
2350 	longitude += 1000 * 60 * 60 * h;
2351 
2352 	if (easterness) {
2353 		longitude += equator;
2354 	} else {
2355 		longitude = equator - longitude;
2356 	}
2357 
2358 	altitude = (uint32_t)(strtod(my_str, &my_str)*100.0 +
2359 		10000000.0 + 0.5);
2360 	if (*my_str == 'm' || *my_str == 'M') {
2361 		my_str++;
2362 	}
2363 
2364 	if (strlen(my_str) > 0) {
2365 		if(!loc_parse_cm(my_str, &my_str, &size_b, &size_e))
2366 			return LDNS_WIREPARSE_ERR_INVALID_STR;
2367 	}
2368 
2369 	if (strlen(my_str) > 0) {
2370 		if(!loc_parse_cm(my_str, &my_str, &horiz_pre_b, &horiz_pre_e))
2371 			return LDNS_WIREPARSE_ERR_INVALID_STR;
2372 	}
2373 
2374 	if (strlen(my_str) > 0) {
2375 		if(!loc_parse_cm(my_str, &my_str, &vert_pre_b, &vert_pre_e))
2376 			return LDNS_WIREPARSE_ERR_INVALID_STR;
2377 	}
2378 
2379 	if(*len < 16)
2380 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2381 	rd[0] = 0;
2382 	rd[1] = ((size_b << 4) & 0xf0) | (size_e & 0x0f);
2383 	rd[2] = ((horiz_pre_b << 4) & 0xf0) | (horiz_pre_e & 0x0f);
2384 	rd[3] = ((vert_pre_b << 4) & 0xf0) | (vert_pre_e & 0x0f);
2385 	sldns_write_uint32(rd + 4, latitude);
2386 	sldns_write_uint32(rd + 8, longitude);
2387 	sldns_write_uint32(rd + 12, altitude);
2388 	*len = 16;
2389 	return LDNS_WIREPARSE_ERR_OK;
2390 }
2391 
2392 static void
2393 ldns_tolower_str(char* s)
2394 {
2395 	if(s) {
2396 		while(*s) {
2397 			*s = (char)tolower((unsigned char)*s);
2398 			s++;
2399 		}
2400 	}
2401 }
2402 
2403 int sldns_str2wire_wks_buf(const char* str, uint8_t* rd, size_t* len)
2404 {
2405 	int rd_len = 1;
2406 	int have_proto = 0;
2407 	char token[50], proto_str[50];
2408 	sldns_buffer strbuf;
2409 	sldns_buffer_init_frm_data(&strbuf, (uint8_t*)str, strlen(str));
2410 	proto_str[0]=0;
2411 
2412 	/* check we have one byte for proto */
2413 	if(*len < 1)
2414 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2415 
2416 	while(sldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) > 0) {
2417 		ldns_tolower_str(token);
2418 		if(!have_proto) {
2419 			struct protoent *p = getprotobyname(token);
2420 			have_proto = 1;
2421 			if(p) rd[0] = (uint8_t)p->p_proto;
2422 			else if(strcasecmp(token, "tcp")==0) rd[0]=6;
2423 			else if(strcasecmp(token, "udp")==0) rd[0]=17;
2424 			else rd[0] = (uint8_t)atoi(token);
2425 			(void)strlcpy(proto_str, token, sizeof(proto_str));
2426 		} else {
2427 			int serv_port;
2428 			struct servent *serv = getservbyname(token, proto_str);
2429 			if(serv) serv_port=(int)ntohs((uint16_t)serv->s_port);
2430 			else if(strcasecmp(token, "domain")==0) serv_port=53;
2431 			else {
2432 				serv_port = atoi(token);
2433 				if(serv_port == 0 && strcmp(token, "0") != 0) {
2434 #ifdef HAVE_ENDSERVENT
2435 					endservent();
2436 #endif
2437 #ifdef HAVE_ENDPROTOENT
2438 					endprotoent();
2439 #endif
2440 					return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX,
2441 						sldns_buffer_position(&strbuf));
2442 				}
2443 				if(serv_port < 0 || serv_port > 65535) {
2444 #ifdef HAVE_ENDSERVENT
2445 					endservent();
2446 #endif
2447 #ifdef HAVE_ENDPROTOENT
2448 					endprotoent();
2449 #endif
2450 					return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX,
2451 						sldns_buffer_position(&strbuf));
2452 				}
2453 			}
2454 			if(rd_len < 1+serv_port/8+1) {
2455 				/* bitmap is larger, init new bytes at 0 */
2456 				if(*len < 1+(size_t)serv_port/8+1) {
2457 #ifdef HAVE_ENDSERVENT
2458 					endservent();
2459 #endif
2460 #ifdef HAVE_ENDPROTOENT
2461 					endprotoent();
2462 #endif
2463 					return RET_ERR(
2464 					LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
2465 					sldns_buffer_position(&strbuf));
2466 				}
2467 				memset(rd+rd_len, 0, 1+(size_t)serv_port/8+1-rd_len);
2468 				rd_len = 1+serv_port/8+1;
2469 			}
2470 			rd[1+ serv_port/8] |= (1 << (7 - serv_port % 8));
2471 		}
2472 	}
2473 	*len = (size_t)rd_len;
2474 
2475 #ifdef HAVE_ENDSERVENT
2476 	endservent();
2477 #endif
2478 #ifdef HAVE_ENDPROTOENT
2479 	endprotoent();
2480 #endif
2481 	return LDNS_WIREPARSE_ERR_OK;
2482 }
2483 
2484 int sldns_str2wire_nsap_buf(const char* str, uint8_t* rd, size_t* len)
2485 {
2486 	const char* s = str;
2487 	size_t slen;
2488 	size_t dlen = 0; /* number of hexdigits parsed */
2489 
2490 	/* just a hex string with optional dots? */
2491 	if (s[0] != '0' || s[1] != 'x')
2492 		return LDNS_WIREPARSE_ERR_INVALID_STR;
2493 	s += 2;
2494 	slen = strlen(s);
2495 	if(slen > LDNS_MAX_RDFLEN*2)
2496 		return LDNS_WIREPARSE_ERR_LABEL_OVERFLOW;
2497 	while(*s) {
2498 		if(isspace((unsigned char)*s) || *s == '.') {
2499 			s++;
2500 			continue;
2501 		}
2502 		if(!isxdigit((unsigned char)*s))
2503 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str);
2504 		if(*len < dlen/2 + 1)
2505 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
2506 				s-str);
2507 		if((dlen&1)==0)
2508 			rd[dlen/2] = (uint8_t)sldns_hexdigit_to_int(*s++) * 16;
2509 		else	rd[dlen/2] += sldns_hexdigit_to_int(*s++);
2510 		dlen++;
2511 	}
2512 	if((dlen&1)!=0)
2513 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str);
2514 	*len = dlen/2;
2515 	return LDNS_WIREPARSE_ERR_OK;
2516 }
2517 
2518 int sldns_str2wire_atma_buf(const char* str, uint8_t* rd, size_t* len)
2519 {
2520 	const char* s = str;
2521 	size_t slen = strlen(str);
2522 	size_t dlen = 0; /* number of hexdigits parsed */
2523 
2524 	/* just a hex string with optional dots? */
2525 	/* notimpl e.164 format */
2526 	if(slen > LDNS_MAX_RDFLEN*2)
2527 		return LDNS_WIREPARSE_ERR_LABEL_OVERFLOW;
2528 	while(*s) {
2529 		if(isspace((unsigned char)*s) || *s == '.') {
2530 			s++;
2531 			continue;
2532 		}
2533 		if(!isxdigit((unsigned char)*s))
2534 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str);
2535 		if(*len < dlen/2 + 1)
2536 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
2537 				s-str);
2538 		if((dlen&1)==0)
2539 			rd[dlen/2] = (uint8_t)sldns_hexdigit_to_int(*s++) * 16;
2540 		else	rd[dlen/2] += sldns_hexdigit_to_int(*s++);
2541 		dlen++;
2542 	}
2543 	if((dlen&1)!=0)
2544 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str);
2545 	*len = dlen/2;
2546 	return LDNS_WIREPARSE_ERR_OK;
2547 }
2548 
2549 int sldns_str2wire_ipseckey_buf(const char* str, uint8_t* rd, size_t* len)
2550 {
2551 	size_t gwlen = 0, keylen = 0;
2552 	int s;
2553 	uint8_t gwtype;
2554 	char token[512];
2555 	sldns_buffer strbuf;
2556 	sldns_buffer_init_frm_data(&strbuf, (uint8_t*)str, strlen(str));
2557 
2558 	if(*len < 3)
2559 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2560 	/* precedence */
2561 	if(sldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) <= 0)
2562 		return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR,
2563 			sldns_buffer_position(&strbuf));
2564 	rd[0] = (uint8_t)atoi(token);
2565 	/* gateway_type */
2566 	if(sldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) <= 0)
2567 		return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR,
2568 			sldns_buffer_position(&strbuf));
2569 	rd[1] = (uint8_t)atoi(token);
2570 	gwtype = rd[1];
2571 	/* algorithm */
2572 	if(sldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) <= 0)
2573 		return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR,
2574 			sldns_buffer_position(&strbuf));
2575 	rd[2] = (uint8_t)atoi(token);
2576 
2577 	/* gateway */
2578 	if(sldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) <= 0)
2579 		return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR,
2580 			sldns_buffer_position(&strbuf));
2581 	if(gwtype == 0) {
2582 		/* NOGATEWAY */
2583 		if(strcmp(token, ".") != 0)
2584 			return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR,
2585 				sldns_buffer_position(&strbuf));
2586 		gwlen = 0;
2587 	} else if(gwtype == 1) {
2588 		/* IP4 */
2589 		gwlen = *len - 3;
2590 		s = sldns_str2wire_a_buf(token, rd+3, &gwlen);
2591 		if(s) return RET_ERR_SHIFT(s, sldns_buffer_position(&strbuf));
2592 	} else if(gwtype == 2) {
2593 		/* IP6 */
2594 		gwlen = *len - 3;
2595 		s = sldns_str2wire_aaaa_buf(token, rd+3, &gwlen);
2596 		if(s) return RET_ERR_SHIFT(s, sldns_buffer_position(&strbuf));
2597 	} else if(gwtype == 3) {
2598 		/* DNAME */
2599 		gwlen = *len - 3;
2600 		s = sldns_str2wire_dname_buf(token, rd+3, &gwlen);
2601 		if(s) return RET_ERR_SHIFT(s, sldns_buffer_position(&strbuf));
2602 	} else {
2603 		/* unknown gateway type */
2604 		return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR,
2605 			sldns_buffer_position(&strbuf));
2606 	}
2607 	/* double check for size */
2608 	if(*len < 3 + gwlen)
2609 		return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
2610 			sldns_buffer_position(&strbuf));
2611 
2612 	/* publickey in remainder of strbuf */
2613 	keylen = *len - 3 - gwlen;
2614 	s = sldns_str2wire_b64_buf((const char*)sldns_buffer_current(&strbuf),
2615 		rd+3+gwlen, &keylen);
2616 	if(s) return RET_ERR_SHIFT(s, sldns_buffer_position(&strbuf));
2617 
2618 	*len = 3 + gwlen + keylen;
2619 	return LDNS_WIREPARSE_ERR_OK;
2620 }
2621 
2622 int sldns_str2wire_nsec3_salt_buf(const char* str, uint8_t* rd, size_t* len)
2623 {
2624 	int i, salt_length_str = (int)strlen(str);
2625 	if (salt_length_str == 1 && str[0] == '-') {
2626 		salt_length_str = 0;
2627 	} else if (salt_length_str % 2 != 0) {
2628 		return LDNS_WIREPARSE_ERR_SYNTAX_HEX;
2629 	}
2630 	if (salt_length_str > 512)
2631 		return LDNS_WIREPARSE_ERR_SYNTAX_HEX;
2632 	if(*len < 1+(size_t)salt_length_str / 2)
2633 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2634 	rd[0] = (uint8_t) (salt_length_str / 2);
2635 	for (i = 0; i < salt_length_str; i += 2) {
2636 		if (isxdigit((unsigned char)str[i]) &&
2637 			isxdigit((unsigned char)str[i+1])) {
2638 			rd[1+i/2] = (uint8_t)(sldns_hexdigit_to_int(str[i])*16
2639 				+ sldns_hexdigit_to_int(str[i+1]));
2640 		} else {
2641 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_HEX, i);
2642 		}
2643 	}
2644 	*len = 1 + (size_t)rd[0];
2645 	return LDNS_WIREPARSE_ERR_OK;
2646 }
2647 
2648 int sldns_str2wire_ilnp64_buf(const char* str, uint8_t* rd, size_t* len)
2649 {
2650 	unsigned int a, b, c, d;
2651 	uint16_t shorts[4];
2652 	int l;
2653 	if(*len < sizeof(shorts))
2654 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2655 
2656 	if (sscanf(str, "%4x:%4x:%4x:%4x%n", &a, &b, &c, &d, &l) != 4 ||
2657 			l != (int)strlen(str) || /* more data to read */
2658 			strpbrk(str, "+-")       /* signed hexes */
2659 			)
2660 		return LDNS_WIREPARSE_ERR_SYNTAX_ILNP64;
2661 	shorts[0] = htons(a);
2662 	shorts[1] = htons(b);
2663 	shorts[2] = htons(c);
2664 	shorts[3] = htons(d);
2665 	memmove(rd, &shorts, sizeof(shorts));
2666 	*len = sizeof(shorts);
2667 	return LDNS_WIREPARSE_ERR_OK;
2668 }
2669 
2670 int sldns_str2wire_eui48_buf(const char* str, uint8_t* rd, size_t* len)
2671 {
2672 	unsigned int a, b, c, d, e, f;
2673 	int l;
2674 
2675 	if(*len < 6)
2676 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2677 	if (sscanf(str, "%2x-%2x-%2x-%2x-%2x-%2x%n",
2678 			&a, &b, &c, &d, &e, &f, &l) != 6 ||
2679 			l != (int)strlen(str))
2680 		return LDNS_WIREPARSE_ERR_SYNTAX_EUI48;
2681 	rd[0] = a;
2682 	rd[1] = b;
2683 	rd[2] = c;
2684 	rd[3] = d;
2685 	rd[4] = e;
2686 	rd[5] = f;
2687 	*len = 6;
2688 	return LDNS_WIREPARSE_ERR_OK;
2689 }
2690 
2691 int sldns_str2wire_eui64_buf(const char* str, uint8_t* rd, size_t* len)
2692 {
2693 	unsigned int a, b, c, d, e, f, g, h;
2694 	int l;
2695 
2696 	if(*len < 8)
2697 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2698 	if (sscanf(str, "%2x-%2x-%2x-%2x-%2x-%2x-%2x-%2x%n",
2699 			&a, &b, &c, &d, &e, &f, &g, &h, &l) != 8 ||
2700 			l != (int)strlen(str))
2701 		return LDNS_WIREPARSE_ERR_SYNTAX_EUI64;
2702 	rd[0] = a;
2703 	rd[1] = b;
2704 	rd[2] = c;
2705 	rd[3] = d;
2706 	rd[4] = e;
2707 	rd[5] = f;
2708 	rd[6] = g;
2709 	rd[7] = h;
2710 	*len = 8;
2711 	return LDNS_WIREPARSE_ERR_OK;
2712 }
2713 
2714 int sldns_str2wire_tag_buf(const char* str, uint8_t* rd, size_t* len)
2715 {
2716 	size_t slen = strlen(str);
2717 	const char* ptr;
2718 
2719 	if (slen > 255)
2720 		return LDNS_WIREPARSE_ERR_SYNTAX_TAG;
2721 	if(*len < slen+1)
2722 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2723 	for (ptr = str; *ptr; ptr++) {
2724 		if(!isalnum((unsigned char)*ptr))
2725 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TAG, ptr-str);
2726 	}
2727 	rd[0] = (uint8_t)slen;
2728 	memmove(rd+1, str, slen);
2729 	*len = slen+1;
2730 	return LDNS_WIREPARSE_ERR_OK;
2731 }
2732 
2733 int sldns_str2wire_long_str_buf(const char* str, uint8_t* rd, size_t* len)
2734 {
2735 	uint8_t ch = 0;
2736 	const char* pstr = str;
2737 	size_t length = 0;
2738 
2739 	/* Fill data with parsed bytes */
2740 	while (sldns_parse_char(&ch, &pstr)) {
2741 		if(*len < length+1)
2742 			return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2743 		rd[length++] = ch;
2744 	}
2745 	if(!pstr)
2746 		return LDNS_WIREPARSE_ERR_SYNTAX_BAD_ESCAPE;
2747 	*len = length;
2748 	return LDNS_WIREPARSE_ERR_OK;
2749 }
2750 
2751 int sldns_str2wire_hip_buf(const char* str, uint8_t* rd, size_t* len)
2752 {
2753 	char* s, *end;
2754 	int e;
2755 	size_t hitlen, pklen = 0;
2756 	/* presentation format:
2757 	 * 	pk-algo HIThex pubkeybase64
2758 	 * wireformat:
2759 	 * 	hitlen[1byte] pkalgo[1byte] pubkeylen[2byte] [hit] [pubkey] */
2760 	if(*len < 4)
2761 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2762 
2763 	/* read PK algorithm */
2764 	rd[1] = (uint8_t)strtol((char*)str, &s, 10);
2765 	if(*s != ' ')
2766 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_INT, s-(char*)str);
2767 	s++;
2768 	while(*s == ' ')
2769 		s++;
2770 
2771 	/* read HIT hex tag */
2772 	/* zero terminate the tag (replace later) */
2773 	end = strchr(s, ' ');
2774 	if(!end) return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX, s-(char*)str);
2775 	*end = 0;
2776 	hitlen = *len - 4;
2777 	if((e = sldns_str2wire_hex_buf(s, rd+4, &hitlen)) != 0) {
2778 		*end = ' ';
2779 		return RET_ERR_SHIFT(e, s-(char*)str);
2780 	}
2781 	if(hitlen > 255) {
2782 		*end = ' ';
2783 		return RET_ERR(LDNS_WIREPARSE_ERR_LABEL_OVERFLOW, s-(char*)str+255*2);
2784 	}
2785 	rd[0] = (uint8_t)hitlen;
2786 	*end = ' ';
2787 	s = end+1;
2788 
2789 	/* read pubkey base64 sequence */
2790 	pklen = *len - 4 - hitlen;
2791 	if((e = sldns_str2wire_b64_buf(s, rd+4+hitlen, &pklen)) != 0)
2792 		return RET_ERR_SHIFT(e, s-(char*)str);
2793 	if(pklen > 65535)
2794 		return RET_ERR(LDNS_WIREPARSE_ERR_LABEL_OVERFLOW, s-(char*)str+65535);
2795 	sldns_write_uint16(rd+2, (uint16_t)pklen);
2796 
2797 	*len = 4 + hitlen + pklen;
2798 	return LDNS_WIREPARSE_ERR_OK;
2799 }
2800 
2801 int sldns_str2wire_int16_data_buf(const char* str, uint8_t* rd, size_t* len)
2802 {
2803 	char* s;
2804 	int n;
2805 	n = strtol(str, &s, 10);
2806 	if(n < 0) /* negative number not allowed */
2807 		return LDNS_WIREPARSE_ERR_SYNTAX;
2808 	if(*len < ((size_t)n)+2)
2809 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2810 	if(n > 65535)
2811 		return LDNS_WIREPARSE_ERR_LABEL_OVERFLOW;
2812 
2813 	if(n == 0) {
2814 		sldns_write_uint16(rd, 0);
2815 		*len = 2;
2816 		return LDNS_WIREPARSE_ERR_OK;
2817 	}
2818 	if(*s != ' ')
2819 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_INT, s-(char*)str);
2820 	s++;
2821 	while(*s == ' ')
2822 		s++;
2823 
2824 	n = sldns_b64_pton(s, rd+2, (*len)-2);
2825 	if(n < 0)
2826 		return LDNS_WIREPARSE_ERR_SYNTAX_B64;
2827 	sldns_write_uint16(rd, (uint16_t)n);
2828 	*len = ((size_t)n)+2;
2829 	return LDNS_WIREPARSE_ERR_OK;
2830 }
2831