parse.c 8.58 KB
Newer Older
1
/*
2
 * a generic (simple) parser. Use to parse rr's, private key
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 * information and /etc/resolv.conf files
 *
 * a Net::DNS like library for C
 * LibDNS Team @ NLnet Labs
 * (c) NLnet Labs, 2005-2006
 * See the file LICENSE for the license
 */
#include <ldns/config.h>
#include <ldns/ldns.h>

#include <limits.h>
#include <strings.h>

ldns_lookup_table ldns_directive_types[] = {
17 18 19
        { LDNS_DIR_TTL, "$TTL" },
        { LDNS_DIR_ORIGIN, "$ORIGIN" },
        { LDNS_DIR_INCLUDE, "$INCLUDE" },
20 21 22 23 24 25
        { 0, NULL }
};

/* add max_limit here? */
ssize_t
ldns_fget_token(FILE *f, char *token, const char *delim, size_t limit)
26
{
27 28 29 30 31
	return ldns_fget_token_l(f, token, delim, limit, NULL);
}

ssize_t
ldns_fget_token_l(FILE *f, char *token, const char *delim, size_t limit, int *line_nr)
32
{
33
	int c, prev_c;
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
	int p; /* 0 -> no parenthese seen, >0 nr of ( seen */
	int com, quoted;
	char *t;
	size_t i;
	const char *d;
	const char *del;

	/* standard delimeters */
	if (!delim) {
		/* from isspace(3) */
		del = LDNS_PARSE_NORMAL;
	} else {
		del = delim;
	}

	p = 0;
	i = 0;
	com = 0;
	quoted = 0;
53
	prev_c = 0;
54 55 56 57 58
	t = token;
	if (del[0] == '"') {
		quoted = 1;
	}
	while ((c = getc(f)) != EOF) {
59 60
		if (c == '\r') /* carriage return */
			c = ' ';
61
		if (c == '(' && prev_c != '\\' && !quoted) {
62 63 64 65
			/* this only counts for non-comments */
			if (com == 0) {
				p++;
			}
66
			prev_c = c;
67 68 69
			continue;
		}

70
		if (c == ')' && prev_c != '\\' && !quoted) {
71 72 73 74
			/* this only counts for non-comments */
			if (com == 0) {
				p--;
			}
75
			prev_c = c;
76 77 78 79 80 81 82 83 84 85 86
			continue;
		}

		if (p < 0) {
			/* more ) then ( - close off the string */
			*t = '\0';
			return 0;
		}

		/* do something with comments ; */
		if (c == ';' && quoted == 0) {
87
			if (prev_c != '\\') {
88 89
				com = 1;
			}
90
		}
91
		if (c == '\"' && com == 0 && prev_c != '\\') {
92 93 94 95 96 97 98 99 100 101 102 103 104
			quoted = 1 - quoted;
		}

		if (c == '\n' && com != 0) {
			/* comments */
			com = 0;
			*t = ' ';
			if (line_nr) {
				*line_nr = *line_nr + 1;
			}
			if (p == 0 && i > 0) {
				goto tokenread;
			} else {
105
				prev_c = c;
106 107 108 109 110 111
				continue;
			}
		}

		if (com == 1) {
			*t = ' ';
112
			prev_c = c;
113 114 115 116 117 118 119 120
			continue;
		}

		if (c == '\n' && p != 0 && t > token) {
			/* in parentheses */
			if (line_nr) {
				*line_nr = *line_nr + 1;
			}
121
			*t++ = ' ';
122
			prev_c = c;
123 124 125 126 127
			continue;
		}

		/* check if we hit the delim */
		for (d = del; *d; d++) {
128
			if (c == *d && i > 0 && prev_c != '\\' && p == 0) {
129 130 131 132 133 134 135 136 137
				if (c == '\n' && line_nr) {
					*line_nr = *line_nr + 1;
				}
				goto tokenread;
			}
		}
		if (c != '\0' && c != '\n') {
			i++;
		}
138
		if (limit > 0 && (i >= limit || (size_t)(t-token) >= limit)) {
139 140 141
			*t = '\0';
			return -1;
		}
142 143 144
		if (c != '\0' && c != '\n') {
			*t++ = c;
		}
145 146 147
		if (c == '\\' && prev_c == '\\')
			prev_c = 0;
		else	prev_c = c;
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
	}
	*t = '\0';
	if (c == EOF) {
		return (ssize_t)i;
	}

	if (i == 0) {
		/* nothing read */
		return -1;
	}
	if (p != 0) {
		return -1;
	}
	return (ssize_t)i;

tokenread:
164 165 166
	if(*del == '"') /* do not skip over quotes, they are significant */
		ldns_fskipcs_l(f, del+1, line_nr);
	else	ldns_fskipcs_l(f, del, line_nr);
167 168 169 170 171 172 173 174 175 176 177 178
	*t = '\0';
	if (p != 0) {
		return -1;
	}

	return (ssize_t)i;
}

