aboutsummaryrefslogtreecommitdiffstats
path: root/epan/tvbparse.c
blob: 7b0114ddba8847d6cd69db010f21eea8f888dc1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
/* tvbparse.c
*
* Copyright 2005, Luis E. Garcia Ontanon <luis.ontanon@gmail.com>
*
* $Id$
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <string.h>
#include <glib.h>

#include <epan/emem.h>
#include <epan/proto.h>
#include <epan/packet_info.h>
#include <epan/tvbparse.h>


#define TVBPARSE_DEBUG_ALL 0xffffffff

/*#define TVBPARSE_DEBUG_ 0x80000000
#define TVBPARSE_DEBUG_ 0x40000000
#define TVBPARSE_DEBUG_ 0x20000000
#define TVBPARSE_DEBUG_ 0x10000000*/

#define TVBPARSE_DEBUG_CHAR 0x08000000
#define TVBPARSE_DEBUG_CHARS 0x04000000
#define TVBPARSE_DEBUG_NOT_CHAR 0x02000000
#define TVBPARSE_DEBUG_NOT_CHARS 0x01000000
#define TVBPARSE_DEBUG_STRING 0x00800000
#define TVBPARSE_DEBUG_CASESTRING 0x00400000
#define TVBPARSE_DEBUG_ONEOF 0x00200000
#define TVBPARSE_DEBUG_HASH 0x00100000
#define TVBPARSE_DEBUG_SEQ 0x00080000
#define TVBPARSE_DEBUG_SOME 0x00040000
#define TVBPARSE_DEBUG_UNTIL 0x00020000
/*#define TVBPARSE_DEBUG_ 0x00010000
#define TVBPARSE_DEBUG_ 0x00008000
#define TVBPARSE_DEBUG_ 0x00004000
#define TVBPARSE_DEBUG_ 0x00002000
#define TVBPARSE_DEBUG_ 0x00001000*/

#define TVBPARSE_DEBUG_TT 0x00000800
#define TVBPARSE_DEBUG_CB 0x00000400
#define TVBPARSE_DEBUG_GET 0x00000200
#define TVBPARSE_DEBUG_FIND 0x00000100
#define TVBPARSE_DEBUG_NEWTOK 0x00000080
#define TVBPARSE_DEBUG_IGNORE 0x00000040
#define TVBPARSE_DEBUG_PEEK 0x00000020
/*#define TVBPARSE_DEBUG_ 0x00000010
#define TVBPARSE_DEBUG_ 0x00000008
#define TVBPARSE_DEBUG_ 0x00000004
#define TVBPARSE_DEBUG_ 0x00000002
#define TVBPARSE_DEBUG_ 0x00000001*/

/*
#define TVBPARSE_DEBUG (TVBPARSE_DEBUG_SOME)
*/

static tvbparse_elem_t* new_tok(tvbparse_t* tt,
                                int id,
                                int offset,
                                int len,
                                const tvbparse_wanted_t* wanted) {
	tvbparse_elem_t* tok;
    
#ifdef TVBPARSE_DEBUG
    if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_NEWTOK) g_warning("new_tok: id=%i offset=%u len=%u",id,offset,len);
#endif

    tok = ep_alloc(sizeof(tvbparse_elem_t));

	tok->tvb = tt->tvb;
	tok->id = id;
	tok->offset = offset;
	tok->len = len;
	tok->data = NULL;
	tok->sub = NULL;
	tok->next = NULL;
	tok->wanted = wanted;
	tok->last = tok;
	
	return tok;
}

static int ignore(tvbparse_t* tt,int offset) {
    int len = 0;
    int consumed;
    tvbparse_elem_t* ignored = NULL;
    
    if (!tt->ignore) return 0;
    
#ifdef TVBPARSE_DEBUG
    if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_IGNORE) g_warning("ignore: enter");
#endif
    
    while ((consumed = tt->ignore->condition(tt,offset,tt->ignore,&ignored)) > 0) {
        len += consumed;
        offset += consumed;
#ifdef TVBPARSE_DEBUG
        if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_IGNORE) g_warning("ignore: consumed=%i",consumed);
#endif
        
    }

#ifdef TVBPARSE_DEBUG
    if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_IGNORE) g_warning("ignore: len=%i",len);
#endif
    
    return len;
}


static int cond_char (tvbparse_t* tt, int offset, const tvbparse_wanted_t * wanted, tvbparse_elem_t** tok) {
    gchar c,t;
    guint i;

#ifdef TVBPARSE_DEBUG
    if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_CHAR) g_warning("cond_char: control='%s'",wanted->control.str);
#endif
    
    if ( offset + 1 > tt->end_offset )
        return -1;
    
    t = (gchar) tvb_get_guint8(tt->tvb,offset);
    
    for(i = 0; (c = wanted->control.str[i]) && offset <= tt->end_offset; i++) {
        if ( c == t ) {
            *tok =  new_tok(tt,wanted->id,offset,1,wanted);
#ifdef TVBPARSE_DEBUG
            if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_CHAR) g_warning("cond_char: GOT: '%c'",c);
#endif
            return 1;
        }
    }
    
    return -1;
}

tvbparse_wanted_t* tvbparse_char(int id,
                                 const gchar* chr,
                                 const void* data,
                                 tvbparse_action_t before_cb,
                                 tvbparse_action_t after_cb) {
	tvbparse_wanted_t* w = g_malloc0(sizeof(tvbparse_wanted_t));
	
    w->condition = cond_char;
    w->id = id;
	w->control.str = chr;
	w->len = 1;
	w->data = data;
	w->before = before_cb;
	w->after = after_cb;
	
	return w;
}

static int cond_chars (tvbparse_t* tt, int offset, const tvbparse_wanted_t * wanted, tvbparse_elem_t** tok) {
    guint length = 0;
    int start = offset;
    int left = tt->end_offset - offset;
    
#ifdef TVBPARSE_DEBUG
    if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_CHARS) g_warning("cond_chars: control='%s'",wanted->control.str);
