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
;
12 @EXPORT_OK = qw(check_null_pointer NeededFunction NeededElement NeededType $res NeededInterface TypeFunctionName ParseElementPrint);
15 use Parse::Pidl::Typelist qw(hasType getType mapTypeName typeHasBody);
16 use Parse
::Pidl
::Util
qw(has_property ParseExpr ParseExprExt print_uuid unmake_str);
17 use Parse
::Pidl
::CUtil
qw(get_pointer_to get_value_of get_array_element);
18 use Parse
::Pidl
::NDR
qw(GetPrevLevel GetNextLevel ContainsDeferred ContainsPipe is_charset_array);
19 use Parse
::Pidl
::Samba4
qw(is_intree choose_header ArrayDynamicallyAllocated);
20 use Parse
::Pidl
::Samba4
::Header
qw(GenerateFunctionInEnv GenerateFunctionOutEnv EnvSubstituteValue GenerateStructEnv);
21 use Parse
::Pidl
qw(warning);
23 use vars
qw($VERSION);
31 my $self = { res => "", res_hdr => "", deferred => [], tabs => "", defer_tabs => "" };
38 return $typefamily{$n};
43 my ($e, $var_name) = @_;
47 foreach my $l (@{$e->{LEVELS}}) {
48 if ($l->{TYPE} eq "POINTER") {
50 } elsif ($l->{TYPE} eq "ARRAY") {
52 if (($pointers == 0) and
53 (not $l->{IS_FIXED}) and
54 (not $l->{IS_INLINE})) {
55 return get_value_of($var_name);
57 } elsif ($l->{TYPE} eq "DATA") {
58 if (Parse::Pidl::Typelist::scalar_is_reference($l->{DATA_TYPE})) {
59 return get_value_of($var_name) unless ($pointers or $arrays);
67 sub has_fast_array($$)
71 return 0 if ($l->{TYPE} ne "ARRAY");
73 my $nl = GetNextLevel($e,$l);
74 return 0 unless ($nl->{TYPE} eq "DATA");
75 return 0 unless (hasType($nl->{DATA_TYPE}));
77 my $t = getType($nl->{DATA_TYPE});
79 # Only uint8 and string have fast array functions at the moment
80 return ($t->{NAME} eq "uint8") or ($t->{NAME} eq "string");
84 ####################################
85 # pidl() is our basic output routine
90 $self->{res} .= $self->{tabs};
96 sub pidl_hdr($$) { my ($self, $d) = @_; $self->{res_hdr} .= "$d\n"; }
98 ####################################
99 # defer() is like pidl(), but adds to
100 # a deferred buffer which is then added to the
101 # output buffer at the end of the structure/union/function
102 # This is needed to cope with code that must be pushed back
103 # to the end of a block of elements
104 sub defer_indent($) { my ($self) = @_; $self->{defer_tabs}.="\t"; }
105 sub defer_deindent($) { my ($self) = @_; $self->{defer_tabs}=substr($self->{defer_tabs}, 0, -1); }
111 push(@{$self->{deferred}}, $self->{defer_tabs}.$d);
115 ########################################
116 # add the deferred content to the current
121 $self->pidl($_) foreach (@{$self->{deferred}});
122 $self->{deferred} = [];
123 $self->{defer_tabs} = "";
129 $self->{tabs} .= "\t";
135 $self->{tabs} = substr($self->{tabs}, 0, -1);
138 #####################################################################
139 # declare a function public or static, depending on its attributes
142 my ($self,$type,$fn,$decl) = @_;
144 if (has_property($fn, "no$type")) {
145 $self->pidl_hdr("$decl;");
149 if (has_property($fn, "public")) {
150 $self->pidl_hdr("$decl;");
151 $self->pidl("_PUBLIC_ $decl");
153 $self->pidl("static $decl");
159 ###################################################################
160 # setup any special flags for an element or structure
163 my ($self, $e, $ndr) = @_;
164 my $flags = has_property($e, "flag");
165 if (defined $flags) {
168 $self->pidl("uint32_t _flags_save_$e->{TYPE} = $ndr->flags;");
169 $self->pidl("ndr_set_flags(&$ndr->flags, $flags);");
173 ###################################################################
174 # end any special flags for an element or structure
177 my ($self, $e, $ndr) = @_;
178 my $flags = has_property($e, "flag");
179 if (defined $flags) {
180 $self->pidl("$ndr->flags = _flags_save_$e->{TYPE};");
186 #####################################################################
187 # parse the data of an array - push side
188 sub ParseArrayPushHeader($$$$$$)
190 my ($self,$e,$l,$ndr,$var_name,$env) = @_;
195 if ($l->{IS_ZERO_TERMINATED}) {
196 if (has_property($e, "charset")) {
197 $size = $length = "ndr_charset_length($var_name, CH_$e->{PROPERTIES}->{charset})";
199 $size = $length = "ndr_string_length($var_name, sizeof(*$var_name))";
201 if (defined($l->{SIZE_IS})) {
202 $size = ParseExpr($l->{SIZE_IS}, $env, $e);
204 if (defined($l->{LENGTH_IS})) {
205 $length = ParseExpr($l->{LENGTH_IS}, $env, $e);
208 $size = ParseExpr($l->{SIZE_IS}, $env, $e);
209 $length = ParseExpr($l->{LENGTH_IS}, $env, $e);
212 if ((!$l->{IS_SURROUNDING}) and $l->{IS_CONFORMANT}) {
213 $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, $size));");
216 if ($l->{IS_VARYING}) {
217 $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, 0));"); # array offset
218 $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, $length));");
224 sub check_fully_dereferenced($$)
226 my ($element, $env) = @_;
232 # Figure out the number of pointers in $ptr
233 my $expandedvar = $origvar;
234 $expandedvar =~ s/^(\**)//;
238 foreach (keys %$env) {
239 if ($env->{$_} eq $expandedvar) {
245 return($origvar) unless (defined($var));
247 foreach (@{$element->{PARENT}->{ELEMENTS}}) {
248 if ($_->{NAME} eq $var) {
254 $e or die("Environment doesn't match siblings");
256 # See if pointer at pointer level $level
257 # needs to be checked.
259 foreach (@{$e->{LEVELS}}) {
260 if ($_->{TYPE} eq "POINTER") {
261 $nump = $_->{POINTER_INDEX}+1;
264 warning($element->{ORIGINAL}, "Got pointer for `$e->{NAME}', expected fully dereferenced variable") if ($nump > length($ptr));
269 sub check_null_pointer($$$$)
271 my ($element, $env, $print_fn, $return) = @_;
274 my $expandedvar = shift;
277 # Figure out the number of pointers in $ptr
278 $expandedvar =~ s/^(\**)//;
282 foreach (keys %$env) {
283 if ($env->{$_} eq $expandedvar) {
292 foreach (@{$element->{PARENT}->{ELEMENTS}}) {
293 if ($_->{NAME} eq $var) {
299 $e or die("Environment doesn't match siblings");
301 # See if pointer at pointer level $level
302 # needs to be checked.
303 foreach my $l (@{$e->{LEVELS}}) {
304 if ($l->{TYPE} eq "POINTER" and
305 $l->{POINTER_INDEX} == length($ptr)) {
306 # No need to check ref pointers
307 $check = ($l->{POINTER_TYPE} ne "ref");
311 if ($l->{TYPE} eq "DATA") {
312 warning($element, "too much dereferences for `$var'");
316 warning($element, "unknown dereferenced expression `$expandedvar'");
320 $print_fn->("if ($ptr$expandedvar == NULL) $return") if $check;
324 sub ParseArrayPullGetSize($$$$$$)
326 my ($self,$e,$l,$ndr,$var_name,$env) = @_;
330 if ($l->{IS_CONFORMANT}) {
331 $size = "ndr_get_array_size($ndr, " . get_pointer_to($var_name) . ")";
332 } elsif ($l->{IS_ZERO_TERMINATED} and $l->{SIZE_IS} == 0 and $l->{LENGTH_IS} == 0) { # Noheader arrays
333 $size = "ndr_get_string_size($ndr, sizeof(*$var_name))";
335 $size = ParseExprExt($l->{SIZE_IS}, $env, $e->{ORIGINAL},
336 check_null_pointer($e, $env, sub { $self->pidl(shift); },
337 "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for size_is()\");"),
338 check_fully_dereferenced($e, $env));
341 $self->pidl("size_$e->{NAME}_$l->{LEVEL_INDEX} = $size;");
342 my $array_size = "size_$e->{NAME}_$l->{LEVEL_INDEX}";
344 if (my $range = has_property($e, "range")) {
345 my ($low, $high) = split(/,/, $range, 2);
347 warning(0, "$low is invalid for the range of an array size");
350 $self->pidl("if ($array_size > $high) {");
352 $self->pidl("if ($array_size < $low || $array_size > $high) {");
354 $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_RANGE, \"value out of range\");");
361 #####################################################################
362 # parse an array - pull side
363 sub ParseArrayPullGetLength($$$$$$;$)
365 my ($self,$e,$l,$ndr,$var_name,$env,$array_size) = @_;
367 if (not defined($array_size)) {
368 $array_size = $self->ParseArrayPullGetSize($e, $l, $ndr, $var_name, $env);
371 if (not $l->{IS_VARYING}) {
375 my $length = "ndr_get_array_length($ndr, " . get_pointer_to($var_name) .")";
376 $self->pidl("length_$e->{NAME}_$l->{LEVEL_INDEX} = $length;");
377 my $array_length = "length_$e->{NAME}_$l->{LEVEL_INDEX}";
379 if (my $range = has_property($e, "range")) {
380 my ($low, $high) = split(/,/, $range, 2);
382 warning(0, "$low is invalid for the range of an array size");
385 $self->pidl("if ($array_length > $high) {");
387 $self->pidl("if ($array_length < $low || $array_length > $high) {");
389 $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_RANGE, \"value out of range\");");
393 return $array_length;
396 #####################################################################
397 # parse an array - pull side
398 sub ParseArrayPullHeader($$$$$$)
400 my ($self,$e,$l,$ndr,$var_name,$env) = @_;
402 if ((!$l->{IS_SURROUNDING}) and $l->{IS_CONFORMANT}) {
403 $self->pidl("NDR_CHECK(ndr_pull_array_size($ndr, " . get_pointer_to($var_name) . "));");
406 if ($l->{IS_VARYING}) {
407 $self->pidl("NDR_CHECK(ndr_pull_array_length($ndr, " . get_pointer_to($var_name) . "));");
410 my $array_size = $self->ParseArrayPullGetSize($e, $l, $ndr, $var_name, $env);
411 my $array_length = $self->ParseArrayPullGetLength($e, $l, $ndr, $var_name, $env, $array_size);
413 if ($array_length ne $array_size) {
414 $self->pidl("if ($array_length > $array_size) {");
416 $self->pidl("return ndr_pull_error($ndr, NDR_ERR_ARRAY_SIZE, \"Bad array size %u should exceed array length %u\", $array_size, $array_length);");
421 if ($l->{IS_CONFORMANT} and (defined($l->{SIZE_IS}) or not $l->{IS_ZERO_TERMINATED})) {
422 $self->defer("if ($var_name) {");
424 my $size = ParseExprExt($l->{SIZE_IS}, $env, $e->{ORIGINAL},
425 check_null_pointer($e, $env, sub { $self->defer(shift); },
426 "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for size_is()\");"),
427 check_fully_dereferenced($e, $env));
428 $self->defer("NDR_CHECK(ndr_check_array_size($ndr, (void*)" . get_pointer_to($var_name) . ", $size));");
429 $self->defer_deindent;
433 if ($l->{IS_VARYING} and (defined($l->{LENGTH_IS}) or not $l->{IS_ZERO_TERMINATED})) {
434 $self->defer("if ($var_name) {");
436 my $length = ParseExprExt($l->{LENGTH_IS}, $env, $e->{ORIGINAL},
437 check_null_pointer($e, $env, sub { $self->defer(shift); },
438 "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for length_is()\");"),
439 check_fully_dereferenced($e, $env));
440 $self->defer("NDR_CHECK(ndr_check_array_length($ndr, (void*)" . get_pointer_to($var_name) . ", $length));");
441 $self->defer_deindent;
445 if (ArrayDynamicallyAllocated($e,$l) and not is_charset_array($e,$l)) {
446 $self->AllocateArrayLevel($e,$l,$ndr,$var_name,$array_size);
449 return $array_length;
452 sub compression_alg($$)
455 my ($alg, $clen, $dlen) = split(/,/, $l->{COMPRESSION});
460 sub compression_clen($$$)
462 my ($e, $l, $env) = @_;
463 my ($alg, $clen, $dlen) = split(/,/, $l->{COMPRESSION});
465 return ParseExpr($clen, $env, $e->{ORIGINAL});
468 sub compression_dlen($$$)
470 my ($e,$l,$env) = @_;
471 my ($alg, $clen, $dlen) = split(/,/, $l->{COMPRESSION});
473 return ParseExpr($dlen, $env, $e->{ORIGINAL});
476 sub ParseCompressionPushStart($$$$$)
478 my ($self,$e,$l,$ndr,$env) = @_;
479 my $comndr = "$ndr\_compressed";
480 my $alg = compression_alg($e, $l);
481 my $dlen = compression_dlen($e, $l, $env);
485 $self->pidl("struct ndr_push *$comndr;");
486 $self->pidl("NDR_CHECK(ndr_push_compression_start($ndr, &$comndr, $alg, $dlen));");
491 sub ParseCompressionPushEnd($$$$$)
493 my ($self,$e,$l,$ndr,$env) = @_;
494 my $comndr = "$ndr\_compressed";
495 my $alg = compression_alg($e, $l);
496 my $dlen = compression_dlen($e, $l, $env);
498 $self->pidl("NDR_CHECK(ndr_push_compression_end($ndr, $comndr, $alg, $dlen));");
503 sub ParseCompressionPullStart($$$$$)
505 my ($self,$e,$l,$ndr,$env) = @_;
506 my $comndr = "$ndr\_compressed";
507 my $alg = compression_alg($e, $l);
508 my $dlen = compression_dlen($e, $l, $env);
512 $self->pidl("struct ndr_pull *$comndr;");
513 $self->pidl("NDR_CHECK(ndr_pull_compression_start($ndr, &$comndr, $alg, $dlen));");
518 sub ParseCompressionPullEnd($$$$$)
520 my ($self,$e,$l,$ndr,$env) = @_;
521 my $comndr = "$ndr\_compressed";
522 my $alg = compression_alg($e, $l);
523 my $dlen = compression_dlen($e, $l, $env);
525 $self->pidl("NDR_CHECK(ndr_pull_compression_end($ndr, $comndr, $alg, $dlen));");
530 sub ParseSubcontextPushStart($$$$$)
532 my ($self,$e,$l,$ndr,$env) = @_;
533 my $subndr = "_ndr_$e->{NAME}";
534 my $subcontext_size = ParseExpr($l->{SUBCONTEXT_SIZE}, $env, $e->{ORIGINAL});
538 $self->pidl("struct ndr_push *$subndr;");
539 $self->pidl("NDR_CHECK(ndr_push_subcontext_start($ndr, &$subndr, $l->{HEADER_SIZE}, $subcontext_size));");
541 if (defined $l->{COMPRESSION}) {
542 $subndr = $self->ParseCompressionPushStart($e, $l, $subndr, $env);
548 sub ParseSubcontextPushEnd($$$$$)
550 my ($self,$e,$l,$ndr,$env) = @_;
551 my $subndr = "_ndr_$e->{NAME}";
552 my $subcontext_size = ParseExpr($l->{SUBCONTEXT_SIZE}, $env, $e->{ORIGINAL});
554 if (defined $l->{COMPRESSION}) {
555 $self->ParseCompressionPushEnd($e, $l, $subndr, $env);
558 $self->pidl("NDR_CHECK(ndr_push_subcontext_end($ndr, $subndr, $l->{HEADER_SIZE}, $subcontext_size));");
563 sub ParseSubcontextPullStart($$$$$)
565 my ($self,$e,$l,$ndr,$env) = @_;
566 my $subndr = "_ndr_$e->{NAME}";
567 my $subcontext_size = ParseExpr($l->{SUBCONTEXT_SIZE}, $env, $e->{ORIGINAL});
571 $self->pidl("struct ndr_pull *$subndr;");
572 $self->pidl("NDR_CHECK(ndr_pull_subcontext_start($ndr, &$subndr, $l->{HEADER_SIZE}, $subcontext_size));");
574 if (defined $l->{COMPRESSION}) {
575 $subndr = $self->ParseCompressionPullStart($e, $l, $subndr, $env);
581 sub ParseSubcontextPullEnd($$$$$)
583 my ($self,$e,$l,$ndr,$env) = @_;
584 my $subndr = "_ndr_$e->{NAME}";
585 my $subcontext_size = ParseExpr($l->{SUBCONTEXT_SIZE}, $env, $e->{ORIGINAL});
587 if (defined $l->{COMPRESSION}) {
588 $self->ParseCompressionPullEnd($e, $l, $subndr, $env);
591 $self->pidl("NDR_CHECK(ndr_pull_subcontext_end($ndr, $subndr, $l->{HEADER_SIZE}, $subcontext_size));");
596 sub ParseElementPushLevel
598 my ($self,$e,$l,$ndr,$var_name,$env,$primitives,$deferred) = @_;
600 my $ndr_flags = CalcNdrFlags($l, $primitives, $deferred);
602 if ($l->{TYPE} eq "ARRAY" and ($l->{IS_CONFORMANT} or $l->{IS_VARYING})) {
603 $var_name = get_pointer_to($var_name);
606 if (defined($ndr_flags)) {
607 if ($l->{TYPE} eq "SUBCONTEXT") {
608 my $subndr = $self->ParseSubcontextPushStart($e, $l, $ndr, $env);
609 $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $subndr, $var_name, $env, 1, 1);
610 $self->ParseSubcontextPushEnd($e, $l, $ndr, $env);
611 } elsif ($l->{TYPE} eq "POINTER") {
612 $self->ParsePtrPush($e, $l, $ndr, $var_name);
613 } elsif ($l->{TYPE} eq "ARRAY") {
614 my $length = $self->ParseArrayPushHeader($e, $l, $ndr, $var_name, $env);
616 my $nl = GetNextLevel($e, $l);
618 # Allow speedups for arrays of scalar types
619 if (is_charset_array($e,$l)) {
620 $self->pidl("NDR_CHECK(ndr_push_charset($ndr, $ndr_flags, $var_name, $length, sizeof(" . mapTypeName($nl->{DATA_TYPE}) . "), CH_$e->{PROPERTIES}->{charset}));");
622 } elsif (has_fast_array($e,$l)) {
623 $self->pidl("NDR_CHECK(ndr_push_array_$nl->{DATA_TYPE}($ndr, $ndr_flags, $var_name, $length));");
626 } elsif ($l->{TYPE} eq "SWITCH") {
627 $self->ParseSwitchPush($e, $l, $ndr, $var_name, $env);
628 } elsif ($l->{TYPE} eq "DATA") {
629 $self->ParseDataPush($e, $l, $ndr, $var_name, $primitives, $deferred);
630 } elsif ($l->{TYPE} eq "TYPEDEF") {
631 $typefamily{$e->{DATA}->{TYPE}}->{PUSH_FN_BODY}->($self, $e->{DATA}, $ndr, $var_name);
635 if ($l->{TYPE} eq "POINTER" and $l->{POINTER_TYPE} eq "ignore") {
636 $self->pidl("/* [ignore] '$e->{NAME}' */");
637 } elsif ($l->{TYPE} eq "POINTER" and $deferred) {
638 my $rel_var_name = $var_name;
639 if ($l->{POINTER_TYPE} ne "ref") {
640 $self->pidl("if ($var_name) {");
642 if ($l->{POINTER_TYPE} eq "relative") {
643 $self->pidl("NDR_CHECK(ndr_push_relative_ptr2_start($ndr, $rel_var_name));");
645 if ($l->{POINTER_TYPE} eq "relative_short") {
646 $self->pidl("NDR_CHECK(ndr_push_short_relative_ptr2($ndr, $var_name));");
649 $var_name = get_value_of($var_name);
650 $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, 1, 1);
652 if ($l->{POINTER_TYPE} ne "ref") {
653 if ($l->{POINTER_TYPE} eq "relative") {
654 $self->pidl("NDR_CHECK(ndr_push_relative_ptr2_end($ndr, $rel_var_name));");
659 } elsif ($l->{TYPE} eq "ARRAY" and not has_fast_array($e,$l) and
660 not is_charset_array($e, $l)) {
661 my $length = ParseExpr($l->{LENGTH_IS}, $env, $e->{ORIGINAL});
662 my $counter = "cntr_$e->{NAME}_$l->{LEVEL_INDEX}";
664 my $array_pointless = ($length eq "0");
666 if ($array_pointless) {
667 warning($e->{ORIGINAL}, "pointless array `$e->{NAME}' will always have size 0");
670 $var_name = get_array_element($var_name, $counter);
672 if ((($primitives and not $l->{IS_DEFERRED}) or ($deferred and $l->{IS_DEFERRED})) and not $array_pointless) {
673 $self->pidl("for ($counter = 0; $counter < ($length); $counter++) {");
675 $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, 1, 0);
680 if ($deferred and ContainsDeferred($e, $l) and not $array_pointless) {
681 $self->pidl("for ($counter = 0; $counter < ($length); $counter++) {");
683 $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, 0, 1);
687 } elsif ($l->{TYPE} eq "SWITCH") {
688 $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, $primitives, $deferred);
692 #####################################################################
693 # parse scalars in a structure element
694 sub ParseElementPush($$$$$$)
696 my ($self,$e,$ndr,$env,$primitives,$deferred) = @_;
699 my $var_name = $env->{$e->{NAME}};
701 return if ContainsPipe($e, $e->{LEVELS}[0]);
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}) {
709 my $transmit_name = "_transmit_$e->{NAME}";
710 $self->pidl(mapTypeName($e->{TYPE}) ." $transmit_name;");
711 $self->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 $self->start_flags($e, $ndr);
719 if (defined(my $value = has_property($e, "value"))) {
720 $var_name = ParseExpr($value, $env, $e->{ORIGINAL});
723 $self->ParseElementPushLevel($e, $e->{LEVELS}[0], $ndr, $var_name, $env, $primitives, $deferred);
725 $self->end_flags($e, $ndr);
727 if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
733 #####################################################################
734 # parse a pointer in a struct element or function
735 sub ParsePtrPush($$$$$)
737 my ($self,$e,$l,$ndr,$var_name) = @_;
739 if ($l->{POINTER_TYPE} eq "ref") {
740 $self->pidl("if ($var_name == NULL) {");
742 $self->pidl("return ndr_push_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL [ref] pointer\");");
745 if ($l->{LEVEL} eq "EMBEDDED") {
746 $self->pidl("NDR_CHECK(ndr_push_ref_ptr(ndr));");
748 } elsif ($l->{POINTER_TYPE} eq "relative") {
749 $self->pidl("NDR_CHECK(ndr_push_relative_ptr1($ndr, $var_name));");
750 } elsif ($l->{POINTER_TYPE} eq "relative_short") {
751 $self->pidl("NDR_CHECK(ndr_push_short_relative_ptr1($ndr, $var_name));");
752 } elsif ($l->{POINTER_TYPE} eq "unique") {
753 $self->pidl("NDR_CHECK(ndr_push_unique_ptr($ndr, $var_name));");
754 } elsif ($l->{POINTER_TYPE} eq "full") {
755 $self->pidl("NDR_CHECK(ndr_push_full_ptr($ndr, $var_name));");
756 } elsif ($l->{POINTER_TYPE} eq "ignore") {
757 # We don't want this pointer to appear on the wire at all
758 $self->pidl("NDR_CHECK(ndr_push_uint3264(ndr, NDR_SCALARS, 0));");
760 die("Unhandled pointer type $l->{POINTER_TYPE}");
764 sub need_pointer_to($$$)
766 my ($e, $l, $scalar_only) = @_;
769 if (ref($l->{DATA_TYPE})) {
770 $t = "$l->{DATA_TYPE}->{TYPE}_$l->{DATA_TYPE}->{NAME}";
772 $t = $l->{DATA_TYPE};
775 if (not Parse::Pidl::Typelist::is_scalar($t)) {
776 return 1 if $scalar_only;
781 foreach my $tl (@{$e->{LEVELS}}) {
783 if ($tl->{TYPE} eq "ARRAY") {
788 if (Parse::Pidl::Typelist::scalar_is_reference($t)) {
789 return 1 unless $arrays;
795 sub ParseDataPrint($$$$$)
797 my ($self, $e, $l, $ndr, $var_name) = @_;
799 if (not ref($l->{DATA_TYPE}) or defined($l->{DATA_TYPE}->{NAME})) {
801 if (need_pointer_to($e, $l, 1)) {
802 $var_name = get_pointer_to($var_name);
805 $self->pidl(TypeFunctionName("ndr_print", $l->{DATA_TYPE})."($ndr, \"$e->{NAME}\", $var_name);");
807 $self->ParseTypePrint($l->{DATA_TYPE}, $ndr, $var_name);
811 #####################################################################
812 # print scalars in a structure element
813 sub ParseElementPrint($$$$$)
815 my($self, $e, $ndr, $var_name, $env) = @_;
817 return if (has_property($e, "noprint"));
819 my $ignore_depth = 0xFFFF;
821 if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
822 $self->pidl("ndr_print_$e->{REPRESENTATION_TYPE}($ndr, \"$e->{NAME}\", $var_name);");
826 $var_name = append_prefix($e, $var_name);
828 if (defined(my $value = has_property($e, "value"))) {
829 $var_name = "($ndr->flags & LIBNDR_PRINT_SET_VALUES)?" . ParseExpr($value,$env, $e->{ORIGINAL}) . ":$var_name";
832 foreach my $l (@{$e->{LEVELS}}) {
835 if ($cur_depth > $ignore_depth) {
839 if ($l->{TYPE} eq "POINTER") {
840 $self->pidl("ndr_print_ptr($ndr, \"$e->{NAME}\", $var_name);");
841 if ($l->{POINTER_TYPE} eq "ignore") {
842 $self->pidl("/* [ignore] '$e->{NAME}' */");
843 $ignore_depth = $cur_depth;
846 $self->pidl("$ndr->depth++;");
847 if ($l->{POINTER_TYPE} ne "ref") {
848 $self->pidl("if ($var_name) {");
851 $var_name = get_value_of($var_name);
852 } elsif ($l->{TYPE} eq "ARRAY") {
855 if ($l->{IS_CONFORMANT} or $l->{IS_VARYING}) {
856 $var_name = get_pointer_to($var_name);
859 if ($l->{IS_ZERO_TERMINATED} and not defined($l->{LENGTH_IS})) {
860 $length = "ndr_string_length($var_name, sizeof(*$var_name))";
862 $length = ParseExprExt($l->{LENGTH_IS}, $env, $e->{ORIGINAL},
863 check_null_pointer($e, $env, sub { $self->pidl(shift); }, "return;"), check_fully_dereferenced($e, $env));
866 if (is_charset_array($e,$l)) {
867 $self->pidl("ndr_print_string($ndr, \"$e->{NAME}\", $var_name);");
869 } elsif (has_fast_array($e, $l)) {
870 my $nl = GetNextLevel($e, $l);
871 $self->pidl("ndr_print_array_$nl->{DATA_TYPE}($ndr, \"$e->{NAME}\", $var_name, $length);");
874 my $counter = "cntr_$e->{NAME}_$l->{LEVEL_INDEX}";
876 $self->pidl("$ndr->print($ndr, \"\%s: ARRAY(\%d)\", \"$e->{NAME}\", (int)$length);");
877 $self->pidl("$ndr->depth++;");
878 $self->pidl("for ($counter = 0; $counter < ($length); $counter++) {");
881 $var_name = get_array_element($var_name, $counter);
883 } elsif ($l->{TYPE} eq "DATA") {
884 $self->ParseDataPrint($e, $l, $ndr, $var_name);
885 } elsif ($l->{TYPE} eq "SWITCH") {
886 my $switch_var = ParseExprExt($l->{SWITCH_IS}, $env, $e->{ORIGINAL},
887 check_null_pointer($e, $env, sub { $self->pidl(shift); }, "return;"), check_fully_dereferenced($e, $env));
888 $self->pidl("ndr_print_set_switch_value($ndr, " . get_pointer_to($var_name) . ", $switch_var);");
892 foreach my $l (reverse @{$e->{LEVELS}}) {
895 if ($cur_depth > $ignore_depth) {
899 if ($l->{TYPE} eq "POINTER") {
900 if ($l->{POINTER_TYPE} eq "ignore") {
904 if ($l->{POINTER_TYPE} ne "ref") {
908 $self->pidl("$ndr->depth--;");
909 } elsif (($l->{TYPE} eq "ARRAY")
910 and not is_charset_array($e,$l)
911 and not has_fast_array($e,$l)) {
914 $self->pidl("$ndr->depth--;");
919 #####################################################################
920 # parse scalars in a structure element - pull size
921 sub ParseSwitchPull($$$$$$)
923 my($self,$e,$l,$ndr,$var_name,$env) = @_;
924 my $switch_var = ParseExprExt($l->{SWITCH_IS}, $env, $e->{ORIGINAL},
925 check_null_pointer($e, $env, sub { $self->pidl(shift); },
926 "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for switch_is()\");"),
927 check_fully_dereferenced($e, $env));
929 $var_name = get_pointer_to($var_name);
930 $self->pidl("NDR_CHECK(ndr_pull_set_switch_value($ndr, $var_name, $switch_var));");
933 #####################################################################
934 # push switch element
935 sub ParseSwitchPush($$$$$$)
937 my($self,$e,$l,$ndr,$var_name,$env) = @_;
938 my $switch_var = ParseExprExt($l->{SWITCH_IS}, $env, $e->{ORIGINAL},
939 check_null_pointer($e, $env, sub { $self->pidl(shift); },
940 "return ndr_push_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for switch_is()\");"),
941 check_fully_dereferenced($e, $env));
943 $var_name = get_pointer_to($var_name);
944 $self->pidl("NDR_CHECK(ndr_push_set_switch_value($ndr, $var_name, $switch_var));");
947 sub ParseDataPull($$$$$$$)
949 my ($self,$e,$l,$ndr,$var_name,$primitives,$deferred) = @_;
951 if (not ref($l->{DATA_TYPE}) or defined($l->{DATA_TYPE}->{NAME})) {
953 my $ndr_flags = CalcNdrFlags($l, $primitives, $deferred);
955 if (need_pointer_to($e, $l, 0)) {
956 $var_name = get_pointer_to($var_name);
959 $var_name = get_pointer_to($var_name);
961 if (has_property($e, "skip")) {
962 $self->pidl("/* [skip] '$var_name' */");
964 $self->pidl("NDR_CHECK(".TypeFunctionName("ndr_pull", $l->{DATA_TYPE})."($ndr, $ndr_flags, $var_name));");
967 my $pl = GetPrevLevel($e, $l);
969 my $range = has_property($e, "range");
970 if ($range and $pl->{TYPE} ne "ARRAY") {
971 $var_name = get_value_of($var_name);
972 my $signed = Parse::Pidl::Typelist::is_signed($l->{DATA_TYPE});
973 my ($low, $high) = split(/,/, $range, 2);
974 if ($low < 0 and not $signed) {
975 warning(0, "$low is invalid for the range of an unsigned type");
977 if ($low == 0 and not $signed) {
978 $self->pidl("if ($var_name > $high) {");
980 $self->pidl("if ($var_name < $low || $var_name > $high) {");
982 $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_RANGE, \"value out of range\");");
986 $self->ParseTypePull($l->{DATA_TYPE}, $ndr, $var_name, $primitives, $deferred);
990 sub ParseDataPush($$$$$$$)
992 my ($self,$e,$l,$ndr,$var_name,$primitives,$deferred) = @_;
994 if (not ref($l->{DATA_TYPE}) or defined($l->{DATA_TYPE}->{NAME})) {
996 my $ndr_flags = CalcNdrFlags($l, $primitives, $deferred);
998 # strings are passed by value rather than reference
999 if (need_pointer_to($e, $l, 1)) {
1000 $var_name = get_pointer_to($var_name);
1003 if (has_property($e, "skip")) {
1004 $self->pidl("/* [skip] '$var_name' */");
1006 $self->pidl("NDR_CHECK(".TypeFunctionName("ndr_push", $l->{DATA_TYPE})."($ndr, $ndr_flags, $var_name));");
1009 $self->ParseTypePush($l->{DATA_TYPE}, $ndr, $var_name, $primitives, $deferred);
1013 sub CalcNdrFlags($$$)
1015 my ($l,$primitives,$deferred) = @_;
1020 # Add NDR_SCALARS if this one is deferred
1021 # and deferreds may be pushed
1022 $scalars = 1 if ($l->{IS_DEFERRED} and $deferred);
1024 # Add NDR_SCALARS if this one is not deferred and
1025 # primitives may be pushed
1026 $scalars = 1 if (!$l->{IS_DEFERRED} and $primitives);
1028 # Add NDR_BUFFERS if this one contains deferred stuff
1029 # and deferreds may be pushed
1030 $buffers = 1 if ($l->{CONTAINS_DEFERRED} and $deferred);
1032 return "NDR_SCALARS|NDR_BUFFERS" if ($scalars and $buffers);
1033 return "NDR_SCALARS" if ($scalars);
1034 return "NDR_BUFFERS" if ($buffers);
1038 sub ParseMemCtxPullFlags($$$$)
1040 my ($self, $e, $l) = @_;
1042 return undef unless ($l->{TYPE} eq "POINTER" or $l->{TYPE} eq "ARRAY");
1043 return undef if (($l->{TYPE} eq "POINTER") and ($l->{POINTER_TYPE} eq "ignore"));
1045 return undef unless ($l->{TYPE} ne "ARRAY" or ArrayDynamicallyAllocated($e,$l));
1046 return undef if has_fast_array($e, $l);
1047 return undef if is_charset_array($e, $l);
1049 my $mem_flags = "0";
1051 if (($l->{TYPE} eq "POINTER") and ($l->{POINTER_TYPE} eq "ref")) {
1052 my $nl = GetNextLevel($e, $l);
1053 return undef if ($nl->{TYPE} eq "PIPE");
1054 return undef if ($nl->{TYPE} eq "ARRAY");
1055 return undef if (($nl->{TYPE} eq "DATA") and ($nl->{DATA_TYPE} eq "string"));
1057 if ($l->{LEVEL} eq "TOP") {
1058 $mem_flags = "LIBNDR_FLAG_REF_ALLOC";
1065 sub ParseMemCtxPullStart($$$$$)
1067 my ($self, $e, $l, $ndr, $ptr_name) = @_;
1069 my $mem_r_ctx = "_mem_save_$e->{NAME}_$l->{LEVEL_INDEX}";
1070 my $mem_c_ctx = $ptr_name;
1071 my $mem_c_flags = $self->ParseMemCtxPullFlags($e, $l);
1073 return unless defined($mem_c_flags);
1075 $self->pidl("$mem_r_ctx = NDR_PULL_GET_MEM_CTX($ndr);");
1076 $self->pidl("NDR_PULL_SET_MEM_CTX($ndr, $mem_c_ctx, $mem_c_flags);");
1079 sub ParseMemCtxPullEnd($$$$)
1081 my ($self, $e, $l, $ndr) = @_;
1083 my $mem_r_ctx = "_mem_save_$e->{NAME}_$l->{LEVEL_INDEX}";
1084 my $mem_r_flags = $self->ParseMemCtxPullFlags($e, $l);
1086 return unless defined($mem_r_flags);
1088 $self->pidl("NDR_PULL_SET_MEM_CTX($ndr, $mem_r_ctx, $mem_r_flags);");
1091 sub CheckStringTerminator($$$$$)
1093 my ($self,$ndr,$e,$l,$length) = @_;
1094 my $nl = GetNextLevel($e, $l);
1096 # Make sure last element is zero!
1097 $self->pidl("NDR_CHECK(ndr_check_string_terminator($ndr, $length, sizeof($nl->{DATA_TYPE}_t)));");
1100 sub ParseElementPullLevel
1102 my($self,$e,$l,$ndr,$var_name,$env,$primitives,$deferred) = @_;
1104 my $ndr_flags = CalcNdrFlags($l, $primitives, $deferred);
1105 my $array_length = undef;
1107 if ($l->{TYPE} eq "ARRAY" and ($l->{IS_VARYING} or $l->{IS_CONFORMANT})) {
1108 $var_name = get_pointer_to($var_name);
1111 # Only pull something if there's actually something to be pulled
1112 if (defined($ndr_flags)) {
1113 if ($l->{TYPE} eq "SUBCONTEXT") {
1114 my $subndr = $self->ParseSubcontextPullStart($e, $l, $ndr, $env);
1115 $self->ParseElementPullLevel($e, GetNextLevel($e,$l), $subndr, $var_name, $env, 1, 1);
1116 $self->ParseSubcontextPullEnd($e, $l, $ndr, $env);
1117 } elsif ($l->{TYPE} eq "ARRAY") {
1118 my $length = $self->ParseArrayPullHeader($e, $l, $ndr, $var_name, $env);
1119 $array_length = $length;
1121 my $nl = GetNextLevel($e, $l);
1123 if (is_charset_array($e,$l)) {
1124 if ($l->{IS_ZERO_TERMINATED}) {
1125 $self->CheckStringTerminator($ndr, $e, $l, $length);
1127 if ($l->{IS_TO_NULL}) {
1128 $self->pidl("NDR_CHECK(ndr_pull_charset_to_null($ndr, $ndr_flags, ".get_pointer_to($var_name).", $length, sizeof(" . mapTypeName($nl->{DATA_TYPE}) . "), CH_$e->{PROPERTIES}->{charset}));");
1130 $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}));");
1133 } elsif (has_fast_array($e, $l)) {
1134 if ($l->{IS_ZERO_TERMINATED}) {
1135 $self->CheckStringTerminator($ndr,$e,$l,$length);
1137 $self->pidl("NDR_CHECK(ndr_pull_array_$nl->{DATA_TYPE}($ndr, $ndr_flags, $var_name, $length));");
1140 } elsif ($l->{TYPE} eq "POINTER") {
1141 $self->ParsePtrPull($e, $l, $ndr, $var_name);
1142 } elsif ($l->{TYPE} eq "SWITCH") {
1143 $self->ParseSwitchPull($e, $l, $ndr, $var_name, $env);
1144 } elsif ($l->{TYPE} eq "DATA") {
1145 $self->ParseDataPull($e, $l, $ndr, $var_name, $primitives, $deferred);
1146 } elsif ($l->{TYPE} eq "TYPEDEF") {
1147 $typefamily{$e->{DATA}->{TYPE}}->{PULL_FN_BODY}->($self, $e->{DATA}, $ndr, $var_name);
1151 # add additional constructions
1152 if ($l->{TYPE} eq "POINTER" and $l->{POINTER_TYPE} eq "ignore") {
1153 $self->pidl("/* [ignore] '$e->{NAME}' */");
1154 } elsif ($l->{TYPE} eq "POINTER" and $deferred) {
1155 if ($l->{POINTER_TYPE} ne "ref") {
1156 $self->pidl("if ($var_name) {");
1159 if ($l->{POINTER_TYPE} eq "relative" or $l->{POINTER_TYPE} eq "relative_short") {
1160 $self->pidl("uint32_t _relative_save_offset;");
1161 $self->pidl("_relative_save_offset = $ndr->offset;");
1162 $self->pidl("NDR_CHECK(ndr_pull_relative_ptr2($ndr, $var_name));");
1166 $self->ParseMemCtxPullStart($e, $l, $ndr, $var_name);
1168 $var_name = get_value_of($var_name);
1169 $self->ParseElementPullLevel($e, GetNextLevel($e,$l), $ndr, $var_name, $env, 1, 1);
1171 $self->ParseMemCtxPullEnd($e, $l, $ndr);
1173 if ($l->{POINTER_TYPE} ne "ref") {
1174 if ($l->{POINTER_TYPE} eq "relative" or $l->{POINTER_TYPE} eq "relative_short") {
1175 $self->pidl("if ($ndr->offset > $ndr->relative_highest_offset) {");
1177 $self->pidl("$ndr->relative_highest_offset = $ndr->offset;");
1180 $self->pidl("$ndr->offset = _relative_save_offset;");
1185 } elsif ($l->{TYPE} eq "ARRAY" and
1186 not has_fast_array($e,$l) and not is_charset_array($e, $l)) {
1187 my $length = $array_length;
1188 my $counter = "cntr_$e->{NAME}_$l->{LEVEL_INDEX}";
1189 my $array_name = $var_name;
1191 if (not defined($length)) {
1192 $length = $self->ParseArrayPullGetLength($e, $l, $ndr, $var_name, $env);
1195 $var_name = get_array_element($var_name, $counter);
1197 $self->ParseMemCtxPullStart($e, $l, $ndr, $array_name);
1199 if (($primitives and not $l->{IS_DEFERRED}) or ($deferred and $l->{IS_DEFERRED})) {
1200 my $nl = GetNextLevel($e,$l);
1202 if ($l->{IS_ZERO_TERMINATED}) {
1203 $self->CheckStringTerminator($ndr,$e,$l,$length);
1206 $self->pidl("for ($counter = 0; $counter < ($length); $counter++) {");
1208 $self->ParseElementPullLevel($e, $nl, $ndr, $var_name, $env, 1, 0);
1213 if ($deferred and ContainsDeferred($e, $l)) {
1214 $self->pidl("for ($counter = 0; $counter < ($length); $counter++) {");
1216 $self->ParseElementPullLevel($e,GetNextLevel($e,$l), $ndr, $var_name, $env, 0, 1);
1221 $self->ParseMemCtxPullEnd($e, $l, $ndr);
1223 } elsif ($l->{TYPE} eq "SWITCH") {
1224 $self->ParseElementPullLevel($e, GetNextLevel($e,$l), $ndr, $var_name, $env, $primitives, $deferred);
1228 #####################################################################
1229 # parse scalars in a structure element - pull size
1230 sub ParseElementPull($$$$$$)
1232 my($self,$e,$ndr,$env,$primitives,$deferred) = @_;
1234 my $var_name = $env->{$e->{NAME}};
1238 return if ContainsPipe($e, $e->{LEVELS}[0]);
1240 return unless $primitives or ($deferred and ContainsDeferred($e, $e->{LEVELS}[0]));
1242 if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
1245 $represent_name = $var_name;
1246 $transmit_name = "_transmit_$e->{NAME}";
1247 $var_name = $transmit_name;
1248 $self->pidl(mapTypeName($e->{TYPE})." $var_name;");
1251 $var_name = append_prefix($e, $var_name);
1253 $self->start_flags($e, $ndr);
1255 $self->ParseElementPullLevel($e,$e->{LEVELS}[0],$ndr,$var_name,$env,$primitives,$deferred);
1257 $self->end_flags($e, $ndr);
1259 # Representation type is different from transmit_as
1260 if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
1261 $self->pidl("NDR_CHECK(ndr_$e->{TYPE}_to_$e->{REPRESENTATION_TYPE}($transmit_name, ".get_pointer_to($represent_name)."));");
1267 #####################################################################
1268 # parse a pointer in a struct element or function
1269 sub ParsePtrPull($$$$$)
1271 my($self, $e,$l,$ndr,$var_name) = @_;
1273 my $nl = GetNextLevel($e, $l);
1274 my $next_is_array = ($nl->{TYPE} eq "ARRAY");
1275 my $next_is_string = (($nl->{TYPE} eq "DATA") and
1276 ($nl->{DATA_TYPE} eq "string"));
1278 if ($l->{POINTER_TYPE} eq "ref" and $l->{LEVEL} eq "TOP") {
1280 if (!$next_is_array and !$next_is_string) {
1281 $self->pidl("if ($ndr->flags & LIBNDR_FLAG_REF_ALLOC) {");
1282 $self->pidl("\tNDR_PULL_ALLOC($ndr, $var_name);");
1287 } elsif ($l->{POINTER_TYPE} eq "ref" and $l->{LEVEL} eq "EMBEDDED") {
1288 $self->pidl("NDR_CHECK(ndr_pull_ref_ptr($ndr, &_ptr_$e->{NAME}));");
1289 } elsif (($l->{POINTER_TYPE} eq "unique") or
1290 ($l->{POINTER_TYPE} eq "relative") or
1291 ($l->{POINTER_TYPE} eq "full")) {
1292 $self->pidl("NDR_CHECK(ndr_pull_generic_ptr($ndr, &_ptr_$e->{NAME}));");
1293 } elsif ($l->{POINTER_TYPE} eq "relative_short") {
1294 $self->pidl("NDR_CHECK(ndr_pull_relative_ptr_short($ndr, &_ptr_$e->{NAME}));");
1295 } elsif ($l->{POINTER_TYPE} eq "ignore") {
1296 #We want to consume the pointer bytes, but ignore the pointer value
1297 $self->pidl("NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &_ptr_$e->{NAME}));");
1298 $self->pidl("_ptr_$e->{NAME} = 0;");
1300 die("Unhandled pointer type $l->{POINTER_TYPE}");
1303 $self->pidl("if (_ptr_$e->{NAME}) {");
1306 if ($l->{POINTER_TYPE} eq "ignore") {
1307 # Don't do anything, we don't want to do the
1308 # allocation, as we forced it to NULL just above, and
1309 # we may not know the declared type anyway.
1311 # Don't do this for arrays, they're allocated at the actual level
1313 unless ($next_is_array or $next_is_string) {
1314 $self->pidl("NDR_PULL_ALLOC($ndr, $var_name);");
1316 # FIXME: Yes, this is nasty.
1317 # We allocate an array twice
1318 # - once just to indicate that it's there,
1319 # - then the real allocation...
1320 $self->pidl("NDR_PULL_ALLOC($ndr, $var_name);");
1324 #$self->pidl("memset($var_name, 0, sizeof($var_name));");
1325 if ($l->{POINTER_TYPE} eq "relative" or $l->{POINTER_TYPE} eq "relative_short") {
1326 $self->pidl("NDR_CHECK(ndr_pull_relative_ptr1($ndr, $var_name, _ptr_$e->{NAME}));");
1329 $self->pidl("} else {");
1330 $self->pidl("\t$var_name = NULL;");
1334 sub ParseStructPushPrimitives($$$$$)
1336 my ($self, $struct, $ndr, $varname, $env) = @_;
1338 # see if the structure contains a conformant array. If it
1339 # does, then it must be the last element of the structure, and
1340 # we need to push the conformant length early, as it fits on
1341 # the wire before the structure (and even before the structure
1343 if (defined($struct->{SURROUNDING_ELEMENT})) {
1344 my $e = $struct->{SURROUNDING_ELEMENT};
1346 if (defined($e->{LEVELS}[0]) and
1347 $e->{LEVELS}[0]->{TYPE} eq "ARRAY") {
1350 if ($e->{LEVELS}[0]->{IS_ZERO_TERMINATED}) {
1351 if (has_property($e, "charset")) {
1352 $size = "ndr_charset_length($varname->$e->{NAME}, CH_$e->{PROPERTIES}->{charset})";
1354 $size = "ndr_string_length($varname->$e->{NAME}, sizeof(*$varname->$e->{NAME}))";
1356 if (defined($e->{LEVELS}[0]->{SIZE_IS})) {
1357 $size = ParseExpr($e->{LEVELS}[0]->{SIZE_IS}, $env, $e->{ORIGINAL});
1360 $size = ParseExpr($e->{LEVELS}[0]->{SIZE_IS}, $env, $e->{ORIGINAL});
1363 $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, $size));");
1365 $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, ndr_string_array_size($ndr, $varname->$e->{NAME})));");
1369 $self->pidl("NDR_CHECK(ndr_push_align($ndr, $struct->{ALIGN}));");
1371 if (defined($struct->{PROPERTIES}{relative_base})) {
1372 # set the current offset as base for relative pointers
1373 # and store it based on the toplevel struct/union
1374 $self->pidl("NDR_CHECK(ndr_push_setup_relative_base_offset1($ndr, $varname, $ndr->offset));");
1377 $self->ParseElementPush($_, $ndr, $env, 1, 0) foreach (@{$struct->{ELEMENTS}});
1379 $self->pidl("NDR_CHECK(ndr_push_trailer_align($ndr, $struct->{ALIGN}));");
1382 sub ParseStructPushDeferred($$$$)
1384 my ($self, $struct, $ndr, $varname, $env) = @_;
1385 if (defined($struct->{PROPERTIES}{relative_base})) {
1386 # retrieve the current offset as base for relative pointers
1387 # based on the toplevel struct/union
1388 $self->pidl("NDR_CHECK(ndr_push_setup_relative_base_offset2($ndr, $varname));");
1390 $self->ParseElementPush($_, $ndr, $env, 0, 1) foreach (@{$struct->{ELEMENTS}});
1393 #####################################################################
1395 sub ParseStructPush($$$$)
1397 my ($self, $struct, $ndr, $varname) = @_;
1399 return unless defined($struct->{ELEMENTS});
1401 my $env = GenerateStructEnv($struct, $varname);
1403 EnvSubstituteValue($env, $struct);
1405 $self->DeclareArrayVariablesNoZero($_, $env) foreach (@{$struct->{ELEMENTS}});
1407 $self->start_flags($struct, $ndr);
1409 $self->pidl("NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);");
1410 $self->pidl("if (ndr_flags & NDR_SCALARS) {");
1412 $self->ParseStructPushPrimitives($struct, $ndr, $varname, $env);
1416 $self->pidl("if (ndr_flags & NDR_BUFFERS) {");
1418 $self->ParseStructPushDeferred($struct, $ndr, $varname, $env);
1422 $self->end_flags($struct, $ndr);
1425 #####################################################################
1426 # generate a push function for an enum
1427 sub ParseEnumPush($$$$)
1429 my($self,$enum,$ndr,$varname) = @_;
1430 my($type_fn) = $enum->{BASE_TYPE};
1432 $self->start_flags($enum, $ndr);
1433 $self->pidl("NDR_CHECK(ndr_push_enum_$type_fn($ndr, NDR_SCALARS, $varname));");
1434 $self->end_flags($enum, $ndr);
1437 #####################################################################
1438 # generate a pull function for an enum
1439 sub ParseEnumPull($$$$)
1441 my($self,$enum,$ndr,$varname) = @_;
1442 my($type_fn) = $enum->{BASE_TYPE};
1443 my($type_v_decl) = mapTypeName($type_fn);
1445 $self->pidl("$type_v_decl v;");
1446 $self->start_flags($enum, $ndr);
1447 $self->pidl("NDR_CHECK(ndr_pull_enum_$type_fn($ndr, NDR_SCALARS, &v));");
1448 $self->pidl("*$varname = v;");
1450 $self->end_flags($enum, $ndr);
1453 #####################################################################
1454 # generate a print function for an enum
1455 sub ParseEnumPrint($$$$$)
1457 my($self,$enum,$ndr,$name,$varname) = @_;
1459 $self->pidl("const char *val = NULL;");
1462 $self->start_flags($enum, $ndr);
1464 $self->pidl("switch ($varname) {");
1466 my $els = \@{$enum->{ELEMENTS}};
1467 foreach my $i (0 .. $#{$els}) {
1468 my $e = ${$els}[$i];
1470 if ($e =~ /^(.*)=/) {
1473 $self->pidl("case $e: val = \"$e\"; break;");
1479 $self->pidl("ndr_print_enum($ndr, name, \"$enum->{TYPE}\", val, $varname);");
1481 $self->end_flags($enum, $ndr);
1486 my ($e,$t,$name,$varname) = @_;
1487 return "enum $name " .
1488 ($t eq "pull"?"*":"") . $varname;
1491 $typefamily{ENUM} = {
1493 PUSH_FN_BODY => \&ParseEnumPush,
1494 PULL_FN_BODY => \&ParseEnumPull,
1495 PRINT_FN_BODY => \&ParseEnumPrint,
1498 #####################################################################
1499 # generate a push function for a bitmap
1500 sub ParseBitmapPush($$$$)
1502 my($self,$bitmap,$ndr,$varname) = @_;
1503 my($type_fn) = $bitmap->{BASE_TYPE};
1505 $self->start_flags($bitmap, $ndr);
1507 $self->pidl("NDR_CHECK(ndr_push_$type_fn($ndr, NDR_SCALARS, $varname));");
1509 $self->end_flags($bitmap, $ndr);
1512 #####################################################################
1513 # generate a pull function for an bitmap
1514 sub ParseBitmapPull($$$$)
1516 my($self,$bitmap,$ndr,$varname) = @_;
1517 my $type_fn = $bitmap->{BASE_TYPE};
1518 my($type_decl) = mapTypeName($bitmap->{BASE_TYPE});
1520 $self->pidl("$type_decl v;");
1521 $self->start_flags($bitmap, $ndr);
1522 $self->pidl("NDR_CHECK(ndr_pull_$type_fn($ndr, NDR_SCALARS, &v));");
1523 $self->pidl("*$varname = v;");
1525 $self->end_flags($bitmap, $ndr);
1528 #####################################################################
1529 # generate a print function for an bitmap
1530 sub ParseBitmapPrintElement($$$$$$)
1532 my($self,$e,$bitmap,$ndr,$name,$varname) = @_;
1533 my($type_decl) = mapTypeName($bitmap->{BASE_TYPE});
1534 my($type_fn) = $bitmap->{BASE_TYPE};
1537 if ($e =~ /^(\w+) .*$/) {
1540 die "Bitmap: \"$name\" invalid Flag: \"$e\"";
1543 $self->pidl("ndr_print_bitmap_flag($ndr, sizeof($type_decl), \"$flag\", $flag, $varname);");
1546 #####################################################################
1547 # generate a print function for an bitmap
1548 sub ParseBitmapPrint($$$$$)
1550 my($self,$bitmap,$ndr,$name,$varname) = @_;
1551 my($type_decl) = mapTypeName($bitmap->{TYPE});
1552 my($type_fn) = $bitmap->{BASE_TYPE};
1554 $self->start_flags($bitmap, $ndr);
1556 $self->pidl("ndr_print_$type_fn($ndr, name, $varname);");
1558 $self->pidl("$ndr->depth++;");
1559 foreach my $e (@{$bitmap->{ELEMENTS}}) {
1560 $self->ParseBitmapPrintElement($e, $bitmap, $ndr, $name, $varname);
1562 $self->pidl("$ndr->depth--;");
1564 $self->end_flags($bitmap, $ndr);
1567 sub DeclBitmap($$$$)
1569 my ($e,$t,$name,$varname) = @_;
1570 return mapTypeName(Parse::Pidl::Typelist::bitmap_type_fn($e)) .
1571 ($t eq "pull"?" *":" ") . $varname;
1574 $typefamily{BITMAP} = {
1575 DECL => \&DeclBitmap,
1576 PUSH_FN_BODY => \&ParseBitmapPush,
1577 PULL_FN_BODY => \&ParseBitmapPull,
1578 PRINT_FN_BODY => \&ParseBitmapPrint,
1581 #####################################################################
1582 # generate a struct print function
1583 sub ParseStructPrint($$$$$)
1585 my($self,$struct,$ndr,$name,$varname) = @_;
1587 return unless defined $struct->{ELEMENTS};
1589 my $env = GenerateStructEnv($struct, $varname);
1591 $self->DeclareArrayVariables($_) foreach (@{$struct->{ELEMENTS}});
1593 $self->pidl("ndr_print_struct($ndr, name, \"$name\");");
1594 $self->pidl("if (r == NULL) { ndr_print_null($ndr); return; }");
1596 $self->start_flags($struct, $ndr);
1598 $self->pidl("$ndr->depth++;");
1600 $self->ParseElementPrint($_, $ndr, $env->{$_->{NAME}}, $env)
1601 foreach (@{$struct->{ELEMENTS}});
1602 $self->pidl("$ndr->depth--;");
1604 $self->end_flags($struct, $ndr);
1607 sub DeclarePtrVariables($$)
1610 foreach my $l (@{$e->{LEVELS}}) {
1612 if ($l->{TYPE} eq "POINTER" and
1613 not ($l->{POINTER_TYPE} eq "ref" and $l->{LEVEL} eq "TOP")) {
1614 if ($l->{POINTER_TYPE} eq "relative_short") {
1617 $self->pidl("uint${size}_t _ptr_$e->{NAME};");
1623 sub DeclareArrayVariables($$;$)
1625 my ($self,$e,$pull) = @_;
1627 foreach my $l (@{$e->{LEVELS}}) {
1628 next if ($l->{TYPE} ne "ARRAY");
1629 if (defined($pull)) {
1630 $self->pidl("uint32_t size_$e->{NAME}_$l->{LEVEL_INDEX} = 0;");
1631 if ($l->{IS_VARYING}) {
1632 $self->pidl("uint32_t length_$e->{NAME}_$l->{LEVEL_INDEX} = 0;");
1635 next if has_fast_array($e,$l);
1636 next if is_charset_array($e,$l);
1637 $self->pidl("uint32_t cntr_$e->{NAME}_$l->{LEVEL_INDEX};");
1641 sub DeclareArrayVariablesNoZero($$$)
1643 my ($self,$e,$env) = @_;
1645 foreach my $l (@{$e->{LEVELS}}) {
1646 next if ($l->{TYPE} ne "ARRAY");
1647 next if has_fast_array($e,$l);
1648 next if is_charset_array($e,$l);
1649 my $length = ParseExpr($l->{LENGTH_IS}, $env, $e->{ORIGINAL});
1650 if ($length eq "0") {
1651 warning($e->{ORIGINAL}, "pointless array cntr: 'cntr_$e->{NAME}_$l->{LEVEL_INDEX}': length=$length");
1653 $self->pidl("uint32_t cntr_$e->{NAME}_$l->{LEVEL_INDEX};");
1658 sub DeclareMemCtxVariables($$)
1661 foreach my $l (@{$e->{LEVELS}}) {
1662 my $mem_flags = $self->ParseMemCtxPullFlags($e, $l);
1664 if (($l->{TYPE} eq "POINTER") and ($l->{POINTER_TYPE} eq "ignore")) {
1668 if (defined($mem_flags)) {
1669 $self->pidl("TALLOC_CTX *_mem_save_$e->{NAME}_$l->{LEVEL_INDEX};");
1674 sub ParseStructPullPrimitives($$$$$)
1676 my($self,$struct,$ndr,$varname,$env) = @_;
1678 if (defined $struct->{SURROUNDING_ELEMENT}) {
1679 $self->pidl("NDR_CHECK(ndr_pull_array_size($ndr, &$varname->$struct->{SURROUNDING_ELEMENT}->{NAME}));");
1682 $self->pidl("NDR_CHECK(ndr_pull_align($ndr, $struct->{ALIGN}));");
1684 if (defined($struct->{PROPERTIES}{relative_base})) {
1685 # set the current offset as base for relative pointers
1686 # and store it based on the toplevel struct/union
1687 $self->pidl("NDR_CHECK(ndr_pull_setup_relative_base_offset1($ndr, $varname, $ndr->offset));");
1690 $self->ParseElementPull($_, $ndr, $env, 1, 0) foreach (@{$struct->{ELEMENTS}});
1692 $self->add_deferred();
1694 $self->pidl("NDR_CHECK(ndr_pull_trailer_align($ndr, $struct->{ALIGN}));");
1697 sub ParseStructPullDeferred($$$$$)
1699 my ($self,$struct,$ndr,$varname,$env) = @_;
1701 if (defined($struct->{PROPERTIES}{relative_base})) {
1702 # retrieve the current offset as base for relative pointers
1703 # based on the toplevel struct/union
1704 $self->pidl("NDR_CHECK(ndr_pull_setup_relative_base_offset2($ndr, $varname));");
1706 foreach my $e (@{$struct->{ELEMENTS}}) {
1707 $self->ParseElementPull($e, $ndr, $env, 0, 1);
1710 $self->add_deferred();
1713 #####################################################################
1714 # parse a struct - pull side
1715 sub ParseStructPull($$$$)
1717 my($self,$struct,$ndr,$varname) = @_;
1719 return unless defined $struct->{ELEMENTS};
1721 # declare any internal pointers we need
1722 foreach my $e (@{$struct->{ELEMENTS}}) {
1723 $self->DeclarePtrVariables($e);
1724 $self->DeclareArrayVariables($e, "pull");
1725 $self->DeclareMemCtxVariables($e);
1728 $self->start_flags($struct, $ndr);
1730 my $env = GenerateStructEnv($struct, $varname);
1732 $self->pidl("NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);");
1733 $self->pidl("if (ndr_flags & NDR_SCALARS) {");
1735 $self->ParseStructPullPrimitives($struct,$ndr,$varname,$env);
1738 $self->pidl("if (ndr_flags & NDR_BUFFERS) {");
1740 $self->ParseStructPullDeferred($struct,$ndr,$varname,$env);
1744 $self->end_flags($struct, $ndr);
1747 #####################################################################
1748 # calculate size of ndr struct
1749 sub ParseStructNdrSize($$$$)
1751 my ($self,$t, $name, $varname) = @_;
1754 if (my $flags = has_property($t, "flag")) {
1755 $self->pidl("flags |= $flags;");
1757 $self->pidl("return ndr_size_struct($varname, flags, (ndr_push_flags_fn_t)ndr_push_$name);");
1760 sub DeclStruct($$$$)
1762 my ($e,$t,$name,$varname) = @_;
1763 return ($t ne "pull"?"const ":"") . "struct $name *$varname";
1766 sub ArgsStructNdrSize($$$)
1768 my ($d, $name, $varname) = @_;
1769 return "const struct $name *$varname, int flags";
1772 $typefamily{STRUCT} = {
1773 PUSH_FN_BODY => \&ParseStructPush,
1774 DECL => \&DeclStruct,
1775 PULL_FN_BODY => \&ParseStructPull,
1776 PRINT_FN_BODY => \&ParseStructPrint,
1777 SIZE_FN_BODY => \&ParseStructNdrSize,
1778 SIZE_FN_ARGS => \&ArgsStructNdrSize,
1781 #####################################################################
1782 # calculate size of ndr struct
1783 sub ParseUnionNdrSize($$$)
1785 my ($self, $t, $name, $varname) = @_;
1788 if (my $flags = has_property($t, "flag")) {
1789 $self->pidl("flags |= $flags;");
1792 $self->pidl("return ndr_size_union($varname, flags, level, (ndr_push_flags_fn_t)ndr_push_$name);");
1795 sub ParseUnionPushPrimitives($$$$)
1797 my ($self, $e, $ndr ,$varname) = @_;
1799 my $have_default = 0;
1801 $self->pidl("uint32_t level = ndr_push_get_switch_value($ndr, $varname);");
1803 if (defined($e->{SWITCH_TYPE})) {
1804 if (defined($e->{ALIGN})) {
1805 $self->pidl("NDR_CHECK(ndr_push_union_align($ndr, $e->{ALIGN}));");
1808 $self->pidl("NDR_CHECK(ndr_push_$e->{SWITCH_TYPE}($ndr, NDR_SCALARS, level));");
1811 if (defined($e->{ALIGN})) {
1812 if ($e->{IS_MS_UNION}) {
1813 $self->pidl("/* ms_union is always aligned to the largest union arm*/");
1814 $self->pidl("NDR_CHECK(ndr_push_align($ndr, $e->{ALIGN}));");
1816 $self->pidl("NDR_CHECK(ndr_push_union_align($ndr, $e->{ALIGN}));");
1820 $self->pidl("switch (level) {");
1822 foreach my $el (@{$e->{ELEMENTS}}) {
1823 if ($el->{CASE} eq "default") {
1826 $self->pidl("$el->{CASE}: {");
1828 if ($el->{TYPE} ne "EMPTY") {
1830 if (defined($e->{PROPERTIES}{relative_base})) {
1831 $self->pidl("NDR_CHECK(ndr_push_align($ndr, $el->{ALIGN}));");
1832 # set the current offset as base for relative pointers
1833 # and store it based on the toplevel struct/union
1834 $self->pidl("NDR_CHECK(ndr_push_setup_relative_base_offset1($ndr, $varname, $ndr->offset));");
1836 $self->DeclareArrayVariables($el);
1837 $self->ParseElementPush($el, $ndr, {$el->{NAME} => "$varname->$el->{NAME}"}, 1, 0);
1840 $self->pidl("break; }");
1843 if (! $have_default) {
1844 $self->pidl("default:");
1845 $self->pidl("\treturn ndr_push_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u at \%s\", level, __location__);");
1851 sub ParseUnionPushDeferred($$$$)
1853 my ($self,$e,$ndr,$varname) = @_;
1855 my $have_default = 0;
1857 $self->pidl("uint32_t level = ndr_push_get_switch_value($ndr, $varname);");
1858 if (defined($e->{PROPERTIES}{relative_base})) {
1859 # retrieve the current offset as base for relative pointers
1860 # based on the toplevel struct/union
1861 $self->pidl("NDR_CHECK(ndr_push_setup_relative_base_offset2($ndr, $varname));");
1863 $self->pidl("switch (level) {");
1865 foreach my $el (@{$e->{ELEMENTS}}) {
1866 if ($el->{CASE} eq "default") {
1870 $self->pidl("$el->{CASE}:");
1871 if ($el->{TYPE} ne "EMPTY") {
1873 $self->ParseElementPush($el, $ndr, {$el->{NAME} => "$varname->$el->{NAME}"}, 0, 1);
1876 $self->pidl("break;");
1879 if (! $have_default) {
1880 $self->pidl("default:");
1881 $self->pidl("\treturn ndr_push_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u at \%s\", level, __location__);");
1887 #####################################################################
1888 # parse a union - push side
1889 sub ParseUnionPush($$$$)
1891 my ($self,$e,$ndr,$varname) = @_;
1892 my $have_default = 0;
1894 $self->start_flags($e, $ndr);
1896 $self->pidl("NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);");
1897 $self->pidl("if (ndr_flags & NDR_SCALARS) {");
1899 $self->ParseUnionPushPrimitives($e, $ndr, $varname);
1902 $self->pidl("if (ndr_flags & NDR_BUFFERS) {");
1904 $self->ParseUnionPushDeferred($e, $ndr, $varname);
1907 $self->end_flags($e, $ndr);
1910 #####################################################################
1912 sub ParseUnionPrint($$$$$)
1914 my ($self,$e,$ndr,$name,$varname) = @_;
1915 my $have_default = 0;
1917 $self->pidl("uint32_t level;");
1918 foreach my $el (@{$e->{ELEMENTS}}) {
1919 $self->DeclareArrayVariables($el);
1922 $self->start_flags($e, $ndr);
1924 $self->pidl("level = ndr_print_get_switch_value($ndr, $varname);");
1926 $self->pidl("ndr_print_union($ndr, name, level, \"$name\");");
1928 $self->pidl("switch (level) {");
1930 foreach my $el (@{$e->{ELEMENTS}}) {
1931 if ($el->{CASE} eq "default") {
1934 $self->pidl("$el->{CASE}:");
1935 if ($el->{TYPE} ne "EMPTY") {
1937 $self->ParseElementPrint($el, $ndr, "$varname->$el->{NAME}", {});
1940 $self->pidl("break;");
1943 if (! $have_default) {
1944 $self->pidl("default:");
1945 $self->pidl("\tndr_print_bad_level($ndr, name, level);");
1950 $self->end_flags($e, $ndr);
1953 sub ParseUnionPullPrimitives($$$$$)
1955 my ($self,$e,$ndr,$varname,$switch_type) = @_;
1956 my $have_default = 0;
1959 if (defined($switch_type)) {
1960 if (defined($e->{ALIGN})) {
1961 $self->pidl("NDR_CHECK(ndr_pull_union_align($ndr, $e->{ALIGN}));");
1964 $self->pidl("NDR_CHECK(ndr_pull_$switch_type($ndr, NDR_SCALARS, &_level));");
1965 $self->pidl("if (_level != level) {");
1966 $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value %u for $varname at \%s\", _level, __location__);");
1970 if (defined($e->{ALIGN})) {
1971 if ($e->{IS_MS_UNION}) {
1972 $self->pidl("/* ms_union is always aligned to the largest union arm*/");
1973 $self->pidl("NDR_CHECK(ndr_pull_align($ndr, $e->{ALIGN}));");
1975 $self->pidl("NDR_CHECK(ndr_pull_union_align($ndr, $e->{ALIGN}));");
1979 $self->pidl("switch (level) {");
1981 foreach my $el (@{$e->{ELEMENTS}}) {
1982 if ($el->{CASE} eq "default") {
1985 $self->pidl("$el->{CASE}: {");
1987 if ($el->{TYPE} ne "EMPTY") {
1989 if (defined($e->{PROPERTIES}{relative_base})) {
1990 $self->pidl("NDR_CHECK(ndr_pull_align($ndr, $el->{ALIGN}));");
1991 # set the current offset as base for relative pointers
1992 # and store it based on the toplevel struct/union
1993 $self->pidl("NDR_CHECK(ndr_pull_setup_relative_base_offset1($ndr, $varname, $ndr->offset));");
1995 $self->ParseElementPull($el, $ndr, {$el->{NAME} => "$varname->$el->{NAME}"}, 1, 0);
1998 $self->pidl("break; }");
2001 if (! $have_default) {
2002 $self->pidl("default:");
2003 $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u at \%s\", level, __location__);");
2009 sub ParseUnionPullDeferred($$$$)
2011 my ($self,$e,$ndr,$varname) = @_;
2012 my $have_default = 0;
2014 if (defined($e->{PROPERTIES}{relative_base})) {
2015 # retrieve the current offset as base for relative pointers
2016 # based on the toplevel struct/union
2017 $self->pidl("NDR_CHECK(ndr_pull_setup_relative_base_offset2($ndr, $varname));");
2019 $self->pidl("switch (level) {");
2021 foreach my $el (@{$e->{ELEMENTS}}) {
2022 if ($el->{CASE} eq "default") {
2026 $self->pidl("$el->{CASE}:");
2027 if ($el->{TYPE} ne "EMPTY") {
2029 $self->ParseElementPull($el, $ndr, {$el->{NAME} => "$varname->$el->{NAME}"}, 0, 1);
2032 $self->pidl("break;");
2035 if (! $have_default) {
2036 $self->pidl("default:");
2037 $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u at \%s\", level, __location__);");
2045 #####################################################################
2046 # parse a union - pull side
2047 sub ParseUnionPull($$$$)
2049 my ($self,$e,$ndr,$varname) = @_;
2050 my $switch_type = $e->{SWITCH_TYPE};
2052 $self->pidl("uint32_t level;");
2053 if (defined($switch_type)) {
2054 if (Parse::Pidl::Typelist::typeIs($switch_type, "ENUM")) {
2055 $switch_type = Parse::Pidl::Typelist::enum_type_fn(getType($switch_type)->{DATA});
2057 $self->pidl(mapTypeName($switch_type) . " _level;");
2060 my %double_cases = ();
2061 foreach my $el (@{$e->{ELEMENTS}}) {
2062 next if ($el->{TYPE} eq "EMPTY");
2063 next if ($double_cases{"$el->{NAME}"});
2064 $self->DeclareMemCtxVariables($el);
2065 $self->DeclarePtrVariables($el);
2066 $self->DeclareArrayVariables($el, "pull");
2067 $double_cases{"$el->{NAME}"} = 1;
2070 $self->start_flags($e, $ndr);
2072 $self->pidl("level = ndr_pull_get_switch_value($ndr, $varname);");
2074 $self->pidl("NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);");
2075 $self->pidl("if (ndr_flags & NDR_SCALARS) {");
2077 $self->ParseUnionPullPrimitives($e,$ndr,$varname,$switch_type);
2081 $self->pidl("if (ndr_flags & NDR_BUFFERS) {");
2083 $self->ParseUnionPullDeferred($e,$ndr,$varname);
2087 $self->add_deferred();
2089 $self->end_flags($e, $ndr);
2094 my ($e,$t,$name,$varname) = @_;
2095 return ($t ne "pull"?"const ":"") . "union $name *$varname";
2098 sub ArgsUnionNdrSize($$)
2101 return "const union $name *r, uint32_t level, int flags";
2104 $typefamily{UNION} = {
2105 PUSH_FN_BODY => \&ParseUnionPush,
2106 DECL => \&DeclUnion,
2107 PULL_FN_BODY => \&ParseUnionPull,
2108 PRINT_FN_BODY => \&ParseUnionPrint,
2109 SIZE_FN_ARGS => \&ArgsUnionNdrSize,
2110 SIZE_FN_BODY => \&ParseUnionNdrSize,
2113 #####################################################################
2114 # parse a typedef - push side
2115 sub ParseTypedefPush($$$$)
2117 my($self,$e,$ndr,$varname) = @_;
2121 $env->{$e->{NAME}} = $varname;
2123 $self->ParseElementPushLevel($e, $e->{LEVELS}[0], $ndr, $varname, $env, 1, 1);
2126 #####################################################################
2127 # parse a typedef - pull side
2128 sub ParseTypedefPull($$$$)
2130 my($self,$e,$ndr,$varname) = @_;
2134 $env->{$e->{NAME}} = $varname;
2136 $self->ParseElementPullLevel($e, $e->{LEVELS}[0], $ndr, $varname, $env, 1, 1);
2139 #####################################################################
2140 # parse a typedef - print side
2141 sub ParseTypedefPrint($$$$$)
2143 my($self,$e,$ndr,$name,$varname) = @_;
2145 $typefamily{$e->{DATA}->{TYPE}}->{PRINT_FN_BODY}->($self, $e->{DATA}, $ndr, $name, $varname);
2148 #####################################################################
2149 ## calculate the size of a structure
2150 sub ParseTypedefNdrSize($$$$)
2152 my($self,$t,$name,$varname) = @_;
2154 $typefamily{$t->{DATA}->{TYPE}}->{SIZE_FN_BODY}->($self, $t->{DATA}, $name, $varname);
2157 sub DeclTypedef($$$$)
2159 my ($e, $t, $name, $varname) = @_;
2161 return $typefamily{$e->{DATA}->{TYPE}}->{DECL}->($e->{DATA}, $t, $name, $varname);
2164 sub ArgsTypedefNdrSize($$$)
2166 my ($d, $name, $varname) = @_;
2167 return $typefamily{$d->{DATA}->{TYPE}}->{SIZE_FN_ARGS}->($d->{DATA}, $name, $varname);
2170 $typefamily{TYPEDEF} = {
2171 PUSH_FN_BODY => \&ParseTypedefPush,
2172 DECL => \&DeclTypedef,
2173 PULL_FN_BODY => \&ParseTypedefPull,
2174 PRINT_FN_BODY => \&ParseTypedefPrint,
2175 SIZE_FN_ARGS => \&ArgsTypedefNdrSize,
2176 SIZE_FN_BODY => \&ParseTypedefNdrSize,
2179 sub ParsePipePushChunk($$)
2181 my ($self, $t) = @_;
2184 $pipe = $t->{DATA} if ($t->{TYPE} eq "TYPEDEF");
2185 my $struct = $pipe->{DATA};
2187 my $name = "$struct->{NAME}";
2191 my $args = $typefamily{$struct->{TYPE}}->{DECL}->($struct, "push", $name, $varname);
2193 $self->fn_declare("push", $struct, "enum ndr_err_code ndr_push_$name(struct ndr_push *$ndr, int ndr_flags, $args)") or return;
2195 return if has_property($t, "nopush");
2200 $self->ParseStructPush($struct, $ndr, $varname);
2203 $self->pidl("NDR_CHECK(ndr_push_pipe_chunk_trailer(ndr, ndr_flags, $varname->count));");
2206 $self->pidl("return NDR_ERR_SUCCESS;");
2212 sub ParsePipePullChunk($$)
2214 my ($self, $t) = @_;
2217 $pipe = $t->{DATA} if ($t->{TYPE} eq "TYPEDEF");
2218 my $struct = $pipe->{DATA};
2220 my $name = "$struct->{NAME}";
2224 my $args = $typefamily{$struct->{TYPE}}->{DECL}->($struct, "pull", $name, $varname);
2226 $self->fn_declare("pull", $struct, "enum ndr_err_code ndr_pull_$name(struct ndr_pull *$ndr, int ndr_flags, $args)") or return;
2228 return if has_property($struct, "nopull");
2233 $self->ParseStructPull($struct, $ndr, $varname);
2236 $self->pidl("NDR_CHECK(ndr_check_pipe_chunk_trailer($ndr, ndr_flags, $varname->count));");
2239 $self->pidl("return NDR_ERR_SUCCESS;");
2245 sub ParsePipePrintChunk($$)
2247 my ($self, $t) = @_;
2250 $pipe = $t->{DATA} if ($t->{TYPE} eq "TYPEDEF");
2251 my $struct = $pipe->{DATA};
2253 my $name = "$struct->{NAME}";
2257 my $args = $typefamily{$struct->{TYPE}}->{DECL}->($struct, "print", $name, $varname);
2259 $self->pidl_hdr("void ndr_print_$name(struct ndr_print *ndr, const char *name, $args);");
2261 return if (has_property($t, "noprint"));
2263 $self->pidl("_PUBLIC_ void ndr_print_$name(struct ndr_print *$ndr, const char *name, $args)");
2266 $self->ParseTypePrint($struct, $ndr, $varname);
2272 #####################################################################
2273 # parse a function - print side
2274 sub ParseFunctionPrint($$)
2276 my($self, $fn) = @_;
2279 $self->pidl_hdr("void ndr_print_$fn->{NAME}(struct ndr_print *$ndr, const char *name, int flags, const struct $fn->{NAME} *r);");
2281 return if has_property($fn, "noprint");
2283 $self->pidl("_PUBLIC_ void ndr_print_$fn->{NAME}(struct ndr_print *$ndr, const char *name, int flags, const struct $fn->{NAME} *r)");
2287 foreach my $e (@{$fn->{ELEMENTS}}) {
2288 $self->DeclareArrayVariables($e);
2291 $self->pidl("ndr_print_struct($ndr, name, \"$fn->{NAME}\");");
2292 $self->pidl("if (r == NULL) { ndr_print_null($ndr); return; }");
2293 $self->pidl("$ndr->depth++;");
2295 $self->pidl("if (flags & NDR_SET_VALUES) {");
2296 $self->pidl("\t$ndr->flags |= LIBNDR_PRINT_SET_VALUES;");
2299 $self->pidl("if (flags & NDR_IN) {");
2301 $self->pidl("ndr_print_struct($ndr, \"in\", \"$fn->{NAME}\");");
2302 $self->pidl("$ndr->depth++;");
2304 my $env = GenerateFunctionInEnv($fn);
2306 foreach my $e (@{$fn->{ELEMENTS}}) {
2307 if (grep(/in/,@{$e->{DIRECTION}})) {
2308 $self->ParseElementPrint($e, $ndr, $env->{$e->{NAME}}, $env);
2311 $self->pidl("$ndr->depth--;");
2315 $self->pidl("if (flags & NDR_OUT) {");
2317 $self->pidl("ndr_print_struct($ndr, \"out\", \"$fn->{NAME}\");");
2318 $self->pidl("$ndr->depth++;");
2320 $env = GenerateFunctionOutEnv($fn);
2321 foreach my $e (@{$fn->{ELEMENTS}}) {
2322 if (grep(/out/,@{$e->{DIRECTION}})) {
2323 $self->ParseElementPrint($e, $ndr, $env->{$e->{NAME}}, $env);
2326 if ($fn->{RETURN_TYPE}) {
2327 $self->pidl("ndr_print_$fn->{RETURN_TYPE}($ndr, \"result\", r->out.result);");
2329 $self->pidl("$ndr->depth--;");
2333 $self->pidl("$ndr->depth--;");
2339 #####################################################################
2341 sub ParseFunctionPush($$)
2343 my($self, $fn) = @_;
2346 $self->fn_declare("push", $fn, "enum ndr_err_code ndr_push_$fn->{NAME}(struct ndr_push *$ndr, int flags, const struct $fn->{NAME} *r)") or return;
2348 return if has_property($fn, "nopush");
2353 foreach my $e (@{$fn->{ELEMENTS}}) {
2354 $self->DeclareArrayVariables($e);
2357 $self->pidl("NDR_PUSH_CHECK_FN_FLAGS(ndr, flags);");
2359 $self->pidl("if (flags & NDR_IN) {");
2362 my $env = GenerateFunctionInEnv($fn);
2364 EnvSubstituteValue($env, $fn);
2366 foreach my $e (@{$fn->{ELEMENTS}}) {
2367 if (grep(/in/,@{$e->{DIRECTION}})) {
2368 $self->ParseElementPush($e, $ndr, $env, 1, 1);
2375 $self->pidl("if (flags & NDR_OUT) {");
2378 $env = GenerateFunctionOutEnv($fn);
2379 foreach my $e (@{$fn->{ELEMENTS}}) {
2380 if (grep(/out/,@{$e->{DIRECTION}})) {
2381 $self->ParseElementPush($e, $ndr, $env, 1, 1);
2385 if ($fn->{RETURN_TYPE}) {
2386 $self->pidl("NDR_CHECK(ndr_push_$fn->{RETURN_TYPE}($ndr, NDR_SCALARS, r->out.result));");
2391 $self->pidl("return NDR_ERR_SUCCESS;");
2397 sub AllocateArrayLevel($$$$$$)
2399 my ($self,$e,$l,$ndr,$var,$size) = @_;
2401 my $pl = GetPrevLevel($e, $l);
2402 if (defined($pl) and
2403 $pl->{TYPE} eq "POINTER" and
2404 $pl->{POINTER_TYPE} eq "ref"
2405 and not $l->{IS_ZERO_TERMINATED}) {
2406 $self->pidl("if ($ndr->flags & LIBNDR_FLAG_REF_ALLOC) {");
2407 $self->pidl("\tNDR_PULL_ALLOC_N($ndr, $var, $size);");
2409 if (grep(/in/,@{$e->{DIRECTION}}) and
2410 grep(/out/,@{$e->{DIRECTION}})) {
2411 $self->pidl("memcpy(r->out.$e->{NAME}, r->in.$e->{NAME}, ($size) * sizeof(*r->in.$e->{NAME}));");
2416 $self->pidl("NDR_PULL_ALLOC_N($ndr, $var, $size);");
2419 #####################################################################
2421 sub ParseFunctionPull($$)
2426 # pull function args
2427 $self->fn_declare("pull", $fn, "enum ndr_err_code ndr_pull_$fn->{NAME}(struct ndr_pull *$ndr, int flags, struct $fn->{NAME} *r)") or return;
2432 # declare any internal pointers we need
2433 foreach my $e (@{$fn->{ELEMENTS}}) {
2434 $self->DeclarePtrVariables($e);
2435 $self->DeclareArrayVariables($e, "pull");
2438 my %double_cases = ();
2439 foreach my $e (@{$fn->{ELEMENTS}}) {
2440 next if ($e->{TYPE} eq "EMPTY");
2441 next if ($double_cases{"$e->{NAME}"});
2442 $self->DeclareMemCtxVariables($e);
2443 $double_cases{"$e->{NAME}"} = 1;
2446 $self->pidl("NDR_PULL_CHECK_FN_FLAGS(ndr, flags);");
2448 $self->pidl("if (flags & NDR_IN) {");
2451 # auto-init the out section of a structure. I originally argued that
2452 # this was a bad idea as it hides bugs, but coping correctly
2453 # with initialisation and not wiping ref vars is turning
2454 # out to be too tricky (tridge)
2455 foreach my $e (@{$fn->{ELEMENTS}}) {
2456 next unless grep(/out/, @{$e->{DIRECTION}});
2457 $self->pidl("ZERO_STRUCT(r->out);");
2462 my $env = GenerateFunctionInEnv($fn);
2464 foreach my $e (@{$fn->{ELEMENTS}}) {
2465 next unless (grep(/in/, @{$e->{DIRECTION}}));
2466 $self->ParseElementPull($e, $ndr, $env, 1, 1);
2469 # allocate the "simple" out ref variables. FIXME: Shouldn't this have it's
2470 # own flag rather than be in NDR_IN ?
2472 foreach my $e (@{$fn->{ELEMENTS}}) {
2473 next unless (grep(/out/, @{$e->{DIRECTION}}));
2474 next unless ($e->{LEVELS}[0]->{TYPE} eq "POINTER" and
2475 $e->{LEVELS}[0]->{POINTER_TYPE} eq "ref");
2476 next if (($e->{LEVELS}[1]->{TYPE} eq "DATA") and
2477 ($e->{LEVELS}[1]->{DATA_TYPE} eq "string"));
2478 next if ($e->{LEVELS}[1]->{TYPE} eq "PIPE");
2479 next if (($e->{LEVELS}[1]->{TYPE} eq "ARRAY")
2480 and $e->{LEVELS}[1]->{IS_ZERO_TERMINATED});
2482 if ($e->{LEVELS}[1]->{TYPE} eq "ARRAY") {
2483 my $size = ParseExprExt($e->{LEVELS}[1]->{SIZE_IS}, $env, $e->{ORIGINAL},
2484 check_null_pointer($e, $env, sub { $self->pidl(shift); },
2485 "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for size_is()\");"),
2486 check_fully_dereferenced($e, $env));
2487 $self->pidl("NDR_PULL_ALLOC_N($ndr, r->out.$e->{NAME}, $size);");
2489 if (grep(/in/, @{$e->{DIRECTION}})) {
2490 $self->pidl("memcpy(r->out.$e->{NAME}, r->in.$e->{NAME}, ($size) * sizeof(*r->in.$e->{NAME}));");
2492 $self->pidl("memset(r->out.$e->{NAME}, 0, ($size) * sizeof(*r->out.$e->{NAME}));");
2494 } elsif ($e->{LEVELS}[1]->{TYPE} eq "ARRAY") {
2495 if (grep(/in/, @{$e->{DIRECTION}})) {
2496 $self->pidl("r->out.$e->{NAME} = r->in.$e->{NAME};");
2498 $self->pidl("r->out.$e->{NAME} = NULL;");
2501 $self->pidl("NDR_PULL_ALLOC($ndr, r->out.$e->{NAME});");
2503 if (grep(/in/, @{$e->{DIRECTION}})) {
2504 $self->pidl("*r->out.$e->{NAME} = *r->in.$e->{NAME};");
2506 $self->pidl("ZERO_STRUCTP(r->out.$e->{NAME});");
2511 $self->add_deferred();
2515 $self->pidl("if (flags & NDR_OUT) {");
2518 $env = GenerateFunctionOutEnv($fn);
2519 foreach my $e (@{$fn->{ELEMENTS}}) {
2520 next unless grep(/out/, @{$e->{DIRECTION}});
2521 $self->ParseElementPull($e, $ndr, $env, 1, 1);
2524 if ($fn->{RETURN_TYPE}) {
2525 $self->pidl("NDR_CHECK(ndr_pull_$fn->{RETURN_TYPE}($ndr, NDR_SCALARS, &r->out.result));");
2528 $self->add_deferred();
2532 $self->pidl("return NDR_ERR_SUCCESS;");
2538 sub AuthServiceStruct($$$)
2540 my ($self, $ifacename, $authservice) = @_;
2541 my @a = split /,/, $authservice;
2542 my $authservice_count = $#a + 1;
2544 $self->pidl("static const char * const $ifacename\_authservice_strings[] = {");
2545 foreach my $ap (@a) {
2546 $self->pidl("\t$ap, ");
2551 $self->pidl("static const struct ndr_interface_string_array $ifacename\_authservices = {");
2552 $self->pidl("\t.count\t= $authservice_count,");
2553 $self->pidl("\t.names\t= $ifacename\_authservice_strings");
2558 sub ParseGeneratePipeArray($$$)
2560 my ($self, $fn, $direction) = @_;
2562 $self->pidl("static const struct ndr_interface_call_pipe $fn->{NAME}\_$direction\_pipes[] = {");
2565 foreach my $e (@{$fn->{ELEMENTS}}) {
2566 next unless ContainsPipe($e, $e->{LEVELS}[0]);
2567 next unless (grep(/$direction/, @{$e->{DIRECTION}}));
2569 my $cname = "$e->{TYPE}_chunk";
2573 $self->pidl("\"$direction.$e->{NAME}\",");
2574 $self->pidl("\"$cname\",");
2575 $self->pidl("sizeof(struct $cname),");
2576 $self->pidl("(ndr_push_flags_fn_t) ndr_push_$cname,");
2577 $self->pidl("(ndr_pull_flags_fn_t) ndr_pull_$cname,");
2578 $self->pidl("(ndr_print_fn_t) ndr_print_$cname,");
2582 $self->pidl("{ NULL, NULL, 0, NULL, NULL, NULL }");
2588 sub FunctionCallPipes($$)
2590 my ($self, $d) = @_;
2591 return if not defined($d->{OPNUM});
2596 foreach my $e (@{$d->{ELEMENTS}}) {
2597 next unless ContainsPipe($e, $e->{LEVELS}[0]);
2599 if (grep(/in/, @{$e->{DIRECTION}})) {
2602 if (grep(/out/, @{$e->{DIRECTION}})) {
2608 $self->ParseGeneratePipeArray($d, "in");
2612 $self->ParseGeneratePipeArray($d, "out");
2616 sub FunctionCallEntry($$)
2618 my ($self, $d) = @_;
2619 return 0 if not defined($d->{OPNUM});
2624 foreach my $e (@{$d->{ELEMENTS}}) {
2625 next unless ContainsPipe($e, $e->{LEVELS}[0]);
2627 if (grep(/in/, @{$e->{DIRECTION}})) {
2630 if (grep(/out/, @{$e->{DIRECTION}})) {
2635 my $in_pipes_ptr = "NULL";
2636 my $out_pipes_ptr = "NULL";
2639 $in_pipes_ptr = "$d->{NAME}_in_pipes";
2643 $out_pipes_ptr = "$d->{NAME}_out_pipes";
2647 $self->pidl("\t\t\"$d->{NAME}\",");
2648 $self->pidl("\t\tsizeof(struct $d->{NAME}),");
2649 $self->pidl("\t\t(ndr_push_flags_fn_t) ndr_push_$d->{NAME},");
2650 $self->pidl("\t\t(ndr_pull_flags_fn_t) ndr_pull_$d->{NAME},");
2651 $self->pidl("\t\t(ndr_print_function_t) ndr_print_$d->{NAME},");
2652 $self->pidl("\t\t{ $in_pipes, $in_pipes_ptr },");
2653 $self->pidl("\t\t{ $out_pipes, $out_pipes_ptr },");
2654 $self->pidl("\t},");
2658 #####################################################################
2659 # produce a function call table
2660 sub FunctionTable($$)
2662 my($self,$interface) = @_;
2664 my $uname = uc $interface->{NAME};
2666 return if ($#{$interface->{FUNCTIONS}}+1 == 0);
2667 return unless defined ($interface->{PROPERTIES}->{uuid});
2669 foreach my $d (@{$interface->{INHERITED_FUNCTIONS}},@{$interface->{FUNCTIONS}}) {
2670 $self->FunctionCallPipes($d);
2673 $self->pidl("static const struct ndr_interface_call $interface->{NAME}\_calls[] = {");
2675 foreach my $d (@{$interface->{INHERITED_FUNCTIONS}},@{$interface->{FUNCTIONS}}) {
2676 $count += $self->FunctionCallEntry($d);
2678 $self->pidl("\t{ NULL, 0, NULL, NULL, NULL }");
2682 $self->pidl("static const char * const $interface->{NAME}\_endpoint_strings[] = {");
2683 foreach my $ep (@{$interface->{ENDPOINTS}}) {
2684 $self->pidl("\t$ep, ");
2686 my $endpoint_count = $#{$interface->{ENDPOINTS}}+1;
2691 $self->pidl("static const struct ndr_interface_string_array $interface->{NAME}\_endpoints = {");
2692 $self->pidl("\t.count\t= $endpoint_count,");
2693 $self->pidl("\t.names\t= $interface->{NAME}\_endpoint_strings");
2697 if (! defined $interface->{PROPERTIES}->{authservice}) {
2698 $interface->{PROPERTIES}->{authservice} = "\"host\"";
2701 $self->AuthServiceStruct($interface->{NAME},
2702 $interface->{PROPERTIES}->{authservice});
2704 $self->pidl("\nconst struct ndr_interface_table ndr_table_$interface->{NAME} = {");
2705 $self->pidl("\t.name\t\t= \"$interface->{NAME}\",");
2706 $self->pidl("\t.syntax_id\t= {");
2707 $self->pidl("\t\t" . print_uuid($interface->{UUID}) .",");
2708 $self->pidl("\t\tNDR_$uname\_VERSION");
2709 $self->pidl("\t},");
2710 $self->pidl("\t.helpstring\t= NDR_$uname\_HELPSTRING,");
2711 $self->pidl("\t.num_calls\t= $count,");
2712 $self->pidl("\t.calls\t\t= $interface->{NAME}\_calls,");
2713 $self->pidl("\t.endpoints\t= &$interface->{NAME}\_endpoints,");
2714 $self->pidl("\t.authservices\t= &$interface->{NAME}\_authservices");
2720 #####################################################################
2721 # generate include statements for imported idl files
2726 foreach (@imports) {
2727 $_ = unmake_str($_);
2729 $self->pidl(choose_header("librpc/gen_ndr/ndr_$_\.h", "gen_ndr/ndr_$_.h"));
2733 #####################################################################
2734 # generate include statements for included header files
2739 foreach (@includes) {
2740 $self->pidl_hdr("#include $_");
2744 #####################################################################
2745 # generate prototypes and defines for the interface definitions
2746 # FIXME: these prototypes are for the DCE/RPC client functions, not the
2747 # NDR parser and so do not belong here, technically speaking
2748 sub HeaderInterface($$$)
2750 my($self,$interface,$needed) = @_;
2754 if ($needed->{"compression"}) {
2755 $self->pidl(choose_header("librpc/ndr/ndr_compression.h", "ndr/compression.h"));
2758 if (has_property($interface, "object")) {
2759 $self->pidl(choose_header("librpc/gen_ndr/ndr_orpc.h", "ndr/orpc.h"));
2762 if (defined $interface->{PROPERTIES}->{helper}) {
2763 $self->HeaderInclude(split /,/, $interface->{PROPERTIES}->{helper});
2766 if (defined $interface->{PROPERTIES}->{uuid}) {
2767 my $name = uc $interface->{NAME};
2768 $self->pidl_hdr("#define NDR_$name\_UUID " .
2769 Parse::Pidl::Util::make_str(lc($interface->{UUID})));
2771 $self->pidl_hdr("#define NDR_$name\_VERSION $interface->{VERSION}");
2773 $self->pidl_hdr("#define NDR_$name\_NAME \"$interface->{NAME}\"");
2775 if(!defined $interface->{PROPERTIES}->{helpstring}) { $interface->{PROPERTIES}->{helpstring} = "NULL"; }
2776 $self->pidl_hdr("#define NDR_$name\_HELPSTRING $interface->{PROPERTIES}->{helpstring}");
2778 $self->pidl_hdr("extern const struct ndr_interface_table ndr_table_$interface->{NAME};");
2781 foreach (@{$interface->{FUNCTIONS}}) {
2782 next if has_property($_, "noopnum");
2783 next if grep(/^$_->{NAME}$/,@{$interface->{INHERITED_FUNCTIONS}});
2784 my $u_name = uc $_->{NAME};
2786 my $val = sprintf("0x%02x", $count);
2787 if (defined($interface->{BASE})) {
2788 $val .= " + NDR_" . uc $interface->{BASE} . "_CALL_COUNT";
2791 $self->pidl_hdr("#define NDR_$u_name ($val)");
2793 $self->pidl_hdr("");
2799 if (defined($interface->{BASE})) {
2800 $val .= " + NDR_" . uc $interface->{BASE} . "_CALL_COUNT";
2803 $self->pidl_hdr("#define NDR_" . uc $interface->{NAME} . "_CALL_COUNT ($val)");
2807 sub ParseTypePush($$$$$$)
2809 my ($self,$e, $ndr, $varname, $primitives, $deferred) = @_;
2811 # save the old relative_base_offset
2812 $self->pidl("uint32_t _save_relative_base_offset = ndr_push_get_relative_base_offset($ndr);") if defined(has_property($e, "relative_base"));
2813 $typefamily{$e->{TYPE}}->{PUSH_FN_BODY}->($self, $e, $ndr, $varname);
2814 # restore the old relative_base_offset
2815 $self->pidl("ndr_push_restore_relative_base_offset($ndr, _save_relative_base_offset);") if defined(has_property($e, "relative_base"));
2818 sub ParseTypePushFunction($$$)
2820 my ($self, $e, $varname) = @_;
2823 my $args = $typefamily{$e->{TYPE}}->{DECL}->($e, "push", $e->{NAME}, $varname);
2825 $self->fn_declare("push", $e, "enum ndr_err_code ".TypeFunctionName("ndr_push", $e)."(struct ndr_push *$ndr, int ndr_flags, $args)") or return;
2829 $self->ParseTypePush($e, $ndr, $varname, 1, 1);
2830 $self->pidl("return NDR_ERR_SUCCESS;");
2836 sub ParseTypePull($$$$$$)
2838 my ($self, $e, $ndr, $varname, $primitives, $deferred) = @_;
2840 # save the old relative_base_offset
2841 $self->pidl("uint32_t _save_relative_base_offset = ndr_pull_get_relative_base_offset($ndr);") if defined(has_property($e, "relative_base"));
2842 $typefamily{$e->{TYPE}}->{PULL_FN_BODY}->($self, $e, $ndr, $varname);
2843 # restore the old relative_base_offset
2844 $self->pidl("ndr_pull_restore_relative_base_offset($ndr, _save_relative_base_offset);") if defined(has_property($e, "relative_base"));
2847 sub ParseTypePullFunction($$)
2849 my ($self, $e, $varname) = @_;
2852 my $args = $typefamily{$e->{TYPE}}->{DECL}->($e, "pull", $e->{NAME}, $varname);
2854 $self->fn_declare("pull", $e, "enum ndr_err_code ".TypeFunctionName("ndr_pull", $e)."(struct ndr_pull *$ndr, int ndr_flags, $args)") or return;
2858 $self->ParseTypePull($e, $ndr, $varname, 1, 1);
2859 $self->pidl("return NDR_ERR_SUCCESS;");
2865 sub ParseTypePrint($$$$)
2867 my ($self, $e, $ndr, $varname) = @_;
2869 $typefamily{$e->{TYPE}}->{PRINT_FN_BODY}->($self, $e, $ndr, $e->{NAME}, $varname);
2872 sub ParseTypePrintFunction($$$)
2874 my ($self, $e, $varname) = @_;
2877 my $args = $typefamily{$e->{TYPE}}->{DECL}->($e, "print", $e->{NAME}, $varname);
2879 $self->pidl_hdr("void ".TypeFunctionName("ndr_print", $e)."(struct ndr_print *ndr, const char *name, $args);");
2881 return if (has_property($e, "noprint"));
2883 $self->pidl("_PUBLIC_ void ".TypeFunctionName("ndr_print", $e)."(struct ndr_print *$ndr, const char *name, $args)");
2886 $self->ParseTypePrint($e, $ndr, $varname);
2892 sub ParseTypeNdrSize($$)
2897 my $tf = $typefamily{$t->{TYPE}};
2898 my $args = $tf->{SIZE_FN_ARGS}->($t, $t->{NAME}, $varname);
2900 $self->fn_declare("size", $t, "size_t ndr_size_$t->{NAME}($args)") or return;
2904 $typefamily{$t->{TYPE}}->{SIZE_FN_BODY}->($self,$t, $t->{NAME}, $varname);
2910 #####################################################################
2911 # parse the interface definitions
2912 sub ParseInterface($$$)
2914 my($self,$interface,$needed) = @_;
2916 $self->pidl_hdr("#ifndef _HEADER_NDR_$interface->{NAME}");
2917 $self->pidl_hdr("#define _HEADER_NDR_$interface->{NAME}");
2919 $self->pidl_hdr("");
2921 $self->HeaderInterface($interface, $needed);
2924 foreach my $d (@{$interface->{TYPES}}) {
2925 if (Parse::Pidl::Typelist::typeIs($d, "PIPE")) {
2926 ($needed->{TypeFunctionName("ndr_push", $d)}) &&
2927 $self->ParsePipePushChunk($d);
2928 ($needed->{TypeFunctionName("ndr_pull", $d)}) &&
2929 $self->ParsePipePullChunk($d);
2930 ($needed->{TypeFunctionName("ndr_print", $d)}) &&
2931 $self->ParsePipePrintChunk($d);
2933 $needed->{TypeFunctionName("ndr_pull", $d)} = 0;
2934 $needed->{TypeFunctionName("ndr_push", $d)} = 0;
2935 $needed->{TypeFunctionName("ndr_print", $d)} = 0;
2939 next unless(typeHasBody($d));
2941 ($needed->{TypeFunctionName("ndr_push", $d)}) && $self->ParseTypePushFunction($d, "r");
2942 ($needed->{TypeFunctionName("ndr_pull", $d)}) && $self->ParseTypePullFunction($d, "r");
2943 ($needed->{TypeFunctionName("ndr_print", $d)}) && $self->ParseTypePrintFunction($d, "r");
2945 # Make sure we don't generate a function twice...
2946 $needed->{TypeFunctionName("ndr_push", $d)} =
2947 $needed->{TypeFunctionName("ndr_pull", $d)} =
2948 $needed->{TypeFunctionName("ndr_print", $d)} = 0;
2950 ($needed->{"ndr_size_$d->{NAME}"}) && $self->ParseTypeNdrSize($d);
2954 foreach my $d (@{$interface->{FUNCTIONS}}) {
2955 ($needed->{"ndr_push_$d->{NAME}"}) && $self->ParseFunctionPush($d);
2956 ($needed->{"ndr_pull_$d->{NAME}"}) && $self->ParseFunctionPull($d);
2957 ($needed->{"ndr_print_$d->{NAME}"}) && $self->ParseFunctionPrint($d);
2960 $self->FunctionTable($interface);
2962 $self->pidl_hdr("#endif /* _HEADER_NDR_$interface->{NAME} */");
2965 sub GenerateIncludes($)
2969 $self->pidl("#include \"includes.h\"");
2971 $self->pidl("#ifndef _GNU_SOURCE");
2972 $self->pidl("#define _GNU_SOURCE");
2973 $self->pidl("#endif");
2974 $self->pidl("#include <stdint.h>");
2975 $self->pidl("#include <stdlib.h>");
2976 $self->pidl("#include <stdio.h>");
2977 $self->pidl("#include <stdbool.h>");
2978 $self->pidl("#include <stdarg.h>");
2979 $self->pidl("#include <string.h>");
2983 #####################################################################
2984 # parse a parsed IDL structure back into an IDL file
2987 my($self, $ndr,$gen_header,$ndr_header) = @_;
2989 $self->pidl_hdr("/* header auto-generated by pidl */");
2990 $self->pidl_hdr("");
2991 $self->pidl_hdr(choose_header("librpc/ndr/libndr.h", "ndr.h"));
2992 $self->pidl_hdr("#include \"$gen_header\"") if ($gen_header);
2993 $self->pidl_hdr("");
2995 $self->pidl("/* parser auto-generated by pidl */");
2997 $self->GenerateIncludes();
2998 $self->pidl("#include \"$ndr_header\"") if ($ndr_header);
3004 ($_->{TYPE} eq "INTERFACE") && NeededInterface($_, \%needed);
3008 ($_->{TYPE} eq "INTERFACE") && $self->ParseInterface($_, \%needed);
3009 ($_->{TYPE} eq "IMPORT") && $self->HeaderImport(@{$_->{PATHS}});
3010 ($_->{TYPE} eq "INCLUDE") && $self->HeaderInclude(@{$_->{PATHS}});
3013 return ($self->{res_hdr}, $self->{res});
3016 sub NeededElement($$$)
3018 my ($e, $dir, $needed) = @_;
3020 return if ($e->{TYPE} eq "EMPTY");
3022 return if (ref($e->{TYPE}) eq "HASH" and
3023 not defined($e->{TYPE}->{NAME}));
3026 if (ref($e->{TYPE}) eq "HASH") {
3027 $t = $e->{TYPE}->{TYPE}."_".$e->{TYPE}->{NAME};
3032 if (ref($e->{REPRESENTATION_TYPE}) eq "HASH") {
3033 $rt = $e->{REPRESENTATION_TYPE}->{TYPE}."_".$e->{REPRESENTATION_TYPE}->{NAME};
3035 $rt = $e->{REPRESENTATION_TYPE};
3038 die ("$e->{NAME} $t, $rt FOO") unless ($rt ne "");
3041 if ($dir eq "print") {
3042 push(@fn, TypeFunctionName("ndr_print", $e->{REPRESENTATION_TYPE}));
3043 } elsif ($dir eq "pull") {
3044 push (@fn, TypeFunctionName("ndr_pull", $e->{TYPE}));
3045 push (@fn, "ndr_$t\_to_$rt")
3047 } elsif ($dir eq "push") {
3048 push (@fn, TypeFunctionName("ndr_push", $e->{TYPE}));
3049 push (@fn, "ndr_$rt\_to_$t")
3052 die("invalid direction `$dir'");
3056 unless (defined($needed->{$_})) {
3062 sub NeededFunction($$)
3064 my ($fn,$needed) = @_;
3065 $needed->{"ndr_pull_$fn->{NAME}"} = 1;
3066 $needed->{"ndr_push_$fn->{NAME}"} = 1;
3067 $needed->{"ndr_print_$fn->{NAME}"} = 1;
3068 foreach my $e (@{$fn->{ELEMENTS}}) {
3070 NeededElement($e, $_, $needed) foreach ("pull", "push", "print");
3076 sub NeededType($$$);
3077 my ($t,$needed,$req) = @_;
3079 NeededType($t->{DATA}, $needed, $req) if ($t->{TYPE} eq "TYPEDEF");
3080 NeededType($t->{DATA}, $needed, $req) if ($t->{TYPE} eq "PIPE");
3082 if ($t->{TYPE} eq "STRUCT" or $t->{TYPE} eq "UNION") {
3083 return unless defined($t->{ELEMENTS});
3084 for my $e (@{$t->{ELEMENTS}}) {
3086 if (has_property($e, "compression")) {
3087 $needed->{"compression"} = 1;
3089 NeededElement($e, $req, $needed);
3090 NeededType($e->{TYPE}, $needed, $req) if (ref($e->{TYPE}) eq "HASH");
3095 #####################################################################
3096 # work out what parse functions are needed
3097 sub NeededInterface($$)
3099 my ($interface,$needed) = @_;
3100 NeededFunction($_, $needed) foreach (@{$interface->{FUNCTIONS}});
3101 foreach (reverse @{$interface->{TYPES}}) {
3103 if (has_property($_, "public")) {
3104 $needed->{TypeFunctionName("ndr_pull", $_)} = $needed->{TypeFunctionName("ndr_push", $_)} =
3105 $needed->{TypeFunctionName("ndr_print", $_)} = 1;
3108 NeededType($_, $needed, "pull") if ($needed->{TypeFunctionName("ndr_pull", $_)});
3109 NeededType($_, $needed, "push") if ($needed->{TypeFunctionName("ndr_push", $_)});
3110 NeededType($_, $needed, "print") if ($needed->{TypeFunctionName("ndr_print", $_)});
3111 if (has_property($_, "gensize")) {
3112 $needed->{"ndr_size_$_->{NAME}"} = 1;
3117 sub TypeFunctionName($$)
3119 my ($prefix, $t) = @_;
3121 return "$prefix\_$t->{NAME}" if (ref($t) eq "HASH" and
3122 $t->{TYPE} eq "TYPEDEF");
3123 return "$prefix\_$t->{TYPE}_$t->{NAME}" if (ref($t) eq "HASH");
3124 return "$prefix\_$t";