r21456: Allow anonymous and non-anonymous nested structures/unions/enums/bitmaps...
[Samba/ekacnet.git] / source4 / pidl / lib / Parse / Pidl / Samba4 / NDR / Parser.pm
blob8a72b1233e2b1b94bd2346e09154e7d5e72b72e9
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
8 package Parse::Pidl::Samba4::NDR::Parser;
10 require Exporter;
11 @ISA = qw(Exporter);
12 @EXPORT = qw(is_charset_array);
13 @EXPORT_OK = qw(check_null_pointer GenerateFunctionInEnv
14 GenerateFunctionOutEnv EnvSubstituteValue GenerateStructEnv NeededFunction
15 NeededElement NeededType $res);
17 use strict;
18 use Parse::Pidl::Typelist qw(hasType getType mapTypeName);
19 use Parse::Pidl::Util qw(has_property ParseExpr ParseExprExt print_uuid);
20 use Parse::Pidl::NDR qw(GetPrevLevel GetNextLevel ContainsDeferred);
21 use Parse::Pidl::Samba4 qw(is_intree choose_header);
22 use Parse::Pidl qw(warning);
24 use vars qw($VERSION);
25 $VERSION = '0.01';
27 # list of known types
28 my %typefamily;
30 sub get_typefamily($)
32 my $n = shift;
33 return $typefamily{$n};
36 sub append_prefix($$)
38 my ($e, $var_name) = @_;
39 my $pointers = 0;
41 foreach my $l (@{$e->{LEVELS}}) {
42 if ($l->{TYPE} eq "POINTER") {
43 $pointers++;
44 } elsif ($l->{TYPE} eq "ARRAY") {
45 if (($pointers == 0) and
46 (not $l->{IS_FIXED}) and
47 (not $l->{IS_INLINE})) {
48 return get_value_of($var_name);
50 } elsif ($l->{TYPE} eq "DATA") {
51 if (Parse::Pidl::Typelist::scalar_is_reference($l->{DATA_TYPE})) {
52 return get_value_of($var_name) unless ($pointers);
57 return $var_name;
60 sub has_fast_array($$)
62 my ($e,$l) = @_;
64 return 0 if ($l->{TYPE} ne "ARRAY");
66 my $nl = GetNextLevel($e,$l);
67 return 0 unless ($nl->{TYPE} eq "DATA");
68 return 0 unless (hasType($nl->{DATA_TYPE}));
70 my $t = getType($nl->{DATA_TYPE});
72 # Only uint8 and string have fast array functions at the moment
73 return ($t->{NAME} eq "uint8") or ($t->{NAME} eq "string");
76 sub is_charset_array($$)
78 my ($e,$l) = @_;
80 return 0 if ($l->{TYPE} ne "ARRAY");
82 my $nl = GetNextLevel($e,$l);
84 return 0 unless ($nl->{TYPE} eq "DATA");
86 return has_property($e, "charset");
89 sub get_pointer_to($)
91 my $var_name = shift;
93 if ($var_name =~ /^\*(.*)$/) {
94 return $1;
95 } elsif ($var_name =~ /^\&(.*)$/) {
96 return "&($var_name)";
97 } else {
98 return "&$var_name";
102 sub get_value_of($)
104 my $var_name = shift;
106 if ($var_name =~ /^\&(.*)$/) {
107 return $1;
108 } else {
109 return "*$var_name";
113 our $res;
114 my $deferred = [];
115 my $tabs = "";
117 ####################################
118 # pidl() is our basic output routine
119 sub pidl($)
121 my $d = shift;
122 if ($d) {
123 $res .= $tabs;
124 $res .= $d;
126 $res .="\n";
129 my $res_hdr;
131 sub pidl_hdr ($) { my $d = shift; $res_hdr .= "$d\n"; }
133 ####################################
134 # defer() is like pidl(), but adds to
135 # a deferred buffer which is then added to the
136 # output buffer at the end of the structure/union/function
137 # This is needed to cope with code that must be pushed back
138 # to the end of a block of elements
139 my $defer_tabs = "";
140 sub defer_indent() { $defer_tabs.="\t"; }
141 sub defer_deindent() { $defer_tabs=substr($defer_tabs, 0, -1); }
143 sub defer($)
145 my $d = shift;
146 if ($d) {
147 push(@$deferred, $defer_tabs.$d);
151 ########################################
152 # add the deferred content to the current
153 # output
154 sub add_deferred()
156 pidl $_ foreach (@$deferred);
157 $deferred = [];
158 $defer_tabs = "";
161 sub indent()
163 $tabs .= "\t";
166 sub deindent()
168 $tabs = substr($tabs, 0, -1);
171 #####################################################################
172 # declare a function public or static, depending on its attributes
173 sub fn_declare($$$)
175 my ($type,$fn,$decl) = @_;
177 if (has_property($fn, "no$type")) {
178 pidl_hdr "$decl;";
179 return 0;
182 if (has_property($fn, "public")) {
183 pidl_hdr "$decl;";
184 pidl "_PUBLIC_ $decl";
185 } else {
186 pidl "static $decl";
189 return 1;
192 ###################################################################
193 # setup any special flags for an element or structure
194 sub start_flags($)
196 my $e = shift;
197 my $flags = has_property($e, "flag");
198 if (defined $flags) {
199 pidl "{";
200 indent;
201 pidl "uint32_t _flags_save_$e->{TYPE} = ndr->flags;";
202 pidl "ndr_set_flags(&ndr->flags, $flags);";
206 ###################################################################
207 # end any special flags for an element or structure
208 sub end_flags($)
210 my $e = shift;
211 my $flags = has_property($e, "flag");
212 if (defined $flags) {
213 pidl "ndr->flags = _flags_save_$e->{TYPE};";
214 deindent;
215 pidl "}";
219 sub GenerateStructEnv($$)
221 my ($x, $v) = @_;
222 my %env;
224 foreach my $e (@{$x->{ELEMENTS}}) {
225 $env{$e->{NAME}} = "$v->$e->{NAME}";
228 $env{"this"} = $v;
230 return \%env;
233 sub EnvSubstituteValue($$)
235 my ($env,$s) = @_;
237 # Substitute the value() values in the env
238 foreach my $e (@{$s->{ELEMENTS}}) {
239 next unless (defined(my $v = has_property($e, "value")));
241 $env->{$e->{NAME}} = ParseExpr($v, $env, $e);
244 return $env;
247 sub GenerateFunctionInEnv($)
249 my $fn = shift;
250 my %env;
252 foreach my $e (@{$fn->{ELEMENTS}}) {
253 if (grep (/in/, @{$e->{DIRECTION}})) {
254 $env{$e->{NAME}} = "r->in.$e->{NAME}";
258 return \%env;
261 sub GenerateFunctionOutEnv($)
263 my $fn = shift;
264 my %env;
266 foreach my $e (@{$fn->{ELEMENTS}}) {
267 if (grep (/out/, @{$e->{DIRECTION}})) {
268 $env{$e->{NAME}} = "r->out.$e->{NAME}";
269 } elsif (grep (/in/, @{$e->{DIRECTION}})) {
270 $env{$e->{NAME}} = "r->in.$e->{NAME}";
274 return \%env;
277 #####################################################################
278 # parse the data of an array - push side
279 sub ParseArrayPushHeader($$$$$)
281 my ($e,$l,$ndr,$var_name,$env) = @_;
283 my $size;
284 my $length;
286 if ($l->{IS_ZERO_TERMINATED}) {
287 if (has_property($e, "charset")) {
288 $size = $length = "ndr_charset_length($var_name, CH_$e->{PROPERTIES}->{charset})";
289 } else {
290 $size = $length = "ndr_string_length($var_name, sizeof(*$var_name))";
292 } else {
293 $size = ParseExpr($l->{SIZE_IS}, $env, $e);
294 $length = ParseExpr($l->{LENGTH_IS}, $env, $e);
297 if ((!$l->{IS_SURROUNDING}) and $l->{IS_CONFORMANT}) {
298 pidl "NDR_CHECK(ndr_push_uint32($ndr, NDR_SCALARS, $size));";
301 if ($l->{IS_VARYING}) {
302 pidl "NDR_CHECK(ndr_push_uint32($ndr, NDR_SCALARS, 0));"; # array offset
303 pidl "NDR_CHECK(ndr_push_uint32($ndr, NDR_SCALARS, $length));";
306 return $length;
309 sub check_fully_dereferenced($$)
311 my ($element, $env) = @_;
313 return sub ($) {
314 my $origvar = shift;
315 my $check = 0;
317 # Figure out the number of pointers in $ptr
318 my $expandedvar = $origvar;
319 $expandedvar =~ s/^(\**)//;
320 my $ptr = $1;
322 my $var = undef;
323 foreach (keys %$env) {
324 if ($env->{$_} eq $expandedvar) {
325 $var = $_;
326 last;
330 return($origvar) unless (defined($var));
331 my $e;
332 foreach (@{$element->{PARENT}->{ELEMENTS}}) {
333 if ($_->{NAME} eq $var) {
334 $e = $_;
335 last;
339 $e or die("Environment doesn't match siblings");
341 # See if pointer at pointer level $level
342 # needs to be checked.
343 my $nump = 0;
344 foreach (@{$e->{LEVELS}}) {
345 if ($_->{TYPE} eq "POINTER") {
346 $nump = $_->{POINTER_INDEX}+1;
349 warning($element->{ORIGINAL}, "Got pointer for `$e->{NAME}', expected fully derefenced variable") if ($nump > length($ptr));
350 return ($origvar);
354 sub check_null_pointer($$$$)
356 my ($element, $env, $print_fn, $return) = @_;
358 return sub ($) {
359 my $expandedvar = shift;
360 my $check = 0;
362 # Figure out the number of pointers in $ptr
363 $expandedvar =~ s/^(\**)//;
364 my $ptr = $1;
366 my $var = undef;
367 foreach (keys %$env) {
368 if ($env->{$_} eq $expandedvar) {
369 $var = $_;
370 last;
374 if (defined($var)) {
375 my $e;
376 # lookup ptr in $e
377 foreach (@{$element->{PARENT}->{ELEMENTS}}) {
378 if ($_->{NAME} eq $var) {
379 $e = $_;
380 last;
384 $e or die("Environment doesn't match siblings");
386 # See if pointer at pointer level $level
387 # needs to be checked.
388 foreach my $l (@{$e->{LEVELS}}) {
389 if ($l->{TYPE} eq "POINTER" and
390 $l->{POINTER_INDEX} == length($ptr)) {
391 # No need to check ref pointers
392 $check = ($l->{POINTER_TYPE} ne "ref");
393 last;
396 if ($l->{TYPE} eq "DATA") {
397 warning($element, "too much dereferences for `$var'");
400 } else {
401 warning($element, "unknown dereferenced expression `$expandedvar'");
402 $check = 1;
405 $print_fn->("if ($ptr$expandedvar == NULL) $return") if $check;
409 #####################################################################
410 # parse an array - pull side
411 sub ParseArrayPullHeader($$$$$)
413 my ($e,$l,$ndr,$var_name,$env) = @_;
415 my $length;
416 my $size;
418 if ($l->{IS_CONFORMANT}) {
419 $length = $size = "ndr_get_array_size($ndr, " . get_pointer_to($var_name) . ")";
420 } elsif ($l->{IS_ZERO_TERMINATED}) { # Noheader arrays
421 $length = $size = "ndr_get_string_size($ndr, sizeof(*$var_name))";
422 } else {
423 $length = $size = ParseExprExt($l->{SIZE_IS}, $env, $e->{ORIGINAL},
424 check_null_pointer($e, $env, \&pidl, "return NT_STATUS_INVALID_PARAMETER_MIX;"), check_fully_dereferenced($e, $env));
427 if ((!$l->{IS_SURROUNDING}) and $l->{IS_CONFORMANT}) {
428 pidl "NDR_CHECK(ndr_pull_array_size(ndr, " . get_pointer_to($var_name) . "));";
431 if ($l->{IS_VARYING}) {
432 pidl "NDR_CHECK(ndr_pull_array_length($ndr, " . get_pointer_to($var_name) . "));";
433 $length = "ndr_get_array_length($ndr, " . get_pointer_to($var_name) .")";
436 if ($length ne $size) {
437 pidl "if ($length > $size) {";
438 indent;
439 pidl "return ndr_pull_error($ndr, NDR_ERR_ARRAY_SIZE, \"Bad array size %u should exceed array length %u\", $size, $length);";
440 deindent;
441 pidl "}";
444 if ($l->{IS_CONFORMANT} and not $l->{IS_ZERO_TERMINATED}) {
445 defer "if ($var_name) {";
446 defer_indent;
447 my $size = ParseExprExt($l->{SIZE_IS}, $env, $e->{ORIGINAL}, check_null_pointer($e, $env, \&defer, "return NT_STATUS_INVALID_PARAMETER_MIX;"), check_fully_dereferenced($e, $env));
448 defer "NDR_CHECK(ndr_check_array_size(ndr, (void*)" . get_pointer_to($var_name) . ", $size));";
449 defer_deindent;
450 defer "}";
453 if ($l->{IS_VARYING} and not $l->{IS_ZERO_TERMINATED}) {
454 defer "if ($var_name) {";
455 defer_indent;
456 my $length = ParseExprExt($l->{LENGTH_IS}, $env, $e->{ORIGINAL},
457 check_null_pointer($e, $env, \&defer, "return NT_STATUS_INVALID_PARAMETER_MIX;"),
458 check_fully_dereferenced($e, $env));
459 defer "NDR_CHECK(ndr_check_array_length(ndr, (void*)" . get_pointer_to($var_name) . ", $length));";
460 defer_deindent;
461 defer "}"
464 if (not $l->{IS_FIXED} and not is_charset_array($e, $l)) {
465 AllocateArrayLevel($e,$l,$ndr,$env,$size);
468 return $length;
471 sub compression_alg($$)
473 my ($e, $l) = @_;
474 my ($alg, $clen, $dlen) = split(/ /, $l->{COMPRESSION});
476 return $alg;
479 sub compression_clen($$$)
481 my ($e, $l, $env) = @_;
482 my ($alg, $clen, $dlen) = split(/ /, $l->{COMPRESSION});
484 return ParseExpr($clen, $env, $e->{ORIGINAL});
487 sub compression_dlen($$$)
489 my ($e,$l,$env) = @_;
490 my ($alg, $clen, $dlen) = split(/ /, $l->{COMPRESSION});
492 return ParseExpr($dlen, $env, $e->{ORIGINAL});
495 sub ParseCompressionPushStart($$$$)
497 my ($e,$l,$ndr,$env) = @_;
498 my $comndr = "$ndr\_compressed";
499 my $alg = compression_alg($e, $l);
500 my $dlen = compression_dlen($e, $l, $env);
502 pidl "{";
503 indent;
504 pidl "struct ndr_push *$comndr;";
505 pidl "NDR_CHECK(ndr_push_compression_start($ndr, &$comndr, $alg, $dlen));";
507 return $comndr;
510 sub ParseCompressionPushEnd($$$$)
512 my ($e,$l,$ndr,$env) = @_;
513 my $comndr = "$ndr\_compressed";
514 my $alg = compression_alg($e, $l);
515 my $dlen = compression_dlen($e, $l, $env);
517 pidl "NDR_CHECK(ndr_push_compression_end($ndr, $comndr, $alg, $dlen));";
518 deindent;
519 pidl "}";
522 sub ParseCompressionPullStart($$$$)
524 my ($e,$l,$ndr,$env) = @_;
525 my $comndr = "$ndr\_compressed";
526 my $alg = compression_alg($e, $l);
527 my $dlen = compression_dlen($e, $l, $env);
529 pidl "{";
530 indent;
531 pidl "struct ndr_pull *$comndr;";
532 pidl "NDR_CHECK(ndr_pull_compression_start($ndr, &$comndr, $alg, $dlen));";
534 return $comndr;
537 sub ParseCompressionPullEnd($$$$)
539 my ($e,$l,$ndr,$env) = @_;
540 my $comndr = "$ndr\_compressed";
541 my $alg = compression_alg($e, $l);
542 my $dlen = compression_dlen($e, $l, $env);
544 pidl "NDR_CHECK(ndr_pull_compression_end($ndr, $comndr, $alg, $dlen));";
545 deindent;
546 pidl "}";
549 sub ParseSubcontextPushStart($$$$)
551 my ($e,$l,$ndr,$env) = @_;
552 my $subndr = "_ndr_$e->{NAME}";
553 my $subcontext_size = ParseExpr($l->{SUBCONTEXT_SIZE}, $env, $e->{ORIGINAL});
555 pidl "{";
556 indent;
557 pidl "struct ndr_push *$subndr;";
558 pidl "NDR_CHECK(ndr_push_subcontext_start($ndr, &$subndr, $l->{HEADER_SIZE}, $subcontext_size));";
560 if (defined $l->{COMPRESSION}) {
561 $subndr = ParseCompressionPushStart($e, $l, $subndr, $env);
564 return $subndr;
567 sub ParseSubcontextPushEnd($$$$)
569 my ($e,$l,$ndr,$env) = @_;
570 my $subndr = "_ndr_$e->{NAME}";
571 my $subcontext_size = ParseExpr($l->{SUBCONTEXT_SIZE}, $env, $e->{ORIGINAL});
573 if (defined $l->{COMPRESSION}) {
574 ParseCompressionPushEnd($e, $l, $subndr, $env);
577 pidl "NDR_CHECK(ndr_push_subcontext_end($ndr, $subndr, $l->{HEADER_SIZE}, $subcontext_size));";
578 deindent;
579 pidl "}";
582 sub ParseSubcontextPullStart($$$$)
584 my ($e,$l,$ndr,$env) = @_;
585 my $subndr = "_ndr_$e->{NAME}";
586 my $subcontext_size = ParseExpr($l->{SUBCONTEXT_SIZE}, $env, $e->{ORIGINAL});
588 pidl "{";
589 indent;
590 pidl "struct ndr_pull *$subndr;";
591 pidl "NDR_CHECK(ndr_pull_subcontext_start($ndr, &$subndr, $l->{HEADER_SIZE}, $subcontext_size));";
593 if (defined $l->{COMPRESSION}) {
594 $subndr = ParseCompressionPullStart($e, $l, $subndr, $env);
597 return $subndr;
600 sub ParseSubcontextPullEnd($$$$)
602 my ($e,$l,$ndr,$env) = @_;
603 my $subndr = "_ndr_$e->{NAME}";
604 my $subcontext_size = ParseExpr($l->{SUBCONTEXT_SIZE}, $env, $e->{ORIGINAL});
606 if (defined $l->{COMPRESSION}) {
607 ParseCompressionPullEnd($e, $l, $subndr, $env);
610 pidl "NDR_CHECK(ndr_pull_subcontext_end($ndr, $subndr, $l->{HEADER_SIZE}, $subcontext_size));";
611 deindent;
612 pidl "}";
615 sub ParseElementPushLevel
617 my ($e,$l,$ndr,$var_name,$env,$primitives,$deferred) = @_;
619 my $ndr_flags = CalcNdrFlags($l, $primitives, $deferred);
621 if ($l->{TYPE} eq "ARRAY" and ($l->{IS_CONFORMANT} or $l->{IS_VARYING})) {
622 $var_name = get_pointer_to($var_name);
625 if (defined($ndr_flags)) {
626 if ($l->{TYPE} eq "SUBCONTEXT") {
627 my $subndr = ParseSubcontextPushStart($e, $l, $ndr, $env);
628 ParseElementPushLevel($e, GetNextLevel($e, $l), $subndr, $var_name, $env, 1, 1);
629 ParseSubcontextPushEnd($e, $l, $ndr, $env);
630 } elsif ($l->{TYPE} eq "POINTER") {
631 ParsePtrPush($e, $l, $var_name);
632 } elsif ($l->{TYPE} eq "ARRAY") {
633 my $length = ParseArrayPushHeader($e, $l, $ndr, $var_name, $env);
635 my $nl = GetNextLevel($e, $l);
637 # Allow speedups for arrays of scalar types
638 if (is_charset_array($e,$l)) {
639 pidl "NDR_CHECK(ndr_push_charset($ndr, $ndr_flags, $var_name, $length, sizeof(" . mapTypeName($nl->{DATA_TYPE}) . "), CH_$e->{PROPERTIES}->{charset}));";
640 return;
641 } elsif (has_fast_array($e,$l)) {
642 pidl "NDR_CHECK(ndr_push_array_$nl->{DATA_TYPE}($ndr, $ndr_flags, $var_name, $length));";
643 return;
645 } elsif ($l->{TYPE} eq "SWITCH") {
646 ParseSwitchPush($e, $l, $ndr, $var_name, $ndr_flags, $env);
647 } elsif ($l->{TYPE} eq "DATA") {
648 ParseDataPush($e, $l, $ndr, $var_name, $ndr_flags);
652 if ($l->{TYPE} eq "POINTER" and $deferred) {
653 if ($l->{POINTER_TYPE} ne "ref") {
654 pidl "if ($var_name) {";
655 indent;
656 if ($l->{POINTER_TYPE} eq "relative") {
657 pidl "NDR_CHECK(ndr_push_relative_ptr2(ndr, $var_name));";
660 $var_name = get_value_of($var_name);
661 ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, 1, 1);
663 if ($l->{POINTER_TYPE} ne "ref") {
664 deindent;
665 pidl "}";
667 } elsif ($l->{TYPE} eq "ARRAY" and not has_fast_array($e,$l) and
668 not is_charset_array($e, $l)) {
669 my $length = ParseExpr($l->{LENGTH_IS}, $env, $e->{ORIGINAL});
670 my $counter = "cntr_$e->{NAME}_$l->{LEVEL_INDEX}";
672 $var_name = $var_name . "[$counter]";
674 if (($primitives and not $l->{IS_DEFERRED}) or ($deferred and $l->{IS_DEFERRED})) {
675 pidl "for ($counter = 0; $counter < $length; $counter++) {";
676 indent;
677 ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, 1, 0);
678 deindent;
679 pidl "}";
682 if ($deferred and ContainsDeferred($e, $l)) {
683 pidl "for ($counter = 0; $counter < $length; $counter++) {";
684 indent;
685 ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, 0, 1);
686 deindent;
687 pidl "}";
689 } elsif ($l->{TYPE} eq "SWITCH") {
690 ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, $primitives, $deferred);
694 #####################################################################
695 # parse scalars in a structure element
696 sub ParseElementPush($$$$$)
698 my ($e,$ndr,$env,$primitives,$deferred) = @_;
699 my $subndr = undef;
701 my $var_name = $env->{$e->{NAME}};
703 return unless $primitives or ($deferred and ContainsDeferred($e, $e->{LEVELS}[0]));
705 # Representation type is different from transmit_as
706 if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
707 pidl "{";
708 indent;
709 my $transmit_name = "_transmit_$e->{NAME}";
710 pidl mapTypeName($e->{TYPE}) ." $transmit_name;";
711 pidl "NDR_CHECK(ndr_$e->{REPRESENTATION_TYPE}_to_$e->{TYPE}($var_name, " . get_pointer_to($transmit_name) . "));";
712 $var_name = $transmit_name;
715 $var_name = append_prefix($e, $var_name);
717 start_flags($e);
719 if (defined(my $value = has_property($e, "value"))) {
720 $var_name = ParseExpr($value, $env, $e->{ORIGINAL});
723 ParseElementPushLevel($e, $e->{LEVELS}[0], $ndr, $var_name, $env, $primitives, $deferred);
725 end_flags($e);
727 if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
728 deindent;
729 pidl "}";
733 #####################################################################
734 # parse a pointer in a struct element or function
735 sub ParsePtrPush($$$)
737 my ($e,$l,$var_name) = @_;
739 if ($l->{POINTER_TYPE} eq "ref") {
740 pidl "if ($var_name == NULL) return NT_STATUS_INVALID_PARAMETER_MIX;";
741 if ($l->{LEVEL} eq "EMBEDDED") {
742 pidl "NDR_CHECK(ndr_push_ref_ptr(ndr));";
744 } elsif ($l->{POINTER_TYPE} eq "relative") {
745 pidl "NDR_CHECK(ndr_push_relative_ptr1(ndr, $var_name));";
746 } elsif ($l->{POINTER_TYPE} eq "unique") {
747 pidl "NDR_CHECK(ndr_push_unique_ptr(ndr, $var_name));";
748 } elsif ($l->{POINTER_TYPE} eq "full") {
749 pidl "NDR_CHECK(ndr_push_full_ptr(ndr, $var_name));";
750 } else {
751 die("Unhandled pointer type $l->{POINTER_TYPE}");
755 sub ParseDataPrint($$$)
757 my ($e, $l, $var_name) = @_;
759 if (not ref($l->{DATA_TYPE}) or
760 defined($l->{DATA_TYPE}->{NAME})) {
761 my $t;
762 if (ref($l->{DATA_TYPE})) {
763 $t = "$l->{DATA_TYPE}->{TYPE}_$l->{DATA_TYPE}->{NAME}";
764 } else {
765 $t = $l->{DATA_TYPE};
767 if (not Parse::Pidl::Typelist::is_scalar($t) or
768 Parse::Pidl::Typelist::scalar_is_reference($t)) {
769 $var_name = get_pointer_to($var_name);
771 pidl "ndr_print_$t(ndr, \"$e->{NAME}\", $var_name);";
772 } else {
773 ParseTypePrint($l->{DATA_TYPE}, $var_name);
777 #####################################################################
778 # print scalars in a structure element
779 sub ParseElementPrint($$$)
781 my($e, $var_name, $env) = @_;
783 return if (has_property($e, "noprint"));
785 if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
786 pidl "ndr_print_$e->{REPRESENTATION_TYPE}(ndr, \"$e->{NAME}\", $var_name);";
787 return;
790 $var_name = append_prefix($e, $var_name);
792 if (defined(my $value = has_property($e, "value"))) {
793 $var_name = "(ndr->flags & LIBNDR_PRINT_SET_VALUES)?" . ParseExpr($value,$env, $e->{ORIGINAL}) . ":$var_name";
796 foreach my $l (@{$e->{LEVELS}}) {
797 if ($l->{TYPE} eq "POINTER") {
798 pidl "ndr_print_ptr(ndr, \"$e->{NAME}\", $var_name);";
799 pidl "ndr->depth++;";
800 if ($l->{POINTER_TYPE} ne "ref") {
801 pidl "if ($var_name) {";
802 indent;
804 $var_name = get_value_of($var_name);
805 } elsif ($l->{TYPE} eq "ARRAY") {
806 my $length;
808 if ($l->{IS_CONFORMANT} or $l->{IS_VARYING}) {
809 $var_name = get_pointer_to($var_name);
812 if ($l->{IS_ZERO_TERMINATED}) {
813 $length = "ndr_string_length($var_name, sizeof(*$var_name))";
814 } else {
815 $length = ParseExprExt($l->{LENGTH_IS}, $env, $e->{ORIGINAL},
816 check_null_pointer($e, $env, \&pidl, "return;"), check_fully_dereferenced($e, $env));
819 if (is_charset_array($e,$l)) {
820 pidl "ndr_print_string(ndr, \"$e->{NAME}\", $var_name);";
821 last;
822 } elsif (has_fast_array($e, $l)) {
823 my $nl = GetNextLevel($e, $l);
824 pidl "ndr_print_array_$nl->{DATA_TYPE}(ndr, \"$e->{NAME}\", $var_name, $length);";
825 last;
826 } else {
827 my $counter = "cntr_$e->{NAME}_$l->{LEVEL_INDEX}";
829 pidl "ndr->print(ndr, \"\%s: ARRAY(\%d)\", \"$e->{NAME}\", $length);";
830 pidl 'ndr->depth++;';
831 pidl "for ($counter=0;$counter<$length;$counter++) {";
832 indent;
833 pidl "char *idx_$l->{LEVEL_INDEX}=NULL;";
834 pidl "asprintf(&idx_$l->{LEVEL_INDEX}, \"[\%d]\", $counter);";
835 pidl "if (idx_$l->{LEVEL_INDEX}) {";
836 indent;
838 $var_name = $var_name . "[$counter]";
840 } elsif ($l->{TYPE} eq "DATA") {
841 ParseDataPrint($e, $l, $var_name);
842 } elsif ($l->{TYPE} eq "SWITCH") {
843 my $switch_var = ParseExprExt($l->{SWITCH_IS}, $env, $e->{ORIGINAL},
844 check_null_pointer($e, $env, \&pidl, "return;"), check_fully_dereferenced($e, $env));
845 pidl "ndr_print_set_switch_value(ndr, " . get_pointer_to($var_name) . ", $switch_var);";
849 foreach my $l (reverse @{$e->{LEVELS}}) {
850 if ($l->{TYPE} eq "POINTER") {
851 if ($l->{POINTER_TYPE} ne "ref") {
852 deindent;
853 pidl "}";
855 pidl "ndr->depth--;";
856 } elsif (($l->{TYPE} eq "ARRAY")
857 and not is_charset_array($e,$l)
858 and not has_fast_array($e,$l)) {
859 pidl "free(idx_$l->{LEVEL_INDEX});";
860 deindent;
861 pidl "}";
862 deindent;
863 pidl "}";
864 pidl "ndr->depth--;";
869 #####################################################################
870 # parse scalars in a structure element - pull size
871 sub ParseSwitchPull($$$$$$)
873 my($e,$l,$ndr,$var_name,$ndr_flags,$env) = @_;
874 my $switch_var = ParseExprExt($l->{SWITCH_IS}, $env, $e->{ORIGINAL},
875 check_null_pointer($e, $env, \&pidl, "return NT_STATUS_INVALID_PARAMETER_MIX;"), check_fully_dereferenced($e, $env));
877 $var_name = get_pointer_to($var_name);
878 pidl "NDR_CHECK(ndr_pull_set_switch_value($ndr, $var_name, $switch_var));";
881 #####################################################################
882 # push switch element
883 sub ParseSwitchPush($$$$$$)
885 my($e,$l,$ndr,$var_name,$ndr_flags,$env) = @_;
886 my $switch_var = ParseExprExt($l->{SWITCH_IS}, $env, $e->{ORIGINAL},
887 check_null_pointer($e, $env, \&pidl, "return NT_STATUS_INVALID_PARAMETER_MIX;"), check_fully_dereferenced($e, $env));
889 $var_name = get_pointer_to($var_name);
890 pidl "NDR_CHECK(ndr_push_set_switch_value($ndr, $var_name, $switch_var));";
893 sub ParseDataPull($$$$$)
895 my ($e,$l,$ndr,$var_name,$ndr_flags) = @_;
897 if (not ref($l->{DATA_TYPE}) or
898 defined($l->{DATA_TYPE}->{NAME})) {
899 my $t;
900 if (ref($l->{DATA_TYPE}) eq "HASH") {
901 $t = "$l->{DATA_TYPE}->{TYPE}_$l->{DATA_TYPE}->{NAME}";
902 } else {
903 $t = $l->{DATA_TYPE};
906 if (Parse::Pidl::Typelist::scalar_is_reference($t)) {
907 $var_name = get_pointer_to($var_name);
910 $var_name = get_pointer_to($var_name);
912 pidl "NDR_CHECK(ndr_pull_$t($ndr, $ndr_flags, $var_name));";
914 if (my $range = has_property($e, "range")) {
915 $var_name = get_value_of($var_name);
916 my ($low, $high) = split(/ /, $range, 2);
917 pidl "if ($var_name < $low || $var_name > $high) {";
918 pidl "\treturn ndr_pull_error($ndr, NDR_ERR_RANGE, \"value out of range\");";
919 pidl "}";
921 } else {
922 ParseTypePull($l->{DATA_TYPE}, $var_name);
926 sub ParseDataPush($$$$$)
928 my ($e,$l,$ndr,$var_name,$ndr_flags) = @_;
930 if (not ref($l->{DATA_TYPE}) or defined($l->{DATA_TYPE}->{NAME})) {
931 my $t;
932 if (ref($l->{DATA_TYPE}) eq "HASH") {
933 $t = "$l->{DATA_TYPE}->{TYPE}_$l->{DATA_TYPE}->{NAME}";
934 } else {
935 $t = $l->{DATA_TYPE};
938 # strings are passed by value rather than reference
939 if (not Parse::Pidl::Typelist::is_scalar($t) or
940 Parse::Pidl::Typelist::scalar_is_reference($t)) {
941 $var_name = get_pointer_to($var_name);
944 pidl "NDR_CHECK(ndr_push_$t($ndr, $ndr_flags, $var_name));";
945 } else {
946 ParseTypePush($l->{DATA_TYPE}, $var_name);
950 sub CalcNdrFlags($$$)
952 my ($l,$primitives,$deferred) = @_;
954 my $scalars = 0;
955 my $buffers = 0;
957 # Add NDR_SCALARS if this one is deferred
958 # and deferreds may be pushed
959 $scalars = 1 if ($l->{IS_DEFERRED} and $deferred);
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);
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);
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;
975 sub ParseMemCtxPullStart($$$)
977 my ($e, $l, $ptr_name) = @_;
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";
983 return if ($l->{TYPE} eq "ARRAY" and $l->{IS_FIXED});
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";
997 pidl "$mem_r_ctx = NDR_PULL_GET_MEM_CTX(ndr);";
998 pidl "NDR_PULL_SET_MEM_CTX(ndr, $mem_c_ctx, $mem_c_flags);";
1001 sub ParseMemCtxPullEnd($$)
1003 my $e = shift;
1004 my $l = shift;
1006 my $mem_r_ctx = "_mem_save_$e->{NAME}_$l->{LEVEL_INDEX}";
1007 my $mem_r_flags = "0";
1009 return if ($l->{TYPE} eq "ARRAY" and $l->{IS_FIXED});
1011 if (($l->{TYPE} eq "POINTER") and ($l->{POINTER_TYPE} eq "ref")) {
1012 my $nl = GetNextLevel($e, $l);
1013 my $next_is_array = ($nl->{TYPE} eq "ARRAY");
1014 my $next_is_string = (($nl->{TYPE} eq "DATA") and
1015 ($nl->{DATA_TYPE} eq "string"));
1016 if ($next_is_array or $next_is_string) {
1017 return;
1018 } else {
1019 $mem_r_flags = "LIBNDR_FLAG_REF_ALLOC";
1023 pidl "NDR_PULL_SET_MEM_CTX(ndr, $mem_r_ctx, $mem_r_flags);";
1026 sub CheckStringTerminator($$$$)
1028 my ($ndr,$e,$l,$length) = @_;
1029 my $nl = GetNextLevel($e, $l);
1031 # Make sure last element is zero!
1032 pidl "NDR_CHECK(ndr_check_string_terminator($ndr, $length, sizeof($nl->{DATA_TYPE}_t)));";
1035 sub ParseElementPullLevel
1037 my($e,$l,$ndr,$var_name,$env,$primitives,$deferred) = @_;
1039 my $ndr_flags = CalcNdrFlags($l, $primitives, $deferred);
1041 if ($l->{TYPE} eq "ARRAY" and ($l->{IS_VARYING} or $l->{IS_CONFORMANT})) {
1042 $var_name = get_pointer_to($var_name);
1045 # Only pull something if there's actually something to be pulled
1046 if (defined($ndr_flags)) {
1047 if ($l->{TYPE} eq "SUBCONTEXT") {
1048 my $subndr = ParseSubcontextPullStart($e, $l, $ndr, $env);
1049 ParseElementPullLevel($e, GetNextLevel($e,$l), $subndr, $var_name, $env, 1, 1);
1050 ParseSubcontextPullEnd($e, $l, $ndr, $env);
1051 } elsif ($l->{TYPE} eq "ARRAY") {
1052 my $length = ParseArrayPullHeader($e, $l, $ndr, $var_name, $env);
1054 my $nl = GetNextLevel($e, $l);
1056 if (is_charset_array($e,$l)) {
1057 if ($l->{IS_ZERO_TERMINATED}) {
1058 CheckStringTerminator($ndr, $e, $l, $length);
1060 pidl "NDR_CHECK(ndr_pull_charset($ndr, $ndr_flags, ".get_pointer_to($var_name).", $length, sizeof(" . mapTypeName($nl->{DATA_TYPE}) . "), CH_$e->{PROPERTIES}->{charset}));";
1061 return;
1062 } elsif (has_fast_array($e, $l)) {
1063 if ($l->{IS_ZERO_TERMINATED}) {
1064 CheckStringTerminator($ndr,$e,$l,$length);
1066 pidl "NDR_CHECK(ndr_pull_array_$nl->{DATA_TYPE}($ndr, $ndr_flags, $var_name, $length));";
1067 return;
1069 } elsif ($l->{TYPE} eq "POINTER") {
1070 ParsePtrPull($e, $l, $ndr, $var_name);
1071 } elsif ($l->{TYPE} eq "SWITCH") {
1072 ParseSwitchPull($e, $l, $ndr, $var_name, $ndr_flags, $env);
1073 } elsif ($l->{TYPE} eq "DATA") {
1074 ParseDataPull($e, $l, $ndr, $var_name, $ndr_flags);
1078 # add additional constructions
1079 if ($l->{TYPE} eq "POINTER" and $deferred) {
1080 if ($l->{POINTER_TYPE} ne "ref") {
1081 pidl "if ($var_name) {";
1082 indent;
1084 if ($l->{POINTER_TYPE} eq "relative") {
1085 pidl "struct ndr_pull_save _relative_save;";
1086 pidl "ndr_pull_save(ndr, &_relative_save);";
1087 pidl "NDR_CHECK(ndr_pull_relative_ptr2(ndr, $var_name));";
1091 ParseMemCtxPullStart($e, $l, $var_name);
1093 $var_name = get_value_of($var_name);
1094 ParseElementPullLevel($e, GetNextLevel($e,$l), $ndr, $var_name, $env, 1, 1);
1096 ParseMemCtxPullEnd($e,$l);
1098 if ($l->{POINTER_TYPE} ne "ref") {
1099 if ($l->{POINTER_TYPE} eq "relative") {
1100 pidl "ndr_pull_restore(ndr, &_relative_save);";
1102 deindent;
1103 pidl "}";
1105 } elsif ($l->{TYPE} eq "ARRAY" and
1106 not has_fast_array($e,$l) and not is_charset_array($e, $l)) {
1107 my $length = ParseExpr($l->{LENGTH_IS}, $env, $e->{ORIGINAL});
1108 my $counter = "cntr_$e->{NAME}_$l->{LEVEL_INDEX}";
1109 my $array_name = $var_name;
1111 $var_name = $var_name . "[$counter]";
1113 ParseMemCtxPullStart($e, $l, $array_name);
1115 if (($primitives and not $l->{IS_DEFERRED}) or ($deferred and $l->{IS_DEFERRED})) {
1116 my $nl = GetNextLevel($e,$l);
1118 if ($l->{IS_ZERO_TERMINATED}) {
1119 CheckStringTerminator($ndr,$e,$l,$length);
1122 pidl "for ($counter = 0; $counter < $length; $counter++) {";
1123 indent;
1124 ParseElementPullLevel($e, $nl, $ndr, $var_name, $env, 1, 0);
1125 deindent;
1126 pidl "}";
1129 if ($deferred and ContainsDeferred($e, $l)) {
1130 pidl "for ($counter = 0; $counter < $length; $counter++) {";
1131 indent;
1132 ParseElementPullLevel($e,GetNextLevel($e,$l), $ndr, $var_name, $env, 0, 1);
1133 deindent;
1134 pidl "}";
1137 ParseMemCtxPullEnd($e, $l);
1139 } elsif ($l->{TYPE} eq "SWITCH") {
1140 ParseElementPullLevel($e, GetNextLevel($e,$l), $ndr, $var_name, $env, $primitives, $deferred);
1144 #####################################################################
1145 # parse scalars in a structure element - pull size
1146 sub ParseElementPull($$$$$)
1148 my($e,$ndr,$env,$primitives,$deferred) = @_;
1150 my $var_name = $env->{$e->{NAME}};
1151 my $represent_name;
1152 my $transmit_name;
1154 return unless $primitives or ($deferred and ContainsDeferred($e, $e->{LEVELS}[0]));
1156 if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
1157 pidl "{";
1158 indent;
1159 $represent_name = $var_name;
1160 $transmit_name = "_transmit_$e->{NAME}";
1161 $var_name = $transmit_name;
1162 pidl mapTypeName($e->{TYPE})." $var_name;";
1165 $var_name = append_prefix($e, $var_name);
1167 start_flags($e);
1169 ParseElementPullLevel($e,$e->{LEVELS}[0],$ndr,$var_name,$env,$primitives,$deferred);
1171 end_flags($e);
1173 # Representation type is different from transmit_as
1174 if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
1175 pidl "NDR_CHECK(ndr_$e->{TYPE}_to_$e->{REPRESENTATION_TYPE}($transmit_name, ".get_pointer_to($represent_name)."));";
1176 deindent;
1177 pidl "}";
1181 #####################################################################
1182 # parse a pointer in a struct element or function
1183 sub ParsePtrPull($$$$)
1185 my($e,$l,$ndr,$var_name) = @_;
1187 my $nl = GetNextLevel($e, $l);
1188 my $next_is_array = ($nl->{TYPE} eq "ARRAY");
1189 my $next_is_string = (($nl->{TYPE} eq "DATA") and
1190 ($nl->{DATA_TYPE} eq "string"));
1192 if ($l->{POINTER_TYPE} eq "ref") {
1193 if ($l->{LEVEL} eq "EMBEDDED") {
1194 pidl "NDR_CHECK(ndr_pull_ref_ptr($ndr, &_ptr_$e->{NAME}));";
1197 if (!$next_is_array and !$next_is_string) {
1198 pidl "if (ndr->flags & LIBNDR_FLAG_REF_ALLOC) {";
1199 pidl "\tNDR_PULL_ALLOC($ndr, $var_name);";
1200 pidl "}";
1203 return;
1204 } elsif (($l->{POINTER_TYPE} eq "unique") or
1205 ($l->{POINTER_TYPE} eq "relative") or
1206 ($l->{POINTER_TYPE} eq "full")) {
1207 pidl "NDR_CHECK(ndr_pull_generic_ptr($ndr, &_ptr_$e->{NAME}));";
1208 pidl "if (_ptr_$e->{NAME}) {";
1209 indent;
1210 } else {
1211 die("Unhandled pointer type $l->{POINTER_TYPE}");
1214 # Don't do this for arrays, they're allocated at the actual level
1215 # of the array
1216 unless ($next_is_array or $next_is_string) {
1217 pidl "NDR_PULL_ALLOC($ndr, $var_name);";
1218 } else {
1219 # FIXME: Yes, this is nasty.
1220 # We allocate an array twice
1221 # - once just to indicate that it's there,
1222 # - then the real allocation...
1223 pidl "NDR_PULL_ALLOC($ndr, $var_name);";
1226 #pidl "memset($var_name, 0, sizeof($var_name));";
1227 if ($l->{POINTER_TYPE} eq "relative") {
1228 pidl "NDR_CHECK(ndr_pull_relative_ptr1($ndr, $var_name, _ptr_$e->{NAME}));";
1230 deindent;
1231 pidl "} else {";
1232 pidl "\t$var_name = NULL;";
1233 pidl "}";
1236 sub ParseStructPushPrimitives($$$$)
1238 my ($struct, $name, $varname, $env) = @_;
1240 # see if the structure contains a conformant array. If it
1241 # does, then it must be the last element of the structure, and
1242 # we need to push the conformant length early, as it fits on
1243 # the wire before the structure (and even before the structure
1244 # alignment)
1245 if (defined($struct->{SURROUNDING_ELEMENT})) {
1246 my $e = $struct->{SURROUNDING_ELEMENT};
1248 if (defined($e->{LEVELS}[0]) and
1249 $e->{LEVELS}[0]->{TYPE} eq "ARRAY") {
1250 my $size;
1252 if ($e->{LEVELS}[0]->{IS_ZERO_TERMINATED}) {
1253 if (has_property($e, "charset")) {
1254 $size = "ndr_charset_length($varname->$e->{NAME}, CH_$e->{PROPERTIES}->{charset})";
1255 } else {
1256 $size = "ndr_string_length($varname->$e->{NAME}, sizeof(*$varname->$e->{NAME}))";
1258 } else {
1259 $size = ParseExpr($e->{LEVELS}[0]->{SIZE_IS}, $env, $e->{ORIGINAL});
1262 pidl "NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, $size));";
1263 } else {
1264 pidl "NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, ndr_string_array_size(ndr, $varname->$e->{NAME})));";
1268 pidl "NDR_CHECK(ndr_push_align(ndr, $struct->{ALIGN}));";
1270 if (defined($struct->{PROPERTIES}{relative_base})) {
1271 # set the current offset as base for relative pointers
1272 # and store it based on the toplevel struct/union
1273 pidl "NDR_CHECK(ndr_push_setup_relative_base_offset1(ndr, $varname, ndr->offset));";
1276 ParseElementPush($_, "ndr", $env, 1, 0) foreach (@{$struct->{ELEMENTS}});
1279 sub ParseStructPushDeferred($$$$)
1281 my ($struct, $name, $varname, $env) = @_;
1282 if (defined($struct->{PROPERTIES}{relative_base})) {
1283 # retrieve the current offset as base for relative pointers
1284 # based on the toplevel struct/union
1285 pidl "NDR_CHECK(ndr_push_setup_relative_base_offset2(ndr, $varname));";
1287 ParseElementPush($_, "ndr", $env, 0, 1) foreach (@{$struct->{ELEMENTS}});
1290 #####################################################################
1291 # parse a struct
1292 sub ParseStructPush($$$)
1294 my ($struct, $name, $varname) = @_;
1296 return unless defined($struct->{ELEMENTS});
1298 my $env = GenerateStructEnv($struct, $varname);
1300 EnvSubstituteValue($env, $struct);
1302 DeclareArrayVariables($_) foreach (@{$struct->{ELEMENTS}});
1304 start_flags($struct);
1306 pidl "if (ndr_flags & NDR_SCALARS) {";
1307 indent;
1308 ParseStructPushPrimitives($struct, $name, $varname, $env);
1309 deindent;
1310 pidl "}";
1312 pidl "if (ndr_flags & NDR_BUFFERS) {";
1313 indent;
1314 ParseStructPushDeferred($struct, $name, $varname, $env);
1315 deindent;
1316 pidl "}";
1318 end_flags($struct);
1321 #####################################################################
1322 # generate a push function for an enum
1323 sub ParseEnumPush($$$)
1325 my($enum,$name,$varname) = @_;
1326 my($type_fn) = $enum->{BASE_TYPE};
1328 start_flags($enum);
1329 pidl "NDR_CHECK(ndr_push_$type_fn(ndr, NDR_SCALARS, $varname));";
1330 end_flags($enum);
1333 #####################################################################
1334 # generate a pull function for an enum
1335 sub ParseEnumPull($$$)
1337 my($enum,$name,$varname) = @_;
1338 my($type_fn) = $enum->{BASE_TYPE};
1339 my($type_v_decl) = mapTypeName($type_fn);
1341 pidl "$type_v_decl v;";
1342 start_flags($enum);
1343 pidl "NDR_CHECK(ndr_pull_$type_fn(ndr, NDR_SCALARS, &v));";
1344 pidl "*$varname = v;";
1346 end_flags($enum);
1349 #####################################################################
1350 # generate a print function for an enum
1351 sub ParseEnumPrint($$$)
1353 my($enum,$name,$varname) = @_;
1355 pidl "const char *val = NULL;";
1356 pidl "";
1358 start_flags($enum);
1360 pidl "switch ($varname) {";
1361 indent;
1362 my $els = \@{$enum->{ELEMENTS}};
1363 foreach my $i (0 .. $#{$els}) {
1364 my $e = ${$els}[$i];
1365 chomp $e;
1366 if ($e =~ /^(.*)=/) {
1367 $e = $1;
1369 pidl "case $e: val = \"$e\"; break;";
1372 deindent;
1373 pidl "}";
1375 pidl "ndr_print_enum(ndr, name, \"$enum->{TYPE}\", val, $varname);";
1377 end_flags($enum);
1380 sub DeclEnum($$$$)
1382 my ($e,$t,$name,$varname) = @_;
1383 return "enum $name " .
1384 ($t eq "pull"?"*":"") . $varname;
1387 $typefamily{ENUM} = {
1388 DECL => \&DeclEnum,
1389 PUSH_FN_BODY => \&ParseEnumPush,
1390 PULL_FN_BODY => \&ParseEnumPull,
1391 PRINT_FN_BODY => \&ParseEnumPrint,
1394 #####################################################################
1395 # generate a push function for a bitmap
1396 sub ParseBitmapPush($$$)
1398 my($bitmap,$name,$varname) = @_;
1399 my($type_fn) = $bitmap->{BASE_TYPE};
1401 start_flags($bitmap);
1403 pidl "NDR_CHECK(ndr_push_$type_fn(ndr, NDR_SCALARS, $varname));";
1405 end_flags($bitmap);
1408 #####################################################################
1409 # generate a pull function for an bitmap
1410 sub ParseBitmapPull($$$)
1412 my($bitmap,$name,$varname) = @_;
1413 my $type_fn = $bitmap->{BASE_TYPE};
1414 my($type_decl) = mapTypeName($bitmap->{BASE_TYPE});
1416 pidl "$type_decl v;";
1417 start_flags($bitmap);
1418 pidl "NDR_CHECK(ndr_pull_$type_fn(ndr, NDR_SCALARS, &v));";
1419 pidl "*$varname = v;";
1421 end_flags($bitmap);
1424 #####################################################################
1425 # generate a print function for an bitmap
1426 sub ParseBitmapPrintElement($$$$)
1428 my($e,$bitmap,$name,$varname) = @_;
1429 my($type_decl) = mapTypeName($bitmap->{BASE_TYPE});
1430 my($type_fn) = $bitmap->{BASE_TYPE};
1431 my($flag);
1433 if ($e =~ /^(\w+) .*$/) {
1434 $flag = "$1";
1435 } else {
1436 die "Bitmap: \"$name\" invalid Flag: \"$e\"";
1439 pidl "ndr_print_bitmap_flag(ndr, sizeof($type_decl), \"$flag\", $flag, $varname);";
1442 #####################################################################
1443 # generate a print function for an bitmap
1444 sub ParseBitmapPrint($$$)
1446 my($bitmap,$name,$varname) = @_;
1447 my($type_decl) = mapTypeName($bitmap->{TYPE});
1448 my($type_fn) = $bitmap->{BASE_TYPE};
1450 start_flags($bitmap);
1452 pidl "ndr_print_$type_fn(ndr, name, $varname);";
1454 pidl "ndr->depth++;";
1455 foreach my $e (@{$bitmap->{ELEMENTS}}) {
1456 ParseBitmapPrintElement($e, $bitmap, $name, $varname);
1458 pidl "ndr->depth--;";
1460 end_flags($bitmap);
1463 sub DeclBitmap($$$$)
1465 my ($e,$t,$name,$varname) = @_;
1466 return mapTypeName(Parse::Pidl::Typelist::bitmap_type_fn($e)) .
1467 ($t eq "pull"?" *":" ") . $varname;
1470 $typefamily{BITMAP} = {
1471 DECL => \&DeclBitmap,
1472 PUSH_FN_BODY => \&ParseBitmapPush,
1473 PULL_FN_BODY => \&ParseBitmapPull,
1474 PRINT_FN_BODY => \&ParseBitmapPrint,
1477 #####################################################################
1478 # generate a struct print function
1479 sub ParseStructPrint($$$)
1481 my($struct,$name,$varname) = @_;
1483 return unless defined $struct->{ELEMENTS};
1485 my $env = GenerateStructEnv($struct, $varname);
1487 EnvSubstituteValue($env, $struct);
1489 DeclareArrayVariables($_) foreach (@{$struct->{ELEMENTS}});
1491 pidl "ndr_print_struct(ndr, name, \"$name\");";
1493 start_flags($struct);
1495 pidl "ndr->depth++;";
1497 ParseElementPrint($_, $env->{$_->{NAME}}, $env)
1498 foreach (@{$struct->{ELEMENTS}});
1499 pidl "ndr->depth--;";
1501 end_flags($struct);
1504 sub DeclarePtrVariables($)
1506 my $e = shift;
1507 foreach my $l (@{$e->{LEVELS}}) {
1508 if ($l->{TYPE} eq "POINTER" and
1509 not ($l->{POINTER_TYPE} eq "ref" and $l->{LEVEL} eq "TOP")) {
1510 pidl "uint32_t _ptr_$e->{NAME};";
1511 last;
1516 sub DeclareArrayVariables($)
1518 my $e = shift;
1520 foreach my $l (@{$e->{LEVELS}}) {
1521 next if has_fast_array($e,$l);
1522 next if is_charset_array($e,$l);
1523 if ($l->{TYPE} eq "ARRAY") {
1524 pidl "uint32_t cntr_$e->{NAME}_$l->{LEVEL_INDEX};";
1529 sub need_decl_mem_ctx($$)
1531 my ($e,$l) = @_;
1533 return 0 if has_fast_array($e,$l);
1534 return 0 if is_charset_array($e,$l);
1535 return 1 if (($l->{TYPE} eq "ARRAY") and not $l->{IS_FIXED});
1537 if (($l->{TYPE} eq "POINTER") and ($l->{POINTER_TYPE} eq "ref")) {
1538 my $nl = GetNextLevel($e, $l);
1539 my $next_is_array = ($nl->{TYPE} eq "ARRAY");
1540 my $next_is_string = (($nl->{TYPE} eq "DATA") and
1541 ($nl->{DATA_TYPE} eq "string"));
1542 return 0 if ($next_is_array or $next_is_string);
1544 return 1 if ($l->{TYPE} eq "POINTER");
1546 return 0;
1549 sub DeclareMemCtxVariables($)
1551 my $e = shift;
1552 foreach my $l (@{$e->{LEVELS}}) {
1553 if (need_decl_mem_ctx($e, $l)) {
1554 pidl "TALLOC_CTX *_mem_save_$e->{NAME}_$l->{LEVEL_INDEX};";
1559 sub ParseStructPullPrimitives($$$$)
1561 my($struct,$name,$varname,$env) = @_;
1563 if (defined $struct->{SURROUNDING_ELEMENT}) {
1564 pidl "NDR_CHECK(ndr_pull_array_size(ndr, &$varname->$struct->{SURROUNDING_ELEMENT}->{NAME}));";
1567 pidl "NDR_CHECK(ndr_pull_align(ndr, $struct->{ALIGN}));";
1569 if (defined($struct->{PROPERTIES}{relative_base})) {
1570 # set the current offset as base for relative pointers
1571 # and store it based on the toplevel struct/union
1572 pidl "NDR_CHECK(ndr_pull_setup_relative_base_offset1(ndr, $varname, ndr->offset));";
1575 ParseElementPull($_, "ndr", $env, 1, 0) foreach (@{$struct->{ELEMENTS}});
1577 add_deferred();
1580 sub ParseStructPullDeferred($$$$)
1582 my ($struct,$name,$varname,$env) = @_;
1584 if (defined($struct->{PROPERTIES}{relative_base})) {
1585 # retrieve the current offset as base for relative pointers
1586 # based on the toplevel struct/union
1587 pidl "NDR_CHECK(ndr_pull_setup_relative_base_offset2(ndr, $varname));";
1589 foreach my $e (@{$struct->{ELEMENTS}}) {
1590 ParseElementPull($e, "ndr", $env, 0, 1);
1593 add_deferred();
1596 #####################################################################
1597 # parse a struct - pull side
1598 sub ParseStructPull($$$)
1600 my($struct,$name,$varname) = @_;
1602 return unless defined $struct->{ELEMENTS};
1604 # declare any internal pointers we need
1605 foreach my $e (@{$struct->{ELEMENTS}}) {
1606 DeclarePtrVariables($e);
1607 DeclareArrayVariables($e);
1608 DeclareMemCtxVariables($e);
1611 start_flags($struct);
1613 my $env = GenerateStructEnv($struct, $varname);
1615 pidl "if (ndr_flags & NDR_SCALARS) {";
1616 indent;
1617 ParseStructPullPrimitives($struct,$name,$varname,$env);
1618 deindent;
1619 pidl "}";
1620 pidl "if (ndr_flags & NDR_BUFFERS) {";
1621 indent;
1622 ParseStructPullDeferred($struct,$name,$varname,$env);
1623 deindent;
1624 pidl "}";
1626 end_flags($struct);
1629 #####################################################################
1630 # calculate size of ndr struct
1631 sub ParseStructNdrSize($$$)
1633 my ($t, $name, $varname) = @_;
1634 my $sizevar;
1636 if (my $flags = has_property($t, "flag")) {
1637 pidl "flags |= $flags;";
1639 pidl "return ndr_size_struct($varname, flags, (ndr_push_flags_fn_t)ndr_push_$name);";
1642 sub DeclStruct($$$$)
1644 my ($e,$t,$name,$varname) = @_;
1645 return ($t ne "pull"?"const ":"") . "struct $name *$varname";
1648 sub ArgsStructNdrSize($$$)
1650 my ($d, $name, $varname) = @_;
1651 return "const struct $name *$varname, int flags";
1654 $typefamily{STRUCT} = {
1655 PUSH_FN_BODY => \&ParseStructPush,
1656 DECL => \&DeclStruct,
1657 PULL_FN_BODY => \&ParseStructPull,
1658 PRINT_FN_BODY => \&ParseStructPrint,
1659 SIZE_FN_BODY => \&ParseStructNdrSize,
1660 SIZE_FN_ARGS => \&ArgsStructNdrSize,
1663 #####################################################################
1664 # calculate size of ndr struct
1665 sub ParseUnionNdrSize($$$)
1667 my ($t, $name, $varname) = @_;
1668 my $sizevar;
1670 if (my $flags = has_property($t, "flag")) {
1671 pidl "flags |= $flags;";
1674 pidl "return ndr_size_union($varname, flags, level, (ndr_push_flags_fn_t)ndr_push_$name);";
1677 #####################################################################
1678 # parse a union - push side
1679 sub ParseUnionPush($$$)
1681 my ($e,$name,$varname) = @_;
1682 my $have_default = 0;
1684 pidl "int level;";
1686 start_flags($e);
1688 pidl "level = ndr_push_get_switch_value(ndr, $varname);";
1690 pidl "if (ndr_flags & NDR_SCALARS) {";
1691 indent;
1693 if (defined($e->{SWITCH_TYPE})) {
1694 pidl "NDR_CHECK(ndr_push_$e->{SWITCH_TYPE}(ndr, NDR_SCALARS, level));";
1697 pidl "switch (level) {";
1698 indent;
1699 foreach my $el (@{$e->{ELEMENTS}}) {
1700 if ($el->{CASE} eq "default") {
1701 $have_default = 1;
1703 pidl "$el->{CASE}:";
1705 if ($el->{TYPE} ne "EMPTY") {
1706 indent;
1707 if (defined($e->{PROPERTIES}{relative_base})) {
1708 pidl "NDR_CHECK(ndr_push_align(ndr, $el->{ALIGN}));";
1709 # set the current offset as base for relative pointers
1710 # and store it based on the toplevel struct/union
1711 pidl "NDR_CHECK(ndr_push_setup_relative_base_offset1(ndr, $varname, ndr->offset));";
1713 DeclareArrayVariables($el);
1714 ParseElementPush($el, "ndr", {$el->{NAME} => "$varname->$el->{NAME}"}, 1, 0);
1715 deindent;
1717 pidl "break;";
1718 pidl "";
1720 if (! $have_default) {
1721 pidl "default:";
1722 pidl "\treturn ndr_push_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);";
1724 deindent;
1725 pidl "}";
1726 deindent;
1727 pidl "}";
1728 pidl "if (ndr_flags & NDR_BUFFERS) {";
1729 indent;
1730 if (defined($e->{PROPERTIES}{relative_base})) {
1731 # retrieve the current offset as base for relative pointers
1732 # based on the toplevel struct/union
1733 pidl "NDR_CHECK(ndr_push_setup_relative_base_offset2(ndr, $varname));";
1735 pidl "switch (level) {";
1736 indent;
1737 foreach my $el (@{$e->{ELEMENTS}}) {
1738 pidl "$el->{CASE}:";
1739 if ($el->{TYPE} ne "EMPTY") {
1740 indent;
1741 ParseElementPush($el, "ndr", {$el->{NAME} => "$varname->$el->{NAME}"}, 0, 1);
1742 deindent;
1744 pidl "break;";
1745 pidl "";
1747 if (! $have_default) {
1748 pidl "default:";
1749 pidl "\treturn ndr_push_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);";
1751 deindent;
1752 pidl "}";
1754 deindent;
1755 pidl "}";
1756 end_flags($e);
1759 #####################################################################
1760 # print a union
1761 sub ParseUnionPrint($$$)
1763 my ($e,$name,$varname) = @_;
1764 my $have_default = 0;
1766 pidl "int level;";
1767 foreach my $el (@{$e->{ELEMENTS}}) {
1768 DeclareArrayVariables($el);
1771 start_flags($e);
1773 pidl "level = ndr_print_get_switch_value(ndr, $varname);";
1775 pidl "ndr_print_union(ndr, name, level, \"$name\");";
1777 pidl "switch (level) {";
1778 indent;
1779 foreach my $el (@{$e->{ELEMENTS}}) {
1780 if ($el->{CASE} eq "default") {
1781 $have_default = 1;
1783 pidl "$el->{CASE}:";
1784 if ($el->{TYPE} ne "EMPTY") {
1785 indent;
1786 ParseElementPrint($el, "$varname->$el->{NAME}", {});
1787 deindent;
1789 pidl "break;";
1790 pidl "";
1792 if (! $have_default) {
1793 pidl "default:";
1794 pidl "\tndr_print_bad_level(ndr, name, level);";
1796 deindent;
1797 pidl "}";
1799 end_flags($e);
1802 sub ParseUnionPullPrimitives($$$$)
1804 my ($e,$name,$varname,$switch_type) = @_;
1805 my $have_default = 0;
1807 if (defined($switch_type)) {
1808 pidl "NDR_CHECK(ndr_pull_$switch_type(ndr, NDR_SCALARS, &_level));";
1809 pidl "if (_level != level) {";
1810 pidl "\treturn ndr_pull_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value %u for $name\", _level);";
1811 pidl "}";
1814 pidl "switch (level) {";
1815 indent;
1816 foreach my $el (@{$e->{ELEMENTS}}) {
1817 if ($el->{CASE} eq "default") {
1818 $have_default = 1;
1820 pidl "$el->{CASE}: {";
1822 if ($el->{TYPE} ne "EMPTY") {
1823 indent;
1824 DeclarePtrVariables($el);
1825 DeclareArrayVariables($el);
1826 if (defined($e->{PROPERTIES}{relative_base})) {
1827 pidl "NDR_CHECK(ndr_pull_align(ndr, $el->{ALIGN}));";
1828 # set the current offset as base for relative pointers
1829 # and store it based on the toplevel struct/union
1830 pidl "NDR_CHECK(ndr_pull_setup_relative_base_offset1(ndr, $varname, ndr->offset));";
1832 ParseElementPull($el, "ndr", {$el->{NAME} => "$varname->$el->{NAME}"}, 1, 0);
1833 deindent;
1835 pidl "break; }";
1836 pidl "";
1838 if (! $have_default) {
1839 pidl "default:";
1840 pidl "\treturn ndr_pull_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);";
1842 deindent;
1843 pidl "}";
1846 sub ParseUnionPullDeferred($$$)
1848 my ($e,$name,$varname) = @_;
1849 my $have_default = 0;
1851 if (defined($e->{PROPERTIES}{relative_base})) {
1852 # retrieve the current offset as base for relative pointers
1853 # based on the toplevel struct/union
1854 pidl "NDR_CHECK(ndr_pull_setup_relative_base_offset2(ndr, $varname));";
1856 pidl "switch (level) {";
1857 indent;
1858 foreach my $el (@{$e->{ELEMENTS}}) {
1859 if ($el->{CASE} eq "default") {
1860 $have_default = 1;
1863 pidl "$el->{CASE}:";
1864 if ($el->{TYPE} ne "EMPTY") {
1865 indent;
1866 ParseElementPull($el, "ndr", {$el->{NAME} => "$varname->$el->{NAME}"}, 0, 1);
1867 deindent;
1869 pidl "break;";
1870 pidl "";
1872 if (! $have_default) {
1873 pidl "default:";
1874 pidl "\treturn ndr_pull_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);";
1876 deindent;
1877 pidl "}";
1882 #####################################################################
1883 # parse a union - pull side
1884 sub ParseUnionPull($$$)
1886 my ($e,$name,$varname) = @_;
1887 my $switch_type = $e->{SWITCH_TYPE};
1889 pidl "int level;";
1890 if (defined($switch_type)) {
1891 if (Parse::Pidl::Typelist::typeIs($switch_type, "ENUM")) {
1892 $switch_type = Parse::Pidl::Typelist::enum_type_fn(getType($switch_type)->{DATA});
1894 pidl mapTypeName($switch_type) . " _level;";
1897 my %double_cases = ();
1898 foreach my $el (@{$e->{ELEMENTS}}) {
1899 next if ($el->{TYPE} eq "EMPTY");
1900 next if ($double_cases{"$el->{NAME}"});
1901 DeclareMemCtxVariables($el);
1902 $double_cases{"$el->{NAME}"} = 1;
1905 start_flags($e);
1907 pidl "level = ndr_pull_get_switch_value(ndr, $varname);";
1909 pidl "if (ndr_flags & NDR_SCALARS) {";
1910 indent;
1911 ParseUnionPullPrimitives($e,$name,$varname,$switch_type);
1912 deindent;
1913 pidl "}";
1915 pidl "if (ndr_flags & NDR_BUFFERS) {";
1916 indent;
1917 ParseUnionPullDeferred($e,$name,$varname);
1918 deindent;
1919 pidl "}";
1921 add_deferred();
1923 end_flags($e);
1926 sub DeclUnion($$$$)
1928 my ($e,$t,$name,$varname) = @_;
1929 return ($t ne "pull"?"const ":"") . "union $name *$varname";
1932 sub ArgsUnionNdrSize($$)
1934 my ($d,$name) = @_;
1935 return "const union $name *r, uint32_t level, int flags";
1938 $typefamily{UNION} = {
1939 PUSH_FN_BODY => \&ParseUnionPush,
1940 DECL => \&DeclUnion,
1941 PULL_FN_BODY => \&ParseUnionPull,
1942 PRINT_FN_BODY => \&ParseUnionPrint,
1943 SIZE_FN_ARGS => \&ArgsUnionNdrSize,
1944 SIZE_FN_BODY => \&ParseUnionNdrSize,
1947 #####################################################################
1948 # parse a typedef - push side
1949 sub ParseTypedefPush($$$)
1951 my($e,$name,$varname) = @_;
1953 $typefamily{$e->{DATA}->{TYPE}}->{PUSH_FN_BODY}->($e->{DATA}, $name, $varname);
1956 #####################################################################
1957 # parse a typedef - pull side
1958 sub ParseTypedefPull($$$)
1960 my($e,$name,$varname) = @_;
1962 $typefamily{$e->{DATA}->{TYPE}}->{PULL_FN_BODY}->($e->{DATA}, $name, $varname);
1965 #####################################################################
1966 # parse a typedef - print side
1967 sub ParseTypedefPrint($$$)
1969 my($e,$name,$varname) = @_;
1971 $typefamily{$e->{DATA}->{TYPE}}->{PRINT_FN_BODY}->($e->{DATA}, $name, $varname);
1974 #####################################################################
1975 ## calculate the size of a structure
1976 sub ParseTypedefNdrSize($$$)
1978 my($t,$name,$varname) = @_;
1980 $typefamily{$t->{DATA}->{TYPE}}->{SIZE_FN_BODY}->($t->{DATA}, $name, $varname);
1983 sub DeclTypedef($$$$)
1985 my ($e, $t, $name, $varname) = @_;
1987 return $typefamily{$e->{DATA}->{TYPE}}->{DECL}->($e->{DATA}, $t, $name, $varname);
1990 sub ArgsTypedefNdrSize($$$)
1992 my ($d, $name, $varname) = @_;
1993 return $typefamily{$d->{DATA}->{TYPE}}->{SIZE_FN_ARGS}->($d->{DATA}, $name, $varname);
1996 $typefamily{TYPEDEF} = {
1997 PUSH_FN_BODY => \&ParseTypedefPush,
1998 DECL => \&DeclTypedef,
1999 PULL_FN_BODY => \&ParseTypedefPull,
2000 PRINT_FN_BODY => \&ParseTypedefPrint,
2001 SIZE_FN_ARGS => \&ArgsTypedefNdrSize,
2002 SIZE_FN_BODY => \&ParseTypedefNdrSize,
2005 #####################################################################
2006 # parse a function - print side
2007 sub ParseFunctionPrint($)
2009 my($fn) = shift;
2011 pidl_hdr "void ndr_print_$fn->{NAME}(struct ndr_print *ndr, const char *name, int flags, const struct $fn->{NAME} *r);";
2013 return if has_property($fn, "noprint");
2015 pidl "_PUBLIC_ void ndr_print_$fn->{NAME}(struct ndr_print *ndr, const char *name, int flags, const struct $fn->{NAME} *r)";
2016 pidl "{";
2017 indent;
2019 foreach my $e (@{$fn->{ELEMENTS}}) {
2020 DeclareArrayVariables($e);
2023 pidl "ndr_print_struct(ndr, name, \"$fn->{NAME}\");";
2024 pidl "ndr->depth++;";
2026 pidl "if (flags & NDR_SET_VALUES) {";
2027 pidl "\tndr->flags |= LIBNDR_PRINT_SET_VALUES;";
2028 pidl "}";
2030 pidl "if (flags & NDR_IN) {";
2031 indent;
2032 pidl "ndr_print_struct(ndr, \"in\", \"$fn->{NAME}\");";
2033 pidl "ndr->depth++;";
2035 my $env = GenerateFunctionInEnv($fn);
2036 EnvSubstituteValue($env, $fn);
2038 foreach my $e (@{$fn->{ELEMENTS}}) {
2039 if (grep(/in/,@{$e->{DIRECTION}})) {
2040 ParseElementPrint($e, $env->{$e->{NAME}}, $env);
2043 pidl "ndr->depth--;";
2044 deindent;
2045 pidl "}";
2047 pidl "if (flags & NDR_OUT) {";
2048 indent;
2049 pidl "ndr_print_struct(ndr, \"out\", \"$fn->{NAME}\");";
2050 pidl "ndr->depth++;";
2052 $env = GenerateFunctionOutEnv($fn);
2053 foreach my $e (@{$fn->{ELEMENTS}}) {
2054 if (grep(/out/,@{$e->{DIRECTION}})) {
2055 ParseElementPrint($e, $env->{$e->{NAME}}, $env);
2058 if ($fn->{RETURN_TYPE}) {
2059 pidl "ndr_print_$fn->{RETURN_TYPE}(ndr, \"result\", r->out.result);";
2061 pidl "ndr->depth--;";
2062 deindent;
2063 pidl "}";
2065 pidl "ndr->depth--;";
2066 deindent;
2067 pidl "}";
2068 pidl "";
2071 #####################################################################
2072 # parse a function
2073 sub ParseFunctionPush($)
2075 my($fn) = shift;
2077 fn_declare("push", $fn, "NTSTATUS ndr_push_$fn->{NAME}(struct ndr_push *ndr, int flags, const struct $fn->{NAME} *r)") or return;
2079 return if has_property($fn, "nopush");
2081 pidl "{";
2082 indent;
2084 foreach my $e (@{$fn->{ELEMENTS}}) {
2085 DeclareArrayVariables($e);
2088 pidl "if (flags & NDR_IN) {";
2089 indent;
2091 my $env = GenerateFunctionInEnv($fn);
2093 EnvSubstituteValue($env, $fn);
2095 foreach my $e (@{$fn->{ELEMENTS}}) {
2096 if (grep(/in/,@{$e->{DIRECTION}})) {
2097 ParseElementPush($e, "ndr", $env, 1, 1);
2101 deindent;
2102 pidl "}";
2104 pidl "if (flags & NDR_OUT) {";
2105 indent;
2107 $env = GenerateFunctionOutEnv($fn);
2108 foreach my $e (@{$fn->{ELEMENTS}}) {
2109 if (grep(/out/,@{$e->{DIRECTION}})) {
2110 ParseElementPush($e, "ndr", $env, 1, 1);
2114 if ($fn->{RETURN_TYPE}) {
2115 pidl "NDR_CHECK(ndr_push_$fn->{RETURN_TYPE}(ndr, NDR_SCALARS, r->out.result));";
2118 deindent;
2119 pidl "}";
2120 pidl "return NT_STATUS_OK;";
2121 deindent;
2122 pidl "}";
2123 pidl "";
2126 sub AllocateArrayLevel($$$$$)
2128 my ($e,$l,$ndr,$env,$size) = @_;
2130 my $var = ParseExpr($e->{NAME}, $env, $e->{ORIGINAL});
2132 my $pl = GetPrevLevel($e, $l);
2133 if (defined($pl) and
2134 $pl->{TYPE} eq "POINTER" and
2135 $pl->{POINTER_TYPE} eq "ref"
2136 and not $l->{IS_ZERO_TERMINATED}) {
2137 pidl "if (ndr->flags & LIBNDR_FLAG_REF_ALLOC) {";
2138 pidl "\tNDR_PULL_ALLOC_N($ndr, $var, $size);";
2139 pidl "}";
2140 if (grep(/in/,@{$e->{DIRECTION}}) and
2141 grep(/out/,@{$e->{DIRECTION}})) {
2142 pidl "memcpy(r->out.$e->{NAME}, r->in.$e->{NAME}, $size * sizeof(*r->in.$e->{NAME}));";
2144 return;
2147 pidl "NDR_PULL_ALLOC_N($ndr, $var, $size);";
2150 #####################################################################
2151 # parse a function
2152 sub ParseFunctionPull($)
2154 my($fn) = shift;
2156 # pull function args
2157 fn_declare("pull", $fn, "NTSTATUS ndr_pull_$fn->{NAME}(struct ndr_pull *ndr, int flags, struct $fn->{NAME} *r)") or return;
2159 pidl "{";
2160 indent;
2162 # declare any internal pointers we need
2163 foreach my $e (@{$fn->{ELEMENTS}}) {
2164 DeclarePtrVariables($e);
2165 DeclareArrayVariables($e);
2168 my %double_cases = ();
2169 foreach my $e (@{$fn->{ELEMENTS}}) {
2170 next if ($e->{TYPE} eq "EMPTY");
2171 next if ($double_cases{"$e->{NAME}"});
2172 DeclareMemCtxVariables($e);
2173 $double_cases{"$e->{NAME}"} = 1;
2176 pidl "if (flags & NDR_IN) {";
2177 indent;
2179 # auto-init the out section of a structure. I originally argued that
2180 # this was a bad idea as it hides bugs, but coping correctly
2181 # with initialisation and not wiping ref vars is turning
2182 # out to be too tricky (tridge)
2183 foreach my $e (@{$fn->{ELEMENTS}}) {
2184 next unless grep(/out/, @{$e->{DIRECTION}});
2185 pidl "ZERO_STRUCT(r->out);";
2186 pidl "";
2187 last;
2190 my $env = GenerateFunctionInEnv($fn);
2192 foreach my $e (@{$fn->{ELEMENTS}}) {
2193 next unless (grep(/in/, @{$e->{DIRECTION}}));
2194 ParseElementPull($e, "ndr", $env, 1, 1);
2197 # allocate the "simple" out ref variables. FIXME: Shouldn't this have it's
2198 # own flag rather than be in NDR_IN ?
2200 foreach my $e (@{$fn->{ELEMENTS}}) {
2201 next unless (grep(/out/, @{$e->{DIRECTION}}));
2202 next unless ($e->{LEVELS}[0]->{TYPE} eq "POINTER" and
2203 $e->{LEVELS}[0]->{POINTER_TYPE} eq "ref");
2204 next if (($e->{LEVELS}[1]->{TYPE} eq "DATA") and
2205 ($e->{LEVELS}[1]->{DATA_TYPE} eq "string"));
2206 next if (($e->{LEVELS}[1]->{TYPE} eq "ARRAY")
2207 and $e->{LEVELS}[1]->{IS_ZERO_TERMINATED});
2209 if ($e->{LEVELS}[1]->{TYPE} eq "ARRAY") {
2210 my $size = ParseExprExt($e->{LEVELS}[1]->{SIZE_IS}, $env, $e->{ORIGINAL}, check_null_pointer($e, $env, \&pidl, "return NT_STATUS_INVALID_PARAMETER_MIX;"),
2211 check_fully_dereferenced($e, $env));
2213 pidl "NDR_PULL_ALLOC_N(ndr, r->out.$e->{NAME}, $size);";
2215 if (grep(/in/, @{$e->{DIRECTION}})) {
2216 pidl "memcpy(r->out.$e->{NAME}, r->in.$e->{NAME}, $size * sizeof(*r->in.$e->{NAME}));";
2217 } else {
2218 pidl "memset(r->out.$e->{NAME}, 0, $size * sizeof(*r->out.$e->{NAME}));";
2220 } else {
2221 pidl "NDR_PULL_ALLOC(ndr, r->out.$e->{NAME});";
2223 if (grep(/in/, @{$e->{DIRECTION}})) {
2224 pidl "*r->out.$e->{NAME} = *r->in.$e->{NAME};";
2225 } else {
2226 pidl "ZERO_STRUCTP(r->out.$e->{NAME});";
2231 add_deferred();
2232 deindent;
2233 pidl "}";
2235 pidl "if (flags & NDR_OUT) {";
2236 indent;
2238 $env = GenerateFunctionOutEnv($fn);
2239 foreach my $e (@{$fn->{ELEMENTS}}) {
2240 next unless grep(/out/, @{$e->{DIRECTION}});
2241 ParseElementPull($e, "ndr", $env, 1, 1);
2244 if ($fn->{RETURN_TYPE}) {
2245 pidl "NDR_CHECK(ndr_pull_$fn->{RETURN_TYPE}(ndr, NDR_SCALARS, &r->out.result));";
2248 add_deferred();
2249 deindent;
2250 pidl "}";
2252 pidl "return NT_STATUS_OK;";
2253 deindent;
2254 pidl "}";
2255 pidl "";
2258 #####################################################################
2259 # produce a function call table
2260 sub FunctionTable($)
2262 my($interface) = shift;
2263 my $count = 0;
2264 my $uname = uc $interface->{NAME};
2266 return if ($#{$interface->{FUNCTIONS}}+1 == 0);
2267 return unless defined ($interface->{PROPERTIES}->{uuid});
2269 pidl "static const struct dcerpc_interface_call $interface->{NAME}\_calls[] = {";
2270 foreach my $d (@{$interface->{FUNCTIONS}}) {
2271 next if not defined($d->{OPNUM});
2272 pidl "\t{";
2273 pidl "\t\t\"$d->{NAME}\",";
2274 pidl "\t\tsizeof(struct $d->{NAME}),";
2275 pidl "\t\t(ndr_push_flags_fn_t) ndr_push_$d->{NAME},";
2276 pidl "\t\t(ndr_pull_flags_fn_t) ndr_pull_$d->{NAME},";
2277 pidl "\t\t(ndr_print_function_t) ndr_print_$d->{NAME},";
2278 pidl "\t\t".($d->{ASYNC}?"True":"False").",";
2279 pidl "\t},";
2280 $count++;
2282 pidl "\t{ NULL, 0, NULL, NULL, NULL, False }";
2283 pidl "};";
2284 pidl "";
2286 pidl "static const char * const $interface->{NAME}\_endpoint_strings[] = {";
2287 foreach my $ep (@{$interface->{ENDPOINTS}}) {
2288 pidl "\t$ep, ";
2290 my $endpoint_count = $#{$interface->{ENDPOINTS}}+1;
2292 pidl "};";
2293 pidl "";
2295 pidl "static const struct dcerpc_endpoint_list $interface->{NAME}\_endpoints = {";
2296 pidl "\t.count\t= $endpoint_count,";
2297 pidl "\t.names\t= $interface->{NAME}\_endpoint_strings";
2298 pidl "};";
2299 pidl "";
2301 if (! defined $interface->{PROPERTIES}->{authservice}) {
2302 $interface->{PROPERTIES}->{authservice} = "\"host\"";
2305 my @a = split / /, $interface->{PROPERTIES}->{authservice};
2306 my $authservice_count = $#a + 1;
2308 pidl "static const char * const $interface->{NAME}\_authservice_strings[] = {";
2309 foreach my $ap (@a) {
2310 pidl "\t$ap, ";
2312 pidl "};";
2313 pidl "";
2315 pidl "static const struct dcerpc_authservice_list $interface->{NAME}\_authservices = {";
2316 pidl "\t.count\t= $endpoint_count,";
2317 pidl "\t.names\t= $interface->{NAME}\_authservice_strings";
2318 pidl "};";
2319 pidl "";
2321 pidl "\nconst struct dcerpc_interface_table dcerpc_table_$interface->{NAME} = {";
2322 pidl "\t.name\t\t= \"$interface->{NAME}\",";
2323 pidl "\t.syntax_id\t= {";
2324 pidl "\t\t" . print_uuid($interface->{UUID}) .",";
2325 pidl "\t\tDCERPC_$uname\_VERSION";
2326 pidl "\t},";
2327 pidl "\t.helpstring\t= DCERPC_$uname\_HELPSTRING,";
2328 pidl "\t.num_calls\t= $count,";
2329 pidl "\t.calls\t\t= $interface->{NAME}\_calls,";
2330 pidl "\t.endpoints\t= &$interface->{NAME}\_endpoints,";
2331 pidl "\t.authservices\t= &$interface->{NAME}\_authservices";
2332 pidl "};";
2333 pidl "";
2337 #####################################################################
2338 # generate include statements for imported idl files
2339 sub HeaderImport
2341 my @imports = @_;
2342 foreach (@imports) {
2343 s/\.idl\"$//;
2344 s/^\"//;
2345 pidl choose_header("librpc/gen_ndr/ndr_$_\.h", "gen_ndr/ndr_$_.h");
2349 #####################################################################
2350 # generate include statements for included header files
2351 sub HeaderInclude
2353 my @includes = @_;
2354 foreach (@includes) {
2355 pidl_hdr "#include $_";
2359 #####################################################################
2360 # generate prototypes and defines for the interface definitions
2361 # FIXME: these prototypes are for the DCE/RPC client functions, not the
2362 # NDR parser and so do not belong here, technically speaking
2363 sub HeaderInterface($)
2365 my($interface) = shift;
2367 my $count = 0;
2369 pidl_hdr choose_header("librpc/ndr/libndr.h", "ndr.h");
2371 if (has_property($interface, "object")) {
2372 pidl choose_header("librpc/gen_ndr/ndr_orpc.h", "ndr/orpc.h");
2375 if (defined $interface->{PROPERTIES}->{helper}) {
2376 HeaderInclude(split / /, $interface->{PROPERTIES}->{helper});
2379 if (defined $interface->{PROPERTIES}->{uuid}) {
2380 my $name = uc $interface->{NAME};
2381 pidl_hdr "#define DCERPC_$name\_UUID " .
2382 Parse::Pidl::Util::make_str(lc($interface->{PROPERTIES}->{uuid}));
2384 if(!defined $interface->{PROPERTIES}->{version}) { $interface->{PROPERTIES}->{version} = "0.0"; }
2385 pidl_hdr "#define DCERPC_$name\_VERSION $interface->{PROPERTIES}->{version}";
2387 pidl_hdr "#define DCERPC_$name\_NAME \"$interface->{NAME}\"";
2389 if(!defined $interface->{PROPERTIES}->{helpstring}) { $interface->{PROPERTIES}->{helpstring} = "NULL"; }
2390 pidl_hdr "#define DCERPC_$name\_HELPSTRING $interface->{PROPERTIES}->{helpstring}";
2392 pidl_hdr "extern const struct dcerpc_interface_table dcerpc_table_$interface->{NAME};";
2393 pidl_hdr "NTSTATUS dcerpc_server_$interface->{NAME}_init(void);";
2396 foreach (@{$interface->{FUNCTIONS}}) {
2397 next if has_property($_, "noopnum");
2398 next if grep(/$_->{NAME}/,@{$interface->{INHERITED_FUNCTIONS}});
2399 my $u_name = uc $_->{NAME};
2401 my $val = sprintf("0x%02x", $count);
2402 if (defined($interface->{BASE})) {
2403 $val .= " + DCERPC_" . uc $interface->{BASE} . "_CALL_COUNT";
2406 pidl_hdr "#define DCERPC_$u_name ($val)";
2408 pidl_hdr "";
2409 $count++;
2412 my $val = $count;
2414 if (defined($interface->{BASE})) {
2415 $val .= " + DCERPC_" . uc $interface->{BASE} . "_CALL_COUNT";
2418 pidl_hdr "#define DCERPC_" . uc $interface->{NAME} . "_CALL_COUNT ($val)";
2422 sub ParseTypePush($$)
2424 my ($e, $varname) = @_;
2426 # save the old relative_base_offset
2427 pidl "uint32_t _save_relative_base_offset = ndr_push_get_relative_base_offset(ndr);" if defined(has_property($e, "relative_base"));
2428 $typefamily{$e->{TYPE}}->{PUSH_FN_BODY}->($e, $e->{NAME}, $varname);
2429 # restore the old relative_base_offset
2430 pidl "ndr_push_restore_relative_base_offset(ndr, _save_relative_base_offset);" if defined(has_property($e, "relative_base"));
2433 sub ParseTypePushFunction($$)
2435 my ($e, $varname) = @_;
2437 my $args = $typefamily{$e->{TYPE}}->{DECL}->($e, "push", $e->{NAME}, $varname);
2438 fn_declare("push", $e, "NTSTATUS ndr_push_$e->{NAME}(struct ndr_push *ndr, int ndr_flags, $args)") or return;
2440 pidl "{";
2441 indent;
2442 ParseTypePush($e, $varname);
2443 pidl "return NT_STATUS_OK;";
2444 deindent;
2445 pidl "}";
2446 pidl "";;
2449 sub ParseTypePull($$)
2451 my ($e, $varname) = @_;
2453 # save the old relative_base_offset
2454 pidl "uint32_t _save_relative_base_offset = ndr_pull_get_relative_base_offset(ndr);" if defined(has_property($e, "relative_base"));
2455 $typefamily{$e->{TYPE}}->{PULL_FN_BODY}->($e, $e->{NAME}, $varname);
2456 # restore the old relative_base_offset
2457 pidl "ndr_pull_restore_relative_base_offset(ndr, _save_relative_base_offset);" if defined(has_property($e, "relative_base"));
2461 sub ParseTypePullFunction($$)
2463 my ($e, $varname) = @_;
2465 my $args = $typefamily{$e->{TYPE}}->{DECL}->($e, "pull", $e->{NAME}, $varname);
2467 fn_declare("pull", $e, "NTSTATUS ndr_pull_$e->{NAME}(struct ndr_pull *ndr, int ndr_flags, $args)") or return;
2469 pidl "{";
2470 indent;
2471 ParseTypePull($e, $varname);
2472 pidl "return NT_STATUS_OK;";
2473 deindent;
2474 pidl "}";
2475 pidl "";
2478 sub ParseTypePrint($$)
2480 my ($e, $varname) = @_;
2482 $typefamily{$e->{TYPE}}->{PRINT_FN_BODY}->($e, $e->{NAME}, $varname);
2485 sub ParseTypePrintFunction($$)
2487 my ($e, $varname) = @_;
2488 my $args = $typefamily{$e->{TYPE}}->{DECL}->($e, "print", $e->{NAME}, $varname);
2490 pidl_hdr "void ndr_print_$e->{NAME}(struct ndr_print *ndr, const char *name, $args);";
2492 return if (has_property($e, "noprint"));
2494 pidl "_PUBLIC_ void ndr_print_$e->{NAME}(struct ndr_print *ndr, const char *name, $args)";
2495 pidl "{";
2496 indent;
2497 ParseTypePrint($e, $varname);
2498 deindent;
2499 pidl "}";
2500 pidl "";
2503 sub ParseTypeNdrSize($)
2505 my ($t) = @_;
2507 my $varname = "r";
2508 my $tf = $typefamily{$t->{TYPE}};
2509 my $args = $tf->{SIZE_FN_ARGS}->($t, $t->{NAME}, $varname);
2511 fn_declare("size", $t, "size_t ndr_size_$t->{NAME}($args)") or return;
2513 pidl "{";
2514 indent;
2515 $typefamily{$t->{TYPE}}->{SIZE_FN_BODY}->($t, $t->{NAME}, $varname);
2516 deindent;
2517 pidl "}";
2518 pidl "";
2521 #####################################################################
2522 # parse the interface definitions
2523 sub ParseInterface($$)
2525 my($interface,$needed) = @_;
2527 pidl_hdr "#ifndef _HEADER_NDR_$interface->{NAME}";
2528 pidl_hdr "#define _HEADER_NDR_$interface->{NAME}";
2530 pidl_hdr "";
2532 if ($needed->{"compression"}) {
2533 pidl choose_header("librpc/ndr/ndr_compression.h", "ndr/compression.h");
2536 HeaderInterface($interface);
2538 # Typedefs
2539 foreach my $d (@{$interface->{TYPES}}) {
2540 ($needed->{"push_$d->{NAME}"}) && ParseTypePushFunction($d, "r");
2541 ($needed->{"pull_$d->{NAME}"}) && ParseTypePullFunction($d, "r");
2542 ($needed->{"print_$d->{NAME}"}) && ParseTypePrintFunction($d, "r");
2544 # Make sure we don't generate a function twice...
2545 $needed->{"push_$d->{NAME}"} = $needed->{"pull_$d->{NAME}"} =
2546 $needed->{"print_$d->{NAME}"} = 0;
2548 ($needed->{"ndr_size_$d->{NAME}"}) && ParseTypeNdrSize($d);
2551 # Functions
2552 foreach my $d (@{$interface->{FUNCTIONS}}) {
2553 ($needed->{"push_$d->{NAME}"}) && ParseFunctionPush($d);
2554 ($needed->{"pull_$d->{NAME}"}) && ParseFunctionPull($d);
2555 ($needed->{"print_$d->{NAME}"}) && ParseFunctionPrint($d);
2557 # Make sure we don't generate a function twice...
2558 $needed->{"push_$d->{NAME}"} = $needed->{"pull_$d->{NAME}"} =
2559 $needed->{"print_$d->{NAME}"} = 0;
2562 FunctionTable($interface);
2564 pidl_hdr "#endif /* _HEADER_NDR_$interface->{NAME} */";
2567 sub GenerateIncludes()
2569 if (is_intree()) {
2570 pidl "#include \"includes.h\"";
2571 } else {
2572 pidl "#define _GNU_SOURCE";
2573 pidl "#include <stdint.h>";
2574 pidl "#include <stdlib.h>";
2575 pidl "#include <stdio.h>";
2576 pidl "#include <stdbool.h>";
2577 pidl "#include <stdarg.h>";
2578 pidl "#include <string.h>";
2581 # Samba3 has everything in include/includes.h
2582 if (is_intree() != 3) {
2583 pidl choose_header("libcli/util/nterr.h", "core/nterr.h");
2584 pidl choose_header("librpc/gen_ndr/ndr_misc.h", "gen_ndr/ndr_misc.h");
2585 pidl choose_header("librpc/gen_ndr/ndr_dcerpc.h", "gen_ndr/ndr_dcerpc.h");
2586 pidl choose_header("librpc/rpc/dcerpc.h", "dcerpc.h"); #FIXME: This shouldn't be here!
2590 #####################################################################
2591 # parse a parsed IDL structure back into an IDL file
2592 sub Parse($$$)
2594 my($ndr,$gen_header,$ndr_header) = @_;
2596 $tabs = "";
2597 $res = "";
2599 $res_hdr = "";
2600 pidl_hdr "/* header auto-generated by pidl */";
2601 pidl_hdr "";
2602 pidl_hdr "#include \"$gen_header\"" if ($gen_header);
2603 pidl_hdr "";
2605 pidl "/* parser auto-generated by pidl */";
2606 pidl "";
2607 GenerateIncludes();
2608 pidl "#include \"$ndr_header\"" if ($ndr_header);
2609 pidl "";
2611 my %needed = ();
2613 foreach (@{$ndr}) {
2614 ($_->{TYPE} eq "INTERFACE") && NeededInterface($_, \%needed);
2617 foreach (@{$ndr}) {
2618 ($_->{TYPE} eq "INTERFACE") && ParseInterface($_, \%needed);
2619 ($_->{TYPE} eq "IMPORT") && HeaderImport(@{$_->{PATHS}});
2620 ($_->{TYPE} eq "INCLUDE") && HeaderInclude(@{$_->{PATHS}});
2623 return ($res_hdr, $res);
2626 sub NeededElement($$$)
2628 my ($e, $dir, $needed) = @_;
2630 return if ($e->{TYPE} eq "EMPTY");
2632 my @fn = ();
2633 if ($dir eq "print") {
2634 push(@fn, "print_$e->{REPRESENTATION_TYPE}");
2635 } elsif ($dir eq "pull") {
2636 push (@fn, "pull_$e->{TYPE}");
2637 push (@fn, "ndr_$e->{TYPE}_to_$e->{REPRESENTATION_TYPE}")
2638 if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE});
2639 } elsif ($dir eq "push") {
2640 push (@fn, "push_$e->{TYPE}");
2641 push (@fn, "ndr_$e->{REPRESENTATION_TYPE}_to_$e->{TYPE}")
2642 if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE});
2643 } else {
2644 die("invalid direction `$dir'");
2647 foreach (@fn) {
2648 unless (defined($needed->{$_})) {
2649 $needed->{$_} = 1;
2654 sub NeededFunction($$)
2656 my ($fn,$needed) = @_;
2657 $needed->{"pull_$fn->{NAME}"} = 1;
2658 $needed->{"push_$fn->{NAME}"} = 1;
2659 $needed->{"print_$fn->{NAME}"} = 1;
2660 foreach my $e (@{$fn->{ELEMENTS}}) {
2661 $e->{PARENT} = $fn;
2662 NeededElement($e, $_, $needed) foreach ("pull", "push", "print");
2666 sub NeededType($$)
2668 my ($t,$needed) = @_;
2669 if (has_property($t, "public")) {
2670 $needed->{"pull_$t->{NAME}"} = 1;
2671 $needed->{"push_$t->{NAME}"} = 1;
2672 $needed->{"print_$t->{NAME}"} = 1;
2675 if ($t->{DATA}->{TYPE} eq "STRUCT" or $t->{DATA}->{TYPE} eq "UNION") {
2676 if (has_property($t, "gensize")) {
2677 $needed->{"ndr_size_$t->{NAME}"} = 1;
2680 for my $e (@{$t->{DATA}->{ELEMENTS}}) {
2681 $e->{PARENT} = $t->{DATA};
2682 if (has_property($e, "compression")) {
2683 $needed->{"compression"} = 1;
2685 NeededElement($e, "pull", $needed) if ($needed->{"pull_$t->{NAME}"});
2686 NeededElement($e, "push", $needed) if ($needed->{"push_$t->{NAME}"});
2687 NeededElement($e, "print", $needed) if ($needed->{"print_$t->{NAME}"});
2692 #####################################################################
2693 # work out what parse functions are needed
2694 sub NeededInterface($$)
2696 my ($interface,$needed) = @_;
2697 NeededFunction($_, $needed) foreach (@{$interface->{FUNCTIONS}});
2698 NeededType($_, $needed) foreach (reverse @{$interface->{TYPES}});