#endif
    
    if ( offset + (int)wanted->min > tt->end_offset )
        return -1;

    left = left < (int) wanted->max ? left :  (int) wanted->max;

    while( left > 0 ) {
        gchar t = (gchar) tvb_get_guint8(tt->tvb,offset++);
        gchar c;
        guint i = 0;
        
        while ( (c = wanted->control.str[i++]) ) {
            if (c == t) goto next_char;
        }
        
        break;
        
next_char:
        length++;
        left--;
    };
    
    if (length < wanted->min) {
        return  -1;
    } else {
        *tok = new_tok(tt,wanted->id,start,length,wanted);
#ifdef TVBPARSE_DEBUG
        if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_CHARS) g_warning("cond_chars: GOT len=%i",length);
#endif
        return length;			
    }
}

tvbparse_wanted_t* tvbparse_chars(int id,
								  guint min_len,
								  guint max_len,
								  const gchar* chr,
								  const void* data,
								  tvbparse_action_t before_cb,
								  tvbparse_action_t after_cb) {
	tvbparse_wanted_t* w = g_malloc0(sizeof(tvbparse_wanted_t));
	
    w->condition = cond_chars;
    w->id = id;
	w->control.str = chr;
	w->min = min_len ? min_len : 1;
	w->max = max_len ? max_len : G_MAXINT/2;
	w->data = data;
	w->before = before_cb;
	w->after = after_cb;
	
	return w;
}


static int cond_not_char(tvbparse_t* tt, int offset, const tvbparse_wanted_t * wanted, tvbparse_elem_t** tok) {
    gchar c, t;
    guint i;
    gboolean not_matched = FALSE;

#ifdef TVBPARSE_DEBUG
    if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_NOT_CHAR) g_warning("cond_not_char: control='%s'",wanted->control.str);
#endif
    
    if (! offset < tt->end_offset ) {
        return -1;
    }
    
    t = (gchar) tvb_get_guint8(tt->tvb,offset);
    
    for(i = 0; (c = wanted->control.str[i]); i++) {
        if ( c == t ) {
            not_matched = TRUE;
        }
    }
    
    if (not_matched) {
        return -1;
    } else {
        *tok =  new_tok(tt,wanted->id,offset,1,wanted);
#ifdef TVBPARSE_DEBUG
        if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_NOT_CHAR) g_warning("cond_not_char: GOT='%c'",t);
#endif        
        return 1;
    }
}

tvbparse_wanted_t* tvbparse_not_char(int id,
                                     const gchar* chr,
                                     const void* data,
                                     tvbparse_action_t before_cb,
                                     tvbparse_action_t after_cb) {
	tvbparse_wanted_t* w = g_malloc0(sizeof(tvbparse_wanted_t));
	
    w->condition = cond_not_char;
    w->id = id;
	w->control.str = chr;
	w->data = data;
	w->before = before_cb;
	w->after = after_cb;
	
	return w;
}

static int cond_not_chars(tvbparse_t* tt, int offset, const tvbparse_wanted_t * wanted, tvbparse_elem_t** tok) {
    guint length = 0;
    int left = tt->end_offset - offset;
    int start = offset;

#ifdef TVBPARSE_DEBUG
    if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_NOT_CHARS) g_warning("cond_not_chars: control='%s'",wanted->control.str);
#endif
    
    if ( offset + (int)wanted->min > tt->end_offset )
        return -1;

    if (left < (int)wanted->min)
        return -1;

    left = left <= (int)wanted->max ? left :  (int)wanted->max;

    while( left > 0 ) {
        gchar c;
        gchar t = (gchar) tvb_get_guint8(tt->tvb,offset);
        guint i = 0;
        
        while ( (c = wanted->control.str[i++]) ) {
            if (c == t) goto end_not_chars;
        }
        
        offset++;
        length++;
        left--;
    }
end_not_chars:
    
    if ( length < wanted->min ) {
        return -1;
    } else {
        *tok = new_tok(tt,wanted->id,start,length,wanted);
#ifdef TVBPARSE_DEBUG
        if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_NOT_CHARS) g_warning("cond_not_chars: GOT len=%i",length);
#endif        
        return length;
    }
}

tvbparse_wanted_t* tvbparse_not_chars(int id,
									  guint min_len,
									  guint max_len,
									  const gchar* chr,
									  const void* data,
									  tvbparse_action_t before_cb,
									  tvbparse_action_t after_cb){
	tvbparse_wanted_t* w = g_malloc0(sizeof(tvbparse_wanted_t));
	
    w->condition = cond_not_chars;
    w->id = id;
	w->control.str = chr;
	w->len = 0;
	w->min = min_len ? min_len : 1;
	w->max = max_len ? max_len : G_MAXINT/2;
	w->data = data;
	w->before = before_cb;
	w->after = after_cb;
	
	return w;
}


static int cond_string(tvbparse_t* tt, int offset, const tvbparse_wanted_t * wanted, tvbparse_elem_t** tok) {
    int len = wanted->len;
#ifdef TVBPARSE_DEBUG
    if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_STRING) g_warning("cond_string: control='%s'",wanted->control.str);
#endif
    
    if ( offset + wanted->len > tt->end_offset )
        return -1;

    if ( tvb_strneql(tt->tvb, offset, wanted->control.str, len) == 0 ) {
        *tok = new_tok(tt,wanted->id,offset,len,wanted);
#ifdef TVBPARSE_DEBUG
        if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_STRING) g_warning("cond_string: GOT len=%i",len);
#endif        
        return len;
    } else {
        return -1;
    }
}

tvbparse_wanted_t* tvbparse_string(int id,
								   const gchar* str,
								   const void* data,
								   tvbparse_action_t before_cb,
								   tvbparse_action_t after_cb) {
	tvbparse_wanted_t* w = g_malloc0(sizeof(tvbparse_wanted_t));
	
    w->condition = cond_string;
    w->id = id;
	w->control.str = str;
	w->len = strlen(str);
	w->data = data;
	w->before = before_cb;
	w->after = after_cb;
	
	return w;
}

