parse.c
Go to the documentation of this file.
1 /*
2  * a generic (simple) parser. Use to parse rr's, private key
3  * information and /etc/resolv.conf files
4  *
5  * a Net::DNS like library for C
6  * LibDNS Team @ NLnet Labs
7  * (c) NLnet Labs, 2005-2006
8  * See the file LICENSE for the license
9  */
10 #include <ldns/config.h>
11 #include <ldns/ldns.h>
12 
13 #include <limits.h>
14 #include <strings.h>
15 
17  { LDNS_DIR_TTL, "$TTL" },
18  { LDNS_DIR_ORIGIN, "$ORIGIN" },
19  { LDNS_DIR_INCLUDE, "$INCLUDE" },
20  { 0, NULL }
21 };
22 
23 /* add max_limit here? */
24 ssize_t
25 ldns_fget_token(FILE *f, char *token, const char *delim, size_t limit)
26 {
27  return ldns_fget_token_l(f, token, delim, limit, NULL);
28 }
29 
30 ssize_t
31 ldns_fget_token_l(FILE *f, char *token, const char *delim, size_t limit, int *line_nr)
32 {
33  int c, prev_c;
34  int p; /* 0 -> no parenthese seen, >0 nr of ( seen */
35  int com, quoted;
36  char *t;
37  size_t i;
38  const char *d;
39  const char *del;
40 
41  /* standard delimeters */
42  if (!delim) {
43  /* from isspace(3) */
44  del = LDNS_PARSE_NORMAL;
45  } else {
46  del = delim;
47  }
48 
49  p = 0;
50  i = 0;
51  com = 0;
52  quoted = 0;
53  prev_c = 0;
54  t = token;
55  if (del[0] == '"') {
56  quoted = 1;
57  }
58  while ((c = getc(f)) != EOF) {
59  if (c == '\r') /* carriage return */
60  c = ' ';
61  if (c == '(' && prev_c != '\\' && !quoted) {
62  /* this only counts for non-comments */
63  if (com == 0) {
64  p++;
65  }
66  prev_c = c;
67  continue;
68  }
69 
70  if (c == ')' && prev_c != '\\' && !quoted) {
71  /* this only counts for non-comments */
72  if (com == 0) {
73  p--;
74  }
75  prev_c = c;
76  continue;
77  }
78 
79  if (p < 0) {
80  /* more ) then ( - close off the string */
81  *t = '\0';
82  return 0;
83  }
84 
85  /* do something with comments ; */
86  if (c == ';' && quoted == 0) {
87  if (prev_c != '\\') {
88  com = 1;
89  }
90  }
91  if (c == '\"' && com == 0 && prev_c != '\\') {
92  quoted = 1 - quoted;
93  }
94 
95  if (c == '\n' && com != 0) {
96  /* comments */
97  com = 0;
98  *t = ' ';
99  if (line_nr) {
100  *line_nr = *line_nr + 1;
101  }
102  if (p == 0 && i > 0) {
103  goto tokenread;
104  } else {
105  prev_c = c;
106  continue;
107  }
108  }
109 
110  if (com == 1) {
111  *t = ' ';
112  prev_c = c;
113  continue;
114  }
115 
116  if (c == '\n' && p != 0 && t > token) {
117  /* in parentheses */
118  if (line_nr) {
119  *line_nr = *line_nr + 1;
120  }
121  if (limit > 0 && (i >= limit || (size_t)(t-token) >= limit)) {
122  *t = '\0';
123  return -1;
124  }
125  *t++ = ' ';
126  prev_c = c;
127  continue;
128  }
129 
130  /* check if we hit the delim */
131  for (d = del; *d; d++) {
132  if (c == *d && i > 0 && prev_c != '\\' && p == 0) {
133  if (c == '\n' && line_nr) {
134  *line_nr = *line_nr + 1;
135  }
136  goto tokenread;
137  }
138  }
139  if (c != '\0' && c != '\n') {
140  i++;
141  }
142  if (limit > 0 && (i >= limit || (size_t)(t-token) >= limit)) {
143  *t = '\0';
144  return -1;
145  }
146  if (c != '\0' && c != '\n') {
147  *t++ = c;
148  }
149  if (c == '\\' && prev_c == '\\')
150  prev_c = 0;
151  else prev_c = c;
152  }
153  *t = '\0';
154  if (c == EOF) {
155  return (ssize_t)i;
156  }
157 
158  if (i == 0) {
159  /* nothing read */
160  return -1;
161  }
162  if (p != 0) {
163  return -1;
164  }
165  return (ssize_t)i;
166 
167 tokenread:
168  ldns_fskipcs_l(f, del, line_nr);
169  *t = '\0';
170  if (p != 0) {
171  return -1;
172  }
173 
174  return (ssize_t)i;
175 }
176 
177 ssize_t
178 ldns_fget_keyword_data(FILE *f, const char *keyword, const char *k_del, char *data,
179  const char *d_del, size_t data_limit)
180 {
181  return ldns_fget_keyword_data_l(f, keyword, k_del, data, d_del,
182  data_limit, NULL);
183 }
184 
185 ssize_t
186 ldns_fget_keyword_data_l(FILE *f, const char *keyword, const char *k_del, char *data,
187  const char *d_del, size_t data_limit, int *line_nr)
188 {
189  /* we assume: keyword|sep|data */
190  char *fkeyword;
191  ssize_t i;
192 
193  if(strlen(keyword) >= LDNS_MAX_KEYWORDLEN)
194  return -1;
195  fkeyword = LDNS_XMALLOC(char, LDNS_MAX_KEYWORDLEN);
196  if(!fkeyword)
197  return -1;
198 
199  i = ldns_fget_token(f, fkeyword, k_del, LDNS_MAX_KEYWORDLEN);
200  if(i==0 || i==-1) {
201  LDNS_FREE(fkeyword);
202  return -1;
203  }
204 
205  /* case??? i instead of strlen? */
206  if (strncmp(fkeyword, keyword, LDNS_MAX_KEYWORDLEN - 1) == 0) {
207  /* whee! */
208  /* printf("%s\n%s\n", "Matching keyword", fkeyword); */
209  i = ldns_fget_token_l(f, data, d_del, data_limit, line_nr);
210  LDNS_FREE(fkeyword);
211  return i;
212  } else {
213  /*printf("no match for %s (read: %s)\n", keyword, fkeyword);*/
214  LDNS_FREE(fkeyword);
215  return -1;
216  }
217 }
218 
219 
220 ssize_t
221 ldns_bget_token(ldns_buffer *b, char *token, const char *delim, size_t limit)
222 {
223  int c, lc;
224  int p; /* 0 -> no parenthese seen, >0 nr of ( seen */
225  int com, quoted;
226  char *t;
227  size_t i;
228  const char *d;
229  const char *del;
230 
231  /* standard delimiters */
232  if (!delim) {
233  /* from isspace(3) */
234  del = LDNS_PARSE_NORMAL;
235  } else {
236  del = delim;
237  }
238 
239  p = 0;
240  i = 0;
241  com = 0;
242  quoted = 0;
243  t = token;
244  lc = 0;
245  if (del[0] == '"') {
246  quoted = 1;
247  }
248 
249  while ((c = ldns_bgetc(b)) != EOF) {
250  if (c == '\r') /* carriage return */
251  c = ' ';
252  if (c == '(' && lc != '\\' && !quoted) {
253  /* this only counts for non-comments */
254  if (com == 0) {
255  p++;
256  }
257  lc = c;
258  continue;
259  }
260 
261  if (c == ')' && lc != '\\' && !quoted) {
262  /* this only counts for non-comments */
263  if (com == 0) {
264  p--;
265  }
266  lc = c;
267  continue;
268  }
269 
270  if (p < 0) {
271  /* more ) then ( */
272  *t = '\0';
273  return 0;
274  }
275 
276  /* do something with comments ; */
277  if (c == ';' && quoted == 0) {
278  if (lc != '\\') {
279  com = 1;
280  }
281  }
282  if (c == '"' && com == 0 && lc != '\\') {
283  quoted = 1 - quoted;
284  }
285 
286  if (c == '\n' && com != 0) {
287  /* comments */
288  com = 0;
289  *t = ' ';
290  lc = c;
291  continue;
292  }
293 
294  if (com == 1) {
295  *t = ' ';
296  lc = c;
297  continue;
298  }
299 
300  if (c == '\n' && p != 0) {
301  /* in parentheses */
302  *t++ = ' ';
303  lc = c;
304  continue;
305  }
306 
307  /* check if we hit the delim */
308  for (d = del; *d; d++) {
309  if (c == *d && lc != '\\' && p == 0) {
310  goto tokenread;
311  }
312  }
313 
314  i++;
315  if (limit > 0 && (i >= limit || (size_t)(t-token) >= limit)) {
316  *t = '\0';
317  return -1;
318  }
319  *t++ = c;
320 
321  if (c == '\\' && lc == '\\') {
322  lc = 0;
323  } else {
324  lc = c;
325  }
326  }
327  *t = '\0';
328  if (i == 0) {
329  /* nothing read */
330  return -1;
331  }
332  if (p != 0) {
333  return -1;
334  }
335  return (ssize_t)i;
336 
337 tokenread:
338  ldns_bskipcs(b, del);
339  *t = '\0';
340 
341  if (p != 0) {
342  return -1;
343  }
344  return (ssize_t)i;
345 }
346 
347 
348 void
349 ldns_bskipcs(ldns_buffer *buffer, const char *s)
350 {
351  bool found;
352  char c;
353  const char *d;
354 
355  while(ldns_buffer_available_at(buffer, buffer->_position, sizeof(char))) {
356  c = (char) ldns_buffer_read_u8_at(buffer, buffer->_position);
357  found = false;
358  for (d = s; *d; d++) {
359  if (*d == c) {
360  found = true;
361  }
362  }
363  if (found && buffer->_limit > buffer->_position) {
364  buffer->_position += sizeof(char);
365  } else {
366  return;
367  }
368  }
369 }
370 
371 void
372 ldns_fskipcs(FILE *fp, const char *s)
373 {
374  ldns_fskipcs_l(fp, s, NULL);
375 }
376 
377 void
378 ldns_fskipcs_l(FILE *fp, const char *s, int *line_nr)
379 {
380  bool found;
381  int c;
382  const char *d;
383 
384  while ((c = fgetc(fp)) != EOF) {
385  if (line_nr && c == '\n') {
386  *line_nr = *line_nr + 1;
387  }
388  found = false;
389  for (d = s; *d; d++) {
390  if (*d == c) {
391  found = true;
392  }
393  }
394  if (!found) {
395  /* with getc, we've read too far */
396  ungetc(c, fp);
397  return;
398  }
399  }
400 }
401 
402 ssize_t
403 ldns_bget_keyword_data(ldns_buffer *b, const char *keyword, const char *k_del, char
404 *data, const char *d_del, size_t data_limit)
405 {
406  /* we assume: keyword|sep|data */
407  char *fkeyword;
408  ssize_t i;
409 
410  if(strlen(keyword) >= LDNS_MAX_KEYWORDLEN)
411  return -1;
412  fkeyword = LDNS_XMALLOC(char, LDNS_MAX_KEYWORDLEN);
413  if(!fkeyword)
414  return -1; /* out of memory */
415 
416  i = ldns_bget_token(b, fkeyword, k_del, data_limit);
417  if(i==0 || i==-1) {
418  LDNS_FREE(fkeyword);
419  return -1; /* nothing read */
420  }
421 
422  /* case??? */
423  if (strncmp(fkeyword, keyword, strlen(keyword)) == 0) {
424  LDNS_FREE(fkeyword);
425  /* whee, the match! */
426  /* retrieve it's data */
427  i = ldns_bget_token(b, data, d_del, 0);
428  return i;
429  } else {
430  LDNS_FREE(fkeyword);
431  return -1;
432  }
433 }
434 
implementation of buffers to ease operations
Definition: buffer.h:50
#define LDNS_PARSE_NORMAL
Definition: parse.h:21
#define LDNS_XMALLOC(type, count)
Definition: util.h:51
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)
Definition: parse.c:186
size_t _limit
The read/write limit.
Definition: buffer.h:56
void ldns_fskipcs(FILE *fp, const char *s)
skips all of the characters in the given string in the fp, moving the position to the first character...
Definition: parse.c:372
ssize_t ldns_bget_token(ldns_buffer *b, char *token, const char *delim, size_t limit)
returns a token/char from the buffer b.
Definition: parse.c:221
int ldns_bgetc(ldns_buffer *buffer)
returns the next character from a buffer.
Definition: buffer.c:157
ldns_lookup_table ldns_directive_types[]
Definition: parse.c:16
Including this file will include all ldns files, and define some lookup tables.
#define LDNS_MAX_KEYWORDLEN
Definition: parse.h:24
ssize_t ldns_fget_token(FILE *f, char *token, const char *delim, size_t limit)
returns a token/char from the stream F.
Definition: parse.c:25
size_t _position
The current position used for reading/writing.
Definition: buffer.h:53
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)
Definition: parse.c:178
void ldns_bskipcs(ldns_buffer *buffer, const char *s)
skips all of the characters in the given string in the buffer, moving the position to the first chara...
Definition: parse.c:349
ssize_t ldns_fget_token_l(FILE *f, char *token, const char *delim, size_t limit, int *line_nr)
returns a token/char from the stream F.
Definition: parse.c:31
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)
Definition: parse.c:403
void ldns_fskipcs_l(FILE *fp, const char *s, int *line_nr)
skips all of the characters in the given string in the fp, moving the position to the first character...
Definition: parse.c:378
A general purpose lookup table.
Definition: util.h:156
#define LDNS_FREE(ptr)
Definition: util.h:60