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