pidl:Samba4/NDR/Parser: add support for 'ms_union' style aligment
[samba.git] / pidl / lib / Parse / Pidl / Samba4 / NDR / Parser.pm
index cc8085a54fdb577290c7c30cc4a3e49e35803bcb..69c13866250f6d997da62a32e7a36d1e6a50fc4a 100644 (file)
@@ -204,13 +204,13 @@ sub ParseArrayPushHeader($$$$$$)
        }
 
        if ((!$l->{IS_SURROUNDING}) and $l->{IS_CONFORMANT}) {
-               $self->pidl("NDR_CHECK(ndr_push_uint32($ndr, NDR_SCALARS, $size));");
+               $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, $size));");
        }
-       
+
        if ($l->{IS_VARYING}) {
-               $self->pidl("NDR_CHECK(ndr_push_uint32($ndr, NDR_SCALARS, 0));");  # array offset
-               $self->pidl("NDR_CHECK(ndr_push_uint32($ndr, NDR_SCALARS, $length));");
-       } 
+               $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;
 }
@@ -567,17 +567,24 @@ sub ParseElementPushLevel
        }
 
        if ($l->{TYPE} eq "POINTER" and $deferred) {
+               my $rel_var_name = $var_name;
                if ($l->{POINTER_TYPE} ne "ref") {
                        $self->pidl("if ($var_name) {");
                        $self->indent;
                        if ($l->{POINTER_TYPE} eq "relative") {
-                               $self->pidl("NDR_CHECK(ndr_push_relative_ptr2($ndr, $var_name));");
+                               $self->pidl("NDR_CHECK(ndr_push_relative_ptr2_start($ndr, $rel_var_name));");
+                       }
+                       if ($l->{POINTER_TYPE} eq "relative_short") {
+                               $self->pidl("NDR_CHECK(ndr_push_short_relative_ptr2($ndr, $var_name));");
                        }
                }
                $var_name = get_value_of($var_name);
                $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, 1, 1);
 
                if ($l->{POINTER_TYPE} ne "ref") {
+                       if ($l->{POINTER_TYPE} eq "relative") {
+                               $self->pidl("NDR_CHECK(ndr_push_relative_ptr2_end($ndr, $rel_var_name));");
+                       }
                        $self->deindent;
                        $self->pidl("}");
                }
@@ -670,6 +677,8 @@ sub ParsePtrPush($$$$$)
                }
        } elsif ($l->{POINTER_TYPE} eq "relative") {
                $self->pidl("NDR_CHECK(ndr_push_relative_ptr1($ndr, $var_name));");
+       } elsif ($l->{POINTER_TYPE} eq "relative_short") {
+               $self->pidl("NDR_CHECK(ndr_push_short_relative_ptr1($ndr, $var_name));");
        } elsif ($l->{POINTER_TYPE} eq "unique") {
                $self->pidl("NDR_CHECK(ndr_push_unique_ptr($ndr, $var_name));");
        } elsif ($l->{POINTER_TYPE} eq "full") {
@@ -782,9 +791,6 @@ sub ParseElementPrint($$$$$)
                                $self->pidl("$ndr->depth++;");
                                $self->pidl("for ($counter=0;$counter<$length;$counter++) {");
                                $self->indent;
-                               $self->pidl("char *idx_$l->{LEVEL_INDEX}=NULL;");
-                               $self->pidl("if (asprintf(&idx_$l->{LEVEL_INDEX}, \"[\%d]\", $counter) != -1) {");
-                               $self->indent;
 
                                $var_name = get_array_element($var_name, $counter);
                        }