ssize_t
ldns_fget_keyword_data(FILE *f, const char *keyword, const char *k_del, char *data,
               const char *d_del, size_t data_limit)
{
179
       return ldns_fget_keyword_data_l(f, keyword, k_del, data, d_del,
180 181 182 183 184 185 186 187 188 189 190
		       data_limit, NULL);
}

ssize_t
ldns_fget_keyword_data_l(FILE *f, const char *keyword, const char *k_del, char *data,
               const char *d_del, size_t data_limit, int *line_nr)
{
       /* we assume: keyword|sep|data */
       char *fkeyword;
       ssize_t i;

191 192
       if(strlen(keyword) >= LDNS_MAX_KEYWORDLEN)
               return -1;
193
       fkeyword = LDNS_XMALLOC(char, LDNS_MAX_KEYWORDLEN);
194 195
       if(!fkeyword)
               return -1;
196 197

       i = ldns_fget_token(f, fkeyword, k_del, LDNS_MAX_KEYWORDLEN);
198 199 200 201
       if(i==0 || i==-1) {
               LDNS_FREE(fkeyword);
               return -1;
       }
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

       /* case??? i instead of strlen? */
       if (strncmp(fkeyword, keyword, LDNS_MAX_KEYWORDLEN - 1) == 0) {
               /* whee! */
               /* printf("%s\n%s\n", "Matching keyword", fkeyword); */
               i = ldns_fget_token_l(f, data, d_del, data_limit, line_nr);
               LDNS_FREE(fkeyword);
               return i;
       } else {
               /*printf("no match for %s (read: %s)\n", keyword, fkeyword);*/
               LDNS_FREE(fkeyword);
               return -1;
       }
}