static int cond_casestring(tvbparse_t* tt, int offset, const tvbparse_wanted_t * wanted, tvbparse_elem_t** tok) {
    int len = wanted->len;
#ifdef TVBPARSE_DEBUG
    if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_CASESTRING) g_warning("cond_casestring: control='%s'",wanted->control.str);
#endif
    
    if ( offset + len > tt->end_offset )
        return -1;

    if ( tvb_strncaseeql(tt->tvb, offset, wanted->control.str, len) == 0 ) {
        *tok = new_tok(tt,wanted->id,offset,len,wanted);
#ifdef TVBPARSE_DEBUG
        if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_CASESTRING) g_warning("cond_casestring: GOT len=%i",len);
#endif                
        return len;
    } else {
        *tok = NULL;
        return -1;
    }
}

tvbparse_wanted_t* tvbparse_casestring(int id,
                                       const gchar* str,
                                       const void* data,
                                       tvbparse_action_t before_cb,
                                       tvbparse_action_t after_cb) {
	tvbparse_wanted_t* w = g_malloc0(sizeof(tvbparse_wanted_t));
	
    w->condition = cond_casestring;
    w->id = id;
	w->control.str = str;
	w->len = strlen(str);
	w->data = data;
	w->before = before_cb;
	w->after = after_cb;
	
	return w;
}

static int cond_one_of(tvbparse_t* tt, int offset, const tvbparse_wanted_t * wanted, tvbparse_elem_t** tok) {
    guint i;
#ifdef TVBPARSE_DEBUG
    if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_ONEOF) g_warning("cond_one_of: START");
#endif
    
    if ( offset > tt->end_offset )
        return -1;

    for(i=0; i < wanted->control.elems->len; i++) {
        tvbparse_wanted_t* w = g_ptr_array_index(wanted->control.elems,i);
        tvbparse_elem_t* new = NULL;
        int curr_len;
        
        if ( offset + w->len > tt->end_offset )
            return -1;
        
        curr_len = w->condition(tt, offset, w,  &new);

        if (curr_len >= 0) {
            *tok = new_tok(tt, wanted->id, new->offset, new->len, wanted);
            (*tok)->sub = new;
#ifdef TVBPARSE_DEBUG
            if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_ONEOF) g_warning("cond_one_of: GOT len=%i",curr_len);
#endif
            return curr_len;			
        }
    }

    return -1;
}

tvbparse_wanted_t* tvbparse_set_oneof(int id,
                                      const void* data, 
                                      tvbparse_action_t before_cb,
                                      tvbparse_action_t after_cb,
                                      ...) {
	tvbparse_wanted_t* w = g_malloc0(sizeof(tvbparse_wanted_t));
	tvbparse_t* el;
	va_list ap;
	
    w->condition = cond_one_of;
    w->id = id;
	w->data = data;
	w->before = before_cb;
	w->after = after_cb;
	w->control.elems = g_ptr_array_new();
	
	va_start(ap,after_cb);
	
	while(( el = va_arg(ap,tvbparse_t*) )) {
		g_ptr_array_add(w->control.elems,el);
	};
	
	va_end(ap);
	
	return w;
}

static int cond_hash(tvbparse_t* tt, int offset, const tvbparse_wanted_t* wanted, tvbparse_elem_t** tok) {
    int key_len;
    gchar* key = NULL;
    tvbparse_elem_t* key_elem = NULL;
    tvbparse_wanted_t* value_wanted = NULL;
    int value_len;
    tvbparse_elem_t* value_elem = NULL;
    int tot_len;
    
#ifdef TVBPARSE_DEBUG
    if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_HASH) g_warning("cond_hash: START");
#endif
    
    tvbparse_elem_t* ret_tok;
    if ( offset > tt->end_offset )
        return -1;
    
    key_len = wanted->control.hash.key->condition(tt, offset, wanted->control.hash.key,  &key_elem);

    if (key_len < 0) 
        return -1;
    
    key = tvb_get_ephemeral_string(key_elem->tvb,key_elem->offset,key_elem->len);
#ifdef TVBPARSE_DEBUG
    if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_HASH) g_warning("cond_hash: got key='%s'",key);
#endif
    
    if ((value_wanted = g_hash_table_lookup(wanted->control.hash.table,key))) {
        value_len = value_wanted->condition(tt, offset + key_len, value_wanted,  &value_elem);
    } else if (wanted->control.hash.other) {
        value_len = wanted->control.hash.other->condition(tt, offset+key_len, wanted->control.hash.other,  &value_elem);
        if (value_len < 0) 
            return -1;
    } else {
        return -1;
    }

    tot_len = key_len + value_len;
    
    ret_tok = new_tok(tt, value_elem->id, offset, tot_len, wanted);
    ret_tok->sub = key_elem;
    ret_tok->sub->last->next = value_elem;
    
    *tok = ret_tok;
#ifdef TVBPARSE_DEBUG
    if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_HASH) g_warning("cond_hash: GOT len=%i",tot_len);
#endif
    
    return tot_len;
}

tvbparse_wanted_t* tvbparse_hashed(int id,
                                   const void* data, 
                                   tvbparse_action_t before_cb,
                                   tvbparse_action_t after_cb,
                                   tvbparse_wanted_t* key,
                                   tvbparse_wanted_t* other,
                                   ...) {
	tvbparse_wanted_t* w = g_malloc0(sizeof(tvbparse_wanted_t));
    gchar* name;
	tvbparse_wanted_t* el;
	va_list ap;
	
    w->condition = cond_hash;
    w->id = id;
	w->data = data;
	w->before = before_cb;
	w->after = after_cb;
	w->control.hash.table = g_hash_table_new(g_str_hash,g_str_equal);
	w->control.hash.key = key;
    w->control.hash.other = other;
    
	va_start(ap,other);
	
	while(( name = va_arg(ap,gchar*) )) {
        el = va_arg(ap,tvbparse_wanted_t*);
		g_hash_table_insert(w->control.hash.table,name,el);
	}
	
	va_end(ap);
	
	return w;
}