@@ -807,9 +813,6 @@ sub ParseElementPrint($$$$$)
                } elsif (($l->{TYPE} eq "ARRAY")
                        and not is_charset_array($e,$l)
                        and not has_fast_array($e,$l)) {
-                       $self->pidl("free(idx_$l->{LEVEL_INDEX});");
-                       $self->deindent;
-                       $self->pidl("}");
                        $self->deindent;
                        $self->pidl("}");
                        $self->pidl("$ndr->depth--;");
@@ -861,7 +864,10 @@ sub ParseDataPull($$$$$$$)
 
                $self->pidl("NDR_CHECK(".TypeFunctionName("ndr_pull", $l->{DATA_TYPE})."($ndr, $ndr_flags, $var_name));");
 
-               if (my $range = has_property($e, "range")) {
+               my $pl = GetPrevLevel($e, $l);
+
+               my $range = has_property($e, "range");
+               if ($range and $pl->{TYPE} ne "ARRAY") {
                        $var_name = get_value_of($var_name);
                        my $signed = Parse::Pidl::Typelist::is_signed($l->{DATA_TYPE});
                        my ($low, $high) = split(/,/, $range, 2);
@@ -939,12 +945,10 @@ sub ParseMemCtxPullFlags($$$$)
 
        if (($l->{TYPE} eq "POINTER") and ($l->{POINTER_TYPE} eq "ref")) {
                my $nl = GetNextLevel($e, $l);
-               my $next_is_array = ($nl->{TYPE} eq "ARRAY");
-               my $next_is_string = (($nl->{TYPE} eq "DATA") and 
-                                       ($nl->{DATA_TYPE} eq "string"));
-               if ($next_is_array or $next_is_string) {
-                       return undef;
-               } elsif ($l->{LEVEL} eq "TOP") {
+               return undef if ($nl->{TYPE} eq "ARRAY");
+               return undef if (($nl->{TYPE} eq "DATA") and ($nl->{DATA_TYPE} eq "string"));
+
+               if ($l->{LEVEL} eq "TOP") {
                        $mem_flags = "LIBNDR_FLAG_REF_ALLOC";
                }
        }
@@ -1006,6 +1010,20 @@ sub ParseElementPullLevel
                } elsif ($l->{TYPE} eq "ARRAY") {
                        my $length = $self->ParseArrayPullHeader($e, $l, $ndr, $var_name, $env);
 
+                       if (my $range = has_property($e, "range")) {
+                               my ($low, $high) = split(/,/, $range, 2);
+                               if ($low < 0) {
+                                       warning(0, "$low is invalid for the range of an array size");
+                               }
+                               if ($low == 0) {
+                                       $self->pidl("if ($length > $high) {");
+                               } else {
+                                       $self->pidl("if ($length < $low || $length > $high) {");
+                               }
+                               $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_RANGE, \"value out of range\");");
+                               $self->pidl("}");
+                       }
+
                        my $nl = GetNextLevel($e, $l);
 
                        if (is_charset_array($e,$l)) {
@@ -1038,7 +1056,7 @@ sub ParseElementPullLevel
                        $self->pidl("if ($var_name) {");
                        $self->indent;
 
-                       if ($l->{POINTER_TYPE} eq "relative") {
+                       if ($l->{POINTER_TYPE} eq "relative" or $l->{POINTER_TYPE} eq "relative_short") {
                                $self->pidl("uint32_t _relative_save_offset;");
                                $self->pidl("_relative_save_offset = $ndr->offset;");
                                $self->pidl("NDR_CHECK(ndr_pull_relative_ptr2($ndr, $var_name));");
@@ -1053,7 +1071,12 @@ sub ParseElementPullLevel
                $self->ParseMemCtxPullEnd($e, $l, $ndr);
 
                if ($l->{POINTER_TYPE} ne "ref") {
-                       if ($l->{POINTER_TYPE} eq "relative") {
+                       if ($l->{POINTER_TYPE} eq "relative") {
+                               $self->pidl("if ($ndr->offset > $ndr->relative_highest_offset) {");
+                               $self->indent;
+                               $self->pidl("$ndr->relative_highest_offset = $ndr->offset;");
+                               $self->deindent;
+                               $self->pidl("}");
                                $self->pidl("$ndr->offset = _relative_save_offset;");
                        }
                        $self->deindent;
@@ -1069,6 +1092,20 @@ sub ParseElementPullLevel
                        $length = "ndr_get_array_length($ndr, " . get_pointer_to($var_name) .")";
                }
 
+               if (my $range = has_property($e, "range")) {
+                       my ($low, $high) = split(/,/, $range, 2);
+                       if ($low < 0) {
+                               warning(0, "$low is invalid for the range of an array size");
+                       }
+                       if ($low == 0) {
+                               $self->pidl("if ($length > $high) {");
+                       } else {
+                               $self->pidl("if ($length < $low || $length > $high) {");
+                       }
+                       $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_RANGE, \"value out of range\");");
+                       $self->pidl("}");
+               }
+
                $var_name = get_array_element($var_name, $counter);
 
                $self->ParseMemCtxPullStart($e, $l, $ndr, $array_name);
@@ -1157,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}));");
@@ -1165,6 +1202,8 @@ sub ParsePtrPull($$$$$)
                 ($l->{POINTER_TYPE} eq "relative") or
                 ($l->{POINTER_TYPE} eq "full")) {
                $self->pidl("NDR_CHECK(ndr_pull_generic_ptr($ndr, &_ptr_$e->{NAME}));");
+       } elsif ($l->{POINTER_TYPE} eq "relative_short") {
+               $self->pidl("NDR_CHECK(ndr_pull_relative_ptr_short($ndr, &_ptr_$e->{NAME}));");
        } else {
                die("Unhandled pointer type $l->{POINTER_TYPE}");
        }
@@ -1185,7 +1224,7 @@ sub ParsePtrPull($$$$$)
        }
 
        #$self->pidl("memset($var_name, 0, sizeof($var_name));");
-       if ($l->{POINTER_TYPE} eq "relative") {
+       if ($l->{POINTER_TYPE} eq "relative" or $l->{POINTER_TYPE} eq "relative_short") {
                $self->pidl("NDR_CHECK(ndr_pull_relative_ptr1($ndr, $var_name, _ptr_$e->{NAME}));");
        }
        $self->deindent;
@@ -1220,9 +1259,9 @@ sub ParseStructPushPrimitives($$$$$)
                                $size = ParseExpr($e->{LEVELS}[0]->{SIZE_IS}, $env, $e->{ORIGINAL});
                        }
 
-                       $self->pidl("NDR_CHECK(ndr_push_uint32($ndr, NDR_SCALARS, $size));");
+                       $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, $size));");
                } else {
-                       $self->pidl("NDR_CHECK(ndr_push_uint32($ndr, NDR_SCALARS, ndr_string_array_size($ndr, $varname->$e->{NAME})));");
+                       $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, ndr_string_array_size($ndr, $varname->$e->{NAME})));");
                }
        }
 
