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