1 ###################################################
2 # Samba4 parser generator for IDL structures
3 # Copyright tridge@samba.org 2000-2003
4 # Copyright tpot@samba.org 2001,2004
5 # released under the GNU GPL
11 # the list of needed functions
25 #####################################################################
26 # parse a properties list
27 sub ParseProperties($)
30 foreach my $d (@{$props}) {
31 if (ref($d) ne "HASH") {
34 foreach my $k (keys %{$d}) {
35 pidl "[$k($d->{$k})] ";
41 ###################################
42 # find a sibling var in a structure
47 my($fn) = $e->{PARENT};
49 if ($name =~ /\*(.*)/) {
53 if ($fn->{TYPE} eq "FUNCTION") {
54 for my $e2 (@{$fn->{DATA}}) {
55 if ($e2->{NAME} eq $name) {
61 for my $e2 (@{$fn->{ELEMENTS}}) {
62 if ($e2->{NAME} eq $name) {
66 die "invalid sibling '$name'";
69 ####################################################################
70 # work out the name of a size_is() variable
76 my($fn) = $e->{PARENT};
78 if (util::is_constant($size)) {
82 if ($size =~ /ndr->|\(/) {
88 if ($size =~ /\*(.*)/) {
93 if ($fn->{TYPE} ne "FUNCTION") {
94 return $prefix . "elt_$size";
97 my $e2 = find_sibling($e, $size);
99 if (util::has_property($e2, "in") && util::has_property($e2, "out")) {
100 return $prefix . "elt_$size";
102 if (util::has_property($e2, "in")) {
103 return $prefix . "elt_$size";
105 if (util::has_property($e2, "out")) {
106 return $prefix . "elt_$size";
109 die "invalid variable in $size for element $e->{NAME} in $fn->{NAME}\n";
113 #####################################################################
114 # work out is a parse function should be declared static or not
118 if ($fn->{TYPE} eq "TYPEDEF") {
119 if (util::has_property($fn->{DATA}, "public")) {
124 if ($fn->{TYPE} eq "FUNCTION") {
125 if (util::has_property($fn, "public")) {
133 ###################################################################
134 # setup any special flags for an element or structure
138 my $flags = util::has_property($e, "flag");
139 if (defined $flags) {
140 pidl "\t{ guint32 _flags_save_$e->{TYPE} = ndr->flags;\n";
141 pidl "\tndr->flags |= $flags;\n";
145 ###################################################################
146 # end any special flags for an element or structure
150 my $flags = util::has_property($e, "flag");
151 if (defined $flags) {
152 pidl "\tndr->flags = _flags_save_$e->{TYPE};\n\t}\n";
157 #####################################################################
158 # work out the correct alignment for a structure
164 for my $e (@{$s->{ELEMENTS}}) {
167 if (!util::need_wire_pointer($e)
168 && defined $structs{$e->{TYPE}}) {
169 if ($structs{$e->{TYPE}}->{DATA}->{TYPE} eq "STRUCT") {
170 $a = struct_alignment($structs{$e->{TYPE}}->{DATA});
171 } elsif ($structs{$e->{TYPE}}->{DATA}->{TYPE} eq "UNION") {
172 if (defined $structs{$e->{TYPE}}->{DATA}) {
173 $a = union_alignment($structs{$e->{TYPE}}->{DATA});
177 $a = util::type_align($e);
188 #####################################################################
189 # work out the correct alignment for a union
196 foreach my $e (@{$u->{DATA}}) {
199 if ($e->{TYPE} eq "EMPTY") {
203 if (!util::need_wire_pointer($e)
204 && defined $structs{$e->{DATA}->{TYPE}}) {
205 my $s = $structs{$e->{DATA}->{TYPE}};
206 if ($s->{DATA}->{TYPE} eq "STRUCT") {
207 $a = struct_alignment($s->{DATA});
208 } elsif ($s->{DATA}->{TYPE} eq "UNION") {
209 $a = union_alignment($s->{DATA});
212 $a = util::type_align($e->{DATA});
223 #####################################################################
224 # parse an array - pull side
225 sub ParseArrayPull($$)
228 my $ndr_flags = shift;
230 my $size = find_size_var($e, util::array_size($e));
231 my $alloc_size = $size;
233 # if this is a conformant array then we use that size to allocate, and make sure
234 # we allocate enough to pull the elements
235 if (defined $e->{CONFORMANT_SIZE}) {
236 $alloc_size = $e->{CONFORMANT_SIZE};
238 pidl "\tif ($size > $alloc_size) {\n";
239 pidl "\t\tproto_tree_add_text(subtree, ndr->tvb, ndr->offset, 0, \"Bad conformant size (%u should be %u)\", $alloc_size, $size);\n";
240 pidl "\t\tif (check_col(ndr->pinfo->cinfo, COL_INFO))\n";
241 pidl "\t\t\tcol_append_fstr(ndr->pinfo->cinfo, COL_INFO, \", Bad conformant size\", $alloc_size, $size);\n";
242 pidl "\t\treturn;\n";
244 } elsif (!util::is_inline_array($e)) {
245 # if ($var_prefix =~ /^r->out/ && $size =~ /^\*r->in/) {
246 # my $size2 = substr($size, 1);
247 # pidl "if (ndr->flags & LIBNDR_FLAG_REF_ALLOC) { NDR_ALLOC(ndr, $size2); }\n";
250 # non fixed arrays encode the size just before the array
252 pidl "\t\tguint32 _array_size;\n\n";
253 pidl "\t\tndr_pull_uint32(ndr, subtree, hf_array_size, &_array_size);\n";
254 if ($size =~ /r->in/) {
255 pidl "\t\tif (!(ndr->flags & LIBNDR_FLAG_REF_ALLOC) && _array_size != $size) {\n";
257 pidl "\t\tif ($size != _array_size) {\n";
259 pidl "\t\t\tproto_tree_add_text(subtree, ndr->tvb, ndr->offset, 0, \"Bad array size (%u should be %u)\", _array_size, $size);\n";
260 pidl "\t\t\tif (check_col(ndr->pinfo->cinfo, COL_INFO))\n";
261 pidl "\t\t\t\tcol_append_fstr(ndr->pinfo->cinfo, COL_INFO, \", Bad array size\", _array_size, $size);\n";
262 pidl "\t\t\treturn;\n";
264 if ($size =~ /r->in/) {
265 pidl "else { $size = _array_size; }\n";
270 # if ((util::need_alloc($e) && !util::is_fixed_array($e)) ||
271 # ($var_prefix eq "r->in." && util::has_property($e, "ref"))) {
272 # if (!util::is_inline_array($e) || $ndr_flags eq "NDR_SCALARS") {
273 # pidl "\t\tNDR_ALLOC_N(ndr, $var_prefix$e->{NAME}, MAX(1, $alloc_size));\n";
277 # if (($var_prefix eq "r->out." && util::has_property($e, "ref"))) {
278 # if (!util::is_inline_array($e) || $ndr_flags eq "NDR_SCALARS") {
279 # pidl "\tif (ndr->flags & LIBNDR_FLAG_REF_ALLOC) {";
280 # pidl "\t\tNDR_ALLOC_N(ndr, $var_prefix$e->{NAME}, MAX(1, $alloc_size));\n";
287 if (my $length = util::has_property($e, "length_is")) {
288 $length = find_size_var($e, $length);
289 pidl "\t\tguint32 _offset, _length;\n";
290 pidl "\t\tndr_pull_uint32(ndr, subtree, hf_array_offset, &_offset);\n";
291 pidl "\t\tndr_pull_uint32(ndr, subtree, hf_array_length, &_length);\n";
292 pidl "\t\tif (_offset != 0) {\n";
293 pidl "\t\t\tproto_tree_add_text(subtree, ndr->tvb, ndr->offset, 0, \"Bad array offset %d\", _offset);\n";
294 pidl "\t\t\tif (check_col(ndr->pinfo->cinfo, COL_INFO))\n";
295 pidl "\t\t\t\tcol_append_fstr(ndr->pinfo->cinfo, COL_INFO, \"Bad array offset %d\", _offset);\n";
296 pidl "\t\t\treturn;\n";
298 pidl "\t\tif (_length > $size || _length != $length) {\n";
299 pidl "\t\t\tproto_tree_add_text(subtree, ndr->tvb, ndr->offset, 0, \"Bad array length %d > size %d\", _offset, $size);\n";
300 pidl "\t\t\tif (check_col(ndr->pinfo->cinfo, COL_INFO))\n";
301 pidl "\t\t\t\tcol_append_fstr(ndr->pinfo->cinfo, COL_INFO, \", Bad array length %d > size %d\", _offset, $size);\n";
302 pidl "\t\t\treturn;\n";
307 if (util::is_scalar_type($e->{TYPE})) {
308 pidl "\t\tndr_pull_array_$e->{TYPE}(ndr, subtree, hf_$e->{NAME}_$e->{TYPE}, $ndr_flags, $size);\n";
310 pidl "\t\tndr_pull_array(ndr, subtree, $ndr_flags, $size, ndr_pull_$e->{TYPE});\n";
317 #####################################################################
318 # parse scalars in a structure element - pull size
319 sub ParseElementPullSwitch($$$)
322 my($ndr_flags) = shift;
324 my $switch_var = find_size_var($e, $switch);
326 my $cprefix = util::c_pull_prefix($e);
328 my $utype = $structs{$e->{TYPE}};
329 if (!defined $utype ||
330 !util::has_property($utype->{DATA}, "nodiscriminant")) {
331 my $e2 = find_sibling($e, $switch);
332 pidl "\tguint16 _level;\n";
333 pidl "\tif (($ndr_flags) & NDR_SCALARS) {\n";
334 pidl "\t\tndr_pull_level(ndr, subtree, hf_level, &_level);\n";
338 my $sub_size = util::has_property($e, "subcontext");
339 if (defined $sub_size) {
340 pidl "\tndr_pull_subcontext_union_fn(ndr, subtree, $sub_size, $switch_var, (ndr_pull_union_fn_t) ndr_pull_$e->{TYPE});\n";
342 pidl "\tndr_pull_$e->{TYPE}(ndr, subtree, $ndr_flags, _level);\n";
348 #####################################################################
349 # parse scalars in a structure element - pull size
350 sub ParseElementPullScalar($$)
353 my($ndr_flags) = shift;
354 my $cprefix = util::c_pull_prefix($e);
355 my $sub_size = util::has_property($e, "subcontext");
359 if (util::has_property($e, "relative")) {
360 pidl "\tndr_pull_relative(ndr, subtree, ndr_pull_$e->{TYPE});\n";
361 } elsif (util::is_inline_array($e)) {
362 ParseArrayPull($e, "NDR_SCALARS");
363 } elsif (util::need_wire_pointer($e)) {
364 pidl "\tndr_pull_ptr(ndr, subtree, hf_ptr, &ptr_$e->{NAME});\n";
365 # pidl "\tif (ptr_$e->{NAME}) {\n";
366 # pidl "\t\tNDR_ALLOC(ndr, $var_prefix$e->{NAME});\n";
367 # pidl "\t} else {\n";
368 # pidl "\t\t$var_prefix$e->{NAME} = NULL;\n";
370 } elsif (util::need_alloc($e)) {
371 # no scalar component
372 } elsif (my $switch = util::has_property($e, "switch_is")) {
373 ParseElementPullSwitch($e, $ndr_flags, $switch);
374 } elsif (defined $sub_size) {
375 if (util::is_builtin_type($e->{TYPE})) {
376 pidl "\tndr_pull_subcontext_fn(ndr, subtree, $sub_size, (ndr_pull_fn_t) ndr_pull_$e->{TYPE});\n";
378 pidl "\tndr_pull_subcontext_flags_fn(ndr, subtree, $sub_size, (ndr_pull_flags_fn_t) ndr_pull_$e->{TYPE});\n";
380 } elsif (util::is_builtin_type($e->{TYPE})) {
381 pidl "\tndr_pull_$e->{TYPE}(ndr, subtree, hf_$e->{NAME}_$e->{TYPE}, &elt_$e->{NAME});\n";
383 pidl "\tndr_pull_$e->{TYPE}(ndr, subtree, $ndr_flags);\n";
389 #####################################################################
390 # parse buffers in a structure element - pull side
391 sub ParseElementPullBuffer($$)
394 my($ndr_flags) = shift;
395 my $cprefix = util::c_pull_prefix($e);
396 my $sub_size = util::has_property($e, "subcontext");
398 if (util::is_pure_scalar($e)) {
402 if (util::has_property($e, "relative")) {
408 if (util::need_wire_pointer($e)) {
409 pidl "\tif (ptr_$e->{NAME}) {\n";
412 if (util::is_inline_array($e)) {
413 ParseArrayPull($e, "NDR_BUFFERS");
414 } elsif (util::array_size($e)) {
415 ParseArrayPull($e, "NDR_SCALARS|NDR_BUFFERS");
416 } elsif (my $switch = util::has_property($e, "switch_is")) {
417 if ($e->{POINTERS}) {
418 ParseElementPullSwitch($e, "NDR_SCALARS|NDR_BUFFERS", $switch);
420 ParseElementPullSwitch($e, "NDR_BUFFERS", $switch);
422 } elsif (defined $sub_size) {
423 if ($e->{POINTERS}) {
424 if (util::is_builtin_type($e->{TYPE})) {
425 pidl "\tndr_pull_subcontext_fn(ndr, subtree, $sub_size, _pull_$e->{TYPE});\n";
427 pidl "\tndr_pull_subcontext_flags_fn(ndr, subtree, $sub_size, ndr_pull_$e->{TYPE});\n";
430 } elsif (util::is_builtin_type($e->{TYPE})) {
431 pidl "\t\tndr_pull_$e->{TYPE}(ndr, subtree, hf_$e->{NAME}_$e->{TYPE}, &elt_$e->{NAME});\n";
432 } elsif ($e->{POINTERS}) {
433 pidl "\t\tndr_pull_$e->{TYPE}(ndr, subtree, NDR_SCALARS|NDR_BUFFERS);\n";
435 pidl "\t\tndr_pull_$e->{TYPE}(ndr, subtree, $ndr_flags);\n";
438 if (util::need_wire_pointer($e)) {
445 #####################################################################
446 # parse a struct - pull side
447 sub ParseStructPull($)
452 for my $x (@{$struct->{ELEMENTS}}) {
453 pidl "\tg$x->{TYPE} elt_$x->{NAME};\n",
454 if util::is_builtin_type($x->{TYPE});
457 if (! defined $struct->{ELEMENTS}) {
461 # see if the structure contains a conformant array. If it
462 # does, then it must be the last element of the structure, and
463 # we need to pull the conformant length early, as it fits on
464 # the wire before the structure (and even before the structure
466 my $e = $struct->{ELEMENTS}[-1];
467 if (defined $e->{ARRAY_LEN} && $e->{ARRAY_LEN} eq "*") {
469 pidl "\tguint32 _conformant_size;\n";
470 $conform_e->{CONFORMANT_SIZE} = "_conformant_size";
473 # declare any internal pointers we need
474 foreach my $e (@{$struct->{ELEMENTS}}) {
475 if (util::need_wire_pointer($e) &&
476 !util::has_property($e, "relative")) {
477 pidl "\tguint32 ptr_$e->{NAME};\n";
483 start_flags($struct);
485 pidl "\titem = proto_tree_add_text(tree, ndr->tvb, ndr->offset, 0, \"$struct->{PARENT}{NAME}\");\n";
486 pidl "\tsubtree = proto_item_add_subtree(item, ett_$struct->{PARENT}{NAME});\n";
488 pidl "\tif (!(ndr_flags & NDR_SCALARS)) goto buffers;\n";
490 pidl "\tndr_pull_struct_start(ndr);\n";
492 if (defined $conform_e) {
493 pidl "\tndr_pull_uint32(ndr, subtree, hf_conformant_size, &$conform_e->{CONFORMANT_SIZE});\n";
496 my $align = struct_alignment($struct);
497 pidl "\tndr_pull_align(ndr, $align);\n";
499 foreach my $e (@{$struct->{ELEMENTS}}) {
500 ParseElementPullScalar($e, "NDR_SCALARS");
504 pidl "\tif (!(ndr_flags & NDR_BUFFERS)) goto done;\n";
505 foreach my $e (@{$struct->{ELEMENTS}}) {
506 ParseElementPullBuffer($e, "NDR_BUFFERS");
509 pidl "\tndr_pull_struct_end(ndr);\n";
516 #####################################################################
517 # parse a union - pull side
518 sub ParseUnionPull($)
521 my $have_default = 0;
525 pidl "\titem = proto_tree_add_text(tree, ndr->tvb, ndr->offset, 0, \"$e->{PARENT}{NAME}\");\n";
526 pidl "\tsubtree = proto_item_add_subtree(item, ett_$e->{PARENT}{NAME});\n";
528 pidl "\tif (!(ndr_flags & NDR_SCALARS)) goto buffers;\n";
530 pidl "\tndr_pull_struct_start(ndr);\n";
532 # my $align = union_alignment($e);
533 # pidl "\tndr_pull_align(ndr, $align);\n";
535 pidl "\tswitch (level) {\n";
536 foreach my $el (@{$e->{DATA}}) {
537 if ($el->{CASE} eq "default") {
538 pidl "\t\tdefault: {\n";
541 pidl "\tcase $el->{CASE}: {\n";
543 if ($el->{TYPE} eq "UNION_ELEMENT") {
544 my $e2 = $el->{DATA};
545 if ($e2->{POINTERS}) {
546 pidl "\t\tguint32 ptr_$e2->{NAME};\n";
548 ParseElementPullScalar($el->{DATA}, "NDR_SCALARS");
550 pidl "\t\tbreak;\n\t}\n";
552 if (! $have_default) {
554 pidl "\t\tproto_tree_add_text(subtree, ndr->tvb, ndr->offset, 0, \"Bad switch value %u\", level);\n";
555 pidl "\t\tif (check_col(ndr->pinfo->cinfo, COL_INFO))\n";
556 pidl "\t\t\tcol_append_fstr(ndr->pinfo->cinfo, COL_INFO, \", 6Bad switch value %u\", level);\n";
557 pidl "\t\treturn;\n";
561 pidl "\tif (!(ndr_flags & NDR_BUFFERS)) goto done;\n";
562 pidl "\tswitch (level) {\n";
563 foreach my $el (@{$e->{DATA}}) {
564 if ($el->{CASE} eq "default") {
567 pidl "\tcase $el->{CASE}:\n";
569 if ($el->{TYPE} eq "UNION_ELEMENT") {
570 ParseElementPullBuffer($el->{DATA}, "NDR_BUFFERS");
572 pidl "\t\tbreak;\n\n";
574 if (! $have_default) {
576 pidl "\t\tproto_tree_add_text(subtree, ndr->tvb, ndr->offset, 0, \"Bad switch value %u\", level);\n";
577 pidl "\t\tif (check_col(ndr->pinfo->cinfo, COL_INFO))\n";
578 pidl "\t\t\tcol_append_fstr(ndr->pinfo->cinfo, COL_INFO, \", 7Bad switch value %u\", level);\n";
579 pidl "\t\treturn;\n";
582 pidl "\tndr_pull_struct_end(ndr);\n";
587 #####################################################################
588 # parse a enum - pull side
596 for my $x (@{$e->{ELEMENTS}}) {
597 if ($x =~ /([a-zA-Z_]+)=([0-9]+)/) {
603 pidl "#define $name $ndx\n";
610 #####################################################################
616 if (ref($data) eq "HASH") {
617 ($data->{TYPE} eq "STRUCT") &&
618 ParseStructPull($data);
619 ($data->{TYPE} eq "UNION") &&
620 ParseUnionPull($data);
621 ($data->{TYPE} eq "ENUM") &&
622 ParseEnumPull($data);
626 #####################################################################
627 # parse a typedef - pull side
628 sub ParseTypedefPull($)
631 my $static = fn_prefix($e);
633 # if (! $needed{"pull_$e->{NAME}"}) {
634 # print "pull_$e->{NAME} not needed\n";
639 pidl IdlDump::DumpTypedef($e);
642 if ($e->{DATA}->{TYPE} eq "STRUCT") {
643 pidl "static gint ett_$e->{NAME} = -1;\n\n";
644 pidl $static . "void ndr_pull_$e->{NAME}(struct e_ndr_pull *ndr, proto_tree *tree, int ndr_flags)";
646 pidl "\tproto_item *item = NULL;\n";
647 pidl "\tproto_tree *subtree = NULL;\n";
648 ParseTypePull($e->{DATA});
652 if ($e->{DATA}->{TYPE} eq "UNION") {
653 pidl "static gint ett_$e->{NAME} = -1;\n\n";
654 pidl $static . "void ndr_pull_$e->{NAME}(struct e_ndr_pull *ndr, proto_tree *tree, int ndr_flags, int level)";
656 pidl "\tproto_item *item = NULL;\n";
657 pidl "\tproto_tree *subtree = NULL;\n";
658 ParseTypePull($e->{DATA});
662 if ($e->{DATA}->{TYPE} eq "ENUM") {
663 ParseEnumPull($e->{DATA});
668 #####################################################################
669 # parse a function element
670 sub ParseFunctionElementPull($$)
675 if (util::array_size($e)) {
676 if (util::need_wire_pointer($e)) {
677 pidl "\tndr_pull_ptr(ndr, &ptr_$e->{NAME});\n";
678 pidl "\tif (ptr_$e->{NAME}) {\n";
679 } elsif ($inout eq "out" && util::has_property($e, "ref")) {
680 pidl "\tif (r->$inout.$e->{NAME}) {\n";
684 ParseArrayPull($e, "NDR_SCALARS|NDR_BUFFERS");
687 if ($inout eq "out" && util::has_property($e, "ref")) {
688 # pidl "\tif (ndr->flags & LIBNDR_FLAG_REF_ALLOC) {\n";
689 # pidl "\tNDR_ALLOC(ndr, r->out.$e->{NAME});\n";
692 if ($inout eq "in" && util::has_property($e, "ref")) {
693 # pidl "\tNDR_ALLOC(ndr, r->in.$e->{NAME});\n";
696 ParseElementPullScalar($e, "NDR_SCALARS|NDR_BUFFERS");
697 if ($e->{POINTERS}) {
698 ParseElementPullBuffer($e, "NDR_SCALARS|NDR_BUFFERS");
703 #####################################################################
705 sub ParseFunctionPull($)
708 my $static = fn_prefix($fn);
710 # Comment displaying IDL for this function
713 pidl IdlDump::DumpFunction($fn);
718 pidl $static . "int $fn->{NAME}_rqst(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep)\n";
720 pidl "\tproto_tree *subtree = tree;\n";
721 pidl "\tstruct e_ndr_pull *ndr = ndr_pull_init(tvb, offset, pinfo, drep);\n";
723 # declare any internal pointers we need
724 foreach my $e (@{$fn->{DATA}}) {
726 if (util::need_wire_pointer($e) &&
727 util::has_property($e, "in")) {
728 pidl "\tguint32 ptr_$e->{NAME};\n";
731 if (util::has_property($e, "in")) {
732 pidl "\tg$e->{TYPE} elt_$e->{NAME};\n",
733 if util::is_builtin_type($e->{TYPE});
739 foreach my $e (@{$fn->{DATA}}) {
740 if (util::has_property($e, "in")) {
741 ParseFunctionElementPull($e, "in");
745 pidl "\toffset = ndr->offset;\n";
746 pidl "\tndr_pull_free(ndr);\n";
748 pidl "\treturn offset;\n";
753 pidl $static . "int $fn->{NAME}_resp(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep)\n";
755 pidl "\tproto_tree *subtree = tree;\n";
756 pidl "\tstruct e_ndr_pull *ndr = ndr_pull_init(tvb, offset, pinfo, drep);\n";
758 # declare any internal pointers we need
759 foreach my $e (@{$fn->{DATA}}) {
761 if (util::need_wire_pointer($e) &&
762 util::has_property($e, "out")) {
763 pidl "\tguint32 ptr_$e->{NAME};\n";
766 if (util::has_property($e, "out")) {
767 pidl "\tg$e->{TYPE} elt_$e->{NAME};\n",
768 if util::is_builtin_type($e->{TYPE});
774 foreach my $e (@{$fn->{DATA}}) {
775 if (util::has_property($e, "out")) {
776 ParseFunctionElementPull($e, "out");
780 if ($fn->{RETURN_TYPE} && $fn->{RETURN_TYPE} ne "void") {
781 pidl "\tndr_pull_$fn->{RETURN_TYPE}(ndr, subtree, hf_rc);\n";
784 pidl "\toffset = ndr->offset;\n";
785 pidl "\tndr_pull_free(ndr);\n";
787 pidl "\treturn offset;\n";
791 #####################################################################
792 # produce a function call table
795 my($interface) = shift;
796 my($data) = $interface->{DATA};
798 my $uname = uc $interface->{NAME};
800 foreach my $d (@{$data}) {
801 if ($d->{TYPE} eq "FUNCTION") { $count++; }
808 pidl "static dcerpc_sub_dissector dcerpc_dissectors[] = {\n";
810 foreach my $d (@{$data}) {
811 if ($d->{TYPE} eq "FUNCTION") {
812 # Strip module name from function name, if present
814 $n = substr($d->{NAME}, length($module) + 1),
815 if $module eq substr($d->{NAME}, 0, length($module));
816 pidl "\t{ $num, \"$n\",\n";
817 pidl "\t\t$d->{NAME}_rqst,\n";
818 pidl "\t\t$d->{NAME}_resp },\n";
825 #####################################################################
826 # parse the interface definitions
827 sub ParseInterface($)
829 my($interface) = shift;
830 my($data) = $interface->{DATA};
832 foreach my $d (@{$data}) {
833 if ($d->{TYPE} eq "TYPEDEF") {
834 $structs{$d->{NAME}} = $d;
838 foreach my $d (@{$data}) {
839 ($d->{TYPE} eq "TYPEDEF") &&
840 ParseTypedefPull($d);
841 ($d->{TYPE} eq "FUNCTION") &&
842 ParseFunctionPull($d);
845 FunctionTable($interface);
852 return "FT_UINT32", if ($t eq "uint32");
853 return "FT_UINT16", if ($t eq "uint16");
854 return "FT_UINT8", if ($t eq "uint8");
862 return "BASE_DEC", if ($t eq "uint32") or ($t eq "uint16") or
867 sub NeededFunction($)
870 foreach my $e (@{$fn->{DATA}}) {
871 $needed{"hf_$e->{NAME}_$e->{TYPE}"} = {
872 'name' => $e->{NAME},
873 'type' => $e->{TYPE},
874 'ft' => type2ft($e->{TYPE}),
875 'base' => type2base($e->{TYPE})
885 if (util::has_property($t->{DATA}, "public")) {
886 $needed{"pull_$t->{NAME}"} = 1;
889 if ($t->{DATA}->{TYPE} eq "STRUCT") {
891 $needed{"ett_$t->{NAME}"} = 1;
893 for my $e (@{$t->{DATA}->{ELEMENTS}}) {
895 $needed{"hf_$e->{NAME}_$e->{TYPE}"} = {
896 'name' => $e->{NAME},
897 'type' => $e->{TYPE},
898 'ft' => type2ft($e->{TYPE}),
899 'base' => type2base($e->{TYPE})
902 $e->{PARENT} = $t->{DATA};
903 if ($needed{"pull_$t->{NAME}"}) {
904 $needed{"pull_$e->{TYPE}"} = 1;
908 if ($t->{DATA}->{TYPE} eq "UNION") {
910 $needed{"ett_$t->{NAME}"} = 1;
912 for my $e (@{$t->{DATA}->{DATA}}) {
913 $e->{PARENT} = $t->{DATA};
914 if ($e->{TYPE} eq "UNION_ELEMENT") {
915 if ($needed{"pull_$t->{NAME}"}) {
916 $needed{"pull_$e->{DATA}->{TYPE}"} = 1;
923 #####################################################################
924 # work out what parse functions are needed
927 my($interface) = shift;
928 my($data) = $interface->{DATA};
929 foreach my $d (@{$data}) {
930 ($d->{TYPE} eq "FUNCTION") &&
933 foreach my $d (reverse @{$data}) {
934 ($d->{TYPE} eq "TYPEDEF") &&
939 #####################################################################
940 # parse the interface definitions
945 $if_uuid = $h->{PROPERTIES}->{uuid};
946 $if_version = $h->{PROPERTIES}->{version};
947 $if_endpoints = $h->{PROPERTIES}->{endpoints};
953 my($filename) = shift;
955 open(OUT, ">$filename") || die "can't open $filename";
957 pidl "/* parser auto-generated by pidl */\n\n";
959 foreach my $x (@{$idl}) {
960 if ($x->{TYPE} eq "INTERFACE") {
961 foreach my $d (@{$x->{DATA}}) {
962 if ($d->{TYPE} eq "TYPEDEF" and
963 util::has_property($d->{DATA}, "public")) {
965 if ($d->{DATA}{TYPE} eq "STRUCT") {
966 pidl "void ndr_pull_$d->{NAME}(struct e_ndr_pull *ndr, proto_tree *tree, int ndr_flags);\n";
969 if ($d->{DATA}{TYPE} eq "UNION") {
970 pidl "void ndr_pull_$d->{NAME}(struct e_ndr_pull *ndr, proto_tree *tree, int ndr_flags, int level);\n";
980 #####################################################################
981 # parse a parsed IDL structure back into an IDL file
985 my($filename) = shift;
989 open(OUT, ">$filename") || die "can't open $filename";
991 foreach my $x (@{$idl}) {
992 ($x->{TYPE} eq "MODULEHEADER") &&
995 if ($x->{TYPE} eq "INTERFACE") {
996 $module = $x->{NAME};
1001 pidl "/* parser auto-generated by pidl */\n\n";
1002 pidl "#ifdef HAVE_CONFIG_H\n";
1003 pidl "#include \"config.h\"\n";
1006 pidl "#include \"packet-dcerpc.h\"\n";
1007 pidl "#include \"packet-dcerpc-nt.h\"\n\n";
1008 pidl "#include \"packet-dcerpc-eparser.h\"\n\n";
1010 pidl "extern const value_string NT_errors[];\n\n";
1012 pidl "static int proto_dcerpc_$module = -1;\n\n";
1014 pidl "static gint ett_dcerpc_$module = -1;\n\n";
1016 pidl "static int hf_opnum = -1;\n";
1017 pidl "static int hf_rc = -1;\n";
1018 pidl "static int hf_ptr = -1;\n";
1019 pidl "static int hf_array_size = -1;\n";
1020 pidl "static int hf_array_offset = -1;\n";
1021 pidl "static int hf_array_length = -1;\n";
1022 pidl "static int hf_conformant_size = -1;\n";
1023 pidl "static int hf_level = -1;\n";
1025 # Declarations for hf variables
1027 foreach my $y (keys(%needed)) {
1028 pidl "static int $y = -1;\n", if $y =~ /^hf_/;
1033 for my $x (@{$idl}) {
1037 # Only perform module initialisation if we found a uuid
1039 if (defined($if_uuid)) {
1041 pidl "static e_uuid_t uuid_dcerpc_$module = {\n";
1042 pidl "\t0x" . substr($if_uuid, 0, 8);
1043 pidl ", 0x" . substr($if_uuid, 9, 4);
1044 pidl ", 0x" . substr($if_uuid, 14, 4) . ",\n";
1045 pidl "\t{ 0x" . substr($if_uuid, 19, 2);
1046 pidl ", 0x" . substr($if_uuid, 21, 2);
1047 pidl ", 0x" . substr($if_uuid, 24, 2);
1048 pidl ", 0x" . substr($if_uuid, 26, 2);
1049 pidl ", 0x" . substr($if_uuid, 28, 2);
1050 pidl ", 0x" . substr($if_uuid, 30, 2);
1051 pidl ", 0x" . substr($if_uuid, 32, 2);
1052 pidl ", 0x" . substr($if_uuid, 34, 2) . " }\n";
1055 pidl "static guint16 ver_dcerpc_$module = " . $if_version . ";\n\n";
1058 pidl "void proto_register_dcerpc_$module(void)\n";
1061 pidl "\tstatic hf_register_info hf[] = {\n";
1063 pidl "\t{ &hf_opnum, { \"Operation\", \"$module.opnum\", FT_UINT16, BASE_DEC, NULL, 0x0, \"Operation\", HFILL }},\n";
1064 pidl "\t{ &hf_rc, { \"Return code\", \"$module.rc\", FT_UINT32, BASE_HEX, VALS(NT_errors), 0x0, \"Return status code\", HFILL }},\n";
1065 pidl "\t{ &hf_array_size, { \"Array size\", \"$module.array_size\", FT_UINT32, BASE_DEC, NULL, 0x0, \"Array size\", HFILL }},\n";
1066 pidl "\t{ &hf_array_offset, { \"Array offset\", \"$module.array_offset\", FT_UINT32, BASE_DEC, NULL, 0x0, \"Array offset\", HFILL }},\n";
1067 pidl "\t{ &hf_array_length, { \"Array length\", \"$module.array_length\", FT_UINT32, BASE_DEC, NULL, 0x0, \"Array length\", HFILL }},\n";
1068 pidl "\t{ &hf_conformant_size, { \"Conformant size\", \"$module.conformant_size\", FT_UINT32, BASE_DEC, NULL, 0x0, \"Conformant size\", HFILL }},\n";
1069 pidl "\t{ &hf_level, { \"Level\", \"$module.level\", FT_UINT32, BASE_DEC, NULL, 0x0, \"Level\", HFILL }},\n";
1070 pidl "\t{ &hf_ptr, { \"Pointer\", \"$module.ptr\", FT_UINT32, BASE_HEX, NULL, 0x0, \"Pointer\", HFILL }},\n";
1072 foreach my $x (keys(%needed)) {
1073 next, if !($x =~ /^hf_/);
1075 pidl "\t { \"$needed{$x}{name}\", \"$x\", $needed{$x}{ft}, $needed{$x}{base}, NULL, 0, \"$x\", HFILL }},\n";
1080 pidl "\tstatic gint *ett[] = {\n";
1081 pidl "\t\t&ett_dcerpc_$module,\n";
1082 foreach my $x (keys(%needed)) {
1083 pidl "\t\t&$x,\n", if $x =~ /^ett_/;
1087 if (defined($if_uuid)) {
1089 pidl "\tproto_dcerpc_$module = proto_register_protocol(\"$module\", \"$module\", \"$module\");\n\n";
1091 pidl "\tproto_register_field_array(proto_dcerpc_$module, hf, array_length (hf));\n";
1092 pidl "\tproto_register_subtree_array(ett, array_length(ett));\n";
1096 pidl "void proto_reg_handoff_dcerpc_$module(void)\n";
1098 pidl "\tdcerpc_init_uuid(proto_dcerpc_$module, ett_dcerpc_$module, \n";
1099 pidl "\t\t&uuid_dcerpc_$module, ver_dcerpc_$module, \n";
1100 pidl "\t\tdcerpc_dissectors, hf_opnum);\n";
1105 pidl "\tint proto_dcerpc;\n\n";
1106 pidl "\tproto_dcerpc = proto_get_id_by_filter_name(\"dcerpc\");\n";
1107 pidl "\tproto_register_field_array(proto_dcerpc, hf, array_length(hf));\n";
1108 pidl "\tproto_register_subtree_array(ett, array_length(ett));\n";