r4770: Change from processing ndr_*.[ch] files all at once to line-by-line.
[ira/wip.git] / source / build / pidl / eparser.pm
index 5117f4349b617a65bd2220b6d6e9ecaa5745e2ca..92467a62113a27d5d69405df58648eeab71be921 100644 (file)
@@ -1,20 +1,17 @@
 ###################################################
 # Samba4 parser generator for IDL structures
 # Copyright tridge@samba.org 2000-2003
-# Copyright tpot@samba.org 2001
+# Copyright tpot@samba.org 2001,2004
 # released under the GNU GPL
 
 package IdlEParser;
 
 use strict;
-use client;
-use Data::Dumper;
 
 # the list of needed functions
 my %needed;
-my %structs;
 
-my $module = "samr";
+my $module;
 my $if_uuid;
 my $if_version;
 my $if_endpoints;
@@ -24,102 +21,13 @@ sub pidl($)
        print OUT shift;
 }
 
-#####################################################################
-# parse a properties list
-sub ParseProperties($)
-{
-    my($props) = shift;
-    foreach my $d (@{$props}) {
-       if (ref($d) ne "HASH") {
-           pidl "[$d] ";
-       } else {
-           foreach my $k (keys %{$d}) {
-               pidl "[$k($d->{$k})] ";
-           }
-       }
-    }
-}
-
-###################################
-# find a sibling var in a structure
-sub find_sibling($$)
-{
-       my($e) = shift;
-       my($name) = shift;
-       my($fn) = $e->{PARENT};
-
-       if ($name =~ /\*(.*)/) {
-               $name = $1;
-       }
-
-       if ($fn->{TYPE} eq "FUNCTION") {
-               for my $e2 (@{$fn->{DATA}}) {
-                       if ($e2->{NAME} eq $name) {
-                               return $e2;
-                       }
-               }
-       }
-
-       for my $e2 (@{$fn->{ELEMENTS}}) {
-               if ($e2->{NAME} eq $name) {
-                       return $e2;
-               }
-       }
-       die "invalid sibling '$name'";
-}
-
-####################################################################
-# work out the name of a size_is() variable
-sub find_size_var($$$)
-{
-       my($e) = shift;
-       my($size) = shift;
-       my($var_prefix) = shift;
-
-       my($fn) = $e->{PARENT};
-
-       if (util::is_constant($size)) {
-               return $size;
-       }
-
-       if ($size =~ /ndr->|\(/) {
-               return $size;
-       }
-
-       my $prefix = "";
-
-       if ($size =~ /\*(.*)/) {
-               $size = $1;
-               $prefix = "*";
-       }
-
-       if ($fn->{TYPE} ne "FUNCTION") {
-               return $prefix . "r->$size";
-       }
-
-       my $e2 = find_sibling($e, $size);
-
-       if (util::has_property($e2, "in") && util::has_property($e2, "out")) {
-               return $prefix . "$var_prefix$size";
-       }
-       if (util::has_property($e2, "in")) {
-               return $prefix . "r->in.$size";
-       }
-       if (util::has_property($e2, "out")) {
-               return $prefix . "r->out.$size";
-       }
-
-       die "invalid variable in $size for element $e->{NAME} in $fn->{NAME}\n";
-}
-
-
 #####################################################################
 # work out is a parse function should be declared static or not
 sub fn_prefix($)
 {
        my $fn = shift;
        if ($fn->{TYPE} eq "TYPEDEF") {
-               if (util::has_property($fn->{DATA}, "public")) {
+               if (util::has_property($fn, "public")) {
                        return "";
                }
        }
@@ -133,1226 +41,631 @@ sub fn_prefix($)
 }
 
 
-###################################################################
-# setup any special flags for an element or structure
-sub start_flags($)
-{
-       my $e = shift;
-       my $flags = util::has_property($e, "flag");
-       if (defined $flags) {
-               pidl "\t{ guint32 _flags_save_$e->{TYPE} = flags;\n";
-               pidl "\tflags |= $flags;\n";
-       }
-}
-
-###################################################################
-# end any special flags for an element or structure
-sub end_flags($)
-{
-       my $e = shift;
-       my $flags = util::has_property($e, "flag");
-       if (defined $flags) {
-               pidl "\tflags = _flags_save_$e->{TYPE};\n\t}\n";
-       }
-}
-
-
 #####################################################################
-# work out the correct alignment for a structure
-sub struct_alignment
-{
-       my $s = shift;
-
-       my $align = 1;
-       for my $e (@{$s->{ELEMENTS}}) {
-               my $a = 1;
-
-               if (!util::need_wire_pointer($e)
-                   && defined $structs{$e->{TYPE}}) {
-                       if ($structs{$e->{TYPE}}->{DATA}->{TYPE} eq "STRUCT") {
-                               $a = struct_alignment($structs{$e->{TYPE}}->{DATA});
-                       } elsif ($structs{$e->{TYPE}}->{DATA}->{TYPE} eq "UNION") {
-                               if (defined $structs{$e->{TYPE}}->{DATA}) {
-                                       $a = union_alignment($structs{$e->{TYPE}}->{DATA});
-                               }
-                       }
-               } else {
-                       $a = util::type_align($e);
-               }
-
-               if ($align < $a) {
-                       $align = $a;
-               }
-       }
-
-       return $align;
-}
-
-#####################################################################
-# work out the correct alignment for a union
-sub union_alignment
-{
-       my $u = shift;
-
-       my $align = 1;
+# parse a function
+sub ParseFunctionPull($)
+{ 
+       my($fn) = shift;
+       my $static = fn_prefix($fn);
 
-       foreach my $e (@{$u->{DATA}}) {
-               my $a = 1;
+       # request function
+       pidl "int $fn->{NAME}_rqst(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep)\n{\n";
 
-               if ($e->{TYPE} eq "EMPTY") {
-                       next;
-               }
+       pidl "\tstruct pidl_pull *ndr = pidl_pull_init(tvb, offset, pinfo, drep);\n";
+       pidl "\tstruct $fn->{NAME} *r = talloc_p(NULL, struct $fn->{NAME});\n";
+       pidl "\tpidl_tree ptree;\n\n";
 
-               if (!util::need_wire_pointer($e)
-                   && defined $structs{$e->{DATA}->{TYPE}}) {
-                       my $s = $structs{$e->{DATA}->{TYPE}};
-                       if ($s->{DATA}->{TYPE} eq "STRUCT") {
-                               $a = struct_alignment($s->{DATA});
-                       } elsif ($s->{DATA}->{TYPE} eq "UNION") {
-                               $a = union_alignment($s->{DATA});
-                       }
-               } else {
-                       $a = util::type_align($e->{DATA});
-               }
+       pidl "\tptree.proto_tree = tree;\n";
+       pidl "\tptree.subtree_list = NULL;\n\n";
 
-               if ($align < $a) {
-                       $align = $a;
-               }
-       }
+       pidl "\tndr_pull_$fn->{NAME}(ndr, NDR_IN, &ptree, r);\n";
 
-       return $align;
-}
+       pidl "\n\treturn ndr->offset;\n";
+       pidl "}\n\n";
 
-#####################################################################
-# parse an array - push side
-sub ParseArrayPush($$$)
-{
-       my $e = shift;
-       my $var_prefix = shift;
-       my $ndr_flags = shift;
+       # response function
+       pidl "int $fn->{NAME}_resp(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep)\n{\n";
 
-       my $size = find_size_var($e, util::array_size($e), $var_prefix);
+       pidl "\tstruct pidl_pull *ndr = pidl_pull_init(tvb, offset, pinfo, drep);\n";
+       pidl "\tstruct $fn->{NAME} *r = talloc_p(NULL, struct $fn->{NAME});\n";
+       pidl "\tpidl_tree ptree;\n\n";
 
-       if (defined $e->{CONFORMANT_SIZE}) {
-               # the conformant size has already been pushed
-       } elsif (!util::is_inline_array($e)) {
-               # we need to emit the array size
-               pidl "\t\toffset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, hf_array_size, NULL);\n";
-       }
+       pidl "\tptree.proto_tree = tree;\n";
+       pidl "\tptree.subtree_list = NULL;\n\n";
 
-       if (my $length = util::has_property($e, "length_is")) {
-               $length = find_size_var($e, $length, $var_prefix);
-               pidl "\t\toffset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, hf_length_is, NULL);\n";
-               pidl "\t\toffset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, hf_length_is, NULL);\n";
-               $size = $length;
-       }
+       pidl "\tndr_pull_$fn->{NAME}(ndr, NDR_OUT, &ptree, r);\n";
 
-       if (util::is_scalar_type($e->{TYPE})) {
-               pidl "\t\t// ndr_push_array_$e->{TYPE}(ndr, $ndr_flags, $var_prefix$e->{NAME}, $size);\n";
-       } else {
-               pidl "\t\t// ndr_push_array(ndr, $ndr_flags, $var_prefix$e->{NAME}, sizeof($var_prefix$e->{NAME}\[0]), $size, (ndr_push_flags_fn_t)ndr_push_$e->{TYPE});\n";
-       }
+       pidl "\n\treturn ndr->offset;\n";
+       pidl "}\n\n";
 }
 
 #####################################################################
-# parse an array - pull side
-sub ParseArrayPull($$$)
+# produce a function call table
+sub FunctionTable($)
 {
-       my $e = shift;
-       my $var_prefix = shift;
-       my $ndr_flags = shift;
-
-       my $size = find_size_var($e, util::array_size($e), $var_prefix);
-       my $alloc_size = $size;
-
-       # if this is a conformant array then we use that size to allocate, and make sure
-       # we allocate enough to pull the elements
-       if (defined $e->{CONFORMANT_SIZE}) {
-               $alloc_size = $e->{CONFORMANT_SIZE};
-
-               pidl "\tif ($size > $alloc_size) {\n";
-               pidl "\t\treturn ndr_pull_error(ndr, NDR_ERR_CONFORMANT_SIZE, \"Bad conformant size %u should be %u\", $alloc_size, $size);\n";
-               pidl "\t}\n";
-       } elsif (!util::is_inline_array($e)) {
-               if ($var_prefix =~ /^r->out/ && $size =~ /^\*r->in/) {
-                       my $size2 = substr($size, 1);
-                       pidl "if (flags & LIBNDR_FLAG_REF_ALLOC) {      NDR_ALLOC(ndr, $size2); }\n";
-               }
-
-               # non fixed arrays encode the size just before the array
-               pidl "\t{\n";
-               pidl "\t\tuint32_t _array_size;\n";
-               pidl "\t\t(ndr_pull_uint32(ndr, &_array_size);\n";
-               if ($size =~ /r->in/) {
-                       pidl "\t\tif (!(flags & LIBNDR_FLAG_REF_ALLOC) && _array_size != $size) {\n";
-               } else {
-                       pidl "\t\tif ($size != _array_size) {\n";
-               }
-               pidl "\t\t\treturn ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, \"Bad array size %u should be %u\", _array_size, $size);\n";
-               pidl "\t\t}\n";
-               if ($size =~ /r->in/) {
-                       pidl "else { $size = _array_size; }\n";
-               }
-               pidl "\t}\n";
-       }
-
-       if ((util::need_alloc($e) && !util::is_fixed_array($e)) ||
-           ($var_prefix eq "r->in." && util::has_property($e, "ref"))) {
-               if (!util::is_inline_array($e) || $ndr_flags eq "NDR_SCALARS") {
-                       pidl "\t\tNDR_ALLOC_N(ndr, $var_prefix$e->{NAME}, MAX(1, $alloc_size));\n";
-               }
-       }
+       my($interface) = shift;
+       my($data) = $interface->{DATA};
 
-       if (($var_prefix eq "r->out." && util::has_property($e, "ref"))) {
-               if (!util::is_inline_array($e) || $ndr_flags eq "NDR_SCALARS") {
-                       pidl "\tif (flags & LIBNDR_FLAG_REF_ALLOC) {";
-                       pidl "\t\tNDR_ALLOC_N(ndr, $var_prefix$e->{NAME}, MAX(1, $alloc_size));\n";
-                       pidl "\t}\n";
+       pidl "static dcerpc_sub_dissector dcerpc_dissectors[] = {\n";
+       my $num = 0;
+       foreach my $d (@{$data}) {
+               if ($d->{TYPE} eq "FUNCTION") {
+                   # Strip module name from function name, if present
+                   my($n) = $d->{NAME};
+                   $n = substr($d->{NAME}, length($module) + 1),
+                       if $module eq substr($d->{NAME}, 0, length($module));
+                   pidl "\t{ $num, \"$n\",\n";
+                   pidl "\t\t$d->{NAME}_rqst,\n";
+                   pidl "\t\t$d->{NAME}_resp },\n";
+                   $num++;
                }
        }
-
-       pidl "\t{\n";
-
-       if (my $length = util::has_property($e, "length_is")) {
-               $length = find_size_var($e, $length, $var_prefix);
-               pidl "\t\tuint32_t _offset, _length;\n";
-               pidl "\t\tndr_pull_uint32(ndr, &_offset);\n";
-               pidl "\t\tndr_pull_uint32(ndr, &_length);\n";
-               pidl "\t\tif (_offset != 0) return ndr_pull_error(ndr, NDR_ERR_OFFSET, \"Bad array offset 0x%08x\", _offset);\n";
-               pidl "\t\tif (_length > $size || _length != $length) return ndr_pull_error(ndr, NDR_ERR_LENGTH, \"Bad array length 0x%08x > size 0x%08x\", _offset, $size);\n\n";
-               $size = "_length";
-       }
-
-       if (util::is_scalar_type($e->{TYPE})) {
-               pidl "\t\tndr_pull_array_$e->{TYPE}(ndr, $ndr_flags, $var_prefix$e->{NAME}, $size);\n";
-       } else {
-               pidl "\t\tndr_pull_array(ndr, $ndr_flags, (void **)$var_prefix$e->{NAME}, sizeof($var_prefix$e->{NAME}\[0]), $size, (ndr_pull_flags_fn_t)ndr_pull_$e->{TYPE});\n";
-       }
-
-       pidl "\t}\n";
-}
-sub ParamPolicyHandle($)
-{
-    my($p) = shift;
-    my($res);
-
-    $res .= "\toffset = dissect_nt_policy_hnd(tvb, offset, pinfo, tree, drep, hf_policy_handle, NULL, NULL, FALSE, FALSE);\n";
-
-    return $res;
-}
-
-
-sub ParamString($)
-{
-    my($p) = shift;
-    my($res);
-
-    $res .= "\toffset = dissect_ndr_pointer_cb(tvb, offset, pinfo, tree, drep, dissect_ndr_wchar_cvstring, NDR_POINTER_UNIQUE, \"$p->{NAME}\", hf_$p->{NAME}_string, cb_wstr_postprocess, GINT_TO_POINTER(1));\n";
-
-    return $res;
+       pidl "};\n\n";
 }
 
-sub ParamDomSID($)
+sub type2ft($)
 {
-    my($p) = shift;
-    my($res);
-
-    $res .= "\toffset = dissect_ndr_nt_SID(tvb, offset, pinfo, tree, drep);\n";
+    my($t) = shift;
+    return "FT_UINT$1" if $t =~ /uint(8|16|32|64)/;
+    return "FT_INT$1" if $t =~ /int(8|16|32|64)/;
+    return "FT_UINT64", if ($t eq "HYPER_T" or $t eq "NTTIME");
+    
+    # Type is an enum
 
-    return $res;
+    return "FT_UINT16";
 }
 
-my %param_handlers = (
-                     'policy_handle' => \&ParamPolicyHandle,
-                     'string' => \&ParamString,
-                     'dom_sid2'      => \&ParamDomSID,
-                     );
+# Determine the display base for an element
 
-####################################################################
-# parse scalars in a structure element
-sub ParseElementPushScalar($$$)
+sub elementbase($)
 {
-       my($e) = shift;
-       my($var_prefix) = shift;
-       my($ndr_flags) = shift;
-       my $cprefix = util::c_push_prefix($e);
-       my $sub_size = util::has_property($e, "subcontext");
+    my($e) = shift;
 
-       start_flags($e);
-
-       if (my $value = util::has_property($e, "value")) {
-               pidl "\t$cprefix$var_prefix$e->{NAME} = $value;\n";
-       }
+    if (my $base = util::has_property($e, "display")) {
+       return "BASE_" . uc($base);
+    }
+    return "BASE_DEC", if $e->{TYPE} eq "ENUM";
+    return "BASE_DEC", if $e->{TYPE} =~ /u?int(8|16|32|64)/;
+    return "BASE_DEC", if $e->{TYPE} eq "NTTIME" or $e->{TYPE} eq "HYPER_T";
 
-       if (util::has_property($e, "relative")) {
-               pidl "\tndr_push_relative(ndr, NDR_SCALARS, $var_prefix$e->{NAME}, (ndr_push_const_fn_t) ndr_push_$e->{TYPE});\n";
-       } elsif (util::is_inline_array($e)) {
-               ParseArrayPush($e, "r->", "NDR_SCALARS");
-       } elsif (util::need_wire_pointer($e)) {
-           pidl "\toffset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, hf_ptr, &ptr);\n";
-       } elsif (util::need_alloc($e)) {
-               # no scalar component
-       } elsif (my $switch = util::has_property($e, "switch_is")) {
-               ParseElementPushSwitch($e, $var_prefix, $ndr_flags, $switch);
-       } elsif (defined $sub_size) {
-               if (util::is_builtin_type($e->{TYPE})) {
-                       pidl "\tndr_push_subcontext_fn(ndr, $sub_size, $cprefix$var_prefix$e->{NAME}, (ndr_push_fn_t) ndr_push_$e->{TYPE});\n";
-               } else {
-                       pidl "\tndr_push_subcontext_flags_fn(ndr, $sub_size, $cprefix$var_prefix$e->{NAME}, (ndr_push_flags_fn_t) ndr_push_$e->{TYPE});\n";
-               }
-       } elsif (util::is_builtin_type($e->{TYPE})) {
-               pidl "\toffset = dissect_ndr_$e->{TYPE}(tvb, offset, pinfo, tree, drep, hf_$e->{NAME}_$e->{TYPE}, NULL);\n";
-       } else {
-           if (defined($param_handlers{$e->{TYPE}})) {
-               pidl &{$param_handlers{$e->{TYPE}}}($e);
-           } else {
-               pidl "\tproto_tree_add_text(tree, tvb, offset, -1, \"Unhandled IDL type '$e->{TYPE}'\");\n";
-           }
-       }
+    # Probably an enum
 
-       end_flags($e);
+    return "BASE_DEC";
 }
 
-#####################################################################
-# print scalars in a structure element
-sub ParseElementPrintScalar($$)
-{
-       my($e) = shift;
-       my($var_prefix) = shift;
-       my $cprefix = util::c_push_prefix($e);
+# Convert a IDL structure field name (e.g access_mask) to a prettier
+# string like 'Access Mask'.
 
-       if (util::has_property($e, "noprint")) {
-               return;
-       }
-
-       if (my $value = util::has_property($e, "value")) {
-               pidl "\tif (flags & LIBNDR_PRINT_SET_VALUES) {\n";
-               pidl "\t\t$cprefix$var_prefix$e->{NAME} = $value;\n";
-               pidl "\t}\n";
-       }
+sub field2name($)
+{
+    my($field) = shift;
 
-       if (util::is_fixed_array($e)) {
-               ParseElementPrintBuffer($e, $var_prefix);
-       } elsif (util::has_direct_buffers($e)) {
-               pidl "\tndr_print_ptr(ndr, \"$e->{NAME}\", $var_prefix$e->{NAME});\n";
-               pidl "\tndr->depth++;\n";
-               ParseElementPrintBuffer($e, $var_prefix);
-               pidl "\tndr->depth--;\n";
-       } elsif (my $switch = util::has_property($e, "switch_is")) {
-               ParseElementPrintSwitch($e, $var_prefix, $switch);
-       } else {
-               pidl "\tndr_print_$e->{TYPE}(ndr, \"$e->{NAME}\", $cprefix$var_prefix$e->{NAME});\n";
-       }
+    $field =~ s/_/ /g;         # Replace underscores with spaces
+    $field =~ s/(\w+)/\u\L$1/g;        # Capitalise each word
+    
+    return $field;
 }
 
-#####################################################################
-# parse scalars in a structure element - pull size
-sub ParseElementPullSwitch($$$$)
+sub NeededFunction($)
 {
-       my($e) = shift;
-       my($var_prefix) = shift;
-       my($ndr_flags) = shift;
-       my $switch = shift;
-       my $switch_var = find_size_var($e, $switch, $var_prefix);
-
-       my $cprefix = util::c_pull_prefix($e);
-
-       my $utype = $structs{$e->{TYPE}};
-       if (!defined $utype ||
-           !util::has_property($utype->{DATA}, "nodiscriminant")) {
-               my $e2 = find_sibling($e, $switch);
-               pidl "\tif (($ndr_flags) & NDR_SCALARS) {\n";
-               pidl "\t\t $e2->{TYPE} _level;\n";
-               pidl "\t\tndr_pull_$e2->{TYPE}(ndr, &_level);\n";
-               if ($switch_var =~ /r->in/) {
-                       pidl "\t\tif (!(flags & LIBNDR_FLAG_REF_ALLOC) && _level != $switch_var) {\n";
+       my $fn = shift;
+       $needed{"pull_$fn->{NAME}"} = 1;
+       foreach my $e (@{$fn->{DATA}}) {
+               $e->{PARENT} = $fn;
+               $needed{"pull_$e->{TYPE}"} = 1;
+
+               if (util::is_scalar_type($e->{TYPE})) {
+                   $needed{"hf_$e->{NAME}_$e->{TYPE}"} = {
+                       'name' => field2name($e->{NAME}),
+                       'type' => $e->{TYPE},
+                       'ft'   => type2ft($e->{TYPE}),
+                       'base' => elementbase($e)
+                       }, if !defined($needed{"hf_$e->{NAME}_$e->{TYPE}"});
+                   $e->{PARENT} = $fn;
                } else {
-                       pidl "\t\tif (_level != $switch_var) {\n";
+                   $needed{"ett_$e->{TYPE}"} = 1;
                }
-               pidl "\t\t\treturn ndr_pull_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value %u in $e->{NAME}\");\t\t}\n";
-               if ($switch_var =~ /r->/) {
-                       pidl "else { $switch_var = _level; }\n";
-               }
-               pidl "\t}\n";
-       }
-
-       my $sub_size = util::has_property($e, "subcontext");
-       if (defined $sub_size) {
-               pidl "\tndr_pull_subcontext_union_fn(ndr, $sub_size, $switch_var, $cprefix$var_prefix$e->{NAME}, (ndr_pull_union_fn_t) ndr_pull_$e->{TYPE});\n";
-       } else {
-               pidl "\tndr_pull_$e->{TYPE}(ndr, $ndr_flags, $switch_var, $cprefix$var_prefix$e->{NAME});\n";
        }
-
-
 }
 
-#####################################################################
-# push switch element
-sub ParseElementPushSwitch($$$$)
+sub NeededTypedef($)
 {
-       my($e) = shift;
-       my($var_prefix) = shift;
-       my($ndr_flags) = shift;
-       my $switch = shift;
-       my $switch_var = find_size_var($e, $switch, $var_prefix);
-       my $cprefix = util::c_push_prefix($e);
-
-       my $utype = $structs{$e->{TYPE}};
-       if (!defined $utype ||
-           !util::has_property($utype->{DATA}, "nodiscriminant")) {
-               my $e2 = find_sibling($e, $switch);
-               pidl "\tif (($ndr_flags) & NDR_SCALARS) {\n";
-               pidl "\t\toffset = dissect_ndr_$e2->{TYPE}(tvb, offset, pinfo, tree, drep, hf_switch, NULL);\n";
-               pidl "\t}\n";
-       }
-
-       my $sub_size = util::has_property($e, "subcontext");
-       if (defined $sub_size) {
-               pidl "\tndr_push_subcontext_union_fn(ndr, $sub_size, $switch_var, $cprefix$var_prefix$e->{NAME}, (ndr_push_union_fn_t) ndr_push_$e->{TYPE});\n";
-       } else {
-           if (defined($param_handlers{$e->{TYPE}})) {
-               pidl &{$param_handlers{$e->{TYPE}}}($e);
-           } else {
-               pidl "\tproto_tree_add_text(tree, tvb, offset, -1, \"Unhandled IDL type '$e->{TYPE}'\");\n";
-           }
-#              pidl "\tndr_push_$e->{TYPE}(ndr, $ndr_flags, $switch_var, $cprefix$var_prefix$e->{NAME});\n";
+       my $t = shift;
+       if (util::has_property($t, "public")) {
+               $needed{"pull_$t->{NAME}"} = 1;
        }
-}
-
-#####################################################################
-# print scalars in a structure element 
-sub ParseElementPrintSwitch($$$)
-{
-       my($e) = shift;
-       my($var_prefix) = shift;
-       my $switch = shift;
-       my $switch_var = find_size_var($e, $switch, $var_prefix);
-       my $cprefix = util::c_push_prefix($e);
 
-       pidl "\tndr_print_$e->{TYPE}(ndr, \"$e->{NAME}\", $switch_var, $cprefix$var_prefix$e->{NAME});\n";
-}
+       if ($t->{DATA}->{TYPE} eq "STRUCT") {
 
+           for my $e (@{$t->{DATA}->{ELEMENTS}}) {
+               $e->{PARENT} = $t->{DATA};
+               if ($needed{"pull_$t->{NAME}"}) {
+                   $needed{"pull_$e->{TYPE}"} = 1;
+               }
+           
+               if (util::is_scalar_type($e->{TYPE})) {
+               
+                   if (defined($e->{ARRAY_LEN}) or 
+                       util::has_property($e, "size_is")) {
+
+                       # Arrays of scalar types are FT_BYTES
+                   
+                       $needed{"hf_$e->{NAME}_$e->{TYPE}_array"} = {
+                           'name' => field2name($e->{NAME}),
+                           'type' => $e->{TYPE},
+                           'ft'   => "FT_BYTES",
+                           'base' => elementbase($e)
+                           };
+
+                   } else {
+                       $needed{"hf_$e->{NAME}_$e->{TYPE}"} = {
+                           'name' => field2name($e->{NAME}),
+                           'type' => $e->{TYPE},
+                           'ft'   => type2ft($e->{TYPE}),
+                           'base' => elementbase($e)
+                           };
+                   }
+                   
+                   $e->{PARENT} = $t->{DATA};
+                   
+                   if ($needed{"pull_$t->{NAME}"}) {
+                       $needed{"pull_$e->{TYPE}"} = 1;
+                   }
 
-#####################################################################
-# parse scalars in a structure element - pull size
-sub ParseElementPullScalar($$$)
-{
-       my($e) = shift;
-       my($var_prefix) = shift;
-       my($ndr_flags) = shift;
-       my $cprefix = util::c_pull_prefix($e);
-       my $sub_size = util::has_property($e, "subcontext");
-
-       start_flags($e);
-
-       if (util::has_property($e, "relative")) {
-               pidl "\tndr_pull_relative(ndr, (const void **)&$var_prefix$e->{NAME}, sizeof(*$var_prefix$e->{NAME}), (ndr_pull_flags_fn_t)ndr_pull_$e->{TYPE});\n";
-       } elsif (util::is_inline_array($e)) {
-               ParseArrayPull($e, "r->", "NDR_SCALARS");
-       } elsif (util::need_wire_pointer($e)) {
-               pidl "\tndr_pull_uint32(ndr, &_ptr_$e->{NAME});\n";
-               pidl "\tif (_ptr_$e->{NAME}) {\n";
-               pidl "\t\tNDR_ALLOC(ndr, $var_prefix$e->{NAME});\n";
-               pidl "\t} else {\n";
-               pidl "\t\t$var_prefix$e->{NAME} = NULL;\n";
-               pidl "\t}\n";
-       } elsif (util::need_alloc($e)) {
-               # no scalar component
-       } elsif (my $switch = util::has_property($e, "switch_is")) {
-               ParseElementPullSwitch($e, $var_prefix, $ndr_flags, $switch);
-       } elsif (defined $sub_size) {
-               if (util::is_builtin_type($e->{TYPE})) {
-                       pidl "\tndr_pull_subcontext_fn(ndr, $sub_size, $cprefix$var_prefix$e->{NAME}, (ndr_pull_fn_t) ndr_pull_$e->{TYPE});\n";
                } else {
-                       pidl "\tndr_pull_subcontext_flags_fn(ndr, $sub_size, $cprefix$var_prefix$e->{NAME}, (ndr_pull_flags_fn_t) ndr_pull_$e->{TYPE});\n";
+                   
+                   $needed{"ett_$e->{TYPE}"} = 1;
+                   
                }
-       } elsif (util::is_builtin_type($e->{TYPE})) {
-               pidl "\tndr_pull_$e->{TYPE}(ndr, $cprefix$var_prefix$e->{NAME});\n";
-       } else {
-               pidl "\tndr_pull_$e->{TYPE}(ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME});\n";
-       }
-
-       end_flags($e);
-}
-
-#####################################################################
-# parse buffers in a structure element
-sub ParseElementPushBuffer($$$)
-{
-       my($e) = shift;
-       my($var_prefix) = shift;
-       my($ndr_flags) = shift;
-       my $cprefix = util::c_push_prefix($e);
-       my $sub_size = util::has_property($e, "subcontext");
-
-       if (util::is_pure_scalar($e)) {
-               return;
+           }
        }
 
-       start_flags($e);
-
-       if (util::need_wire_pointer($e)) {
-               pidl "\tif (ptr) {\n";
-       }
-           
-       if (util::has_property($e, "relative")) {
-               pidl "\tndr_push_relative(ndr, NDR_BUFFERS, $cprefix$var_prefix$e->{NAME}, (ndr_push_const_fn_t) ndr_push_$e->{TYPE});\n";
-       } elsif (util::is_inline_array($e)) {
-               ParseArrayPush($e, "r->", "NDR_BUFFERS");
-       } elsif (util::array_size($e)) {
-               ParseArrayPush($e, "r->", "NDR_SCALARS|NDR_BUFFERS");
-       } elsif (my $switch = util::has_property($e, "switch_is")) {
-               if ($e->{POINTERS}) {
-                       ParseElementPushSwitch($e, $var_prefix, "NDR_BUFFERS|NDR_SCALARS", $switch);
-               } else {
-                       ParseElementPushSwitch($e, $var_prefix, "NDR_BUFFERS", $switch);
-               }
-       } elsif (defined $sub_size) {
-               if ($e->{POINTERS}) {
-                       if (util::is_builtin_type($e->{TYPE})) {
-                               pidl "\tndr_push_subcontext_fn(ndr, $sub_size, $cprefix$var_prefix$e->{NAME}, (ndr_push_fn_t) ndr_push_$e->{TYPE});\n";
-                       } else {
-                               pidl "\tndr_push_subcontext_flags_fn(ndr, $sub_size, $cprefix$var_prefix$e->{NAME}, (ndr_push_flags_fn_t) ndr_push_$e->{TYPE});\n";
+       if ($t->{DATA}->{TYPE} eq "UNION") {
+               for my $e (@{$t->{DATA}->{DATA}}) {
+                       $e->{PARENT} = $t->{DATA};
+                       if ($e->{TYPE} eq "UNION_ELEMENT") {
+                               if ($needed{"pull_$t->{NAME}"}) {
+                                       $needed{"pull_$e->{DATA}->{TYPE}"} = 1;
+                               }
+                               $needed{"ett_$e->{DATA}{TYPE}"} = 1;
                        }
                }
-       } elsif (util::is_builtin_type($e->{TYPE})) {
-               pidl "\toffset = dissect_ndr_$e->{TYPE}(tvb, offset, pinfo, tree, drep, hf_$e->{NAME}_$e->{TYPE}, NULL);\n";
-       } elsif ($e->{POINTERS}) {
-           if (defined($param_handlers{$e->{TYPE}})) {
-               pidl &{$param_handlers{$e->{TYPE}}}($e);
-           } else {
-               pidl "\t\toffset = dissect_$e->{TYPE}(tvb, offset, pinfo, tree, drep, NDR_SCALARS|NDR_BUFFERS);\n";
-           }
-       } else {
-           if (defined($param_handlers{$e->{TYPE}})) {
-               pidl &{$param_handlers{$e->{TYPE}}}($e);
-           } else {
-               pidl "\t\toffset = dissect__$e->{TYPE}(tvb, offset, pinfo, tree, drep, $ndr_flags);\n";
-           }
-       }
 
-       if (util::need_wire_pointer($e)) {
-               pidl "\t}\n";
-       }       
+           $needed{"ett_$t->{NAME}"} = 1;
+       }
 
-       end_flags($e);
+       if ($t->{DATA}->{TYPE} eq "ENUM") {
+           $needed{"hf_$t->{NAME}"} = {
+               'name' => $t->{NAME},
+               'ft' => 'FT_UINT16',
+               'base' => 'BASE_DEC'
+               };
+       }
 }
 
 #####################################################################
-# print buffers in a structure element
-sub ParseElementPrintBuffer($$)
+# work out what parse functions are needed
+sub BuildNeeded($)
 {
-       my($e) = shift;
-       my($var_prefix) = shift;
-       my $cprefix = util::c_push_prefix($e);
-
-       if (util::need_wire_pointer($e)) {
-               pidl "\tif ($var_prefix$e->{NAME}) {\n";
+       my($interface) = shift;
+       my($data) = $interface->{DATA};
+       foreach my $d (@{$data}) {
+               ($d->{TYPE} eq "FUNCTION") && 
+                   NeededFunction($d);
        }
-           
-       if (util::array_size($e)) {
-               ParseArrayPrint($e, $var_prefix);
-       } elsif (my $switch = util::has_property($e, "switch_is")) {
-               ParseElementPrintSwitch($e, $var_prefix, $switch);
-       } else {
-               pidl "\t\tndr_print_$e->{TYPE}(ndr, \"$e->{NAME}\", $cprefix$var_prefix$e->{NAME});\n";
+       foreach my $d (reverse @{$data}) {
+               ($d->{TYPE} eq "TYPEDEF") &&
+                   NeededTypedef($d);
        }
-
-       if (util::need_wire_pointer($e)) {
-               pidl "\t}\n";
-       }       
 }
 
-
 #####################################################################
-# parse buffers in a structure element - pull side
-sub ParseElementPullBuffer($$$)
+# parse the interface definitions
+sub ModuleHeader($)
 {
-       my($e) = shift;
-       my($var_prefix) = shift;
-       my($ndr_flags) = shift;
-       my $cprefix = util::c_pull_prefix($e);
-       my $sub_size = util::has_property($e, "subcontext");
-
-       if (util::is_pure_scalar($e)) {
-               return;
-       }
-
-       if (util::has_property($e, "relative")) {
-               return;
-       }
-
-       start_flags($e);
-
-       if (util::need_wire_pointer($e)) {
-               pidl "\tif ($var_prefix$e->{NAME}) {\n";
-       }
-           
-       if (util::is_inline_array($e)) {
-               ParseArrayPull($e, "r->", "NDR_BUFFERS");
-       } elsif (util::array_size($e)) {
-               ParseArrayPull($e, "r->", "NDR_SCALARS|NDR_BUFFERS");
-       } elsif (my $switch = util::has_property($e, "switch_is")) {
-               if ($e->{POINTERS}) {
-                       ParseElementPullSwitch($e, $var_prefix, "NDR_SCALARS|NDR_BUFFERS", $switch);
-               } else {
-                       ParseElementPullSwitch($e, $var_prefix, "NDR_BUFFERS", $switch);
-               }
-       } elsif (defined $sub_size) {
-               if ($e->{POINTERS}) {
-                       if (util::is_builtin_type($e->{TYPE})) {
-                               pidl "\tndr_pull_subcontext_fn(ndr, $sub_size, $cprefix$var_prefix$e->{NAME}, (ndr_pull_fn_t) ndr_pull_$e->{TYPE});\n";
-                       } else {
-                               pidl "\tndr_pull_subcontext_flags_fn(ndr, $sub_size, $cprefix$var_prefix$e->{NAME}, (ndr_pull_flags_fn_t) ndr_pull_$e->{TYPE});\n";
-                       }
-               }
-       } elsif (util::is_builtin_type($e->{TYPE})) {
-               pidl "\t\tndr_pull_$e->{TYPE}(ndr, $cprefix$var_prefix$e->{NAME});\n";
-       } elsif ($e->{POINTERS}) {
-               pidl "\t\tndr_pull_$e->{TYPE}(ndr, NDR_SCALARS|NDR_BUFFERS, $cprefix$var_prefix$e->{NAME});\n";
-       } else {
-               pidl "\t\tndr_pull_$e->{TYPE}(ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME});\n";
-       }
-
-       if (util::need_wire_pointer($e)) {
-               pidl "\t}\n";
-       }       
+    my($h) = shift;
 
-       end_flags($e);
+    $if_uuid = $h->{PROPERTIES}->{uuid};
+    $if_version = $h->{PROPERTIES}->{version};
+    $if_endpoints = $h->{PROPERTIES}->{endpoints};
 }
 
 #####################################################################
-# parse a struct
-sub ParseStructPush($)
+# Generate a header file that contains function prototypes for 
+# structs and typedefs.
+sub ParseHeader($$)
 {
-       my($struct) = shift;
-       my $conform_e;
-       
-       if (! defined $struct->{ELEMENTS}) {
-               return;
-       }
+       my($idl) = shift;
+       my($filename) = shift;
 
-       start_flags($struct);
-
-       # see if the structure contains a conformant array. If it
-       # does, then it must be the last element of the structure, and
-       # we need to push the conformant length early, as it fits on
-       # the wire before the structure (and even before the structure
-       # alignment)
-       my $e = $struct->{ELEMENTS}[-1];
-       if (defined $e->{ARRAY_LEN} && $e->{ARRAY_LEN} eq "*") {
-               my $size = find_size_var($e, util::array_size($e), "r->");
-               $e->{CONFORMANT_SIZE} = $size;
-               $conform_e = $e;
-               pidl "\tndr_push_uint32(ndr, $size);\n";
-       }
+       open(OUT, ">$filename") || die "can't open $filename";    
 
-       pidl "\tif (!(ndr_flags & NDR_SCALARS)) goto buffers;\n";
+       pidl "/* parser auto-generated by pidl */\n\n";
 
-       pidl "\tndr_push_struct_start(ndr);\n";
+       foreach my $x (@{$idl}) {
+           if ($x->{TYPE} eq "INTERFACE") { 
+               foreach my $d (@{$x->{DATA}}) {
 
-       my $align = struct_alignment($struct);
-       pidl "\tndr_push_align(ndr, $align);\n";
+                   # Make prototypes for [public] structures and
+                   # unions.
 
-       foreach my $e (@{$struct->{ELEMENTS}}) {
-               ParseElementPushScalar($e, "r->", "NDR_SCALARS");
-       }       
+                   if ($d->{TYPE} eq "TYPEDEF" and 
+                       util::has_property($d, "public")) {
+                       
+                       if ($d->{DATA}{TYPE} eq "STRUCT") { 
+                           pidl "void ndr_pull_$d->{NAME}(struct ndr_pull *ndr, int ndr_flags, proto_tree *tree, struct $d->{NAME} *r);\n\n";
+                       }
 
-       pidl "buffers:\n";
-       pidl "\tif (!(ndr_flags & NDR_BUFFERS)) goto done;\n";
-       foreach my $e (@{$struct->{ELEMENTS}}) {
-               ParseElementPushBuffer($e, "r->", "NDR_BUFFERS");
+                       if ($d->{DATA}{TYPE} eq "UNION") {
+                           pidl "void ndr_pull_$d->{NAME}(struct ndr_pull *ndr, int ndr_flags, proto_tree *tree, union $d->{NAME} *r, uint16 level);\n\n";
+                       }
+                   }
+               }
+           }
        }
 
-       pidl "\tndr_push_struct_end(ndr);\n";
-
-       pidl "done:\n";
-
-       end_flags($struct);
+       close(OUT);
 }
 
 #####################################################################
-# parse a struct - pull side
-sub ParseStructPull($)
+# rewrite autogenerated header file
+sub RewriteHeader($$$)
 {
-       my($struct) = shift;
-       my $conform_e;
+    my($idl) = shift;
+    my($input) = shift;
+    my($output) = shift;
 
-       if (! defined $struct->{ELEMENTS}) {
-               return;
-       }
+    %needed = ();
 
-       # see if the structure contains a conformant array. If it
-       # does, then it must be the last element of the structure, and
-       # we need to pull the conformant length early, as it fits on
-       # the wire before the structure (and even before the structure
-       # alignment)
-       my $e = $struct->{ELEMENTS}[-1];
-       if (defined $e->{ARRAY_LEN} && $e->{ARRAY_LEN} eq "*") {
-               $conform_e = $e;
-               pidl "\tuint32_t _conformant_size;\n";
-               $conform_e->{CONFORMANT_SIZE} = "_conformant_size";
-       }
+    # Open files
 
-       # declare any internal pointers we need
-       foreach my $e (@{$struct->{ELEMENTS}}) {
-               if (util::need_wire_pointer($e) &&
-                   !util::has_property($e, "relative")) {
-                       pidl "\tuint32_t _ptr_$e->{NAME};\n";
-               }
-       }
+    open(IN, "<$input") || die "can't open $input for reading";
+    open(OUT, ">$output") || die "can't open $output for writing";    
+   
+    # Read through file
 
-       start_flags($struct);
+    while(<IN>) {
 
-       pidl "\tif (!(ndr_flags & NDR_SCALARS)) goto buffers;\n";
+       # Not interested in ndr_push or ndr_print routines as they
+       # define structures we aren't interested in.
 
-       pidl "\tndr_pull_struct_start(ndr);\n";
+       s/^NTSTATUS ndr_push.*?;\n//smg;
+       s/^void ndr_print.*?;\n//smg;
 
-       if (defined $conform_e) {
-               pidl "\tndr_pull_uint32(ndr, &$conform_e->{CONFORMANT_SIZE});\n";
-       }
+       # Get rid of async send and receive function.
 
-       my $align = struct_alignment($struct);
-       pidl "\tndr_pull_align(ndr, $align);\n";
+       s/^NTSTATUS dcerpc_.*?;//smg;
+       s/^struct rpc_request.*?;//smg;
 
-       foreach my $e (@{$struct->{ELEMENTS}}) {
-               ParseElementPullScalar($e, "r->", "NDR_SCALARS");
-       }       
+       # Rewrite librpc includes
 
-       pidl "buffers:\n";
-       pidl "\tif (!(ndr_flags & NDR_BUFFERS)) goto done;\n";
-       foreach my $e (@{$struct->{ELEMENTS}}) {
-               ParseElementPullBuffer($e, "r->", "NDR_BUFFERS");
-       }
+       s/^\#include\ \"librpc\/gen_ndr\/ndr_(.*?).h\"$
+           /\#include \"packet-dcerpc-$1.h\"/smgx;
 
-       pidl "\tndr_pull_struct_end(ndr);\n";
+       # Convert samba fixed width types to stdint types
 
-       pidl "done:\n";
+       s/((u)?int)([0-9]+)/$1$3_t/smg;
 
-       end_flags($struct);
-}
+       # Rename struct ndr_pull to struct pidl_pull
 
+       s/struct ndr_pull \*ndr/struct pidl_pull \*ndr/smg;
 
-#####################################################################
-# parse a union - push side
-sub ParseUnionPush($)
-{
-       my $e = shift;
-       my $have_default = 0;
+       # Change prototypes for public functions
 
-       start_flags($e);
+       s/(struct pidl_pull \*ndr, int ndr_flags)/$1, pidl_tree *tree/smg;
 
-       pidl "\tif (!(ndr_flags & NDR_SCALARS)) goto buffers;\n";
+       # Bitmaps
 
-       pidl "\tndr_push_struct_start(ndr);\n";
+       s/(uint32_t \*r\);)/pidl_tree *tree, int hf, $1/smg;
 
-#      my $align = union_alignment($e);
-#      pidl "\tndr_push_align(ndr, $align);\n";
+       pidl $_;
+    }
 
-       pidl "\tswitch (level) {\n";
-       foreach my $el (@{$e->{DATA}}) {
-               if ($el->{CASE} eq "default") {
-                       pidl "\tdefault:\n";
-                       $have_default = 1;
-               } else {
-                       pidl "\tcase $el->{CASE}:\n";
-               }
-               if ($el->{TYPE} eq "UNION_ELEMENT") {
-                       ParseElementPushScalar($el->{DATA}, "r->", "NDR_SCALARS");
-               }
-               pidl "\tbreak;\n\n";
-       }
-       if (! $have_default) {
-               pidl "\tdefault:\n";
-               pidl "\t\treturn ndr_push_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);\n";
-       }
-       pidl "\t}\n";
-       pidl "buffers:\n";
-       pidl "\tif (!(ndr_flags & NDR_BUFFERS)) goto done;\n";
-       pidl "\tswitch (level) {\n";
-       foreach my $el (@{$e->{DATA}}) {
-               if ($el->{CASE} eq "default") {
-                       pidl "\tdefault:\n";
-               } else {
-                       pidl "\tcase $el->{CASE}:\n";
-               }
-               if ($el->{TYPE} eq "UNION_ELEMENT") {
-                       ParseElementPushBuffer($el->{DATA}, "r->", "NDR_BUFFERS");
-               }
-               pidl "\tbreak;\n\n";
-       }
-       if (! $have_default) {
-               pidl "\tdefault:\n";
-               pidl "\t\treturn ndr_push_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);\n";
-       }
-       pidl "\t}\n";
-       pidl "\tndr_push_struct_end(ndr);\n";
-       pidl "done:\n";
-       end_flags($e);
+    close(OUT);   
 }
 
 #####################################################################
-# parse a union - pull side
-sub ParseUnionPull($)
+# rewrite autogenerated C file
+sub RewriteC($$$)
 {
-       my $e = shift;
-       my $have_default = 0;
+    my($idl) = shift;
+    my($input) = shift;
+    my($output) = shift;
 
-       start_flags($e);
+    # Open files
 
-       pidl "\tif (!(ndr_flags & NDR_SCALARS)) goto buffers;\n";
+    open(IN, "<$input") || die "can't open $input for reading";
+    open(OUT, ">$output") || die "can't open $output for writing";    
+    
+    # Get name of module
 
-       pidl "\tndr_pull_struct_start(ndr);\n";
-
-#      my $align = union_alignment($e);
-#      pidl "\tndr_pull_align(ndr, $align);\n";
-
-       pidl "\tswitch (level) {\n";
-       foreach my $el (@{$e->{DATA}}) {
-               if ($el->{CASE} eq "default") {
-                       pidl "\tdefault: {\n";
-                       $have_default = 1;
-               } else {
-                       pidl "\tcase $el->{CASE}: {\n";
-               }
-               if ($el->{TYPE} eq "UNION_ELEMENT") {
-                       my $e2 = $el->{DATA};
-                       if ($e2->{POINTERS}) {
-                               pidl "\t\tuint32_t _ptr_$e2->{NAME};\n";
-                       }
-                       ParseElementPullScalar($el->{DATA}, "r->", "NDR_SCALARS");
-               }
-               pidl "\tbreak; }\n\n";
-       }
-       if (! $have_default) {
-               pidl "\tdefault:\n";
-               pidl "\t\treturn ndr_pull_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);\n";
-       }
-       pidl "\t}\n";
-       pidl "buffers:\n";
-       pidl "\tif (!(ndr_flags & NDR_BUFFERS)) goto done;\n";
-       pidl "\tswitch (level) {\n";
-       foreach my $el (@{$e->{DATA}}) {
-               if ($el->{CASE} eq "default") {
-                       pidl "\tdefault:\n";
-               } else {
-                       pidl "\tcase $el->{CASE}:\n";
-               }
-               if ($el->{TYPE} eq "UNION_ELEMENT") {
-                       ParseElementPullBuffer($el->{DATA}, "r->", "NDR_BUFFERS");
-               }
-               pidl "\tbreak;\n\n";
-       }
-       if (! $have_default) {
-               pidl "\tdefault:\n";
-               pidl "\t\treturn ndr_pull_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);\n";
+    foreach my $x (@{$idl}) {
+       if ($x->{TYPE} eq "INTERFACE") { 
+           ModuleHeader($x);
+           $module = $x->{NAME};
+           BuildNeeded($x);
        }
-       pidl "\t}\n";
-       pidl "\tndr_pull_struct_end(ndr);\n";
-       pidl "done:\n";
-       end_flags($e);
-}
+    }
 
-#####################################################################
-# parse a type
-sub ParseTypePush($)
-{
-       my($data) = shift;
+    pidl "#include \"eparser.h\"\n\n";
 
-       if (ref($data) eq "HASH") {
-               ($data->{TYPE} eq "STRUCT") &&
-                   ParseStructPush($data);
-               ($data->{TYPE} eq "UNION") &&
-                   ParseUnionPush($data);
-       }
-}
+    pidl "extern const value_string NT_errors[];\n\n";
 
-#####################################################################
-# parse a type
-sub ParseTypePull($)
-{
-       my($data) = shift;
+    # Declarations for hf variables
 
-       if (ref($data) eq "HASH") {
-               ($data->{TYPE} eq "STRUCT") &&
-                   ParseStructPull($data);
-               ($data->{TYPE} eq "UNION") &&
-                   ParseUnionPull($data);
-       }
-}
+    pidl "static int hf_opnum = -1;\n";
+    pidl "static int hf_ptr = -1;\n";
+    pidl "static int hf_array_size = -1;\n";
+    pidl "static int hf_result_NTSTATUS = -1;\n";
 
-#####################################################################
-# parse a typedef
-sub ParseTypedefEthereal($)
-{
-       my($e) = shift;
-       my $static = fn_prefix($e);
-
-       if ($e->{DATA}->{TYPE} eq "STRUCT") {
-           pidl $static . "int dissect_$e->{NAME}(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int flags)\n";
-           pidl "\n{\n";
-#              ParseTypePush($e->{DATA});
-           pidl "\treturn offset;\n";
-           pidl "}\n\n";
-       }
+    foreach my $y (keys(%needed)) {
+       pidl "static int $y = -1;\n", if $y =~ /^hf_/;
+    }
 
-       if ($e->{DATA}->{TYPE} eq "UNION") {
-           pidl $static . "int dissect_$e->{NAME}(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int flags)\n";
-               pidl "\n{\n";
-#              ParseTypePush($e->{DATA});
-           pidl "\treturn offset;\n";
-               pidl "}\n\n";
-       }
-}
+    pidl "\n";
 
+    foreach my $y (keys(%needed)) {
+       pidl "static gint $y = -1;\n", if $y =~ /^ett_/;
+    }
 
-#####################################################################
-# parse a function element
-sub ParseFunctionElementPush($$)
-{ 
-       my $e = shift;
-       my $inout = shift;
-
-       if (util::array_size($e)) {
-               if (util::need_wire_pointer($e)) {
-                   pidl "\toffset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, hf_ptr, &ptr);\n";
-                   pidl "\tif (ptr) {\n";
-                       ParseArrayPush($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
-                       pidl "\t}\n";
-               } else {
-                       ParseArrayPush($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
-               }
-       } else {
-               ParseElementPushScalar($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
-               if ($e->{POINTERS}) {
-                       ParseElementPushBuffer($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
-               }
-       }
-}      
+    pidl "\n";
 
-#####################################################################
-# parse a function
-sub ParseFunctionEthereal($)
-{ 
-       my($fn) = shift;
-       my $static = fn_prefix($fn);
+    # Read through file
 
-       # Comment displaying IDL for this function
-       
-       pidl "/*\n\n";
-       pidl IdlDump::DumpFunction($fn);
-       pidl "*/\n\n";
+    while(<IN>) {
 
-       # Request
+       #
+        # Regexps to do a first pass at removing stuff we aren't
+       # interested in for ehtereal parsers.
+       #
+
+       # Remove the NDR_CHECK() macro calls.  Ethereal take care of
+       # this for us as part of the tvbuff_t structure.
 
-       pidl $static . "int $fn->{NAME}_rqst(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep)\n";
-       pidl "{\n";
-       pidl "\tint flags = NDR_SCALARS|NDR_BUFFERS;\n\n";
-       pidl "\tguint32 ptr;\n\n";
+       s/NDR_CHECK\((.*)\)/$1/g;
+
+       # We're not interested in ndr_{print,push,size} functions so
+       # just delete them.
+
+       next, if /^(static )?NTSTATUS ndr_push/ .. /^}/;
+        next, if /^void ndr_print/ .. /^}/;
+        next, if /^size_t ndr_size/ .. /^}/;
+
+       # Get rid of dcerpc interface structures and functions since
+       # they are also not very interesting.
 
-       foreach my $e (@{$fn->{DATA}}) {
-               if (util::has_property($e, "in")) {
-                       ParseFunctionElementPush($e, "in");
-               }               
-       }
+next, if /^static const struct dcerpc_interface_call/ .. /^};/;
+next, if /^static const char \* const [a-z]+_endpoint_strings/ ../^};/;
+next, if /^static const struct dcerpc_endpoint_list/ .. /^};/;
+next, if /^const struct dcerpc_interface_table/ .. /^};/;
+next, if /^static NTSTATUS dcerpc_ndr_[a-z]+_init/ .. /^}/;
+next, if /^NTSTATUS dcerpc_[a-z]+_init/ .. /^}/;
 
-       pidl "\n\treturn offset;\n";
-       pidl "}\n\n";
+       # Rewrite includes to packet-dcerpc-foo.h instead of ndr_foo.h
 
-       # Response
+       s/^\#include \".*?ndr_(.*?).h\"$/\#include \"packet-dcerpc-$1.h\"/smg;
 
-       pidl $static . "int $fn->{NAME}_resp(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep)\n";
-       pidl "{\n";
-       pidl "\tint flags = NDR_SCALARS|NDR_BUFFERS;\n\n";
-       pidl "\tguint32 ptr;\n\n";
+       #
+       # OK start wrapping the ndr_pull functions that actually
+       # implement the NDR decoding routines.  This mainly consists
+       # of adding a couple of parameters to each function call.
+        #
 
-       foreach my $e (@{$fn->{DATA}}) {
-               if (util::has_property($e, "out")) {
-                       ParseFunctionElementPush($e, "out");
-               }               
-       }
+       # Add proto tree and hf argument to ndr_pull_ptr() calls.
 
-       if ($fn->{RETURN_TYPE} && $fn->{RETURN_TYPE} ne "void") {
-           if ($fn->{RETURN_TYPE} eq "NTSTATUS") {
-               pidl "\n\toffset = dissect_ntstatus(tvb, offset, pinfo, tree, drep, hf_rc, NULL);\n";
-           } else {
-               pidl "\tproto_tree_add_text(tree, tvb, offset, -1, \"Unhandled return type '$fn->{RETURN_TYPE}'\");\n";
-           }
-       }
+       s/(ndr_pull_ptr\(ndr,\ ([^\)]*?)\);)
+           /ndr_pull_ptr(ndr, tree, hf_ptr, $2);/smgx;
 
-       pidl "\n\treturn offset;\n";
-       pidl "}\n\n";
-}
+       # Wrap ndr_pull_array_size() and ndr_pull_array_length()
+       # functions.  Add leading space in front of first parameter so
+       # we won't get caught by later regexps.
 
-#####################################################################
-# produce a function call table
-sub FunctionTable($)
-{
-       my($interface) = shift;
-       my($data) = $interface->{DATA};
-       my $count = 0;
-       my $uname = uc $interface->{NAME};
+       s/(ndr_pull_array_(size|length)\(ndr,\ ([^\)]*?)\);)
+           /ndr_pull_array_$2( ndr, tree, $3);/smgx;
 
-       foreach my $d (@{$data}) {
-               if ($d->{TYPE} eq "FUNCTION") { $count++; }
-       }
+       # Add tree argument to ndr_pull_array() and
+       # ndr_pull_array_foo() calls.
 
-       if ($count == 0) {
-               return;
-       }
-
-       pidl "static dcerpc_sub_dissector dcerpc_dissectors[] = {\n";
-       my $num = 0;
-       foreach my $d (@{$data}) {
-               if ($d->{TYPE} eq "FUNCTION") {
-                   # Strip module name from function name, if present
-                   my($n) = $d->{NAME};
-                   $n = substr($d->{NAME}, length($module) + 1),
-                       if $module eq substr($d->{NAME}, 0, length($module));
-                   pidl "\t{ $num, \"$n\",\n";
-                   pidl "\t\t$d->{NAME}_rqst,\n";
-                   pidl "\t\t$d->{NAME}_resp },\n";
-                   $num++;
-               }
-       }
-       pidl "};\n\n";
-}
+       s/(ndr_pull_array\(
+          ndr,\ 
+          ([^,]*?),\                                # NDR_SCALARS etc
+          (\(void\ \*\*\)r->(in|out|)\.?([^,]*?)),\ # Pointer to array entries
+          ([^\)].*?)\);)                            # All other arguments
+           /ndr_pull_array( ndr, $2, tree, $3, $6);/smgx;
 
+       s/(ndr_pull_array_([^\(]*?)\(
+          ndr,\ 
+          ([^,]*?),\                               # NDR_SCALARS etc
+          (r->((in|out).)?([^,]*?)),\              # Pointer to array elements
+          (.*?)\);)                                # Number of elements
+           /ndr_pull_array_$2( ndr, $3, tree, hf_$7_$2_array, $4, $8);/smgx;
+       # Save ndr_pull_relative{1,2}() calls from being wrapped by the
+       # proceeding regexp by adding a leading space.
 
-#####################################################################
-# parse the interface definitions
-sub ParseInterface($)
-{
-       my($interface) = shift;
-       my($data) = $interface->{DATA};
+       s/ndr_pull_(relative1|relative2)\((.*?)\);/
+           ndr_pull_$1( $2);/smgx;
 
-       foreach my $d (@{$data}) {
-               if ($d->{TYPE} eq "TYPEDEF") {
-                   $structs{$d->{NAME}} = $d;
-           }
-       }
+       # Call ethereal wrappers for pull of scalar values in
+       # structures and functions, e.g
+       #
+       # ndr_pull_uint32(ndr, &r->in.access_mask);
+       # ndr_pull_uint32(ndr, &r->idx);
 
-       foreach my $d (@{$data}) {
-               ($d->{TYPE} eq "TYPEDEF") &&
-                   ParseTypedefEthereal($d);
-               ($d->{TYPE} eq "FUNCTION") && 
-                   ParseFunctionEthereal($d);
-       }
+       s/(ndr_pull_([^\)]*?)
+          \(ndr,\ 
+          (&?r->((in|out)\.)?         # Function args contain leading junk
+           ([^\)]*?))                 # Element name
+          \);)          
+           /ndr_pull_$2(ndr, tree, hf_$6_$2, $3);/smgx;
 
-       FunctionTable($interface);
-}
+       # Add tree and hf argument to pulls of "internal" scalars like
+       # array sizes, levels, etc.
 
-# Convert an idl type to an ethereal FT_* type
+       s/(ndr_pull_(uint32|uint16)\(
+          ndr,\ 
+          (&_([^\)]*?))        # Internal arg names have leading underscore
+          \);)
+           /ndr_pull_$2(ndr, tree, hf_$4, $3);/smgx;
 
-sub type2ft($)
-{
-    my($t) = shift;
+       # Add subtree argument to calls dissecting structures, e.g
+       #
+       # ndr_pull_string(ndr, NDR_SCALARS|NDR_BUFFERS, &r->command);
+       # ndr_pull_atsvc_enum_ctr(ndr, NDR_SCALARS|NDR_BUFFERS, r->in.ctr);
 
-    return "FT_UINT32", if ($t eq "uint32");
-    return "FT_UINT16", if ($t eq "uint16");
-    return "FT_BYTES";
-}
+       s/(ndr_pull_([^\)]*?)\(
+          ndr,\ 
+          (NDR_[^,]*?),\ 
+          ([^\(].*?)\);)
+           /ndr_pull_$2(ndr, $3, get_subtree(tree, \"$2\", ndr, ett_$2), $4);
+       /smgx;
 
-# Select an ethereal BASE_* type for an idl type
+       # Add proto_tree parameter to pull function prototypes, e.g
+       #
+       # static NTSTATUS ndr_pull_atsvc_JobInfo(struct ndr_pull *ndr, 
+       #         int ndr_flags, struct atsvc_JobInfo *r)
 
-sub type2base($)
-{
-    my($t) = shift;
+       s/^((static\ )?NTSTATUS\ ndr_pull_([^\(]*?)\(
+           struct\ ndr_pull\ \*ndr,\ 
+           int\ (ndr_)?flags)
+           /$1, proto_tree \*tree/smgx;
 
-    return "BASE_DEC", if ($t eq "uint32") or ($t eq "uint16");
-    return "BASE_NONE";
-}
+       # Add proto_tree parameter to ndr_pull_subcontext_flags_fn()
 
-sub NeededFunction($)
-{
-       my $fn = shift;
-       $needed{"pull_$fn->{NAME}"} = 1;
-       $needed{"push_$fn->{NAME}"} = 1;
-       foreach my $e (@{$fn->{DATA}}) {
-           $needed{"hf_$e->{NAME}_$e->{TYPE}"} = {
-               'name' => $e->{NAME},
-               'type' => $e->{TYPE},
-               'ft'   => type2ft($e->{TYPE}),
-               'base' => type2base($e->{TYPE})
-               };
-           $e->{PARENT} = $fn;
-           $needed{"pull_$e->{TYPE}"} = 1;
-           $needed{"push_$e->{TYPE}"} = 1;
-       }
-}
+        s/(ndr_pull_subcontext_flags_fn\(ndr)(.*?);/$1, tree$2;/smg;
 
-sub NeededTypedef($)
-{
-       my $t = shift;
-       if (util::has_property($t->{DATA}, "public")) {
-               $needed{"pull_$t->{NAME}"} = 1;
-               $needed{"push_$t->{NAME}"} = 1;         
-       }
-       if ($t->{DATA}->{TYPE} eq "STRUCT") {
-               for my $e (@{$t->{DATA}->{ELEMENTS}}) {
-                       $e->{PARENT} = $t->{DATA};
-                       if ($needed{"pull_$t->{NAME}"}) {
-                               $needed{"pull_$e->{TYPE}"} = 1;
-                       }
-                       if ($needed{"push_$t->{NAME}"}) {
-                               $needed{"push_$e->{TYPE}"} = 1;
-                       }
-               }
-       }
-       if ($t->{DATA}->{TYPE} eq "UNION") {
-               for my $e (@{$t->{DATA}->{DATA}}) {
-                       $e->{PARENT} = $t->{DATA};
-                       if ($e->{TYPE} eq "UNION_ELEMENT") {
-                               if ($needed{"pull_$t->{NAME}"}) {
-                                       $needed{"pull_$e->{DATA}->{TYPE}"} = 1;
-                               }
-                               if ($needed{"push_$t->{NAME}"}) {
-                                       $needed{"push_$e->{DATA}->{TYPE}"} = 1;
-                               }
-                       }
-               }
-       }
-}
+       # Get rid of ndr_pull_error() calls for the moment. Ethereal
+       # should take care of buffer overruns and inconsistent array
+       # sizes for us but it would be nice to have some error text in
+       # the dissection.
 
-#####################################################################
-# work out what parse functions are needed
-sub BuildNeeded($)
-{
-       my($interface) = shift;
-       my($data) = $interface->{DATA};
-       foreach my $d (@{$data}) {
-               ($d->{TYPE} eq "FUNCTION") && 
-                   NeededFunction($d);
-       }
-       foreach my $d (reverse @{$data}) {
-               ($d->{TYPE} eq "TYPEDEF") &&
-                   NeededTypedef($d);
-       }
-}
+       s/(return ndr_pull_error([^;]*?);)/return NT_STATUS_OK; \/\/ $1/smg;
 
-#####################################################################
-# parse the interface definitions
-sub ModuleHeader($)
-{
-    my($h) = shift;
+       # Rename proto_tree args to pidl_tree
 
-    $if_uuid = $h->{PROPERTIES}->{uuid};
-    $if_version = $h->{PROPERTIES}->{version};
-    $if_endpoints = $h->{PROPERTIES}->{endpoints};
-}
+       s/(int (ndr_)?flags), proto_tree \*tree/$1, pidl_tree \*tree/smg;
 
-#####################################################################
-# parse a parsed IDL structure back into an IDL file
-sub Parse($$)
-{
-       my($idl) = shift;
-       my($filename) = shift;
+       # Rename struct ndr_pull to struct pidl_pull
 
-       open(OUT, ">$filename") || die "can't open $filename";    
+       s/struct ndr_pull \*ndr/struct pidl_pull \*ndr/smg;
 
-       pidl "/* parser auto-generated by pidl */\n\n";
-       pidl "#ifdef HAVE_CONFIG_H\n";
-       pidl "#include \"config.h\"\n";
-        pidl "#endif\n\n";
+       # Fix some internal variable declarations
 
-        pidl "#include \"packet-dcerpc.h\"\n";
-        pidl "#include \"packet-dcerpc-nt.h\"\n\n";
-        pidl "#include \"packet-dcerpc-common.h\"\n\n";
+        s/uint(16|32) _level/uint$1_t _level/smg;
+        s/ndr_pull_([^\(]*)\(ndr,\ tree,\ hf_level,\ &_level\);
+       /ndr_pull_$1(ndr, tree, hf_level_$1, &_level);/smgx;
+                               
+       # Enums
+
+        s/(^static\ NTSTATUS\ ndr_pull_(.+?),\ (enum\ .+?)\))
+           /static NTSTATUS ndr_pull_$2, pidl_tree *tree, int hf, $3)/smgx;
+       s/uint(8|16|32) v;/uint$1_t v;/smg;
+       s/(ndr_pull_([^\)]*?)\(ndr,\ &v\);)
+           /ndr_pull_$2(ndr, tree, hf, &v);/smgx;
 
-       pidl "#define NDR_SCALARS 1\n";
-       pidl "#define NDR_BUFFERS 2\n\n";
+       s/(ndr_pull_([^\(]+?)\(ndr,\ &_level\);)
+           /ndr_pull_$2(ndr, tree, hf_$2, &_level);/smgx;
 
-       pidl "extern const value_string NT_errors[];\n\n";
+       # Bitmaps
+
+s/(^(static\ )?NTSTATUS\ ndr_pull_(.+?),\ uint32\ \*r\))
+           /NTSTATUS ndr_pull_$3, pidl_tree *tree, int hf, uint32_t *r)/smgx;
 
-       foreach my $x (@{$idl}) {
-           $module = $x->{NAME}, if $x->{TYPE} eq "INTERFACE";
-       }
+       pidl $_;
+    }
 
-       pidl "static int proto_dcerpc_$module = -1;\n\n";
+    # Function call table
 
-       pidl "static gint ett_dcerpc_$module = -1;\n\n";
+    foreach my $x (@{$idl}) {
+       if ($x->{TYPE} eq "INTERFACE") { 
+           foreach my $y (@{$x->{"INHERITED_DATA"}}) {
+               ($y->{TYPE} eq "FUNCTION") && ParseFunctionPull($y);
+           }
 
-       pidl "static int hf_opnum = -1;\n";
-       pidl "static int hf_rc = -1;\n";
-       pidl "static int hf_ptr = -1;\n";
-       pidl "static int hf_switch = -1;\n";
-       pidl "static int hf_policy_handle = -1;\n";
-       pidl "static int hf_array_size = -1;\n";
-       pidl "static int hf_length_is = -1;\n";
+           FunctionTable($x);
+       }
+    }
 
-       foreach my $x (@{$idl}) {
-           ($x->{TYPE} eq "MODULEHEADER") && 
-               ModuleHeader($x);
+    # Ethereal protocol registration
 
-           if ($x->{TYPE} eq "INTERFACE") { 
-               BuildNeeded($x);
+    pidl "int proto_dcerpc_pidl_$module = -1;\n\n";
 
-               foreach my $y (keys(%needed)) {
-                   pidl "static int $y = -1;\n", if $y =~ /^hf_/;
-               }
+    pidl "static gint ett_dcerpc_$module = -1;\n\n";
 
-               ParseInterface($x);
-           }
-       }
+    if (defined($if_uuid)) {
 
        pidl "static e_uuid_t uuid_dcerpc_$module = {\n";
-       pidl "\t0x" . substr($if_uuid, 0, 8);
-       pidl ", 0x" . substr($if_uuid, 9, 4);
-       pidl ", 0x" . substr($if_uuid, 14, 4) . ",\n";
-       pidl "\t{ 0x" . substr($if_uuid, 19, 2);
-       pidl ", 0x" . substr($if_uuid, 21, 2);
-       pidl ", 0x" . substr($if_uuid, 24, 2);
-       pidl ", 0x" . substr($if_uuid, 26, 2);
-       pidl ", 0x" . substr($if_uuid, 28, 2);
-       pidl ", 0x" . substr($if_uuid, 30, 2);
-       pidl ", 0x" . substr($if_uuid, 32, 2);
-       pidl ", 0x" . substr($if_uuid, 34, 2) . " }\n";
+       pidl "\t0x" . substr($if_uuid, 1, 8);
+       pidl ", 0x" . substr($if_uuid, 10, 4);
+       pidl ", 0x" . substr($if_uuid, 15, 4) . ",\n";
+       pidl "\t{ 0x" . substr($if_uuid, 20, 2);
+       pidl ", 0x" . substr($if_uuid, 22, 2);
+       pidl ", 0x" . substr($if_uuid, 25, 2);
+       pidl ", 0x" . substr($if_uuid, 27, 2);
+       pidl ", 0x" . substr($if_uuid, 29, 2);
+       pidl ", 0x" . substr($if_uuid, 31, 2);
+       pidl ", 0x" . substr($if_uuid, 33, 2);
+       pidl ", 0x" . substr($if_uuid, 35, 2) . " }\n";
        pidl "};\n\n";
+    }
 
+    if (defined($if_version)) {
        pidl "static guint16 ver_dcerpc_$module = " . $if_version . ";\n\n";
+    }
 
+    pidl "void proto_register_dcerpc_pidl_$module(void)\n";
+    pidl "{\n";
 
-       pidl "void proto_register_dcerpc_samr(void)\n";
-       pidl "{\n";
-        pidl "\tstatic hf_register_info hf[] = {\n";
-
-       pidl "\t{ &hf_opnum, { \"Operation\", \"$module.opnum\", FT_UINT16, BASE_DEC, NULL, 0x0, \"Operation\", HFILL }},\n";
-       pidl "\t{ &hf_policy_handle, { \"Policy handle\", \"$module.policy\", FT_BYTES, BASE_NONE, NULL, 0x0, \"Policy handle\", HFILL }},\n";
-       pidl "\t{ &hf_rc, { \"Return code\", \"$module.rc\", FT_UINT32, BASE_HEX, VALS(NT_errors), 0x0, \"Return status code\", HFILL }},\n";
-       pidl "\t{ &hf_switch, { \"Switch\", \"$module.switch\", FT_UINT16, BASE_DEC, NULL, 0x0, \"Switch\", HFILL }},\n";
-       pidl "\t{ &hf_array_size, { \"Array size\", \"$module.array_size\", FT_UINT32, BASE_DEC, NULL, 0x0, \"Array size\", HFILL }},\n";
-       pidl "\t{ &hf_length_is, { \"Length is\", \"$module.length_is\", FT_UINT32, BASE_DEC, NULL, 0x0, \"Length is\", HFILL }},\n";
-       pidl "\t{ &hf_ptr, { \"Pointer\", \"$module.ptr\", FT_UINT32, BASE_HEX, NULL, 0x0, \"Pointer\", HFILL }},\n";
-
-       foreach my $x (keys(%needed)) {
-           next, if !($x =~ /^hf_/);
-
-           pidl "\t{ &$x,\n";
-           pidl "\t  { \"$needed{$x}{name}\", \"$x\", $needed{$x}{ft}, $needed{$x}{base},\n";
-           pidl"\t  NULL, 0, \"$x\", HFILL }},\n";
-       }
+    pidl "\tstatic hf_register_info hf[] = {\n";
+    pidl "\t{ &hf_opnum, { \"Operation\", \"$module.opnum\", FT_UINT16, BASE_DEC, NULL, 0x0, \"Operation\", HFILL }},\n";
+       pidl "\t{ &hf_result_NTSTATUS, { \"Return code\", \"$module.rc\", FT_UINT32, BASE_HEX, VALS(NT_errors), 0x0, \"Return status code\", HFILL }},\n";
+    pidl "\t{ &hf_ptr, { \"Pointer\", \"$module.ptr\", FT_UINT32, BASE_HEX, NULL, 0x0, \"Pointer\", HFILL }},\n";
 
-       pidl "\t};\n\n";
+    foreach my $x (keys(%needed)) {
+       next, if !($x =~ /^hf_/);
+       pidl "\t{ &$x,\n";
+       pidl "\t  { \"$needed{$x}{name}\", \"$x\", $needed{$x}{ft}, $needed{$x}{base}, NULL, 0, \"$x\", HFILL }},\n";
+    }
 
-        pidl "\tstatic gint *ett[] = {\n";
-       pidl "\t\t&ett_dcerpc_$module,\n";
-       pidl "\t};\n\n";
+    pidl "\t};\n\n";
 
-        pidl "\tproto_dcerpc_$module = proto_register_protocol(\"$module\", \"$module\", \"$module\");\n\n";
+    pidl "\tstatic gint *ett[] = {\n";
+    pidl "\t\t&ett_dcerpc_$module,\n";
+    foreach my $x (keys(%needed)) {
+       pidl "\t\t&$x,\n", if $x =~ /^ett_/;
+    }
+    pidl "\t};\n\n";
+    
+    if (defined($if_uuid)) {
+
+       pidl "\tproto_dcerpc_pidl_$module = proto_register_protocol(\"pidl_$module\", \"pidl_$module\", \"pidl_$module\");\n\n";
 
-       pidl "\tproto_register_field_array(proto_dcerpc_$module, hf, array_length (hf));\n";
-        pidl "\tproto_register_subtree_array(ett, array_length(ett));\n";
+       pidl "\tproto_register_field_array(proto_dcerpc_pidl_$module, hf, array_length (hf));\n";
+       pidl "\tproto_register_subtree_array(ett, array_length(ett));\n";
 
-        pidl "}\n\n";
+       pidl "}\n\n";
 
-       pidl "void proto_reg_handoff_dcerpc_$module(void)\n";
+       pidl "void proto_reg_handoff_dcerpc_pidl_$module(void)\n";
        pidl "{\n";
-        pidl "\tdcerpc_init_uuid(proto_dcerpc_$module, ett_dcerpc_$module, \n";
+       pidl "\tdcerpc_init_uuid(proto_dcerpc_pidl_$module, ett_dcerpc_$module, \n";
        pidl "\t\t&uuid_dcerpc_$module, ver_dcerpc_$module, \n";
        pidl "\t\tdcerpc_dissectors, hf_opnum);\n";
-        pidl "}\n";
+       pidl "}\n";
 
-       close(OUT);
+    } else {
+
+       pidl "\tint proto_dcerpc;\n\n";
+       pidl "\tproto_dcerpc = proto_get_id_by_filter_name(\"dcerpc\");\n";
+       pidl "\tproto_register_field_array(proto_dcerpc, hf, array_length(hf));\n";
+       pidl "\tproto_register_subtree_array(ett, array_length(ett));\n";
+
+       pidl "}\n";
+
+    }
+
+    close(OUT);   
 }
 
 1;