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