ssize_t
ldns_bget_token(ldns_buffer *b, char *token, const char *delim, size_t limit)
220
{
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	int c, lc;
	int p; /* 0 -> no parenthese seen, >0 nr of ( seen */
	int com, quoted;
	char *t;
	size_t i;
	const char *d;
	const char *del;

	/* standard delimiters */
	if (!delim) {
		/* from isspace(3) */
		del = LDNS_PARSE_NORMAL;
	} else {
		del = delim;
	}

	p = 0;
	i = 0;
	com = 0;
	quoted = 0;
	t = token;
	lc = 0;
243
	if (del[0] == '"') {
244 245 246 247
		quoted = 1;
	}

	while ((c = ldns_bgetc(b)) != EOF) {
248 249
		if (c == '\r') /* carriage return */
			c = ' ';
250
		if (c == '(' && lc != '\\' && !quoted) {
251 252 253 254
			/* this only counts for non-comments */
			if (com == 0) {
				p++;
			}
255
			lc = c;
256 257 258
			continue;
		}

259
		if (c == ')' && lc != '\\' && !quoted) {
260 261 262 263
			/* this only counts for non-comments */
			if (com == 0) {
				p--;
			}
264
			lc = c;
265 266 267 268 269 270 271 272 273 274 275
			continue;
		}

		if (p < 0) {
			/* more ) then ( */
			*t = '\0';
			return 0;
		}

		/* do something with comments ; */
		if (c == ';' && quoted == 0) {
276 277 278
			if (lc != '\\') {
				com = 1;
			}
279
		}
280
		if (c == '"' && com == 0 && lc != '\\') {
281 282 283 284 285 286 287
			quoted = 1 - quoted;
		}

		if (c == '\n' && com != 0) {
			/* comments */
			com = 0;
			*t = ' ';
288
			lc = c;
289 290 291 292 293
			continue;
		}

		if (com == 1) {
			*t = ' ';
294
			lc = c;
295 296 297 298 299
			continue;
		}

		if (c == '\n' && p != 0) {
			/* in parentheses */
300
			*t++ = ' ';
301
			lc = c;
302 303 304 305 306
			continue;
		}

		/* check if we hit the delim */
		for (d = del; *d; d++) {
307
                        if (c == *d && lc != '\\' && p == 0) {
308 309 310
				goto tokenread;
                        }
		}
311

312
		i++;
313
		if (limit > 0 && (i >= limit || (size_t)(t-token) >= limit)) {
314 315 316
			*t = '\0';
			return -1;
		}
317
		*t++ = c;
318 319 320

		if (c == '\\' && lc == '\\') {
			lc = 0;
321 322
		} else {
			lc = c;
323 324 325 326 327 328 329 330 331 332 333 334 335
		}
	}
	*t = '\0';
	if (i == 0) {
		/* nothing read */
		return -1;
	}
	if (p != 0) {
		return -1;
	}
	return (ssize_t)i;

tokenread:
336 337 338
	if(*del == '"') /* do not skip over quotes, they are significant */
		ldns_bskipcs(b, del+1);
	else	ldns_bskipcs(b, del);
339
	*t = '\0';
340

341
	if (p != 0) {
342
		return -1;
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
	}
	return (ssize_t)i;
}


void
ldns_bskipcs(ldns_buffer *buffer, const char *s)
{
        bool found;
        char c;
        const char *d;

        while(ldns_buffer_available_at(buffer, buffer->_position, sizeof(char))) {
                c = (char) ldns_buffer_read_u8_at(buffer, buffer->_position);
                found = false;
                for (d = s; *d; d++) {
                        if (*d == c) {
                                found = true;
                        }
                }
                if (found && buffer->_limit > buffer->_position) {
                        buffer->_position += sizeof(char);
                } else {
                        return;
                }
        }
}

void
ldns_fskipcs(FILE *fp, const char *s)
{
	ldns_fskipcs_l(fp, s, NULL);
}

void
ldns_fskipcs_l(FILE *fp, const char *s, int *line_nr)
{
        bool found;
        int c;
        const char *d;

	while ((c = fgetc(fp)) != EOF) {
		if (line_nr && c == '\n') {
			*line_nr = *line_nr + 1;
		}
                found = false;
                for (d = s; *d; d++) {
                        if (*d == c) {
                                found = true;
                        }
                }
		if (!found) {
			/* with getc, we've read too far */
			ungetc(c, fp);
			return;
		}
	}
}

ssize_t
ldns_bget_keyword_data(ldns_buffer *b, const char *keyword, const char *k_del, char
*data, const char *d_del, size_t data_limit)
{
       /* we assume: keyword|sep|data */
       char *fkeyword;
       ssize_t i;

410 411
       if(strlen(keyword) >= LDNS_MAX_KEYWORDLEN)
               return -1;
412
       fkeyword = LDNS_XMALLOC(char, LDNS_MAX_KEYWORDLEN);
413 414
       if(!fkeyword)
               return -1; /* out of memory */
415 416

       i = ldns_bget_token(b, fkeyword, k_del, data_limit);
417 418 419 420
       if(i==0 || i==-1) {
               LDNS_FREE(fkeyword);
               return -1; /* nothing read */
       }
421 422 423

       /* case??? */
       if (strncmp(fkeyword, keyword, strlen(keyword)) == 0) {
424
               LDNS_FREE(fkeyword);
425 426 427 428 429
               /* whee, the match! */
               /* retrieve it's data */
               i = ldns_bget_token(b, data, d_del, 0);
               return i;
       } else {
430
               LDNS_FREE(fkeyword);
431 432 433 434
               return -1;
       }
}