void tvbparse_hashed_add(tvbparse_wanted_t* w, ...) {
    tvbparse_wanted_t* el;
	va_list ap;
    gchar* name;
    
    va_start(ap,w);
	
	while (( name = va_arg(ap,gchar*) )) {
        el = va_arg(ap,tvbparse_wanted_t*);
		g_hash_table_insert(w->control.hash.table,name,el);
	}
	
	va_end(ap);
}    

static int cond_seq(tvbparse_t* tt, int offset, const tvbparse_wanted_t * wanted, tvbparse_elem_t** tok) {
    guint i;
    int len = 0;
    int start = offset;
    tvbparse_elem_t* ret_tok = NULL;
    
    if ( offset > tt->end_offset )
        return -1;
#ifdef TVBPARSE_DEBUG
    if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_SEQ) g_warning("cond_seq: START");
#endif
    
    for(i=0; i < wanted->control.elems->len; i++) {
        tvbparse_wanted_t* w = g_ptr_array_index(wanted->control.elems,i);
        tvbparse_elem_t* new = NULL;

        if ( offset + w->len > tt->end_offset )
            return -1;
        
        
        len = w->condition(tt, offset, w, &new);
        
        if (len >= 0) {
            if (ret_tok) {
                ret_tok->len = (new->offset - ret_tok->offset) + new->len;
                ret_tok->sub->last->next = new;
                ret_tok->sub->last = new;
            } else {
                ret_tok = new_tok(tt, wanted->id, new->offset, new->len, wanted);
                ret_tok->sub = new;
                new->last = new;
            }
        } else {
            return -1;
        }
        
        offset += len;
        offset += ignore(tt,offset);
    }

    *tok = ret_tok;
    
#ifdef TVBPARSE_DEBUG
    if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_SEQ) g_warning("cond_seq: GOT len=%i",offset - start);
#endif
    
    return offset - start;			
}


tvbparse_wanted_t* tvbparse_set_seq(int id,
                                    const void* data,
                                    tvbparse_action_t before_cb,
                                    tvbparse_action_t after_cb,
                                    ...) {
	tvbparse_wanted_t* w = g_malloc0(sizeof(tvbparse_wanted_t));
	tvbparse_wanted_t*  el = NULL;
	va_list ap;
	
    w->condition = cond_seq;
    w->id = id;
	w->data = data;
	w->before = before_cb;
	w->after = after_cb;
	w->control.elems = g_ptr_array_new();
	
	va_start(ap,after_cb);
	
	while(( el = va_arg(ap,tvbparse_wanted_t*) )) {
		g_ptr_array_add(w->control.elems,el);
	};
	
	va_end(ap);
	return w;
}

static int cond_some(tvbparse_t* tt, int offset, const tvbparse_wanted_t * wanted, tvbparse_elem_t** tok) {
    guint got_so_far = 0;
    int start = offset;
    tvbparse_elem_t* ret_tok = NULL;
#ifdef TVBPARSE_DEBUG
    if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_SOME) g_warning("cond_some: START");
#endif
    
    if ( offset > tt->end_offset )
        return -1;
    
    if ( wanted->min == 0 ) {
        ret_tok = new_tok(tt,wanted->id,tt->offset,0,wanted);
    }
    
    while (got_so_far < wanted->max) {
        tvbparse_elem_t* new = NULL;
        int consumed;
        
        if ( offset > tt->end_offset )
            return -1;
        
        consumed = wanted->control.subelem->condition(tt, offset, wanted->control.subelem, &new);
        
        if(consumed >= 0) {
            if (ret_tok) {
                ret_tok->len = (new->offset - ret_tok->offset) + new->len;
                
                if (ret_tok->sub) {
                    ret_tok->sub->last->next = new;
                    ret_tok->sub->last = new;
                } else {
                    ret_tok->sub = new;                            
                }
            } else {
                ret_tok = new_tok(tt, wanted->id, new->offset, new->len, wanted);
                ret_tok->sub = new;
            }
        } else {
            break;
        }
        
        offset += consumed;
        got_so_far++;
    }
    
#ifdef TVBPARSE_DEBUG
    if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_SOME) g_warning("cond_some: got num=%u",got_so_far);
#endif
    
    if(got_so_far < wanted->min) {
        return -1;
    }
    
    *tok = ret_tok;
#ifdef TVBPARSE_DEBUG
    if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_SOME) g_warning("cond_some: GOT len=%i",offset - start);
#endif
    return offset - start;
}

tvbparse_wanted_t* tvbparse_some(int id,
								 guint from,
								 guint to,
								 const void* data,
								 tvbparse_action_t before_cb,
								 tvbparse_action_t after_cb,
								 const tvbparse_wanted_t* el) {
	
	tvbparse_wanted_t* w = g_malloc0(sizeof(tvbparse_wanted_t));
	
	g_assert(from <= to);
	
    w->condition = cond_some;
    w->id = id;
	w->min = from;
	w->max = to;
	w->data = data;
	w->before = before_cb;
	w->after = after_cb;
	w->control.subelem = el;
	
	return w;
}


static int cond_until(tvbparse_t* tt, int offset, const tvbparse_wanted_t * wanted, tvbparse_elem_t** tok) {
    tvbparse_elem_t* new = NULL;
    int len = 0;
    int target_offset = offset;
#ifdef TVBPARSE_DEBUG
    if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_UNTIL) g_warning("cond_until: START");
