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