Bug Summary

File:builds/wireshark/wireshark/epan/dissectors/dcerpc/idl2wrs.c
Warning:line 3045, 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-30-100354-3660-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 && !(bi && (bi->flags&(BI_SIZE_IS0x00000010|BI_LENGTH_IS0x00000020)))){
1727 /* A [size_is]/[length_is] array is dissected element by
1728 * element by the ucarray/uvarray wrapper below; the
1729 * fixed size is only its maximum. */
1730 snprintf(tmpstr, BASE_BUFFER_SIZE256, "fixedarray_%s", ptmpstr);
1731 if(check_if_to_emit(tmpstr)){
1732 FPRINTF(eth_code, "static unsigned\n");
1733 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep)\n", tmpstr);
1734 FPRINTF(eth_code, "{\n");
1735 FPRINTF(eth_code, " int count=%d;\n",fixed_array_size);
1736 FPRINTF(eth_code, " while(count--){\n");
1737 FPRINTF(eth_code, " offset=%s(tvb, offset, pinfo, tree, di, drep);\n", ptmpstr);
1738 FPRINTF(eth_code, " }\n");
1739 FPRINTF(eth_code, "\n");
1740 FPRINTF(eth_code, " return offset;\n");
1741 FPRINTF(eth_code, "}\n");
1742 FPRINTF(eth_code, "\n");
1743 } else {
1744 FPRINTF(NULL((void*)0),"NOEMIT Skipping this struct item :%s\n",tmpstr);
1745 }
1746 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
1747 }
1748
1749 /* handle switch_is */
1750 if(bi){
1751 switch(bi->flags&(BI_SIZE_IS0x00000010|BI_LENGTH_IS0x00000020)){
1752 case 0:
1753 break;
1754 case BI_SIZE_IS0x00000010:
1755 snprintf(tmpstr, BASE_BUFFER_SIZE256, "ucarray_%s", ptmpstr);
1756 if(check_if_to_emit(tmpstr)){
1757 FPRINTF(eth_code, "static unsigned\n");
1758 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep)\n", tmpstr);
1759 FPRINTF(eth_code, "{\n");
1760 FPRINTF(eth_code, " offset=dissect_ndr_ucarray(tvb, offset, pinfo, tree, di, drep, %s);\n", ptmpstr);
1761 FPRINTF(eth_code, " return offset;\n");
1762 FPRINTF(eth_code, "}\n");
1763 FPRINTF(eth_code, "\n");
1764 } else {
1765 FPRINTF(NULL((void*)0),"NOEMIT Skipping this struct item :%s\n",tmpstr);
1766 }
1767 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
1768 break;
1769 case BI_LENGTH_IS0x00000020:
1770 snprintf(tmpstr, BASE_BUFFER_SIZE256, "uvarray_%s", ptmpstr);
1771 if(check_if_to_emit(tmpstr)){
1772 FPRINTF(eth_code, "static unsigned\n");
1773 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep)\n", tmpstr);
1774 FPRINTF(eth_code, "{\n");
1775 FPRINTF(eth_code, " offset=dissect_ndr_uvarray(tvb, offset, pinfo, tree, di, drep, %s);\n", ptmpstr);
1776 FPRINTF(eth_code, " return offset;\n");
1777 FPRINTF(eth_code, "}\n");
1778 FPRINTF(eth_code, "\n");
1779 } else {
1780 FPRINTF(NULL((void*)0),"NOEMIT Skipping this struct item :%s\n",tmpstr);
1781 }
1782 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
1783 break;
1784 case BI_SIZE_IS0x00000010|BI_LENGTH_IS0x00000020:
1785 snprintf(tmpstr, BASE_BUFFER_SIZE256, "ucvarray_%s", ptmpstr);
1786 if(check_if_to_emit(tmpstr)){
1787 FPRINTF(eth_code, "static unsigned\n");
1788 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep)\n", tmpstr);
1789 FPRINTF(eth_code, "{\n");
1790 FPRINTF(eth_code, " offset=dissect_ndr_ucvarray(tvb, offset, pinfo, tree, di, drep, %s);\n", ptmpstr);
1791 FPRINTF(eth_code, " return offset;\n");
1792 FPRINTF(eth_code, "}\n");
1793 FPRINTF(eth_code, "\n");
1794 } else {
1795 FPRINTF(NULL((void*)0),"NOEMIT Skipping this struct item :%s\n",tmpstr);
1796 }
1797 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
1798 break;
1799 default:
1800 FPRINTF(stderrstderr, "ERROR: typedefstruct can not handle this combination of sizeis/lengthis\n");
1801 Exit(10);
1802 }
1803 }
1804
1805 /* handle pointers */
1806 while(num_pointers--){
1807 pointer_type=pi->type;
1808 pi=pi->next;
1809 snprintf(tmpstr, BASE_BUFFER_SIZE256, "%s_%s", pointer_type, ptmpstr);
1810 if(check_if_to_emit(tmpstr)){
1811 FPRINTF(eth_code, "static unsigned\n");
1812 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep)\n", tmpstr);
1813 FPRINTF(eth_code, "{\n");
1814 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);
1815 FPRINTF(eth_code, " return offset;\n");
1816 FPRINTF(eth_code, "}\n");
1817 FPRINTF(eth_code, "\n");
1818 } else {
1819 FPRINTF(NULL((void*)0),"NOEMIT Skipping this struct item :%s\n",tmpstr);
1820 }
1821
1822 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
1823 }
1824 }
1825
1826 if(pass==1){
1827 snprintf(tmpstr, BASE_BUFFER_SIZE256, "%s_dissect_%s_%s", ifname, struct_name, field_name);
1828 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
1829
1830 /* handle fixedsizearrays */
1831 if(is_array_of_pointers){
1832 pointer_type=pi->type;
1833 pi=pi->next;
1834 snprintf(tmpstr, BASE_BUFFER_SIZE256, "%s_%s", pointer_type, ptmpstr);
1835 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
1836 } else if(fixed_array_size && !(bi && (bi->flags&(BI_SIZE_IS0x00000010|BI_LENGTH_IS0x00000020)))){
1837 /* A [size_is]/[length_is] array is dissected element by
1838 * element by the ucarray/uvarray wrapper below; the
1839 * fixed size is only its maximum. */
1840 snprintf(tmpstr, BASE_BUFFER_SIZE256, "fixedarray_%s", ptmpstr);
1841 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
1842 }
1843
1844 /* handle switch_is */
1845 if(bi){
1846 switch(bi->flags&(BI_SIZE_IS0x00000010|BI_LENGTH_IS0x00000020)){
1847 case 0:
1848 break;
1849 case BI_SIZE_IS0x00000010:
1850 snprintf(tmpstr, BASE_BUFFER_SIZE256, "ucarray_%s", ptmpstr);
1851 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
1852 break;
1853 case BI_LENGTH_IS0x00000020:
1854 snprintf(tmpstr, BASE_BUFFER_SIZE256, "uvarray_%s", ptmpstr);
1855 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
1856 break;
1857 case BI_SIZE_IS0x00000010|BI_LENGTH_IS0x00000020:
1858 snprintf(tmpstr, BASE_BUFFER_SIZE256, "ucvarray_%s", ptmpstr);
1859 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
1860 break;
1861 default:
1862 FPRINTF(stderrstderr, "ERROR: typedefstruct can not handle this combination of sizeis/lengthis\n");
1863 Exit(10);
1864 }
1865 }
1866
1867 /* handle pointers */
1868 while(num_pointers--){
1869 pointer_type=pi->type;
1870 pi=pi->next;
1871 snprintf(tmpstr, BASE_BUFFER_SIZE256, "%s_%s", pointer_type, ptmpstr);
1872 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
1873 }
1874
1875 FPRINTF(eth_code, " offset=%s(tvb, offset, pinfo, tree, di, drep);\n", ptmpstr);
1876 FPRINTF(eth_code, "\n");
1877 }
1878
1879 if(g_strcmp0(ti->str,";")){
1880 FPRINTF(stderrstderr, "ERROR: field does not en with ';'\n");
1881 Exit(10);
1882 }
1883 ti=ti->next;
1884 bi=NULL((void*)0); /* clear bi before we start on the next field */
1885 }
1886
1887 if(pass==1){
1888 FPRINTF(eth_code, " proto_item_set_len(item, offset-old_offset);\n");
1889 FPRINTF(eth_code, "\n");
1890 FPRINTF(eth_code, " return offset;\n");
1891 FPRINTF(eth_code, "}\n");
1892 register_new_type(struct_name, dissectorname, "FT_NONE", "BASE_NONE", "0", "NULL", alignment);
1893 }
1894
1895
1896typedef_struct_finished:
1897 FPRINTF(NULL((void*)0),"\nEND STRUCT:%s pass:%d\n-------\n",struct_name,pass);
1898
1899 /* only advance token_list for pass==1
1900 ti now points to the '}' token
1901 */
1902 if(pass==1){
1903 if(!ti || g_strcmp0(ti->str,"}")){
1904 FPRINTF(stderrstderr, "ERROR: struct does not end with '}'\n");
1905 Exit(10);
1906 }
1907 ti=ti->next;
1908
1909 /* just skip the name */
1910 ti=ti->next;
1911
1912 if(!ti || g_strcmp0(ti->str,";")){
1913 FPRINTF(stderrstderr, "ERROR: struct does not end with ';'\n");
1914 Exit(10);
1915 }
1916 ti=ti->next;
1917
1918 token_list=ti;
1919 }
1920}
1921
1922/* this function will parse a
1923 typedef bitmap {
1924 construct and generate the appropriate code.
1925 the typedef will be g_removed from the token_list once it has been processed
1926 the function assumes that the typedef is the first object in the token_list
1927 the function will be called twice, once with pass=0 and once with pass=1
1928 which controls whether subdissectors are to be generated or whether the
1929 bitmap dissector itself is to be generated
1930
1931 bitmaps are by default 32 bits
1932*/
1933static void parsetypedefbitmap(int pass)
1934{
1935 token_item_t *ti, *tmpti;
1936 char *bitmap_name;
1937 char dissectorname[BASE_BUFFER_SIZE256], hf_bitname[BASE_BUFFER_SIZE256];
1938 int alignment;
1939 unsigned int val;
1940 char *name, *value;
1941 bracket_item_t *bi=NULL((void*)0);
1942
1943 ti=token_list;
1944 if(g_strcmp0(ti->str, "typedef")){
1945 FPRINTF(stderrstderr, "ERROR: typedefbitmap first token is not 'typedef'\n");
1946 Exit(10);
1947 }
1948 ti=ti->next;
1949
1950 alignment=4; /* default size is 32 bits */
1951
1952 if(!g_strcmp0(ti->str, "[")){
1953 ti=parsebrackets(ti, &bi);
1954 }
1955 /* check that we know how to handle the bracket thing */
1956 if(bi){
1957 if(bi->flags&(~(BI_BITMAP320x00000200|BI_BITMAP80x00000100))){
1958 FPRINTF(stderrstderr, "ERROR: typedefbitmap unknown bracket flags encountered : 0x%08x\n",bi->flags);
1959 Exit(10);
1960 }
1961 if(bi->flags&BI_BITMAP320x00000200){
1962 alignment=4;
1963 }
1964 if(bi->flags&BI_BITMAP80x00000100){
1965 alignment=1;
1966 }
1967 }
1968
1969
1970 if(g_strcmp0(ti->str, "bitmap")){
1971 FPRINTF(stderrstderr, "ERROR: typedefbitmap second token is not 'bitmap'\n");
1972 Exit(10);
1973 }
1974 ti=ti->next;
1975
1976 if(g_strcmp0(ti->str, "{")){
1977 FPRINTF(stderrstderr, "ERROR: typedefbitmap third token is not '{'\n");
1978 Exit(10);
1979 }
1980 ti=ti->next;
1981
1982 /* search forward until the '}' so we can find the name of the bitmap */
1983 for(tmpti=ti;tmpti;tmpti=tmpti->next){
1984 if(!g_strcmp0(tmpti->str, "{")){
1985 FPRINTF(stderrstderr, "ERROR: typedefbitmap '{' encountered inside bitmap\n");
1986 Exit(10);
1987 }
1988 if(!g_strcmp0(tmpti->str, "}")){
1989 break;
1990 }
1991 }
1992 if (!tmpti || !tmpti->next){
1993 FPRINTF(stderrstderr, "ERROR: typedefbitmap missing matching '}'\n");
1994 Exit(10);
1995 }
1996 bitmap_name=tmpti->next->str;
1997 snprintf(dissectorname, DISSECTORNAME_MAXLEN256, "%s_dissect_%s", ifname, bitmap_name);
1998
1999 FPRINTF(NULL((void*)0),"\nBITMAP:%s pass:%d\n-------\n",bitmap_name,pass);
2000
2001 /* pass 1 generate header for the struct dissector */
2002 if(pass==1){
2003 FPRINTF(eth_ett, "static int ett_%s_%s;\n", ifname, bitmap_name);
2004 FPRINTF(eth_ettarr, " &ett_%s_%s,\n", ifname, bitmap_name);
2005 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);
2006 FPRINTF(eth_code, "\n");
2007 FPRINTF(eth_code, "int\n");
2008 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);
2009 FPRINTF(eth_code, "{\n");
2010 FPRINTF(eth_code, " proto_item *item=NULL;\n");
2011 FPRINTF(eth_code, " proto_tree *tree=NULL;\n");
2012 switch(alignment){
2013 case 1:
2014 FPRINTF(eth_code, " uint8_t flags;\n");
2015 FPRINTF(eth_code, "\n");
2016 break;
2017 case 4:
2018 FPRINTF(eth_code, " uint32_t flags;\n");
2019 FPRINTF(eth_code, "\n");
2020 FPRINTF(eth_code, " ALIGN_TO_4_BYTES;\n");
2021 break;
2022 default:
2023 FPRINTF(stderrstderr, "ERROR: typedefbitmap can not handle alignment:%d\n",alignment);
2024 Exit(10);
2025 }
2026 FPRINTF(eth_code, "\n");
2027 FPRINTF(eth_code, " if(parent_tree){\n");
2028 FPRINTF(eth_code, " item=proto_tree_add_item(parent_tree, hf_index, tvb, offset, %d, ENC_LITTLE_ENDIAN);\n", alignment);
2029 FPRINTF(eth_code, " tree=proto_item_add_subtree(item, ett_%s_%s);\n", ifname, bitmap_name);
2030 FPRINTF(eth_code, " }\n");
2031 FPRINTF(eth_code, "\n");
2032 switch(alignment){
2033 case 1:
2034 FPRINTF(eth_code, " offset=dissect_ndr_uint8(tvb, offset, pinfo, NULL, di, drep, -1, &flags);\n");
2035 FPRINTF(eth_code, "\n");
2036 break;
2037 case 4:
2038 FPRINTF(eth_code, " offset=dissect_ndr_uint32(tvb, offset, pinfo, NULL, di, drep, -1, &flags);\n");
2039 FPRINTF(eth_code, "\n");
2040 break;
2041 default:
2042 FPRINTF(stderrstderr, "ERROR: typedefbitmap can not handle alignment:%d\n",alignment);
2043 Exit(10);
2044 }
2045 FPRINTF(eth_code, "\n");
2046 }
2047
2048 /* scan the struct and create call for all bits */
2049 while(ti){
2050 if(!g_strcmp0(ti->str, "}")){
2051 break;
2052 }
2053 if(!g_strcmp0(ti->str, "[")){
2054 FPRINTF(stderrstderr, "ERROR: typedefbitmap can not handle '[' yet\n");
2055 Exit(10);
2056 }
2057
2058 name=ti->str;
2059 ti=ti->next;
2060 snprintf(hf_bitname, BASE_BUFFER_SIZE256, "hf_%s_%s_%s", ifname, bitmap_name, name);
2061
2062 if(g_strcmp0(ti->str, "=")){
2063 FPRINTF(stderrstderr, "ERROR: typedefbitmap i expected a '=' here\n");
2064 Exit(10);
2065 }
2066 ti=ti->next;
2067
2068 value=ti->str;
2069 ti=ti->next;
2070 val=0;
2071 if(!strncmp(value, "0x", 2)){
2072 sscanf(value, "0x%x", &val);
2073 } else {
2074 FPRINTF(stderrstderr, "ERROR: typedefbitmap can only handle hexadecimal constants\n");
2075 Exit(10);
2076 }
2077
2078 if((val == 0) || (val&(val-1))){
2079 FPRINTF(stderrstderr, "ERROR: typedefbitmap can only handle single bit fields\n");
2080 Exit(10);
2081 }
2082
2083 if(pass==0){
2084 char filter_name[BASE_BUFFER_SIZE256], base_name[BASE_BUFFER_SIZE256], tfs_name[BASE_BUFFER_SIZE256];
2085
2086 snprintf(filter_name, BASE_BUFFER_SIZE256, "%s.%s.%s", ifname, bitmap_name, name);
2087 snprintf(base_name, BASE_BUFFER_SIZE256, "%d", alignment*8);
2088 snprintf(tfs_name, BASE_BUFFER_SIZE256, "TFS(&%s_tfs)", name);
2089 register_hf_field(hf_bitname, name, filter_name, "FT_BOOLEAN", base_name, tfs_name, value, "");
2090
2091 FPRINTF(eth_code, "static const true_false_string %s_tfs = {\n",name);
2092 FPRINTF(eth_code, " \"%s is SET\",\n", name);
2093 FPRINTF(eth_code, " \"%s is NOT set\"\n", name);
2094 FPRINTF(eth_code, "};\n");
2095 FPRINTF(eth_code, "\n");
2096 }
2097
2098 if(pass==1){
2099 FPRINTF(eth_code, " proto_tree_add_boolean(tree, %s, tvb, offset-%d, %d, flags);\n", hf_bitname, alignment, alignment);
2100 FPRINTF(eth_code, " if(flags&%s){\n", value);
2101 FPRINTF(eth_code, " proto_item_append_text(item, \" %s\");\n", name);
2102 FPRINTF(eth_code, " }\n");
2103 FPRINTF(eth_code, " flags&=(~%s);\n", value);
2104 FPRINTF(eth_code, "\n");
2105 }
2106
2107 if(!g_strcmp0(ti->str, ",")){
2108 ti=ti->next;
2109 continue;
2110 }
2111 }
2112
2113 if(pass==1){
2114 FPRINTF(eth_code, " if(flags){\n");
2115 FPRINTF(eth_code, " proto_item_append_text(item, \"UNKNOWN-FLAGS\");\n");
2116 FPRINTF(eth_code, " }\n");
2117 FPRINTF(eth_code, "\n");
2118 FPRINTF(eth_code, " return offset;\n");
2119 FPRINTF(eth_code, "}\n");
2120 switch(alignment){
2121 case 1:
2122 register_new_type(bitmap_name, dissectorname, "FT_UINT8", "BASE_HEX", "0", "NULL", alignment);
2123 break;
2124 case 4:
2125 register_new_type(bitmap_name, dissectorname, "FT_UINT32", "BASE_HEX", "0", "NULL", alignment);
2126 break;
2127 default:
2128 FPRINTF(stderrstderr, "ERROR: typedefbitmap can not handle alignment:%d\n",alignment);
2129 Exit(10);
2130 }
2131 }
2132
2133 FPRINTF(NULL((void*)0),"\nEND BITMAP:%s pass:%d\n-------\n",bitmap_name,pass);
2134
2135 /* only advance token_list for pass==1
2136 ti now points to the '}' token
2137 */
2138 if(pass==1){
2139 if(!ti || g_strcmp0(ti->str,"}")){
2140 FPRINTF(stderrstderr, "ERROR: bitmap does not end with '}'\n");
2141 Exit(10);
2142 }
2143 ti=ti->next;
2144
2145 /* just skip the name */
2146 ti=ti->next;
2147
2148 if(!ti || g_strcmp0(ti->str,";")){
2149 FPRINTF(stderrstderr, "ERROR: bitmap does not end with ';'\n");
2150 Exit(10);
2151 }
2152 ti=ti->next;
2153
2154 token_list=ti;
2155 }
2156}
2157
2158/* a case tag might be a negative number, i.e. contain a '-' sign which
2159 is not valid inside a symbol name in c.
2160*/
2161static const char *
2162case2str(const char *str)
2163{
2164 char *newstr;
2165 if(str[0]!='-'){
2166 return str;
2167 }
2168 newstr=g_strdup(str)g_strdup_inline (str);
2169 newstr[0]='m';
2170 return newstr;
2171}
2172
2173/* this function will parse a
2174 typedef union {
2175 construct and generate the appropriate code.
2176 the typedef will be g_removed from the token_list once it has been processed
2177 the function assumes that the typedef is the first object in the token_list
2178 the function will be called twice, once with pass=0 and once with pass=1
2179 which controls whether subdissectors are to be generated or whether the
2180 union dissector itself is to be generated
2181*/
2182static void parsetypedefunion(int pass)
2183{
2184 char *union_name;
2185 token_item_t *ti, *tmpti;
2186 char dissectorname[BASE_BUFFER_SIZE256];
2187 bracket_item_t *bi=NULL((void*)0);
2188 char tmpstr[BASE_BUFFER_SIZE256], *ptmpstr;
2189 int level, num_pointers;
2190 static int alignment;
2191 type_item_t *type_item;
2192 char hf_index[BASE_BUFFER_SIZE256];
2193 int tag_alignment, item_alignment;
2194
2195 ti=token_list;
2196 if(g_strcmp0(ti->str, "typedef")){
2197 FPRINTF(stderrstderr, "ERROR: typedefunion first token is not 'typedef'\n");
2198 Exit(10);
2199 }
2200 ti=ti->next;
2201
2202 if(!g_strcmp0(ti->str, "[")){
2203 ti=parsebrackets(ti, &bi);
2204 }
2205 /* check that we know how to handle the bracket thing */
2206 if(bi){
2207 if(bi->flags&(~(BI_SWITCH_TYPE0x00000400))){
2208 FPRINTF(stderrstderr, "ERROR: typedefunion unknown bracket flags encountered : 0x%08x\n",bi->flags);
2209 Exit(10);
2210 }
2211 }
2212
2213 if(g_strcmp0(ti->str, "union")){
2214 FPRINTF(stderrstderr, "ERROR: typedefunion second token is not 'union'\n");
2215 Exit(10);
2216 }
2217 ti=ti->next;
2218
2219 if(g_strcmp0(ti->str, "{")){
2220 FPRINTF(stderrstderr, "ERROR: typedefunion third token is not '{'\n");
2221 Exit(10);
2222 }
2223 ti=ti->next;
2224
2225 /* search forward until the '}' so we can find the name of the union */
2226 for(tmpti=ti,level=0;tmpti;tmpti=tmpti->next){
2227 if(!g_strcmp0(tmpti->str, "{")){
2228 level++;
2229 continue;
2230 }
2231 if(!g_strcmp0(tmpti->str, "}")){
2232 if(!level){
2233 break;
2234 }
2235 level--;
2236 continue;
2237 }
2238 }
2239
2240 if (!tmpti || !tmpti->next){
2241 FPRINTF(stderrstderr, "ERROR: typedefunion missing matching '}'\n");
2242 Exit(10);
2243 }
2244 union_name=tmpti->next->str;
2245 snprintf(dissectorname, DISSECTORNAME_MAXLEN256, "%s_dissect_union_%s", ifname, union_name);
2246
2247 FPRINTF(NULL((void*)0),"\nUNION:%s pass:%d\n-------\n",union_name,pass);
2248
2249 if(bi && bi->flags&BI_SWITCH_TYPE0x00000400){
2250 tag_alignment=bi->union_tag_size;
2251 } else {
2252 tag_alignment=get_union_tag_size(union_name);
2253 }
2254
2255 /* this is pass 0 so reset alignment to the minimum possible value
2256 and update as items are processed.
2257 we need alignment when pass 1 is run
2258 */
2259 if(pass==0){
2260 alignment=tag_alignment;
2261 }
2262
2263 /* pass 1 generate header for the struct dissector */
2264 if(pass==1){
2265 FPRINTF(eth_ett, "static int ett_%s_%s;\n", ifname, union_name);
2266 FPRINTF(eth_ettarr, " &ett_%s_%s,\n", ifname, union_name);
2267 FPRINTF(eth_code, "\n");
2268 FPRINTF(eth_code, "static unsigned\n");
2269 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);
2270 FPRINTF(eth_code, "{\n");
2271 FPRINTF(eth_code, " proto_item *item=NULL;\n");
2272 FPRINTF(eth_code, " proto_tree *tree=NULL;\n");
2273 FPRINTF(eth_code, " int old_offset;\n");
2274 /* we do alignment on the tag itself here so that
2275 we skip any alignment padding prior to where the tag
2276 itself starts, this makes the byterange in the hexpane
2277 for the union expansion start with the first byte of the tag
2278 */
2279 switch(tag_alignment){
2280 case 1:
2281 break;
2282 case 2:
2283 FPRINTF(eth_code, " uint16_t level;\n");
2284 FPRINTF(eth_code, "\n");
2285 FPRINTF(eth_code, " ALIGN_TO_2_BYTES;\n");
2286 FPRINTF(eth_code, "\n");
2287 break;
2288 case 4:
2289 FPRINTF(eth_code, " uint32_t level = 0;\n");
2290 FPRINTF(eth_code, "\n");
2291 FPRINTF(eth_code, " ALIGN_TO_4_BYTES;\n");
2292 FPRINTF(eth_code, "\n");
2293 break;
2294 default:
2295 FPRINTF(stderrstderr, "ERROR: typedefunion 1 can not handle alignment:%d\n",alignment);
2296 Exit(10);
2297 }
2298 FPRINTF(eth_code, " old_offset=offset;\n");
2299 FPRINTF(eth_code, " if(parent_tree){\n");
2300 FPRINTF(eth_code, " tree=proto_tree_add_subtree(parent_tree, tvb, offset, -1, ett_%s_%s, &item, \"%s\");\n", ifname, union_name, union_name);
2301 FPRINTF(eth_code, " }\n");
2302 FPRINTF(eth_code, "\n");
2303 switch(tag_alignment){
2304 case 1:
2305 break;
2306 case 2:
2307 FPRINTF(eth_code, " offset=dissect_ndr_uint16(tvb, offset, pinfo, tree,\n");
2308 FPRINTF(eth_code, " di, drep, hf_index, &level);\n");
2309 break;
2310 case 4:
2311 FPRINTF(eth_code, " offset=dissect_ndr_uint32(tvb, offset, pinfo, tree,\n");
2312 FPRINTF(eth_code, " di, drep, hf_index, &level);\n");
2313 break;
2314 default:
2315 FPRINTF(stderrstderr, "ERROR: typedefunion 2 can not handle alignment:%d\n",alignment);
2316 Exit(10);
2317 }
2318 FPRINTF(eth_code, "\n");
2319 FPRINTF(eth_code, " switch(level){\n");
2320 }
2321
2322 /* scan the struct and create all subdissectors */
2323 level=0;
2324 while(ti){
2325 if(!g_strcmp0(ti->str, "{")){
2326 ti=ti->next;
2327 level++;
2328 continue;
2329 }
2330 if(!g_strcmp0(ti->str, "}")){
2331 if(!level){
2332 break;
2333 }
2334 ti=ti->next;
2335 level--;
2336 continue;
2337 }
2338 if(!g_strcmp0(ti->str, "[")){
2339 ti=parsebrackets(ti, &bi);
2340 continue;
2341 }
2342
2343 if(!bi){
2344 FPRINTF(stderrstderr, "ERROR : typedefunion no brackets found for case\n");
2345 Exit(10);
2346 }
2347 /* make sure we catch when we havent implemented everything
2348 yet.
2349 we currently only know about CASE and CASE_DEFAULT flags
2350 */
2351 if(bi->flags&(~(BI_CASE0x00000001|BI_CASE_DEFAULT0x00000002|BI_POINTER0x00000040))){
2352 FPRINTF(stderrstderr, "ERROR: typedefunion unknown bracket flags encountered : 0x%08x\n",bi->flags);
2353 Exit(10);
2354 }
2355 if(!(bi->flags&BI_CASE0x00000001)){
2356 FPRINTF(stderrstderr, "ERROR : typedefunion no case found in brackets\n");
2357 Exit(10);
2358 }
2359#ifdef g_removeD
2360 /* only empty default cases for now */
2361 if(bi->flags&BI_CASE_DEFAULT0x00000002){
2362 if(g_strcmp0(ti->str,";")){
2363 FPRINTF(stderrstderr, "ERROR: default tag is not empty\n");
2364 Exit(10);
2365 }
2366 ti=ti->next;
2367 continue;
2368 }
2369#endif
2370
2371 /* just skip all and any 'empty' arms */
2372 if(!g_strcmp0(ti->str, ";")){
2373 ti=ti->next;
2374 continue;
2375 }
2376
2377 /* handle the type, verify that we KNOW this type */
2378 type_item=find_type(ti->str);
2379 if(!type_item){
2380 FPRINTF(stderrstderr, "ERROR : typedefunion unknown type %s\n",ti->str);
2381 Exit(10);
2382 }
2383 ti=ti->next;
2384 /* count the levels of pointers */
2385 for(num_pointers=0;!g_strcmp0(ti->str, "*");ti=ti->next){
2386 num_pointers++;
2387 }
2388
2389 /* keep track of alignment */
2390 if(num_pointers){
2391 item_alignment=4;
2392 } else {
2393 item_alignment=type_item->alignment;
2394 }
2395 if(alignment<item_alignment){
2396 alignment=item_alignment;
2397 }
2398
2399 snprintf(hf_index, BASE_BUFFER_SIZE256, "hf_%s_%s_%s_%s", ifname, union_name, case2str(bi->case_name), ti->str);
2400 /* pass 0 generate subdissectors */
2401 if(pass==0){
2402 char filter_name[BASE_BUFFER_SIZE256];
2403 const char *hf;
2404
2405 snprintf(tmpstr, BASE_BUFFER_SIZE256, "%s_dissect_union_%s_%s_%s", ifname, union_name, case2str(bi->case_name), ti->str);
2406 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
2407
2408 snprintf(filter_name, BASE_BUFFER_SIZE256, "%s.%s.%s", ifname, union_name, ti->str);
2409 hf=register_hf_field(hf_index, ti->str, filter_name, type_item->ft_type, type_item->base_type, type_item->vals, type_item->mask, "");
2410
2411 FPRINTF(eth_code, "static unsigned\n");
2412 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep)\n", ptmpstr);
2413 FPRINTF(eth_code, "{\n");
2414 FPRINTF(eth_code, " uint32_t param=%s;\n",find_dissector_param_value(ptmpstr));
2415 FPRINTF(eth_code, " offset=%s(tvb, offset, pinfo, tree, di, drep, %s, param);\n", type_item->dissector, hf);
2416 FPRINTF(eth_code, " return offset;\n");
2417 FPRINTF(eth_code, "}\n");
2418 FPRINTF(eth_code, "\n");
2419
2420 /* handle pointers */
2421 while(num_pointers--){
2422 snprintf(tmpstr, BASE_BUFFER_SIZE256, "%s_%s", ptmpstr, "unique");
2423 FPRINTF(eth_code, "static unsigned\n");
2424 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep)\n", tmpstr);
2425 FPRINTF(eth_code, "{\n");
2426 FPRINTF(eth_code, " offset=dissect_ndr_embedded_pointer(tvb, offset, pinfo, tree, di, drep, %s, NDR_POINTER_UNIQUE, \"%s\", -1);\n", ptmpstr, ti->str);
2427 FPRINTF(eth_code, " return offset;\n");
2428 FPRINTF(eth_code, "}\n");
2429 FPRINTF(eth_code, "\n");
2430
2431 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
2432
2433 }
2434 }
2435
2436 if(pass==1){
2437 /* handle pointers */
2438 snprintf(tmpstr, BASE_BUFFER_SIZE256, "%s_dissect_union_%s_%s_%s", ifname, union_name, case2str(bi->case_name), ti->str);
2439 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
2440 while(num_pointers--){
2441 snprintf(tmpstr, BASE_BUFFER_SIZE256, "%s_%s", ptmpstr, "unique");
2442 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
2443 }
2444
2445 if(bi->flags&BI_CASE_DEFAULT0x00000002){
2446 FPRINTF(eth_code, " default:\n");
2447 } else {
2448 FPRINTF(eth_code, " case %s:\n",bi->case_name);
2449 }
2450 /* each arm itself is aligned independently */
2451 switch(item_alignment){
2452 case 1:
2453 break;
2454 case 2:
2455 FPRINTF(eth_code, " ALIGN_TO_2_BYTES;\n");
2456 break;
2457 case 4:
2458 FPRINTF(eth_code, " ALIGN_TO_4_BYTES;\n");
2459 break;
2460 case 8:
2461 FPRINTF(eth_code, " ALIGN_TO_8_BYTES;\n");
2462 break;
2463 default:
2464 FPRINTF(stderrstderr, "ERROR: typedefunion 3 can not handle alignment:%d\n",item_alignment);
2465 Exit(10);
2466 }
2467 FPRINTF(eth_code, " offset=%s(tvb, offset, pinfo, tree, di, drep);\n", ptmpstr);
2468 FPRINTF(eth_code, " break;\n");
2469 FPRINTF(eth_code, "\n");
2470 }
2471 ti=ti->next;
2472
2473 if(g_strcmp0(ti->str,";")){
2474 FPRINTF(stderrstderr, "ERROR: field does not end with ';'\n");
2475 Exit(10);
2476 }
2477 ti=ti->next;
2478 }
2479
2480 if(pass==1){
2481 FPRINTF(eth_code, " }\n");
2482 FPRINTF(eth_code, "\n");
2483 FPRINTF(eth_code, " proto_item_set_len(item, offset-old_offset);\n");
2484 FPRINTF(eth_code, "\n");
2485 FPRINTF(eth_code, " return offset;\n");
2486 FPRINTF(eth_code, "}\n");
2487 switch(tag_alignment){
2488 case 2:
2489 register_new_type(union_name, dissectorname, "FT_UINT16", "BASE_DEC", "0", "NULL", alignment);
2490 break;
2491 case 4:
2492 register_new_type(union_name, dissectorname, "FT_UINT32", "BASE_DEC", "0", "NULL", alignment);
2493 break;
2494 default:
2495 FPRINTF(stderrstderr, "ERROR: typedefunion 4 can not handle alignment:%d\n",alignment);
2496 Exit(10);
2497 }
2498 }
2499
2500 FPRINTF(NULL((void*)0),"\nEND UNION:%s pass:%d\n-------\n",union_name,pass);
2501
2502 /* only advance token_list for pass==1
2503 ti now points to the '}' token
2504 */
2505 if(pass==1){
2506 if(!ti || g_strcmp0(ti->str,"}")){
2507 FPRINTF(stderrstderr, "ERROR: union does not end with '}'\n");
2508 Exit(10);
2509 }
2510 ti=ti->next;
2511
2512 /* just skip the name */
2513 ti=ti->next;
2514
2515 if(!ti || g_strcmp0(ti->str,";")){
2516 FPRINTF(stderrstderr, "ERROR: union does not end with ';'\n");
2517 Exit(10);
2518 }
2519 ti=ti->next;
2520
2521 token_list=ti;
2522 }
2523}
2524
2525
2526/* this function will parse a
2527 WERROR function (
2528 construct and generate the appropriate code.
2529 the function will be g_removed from the token_list once it has been processed
2530 the function assumes that the function is the first object in the token_list
2531 the function will be called three times with
2532 pass=0 generate subdissectors and entries for the function table
2533 pass=1 generate code for the REQUEST
2534 pass=2 generate code for the REPLY
2535*/
2536static void parsefunction(int pass)
2537{
2538 char *function_name;
2539 static int funcno=0;
2540 token_item_t *ti;
2541 bracket_item_t *bi=NULL((void*)0);
2542 pointer_item_t *pi;
2543 const char *pointer_type;
2544
2545 char tmpstr[BASE_BUFFER_SIZE256], *ptmpstr;
2546 int level, num_pointers;
2547 type_item_t *type_item;
2548 char hf_index[BASE_BUFFER_SIZE256];
2549
2550 ti=token_list;
2551 if(g_strcmp0(ti->str, "WERROR")){
2552 FPRINTF(stderrstderr, "ERROR: function first token is not 'WERROR'\n");
2553 Exit(10);
2554 }
2555 ti=ti->next;
2556
2557 function_name=ti->str;
2558 ti=ti->next;
2559
2560 if(g_strcmp0(ti->str, "(")){
2561 FPRINTF(stderrstderr, "ERROR: function third token is not '('\n");
2562 Exit(10);
2563 }
2564 ti=ti->next;
2565
2566 FPRINTF(NULL((void*)0),"\nFUNCTION:%s pass:%d\n-------\n",function_name,pass);
2567
2568 if(pass==0){
2569 FPRINTF(eth_ft, " { %d, \"%s\",\n",funcno,function_name);
2570 FPRINTF(eth_ft, " %s_dissect_%s_request,\n", ifname, function_name);
2571 FPRINTF(eth_ft, " %s_dissect_%s_response },\n", ifname, function_name);
2572 funcno++;
2573 }
2574
2575 /* pass 1,2 generate header for the function dissector */
2576 if((pass==1)||(pass==2)){
2577 FPRINTF(eth_code, "\n");
2578 FPRINTF(eth_code, "static unsigned\n");
2579 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");
2580 FPRINTF(eth_code, "{\n");
2581 }
2582
2583 /* scan the struct and create all subdissectors */
2584 level=0;
2585 while(ti){
2586 if(!g_strcmp0(ti->str, "(")){
2587 ti=ti->next;
2588 level++;
2589 continue;
2590 }
2591 if(!g_strcmp0(ti->str, ")")){
2592 if(!level){
2593 break;
2594 }
2595 ti=ti->next;
2596 level--;
2597 continue;
2598 }
2599 if(!g_strcmp0(ti->str, "[")){
2600 ti=parsebrackets(ti, &bi);
2601 continue;
2602 }
2603
2604 if(!bi){
2605 FPRINTF(stderrstderr, "ERROR : function no brackets found for case\n");
2606 Exit(10);
2607 }
2608
2609 /* make sure we catch when we havent implemented everything
2610 yet.
2611 we currently only know about IN and OUT flags
2612 */
2613 if(bi->flags&(~(BI_IN0x00000004|BI_OUT0x00000008|BI_POINTER0x00000040|BI_SIZE_IS0x00000010|BI_LENGTH_IS0x00000020))){
2614 FPRINTF(stderrstderr, "ERROR: function unknown bracket flags encountered : 0x%08x\n",bi->flags);
2615 Exit(10);
2616 }
2617 if(!(bi->flags&(BI_IN0x00000004|BI_OUT0x00000008))){
2618 FPRINTF(stderrstderr, "ERROR : function parameter is neither in nor out\n");
2619 Exit(10);
2620 }
2621
2622 /* handle the type, verify that we KNOW this type */
2623 type_item=find_type(ti->str);
2624 if(!type_item){
2625 FPRINTF(stderrstderr, "ERROR : function unknown type %s\n",ti->str);
2626 Exit(10);
2627 }
2628 ti=ti->next;
2629 /* count the levels of pointers */
2630 for(num_pointers=0;!g_strcmp0(ti->str, "*");ti=ti->next){
2631 num_pointers++;
2632 }
2633
2634 /* now that we know how many real pointer there were we must
2635 prepend default pointers to the list so it has the right
2636 length.
2637 */
2638 pi=prepend_pointer_list(bi->pointer_list, num_pointers);
2639
2640 snprintf(hf_index, BASE_BUFFER_SIZE256, "hf_%s_%s_%s", ifname, function_name, ti->str);
2641 /* pass 0 generate subdissectors */
2642 if(pass==0){
2643 char filter_name[BASE_BUFFER_SIZE256];
2644 const char *hf;
2645
2646 snprintf(tmpstr, BASE_BUFFER_SIZE256, "%s_dissect_%s_%s", ifname, function_name, ti->str);
2647 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
2648
2649 snprintf(filter_name, BASE_BUFFER_SIZE256, "%s.%s.%s", ifname, function_name, ti->str);
2650 hf=register_hf_field(hf_index, ti->str, filter_name, type_item->ft_type, type_item->base_type, type_item->vals, type_item->mask, "");
2651
2652 FPRINTF(eth_code, "static unsigned\n");
2653 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep)\n", ptmpstr);
2654 FPRINTF(eth_code, "{\n");
2655 FPRINTF(eth_code, " uint32_t param=%s;\n",find_dissector_param_value(ptmpstr));
2656 FPRINTF(eth_code, " offset=%s(tvb, offset, pinfo, tree, di, drep, %s, param);\n", type_item->dissector, hf);
2657 FPRINTF(eth_code, " return offset;\n");
2658 FPRINTF(eth_code, "}\n");
2659 FPRINTF(eth_code, "\n");
2660
2661
2662 /* handle switch_is */
2663 switch(bi->flags&(BI_SIZE_IS0x00000010|BI_LENGTH_IS0x00000020)){
2664 case 0:
2665 break;
2666 case BI_SIZE_IS0x00000010|BI_LENGTH_IS0x00000020:
2667 snprintf(tmpstr, BASE_BUFFER_SIZE256, "ucvarray_%s", ptmpstr);
2668 FPRINTF(eth_code, "static unsigned\n");
2669 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep)\n", tmpstr);
2670 FPRINTF(eth_code, "{\n");
2671 FPRINTF(eth_code, " offset=dissect_ndr_ucvarray(tvb, offset, pinfo, tree, di, drep, %s);\n", ptmpstr);
2672 FPRINTF(eth_code, " return offset;\n");
2673 FPRINTF(eth_code, "}\n");
2674 FPRINTF(eth_code, "\n");
2675 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
2676 break;
2677 case BI_SIZE_IS0x00000010:
2678 snprintf(tmpstr, BASE_BUFFER_SIZE256, "ucarray_%s", ptmpstr);
2679 FPRINTF(eth_code, "static unsigned\n");
2680 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep)\n", tmpstr);
2681 FPRINTF(eth_code, "{\n");
2682 FPRINTF(eth_code, " offset=dissect_ndr_ucarray(tvb, offset, pinfo, tree, di, drep, %s);\n", ptmpstr);
2683 FPRINTF(eth_code, " return offset;\n");
2684 FPRINTF(eth_code, "}\n");
2685 FPRINTF(eth_code, "\n");
2686 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
2687 break;
2688 default:
2689 FPRINTF(stderrstderr, "ERROR: typedeffunction can not handle this combination of sizeis/lengthis\n");
2690 Exit(10);
2691 }
2692
2693 /* handle pointers */
2694 while(num_pointers--){
2695 pointer_type=pi->type;
2696 pi=pi->next;
2697 snprintf(tmpstr, BASE_BUFFER_SIZE256, "%s_%s", pointer_type, ptmpstr);
2698 FPRINTF(eth_code, "static unsigned\n");
2699 FPRINTF(eth_code, "%s(tvbuff_t *tvb, unsigned offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, uint8_t *drep)\n", tmpstr);
2700 FPRINTF(eth_code, "{\n");
2701 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);
2702 FPRINTF(eth_code, " return offset;\n");
2703 FPRINTF(eth_code, "}\n");
2704 FPRINTF(eth_code, "\n");
2705
2706 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
2707
2708 }
2709 }
2710
2711 if((pass==1)||(pass==2)){
2712 snprintf(tmpstr, BASE_BUFFER_SIZE256, "%s_dissect_%s_%s", ifname, function_name, ti->str);
2713 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
2714
2715 switch(bi->flags&(BI_SIZE_IS0x00000010|BI_LENGTH_IS0x00000020)){
2716 case 0:
2717 break;
2718 case BI_SIZE_IS0x00000010|BI_LENGTH_IS0x00000020:
2719 snprintf(tmpstr, BASE_BUFFER_SIZE256, "ucvarray_%s", ptmpstr);
2720 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
2721 break;
2722 case BI_SIZE_IS0x00000010:
2723 snprintf(tmpstr, BASE_BUFFER_SIZE256, "ucarray_%s", ptmpstr);
2724 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
2725 break;
2726 default:
2727 FPRINTF(stderrstderr, "ERROR: typedeffunction can not handle this combination of sizeis/lengthis\n");
2728 Exit(10);
2729 }
2730
2731 /* handle pointers */
2732 while(num_pointers--){
2733 pointer_type=pi->type;
2734 pi=pi->next;
2735 snprintf(tmpstr, BASE_BUFFER_SIZE256, "%s_%s", pointer_type, ptmpstr);
2736 ptmpstr=g_strdup(tmpstr)g_strdup_inline (tmpstr);
2737 }
2738
2739 if((pass==1)&&(bi->flags&BI_IN0x00000004)){
2740 FPRINTF(eth_code, " offset=%s(tvb, offset, pinfo, tree, di, drep);\n", ptmpstr);
2741 FPRINTF(eth_code, " offset=dissect_deferred_pointers(pinfo, tvb, offset, di, drep);\n");
2742 FPRINTF(eth_code, "\n");
2743 }
2744 if((pass==2)&&(bi->flags&BI_OUT0x00000008)){
2745 FPRINTF(eth_code, " offset=%s(tvb, offset, pinfo, tree, di, drep);\n", ptmpstr);
2746 FPRINTF(eth_code, " offset=dissect_deferred_pointers(pinfo, tvb, offset, di, drep);\n");
2747 FPRINTF(eth_code, "\n");
2748 }
2749 }
2750 ti=ti->next;
2751
2752
2753 if(!g_strcmp0(ti->str,",")){
2754 ti=ti->next;
2755 continue;
2756 }
2757 }
2758
2759 if((pass==1)||(pass==2)){
2760 if(pass==2){
2761 FPRINTF(eth_code, " offset=dissect_ntstatus(tvb, offset, pinfo, tree, di, drep, %s, NULL);\n", hf_status);
2762 FPRINTF(eth_code, "\n");
2763 }
2764 FPRINTF(eth_code, "\n");
2765 FPRINTF(eth_code, " return offset;\n");
2766 FPRINTF(eth_code, "}\n");
2767 }
2768
2769 FPRINTF(NULL((void*)0),"\nEND FUNCTION:%s pass:%d\n-------\n",function_name,pass);
2770
2771 /* only advance token_list for pass==2
2772 ti now points to the ')' token
2773 */
2774 if(pass==2){
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 if(!ti || g_strcmp0(ti->str,";")){
2782 FPRINTF(stderrstderr, "ERROR: function does not end with ';'\n");
2783 Exit(10);
2784 }
2785 ti=ti->next;
2786
2787 token_list=ti;
2788 }
2789}
2790
2791
2792/* this function will parse a
2793 typedef enum {
2794 or a
2795 typedef [ v1_enum ] enum {
2796 construct and generate the appropriate code.
2797 the typedef will be g_removed from the token_list once it has been processed
2798 the function assumes that the typedef is the first object in the token_list
2799*/
2800static void parsetypedefenum(void)
2801{
2802 token_item_t *ti;
2803 enum_list_t *enum_list, *el, *lastel;
2804 char *p;
2805 long val;
2806 int eval, enumsize;
2807 char dissectorname[BASE_BUFFER_SIZE256], valsstring[BASE_BUFFER_SIZE256];
2808 char *hfvalsstring;
2809
2810 enumsize=16;
2811
2812 ti=token_list;
2813 if(g_strcmp0(ti->str, "typedef")){
2814 FPRINTF(stderrstderr, "ERROR: typedefenum first token is not 'typedef'\n");
2815 Exit(10);
2816 }
2817 ti=ti->next;
2818
2819 /* this could be a [ v1_enum ] */
2820 if(!g_strcmp0(ti->str, "[")){
2821 ti=ti->next;
2822
2823 if(g_strcmp0(ti->str, "v1_enum")){
2824 FPRINTF(stderrstderr, "ERROR: typedefenum not 'v1_enum' inside brackets\n");
2825 Exit(10);
2826 }
2827 ti=ti->next;
2828
2829 if(g_strcmp0(ti->str, "]")){
2830 FPRINTF(stderrstderr, "ERROR: typedefenum 'v1_enum' is not followed by ']'\n");
2831 Exit(10);
2832 }
2833 ti=ti->next;
2834
2835 enumsize=32;
2836 }
2837
2838
2839 if(g_strcmp0(ti->str, "enum")){
2840 FPRINTF(stderrstderr, "ERROR: typedefenum second token is not 'enum'\n");
2841 Exit(10);
2842 }
2843 ti=ti->next;
2844
2845 if(g_strcmp0(ti->str, "{")){
2846 FPRINTF(stderrstderr, "ERROR: typedefenum third token is not '{'\n");
2847 Exit(10);
2848 }
2849 ti=ti->next;
2850
2851 /* now parse all values until we find the "}" */
2852 eval=0;
2853 enum_list=NULL((void*)0);
2854 lastel=NULL((void*)0);
2855 while(1){
2856 /* check for '}' */
2857 if(!g_strcmp0(ti->str,"}")){
2858 ti=ti->next;
2859 break;
2860 }
2861
2862 /* handle 4 types of entries:
2863 * 1, CONST = value,
2864 * 2, CONST,
2865 * 3, CONST = value}
2866 * 4, CONST}
2867 */
2868 el=g_new0(enum_list_t, 1)((enum_list_t *) g_malloc0_n ((1), sizeof (enum_list_t)));
2869 if (!el) {
2870 FPRINTF(stderrstderr, "Can't allocate memory. Exit.\n");
2871 exit(10);
2872 }
2873 el->next=NULL((void*)0);
2874 if(!enum_list){
2875 enum_list=el;
2876 } else {
2877 lastel->next=el;
2878 }
2879 lastel=el;
2880
2881 /* grab CONST */
2882 el->name=ti->str;
2883 ti=ti->next;
2884
2885 /* grab separator */
2886 if(!g_strcmp0(ti->str,"=")){
2887 ti=ti->next;
2888 /* grab value */
2889 val=strtol(ti->str,&p,0);
2890 if (p==ti->str||*p) {
2891 FPRINTF(stderrstderr, "ERROR: typedefenum value is not a number\n");
2892 Exit(10);
2893 }
2894 el->val=(int)val;
2895 ti=ti->next;
2896 } else {
2897 el->val=eval;
2898 }
2899 eval=el->val+1;
2900
2901 /* check for ',' */
2902 if(!g_strcmp0(ti->str,",")){
2903 ti=ti->next;
2904 continue;
2905 }
2906
2907 /* check for '}' */
2908 if(!g_strcmp0(ti->str,"}")){
2909 ti=ti->next;
2910 break;
2911 }
2912
2913 FPRINTF(stderrstderr,"ERROR: typedefenum should not be reached\n");
2914 Exit(10);
2915 }
2916
2917 /* verify that it ends with a ';' */
2918 if(g_strcmp0(ti->next->str,";")){
2919 FPRINTF(stderrstderr,"ERROR enum terminator is not ';'\n");
2920 Exit(10);
2921 }
2922
2923 snprintf(valsstring, BASE_BUFFER_SIZE256, "%s_%s_vals", ifname, ti->str);
2924 snprintf(dissectorname, DISSECTORNAME_MAXLEN256, "%s_dissect_%s", ifname, ti->str);
2925
2926 FPRINTF(NULL((void*)0),"\nENUM:%s\n-------\n",ti->str);
2927
2928 FPRINTF(eth_hdr, "\n");
2929 for(el=enum_list;el;el=el->next){
2930 FPRINTF(eth_hdr, "#define %s %d\n", el->name, el->val);
2931 }
2932
2933 FPRINTF(eth_hdr, "\n");
2934 FPRINTF(eth_hdr, "extern const value_string %s[];\n", valsstring);
2935 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);
2936
2937 FPRINTF(eth_code, "\n");
2938 FPRINTF(eth_code, "const value_string %s[] = {\n", valsstring);
2939
2940 for(el=enum_list;el;el=el->next){
2941 FPRINTF(eth_code, " { %d , \"%s\" },\n", el->val, el->name);
2942 }
2943 FPRINTF(eth_code, " { 0 , NULL }\n");
2944 FPRINTF(eth_code, "};\n");
2945
2946 FPRINTF(eth_code, "\n");
2947 FPRINTF(eth_code, "int\n");
2948 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);
2949 FPRINTF(eth_code, "{\n");
2950 switch(enumsize){
2951 case 16:
2952 FPRINTF(eth_code, " offset=dissect_ndr_uint16(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
2953 break;
2954 case 32:
2955 FPRINTF(eth_code, " offset=dissect_ndr_uint32(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
2956 break;
2957 default:
2958 FPRINTF(stderrstderr,"ERROR enum unknown size\n");
2959 Exit(10);
2960 }
2961
2962 FPRINTF(eth_code, " return offset;\n");
2963 FPRINTF(eth_code, "}\n");
2964 FPRINTF(eth_code, "\n");
2965
2966 hfvalsstring = g_strdup_printf("VALS(%s)", valsstring);
2967 switch(enumsize){
2968 case 16:
2969 register_new_type(ti->str, dissectorname, "FT_INT16", "BASE_DEC", "0", hfvalsstring, 2);
2970 break;
2971 case 32:
2972 register_new_type(ti->str, dissectorname, "FT_INT32", "BASE_DEC", "0", hfvalsstring, 4);
2973 break;
2974 default:
2975 FPRINTF(stderrstderr,"ERROR enum unknown size\n");
2976 g_free(hfvalsstring)(__builtin_object_size ((hfvalsstring), 0) != ((size_t) - 1))
? g_free_sized (hfvalsstring, __builtin_object_size ((hfvalsstring
), 0)) : (g_free) (hfvalsstring)
;
2977 Exit(10);
2978 }
2979 g_free(hfvalsstring)(__builtin_object_size ((hfvalsstring), 0) != ((size_t) - 1))
? g_free_sized (hfvalsstring, __builtin_object_size ((hfvalsstring
), 0)) : (g_free) (hfvalsstring)
;
2980
2981 FPRINTF(NULL((void*)0),"\n----------\nEND ENUM:%s\n",ti->str);
2982
2983 /* skip past the name and the ';' */
2984 token_list=ti->next->next;
2985}
2986
2987typedef struct _trimmed_prefixes_t {
2988 struct _trimmed_prefixes_t *next;
2989 char *name;
2990} trimmed_prefixes_t;
2991static trimmed_prefixes_t *prefixes_to_trim=NULL((void*)0);
2992
2993static void preparetrimprefix(char *prefix_name)
2994{
2995 trimmed_prefixes_t *new_prefix;
2996 new_prefix=g_new0(trimmed_prefixes_t, 1)((trimmed_prefixes_t *) g_malloc0_n ((1), sizeof (trimmed_prefixes_t
)))
;
2997 if (!new_prefix) {
2998 FPRINTF(stderrstderr, "Can't allocate memory. Exit.\n");
2999 exit(10);
3000 }
3001 new_prefix->next=prefixes_to_trim;
3002 prefixes_to_trim=new_prefix;
3003 new_prefix->name=g_strdup(prefix_name)g_strdup_inline (prefix_name);
3004}
3005
3006static void
3007trimprefix(void)
3008{
3009 token_item_t *ti;
3010 trimmed_prefixes_t *pfx;
3011 size_t len;
3012
3013 for(pfx=prefixes_to_trim;pfx;pfx=pfx->next){
3014 len=strlen(pfx->name);
3015 for(ti=token_list;ti;ti=ti->next){
3016 if(!strncmp(ti->str, pfx->name, len)){
3017 ti->str+=len;
3018 }
3019 }
3020 }
3021}
3022
3023static int Exit(int code)
3024{
3025 FPRINTF(stderrstderr, "The tokens remaining when aborting:\n");
3026 printtokenlist(10);
3027
3028 exit(code);
3029}
3030
3031static void usage(void)
3032{
3033 FPRINTF(stderrstderr, "Usage: idl2wrs <interface>\n");
3034}
3035
3036static void
3037mergefile(const char *name, FILE *outfile)
3038{
3039 FILE *infile;
3040
3041 FPRINTF(outfile, "\n\n/* INCLUDED FILE : %s */\n", name);
3042 infile=g_fopenfopen(name, "r");
3043 while(!feof(infile)){
1
Loop condition is true. Entering loop body
4
Loop condition is true. Entering loop body
3044 int ch;
3045 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
3046 if(ch!=-1){
3
Taking false branch
3047 fputc(ch, outfile);
3048 }
3049 }
3050 fclose(infile);
3051 FPRINTF(outfile, "/* END OF INCLUDED FILE : %s */\n\n\n", name);
3052}
3053
3054
3055
3056static char *
3057str_read_string(char *str, char **name)
3058{
3059 char tmpstr[BASE_BUFFER_SIZE256], *strptr;
3060 int skip_blanks;
3061 int quoted_string;
3062
3063 strptr=tmpstr;
3064 skip_blanks=1;
3065 quoted_string=0;
3066 while(1){
3067 if(!*str){
3068 *strptr=0;
3069 *name=g_strdup(tmpstr)g_strdup_inline (tmpstr);
3070 return str;
3071 }
3072 if(skip_blanks){
3073 if( (*str==' ') || (*str=='\t') ){
3074 str++;
3075 continue;
3076 }
3077 if( *str=='"' ){
3078 str++;
3079 quoted_string=1;
3080 }
3081 skip_blanks=0;
3082 continue;
3083 }
3084 if( (*str==' ') || (*str=='\t') ){
3085 if(quoted_string){
3086 *strptr++ = *str++;
3087 continue;
3088 }
3089 *strptr=0;
3090 *name=g_strdup(tmpstr)g_strdup_inline (tmpstr);
3091 return str;
3092 }
3093 if( (*str=='"') || (*str=='\n') ){
3094 *strptr=0;
3095 *name=g_strdup(tmpstr)g_strdup_inline (tmpstr);
3096 return ++str;
3097 }
3098 *strptr++ = *str++;
3099 }
3100 return NULL((void*)0);
3101}
3102
3103static void
3104readcnffile(FILE *fh)
3105{
3106 char cnfline[4 * BASE_BUFFER_SIZE256];
3107
3108 FPRINTF(NULL((void*)0), "Reading conformance file\n=======================\n");
3109 while(!feof(fh)){
3110 cnfline[0]=0;
3111 if(!fgets(cnfline, 4 * BASE_BUFFER_SIZE256, fh) || !cnfline[0]){
3112 continue;
3113 }
3114 if(cnfline[0]=='#'){
3115 /* ignore all comments */
3116 } else if(!strncmp(cnfline, "NOEMIT", 6)){
3117 no_emit_item_t *nei;
3118 char *str, *name;
3119
3120 str=cnfline+6;
3121 str_read_string(str, &name);
3122 nei=g_new0(no_emit_item_t, 1)((no_emit_item_t *) g_malloc0_n ((1), sizeof (no_emit_item_t)
))
;
3123 if (!nei) {
3124 FPRINTF(stderrstderr, "Can't allocate memory. Exit.\n");
3125 exit(10);
3126 }
3127 nei->next=no_emit_list;
3128 no_emit_list=nei;
3129 nei->name=name;
3130 FPRINTF(NULL((void*)0), "NOEMIT : %s\n", nei->name);
3131 } else if(!strncmp(cnfline, "TYPE", 4)){
3132 char *name, *dissectorname, *ft_type, *base_type;
3133 char *mask, *valsstring, *al;
3134 char *str;
3135 int alignment;
3136
3137 str=cnfline+4;
3138 str=str_read_string(str, &name);
3139 str=str_read_string(str, &dissectorname);
3140 str=str_read_string(str, &ft_type);
3141 str=str_read_string(str, &base_type);
3142 str=str_read_string(str, &mask);
3143 str=str_read_string(str, &valsstring);
3144 str_read_string(str, &al);
3145 if (ws_strtoi32(al, NULL((void*)0), &alignment)) {
3146 FPRINTF(NULL((void*)0), "TYPE : X%s,%sX\n", name, dissectorname);
3147 register_new_type(name, dissectorname, ft_type, base_type, mask, valsstring, alignment);
3148 }
3149 } else if(!strncmp(cnfline, "PARAM_VALUE", 11)){
3150 char *dissectorname, *value;
3151 char *str;
3152
3153 str=cnfline+11;
3154 str=str_read_string(str, &dissectorname);
3155 str_read_string(str, &value);
3156
3157 FPRINTF(NULL((void*)0), "PARAM_VALUE : %s=%s\n", dissectorname,value);
3158 register_dissector_param_value(dissectorname, value);
3159 } else if(!strncmp(cnfline, "HF_FIELD", 8)){
3160 char *hf_index, *title, *filter_name, *ft_type;
3161 char *base_type, *valsstring, *mask, *blurb;
3162 char *str;
3163
3164 str=cnfline+8;
3165 str=str_read_string(str, &hf_index);
3166 str=str_read_string(str, &title);
3167 str=str_read_string(str, &filter_name);
3168 str=str_read_string(str, &ft_type);
3169 str=str_read_string(str, &base_type);
3170 str=str_read_string(str, &valsstring);
3171 str=str_read_string(str, &mask);
3172 str_read_string(str, &blurb);
3173 FPRINTF(NULL((void*)0), "HF_FIELD: %s \"%s\"\n", hf_index, title);
3174 register_hf_field(hf_index, title, filter_name, ft_type, base_type, valsstring, mask, blurb);
3175 } else if(!strncmp(cnfline, "HF_RENAME", 9)){
3176 char *old_name, *new_name;
3177 char *str;
3178
3179 str=cnfline+9;
3180 str=str_read_string(str, &old_name);
3181 str_read_string(str, &new_name);
3182 FPRINTF(NULL((void*)0), "HF_RENAME: %s -> %s\n", old_name, new_name);
3183 register_hf_rename(old_name, new_name);
3184 } else if(!strncmp(cnfline, "UNION_TAG_SIZE", 14)){
3185 char *union_name, *union_tag;
3186 int32_t union_tag_size;
3187 union_tag_size_item_t *utsi;
3188 char *str;
3189
3190 str=cnfline+14;
3191 str=str_read_string(str, &union_name);
3192 str_read_string(str, &union_tag);
3193 if (!ws_strtoi32(union_tag, NULL((void*)0), &union_tag_size)) {
3194 FPRINTF(NULL((void*)0), "UNION_TAG_SIZE: invalid string: %s\n", union_tag);
3195 exit(10);
3196 }
3197 FPRINTF(NULL((void*)0), "UNION_TAG_SIZE: %s == %d\n", union_name, union_tag_size);
3198 utsi=g_new0(union_tag_size_item_t, 1)((union_tag_size_item_t *) g_malloc0_n ((1), sizeof (union_tag_size_item_t
)))
;
3199 if (!utsi) {
3200 FPRINTF(stderrstderr, "Can't allocate memory. Exit.\n");
3201 exit(10);
3202 }
3203 utsi->next=union_tag_size_list;
3204 union_tag_size_list=utsi;
3205 utsi->name=g_strdup(union_name)g_strdup_inline (union_name);
3206 utsi->size=union_tag_size;
3207 } else if(!strncmp(cnfline, "STRIP_PREFIX", 12)){
3208 char *prefix_name;
3209 char *str;
3210
3211 str=cnfline+12;
3212 str_read_string(str, &prefix_name);
3213 FPRINTF(NULL((void*)0), "STRIP_PREFIX: %s\n", prefix_name);
3214 preparetrimprefix(prefix_name);
3215 } else {
3216 FPRINTF(stderrstderr, "ERROR: could not parse cnf directive:%s\n",cnfline);
3217 exit(10);
3218 }
3219 }
3220}
3221
3222int main(int argc, const char *argv[])
3223{
3224 char idlfile[BASE_BUFFER_SIZE256];
3225 char tmplfile[BASE_BUFFER_SIZE256];
3226 char prefix_str[BASE_BUFFER_SIZE256];
3227 bracket_item_t *bi;
3228 FILE *fh;
3229
3230 if(argc!=2){
3231 usage();
3232 exit(0);
3233 }
3234
3235 eth_code=g_fopenfopen("ETH_CODE", "w");
3236 eth_hdr=g_fopenfopen("ETH_HDR", "w");
3237 eth_hfarr=g_fopenfopen("ETH_HFARR", "w");
3238 eth_hf=g_fopenfopen("ETH_HF", "w");
3239 eth_ettarr=g_fopenfopen("ETH_ETTARR", "w");
3240 eth_ett=g_fopenfopen("ETH_ETT", "w");
3241 eth_ft=g_fopenfopen("ETH_FT", "w");
3242 eth_handoff=g_fopenfopen("ETH_HANDOFF", "w");
3243
3244 snprintf(idlfile, BASE_BUFFER_SIZE256, "%s.cnf", argv[1]);
3245 fh=g_fopenfopen(idlfile,"r");
3246 if(fh){
3247 readcnffile(fh);
3248 fclose(fh);
3249 }
3250
3251 snprintf(idlfile, BASE_BUFFER_SIZE256, "%s.idl", argv[1]);
3252 fh=g_fopenfopen(idlfile,"r");
3253 if(!fh){
3254 FPRINTF(stderrstderr, "ERROR: could not open idl-file:%s\n", idlfile);
3255 Exit(0);
3256 }
3257
3258 linepos=0;
3259 tokenize(fh);
3260 prune_keyword_parameters("size_is");
3261 prune_keyword_parameters("length_is");
3262 rename_tokens("NTSTATUS", "WERROR");
3263 rename_tokens("unistr_noterm", "unistr");
3264 rename_tokens("ascstr_noterm", "ascstr");
3265 rename_tokens("hyper", "uint64");
3266 FPRINTF(NULL((void*)0),"\n\nParsing header:\n================\n");
3267 parseheader();
3268
3269 /* some idl files prepend a lot of symbols with <ifname>_
3270 search through the tokenlist and g_remove all such
3271 prefixes
3272 */
3273 snprintf(prefix_str, BASE_BUFFER_SIZE256, "%s_", ifname);
3274 preparetrimprefix(prefix_str);
3275 trimprefix();
3276
3277 /* this is the main loop, each iteration it tries to identify what
3278 kind of construct is the first entry in the token_list and call
3279 the appropriate handler
3280 */
3281 while(1) {
3282 /* just skip any [ ] that starts a new construct */
3283 if( !g_strcmp0(token_list->str, "[") ){
3284 token_list=parsebrackets(token_list, &bi);
3285 continue;
3286 }
3287
3288 /* typedef enum { */
3289 if( !g_strcmp0(token_list->str,"typedef")
3290 &&!g_strcmp0(token_list->next->str,"enum") ){
3291 parsetypedefenum();
3292 continue;
3293 }
3294
3295 /* typedef [ v1_enum ] enum { */
3296 if( !g_strcmp0(token_list->str,"typedef")
3297 &&!g_strcmp0(token_list->next->str,"[")
3298 &&!g_strcmp0(token_list->next->next->str,"v1_enum")
3299 &&!g_strcmp0(token_list->next->next->next->str,"]")
3300 &&!g_strcmp0(token_list->next->next->next->next->str,"enum") ){
3301 parsetypedefenum();
3302 continue;
3303 }
3304
3305 /* const */
3306 if( !g_strcmp0(token_list->str,"const") ){
3307 parseconst();
3308 continue;
3309 }
3310
3311 /* typedef struct { */
3312 if( !g_strcmp0(token_list->str,"typedef") ){
3313 token_item_t *tmpti;
3314
3315 tmpti=token_list->next;
3316 if( !g_strcmp0(tmpti->str, "[") ){
3317 tmpti=parsebrackets(tmpti, &bi);
3318 /* do some sanity checks here of bi->flags */
3319 }
3320 if( !g_strcmp0(tmpti->str, "struct") ){
3321 parsetypedefstruct(0);
3322 parsetypedefstruct(1);
3323 continue;
3324 }
3325 }
3326
3327 /* typedef union { */
3328 if( !g_strcmp0(token_list->str,"typedef") ){
3329 token_item_t *tmpti;
3330
3331 tmpti=token_list->next;
3332 if( !g_strcmp0(tmpti->str, "[") ){
3333 tmpti=parsebrackets(tmpti, &bi);
3334 /* do some sanity checks here of bi->flags */
3335 }
3336 if( !g_strcmp0(tmpti->str, "union") ){
3337 parsetypedefunion(0);
3338 parsetypedefunion(1);
3339 continue;
3340 }
3341 }
3342
3343 /* typedef bitmap { */
3344 if( !g_strcmp0(token_list->str,"typedef") ){
3345 token_item_t *tmpti;
3346
3347 tmpti=token_list->next;
3348 if( !g_strcmp0(tmpti->str, "[") ){
3349 tmpti=parsebrackets(tmpti, &bi);
3350 /* do some sanity checks here of bi->flags */
3351 }
3352 if( !g_strcmp0(tmpti->str, "bitmap") ){
3353 parsetypedefbitmap(0);
3354 parsetypedefbitmap(1);
3355 continue;
3356 }
3357 }
3358
3359 /* functions: WERROR function '(' */
3360 if( !g_strcmp0(token_list->str,"WERROR")
3361 &&!g_strcmp0(token_list->next->next->str,"(") ){
3362 parsefunction(0);
3363 parsefunction(1);
3364 parsefunction(2);
3365 continue;
3366 }
3367
3368 /* declare ... ; */
3369 if( !g_strcmp0(token_list->str,"declare") ){
3370 skipdeclare();
3371 continue;
3372 }
3373
3374
3375 break;
3376 };
3377
3378
3379 fclose(eth_code);
3380 fclose(eth_hdr);
3381 fclose(eth_hf);
3382 fclose(eth_hfarr);
3383 fclose(eth_ett);
3384 fclose(eth_ettarr);
3385 fclose(eth_ft);
3386 fclose(eth_handoff);
3387
3388 /* unless the token_list now only contains a single token : '}'
3389 we have failed to compile the idl file properly
3390 */
3391 if( g_strcmp0(token_list->str, "}") || token_list->next){
3392 FPRINTF(stderrstderr, "ERROR: we did not process all tokens. Compiler is incomplete.\n===========================================\n");
3393 printtokenlist(10);
3394 exit(10);
3395 }
3396
3397 check_hf_rename_refcount();
3398
3399 /* merge code and template into dissector */
3400 snprintf(line, 4 * BASE_BUFFER_SIZE256, "packet-dcerpc-%s.c", ifname);
3401 fh=g_fopenfopen(line, "w");
3402 snprintf(tmplfile, BASE_BUFFER_SIZE256, "packet-dcerpc-%s-template.c", argv[1]);
3403 tfh=g_fopenfopen(tmplfile, "r");
3404 if(!tfh){
3405 FPRINTF(stderrstderr, "ERROR: could not find %s\n", tmplfile);
3406 exit(10);
3407 }
3408 while(!feof(tfh)){
3409 line[0]=0;
3410 if(!fgets(line, 4 * BASE_BUFFER_SIZE256, tfh) || !line[0]){
3411 continue;
3412 }
3413 if(!strncmp(line, "ETH_CODE", 8)){
3414 mergefile("ETH_CODE",fh);
3415 } else if(!strncmp(line, "ETH_HDR", 7)){
3416 mergefile("ETH_HDR",fh);
3417 } else if(!strncmp(line, "ETH_HFARR", 9)){
3418 mergefile("ETH_HFARR",fh);
3419 } else if(!strncmp(line, "ETH_HF", 6)){
3420 mergefile("ETH_HF",fh);
3421 } else if(!strncmp(line, "ETH_ETTARR", 10)){
3422 mergefile("ETH_ETTARR",fh);
3423 } else if(!strncmp(line, "ETH_ETT", 7)){
3424 mergefile("ETH_ETT",fh);
3425 } else if(!strncmp(line, "ETH_FT", 6)){
3426 mergefile("ETH_FT",fh);
3427 } else if(!strncmp(line, "ETH_HANDOFF", 11)){
3428 mergefile("ETH_HANDOFF",fh);
3429 } else {
3430 fputs(line, fh);
3431 }
3432 }
3433 fclose(fh);
3434 fclose(tfh);
3435
3436 snprintf(line, 4 * BASE_BUFFER_SIZE256, "packet-dcerpc-%s.h", ifname);
3437 fh=g_fopenfopen(line, "w");
3438 snprintf(tmplfile, BASE_BUFFER_SIZE256, "packet-dcerpc-%s-template.h", argv[1]);
3439 tfh=g_fopenfopen(tmplfile, "r");
3440 if(!tfh){
3441 FPRINTF(stderrstderr, "ERROR: could not find %s\n", tmplfile);
3442 exit(10);
3443 }
3444 while(!feof(tfh)){
3445 line[0]=0;
3446 if(!fgets(line, 4 * BASE_BUFFER_SIZE256, tfh) || !line[0]){
3447 continue;
3448 }
3449 if(!strncmp(line, "ETH_CODE", 8)){
3450 mergefile("ETH_CODE",fh);
3451 } else if(!strncmp(line, "ETH_HDR", 7)){
3452 mergefile("ETH_HDR",fh);
3453 } else if(!strncmp(line, "ETH_HFARR", 9)){
3454 mergefile("ETH_HFARR",fh);
3455 } else if(!strncmp(line, "ETH_HF", 6)){
3456 mergefile("ETH_HF",fh);
3457 } else if(!strncmp(line, "ETH_ETTARR", 10)){
3458 mergefile("ETH_ETTARR",fh);
3459 } else if(!strncmp(line, "ETH_ETT", 7)){
3460 mergefile("ETH_ETT",fh);
3461 } else if(!strncmp(line, "ETH_FT", 6)){
3462 mergefile("ETH_FT",fh);
3463 } else if(!strncmp(line, "ETH_HANDOFF", 11)){
3464 mergefile("ETH_HANDOFF",fh);
3465 } else {
3466 fputs(line, fh);
3467 }
3468 }
3469
3470 g_print("%s was successfully compiled\n", ifname);
3471
3472 fclose(fh);
3473 fclose(tfh);
3474
3475 if (g_removeremove("ETH_CODE") == -1) {
3476 FPRINTF(stderrstderr, "Can't remove ETH_CODE");
3477 }
3478 if (g_removeremove("ETH_HDR") == -1) {
3479 FPRINTF(stderrstderr, "Can't remove ETH_CODE");
3480 }
3481 if (g_removeremove("ETH_HFARR") == -1) {
3482 FPRINTF(stderrstderr, "Can't remove ETH_CODE");
3483 }
3484 if (g_removeremove("ETH_HF") == -1) {
3485 FPRINTF(stderrstderr, "Can't remove ETH_CODE");
3486 }
3487 if (g_removeremove("ETH_ETTARR") == -1) {
3488 FPRINTF(stderrstderr, "Can't remove ETH_CODE");
3489 }
3490 if (g_removeremove("ETH_ETT") == -1) {
3491 FPRINTF(stderrstderr, "Can't remove ETH_CODE");
3492 }
3493 if (g_removeremove("ETH_FT") == -1) {
3494 FPRINTF(stderrstderr, "Can't remove ETH_CODE");
3495 }
3496 if (g_removeremove("ETH_HANDOFF") == -1) {
3497 FPRINTF(stderrstderr, "Can't remove ETH_CODE");
3498 }
3499
3500
3501 return 0;
3502}
3503
3504/*
3505 * Editor modelines - https://www.wireshark.org/tools/modelines.html
3506 *
3507 * Local variables:
3508 * c-basic-offset: 8
3509 * tab-width: 8
3510 * indent-tabs-mode: t
3511 * End:
3512 *
3513 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
3514 * :indentSize=8:tabSize=8:noTabs=false:
3515 */