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