Finish removal of iconv_convenience in public API's.
[bbaumbach/samba-autobuild/.git] / pidl / lib / Parse / Pidl / Samba4 / NDR / Parser.pm
index fb73075f1ad218f07880989860eeb86c85627f83..83bca288edee8bc760cb82b5943cd48849031382 100644 (file)
@@ -16,7 +16,7 @@ use Parse::Pidl::Typelist qw(hasType getType mapTypeName typeHasBody);
 use Parse::Pidl::Util qw(has_property ParseExpr ParseExprExt print_uuid unmake_str);
 use Parse::Pidl::CUtil qw(get_pointer_to get_value_of get_array_element);
 use Parse::Pidl::NDR qw(GetPrevLevel GetNextLevel ContainsDeferred is_charset_array);
-use Parse::Pidl::Samba4 qw(is_intree choose_header);
+use Parse::Pidl::Samba4 qw(is_intree choose_header ArrayDynamicallyAllocated);
 use Parse::Pidl::Samba4::Header qw(GenerateFunctionInEnv GenerateFunctionOutEnv EnvSubstituteValue GenerateStructEnv);
 use Parse::Pidl qw(warning);
 
@@ -204,12 +204,12 @@ 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;
@@ -326,7 +326,7 @@ sub ParseArrayPullHeader($$$$$$)
 
        if ($l->{IS_CONFORMANT}) {
                $length = $size = "ndr_get_array_size($ndr, " . get_pointer_to($var_name) . ")";
-       } elsif ($l->{IS_ZERO_TERMINATED}) { # Noheader arrays
+       } elsif ($l->{IS_ZERO_TERMINATED} and $l->{SIZE_IS} == 0 and $l->{LENGTH_IS} == 0) { # Noheader arrays
                $length = $size = "ndr_get_string_size($ndr, sizeof(*$var_name))";
        } else {
                $length = $size = ParseExprExt($l->{SIZE_IS}, $env, $e->{ORIGINAL},
@@ -376,7 +376,7 @@ sub ParseArrayPullHeader($$$$$$)
                $self->defer("}");
        }
 
-       if (not $l->{IS_FIXED} and not is_charset_array($e, $l)) {
+       if (ArrayDynamicallyAllocated($e,$l) and not is_charset_array($e,$l)) {
                $self->AllocateArrayLevel($e,$l,$ndr,$var_name,$size);
        }
 
@@ -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("}");
                }
@@ -586,9 +593,15 @@ sub ParseElementPushLevel
                my $length = ParseExpr($l->{LENGTH_IS}, $env, $e->{ORIGINAL});
                my $counter = "cntr_$e->{NAME}_$l->{LEVEL_INDEX}";
 
+               my $array_pointless = ($length eq "0");
+
+               if ($array_pointless) {
+                       warning($e->{ORIGINAL}, "pointless array `$e->{NAME}' will always have size 0");
+               }
+
                $var_name = get_array_element($var_name, $counter);
 
-               if (($primitives and not $l->{IS_DEFERRED}) or ($deferred and $l->{IS_DEFERRED})) {
+               if ((($primitives and not $l->{IS_DEFERRED}) or ($deferred and $l->{IS_DEFERRED})) and not $array_pointless) {
                        $self->pidl("for ($counter = 0; $counter < $length; $counter++) {");
                        $self->indent;
                        $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, 1, 0);
@@ -596,7 +609,7 @@ sub ParseElementPushLevel
                        $self->pidl("}");
                }
 
-               if ($deferred and ContainsDeferred($e, $l)) {
+               if ($deferred and ContainsDeferred($e, $l) and not $array_pointless) {
                        $self->pidl("for ($counter = 0; $counter < $length; $counter++) {");
                        $self->indent;
                        $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, 0, 1);
@@ -664,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") {
@@ -857,8 +872,16 @@ sub ParseDataPull($$$$$$$)
 
                if (my $range = has_property($e, "range")) {
                        $var_name = get_value_of($var_name);
+                       my $signed = Parse::Pidl::Typelist::is_signed($l->{DATA_TYPE});
                        my ($low, $high) = split(/,/, $range, 2);
-                       $self->pidl("if ($var_name < $low || $var_name > $high) {");
+                       if ($low < 0 and not $signed) {
+                               warning(0, "$low is invalid for the range of an unsigned type");
+                       }
+                       if ($low == 0 and not $signed) {
+                               $self->pidl("if ($var_name > $high) {");
+                       } else {
+                               $self->pidl("if ($var_name < $low || $var_name > $high) {");
+                       }
                        $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_RANGE, \"value out of range\");");
                        $self->pidl("}");
                }
@@ -917,7 +940,7 @@ sub ParseMemCtxPullFlags($$$$)
 
        return undef unless ($l->{TYPE} eq "POINTER" or $l->{TYPE} eq "ARRAY");
 
-       return undef if ($l->{TYPE} eq "ARRAY" and $l->{IS_FIXED});
+       return undef unless ($l->{TYPE} ne "ARRAY" or ArrayDynamicallyAllocated($e,$l));
        return undef if has_fast_array($e, $l);
        return undef if is_charset_array($e, $l);
 
@@ -1024,7 +1047,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));");
@@ -1039,7 +1062,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;
@@ -1051,6 +1079,10 @@ sub ParseElementPullLevel
                my $counter = "cntr_$e->{NAME}_$l->{LEVEL_INDEX}";
                my $array_name = $var_name;
 