#endif
    
    if ( offset + wanted->control.until.subelem->len > tt->end_offset )
        return -1;
    
    do {
        len = wanted->control.until.subelem->condition(tt, target_offset++, wanted->control.until.subelem,  &new);
    } while(len < 0  && target_offset+1 < tt->end_offset);
    
    if (len >= 0) {
        
        new->id = wanted->id;
        new->next = NULL;
        new->last = NULL;
        new->wanted = wanted;
        new->offset = offset;
        
        (*tok) = new;        
        
        switch (wanted->control.until.mode) {
            case TP_UNTIL_INCLUDE:
                new->len = target_offset - offset - 1 + len;
#ifdef TVBPARSE_DEBUG
                if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_UNTIL) g_warning("cond_until: GOT len=%i",target_offset - offset -1 + len);
#endif
                return target_offset - offset -1 + len;
            case TP_UNTIL_SPEND:
                new->len = target_offset - offset - 1;
#ifdef TVBPARSE_DEBUG
                if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_UNTIL) g_warning("cond_until: GOT len=%i",target_offset - offset -1 + len);
#endif
                return target_offset - offset - 1 + len;
            case TP_UNTIL_LEAVE:
                new->len = target_offset - offset - 1;
#ifdef TVBPARSE_DEBUG
                if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_UNTIL) g_warning("cond_until: GOT len=%i",target_offset - offset -1);
#endif                
                return target_offset - offset -1;
            default:
                DISSECTOR_ASSERT_NOT_REACHED();
                return -1;
        }
        
    } else {
        return -1;
    }
}

tvbparse_wanted_t* tvbparse_until(int id,
                                  const void* data,
                                  tvbparse_action_t before_cb,
                                  tvbparse_action_t after_cb,
                                  const tvbparse_wanted_t* el,
                                  until_mode_t until_mode) {
	tvbparse_wanted_t* w = g_malloc0(sizeof(tvbparse_wanted_t));
	
    w->condition = cond_until;
    w->control.until.mode = until_mode;
	w->control.until.subelem = el;
    w->id = id;
	w->data = data;
	w->before = before_cb;
	w->after = after_cb;
	
	return w;
}

static int cond_handle(tvbparse_t* tt, int offset, const tvbparse_wanted_t * wanted, tvbparse_elem_t** tok) {
    tvbparse_wanted_t* w = *(wanted->control.handle);
    int len = w->condition(tt, offset, w,  tok);

    if (len >= 0) {
        return len;
    } else {
        return -1;
    }
}

tvbparse_wanted_t* tvbparse_handle(tvbparse_wanted_t** handle) {
	tvbparse_wanted_t* w = g_malloc0(sizeof(tvbparse_wanted_t));
	
    w->condition = cond_handle;
	w->control.handle = handle;
	
	return w;
}

static int cond_end(tvbparse_t* tt, int offset, const tvbparse_wanted_t * wanted _U_, tvbparse_elem_t** tok) {
    if (offset == tt->end_offset) {
        *tok = new_tok(tt,wanted->id,offset,0,wanted);
        return 0;
    } else {
        return -1;
    }
}

tvbparse_wanted_t* tvbparse_end_of_buffer(int id,
                               const void* data,
                               tvbparse_action_t before_cb,
                               tvbparse_action_t after_cb) {
    tvbparse_wanted_t* w = g_malloc0(sizeof(tvbparse_wanted_t));
	
    w->id = id;
    w->condition = cond_end;
	w->after = after_cb;
    w->before = before_cb;
    w->data = data;
	return w;
    
}


#if 0
/* these extract binary values */

static int cond_ft(tvbparse_t* tt, int offset, const tvbparse_wanted_t * wanted, tvbparse_elem_t** tok) {
    guint len = 0;

    if ( offset + wanted->len > tt->end_offset )
        return -1;

    if (wanted->len) {
        return wanted->len;
    } else if (wanted->control.ftenum == FT_STRINGZ) {
            if (( len = tvb_find_guint8(tt->tvb,offset,tt->end_offset - offset,'\0') >= 0 )) {
                *tok = new_tok(tt,wanted->id,offset,len,wanted);
                return len;
            } else {
                return -1;
            }
    } else {
        return -2;
    }
}

gint ft_lens[] = {-1,-1,-1, 1, 2, 3, 4, 8, 1, 2, 3, 4, 8, 4, 8,-1,-1,-1, 0, -1, 6, -1, -1, 4, sizeof(struct e_in6_addr), -1, -1, -1, -1 };

tvbparse_wanted_t* tvbparse_ft(int id,
                               const void* data,
                               tvbparse_action_t before_cb,
                               tvbparse_action_t after_cb,
                               enum ftenum ftenum) {
    gint len = ft_lens[ftenum];
    
    if (len >= 0) {
        tvbparse_wanted_t* w = g_malloc0(sizeof(tvbparse_wanted_t));
        
        w->id = id;
        w->condition = cond_ft;
        w->len = len;
        w->control.ftenum = ftenum;
        w->after = after_cb;
        w->before = before_cb;
        w->data = data;
        
        return w;
    } else {
        g_assert(! "unsupported ftenum" );
        return NULL;
    }
}    

static int cond_ft_comp(tvbparse_t* tt, int offset, const tvbparse_wanted_t * wanted _U_, tvbparse_elem_t** tok) {
    void* l = wanted->control.number.extract(tt->tvb,offset);
    const void* r = &(wanted->control.number.value);
    
    if ( offset + wanted->len > tt->end_offset )
        return -1;

    if ( wanted->control.number.comp(&l,&r) ) {
        *tok = new_tok(tt,wanted->id,offset,wanted->len,wanted);
        return wanted->len;
    } else {
        return -1;
    }
}

static gboolean comp_gt_i(void* lp, const void* rp) { return ( *((gint64*)lp) > *((gint64*)rp) ); }
static gboolean comp_ge_i(void* lp, const void* rp) { return ( *((gint64*)lp) >= *((gint64*)rp) ); }
static gboolean comp_eq_i(void* lp, const void* rp) { return ( *((gint64*)lp) == *((gint64*)rp) ); }
static gboolean comp_ne_i(void* lp, const void* rp) { return ( *((gint64*)lp) != *((gint64*)rp) ); }
static gboolean comp_le_i(void* lp, const void* rp) { return ( *((gint64*)lp) <= *((gint64*)rp) ); }
static gboolean comp_lt_i(void* lp, const void* rp) { return ( *((gint64*)lp) < *((gint64*)rp) ); }

