4 * an API for text tvb parsers
6 * Copyright 2005, Luis E. Garcia Ontanon <luis@ontanon.org>
10 * Wireshark - Network traffic analyzer
11 * By Gerald Combs <gerald@wireshark.org>
12 * Copyright 1998 Gerald Combs
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 The intention behind this is to ease the writing of dissectors that have to
31 parse text without the need of writing into buffers.
33 It was originally written to avoid using lex and yacc for the xml dissector.
35 the parser is able to look for wanted elements these can be:
38 - a char out of a string of needles
39 - a char not belonging to a string of needles
40 - a sequence of chars that belong to a set of chars
41 - a sequence of chars that do not belong to a set of chars
44 - all the characters up to a certain wanted element (included or excluded)
47 - one of a given group of wanted elements
48 - a sequence of wanted elements
49 - some (at least one) instances of a wanted element
51 Once a wanted element is successfully extracted, by either tvbparse_get or
52 tvbparse_find, the parser will invoke a given callback
53 before and another one after every of its component's subelement's callbacks
56 If tvbparse_get or tvbparse_find fail to extract the wanted element the
57 subelements callbacks are not going to be invoked.
59 The wanted elements are instantiated once by the proto_register_xxx function.
61 The parser is instantiated for every packet and it mantains its state.
63 The element's data is destroyed before the next packet is dissected.
69 #include <epan/tvbuff.h>
72 typedef struct _tvbparse_elem_t tvbparse_elem_t;
73 typedef struct _tvbparse_wanted_t tvbparse_wanted_t;
74 typedef struct _tvbparse_t tvbparse_t;
78 * a callback function to be called before or after an element has been
79 * successfuly extracted.
81 * Note that if the token belongs to a composed token the callbacks of the
82 * components won't be called unless the composed token is successfully
85 * tvbparse_data: the private data of the parser
86 * wanted_data: the private data of the wanted element
87 * elem: the extracted element
89 typedef void (*tvbparse_action_t)(void* tvbparse_data, const void* wanted_data, struct _tvbparse_elem_t* elem);
91 typedef int (*tvbparse_condition_t)
93 const tvbparse_wanted_t*,
98 TP_UNTIL_INCLUDE, /* last elem is included, its span is spent by the parser */
99 TP_UNTIL_SPEND, /* last elem is not included, but its span is spent by the parser */
100 TP_UNTIL_LEAVE /* last elem is not included, neither its span is spent by the parser */
104 struct _tvbparse_wanted_t {
106 tvbparse_condition_t condition;
110 struct _tvbparse_wanted_t** handle;
117 gboolean (*comp)(void*,const void*);
118 void* (*extract)(tvbuff_t*,guint);
123 const tvbparse_wanted_t* subelem;
127 struct _tvbparse_wanted_t* key;
128 struct _tvbparse_wanted_t* other;
131 const tvbparse_wanted_t* subelem;
142 tvbparse_action_t before;
143 tvbparse_action_t after;
147 /* an instance of a per packet parser */
153 const tvbparse_wanted_t* ignore;
157 /* a matching token returned by either tvbparser_get or tvb_parser_find */
158 struct _tvbparse_elem_t {
167 struct _tvbparse_elem_t* sub;
169 struct _tvbparse_elem_t* next;
170 struct _tvbparse_elem_t* last;
172 const tvbparse_wanted_t* wanted;
177 * definition of wanted token types
179 * the following functions define the tokens we will be able to look for in a tvb
180 * common parameters are:
182 * id: an arbitrary id that will be copied to the eventual token (don't use 0)
183 * private_data: persistent data to be passed to the callback action (wanted_data)
184 * before_cb: an callback function to be called before those of the subelements
185 * after_cb: an callback function to be called after those of the subelements
192 * When looked for it returns a simple element one character long if the char
193 * at the current offset matches one of the the needles.
195 tvbparse_wanted_t* tvbparse_char(int id,
196 const gchar* needles,
197 const void* private_data,
198 tvbparse_action_t before_cb,
199 tvbparse_action_t after_cb);
202 * a not_char element.
204 * When looked for it returns a simple element one character long if the char
205 * at the current offset does not match one of the the needles.
207 tvbparse_wanted_t* tvbparse_not_char(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.
221 tvbparse_wanted_t* tvbparse_chars(int id,
224 const gchar* needles,
225 const void* private_data,
226 tvbparse_action_t before_cb,
227 tvbparse_action_t after_cb);
230 * a not_chars element
232 * When looked for it returns a simple element one or more characters long if
233 * one or more char(s) starting from the current offset do not match one of the
235 * An element will be returned if at least min_len chars are given (1 if it's 0)
236 * It will get at most max_len chars or as much as it can if max_len is 0.
238 tvbparse_wanted_t* tvbparse_not_chars(int id,
241 const gchar* needles,
242 const void* private_data,
243 tvbparse_action_t before_cb,
244 tvbparse_action_t after_cb);
249 * When looked for it returns a simple element if we have the given string at
252 tvbparse_wanted_t* tvbparse_string(int id,
254 const void* private_data,
255 tvbparse_action_t before_cb,
256 tvbparse_action_t after_cb);
261 * When looked for it returns a simple element if we have a matching string at
264 tvbparse_wanted_t* tvbparse_casestring(int id,
267 tvbparse_action_t before_cb,
268 tvbparse_action_t after_cb);
273 * When looked for it returns a simple element containing all the characters
274 * found until the first match of the ending element if the ending element is
277 * When looking for until elements it calls tvbparse_find so it can be very slow.
279 * It won't have a subelement, the ending's callbacks won't get called.
283 * op_mode values determine how the terminating element and the current offset
284 * of the parser are handled
286 tvbparse_wanted_t* tvbparse_until(int id,
287 const void* private_data,
288 tvbparse_action_t before_cb,
289 tvbparse_action_t after_cb,
290 const tvbparse_wanted_t* ending,
291 until_mode_t until_mode);
296 * When looked for it will try to match to the given candidates and return a
297 * composed element whose subelement is the first match.
299 * The list of candidates is terminated with a NULL
302 tvbparse_wanted_t* tvbparse_set_oneof(int id,
303 const void* private_data,
304 tvbparse_action_t before_cb,
305 tvbparse_action_t after_cb,
312 tvbparse_wanted_t* tvbparse_hashed(int id,
314 tvbparse_action_t before_cb,
315 tvbparse_action_t after_cb,
316 tvbparse_wanted_t* key,
317 tvbparse_wanted_t* other,
320 void tvbparse_hashed_add(tvbparse_wanted_t* w, ...);
325 * When looked for it will try to match in order all the given candidates. If
326 * every candidate is found in the given order it will return a composed
327 * element whose subelements are the matcheed elemets.
329 * The list of candidates is terminated with a NULL.
332 tvbparse_wanted_t* tvbparse_set_seq(int id,
333 const void* private_data,
334 tvbparse_action_t before_cb,
335 tvbparse_action_t after_cb,
340 * When looked for it will try to match the given candidate at least min times
341 * and at most max times. If the given candidate is matched at least min times
342 * a composed element is returned.
345 tvbparse_wanted_t* tvbparse_some(int id,
348 const void* private_data,
349 tvbparse_action_t before_cb,
350 tvbparse_action_t after_cb,
351 const tvbparse_wanted_t* wanted);
353 #define tvbparse_one_or_more(id, private_data, before_cb, after_cb, wanted)\
354 tvbparse_some(id, 1, G_MAXINT, private_data, before_cb, after_cb, wanted)
360 * this is a pointer to a pointer to a wanted element (that might have not
361 * been initialized yet) so that recursive structures
363 tvbparse_wanted_t* tvbparse_handle(tvbparse_wanted_t** handle);
377 tvbparse_wanted_t* tvbparse_ft(int id,
379 tvbparse_action_t before_cb,
380 tvbparse_action_t after_cb,
384 tvbparse_wanted_t* tvbparse_end_of_buffer(int id,
386 tvbparse_action_t before_cb,
387 tvbparse_action_t after_cb);
389 tvbparse_wanted_t* tvbparse_ft_numcmp(int id,
391 tvbparse_action_t before_cb,
392 tvbparse_action_t after_cb,
395 enum ft_cmp_op ft_cmp_op,
401 * this is a composed candidate, that will try to match a quoted string
402 * (included the quotes) including into it every escaped quote.
404 * C strings are matched with tvbparse_quoted(-1,NULL,NULL,NULL,"\"","\\")
406 tvbparse_wanted_t* tvbparse_quoted(int id,
408 tvbparse_action_t before_cb,
409 tvbparse_action_t after_cb,
414 * a helper callback for quoted strings that will shrink the token to contain
415 * only the string andnot the quotes
417 void tvbparse_shrink_token_cb(void* tvbparse_data,
418 const void* wanted_data,
419 tvbparse_elem_t* tok);
424 /* initialize the parser (at every packet)
425 * tvb: what are we parsing?
427 * len: for how many bytes
428 * private_data: will be passed to the action callbacks
429 * ignore: a wanted token type to be ignored (the associated cb WILL be called when it matches)
431 tvbparse_t* tvbparse_init(tvbuff_t* tvb,
435 const tvbparse_wanted_t* ignore);
437 /* reset the parser */
438 gboolean tvbparse_reset(tvbparse_t* tt, int offset, int len);
440 guint tvbparse_curr_offset(tvbparse_t* tt);
441 guint tvbparse_len_left(tvbparse_t* tt);
446 * This will look for the wanted token at the current offset or after any given
447 * number of ignored tokens returning FALSE if there's no match or TRUE if there
449 * The parser will be left in its original state and no callbacks will be called.
451 gboolean tvbparse_peek(tvbparse_t* tt,
452 const tvbparse_wanted_t* wanted);
455 * This will look for the wanted token at the current offset or after any given
456 * number of ignored tokens returning NULL if there's no match.
457 * if there is a match it will set the offset of the current parser after
458 * the end of the token
460 tvbparse_elem_t* tvbparse_get(tvbparse_t* tt,
461 const tvbparse_wanted_t* wanted);
464 * Like tvbparse_get but this will look for a wanted token even beyond the
466 * This function is slow.
469 tvbparse_elem_t* tvbparse_find(tvbparse_t* tt,
470 const tvbparse_wanted_t* wanted);
473 void tvbparse_tree_add_elem(proto_tree* tree, tvbparse_elem_t* curr);