Fix some warnings at higher -W levels in pidl generated code.
[kai/samba.git] / pidl / lib / Parse / Pidl / Samba4 / NDR / Parser.pm
1 ###################################################
2 # Samba4 NDR parser generator for IDL structures
3 # Copyright tridge@samba.org 2000-2003
4 # Copyright tpot@samba.org 2001
5 # Copyright jelmer@samba.org 2004-2006
6 # released under the GNU GPL
7
8 package Parse::Pidl::Samba4::NDR::Parser;
9
10 require Exporter;
11 @ISA = qw(Exporter);
12 @EXPORT_OK = qw(check_null_pointer NeededFunction NeededElement NeededType $res NeededInterface TypeFunctionName ParseElementPrint);
13
14 use strict;
15 use Parse::Pidl::Typelist qw(hasType getType mapTypeName typeHasBody);
16 use Parse::Pidl::Util qw(has_property ParseExpr ParseExprExt print_uuid unmake_str);
17 use Parse::Pidl::CUtil qw(get_pointer_to get_value_of get_array_element);
18 use Parse::Pidl::NDR qw(GetPrevLevel GetNextLevel ContainsDeferred is_charset_array);
19 use Parse::Pidl::Samba4 qw(is_intree choose_header ArrayDynamicallyAllocated);
20 use Parse::Pidl::Samba4::Header qw(GenerateFunctionInEnv GenerateFunctionOutEnv EnvSubstituteValue GenerateStructEnv);
21 use Parse::Pidl qw(warning);
22
23 use vars qw($VERSION);
24 $VERSION = '0.01';
25
26 # list of known types
27 my %typefamily;
28
29 sub new($$) {
30         my ($class) = @_;
31         my $self = { res => "", res_hdr => "", deferred => [], tabs => "", defer_tabs => "" };
32         bless($self, $class);
33 }
34
35 sub get_typefamily($)
36 {
37         my $n = shift;
38         return $typefamily{$n};
39 }
40
41 sub append_prefix($$)
42 {
43         my ($e, $var_name) = @_;
44         my $pointers = 0;
45         my $arrays = 0;
46
47         foreach my $l (@{$e->{LEVELS}}) {
48                 if ($l->{TYPE} eq "POINTER") {
49                         $pointers++;
50                 } elsif ($l->{TYPE} eq "ARRAY") {
51                         $arrays++;
52                         if (($pointers == 0) and 
53                             (not $l->{IS_FIXED}) and
54                             (not $l->{IS_INLINE})) {
55                                 return get_value_of($var_name);
56                         }
57                 } elsif ($l->{TYPE} eq "DATA") {
58                         if (Parse::Pidl::Typelist::scalar_is_reference($l->{DATA_TYPE})) {
59                                 return get_value_of($var_name) unless ($pointers or $arrays);
60                         }
61                 }
62         }
63         
64         return $var_name;
65 }
66
67 sub has_fast_array($$)
68 {
69         my ($e,$l) = @_;
70
71         return 0 if ($l->{TYPE} ne "ARRAY");
72
73         my $nl = GetNextLevel($e,$l);
74         return 0 unless ($nl->{TYPE} eq "DATA");
75         return 0 unless (hasType($nl->{DATA_TYPE}));
76
77         my $t = getType($nl->{DATA_TYPE});
78
79         # Only uint8 and string have fast array functions at the moment
80         return ($t->{NAME} eq "uint8") or ($t->{NAME} eq "string");
81 }
82
83
84 ####################################
85 # pidl() is our basic output routine
86 sub pidl($$)
87 {
88         my ($self, $d) = @_;
89         if ($d) {
90                 $self->{res} .= $self->{tabs};
91                 $self->{res} .= $d;
92         }
93         $self->{res} .="\n";
94 }
95
96 sub pidl_hdr($$) { my ($self, $d) = @_; $self->{res_hdr} .= "$d\n"; }
97
98 ####################################
99 # defer() is like pidl(), but adds to 
100 # a deferred buffer which is then added to the 
101 # output buffer at the end of the structure/union/function
102 # This is needed to cope with code that must be pushed back
103 # to the end of a block of elements
104 sub defer_indent($) { my ($self) = @_; $self->{defer_tabs}.="\t"; }
105 sub defer_deindent($) { my ($self) = @_; $self->{defer_tabs}=substr($self->{defer_tabs}, 0, -1); }
106
107 sub defer($$)
108 {
109         my ($self, $d) = @_;
110         if ($d) {
111                 push(@{$self->{deferred}}, $self->{defer_tabs}.$d);
112         }
113 }
114
115 ########################################
116 # add the deferred content to the current
117 # output
118 sub add_deferred($)
119 {
120         my ($self) = @_;
121         $self->pidl($_) foreach (@{$self->{deferred}});
122         $self->{deferred} = [];
123         $self->{defer_tabs} = "";
124 }
125
126 sub indent($)
127 {
128         my ($self) = @_;
129         $self->{tabs} .= "\t";
130 }
131
132 sub deindent($)
133 {
134         my ($self) = @_;
135         $self->{tabs} = substr($self->{tabs}, 0, -1);
136 }
137
138 #####################################################################
139 # declare a function public or static, depending on its attributes
140 sub fn_declare($$$$)
141 {
142         my ($self,$type,$fn,$decl) = @_;
143
144         if (has_property($fn, "no$type")) {
145                 $self->pidl_hdr("$decl;");
146                 return 0;
147         }
148
149         if (has_property($fn, "public")) {
150                 $self->pidl_hdr("$decl;");
151                 $self->pidl("_PUBLIC_ $decl");
152         } else {
153                 $self->pidl("static $decl");
154         }
155
156         return 1;
157 }
158
159 ###################################################################
160 # setup any special flags for an element or structure
161 sub start_flags($$$)
162 {
163         my ($self, $e, $ndr) = @_;
164         my $flags = has_property($e, "flag");
165         if (defined $flags) {
166                 $self->pidl("{");
167                 $self->indent;
168                 $self->pidl("uint32_t _flags_save_$e->{TYPE} = $ndr->flags;");
169                 $self->pidl("ndr_set_flags(&$ndr->flags, $flags);");
170         }
171 }
172
173 ###################################################################
174 # end any special flags for an element or structure
175 sub end_flags($$$)
176 {
177         my ($self, $e, $ndr) = @_;
178         my $flags = has_property($e, "flag");
179         if (defined $flags) {
180                 $self->pidl("$ndr->flags = _flags_save_$e->{TYPE};");
181                 $self->deindent;
182                 $self->pidl("}");
183         }
184 }
185
186 #####################################################################
187 # parse the data of an array - push side
188 sub ParseArrayPushHeader($$$$$$)
189 {
190         my ($self,$e,$l,$ndr,$var_name,$env) = @_;
191
192         my $size;
193         my $length;
194
195         if ($l->{IS_ZERO_TERMINATED}) {
196                 if (has_property($e, "charset")) {
197                         $size = $length = "ndr_charset_length($var_name, CH_$e->{PROPERTIES}->{charset})";
198                 } else {
199                         $size = $length = "ndr_string_length($var_name, sizeof(*$var_name))";
200                 }
201         } else {
202                 $size = ParseExpr($l->{SIZE_IS}, $env, $e);
203                 $length = ParseExpr($l->{LENGTH_IS}, $env, $e);
204         }
205
206         if ((!$l->{IS_SURROUNDING}) and $l->{IS_CONFORMANT}) {
207                 $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, $size));");
208         }
209         
210         if ($l->{IS_VARYING}) {
211                 $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, 0));");  # array offset
212                 $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, $length));");
213         } 
214
215         return $length;
216 }
217
218 sub check_fully_dereferenced($$)
219 {
220         my ($element, $env) = @_;
221
222         return sub ($) {
223                 my $origvar = shift;
224                 my $check = 0;
225
226                 # Figure out the number of pointers in $ptr
227                 my $expandedvar = $origvar;
228                 $expandedvar =~ s/^(\**)//;
229                 my $ptr = $1;
230
231                 my $var = undef;
232                 foreach (keys %$env) {
233                         if ($env->{$_} eq $expandedvar) {
234                                 $var = $_;
235                                 last;
236                         }
237                 }
238                 
239                 return($origvar) unless (defined($var));
240                 my $e;
241                 foreach (@{$element->{PARENT}->{ELEMENTS}}) {
242                         if ($_->{NAME} eq $var) {
243                                 $e = $_;
244                                 last;
245                         }
246                 }
247
248                 $e or die("Environment doesn't match siblings");
249
250                 # See if pointer at pointer level $level
251                 # needs to be checked.
252                 my $nump = 0;
253                 foreach (@{$e->{LEVELS}}) {
254                         if ($_->{TYPE} eq "POINTER") {
255                                 $nump = $_->{POINTER_INDEX}+1;
256                         }
257                 }
258                 warning($element->{ORIGINAL}, "Got pointer for `$e->{NAME}', expected fully derefenced variable") if ($nump > length($ptr));
259                 return ($origvar);
260         }
261 }       
262
263 sub check_null_pointer($$$$)
264 {
265         my ($element, $env, $print_fn, $return) = @_;
266
267         return sub ($) {
268                 my $expandedvar = shift;
269                 my $check = 0;
270
271                 # Figure out the number of pointers in $ptr
272                 $expandedvar =~ s/^(\**)//;
273                 my $ptr = $1;
274
275                 my $var = undef;
276                 foreach (keys %$env) {
277                         if ($env->{$_} eq $expandedvar) {
278                                 $var = $_;
279                                 last;
280                         }
281                 }
282                 
283                 if (defined($var)) {
284                         my $e;
285                         # lookup ptr in $e
286                         foreach (@{$element->{PARENT}->{ELEMENTS}}) {
287                                 if ($_->{NAME} eq $var) {
288                                         $e = $_;
289                                         last;
290                                 }
291                         }
292
293                         $e or die("Environment doesn't match siblings");
294
295                         # See if pointer at pointer level $level
296                         # needs to be checked.
297                         foreach my $l (@{$e->{LEVELS}}) {
298                                 if ($l->{TYPE} eq "POINTER" and 
299                                         $l->{POINTER_INDEX} == length($ptr)) {
300                                         # No need to check ref pointers
301                                         $check = ($l->{POINTER_TYPE} ne "ref");
302                                         last;
303                                 }
304
305                                 if ($l->{TYPE} eq "DATA") {
306                                         warning($element, "too much dereferences for `$var'");
307                                 }
308                         }
309                 } else {
310                         warning($element, "unknown dereferenced expression `$expandedvar'");
311                         $check = 1;
312                 }
313                 
314                 $print_fn->("if ($ptr$expandedvar == NULL) $return") if $check;
315         }
316 }
317
318 #####################################################################
319 # parse an array - pull side
320 sub ParseArrayPullHeader($$$$$$)
321 {
322         my ($self,$e,$l,$ndr,$var_name,$env) = @_;
323
324         my $length;
325         my $size;
326
327         if ($l->{IS_CONFORMANT}) {
328                 $length = $size = "ndr_get_array_size($ndr, " . get_pointer_to($var_name) . ")";
329         } elsif ($l->{IS_ZERO_TERMINATED} and $l->{SIZE_IS} == 0 and $l->{LENGTH_IS} == 0) { # Noheader arrays
330                 $length = $size = "ndr_get_string_size($ndr, sizeof(*$var_name))";
331         } else {
332                 $length = $size = ParseExprExt($l->{SIZE_IS}, $env, $e->{ORIGINAL},
333                         check_null_pointer($e, $env, sub { $self->pidl(shift); },
334                                            "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for size_is()\");"),
335                         check_fully_dereferenced($e, $env));
336         }
337
338         if ((!$l->{IS_SURROUNDING}) and $l->{IS_CONFORMANT}) {
339                 $self->pidl("NDR_CHECK(ndr_pull_array_size($ndr, " . get_pointer_to($var_name) . "));");
340         }
341
342         if ($l->{IS_VARYING}) {
343                 $self->pidl("NDR_CHECK(ndr_pull_array_length($ndr, " . get_pointer_to($var_name) . "));");
344                 $length = "ndr_get_array_length($ndr, " . get_pointer_to($var_name) .")";
345         }
346
347         if ($length ne $size) {
348                 $self->pidl("if ($length > $size) {");
349                 $self->indent;
350                 $self->pidl("return ndr_pull_error($ndr, NDR_ERR_ARRAY_SIZE, \"Bad array size %u should exceed array length %u\", $size, $length);");
351                 $self->deindent;
352                 $self->pidl("}");
353         }
354
355         if ($l->{IS_CONFORMANT} and not $l->{IS_ZERO_TERMINATED}) {
356                 $self->defer("if ($var_name) {");
357                 $self->defer_indent;
358                 my $size = ParseExprExt($l->{SIZE_IS}, $env, $e->{ORIGINAL},
359                         check_null_pointer($e, $env, sub { $self->defer(shift); },
360                                            "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for size_is()\");"),
361                         check_fully_dereferenced($e, $env));
362                 $self->defer("NDR_CHECK(ndr_check_array_size($ndr, (void*)" . get_pointer_to($var_name) . ", $size));");
363                 $self->defer_deindent;
364                 $self->defer("}");
365         }
366
367         if ($l->{IS_VARYING} and not $l->{IS_ZERO_TERMINATED}) {
368                 $self->defer("if ($var_name) {");
369                 $self->defer_indent;
370                 my $length = ParseExprExt($l->{LENGTH_IS}, $env, $e->{ORIGINAL}, 
371                         check_null_pointer($e, $env, sub { $self->defer(shift); },
372                                            "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for length_is()\");"),
373                         check_fully_dereferenced($e, $env));
374                 $self->defer("NDR_CHECK(ndr_check_array_length($ndr, (void*)" . get_pointer_to($var_name) . ", $length));");
375                 $self->defer_deindent;
376                 $self->defer("}");
377         }
378
379         if (ArrayDynamicallyAllocated($e,$l) and not is_charset_array($e,$l)) {
380                 $self->AllocateArrayLevel($e,$l,$ndr,$var_name,$size);
381         }
382
383         return $length;
384 }
385
386 sub compression_alg($$)
387 {
388         my ($e, $l) = @_;
389         my ($alg, $clen, $dlen) = split(/,/, $l->{COMPRESSION});
390
391         return $alg;
392 }
393
394 sub compression_clen($$$)
395 {
396         my ($e, $l, $env) = @_;
397         my ($alg, $clen, $dlen) = split(/,/, $l->{COMPRESSION});
398
399         return ParseExpr($clen, $env, $e->{ORIGINAL});
400 }
401
402 sub compression_dlen($$$)
403 {
404         my ($e,$l,$env) = @_;
405         my ($alg, $clen, $dlen) = split(/,/, $l->{COMPRESSION});
406
407         return ParseExpr($dlen, $env, $e->{ORIGINAL});
408 }
409
410 sub ParseCompressionPushStart($$$$$)
411 {
412         my ($self,$e,$l,$ndr,$env) = @_;
413         my $comndr = "$ndr\_compressed";
414         my $alg = compression_alg($e, $l);
415         my $dlen = compression_dlen($e, $l, $env);
416
417         $self->pidl("{");
418         $self->indent;
419         $self->pidl("struct ndr_push *$comndr;");
420         $self->pidl("NDR_CHECK(ndr_push_compression_start($ndr, &$comndr, $alg, $dlen));");
421
422         return $comndr;
423 }
424
425 sub ParseCompressionPushEnd($$$$$)
426 {
427         my ($self,$e,$l,$ndr,$env) = @_;
428         my $comndr = "$ndr\_compressed";
429         my $alg = compression_alg($e, $l);
430         my $dlen = compression_dlen($e, $l, $env);
431
432         $self->pidl("NDR_CHECK(ndr_push_compression_end($ndr, $comndr, $alg, $dlen));");
433         $self->deindent;
434         $self->pidl("}");
435 }
436
437 sub ParseCompressionPullStart($$$$$)
438 {
439         my ($self,$e,$l,$ndr,$env) = @_;
440         my $comndr = "$ndr\_compressed";
441         my $alg = compression_alg($e, $l);
442         my $dlen = compression_dlen($e, $l, $env);
443
444         $self->pidl("{");
445         $self->indent;
446         $self->pidl("struct ndr_pull *$comndr;");
447         $self->pidl("NDR_CHECK(ndr_pull_compression_start($ndr, &$comndr, $alg, $dlen));");
448
449         return $comndr;
450 }
451
452 sub ParseCompressionPullEnd($$$$$)
453 {
454         my ($self,$e,$l,$ndr,$env) = @_;
455         my $comndr = "$ndr\_compressed";
456         my $alg = compression_alg($e, $l);
457         my $dlen = compression_dlen($e, $l, $env);
458
459         $self->pidl("NDR_CHECK(ndr_pull_compression_end($ndr, $comndr, $alg, $dlen));");
460         $self->deindent;
461         $self->pidl("}");
462 }
463
464 sub ParseSubcontextPushStart($$$$$)
465 {
466         my ($self,$e,$l,$ndr,$env) = @_;
467         my $subndr = "_ndr_$e->{NAME}";
468         my $subcontext_size = ParseExpr($l->{SUBCONTEXT_SIZE}, $env, $e->{ORIGINAL});
469
470         $self->pidl("{");
471         $self->indent;
472         $self->pidl("struct ndr_push *$subndr;");
473         $self->pidl("NDR_CHECK(ndr_push_subcontext_start($ndr, &$subndr, $l->{HEADER_SIZE}, $subcontext_size));");
474
475         if (defined $l->{COMPRESSION}) {
476                 $subndr = $self->ParseCompressionPushStart($e, $l, $subndr, $env);
477         }
478
479         return $subndr;
480 }
481
482 sub ParseSubcontextPushEnd($$$$$)
483 {
484         my ($self,$e,$l,$ndr,$env) = @_;
485         my $subndr = "_ndr_$e->{NAME}";
486         my $subcontext_size = ParseExpr($l->{SUBCONTEXT_SIZE}, $env, $e->{ORIGINAL});
487
488         if (defined $l->{COMPRESSION}) {
489                 $self->ParseCompressionPushEnd($e, $l, $subndr, $env);
490         }
491
492         $self->pidl("NDR_CHECK(ndr_push_subcontext_end($ndr, $subndr, $l->{HEADER_SIZE}, $subcontext_size));");
493         $self->deindent;
494         $self->pidl("}");
495 }
496
497 sub ParseSubcontextPullStart($$$$$)
498 {
499         my ($self,$e,$l,$ndr,$env) = @_;
500         my $subndr = "_ndr_$e->{NAME}";
501         my $subcontext_size = ParseExpr($l->{SUBCONTEXT_SIZE}, $env, $e->{ORIGINAL});
502
503         $self->pidl("{");
504         $self->indent;
505         $self->pidl("struct ndr_pull *$subndr;");
506         $self->pidl("NDR_CHECK(ndr_pull_subcontext_start($ndr, &$subndr, $l->{HEADER_SIZE}, $subcontext_size));");
507
508         if (defined $l->{COMPRESSION}) {
509                 $subndr = $self->ParseCompressionPullStart($e, $l, $subndr, $env);
510         }
511
512         return $subndr;
513 }
514
515 sub ParseSubcontextPullEnd($$$$$)
516 {
517         my ($self,$e,$l,$ndr,$env) = @_;
518         my $subndr = "_ndr_$e->{NAME}";
519         my $subcontext_size = ParseExpr($l->{SUBCONTEXT_SIZE}, $env, $e->{ORIGINAL});
520
521         if (defined $l->{COMPRESSION}) {
522                 $self->ParseCompressionPullEnd($e, $l, $subndr, $env);
523         }
524
525         $self->pidl("NDR_CHECK(ndr_pull_subcontext_end($ndr, $subndr, $l->{HEADER_SIZE}, $subcontext_size));");
526         $self->deindent;
527         $self->pidl("}");
528 }
529
530 sub ParseElementPushLevel
531 {
532         my ($self,$e,$l,$ndr,$var_name,$env,$primitives,$deferred) = @_;
533
534         my $ndr_flags = CalcNdrFlags($l, $primitives, $deferred);
535
536         if ($l->{TYPE} eq "ARRAY" and ($l->{IS_CONFORMANT} or $l->{IS_VARYING})) {
537                 $var_name = get_pointer_to($var_name);
538         }
539
540         if (defined($ndr_flags)) {
541                 if ($l->{TYPE} eq "SUBCONTEXT") {
542                         my $subndr = $self->ParseSubcontextPushStart($e, $l, $ndr, $env);
543                         $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $subndr, $var_name, $env, 1, 1);
544                         $self->ParseSubcontextPushEnd($e, $l, $ndr, $env);
545                 } elsif ($l->{TYPE} eq "POINTER") {
546                         $self->ParsePtrPush($e, $l, $ndr, $var_name);
547                 } elsif ($l->{TYPE} eq "ARRAY") {
548                         my $length = $self->ParseArrayPushHeader($e, $l, $ndr, $var_name, $env); 
549
550                         my $nl = GetNextLevel($e, $l);
551
552                         # Allow speedups for arrays of scalar types
553                         if (is_charset_array($e,$l)) {
554                                 $self->pidl("NDR_CHECK(ndr_push_charset($ndr, $ndr_flags, $var_name, $length, sizeof(" . mapTypeName($nl->{DATA_TYPE}) . "), CH_$e->{PROPERTIES}->{charset}));");
555                                 return;
556                         } elsif (has_fast_array($e,$l)) {
557                                 $self->pidl("NDR_CHECK(ndr_push_array_$nl->{DATA_TYPE}($ndr, $ndr_flags, $var_name, $length));");
558                                 return;
559                         } 
560                 } elsif ($l->{TYPE} eq "SWITCH") {
561                         $self->ParseSwitchPush($e, $l, $ndr, $var_name, $env);
562                 } elsif ($l->{TYPE} eq "DATA") {
563                         $self->ParseDataPush($e, $l, $ndr, $var_name, $primitives, $deferred);
564                 } elsif ($l->{TYPE} eq "TYPEDEF") {
565                         $typefamily{$e->{DATA}->{TYPE}}->{PUSH_FN_BODY}->($self, $e->{DATA}, $ndr, $var_name);
566                 }
567         }
568
569         if ($l->{TYPE} eq "POINTER" and $deferred) {
570                 my $rel_var_name = $var_name;
571                 if ($l->{POINTER_TYPE} ne "ref") {
572                         $self->pidl("if ($var_name) {");
573                         $self->indent;
574                         if ($l->{POINTER_TYPE} eq "relative") {
575                                 $self->pidl("NDR_CHECK(ndr_push_relative_ptr2_start($ndr, $rel_var_name));");
576                         }
577                         if ($l->{POINTER_TYPE} eq "relative_short") {
578                                 $self->pidl("NDR_CHECK(ndr_push_short_relative_ptr2($ndr, $var_name));");
579                         }
580                 }
581                 $var_name = get_value_of($var_name);
582                 $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, 1, 1);
583
584                 if ($l->{POINTER_TYPE} ne "ref") {
585                         if ($l->{POINTER_TYPE} eq "relative") {
586                                 $self->pidl("NDR_CHECK(ndr_push_relative_ptr2_end($ndr, $rel_var_name));");
587                         }
588                         $self->deindent;
589                         $self->pidl("}");
590                 }
591         } elsif ($l->{TYPE} eq "ARRAY" and not has_fast_array($e,$l) and
592                 not is_charset_array($e, $l)) {
593                 my $length = ParseExpr($l->{LENGTH_IS}, $env, $e->{ORIGINAL});
594                 my $counter = "cntr_$e->{NAME}_$l->{LEVEL_INDEX}";
595
596                 my $array_pointless = ($length eq "0");
597
598                 if ($array_pointless) {
599                         warning($e->{ORIGINAL}, "pointless array `$e->{NAME}' will always have size 0");
600                 }
601
602                 $var_name = get_array_element($var_name, $counter);
603
604                 if ((($primitives and not $l->{IS_DEFERRED}) or ($deferred and $l->{IS_DEFERRED})) and not $array_pointless) {
605                         $self->pidl("for ($counter = 0; $counter < $length; $counter++) {");
606                         $self->indent;
607                         $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, 1, 0);
608                         $self->deindent;
609                         $self->pidl("}");
610                 }
611
612                 if ($deferred and ContainsDeferred($e, $l) and not $array_pointless) {
613                         $self->pidl("for ($counter = 0; $counter < $length; $counter++) {");
614                         $self->indent;
615                         $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, 0, 1);
616                         $self->deindent;
617                         $self->pidl("}");
618                 }       
619         } elsif ($l->{TYPE} eq "SWITCH") {
620                 $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, $primitives, $deferred);
621         }
622 }
623
624 #####################################################################
625 # parse scalars in a structure element
626 sub ParseElementPush($$$$$$)
627 {
628         my ($self,$e,$ndr,$env,$primitives,$deferred) = @_;
629         my $subndr = undef;
630
631         my $var_name = $env->{$e->{NAME}};
632
633         return unless $primitives or ($deferred and ContainsDeferred($e, $e->{LEVELS}[0]));
634
635         # Representation type is different from transmit_as
636         if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
637                 $self->pidl("{");
638                 $self->indent;
639                 my $transmit_name = "_transmit_$e->{NAME}";
640                 $self->pidl(mapTypeName($e->{TYPE}) ." $transmit_name;");
641                 $self->pidl("NDR_CHECK(ndr_$e->{REPRESENTATION_TYPE}_to_$e->{TYPE}($var_name, " . get_pointer_to($transmit_name) . "));");
642                 $var_name = $transmit_name;
643         }
644
645         $var_name = append_prefix($e, $var_name);
646
647         $self->start_flags($e, $ndr);
648
649         if (defined(my $value = has_property($e, "value"))) {
650                 $var_name = ParseExpr($value, $env, $e->{ORIGINAL});
651         }
652
653         $self->ParseElementPushLevel($e, $e->{LEVELS}[0], $ndr, $var_name, $env, $primitives, $deferred);
654
655         $self->end_flags($e, $ndr);
656
657         if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
658                 $self->deindent;
659                 $self->pidl("}");
660         }
661 }
662
663 #####################################################################
664 # parse a pointer in a struct element or function
665 sub ParsePtrPush($$$$$)
666 {
667         my ($self,$e,$l,$ndr,$var_name) = @_;
668
669         if ($l->{POINTER_TYPE} eq "ref") {
670                 $self->pidl("if ($var_name == NULL) {");
671                 $self->indent;
672                 $self->pidl("return ndr_push_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL [ref] pointer\");");
673                 $self->deindent;
674                 $self->pidl("}");
675                 if ($l->{LEVEL} eq "EMBEDDED") {
676                         $self->pidl("NDR_CHECK(ndr_push_ref_ptr(ndr));");
677                 }
678         } elsif ($l->{POINTER_TYPE} eq "relative") {
679                 $self->pidl("NDR_CHECK(ndr_push_relative_ptr1($ndr, $var_name));");
680         } elsif ($l->{POINTER_TYPE} eq "relative_short") {
681                 $self->pidl("NDR_CHECK(ndr_push_short_relative_ptr1($ndr, $var_name));");
682         } elsif ($l->{POINTER_TYPE} eq "unique") {
683                 $self->pidl("NDR_CHECK(ndr_push_unique_ptr($ndr, $var_name));");
684         } elsif ($l->{POINTER_TYPE} eq "full") {
685                 $self->pidl("NDR_CHECK(ndr_push_full_ptr($ndr, $var_name));");
686         } else {
687                 die("Unhandled pointer type $l->{POINTER_TYPE}");
688         }
689 }
690
691 sub need_pointer_to($$$)
692 {
693         my ($e, $l, $scalar_only) = @_;
694
695         my $t;
696         if (ref($l->{DATA_TYPE})) {
697                 $t = "$l->{DATA_TYPE}->{TYPE}_$l->{DATA_TYPE}->{NAME}";
698         } else {
699                 $t = $l->{DATA_TYPE};
700         }
701
702         if (not Parse::Pidl::Typelist::is_scalar($t)) {
703                 return 1 if $scalar_only;
704         }
705
706         my $arrays = 0;
707
708         foreach my $tl (@{$e->{LEVELS}}) {
709                 last if $l == $tl;
710                 if ($tl->{TYPE} eq "ARRAY") {
711                         $arrays++;
712                 }
713         }
714
715         if (Parse::Pidl::Typelist::scalar_is_reference($t)) {
716                 return 1 unless $arrays;
717         }
718
719         return 0;
720 }
721
722 sub ParseDataPrint($$$$$)
723 {
724         my ($self, $e, $l, $ndr, $var_name) = @_;
725
726         if (not ref($l->{DATA_TYPE}) or defined($l->{DATA_TYPE}->{NAME})) {
727
728                 if (need_pointer_to($e, $l, 1)) {
729                         $var_name = get_pointer_to($var_name);
730                 }
731
732                 $self->pidl(TypeFunctionName("ndr_print", $l->{DATA_TYPE})."($ndr, \"$e->{NAME}\", $var_name);");
733         } else {
734                 $self->ParseTypePrint($l->{DATA_TYPE}, $ndr, $var_name);
735         }
736 }
737
738 #####################################################################
739 # print scalars in a structure element
740 sub ParseElementPrint($$$$$)
741 {
742         my($self, $e, $ndr, $var_name, $env) = @_;
743
744         return if (has_property($e, "noprint"));
745
746         if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
747                 $self->pidl("ndr_print_$e->{REPRESENTATION_TYPE}($ndr, \"$e->{NAME}\", $var_name);");
748                 return;
749         }
750
751         $var_name = append_prefix($e, $var_name);
752
753         if (defined(my $value = has_property($e, "value"))) {
754                 $var_name = "($ndr->flags & LIBNDR_PRINT_SET_VALUES)?" . ParseExpr($value,$env, $e->{ORIGINAL}) . ":$var_name";
755         }
756
757         foreach my $l (@{$e->{LEVELS}}) {
758                 if ($l->{TYPE} eq "POINTER") {
759                         $self->pidl("ndr_print_ptr($ndr, \"$e->{NAME}\", $var_name);");
760                         $self->pidl("$ndr->depth++;");
761                         if ($l->{POINTER_TYPE} ne "ref") {
762                                 $self->pidl("if ($var_name) {");
763                                 $self->indent;
764                         }
765                         $var_name = get_value_of($var_name);
766                 } elsif ($l->{TYPE} eq "ARRAY") {
767                         my $length;
768
769                         if ($l->{IS_CONFORMANT} or $l->{IS_VARYING}) {
770                                 $var_name = get_pointer_to($var_name); 
771                         }
772                         
773                         if ($l->{IS_ZERO_TERMINATED}) {
774                                 $length = "ndr_string_length($var_name, sizeof(*$var_name))";
775                         } else {
776                                 $length = ParseExprExt($l->{LENGTH_IS}, $env, $e->{ORIGINAL}, 
777                                                         check_null_pointer($e, $env, sub { $self->pidl(shift); }, "return;"), check_fully_dereferenced($e, $env));
778                         }
779
780                         if (is_charset_array($e,$l)) {
781                                 $self->pidl("ndr_print_string($ndr, \"$e->{NAME}\", $var_name);");
782                                 last;
783                         } elsif (has_fast_array($e, $l)) {
784                                 my $nl = GetNextLevel($e, $l);
785                                 $self->pidl("ndr_print_array_$nl->{DATA_TYPE}($ndr, \"$e->{NAME}\", $var_name, $length);");
786                                 last;
787                         } else {
788                                 my $counter = "cntr_$e->{NAME}_$l->{LEVEL_INDEX}";
789
790                                 $self->pidl("$ndr->print($ndr, \"\%s: ARRAY(\%d)\", \"$e->{NAME}\", (int)$length);");
791                                 $self->pidl("$ndr->depth++;");
792                                 $self->pidl("for ($counter=0;$counter<$length;$counter++) {");
793                                 $self->indent;
794
795                                 $var_name = get_array_element($var_name, $counter);
796                         }
797                 } elsif ($l->{TYPE} eq "DATA") {
798                         $self->ParseDataPrint($e, $l, $ndr, $var_name);
799                 } elsif ($l->{TYPE} eq "SWITCH") {
800                         my $switch_var = ParseExprExt($l->{SWITCH_IS}, $env, $e->{ORIGINAL}, 
801                                                 check_null_pointer($e, $env, sub { $self->pidl(shift); }, "return;"), check_fully_dereferenced($e, $env));
802                         $self->pidl("ndr_print_set_switch_value($ndr, " . get_pointer_to($var_name) . ", $switch_var);");
803                 } 
804         }
805
806         foreach my $l (reverse @{$e->{LEVELS}}) {
807                 if ($l->{TYPE} eq "POINTER") {
808                         if ($l->{POINTER_TYPE} ne "ref") {
809                                 $self->deindent;
810                                 $self->pidl("}");
811                         }
812                         $self->pidl("$ndr->depth--;");
813                 } elsif (($l->{TYPE} eq "ARRAY")
814                         and not is_charset_array($e,$l)
815                         and not has_fast_array($e,$l)) {
816                         $self->deindent;
817                         $self->pidl("}");
818                         $self->pidl("$ndr->depth--;");
819                 }
820         }
821 }
822
823 #####################################################################
824 # parse scalars in a structure element - pull size
825 sub ParseSwitchPull($$$$$$)
826 {
827         my($self,$e,$l,$ndr,$var_name,$env) = @_;
828         my $switch_var = ParseExprExt($l->{SWITCH_IS}, $env, $e->{ORIGINAL}, 
829                 check_null_pointer($e, $env, sub { $self->pidl(shift); },
830                                    "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for switch_is()\");"),
831                 check_fully_dereferenced($e, $env));
832
833         $var_name = get_pointer_to($var_name);
834         $self->pidl("NDR_CHECK(ndr_pull_set_switch_value($ndr, $var_name, $switch_var));");
835 }
836
837 #####################################################################
838 # push switch element
839 sub ParseSwitchPush($$$$$$)
840 {
841         my($self,$e,$l,$ndr,$var_name,$env) = @_;
842         my $switch_var = ParseExprExt($l->{SWITCH_IS}, $env, $e->{ORIGINAL}, 
843                 check_null_pointer($e, $env, sub { $self->pidl(shift); },
844                                    "return ndr_push_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for switch_is()\");"),
845                 check_fully_dereferenced($e, $env));
846
847         $var_name = get_pointer_to($var_name);
848         $self->pidl("NDR_CHECK(ndr_push_set_switch_value($ndr, $var_name, $switch_var));");
849 }
850
851 sub ParseDataPull($$$$$$$)
852 {
853         my ($self,$e,$l,$ndr,$var_name,$primitives,$deferred) = @_;
854
855         if (not ref($l->{DATA_TYPE}) or defined($l->{DATA_TYPE}->{NAME})) {
856
857                 my $ndr_flags = CalcNdrFlags($l, $primitives, $deferred);
858
859                 if (need_pointer_to($e, $l, 0)) {
860                         $var_name = get_pointer_to($var_name);
861                 }
862
863                 $var_name = get_pointer_to($var_name);
864
865                 $self->pidl("NDR_CHECK(".TypeFunctionName("ndr_pull", $l->{DATA_TYPE})."($ndr, $ndr_flags, $var_name));");
866
867                 my $pl = GetPrevLevel($e, $l);
868
869                 my $range = has_property($e, "range");
870                 if ($range and $pl->{TYPE} ne "ARRAY") {
871                         $var_name = get_value_of($var_name);
872                         my $signed = Parse::Pidl::Typelist::is_signed($l->{DATA_TYPE});
873                         my ($low, $high) = split(/,/, $range, 2);
874                         if ($low < 0 and not $signed) {
875                                 warning(0, "$low is invalid for the range of an unsigned type");
876                         }
877                         if ($low == 0 and not $signed) {
878                                 $self->pidl("if ($var_name > $high) {");
879                         } else {
880                                 $self->pidl("if ($var_name < $low || $var_name > $high) {");
881                         }
882                         $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_RANGE, \"value out of range\");");
883                         $self->pidl("}");
884                 }
885         } else {
886                 $self->ParseTypePull($l->{DATA_TYPE}, $ndr, $var_name, $primitives, $deferred);
887         }
888 }
889
890 sub ParseDataPush($$$$$$$)
891 {
892         my ($self,$e,$l,$ndr,$var_name,$primitives,$deferred) = @_;
893
894         if (not ref($l->{DATA_TYPE}) or defined($l->{DATA_TYPE}->{NAME})) {
895
896                 my $ndr_flags = CalcNdrFlags($l, $primitives, $deferred);
897
898                 # strings are passed by value rather than reference
899                 if (need_pointer_to($e, $l, 1)) {
900                         $var_name = get_pointer_to($var_name);
901                 }
902
903                 $self->pidl("NDR_CHECK(".TypeFunctionName("ndr_push", $l->{DATA_TYPE})."($ndr, $ndr_flags, $var_name));");
904         } else {
905                 $self->ParseTypePush($l->{DATA_TYPE}, $ndr, $var_name, $primitives, $deferred);
906         }
907 }
908
909 sub CalcNdrFlags($$$)
910 {
911         my ($l,$primitives,$deferred) = @_;
912
913         my $scalars = 0;
914         my $buffers = 0;
915
916         # Add NDR_SCALARS if this one is deferred 
917         # and deferreds may be pushed
918         $scalars = 1 if ($l->{IS_DEFERRED} and $deferred);
919
920         # Add NDR_SCALARS if this one is not deferred and 
921         # primitives may be pushed
922         $scalars = 1 if (!$l->{IS_DEFERRED} and $primitives);
923         
924         # Add NDR_BUFFERS if this one contains deferred stuff
925         # and deferreds may be pushed
926         $buffers = 1 if ($l->{CONTAINS_DEFERRED} and $deferred);
927
928         return "NDR_SCALARS|NDR_BUFFERS" if ($scalars and $buffers);
929         return "NDR_SCALARS" if ($scalars);
930         return "NDR_BUFFERS" if ($buffers);
931         return undef;
932 }
933
934 sub ParseMemCtxPullFlags($$$$)
935 {
936         my ($self, $e, $l) = @_;
937
938         return undef unless ($l->{TYPE} eq "POINTER" or $l->{TYPE} eq "ARRAY");
939
940         return undef unless ($l->{TYPE} ne "ARRAY" or ArrayDynamicallyAllocated($e,$l));
941         return undef if has_fast_array($e, $l);
942         return undef if is_charset_array($e, $l);
943
944         my $mem_flags = "0";
945
946         if (($l->{TYPE} eq "POINTER") and ($l->{POINTER_TYPE} eq "ref")) {
947                 my $nl = GetNextLevel($e, $l);
948                 return undef if ($nl->{TYPE} eq "ARRAY");
949                 return undef if (($nl->{TYPE} eq "DATA") and ($nl->{DATA_TYPE} eq "string"));
950
951                 if ($l->{LEVEL} eq "TOP") {
952                         $mem_flags = "LIBNDR_FLAG_REF_ALLOC";
953                 }
954         }
955
956         return $mem_flags;
957 }
958
959 sub ParseMemCtxPullStart($$$$$)
960 {
961         my ($self, $e, $l, $ndr, $ptr_name) = @_;
962
963         my $mem_r_ctx = "_mem_save_$e->{NAME}_$l->{LEVEL_INDEX}";
964         my $mem_c_ctx = $ptr_name;
965         my $mem_c_flags = $self->ParseMemCtxPullFlags($e, $l);
966
967         return unless defined($mem_c_flags);
968
969         $self->pidl("$mem_r_ctx = NDR_PULL_GET_MEM_CTX($ndr);");
970         $self->pidl("NDR_PULL_SET_MEM_CTX($ndr, $mem_c_ctx, $mem_c_flags);");
971 }
972
973 sub ParseMemCtxPullEnd($$$$)
974 {
975         my ($self, $e, $l, $ndr) = @_;
976
977         my $mem_r_ctx = "_mem_save_$e->{NAME}_$l->{LEVEL_INDEX}";
978         my $mem_r_flags = $self->ParseMemCtxPullFlags($e, $l);
979
980         return unless defined($mem_r_flags);
981
982         $self->pidl("NDR_PULL_SET_MEM_CTX($ndr, $mem_r_ctx, $mem_r_flags);");
983 }
984
985 sub CheckStringTerminator($$$$$)
986 {
987         my ($self,$ndr,$e,$l,$length) = @_;
988         my $nl = GetNextLevel($e, $l);
989
990         # Make sure last element is zero!
991         $self->pidl("NDR_CHECK(ndr_check_string_terminator($ndr, $length, sizeof($nl->{DATA_TYPE}_t)));");
992 }
993
994 sub ParseElementPullLevel
995 {
996         my($self,$e,$l,$ndr,$var_name,$env,$primitives,$deferred) = @_;
997
998         my $ndr_flags = CalcNdrFlags($l, $primitives, $deferred);
999
1000         if ($l->{TYPE} eq "ARRAY" and ($l->{IS_VARYING} or $l->{IS_CONFORMANT})) {
1001                 $var_name = get_pointer_to($var_name);
1002         }
1003
1004         # Only pull something if there's actually something to be pulled
1005         if (defined($ndr_flags)) {
1006                 if ($l->{TYPE} eq "SUBCONTEXT") {
1007                         my $subndr = $self->ParseSubcontextPullStart($e, $l, $ndr, $env);
1008                         $self->ParseElementPullLevel($e, GetNextLevel($e,$l), $subndr, $var_name, $env, 1, 1);
1009                         $self->ParseSubcontextPullEnd($e, $l, $ndr, $env);
1010                 } elsif ($l->{TYPE} eq "ARRAY") {
1011                         my $length = $self->ParseArrayPullHeader($e, $l, $ndr, $var_name, $env);
1012
1013                         if (my $range = has_property($e, "range")) {
1014                                 my ($low, $high) = split(/,/, $range, 2);
1015                                 if ($low < 0) {
1016                                         warning(0, "$low is invalid for the range of an array size");
1017                                 }
1018                                 if ($low == 0) {
1019                                         $self->pidl("if ($length > $high) {");
1020                                 } else {
1021                                         $self->pidl("if ($length < $low || $length > $high) {");
1022                                 }
1023                                 $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_RANGE, \"value out of range\");");
1024                                 $self->pidl("}");
1025                         }
1026
1027                         my $nl = GetNextLevel($e, $l);
1028
1029                         if (is_charset_array($e,$l)) {
1030                                 if ($l->{IS_ZERO_TERMINATED}) {
1031                                         $self->CheckStringTerminator($ndr, $e, $l, $length);
1032                                 }
1033                                 $self->pidl("NDR_CHECK(ndr_pull_charset($ndr, $ndr_flags, ".get_pointer_to($var_name).", $length, sizeof(" . mapTypeName($nl->{DATA_TYPE}) . "), CH_$e->{PROPERTIES}->{charset}));");
1034                                 return;
1035                         } elsif (has_fast_array($e, $l)) {
1036                                 if ($l->{IS_ZERO_TERMINATED}) {
1037                                         $self->CheckStringTerminator($ndr,$e,$l,$length);
1038                                 }
1039                                 $self->pidl("NDR_CHECK(ndr_pull_array_$nl->{DATA_TYPE}($ndr, $ndr_flags, $var_name, $length));");
1040                                 return;
1041                         }
1042                 } elsif ($l->{TYPE} eq "POINTER") {
1043                         $self->ParsePtrPull($e, $l, $ndr, $var_name);
1044                 } elsif ($l->{TYPE} eq "SWITCH") {
1045                         $self->ParseSwitchPull($e, $l, $ndr, $var_name, $env);
1046                 } elsif ($l->{TYPE} eq "DATA") {
1047                         $self->ParseDataPull($e, $l, $ndr, $var_name, $primitives, $deferred);
1048                 } elsif ($l->{TYPE} eq "TYPEDEF") {
1049                         $typefamily{$e->{DATA}->{TYPE}}->{PULL_FN_BODY}->($self, $e->{DATA}, $ndr, $var_name);
1050                 }
1051         }
1052
1053         # add additional constructions
1054         if ($l->{TYPE} eq "POINTER" and $deferred) {
1055                 if ($l->{POINTER_TYPE} ne "ref") {
1056                         $self->pidl("if ($var_name) {");
1057                         $self->indent;
1058
1059                         if ($l->{POINTER_TYPE} eq "relative" or $l->{POINTER_TYPE} eq "relative_short") {
1060                                 $self->pidl("uint32_t _relative_save_offset;");
1061                                 $self->pidl("_relative_save_offset = $ndr->offset;");
1062                                 $self->pidl("NDR_CHECK(ndr_pull_relative_ptr2($ndr, $var_name));");
1063                         }
1064                 }
1065
1066                 $self->ParseMemCtxPullStart($e, $l, $ndr, $var_name);
1067
1068                 $var_name = get_value_of($var_name);
1069                 $self->ParseElementPullLevel($e, GetNextLevel($e,$l), $ndr, $var_name, $env, 1, 1);
1070
1071                 $self->ParseMemCtxPullEnd($e, $l, $ndr);
1072
1073                 if ($l->{POINTER_TYPE} ne "ref") {
1074                         if ($l->{POINTER_TYPE} eq "relative") {
1075                                 $self->pidl("if ($ndr->offset > $ndr->relative_highest_offset) {");
1076                                 $self->indent;
1077                                 $self->pidl("$ndr->relative_highest_offset = $ndr->offset;");
1078                                 $self->deindent;
1079                                 $self->pidl("}");
1080                                 $self->pidl("$ndr->offset = _relative_save_offset;");
1081                         }
1082                         $self->deindent;
1083                         $self->pidl("}");
1084                 }
1085         } elsif ($l->{TYPE} eq "ARRAY" and 
1086                         not has_fast_array($e,$l) and not is_charset_array($e, $l)) {
1087                 my $length = ParseExpr($l->{LENGTH_IS}, $env, $e->{ORIGINAL});
1088                 my $counter = "cntr_$e->{NAME}_$l->{LEVEL_INDEX}";
1089                 my $array_name = $var_name;
1090
1091                 if ($l->{IS_VARYING}) {
1092                         $length = "ndr_get_array_length($ndr, " . get_pointer_to($var_name) .")";
1093                 }
1094
1095                 if (my $range = has_property($e, "range")) {
1096                         my ($low, $high) = split(/,/, $range, 2);
1097                         if ($low < 0) {
1098                                 warning(0, "$low is invalid for the range of an array size");
1099                         }
1100                         if ($low == 0) {
1101                                 $self->pidl("if ($length > $high) {");
1102                         } else {
1103                                 $self->pidl("if ($length < $low || $length > $high) {");
1104                         }
1105                         $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_RANGE, \"value out of range\");");
1106                         $self->pidl("}");
1107                 }
1108
1109                 $var_name = get_array_element($var_name, $counter);
1110
1111                 $self->ParseMemCtxPullStart($e, $l, $ndr, $array_name);
1112
1113                 if (($primitives and not $l->{IS_DEFERRED}) or ($deferred and $l->{IS_DEFERRED})) {
1114                         my $nl = GetNextLevel($e,$l);
1115
1116                         if ($l->{IS_ZERO_TERMINATED}) {
1117                                 $self->CheckStringTerminator($ndr,$e,$l,$length);
1118                         }
1119
1120                         $self->pidl("for ($counter = 0; $counter < $length; $counter++) {");
1121                         $self->indent;
1122                         $self->ParseElementPullLevel($e, $nl, $ndr, $var_name, $env, 1, 0);
1123                         $self->deindent;
1124                         $self->pidl("}");
1125                 }
1126
1127                 if ($deferred and ContainsDeferred($e, $l)) {
1128                         $self->pidl("for ($counter = 0; $counter < $length; $counter++) {");
1129                         $self->indent;
1130                         $self->ParseElementPullLevel($e,GetNextLevel($e,$l), $ndr, $var_name, $env, 0, 1);
1131                         $self->deindent;
1132                         $self->pidl("}");
1133                 }
1134
1135                 $self->ParseMemCtxPullEnd($e, $l, $ndr);
1136
1137         } elsif ($l->{TYPE} eq "SWITCH") {
1138                 $self->ParseElementPullLevel($e, GetNextLevel($e,$l), $ndr, $var_name, $env, $primitives, $deferred);
1139         }
1140 }
1141
1142 #####################################################################
1143 # parse scalars in a structure element - pull size
1144 sub ParseElementPull($$$$$$)
1145 {
1146         my($self,$e,$ndr,$env,$primitives,$deferred) = @_;
1147
1148         my $var_name = $env->{$e->{NAME}};
1149         my $represent_name;
1150         my $transmit_name;
1151
1152         return unless $primitives or ($deferred and ContainsDeferred($e, $e->{LEVELS}[0]));
1153
1154         if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
1155                 $self->pidl("{");
1156                 $self->indent;
1157                 $represent_name = $var_name;
1158                 $transmit_name = "_transmit_$e->{NAME}";
1159                 $var_name = $transmit_name;
1160                 $self->pidl(mapTypeName($e->{TYPE})." $var_name;");
1161         }
1162
1163         $var_name = append_prefix($e, $var_name);
1164
1165         $self->start_flags($e, $ndr);
1166
1167         $self->ParseElementPullLevel($e,$e->{LEVELS}[0],$ndr,$var_name,$env,$primitives,$deferred);
1168
1169         $self->end_flags($e, $ndr);
1170
1171         # Representation type is different from transmit_as
1172         if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
1173                 $self->pidl("NDR_CHECK(ndr_$e->{TYPE}_to_$e->{REPRESENTATION_TYPE}($transmit_name, ".get_pointer_to($represent_name)."));");
1174                 $self->deindent;
1175                 $self->pidl("}");
1176         }
1177 }
1178
1179 #####################################################################
1180 # parse a pointer in a struct element or function
1181 sub ParsePtrPull($$$$$)
1182 {
1183         my($self, $e,$l,$ndr,$var_name) = @_;
1184
1185         my $nl = GetNextLevel($e, $l);
1186         my $next_is_array = ($nl->{TYPE} eq "ARRAY");
1187         my $next_is_string = (($nl->{TYPE} eq "DATA") and 
1188                                                  ($nl->{DATA_TYPE} eq "string"));
1189
1190         if ($l->{POINTER_TYPE} eq "ref" and $l->{LEVEL} eq "TOP") {
1191
1192                 if (!$next_is_array and !$next_is_string) {
1193                         $self->pidl("if ($ndr->flags & LIBNDR_FLAG_REF_ALLOC) {");
1194                         $self->pidl("\tNDR_PULL_ALLOC($ndr, $var_name);"); 
1195                         $self->pidl("}");
1196                 }
1197                 
1198                 return;
1199         } elsif ($l->{POINTER_TYPE} eq "ref" and $l->{LEVEL} eq "EMBEDDED") {
1200                 $self->pidl("NDR_CHECK(ndr_pull_ref_ptr($ndr, &_ptr_$e->{NAME}));");
1201         } elsif (($l->{POINTER_TYPE} eq "unique") or 
1202                  ($l->{POINTER_TYPE} eq "relative") or
1203                  ($l->{POINTER_TYPE} eq "full")) {
1204                 $self->pidl("NDR_CHECK(ndr_pull_generic_ptr($ndr, &_ptr_$e->{NAME}));");
1205         } elsif ($l->{POINTER_TYPE} eq "relative_short") {
1206                 $self->pidl("NDR_CHECK(ndr_pull_relative_ptr_short($ndr, &_ptr_$e->{NAME}));");
1207         } else {
1208                 die("Unhandled pointer type $l->{POINTER_TYPE}");
1209         }
1210
1211         $self->pidl("if (_ptr_$e->{NAME}) {");
1212         $self->indent;
1213
1214         # Don't do this for arrays, they're allocated at the actual level 
1215         # of the array
1216         unless ($next_is_array or $next_is_string) { 
1217                 $self->pidl("NDR_PULL_ALLOC($ndr, $var_name);"); 
1218         } else {
1219                 # FIXME: Yes, this is nasty.
1220                 # We allocate an array twice
1221                 # - once just to indicate that it's there,
1222                 # - then the real allocation...
1223                 $self->pidl("NDR_PULL_ALLOC($ndr, $var_name);");
1224         }
1225
1226         #$self->pidl("memset($var_name, 0, sizeof($var_name));");
1227         if ($l->{POINTER_TYPE} eq "relative" or $l->{POINTER_TYPE} eq "relative_short") {
1228                 $self->pidl("NDR_CHECK(ndr_pull_relative_ptr1($ndr, $var_name, _ptr_$e->{NAME}));");
1229         }
1230         $self->deindent;
1231         $self->pidl("} else {");
1232         $self->pidl("\t$var_name = NULL;");
1233         $self->pidl("}");
1234 }
1235
1236 sub ParseStructPushPrimitives($$$$$)
1237 {
1238         my ($self, $struct, $ndr, $varname, $env) = @_;
1239
1240         # see if the structure contains a conformant array. If it
1241         # does, then it must be the last element of the structure, and
1242         # we need to push the conformant length early, as it fits on
1243         # the wire before the structure (and even before the structure
1244         # alignment)
1245         if (defined($struct->{SURROUNDING_ELEMENT})) {
1246                 my $e = $struct->{SURROUNDING_ELEMENT};
1247
1248                 if (defined($e->{LEVELS}[0]) and 
1249                         $e->{LEVELS}[0]->{TYPE} eq "ARRAY") {
1250                         my $size;
1251                         
1252                         if ($e->{LEVELS}[0]->{IS_ZERO_TERMINATED}) {
1253                                 if (has_property($e, "charset")) {
1254                                         $size = "ndr_charset_length($varname->$e->{NAME}, CH_$e->{PROPERTIES}->{charset})";
1255                                 } else {
1256                                         $size = "ndr_string_length($varname->$e->{NAME}, sizeof(*$varname->$e->{NAME}))";
1257                                 }
1258                         } else {
1259                                 $size = ParseExpr($e->{LEVELS}[0]->{SIZE_IS}, $env, $e->{ORIGINAL});
1260                         }
1261
1262                         $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, $size));");
1263                 } else {
1264                         $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, ndr_string_array_size($ndr, $varname->$e->{NAME})));");
1265                 }
1266         }
1267
1268         $self->pidl("NDR_CHECK(ndr_push_align($ndr, $struct->{ALIGN}));");
1269
1270         if (defined($struct->{PROPERTIES}{relative_base})) {
1271                 # set the current offset as base for relative pointers
1272                 # and store it based on the toplevel struct/union
1273                 $self->pidl("NDR_CHECK(ndr_push_setup_relative_base_offset1($ndr, $varname, $ndr->offset));");
1274         }
1275
1276         $self->ParseElementPush($_, $ndr, $env, 1, 0) foreach (@{$struct->{ELEMENTS}});
1277
1278         $self->pidl("NDR_CHECK(ndr_push_trailer_align($ndr, $struct->{ALIGN}));");
1279 }
1280
1281 sub ParseStructPushDeferred($$$$)
1282 {
1283         my ($self, $struct, $ndr, $varname, $env) = @_;
1284         if (defined($struct->{PROPERTIES}{relative_base})) {
1285                 # retrieve the current offset as base for relative pointers
1286                 # based on the toplevel struct/union
1287                 $self->pidl("NDR_CHECK(ndr_push_setup_relative_base_offset2($ndr, $varname));");
1288         }
1289         $self->ParseElementPush($_, $ndr, $env, 0, 1) foreach (@{$struct->{ELEMENTS}});
1290 }
1291
1292 #####################################################################
1293 # parse a struct
1294 sub ParseStructPush($$$$)
1295 {
1296         my ($self, $struct, $ndr, $varname) = @_;
1297         
1298         return unless defined($struct->{ELEMENTS});
1299
1300         my $env = GenerateStructEnv($struct, $varname);
1301
1302         EnvSubstituteValue($env, $struct);
1303
1304         $self->DeclareArrayVariablesNoZero($_, $env) foreach (@{$struct->{ELEMENTS}});
1305
1306         $self->start_flags($struct, $ndr);
1307
1308         $self->pidl("if (ndr_flags & NDR_SCALARS) {");
1309         $self->indent;
1310         $self->ParseStructPushPrimitives($struct, $ndr, $varname, $env);
1311         $self->deindent;
1312         $self->pidl("}");
1313
1314         $self->pidl("if (ndr_flags & NDR_BUFFERS) {");
1315         $self->indent;
1316         $self->ParseStructPushDeferred($struct, $ndr, $varname, $env);
1317         $self->deindent;
1318         $self->pidl("}");
1319
1320         $self->end_flags($struct, $ndr);
1321 }
1322
1323 #####################################################################
1324 # generate a push function for an enum
1325 sub ParseEnumPush($$$$)
1326 {
1327         my($self,$enum,$ndr,$varname) = @_;
1328         my($type_fn) = $enum->{BASE_TYPE};
1329
1330         $self->start_flags($enum, $ndr);
1331         $self->pidl("NDR_CHECK(ndr_push_enum_$type_fn($ndr, NDR_SCALARS, $varname));");
1332         $self->end_flags($enum, $ndr);
1333 }
1334
1335 #####################################################################
1336 # generate a pull function for an enum
1337 sub ParseEnumPull($$$$)
1338 {
1339         my($self,$enum,$ndr,$varname) = @_;
1340         my($type_fn) = $enum->{BASE_TYPE};
1341         my($type_v_decl) = mapTypeName($type_fn);
1342
1343         $self->pidl("$type_v_decl v;");
1344         $self->start_flags($enum, $ndr);
1345         $self->pidl("NDR_CHECK(ndr_pull_enum_$type_fn($ndr, NDR_SCALARS, &v));");
1346         $self->pidl("*$varname = v;");
1347
1348         $self->end_flags($enum, $ndr);
1349 }
1350
1351 #####################################################################
1352 # generate a print function for an enum
1353 sub ParseEnumPrint($$$$$)
1354 {
1355         my($self,$enum,$ndr,$name,$varname) = @_;
1356
1357         $self->pidl("const char *val = NULL;");
1358         $self->pidl("");
1359
1360         $self->start_flags($enum, $ndr);
1361
1362         $self->pidl("switch ($varname) {");
1363         $self->indent;
1364         my $els = \@{$enum->{ELEMENTS}};
1365         foreach my $i (0 .. $#{$els}) {
1366                 my $e = ${$els}[$i];
1367                 chomp $e;
1368                 if ($e =~ /^(.*)=/) {
1369                         $e = $1;
1370                 }
1371                 $self->pidl("case $e: val = \"$e\"; break;");
1372         }
1373
1374         $self->deindent;
1375         $self->pidl("}");
1376         
1377         $self->pidl("ndr_print_enum($ndr, name, \"$enum->{TYPE}\", val, $varname);");
1378
1379         $self->end_flags($enum, $ndr);
1380 }
1381
1382 sub DeclEnum($$$$)
1383 {
1384         my ($e,$t,$name,$varname) = @_;
1385         return "enum $name " . 
1386                 ($t eq "pull"?"*":"") . $varname;
1387 }
1388
1389 $typefamily{ENUM} = {
1390         DECL => \&DeclEnum,
1391         PUSH_FN_BODY => \&ParseEnumPush,
1392         PULL_FN_BODY => \&ParseEnumPull,
1393         PRINT_FN_BODY => \&ParseEnumPrint,
1394 };
1395
1396 #####################################################################
1397 # generate a push function for a bitmap
1398 sub ParseBitmapPush($$$$)
1399 {
1400         my($self,$bitmap,$ndr,$varname) = @_;
1401         my($type_fn) = $bitmap->{BASE_TYPE};
1402
1403         $self->start_flags($bitmap, $ndr);
1404
1405         $self->pidl("NDR_CHECK(ndr_push_$type_fn($ndr, NDR_SCALARS, $varname));");
1406
1407         $self->end_flags($bitmap, $ndr);
1408 }
1409
1410 #####################################################################
1411 # generate a pull function for an bitmap
1412 sub ParseBitmapPull($$$$)
1413 {
1414         my($self,$bitmap,$ndr,$varname) = @_;
1415         my $type_fn = $bitmap->{BASE_TYPE};
1416         my($type_decl) = mapTypeName($bitmap->{BASE_TYPE});
1417
1418         $self->pidl("$type_decl v;");
1419         $self->start_flags($bitmap, $ndr);
1420         $self->pidl("NDR_CHECK(ndr_pull_$type_fn($ndr, NDR_SCALARS, &v));");
1421         $self->pidl("*$varname = v;");
1422
1423         $self->end_flags($bitmap, $ndr);
1424 }
1425
1426 #####################################################################
1427 # generate a print function for an bitmap
1428 sub ParseBitmapPrintElement($$$$$$)
1429 {
1430         my($self,$e,$bitmap,$ndr,$name,$varname) = @_;
1431         my($type_decl) = mapTypeName($bitmap->{BASE_TYPE});
1432         my($type_fn) = $bitmap->{BASE_TYPE};
1433         my($flag);
1434
1435         if ($e =~ /^(\w+) .*$/) {
1436                 $flag = "$1";
1437         } else {
1438                 die "Bitmap: \"$name\" invalid Flag: \"$e\"";
1439         }
1440
1441         $self->pidl("ndr_print_bitmap_flag($ndr, sizeof($type_decl), \"$flag\", $flag, $varname);");
1442 }
1443
1444 #####################################################################
1445 # generate a print function for an bitmap
1446 sub ParseBitmapPrint($$$$$)
1447 {
1448         my($self,$bitmap,$ndr,$name,$varname) = @_;
1449         my($type_decl) = mapTypeName($bitmap->{TYPE});
1450         my($type_fn) = $bitmap->{BASE_TYPE};
1451
1452         $self->start_flags($bitmap, $ndr);
1453
1454         $self->pidl("ndr_print_$type_fn($ndr, name, $varname);");
1455
1456         $self->pidl("$ndr->depth++;");
1457         foreach my $e (@{$bitmap->{ELEMENTS}}) {
1458                 $self->ParseBitmapPrintElement($e, $bitmap, $ndr, $name, $varname);
1459         }
1460         $self->pidl("$ndr->depth--;");
1461
1462         $self->end_flags($bitmap, $ndr);
1463 }
1464
1465 sub DeclBitmap($$$$)
1466 {
1467         my ($e,$t,$name,$varname) = @_;
1468         return mapTypeName(Parse::Pidl::Typelist::bitmap_type_fn($e)) . 
1469                 ($t eq "pull"?" *":" ") . $varname;
1470 }
1471
1472 $typefamily{BITMAP} = {
1473         DECL => \&DeclBitmap,
1474         PUSH_FN_BODY => \&ParseBitmapPush,
1475         PULL_FN_BODY => \&ParseBitmapPull,
1476         PRINT_FN_BODY => \&ParseBitmapPrint,
1477 };
1478
1479 #####################################################################
1480 # generate a struct print function
1481 sub ParseStructPrint($$$$$)
1482 {
1483         my($self,$struct,$ndr,$name,$varname) = @_;
1484
1485         return unless defined $struct->{ELEMENTS};
1486
1487         my $env = GenerateStructEnv($struct, $varname);
1488
1489         $self->DeclareArrayVariables($_) foreach (@{$struct->{ELEMENTS}});
1490
1491         $self->pidl("ndr_print_struct($ndr, name, \"$name\");");
1492         $self->pidl("if (r == NULL) { ndr_print_null($ndr); return; }");
1493
1494         $self->start_flags($struct, $ndr);
1495
1496         $self->pidl("$ndr->depth++;");
1497         
1498         $self->ParseElementPrint($_, $ndr, $env->{$_->{NAME}}, $env)
1499                 foreach (@{$struct->{ELEMENTS}});
1500         $self->pidl("$ndr->depth--;");
1501
1502         $self->end_flags($struct, $ndr);
1503 }
1504
1505 sub DeclarePtrVariables($$)
1506 {
1507         my ($self,$e) = @_;
1508         foreach my $l (@{$e->{LEVELS}}) {
1509                 my $size = 32;
1510                 if ($l->{TYPE} eq "POINTER" and 
1511                         not ($l->{POINTER_TYPE} eq "ref" and $l->{LEVEL} eq "TOP")) {
1512                         if ($l->{POINTER_TYPE} eq "relative_short") {
1513                                 $size = 16;
1514                         }
1515                         $self->pidl("uint${size}_t _ptr_$e->{NAME};");
1516                         last;
1517                 }
1518         }
1519 }
1520
1521 sub DeclareArrayVariables($$)
1522 {
1523         my ($self,$e) = @_;
1524
1525         foreach my $l (@{$e->{LEVELS}}) {
1526                 next if has_fast_array($e,$l);
1527                 next if is_charset_array($e,$l);
1528                 if ($l->{TYPE} eq "ARRAY") {
1529                         $self->pidl("uint32_t cntr_$e->{NAME}_$l->{LEVEL_INDEX};");
1530                 }
1531         }
1532 }
1533
1534 sub DeclareArrayVariablesNoZero($$$)
1535 {
1536         my ($self,$e,$env) = @_;
1537
1538         foreach my $l (@{$e->{LEVELS}}) {
1539                 next if has_fast_array($e,$l);
1540                 next if is_charset_array($e,$l);
1541                 if ($l->{TYPE} eq "ARRAY") {
1542                     my $length = ParseExpr($l->{LENGTH_IS}, $env, $e->{ORIGINAL});
1543                     if ($length eq "0") {
1544                         warning($e->{ORIGINAL}, "pointless array cntr: 'cntr_$e->{NAME}_$l->{LEVEL_INDEX}': length=$length");
1545                     } else {
1546                         $self->pidl("uint32_t cntr_$e->{NAME}_$l->{LEVEL_INDEX};");
1547                     }
1548                 }
1549         }
1550 }
1551
1552 sub DeclareMemCtxVariables($$)
1553 {
1554         my ($self,$e) = @_;
1555         foreach my $l (@{$e->{LEVELS}}) {
1556                 my $mem_flags = $self->ParseMemCtxPullFlags($e, $l);
1557                 if (defined($mem_flags)) {
1558                         $self->pidl("TALLOC_CTX *_mem_save_$e->{NAME}_$l->{LEVEL_INDEX};");
1559                 }
1560         }
1561 }
1562
1563 sub ParseStructPullPrimitives($$$$$)
1564 {
1565         my($self,$struct,$ndr,$varname,$env) = @_;
1566
1567         if (defined $struct->{SURROUNDING_ELEMENT}) {
1568                 $self->pidl("NDR_CHECK(ndr_pull_array_size($ndr, &$varname->$struct->{SURROUNDING_ELEMENT}->{NAME}));");
1569         }
1570
1571         $self->pidl("NDR_CHECK(ndr_pull_align($ndr, $struct->{ALIGN}));");
1572
1573         if (defined($struct->{PROPERTIES}{relative_base})) {
1574                 # set the current offset as base for relative pointers
1575                 # and store it based on the toplevel struct/union
1576                 $self->pidl("NDR_CHECK(ndr_pull_setup_relative_base_offset1($ndr, $varname, $ndr->offset));");
1577         }
1578
1579         $self->ParseElementPull($_, $ndr, $env, 1, 0) foreach (@{$struct->{ELEMENTS}});
1580
1581         $self->add_deferred();
1582
1583         $self->pidl("NDR_CHECK(ndr_pull_trailer_align($ndr, $struct->{ALIGN}));");
1584 }
1585
1586 sub ParseStructPullDeferred($$$$$)
1587 {
1588         my ($self,$struct,$ndr,$varname,$env) = @_;
1589
1590         if (defined($struct->{PROPERTIES}{relative_base})) {
1591                 # retrieve the current offset as base for relative pointers
1592                 # based on the toplevel struct/union
1593                 $self->pidl("NDR_CHECK(ndr_pull_setup_relative_base_offset2($ndr, $varname));");
1594         }
1595         foreach my $e (@{$struct->{ELEMENTS}}) {
1596                 $self->ParseElementPull($e, $ndr, $env, 0, 1);
1597         }
1598
1599         $self->add_deferred();
1600 }
1601
1602 #####################################################################
1603 # parse a struct - pull side
1604 sub ParseStructPull($$$$)
1605 {
1606         my($self,$struct,$ndr,$varname) = @_;
1607
1608         return unless defined $struct->{ELEMENTS};
1609
1610         # declare any internal pointers we need
1611         foreach my $e (@{$struct->{ELEMENTS}}) {
1612                 $self->DeclarePtrVariables($e);
1613                 $self->DeclareArrayVariables($e);
1614                 $self->DeclareMemCtxVariables($e);
1615         }
1616
1617         $self->start_flags($struct, $ndr);
1618
1619         my $env = GenerateStructEnv($struct, $varname);
1620
1621         $self->pidl("if (ndr_flags & NDR_SCALARS) {");
1622         $self->indent;
1623         $self->ParseStructPullPrimitives($struct,$ndr,$varname,$env);
1624         $self->deindent;
1625         $self->pidl("}");
1626         $self->pidl("if (ndr_flags & NDR_BUFFERS) {");
1627         $self->indent;
1628         $self->ParseStructPullDeferred($struct,$ndr,$varname,$env);
1629         $self->deindent;
1630         $self->pidl("}");
1631
1632         $self->end_flags($struct, $ndr);
1633 }
1634
1635 #####################################################################
1636 # calculate size of ndr struct
1637 sub ParseStructNdrSize($$$$)
1638 {
1639         my ($self,$t, $name, $varname) = @_;
1640         my $sizevar;
1641
1642         if (my $flags = has_property($t, "flag")) {
1643                 $self->pidl("flags |= $flags;");
1644         }
1645         $self->pidl("return ndr_size_struct($varname, flags, (ndr_push_flags_fn_t)ndr_push_$name);");
1646 }
1647
1648 sub DeclStruct($$$$)
1649 {
1650         my ($e,$t,$name,$varname) = @_;
1651         return ($t ne "pull"?"const ":"") . "struct $name *$varname";
1652 }
1653
1654 sub ArgsStructNdrSize($$$)
1655 {
1656         my ($d, $name, $varname) = @_;
1657         return "const struct $name *$varname, int flags";
1658 }
1659
1660 $typefamily{STRUCT} = {
1661         PUSH_FN_BODY => \&ParseStructPush,
1662         DECL => \&DeclStruct,
1663         PULL_FN_BODY => \&ParseStructPull,
1664         PRINT_FN_BODY => \&ParseStructPrint,
1665         SIZE_FN_BODY => \&ParseStructNdrSize,
1666         SIZE_FN_ARGS => \&ArgsStructNdrSize,
1667 };
1668
1669 #####################################################################
1670 # calculate size of ndr struct
1671 sub ParseUnionNdrSize($$$)
1672 {
1673         my ($self, $t, $name, $varname) = @_;
1674         my $sizevar;
1675
1676         if (my $flags = has_property($t, "flag")) {
1677                 $self->pidl("flags |= $flags;");
1678         }
1679
1680         $self->pidl("return ndr_size_union($varname, flags, level, (ndr_push_flags_fn_t)ndr_push_$name);");
1681 }
1682
1683 sub ParseUnionPushPrimitives($$$$)
1684 {
1685         my ($self, $e, $ndr ,$varname) = @_;
1686
1687         my $have_default = 0;
1688
1689         $self->pidl("uint32_t level = ndr_push_get_switch_value($ndr, $varname);");
1690
1691         if (defined($e->{SWITCH_TYPE})) {
1692                 $self->pidl("NDR_CHECK(ndr_push_$e->{SWITCH_TYPE}($ndr, NDR_SCALARS, level));");
1693         }
1694
1695         if (defined($e->{ALIGN})) {
1696                 $self->pidl("NDR_CHECK(ndr_push_union_align($ndr, $e->{ALIGN}));");
1697         }
1698
1699         $self->pidl("switch (level) {");
1700         $self->indent;
1701         foreach my $el (@{$e->{ELEMENTS}}) {
1702                 if ($el->{CASE} eq "default") {
1703                         $have_default = 1;
1704                 }
1705                 $self->pidl("$el->{CASE}: {");
1706
1707                 if ($el->{TYPE} ne "EMPTY") {
1708                         $self->indent;
1709                         if (defined($e->{PROPERTIES}{relative_base})) {
1710                                 $self->pidl("NDR_CHECK(ndr_push_align($ndr, $el->{ALIGN}));");
1711                                 # set the current offset as base for relative pointers
1712                                 # and store it based on the toplevel struct/union
1713                                 $self->pidl("NDR_CHECK(ndr_push_setup_relative_base_offset1($ndr, $varname, $ndr->offset));");
1714                         }
1715                         $self->DeclareArrayVariables($el);
1716                         $self->ParseElementPush($el, $ndr, {$el->{NAME} => "$varname->$el->{NAME}"}, 1, 0);
1717                         $self->deindent;
1718                 }
1719                 $self->pidl("break; }");
1720                 $self->pidl("");
1721         }
1722         if (! $have_default) {
1723                 $self->pidl("default:");
1724                 $self->pidl("\treturn ndr_push_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u at \%s\", level, __location__);");
1725         }
1726         $self->deindent;
1727         $self->pidl("}");
1728 }
1729
1730 sub ParseUnionPushDeferred($$$$)
1731 {
1732         my ($self,$e,$ndr,$varname) = @_;
1733
1734         my $have_default = 0;
1735
1736         $self->pidl("uint32_t level = ndr_push_get_switch_value($ndr, $varname);");
1737         if (defined($e->{PROPERTIES}{relative_base})) {
1738                 # retrieve the current offset as base for relative pointers
1739                 # based on the toplevel struct/union
1740                 $self->pidl("NDR_CHECK(ndr_push_setup_relative_base_offset2($ndr, $varname));");
1741         }
1742         $self->pidl("switch (level) {");
1743         $self->indent;
1744         foreach my $el (@{$e->{ELEMENTS}}) {
1745                 if ($el->{CASE} eq "default") {
1746                         $have_default = 1;
1747                 }
1748
1749                 $self->pidl("$el->{CASE}:");
1750                 if ($el->{TYPE} ne "EMPTY") {
1751                         $self->indent;
1752                         $self->ParseElementPush($el, $ndr, {$el->{NAME} => "$varname->$el->{NAME}"}, 0, 1);
1753                         $self->deindent;
1754                 }
1755                 $self->pidl("break;");
1756                 $self->pidl("");
1757         }
1758         if (! $have_default) {
1759                 $self->pidl("default:");
1760                 $self->pidl("\treturn ndr_push_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u at \%s\", level, __location__);");
1761         }
1762         $self->deindent;
1763         $self->pidl("}");
1764 }
1765
1766 #####################################################################
1767 # parse a union - push side
1768 sub ParseUnionPush($$$$)
1769 {
1770         my ($self,$e,$ndr,$varname) = @_;
1771         my $have_default = 0;
1772
1773         $self->start_flags($e, $ndr);
1774
1775         $self->pidl("if (ndr_flags & NDR_SCALARS) {");
1776         $self->indent;
1777         $self->ParseUnionPushPrimitives($e, $ndr, $varname);
1778         $self->deindent;
1779         $self->pidl("}");
1780         $self->pidl("if (ndr_flags & NDR_BUFFERS) {");
1781         $self->indent;
1782         $self->ParseUnionPushDeferred($e, $ndr, $varname);
1783         $self->deindent;
1784         $self->pidl("}");
1785         $self->end_flags($e, $ndr);
1786 }
1787
1788 #####################################################################
1789 # print a union
1790 sub ParseUnionPrint($$$$$)
1791 {
1792         my ($self,$e,$ndr,$name,$varname) = @_;
1793         my $have_default = 0;
1794
1795         $self->pidl("uint32_t level;");
1796         foreach my $el (@{$e->{ELEMENTS}}) {
1797                 $self->DeclareArrayVariables($el);
1798         }
1799
1800         $self->start_flags($e, $ndr);
1801
1802         $self->pidl("level = ndr_print_get_switch_value($ndr, $varname);");
1803
1804         $self->pidl("ndr_print_union($ndr, name, level, \"$name\");");
1805
1806         $self->pidl("switch (level) {");
1807         $self->indent;
1808         foreach my $el (@{$e->{ELEMENTS}}) {
1809                 if ($el->{CASE} eq "default") {
1810                         $have_default = 1;
1811                 }
1812                 $self->pidl("$el->{CASE}:");
1813                 if ($el->{TYPE} ne "EMPTY") {
1814                         $self->indent;
1815                         $self->ParseElementPrint($el, $ndr, "$varname->$el->{NAME}", {});
1816                         $self->deindent;
1817                 }
1818                 $self->pidl("break;");
1819                 $self->pidl("");
1820         }
1821         if (! $have_default) {
1822                 $self->pidl("default:");
1823                 $self->pidl("\tndr_print_bad_level($ndr, name, level);");
1824         }
1825         $self->deindent;
1826         $self->pidl("}");
1827
1828         $self->end_flags($e, $ndr);
1829 }
1830
1831 sub ParseUnionPullPrimitives($$$$$)
1832 {
1833         my ($self,$e,$ndr,$varname,$switch_type) = @_;
1834         my $have_default = 0;
1835
1836         if (defined($switch_type)) {
1837                 $self->pidl("NDR_CHECK(ndr_pull_$switch_type($ndr, NDR_SCALARS, &_level));");
1838                 $self->pidl("if (_level != level) {"); 
1839                 $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value %u for $varname at \%s\", _level, __location__);");
1840                 $self->pidl("}");
1841         }
1842
1843         if (defined($e->{ALIGN})) {
1844                 $self->pidl("NDR_CHECK(ndr_pull_union_align($ndr, $e->{ALIGN}));");
1845         }
1846
1847         $self->pidl("switch (level) {");
1848         $self->indent;
1849         foreach my $el (@{$e->{ELEMENTS}}) {
1850                 if ($el->{CASE} eq "default") {
1851                         $have_default = 1;
1852                 } 
1853                 $self->pidl("$el->{CASE}: {");
1854
1855                 if ($el->{TYPE} ne "EMPTY") {
1856                         $self->indent;
1857                         $self->DeclarePtrVariables($el);
1858                         $self->DeclareArrayVariables($el);
1859                         if (defined($e->{PROPERTIES}{relative_base})) {
1860                                 $self->pidl("NDR_CHECK(ndr_pull_align($ndr, $el->{ALIGN}));");
1861                                 # set the current offset as base for relative pointers
1862                                 # and store it based on the toplevel struct/union
1863                                 $self->pidl("NDR_CHECK(ndr_pull_setup_relative_base_offset1($ndr, $varname, $ndr->offset));");
1864                         }
1865                         $self->ParseElementPull($el, $ndr, {$el->{NAME} => "$varname->$el->{NAME}"}, 1, 0);
1866                         $self->deindent;
1867                 }
1868                 $self->pidl("break; }");
1869                 $self->pidl("");
1870         }
1871         if (! $have_default) {
1872                 $self->pidl("default:");
1873                 $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u at \%s\", level, __location__);");
1874         }
1875         $self->deindent;
1876         $self->pidl("}");
1877 }
1878
1879 sub ParseUnionPullDeferred($$$$)
1880 {
1881         my ($self,$e,$ndr,$varname) = @_;
1882         my $have_default = 0;
1883
1884         if (defined($e->{PROPERTIES}{relative_base})) {
1885                 # retrieve the current offset as base for relative pointers
1886                 # based on the toplevel struct/union
1887                 $self->pidl("NDR_CHECK(ndr_pull_setup_relative_base_offset2($ndr, $varname));");
1888         }
1889         $self->pidl("switch (level) {");
1890         $self->indent;
1891         foreach my $el (@{$e->{ELEMENTS}}) {
1892                 if ($el->{CASE} eq "default") {
1893                         $have_default = 1;
1894                 } 
1895
1896                 $self->pidl("$el->{CASE}:");
1897                 if ($el->{TYPE} ne "EMPTY") {
1898                         $self->indent;
1899                         $self->ParseElementPull($el, $ndr, {$el->{NAME} => "$varname->$el->{NAME}"}, 0, 1);
1900                         $self->deindent;
1901                 }
1902                 $self->pidl("break;");
1903                 $self->pidl("");
1904         }
1905         if (! $have_default) {
1906                 $self->pidl("default:");
1907                 $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u at \%s\", level, __location__);");
1908         }
1909         $self->deindent;
1910         $self->pidl("}");
1911
1912
1913 }
1914
1915 #####################################################################
1916 # parse a union - pull side
1917 sub ParseUnionPull($$$$)
1918 {
1919         my ($self,$e,$ndr,$varname) = @_;
1920         my $switch_type = $e->{SWITCH_TYPE};
1921
1922         $self->pidl("uint32_t level;");
1923         if (defined($switch_type)) {
1924                 if (Parse::Pidl::Typelist::typeIs($switch_type, "ENUM")) {
1925                         $switch_type = Parse::Pidl::Typelist::enum_type_fn(getType($switch_type)->{DATA});
1926                 }
1927                 $self->pidl(mapTypeName($switch_type) . " _level;");
1928         }
1929
1930         my %double_cases = ();
1931         foreach my $el (@{$e->{ELEMENTS}}) {
1932                 next if ($el->{TYPE} eq "EMPTY");
1933                 next if ($double_cases{"$el->{NAME}"});
1934                 $self->DeclareMemCtxVariables($el);
1935                 $double_cases{"$el->{NAME}"} = 1;
1936         }
1937
1938         $self->start_flags($e, $ndr);
1939
1940         $self->pidl("level = ndr_pull_get_switch_value($ndr, $varname);");
1941
1942         $self->pidl("if (ndr_flags & NDR_SCALARS) {");
1943         $self->indent;
1944         $self->ParseUnionPullPrimitives($e,$ndr,$varname,$switch_type);
1945         $self->deindent;
1946         $self->pidl("}");
1947
1948         $self->pidl("if (ndr_flags & NDR_BUFFERS) {");
1949         $self->indent;
1950         $self->ParseUnionPullDeferred($e,$ndr,$varname);
1951         $self->deindent;
1952         $self->pidl("}");
1953
1954         $self->add_deferred();
1955
1956         $self->end_flags($e, $ndr);
1957 }
1958
1959 sub DeclUnion($$$$)
1960 {
1961         my ($e,$t,$name,$varname) = @_;
1962         return ($t ne "pull"?"const ":"") . "union $name *$varname";
1963 }
1964
1965 sub ArgsUnionNdrSize($$)
1966 {
1967         my ($d,$name) = @_;
1968         return "const union $name *r, uint32_t level, int flags";
1969 }
1970
1971 $typefamily{UNION} = {
1972         PUSH_FN_BODY => \&ParseUnionPush,
1973         DECL => \&DeclUnion,
1974         PULL_FN_BODY => \&ParseUnionPull,
1975         PRINT_FN_BODY => \&ParseUnionPrint,
1976         SIZE_FN_ARGS => \&ArgsUnionNdrSize,
1977         SIZE_FN_BODY => \&ParseUnionNdrSize,
1978 };
1979         
1980 #####################################################################
1981 # parse a typedef - push side
1982 sub ParseTypedefPush($$$$)
1983 {
1984         my($self,$e,$ndr,$varname) = @_;
1985
1986         my $env;
1987
1988         $env->{$e->{NAME}} = $varname;
1989
1990         $self->ParseElementPushLevel($e, $e->{LEVELS}[0], $ndr, $varname, $env, 1, 1);
1991 }
1992
1993 #####################################################################
1994 # parse a typedef - pull side
1995 sub ParseTypedefPull($$$$)
1996 {
1997         my($self,$e,$ndr,$varname) = @_;
1998
1999         my $env;
2000
2001         $env->{$e->{NAME}} = $varname;
2002
2003         $self->ParseElementPullLevel($e, $e->{LEVELS}[0], $ndr, $varname, $env, 1, 1);
2004 }
2005
2006 #####################################################################
2007 # parse a typedef - print side
2008 sub ParseTypedefPrint($$$$$)
2009 {
2010         my($self,$e,$ndr,$name,$varname) = @_;
2011
2012         $typefamily{$e->{DATA}->{TYPE}}->{PRINT_FN_BODY}->($self, $e->{DATA}, $ndr, $name, $varname);
2013 }
2014
2015 #####################################################################
2016 ## calculate the size of a structure
2017 sub ParseTypedefNdrSize($$$$)
2018 {
2019         my($self,$t,$name,$varname) = @_;
2020
2021         $typefamily{$t->{DATA}->{TYPE}}->{SIZE_FN_BODY}->($self, $t->{DATA}, $name, $varname);
2022 }
2023
2024 sub DeclTypedef($$$$)
2025 {
2026         my ($e, $t, $name, $varname) = @_;
2027         
2028         return $typefamily{$e->{DATA}->{TYPE}}->{DECL}->($e->{DATA}, $t, $name, $varname);
2029 }
2030
2031 sub ArgsTypedefNdrSize($$$)
2032 {
2033         my ($d, $name, $varname) = @_;
2034         return $typefamily{$d->{DATA}->{TYPE}}->{SIZE_FN_ARGS}->($d->{DATA}, $name, $varname);
2035 }
2036
2037 $typefamily{TYPEDEF} = {
2038         PUSH_FN_BODY => \&ParseTypedefPush,
2039         DECL => \&DeclTypedef,
2040         PULL_FN_BODY => \&ParseTypedefPull,
2041         PRINT_FN_BODY => \&ParseTypedefPrint,
2042         SIZE_FN_ARGS => \&ArgsTypedefNdrSize,
2043         SIZE_FN_BODY => \&ParseTypedefNdrSize,
2044 };
2045
2046 #####################################################################
2047 # parse a function - print side
2048 sub ParseFunctionPrint($$)
2049 {
2050         my($self, $fn) = @_;
2051         my $ndr = "ndr";
2052
2053         $self->pidl_hdr("void ndr_print_$fn->{NAME}(struct ndr_print *$ndr, const char *name, int flags, const struct $fn->{NAME} *r);");
2054
2055         return if has_property($fn, "noprint");
2056
2057         $self->pidl("_PUBLIC_ void ndr_print_$fn->{NAME}(struct ndr_print *$ndr, const char *name, int flags, const struct $fn->{NAME} *r)");
2058         $self->pidl("{");
2059         $self->indent;
2060
2061         foreach my $e (@{$fn->{ELEMENTS}}) {
2062                 $self->DeclareArrayVariables($e);
2063         }
2064
2065         $self->pidl("ndr_print_struct($ndr, name, \"$fn->{NAME}\");");
2066         $self->pidl("if (r == NULL) { ndr_print_null($ndr); return; }");
2067         $self->pidl("$ndr->depth++;");
2068
2069         $self->pidl("if (flags & NDR_SET_VALUES) {");
2070         $self->pidl("\t$ndr->flags |= LIBNDR_PRINT_SET_VALUES;");
2071         $self->pidl("}");
2072
2073         $self->pidl("if (flags & NDR_IN) {");
2074         $self->indent;
2075         $self->pidl("ndr_print_struct($ndr, \"in\", \"$fn->{NAME}\");");
2076         $self->pidl("$ndr->depth++;");
2077
2078         my $env = GenerateFunctionInEnv($fn);
2079
2080         foreach my $e (@{$fn->{ELEMENTS}}) {
2081                 if (grep(/in/,@{$e->{DIRECTION}})) {
2082                         $self->ParseElementPrint($e, $ndr, $env->{$e->{NAME}}, $env);
2083                 }
2084         }
2085         $self->pidl("$ndr->depth--;");
2086         $self->deindent;
2087         $self->pidl("}");
2088         
2089         $self->pidl("if (flags & NDR_OUT) {");
2090         $self->indent;
2091         $self->pidl("ndr_print_struct($ndr, \"out\", \"$fn->{NAME}\");");
2092         $self->pidl("$ndr->depth++;");
2093
2094         $env = GenerateFunctionOutEnv($fn);
2095         foreach my $e (@{$fn->{ELEMENTS}}) {
2096                 if (grep(/out/,@{$e->{DIRECTION}})) {
2097                         $self->ParseElementPrint($e, $ndr, $env->{$e->{NAME}}, $env);
2098                 }
2099         }
2100         if ($fn->{RETURN_TYPE}) {
2101                 $self->pidl("ndr_print_$fn->{RETURN_TYPE}($ndr, \"result\", r->out.result);");
2102         }
2103         $self->pidl("$ndr->depth--;");
2104         $self->deindent;
2105         $self->pidl("}");
2106         
2107         $self->pidl("$ndr->depth--;");
2108         $self->deindent;
2109         $self->pidl("}");
2110         $self->pidl("");
2111 }
2112
2113 #####################################################################
2114 # parse a function
2115 sub ParseFunctionPush($$)
2116
2117         my($self, $fn) = @_;
2118         my $ndr = "ndr";
2119
2120         $self->fn_declare("push", $fn, "enum ndr_err_code ndr_push_$fn->{NAME}(struct ndr_push *$ndr, int flags, const struct $fn->{NAME} *r)") or return;
2121
2122         return if has_property($fn, "nopush");
2123
2124         $self->pidl("{");
2125         $self->indent;
2126
2127         foreach my $e (@{$fn->{ELEMENTS}}) { 
2128                 $self->DeclareArrayVariables($e);
2129         }
2130
2131         $self->pidl("if (flags & NDR_IN) {");
2132         $self->indent;
2133
2134         my $env = GenerateFunctionInEnv($fn);
2135
2136         EnvSubstituteValue($env, $fn);
2137
2138         foreach my $e (@{$fn->{ELEMENTS}}) {
2139                 if (grep(/in/,@{$e->{DIRECTION}})) {
2140                         $self->ParseElementPush($e, $ndr, $env, 1, 1);
2141                 }
2142         }
2143
2144         $self->deindent;
2145         $self->pidl("}");
2146
2147         $self->pidl("if (flags & NDR_OUT) {");
2148         $self->indent;
2149
2150         $env = GenerateFunctionOutEnv($fn);
2151         foreach my $e (@{$fn->{ELEMENTS}}) {
2152                 if (grep(/out/,@{$e->{DIRECTION}})) {
2153                         $self->ParseElementPush($e, $ndr, $env, 1, 1);
2154                 }
2155         }
2156
2157         if ($fn->{RETURN_TYPE}) {
2158                 $self->pidl("NDR_CHECK(ndr_push_$fn->{RETURN_TYPE}($ndr, NDR_SCALARS, r->out.result));");
2159         }
2160     
2161         $self->deindent;
2162         $self->pidl("}");
2163         $self->pidl("return NDR_ERR_SUCCESS;");
2164         $self->deindent;
2165         $self->pidl("}");
2166         $self->pidl("");
2167 }
2168
2169 sub AllocateArrayLevel($$$$$$)
2170 {
2171         my ($self,$e,$l,$ndr,$var,$size) = @_;
2172
2173         my $pl = GetPrevLevel($e, $l);
2174         if (defined($pl) and 
2175             $pl->{TYPE} eq "POINTER" and 
2176             $pl->{POINTER_TYPE} eq "ref"
2177             and not $l->{IS_ZERO_TERMINATED}) {
2178                 $self->pidl("if ($ndr->flags & LIBNDR_FLAG_REF_ALLOC) {");
2179                 $self->pidl("\tNDR_PULL_ALLOC_N($ndr, $var, $size);");
2180                 $self->pidl("}");
2181                 if (grep(/in/,@{$e->{DIRECTION}}) and
2182                     grep(/out/,@{$e->{DIRECTION}})) {
2183                         $self->pidl("memcpy(r->out.$e->{NAME}, r->in.$e->{NAME}, ($size) * sizeof(*r->in.$e->{NAME}));");
2184                 }
2185                 return;
2186         }
2187
2188         $self->pidl("NDR_PULL_ALLOC_N($ndr, $var, $size);");
2189 }
2190
2191 #####################################################################
2192 # parse a function
2193 sub ParseFunctionPull($$)
2194
2195         my($self,$fn) = @_;
2196         my $ndr = "ndr";
2197
2198         # pull function args
2199         $self->fn_declare("pull", $fn, "enum ndr_err_code ndr_pull_$fn->{NAME}(struct ndr_pull *$ndr, int flags, struct $fn->{NAME} *r)") or return;
2200
2201         $self->pidl("{");
2202         $self->indent;
2203
2204         # declare any internal pointers we need
2205         foreach my $e (@{$fn->{ELEMENTS}}) { 
2206                 $self->DeclarePtrVariables($e);
2207                 $self->DeclareArrayVariables($e);
2208         }
2209
2210         my %double_cases = ();
2211         foreach my $e (@{$fn->{ELEMENTS}}) {
2212                 next if ($e->{TYPE} eq "EMPTY");
2213                 next if ($double_cases{"$e->{NAME}"});
2214                 $self->DeclareMemCtxVariables($e);
2215                 $double_cases{"$e->{NAME}"} = 1;
2216         }
2217
2218         $self->pidl("if (flags & NDR_IN) {");
2219         $self->indent;
2220
2221         # auto-init the out section of a structure. I originally argued that
2222         # this was a bad idea as it hides bugs, but coping correctly
2223         # with initialisation and not wiping ref vars is turning
2224         # out to be too tricky (tridge)
2225         foreach my $e (@{$fn->{ELEMENTS}}) {
2226                 next unless grep(/out/, @{$e->{DIRECTION}});
2227                 $self->pidl("ZERO_STRUCT(r->out);");
2228                 $self->pidl("");
2229                 last;
2230         }
2231
2232         my $env = GenerateFunctionInEnv($fn);
2233
2234         foreach my $e (@{$fn->{ELEMENTS}}) {
2235                 next unless (grep(/in/, @{$e->{DIRECTION}}));
2236                 $self->ParseElementPull($e, $ndr, $env, 1, 1);
2237         }
2238
2239         # allocate the "simple" out ref variables. FIXME: Shouldn't this have it's
2240         # own flag rather than be in NDR_IN ?
2241
2242         foreach my $e (@{$fn->{ELEMENTS}}) {
2243                 next unless (grep(/out/, @{$e->{DIRECTION}}));
2244                 next unless ($e->{LEVELS}[0]->{TYPE} eq "POINTER" and 
2245                              $e->{LEVELS}[0]->{POINTER_TYPE} eq "ref");
2246                 next if (($e->{LEVELS}[1]->{TYPE} eq "DATA") and 
2247                                  ($e->{LEVELS}[1]->{DATA_TYPE} eq "string"));
2248                 next if (($e->{LEVELS}[1]->{TYPE} eq "ARRAY") 
2249                         and   $e->{LEVELS}[1]->{IS_ZERO_TERMINATED});
2250
2251                 if ($e->{LEVELS}[1]->{TYPE} eq "ARRAY") {
2252                         my $size = ParseExprExt($e->{LEVELS}[1]->{SIZE_IS}, $env, $e->{ORIGINAL},
2253                                 check_null_pointer($e, $env, sub { $self->pidl(shift); },
2254                                                    "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for size_is()\");"),
2255                                 check_fully_dereferenced($e, $env));
2256                         $self->pidl("NDR_PULL_ALLOC_N($ndr, r->out.$e->{NAME}, $size);");
2257
2258                         if (grep(/in/, @{$e->{DIRECTION}})) {
2259                                 $self->pidl("memcpy(r->out.$e->{NAME}, r->in.$e->{NAME}, ($size) * sizeof(*r->in.$e->{NAME}));");
2260                         } else {
2261                                 $self->pidl("memset(r->out.$e->{NAME}, 0, ($size) * sizeof(*r->out.$e->{NAME}));");
2262                         }
2263                 } else {
2264                         $self->pidl("NDR_PULL_ALLOC($ndr, r->out.$e->{NAME});");
2265                 
2266                         if (grep(/in/, @{$e->{DIRECTION}})) {
2267                                 $self->pidl("*r->out.$e->{NAME} = *r->in.$e->{NAME};");
2268                         } else {
2269                                 $self->pidl("ZERO_STRUCTP(r->out.$e->{NAME});");
2270                         }
2271                 }
2272         }
2273
2274         $self->add_deferred();
2275         $self->deindent;
2276         $self->pidl("}");
2277         
2278         $self->pidl("if (flags & NDR_OUT) {");
2279         $self->indent;
2280
2281         $env = GenerateFunctionOutEnv($fn);
2282         foreach my $e (@{$fn->{ELEMENTS}}) {
2283                 next unless grep(/out/, @{$e->{DIRECTION}});
2284                 $self->ParseElementPull($e, $ndr, $env, 1, 1);
2285         }
2286
2287         if ($fn->{RETURN_TYPE}) {
2288                 $self->pidl("NDR_CHECK(ndr_pull_$fn->{RETURN_TYPE}($ndr, NDR_SCALARS, &r->out.result));");
2289         }
2290
2291         $self->add_deferred();
2292         $self->deindent;
2293         $self->pidl("}");
2294
2295         $self->pidl("return NDR_ERR_SUCCESS;");
2296         $self->deindent;
2297         $self->pidl("}");
2298         $self->pidl("");
2299 }
2300
2301 sub AuthServiceStruct($$$)
2302 {
2303         my ($self, $ifacename, $authservice) = @_;
2304         my @a = split /,/, $authservice;
2305         my $authservice_count = $#a + 1;
2306
2307         $self->pidl("static const char * const $ifacename\_authservice_strings[] = {");
2308         foreach my $ap (@a) {
2309                 $self->pidl("\t$ap, ");
2310         }
2311         $self->pidl("};");
2312         $self->pidl("");
2313
2314         $self->pidl("static const struct ndr_interface_string_array $ifacename\_authservices = {");
2315         $self->pidl("\t.count\t= $authservice_count,");
2316         $self->pidl("\t.names\t= $ifacename\_authservice_strings");
2317         $self->pidl("};");
2318         $self->pidl("");
2319 }
2320
2321 sub FunctionCallEntry($$)
2322 {
2323         my ($self, $d) = @_;
2324         return 0 if not defined($d->{OPNUM});
2325         $self->pidl("\t{");
2326         $self->pidl("\t\t\"$d->{NAME}\",");
2327         $self->pidl("\t\tsizeof(struct $d->{NAME}),");
2328         $self->pidl("\t\t(ndr_push_flags_fn_t) ndr_push_$d->{NAME},");
2329         $self->pidl("\t\t(ndr_pull_flags_fn_t) ndr_pull_$d->{NAME},");
2330         $self->pidl("\t\t(ndr_print_function_t) ndr_print_$d->{NAME},");
2331         $self->pidl("\t},");
2332         return 1;
2333 }
2334
2335 #####################################################################
2336 # produce a function call table
2337 sub FunctionTable($$)
2338 {
2339         my($self,$interface) = @_;
2340         my $count = 0;
2341         my $uname = uc $interface->{NAME};
2342
2343         return if ($#{$interface->{FUNCTIONS}}+1 == 0);
2344         return unless defined ($interface->{PROPERTIES}->{uuid});
2345
2346         $self->pidl("static const struct ndr_interface_call $interface->{NAME}\_calls[] = {");
2347
2348         foreach my $d (@{$interface->{INHERITED_FUNCTIONS}},@{$interface->{FUNCTIONS}}) {
2349                 $count += $self->FunctionCallEntry($d);
2350         }
2351         $self->pidl("\t{ NULL, 0, NULL, NULL, NULL }");
2352         $self->pidl("};");
2353         $self->pidl("");
2354
2355         $self->pidl("static const char * const $interface->{NAME}\_endpoint_strings[] = {");
2356         foreach my $ep (@{$interface->{ENDPOINTS}}) {
2357                 $self->pidl("\t$ep, ");
2358         }
2359         my $endpoint_count = $#{$interface->{ENDPOINTS}}+1;
2360         
2361         $self->pidl("};");
2362         $self->pidl("");
2363
2364         $self->pidl("static const struct ndr_interface_string_array $interface->{NAME}\_endpoints = {");
2365         $self->pidl("\t.count\t= $endpoint_count,");
2366         $self->pidl("\t.names\t= $interface->{NAME}\_endpoint_strings");
2367         $self->pidl("};");
2368         $self->pidl("");
2369
2370         if (! defined $interface->{PROPERTIES}->{authservice}) {
2371                 $interface->{PROPERTIES}->{authservice} = "\"host\"";
2372         }
2373
2374         $self->AuthServiceStruct($interface->{NAME}, 
2375                                      $interface->{PROPERTIES}->{authservice});
2376
2377         $self->pidl("\nconst struct ndr_interface_table ndr_table_$interface->{NAME} = {");
2378         $self->pidl("\t.name\t\t= \"$interface->{NAME}\",");
2379         $self->pidl("\t.syntax_id\t= {");
2380         $self->pidl("\t\t" . print_uuid($interface->{UUID}) .",");
2381         $self->pidl("\t\tNDR_$uname\_VERSION");
2382         $self->pidl("\t},");
2383         $self->pidl("\t.helpstring\t= NDR_$uname\_HELPSTRING,");
2384         $self->pidl("\t.num_calls\t= $count,");
2385         $self->pidl("\t.calls\t\t= $interface->{NAME}\_calls,");
2386         $self->pidl("\t.endpoints\t= &$interface->{NAME}\_endpoints,");
2387         $self->pidl("\t.authservices\t= &$interface->{NAME}\_authservices");
2388         $self->pidl("};");
2389         $self->pidl("");
2390
2391 }
2392
2393 #####################################################################
2394 # generate include statements for imported idl files
2395 sub HeaderImport
2396 {
2397         my $self = shift;
2398         my @imports = @_;
2399         foreach (@imports) {
2400                 $_ = unmake_str($_);
2401                 s/\.idl$//;
2402                 $self->pidl(choose_header("librpc/gen_ndr/ndr_$_\.h", "gen_ndr/ndr_$_.h"));
2403         }
2404 }
2405
2406 #####################################################################
2407 # generate include statements for included header files
2408 sub HeaderInclude
2409 {
2410         my $self = shift;
2411         my @includes = @_;
2412         foreach (@includes) {
2413                 $self->pidl_hdr("#include $_");
2414         }
2415 }
2416
2417 #####################################################################
2418 # generate prototypes and defines for the interface definitions
2419 # FIXME: these prototypes are for the DCE/RPC client functions, not the 
2420 # NDR parser and so do not belong here, technically speaking
2421 sub HeaderInterface($$$)
2422 {
2423         my($self,$interface,$needed) = @_;
2424
2425         my $count = 0;
2426
2427         if ($needed->{"compression"}) {
2428                 $self->pidl(choose_header("librpc/ndr/ndr_compression.h", "ndr/compression.h"));
2429         }
2430
2431         if (has_property($interface, "object")) {
2432                 $self->pidl(choose_header("librpc/gen_ndr/ndr_orpc.h", "ndr/orpc.h"));
2433         }
2434
2435         if (defined $interface->{PROPERTIES}->{helper}) {
2436                 $self->HeaderInclude(split /,/, $interface->{PROPERTIES}->{helper});
2437         }
2438
2439         if (defined $interface->{PROPERTIES}->{uuid}) {
2440                 my $name = uc $interface->{NAME};
2441                 $self->pidl_hdr("#define NDR_$name\_UUID " . 
2442                 Parse::Pidl::Util::make_str(lc($interface->{PROPERTIES}->{uuid})));
2443
2444                 if(!defined $interface->{PROPERTIES}->{version}) { $interface->{PROPERTIES}->{version} = "0.0"; }
2445                 $self->pidl_hdr("#define NDR_$name\_VERSION $interface->{PROPERTIES}->{version}");
2446
2447                 $self->pidl_hdr("#define NDR_$name\_NAME \"$interface->{NAME}\"");
2448
2449                 if(!defined $interface->{PROPERTIES}->{helpstring}) { $interface->{PROPERTIES}->{helpstring} = "NULL"; }
2450                 $self->pidl_hdr("#define NDR_$name\_HELPSTRING $interface->{PROPERTIES}->{helpstring}");
2451
2452                 $self->pidl_hdr("extern const struct ndr_interface_table ndr_table_$interface->{NAME};");
2453         }
2454
2455         foreach (@{$interface->{FUNCTIONS}}) {
2456                 next if has_property($_, "noopnum");
2457                 next if grep(/^$_->{NAME}$/,@{$interface->{INHERITED_FUNCTIONS}});
2458                 my $u_name = uc $_->{NAME};
2459         
2460                 my $val = sprintf("0x%02x", $count);
2461                 if (defined($interface->{BASE})) {
2462                         $val .= " + NDR_" . uc $interface->{BASE} . "_CALL_COUNT";
2463                 }
2464                 
2465                 $self->pidl_hdr("#define NDR_$u_name ($val)");
2466
2467                 $self->pidl_hdr("");
2468                 $count++;
2469         }
2470
2471         my $val = $count;
2472
2473         if (defined($interface->{BASE})) {
2474                 $val .= " + NDR_" . uc $interface->{BASE} . "_CALL_COUNT";
2475         }
2476
2477         $self->pidl_hdr("#define NDR_" . uc $interface->{NAME} . "_CALL_COUNT ($val)");
2478
2479 }
2480
2481 sub ParseTypePush($$$$$$)
2482 {
2483         my ($self,$e, $ndr, $varname, $primitives, $deferred) = @_;
2484
2485         # save the old relative_base_offset
2486         $self->pidl("uint32_t _save_relative_base_offset = ndr_push_get_relative_base_offset($ndr);") if defined(has_property($e, "relative_base"));
2487         $typefamily{$e->{TYPE}}->{PUSH_FN_BODY}->($self, $e, $ndr, $varname);
2488         # restore the old relative_base_offset
2489         $self->pidl("ndr_push_restore_relative_base_offset($ndr, _save_relative_base_offset);") if defined(has_property($e, "relative_base"));
2490 }
2491
2492 sub ParseTypePushFunction($$$)
2493 {
2494         my ($self, $e, $varname) = @_;
2495         my $ndr = "ndr";
2496
2497         my $args = $typefamily{$e->{TYPE}}->{DECL}->($e, "push", $e->{NAME}, $varname);
2498
2499         $self->fn_declare("push", $e, "enum ndr_err_code ".TypeFunctionName("ndr_push", $e)."(struct ndr_push *$ndr, int ndr_flags, $args)") or return;
2500
2501         $self->pidl("{");
2502         $self->indent;
2503         $self->ParseTypePush($e, $ndr, $varname, 1, 1);
2504         $self->pidl("return NDR_ERR_SUCCESS;");
2505         $self->deindent;
2506         $self->pidl("}");
2507         $self->pidl("");;
2508 }
2509
2510 sub ParseTypePull($$$$$$)
2511 {
2512         my ($self, $e, $ndr, $varname, $primitives, $deferred) = @_;
2513
2514         # save the old relative_base_offset
2515         $self->pidl("uint32_t _save_relative_base_offset = ndr_pull_get_relative_base_offset($ndr);") if defined(has_property($e, "relative_base"));
2516         $typefamily{$e->{TYPE}}->{PULL_FN_BODY}->($self, $e, $ndr, $varname);
2517         # restore the old relative_base_offset
2518         $self->pidl("ndr_pull_restore_relative_base_offset($ndr, _save_relative_base_offset);") if defined(has_property($e, "relative_base"));
2519 }
2520
2521 sub ParseTypePullFunction($$)
2522 {
2523         my ($self, $e, $varname) = @_;
2524         my $ndr = "ndr";
2525
2526         my $args = $typefamily{$e->{TYPE}}->{DECL}->($e, "pull", $e->{NAME}, $varname);
2527
2528         $self->fn_declare("pull", $e, "enum ndr_err_code ".TypeFunctionName("ndr_pull", $e)."(struct ndr_pull *$ndr, int ndr_flags, $args)") or return;
2529
2530         $self->pidl("{");
2531         $self->indent;
2532         $self->ParseTypePull($e, $ndr, $varname, 1, 1);
2533         $self->pidl("return NDR_ERR_SUCCESS;");
2534         $self->deindent;
2535         $self->pidl("}");
2536         $self->pidl("");
2537 }
2538
2539 sub ParseTypePrint($$$$)
2540 {
2541         my ($self, $e, $ndr, $varname) = @_;
2542
2543         $typefamily{$e->{TYPE}}->{PRINT_FN_BODY}->($self, $e, $ndr, $e->{NAME}, $varname);
2544 }
2545
2546 sub ParseTypePrintFunction($$$)
2547 {
2548         my ($self, $e, $varname) = @_;
2549         my $ndr = "ndr";
2550
2551         my $args = $typefamily{$e->{TYPE}}->{DECL}->($e, "print", $e->{NAME}, $varname);
2552
2553         $self->pidl_hdr("void ".TypeFunctionName("ndr_print", $e)."(struct ndr_print *ndr, const char *name, $args);");
2554
2555         return if (has_property($e, "noprint"));
2556
2557         $self->pidl("_PUBLIC_ void ".TypeFunctionName("ndr_print", $e)."(struct ndr_print *$ndr, const char *name, $args)");
2558         $self->pidl("{");
2559         $self->indent;
2560         $self->ParseTypePrint($e, $ndr, $varname);
2561         $self->deindent;
2562         $self->pidl("}");
2563         $self->pidl("");
2564 }
2565
2566 sub ParseTypeNdrSize($$)
2567 {
2568         my ($self,$t) = @_;
2569
2570         my $varname = "r";
2571         my $tf = $typefamily{$t->{TYPE}};
2572         my $args = $tf->{SIZE_FN_ARGS}->($t, $t->{NAME}, $varname);
2573
2574         $self->fn_declare("size", $t, "size_t ndr_size_$t->{NAME}($args)") or return;
2575
2576         $self->pidl("{");
2577         $self->indent;
2578         $typefamily{$t->{TYPE}}->{SIZE_FN_BODY}->($self,$t, $t->{NAME}, $varname);
2579         $self->deindent;
2580         $self->pidl("}");
2581         $self->pidl("");
2582 }
2583
2584 #####################################################################
2585 # parse the interface definitions
2586 sub ParseInterface($$$)
2587 {
2588         my($self,$interface,$needed) = @_;
2589
2590         $self->pidl_hdr("#ifndef _HEADER_NDR_$interface->{NAME}");
2591         $self->pidl_hdr("#define _HEADER_NDR_$interface->{NAME}");
2592
2593         $self->pidl_hdr("");
2594
2595         $self->HeaderInterface($interface, $needed);
2596
2597         # Typedefs
2598         foreach my $d (@{$interface->{TYPES}}) {
2599                 next unless(typeHasBody($d));
2600
2601                 ($needed->{TypeFunctionName("ndr_push", $d)}) && $self->ParseTypePushFunction($d, "r");
2602                 ($needed->{TypeFunctionName("ndr_pull", $d)}) && $self->ParseTypePullFunction($d, "r");
2603                 ($needed->{TypeFunctionName("ndr_print", $d)}) && $self->ParseTypePrintFunction($d, "r");
2604
2605                 # Make sure we don't generate a function twice...
2606                 $needed->{TypeFunctionName("ndr_push", $d)} = 
2607                     $needed->{TypeFunctionName("ndr_pull", $d)} = 
2608                         $needed->{TypeFunctionName("ndr_print", $d)} = 0;
2609
2610                 ($needed->{"ndr_size_$d->{NAME}"}) && $self->ParseTypeNdrSize($d);
2611         }
2612
2613         # Functions
2614         foreach my $d (@{$interface->{FUNCTIONS}}) {
2615                 ($needed->{"ndr_push_$d->{NAME}"}) && $self->ParseFunctionPush($d);
2616                 ($needed->{"ndr_pull_$d->{NAME}"}) && $self->ParseFunctionPull($d);
2617                 ($needed->{"ndr_print_$d->{NAME}"}) && $self->ParseFunctionPrint($d);
2618
2619                 # Make sure we don't generate a function twice...
2620                 $needed->{"ndr_push_$d->{NAME}"} = $needed->{"ndr_pull_$d->{NAME}"} = 
2621                         $needed->{"ndr_print_$d->{NAME}"} = 0;
2622         }
2623
2624         $self->FunctionTable($interface);
2625
2626         $self->pidl_hdr("#endif /* _HEADER_NDR_$interface->{NAME} */");
2627 }
2628
2629 sub GenerateIncludes($)
2630 {
2631         my ($self) = @_;
2632         if (is_intree()) {
2633                 $self->pidl("#include \"includes.h\"");
2634         } else {
2635                 $self->pidl("#ifndef _GNU_SOURCE");
2636                 $self->pidl("#define _GNU_SOURCE");
2637                 $self->pidl("#endif");
2638                 $self->pidl("#include <stdint.h>");
2639                 $self->pidl("#include <stdlib.h>");
2640                 $self->pidl("#include <stdio.h>");
2641                 $self->pidl("#include <stdbool.h>");
2642                 $self->pidl("#include <stdarg.h>");
2643                 $self->pidl("#include <string.h>");
2644         }
2645 }
2646
2647 #####################################################################
2648 # parse a parsed IDL structure back into an IDL file
2649 sub Parse($$$$)
2650 {
2651         my($self, $ndr,$gen_header,$ndr_header) = @_;
2652
2653         $self->pidl_hdr("/* header auto-generated by pidl */");
2654         $self->pidl_hdr("");
2655         $self->pidl_hdr(choose_header("librpc/ndr/libndr.h", "ndr.h"));
2656         $self->pidl_hdr("#include \"$gen_header\"") if ($gen_header);
2657         $self->pidl_hdr("");
2658
2659         $self->pidl("/* parser auto-generated by pidl */");
2660         $self->pidl("");
2661         $self->GenerateIncludes();
2662         $self->pidl("#include \"$ndr_header\"") if ($ndr_header);
2663         $self->pidl("");
2664
2665         my %needed = ();
2666
2667         foreach (@{$ndr}) {
2668                 ($_->{TYPE} eq "INTERFACE") && NeededInterface($_, \%needed);
2669         }
2670
2671         foreach (@{$ndr}) {
2672                 ($_->{TYPE} eq "INTERFACE") && $self->ParseInterface($_, \%needed);
2673                 ($_->{TYPE} eq "IMPORT") && $self->HeaderImport(@{$_->{PATHS}});
2674                 ($_->{TYPE} eq "INCLUDE") && $self->HeaderInclude(@{$_->{PATHS}});
2675         }
2676
2677         return ($self->{res_hdr}, $self->{res});
2678 }
2679
2680 sub NeededElement($$$)
2681 {
2682         my ($e, $dir, $needed) = @_;
2683
2684         return if ($e->{TYPE} eq "EMPTY");
2685
2686         return if (ref($e->{TYPE}) eq "HASH" and 
2687                        not defined($e->{TYPE}->{NAME}));
2688
2689         my ($t, $rt);
2690         if (ref($e->{TYPE}) eq "HASH") {
2691                 $t = $e->{TYPE}->{TYPE}."_".$e->{TYPE}->{NAME};
2692         } else {
2693                 $t = $e->{TYPE};
2694         }
2695
2696         if (ref($e->{REPRESENTATION_TYPE}) eq "HASH") {
2697                 $rt = $e->{REPRESENTATION_TYPE}->{TYPE}."_".$e->{REPRESENTATION_TYPE}->{NAME};
2698         } else {
2699                 $rt = $e->{REPRESENTATION_TYPE};
2700         }
2701
2702         die ("$e->{NAME} $t, $rt FOO") unless ($rt ne "");
2703
2704         my @fn = ();
2705         if ($dir eq "print") {
2706                 push(@fn, TypeFunctionName("ndr_print", $e->{REPRESENTATION_TYPE}));
2707         } elsif ($dir eq "pull") {
2708                 push (@fn, TypeFunctionName("ndr_pull", $e->{TYPE}));
2709                 push (@fn, "ndr_$t\_to_$rt")
2710                         if ($rt ne $t);
2711         } elsif ($dir eq "push") {
2712                 push (@fn, TypeFunctionName("ndr_push", $e->{TYPE}));
2713                 push (@fn, "ndr_$rt\_to_$t")
2714                         if ($rt ne $t);
2715         } else {
2716                 die("invalid direction `$dir'");
2717         }
2718
2719         foreach (@fn) {
2720                 unless (defined($needed->{$_})) {
2721                         $needed->{$_} = 1;
2722                 }
2723         }
2724 }
2725
2726 sub NeededFunction($$)
2727 {
2728         my ($fn,$needed) = @_;
2729         $needed->{"ndr_pull_$fn->{NAME}"} = 1;
2730         $needed->{"ndr_push_$fn->{NAME}"} = 1;
2731         $needed->{"ndr_print_$fn->{NAME}"} = 1;
2732         foreach my $e (@{$fn->{ELEMENTS}}) {
2733                 $e->{PARENT} = $fn;
2734                 NeededElement($e, $_, $needed) foreach ("pull", "push", "print");
2735         }
2736 }
2737
2738 sub NeededType($$$)
2739 {
2740         sub NeededType($$$);
2741         my ($t,$needed,$req) = @_;
2742
2743         NeededType($t->{DATA}, $needed, $req) if ($t->{TYPE} eq "TYPEDEF");
2744
2745         if ($t->{TYPE} eq "STRUCT" or $t->{TYPE} eq "UNION") {
2746                 return unless defined($t->{ELEMENTS});
2747                 for my $e (@{$t->{ELEMENTS}}) {
2748                         $e->{PARENT} = $t;
2749                         if (has_property($e, "compression")) { 
2750                                 $needed->{"compression"} = 1;
2751                         }
2752                         NeededElement($e, $req, $needed);
2753                         NeededType($e->{TYPE}, $needed, $req) if (ref($e->{TYPE}) eq "HASH");
2754                 }
2755         }
2756 }
2757
2758 #####################################################################
2759 # work out what parse functions are needed
2760 sub NeededInterface($$)
2761 {
2762         my ($interface,$needed) = @_;
2763         NeededFunction($_, $needed) foreach (@{$interface->{FUNCTIONS}});
2764         foreach (reverse @{$interface->{TYPES}}) {
2765                 if (has_property($_, "public")) {
2766                         $needed->{TypeFunctionName("ndr_pull", $_)} = $needed->{TypeFunctionName("ndr_push", $_)} = 
2767                                 $needed->{TypeFunctionName("ndr_print", $_)} = 1;
2768                 }
2769
2770                 NeededType($_, $needed, "pull") if ($needed->{TypeFunctionName("ndr_pull", $_)});
2771                 NeededType($_, $needed, "push") if ($needed->{TypeFunctionName("ndr_push", $_)});
2772                 NeededType($_, $needed, "print") if ($needed->{TypeFunctionName("ndr_print", $_)});
2773                 if (has_property($_, "gensize")) {
2774                         $needed->{"ndr_size_$_->{NAME}"} = 1;
2775                 }
2776         }
2777 }
2778
2779 sub TypeFunctionName($$)
2780 {
2781         my ($prefix, $t) = @_;
2782
2783         return "$prefix\_$t->{NAME}" if (ref($t) eq "HASH" and 
2784                         $t->{TYPE} eq "TYPEDEF");
2785         return "$prefix\_$t->{TYPE}_$t->{NAME}" if (ref($t) eq "HASH");
2786         return "$prefix\_$t";
2787 }
2788
2789 1;