@@ -1235,6 +1274,8 @@ sub ParseStructPushPrimitives($$$$$)
        }
 
        $self->ParseElementPush($_, $ndr, $env, 1, 0) foreach (@{$struct->{ELEMENTS}});
+
+       $self->pidl("NDR_CHECK(ndr_push_trailer_align($ndr, $struct->{ALIGN}));");
 }
 
 sub ParseStructPushDeferred($$$$)
@@ -1287,7 +1328,7 @@ sub ParseEnumPush($$$$)
        my($type_fn) = $enum->{BASE_TYPE};
 
        $self->start_flags($enum, $ndr);
-       $self->pidl("NDR_CHECK(ndr_push_$type_fn($ndr, NDR_SCALARS, $varname));");
+       $self->pidl("NDR_CHECK(ndr_push_enum_$type_fn($ndr, NDR_SCALARS, $varname));");
        $self->end_flags($enum, $ndr);
 }
 
@@ -1301,7 +1342,7 @@ sub ParseEnumPull($$$$)
 
        $self->pidl("$type_v_decl v;");
        $self->start_flags($enum, $ndr);
-       $self->pidl("NDR_CHECK(ndr_pull_$type_fn($ndr, NDR_SCALARS, &v));");
+       $self->pidl("NDR_CHECK(ndr_pull_enum_$type_fn($ndr, NDR_SCALARS, &v));");
        $self->pidl("*$varname = v;");
 
        $self->end_flags($enum, $ndr);
@@ -1448,6 +1489,7 @@ sub ParseStructPrint($$$$$)
        $self->DeclareArrayVariables($_) foreach (@{$struct->{ELEMENTS}});
 
        $self->pidl("ndr_print_struct($ndr, name, \"$name\");");
+       $self->pidl("if (r == NULL) { ndr_print_null($ndr); return; }");
 
        $self->start_flags($struct, $ndr);
 
@@ -1464,9 +1506,13 @@ sub DeclarePtrVariables($$)
 {
        my ($self,$e) = @_;
        foreach my $l (@{$e->{LEVELS}}) {
+               my $size = 32;
                if ($l->{TYPE} eq "POINTER" and 
                        not ($l->{POINTER_TYPE} eq "ref" and $l->{LEVEL} eq "TOP")) {
-                       $self->pidl("uint32_t _ptr_$e->{NAME};");
+                       if ($l->{POINTER_TYPE} eq "relative_short") {
+                               $size = 16;
+                       }
+                       $self->pidl("uint${size}_t _ptr_$e->{NAME};");
                        last;
                }
        }
@@ -1533,6 +1579,8 @@ sub ParseStructPullPrimitives($$$$$)
        $self->ParseElementPull($_, $ndr, $env, 1, 0) foreach (@{$struct->{ELEMENTS}});
 
        $self->add_deferred();
+
+       $self->pidl("NDR_CHECK(ndr_pull_trailer_align($ndr, $struct->{ALIGN}));");
 }
 
 sub ParseStructPullDeferred($$$$$)