static gboolean comp_gt_u(void* lp, const void* rp) { return ( *((guint64*)lp) > *((guint64*)rp) ); }
static gboolean comp_ge_u(void* lp, const void* rp) { return ( *((guint64*)lp) >= *((guint64*)rp) ); }
static gboolean comp_eq_u(void* lp, const void* rp) { return ( *((guint64*)lp) == *((guint64*)rp) ); }
static gboolean comp_ne_u(void* lp, const void* rp) { return ( *((guint64*)lp) != *((guint64*)rp) ); }
static gboolean comp_le_u(void* lp, const void* rp) { return ( *((guint64*)lp) <= *((guint64*)rp) ); }
static gboolean comp_lt_u(void* lp, const void* rp) { return ( *((guint64*)lp) < *((guint64*)rp) ); }

static gboolean comp_gt_f(void* lp, const void* rp) { return ( *((gdouble*)lp) > *((gdouble*)rp) ); }
static gboolean comp_ge_f(void* lp, const void* rp) { return ( *((gdouble*)lp) >= *((gdouble*)rp) ); }
static gboolean comp_eq_f(void* lp, const void* rp) { return ( *((gdouble*)lp) == *((gdouble*)rp) ); }
static gboolean comp_ne_f(void* lp, const void* rp) { return ( *((gdouble*)lp) != *((gdouble*)rp) ); }
static gboolean comp_le_f(void* lp, const void* rp) { return ( *((gdouble*)lp) <= *((gdouble*)rp) ); }
static gboolean comp_lt_f(void* lp, const void* rp) { return ( *((gdouble*)lp) < *((gdouble*)rp) ); }

static void* extract_u8(tvbuff_t* tvb, guint offset) {
    guint64* p = ep_new(guint64);
    *p = tvb_get_guint8(tvb,offset);
    return p;
}

static void* extract_uns(tvbuff_t* tvb, guint offset) {
    guint64* p = ep_new(guint64);
    *p = tvb_get_ntohs(tvb,offset);
    return p;
}

static void* extract_un24(tvbuff_t* tvb, guint offset) {
    guint64* p = ep_new(guint64);
    *p = tvb_get_ntoh24(tvb,offset);
    return p;
}

static void* extract_unl(tvbuff_t* tvb, guint offset) {
    guint64* p = ep_new(guint64);
    *p = tvb_get_ntohl(tvb,offset);
    return p;
}

static void* extract_un64(tvbuff_t* tvb, guint offset) {
    guint64* p = ep_new(guint64);
    *p = tvb_get_ntoh64(tvb,offset);
    return p;
}

static void* extract_ules(tvbuff_t* tvb, guint offset) {
    guint64* p = ep_new(guint64);
    *p = tvb_get_letohs(tvb,offset);
    return p;
}

static void* extract_ule24(tvbuff_t* tvb, guint offset) {
    guint64* p = ep_new(guint64);
    *p = tvb_get_letoh24(tvb,offset);
    return p;
}

static void* extract_ulel(tvbuff_t* tvb, guint offset) {
    guint64* p = ep_new(guint64);
    *p = tvb_get_letohl(tvb,offset);
    return p;
}

static void* extract_ule64(tvbuff_t* tvb, guint offset) {
    guint64* p = ep_new(guint64);
    *p = tvb_get_letoh64(tvb,offset);
    return p;
}

static void* extract_ins(tvbuff_t* tvb, guint offset) {
    guint64* p = ep_new(guint64);
    *p = tvb_get_ntohs(tvb,offset);
    return p;
}

static void* extract_in24(tvbuff_t* tvb, guint offset) {
    guint64* p = ep_new(guint64);
    *p = tvb_get_ntoh24(tvb,offset);
    return p;
}

static void* extract_inl(tvbuff_t* tvb, guint offset) {
    guint64* p = ep_new(guint64);
    *p = tvb_get_ntohl(tvb,offset);
    return p;
}

static void* extract_in64(tvbuff_t* tvb, guint offset) {
    guint64* p = ep_new(guint64);
    *p = tvb_get_ntoh64(tvb,offset);
    return p;
}

static void* extract_iles(tvbuff_t* tvb, guint offset) {
    guint64* p = ep_new(guint64);
    *p = tvb_get_letohs(tvb,offset);
    return p;
}

static void* extract_ile24(tvbuff_t* tvb, guint offset) {
    guint64* p = ep_new(guint64);
    *p = tvb_get_letoh24(tvb,offset);
    return p;
}

static void* extract_ilel(tvbuff_t* tvb, guint offset) {
    guint64* p = ep_new(guint64);
    *p = tvb_get_letohl(tvb,offset);
    return p;
}

static void* extract_ile64(tvbuff_t* tvb, guint offset) {
    guint64* p = ep_new(guint64);
    *p = tvb_get_letoh64(tvb,offset);
    return p;
}

static void* extract_inf(tvbuff_t* tvb, guint offset) {
    gdouble* p = ep_new(gdouble);
    *p = tvb_get_ntohieee_float(tvb,offset);
    return p;
}

static void* extract_ind(tvbuff_t* tvb, guint offset) {
    gdouble* p = ep_new(gdouble);
    *p = tvb_get_ntohieee_double(tvb,offset);
    return p;
}

static void* extract_ilef(tvbuff_t* tvb, guint offset) {
    gdouble* p = ep_new(gdouble);
    *p = tvb_get_letohieee_float(tvb,offset);
    return p;
}

static void* extract_iled(tvbuff_t* tvb, guint offset) {
    gdouble* p = ep_new(gdouble);
    *p = tvb_get_letohieee_double(tvb,offset);
    return p;
}