+               if ($l->{IS_VARYING}) {
+                       $length = "ndr_get_array_length($ndr, " . get_pointer_to($var_name) .")";
+               }
+
                $var_name = get_array_element($var_name, $counter);
 
                $self->ParseMemCtxPullStart($e, $l, $ndr, $array_name);
@@ -1147,6 +1179,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}");
        }
@@ -1167,7 +1201,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;
@@ -1202,9 +1236,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})));");
                }
        }
 
@@ -1217,6 +1251,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($$$$)
@@ -1242,7 +1278,7 @@ sub ParseStructPush($$$$)
 
        EnvSubstituteValue($env, $struct);
 
-       $self->DeclareArrayVariables($_) foreach (@{$struct->{ELEMENTS}});
+       $self->DeclareArrayVariablesNoZero($_, $env) foreach (@{$struct->{ELEMENTS}});
 
        $self->start_flags($struct, $ndr);
 
@@ -1269,7 +1305,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);
 }
 
@@ -1283,7 +1319,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);
@@ -1446,9 +1482,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;
                }
        }
@@ -1467,6 +1507,24 @@ sub DeclareArrayVariables($$)
        }
 }
 
+sub DeclareArrayVariablesNoZero($$$)
+{
+       my ($self,$e,$env) = @_;
+
+       foreach my $l (@{$e->{LEVELS}}) {
+               next if has_fast_array($e,$l);
+               next if is_charset_array($e,$l);
+               if ($l->{TYPE} eq "ARRAY") {
+                   my $length = ParseExpr($l->{LENGTH_IS}, $env, $e->{ORIGINAL});
+                   if ($length eq "0") {
+                       warning($e->{ORIGINAL}, "pointless array cntr: 'cntr_$e->{NAME}_$l->{LEVEL_INDEX}': length=$length");
+                   } else {
+                       $self->pidl("uint32_t cntr_$e->{NAME}_$l->{LEVEL_INDEX};");
+                   }
+               }
+       }
+}
+
 sub DeclareMemCtxVariables($$)
 {
        my ($self,$e) = @_;
@@ -1497,6 +1555,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($$$$$)
@@ -1608,6 +1668,10 @@ sub ParseUnionPushPrimitives($$$$)
                $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}));");
+       }
+
        $self->pidl("switch (level) {");
        $self->indent;
        foreach my $el (@{$e->{ELEMENTS}}) {
@@ -1633,7 +1697,7 @@ sub ParseUnionPushPrimitives($$$$)
        }
        if (! $have_default) {
                $self->pidl("default:");
-               $self->pidl("\treturn ndr_push_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);");
+               $self->pidl("\treturn ndr_push_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u at \%s\", level, __location__);");
        }
        $self->deindent;
        $self->pidl("}");
@@ -1669,7 +1733,7 @@ sub ParseUnionPushDeferred($$$$)
        }
        if (! $have_default) {
                $self->pidl("default:");
-               $self->pidl("\treturn ndr_push_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);");
+               $self->pidl("\treturn ndr_push_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u at \%s\", level, __location__);");
        }
        $self->deindent;
        $self->pidl("}");
@@ -1748,10 +1812,14 @@ sub ParseUnionPullPrimitives($$$$$)
        if (defined($switch_type)) {
                $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\", _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})) {
+               $self->pidl("NDR_CHECK(ndr_pull_union_align($ndr, $e->{ALIGN}));");
+       }
+
        $self->pidl("switch (level) {");
        $self->indent;
        foreach my $el (@{$e->{ELEMENTS}}) {
@@ -1778,7 +1846,7 @@ sub ParseUnionPullPrimitives($$$$$)
        }
        if (! $have_default) {
                $self->pidl("default:");
-               $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);");
+               $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u at \%s\", level, __location__);");
        }
        $self->deindent;
        $self->pidl("}");
@@ -1812,7 +1880,7 @@ sub ParseUnionPullDeferred($$$$)
        }
        if (! $have_default) {
                $self->pidl("default:");
-               $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);");
+               $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u at \%s\", level, __location__);");
        }
        $self->deindent;
        $self->pidl("}");
@@ -2087,7 +2155,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;
        }
@@ -2228,7 +2296,7 @@ sub AuthServiceStruct($$$)
 sub FunctionCallEntry($$)
 {
        my ($self, $d) = @_;
-       return if not defined($d->{OPNUM});
+       return if not defined($d->{OPNUM});
        $self->pidl("\t{");
        $self->pidl("\t\t\"$d->{NAME}\",");
        $self->pidl("\t\tsizeof(struct $d->{NAME}),");
@@ -2237,6 +2305,7 @@ sub FunctionCallEntry($$)
        $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;
 }
 
 #####################################################################
@@ -2253,8 +2322,7 @@ sub FunctionTable($$)
        $self->pidl("static const struct ndr_interface_call $interface->{NAME}\_calls[] = {");
 
        foreach my $d (@{$interface->{INHERITED_FUNCTIONS}},@{$interface->{FUNCTIONS}}) {
-               $self->FunctionCallEntry($d);
-               $count++;
+               $count += $self->FunctionCallEntry($d);
        }
        $self->pidl("\t{ NULL, 0, NULL, NULL, NULL, false }");
        $self->pidl("};");
@@ -2540,7 +2608,9 @@ sub GenerateIncludes($)
        if (is_intree()) {
                $self->pidl("#include \"includes.h\"");
        } else {
+               $self->pidl("#ifndef _GNU_SOURCE");
                $self->pidl("#define _GNU_SOURCE");
+               $self->pidl("#endif");
                $self->pidl("#include <stdint.h>");
                $self->pidl("#include <stdlib.h>");
                $self->pidl("#include <stdio.h>");