pidl/ws: Fix Dead Store (Dead assignement/Dead increment) warning found by Clang
[metze/samba/wip.git] / pidl / lib / Parse / Pidl / Wireshark / NDR.pm
index a9ad555cca4acfe442c1a6906547308109e75bca..40c59c9a5176fc203654965ae493c9a19e79594b 100644 (file)
@@ -1,5 +1,5 @@
 ##################################################
-# Samba4 NDR parser generator for IDL structures
+# Wireshark NDR parser generator for IDL structures
 # Copyright tridge@samba.org 2000-2003
 # Copyright tpot@samba.org 2001,2005
 # Copyright jelmer@samba.org 2004-2007
@@ -27,7 +27,7 @@ use Parse::Pidl::Util qw(has_property property_matches make_str);
 use Parse::Pidl::NDR qw(ContainsString GetNextLevel);
 use Parse::Pidl::Dump qw(DumpType DumpFunction);
 use Parse::Pidl::Wireshark::Conformance qw(ReadConformance);
-use File::Basename;    
+use File::Basename;
 
 use vars qw($VERSION);
 $VERSION = '0.01';
@@ -57,12 +57,12 @@ sub StripPrefixes($$)
 
 sub field2name($)
 {
-    my($field) = shift;
+       my($field) = shift;
 
-    $field =~ s/_/ /g;         # Replace underscores with spaces
-    $field =~ s/(\w+)/\u\L$1/g;        # Capitalise each word
-    
-    return $field;
+       $field =~ s/_/ /g;              # Replace underscores with spaces
+       $field =~ s/(\w+)/\u\L$1/g;     # Capitalise each word
+
+       return $field;
 }
 
 sub new($)
