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