@@ -1594,7 +1642,7 @@ sub ParseStructNdrSize($$$$)
        if (my $flags = has_property($t, "flag")) {
                $self->pidl("flags |= $flags;");
        }
-       $self->pidl("return ndr_size_struct($varname, flags, (ndr_push_flags_fn_t)ndr_push_$name, ic);");
+       $self->pidl("return ndr_size_struct($varname, flags, (ndr_push_flags_fn_t)ndr_push_$name);");
 }
 
 sub DeclStruct($$$$)
@@ -1606,7 +1654,7 @@ sub DeclStruct($$$$)
 sub ArgsStructNdrSize($$$)
 {
        my ($d, $name, $varname) = @_;
-       return "const struct $name *$varname, struct smb_iconv_convenience *ic, int flags";
+       return "const struct $name *$varname, int flags";
 }
 
 $typefamily{STRUCT} = {
@@ -1629,7 +1677,7 @@ sub ParseUnionNdrSize($$$)
                $self->pidl("flags |= $flags;");
        }
 
-       $self->pidl("return ndr_size_union($varname, flags, level, (ndr_push_flags_fn_t)ndr_push_$name, ic);");
+       $self->pidl("return ndr_size_union($varname, flags, level, (ndr_push_flags_fn_t)ndr_push_$name);");
 }
 
 sub ParseUnionPushPrimitives($$$$)
@@ -1638,12 +1686,25 @@ 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})) {
+               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) {");
        $self->indent;
        foreach my $el (@{$e->{ELEMENTS}}) {
@@ -1681,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
@@ -1740,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);
        }
@@ -1781,13 +1842,27 @@ 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__);");
                $self->pidl("}");
        }
 
+       if (defined($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) {");
        $self->indent;
        foreach my $el (@{$e->{ELEMENTS}}) {
@@ -1863,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});
@@ -1909,7 +1984,7 @@ sub DeclUnion($$$$)
 sub ArgsUnionNdrSize($$)
 {
        my ($d,$name) = @_;
-       return "const union $name *r, uint32_t level, struct smb_iconv_convenience *ic, int flags";
+       return "const union $name *r, uint32_t level, int flags";
 }
 
 $typefamily{UNION} = {
@@ -2007,6 +2082,7 @@ sub ParseFunctionPrint($$)
        }
 
        $self->pidl("ndr_print_struct($ndr, name, \"$fn->{NAME}\");");
+       $self->pidl("if (r == NULL) { ndr_print_null($ndr); return; }");
        $self->pidl("$ndr->depth++;");
 
        $self->pidl("if (flags & NDR_SET_VALUES) {");
@@ -2123,7 +2199,7 @@ sub AllocateArrayLevel($$$$$$)
                $self->pidl("}");
                if (grep(/in/,@{$e->{DIRECTION}}) and
                    grep(/out/,@{$e->{DIRECTION}})) {
-                       $self->pidl("memcpy(r->out.$e->{NAME}, r->in.$e->{NAME}, $size * sizeof(*r->in.$e->{NAME}));");
+                       $self->pidl("memcpy(r->out.$e->{NAME}, r->in.$e->{NAME}, ($size) * sizeof(*r->in.$e->{NAME}));");
                }
                return;
        }
@@ -2271,7 +2347,6 @@ sub FunctionCallEntry($$)
        $self->pidl("\t\t(ndr_push_flags_fn_t) ndr_push_$d->{NAME},");
        $self->pidl("\t\t(ndr_pull_flags_fn_t) ndr_pull_$d->{NAME},");
        $self->pidl("\t\t(ndr_print_function_t) ndr_print_$d->{NAME},");
-       $self->pidl("\t\t".($d->{ASYNC}?"true":"false").",");
        $self->pidl("\t},");
        return 1;
 }
@@ -2292,7 +2367,7 @@ sub FunctionTable($$)
        foreach my $d (@{$interface->{INHERITED_FUNCTIONS}},@{$interface->{FUNCTIONS}}) {
                $count += $self->FunctionCallEntry($d);
        }
-       $self->pidl("\t{ NULL, 0, NULL, NULL, NULL, false }");
+       $self->pidl("\t{ NULL, 0, NULL, NULL, NULL }");
        $self->pidl("};");
        $self->pidl("");
 
@@ -2559,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);