pidl:Samba4/NDR/Parser: add support for 'ms_union' style aligment
[samba.git] / pidl / lib / Parse / Pidl / Samba4 / NDR / Parser.pm
index c76ef6bfcd502fbc2917de4bdf66e5c4c549caff..69c13866250f6d997da62a32e7a36d1e6a50fc4a 100644 (file)
@@ -206,11 +206,11 @@ sub ParseArrayPushHeader($$$$$$)
        if ((!$l->{IS_SURROUNDING}) and $l->{IS_CONFORMANT}) {
                $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, $size));");
        }
-       
+
        if ($l->{IS_VARYING}) {
                $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, 0));");  # array offset
                $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, $length));");
-       } 
+       }
 
        return $length;
 }
@@ -1194,7 +1194,7 @@ sub ParsePtrPull($$$$$)
                        $self->pidl("\tNDR_PULL_ALLOC($ndr, $var_name);"); 
                        $self->pidl("}");
                }
-               
+
                return;
        } elsif ($l->{POINTER_TYPE} eq "ref" and $l->{LEVEL} eq "EMBEDDED") {
                $self->pidl("NDR_CHECK(ndr_pull_ref_ptr($ndr, &_ptr_$e->{NAME}));");
@@ -1686,14 +1686,23 @@ sub ParseUnionPushPrimitives($$$$)
 
        my $have_default = 0;
 
-       $self->pidl("int level = ndr_push_get_switch_value($ndr, $varname);");
+       $self->pidl("uint32_t level = ndr_push_get_switch_value($ndr, $varname);");
 
        if (defined($e->{SWITCH_TYPE})) {
+               if (defined($e->{ALIGN})) {
+                       $self->pidl("NDR_CHECK(ndr_push_union_align($ndr, $e->{ALIGN}));");
+               }
+
                $self->pidl("NDR_CHECK(ndr_push_$e->{SWITCH_TYPE}($ndr, NDR_SCALARS, level));");
        }
 
        if (defined($e->{ALIGN})) {
-               $self->pidl("NDR_CHECK(ndr_push_union_align($ndr, $e->{ALIGN}));");
+               if ($e->{IS_MS_UNION}) {
+                       $self->pidl("/* ms_union is always aligned to the largest union arm*/");
+                       $self->pidl("NDR_CHECK(ndr_push_align($ndr, $e->{ALIGN}));");
+               } else {
+                       $self->pidl("NDR_CHECK(ndr_push_union_align($ndr, $e->{ALIGN}));");
+               }
        }
 
        $self->pidl("switch (level) {");
@@ -1733,7 +1742,7 @@ sub ParseUnionPushDeferred($$$$)
 
        my $have_default = 0;
 
-       $self->pidl("int level = ndr_push_get_switch_value($ndr, $varname);");
+       $self->pidl("uint32_t level = ndr_push_get_switch_value($ndr, $varname);");
        if (defined($e->{PROPERTIES}{relative_base})) {
                # retrieve the current offset as base for relative pointers
                # based on the toplevel struct/union
@@ -1792,7 +1801,7 @@ sub ParseUnionPrint($$$$$)
        my ($self,$e,$ndr,$name,$varname) = @_;
        my $have_default = 0;
 
-       $self->pidl("int level;");
+       $self->pidl("uint32_t level;");
        foreach my $el (@{$e->{ELEMENTS}}) {
                $self->DeclareArrayVariables($el);
        }
@@ -1833,7 +1842,12 @@ sub ParseUnionPullPrimitives($$$$$)
        my ($self,$e,$ndr,$varname,$switch_type) = @_;
        my $have_default = 0;
 
+
        if (defined($switch_type)) {
+               if (defined($e->{ALIGN})) {
+                       $self->pidl("NDR_CHECK(ndr_pull_union_align($ndr, $e->{ALIGN}));");
+               }
+
                $self->pidl("NDR_CHECK(ndr_pull_$switch_type($ndr, NDR_SCALARS, &_level));");
                $self->pidl("if (_level != level) {"); 
                $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value %u for $varname at \%s\", _level, __location__);");
@@ -1841,7 +1855,12 @@ sub ParseUnionPullPrimitives($$$$$)
        }
 
        if (defined($e->{ALIGN})) {
-               $self->pidl("NDR_CHECK(ndr_pull_union_align($ndr, $e->{ALIGN}));");
+               if ($e->{IS_MS_UNION}) {
+                       $self->pidl("/* ms_union is always aligned to the largest union arm*/");
+                       $self->pidl("NDR_CHECK(ndr_push_align($ndr, $e->{ALIGN}));");
+               } else {
+                       $self->pidl("NDR_CHECK(ndr_pull_union_align($ndr, $e->{ALIGN}));");
+               }
        }
 
        $self->pidl("switch (level) {");
@@ -1919,7 +1938,7 @@ sub ParseUnionPull($$$$)
        my ($self,$e,$ndr,$varname) = @_;
        my $switch_type = $e->{SWITCH_TYPE};
 
-       $self->pidl("int level;");
+       $self->pidl("uint32_t level;");
        if (defined($switch_type)) {
                if (Parse::Pidl::Typelist::typeIs($switch_type, "ENUM")) {
                        $switch_type = Parse::Pidl::Typelist::enum_type_fn(getType($switch_type)->{DATA});
@@ -2615,10 +2634,6 @@ sub ParseInterface($$$)
                ($needed->{"ndr_push_$d->{NAME}"}) && $self->ParseFunctionPush($d);
                ($needed->{"ndr_pull_$d->{NAME}"}) && $self->ParseFunctionPull($d);
                ($needed->{"ndr_print_$d->{NAME}"}) && $self->ParseFunctionPrint($d);
-
-               # Make sure we don't generate a function twice...
-               $needed->{"ndr_push_$d->{NAME}"} = $needed->{"ndr_pull_$d->{NAME}"} = 
-                       $needed->{"ndr_print_$d->{NAME}"} = 0;
        }
 
        $self->FunctionTable($interface);