static gboolean (*comps_u[])(void*, const void*) = {comp_gt_u,comp_ge_u,comp_eq_u,comp_ne_u,comp_le_u,comp_lt_u};
static gboolean (*comps_i[])(void*, const void*) = {comp_gt_i,comp_ge_i,comp_eq_i,comp_ne_i,comp_le_i,comp_lt_i};
static gboolean (*comps_f[])(void*, const void*) = {comp_gt_f,comp_ge_f,comp_eq_f,comp_ne_f,comp_le_f,comp_lt_f};

static gboolean (**comps[])(void*, const void*) = {comps_u,comps_i,comps_f};

static void* (*extract_n[])(tvbuff_t* tvb, guint offset)  =  {	NULL, NULL, NULL, extract_u8, extract_uns, extract_un24, extract_unl, extract_un64, extract_u8, extract_ins, extract_in24, extract_inl, extract_in64, extract_inf, extract_ind, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,NULL, NULL };
static void* (*extract_le[])(tvbuff_t* tvb, guint offset)  =  {	NULL, NULL, NULL, extract_u8, extract_ules, extract_ule24, extract_ulel, extract_ule64, extract_u8, extract_iles, extract_ile24, extract_ilel, extract_ile64, extract_ilef, extract_iled, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,NULL, NULL };

static void* (**extracts[])(tvbuff_t* tvb, guint offset) = { extract_n, extract_le};


tvbparse_wanted_t* tvbparse_ft_numcmp(int id,
                                     const void* data,
                                     tvbparse_action_t before_cb,
                                     tvbparse_action_t after_cb,
                                     enum ftenum ftenum,
                                     int little_endian,
                                     enum ft_cmp_op ft_cmp_op,
                                     ... ) {
	tvbparse_wanted_t* w = g_malloc0(sizeof(tvbparse_wanted_t));
    va_list ap;
    
    va_start(ap,ft_cmp_op);
    
    switch (ftenum) {
        case FT_UINT8:
        case FT_UINT16:
        case FT_UINT24:
        case FT_UINT32:
            w->control.number.comp = comps[0][ft_cmp_op];
            w->control.number.value.u = va_arg(ap,guint32);
            break;
        case FT_UINT64:
            w->control.number.comp = comps[0][ft_cmp_op];
            w->control.number.value.u = va_arg(ap,guint64);
            break;
        case FT_INT8:
        case FT_INT16:
        case FT_INT24:
        case FT_INT32:
            w->control.number.comp = comps[1][ft_cmp_op];
            w->control.number.value.i = va_arg(ap,gint32);
            break;            
        case FT_INT64:
            w->control.number.comp = comps[1][ft_cmp_op];
            w->control.number.value.i = va_arg(ap,gint64);
            break;            
        case FT_FLOAT:
        case FT_DOUBLE:
            w->control.number.comp = comps[1][ft_cmp_op];
            w->control.number.value.i = va_arg(ap,gdouble);
            break;
        default:
            g_assert(! "comparision unsupported");
    }

    w->control.number.extract = extracts[little_endian][ftenum];

    g_assert(w->control.number.extract && "extraction unsupported");
    
    w->id = id;
    w->condition = cond_ft_comp;
    w->after = after_cb;
    w->before = before_cb;
    w->data = data;

    return w;
}

#endif


tvbparse_wanted_t* tvbparse_quoted(int id,
								   const void* data,
								   tvbparse_action_t before_cb,
								   tvbparse_action_t after_cb,
								   char quote,
								   char esc) {
	
	gchar* esc_quot = g_strdup_printf("%c%c",esc,quote);
	gchar* quot = g_strdup_printf("%c",quote);
	tvbparse_wanted_t* want_quot = tvbparse_char(-1,quot,NULL,NULL,NULL);
	
	return tvbparse_set_oneof(id, data, before_cb, after_cb,
                              tvbparse_set_seq(-1, NULL, NULL, NULL,
                                               want_quot,
                                               tvbparse_set_seq(-1,NULL,NULL,NULL,
                                                                tvbparse_set_oneof(-1, NULL, NULL, NULL,
                                                                                   tvbparse_string(-1,esc_quot,NULL,NULL,NULL),
                                                                                   tvbparse_not_chars(-1,0,0,quot,NULL,NULL,NULL),
                                                                                   NULL),
                                                                NULL),
                                               want_quot,
                                               NULL),
                              tvbparse_set_seq(-1, NULL, NULL, NULL,
                                               want_quot,
                                               want_quot,
                                               NULL),
                              NULL);
}

void tvbparse_shrink_token_cb(void* tvbparse_data _U_,
							  const void* wanted_data _U_,
							  tvbparse_elem_t* tok) {
	tok->offset += 1;
	tok->len -= 2;
}

tvbparse_t* tvbparse_init(tvbuff_t* tvb,
						  int offset,
						  int len,
						  void* data,
						  const tvbparse_wanted_t* ignore) {
	tvbparse_t* tt = ep_alloc(sizeof(tvbparse_t));
	
#ifdef TVBPARSE_DEBUG
    if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_TT) g_warning("tvbparse_init: offset=%i len=%i",offset,len);
#endif                
    
    
	tt->tvb = tvb;
	tt->offset = offset;
	len = (len == -1) ? (int) tvb_length(tvb) : len;
    tt->end_offset = offset + len;
	tt->data = data;
	tt->ignore = ignore;
	return tt;
}

gboolean tvbparse_reset(tvbparse_t* tt,
						int offset,
						int len) {
	
#ifdef TVBPARSE_DEBUG
    if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_TT) g_warning("tvbparse_init: offset=%i len=%i",offset,len);
#endif                
    
	len = (len == -1) ? (int) tvb_length(tt->tvb) : len;
	
	if( tvb_length_remaining(tt->tvb, offset) >= len) {
		tt->offset = offset;
		tt->end_offset = offset + len;
		return TRUE;
	} else {
		return FALSE;
	}
}

guint tvbparse_curr_offset(tvbparse_t* tt) {
    return tt->offset;
}

