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