3 * an API for text tvb parsers
5 * Copyright 2005, Luis E. Garcia Ontanon <luis@ontanon.org>
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 The intention behind this is to ease the writing of dissectors that have to
28 parse text without the need of writing into buffers.
30 It was originally written to avoid using lex and yacc for the xml dissector.
32 the parser is able to look for wanted elements these can be:
35 - a char out of a string of needles
36 - a char not belonging to a string of needles
37 - a sequence of chars that belong to a set of chars
38 - a sequence of chars that do not belong to a set of chars
41 - all the characters up to a certain wanted element (included or excluded)
44 - one of a given group of wanted elements
45 - a sequence of wanted elements
46 - some (at least one) instances of a wanted element
48 Once a wanted element is successfully extracted, by either tvbparse_get or
49 tvbparse_find, the parser will invoke a given callback
50 before and another one after every of its component's subelement's callbacks
53 If tvbparse_get or tvbparse_find fail to extract the wanted element the
54 subelements callbacks are not going to be invoked.
56 The wanted elements are instantiated once by the proto_register_xxx function.
58 The parser is instantiated for every packet and it mantains its state.
60 The element's data is destroyed before the next packet is dissected.
66 #include <epan/tvbuff.h>
68 #include "ws_symbol_export.h"
70 typedef struct _tvbparse_elem_t tvbparse_elem_t;
71 typedef struct _tvbparse_wanted_t tvbparse_wanted_t;
72 typedef struct _tvbparse_t tvbparse_t;
76 * a callback function to be called before or after an element has been
77 * successfuly extracted.
79 * Note that if the token belongs to a composed token the callbacks of the
80 * components won't be called unless the composed token is successfully
83 * tvbparse_data: the private data of the parser
84 * wanted_data: the private data of the wanted element
85 * elem: the extracted element
87 typedef void (*tvbparse_action_t)(void* tvbparse_data, const void* wanted_data, struct _tvbparse_elem_t* elem);
89 typedef int (*tvbparse_condition_t)
90 (tvbparse_t*, const int,
91 const tvbparse_wanted_t*,
96 TP_UNTIL_INCLUDE, /* last elem is included, its span is spent by the parser */
97 TP_UNTIL_SPEND, /* last elem is not included, but its span is spent by the parser */
98 TP_UNTIL_LEAVE /* last elem is not included, neither its span is spent by the parser */
102 struct _tvbparse_wanted_t {
104 tvbparse_condition_t condition;
108 struct _tvbparse_wanted_t** handle;
115 gboolean (*comp)(void*,const void*);
116 void* (*extract)(tvbuff_t*,guint);
121 const tvbparse_wanted_t* subelem;
125 struct _tvbparse_wanted_t* key;
126 struct _tvbparse_wanted_t* other;
129 const tvbparse_wanted_t* subelem;
140 tvbparse_action_t before;
141 tvbparse_action_t after;
144 /* an instance of a per packet parser */
150 const tvbparse_wanted_t* ignore;
155 /* a matching token returned by either tvbparser_get or tvb_parser_find */
156 struct _tvbparse_elem_t {
165 struct _tvbparse_elem_t* sub;
167 struct _tvbparse_elem_t* next;
168 struct _tvbparse_elem_t* last;
170 const tvbparse_wanted_t* wanted;
175 * definition of wanted token types
177 * the following functions define the tokens we will be able to look for in a tvb
178 * common parameters are:
180 * id: an arbitrary id that will be copied to the eventual token (don't use 0)
181 * private_data: persistent data to be passed to the callback action (wanted_data)
182 * before_cb: an callback function to be called before those of the subelements
183 * after_cb: an callback function to be called after those of the subelements
190 * When looked for it returns a simple element one character long if the char
191 * at the current offset matches one of the the needles.
194 tvbparse_wanted_t* tvbparse_char(const int id,
195 const gchar* needles,
196 const void* private_data,
197 tvbparse_action_t before_cb,
198 tvbparse_action_t after_cb);
201 * a not_char element.
203 * When looked for it returns a simple element one character long if the char
204 * at the current offset does not match one of the the needles.
207 tvbparse_wanted_t* tvbparse_not_char(const int id,
209 const void* private_data,
210 tvbparse_action_t before_cb,
211 tvbparse_action_t after_cb);
216 * When looked for it returns a simple element one or more characters long if
217 * one or more char(s) starting from the current offset match one of the needles.
218 * An element will be returned if at least min_len chars are given (1 if it's 0)
219 * It will get at most max_len chars or as much as it can if max_len is 0.
222 tvbparse_wanted_t* tvbparse_chars(const int id,
225 const gchar* needles,
226 const void* private_data,
227 tvbparse_action_t before_cb,
228 tvbparse_action_t after_cb);
231 * a not_chars element
233 * When looked for it returns a simple element one or more characters long if
234 * one or more char(s) starting from the current offset do not match one of the
236 * An element will be returned if at least min_len chars are given (1 if it's 0)
237 * It will get at most max_len chars or as much as it can if max_len is 0.
240 tvbparse_wanted_t* tvbparse_not_chars(const int id,
243 const gchar* needles,
244 const void* private_data,
245 tvbparse_action_t before_cb,
246 tvbparse_action_t after_cb);
251 * When looked for it returns a simple element if we have the given string at
255 tvbparse_wanted_t* tvbparse_string(const int id,
257 const void* private_data,
258 tvbparse_action_t before_cb,
259 tvbparse_action_t after_cb);
264 * When looked for it returns a simple element if we have a matching string at
268 tvbparse_wanted_t* tvbparse_casestring(const int id,
271 tvbparse_action_t before_cb,
272 tvbparse_action_t after_cb);
277 * When looked for it returns a simple element containing all the characters
278 * found until the first match of the ending element if the ending element is
281 * When looking for until elements it calls tvbparse_find so it can be very slow.
283 * It won't have a subelement, the ending's callbacks won't get called.
287 * op_mode values determine how the terminating element and the current offset
288 * of the parser are handled
291 tvbparse_wanted_t* tvbparse_until(const int id,
292 const void* private_data,
293 tvbparse_action_t before_cb,
294 tvbparse_action_t after_cb,
295 const tvbparse_wanted_t* ending,
296 until_mode_t until_mode);
301 * When looked for it will try to match to the given candidates and return a
302 * composed element whose subelement is the first match.
304 * The list of candidates is terminated with a NULL
308 tvbparse_wanted_t* tvbparse_set_oneof(const int id,
309 const void* private_data,
310 tvbparse_action_t before_cb,
311 tvbparse_action_t after_cb,
318 tvbparse_wanted_t* tvbparse_hashed(const int id,
320 tvbparse_action_t before_cb,
321 tvbparse_action_t after_cb,
322 tvbparse_wanted_t* key,
323 tvbparse_wanted_t* other,
327 void tvbparse_hashed_add(tvbparse_wanted_t* w, ...);
332 * When looked for it will try to match in order all the given candidates. If
333 * every candidate is found in the given order it will return a composed
334 * element whose subelements are the matcheed elemets.
336 * The list of candidates is terminated with a NULL.
340 tvbparse_wanted_t* tvbparse_set_seq(const int id,
341 const void* private_data,
342 tvbparse_action_t before_cb,
343 tvbparse_action_t after_cb,
349 * When looked for it will try to match the given candidate at least min times
350 * and at most max times. If the given candidate is matched at least min times
351 * a composed element is returned.
355 tvbparse_wanted_t* tvbparse_some(const int id,
358 const void* private_data,
359 tvbparse_action_t before_cb,
360 tvbparse_action_t after_cb,
361 const tvbparse_wanted_t* wanted);
363 #define tvbparse_one_or_more(id, private_data, before_cb, after_cb, wanted)\
364 tvbparse_some(id, 1, G_MAXINT, private_data, before_cb, after_cb, wanted)
370 * this is a pointer to a pointer to a wanted element (that might have not
371 * been initialized yet) so that recursive structures
374 tvbparse_wanted_t* tvbparse_handle(tvbparse_wanted_t** handle);
388 tvbparse_wanted_t* tvbparse_ft(int id,
390 tvbparse_action_t before_cb,
391 tvbparse_action_t after_cb,
395 tvbparse_wanted_t* tvbparse_end_of_buffer(int id,
397 tvbparse_action_t before_cb,
398 tvbparse_action_t after_cb);
400 tvbparse_wanted_t* tvbparse_ft_numcmp(int id,
402 tvbparse_action_t before_cb,
403 tvbparse_action_t after_cb,
406 enum ft_cmp_op ft_cmp_op,
412 * this is a composed candidate, that will try to match a quoted string
413 * (included the quotes) including into it every escaped quote.
415 * C strings are matched with tvbparse_quoted(-1,NULL,NULL,NULL,"\"","\\")
418 tvbparse_wanted_t* tvbparse_quoted(const int id,
420 tvbparse_action_t before_cb,
421 tvbparse_action_t after_cb,
426 * a helper callback for quoted strings that will shrink the token to contain
427 * only the string andnot the quotes
430 void tvbparse_shrink_token_cb(void* tvbparse_data,
431 const void* wanted_data,
432 tvbparse_elem_t* tok);
437 /* initialize the parser (at every packet)
438 * tvb: what are we parsing?
440 * len: for how many bytes
441 * private_data: will be passed to the action callbacks
442 * ignore: a wanted token type to be ignored (the associated cb WILL be called when it matches)
445 tvbparse_t* tvbparse_init(tvbuff_t* tvb,
449 const tvbparse_wanted_t* ignore);
451 /* reset the parser */
453 gboolean tvbparse_reset(tvbparse_t* tt, const int offset, int len);
456 guint tvbparse_curr_offset(tvbparse_t* tt);
457 guint tvbparse_len_left(tvbparse_t* tt);
462 * This will look for the wanted token at the current offset or after any given
463 * number of ignored tokens returning FALSE if there's no match or TRUE if there
465 * The parser will be left in its original state and no callbacks will be called.
468 gboolean tvbparse_peek(tvbparse_t* tt,
469 const tvbparse_wanted_t* wanted);
472 * This will look for the wanted token at the current offset or after any given
473 * number of ignored tokens returning NULL if there's no match.
474 * if there is a match it will set the offset of the current parser after
475 * the end of the token
478 tvbparse_elem_t* tvbparse_get(tvbparse_t* tt,
479 const tvbparse_wanted_t* wanted);
482 * Like tvbparse_get but this will look for a wanted token even beyond the
484 * This function is slow.
487 tvbparse_elem_t* tvbparse_find(tvbparse_t* tt,
488 const tvbparse_wanted_t* wanted);
492 void tvbparse_tree_add_elem(proto_tree* tree, tvbparse_elem_t* curr);