Bug Summary

File:builds/wireshark/wireshark/epan/dissectors/dcerpc/idl2wrs.c
Warning:line 3039, column 6
File position of the stream might be 'indeterminate' after a failed operation. Can cause undefined behavior

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name idl2wrs.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -pic-is-pie -fno-delete-null-pointer-checks -mframe-pointer=all -relaxed-aliasing -fmath-errno -ffp-contract=on -fno-rounding-math -ffloat16-excess-precision=fast -fbfloat16-excess-precision=fast -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/builds/wireshark/wireshark/build -fcoverage-compilation-dir=/builds/wireshark/wireshark/build -resource-dir /usr/lib/llvm-22/lib/clang/22 -isystem /usr/include/glib-2.0 -isystem /usr/lib/x86_64-linux-gnu/glib-2.0/include -D CARES_NO_DEPRECATED -D G_DISABLE_DEPRECATED -D G_DISABLE_SINGLE_INCLUDES -D WS_DEBUG -D WS_DEBUG_UTF_8 -I /builds/wireshark/wireshark/build -I /builds/wireshark/wireshark -I /builds/wireshark/wireshark/include -D _GLIBCXX_ASSERTIONS -internal-isystem /usr/lib/llvm-22/lib/clang/22/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/builds/wireshark/wireshark/= -fmacro-prefix-map=/builds/wireshark/wireshark/build/= -fmacro-prefix-map=../= -Wno-format-nonliteral -std=gnu17 -ferror-limit 19 -fvisibility=hidden -fwrapv -fwrapv-pointer -fstrict-flex-arrays=3 -stack-protector 2 -fstack-clash-protection -fcf-protection=full -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fexceptions -fcolor-diagnostics -analyzer-output=html -faddrsig -fdwarf2-cfi-asm -o /builds/wireshark/wireshark/sbout/2026-11-25-100324-3661-1 -x c /builds/wireshark/wireshark/epan/dissectors/dcerpc/idl2wrs.c
1/* idl2wrs.c
2 * DCE RPC IDL to Wireshark dissector compiler
3 *
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <[email protected]>
6 * Copyright 1998 Gerald Combs
7 *
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
10
11/*
12TODO
13 check that every cnf defined type,hffield,rename,... has been referenced
14 at least once and if not, abort with an error
15
16 need to distinguish between NTTIME (absolute time) and relative time
17
18 prune_xxx should only act inside of '[' ']'
19
20 add support for bool8,16,32,64 with tfs strings
21
22 add the remaining array type (uvarray)
23
24 add code to verify that union tag length is correct
25*/
26/* List of built in types :
27 WERROR A 32 bit integer holding a DCE/NT status code.
28
29 uint8 A 8 bit integer
30 int8
31
32 uint16 A 16 bit integer
33 int16
34
35 uint32 A 32 bit integer
36 int32
37
38 uint64 A 64 bit integer
39
40 udlong A 64 bit integer aligned on 4 byte boundary
41 dlong
42
43 time_t A 32 bit integer holding a unix style time_t
44
45 NTTIME_hyper A 64 bit integer representing a NTTIME
46 NTTIME_1sec
47
48 unistr A conformant and varying unicode string
49
50 ascstr A conformant and varying ascii string
51
52
53 SID A SID structure.
54
55 uuid_t A 16 byte FT_GUID blob.
56 GUID
57
58
59 policy_handle
60 bool8
61 uuid_t
62 policy_handle
63 NTTIME
64*/
65
66/* All field dissectors that call a normal type
67 (i.e. not a pointer, not an array)
68 has a local variable unsigned param declared which is passed on to the
69 type dissector.
70 The default value is 0 but the PARAM_VALUE conformance tag can be used to
71 change it.
72 This is only meaningful if the called type dissector actually does anything
73 with this parameter.
74*/
75
76#define _GNU_SOURCE
77
78#include <glib.h>
79#include <glib/gstdio.h>
80#include <string.h>
81#include <stdlib.h>
82#include <stdarg.h>
83#include <wsutil/strtoi.h>
84
85#undef IDL2WRS_DEBUG
86
87#define DISSECTORNAME_MAXLEN256 256
88
89#define BASE_BUFFER_SIZE256 256
90
91static FILE *tfh, *eth_code, *eth_hdr, *eth_hf, *eth_hfarr, *eth_ett, *eth_ettarr, *eth_ft, *eth_handoff;
92static char *uuid=NULL((void*)0);
93static char *version=NULL((void*)0);
94static const char *pointer_default=NULL((void*)0);
95static char *ifname=NULL((void*)0);
96static char hf_status[BASE_BUFFER_SIZE256];
97static int linepos;
98static char line[4 * BASE_BUFFER_SIZE256];
99
100static void FPRINTF(FILE *fh, const char *format, ...)
101{
102 va_list args;
103
104#ifdef IDL2WRS_DEBUG
105 va_start(args, format)__builtin_va_start(args, format);
106 vfprintf (stderrstderr, format, args);
107 va_end(args)__builtin_va_end(args);
108#endif
109 if (fh) {
110 va_start(args, format)__builtin_va_start(args, format);
111 vfprintf (fh, format, args);
112 va_end(args)__builtin_va_end(args);
113 }
114}
115
116typedef struct _pointer_item_t {
117 struct _pointer_item_t *next;
118 const char *type;
119} pointer_item_t;
120
121#define BI_CASE0x00000001 0x00000001
122#define BI_CASE_DEFAULT0x00000002 0x00000002
123#define BI_IN0x00000004 0x00000004
124#define BI_OUT0x00000008 0x00000008
125#define BI_SIZE_IS0x00000010 0x00000010
126#define BI_LENGTH_IS0x00000020 0x00000020
127#define BI_POINTER0x00000040 0x00000040
128#define BI_BITMAP80x00000100 0x00000100
129#define BI_BITMAP320x00000200 0x00000200
130#define BI_SWITCH_TYPE0x00000400 0x00000400
131typedef struct _bracket_item_t {
132 unsigned int flags;
133 const char *case_name;
134 pointer_item_t *pointer_list;
135 int union_tag_size;
136} bracket_item_t;
137
138typedef struct _no_emit_item_t {
139 struct _no_emit_item_t *next;
140 char *name;
141} no_emit_item_t;
142static no_emit_item_t *no_emit_list=NULL((void*)0);
143
144typedef struct _hf_rename_item_t {
145 struct _hf_rename_item_t *next;
146 int refcount; /* number of times this rename has been referenced */
147 char *old_name;
148 char *new_name;
149} hf_rename_item_t;
150static hf_rename_item_t *hf_rename_list=NULL((void*)0);
151
152typedef struct _enum_list_t {
153 struct _enum_list_t *next;
154 char *name;
155 int val;
156} enum_list_t;
157
158typedef struct _token_item_t {
159 struct _token_item_t *next;
160 char *str;
161} token_item_t;
162static token_item_t *token_list=NULL((void*)0);
163static token_item_t *last_token_item=NULL((void*)0);
164
165typedef struct _type_item_t {
166 struct _type_item_t *next;
167 char *name;
168 char *dissector;
169 char *ft_type;
170 char *base_type;
171 char *mask;
172 char *vals;
173 int alignment;
174} type_item_t;
175static type_item_t *type_list=NULL((void*)0);
176
177typedef struct _union_tag_size_item_t {
178 struct _union_tag_size_item_t *next;
179 char *name;
180 int size;
181} union_tag_size_item_t;
182static union_tag_size_item_t *union_tag_size_list=NULL((void*)0);
183
184typedef struct _hf_field_item_t {
185 struct _hf_field_item_t *next;
186 char *name;
187 char *ft_type;
188} hf_field_item_t;
189static hf_field_item_t *hf_field_list=NULL((void*)0);
190
191typedef struct _dissector_param_value_t {
192 struct _dissector_param_value_t *next;
193 char *name;
194 char *value;
195} dissector_param_value_t;
196static dissector_param_value_t *dissector_param_list=NULL((void*)0);
197
198static type_item_t *find_type(char *name);
199static int Exit(int code);
200
201static void
202register_dissector_param_value(const char *name, const char *value)
203{
204 dissector_param_value_t *dpv;
205 dpv=g_new0(dissector_param_value_t, 1)((dissector_param_value_t *) g_malloc0_n ((1), sizeof (dissector_param_value_t
)))
;
206 if (!dpv) {
207 FPRINTF(stderrstderr, "Can't allocate memory. Exit.\n");
208 exit(10);
209 }
210 dpv->next=dissector_param_list;
211 dissector_param_list=dpv;
212 dpv->name=g_strdup(name)g_strdup_inline (name);
213 dpv->value=g_strdup(value)g_strdup_inline (value);
214}
215
216static const char *
217find_dissector_param_value(char *name)
218{
219 dissector_param_value_t *dpv;
220 for(dpv=dissector_param_list;dpv;dpv=dpv->next){
221 if(!g_strcmp0(name,dpv->name)){
222 return dpv->value;
223 }
224 }
225 return "0";
226}
227
228static pointer_item_t *
229prepend_pointer_list(pointer_item_t *ptrs, int num_pointers)
230{
231 pointer_item_t *pi;
232
233 pi=ptrs;
234 while(pi){
235 if(num_pointers)num_pointers--;
236 pi=pi->next;
237 }
238 pi=ptrs;
239 while(num_pointers--){
240 pi=g_new0(pointer_item_t, 1)((pointer_item_t *) g_malloc0_n ((1), sizeof (pointer_item_t)
))
;
241 if (!pi) {
242 FPRINTF(stderrstderr, "Can't allocate memory. Exit.\n");
243 exit(10);
244 }
245 pi->next=ptrs;
246 pi->type=pointer_default;
247 ptrs=pi;
248 }
249 ptrs=pi;
250
251 return ptrs;
252}
253
254static const char *
255ptr_to_define(const char *pointer_type)
256{
257 if(!g_strcmp0(pointer_type, "unique")){
258 return "NDR_POINTER_UNIQUE";
259 } else if(!g_strcmp0(pointer_type, "ref")){
260 return "NDR_POINTER_REF";
261 } else if(!g_strcmp0(pointer_type, "ptr")){
262 return "NDR_POINTER_PTR";
263 }
264
265 FPRINTF(stderrstderr, "prt_to_define, weirdo pointer :%s\n", pointer_type);
266 exit(10);
267}
268
269static int
270get_union_tag_size(char *name)
271{
272 union_tag_size_item_t *utsi;
273 for(utsi=union_tag_size_list;utsi;utsi=utsi->next){
274 if(!g_strcmp0(name, utsi->name)){
275 return utsi->size;
276 }
277 }
278 FPRINTF(stderrstderr, "ERROR: size of tag for union:%s is not known\n", name);
279 FPRINTF(stderrstderr, " use the UNION_TAG_SIZE directive to specify it in the conformance file\n");
280 exit(10);
281}
282
283
284/* this function will add an entry to the hf_rename list */
285static void
286register_hf_rename(const char *old_name, const char *new_name)
287{
288 hf_rename_item_t *new_item;
289 new_item=g_new0(hf_rename_item_t, 1)((hf_rename_item_t *) g_malloc0_n ((1), sizeof (hf_rename_item_t
)))
;
290 if (!new_item) {
291 FPRINTF(stderrstderr, "Can't allocate memory. Exit.\n");
292 exit(10);
293 }
294 new_item->next=hf_rename_list;
295 hf_rename_list=new_item;
296 new_item->refcount=0;
297 new_item->old_name=g_strdup(old_name)g_strdup_inline (old_name);
298 new_item->new_name=g_strdup(new_name)g_strdup_inline (new_name);
299}
300
301/* this function checks that all hf_rename fields have actually been referenced
302 if not out conformance file is stale
303*/
304static void
305check_hf_rename_refcount(void)
306{
307 hf_rename_item_t *hri;
308
309 /* don't generate code for renamed hf fields just return the new name*/
310 for(hri=hf_rename_list;hri;hri=hri->next){
311 if(!hri->refcount){
312 FPRINTF(stderrstderr, "ERROR: the hf_rename field:%s was never referenced. it is likely the conformance file is stale\n", hri->old_name);
313 exit(10);
314 }
315 }
316}
317
318static hf_field_item_t *
319find_hf_field(char *name)
320{
321 hf_field_item_t *hfi;
322
323 for(hfi=hf_field_list;hfi;hfi=hfi->next){
324 if(!g_strcmp0(hfi->name, name)){
325 break;
326 }
327 }
328 if (!hfi) {
329 FPRINTF(stderrstderr, "find_hf_field: unknown hf_field:%s\n",name);
330 Exit(10);
331 }
332
333 return hfi;
334}
335
336
337/* this function will create the code required for a hf field.
338 it MIGHT rename the field so a user MUST use the name returned
339 from this function.
340 for fields that are to be renamed no code is generated
341*/
342static const char *
343register_hf_field(const char *hf_name, const char *title, const char *filter_name, const char *ft_type, const char *base_type, const char *valsstring, const char *mask, const char *blurb)
344{
345 hf_field_item_t *hfi;
346 hf_rename_item_t *hri;
347
348 /* don't generate code for renamed hf fields just return the new name*/
349 for(hri=hf_rename_list;hri;hri=hri->next){
350 if(!strncmp(hf_name, hri->old_name, strlen(hf_name))){
351 hfi=find_hf_field(hri->new_name);
352 if(g_strcmp0(ft_type, hfi->ft_type)){
353 FPRINTF(stderrstderr, "register_hf_field: hf_fields %s and %s have different types %s %s\n",hf_name,hfi->name,ft_type,hfi->ft_type);
354 Exit(10);
355 }
356 hri->refcount++;
357 return hri->new_name;
358 }
359 }
360
361 hfi=g_new0(hf_field_item_t, 1)((hf_field_item_t *) g_malloc0_n ((1), sizeof (hf_field_item_t
)))
;
362 if (!hfi) {
363 FPRINTF(stderrstderr, "Can't allocate memory. Exit.\n");
364 exit(10);
365 }
366 hfi->next=hf_field_list;
367 hf_field_list=hfi;
368 hfi->name=g_strdup(hf_name)g_strdup_inline (hf_name);
369 hfi->ft_type=g_strdup(ft_type)g_strdup_inline (ft_type);
370
371 FPRINTF(eth_hf, "static int %s;\n", hf_name);
372 FPRINTF(eth_hfarr, " { &%s,\n", hf_name);
373 FPRINTF(eth_hfarr, " { \"%s\", \"%s\", %s, %s,\n", title, filter_name, ft_type, base_type);
374 FPRINTF(eth_hfarr, " %s, %s,\n", valsstring, mask);
375 if (strlen(blurb) > 0)
376 FPRINTF(eth_hfarr, " \"%s\", HFILL }},\n", blurb);
377 else
378 FPRINTF(eth_hfarr, " NULL, HFILL }},\n");
379 FPRINTF(eth_hfarr, "\n");
380
381 return hf_name;
382}
383
384/* this function will parse the no emit list and decide whether code should
385 be generated for this dissector or if we should only register the type.
386*/
387static int
388check_if_to_emit(char *name)
389{
390 no_emit_item_t *nel;
391
392 for(nel=no_emit_list;nel;nel=nel->next){
393 if(!g_strcmp0(name, nel->name)){
394 FPRINTF(NULL((void*)0), "SKIPPED emitting of %s\n",name);
395 return 0;
396 }
397 }
398 return 1;
399}
400
401#if 0
402static void
403prune_keywords(char *name)
404{
405 token_item_t *ti;
406
407 for(ti=token_list;ti;ti=ti->next){
408 if(!ti->next){
409 break;
410 }
411 if(!g_strcmp0(ti->next->str, name)){
412 if(!g_strcmp0(ti->next->next->str, ",")){
413 ti->next=ti->next->next->next;
414 } else {
415 ti->next=ti->next->next;
416 }
417 }
418 }
419}
420#endif
421
422static void
423rename_tokens(const char *old_name, const char *new_name)
424{
425 token_item_t *ti;
426
427 for(ti=token_list;ti;ti=ti->next){
428 if(!g_strcmp0(ti->str, old_name)){
429 ti->str=g_strdup(new_name)g_strdup_inline (new_name);
430 }
431 }
432}
433
434static void
435prune_keyword_parameters(const char *name)
436{
437 token_item_t *ti, *tmpti;
438
439 for(ti=token_list;ti;ti=ti->next){
440 if(!g_strcmp0(ti->str, name)){
441 if(!g_strcmp0(ti->next->str, "(")){
442 tmpti=ti;
443 while(1){
444 if(!g_strcmp0(tmpti->str, ")")){
445 ti->next=tmpti->next;
446 break;
447 }
448 tmpti=tmpti->next;
449 }
450 }
451 }
452 }
453}
454
455/* this function will parse a bracket item
456 [ ... ]
457 it will return the token of the next item following the ']'
458*/
459static token_item_t *
460parsebrackets(token_item_t *ti, bracket_item_t **bracket){
461 bracket_item_t *br;
462 const type_item_t *type_item;
463
464 if(g_strcmp0(ti->str, "[")){
465 FPRINTF(stderrstderr, "ERROR: parsebrackets first token is not '['\n");
466 Exit(10);
467 }
468 ti=ti->next;
469
470 br=g_new0(bracket_item_t, 1)((bracket_item_t *) g_malloc0_n ((1), sizeof (bracket_item_t)
))
;
471 if (!br) {
472 FPRINTF(stderrstderr, "Can't allocate memory. Exit.\n");
473 exit(10);
474 }
475 *bracket=br;
476 br->flags=0;
477 br->case_name=NULL((void*)0);
478 br->pointer_list=NULL((void*)0);
479
480 while(ti){
481 if( !g_strcmp0(ti->str, "{")
482 ||!g_strcmp0(ti->str, "}")){
483 FPRINTF(stderrstderr, "ERROR: parsebrackets '{' '}' inside bracket item\n");
484 Exit(10);
485 }
486
487 if(!g_strcmp0(ti->str, "[")){
488 FPRINTF(stderrstderr, "ERROR: parsebrackets '[' inside bracket item\n");
489 Exit(10);
490 }
491
492 /* finished */
493 if(!g_strcmp0(ti->str, "]")){
494 /* check for [ ... ] [ ...] */
495 ti=ti->next;
496
497 if(!g_strcmp0(ti->str, "[")){
498 ti=ti->next;
499 continue;
500 }
501 return ti;
502 }
503
504 /* just ignore all ',' */
505 if(!g_strcmp0(ti->str, ",")){
506 ti=ti->next;
507 continue;
508 }
509
510 /* case '(' tag ')' */
511 if(!g_strcmp0(ti->str, "case")){
512 br->flags|=BI_CASE0x00000001;
513 ti=ti->next;
514
515 if(g_strcmp0(ti->str, "(")){
516 FPRINTF(stderrstderr, "ERROR: parsebrackets case not followed by '('\n");
517 Exit(10);
518 }
519 ti=ti->next;
520
521 /* name */
522 br->case_name=ti->str;
523 ti=ti->next;
524
525 if(g_strcmp0(ti->str, ")")){
526 FPRINTF(stderrstderr, "ERROR: parsebrackets case does not end with ')'\n");
527 Exit(10);
528 }
529 ti=ti->next;
530 continue;
531 }
532
533 /* default */
534 if(!g_strcmp0(ti->str, "default")){
535 br->flags|=BI_CASE0x00000001;
536 br->flags|=BI_CASE_DEFAULT0x00000002;
537 br->case_name="default";
538 ti=ti->next;
539 continue;
540 }
541
542
543 /* in */
544 if(!g_strcmp0(ti->str, "in")){
545 br->flags|=BI_IN0x00000004;
546 ti=ti->next;
547 continue;
548 }
549
550 /* out */
551 if(!g_strcmp0(ti->str, "out")){
552 br->flags|=BI_OUT0x00000008;
553 ti=ti->next;
554 continue;
555 }
556
557 /* public : we don't care about this one */
558 if(!g_strcmp0(ti->str, "public")){
559 ti=ti->next;
560 continue;
561 }
562
563 /* gensize : we don't care about this one */
564 if(!g_strcmp0(ti->str, "gensize")){
565 ti=ti->next;
566 continue;
567 }
568
569 /* switch_is */
570 if(!g_strcmp0(ti->str, "switch_is")){
571 FPRINTF(stderrstderr, "WARNING: parsebrackets can not handle switch_is properly yet so we can not verify the tag size\n");
572 while(ti){
573 if(!g_strcmp0(ti->str, ")")){
574 ti=ti->next;
575 break;
576 }
577 ti=ti->next;
578 }
579 continue;
580 }
581
582 /* switch_is */
583 if(!g_strcmp0(ti->str, "subcontext")){
584 while(ti){
585 if(!g_strcmp0(ti->str, ")")){
586 ti=ti->next;
587 break;
588 }
589 ti=ti->next;
590 }
591 continue;
592 }
593
594 /* value we don't care about this one so just skip it */
595 if(!g_strcmp0(ti->str, "value")){
596 int level;
597 ti=ti->next;
598 if( g_strcmp0(ti->str, "(") ){
599 FPRINTF(stderrstderr, "WARNING: parsebrackets value was not followed by '('\n");
600 Exit(10);
601 }
602 level=0;
603 while(ti){
604 if(!g_strcmp0(ti->str, "(")){
605 ti=ti->next;
606 level++;
607 continue;
608 }
609 if(!g_strcmp0(ti->str, ")")){
610 ti=ti->next;
611 level--;
612 if(level){
613 continue;
614 }
615 break;
616 }
617 ti=ti->next;
618 }
619 continue;
620 }
621
622 /* range we don't care about this one so just skip it */
623 if(!g_strcmp0(ti->str, "range")){
624 int level;
625 ti=ti->next;
626 if( g_strcmp0(ti->str, "(") ){
627 FPRINTF(stderrstderr, "WARNING: parsebrackets range was not followed by '('\n");
628 Exit(10);
629 }
630 level=0;
631 while(ti){
632 if(!g_strcmp0(ti->str, "(")){
633 ti=ti->next;
634 level++;
635 continue;
636 }
637 if(!g_strcmp0(ti->str, ")")){
638 ti=ti->next;
639 level--;
640 if(level){
641 continue;
642 }
643 break;
644 }
645 ti=ti->next;
646 }
647 continue;
648 }
649
650 /* flag we don't care about this one so just skip it */
651 if(!g_strcmp0(ti->str, "flag")){
652 int level;
653 ti=ti->next;
654 if( g_strcmp0(ti->str, "(") ){
655 FPRINTF(stderrstderr, "WARNING: parsebrackets flag was not followed by '('\n");
656 Exit(10);
657 }
658 level=0;
659 while(ti){
660 if(!g_strcmp0(ti->str, "(")){
661 ti=ti->next;
662 level++;
663 continue;
664 }
665 if(!g_strcmp0(ti->str, ")")){
666 ti=ti->next;
667 level--;
668 if(level){
669 continue;
670 }
671 break;
672 }
673 ti=ti->next;
674 }
675 continue;
676 }
677
678 /* switch_type */
679 if(!g_strcmp0(ti->str, "switch_type")){
680 br->flags|=BI_SWITCH_TYPE0x00000400;
681 ti=ti->next;
682
683 if(g_strcmp0(ti->str, "(")){
684 FPRINTF(stderrstderr, "WARNING: parsebrackets switch_type was not followed by '('\n");
685 Exit(10);
686 }
687 ti=ti->next;
688
689 type_item=find_type(ti->str);
690 if(!type_item){
691 FPRINTF(stderrstderr, "ERROR : parsebrackets switch_type unknown type %s\n",ti->str);
692 Exit(10);
693 }
694 br->union_tag_size=type_item->alignment;
695 ti=ti->next;
696
697 if(g_strcmp0(ti->str, ")")){
698 FPRINTF(stderrstderr, "WARNING: parsebrackets switch_type did not end with ')'\n");
699 Exit(10);
700 }
701 ti=ti->next;
702
703 continue;
704 }
705
706 /* size_is */
707 if(!g_strcmp0(ti->str, "size_is")){
708 br->flags|=BI_SIZE_IS0x00000010;
709 ti=ti->next;
710 continue;
711 }
712
713 /* length_is */
714 if(!g_strcmp0(ti->str, "length_is")){
715 br->flags|=BI_LENGTH_IS0x00000020;
716 ti=ti->next;
717 continue;
718 }
719
720 /* bitmap8bit */
721 if(!g_strcmp0(ti->str, "bitmap8bit")){
722 br->flags|=BI_BITMAP80x00000100;
723 ti=ti->next;
724 continue;
725 }
726
727 /* bitmap32bit */
728 if(!g_strcmp0(ti->str, "bitmap32bit")){
729 br->flags|=BI_BITMAP320x00000200;
730 ti=ti->next;
731 continue;
732 }
733
734 /* ref, unique or ptr */
735 if(!g_strcmp0(ti->str, "ref")
736 || !g_strcmp0(ti->str, "unique")
737 || !g_strcmp0(ti->str, "ptr")){
738 pointer_item_t *newpi;
739
740 br->flags|=BI_POINTER0x00000040;
741 newpi=g_new0(pointer_item_t, 1)((pointer_item_t *) g_malloc0_n ((1), sizeof (pointer_item_t)
))
;
742 if (!newpi) {
743 FPRINTF(stderrstderr, "Can't allocate memory. Exit.\n");
744 exit(10);
745 }
746 newpi->next=NULL((void*)0);
747 newpi->type=ti->str;
748 newpi->next=br->pointer_list;
749 br->pointer_list=newpi;
750 ti=ti->next;
751 continue;
752 }
753 if(ti){
754 FPRINTF(stderrstderr, "ERROR: parsebrackets should not be reached unknown tag:%s\n", ti->str);
755 }
756 Exit(10);
757 }
758
759 return NULL((void*)0);
760}
761
762/* this function will register a new type learnt from the IDL file
763*/
764static type_item_t *
765register_new_type(const char *name, const char *dissectorname, const char *ft_type, const char *base_type, const char *mask, const char *valsstring, int alignment){
766 type_item_t *new_type;
767
768 FPRINTF(NULL((void*)0),"XXX new type:%s dissector:%s Type:%s Base:%s Mask:%s Vals:%s alignment:%d\n", name, dissectorname, ft_type, base_type, mask, valsstring, alignment);
769
770 new_type=g_new0(type_item_t, 1)((type_item_t *) g_malloc0_n ((1), sizeof (type_item_t)));
771 if (!new_type) {
772 FPRINTF(stderrstderr, "Can't allocate memory. Exit.\n");
773 exit(10);
774 }
775 new_type->next=type_list;
776 new_type->name=g_strdup(name)g_strdup_inline (name);
777 new_type->dissector=g_strdup(dissectorname)g_strdup_inline (dissectorname);
778 new_type->ft_type=g_strdup(ft_type)g_strdup_inline (ft_type);
779 new_type->base_type=g_strdup(base_type)g_strdup_inline (base_type);
780 new_type->mask=g_strdup(mask)g_strdup_inline (mask);
781 new_type->vals=g_strdup(valsstring)g_strdup_inline (valsstring);
782 new_type->alignment=alignment;
783 type_list=new_type;
784
785 return new_type;
786}
787
788
789/* this function will print the remaining content of the token list
790*/
791static void printtokenlist(int count)
792{
793 token_item_t *ti;
794 FPRINTF(stderrstderr, "TOKENLIST:\n");
795 for(ti=token_list;ti&&count;count--,ti=ti->next){
796 FPRINTF(stderrstderr, "Token \"%s\"\n",ti->str);
797 }
798 if(!count){
799 FPRINTF(stderrstderr, " ...\n");
800 }
801}
802
803
804/* this function will parse the header and pick up the fields
805 * we are interested in.
806 * the header is supposed to start at the very first token and look like
807 * [ <fields> ] interface <ifname> {
808 *
809 * we are interested in the fields:
810 * uuid
811 * version
812 * pointer_default
813 *
814 * this function will also g_remove the header from the token list
815 */
816static void parseheader(void)
817{
818 char filter_name[BASE_BUFFER_SIZE256];
819 token_item_t *ti;
820 int level=0;
821 int major, minor;
822
823 ti=token_list;
824 if(!ti){
825 FPRINTF(stderrstderr, "ERROR: no tokens\n");
826 Exit(10);
827 }
828
829 /* first token must be '[' */
830 if( g_strcmp0(ti->str, "[") ){
831 FPRINTF(stderrstderr, "ERROR: first token is not '['\n");
832 Exit(10);
833 }
834
835 for(ti=token_list;ti;ti=ti->next){
836 if( !g_strcmp0(ti->str, "[")){
837 level++;
838 continue;
839 }
840 if( !g_strcmp0(ti->str, "]")){
841 level--;
842 if(!level){
843 token_list=ti->next;
844 break;
845 }
846 }
847 if(level==1){
848 if( !g_strcmp0(ti->str, "uuid")){
849 uuid=ti->next->next->str;
850 FPRINTF(NULL((void*)0),"UUID:%s\n",uuid);
851 }
852 if( !g_strcmp0(ti->str, "version")){
853 version=ti->next->next->str;
854 FPRINTF(NULL((void*)0),"VERSION:%s\n",version);
855 }
856 if( !g_strcmp0(ti->str, "pointer_default")){
857 if(!g_strcmp0(ti->next->next->str, "unique")){
858 pointer_default="unique";
859 } else if(!g_strcmp0(ti->next->next->str, "ptr")){
860 pointer_default="ptr";
861 } else {
862 FPRINTF(stderrstderr, "ERROR: unknown pointer type\n");
863 Exit(10);
864 }
865 FPRINTF(NULL((void*)0),"POINTER_DEFAULT:%s\n",pointer_default);
866 }
867 }
868 }
869 if(!token_list){
870 FPRINTF(stderrstderr, "ERROR: ran out of tokens inside header\n");
871 Exit(10);
872 }
873 /* interface */
874 if(g_strcmp0(token_list->str, "interface")){
875 FPRINTF(stderrstderr, "ERROR: interface not found\n");
876 Exit(10);
877 }
878 token_list=token_list->next;
879 if (!token_list){
880 FPRINTF(stderrstderr, "ERROR: ran out of tokens\n");
881 Exit(10);
882 }
883 /* interface name */
884 ifname=token_list->str;
885 token_list=token_list->next;
886 FPRINTF(NULL((void*)0),"Interface:%s\n",ifname);
887
888 /* opnum */
889 snprintf(hf_status, BASE_BUFFER_SIZE256, "hf_%s_opnum", ifname);
890 snprintf(filter_name, BASE_BUFFER_SIZE256, "%s.opnum", ifname);
891 register_hf_field(hf_status, "Operation", filter_name, "FT_UINT16", "BASE_DEC", "NULL", "0", "");
892
893 /* status */
894 snprintf(hf_status, BASE_BUFFER_SIZE256, "hf_%s_rc", ifname);
895 snprintf(filter_name, BASE_BUFFER_SIZE256, "%s.rc", ifname);
896 register_hf_field(hf_status, "Return code", filter_name, "FT_UINT32", "BASE_HEX|BASE_EXT_STRING", "&NT_errors_ext", "0", "");
897
898 FPRINTF(eth_ett, "static int ett_%s;\n", ifname);
899 FPRINTF(eth_ettarr, " &ett_%s,\n", ifname);
900
901 /* the body must start with { */
902 if(g_strcmp0(token_list->str, "{")){
903 FPRINTF(stderrstderr, "ERROR: body does not start with '{'\n");
904 Exit(10);
905 }
906
907 /* skip the initial '{' */
908 token_list=token_list->next;
909
910 if(!uuid){
911 FPRINTF(stderrstderr, "ERROR: no uuid found\n");
912 Exit(10);
913 }
914 FPRINTF(eth_code,"static e_guid_t uuid_dcerpc_%s = {\n", ifname);
915 FPRINTF(eth_code," 0x%c%c%c%c%c%c%c%c, 0x%c%c%c%c, 0x%c%c%c%c,\n",uuid[1],uuid[2],uuid[3],uuid[4],uuid[5],uuid[6],uuid[7],uuid[8],uuid[10],uuid[11],uuid[12],uuid[13],uuid[15],uuid[16],uuid[17],uuid[18]);
916 FPRINTF(eth_code," { 0x%c%c, 0x%c%c, 0x%c%c, 0x%c%c, 0x%c%c, 0x%c%c, 0x%c%c, 0x%c%c}\n",uuid[20],uuid[21],uuid[22],uuid[23],uuid[25],uuid[26],uuid[27],uuid[28],uuid[29],uuid[30],uuid[31],uuid[32],uuid[33],uuid[34],uuid[35],uuid[36]);
917 FPRINTF(eth_code,"};\n");
918 FPRINTF(eth_code,"\n");
919
920 sscanf(version, "%d.%d", &major, &minor);
921 FPRINTF(eth_code,"static uint16_t ver_%s = %d;\n", ifname, major);
922 FPRINTF(eth_code,"\n");
923
924 FPRINTF(eth_handoff, " dcerpc_init_uuid(proto_%s, ett_%s,\n", ifname, ifname);
925 FPRINTF(eth_handoff, " &uuid_dcerpc_%s, ver_%s,\n", ifname, ifname);
926 FPRINTF(eth_handoff, " function_dissectors, hf_%s_opnum);\n", ifname);
927}
928
929
930
931/* this helper function is called by the tokenizer and will just append the
932 current token to the linked list
933*/
934static void pushtoken(char *token)
935{
936 token_item_t *new_token_item;
937 new_token_item=g_new0(token_item_t, 1)((token_item_t *) g_malloc0_n ((1), sizeof (token_item_t)));
938 if (!new_token_item) {
939 FPRINTF(stderrstderr, "Can't allocate memory. Exit.\n");
940 exit(10);
941 }
942 new_token_item->next=NULL((void*)0);
943 new_token_item->str=token;
944 if(!token_list){
945 token_list=new_token_item;
946 } else {
947 last_token_item->next=new_token_item;
948 }
949 last_token_item=new_token_item;
950}
951
952/* this function reads the idl file and translates it into tokens.
953 the tokens are stored in a linked list token_list of type token_item_t
954*/
955static void tokenize(FILE *fh)
956{
957 int ch;
958 int fullinecomment=0;
959 int normalcomment=0;
960 int insidequote=0;
961 char qs[4 * BASE_BUFFER_SIZE256];
962 int qspos=0;
963 int insidetoken=0;
964 char token[4 * BASE_BUFFER_SIZE256];
965 int tokenpos=0;
966
967 while(!feof(fh)){
968 ch=fgetc(fh);
969
970 /* full line comment */
971 if(fullinecomment){
972 if( (ch=='\n')||(ch=='\r') ){
973 fullinecomment=0;
974 linepos=0;
975 }
976 continue;
977 }
978 if( (ch=='#')&&(linepos==0) ){
979 fullinecomment=1;
980 continue;
981 }
982
983 /* normal comment */
984 if(normalcomment==0){
985 if(ch=='/'){
986 int nextch;
987 nextch=fgetc(fh);
988 if(nextch=='*'){
989 normalcomment=1;
990 continue;
991 }
992 ungetc(nextch, fh);
993 }
994 } else {
995 if(ch=='*'){
996 int nextch;
997 nextch=fgetc(fh);
998 if(nextch=='/'){
999 normalcomment=0;
1000 continue;
1001 }
1002 ungetc(nextch, fh);
1003 }
1004 continue;
1005 }
1006
1007 /* quoted string */
1008 if(insidequote){
1009 if(ch=='"'){
1010 insidequote=0;
1011 qs[qspos++]='"';
1012 qs[qspos]=0;
1013 pushtoken(g_strdup(qs)g_strdup_inline (qs));
1014 continue;
1015 } else {
1016 qs[qspos++]=(char)ch;
1017 continue;
1018 }
1019 } else {
1020 if(ch=='"'){
1021 insidequote=1;
1022 qs[0]='"';
1023 qspos=1;
1024 continue;
1025 }
1026 }
1027
1028
1029 switch(ch){
1030 case '\n':
1031 case '\r':
1032 if(insidetoken){
1033 insidetoken=0;
1034 token[tokenpos]=0;
1035 pushtoken(g_strdup(token)g_strdup_inline (token));
1036 }
1037 line[linepos]=0;
1038
1039 linepos=0;
1040 break;
1041 case '\t':
1042 case ' ':
1043 if(insidetoken){
1044 insidetoken=0;
1045 token[tokenpos]=0;
1046 pushtoken(g_strdup(token)g_strdup_inline (token));
1047 }
1048 break;
1049 case '[':
1050 case ']':
1051 case '(':
1052 case ')':
1053 case ',':
1054 case ';':
1055 case '*':
1056 case '=':
1057 if(insidetoken){
1058 insidetoken=0;
1059 token[tokenpos]=0;
1060 pushtoken(g_strdup(token)g_strdup_inline (token));
1061 }
1062 token[0]=(char)ch;
1063 token[1]=0;
1064 pushtoken(g_strdup(token)g_strdup_inline (token));
1065 break;
1066 default:
1067 if(!insidetoken){
1068 tokenpos=0;
1069 }
1070 insidetoken=1;
1071 token[tokenpos++]=(char)ch;
1072 line[linepos++]=(char)ch;
1073 break;
1074 }
1075
1076 }
1077}
1078
1079
1080static type_item_t *
1081find_type(char *name)
1082{
1083 type_item_t *tmptype;
1084 for(tmptype=type_list;tmptype;tmptype=tmptype->next){
1085 if(!g_strcmp0(tmptype->name, name)){
1086 break;
1087 }
1088 }
1089 /* autogenerate built in types */
1090 if(!tmptype){
1091 char dissectorname[DISSECTORNAME_MAXLEN256];
1092 if(!g_strcmp0(name,"uint16")){
1093 snprintf(dissectorname, DISSECTORNAME_MAXLEN256, "%s_dissect_%s", ifname, name);
1094 FPRINTF(NULL((void*)0),"\nAutogenerating built-in type:%s\n------------\n",name);
1095 FPRINTF(eth_code, "\n");
1096 FPRINTF(eth_code, "static unsigned\n");
1097 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep, int hf_index, uint32_t param _U_)\n", dissectorname);
1098 FPRINTF(eth_code, "{\n");
1099 FPRINTF(eth_code, " offset=dissect_ndr_uint16(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
1100 FPRINTF(eth_code, " return offset;\n");
1101 FPRINTF(eth_code, "}\n");
1102 FPRINTF(eth_code, "\n");
1103 tmptype=register_new_type("uint16", dissectorname, "FT_UINT16", "BASE_DEC", "0", "NULL", 2);
1104 } else if(!g_strcmp0(name,"int16")){
1105 snprintf(dissectorname, DISSECTORNAME_MAXLEN256, "%s_dissect_%s", ifname, name);
1106 FPRINTF(NULL((void*)0),"\nAutogenerating built-in type:%s\n------------\n",name);
1107 FPRINTF(eth_code, "\n");
1108 FPRINTF(eth_code, "static unsigned\n");
1109 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep, int hf_index, uint32_t param _U_)\n", dissectorname);
1110 FPRINTF(eth_code, "{\n");
1111 FPRINTF(eth_code, " offset=dissect_ndr_uint16(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
1112 FPRINTF(eth_code, " return offset;\n");
1113 FPRINTF(eth_code, "}\n");
1114 FPRINTF(eth_code, "\n");
1115 tmptype=register_new_type("int16", dissectorname, "FT_INT16", "BASE_DEC", "0", "NULL", 2);
1116 } else if(!g_strcmp0(name,"uint32")){
1117 snprintf(dissectorname, DISSECTORNAME_MAXLEN256, "%s_dissect_%s", ifname, name);
1118 FPRINTF(NULL((void*)0),"\nAutogenerating built-in type:%s\n------------\n",name);
1119 FPRINTF(eth_code, "\n");
1120 FPRINTF(eth_code, "static unsigned\n");
1121 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep, int hf_index, uint32_t param _U_)\n", dissectorname);
1122 FPRINTF(eth_code, "{\n");
1123 FPRINTF(eth_code, " offset=dissect_ndr_uint32(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
1124 FPRINTF(eth_code, " return offset;\n");
1125 FPRINTF(eth_code, "}\n");
1126 FPRINTF(eth_code, "\n");
1127 tmptype=register_new_type("uint32", dissectorname, "FT_UINT32", "BASE_DEC", "0", "NULL", 4);
1128 } else if( (!g_strcmp0(name,"int32"))
1129 || (!g_strcmp0(name,"long")) ){
1130 snprintf(dissectorname, DISSECTORNAME_MAXLEN256, "%s_dissect_%s", ifname, name);
1131 FPRINTF(NULL((void*)0),"\nAutogenerating built-in type:%s\n------------\n",name);
1132 FPRINTF(eth_code, "\n");
1133 FPRINTF(eth_code, "static unsigned\n");
1134 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep, int hf_index, uint32_t param _U_)\n", dissectorname);
1135 FPRINTF(eth_code, "{\n");
1136 FPRINTF(eth_code, " offset=dissect_ndr_uint32(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
1137 FPRINTF(eth_code, " return offset;\n");
1138 FPRINTF(eth_code, "}\n");
1139 FPRINTF(eth_code, "\n");
1140 if (!g_strcmp0(name,"int32"))
1141 tmptype=register_new_type("int32", dissectorname, "FT_INT32", "BASE_DEC", "0", "NULL", 4);
1142 else
1143 tmptype=register_new_type("long", dissectorname, "FT_INT32", "BASE_DEC", "0", "NULL", 4);
1144 } else if( (!g_strcmp0(name,"uint8")) ){
1145 snprintf(dissectorname, DISSECTORNAME_MAXLEN256, "%s_dissect_%s", ifname, name);
1146 FPRINTF(NULL((void*)0),"\nAutogenerating built-in type:%s\n------------\n",name);
1147 FPRINTF(eth_code, "\n");
1148 FPRINTF(eth_code, "static unsigned\n");
1149 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep, int hf_index, uint32_t param _U_)\n", dissectorname);
1150 FPRINTF(eth_code, "{\n");
1151 FPRINTF(eth_code, " offset=dissect_ndr_uint8(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
1152 FPRINTF(eth_code, " return offset;\n");
1153 FPRINTF(eth_code, "}\n");
1154 FPRINTF(eth_code, "\n");
1155 tmptype=register_new_type("uint8", dissectorname, "FT_UINT8", "BASE_DEC", "0", "NULL", 1);
1156 } else if( (!g_strcmp0(name,"int8"))
1157 || (!g_strcmp0(name, "char")) ){
1158 snprintf(dissectorname, DISSECTORNAME_MAXLEN256, "%s_dissect_%s", ifname, name);
1159 FPRINTF(NULL((void*)0),"\nAutogenerating built-in type:%s\n------------\n",name);
1160 FPRINTF(eth_code, "\n");
1161 FPRINTF(eth_code, "static unsigned\n");
1162 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep, int hf_index, uint32_t param _U_)\n", dissectorname);
1163 FPRINTF(eth_code, "{\n");
1164 FPRINTF(eth_code, " offset=dissect_ndr_uint8(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
1165 FPRINTF(eth_code, " return offset;\n");
1166 FPRINTF(eth_code, "}\n");
1167 FPRINTF(eth_code, "\n");
1168 if (!g_strcmp0(name,"int8"))
1169 tmptype=register_new_type("int8", dissectorname, "FT_INT8", "BASE_DEC", "0", "NULL", 1);
1170 else
1171 tmptype=register_new_type("char", dissectorname, "FT_INT8", "BASE_DEC", "0", "NULL", 1);
1172 } else if(!g_strcmp0(name,"bool8")){
1173 snprintf(dissectorname, DISSECTORNAME_MAXLEN256, "%s_dissect_%s", ifname, name);
1174 FPRINTF(NULL((void*)0),"\nAutogenerating built-in type:%s\n------------\n",name);
1175 FPRINTF(eth_code, "\n");
1176 FPRINTF(eth_code, "static unsigned\n");
1177 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep, int hf_index, uint32_t param _U_)\n", dissectorname);
1178 FPRINTF(eth_code, "{\n");
1179 FPRINTF(eth_code, " offset=dissect_ndr_uint8(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
1180 FPRINTF(eth_code, " return offset;\n");
1181 FPRINTF(eth_code, "}\n");
1182 FPRINTF(eth_code, "\n");
1183 tmptype=register_new_type("bool8", dissectorname, "FT_INT8", "BASE_DEC", "0", "NULL", 1);
1184 } else if(!g_strcmp0(name,"unistr")){
1185 snprintf(dissectorname, DISSECTORNAME_MAXLEN256, "%s_dissect_%s", ifname, name);
1186 FPRINTF(NULL((void*)0),"\nAutogenerating built-in type:%s\n------------\n",name);
1187 FPRINTF(eth_code, "\n");
1188 FPRINTF(eth_code, "static unsigned\n");
1189 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep, int hf_index, uint32_t param _U_)\n", dissectorname);
1190 FPRINTF(eth_code, "{\n");
1191 FPRINTF(eth_code, " offset=dissect_ndr_cvstring(tvb, offset, pinfo, tree, di, drep, 2, hf_index, false, NULL);\n");
1192 FPRINTF(eth_code, " return offset;\n");
1193 FPRINTF(eth_code, "}\n");
1194 FPRINTF(eth_code, "\n");
1195 tmptype=register_new_type("unistr", dissectorname, "FT_STRING", "BASE_NONE", "0", "NULL", 4);
1196 } else if(!g_strcmp0(name,"ascstr")){
1197 snprintf(dissectorname, DISSECTORNAME_MAXLEN256, "%s_dissect_%s", ifname, name);
1198 FPRINTF(NULL((void*)0),"\nAutogenerating built-in type:%s\n------------\n",name);
1199 FPRINTF(eth_code, "\n");
1200 FPRINTF(eth_code, "static unsigned\n");
1201 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep, int hf_index, uint32_t param _U_)\n", dissectorname);
1202 FPRINTF(eth_code, "{\n");
1203 FPRINTF(eth_code, " offset=dissect_ndr_cvstring(tvb, offset, pinfo, tree, di, drep, 1, hf_index, false, NULL);\n");
1204 FPRINTF(eth_code, " return offset;\n");
1205 FPRINTF(eth_code, "}\n");
1206 FPRINTF(eth_code, "\n");
1207 tmptype=register_new_type("ascstr", dissectorname, "FT_STRING", "BASE_NONE", "0", "NULL", 4);
1208 } else if(!g_strcmp0(name,"GUID")
1209 ||!g_strcmp0(name,"uuid_t")){
1210 snprintf(dissectorname, DISSECTORNAME_MAXLEN256, "%s_dissect_%s", ifname, name);
1211 FPRINTF(NULL((void*)0),"\nAutogenerating built-in type:%s\n------------\n",name);
1212 FPRINTF(eth_code, "\n");
1213 FPRINTF(eth_code, "static unsigned\n");
1214 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep, int hf_index, uint32_t param _U_)\n", dissectorname);
1215 FPRINTF(eth_code, "{\n");
1216 FPRINTF(eth_code, " offset=dissect_ndr_uuid_t(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
1217 FPRINTF(eth_code, " return offset;\n");
1218 FPRINTF(eth_code, "}\n");
1219 FPRINTF(eth_code, "\n");
1220 tmptype=register_new_type(name, dissectorname, "FT_GUID", "BASE_NONE", "0", "NULL", 4);
1221 } else if(!g_strcmp0(name,"policy_handle")){
1222 snprintf(dissectorname, DISSECTORNAME_MAXLEN256, "%s_dissect_%s", ifname, name);
1223 FPRINTF(NULL((void*)0),"\nAutogenerating built-in type:%s\n------------\n",name);
1224 FPRINTF(eth_code, "\n");
1225 FPRINTF(eth_code, "static e_ctx_hnd policy_hnd;\n");
1226 FPRINTF(eth_code, "static proto_item *hnd_item;\n");
1227 FPRINTF(eth_code, "\n");
1228 FPRINTF(eth_code, "static unsigned\n");
1229 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep, int hf_index, uint32_t param)\n", dissectorname);
1230 FPRINTF(eth_code, "{\n");
1231 FPRINTF(eth_code, " offset = dissect_nt_policy_hnd(tvb, offset, pinfo, tree, di, drep,\n");
1232 FPRINTF(eth_code, " hf_index, &policy_hnd, &hnd_item,\n");
1233 FPRINTF(eth_code, " param);\n");
1234
1235 FPRINTF(eth_code, " return offset;\n");
1236 FPRINTF(eth_code, "}\n");
1237 FPRINTF(eth_code, "\n");
1238 tmptype=register_new_type("policy_handle", dissectorname, "FT_BYTES", "BASE_NONE", "0", "NULL", 4);
1239 } else if(!g_strcmp0(name,"NTTIME")){
1240 /* 8 bytes, aligned to 4 bytes */
1241 snprintf(dissectorname, DISSECTORNAME_MAXLEN256, "%s_dissect_%s", ifname, name);
1242 FPRINTF(NULL((void*)0),"\nAutogenerating built-in type:%s\n------------\n",name);
1243 FPRINTF(eth_code, "\n");
1244 FPRINTF(eth_code, "static unsigned\n");
1245 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep, int hf_index, uint32_t param _U_)\n", dissectorname);
1246 FPRINTF(eth_code, "{\n");
1247 FPRINTF(eth_code, " offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, di, drep, hf_index);\n");
1248
1249 FPRINTF(eth_code, "\n");
1250 FPRINTF(eth_code, " return offset;\n");
1251 FPRINTF(eth_code, "}\n");
1252 FPRINTF(eth_code, "\n");
1253 tmptype=register_new_type("NTTIME", dissectorname, "FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", "0", "NULL", 4);
1254 } else if(!g_strcmp0(name,"NTTIME_hyper")){
1255 /* 8 bytes, aligned to 8 bytes */
1256 snprintf(dissectorname, DISSECTORNAME_MAXLEN256, "%s_dissect_%s", ifname, name);
1257 FPRINTF(NULL((void*)0),"\nAutogenerating built-in type:%s\n------------\n",name);
1258 FPRINTF(eth_code, "\n");
1259 FPRINTF(eth_code, "static unsigned\n");
1260 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep, int hf_index, uint32_t param _U_)\n", dissectorname);
1261 FPRINTF(eth_code, "{\n");
1262 FPRINTF(eth_code, " ALIGN_TO_8_BYTES;\n");
1263 FPRINTF(eth_code, " offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, di, drep, hf_index);\n");
1264
1265 FPRINTF(eth_code, "\n");
1266 FPRINTF(eth_code, " return offset;\n");
1267 FPRINTF(eth_code, "}\n");
1268 FPRINTF(eth_code, "\n");
1269 tmptype=register_new_type("NTTIME_hyper", dissectorname, "FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", "0", "NULL", 4);
1270 } else if(!g_strcmp0(name,"NTTIME_1sec")){
1271 /* 8 bytes, aligned to 8 bytes */
1272 snprintf(dissectorname, DISSECTORNAME_MAXLEN256, "%s_dissect_%s", ifname, name);
1273 FPRINTF(NULL((void*)0),"\nAutogenerating built-in type:%s\n------------\n",name);
1274 FPRINTF(eth_code, "\n");
1275 FPRINTF(eth_code, "static unsigned\n");
1276 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep, int hf_index, uint32_t param _U_)\n", dissectorname);
1277 FPRINTF(eth_code, "{\n");
1278 FPRINTF(eth_code, " ALIGN_TO_8_BYTES;\n");
1279 FPRINTF(eth_code, " offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, di, drep, hf_index);\n");
1280
1281 FPRINTF(eth_code, "\n");
1282 FPRINTF(eth_code, " return offset;\n");
1283 FPRINTF(eth_code, "}\n");
1284 FPRINTF(eth_code, "\n");
1285 tmptype=register_new_type("NTTIME_1sec", dissectorname, "FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", "0", "NULL", 4);
1286 } else if(!g_strcmp0(name,"udlong")){
1287 /* 8 bytes, aligned to 4 bytes */
1288 snprintf(dissectorname, DISSECTORNAME_MAXLEN256, "%s_dissect_%s", ifname, name);
1289 FPRINTF(NULL((void*)0),"\nAutogenerating built-in type:%s\n------------\n",name);
1290 FPRINTF(eth_code, "\n");
1291 FPRINTF(eth_code, "static unsigned\n");
1292 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep, int hf_index, uint32_t param _U_)\n", dissectorname);
1293 FPRINTF(eth_code, "{\n");
1294 FPRINTF(eth_code, "\n");
1295 FPRINTF(eth_code, " offset=dissect_ndr_duint32(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
1296 FPRINTF(eth_code, "\n");
1297 FPRINTF(eth_code, " return offset;\n");
1298 FPRINTF(eth_code, "}\n");
1299 FPRINTF(eth_code, "\n");
1300 tmptype=register_new_type("udlong", dissectorname, "FT_UINT64", "BASE_DEC", "0", "NULL", 4);
1301 } else if(!g_strcmp0(name,"dlong")){
1302 /* 8 bytes, aligned to 4 bytes */
1303 snprintf(dissectorname, DISSECTORNAME_MAXLEN256, "%s_dissect_%s", ifname, name);
1304 FPRINTF(NULL((void*)0),"\nAutogenerating built-in type:%s\n------------\n",name);
1305 FPRINTF(eth_code, "\n");
1306 FPRINTF(eth_code, "static unsigned\n");
1307 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep, int hf_index, uint32_t param _U_)\n", dissectorname);
1308 FPRINTF(eth_code, "{\n");
1309 FPRINTF(eth_code, "\n");
1310 FPRINTF(eth_code, " offset=dissect_ndr_duint32(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
1311 FPRINTF(eth_code, "\n");
1312 FPRINTF(eth_code, " return offset;\n");
1313 FPRINTF(eth_code, "}\n");
1314 FPRINTF(eth_code, "\n");
1315 tmptype=register_new_type("dlong", dissectorname, "FT_INT64", "BASE_DEC", "0", "NULL", 4);
1316 } else if(!g_strcmp0(name,"uint64")){
1317 /* 8 bytes, aligned to 8 bytes */
1318 snprintf(dissectorname, DISSECTORNAME_MAXLEN256, "%s_dissect_%s", ifname, name);
1319 FPRINTF(NULL((void*)0),"\nAutogenerating built-in type:%s\n------------\n",name);
1320 FPRINTF(eth_code, "\n");
1321 FPRINTF(eth_code, "static unsigned\n");
1322 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep, int hf_index, uint32_t param _U_)\n", dissectorname);
1323 FPRINTF(eth_code, "{\n");
1324 FPRINTF(eth_code, " \n");
1325 FPRINTF(eth_code, " ALIGN_TO_8_BYTES;\n");
1326 FPRINTF(eth_code, " offset=dissect_ndr_uint64(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
1327 FPRINTF(eth_code, "\n");
1328 FPRINTF(eth_code, " return offset;\n");
1329 FPRINTF(eth_code, "}\n");
1330 FPRINTF(eth_code, "\n");
1331 tmptype=register_new_type("uint64", dissectorname, "FT_UINT64", "BASE_DEC", "0", "NULL", 8);
1332 } else if(!g_strcmp0(name,"time_t")){
1333 snprintf(dissectorname, DISSECTORNAME_MAXLEN256, "%s_dissect_%s", ifname, name);
1334 FPRINTF(NULL((void*)0),"\nAutogenerating built-in type:%s\n------------\n",name);
1335 FPRINTF(eth_code, "\n");
1336 FPRINTF(eth_code, "static unsigned\n");
1337 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep, int hf_index, uint32_t param _U_)\n", dissectorname);
1338 FPRINTF(eth_code, "{\n");
1339 FPRINTF(eth_code, "\n");
1340 FPRINTF(eth_code, " offset=dissect_ndr_time_t(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
1341 FPRINTF(eth_code, "\n");
1342 FPRINTF(eth_code, " return offset;\n");
1343 FPRINTF(eth_code, "}\n");
1344 FPRINTF(eth_code, "\n");
1345 tmptype=register_new_type("time_t", dissectorname, "FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", "0", "NULL", 4);
1346 } else if(!g_strcmp0(name,"SID")){
1347 snprintf(dissectorname, DISSECTORNAME_MAXLEN256, "%s_dissect_%s", ifname, name);
1348 FPRINTF(NULL((void*)0),"\nAutogenerating built-in type:%s\n------------\n",name);
1349 FPRINTF(eth_code, "\n");
1350 FPRINTF(eth_code, "static unsigned\n");
1351 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep, int hf_index, uint32_t param)\n", dissectorname);
1352 FPRINTF(eth_code, "{\n");
1353 FPRINTF(eth_code, " offset=dissect_ndr_nt_SID_with_options(tvb, offset, pinfo, tree, di, drep, param, hf_index);\n");
1354 FPRINTF(eth_code, " return offset;\n");
1355 FPRINTF(eth_code, "}\n");
1356 FPRINTF(eth_code, "\n");
1357 tmptype=register_new_type("SID", dissectorname, "FT_STRING", "BASE_NONE", "0", "NULL", 4);
1358 } else if(!g_strcmp0(name,"WERROR")){
1359 snprintf(dissectorname, DISSECTORNAME_MAXLEN256, "%s_dissect_%s", ifname, name);
1360 FPRINTF(NULL((void*)0),"\nAutogenerating built-in type:%s\n------------\n",name);
1361 FPRINTF(eth_code, "\n");
1362 FPRINTF(eth_code, "static unsigned\n");
1363 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep, int hf_index, uint32_t param _U_)\n", dissectorname);
1364 FPRINTF(eth_code, "{\n");
1365 FPRINTF(eth_code, " \n");
1366 FPRINTF(eth_code, " offset=dissect_ndr_uint32(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
1367 FPRINTF(eth_code, "\n");
1368 FPRINTF(eth_code, " return offset;\n");
1369 FPRINTF(eth_code, "}\n");
1370 FPRINTF(eth_code, "\n");
1371 tmptype=register_new_type("WERROR", dissectorname, "FT_UINT32", "BASE_DEC|BASE_EXT_STRING", "0", "&WERR_errors_ext", 4);
1372 }
1373 }
1374
1375 return tmptype;
1376}
1377
1378
1379/* this function will skip past an entire declare ... ; statement */
1380static void skipdeclare(void)
1381{
1382 token_item_t *ti;
1383
1384 /* first must be the keyword const */
1385 ti=token_list;
1386 if(strncmp(ti->str, "declare", 7)){
1387 FPRINTF(stderrstderr, "ERROR: skipdeclare first token is not 'declare'\n");
1388 Exit(10);
1389 }
1390 while(ti->str[0] != ';'){
1391 ti=ti->next;
1392 }
1393 ti=ti->next;
1394
1395 token_list=ti;
1396}
1397
1398/* this function will parse a
1399 const
1400 and generate the appropriate code
1401 const must be followed by a suitable keyword [uint16|uint32|...]
1402 the const will later be g_removed from the token list
1403 the function assumes that the const is the first object in the token_list
1404*/
1405static void parseconst(void)
1406{
1407 token_item_t *ti;
1408 char *name, *value;
1409
1410 /* first must be the keyword const */
1411 ti=token_list;
1412 if(strncmp(ti->str, "const", 5)){
1413 FPRINTF(stderrstderr, "ERROR: const first token is not 'const'\n");
1414 Exit(10);
1415 }
1416 ti=ti->next;
1417
1418 /* just skip second token */
1419 ti=ti->next;
1420
1421 /* third is a variable and not a type */
1422 if(find_type(ti->str)){
1423 FPRINTF(stderrstderr, "ERROR: const, not a variable name:%s\n", ti->str);
1424 Exit(10);
1425 }
1426 name=ti->str;
1427 ti=ti->next;
1428
1429 /* fourth is '=' */
1430 if(ti->str[0] != '='){
1431 FPRINTF(stderrstderr, "ERROR: const fourth token is not '='\n");
1432 Exit(10);
1433 }
1434 ti=ti->next;
1435
1436 /* fifth is the value */
1437 value=ti->str;
1438 ti=ti->next;
1439
1440 /* sixth is ';' */
1441 if(ti->str[0] != ';'){
1442 FPRINTF(stderrstderr, "ERROR: const sixth token is not ';'\n");
1443 Exit(10);
1444 }
1445 ti=ti->next;
1446
1447 FPRINTF(NULL((void*)0),"\nCONST:%s\n-------\n",name);
1448
1449 FPRINTF(eth_hdr, "#define %s %s\n", name, value);
1450
1451 FPRINTF(NULL((void*)0),"\n----------\nEND CONST:%s\n",name);
1452
1453 token_list=ti;
1454}
1455
1456/* this function will parse a
1457 typedef struct {
1458 construct and generate the appropriate code.
1459 the typedef will be g_removed from the token_list once it has been processed
1460 the function assumes that the typedef is the first object in the token_list
1461 the function will be called twice, once with pass=0 and once with pass=1
1462 which controls whether subdissectors are to be generated or whether the
1463 struct dissector itself is to be generated
1464*/
1465static void parsetypedefstruct(int pass)
1466{
1467 token_item_t *ti, *tmpti;
1468 char *struct_name;
1469 char dissectorname[DISSECTORNAME_MAXLEN256];
1470 char tmpstr[BASE_BUFFER_SIZE256], *ptmpstr;
1471 int level, num_pointers;
1472 static int alignment;
1473 type_item_t *type_item;
1474 char hf_index[BASE_BUFFER_SIZE256];
1475 bracket_item_t *bi=NULL((void*)0);
1476 pointer_item_t *pi;
1477 const char *pointer_type;
1478 char *field_name;
1479 uint32_t fixed_array_size;
1480 int is_array_of_pointers;
1481 int empty_struct = 0;
1482
1483 ti=token_list;
1484 if(g_strcmp0(ti->str, "typedef")){
1485 FPRINTF(stderrstderr, "ERROR: typedefstruct first token is not 'typedef'\n");
1486 Exit(10);
1487 }
1488 ti=ti->next;
1489
1490 if(!g_strcmp0(ti->str, "[")){
1491 ti=parsebrackets(ti, &bi);
1492 }
1493 /* check that we know how to handle the bracket thing */
1494 if(bi){
1495 if(bi->flags){
1496 FPRINTF(stderrstderr, "ERROR: typedefstruct unknown bracket flags encountered : 0x%08x\n",bi->flags);
1497 Exit(10);
1498 }
1499 }
1500
1501 if(g_strcmp0(ti->str, "struct")){
1502 FPRINTF(stderrstderr, "ERROR: typedefstruct second token is not 'struct'\n");
1503 Exit(10);
1504 }
1505 ti=ti->next;
1506
1507 if(g_strcmp0(ti->str, "{")){
1508 FPRINTF(stderrstderr, "ERROR: typedefstruct third token is not '{'\n");
1509 Exit(10);
1510 }
1511 ti=ti->next;
1512
1513 /* Check if the struct is empty (search if there is no end bracket) */
1514 if(g_strcmp0(ti->str, "}") == 0){
1515 empty_struct = 1;
1516 }
1517
1518 /* search forward until the '}' so we can find the name of the struct */
1519 for(tmpti=ti,level=0;tmpti;tmpti=tmpti->next){
1520 if(!g_strcmp0(tmpti->str, "{")){
1521 level++;
1522 continue;
1523 }
1524 if(!g_strcmp0(tmpti->str, "}")){
1525 if(!level){
1526 break;
1527 }
1528 level--;
1529 continue;
1530 }
1531 }
1532 if(!tmpti || !tmpti->next){
1533 FPRINTF(stderrstderr, "ERROR: typedefstruct missing matching '}'\n");
1534 Exit(10);
1535 }
1536
1537 struct_name=tmpti->next->str;
1538 snprintf(dissectorname, DISSECTORNAME_MAXLEN256, "%s_dissect_%s", ifname, struct_name);
1539
1540 FPRINTF(NULL((void*)0),"\nSTRUCT:%s pass:%d\n-------\n",struct_name,pass);
1541
1542 if(!check_if_to_emit(dissectorname)){
1543 FPRINTF(NULL((void*)0),"NOEMIT Skipping this struct dissector.\n");
1544 ti=tmpti;
1545 goto typedef_struct_finished;
1546 }
1547
1548 /* this is pass 0 so reset alignment to zero and update as items are
1549 processed. we need alignment when pass 1 is run.
1550 set alignment initially to 1 so we don't fail for empty structs
1551 */
1552 if(pass==0){
1553 alignment=1;
1554 }
1555 /* pass 1 generate header for the struct dissector */
1556 if(pass==1){
1557 FPRINTF(eth_ett, "static int ett_%s_%s;\n", ifname, struct_name);
1558 FPRINTF(eth_ettarr, " &ett_%s_%s,\n", ifname, struct_name);
1559 FPRINTF(eth_hdr, "int %s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep, int hf_index, uint32_t param);\n", dissectorname);
1560 FPRINTF(eth_code, "\n");
1561 FPRINTF(eth_code, "int\n");
1562 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo _U_, proto_tree *parent_tree, dcerpc_info *di _U_, uint8_t *drep _U_, int hf_index, uint32_t param _U_)\n", dissectorname);
1563 FPRINTF(eth_code, "{\n");
1564 FPRINTF(eth_code, " proto_item *item=NULL;\n");
1565 if(!empty_struct){
1566 FPRINTF(eth_code, " proto_tree *tree=NULL;\n");
1567 }
1568 FPRINTF(eth_code, " int old_offset;\n");
1569 FPRINTF(eth_code, "\n");
1570 switch(alignment){
1571 case 1:
1572 break;
1573 case 2:
1574 FPRINTF(eth_code, " ALIGN_TO_2_BYTES;\n");
1575 FPRINTF(eth_code, "\n");
1576 break;
1577 case 4:
1578 FPRINTF(eth_code, " ALIGN_TO_4_BYTES;\n");
1579 FPRINTF(eth_code, "\n");
1580 break;
1581 case 8:
1582 FPRINTF(eth_code, " ALIGN_TO_8_BYTES;\n");
1583 FPRINTF(eth_code, "\n");
1584 break;
1585 default:
1586 FPRINTF(stderrstderr, "ERROR: can not handle alignment:%d\n",alignment);
1587 Exit(10);
1588 }
1589 FPRINTF(eth_code, " old_offset=offset;\n");
1590 FPRINTF(eth_code, " if(parent_tree){\n");
1591 FPRINTF(eth_code, " item=proto_tree_add_item(parent_tree, hf_index, tvb, offset, -1, ENC_NA);\n");
1592 if(!empty_struct){
1593 FPRINTF(eth_code, " tree=proto_item_add_subtree(item, ett_%s_%s);\n", ifname, struct_name);
1594 }
1595 FPRINTF(eth_code, " }\n");
1596 FPRINTF(eth_code, "\n");
1597 }
1598
1599 /* scan the struct and create all subdissectors */
1600 level=0;
1601 while(ti){
1602 if(!g_strcmp0(ti->str, "{")){
1603 level++;
1604 ti=ti->next;
1605 continue;
1606 }
1607 if(!g_strcmp0(ti->str, "}")){
1608 if(!level){
1609 break;
1610 }
1611 level--;
1612 ti=ti->next;
1613 continue;
1614 }
1615 if(!g_strcmp0(ti->str, "[")){
1616 ti=parsebrackets(ti, &bi);
1617 continue;
1618 }
1619
1620 /* check that we know how to handle the bracket thing */
1621 if(bi){
1622 if(bi->flags&(~(BI_SIZE_IS0x00000010|BI_LENGTH_IS0x00000020|BI_POINTER0x00000040))){
1623 FPRINTF(stderrstderr, "ERROR: typedefstruct unknown bracket flags encountered : 0x%08x\n",bi->flags);
1624 Exit(10);
1625 }
1626 }
1627
1628 /* handle the type, verify that we KNOW this type */
1629 type_item=find_type(ti->str);
1630 if(!type_item){
1631 FPRINTF(stderrstderr, "ERROR : typedefstruct unknown type %s\n",ti->str);
1632 Exit(10);
1633 }
1634 ti=ti->next;
1635 /* count the levels of pointers */
1636 for(num_pointers=0;!g_strcmp0(ti->str, "*");ti=ti->next){
1637 num_pointers++;
1638 /* pointers are aligned at 4 byte boundaries */
1639 if(alignment<4){
1640 alignment=4;
1641 }
1642 }
1643 /* now that we know how many real pointers there were we must
1644 prepend default pointers to the list so it has the right
1645 length.
1646 */
1647 pi=prepend_pointer_list(bi?bi->pointer_list:NULL((void*)0), num_pointers);
1648 /* keep track of alignment */
1649 if(alignment<type_item->alignment){
1650 alignment=type_item->alignment;
1651 }
1652
1653 field_name=ti->str;
1654 ti=ti->next;
1655
1656 /* see if it is a fixed array */
1657 fixed_array_size=0;
1658 is_array_of_pointers=0;
1659 if(!g_strcmp0(ti->str, "[")){
1660 /* this might be a fixed array */
1661 ti=ti->next;
1662
1663 if(!g_strcmp0("]", ti->str)){
1664 /* this is just a normal [] array */
1665 fixed_array_size=0;
1666 } else if(!g_strcmp0("*", ti->str)){
1667 pi=prepend_pointer_list(pi, num_pointers+1);
1668 fixed_array_size=0;
1669 is_array_of_pointers=1;
1670 ti=ti->next;
1671 } else if (!ws_strtou32(ti->str, NULL((void*)0), &fixed_array_size)) {
1672 FPRINTF(stderrstderr, "ERROR: invalid integer: %s\n", ti->str);
1673 Exit(10);
1674 } else {
1675 ti=ti->next;
1676 }
1677
1678 if(g_strcmp0(ti->str, "]")){
1679 FPRINTF(stderrstderr, "ERROR: typedefstruct fixed array does not end with ']' it ended with %s\n",ti->str);
1680 Exit(10);
1681 }
1682 ti=ti->next;
1683 }
1684
1685 snprintf(hf_index, BASE_BUFFER_SIZE256, "hf_%s_%s_%s", ifname, struct_name, field_name);
1686 /* pass 0 generate subdissectors */
1687 if(pass==0){
1688 char filter_name[BASE_BUFFER_SIZE256];
1689 const char *hf;
1690
1691 snprintf(tmpstr, BASE_BUFFER_SIZE256, "%s_dissect_%s_%s", ifname, struct_name, field_name);
1692 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
1693
1694 if(check_if_to_emit(tmpstr)){
1695 snprintf(filter_name, BASE_BUFFER_SIZE256, "%s.%s.%s", ifname, struct_name, field_name);
1696 hf=register_hf_field(hf_index, field_name, filter_name, type_item->ft_type, type_item->base_type, type_item->vals, type_item->mask, "");
1697 FPRINTF(eth_code, "static unsigned\n");
1698 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep)\n", ptmpstr);
1699 FPRINTF(eth_code, "{\n");
1700 FPRINTF(eth_code, " uint32_t param=%s;\n",find_dissector_param_value(ptmpstr));
1701 FPRINTF(eth_code, " offset=%s(tvb, offset, pinfo, tree, di, drep, %s, param);\n", type_item->dissector, hf);
1702 FPRINTF(eth_code, " return offset;\n");
1703 FPRINTF(eth_code, "}\n");
1704 FPRINTF(eth_code, "\n");
1705 } else {
1706 FPRINTF(NULL((void*)0),"NOEMIT Skipping this struct item :%s\n",tmpstr);
1707 }
1708
1709 if(is_array_of_pointers){
1710 pointer_type=pi->type;
1711 pi=pi->next;
1712 snprintf(tmpstr, BASE_BUFFER_SIZE256, "%s_%s", pointer_type, ptmpstr);
1713 if(check_if_to_emit(tmpstr)){
1714 FPRINTF(eth_code, "static unsigned\n");
1715 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep)\n", tmpstr);
1716 FPRINTF(eth_code, "{\n");
1717 FPRINTF(eth_code, " offset=dissect_ndr_embedded_pointer(tvb, offset, pinfo, tree, di, drep, %s, %s, \"%s\", -1);\n", ptmpstr, ptr_to_define(pointer_type), field_name);
1718 FPRINTF(eth_code, " return offset;\n");
1719 FPRINTF(eth_code, "}\n");
1720 FPRINTF(eth_code, "\n");
1721 } else {
1722 FPRINTF(NULL((void*)0),"NOEMIT Skipping this struct item :%s\n",tmpstr);
1723 }
1724
1725 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
1726 } else if(fixed_array_size){
1727 snprintf(tmpstr, BASE_BUFFER_SIZE256, "fixedarray_%s", ptmpstr);
1728 if(check_if_to_emit(tmpstr)){
1729 FPRINTF(eth_code, "static unsigned\n");
1730 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep)\n", tmpstr);
1731 FPRINTF(eth_code, "{\n");
1732 FPRINTF(eth_code, " int count=%d;\n",fixed_array_size);
1733 FPRINTF(eth_code, " while(count--){\n");
1734 FPRINTF(eth_code, " offset=%s(tvb, offset, pinfo, tree, di, drep);\n", ptmpstr);
1735 FPRINTF(eth_code, " }\n");
1736 FPRINTF(eth_code, "\n");
1737 FPRINTF(eth_code, " return offset;\n");
1738 FPRINTF(eth_code, "}\n");
1739 FPRINTF(eth_code, "\n");
1740 } else {
1741 FPRINTF(NULL((void*)0),"NOEMIT Skipping this struct item :%s\n",tmpstr);
1742 }
1743 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
1744 }
1745
1746 /* handle switch_is */
1747 if(bi){
1748 switch(bi->flags&(BI_SIZE_IS0x00000010|BI_LENGTH_IS0x00000020)){
1749 case 0:
1750 break;
1751 case BI_SIZE_IS0x00000010:
1752 snprintf(tmpstr, BASE_BUFFER_SIZE256, "ucarray_%s", ptmpstr);
1753 if(check_if_to_emit(tmpstr)){
1754 FPRINTF(eth_code, "static unsigned\n");
1755 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep)\n", tmpstr);
1756 FPRINTF(eth_code, "{\n");
1757 FPRINTF(eth_code, " offset=dissect_ndr_ucarray(tvb, offset, pinfo, tree, di, drep, %s);\n", ptmpstr);
1758 FPRINTF(eth_code, " return offset;\n");
1759 FPRINTF(eth_code, "}\n");
1760 FPRINTF(eth_code, "\n");
1761 } else {
1762 FPRINTF(NULL((void*)0),"NOEMIT Skipping this struct item :%s\n",tmpstr);
1763 }
1764 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
1765 break;
1766 case BI_LENGTH_IS0x00000020:
1767 snprintf(tmpstr, BASE_BUFFER_SIZE256, "uvarray_%s", ptmpstr);
1768 if(check_if_to_emit(tmpstr)){
1769 FPRINTF(eth_code, "static unsigned\n");
1770 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep)\n", tmpstr);
1771 FPRINTF(eth_code, "{\n");
1772 FPRINTF(eth_code, " offset=dissect_ndr_uvarray(tvb, offset, pinfo, tree, di, drep, %s);\n", ptmpstr);
1773 FPRINTF(eth_code, " return offset;\n");
1774 FPRINTF(eth_code, "}\n");
1775 FPRINTF(eth_code, "\n");
1776 } else {
1777 FPRINTF(NULL((void*)0),"NOEMIT Skipping this struct item :%s\n",tmpstr);
1778 }
1779 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
1780 break;
1781 case BI_SIZE_IS0x00000010|BI_LENGTH_IS0x00000020:
1782 snprintf(tmpstr, BASE_BUFFER_SIZE256, "ucvarray_%s", ptmpstr);
1783 if(check_if_to_emit(tmpstr)){
1784 FPRINTF(eth_code, "static unsigned\n");
1785 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep)\n", tmpstr);
1786 FPRINTF(eth_code, "{\n");
1787 FPRINTF(eth_code, " offset=dissect_ndr_ucvarray(tvb, offset, pinfo, tree, di, drep, %s);\n", ptmpstr);
1788 FPRINTF(eth_code, " return offset;\n");
1789 FPRINTF(eth_code, "}\n");
1790 FPRINTF(eth_code, "\n");
1791 } else {
1792 FPRINTF(NULL((void*)0),"NOEMIT Skipping this struct item :%s\n",tmpstr);
1793 }
1794 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
1795 break;
1796 default:
1797 FPRINTF(stderrstderr, "ERROR: typedefstruct can not handle this combination of sizeis/lengthis\n");
1798 Exit(10);
1799 }
1800 }
1801
1802 /* handle pointers */
1803 while(num_pointers--){
1804 pointer_type=pi->type;
1805 pi=pi->next;
1806 snprintf(tmpstr, BASE_BUFFER_SIZE256, "%s_%s", pointer_type, ptmpstr);
1807 if(check_if_to_emit(tmpstr)){
1808 FPRINTF(eth_code, "static unsigned\n");
1809 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep)\n", tmpstr);
1810 FPRINTF(eth_code, "{\n");
1811 FPRINTF(eth_code, " offset=dissect_ndr_embedded_pointer(tvb, offset, pinfo, tree, di, drep, %s, %s, \"%s\", -1);\n", ptmpstr, ptr_to_define(pointer_type), field_name);
1812 FPRINTF(eth_code, " return offset;\n");
1813 FPRINTF(eth_code, "}\n");
1814 FPRINTF(eth_code, "\n");
1815 } else {
1816 FPRINTF(NULL((void*)0),"NOEMIT Skipping this struct item :%s\n",tmpstr);
1817 }
1818
1819 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
1820 }
1821 }
1822
1823 if(pass==1){
1824 snprintf(tmpstr, BASE_BUFFER_SIZE256, "%s_dissect_%s_%s", ifname, struct_name, field_name);
1825 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
1826
1827 /* handle fixedsizearrays */
1828 if(is_array_of_pointers){
1829 pointer_type=pi->type;
1830 pi=pi->next;
1831 snprintf(tmpstr, BASE_BUFFER_SIZE256, "%s_%s", pointer_type, ptmpstr);
1832 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
1833 } else if(fixed_array_size){
1834 snprintf(tmpstr, BASE_BUFFER_SIZE256, "fixedarray_%s", ptmpstr);
1835 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
1836 }
1837
1838 /* handle switch_is */
1839 if(bi){
1840 switch(bi->flags&(BI_SIZE_IS0x00000010|BI_LENGTH_IS0x00000020)){
1841 case 0:
1842 break;
1843 case BI_SIZE_IS0x00000010:
1844 snprintf(tmpstr, BASE_BUFFER_SIZE256, "ucarray_%s", ptmpstr);
1845 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
1846 break;
1847 case BI_LENGTH_IS0x00000020:
1848 snprintf(tmpstr, BASE_BUFFER_SIZE256, "uvarray_%s", ptmpstr);
1849 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
1850 break;
1851 case BI_SIZE_IS0x00000010|BI_LENGTH_IS0x00000020:
1852 snprintf(tmpstr, BASE_BUFFER_SIZE256, "ucvarray_%s", ptmpstr);
1853 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
1854 break;
1855 default:
1856 FPRINTF(stderrstderr, "ERROR: typedefstruct can not handle this combination of sizeis/lengthis\n");
1857 Exit(10);
1858 }
1859 }
1860
1861 /* handle pointers */
1862 while(num_pointers--){
1863 pointer_type=pi->type;
1864 pi=pi->next;
1865 snprintf(tmpstr, BASE_BUFFER_SIZE256, "%s_%s", pointer_type, ptmpstr);
1866 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
1867 }
1868
1869 FPRINTF(eth_code, " offset=%s(tvb, offset, pinfo, tree, di, drep);\n", ptmpstr);
1870 FPRINTF(eth_code, "\n");
1871 }
1872
1873 if(g_strcmp0(ti->str,";")){
1874 FPRINTF(stderrstderr, "ERROR: field does not en with ';'\n");
1875 Exit(10);
1876 }
1877 ti=ti->next;
1878 bi=NULL((void*)0); /* clear bi before we start on the next field */
1879 }
1880
1881 if(pass==1){
1882 FPRINTF(eth_code, " proto_item_set_len(item, offset-old_offset);\n");
1883 FPRINTF(eth_code, "\n");
1884 FPRINTF(eth_code, " return offset;\n");
1885 FPRINTF(eth_code, "}\n");
1886 register_new_type(struct_name, dissectorname, "FT_NONE", "BASE_NONE", "0", "NULL", alignment);
1887 }
1888
1889
1890typedef_struct_finished:
1891 FPRINTF(NULL((void*)0),"\nEND STRUCT:%s pass:%d\n-------\n",struct_name,pass);
1892
1893 /* only advance token_list for pass==1
1894 ti now points to the '}' token
1895 */
1896 if(pass==1){
1897 if(!ti || g_strcmp0(ti->str,"}")){
1898 FPRINTF(stderrstderr, "ERROR: struct does not end with '}'\n");
1899 Exit(10);
1900 }
1901 ti=ti->next;
1902
1903 /* just skip the name */
1904 ti=ti->next;
1905
1906 if(!ti || g_strcmp0(ti->str,";")){
1907 FPRINTF(stderrstderr, "ERROR: struct does not end with ';'\n");
1908 Exit(10);
1909 }
1910 ti=ti->next;
1911
1912 token_list=ti;
1913 }
1914}
1915
1916/* this function will parse a
1917 typedef bitmap {
1918 construct and generate the appropriate code.
1919 the typedef will be g_removed from the token_list once it has been processed
1920 the function assumes that the typedef is the first object in the token_list
1921 the function will be called twice, once with pass=0 and once with pass=1
1922 which controls whether subdissectors are to be generated or whether the
1923 bitmap dissector itself is to be generated
1924
1925 bitmaps are by default 32 bits
1926*/
1927static void parsetypedefbitmap(int pass)
1928{
1929 token_item_t *ti, *tmpti;
1930 char *bitmap_name;
1931 char dissectorname[BASE_BUFFER_SIZE256], hf_bitname[BASE_BUFFER_SIZE256];
1932 int alignment;
1933 unsigned int val;
1934 char *name, *value;
1935 bracket_item_t *bi=NULL((void*)0);
1936
1937 ti=token_list;
1938 if(g_strcmp0(ti->str, "typedef")){
1939 FPRINTF(stderrstderr, "ERROR: typedefbitmap first token is not 'typedef'\n");
1940 Exit(10);
1941 }
1942 ti=ti->next;
1943
1944 alignment=4; /* default size is 32 bits */
1945
1946 if(!g_strcmp0(ti->str, "[")){
1947 ti=parsebrackets(ti, &bi);
1948 }
1949 /* check that we know how to handle the bracket thing */
1950 if(bi){
1951 if(bi->flags&(~(BI_BITMAP320x00000200|BI_BITMAP80x00000100))){
1952 FPRINTF(stderrstderr, "ERROR: typedefbitmap unknown bracket flags encountered : 0x%08x\n",bi->flags);
1953 Exit(10);
1954 }
1955 if(bi->flags&BI_BITMAP320x00000200){
1956 alignment=4;
1957 }
1958 if(bi->flags&BI_BITMAP80x00000100){
1959 alignment=1;
1960 }
1961 }
1962
1963
1964 if(g_strcmp0(ti->str, "bitmap")){
1965 FPRINTF(stderrstderr, "ERROR: typedefbitmap second token is not 'bitmap'\n");
1966 Exit(10);
1967 }
1968 ti=ti->next;
1969
1970 if(g_strcmp0(ti->str, "{")){
1971 FPRINTF(stderrstderr, "ERROR: typedefbitmap third token is not '{'\n");
1972 Exit(10);
1973 }
1974 ti=ti->next;
1975
1976 /* search forward until the '}' so we can find the name of the bitmap */
1977 for(tmpti=ti;tmpti;tmpti=tmpti->next){
1978 if(!g_strcmp0(tmpti->str, "{")){
1979 FPRINTF(stderrstderr, "ERROR: typedefbitmap '{' encountered inside bitmap\n");
1980 Exit(10);
1981 }
1982 if(!g_strcmp0(tmpti->str, "}")){
1983 break;
1984 }
1985 }
1986 if (!tmpti || !tmpti->next){
1987 FPRINTF(stderrstderr, "ERROR: typedefbitmap missing matching '}'\n");
1988 Exit(10);
1989 }
1990 bitmap_name=tmpti->next->str;
1991 snprintf(dissectorname, DISSECTORNAME_MAXLEN256, "%s_dissect_%s", ifname, bitmap_name);
1992
1993 FPRINTF(NULL((void*)0),"\nBITMAP:%s pass:%d\n-------\n",bitmap_name,pass);
1994
1995 /* pass 1 generate header for the struct dissector */
1996 if(pass==1){
1997 FPRINTF(eth_ett, "static int ett_%s_%s;\n", ifname, bitmap_name);
1998 FPRINTF(eth_ettarr, " &ett_%s_%s,\n", ifname, bitmap_name);
1999 FPRINTF(eth_hdr, "int %s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep, int hf_index, uint32_t param);\n", dissectorname);
2000 FPRINTF(eth_code, "\n");
2001 FPRINTF(eth_code, "int\n");
2002 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *parent_tree, dcerpc_info *di, uint8_t *drep, int hf_index, uint32_t param _U_)\n", dissectorname);
2003 FPRINTF(eth_code, "{\n");
2004 FPRINTF(eth_code, " proto_item *item=NULL;\n");
2005 FPRINTF(eth_code, " proto_tree *tree=NULL;\n");
2006 switch(alignment){
2007 case 1:
2008 FPRINTF(eth_code, " uint8_t flags;\n");
2009 FPRINTF(eth_code, "\n");
2010 break;
2011 case 4:
2012 FPRINTF(eth_code, " uint32_t flags;\n");
2013 FPRINTF(eth_code, "\n");
2014 FPRINTF(eth_code, " ALIGN_TO_4_BYTES;\n");
2015 break;
2016 default:
2017 FPRINTF(stderrstderr, "ERROR: typedefbitmap can not handle alignment:%d\n",alignment);
2018 Exit(10);
2019 }
2020 FPRINTF(eth_code, "\n");
2021 FPRINTF(eth_code, " if(parent_tree){\n");
2022 FPRINTF(eth_code, " item=proto_tree_add_item(parent_tree, hf_index, tvb, offset, %d, ENC_LITTLE_ENDIAN);\n", alignment);
2023 FPRINTF(eth_code, " tree=proto_item_add_subtree(item, ett_%s_%s);\n", ifname, bitmap_name);
2024 FPRINTF(eth_code, " }\n");
2025 FPRINTF(eth_code, "\n");
2026 switch(alignment){
2027 case 1:
2028 FPRINTF(eth_code, " offset=dissect_ndr_uint8(tvb, offset, pinfo, NULL, di, drep, -1, &flags);\n");
2029 FPRINTF(eth_code, "\n");
2030 break;
2031 case 4:
2032 FPRINTF(eth_code, " offset=dissect_ndr_uint32(tvb, offset, pinfo, NULL, di, drep, -1, &flags);\n");
2033 FPRINTF(eth_code, "\n");
2034 break;
2035 default:
2036 FPRINTF(stderrstderr, "ERROR: typedefbitmap can not handle alignment:%d\n",alignment);
2037 Exit(10);
2038 }
2039 FPRINTF(eth_code, "\n");
2040 }
2041
2042 /* scan the struct and create call for all bits */
2043 while(ti){
2044 if(!g_strcmp0(ti->str, "}")){
2045 break;
2046 }
2047 if(!g_strcmp0(ti->str, "[")){
2048 FPRINTF(stderrstderr, "ERROR: typedefbitmap can not handle '[' yet\n");
2049 Exit(10);
2050 }
2051
2052 name=ti->str;
2053 ti=ti->next;
2054 snprintf(hf_bitname, BASE_BUFFER_SIZE256, "hf_%s_%s_%s", ifname, bitmap_name, name);
2055
2056 if(g_strcmp0(ti->str, "=")){
2057 FPRINTF(stderrstderr, "ERROR: typedefbitmap i expected a '=' here\n");
2058 Exit(10);
2059 }
2060 ti=ti->next;
2061
2062 value=ti->str;
2063 ti=ti->next;
2064 val=0;
2065 if(!strncmp(value, "0x", 2)){
2066 sscanf(value, "0x%x", &val);
2067 } else {
2068 FPRINTF(stderrstderr, "ERROR: typedefbitmap can only handle hexadecimal constants\n");
2069 Exit(10);
2070 }
2071
2072 if((val == 0) || (val&(val-1))){
2073 FPRINTF(stderrstderr, "ERROR: typedefbitmap can only handle single bit fields\n");
2074 Exit(10);
2075 }
2076
2077 if(pass==0){
2078 char filter_name[BASE_BUFFER_SIZE256], base_name[BASE_BUFFER_SIZE256], tfs_name[BASE_BUFFER_SIZE256];
2079
2080 snprintf(filter_name, BASE_BUFFER_SIZE256, "%s.%s.%s", ifname, bitmap_name, name);
2081 snprintf(base_name, BASE_BUFFER_SIZE256, "%d", alignment*8);
2082 snprintf(tfs_name, BASE_BUFFER_SIZE256, "TFS(&%s_tfs)", name);
2083 register_hf_field(hf_bitname, name, filter_name, "FT_BOOLEAN", base_name, tfs_name, value, "");
2084
2085 FPRINTF(eth_code, "static const true_false_string %s_tfs = {\n",name);
2086 FPRINTF(eth_code, " \"%s is SET\",\n", name);
2087 FPRINTF(eth_code, " \"%s is NOT set\"\n", name);
2088 FPRINTF(eth_code, "};\n");
2089 FPRINTF(eth_code, "\n");
2090 }
2091
2092 if(pass==1){
2093 FPRINTF(eth_code, " proto_tree_add_boolean(tree, %s, tvb, offset-%d, %d, flags);\n", hf_bitname, alignment, alignment);
2094 FPRINTF(eth_code, " if(flags&%s){\n", value);
2095 FPRINTF(eth_code, " proto_item_append_text(item, \" %s\");\n", name);
2096 FPRINTF(eth_code, " }\n");
2097 FPRINTF(eth_code, " flags&=(~%s);\n", value);
2098 FPRINTF(eth_code, "\n");
2099 }
2100
2101 if(!g_strcmp0(ti->str, ",")){
2102 ti=ti->next;
2103 continue;
2104 }
2105 }
2106
2107 if(pass==1){
2108 FPRINTF(eth_code, " if(flags){\n");
2109 FPRINTF(eth_code, " proto_item_append_text(item, \"UNKNOWN-FLAGS\");\n");
2110 FPRINTF(eth_code, " }\n");
2111 FPRINTF(eth_code, "\n");
2112 FPRINTF(eth_code, " return offset;\n");
2113 FPRINTF(eth_code, "}\n");
2114 switch(alignment){
2115 case 1:
2116 register_new_type(bitmap_name, dissectorname, "FT_UINT8", "BASE_HEX", "0", "NULL", alignment);
2117 break;
2118 case 4:
2119 register_new_type(bitmap_name, dissectorname, "FT_UINT32", "BASE_HEX", "0", "NULL", alignment);
2120 break;
2121 default:
2122 FPRINTF(stderrstderr, "ERROR: typedefbitmap can not handle alignment:%d\n",alignment);
2123 Exit(10);
2124 }
2125 }
2126
2127 FPRINTF(NULL((void*)0),"\nEND BITMAP:%s pass:%d\n-------\n",bitmap_name,pass);
2128
2129 /* only advance token_list for pass==1
2130 ti now points to the '}' token
2131 */
2132 if(pass==1){
2133 if(!ti || g_strcmp0(ti->str,"}")){
2134 FPRINTF(stderrstderr, "ERROR: bitmap does not end with '}'\n");
2135 Exit(10);
2136 }
2137 ti=ti->next;
2138
2139 /* just skip the name */
2140 ti=ti->next;
2141
2142 if(!ti || g_strcmp0(ti->str,";")){
2143 FPRINTF(stderrstderr, "ERROR: bitmap does not end with ';'\n");
2144 Exit(10);
2145 }
2146 ti=ti->next;
2147
2148 token_list=ti;
2149 }
2150}
2151
2152/* a case tag might be a negative number, i.e. contain a '-' sign which
2153 is not valid inside a symbol name in c.
2154*/
2155static const char *
2156case2str(const char *str)
2157{
2158 char *newstr;
2159 if(str[0]!='-'){
2160 return str;
2161 }
2162 newstr=g_strdup(str)g_strdup_inline (str);
2163 newstr[0]='m';
2164 return newstr;
2165}
2166
2167/* this function will parse a
2168 typedef union {
2169 construct and generate the appropriate code.
2170 the typedef will be g_removed from the token_list once it has been processed
2171 the function assumes that the typedef is the first object in the token_list
2172 the function will be called twice, once with pass=0 and once with pass=1
2173 which controls whether subdissectors are to be generated or whether the
2174 union dissector itself is to be generated
2175*/
2176static void parsetypedefunion(int pass)
2177{
2178 char *union_name;
2179 token_item_t *ti, *tmpti;
2180 char dissectorname[BASE_BUFFER_SIZE256];
2181 bracket_item_t *bi=NULL((void*)0);
2182 char tmpstr[BASE_BUFFER_SIZE256], *ptmpstr;
2183 int level, num_pointers;
2184 static int alignment;
2185 type_item_t *type_item;
2186 char hf_index[BASE_BUFFER_SIZE256];
2187 int tag_alignment, item_alignment;
2188
2189 ti=token_list;
2190 if(g_strcmp0(ti->str, "typedef")){
2191 FPRINTF(stderrstderr, "ERROR: typedefunion first token is not 'typedef'\n");
2192 Exit(10);
2193 }
2194 ti=ti->next;
2195
2196 if(!g_strcmp0(ti->str, "[")){
2197 ti=parsebrackets(ti, &bi);
2198 }
2199 /* check that we know how to handle the bracket thing */
2200 if(bi){
2201 if(bi->flags&(~(BI_SWITCH_TYPE0x00000400))){
2202 FPRINTF(stderrstderr, "ERROR: typedefunion unknown bracket flags encountered : 0x%08x\n",bi->flags);
2203 Exit(10);
2204 }
2205 }
2206
2207 if(g_strcmp0(ti->str, "union")){
2208 FPRINTF(stderrstderr, "ERROR: typedefunion second token is not 'union'\n");
2209 Exit(10);
2210 }
2211 ti=ti->next;
2212
2213 if(g_strcmp0(ti->str, "{")){
2214 FPRINTF(stderrstderr, "ERROR: typedefunion third token is not '{'\n");
2215 Exit(10);
2216 }
2217 ti=ti->next;
2218
2219 /* search forward until the '}' so we can find the name of the union */
2220 for(tmpti=ti,level=0;tmpti;tmpti=tmpti->next){
2221 if(!g_strcmp0(tmpti->str, "{")){
2222 level++;
2223 continue;
2224 }
2225 if(!g_strcmp0(tmpti->str, "}")){
2226 if(!level){
2227 break;
2228 }
2229 level--;
2230 continue;
2231 }
2232 }
2233
2234 if (!tmpti || !tmpti->next){
2235 FPRINTF(stderrstderr, "ERROR: typedefunion missing matching '}'\n");
2236 Exit(10);
2237 }
2238 union_name=tmpti->next->str;
2239 snprintf(dissectorname, DISSECTORNAME_MAXLEN256, "%s_dissect_union_%s", ifname, union_name);
2240
2241 FPRINTF(NULL((void*)0),"\nUNION:%s pass:%d\n-------\n",union_name,pass);
2242
2243 if(bi && bi->flags&BI_SWITCH_TYPE0x00000400){
2244 tag_alignment=bi->union_tag_size;
2245 } else {
2246 tag_alignment=get_union_tag_size(union_name);
2247 }
2248
2249 /* this is pass 0 so reset alignment to the minimum possible value
2250 and update as items are processed.
2251 we need alignment when pass 1 is run
2252 */
2253 if(pass==0){
2254 alignment=tag_alignment;
2255 }
2256
2257 /* pass 1 generate header for the struct dissector */
2258 if(pass==1){
2259 FPRINTF(eth_ett, "static int ett_%s_%s;\n", ifname, union_name);
2260 FPRINTF(eth_ettarr, " &ett_%s_%s,\n", ifname, union_name);
2261 FPRINTF(eth_code, "\n");
2262 FPRINTF(eth_code, "static unsigned\n");
2263 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *parent_tree, dcerpc_info *di, uint8_t *drep, int hf_index, uint32_t param _U_)\n", dissectorname);
2264 FPRINTF(eth_code, "{\n");
2265 FPRINTF(eth_code, " proto_item *item=NULL;\n");
2266 FPRINTF(eth_code, " proto_tree *tree=NULL;\n");
2267 FPRINTF(eth_code, " int old_offset;\n");
2268 /* we do alignment on the tag itself here so that
2269 we skip any alignment padding prior to where the tag
2270 itself starts, this makes the byterange in the hexpane
2271 for the union expansion start with the first byte of the tag
2272 */
2273 switch(tag_alignment){
2274 case 1:
2275 break;
2276 case 2:
2277 FPRINTF(eth_code, " uint16_t level;\n");
2278 FPRINTF(eth_code, "\n");
2279 FPRINTF(eth_code, " ALIGN_TO_2_BYTES;\n");
2280 FPRINTF(eth_code, "\n");
2281 break;
2282 case 4:
2283 FPRINTF(eth_code, " uint32_t level = 0;\n");
2284 FPRINTF(eth_code, "\n");
2285 FPRINTF(eth_code, " ALIGN_TO_4_BYTES;\n");
2286 FPRINTF(eth_code, "\n");
2287 break;
2288 default:
2289 FPRINTF(stderrstderr, "ERROR: typedefunion 1 can not handle alignment:%d\n",alignment);
2290 Exit(10);
2291 }
2292 FPRINTF(eth_code, " old_offset=offset;\n");
2293 FPRINTF(eth_code, " if(parent_tree){\n");
2294 FPRINTF(eth_code, " tree=proto_tree_add_subtree(parent_tree, tvb, offset, -1, ett_%s_%s, &item, \"%s\");\n", ifname, union_name, union_name);
2295 FPRINTF(eth_code, " }\n");
2296 FPRINTF(eth_code, "\n");
2297 switch(tag_alignment){
2298 case 1:
2299 break;
2300 case 2:
2301 FPRINTF(eth_code, " offset=dissect_ndr_uint16(tvb, offset, pinfo, tree,\n");
2302 FPRINTF(eth_code, " di, drep, hf_index, &level);\n");
2303 break;
2304 case 4:
2305 FPRINTF(eth_code, " offset=dissect_ndr_uint32(tvb, offset, pinfo, tree,\n");
2306 FPRINTF(eth_code, " di, drep, hf_index, &level);\n");
2307 break;
2308 default:
2309 FPRINTF(stderrstderr, "ERROR: typedefunion 2 can not handle alignment:%d\n",alignment);
2310 Exit(10);
2311 }
2312 FPRINTF(eth_code, "\n");
2313 FPRINTF(eth_code, " switch(level){\n");
2314 }
2315
2316 /* scan the struct and create all subdissectors */
2317 level=0;
2318 while(ti){
2319 if(!g_strcmp0(ti->str, "{")){
2320 ti=ti->next;
2321 level++;
2322 continue;
2323 }
2324 if(!g_strcmp0(ti->str, "}")){
2325 if(!level){
2326 break;
2327 }
2328 ti=ti->next;
2329 level--;
2330 continue;
2331 }
2332 if(!g_strcmp0(ti->str, "[")){
2333 ti=parsebrackets(ti, &bi);
2334 continue;
2335 }
2336
2337 if(!bi){
2338 FPRINTF(stderrstderr, "ERROR : typedefunion no brackets found for case\n");
2339 Exit(10);
2340 }
2341 /* make sure we catch when we havent implemented everything
2342 yet.
2343 we currently only know about CASE and CASE_DEFAULT flags
2344 */
2345 if(bi->flags&(~(BI_CASE0x00000001|BI_CASE_DEFAULT0x00000002|BI_POINTER0x00000040))){
2346 FPRINTF(stderrstderr, "ERROR: typedefunion unknown bracket flags encountered : 0x%08x\n",bi->flags);
2347 Exit(10);
2348 }
2349 if(!(bi->flags&BI_CASE0x00000001)){
2350 FPRINTF(stderrstderr, "ERROR : typedefunion no case found in brackets\n");
2351 Exit(10);
2352 }
2353#ifdef g_removeD
2354 /* only empty default cases for now */
2355 if(bi->flags&BI_CASE_DEFAULT0x00000002){
2356 if(g_strcmp0(ti->str,";")){
2357 FPRINTF(stderrstderr, "ERROR: default tag is not empty\n");
2358 Exit(10);
2359 }
2360 ti=ti->next;
2361 continue;
2362 }
2363#endif
2364
2365 /* just skip all and any 'empty' arms */
2366 if(!g_strcmp0(ti->str, ";")){
2367 ti=ti->next;
2368 continue;
2369 }
2370
2371 /* handle the type, verify that we KNOW this type */
2372 type_item=find_type(ti->str);
2373 if(!type_item){
2374 FPRINTF(stderrstderr, "ERROR : typedefunion unknown type %s\n",ti->str);
2375 Exit(10);
2376 }
2377 ti=ti->next;
2378 /* count the levels of pointers */
2379 for(num_pointers=0;!g_strcmp0(ti->str, "*");ti=ti->next){
2380 num_pointers++;
2381 }
2382
2383 /* keep track of alignment */
2384 if(num_pointers){
2385 item_alignment=4;
2386 } else {
2387 item_alignment=type_item->alignment;
2388 }
2389 if(alignment<item_alignment){
2390 alignment=item_alignment;
2391 }
2392
2393 snprintf(hf_index, BASE_BUFFER_SIZE256, "hf_%s_%s_%s_%s", ifname, union_name, case2str(bi->case_name), ti->str);
2394 /* pass 0 generate subdissectors */
2395 if(pass==0){
2396 char filter_name[BASE_BUFFER_SIZE256];
2397 const char *hf;
2398
2399 snprintf(tmpstr, BASE_BUFFER_SIZE256, "%s_dissect_union_%s_%s_%s", ifname, union_name, case2str(bi->case_name), ti->str);
2400 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
2401
2402 snprintf(filter_name, BASE_BUFFER_SIZE256, "%s.%s.%s", ifname, union_name, ti->str);
2403 hf=register_hf_field(hf_index, ti->str, filter_name, type_item->ft_type, type_item->base_type, type_item->vals, type_item->mask, "");
2404
2405 FPRINTF(eth_code, "static unsigned\n");
2406 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep)\n", ptmpstr);
2407 FPRINTF(eth_code, "{\n");
2408 FPRINTF(eth_code, " uint32_t param=%s;\n",find_dissector_param_value(ptmpstr));
2409 FPRINTF(eth_code, " offset=%s(tvb, offset, pinfo, tree, di, drep, %s, param);\n", type_item->dissector, hf);
2410 FPRINTF(eth_code, " return offset;\n");
2411 FPRINTF(eth_code, "}\n");
2412 FPRINTF(eth_code, "\n");
2413
2414 /* handle pointers */
2415 while(num_pointers--){
2416 snprintf(tmpstr, BASE_BUFFER_SIZE256, "%s_%s", ptmpstr, "unique");
2417 FPRINTF(eth_code, "static unsigned\n");
2418 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep)\n", tmpstr);
2419 FPRINTF(eth_code, "{\n");
2420 FPRINTF(eth_code, " offset=dissect_ndr_embedded_pointer(tvb, offset, pinfo, tree, di, drep, %s, NDR_POINTER_UNIQUE, \"%s\", -1);\n", ptmpstr, ti->str);
2421 FPRINTF(eth_code, " return offset;\n");
2422 FPRINTF(eth_code, "}\n");
2423 FPRINTF(eth_code, "\n");
2424
2425 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
2426
2427 }
2428 }
2429
2430 if(pass==1){
2431 /* handle pointers */
2432 snprintf(tmpstr, BASE_BUFFER_SIZE256, "%s_dissect_union_%s_%s_%s", ifname, union_name, case2str(bi->case_name), ti->str);
2433 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
2434 while(num_pointers--){
2435 snprintf(tmpstr, BASE_BUFFER_SIZE256, "%s_%s", ptmpstr, "unique");
2436 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
2437 }
2438
2439 if(bi->flags&BI_CASE_DEFAULT0x00000002){
2440 FPRINTF(eth_code, " default:\n");
2441 } else {
2442 FPRINTF(eth_code, " case %s:\n",bi->case_name);
2443 }
2444 /* each arm itself is aligned independently */
2445 switch(item_alignment){
2446 case 1:
2447 break;
2448 case 2:
2449 FPRINTF(eth_code, " ALIGN_TO_2_BYTES;\n");
2450 break;
2451 case 4:
2452 FPRINTF(eth_code, " ALIGN_TO_4_BYTES;\n");
2453 break;
2454 case 8:
2455 FPRINTF(eth_code, " ALIGN_TO_8_BYTES;\n");
2456 break;
2457 default:
2458 FPRINTF(stderrstderr, "ERROR: typedefunion 3 can not handle alignment:%d\n",item_alignment);
2459 Exit(10);
2460 }
2461 FPRINTF(eth_code, " offset=%s(tvb, offset, pinfo, tree, di, drep);\n", ptmpstr);
2462 FPRINTF(eth_code, " break;\n");
2463 FPRINTF(eth_code, "\n");
2464 }
2465 ti=ti->next;
2466
2467 if(g_strcmp0(ti->str,";")){
2468 FPRINTF(stderrstderr, "ERROR: field does not end with ';'\n");
2469 Exit(10);
2470 }
2471 ti=ti->next;
2472 }
2473
2474 if(pass==1){
2475 FPRINTF(eth_code, " }\n");
2476 FPRINTF(eth_code, "\n");
2477 FPRINTF(eth_code, " proto_item_set_len(item, offset-old_offset);\n");
2478 FPRINTF(eth_code, "\n");
2479 FPRINTF(eth_code, " return offset;\n");
2480 FPRINTF(eth_code, "}\n");
2481 switch(tag_alignment){
2482 case 2:
2483 register_new_type(union_name, dissectorname, "FT_UINT16", "BASE_DEC", "0", "NULL", alignment);
2484 break;
2485 case 4:
2486 register_new_type(union_name, dissectorname, "FT_UINT32", "BASE_DEC", "0", "NULL", alignment);
2487 break;
2488 default:
2489 FPRINTF(stderrstderr, "ERROR: typedefunion 4 can not handle alignment:%d\n",alignment);
2490 Exit(10);
2491 }
2492 }
2493
2494 FPRINTF(NULL((void*)0),"\nEND UNION:%s pass:%d\n-------\n",union_name,pass);
2495
2496 /* only advance token_list for pass==1
2497 ti now points to the '}' token
2498 */
2499 if(pass==1){
2500 if(!ti || g_strcmp0(ti->str,"}")){
2501 FPRINTF(stderrstderr, "ERROR: union does not end with '}'\n");
2502 Exit(10);
2503 }
2504 ti=ti->next;
2505
2506 /* just skip the name */
2507 ti=ti->next;
2508
2509 if(!ti || g_strcmp0(ti->str,";")){
2510 FPRINTF(stderrstderr, "ERROR: union does not end with ';'\n");
2511 Exit(10);
2512 }
2513 ti=ti->next;
2514
2515 token_list=ti;
2516 }
2517}
2518
2519
2520/* this function will parse a
2521 WERROR function (
2522 construct and generate the appropriate code.
2523 the function will be g_removed from the token_list once it has been processed
2524 the function assumes that the function is the first object in the token_list
2525 the function will be called three times with
2526 pass=0 generate subdissectors and entries for the function table
2527 pass=1 generate code for the REQUEST
2528 pass=2 generate code for the REPLY
2529*/
2530static void parsefunction(int pass)
2531{
2532 char *function_name;
2533 static int funcno=0;
2534 token_item_t *ti;
2535 bracket_item_t *bi=NULL((void*)0);
2536 pointer_item_t *pi;
2537 const char *pointer_type;
2538
2539 char tmpstr[BASE_BUFFER_SIZE256], *ptmpstr;
2540 int level, num_pointers;
2541 type_item_t *type_item;
2542 char hf_index[BASE_BUFFER_SIZE256];
2543
2544 ti=token_list;
2545 if(g_strcmp0(ti->str, "WERROR")){
2546 FPRINTF(stderrstderr, "ERROR: function first token is not 'WERROR'\n");
2547 Exit(10);
2548 }
2549 ti=ti->next;
2550
2551 function_name=ti->str;
2552 ti=ti->next;
2553
2554 if(g_strcmp0(ti->str, "(")){
2555 FPRINTF(stderrstderr, "ERROR: function third token is not '('\n");
2556 Exit(10);
2557 }
2558 ti=ti->next;
2559
2560 FPRINTF(NULL((void*)0),"\nFUNCTION:%s pass:%d\n-------\n",function_name,pass);
2561
2562 if(pass==0){
2563 FPRINTF(eth_ft, " { %d, \"%s\",\n",funcno,function_name);
2564 FPRINTF(eth_ft, " %s_dissect_%s_request,\n", ifname, function_name);
2565 FPRINTF(eth_ft, " %s_dissect_%s_response },\n", ifname, function_name);
2566 funcno++;
2567 }
2568
2569 /* pass 1,2 generate header for the function dissector */
2570 if((pass==1)||(pass==2)){
2571 FPRINTF(eth_code, "\n");
2572 FPRINTF(eth_code, "static unsigned\n");
2573 FPRINTF(eth_code, "%s_dissect_%s_%s(tvbuff_t *tvb _U_, unsigned offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, dcerpc_info* di _U_, uint8_t *drep _U_)\n", ifname, function_name, (pass==1)?"request":"response");
2574 FPRINTF(eth_code, "{\n");
2575 }
2576
2577 /* scan the struct and create all subdissectors */
2578 level=0;
2579 while(ti){
2580 if(!g_strcmp0(ti->str, "(")){
2581 ti=ti->next;
2582 level++;
2583 continue;
2584 }
2585 if(!g_strcmp0(ti->str, ")")){
2586 if(!level){
2587 break;
2588 }
2589 ti=ti->next;
2590 level--;
2591 continue;
2592 }
2593 if(!g_strcmp0(ti->str, "[")){
2594 ti=parsebrackets(ti, &bi);
2595 continue;
2596 }
2597
2598 if(!bi){
2599 FPRINTF(stderrstderr, "ERROR : function no brackets found for case\n");
2600 Exit(10);
2601 }
2602
2603 /* make sure we catch when we havent implemented everything
2604 yet.
2605 we currently only know about IN and OUT flags
2606 */
2607 if(bi->flags&(~(BI_IN0x00000004|BI_OUT0x00000008|BI_POINTER0x00000040|BI_SIZE_IS0x00000010|BI_LENGTH_IS0x00000020))){
2608 FPRINTF(stderrstderr, "ERROR: function unknown bracket flags encountered : 0x%08x\n",bi->flags);
2609 Exit(10);
2610 }
2611 if(!(bi->flags&(BI_IN0x00000004|BI_OUT0x00000008))){
2612 FPRINTF(stderrstderr, "ERROR : function parameter is neither in nor out\n");
2613 Exit(10);
2614 }
2615
2616 /* handle the type, verify that we KNOW this type */
2617 type_item=find_type(ti->str);
2618 if(!type_item){
2619 FPRINTF(stderrstderr, "ERROR : function unknown type %s\n",ti->str);
2620 Exit(10);
2621 }
2622 ti=ti->next;
2623 /* count the levels of pointers */
2624 for(num_pointers=0;!g_strcmp0(ti->str, "*");ti=ti->next){
2625 num_pointers++;
2626 }
2627
2628 /* now that we know how many real pointer there were we must
2629 prepend default pointers to the list so it has the right
2630 length.
2631 */
2632 pi=prepend_pointer_list(bi->pointer_list, num_pointers);
2633
2634 snprintf(hf_index, BASE_BUFFER_SIZE256, "hf_%s_%s_%s", ifname, function_name, ti->str);
2635 /* pass 0 generate subdissectors */
2636 if(pass==0){
2637 char filter_name[BASE_BUFFER_SIZE256];
2638 const char *hf;
2639
2640 snprintf(tmpstr, BASE_BUFFER_SIZE256, "%s_dissect_%s_%s", ifname, function_name, ti->str);
2641 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
2642
2643 snprintf(filter_name, BASE_BUFFER_SIZE256, "%s.%s.%s", ifname, function_name, ti->str);
2644 hf=register_hf_field(hf_index, ti->str, filter_name, type_item->ft_type, type_item->base_type, type_item->vals, type_item->mask, "");
2645
2646 FPRINTF(eth_code, "static unsigned\n");
2647 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep)\n", ptmpstr);
2648 FPRINTF(eth_code, "{\n");
2649 FPRINTF(eth_code, " uint32_t param=%s;\n",find_dissector_param_value(ptmpstr));
2650 FPRINTF(eth_code, " offset=%s(tvb, offset, pinfo, tree, di, drep, %s, param);\n", type_item->dissector, hf);
2651 FPRINTF(eth_code, " return offset;\n");
2652 FPRINTF(eth_code, "}\n");
2653 FPRINTF(eth_code, "\n");
2654
2655
2656 /* handle switch_is */
2657 switch(bi->flags&(BI_SIZE_IS0x00000010|BI_LENGTH_IS0x00000020)){
2658 case 0:
2659 break;
2660 case BI_SIZE_IS0x00000010|BI_LENGTH_IS0x00000020:
2661 snprintf(tmpstr, BASE_BUFFER_SIZE256, "ucvarray_%s", ptmpstr);
2662 FPRINTF(eth_code, "static unsigned\n");
2663 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep)\n", tmpstr);
2664 FPRINTF(eth_code, "{\n");
2665 FPRINTF(eth_code, " offset=dissect_ndr_ucvarray(tvb, offset, pinfo, tree, di, drep, %s);\n", ptmpstr);
2666 FPRINTF(eth_code, " return offset;\n");
2667 FPRINTF(eth_code, "}\n");
2668 FPRINTF(eth_code, "\n");
2669 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
2670 break;
2671 case BI_SIZE_IS0x00000010:
2672 snprintf(tmpstr, BASE_BUFFER_SIZE256, "ucarray_%s", ptmpstr);
2673 FPRINTF(eth_code, "static unsigned\n");
2674 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep)\n", tmpstr);
2675 FPRINTF(eth_code, "{\n");
2676 FPRINTF(eth_code, " offset=dissect_ndr_ucarray(tvb, offset, pinfo, tree, di, drep, %s);\n", ptmpstr);
2677 FPRINTF(eth_code, " return offset;\n");
2678 FPRINTF(eth_code, "}\n");
2679 FPRINTF(eth_code, "\n");
2680 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
2681 break;
2682 default:
2683 FPRINTF(stderrstderr, "ERROR: typedeffunction can not handle this combination of sizeis/lengthis\n");
2684 Exit(10);
2685 }
2686
2687 /* handle pointers */
2688 while(num_pointers--){
2689 pointer_type=pi->type;
2690 pi=pi->next;
2691 snprintf(tmpstr, BASE_BUFFER_SIZE256, "%s_%s", pointer_type, ptmpstr);
2692 FPRINTF(eth_code, "static unsigned\n");
2693 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep)\n", tmpstr);
2694 FPRINTF(eth_code, "{\n");
2695 FPRINTF(eth_code, " offset=dissect_ndr_toplevel_pointer(tvb, offset, pinfo, tree, di, drep, %s, %s, \"%s\", -1);\n", ptmpstr, ptr_to_define(pointer_type), ti->str);
2696 FPRINTF(eth_code, " return offset;\n");
2697 FPRINTF(eth_code, "}\n");
2698 FPRINTF(eth_code, "\n");
2699
2700 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
2701
2702 }
2703 }
2704
2705 if((pass==1)||(pass==2)){
2706 snprintf(tmpstr, BASE_BUFFER_SIZE256, "%s_dissect_%s_%s", ifname, function_name, ti->str);
2707 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
2708
2709 switch(bi->flags&(BI_SIZE_IS0x00000010|BI_LENGTH_IS0x00000020)){
2710 case 0:
2711 break;
2712 case BI_SIZE_IS0x00000010|BI_LENGTH_IS0x00000020:
2713 snprintf(tmpstr, BASE_BUFFER_SIZE256, "ucvarray_%s", ptmpstr);
2714 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
2715 break;
2716 case BI_SIZE_IS0x00000010:
2717 snprintf(tmpstr, BASE_BUFFER_SIZE256, "ucarray_%s", ptmpstr);
2718 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
2719 break;
2720 default:
2721 FPRINTF(stderrstderr, "ERROR: typedeffunction can not handle this combination of sizeis/lengthis\n");
2722 Exit(10);
2723 }
2724
2725 /* handle pointers */
2726 while(num_pointers--){
2727 pointer_type=pi->type;
2728 pi=pi->next;
2729 snprintf(tmpstr, BASE_BUFFER_SIZE256, "%s_%s", pointer_type, ptmpstr);
2730 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
2731 }
2732
2733 if((pass==1)&&(bi->flags&BI_IN0x00000004)){
2734 FPRINTF(eth_code, " offset=%s(tvb, offset, pinfo, tree, di, drep);\n", ptmpstr);
2735 FPRINTF(eth_code, " offset=dissect_deferred_pointers(pinfo, tvb, offset, di, drep);\n");
2736 FPRINTF(eth_code, "\n");
2737 }
2738 if((pass==2)&&(bi->flags&BI_OUT0x00000008)){
2739 FPRINTF(eth_code, " offset=%s(tvb, offset, pinfo, tree, di, drep);\n", ptmpstr);
2740 FPRINTF(eth_code, " offset=dissect_deferred_pointers(pinfo, tvb, offset, di, drep);\n");
2741 FPRINTF(eth_code, "\n");
2742 }
2743 }
2744 ti=ti->next;
2745
2746
2747 if(!g_strcmp0(ti->str,",")){
2748 ti=ti->next;
2749 continue;
2750 }
2751 }
2752
2753 if((pass==1)||(pass==2)){
2754 if(pass==2){
2755 FPRINTF(eth_code, " offset=dissect_ntstatus(tvb, offset, pinfo, tree, di, drep, %s, NULL);\n", hf_status);
2756 FPRINTF(eth_code, "\n");
2757 }
2758 FPRINTF(eth_code, "\n");
2759 FPRINTF(eth_code, " return offset;\n");
2760 FPRINTF(eth_code, "}\n");
2761 }
2762
2763 FPRINTF(NULL((void*)0),"\nEND FUNCTION:%s pass:%d\n-------\n",function_name,pass);
2764
2765 /* only advance token_list for pass==2
2766 ti now points to the ')' token
2767 */
2768 if(pass==2){
2769 if(!ti || g_strcmp0(ti->str,")")){
2770 FPRINTF(stderrstderr, "ERROR: function does not end with ')'\n");
2771 Exit(10);
2772 }
2773 ti=ti->next;
2774
2775 if(!ti || g_strcmp0(ti->str,";")){
2776 FPRINTF(stderrstderr, "ERROR: function does not end with ';'\n");
2777 Exit(10);
2778 }
2779 ti=ti->next;
2780
2781 token_list=ti;
2782 }
2783}
2784
2785
2786/* this function will parse a
2787 typedef enum {
2788 or a
2789 typedef [ v1_enum ] enum {
2790 construct and generate the appropriate code.
2791 the typedef will be g_removed from the token_list once it has been processed
2792 the function assumes that the typedef is the first object in the token_list
2793*/
2794static void parsetypedefenum(void)
2795{
2796 token_item_t *ti;
2797 enum_list_t *enum_list, *el, *lastel;
2798 char *p;
2799 long val;
2800 int eval, enumsize;
2801 char dissectorname[BASE_BUFFER_SIZE256], valsstring[BASE_BUFFER_SIZE256];
2802 char *hfvalsstring;
2803
2804 enumsize=16;
2805
2806 ti=token_list;
2807 if(g_strcmp0(ti->str, "typedef")){
2808 FPRINTF(stderrstderr, "ERROR: typedefenum first token is not 'typedef'\n");
2809 Exit(10);
2810 }
2811 ti=ti->next;
2812
2813 /* this could be a [ v1_enum ] */
2814 if(!g_strcmp0(ti->str, "[")){
2815 ti=ti->next;
2816
2817 if(g_strcmp0(ti->str, "v1_enum")){
2818 FPRINTF(stderrstderr, "ERROR: typedefenum not 'v1_enum' inside brackets\n");
2819 Exit(10);
2820 }
2821 ti=ti->next;
2822
2823 if(g_strcmp0(ti->str, "]")){
2824 FPRINTF(stderrstderr, "ERROR: typedefenum 'v1_enum' is not followed by ']'\n");
2825 Exit(10);
2826 }
2827 ti=ti->next;
2828
2829 enumsize=32;
2830 }
2831
2832
2833 if(g_strcmp0(ti->str, "enum")){
2834 FPRINTF(stderrstderr, "ERROR: typedefenum second token is not 'enum'\n");
2835 Exit(10);
2836 }
2837 ti=ti->next;
2838
2839 if(g_strcmp0(ti->str, "{")){
2840 FPRINTF(stderrstderr, "ERROR: typedefenum third token is not '{'\n");
2841 Exit(10);
2842 }
2843 ti=ti->next;
2844
2845 /* now parse all values until we find the "}" */
2846 eval=0;
2847 enum_list=NULL((void*)0);
2848 lastel=NULL((void*)0);
2849 while(1){
2850 /* check for '}' */
2851 if(!g_strcmp0(ti->str,"}")){
2852 ti=ti->next;
2853 break;
2854 }
2855
2856 /* handle 4 types of entries:
2857 * 1, CONST = value,
2858 * 2, CONST,
2859 * 3, CONST = value}
2860 * 4, CONST}
2861 */
2862 el=g_new0(enum_list_t, 1)((enum_list_t *) g_malloc0_n ((1), sizeof (enum_list_t)));
2863 if (!el) {
2864 FPRINTF(stderrstderr, "Can't allocate memory. Exit.\n");
2865 exit(10);
2866 }
2867 el->next=NULL((void*)0);
2868 if(!enum_list){
2869 enum_list=el;
2870 } else {
2871 lastel->next=el;
2872 }
2873 lastel=el;
2874
2875 /* grab CONST */
2876 el->name=ti->str;
2877 ti=ti->next;
2878
2879 /* grab separator */
2880 if(!g_strcmp0(ti->str,"=")){
2881 ti=ti->next;
2882 /* grab value */
2883 val=strtol(ti->str,&p,0);
2884 if (p==ti->str||*p) {
2885 FPRINTF(stderrstderr, "ERROR: typedefenum value is not a number\n");
2886 Exit(10);
2887 }
2888 el->val=(int)val;
2889 ti=ti->next;
2890 } else {
2891 el->val=eval;
2892 }
2893 eval=el->val+1;
2894
2895 /* check for ',' */
2896 if(!g_strcmp0(ti->str,",")){
2897 ti=ti->next;
2898 continue;
2899 }
2900
2901 /* check for '}' */
2902 if(!g_strcmp0(ti->str,"}")){
2903 ti=ti->next;
2904 break;
2905 }
2906
2907 FPRINTF(stderrstderr,"ERROR: typedefenum should not be reached\n");
2908 Exit(10);
2909 }
2910
2911 /* verify that it ends with a ';' */
2912 if(g_strcmp0(ti->next->str,";")){
2913 FPRINTF(stderrstderr,"ERROR enum terminator is not ';'\n");
2914 Exit(10);
2915 }
2916
2917 snprintf(valsstring, BASE_BUFFER_SIZE256, "%s_%s_vals", ifname, ti->str);
2918 snprintf(dissectorname, DISSECTORNAME_MAXLEN256, "%s_dissect_%s", ifname, ti->str);
2919
2920 FPRINTF(NULL((void*)0),"\nENUM:%s\n-------\n",ti->str);
2921
2922 FPRINTF(eth_hdr, "\n");
2923 for(el=enum_list;el;el=el->next){
2924 FPRINTF(eth_hdr, "#define %s %d\n", el->name, el->val);
2925 }
2926
2927 FPRINTF(eth_hdr, "\n");
2928 FPRINTF(eth_hdr, "extern const value_string %s[];\n", valsstring);
2929 FPRINTF(eth_hdr, "unsigned %s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep, int hf_index, uint32_t param);\n", dissectorname);
2930
2931 FPRINTF(eth_code, "\n");
2932 FPRINTF(eth_code, "const value_string %s[] = {\n", valsstring);
2933
2934 for(el=enum_list;el;el=el->next){
2935 FPRINTF(eth_code, " { %d , \"%s\" },\n", el->val, el->name);
2936 }
2937 FPRINTF(eth_code, " { 0 , NULL }\n");
2938 FPRINTF(eth_code, "};\n");
2939
2940 FPRINTF(eth_code, "\n");
2941 FPRINTF(eth_code, "int\n");
2942 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep, int hf_index, uint32_t param _U_)\n", dissectorname);
2943 FPRINTF(eth_code, "{\n");
2944 switch(enumsize){
2945 case 16:
2946 FPRINTF(eth_code, " offset=dissect_ndr_uint16(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
2947 break;
2948 case 32:
2949 FPRINTF(eth_code, " offset=dissect_ndr_uint32(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
2950 break;
2951 default:
2952 FPRINTF(stderrstderr,"ERROR enum unknown size\n");
2953 Exit(10);
2954 }
2955
2956 FPRINTF(eth_code, " return offset;\n");
2957 FPRINTF(eth_code, "}\n");
2958 FPRINTF(eth_code, "\n");
2959
2960 hfvalsstring = g_strdup_printf("VALS(%s)", valsstring);
2961 switch(enumsize){
2962 case 16:
2963 register_new_type(ti->str, dissectorname, "FT_INT16", "BASE_DEC", "0", hfvalsstring, 2);
2964 break;
2965 case 32:
2966 register_new_type(ti->str, dissectorname, "FT_INT32", "BASE_DEC", "0", hfvalsstring, 4);
2967 break;
2968 default:
2969 FPRINTF(stderrstderr,"ERROR enum unknown size\n");
2970 g_free(hfvalsstring)(__builtin_object_size ((hfvalsstring), 0) != ((size_t) - 1))
? g_free_sized (hfvalsstring, __builtin_object_size ((hfvalsstring
), 0)) : (g_free) (hfvalsstring)
;
2971 Exit(10);
2972 }
2973 g_free(hfvalsstring)(__builtin_object_size ((hfvalsstring), 0) != ((size_t) - 1))
? g_free_sized (hfvalsstring, __builtin_object_size ((hfvalsstring
), 0)) : (g_free) (hfvalsstring)
;
2974
2975 FPRINTF(NULL((void*)0),"\n----------\nEND ENUM:%s\n",ti->str);
2976
2977 /* skip past the name and the ';' */
2978 token_list=ti->next->next;
2979}
2980
2981typedef struct _trimmed_prefixes_t {
2982 struct _trimmed_prefixes_t *next;
2983 char *name;
2984} trimmed_prefixes_t;
2985static trimmed_prefixes_t *prefixes_to_trim=NULL((void*)0);
2986
2987static void preparetrimprefix(char *prefix_name)
2988{
2989 trimmed_prefixes_t *new_prefix;
2990 new_prefix=g_new0(trimmed_prefixes_t, 1)((trimmed_prefixes_t *) g_malloc0_n ((1), sizeof (trimmed_prefixes_t
)))
;
2991 if (!new_prefix) {
2992 FPRINTF(stderrstderr, "Can't allocate memory. Exit.\n");
2993 exit(10);
2994 }
2995 new_prefix->next=prefixes_to_trim;
2996 prefixes_to_trim=new_prefix;
2997 new_prefix->name=g_strdup(prefix_name)g_strdup_inline (prefix_name);
2998}
2999
3000static void
3001trimprefix(void)
3002{
3003 token_item_t *ti;
3004 trimmed_prefixes_t *pfx;
3005 size_t len;
3006
3007 for(pfx=prefixes_to_trim;pfx;pfx=pfx->next){
3008 len=strlen(pfx->name);
3009 for(ti=token_list;ti;ti=ti->next){
3010 if(!strncmp(ti->str, pfx->name, len)){
3011 ti->str+=len;
3012 }
3013 }
3014 }
3015}
3016
3017static int Exit(int code)
3018{
3019 FPRINTF(stderrstderr, "The tokens remaining when aborting:\n");
3020 printtokenlist(10);
3021
3022 exit(code);
3023}
3024
3025static void usage(void)
3026{
3027 FPRINTF(stderrstderr, "Usage: idl2wrs <interface>\n");
3028}
3029
3030static void
3031mergefile(const char *name, FILE *outfile)
3032{
3033 FILE *infile;
3034
3035 FPRINTF(outfile, "\n\n/* INCLUDED FILE : %s */\n", name);
3036 infile=g_fopenfopen(name, "r");
3037 while(!feof(infile)){
1
Loop condition is true. Entering loop body
4
Loop condition is true. Entering loop body
3038 int ch;
3039 ch=fgetc(infile);
2
Assuming this stream operation fails
5
File position of the stream might be 'indeterminate' after a failed operation. Can cause undefined behavior
3040 if(ch!=-1){
3
Taking false branch
3041 fputc(ch, outfile);
3042 }
3043 }
3044 fclose(infile);
3045 FPRINTF(outfile, "/* END OF INCLUDED FILE : %s */\n\n\n", name);
3046}
3047
3048
3049
3050static char *
3051str_read_string(char *str, char **name)
3052{
3053 char tmpstr[BASE_BUFFER_SIZE256], *strptr;
3054 int skip_blanks;
3055 int quoted_string;
3056
3057 strptr=tmpstr;
3058 skip_blanks=1;
3059 quoted_string=0;
3060 while(1){
3061 if(!*str){
3062 *strptr=0;
3063 *name=g_strdup(tmpstr)g_strdup_inline (tmpstr);
3064 return str;
3065 }
3066 if(skip_blanks){
3067 if( (*str==' ') || (*str=='\t') ){
3068 str++;
3069 continue;
3070 }
3071 if( *str=='"' ){
3072 str++;
3073 quoted_string=1;
3074 }
3075 skip_blanks=0;
3076 continue;
3077 }
3078 if( (*str==' ') || (*str=='\t') ){
3079 if(quoted_string){
3080 *strptr++ = *str++;
3081 continue;
3082 }
3083 *strptr=0;
3084 *name=g_strdup(tmpstr)g_strdup_inline (tmpstr);
3085 return str;
3086 }
3087 if( (*str=='"') || (*str=='\n') ){
3088 *strptr=0;
3089 *name=g_strdup(tmpstr)g_strdup_inline (tmpstr);
3090 return ++str;
3091 }
3092 *strptr++ = *str++;
3093 }
3094 return NULL((void*)0);
3095}
3096
3097static void
3098readcnffile(FILE *fh)
3099{
3100 char cnfline[4 * BASE_BUFFER_SIZE256];
3101
3102 FPRINTF(NULL((void*)0), "Reading conformance file\n=======================\n");
3103 while(!feof(fh)){
3104 cnfline[0]=0;
3105 if(!fgets(cnfline, 4 * BASE_BUFFER_SIZE256, fh) || !cnfline[0]){
3106 continue;
3107 }
3108 if(cnfline[0]=='#'){
3109 /* ignore all comments */
3110 } else if(!strncmp(cnfline, "NOEMIT", 6)){
3111 no_emit_item_t *nei;
3112 char *str, *name;
3113
3114 str=cnfline+6;
3115 str_read_string(str, &name);
3116 nei=g_new0(no_emit_item_t, 1)((no_emit_item_t *) g_malloc0_n ((1), sizeof (no_emit_item_t)
))
;
3117 if (!nei) {
3118 FPRINTF(stderrstderr, "Can't allocate memory. Exit.\n");
3119 exit(10);
3120 }
3121 nei->next=no_emit_list;
3122 no_emit_list=nei;
3123 nei->name=name;
3124 FPRINTF(NULL((void*)0), "NOEMIT : %s\n", nei->name);
3125 } else if(!strncmp(cnfline, "TYPE", 4)){
3126 char *name, *dissectorname, *ft_type, *base_type;
3127 char *mask, *valsstring, *al;
3128 char *str;
3129 int alignment;
3130
3131 str=cnfline+4;
3132 str=str_read_string(str, &name);
3133 str=str_read_string(str, &dissectorname);
3134 str=str_read_string(str, &ft_type);
3135 str=str_read_string(str, &base_type);
3136 str=str_read_string(str, &mask);
3137 str=str_read_string(str, &valsstring);
3138 str_read_string(str, &al);
3139 if (ws_strtoi32(al, NULL((void*)0), &alignment)) {
3140 FPRINTF(NULL((void*)0), "TYPE : X%s,%sX\n", name, dissectorname);
3141 register_new_type(name, dissectorname, ft_type, base_type, mask, valsstring, alignment);
3142 }
3143 } else if(!strncmp(cnfline, "PARAM_VALUE", 11)){
3144 char *dissectorname, *value;
3145 char *str;
3146
3147 str=cnfline+11;
3148 str=str_read_string(str, &dissectorname);
3149 str_read_string(str, &value);
3150
3151 FPRINTF(NULL((void*)0), "PARAM_VALUE : %s=%s\n", dissectorname,value);
3152 register_dissector_param_value(dissectorname, value);
3153 } else if(!strncmp(cnfline, "HF_FIELD", 8)){
3154 char *hf_index, *title, *filter_name, *ft_type;
3155 char *base_type, *valsstring, *mask, *blurb;
3156 char *str;
3157
3158 str=cnfline+8;
3159 str=str_read_string(str, &hf_index);
3160 str=str_read_string(str, &title);
3161 str=str_read_string(str, &filter_name);
3162 str=str_read_string(str, &ft_type);
3163 str=str_read_string(str, &base_type);
3164 str=str_read_string(str, &valsstring);
3165 str=str_read_string(str, &mask);
3166 str_read_string(str, &blurb);
3167 FPRINTF(NULL((void*)0), "HF_FIELD: %s \"%s\"\n", hf_index, title);
3168 register_hf_field(hf_index, title, filter_name, ft_type, base_type, valsstring, mask, blurb);
3169 } else if(!strncmp(cnfline, "HF_RENAME", 9)){
3170 char *old_name, *new_name;
3171 char *str;
3172
3173 str=cnfline+9;
3174 str=str_read_string(str, &old_name);
3175 str_read_string(str, &new_name);
3176 FPRINTF(NULL((void*)0), "HF_RENAME: %s -> %s\n", old_name, new_name);
3177 register_hf_rename(old_name, new_name);
3178 } else if(!strncmp(cnfline, "UNION_TAG_SIZE", 14)){
3179 char *union_name, *union_tag;
3180 int32_t union_tag_size;
3181 union_tag_size_item_t *utsi;
3182 char *str;
3183
3184 str=cnfline+14;
3185 str=str_read_string(str, &union_name);
3186 str_read_string(str, &union_tag);
3187 if (!ws_strtoi32(union_tag, NULL((void*)0), &union_tag_size)) {
3188 FPRINTF(NULL((void*)0), "UNION_TAG_SIZE: invalid string: %s\n", union_tag);
3189 exit(10);
3190 }
3191 FPRINTF(NULL((void*)0), "UNION_TAG_SIZE: %s == %d\n", union_name, union_tag_size);
3192 utsi=g_new0(union_tag_size_item_t, 1)((union_tag_size_item_t *) g_malloc0_n ((1), sizeof (union_tag_size_item_t
)))
;
3193 if (!utsi) {
3194 FPRINTF(stderrstderr, "Can't allocate memory. Exit.\n");
3195 exit(10);
3196 }
3197 utsi->next=union_tag_size_list;
3198 union_tag_size_list=utsi;
3199 utsi->name=g_strdup(union_name)g_strdup_inline (union_name);
3200 utsi->size=union_tag_size;
3201 } else if(!strncmp(cnfline, "STRIP_PREFIX", 12)){
3202 char *prefix_name;
3203 char *str;
3204
3205 str=cnfline+12;
3206 str_read_string(str, &prefix_name);
3207 FPRINTF(NULL((void*)0), "STRIP_PREFIX: %s\n", prefix_name);
3208 preparetrimprefix(prefix_name);
3209 } else {
3210 FPRINTF(stderrstderr, "ERROR: could not parse cnf directive:%s\n",cnfline);
3211 exit(10);
3212 }
3213 }
3214}
3215
3216int main(int argc, const char *argv[])
3217{
3218 char idlfile[BASE_BUFFER_SIZE256];
3219 char tmplfile[BASE_BUFFER_SIZE256];
3220 char prefix_str[BASE_BUFFER_SIZE256];
3221 bracket_item_t *bi;
3222 FILE *fh;
3223
3224 if(argc!=2){
3225 usage();
3226 exit(0);
3227 }
3228
3229 eth_code=g_fopenfopen("ETH_CODE", "w");
3230 eth_hdr=g_fopenfopen("ETH_HDR", "w");
3231 eth_hfarr=g_fopenfopen("ETH_HFARR", "w");
3232 eth_hf=g_fopenfopen("ETH_HF", "w");
3233 eth_ettarr=g_fopenfopen("ETH_ETTARR", "w");
3234 eth_ett=g_fopenfopen("ETH_ETT", "w");
3235 eth_ft=g_fopenfopen("ETH_FT", "w");
3236 eth_handoff=g_fopenfopen("ETH_HANDOFF", "w");
3237
3238 snprintf(idlfile, BASE_BUFFER_SIZE256, "%s.cnf", argv[1]);
3239 fh=g_fopenfopen(idlfile,"r");
3240 if(fh){
3241 readcnffile(fh);
3242 fclose(fh);
3243 }
3244
3245 snprintf(idlfile, BASE_BUFFER_SIZE256, "%s.idl", argv[1]);
3246 fh=g_fopenfopen(idlfile,"r");
3247 if(!fh){
3248 FPRINTF(stderrstderr, "ERROR: could not open idl-file:%s\n", idlfile);
3249 Exit(0);
3250 }
3251
3252 linepos=0;
3253 tokenize(fh);
3254 prune_keyword_parameters("size_is");
3255 prune_keyword_parameters("length_is");
3256 rename_tokens("NTSTATUS", "WERROR");
3257 rename_tokens("unistr_noterm", "unistr");
3258 rename_tokens("ascstr_noterm", "ascstr");
3259 rename_tokens("hyper", "uint64");
3260 FPRINTF(NULL((void*)0),"\n\nParsing header:\n================\n");
3261 parseheader();
3262
3263 /* some idl files prepend a lot of symbols with <ifname>_
3264 search through the tokenlist and g_remove all such
3265 prefixes
3266 */
3267 snprintf(prefix_str, BASE_BUFFER_SIZE256, "%s_", ifname);
3268 preparetrimprefix(prefix_str);
3269 trimprefix();
3270
3271 /* this is the main loop, each iteration it tries to identify what
3272 kind of construct is the first entry in the token_list and call
3273 the appropriate handler
3274 */
3275 while(1) {
3276 /* just skip any [ ] that starts a new construct */
3277 if( !g_strcmp0(token_list->str, "[") ){
3278 token_list=parsebrackets(token_list, &bi);
3279 continue;
3280 }
3281
3282 /* typedef enum { */
3283 if( !g_strcmp0(token_list->str,"typedef")
3284 &&!g_strcmp0(token_list->next->str,"enum") ){
3285 parsetypedefenum();
3286 continue;
3287 }
3288
3289 /* typedef [ v1_enum ] enum { */
3290 if( !g_strcmp0(token_list->str,"typedef")
3291 &&!g_strcmp0(token_list->next->str,"[")
3292 &&!g_strcmp0(token_list->next->next->str,"v1_enum")
3293 &&!g_strcmp0(token_list->next->next->next->str,"]")
3294 &&!g_strcmp0(token_list->next->next->next->next->str,"enum") ){
3295 parsetypedefenum();
3296 continue;
3297 }
3298
3299 /* const */
3300 if( !g_strcmp0(token_list->str,"const") ){
3301 parseconst();
3302 continue;
3303 }
3304
3305 /* typedef struct { */
3306 if( !g_strcmp0(token_list->str,"typedef") ){
3307 token_item_t *tmpti;
3308
3309 tmpti=token_list->next;
3310 if( !g_strcmp0(tmpti->str, "[") ){
3311 tmpti=parsebrackets(tmpti, &bi);
3312 /* do some sanity checks here of bi->flags */
3313 }
3314 if( !g_strcmp0(tmpti->str, "struct") ){
3315 parsetypedefstruct(0);
3316 parsetypedefstruct(1);
3317 continue;
3318 }
3319 }
3320
3321 /* typedef union { */
3322 if( !g_strcmp0(token_list->str,"typedef") ){
3323 token_item_t *tmpti;
3324
3325 tmpti=token_list->next;
3326 if( !g_strcmp0(tmpti->str, "[") ){
3327 tmpti=parsebrackets(tmpti, &bi);
3328 /* do some sanity checks here of bi->flags */
3329 }
3330 if( !g_strcmp0(tmpti->str, "union") ){
3331 parsetypedefunion(0);
3332 parsetypedefunion(1);
3333 continue;
3334 }
3335 }
3336
3337 /* typedef bitmap { */
3338 if( !g_strcmp0(token_list->str,"typedef") ){
3339 token_item_t *tmpti;
3340
3341 tmpti=token_list->next;
3342 if( !g_strcmp0(tmpti->str, "[") ){
3343 tmpti=parsebrackets(tmpti, &bi);
3344 /* do some sanity checks here of bi->flags */
3345 }
3346 if( !g_strcmp0(tmpti->str, "bitmap") ){
3347 parsetypedefbitmap(0);
3348 parsetypedefbitmap(1);
3349 continue;
3350 }
3351 }
3352
3353 /* functions: WERROR function '(' */
3354 if( !g_strcmp0(token_list->str,"WERROR")
3355 &&!g_strcmp0(token_list->next->next->str,"(") ){
3356 parsefunction(0);
3357 parsefunction(1);
3358 parsefunction(2);
3359 continue;
3360 }
3361
3362 /* declare ... ; */
3363 if( !g_strcmp0(token_list->str,"declare") ){
3364 skipdeclare();
3365 continue;
3366 }
3367
3368
3369 break;
3370 };
3371
3372
3373 fclose(eth_code);
3374 fclose(eth_hdr);
3375 fclose(eth_hf);
3376 fclose(eth_hfarr);
3377 fclose(eth_ett);
3378 fclose(eth_ettarr);
3379 fclose(eth_ft);
3380 fclose(eth_handoff);
3381
3382 /* unless the token_list now only contains a single token : '}'
3383 we have failed to compile the idl file properly
3384 */
3385 if( g_strcmp0(token_list->str, "}") || token_list->next){
3386 FPRINTF(stderrstderr, "ERROR: we did not process all tokens. Compiler is incomplete.\n===========================================\n");
3387 printtokenlist(10);
3388 exit(10);
3389 }
3390
3391 check_hf_rename_refcount();
3392
3393 /* merge code and template into dissector */
3394 snprintf(line, 4 * BASE_BUFFER_SIZE256, "packet-dcerpc-%s.c", ifname);
3395 fh=g_fopenfopen(line, "w");
3396 snprintf(tmplfile, BASE_BUFFER_SIZE256, "packet-dcerpc-%s-template.c", argv[1]);
3397 tfh=g_fopenfopen(tmplfile, "r");
3398 if(!tfh){
3399 FPRINTF(stderrstderr, "ERROR: could not find %s\n", tmplfile);
3400 exit(10);
3401 }
3402 while(!feof(tfh)){
3403 line[0]=0;
3404 if(!fgets(line, 4 * BASE_BUFFER_SIZE256, tfh) || !line[0]){
3405 continue;
3406 }
3407 if(!strncmp(line, "ETH_CODE", 8)){
3408 mergefile("ETH_CODE",fh);
3409 } else if(!strncmp(line, "ETH_HDR", 7)){
3410 mergefile("ETH_HDR",fh);
3411 } else if(!strncmp(line, "ETH_HFARR", 9)){
3412 mergefile("ETH_HFARR",fh);
3413 } else if(!strncmp(line, "ETH_HF", 6)){
3414 mergefile("ETH_HF",fh);
3415 } else if(!strncmp(line, "ETH_ETTARR", 10)){
3416 mergefile("ETH_ETTARR",fh);
3417 } else if(!strncmp(line, "ETH_ETT", 7)){
3418 mergefile("ETH_ETT",fh);
3419 } else if(!strncmp(line, "ETH_FT", 6)){
3420 mergefile("ETH_FT",fh);
3421 } else if(!strncmp(line, "ETH_HANDOFF", 11)){
3422 mergefile("ETH_HANDOFF",fh);
3423 } else {
3424 fputs(line, fh);
3425 }
3426 }
3427 fclose(fh);
3428 fclose(tfh);
3429
3430 snprintf(line, 4 * BASE_BUFFER_SIZE256, "packet-dcerpc-%s.h", ifname);
3431 fh=g_fopenfopen(line, "w");
3432 snprintf(tmplfile, BASE_BUFFER_SIZE256, "packet-dcerpc-%s-template.h", argv[1]);
3433 tfh=g_fopenfopen(tmplfile, "r");
3434 if(!tfh){
3435 FPRINTF(stderrstderr, "ERROR: could not find %s\n", tmplfile);
3436 exit(10);
3437 }
3438 while(!feof(tfh)){
3439 line[0]=0;
3440 if(!fgets(line, 4 * BASE_BUFFER_SIZE256, tfh) || !line[0]){
3441 continue;
3442 }
3443 if(!strncmp(line, "ETH_CODE", 8)){
3444 mergefile("ETH_CODE",fh);
3445 } else if(!strncmp(line, "ETH_HDR", 7)){
3446 mergefile("ETH_HDR",fh);
3447 } else if(!strncmp(line, "ETH_HFARR", 9)){
3448 mergefile("ETH_HFARR",fh);
3449 } else if(!strncmp(line, "ETH_HF", 6)){
3450 mergefile("ETH_HF",fh);
3451 } else if(!strncmp(line, "ETH_ETTARR", 10)){
3452 mergefile("ETH_ETTARR",fh);
3453 } else if(!strncmp(line, "ETH_ETT", 7)){
3454 mergefile("ETH_ETT",fh);
3455 } else if(!strncmp(line, "ETH_FT", 6)){
3456 mergefile("ETH_FT",fh);
3457 } else if(!strncmp(line, "ETH_HANDOFF", 11)){
3458 mergefile("ETH_HANDOFF",fh);
3459 } else {
3460 fputs(line, fh);
3461 }
3462 }
3463
3464 g_print("%s was successfully compiled\n", ifname);
3465
3466 fclose(fh);
3467 fclose(tfh);
3468
3469 if (g_removeremove("ETH_CODE") == -1) {
3470 FPRINTF(stderrstderr, "Can't remove ETH_CODE");
3471 }
3472 if (g_removeremove("ETH_HDR") == -1) {
3473 FPRINTF(stderrstderr, "Can't remove ETH_CODE");
3474 }
3475 if (g_removeremove("ETH_HFARR") == -1) {
3476 FPRINTF(stderrstderr, "Can't remove ETH_CODE");
3477 }
3478 if (g_removeremove("ETH_HF") == -1) {
3479 FPRINTF(stderrstderr, "Can't remove ETH_CODE");
3480 }
3481 if (g_removeremove("ETH_ETTARR") == -1) {
3482 FPRINTF(stderrstderr, "Can't remove ETH_CODE");
3483 }
3484 if (g_removeremove("ETH_ETT") == -1) {
3485 FPRINTF(stderrstderr, "Can't remove ETH_CODE");
3486 }
3487 if (g_removeremove("ETH_FT") == -1) {
3488 FPRINTF(stderrstderr, "Can't remove ETH_CODE");
3489 }
3490 if (g_removeremove("ETH_HANDOFF") == -1) {
3491 FPRINTF(stderrstderr, "Can't remove ETH_CODE");
3492 }
3493
3494
3495 return 0;
3496}
3497
3498/*
3499 * Editor modelines - https://www.wireshark.org/tools/modelines.html
3500 *
3501 * Local variables:
3502 * c-basic-offset: 8
3503 * tab-width: 8
3504 * indent-tabs-mode: t
3505 * End:
3506 *
3507 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
3508 * :indentSize=8:tabSize=8:noTabs=false:
3509 */