static void execute_callbacks(tvbparse_t* tt, tvbparse_elem_t* curr) {
    ep_stack_t stack = ep_stack_new();
    
    while (curr) {
        if(curr->wanted->before) {
#ifdef TVBPARSE_DEBUG
            if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_CB) g_warning("execute_callbacks: BEFORE: id=%i offset=%i len=%i",curr->id,curr->offset,curr->len);
#endif                            
            curr->wanted->before(tt->data, curr->wanted->data, curr);
        }
        
        if(curr->sub) {
            ep_stack_push(stack,curr);
            curr = curr->sub;
            continue;
        } else {
#ifdef TVBPARSE_DEBUG
            if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_CB) g_warning("execute_callbacks: AFTER: id=%i offset=%i len=%i",curr->id,curr->offset,curr->len);
#endif                            
            if(curr->wanted->after) curr->wanted->after(tt->data, curr->wanted->data, curr);
        }
        
        curr = curr->next;
        
        while( !curr && ep_stack_peek(stack) ) {
            curr = ep_stack_pop(stack);
#ifdef TVBPARSE_DEBUG
            if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_CB) g_warning("execute_callbacks: AFTER: id=%i offset=%i len=%i",curr->id,curr->offset,curr->len);
#endif                            
            if( curr->wanted->after ) curr->wanted->after(tt->data, curr->wanted->data, curr);
            curr = curr->next;
        }
    }

}

gboolean tvbparse_peek(tvbparse_t* tt,
                              const tvbparse_wanted_t* wanted) {
	tvbparse_elem_t* tok = NULL;
	int consumed;
    int offset = tt->offset;
    
#ifdef TVBPARSE_DEBUG
    if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_PEEK) g_warning("tvbparse_peek: ENTER offset=%i",offset);
#endif                            
    
    offset += ignore(tt,offset);
    
#ifdef TVBPARSE_DEBUG
    if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_PEEK) g_warning("tvbparse_peek: after ignore offset=%i",offset);
#endif                            
    
    consumed = wanted->condition(tt,offset,wanted,&tok);
    
    if (consumed >= 0) {
#ifdef TVBPARSE_DEBUG
        if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_PEEK) g_warning("tvbparse_peek: GOT len=%i",consumed);
#endif                            
        return TRUE;
    } else {
#ifdef TVBPARSE_DEBUG
        if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_PEEK) g_warning("tvbparse_peek: NOT GOT");
#endif                            
        return FALSE;
    }
    
}

tvbparse_elem_t* tvbparse_get(tvbparse_t* tt,
                              const tvbparse_wanted_t* wanted) {
	tvbparse_elem_t* tok = NULL;
	int consumed;
    int offset = tt->offset;
    
#ifdef TVBPARSE_DEBUG
    if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_GET) g_warning("tvbparse_get: ENTER offset=%i",offset);
#endif                            
    
    offset += ignore(tt,offset);

#ifdef TVBPARSE_DEBUG
    if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_GET) g_warning("tvbparse_get: after ignore offset=%i",offset);
#endif                            
    
    consumed = wanted->condition(tt,offset,wanted,&tok);

    if (consumed >= 0) {
#ifdef TVBPARSE_DEBUG
        if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_GET) g_warning("tvbparse_get: GOT len=%i",consumed);
#endif                            
        execute_callbacks(tt,tok);
        tt->offset = offset + consumed;
#ifdef TVBPARSE_DEBUG
        if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_GET) g_warning("tvbparse_get: DONE offset=%i", tt->offset);
#endif                            
        return tok; 
    } else {
        return NULL;
    }
            
}


tvbparse_elem_t* tvbparse_find(tvbparse_t* tt, const tvbparse_wanted_t* wanted) {
	tvbparse_elem_t* tok = NULL;
    int len = 0;
    int offset = tt->offset;
    int target_offset = offset -1;

#ifdef TVBPARSE_DEBUG
    if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_FIND) g_warning("tvbparse_get: ENTER offset=%i", tt->offset);
#endif                            
    
    do {
        len = wanted->condition(tt, target_offset+1, wanted,  &tok);
    } while(len < 0  && ++target_offset < tt->end_offset);

    if (len >= 0) {
#ifdef TVBPARSE_DEBUG
        if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_FIND) g_warning("tvbparse_get: FOUND offset=%i len=%i", target_offset,len);
#endif                            
                execute_callbacks(tt,tok);
        tt->offset = target_offset + len;

#ifdef TVBPARSE_DEBUG
        if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_FIND) g_warning("tvbparse_get: DONE offset=%i", tt->offset);
#endif                            
        return tok;
    } else {
#ifdef TVBPARSE_DEBUG
        if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_FIND) g_warning("tvbparse_get: NOT FOUND");
#endif                            
        return NULL;
    }
}

struct _elem_tree_stack_frame {
    proto_tree* tree;
    tvbparse_elem_t* elem;
};

void tvbparse_tree_add_elem(proto_tree* tree, tvbparse_elem_t* curr) {
    ep_stack_t stack = ep_stack_new();
    struct _elem_tree_stack_frame* frame = ep_alloc(sizeof(struct _elem_tree_stack_frame));
    proto_item* pi;
    frame->tree = tree;
    frame->elem = curr;
    
    while (curr) {
        pi = proto_tree_add_text(frame->tree,curr->tvb,curr->offset,curr->len,"%s",tvb_format_text(curr->tvb,curr->offset,curr->len));
        
        if(curr->sub) {
            frame->elem = curr;
            ep_stack_push(stack,frame);
            frame = ep_alloc(sizeof(struct _elem_tree_stack_frame));
            frame->tree = proto_item_add_subtree(pi,0);
            curr = curr->sub;
            continue;
        }
        
        curr = curr->next;
        
        while( !curr && ep_stack_peek(stack) ) {
            frame = ep_stack_pop(stack);
            curr = frame->elem->next;
        }
        
    }
}