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