@@ -91,7 +91,7 @@ sub pidl_code($$)
 {
        my ($self, $d) = @_;
        return if (defined($self->{cur_fn}) and defined($self->{conformance}->{manual}->{$self->{cur_fn}}));
+
        if ($d) {
                $self->{res}->{code} .= $self->{tabs};
                $self->{res}->{code} .= $d;
@@ -143,17 +143,17 @@ sub Enum($$$$)
 
        return if (defined($self->{conformance}->{noemit}->{StripPrefixes($name, $self->{conformance}->{strip_prefixes})}));
 
-       foreach (@{$e->{ELEMENTS}}) {
+       foreach (@{$e->{ELEMENTS}}) {
                if (/([^=]*)=(.*)/) {
                        $self->pidl_hdr("#define $1 ($2)");
                }
        }
-       
+
        $self->pidl_hdr("extern const value_string $valsstring\[];");
-       $self->pidl_hdr("int $dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, guint8 *drep _U_, int hf_index _U_, guint32 *param _U_);");
+       $self->pidl_hdr("int $dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, guint8 *drep _U_, int hf_index _U_, g$e->{BASE_TYPE} *param _U_);");
 
        $self->pidl_def("const value_string ".$valsstring."[] = {");
-       foreach (@{$e->{ELEMENTS}}) {
+       foreach (@{$e->{ELEMENTS}}) {
                next unless (/([^=]*)=(.*)/);
                $self->pidl_def("\t{ $1, \"$1\" },");
        }
@@ -163,19 +163,19 @@ sub Enum($$$$)
 
        $self->pidl_fn_start($dissectorname);
        $self->pidl_code("int");
-       $self->pidl_code("$dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, guint8 *drep _U_, int hf_index _U_, guint32 *param _U_)");
+       $self->pidl_code("$dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, guint8 *drep _U_, int hf_index _U_, g$e->{BASE_TYPE} *param _U_)");
        $self->pidl_code("{");
        $self->indent;
        $self->pidl_code("g$e->{BASE_TYPE} parameter=0;");
-       $self->pidl_code("if(param){");
+       $self->pidl_code("if (param) {");
        $self->indent;
-       $self->pidl_code("parameter=(g$e->{BASE_TYPE})*param;");
+       $self->pidl_code("parameter = *param;");
        $self->deindent;
        $self->pidl_code("}");
        $self->pidl_code("offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, tree, drep, hf_index, &parameter);");
-       $self->pidl_code("if(param){");
+       $self->pidl_code("if (param) {");
        $self->indent;
-       $self->pidl_code("*param=(guint32)parameter;");
+       $self->pidl_code("*param = parameter;");
        $self->deindent;
        $self->pidl_code("}");
        $self->pidl_code("return offset;");
@@ -188,6 +188,13 @@ sub Enum($$$$)
        $self->register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_UINT$enum_size", "BASE_DEC", "0", "VALS($valsstring)", $enum_size / 8);
 }
 
+sub Pipe($$$$)
+{
+       my ($self,$e,$name,$ifname) = @_;
+       error($e->{ORIGINAL}, "Pipe not yet supported");
+       return;
+}
+
 sub Bitmap($$$$)
 {
        my ($self,$e,$name,$ifname) = @_;
@@ -205,7 +212,7 @@ sub Bitmap($$$$)
        $self->pidl_code("proto_item *item = NULL;");
        $self->pidl_code("proto_tree *tree = NULL;");
        $self->pidl_code("");
-               
+
        $self->pidl_code("g$e->{BASE_TYPE} flags;");
        if ($e->{ALIGN} > 1) {
                $self->pidl_code("ALIGN_TO_$e->{ALIGN}_BYTES;");
@@ -215,12 +222,12 @@ sub Bitmap($$$$)
 
        $self->pidl_code("if (parent_tree) {");
        $self->indent;
-       $self->pidl_code("item = proto_tree_add_item(parent_tree, hf_index, tvb, offset, $e->{ALIGN}, TRUE);");
+       $self->pidl_code("item = proto_tree_add_item(parent_tree, hf_index, tvb, offset, $e->{ALIGN}, DREP_ENC_INTEGER(drep));");
        $self->pidl_code("tree = proto_item_add_subtree(item,ett_$ifname\_$name);");
        $self->deindent;
        $self->pidl_code("}\n");
 
-       $self->pidl_code("offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, NULL, drep, -1, &flags);");
+       $self->pidl_code("offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, tree, di, drep, -1, &flags);");
 
        $self->pidl_code("proto_item_append_text(item, \": \");\n");
        $self->pidl_code("if (!flags)");
@@ -233,7 +240,7 @@ sub Bitmap($$$$)
                my $filtername = "$ifname\.$name\.$en";
 
                $self->{hf_used}->{$hf_bitname} = 1;
-               
+
                $self->register_hf_field($hf_bitname, field2name($en), $filtername, "FT_BOOLEAN", $e->{ALIGN} * 8, "TFS(&$name\_$en\_tfs)", $ev, "");
 
                $self->pidl_def("static const true_false_string $name\_$en\_tfs = {");
@@ -246,7 +253,7 @@ sub Bitmap($$$$)
                        $self->pidl_def("   \"$en is NOT SET\",");
                }
                $self->pidl_def("};");
-               
+
                $self->pidl_code("proto_tree_add_boolean(tree, $hf_bitname, tvb, offset-$e->{ALIGN}, $e->{ALIGN}, flags);");
                $self->pidl_code("if (flags&$ev){");
                $self->pidl_code("\tproto_item_append_text(item, \"$en\");");
@@ -270,11 +277,9 @@ sub Bitmap($$$$)
        $self->register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_UINT$size", "BASE_HEX", "0", "NULL", $size/8);
 }
 
-sub ElementLevel($$$$$$$)
+sub ElementLevel($$$$$$$$)
 {
-       my ($self,$e,$l,$hf,$myname,$pn,$ifname) = @_;
-
-       my $param = 0;
+       my ($self,$e,$l,$hf,$myname,$pn,$ifname,$param) = @_;
 
        if (defined($self->{conformance}->{dissectorparams}->{$myname})) {
                $param = $self->{conformance}->{dissectorparams}->{$myname}->{PARAM};
@@ -316,31 +321,49 @@ sub ElementLevel($$$$$$$)
 
 
                        ($bs = 1) if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_ASCII.*"));
-                       
+
                        if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*") and property_matches($e, "flag", ".*LIBNDR_FLAG_STR_LEN4.*")) {
                                $self->pidl_code("char *data;\n");
                                $self->pidl_code("offset = dissect_ndr_cvstring(tvb, offset, pinfo, tree, drep, $bs, $hf, FALSE, &data);");
                                $self->pidl_code("proto_item_append_text(tree, \": %s\", data);");
                        } elsif (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*")) {
                                $self->pidl_code("offset = dissect_ndr_vstring(tvb, offset, pinfo, tree, drep, $bs, $hf, FALSE, NULL);");
+                       } elsif (property_matches($e, "flag", ".*STR_NULLTERM.*")) {
+                               if ($bs == 2) {
+                                       $self->pidl_code("offset = dissect_null_term_wstring(tvb, offset, pinfo, tree, drep, $hf , 0);")
+                               } else {
+                                       $self->pidl_code("offset = dissect_null_term_string(tvb, offset, pinfo, tree, drep, $hf , 0);")
+                               }
                        } else {
                                warn("Unable to handle string with flags $e->{PROPERTIES}->{flag}");
                        }
+               } elsif ($l->{DATA_TYPE} eq "DATA_BLOB") {
+                       my $remain = 0;
+                       $remain = 1 if (property_matches($e->{ORIGINAL}, "flag", ".*LIBNDR_FLAG_REMAINING.*"));
+                       $self->pidl_code("offset = dissect_ndr_datablob(tvb, offset, pinfo, tree, drep, $hf, $remain);");
                } else {
                        my $call;
 
                        if ($self->{conformance}->{imports}->{$l->{DATA_TYPE}}) {
-                               $call = $self->{conformance}->{imports}->{$l->{DATA_TYPE}}->{DATA};     
+                               $call = $self->{conformance}->{imports}->{$l->{DATA_TYPE}}->{DATA};
                                $self->{conformance}->{imports}->{$l->{DATA_TYPE}}->{USED} = 1;
-                       } elsif (defined($self->{conformance}->{imports}->{"$pn.$e->{NAME}"})) {
-                               $call = $self->{conformance}->{imports}->{"$pn.$e->{NAME}"}->{DATA};
+
+                       } elsif (defined($self->{conformance}->{imports}->{"$pn.$e->{NAME}"})) {
+                               $call = $self->{conformance}->{imports}->{"$pn.$e->{NAME}"}->{DATA};
                                $self->{conformance}->{imports}->{"$pn.$e->{NAME}"}->{USED} = 1;
-                           
+
                        } elsif (defined($self->{conformance}->{types}->{$l->{DATA_TYPE}})) {
                                $call= $self->{conformance}->{types}->{$l->{DATA_TYPE}}->{DISSECTOR_NAME};
                                $self->{conformance}->{types}->{$l->{DATA_TYPE}}->{USED} = 1;
                        } else {
-                               $self->pidl_code("offset = $ifname\_dissect_struct_" . $l->{DATA_TYPE} . "(tvb,offset,pinfo,tree,drep,$hf,$param);");
+                               my $t;
+                               if (ref($l->{DATA_TYPE}) eq "HASH" ) {
+                                       $t = "$l->{DATA_TYPE}->{TYPE}_$l->{DATA_TYPE}->{NAME}";
+                               } else {
+                                       $t = $l->{DATA_TYPE};
+                               }
+
+                               $self->pidl_code("offset = $ifname\_dissect_struct_" . $t . "(tvb,offset,pinfo,tree,drep,$hf,$param);");
 
                                return;
                        }
@@ -350,27 +373,96 @@ sub ElementLevel($$$$$$$)
                        $self->pidl_code($call);
                }
        } elsif ($_->{TYPE} eq "SUBCONTEXT") {
+               my $varswitch;
+               if (has_property($e, "switch_is")) {
+                       $varswitch = $e->{PROPERTIES}->{switch_is};
+               }
                my $num_bits = ($l->{HEADER_SIZE}*8);
+               my $hf2 = $self->register_hf_field($hf."_", "Subcontext length", "$ifname.$pn.$_->{NAME}subcontext", "FT_UINT$num_bits", "BASE_HEX", "NULL", 0, "");
+               $num_bits = 3264 if ($num_bits == 32);
+               $self->{hf_used}->{$hf2} = 1;
+               $self->pidl_code("dcerpc_info *di = (dcerpc_info*)pinfo->private_data;");
                $self->pidl_code("guint$num_bits size;");
-               $self->pidl_code("int start_offset = offset;");
+               $self->pidl_code("int conformant = di->conformant_run;");
                $self->pidl_code("tvbuff_t *subtvb;");
-               $self->pidl_code("offset = dissect_ndr_uint$num_bits(tvb, offset, pinfo, tree, drep, $hf, &size);");
-               $self->pidl_code("proto_tree_add_text(tree, tvb, start_offset, offset - start_offset + size, \"Subcontext size\");");
+               $self->pidl_code("");
+               # We need to be able to dissect the length of the context in every case
+               # and conformant run skips the dissections of scalars ...
+               $self->pidl_code("if (!conformant) {");
+               $self->indent;
+               $self->pidl_code("guint32 saved_flags = di->call_data->flags;");
+               $self->pidl_code("offset = dissect_ndr_uint$num_bits(tvb, offset, pinfo, tree, drep, $hf2, &size);");
+               # This is a subcontext, there is normally no such thing as
+               # 64 bit NDR is subcontext so we clear the flag so that we can
+               # continue to dissect handmarshalled stuff with pidl
+               $self->pidl_code("di->call_data->flags &= ~DCERPC_IS_NDR64;");
 
                $self->pidl_code("subtvb = tvb_new_subset(tvb, offset, size, -1);");
-               $self->pidl_code("$myname\_(subtvb, 0, pinfo, tree, drep);");
+               if ($param ne 0) {
+                       $self->pidl_code("$myname\_(subtvb, 0, pinfo, tree, drep, $param);");
+               } else {
+                       $self->pidl_code("$myname\_(subtvb, 0, pinfo, tree, drep);");
+               }
+               $self->pidl_code("offset += size;");
+               $self->pidl_code("di->call_data->flags = saved_flags;");
+               $self->deindent;
+               $self->pidl_code("}");
+       } elsif ($_->{TYPE} eq "PIPE") {
+               error($e->{ORIGINAL}, "Type PIPE not yet supported");
        } else {
                die("Unknown type `$_->{TYPE}'");
        }
 }
 
-sub Element($$$)
+sub SwitchType($$;$)
+{
+       my ($e, $type, $nodiscriminant) = @_;
+
+       my $switch_dt =  getType($type);
+       my $switch_type = undef;
+       if ($switch_dt->{DATA}->{TYPE} eq "ENUM") {
+               $switch_type = Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
+       } elsif ($switch_dt->{DATA}->{TYPE} eq "BITMAP") {
+               $switch_type = Parse::Pidl::Typelist::bitmap_type_fn($switch_dt->{DATA});
+       } elsif ($switch_dt->{DATA}->{TYPE} eq "SCALAR") {
+               if (defined $e->{SWITCH_TYPE}) {
+                       $switch_type = "$e->{SWITCH_TYPE}";
+               } else {
+                       $switch_type = "$switch_dt->{DATA}->{NAME}";
+               }
+       } elsif (not defined $e->{SWITCH_TYPE}) {
+               $switch_type = $nodiscriminant;
+       }
+
+       return $switch_type
+}
+
+sub Element($$$$$)
 {
-       my ($self,$e,$pn,$ifname) = @_;
+       my ($self,$e,$pn,$ifname,$isoruseswitch) = @_;
 
        my $dissectorname = "$ifname\_dissect\_element\_".StripPrefixes($pn, $self->{conformance}->{strip_prefixes})."\_".StripPrefixes($e->{NAME}, $self->{conformance}->{strip_prefixes});
 
-       my $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, drep);";
+       my ($call_code, $moreparam);
+       my $param = 0;
+       if (defined $isoruseswitch) {
+               my $type = $isoruseswitch->[0];
+               my $name = $isoruseswitch->[1];
+
+               my $switch_raw_type = SwitchType($e, $type, "uint32");
+               if (not defined($switch_raw_type)) {
+                       die("Unknown type[$type]\n");
+               }
+               my $switch_type = "g${switch_raw_type}";
+
+               $moreparam = ", $switch_type *".$name;
+               $param = $name;
+               $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, drep, &$name);";
+       } else {
+               $moreparam = "";
+               $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, drep);";
+       }
+
 
        my $type = $self->find_type($e->{TYPE});
 
