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