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