@@ -392,6 +484,12 @@ sub Element($$$)
                        BASE_TYPE => "BASE_NONE"
                };
        }
+       if (property_matches($e, "flag", ".*LIBNDR_FLAG_ALIGN.*")) {
+               my $align_flag = $e->{PROPERTIES}->{flag};
+               if ($align_flag =~ m/LIBNDR_FLAG_ALIGN(\d+)/) {
+                       $call_code = "ALIGN_TO_$1_BYTES; ".$call_code;
+               }
+       }
 
        my $hf = $self->register_hf_field("hf_$ifname\_$pn\_$e->{NAME}", field2name($e->{NAME}), "$ifname.$pn.$e->{NAME}", $type->{FT_TYPE}, $type->{BASE_TYPE}, $type->{VALSSTRING}, $type->{MASK}, "");
        $self->{hf_used}->{$hf} = 1;
@@ -403,16 +501,25 @@ sub Element($$$)
 
        my $add = "";
 
+       my $oldparam = undef;
        foreach (@{$e->{LEVELS}}) {
+               if (defined $_->{SWITCH_IS}) {
+                       $oldparam = $param;
+                       $param = "*$param";
+               }
                next if ($_->{TYPE} eq "SWITCH");
-               $self->pidl_def("static int $dissectorname$add(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, guint8 *drep _U_);");
+               next if (defined($self->{conformance}->{noemit}->{"$dissectorname$add"}));
+               $self->pidl_def("static int $dissectorname$add(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, guint8 *drep _U_$moreparam);");
                $self->pidl_fn_start("$dissectorname$add");
                $self->pidl_code("static int");
-               $self->pidl_code("$dissectorname$add(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, guint8 *drep _U_)");
+               $self->pidl_code("$dissectorname$add(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, guint8 *drep _U_$moreparam)");
                $self->pidl_code("{");
                $self->indent;
 
-               $self->ElementLevel($e,$_,$hf,$dissectorname.$add,$pn,$ifname);
+               $self->ElementLevel($e,$_,$hf,$dissectorname.$add,$pn,$ifname,$param);
+               if (defined $oldparam) {
+                       $param = $oldparam;
+               }
 
                $self->pidl_code("");
                $self->pidl_code("return offset;");
@@ -433,9 +540,9 @@ sub Function($$$)
        my %dissectornames;
 
        foreach (@{$fn->{ELEMENTS}}) {
-           $dissectornames{$_->{NAME}} = $self->Element($_, $fn->{NAME}, $ifname) if not defined($dissectornames{$_->{NAME}});
+           $dissectornames{$_->{NAME}} = $self->Element($_, $fn->{NAME}, $ifname, undef) if not defined($dissectornames{$_->{NAME}});
        }
-       
+
        my $fn_name = $_->{NAME};
        $fn_name =~ s/^${ifname}_//;
 
@@ -446,7 +553,7 @@ sub Function($$$)
        $self->pidl_code("{");
        $self->indent;
        if ( not defined($fn->{RETURN_TYPE})) {
-       } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS" or $fn->{RETURN_TYPE} eq "WERROR") 
+       } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS" or $fn->{RETURN_TYPE} eq "WERROR")
        {
                $self->pidl_code("guint32 status;\n");
        } elsif (my $type = getType($fn->{RETURN_TYPE})) {
@@ -455,7 +562,7 @@ sub Function($$$)
                } elsif ($type->{DATA}->{TYPE} eq "SCALAR") {
                        $self->pidl_code("g$fn->{RETURN_TYPE} status;\n");
                } else {
-               error($fn, "return type `$fn->{RETURN_TYPE}' not yet supported");
+                       error($fn, "return type `$fn->{RETURN_TYPE}' not yet supported");
                }
        } else {
                error($fn, "unknown return type `$fn->{RETURN_TYPE}'");
@@ -473,14 +580,14 @@ sub Function($$$)
        if (not defined($fn->{RETURN_TYPE})) {
        } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS") {
                $self->pidl_code("offset = dissect_ntstatus(tvb, offset, pinfo, tree, drep, hf\_$ifname\_status, &status);\n");
-               $self->pidl_code("if (status != 0 && check_col(pinfo->cinfo, COL_INFO))");
+               $self->pidl_code("if (status != 0)");
                $self->pidl_code("\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, NT_errors, \"Unknown NT status 0x%08x\"));\n");
                $return_types{$ifname}->{"status"} = ["NTSTATUS", "NT Error"];
        } elsif ($fn->{RETURN_TYPE} eq "WERROR") {
                $self->pidl_code("offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, hf\_$ifname\_werror, &status);\n");
-               $self->pidl_code("if (status != 0 && check_col(pinfo->cinfo, COL_INFO))");
+               $self->pidl_code("if (status != 0)");
                $self->pidl_code("\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, WERR_errors, \"Unknown DOS error 0x%08x\"));\n");
-               
+
                $return_types{$ifname}->{"werror"} = ["WERROR", "Windows Error"];
        } elsif (my $type = getType($fn->{RETURN_TYPE})) {
                if ($type->{DATA}->{TYPE} eq "ENUM") {
@@ -488,12 +595,12 @@ sub Function($$$)
                        my $return_dissect = "dissect_ndr_" .Parse::Pidl::Typelist::enum_type_fn($type->{DATA});
 
                        $self->pidl_code("offset = $return_dissect(tvb, offset, pinfo, tree, drep, hf\_$ifname\_$fn->{RETURN_TYPE}_status, &status);");
-                       $self->pidl_code("if (status != 0 && check_col(pinfo->cinfo, COL_INFO))");
+                       $self->pidl_code("if (status != 0)");
                        $self->pidl_code("\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Status: %s\", val_to_str(status, $ifname\_$fn->{RETURN_TYPE}\_vals, \"Unknown " . $fn->{RETURN_TYPE} . " error 0x%08x\"));\n");
                        $return_types{$ifname}->{$fn->{RETURN_TYPE}."_status"} = [$fn->{RETURN_TYPE}, $fn->{RETURN_TYPE}];
                } elsif ($type->{DATA}->{TYPE} eq "SCALAR") {
                        $self->pidl_code("offset = dissect_ndr_$fn->{RETURN_TYPE}(tvb, offset, pinfo, tree, drep, hf\_$ifname\_$fn->{RETURN_TYPE}_status, &status);");
-                       $self->pidl_code("if (status != 0 && check_col(pinfo->cinfo, COL_INFO))");
+                       $self->pidl_code("if (status != 0)");
                        $self->pidl_code("\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Status: %d\", status);\n");
                        $return_types{$ifname}->{$fn->{RETURN_TYPE}."_status"} = [$fn->{RETURN_TYPE}, $fn->{RETURN_TYPE}];
                }
@@ -534,7 +641,45 @@ sub Struct($$$$)
        $self->register_ett("ett_$ifname\_$name");
 
        my $res = "";
-       ($res.="\t".$self->Element($_, $name, $ifname)."\n\n") foreach (@{$e->{ELEMENTS}});
+       my $varswitchs = {};
+       # will contain the switch var declaration;
+       my $vars = [];
+       foreach (@{$e->{ELEMENTS}}) {
+               if (has_property($_, "switch_is")) {
+                       $varswitchs->{$_->{PROPERTIES}->{switch_is}} = [];
+               }
+       }
+       foreach (@{$e->{ELEMENTS}}) {
+               my $switch_info = undef;
+
+               my $v = $_->{NAME};
+               if (scalar(grep {/^$v$/} keys(%$varswitchs)) == 1) {
+                       # This element is one of the switch attribute
+                       my $switch_raw_type = SwitchType($e, $_->{TYPE}, "uint32");
+                       if (not defined($switch_raw_type)) {
+                               die("Unknown type[$_->{TYPE}]\n");
+                       }
+                       my $switch_type = "g${switch_raw_type}";
+
+                       push @$vars, "$switch_type $v;";
+                       $switch_info = [ $_->{TYPE}, $v ];
+                       $varswitchs->{$v} = $switch_info;
+               }
+
+               if (has_property($_, "switch_is")) {
+                       my $varswitch = $_->{PROPERTIES}->{switch_is};
+                       $switch_info = $varswitchs->{$varswitch};
+               }
+
+               $res.="\t".$self->Element($_, $name, $ifname, $switch_info)."\n\n";
+       }
+
+       my $doalign = undef;
+       if ($e->{ALIGN} > 1 and not property_matches($e, "flag", ".*LIBNDR_FLAG_NOALIGN.*")) {
+               $doalign = 1;
+       } elsif (property_matches($e, "flag", ".*LIBNDR_FLAG_NOALIGN.*")) {
+               $doalign = 0;
+       }
 
        $self->pidl_hdr("int $dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *parent_tree _U_, guint8 *drep _U_, int hf_index _U_, guint32 param _U_);");
 
@@ -543,28 +688,57 @@ sub Struct($$$$)
        $self->pidl_code("$dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *parent_tree _U_, guint8 *drep _U_, int hf_index _U_, guint32 param _U_)");
        $self->pidl_code("{");
        $self->indent;
+       $self->pidl_code($_) foreach (@$vars);
        $self->pidl_code("proto_item *item = NULL;");
-       $self->pidl_code("proto_tree *tree = NULL;");
+       if($res) {
+               $self->pidl_code("proto_tree *tree = NULL;");
+       }
+       if (defined($doalign)) {
+               $self->pidl_code("dcerpc_info *di = (dcerpc_info *)pinfo->private_data;");
+               if ($doalign == 0) {
+                       $self->pidl_code("gboolean oldalign = di->no_align;");
+               }
+       }
        $self->pidl_code("int old_offset;");
        $self->pidl_code("");
 
-       if ($e->{ALIGN} > 1) {
-               $self->pidl_code("ALIGN_TO_$e->{ALIGN}_BYTES;");
+       if (defined($doalign)) {
+               if ($doalign == 1) {
+                       $self->pidl_code("ALIGN_TO_$e->{ALIGN}_BYTES;");
+               }
+               if ($doalign == 0) {
+                       $self->pidl_code("di->no_align = TRUE;");
+               }
+               $self->pidl_code("");
        }
-       $self->pidl_code("");
 
        $self->pidl_code("old_offset = offset;");
        $self->pidl_code("");
        $self->pidl_code("if (parent_tree) {");
        $self->indent;
-       $self->pidl_code("item = proto_tree_add_item(parent_tree, hf_index, tvb, offset, -1, TRUE);");
-       $self->pidl_code("tree = proto_item_add_subtree(item, ett_$ifname\_$name);");
+       $self->pidl_code("item = proto_tree_add_item(parent_tree, hf_index, tvb, offset, -1, ENC_NA);");
+       if($res) {
+               $self->pidl_code("tree = proto_item_add_subtree(item, ett_$ifname\_$name);");
+       }
        $self->deindent;
        $self->pidl_code("}");
 
        $self->pidl_code("\n$res");
 
        $self->pidl_code("proto_item_set_len(item, offset-old_offset);\n");
+       if (defined($doalign) and $doalign == 1) {
+               $self->pidl_code("");
+               $self->pidl_code("if (di->call_data->flags & DCERPC_IS_NDR64) {");
+               $self->indent;
+               $self->pidl_code("ALIGN_TO_$e->{ALIGN}_BYTES;");
+               $self->deindent;
+               $self->pidl_code("}");
+       }
+       if (defined($doalign) and $doalign == 0) {
+               $self->pidl_code("");
+               $self->pidl_code("di->no_align = oldalign;");
+       }
+       $self->pidl_code("");
        $self->pidl_code("return offset;");
        $self->deindent;
        $self->pidl_code("}\n");
@@ -580,27 +754,24 @@ sub Union($$$$)
        my $dissectorname = "$ifname\_dissect_".StripPrefixes($name, $self->{conformance}->{strip_prefixes});
 
        return if (defined($self->{conformance}->{noemit}->{StripPrefixes($name, $self->{conformance}->{strip_prefixes})}));
-       
+
        $self->register_ett("ett_$ifname\_$name");
 
        my $res = "";
        foreach (@{$e->{ELEMENTS}}) {
                $res.="\n\t\t$_->{CASE}:\n";
                if ($_->{TYPE} ne "EMPTY") {
-                       $res.="\t\t\t".$self->Element($_, $name, $ifname)."\n";
+                       $res.="\t\t\t".$self->Element($_, $name, $ifname, undef)."\n";
                }
                $res.="\t\tbreak;\n";
        }
 
-       my $switch_type;
-       my $switch_dissect;
-       my $switch_dt = getType($e->{SWITCH_TYPE});
-       if ($switch_dt->{DATA}->{TYPE} eq "ENUM") {
-               $switch_type = "g".Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
-               $switch_dissect = "dissect_ndr_" .Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
-       } elsif ($switch_dt->{DATA}->{TYPE} eq "SCALAR") {
-               $switch_type = "g$e->{SWITCH_TYPE}";
-               $switch_dissect = "dissect_ndr_$e->{SWITCH_TYPE}";
+       my $switch_type = undef;
+       my $switch_dissect = undef;
+       my $switch_raw_type = SwitchType($e, $e->{SWITCH_TYPE});
+       if (defined($switch_raw_type)) {
+               $switch_type = "g${switch_raw_type}";
+               $switch_dissect = "dissect_ndr_${switch_raw_type}";
        }
 
        $self->pidl_fn_start($dissectorname);
@@ -611,7 +782,11 @@ sub Union($$$$)
        $self->pidl_code("proto_item *item = NULL;");
        $self->pidl_code("proto_tree *tree = NULL;");
        $self->pidl_code("int old_offset;");
-       $self->pidl_code("$switch_type level;");
+       if (!defined $switch_type) {
+               $self->pidl_code("guint32 level = param;");
+       } else {
+               $self->pidl_code("$switch_type level;");
+       }
        $self->pidl_code("");
 
        $self->pidl_code("old_offset = offset;");
@@ -624,16 +799,20 @@ sub Union($$$$)
 
        $self->pidl_code("");
 
-       $self->pidl_code("offset = $switch_dissect(tvb, offset, pinfo, tree, drep, hf_index, &level);");
+       if (defined $switch_type) {
+               $self->pidl_code("offset = $switch_dissect(tvb, offset, pinfo, tree, drep, hf_index, &level);");
 
-       if ($e->{ALIGN} > 1) {
-               $self->pidl_code("ALIGN_TO_$e->{ALIGN}_BYTES;");
-               $self->pidl_code("");
+               if ($e->{ALIGN} > 1) {
+                       $self->pidl_code("ALIGN_TO_$e->{ALIGN}_BYTES;");
+                       $self->pidl_code("");
+               }
        }
 
 
        $self->pidl_code("switch(level) {$res\t}");
        $self->pidl_code("proto_item_set_len(item, offset-old_offset);\n");
+       $self->pidl_code("");
+
        $self->pidl_code("return offset;");
        $self->deindent;
        $self->pidl_code("}");
@@ -645,12 +824,12 @@ sub Union($$$$)
 sub Const($$$)
 {
        my ($self,$const,$ifname) = @_;
-       
+
        if (!defined($const->{ARRAY_LEN}[0])) {
-               $self->pidl_hdr("#define $const->{NAME}\t( $const->{VALUE} )\n");
-       } else {
-               $self->pidl_hdr("#define $const->{NAME}\t $const->{VALUE}\n");
-       }
+               $self->pidl_hdr("#define $const->{NAME}\t( $const->{VALUE} )\n");
+       } else {
+               $self->pidl_hdr("#define $const->{NAME}\t $const->{VALUE}\n");
+       }
 }
 
 sub Typedef($$$$)
@@ -665,13 +844,13 @@ sub Type($$$$)
        my ($self, $e, $name, $ifname) = @_;
 
        $self->PrintIdl(DumpType($e->{ORIGINAL}));
-
        {
                ENUM => \&Enum,
                STRUCT => \&Struct,
                UNION => \&Union,
                BITMAP => \&Bitmap,
-               TYPEDEF => \&Typedef
+               TYPEDEF => \&Typedef,
+               PIPE    => \&Pipe
        }->{$e->{TYPE}}->($self, $e, $name, $ifname);
 }
 
@@ -684,37 +863,39 @@ sub RegisterInterface($$)
        $self->pidl_code("{");
        $self->indent;
 
+       $self->{res}->{headers} .= "void proto_register_dcerpc_$x->{NAME}(void);\n";
+
        $self->{res}->{code}.=$self->DumpHfList()."\n";
        $self->{res}->{code}.="\n".DumpEttList($self->{ett})."\n";
-       
+
        if (defined($x->{UUID})) {
-           # These can be changed to non-pidl_code names if the old dissectors
-           # in epan/dissctors are deleted.
-    
-           my $name = uc($x->{NAME}) . " (pidl)";
-           my $short_name = uc($x->{NAME});
-           my $filter_name = $x->{NAME};
-
-           if (has_property($x, "helpstring")) {
-               $name = $x->{PROPERTIES}->{helpstring};
-           }
-
-           if (defined($self->{conformance}->{protocols}->{$x->{NAME}})) {
+               # These can be changed to non-pidl_code names if the old
+               # dissectors in epan/dissectors are deleted.
+
+               my $name = uc($x->{NAME}) . " (pidl)";
+               my $short_name = uc($x->{NAME});
+               my $filter_name = $x->{NAME};
+
+               if (has_property($x, "helpstring")) {
+                       $name = $x->{PROPERTIES}->{helpstring};
+               }
+
+               if (defined($self->{conformance}->{protocols}->{$x->{NAME}})) {
                $short_name = $self->{conformance}->{protocols}->{$x->{NAME}}->{SHORTNAME};
                $name = $self->{conformance}->{protocols}->{$x->{NAME}}->{LONGNAME};
                $filter_name = $self->{conformance}->{protocols}->{$x->{NAME}}->{FILTERNAME};
-           }
+               }
 
-           $self->pidl_code("proto_dcerpc_$x->{NAME} = proto_register_protocol(".make_str($name).", ".make_str($short_name).", ".make_str($filter_name).");");
-           
-           $self->pidl_code("proto_register_field_array(proto_dcerpc_$x->{NAME}, hf, array_length (hf));");
-           $self->pidl_code("proto_register_subtree_array(ett, array_length(ett));");
+               $self->pidl_code("proto_dcerpc_$x->{NAME} = proto_register_protocol(".make_str($name).", ".make_str($short_name).", ".make_str($filter_name).");");
+
+               $self->pidl_code("proto_register_field_array(proto_dcerpc_$x->{NAME}, hf, array_length (hf));");
+               $self->pidl_code("proto_register_subtree_array(ett, array_length(ett));");
        } else {
-           $self->pidl_code("proto_dcerpc = proto_get_id_by_filter_name(\"dcerpc\");");
-           $self->pidl_code("proto_register_field_array(proto_dcerpc, hf, array_length(hf));");
-           $self->pidl_code("proto_register_subtree_array(ett, array_length(ett));");
+               $self->pidl_code("proto_dcerpc = proto_get_id_by_filter_name(\"dcerpc\");");
+               $self->pidl_code("proto_register_field_array(proto_dcerpc, hf, array_length(hf));");
+               $self->pidl_code("proto_register_subtree_array(ett, array_length(ett));");
        }
-           
+
        $self->deindent;
        $self->pidl_code("}\n");
        $self->pidl_fn_end("proto_register_dcerpc_$x->{NAME}");
@@ -726,16 +907,18 @@ sub RegisterInterfaceHandoff($$)
 
        if (defined($x->{UUID})) {
                $self->pidl_fn_start("proto_reg_handoff_dcerpc_$x->{NAME}");
-           $self->pidl_code("void proto_reg_handoff_dcerpc_$x->{NAME}(void)");
-           $self->pidl_code("{");
-           $self->indent;
-           $self->pidl_code("dcerpc_init_uuid(proto_dcerpc_$x->{NAME}, ett_dcerpc_$x->{NAME},");
-           $self->pidl_code("\t&uuid_dcerpc_$x->{NAME}, ver_dcerpc_$x->{NAME},");
-           $self->pidl_code("\t$x->{NAME}_dissectors, hf_$x->{NAME}_opnum);");
-           $self->deindent;
-           $self->pidl_code("}");
+               $self->pidl_code("void proto_reg_handoff_dcerpc_$x->{NAME}(void)");
+               $self->pidl_code("{");
+               $self->indent;
+               $self->pidl_code("dcerpc_init_uuid(proto_dcerpc_$x->{NAME}, ett_dcerpc_$x->{NAME},");
+               $self->pidl_code("\t&uuid_dcerpc_$x->{NAME}, ver_dcerpc_$x->{NAME},");
+               $self->pidl_code("\t$x->{NAME}_dissectors, hf_$x->{NAME}_opnum);");
+               $self->deindent;
+               $self->pidl_code("}");
                $self->pidl_fn_end("proto_reg_handoff_dcerpc_$x->{NAME}");
 
+               $self->{res}->{headers} .= "void proto_reg_handoff_dcerpc_$x->{NAME}(void);\n";
+
                $self->{hf_used}->{"hf_$x->{NAME}_opnum"} = 1;
        }
 }
@@ -756,8 +939,8 @@ sub ProcessImport
        my @imports = @_;
        foreach (@imports) {
                next if($_ eq "security");
-               s/\.idl\"$//;
                s/^\"//;
+               s/\.idl"?$//;
                $self->pidl_hdr("#include \"packet-dcerpc-$_\.h\"");
        }
        $self->pidl_hdr("");
@@ -781,35 +964,34 @@ sub ProcessInterface($$)
        if (defined($x->{UUID})) {
                my $if_uuid = $x->{UUID};
 
-           $self->pidl_def("/* Version information */\n\n");
-           
-           $self->pidl_def("static e_uuid_t uuid_dcerpc_$x->{NAME} = {");
-           $self->pidl_def("\t0x" . substr($if_uuid, 1, 8) 
-               . ", 0x" . substr($if_uuid, 10, 4)
-           . ", 0x" . substr($if_uuid, 15, 4) . ",");
-           $self->pidl_def("\t{ 0x" . substr($if_uuid, 20, 2) 
+               $self->pidl_def("/* Version information */\n\n");
+
+               $self->pidl_def("static e_uuid_t uuid_dcerpc_$x->{NAME} = {");
+               $self->pidl_def("\t0x" . substr($if_uuid, 1, 8)
+               . ", 0x" . substr($if_uuid, 10, 4)
+               . ", 0x" . substr($if_uuid, 15, 4) . ",");
+               $self->pidl_def("\t{ 0x" . substr($if_uuid, 20, 2)
                . ", 0x" . substr($if_uuid, 22, 2)
-           . ", 0x" . substr($if_uuid, 25, 2)
-           . ", 0x" . substr($if_uuid, 27, 2)
-           . ", 0x" . substr($if_uuid, 29, 2)
-           . ", 0x" . substr($if_uuid, 31, 2)
-           . ", 0x" . substr($if_uuid, 33, 2)
-           . ", 0x" . substr($if_uuid, 35, 2) . " }");
-           $self->pidl_def("};");
-       
-           my $maj = $x->{VERSION};
-           $maj =~ s/\.(.*)$//g;
-           $self->pidl_def("static guint16 ver_dcerpc_$x->{NAME} = $maj;");
-           $self->pidl_def("");
+               . ", 0x" . substr($if_uuid, 25, 2)
+               . ", 0x" . substr($if_uuid, 27, 2)
+               . ", 0x" . substr($if_uuid, 29, 2)
+               . ", 0x" . substr($if_uuid, 31, 2)
+               . ", 0x" . substr($if_uuid, 33, 2)
+               . ", 0x" . substr($if_uuid, 35, 2) . " }");
+               $self->pidl_def("};");
+
+               my $maj = 0x0000FFFF & $x->{VERSION};
+               $maj =~ s/\.(.*)$//g;
+               $self->pidl_def("static guint16 ver_dcerpc_$x->{NAME} = $maj;");
+               $self->pidl_def("");
        }
 
        $return_types{$x->{NAME}} = {};
 
        $self->Interface($x);
-
        $self->pidl_code("\n".DumpFunctionTable($x));
 
-       foreach (keys %{$return_types{$x->{NAME}}}) {
+       foreach (sort(keys %{$return_types{$x->{NAME}}})) {
                my ($type, $desc) = @{$return_types{$x->{NAME}}->{$_}};
                my $dt = $self->find_type($type);
                $dt or die("Unable to find information about return type `$type'");
@@ -854,17 +1036,19 @@ sub Initialize($$)
 
        $self->{conformance} = {
                imports => {},
-               header_fields=> {} 
+               header_fields=> {}
        };
 
        ReadConformance($cnf_file, $self->{conformance}) or print STDERR "warning: No conformance file `$cnf_file'\n";
-       
+
        foreach my $bytes (qw(1 2 4 8)) {
                my $bits = $bytes * 8;
                $self->register_type("uint$bits", "offset = PIDL_dissect_uint$bits(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_UINT$bits", "BASE_DEC", 0, "NULL", $bytes);
                $self->register_type("int$bits", "offset = PIDL_dissect_uint$bits(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_INT$bits", "BASE_DEC", 0, "NULL", $bytes);
        }
-               
+
+       $self->register_type("uint3264", "offset = dissect_ndr_uint3264(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);", "FT_UINT32", "BASE_DEC", 0, "NULL", 8);
+       $self->register_type("hyper", "offset = dissect_ndr_uint64(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);", "FT_UINT64", "BASE_DEC", 0, "NULL", 8);
        $self->register_type("udlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);", "FT_UINT64", "BASE_DEC", 0, "NULL", 4);
        $self->register_type("bool8", "offset = PIDL_dissect_uint8(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
        $self->register_type("char", "offset = PIDL_dissect_uint8(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
@@ -872,21 +1056,29 @@ sub Initialize($$)
        $self->register_type("dlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT64", "BASE_DEC", 0, "NULL", 8);
        $self->register_type("GUID", "offset = dissect_ndr_uuid_t(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_GUID", "BASE_NONE", 0, "NULL", 4);
        $self->register_type("policy_handle", "offset = PIDL_dissect_policy_hnd(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_BYTES", "BASE_NONE", 0, "NULL", 4);
-       $self->register_type("NTTIME", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
-       $self->register_type("NTTIME_hyper", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
-       $self->register_type("time_t", "offset = dissect_ndr_time_t(tvb, offset, pinfo,tree, drep, \@HF\@, NULL);","FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
-       $self->register_type("NTTIME_1sec", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);", "FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
-       $self->register_type("SID", "
+       $self->register_type("NTTIME", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", 0, "NULL", 4);
+       $self->register_type("NTTIME_hyper", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", 0, "NULL", 4);
+       $self->register_type("time_t", "offset = dissect_ndr_time_t(tvb, offset, pinfo,tree, drep, \@HF\@, NULL);","FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", 0, "NULL", 4);
+       $self->register_type("NTTIME_1sec", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);", "FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", 0, "NULL", 4);
+       $self->register_type("dom_sid28", "{
+               dcerpc_info *di = (dcerpc_info *)pinfo->private_data;
+               di->hf_index = \@HF\@;
+
+               offset = dissect_ndr_nt_SID28(tvb, offset, pinfo, tree, drep);
+       }", "FT_STRING", "BASE_NONE", 0, "NULL", 4);
+       $self->register_type("SID", "{
                dcerpc_info *di = (dcerpc_info *)pinfo->private_data;
 
                di->hf_index = \@HF\@;
 
                offset = dissect_ndr_nt_SID_with_options(tvb, offset, pinfo, tree, drep, param);
-       ","FT_STRING", "BASE_NONE", 0, "NULL", 4);
-       $self->register_type("WERROR", 
+       }", "FT_STRING", "BASE_NONE", 0, "NULL", 4);
+       $self->register_type("WERROR",
                "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_UINT32", "BASE_DEC", 0, "VALS(WERR_errors)", 4);
-       $self->register_type("NTSTATUS", 
+       $self->register_type("NTSTATUS",
                "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_UINT32", "BASE_DEC", 0, "VALS(NT_errors)", 4);
+       $self->register_type("ipv6address", "proto_tree_add_item(tree, \@HF\@, tvb, offset, 16, ENC_NA); offset += 16;", "FT_IPv6", "BASE_NONE", 0, "NULL", 16);
+       $self->register_type("ipv4address", "proto_tree_add_item(tree, \@HF\@, tvb, offset, 4, ENC_BIG_ENDIAN); offset += 4;", "FT_IPv4", "BASE_NONE", 0, "NULL", 4);
 
 }
 
@@ -895,19 +1087,19 @@ sub Initialize($$)
 sub Parse($$$$$)
 {
        my($self,$ndr,$idl_file,$h_filename,$cnf_file) = @_;
-       
+
        $self->Initialize($cnf_file);
 
        return (undef, undef) if defined($self->{conformance}->{noemit_dissector});
 
-       my $notice = 
+       my $notice =
 "/* DO NOT EDIT
        This filter was automatically generated
        from $idl_file and $cnf_file.
-       
-       Pidl is a perl based IDL compiler for DCE/RPC idl files. 
+
+       Pidl is a perl based IDL compiler for DCE/RPC idl files.
        It is maintained by the Samba team, not the Wireshark team.
-       Instructions on how to download and install Pidl can be 
+       Instructions on how to download and install Pidl can be
        found at http://wiki.wireshark.org/Pidl
 */
 
@@ -916,9 +1108,7 @@ sub Parse($$$$$)
        $self->pidl_hdr($notice);
 
        $self->{res}->{headers} = "\n";
-       $self->{res}->{headers} .= "#ifdef HAVE_CONFIG_H\n";
        $self->{res}->{headers} .= "#include \"config.h\"\n";
-       $self->{res}->{headers} .= "#endif\n\n";
 
        $self->{res}->{headers} .= "#ifdef _MSC_VER\n";
        $self->{res}->{headers} .= "#pragma warning(disable:4005)\n";
@@ -938,12 +1128,12 @@ sub Parse($$$$$)
        my $h_basename = basename($h_filename);
 
        $self->{res}->{headers} .= "#include \"$h_basename\"\n";
+
        $self->pidl_code("");
 
        if (defined($self->{conformance}->{ett})) {
                register_ett($self,$_) foreach(@{$self->{conformance}->{ett}})
        }
-
        # Wireshark protocol registration
 
        foreach (@$ndr) {
@@ -969,7 +1159,7 @@ sub Parse($$$$$)
        $header.=$self->{res}->{hdr};
 
        $self->CheckUsed($self->{conformance});
-    
+
        return ($parser,$header);
 }
 
@@ -981,7 +1171,7 @@ sub register_ett($$)
 {
        my ($self, $name) = @_;
 
-       push (@{$self->{ett}}, $name);  
+       push (@{$self->{ett}}, $name);
 }
 
 sub DumpEttList
@@ -1010,7 +1200,7 @@ sub DumpEttDeclaration
 # HF
 ###############################################################################
 
-sub register_hf_field($$$$$$$$$) 
+sub register_hf_field($$$$$$$$$)
 {
        my ($self,$index,$name,$filter_name,$ft_type,$base_type,$valsstring,$mask,$blurb) = @_;
 
@@ -1030,9 +1220,9 @@ sub register_hf_field($$$$$$$$$)
                BLURB => $blurb
        };
 
-       if ((not defined($blurb) or $blurb eq "") and 
+       if ((not defined($blurb) or $blurb eq "") and
                        defined($self->{conformance}->{fielddescription}->{$index})) {
-               $self->{conformance}->{header_fields}->{$index}->{BLURB} = 
+               $self->{conformance}->{header_fields}->{$index}->{BLURB} =
                        $self->{conformance}->{fielddescription}->{$index}->{DESCRIPTION};
                $self->{conformance}->{fielddescription}->{$index}->{USED} = 1;
        }
@@ -1040,6 +1230,23 @@ sub register_hf_field($$$$$$$$$)
        return $index;
 }
 
+sub change_hf_field_type($$$$)
+{
+       my ($self,$index,$ft_type,$base_type) = @_;
+       if (defined ($self->{conformance}->{hf_renames}->{$index})) {
+               print "Field $index has been renamed to ".$self->{conformance}->{hf_renames}->{$index}->{NEWNAME}." you can't change it's type";
+               return 0;
+       }
+
+       if (!defined ($self->{conformance}->{header_fields}->{$index})) {
+               print "Field $index doesn't exists";
+               return 0;
+       }
+       $self->{conformance}->{header_fields}->{$index}->{FT_TYPE} = $ft_type;
+       $self->{conformance}->{header_fields}->{$index}->{BASE_TYPE} = $base_type;
+       return 1;
+}
+
 sub DumpHfDeclaration($)
 {
        my ($self) = @_;
@@ -1047,7 +1254,7 @@ sub DumpHfDeclaration($)
 
        $res = "\n/* Header field declarations */\n";
 
-       foreach (keys %{$self->{conformance}->{header_fields}}) 
+       foreach (sort(keys %{$self->{conformance}->{header_fields}}))
        {
                $res .= "static gint $_ = -1;\n";
        }
@@ -1057,16 +1264,16 @@ sub DumpHfDeclaration($)
 
 sub make_str_or_null($)
 {
-      my $str = shift;
-      if (substr($str, 0, 1) eq "\"") {
-              $str = substr($str, 1, length($str)-2);
-      }
-      $str =~ s/^\s*//;
-      $str =~ s/\s*$//;
-      if ($str eq "") {
-              return "NULL";
-      }
-      return make_str($str);
+       my $str = shift;
+       if (substr($str, 0, 1) eq "\"") {
+               $str = substr($str, 1, length($str)-2);
+       }
+       $str =~ s/^\s*//;
+       $str =~ s/\s*$//;
+       if ($str eq "") {
+               return "NULL";
+       }
+       return make_str($str);
 }
 
 sub DumpHfList($)
@@ -1074,9 +1281,9 @@ sub DumpHfList($)
        my ($self) = @_;
        my $res = "\tstatic hf_register_info hf[] = {\n";
 
-       foreach (values %{$self->{conformance}->{header_fields}}) 
+       foreach (sort {$a->{INDEX} cmp $b->{INDEX}} values %{$self->{conformance}->{header_fields}})
        {
-               $res .= "\t{ &$_->{INDEX}, 
+               $res .= "\t{ &$_->{INDEX},
          { ".make_str($_->{NAME}).", ".make_str($_->{FILTER}).", $_->{FT_TYPE}, $_->{BASE_TYPE}, $_->{VALSSTRING}, $_->{MASK}, ".make_str_or_null($_->{BLURB}).", HFILL }},
 ";
        }
@@ -1095,7 +1302,7 @@ sub DumpFunctionTable($)
 
        my $res = "static dcerpc_sub_dissector $if->{NAME}\_dissectors[] = {\n";
        foreach (@{$if->{FUNCTIONS}}) {
-               my $fn_name = $_->{NAME};
+               my $fn_name = $_->{NAME};
                $fn_name =~ s/^$if->{NAME}_//;
                $res.= "\t{ $_->{OPNUM}, \"$fn_name\",\n";
                $res.= "\t   $if->{NAME}_dissect_${fn_name}_request, $if->{NAME}_dissect_${fn_name}_response},\n";