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 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))";
202 $size = ParseExpr($l->{SIZE_IS}, $env, $e);
203 $length = ParseExpr($l->{LENGTH_IS}, $env, $e);
206 if ((!$l->{IS_SURROUNDING}) and $l->{IS_CONFORMANT}) {
207 $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, $size));");
210 if ($l->{IS_VARYING}) {
211 $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, 0));"); # array offset
212 $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, $length));");
218 sub check_fully_dereferenced($$)
220 my ($element, $env) = @_;
226 # Figure out the number of pointers in $ptr
227 my $expandedvar = $origvar;
228 $expandedvar =~ s/^(\**)//;
232 foreach (keys %$env) {
233 if ($env->{$_} eq $expandedvar) {
239 return($origvar) unless (defined($var));
241 foreach (@{$element->{PARENT}->{ELEMENTS}}) {
242 if ($_->{NAME} eq $var) {
248 $e or die("Environment doesn't match siblings");
250 # See if pointer at pointer level $level
251 # needs to be checked.
253 foreach (@{$e->{LEVELS}}) {
254 if ($_->{TYPE} eq "POINTER") {
255 $nump = $_->{POINTER_INDEX}+1;
258 warning($element->{ORIGINAL}, "Got pointer for `$e->{NAME}', expected fully derefenced variable") if ($nump > length($ptr));
263 sub check_null_pointer($$$$)
265 my ($element, $env, $print_fn, $return) = @_;
268 my $expandedvar = shift;
271 # Figure out the number of pointers in $ptr
272 $expandedvar =~ s/^(\**)//;
276 foreach (keys %$env) {
277 if ($env->{$_} eq $expandedvar) {
286 foreach (@{$element->{PARENT}->{ELEMENTS}}) {
287 if ($_->{NAME} eq $var) {
293 $e or die("Environment doesn't match siblings");
295 # See if pointer at pointer level $level
296 # needs to be checked.
297 foreach my $l (@{$e->{LEVELS}}) {
298 if ($l->{TYPE} eq "POINTER" and
299 $l->{POINTER_INDEX} == length($ptr)) {
300 # No need to check ref pointers
301 $check = ($l->{POINTER_TYPE} ne "ref");
305 if ($l->{TYPE} eq "DATA") {
306 warning($element, "too much dereferences for `$var'");
310 warning($element, "unknown dereferenced expression `$expandedvar'");
314 $print_fn->("if ($ptr$expandedvar == NULL) $return") if $check;
318 #####################################################################
319 # parse an array - pull side
320 sub ParseArrayPullHeader($$$$$$)
322 my ($self,$e,$l,$ndr,$var_name,$env) = @_;
327 if ($l->{IS_CONFORMANT}) {
328 $length = $size = "ndr_get_array_size($ndr, " . get_pointer_to($var_name) . ")";
329 } elsif ($l->{IS_ZERO_TERMINATED} and $l->{SIZE_IS} == 0 and $l->{LENGTH_IS} == 0) { # Noheader arrays
330 $length = $size = "ndr_get_string_size($ndr, sizeof(*$var_name))";
332 $length = $size = ParseExprExt($l->{SIZE_IS}, $env, $e->{ORIGINAL},
333 check_null_pointer($e, $env, sub { $self->pidl(shift); },
334 "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for size_is()\");"),
335 check_fully_dereferenced($e, $env));
338 if ((!$l->{IS_SURROUNDING}) and $l->{IS_CONFORMANT}) {
339 $self->pidl("NDR_CHECK(ndr_pull_array_size($ndr, " . get_pointer_to($var_name) . "));");
342 if ($l->{IS_VARYING}) {
343 $self->pidl("NDR_CHECK(ndr_pull_array_length($ndr, " . get_pointer_to($var_name) . "));");
344 $length = "ndr_get_array_length($ndr, " . get_pointer_to($var_name) .")";
347 if ($length ne $size) {
348 $self->pidl("if ($length > $size) {");
350 $self->pidl("return ndr_pull_error($ndr, NDR_ERR_ARRAY_SIZE, \"Bad array size %u should exceed array length %u\", $size, $length);");
355 if ($l->{IS_CONFORMANT} and not $l->{IS_ZERO_TERMINATED}) {
356 $self->defer("if ($var_name) {");
358 my $size = ParseExprExt($l->{SIZE_IS}, $env, $e->{ORIGINAL},
359 check_null_pointer($e, $env, sub { $self->defer(shift); },
360 "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for size_is()\");"),
361 check_fully_dereferenced($e, $env));
362 $self->defer("NDR_CHECK(ndr_check_array_size($ndr, (void*)" . get_pointer_to($var_name) . ", $size));");
363 $self->defer_deindent;
367 if ($l->{IS_VARYING} and not $l->{IS_ZERO_TERMINATED}) {
368 $self->defer("if ($var_name) {");
370 my $length = ParseExprExt($l->{LENGTH_IS}, $env, $e->{ORIGINAL},
371 check_null_pointer($e, $env, sub { $self->defer(shift); },
372 "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for length_is()\");"),
373 check_fully_dereferenced($e, $env));
374 $self->defer("NDR_CHECK(ndr_check_array_length($ndr, (void*)" . get_pointer_to($var_name) . ", $length));");
375 $self->defer_deindent;
379 if (ArrayDynamicallyAllocated($e,$l) and not is_charset_array($e,$l)) {
380 $self->AllocateArrayLevel($e,$l,$ndr,$var_name,$size);
386 sub compression_alg($$)
389 my ($alg, $clen, $dlen) = split(/,/, $l->{COMPRESSION});
394 sub compression_clen($$$)
396 my ($e, $l, $env) = @_;
397 my ($alg, $clen, $dlen) = split(/,/, $l->{COMPRESSION});
399 return ParseExpr($clen, $env, $e->{ORIGINAL});
402 sub compression_dlen($$$)
404 my ($e,$l,$env) = @_;
405 my ($alg, $clen, $dlen) = split(/,/, $l->{COMPRESSION});
407 return ParseExpr($dlen, $env, $e->{ORIGINAL});
410 sub ParseCompressionPushStart($$$$$)
412 my ($self,$e,$l,$ndr,$env) = @_;
413 my $comndr = "$ndr\_compressed";
414 my $alg = compression_alg($e, $l);
415 my $dlen = compression_dlen($e, $l, $env);
419 $self->pidl("struct ndr_push *$comndr;");
420 $self->pidl("NDR_CHECK(ndr_push_compression_start($ndr, &$comndr, $alg, $dlen));");
425 sub ParseCompressionPushEnd($$$$$)
427 my ($self,$e,$l,$ndr,$env) = @_;
428 my $comndr = "$ndr\_compressed";
429 my $alg = compression_alg($e, $l);
430 my $dlen = compression_dlen($e, $l, $env);
432 $self->pidl("NDR_CHECK(ndr_push_compression_end($ndr, $comndr, $alg, $dlen));");
437 sub ParseCompressionPullStart($$$$$)
439 my ($self,$e,$l,$ndr,$env) = @_;
440 my $comndr = "$ndr\_compressed";
441 my $alg = compression_alg($e, $l);
442 my $dlen = compression_dlen($e, $l, $env);
446 $self->pidl("struct ndr_pull *$comndr;");
447 $self->pidl("NDR_CHECK(ndr_pull_compression_start($ndr, &$comndr, $alg, $dlen));");
452 sub ParseCompressionPullEnd($$$$$)
454 my ($self,$e,$l,$ndr,$env) = @_;
455 my $comndr = "$ndr\_compressed";
456 my $alg = compression_alg($e, $l);
457 my $dlen = compression_dlen($e, $l, $env);
459 $self->pidl("NDR_CHECK(ndr_pull_compression_end($ndr, $comndr, $alg, $dlen));");
464 sub ParseSubcontextPushStart($$$$$)
466 my ($self,$e,$l,$ndr,$env) = @_;
467 my $subndr = "_ndr_$e->{NAME}";
468 my $subcontext_size = ParseExpr($l->{SUBCONTEXT_SIZE}, $env, $e->{ORIGINAL});
472 $self->pidl("struct ndr_push *$subndr;");
473 $self->pidl("NDR_CHECK(ndr_push_subcontext_start($ndr, &$subndr, $l->{HEADER_SIZE}, $subcontext_size));");
475 if (defined $l->{COMPRESSION}) {
476 $subndr = $self->ParseCompressionPushStart($e, $l, $subndr, $env);
482 sub ParseSubcontextPushEnd($$$$$)
484 my ($self,$e,$l,$ndr,$env) = @_;
485 my $subndr = "_ndr_$e->{NAME}";
486 my $subcontext_size = ParseExpr($l->{SUBCONTEXT_SIZE}, $env, $e->{ORIGINAL});
488 if (defined $l->{COMPRESSION}) {
489 $self->ParseCompressionPushEnd($e, $l, $subndr, $env);
492 $self->pidl("NDR_CHECK(ndr_push_subcontext_end($ndr, $subndr, $l->{HEADER_SIZE}, $subcontext_size));");
497 sub ParseSubcontextPullStart($$$$$)
499 my ($self,$e,$l,$ndr,$env) = @_;
500 my $subndr = "_ndr_$e->{NAME}";
501 my $subcontext_size = ParseExpr($l->{SUBCONTEXT_SIZE}, $env, $e->{ORIGINAL});
505 $self->pidl("struct ndr_pull *$subndr;");
506 $self->pidl("NDR_CHECK(ndr_pull_subcontext_start($ndr, &$subndr, $l->{HEADER_SIZE}, $subcontext_size));");
508 if (defined $l->{COMPRESSION}) {
509 $subndr = $self->ParseCompressionPullStart($e, $l, $subndr, $env);
515 sub ParseSubcontextPullEnd($$$$$)
517 my ($self,$e,$l,$ndr,$env) = @_;
518 my $subndr = "_ndr_$e->{NAME}";
519 my $subcontext_size = ParseExpr($l->{SUBCONTEXT_SIZE}, $env, $e->{ORIGINAL});
521 if (defined $l->{COMPRESSION}) {
522 $self->ParseCompressionPullEnd($e, $l, $subndr, $env);
525 $self->pidl("NDR_CHECK(ndr_pull_subcontext_end($ndr, $subndr, $l->{HEADER_SIZE}, $subcontext_size));");
530 sub ParseElementPushLevel
532 my ($self,$e,$l,$ndr,$var_name,$env,$primitives,$deferred) = @_;
534 my $ndr_flags = CalcNdrFlags($l, $primitives, $deferred);
536 if ($l->{TYPE} eq "ARRAY" and ($l->{IS_CONFORMANT} or $l->{IS_VARYING})) {
537 $var_name = get_pointer_to($var_name);
540 if (defined($ndr_flags)) {
541 if ($l->{TYPE} eq "SUBCONTEXT") {
542 my $subndr = $self->ParseSubcontextPushStart($e, $l, $ndr, $env);
543 $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $subndr, $var_name, $env, 1, 1);
544 $self->ParseSubcontextPushEnd($e, $l, $ndr, $env);
545 } elsif ($l->{TYPE} eq "POINTER") {
546 $self->ParsePtrPush($e, $l, $ndr, $var_name);
547 } elsif ($l->{TYPE} eq "ARRAY") {
548 my $length = $self->ParseArrayPushHeader($e, $l, $ndr, $var_name, $env);
550 my $nl = GetNextLevel($e, $l);
552 # Allow speedups for arrays of scalar types
553 if (is_charset_array($e,$l)) {
554 $self->pidl("NDR_CHECK(ndr_push_charset($ndr, $ndr_flags, $var_name, $length, sizeof(" . mapTypeName($nl->{DATA_TYPE}) . "), CH_$e->{PROPERTIES}->{charset}));");
556 } elsif (has_fast_array($e,$l)) {
557 $self->pidl("NDR_CHECK(ndr_push_array_$nl->{DATA_TYPE}($ndr, $ndr_flags, $var_name, $length));");
560 } elsif ($l->{TYPE} eq "SWITCH") {
561 $self->ParseSwitchPush($e, $l, $ndr, $var_name, $env);
562 } elsif ($l->{TYPE} eq "DATA") {
563 $self->ParseDataPush($e, $l, $ndr, $var_name, $primitives, $deferred);
564 } elsif ($l->{TYPE} eq "TYPEDEF") {
565 $typefamily{$e->{DATA}->{TYPE}}->{PUSH_FN_BODY}->($self, $e->{DATA}, $ndr, $var_name);
569 if ($l->{TYPE} eq "POINTER" and $deferred) {
570 if ($l->{POINTER_TYPE} ne "ref") {
571 $self->pidl("if ($var_name) {");
573 if ($l->{POINTER_TYPE} eq "relative") {
574 $self->pidl("NDR_CHECK(ndr_push_relative_ptr2($ndr, $var_name));");
577 $var_name = get_value_of($var_name);
578 $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, 1, 1);
580 if ($l->{POINTER_TYPE} ne "ref") {
584 } elsif ($l->{TYPE} eq "ARRAY" and not has_fast_array($e,$l) and
585 not is_charset_array($e, $l)) {
586 my $length = ParseExpr($l->{LENGTH_IS}, $env, $e->{ORIGINAL});
587 my $counter = "cntr_$e->{NAME}_$l->{LEVEL_INDEX}";
589 my $array_pointless = ($length eq "0");
591 if ($array_pointless) {
592 warning($e->{ORIGINAL}, "pointless array `$e->{NAME}' will always have size 0");
595 $var_name = get_array_element($var_name, $counter);
597 if ((($primitives and not $l->{IS_DEFERRED}) or ($deferred and $l->{IS_DEFERRED})) and not $array_pointless) {
598 $self->pidl("for ($counter = 0; $counter < $length; $counter++) {");
600 $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, 1, 0);
605 if ($deferred and ContainsDeferred($e, $l) and not $array_pointless) {
606 $self->pidl("for ($counter = 0; $counter < $length; $counter++) {");
608 $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, 0, 1);
612 } elsif ($l->{TYPE} eq "SWITCH") {
613 $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, $primitives, $deferred);
617 #####################################################################
618 # parse scalars in a structure element
619 sub ParseElementPush($$$$$$)
621 my ($self,$e,$ndr,$env,$primitives,$deferred) = @_;
624 my $var_name = $env->{$e->{NAME}};
626 return unless $primitives or ($deferred and ContainsDeferred($e, $e->{LEVELS}[0]));
628 # Representation type is different from transmit_as
629 if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
632 my $transmit_name = "_transmit_$e->{NAME}";
633 $self->pidl(mapTypeName($e->{TYPE}) ." $transmit_name;");
634 $self->pidl("NDR_CHECK(ndr_$e->{REPRESENTATION_TYPE}_to_$e->{TYPE}($var_name, " . get_pointer_to($transmit_name) . "));");
635 $var_name = $transmit_name;
638 $var_name = append_prefix($e, $var_name);
640 $self->start_flags($e, $ndr);
642 if (defined(my $value = has_property($e, "value"))) {
643 $var_name = ParseExpr($value, $env, $e->{ORIGINAL});
646 $self->ParseElementPushLevel($e, $e->{LEVELS}[0], $ndr, $var_name, $env, $primitives, $deferred);
648 $self->end_flags($e, $ndr);
650 if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
656 #####################################################################
657 # parse a pointer in a struct element or function
658 sub ParsePtrPush($$$$$)
660 my ($self,$e,$l,$ndr,$var_name) = @_;
662 if ($l->{POINTER_TYPE} eq "ref") {
663 $self->pidl("if ($var_name == NULL) {");
665 $self->pidl("return ndr_push_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL [ref] pointer\");");
668 if ($l->{LEVEL} eq "EMBEDDED") {
669 $self->pidl("NDR_CHECK(ndr_push_ref_ptr(ndr));");
671 } elsif ($l->{POINTER_TYPE} eq "relative") {
672 $self->pidl("NDR_CHECK(ndr_push_relative_ptr1($ndr, $var_name));");
673 } elsif ($l->{POINTER_TYPE} eq "unique") {
674 $self->pidl("NDR_CHECK(ndr_push_unique_ptr($ndr, $var_name));");
675 } elsif ($l->{POINTER_TYPE} eq "full") {
676 $self->pidl("NDR_CHECK(ndr_push_full_ptr($ndr, $var_name));");
678 die("Unhandled pointer type $l->{POINTER_TYPE}");
682 sub need_pointer_to($$$)
684 my ($e, $l, $scalar_only) = @_;
687 if (ref($l->{DATA_TYPE})) {
688 $t = "$l->{DATA_TYPE}->{TYPE}_$l->{DATA_TYPE}->{NAME}";
690 $t = $l->{DATA_TYPE};
693 if (not Parse::Pidl::Typelist::is_scalar($t)) {
694 return 1 if $scalar_only;
699 foreach my $tl (@{$e->{LEVELS}}) {
701 if ($tl->{TYPE} eq "ARRAY") {
706 if (Parse::Pidl::Typelist::scalar_is_reference($t)) {
707 return 1 unless $arrays;
713 sub ParseDataPrint($$$$$)
715 my ($self, $e, $l, $ndr, $var_name) = @_;
717 if (not ref($l->{DATA_TYPE}) or defined($l->{DATA_TYPE}->{NAME})) {
719 if (need_pointer_to($e, $l, 1)) {
720 $var_name = get_pointer_to($var_name);
723 $self->pidl(TypeFunctionName("ndr_print", $l->{DATA_TYPE})."($ndr, \"$e->{NAME}\", $var_name);");
725 $self->ParseTypePrint($l->{DATA_TYPE}, $ndr, $var_name);
729 #####################################################################
730 # print scalars in a structure element
731 sub ParseElementPrint($$$$$)
733 my($self, $e, $ndr, $var_name, $env) = @_;
735 return if (has_property($e, "noprint"));
737 if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
738 $self->pidl("ndr_print_$e->{REPRESENTATION_TYPE}($ndr, \"$e->{NAME}\", $var_name);");
742 $var_name = append_prefix($e, $var_name);
744 if (defined(my $value = has_property($e, "value"))) {
745 $var_name = "($ndr->flags & LIBNDR_PRINT_SET_VALUES)?" . ParseExpr($value,$env, $e->{ORIGINAL}) . ":$var_name";
748 foreach my $l (@{$e->{LEVELS}}) {
749 if ($l->{TYPE} eq "POINTER") {
750 $self->pidl("ndr_print_ptr($ndr, \"$e->{NAME}\", $var_name);");
751 $self->pidl("$ndr->depth++;");
752 if ($l->{POINTER_TYPE} ne "ref") {
753 $self->pidl("if ($var_name) {");
756 $var_name = get_value_of($var_name);
757 } elsif ($l->{TYPE} eq "ARRAY") {
760 if ($l->{IS_CONFORMANT} or $l->{IS_VARYING}) {
761 $var_name = get_pointer_to($var_name);
764 if ($l->{IS_ZERO_TERMINATED}) {
765 $length = "ndr_string_length($var_name, sizeof(*$var_name))";
767 $length = ParseExprExt($l->{LENGTH_IS}, $env, $e->{ORIGINAL},
768 check_null_pointer($e, $env, sub { $self->pidl(shift); }, "return;"), check_fully_dereferenced($e, $env));
771 if (is_charset_array($e,$l)) {
772 $self->pidl("ndr_print_string($ndr, \"$e->{NAME}\", $var_name);");
774 } elsif (has_fast_array($e, $l)) {
775 my $nl = GetNextLevel($e, $l);
776 $self->pidl("ndr_print_array_$nl->{DATA_TYPE}($ndr, \"$e->{NAME}\", $var_name, $length);");
779 my $counter = "cntr_$e->{NAME}_$l->{LEVEL_INDEX}";
781 $self->pidl("$ndr->print($ndr, \"\%s: ARRAY(\%d)\", \"$e->{NAME}\", (int)$length);");
782 $self->pidl("$ndr->depth++;");
783 $self->pidl("for ($counter=0;$counter<$length;$counter++) {");
785 $self->pidl("char *idx_$l->{LEVEL_INDEX}=NULL;");
786 $self->pidl("if (asprintf(&idx_$l->{LEVEL_INDEX}, \"[\%d]\", $counter) != -1) {");
789 $var_name = get_array_element($var_name, $counter);
791 } elsif ($l->{TYPE} eq "DATA") {
792 $self->ParseDataPrint($e, $l, $ndr, $var_name);
793 } elsif ($l->{TYPE} eq "SWITCH") {
794 my $switch_var = ParseExprExt($l->{SWITCH_IS}, $env, $e->{ORIGINAL},
795 check_null_pointer($e, $env, sub { $self->pidl(shift); }, "return;"), check_fully_dereferenced($e, $env));
796 $self->pidl("ndr_print_set_switch_value($ndr, " . get_pointer_to($var_name) . ", $switch_var);");
800 foreach my $l (reverse @{$e->{LEVELS}}) {
801 if ($l->{TYPE} eq "POINTER") {
802 if ($l->{POINTER_TYPE} ne "ref") {
806 $self->pidl("$ndr->depth--;");
807 } elsif (($l->{TYPE} eq "ARRAY")
808 and not is_charset_array($e,$l)
809 and not has_fast_array($e,$l)) {
810 $self->pidl("free(idx_$l->{LEVEL_INDEX});");
815 $self->pidl("$ndr->depth--;");
820 #####################################################################
821 # parse scalars in a structure element - pull size
822 sub ParseSwitchPull($$$$$$)
824 my($self,$e,$l,$ndr,$var_name,$env) = @_;
825 my $switch_var = ParseExprExt($l->{SWITCH_IS}, $env, $e->{ORIGINAL},
826 check_null_pointer($e, $env, sub { $self->pidl(shift); },
827 "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for switch_is()\");"),
828 check_fully_dereferenced($e, $env));
830 $var_name = get_pointer_to($var_name);
831 $self->pidl("NDR_CHECK(ndr_pull_set_switch_value($ndr, $var_name, $switch_var));");
834 #####################################################################
835 # push switch element
836 sub ParseSwitchPush($$$$$$)
838 my($self,$e,$l,$ndr,$var_name,$env) = @_;
839 my $switch_var = ParseExprExt($l->{SWITCH_IS}, $env, $e->{ORIGINAL},
840 check_null_pointer($e, $env, sub { $self->pidl(shift); },
841 "return ndr_push_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for switch_is()\");"),
842 check_fully_dereferenced($e, $env));
844 $var_name = get_pointer_to($var_name);
845 $self->pidl("NDR_CHECK(ndr_push_set_switch_value($ndr, $var_name, $switch_var));");
848 sub ParseDataPull($$$$$$$)
850 my ($self,$e,$l,$ndr,$var_name,$primitives,$deferred) = @_;
852 if (not ref($l->{DATA_TYPE}) or defined($l->{DATA_TYPE}->{NAME})) {
854 my $ndr_flags = CalcNdrFlags($l, $primitives, $deferred);
856 if (need_pointer_to($e, $l, 0)) {
857 $var_name = get_pointer_to($var_name);
860 $var_name = get_pointer_to($var_name);
862 $self->pidl("NDR_CHECK(".TypeFunctionName("ndr_pull", $l->{DATA_TYPE})."($ndr, $ndr_flags, $var_name));");
864 if (my $range = has_property($e, "range")) {
865 $var_name = get_value_of($var_name);
866 my $signed = Parse::Pidl::Typelist::is_signed($l->{DATA_TYPE});
867 my ($low, $high) = split(/,/, $range, 2);
868 if ($low < 0 and not $signed) {
869 warning(0, "$low is invalid for the range of an unsigned type");
871 if ($low == 0 and not $signed) {
872 $self->pidl("if ($var_name > $high) {");
874 $self->pidl("if ($var_name < $low || $var_name > $high) {");
876 $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_RANGE, \"value out of range\");");
880 $self->ParseTypePull($l->{DATA_TYPE}, $ndr, $var_name, $primitives, $deferred);
884 sub ParseDataPush($$$$$$$)
886 my ($self,$e,$l,$ndr,$var_name,$primitives,$deferred) = @_;
888 if (not ref($l->{DATA_TYPE}) or defined($l->{DATA_TYPE}->{NAME})) {
890 my $ndr_flags = CalcNdrFlags($l, $primitives, $deferred);
892 # strings are passed by value rather than reference
893 if (need_pointer_to($e, $l, 1)) {
894 $var_name = get_pointer_to($var_name);
897 $self->pidl("NDR_CHECK(".TypeFunctionName("ndr_push", $l->{DATA_TYPE})."($ndr, $ndr_flags, $var_name));");
899 $self->ParseTypePush($l->{DATA_TYPE}, $ndr, $var_name, $primitives, $deferred);
903 sub CalcNdrFlags($$$)
905 my ($l,$primitives,$deferred) = @_;
910 # Add NDR_SCALARS if this one is deferred
911 # and deferreds may be pushed
912 $scalars = 1 if ($l->{IS_DEFERRED} and $deferred);
914 # Add NDR_SCALARS if this one is not deferred and
915 # primitives may be pushed
916 $scalars = 1 if (!$l->{IS_DEFERRED} and $primitives);
918 # Add NDR_BUFFERS if this one contains deferred stuff
919 # and deferreds may be pushed
920 $buffers = 1 if ($l->{CONTAINS_DEFERRED} and $deferred);
922 return "NDR_SCALARS|NDR_BUFFERS" if ($scalars and $buffers);
923 return "NDR_SCALARS" if ($scalars);
924 return "NDR_BUFFERS" if ($buffers);
928 sub ParseMemCtxPullFlags($$$$)
930 my ($self, $e, $l) = @_;
932 return undef unless ($l->{TYPE} eq "POINTER" or $l->{TYPE} eq "ARRAY");
934 return undef unless ($l->{TYPE} ne "ARRAY" or ArrayDynamicallyAllocated($e,$l));
935 return undef if has_fast_array($e, $l);
936 return undef if is_charset_array($e, $l);
940 if (($l->{TYPE} eq "POINTER") and ($l->{POINTER_TYPE} eq "ref")) {
941 my $nl = GetNextLevel($e, $l);
942 my $next_is_array = ($nl->{TYPE} eq "ARRAY");
943 my $next_is_string = (($nl->{TYPE} eq "DATA") and
944 ($nl->{DATA_TYPE} eq "string"));
945 if ($next_is_array or $next_is_string) {
947 } elsif ($l->{LEVEL} eq "TOP") {
948 $mem_flags = "LIBNDR_FLAG_REF_ALLOC";
955 sub ParseMemCtxPullStart($$$$$)
957 my ($self, $e, $l, $ndr, $ptr_name) = @_;
959 my $mem_r_ctx = "_mem_save_$e->{NAME}_$l->{LEVEL_INDEX}";
960 my $mem_c_ctx = $ptr_name;
961 my $mem_c_flags = $self->ParseMemCtxPullFlags($e, $l);
963 return unless defined($mem_c_flags);
965 $self->pidl("$mem_r_ctx = NDR_PULL_GET_MEM_CTX($ndr);");
966 $self->pidl("NDR_PULL_SET_MEM_CTX($ndr, $mem_c_ctx, $mem_c_flags);");
969 sub ParseMemCtxPullEnd($$$$)
971 my ($self, $e, $l, $ndr) = @_;
973 my $mem_r_ctx = "_mem_save_$e->{NAME}_$l->{LEVEL_INDEX}";
974 my $mem_r_flags = $self->ParseMemCtxPullFlags($e, $l);
976 return unless defined($mem_r_flags);
978 $self->pidl("NDR_PULL_SET_MEM_CTX($ndr, $mem_r_ctx, $mem_r_flags);");
981 sub CheckStringTerminator($$$$$)
983 my ($self,$ndr,$e,$l,$length) = @_;
984 my $nl = GetNextLevel($e, $l);
986 # Make sure last element is zero!
987 $self->pidl("NDR_CHECK(ndr_check_string_terminator($ndr, $length, sizeof($nl->{DATA_TYPE}_t)));");
990 sub ParseElementPullLevel
992 my($self,$e,$l,$ndr,$var_name,$env,$primitives,$deferred) = @_;
994 my $ndr_flags = CalcNdrFlags($l, $primitives, $deferred);
996 if ($l->{TYPE} eq "ARRAY" and ($l->{IS_VARYING} or $l->{IS_CONFORMANT})) {
997 $var_name = get_pointer_to($var_name);
1000 # Only pull something if there's actually something to be pulled
1001 if (defined($ndr_flags)) {
1002 if ($l->{TYPE} eq "SUBCONTEXT") {
1003 my $subndr = $self->ParseSubcontextPullStart($e, $l, $ndr, $env);
1004 $self->ParseElementPullLevel($e, GetNextLevel($e,$l), $subndr, $var_name, $env, 1, 1);
1005 $self->ParseSubcontextPullEnd($e, $l, $ndr, $env);
1006 } elsif ($l->{TYPE} eq "ARRAY") {
1007 my $length = $self->ParseArrayPullHeader($e, $l, $ndr, $var_name, $env);
1009 my $nl = GetNextLevel($e, $l);
1011 if (is_charset_array($e,$l)) {
1012 if ($l->{IS_ZERO_TERMINATED}) {
1013 $self->CheckStringTerminator($ndr, $e, $l, $length);
1015 $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}));");
1017 } elsif (has_fast_array($e, $l)) {
1018 if ($l->{IS_ZERO_TERMINATED}) {
1019 $self->CheckStringTerminator($ndr,$e,$l,$length);
1021 $self->pidl("NDR_CHECK(ndr_pull_array_$nl->{DATA_TYPE}($ndr, $ndr_flags, $var_name, $length));");
1024 } elsif ($l->{TYPE} eq "POINTER") {
1025 $self->ParsePtrPull($e, $l, $ndr, $var_name);
1026 } elsif ($l->{TYPE} eq "SWITCH") {
1027 $self->ParseSwitchPull($e, $l, $ndr, $var_name, $env);
1028 } elsif ($l->{TYPE} eq "DATA") {
1029 $self->ParseDataPull($e, $l, $ndr, $var_name, $primitives, $deferred);
1030 } elsif ($l->{TYPE} eq "TYPEDEF") {
1031 $typefamily{$e->{DATA}->{TYPE}}->{PULL_FN_BODY}->($self, $e->{DATA}, $ndr, $var_name);
1035 # add additional constructions
1036 if ($l->{TYPE} eq "POINTER" and $deferred) {
1037 if ($l->{POINTER_TYPE} ne "ref") {
1038 $self->pidl("if ($var_name) {");
1041 if ($l->{POINTER_TYPE} eq "relative") {
1042 $self->pidl("uint32_t _relative_save_offset;");
1043 $self->pidl("_relative_save_offset = $ndr->offset;");
1044 $self->pidl("NDR_CHECK(ndr_pull_relative_ptr2($ndr, $var_name));");
1048 $self->ParseMemCtxPullStart($e, $l, $ndr, $var_name);
1050 $var_name = get_value_of($var_name);
1051 $self->ParseElementPullLevel($e, GetNextLevel($e,$l), $ndr, $var_name, $env, 1, 1);
1053 $self->ParseMemCtxPullEnd($e, $l, $ndr);
1055 if ($l->{POINTER_TYPE} ne "ref") {
1056 if ($l->{POINTER_TYPE} eq "relative") {
1057 $self->pidl("$ndr->offset = _relative_save_offset;");
1062 } elsif ($l->{TYPE} eq "ARRAY" and
1063 not has_fast_array($e,$l) and not is_charset_array($e, $l)) {
1064 my $length = ParseExpr($l->{LENGTH_IS}, $env, $e->{ORIGINAL});
1065 my $counter = "cntr_$e->{NAME}_$l->{LEVEL_INDEX}";
1066 my $array_name = $var_name;
1068 if ($l->{IS_VARYING}) {
1069 $length = "ndr_get_array_length($ndr, " . get_pointer_to($var_name) .")";
1072 $var_name = get_array_element($var_name, $counter);
1074 $self->ParseMemCtxPullStart($e, $l, $ndr, $array_name);
1076 if (($primitives and not $l->{IS_DEFERRED}) or ($deferred and $l->{IS_DEFERRED})) {
1077 my $nl = GetNextLevel($e,$l);
1079 if ($l->{IS_ZERO_TERMINATED}) {
1080 $self->CheckStringTerminator($ndr,$e,$l,$length);
1083 $self->pidl("for ($counter = 0; $counter < $length; $counter++) {");
1085 $self->ParseElementPullLevel($e, $nl, $ndr, $var_name, $env, 1, 0);
1090 if ($deferred and ContainsDeferred($e, $l)) {
1091 $self->pidl("for ($counter = 0; $counter < $length; $counter++) {");
1093 $self->ParseElementPullLevel($e,GetNextLevel($e,$l), $ndr, $var_name, $env, 0, 1);
1098 $self->ParseMemCtxPullEnd($e, $l, $ndr);
1100 } elsif ($l->{TYPE} eq "SWITCH") {
1101 $self->ParseElementPullLevel($e, GetNextLevel($e,$l), $ndr, $var_name, $env, $primitives, $deferred);
1105 #####################################################################
1106 # parse scalars in a structure element - pull size
1107 sub ParseElementPull($$$$$$)
1109 my($self,$e,$ndr,$env,$primitives,$deferred) = @_;
1111 my $var_name = $env->{$e->{NAME}};
1115 return unless $primitives or ($deferred and ContainsDeferred($e, $e->{LEVELS}[0]));
1117 if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
1120 $represent_name = $var_name;
1121 $transmit_name = "_transmit_$e->{NAME}";
1122 $var_name = $transmit_name;
1123 $self->pidl(mapTypeName($e->{TYPE})." $var_name;");
1126 $var_name = append_prefix($e, $var_name);
1128 $self->start_flags($e, $ndr);
1130 $self->ParseElementPullLevel($e,$e->{LEVELS}[0],$ndr,$var_name,$env,$primitives,$deferred);
1132 $self->end_flags($e, $ndr);
1134 # Representation type is different from transmit_as
1135 if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
1136 $self->pidl("NDR_CHECK(ndr_$e->{TYPE}_to_$e->{REPRESENTATION_TYPE}($transmit_name, ".get_pointer_to($represent_name)."));");
1142 #####################################################################
1143 # parse a pointer in a struct element or function
1144 sub ParsePtrPull($$$$$)
1146 my($self, $e,$l,$ndr,$var_name) = @_;
1148 my $nl = GetNextLevel($e, $l);
1149 my $next_is_array = ($nl->{TYPE} eq "ARRAY");
1150 my $next_is_string = (($nl->{TYPE} eq "DATA") and
1151 ($nl->{DATA_TYPE} eq "string"));
1153 if ($l->{POINTER_TYPE} eq "ref" and $l->{LEVEL} eq "TOP") {
1155 if (!$next_is_array and !$next_is_string) {
1156 $self->pidl("if ($ndr->flags & LIBNDR_FLAG_REF_ALLOC) {");
1157 $self->pidl("\tNDR_PULL_ALLOC($ndr, $var_name);");
1162 } elsif ($l->{POINTER_TYPE} eq "ref" and $l->{LEVEL} eq "EMBEDDED") {
1163 $self->pidl("NDR_CHECK(ndr_pull_ref_ptr($ndr, &_ptr_$e->{NAME}));");
1164 } elsif (($l->{POINTER_TYPE} eq "unique") or
1165 ($l->{POINTER_TYPE} eq "relative") or
1166 ($l->{POINTER_TYPE} eq "full")) {
1167 $self->pidl("NDR_CHECK(ndr_pull_generic_ptr($ndr, &_ptr_$e->{NAME}));");
1169 die("Unhandled pointer type $l->{POINTER_TYPE}");
1172 $self->pidl("if (_ptr_$e->{NAME}) {");
1175 # Don't do this for arrays, they're allocated at the actual level
1177 unless ($next_is_array or $next_is_string) {
1178 $self->pidl("NDR_PULL_ALLOC($ndr, $var_name);");
1180 # FIXME: Yes, this is nasty.
1181 # We allocate an array twice
1182 # - once just to indicate that it's there,
1183 # - then the real allocation...
1184 $self->pidl("NDR_PULL_ALLOC($ndr, $var_name);");
1187 #$self->pidl("memset($var_name, 0, sizeof($var_name));");
1188 if ($l->{POINTER_TYPE} eq "relative") {
1189 $self->pidl("NDR_CHECK(ndr_pull_relative_ptr1($ndr, $var_name, _ptr_$e->{NAME}));");
1192 $self->pidl("} else {");
1193 $self->pidl("\t$var_name = NULL;");
1197 sub ParseStructPushPrimitives($$$$$)
1199 my ($self, $struct, $ndr, $varname, $env) = @_;
1201 # see if the structure contains a conformant array. If it
1202 # does, then it must be the last element of the structure, and
1203 # we need to push the conformant length early, as it fits on
1204 # the wire before the structure (and even before the structure
1206 if (defined($struct->{SURROUNDING_ELEMENT})) {
1207 my $e = $struct->{SURROUNDING_ELEMENT};
1209 if (defined($e->{LEVELS}[0]) and
1210 $e->{LEVELS}[0]->{TYPE} eq "ARRAY") {
1213 if ($e->{LEVELS}[0]->{IS_ZERO_TERMINATED}) {
1214 if (has_property($e, "charset")) {
1215 $size = "ndr_charset_length($varname->$e->{NAME}, CH_$e->{PROPERTIES}->{charset})";
1217 $size = "ndr_string_length($varname->$e->{NAME}, sizeof(*$varname->$e->{NAME}))";
1220 $size = ParseExpr($e->{LEVELS}[0]->{SIZE_IS}, $env, $e->{ORIGINAL});
1223 $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, $size));");
1225 $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, ndr_string_array_size($ndr, $varname->$e->{NAME})));");
1229 $self->pidl("NDR_CHECK(ndr_push_align($ndr, $struct->{ALIGN}));");
1231 if (defined($struct->{PROPERTIES}{relative_base})) {
1232 # set the current offset as base for relative pointers
1233 # and store it based on the toplevel struct/union
1234 $self->pidl("NDR_CHECK(ndr_push_setup_relative_base_offset1($ndr, $varname, $ndr->offset));");
1237 $self->ParseElementPush($_, $ndr, $env, 1, 0) foreach (@{$struct->{ELEMENTS}});
1240 sub ParseStructPushDeferred($$$$)
1242 my ($self, $struct, $ndr, $varname, $env) = @_;
1243 if (defined($struct->{PROPERTIES}{relative_base})) {
1244 # retrieve the current offset as base for relative pointers
1245 # based on the toplevel struct/union
1246 $self->pidl("NDR_CHECK(ndr_push_setup_relative_base_offset2($ndr, $varname));");
1248 $self->ParseElementPush($_, $ndr, $env, 0, 1) foreach (@{$struct->{ELEMENTS}});
1251 #####################################################################
1253 sub ParseStructPush($$$$)
1255 my ($self, $struct, $ndr, $varname) = @_;
1257 return unless defined($struct->{ELEMENTS});
1259 my $env = GenerateStructEnv($struct, $varname);
1261 EnvSubstituteValue($env, $struct);
1263 $self->DeclareArrayVariablesNoZero($_, $env) foreach (@{$struct->{ELEMENTS}});
1265 $self->start_flags($struct, $ndr);
1267 $self->pidl("if (ndr_flags & NDR_SCALARS) {");
1269 $self->ParseStructPushPrimitives($struct, $ndr, $varname, $env);
1273 $self->pidl("if (ndr_flags & NDR_BUFFERS) {");
1275 $self->ParseStructPushDeferred($struct, $ndr, $varname, $env);
1279 $self->end_flags($struct, $ndr);
1282 #####################################################################
1283 # generate a push function for an enum
1284 sub ParseEnumPush($$$$)
1286 my($self,$enum,$ndr,$varname) = @_;
1287 my($type_fn) = $enum->{BASE_TYPE};
1289 $self->start_flags($enum, $ndr);
1290 $self->pidl("NDR_CHECK(ndr_push_$type_fn($ndr, NDR_SCALARS, $varname));");
1291 $self->end_flags($enum, $ndr);
1294 #####################################################################
1295 # generate a pull function for an enum
1296 sub ParseEnumPull($$$$)
1298 my($self,$enum,$ndr,$varname) = @_;
1299 my($type_fn) = $enum->{BASE_TYPE};
1300 my($type_v_decl) = mapTypeName($type_fn);
1302 $self->pidl("$type_v_decl v;");
1303 $self->start_flags($enum, $ndr);
1304 $self->pidl("NDR_CHECK(ndr_pull_$type_fn($ndr, NDR_SCALARS, &v));");
1305 $self->pidl("*$varname = v;");
1307 $self->end_flags($enum, $ndr);
1310 #####################################################################
1311 # generate a print function for an enum
1312 sub ParseEnumPrint($$$$$)
1314 my($self,$enum,$ndr,$name,$varname) = @_;
1316 $self->pidl("const char *val = NULL;");
1319 $self->start_flags($enum, $ndr);
1321 $self->pidl("switch ($varname) {");
1323 my $els = \@{$enum->{ELEMENTS}};
1324 foreach my $i (0 .. $#{$els}) {
1325 my $e = ${$els}[$i];
1327 if ($e =~ /^(.*)=/) {
1330 $self->pidl("case $e: val = \"$e\"; break;");
1336 $self->pidl("ndr_print_enum($ndr, name, \"$enum->{TYPE}\", val, $varname);");
1338 $self->end_flags($enum, $ndr);
1343 my ($e,$t,$name,$varname) = @_;
1344 return "enum $name " .
1345 ($t eq "pull"?"*":"") . $varname;
1348 $typefamily{ENUM} = {
1350 PUSH_FN_BODY => \&ParseEnumPush,
1351 PULL_FN_BODY => \&ParseEnumPull,
1352 PRINT_FN_BODY => \&ParseEnumPrint,
1355 #####################################################################
1356 # generate a push function for a bitmap
1357 sub ParseBitmapPush($$$$)
1359 my($self,$bitmap,$ndr,$varname) = @_;
1360 my($type_fn) = $bitmap->{BASE_TYPE};
1362 $self->start_flags($bitmap, $ndr);
1364 $self->pidl("NDR_CHECK(ndr_push_$type_fn($ndr, NDR_SCALARS, $varname));");
1366 $self->end_flags($bitmap, $ndr);
1369 #####################################################################
1370 # generate a pull function for an bitmap
1371 sub ParseBitmapPull($$$$)
1373 my($self,$bitmap,$ndr,$varname) = @_;
1374 my $type_fn = $bitmap->{BASE_TYPE};
1375 my($type_decl) = mapTypeName($bitmap->{BASE_TYPE});
1377 $self->pidl("$type_decl v;");
1378 $self->start_flags($bitmap, $ndr);
1379 $self->pidl("NDR_CHECK(ndr_pull_$type_fn($ndr, NDR_SCALARS, &v));");
1380 $self->pidl("*$varname = v;");
1382 $self->end_flags($bitmap, $ndr);
1385 #####################################################################
1386 # generate a print function for an bitmap
1387 sub ParseBitmapPrintElement($$$$$$)
1389 my($self,$e,$bitmap,$ndr,$name,$varname) = @_;
1390 my($type_decl) = mapTypeName($bitmap->{BASE_TYPE});
1391 my($type_fn) = $bitmap->{BASE_TYPE};
1394 if ($e =~ /^(\w+) .*$/) {
1397 die "Bitmap: \"$name\" invalid Flag: \"$e\"";
1400 $self->pidl("ndr_print_bitmap_flag($ndr, sizeof($type_decl), \"$flag\", $flag, $varname);");
1403 #####################################################################
1404 # generate a print function for an bitmap
1405 sub ParseBitmapPrint($$$$$)
1407 my($self,$bitmap,$ndr,$name,$varname) = @_;
1408 my($type_decl) = mapTypeName($bitmap->{TYPE});
1409 my($type_fn) = $bitmap->{BASE_TYPE};
1411 $self->start_flags($bitmap, $ndr);
1413 $self->pidl("ndr_print_$type_fn($ndr, name, $varname);");
1415 $self->pidl("$ndr->depth++;");
1416 foreach my $e (@{$bitmap->{ELEMENTS}}) {
1417 $self->ParseBitmapPrintElement($e, $bitmap, $ndr, $name, $varname);
1419 $self->pidl("$ndr->depth--;");
1421 $self->end_flags($bitmap, $ndr);
1424 sub DeclBitmap($$$$)
1426 my ($e,$t,$name,$varname) = @_;
1427 return mapTypeName(Parse::Pidl::Typelist::bitmap_type_fn($e)) .
1428 ($t eq "pull"?" *":" ") . $varname;
1431 $typefamily{BITMAP} = {
1432 DECL => \&DeclBitmap,
1433 PUSH_FN_BODY => \&ParseBitmapPush,
1434 PULL_FN_BODY => \&ParseBitmapPull,
1435 PRINT_FN_BODY => \&ParseBitmapPrint,
1438 #####################################################################
1439 # generate a struct print function
1440 sub ParseStructPrint($$$$$)
1442 my($self,$struct,$ndr,$name,$varname) = @_;
1444 return unless defined $struct->{ELEMENTS};
1446 my $env = GenerateStructEnv($struct, $varname);
1448 $self->DeclareArrayVariables($_) foreach (@{$struct->{ELEMENTS}});
1450 $self->pidl("ndr_print_struct($ndr, name, \"$name\");");
1452 $self->start_flags($struct, $ndr);
1454 $self->pidl("$ndr->depth++;");
1456 $self->ParseElementPrint($_, $ndr, $env->{$_->{NAME}}, $env)
1457 foreach (@{$struct->{ELEMENTS}});
1458 $self->pidl("$ndr->depth--;");
1460 $self->end_flags($struct, $ndr);
1463 sub DeclarePtrVariables($$)
1466 foreach my $l (@{$e->{LEVELS}}) {
1467 if ($l->{TYPE} eq "POINTER" and
1468 not ($l->{POINTER_TYPE} eq "ref" and $l->{LEVEL} eq "TOP")) {
1469 $self->pidl("uint32_t _ptr_$e->{NAME};");
1475 sub DeclareArrayVariables($$)
1479 foreach my $l (@{$e->{LEVELS}}) {
1480 next if has_fast_array($e,$l);
1481 next if is_charset_array($e,$l);
1482 if ($l->{TYPE} eq "ARRAY") {
1483 $self->pidl("uint32_t cntr_$e->{NAME}_$l->{LEVEL_INDEX};");
1488 sub DeclareArrayVariablesNoZero($$$)
1490 my ($self,$e,$env) = @_;
1492 foreach my $l (@{$e->{LEVELS}}) {
1493 next if has_fast_array($e,$l);
1494 next if is_charset_array($e,$l);
1495 if ($l->{TYPE} eq "ARRAY") {
1496 my $length = ParseExpr($l->{LENGTH_IS}, $env, $e->{ORIGINAL});
1497 if ($length eq "0") {
1498 warning($e->{ORIGINAL}, "pointless array cntr: 'cntr_$e->{NAME}_$l->{LEVEL_INDEX}': length=$length");
1500 $self->pidl("uint32_t cntr_$e->{NAME}_$l->{LEVEL_INDEX};");
1506 sub DeclareMemCtxVariables($$)
1509 foreach my $l (@{$e->{LEVELS}}) {
1510 my $mem_flags = $self->ParseMemCtxPullFlags($e, $l);
1511 if (defined($mem_flags)) {
1512 $self->pidl("TALLOC_CTX *_mem_save_$e->{NAME}_$l->{LEVEL_INDEX};");
1517 sub ParseStructPullPrimitives($$$$$)
1519 my($self,$struct,$ndr,$varname,$env) = @_;
1521 if (defined $struct->{SURROUNDING_ELEMENT}) {
1522 $self->pidl("NDR_CHECK(ndr_pull_array_size($ndr, &$varname->$struct->{SURROUNDING_ELEMENT}->{NAME}));");
1525 $self->pidl("NDR_CHECK(ndr_pull_align($ndr, $struct->{ALIGN}));");
1527 if (defined($struct->{PROPERTIES}{relative_base})) {
1528 # set the current offset as base for relative pointers
1529 # and store it based on the toplevel struct/union
1530 $self->pidl("NDR_CHECK(ndr_pull_setup_relative_base_offset1($ndr, $varname, $ndr->offset));");
1533 $self->ParseElementPull($_, $ndr, $env, 1, 0) foreach (@{$struct->{ELEMENTS}});
1535 $self->add_deferred();
1538 sub ParseStructPullDeferred($$$$$)
1540 my ($self,$struct,$ndr,$varname,$env) = @_;
1542 if (defined($struct->{PROPERTIES}{relative_base})) {
1543 # retrieve the current offset as base for relative pointers
1544 # based on the toplevel struct/union
1545 $self->pidl("NDR_CHECK(ndr_pull_setup_relative_base_offset2($ndr, $varname));");
1547 foreach my $e (@{$struct->{ELEMENTS}}) {
1548 $self->ParseElementPull($e, $ndr, $env, 0, 1);
1551 $self->add_deferred();
1554 #####################################################################
1555 # parse a struct - pull side
1556 sub ParseStructPull($$$$)
1558 my($self,$struct,$ndr,$varname) = @_;
1560 return unless defined $struct->{ELEMENTS};
1562 # declare any internal pointers we need
1563 foreach my $e (@{$struct->{ELEMENTS}}) {
1564 $self->DeclarePtrVariables($e);
1565 $self->DeclareArrayVariables($e);
1566 $self->DeclareMemCtxVariables($e);
1569 $self->start_flags($struct, $ndr);
1571 my $env = GenerateStructEnv($struct, $varname);
1573 $self->pidl("if (ndr_flags & NDR_SCALARS) {");
1575 $self->ParseStructPullPrimitives($struct,$ndr,$varname,$env);
1578 $self->pidl("if (ndr_flags & NDR_BUFFERS) {");
1580 $self->ParseStructPullDeferred($struct,$ndr,$varname,$env);
1584 $self->end_flags($struct, $ndr);
1587 #####################################################################
1588 # calculate size of ndr struct
1589 sub ParseStructNdrSize($$$$)
1591 my ($self,$t, $name, $varname) = @_;
1594 if (my $flags = has_property($t, "flag")) {
1595 $self->pidl("flags |= $flags;");
1597 $self->pidl("return ndr_size_struct($varname, flags, (ndr_push_flags_fn_t)ndr_push_$name, ic);");
1600 sub DeclStruct($$$$)
1602 my ($e,$t,$name,$varname) = @_;
1603 return ($t ne "pull"?"const ":"") . "struct $name *$varname";
1606 sub ArgsStructNdrSize($$$)
1608 my ($d, $name, $varname) = @_;
1609 return "const struct $name *$varname, struct smb_iconv_convenience *ic, int flags";
1612 $typefamily{STRUCT} = {
1613 PUSH_FN_BODY => \&ParseStructPush,
1614 DECL => \&DeclStruct,
1615 PULL_FN_BODY => \&ParseStructPull,
1616 PRINT_FN_BODY => \&ParseStructPrint,
1617 SIZE_FN_BODY => \&ParseStructNdrSize,
1618 SIZE_FN_ARGS => \&ArgsStructNdrSize,
1621 #####################################################################
1622 # calculate size of ndr struct
1623 sub ParseUnionNdrSize($$$)
1625 my ($self, $t, $name, $varname) = @_;
1628 if (my $flags = has_property($t, "flag")) {
1629 $self->pidl("flags |= $flags;");
1632 $self->pidl("return ndr_size_union($varname, flags, level, (ndr_push_flags_fn_t)ndr_push_$name, ic);");
1635 sub ParseUnionPushPrimitives($$$$)
1637 my ($self, $e, $ndr ,$varname) = @_;
1639 my $have_default = 0;
1641 $self->pidl("int level = ndr_push_get_switch_value($ndr, $varname);");
1643 if (defined($e->{SWITCH_TYPE})) {
1644 $self->pidl("NDR_CHECK(ndr_push_$e->{SWITCH_TYPE}($ndr, NDR_SCALARS, level));");
1647 $self->pidl("switch (level) {");
1649 foreach my $el (@{$e->{ELEMENTS}}) {
1650 if ($el->{CASE} eq "default") {
1653 $self->pidl("$el->{CASE}: {");
1655 if ($el->{TYPE} ne "EMPTY") {
1657 if (defined($e->{PROPERTIES}{relative_base})) {
1658 $self->pidl("NDR_CHECK(ndr_push_align($ndr, $el->{ALIGN}));");
1659 # set the current offset as base for relative pointers
1660 # and store it based on the toplevel struct/union
1661 $self->pidl("NDR_CHECK(ndr_push_setup_relative_base_offset1($ndr, $varname, $ndr->offset));");
1663 $self->DeclareArrayVariables($el);
1664 $self->ParseElementPush($el, $ndr, {$el->{NAME} => "$varname->$el->{NAME}"}, 1, 0);
1667 $self->pidl("break; }");
1670 if (! $have_default) {
1671 $self->pidl("default:");
1672 $self->pidl("\treturn ndr_push_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u at \%s\", level, __location__);");
1678 sub ParseUnionPushDeferred($$$$)
1680 my ($self,$e,$ndr,$varname) = @_;
1682 my $have_default = 0;
1684 $self->pidl("int level = ndr_push_get_switch_value($ndr, $varname);");
1685 if (defined($e->{PROPERTIES}{relative_base})) {
1686 # retrieve the current offset as base for relative pointers
1687 # based on the toplevel struct/union
1688 $self->pidl("NDR_CHECK(ndr_push_setup_relative_base_offset2($ndr, $varname));");
1690 $self->pidl("switch (level) {");
1692 foreach my $el (@{$e->{ELEMENTS}}) {
1693 if ($el->{CASE} eq "default") {
1697 $self->pidl("$el->{CASE}:");
1698 if ($el->{TYPE} ne "EMPTY") {
1700 $self->ParseElementPush($el, $ndr, {$el->{NAME} => "$varname->$el->{NAME}"}, 0, 1);
1703 $self->pidl("break;");
1706 if (! $have_default) {
1707 $self->pidl("default:");
1708 $self->pidl("\treturn ndr_push_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u at \%s\", level, __location__);");
1714 #####################################################################
1715 # parse a union - push side
1716 sub ParseUnionPush($$$$)
1718 my ($self,$e,$ndr,$varname) = @_;
1719 my $have_default = 0;
1721 $self->start_flags($e, $ndr);
1723 $self->pidl("if (ndr_flags & NDR_SCALARS) {");
1725 $self->ParseUnionPushPrimitives($e, $ndr, $varname);
1728 $self->pidl("if (ndr_flags & NDR_BUFFERS) {");
1730 $self->ParseUnionPushDeferred($e, $ndr, $varname);
1733 $self->end_flags($e, $ndr);
1736 #####################################################################
1738 sub ParseUnionPrint($$$$$)
1740 my ($self,$e,$ndr,$name,$varname) = @_;
1741 my $have_default = 0;
1743 $self->pidl("int level;");
1744 foreach my $el (@{$e->{ELEMENTS}}) {
1745 $self->DeclareArrayVariables($el);
1748 $self->start_flags($e, $ndr);
1750 $self->pidl("level = ndr_print_get_switch_value($ndr, $varname);");
1752 $self->pidl("ndr_print_union($ndr, name, level, \"$name\");");
1754 $self->pidl("switch (level) {");
1756 foreach my $el (@{$e->{ELEMENTS}}) {
1757 if ($el->{CASE} eq "default") {
1760 $self->pidl("$el->{CASE}:");
1761 if ($el->{TYPE} ne "EMPTY") {
1763 $self->ParseElementPrint($el, $ndr, "$varname->$el->{NAME}", {});
1766 $self->pidl("break;");
1769 if (! $have_default) {
1770 $self->pidl("default:");
1771 $self->pidl("\tndr_print_bad_level($ndr, name, level);");
1776 $self->end_flags($e, $ndr);
1779 sub ParseUnionPullPrimitives($$$$$)
1781 my ($self,$e,$ndr,$varname,$switch_type) = @_;
1782 my $have_default = 0;
1784 if (defined($switch_type)) {
1785 $self->pidl("NDR_CHECK(ndr_pull_$switch_type($ndr, NDR_SCALARS, &_level));");
1786 $self->pidl("if (_level != level) {");
1787 $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value %u for $varname at \%s\", _level, __location__);");
1791 $self->pidl("switch (level) {");
1793 foreach my $el (@{$e->{ELEMENTS}}) {
1794 if ($el->{CASE} eq "default") {
1797 $self->pidl("$el->{CASE}: {");
1799 if ($el->{TYPE} ne "EMPTY") {
1801 $self->DeclarePtrVariables($el);
1802 $self->DeclareArrayVariables($el);
1803 if (defined($e->{PROPERTIES}{relative_base})) {
1804 $self->pidl("NDR_CHECK(ndr_pull_align($ndr, $el->{ALIGN}));");
1805 # set the current offset as base for relative pointers
1806 # and store it based on the toplevel struct/union
1807 $self->pidl("NDR_CHECK(ndr_pull_setup_relative_base_offset1($ndr, $varname, $ndr->offset));");
1809 $self->ParseElementPull($el, $ndr, {$el->{NAME} => "$varname->$el->{NAME}"}, 1, 0);
1812 $self->pidl("break; }");
1815 if (! $have_default) {
1816 $self->pidl("default:");
1817 $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u at \%s\", level, __location__);");
1823 sub ParseUnionPullDeferred($$$$)
1825 my ($self,$e,$ndr,$varname) = @_;
1826 my $have_default = 0;
1828 if (defined($e->{PROPERTIES}{relative_base})) {
1829 # retrieve the current offset as base for relative pointers
1830 # based on the toplevel struct/union
1831 $self->pidl("NDR_CHECK(ndr_pull_setup_relative_base_offset2($ndr, $varname));");
1833 $self->pidl("switch (level) {");
1835 foreach my $el (@{$e->{ELEMENTS}}) {
1836 if ($el->{CASE} eq "default") {
1840 $self->pidl("$el->{CASE}:");
1841 if ($el->{TYPE} ne "EMPTY") {
1843 $self->ParseElementPull($el, $ndr, {$el->{NAME} => "$varname->$el->{NAME}"}, 0, 1);
1846 $self->pidl("break;");
1849 if (! $have_default) {
1850 $self->pidl("default:");
1851 $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u at \%s\", level, __location__);");
1859 #####################################################################
1860 # parse a union - pull side
1861 sub ParseUnionPull($$$$)
1863 my ($self,$e,$ndr,$varname) = @_;
1864 my $switch_type = $e->{SWITCH_TYPE};
1866 $self->pidl("int level;");
1867 if (defined($switch_type)) {
1868 if (Parse::Pidl::Typelist::typeIs($switch_type, "ENUM")) {
1869 $switch_type = Parse::Pidl::Typelist::enum_type_fn(getType($switch_type)->{DATA});
1871 $self->pidl(mapTypeName($switch_type) . " _level;");
1874 my %double_cases = ();
1875 foreach my $el (@{$e->{ELEMENTS}}) {
1876 next if ($el->{TYPE} eq "EMPTY");
1877 next if ($double_cases{"$el->{NAME}"});
1878 $self->DeclareMemCtxVariables($el);
1879 $double_cases{"$el->{NAME}"} = 1;
1882 $self->start_flags($e, $ndr);
1884 $self->pidl("level = ndr_pull_get_switch_value($ndr, $varname);");
1886 $self->pidl("if (ndr_flags & NDR_SCALARS) {");
1888 $self->ParseUnionPullPrimitives($e,$ndr,$varname,$switch_type);
1892 $self->pidl("if (ndr_flags & NDR_BUFFERS) {");
1894 $self->ParseUnionPullDeferred($e,$ndr,$varname);
1898 $self->add_deferred();
1900 $self->end_flags($e, $ndr);
1905 my ($e,$t,$name,$varname) = @_;
1906 return ($t ne "pull"?"const ":"") . "union $name *$varname";
1909 sub ArgsUnionNdrSize($$)
1912 return "const union $name *r, uint32_t level, struct smb_iconv_convenience *ic, int flags";
1915 $typefamily{UNION} = {
1916 PUSH_FN_BODY => \&ParseUnionPush,
1917 DECL => \&DeclUnion,
1918 PULL_FN_BODY => \&ParseUnionPull,
1919 PRINT_FN_BODY => \&ParseUnionPrint,
1920 SIZE_FN_ARGS => \&ArgsUnionNdrSize,
1921 SIZE_FN_BODY => \&ParseUnionNdrSize,
1924 #####################################################################
1925 # parse a typedef - push side
1926 sub ParseTypedefPush($$$$)
1928 my($self,$e,$ndr,$varname) = @_;
1932 $env->{$e->{NAME}} = $varname;
1934 $self->ParseElementPushLevel($e, $e->{LEVELS}[0], $ndr, $varname, $env, 1, 1);
1937 #####################################################################
1938 # parse a typedef - pull side
1939 sub ParseTypedefPull($$$$)
1941 my($self,$e,$ndr,$varname) = @_;
1945 $env->{$e->{NAME}} = $varname;
1947 $self->ParseElementPullLevel($e, $e->{LEVELS}[0], $ndr, $varname, $env, 1, 1);
1950 #####################################################################
1951 # parse a typedef - print side
1952 sub ParseTypedefPrint($$$$$)
1954 my($self,$e,$ndr,$name,$varname) = @_;
1956 $typefamily{$e->{DATA}->{TYPE}}->{PRINT_FN_BODY}->($self, $e->{DATA}, $ndr, $name, $varname);
1959 #####################################################################
1960 ## calculate the size of a structure
1961 sub ParseTypedefNdrSize($$$$)
1963 my($self,$t,$name,$varname) = @_;
1965 $typefamily{$t->{DATA}->{TYPE}}->{SIZE_FN_BODY}->($self, $t->{DATA}, $name, $varname);
1968 sub DeclTypedef($$$$)
1970 my ($e, $t, $name, $varname) = @_;
1972 return $typefamily{$e->{DATA}->{TYPE}}->{DECL}->($e->{DATA}, $t, $name, $varname);
1975 sub ArgsTypedefNdrSize($$$)
1977 my ($d, $name, $varname) = @_;
1978 return $typefamily{$d->{DATA}->{TYPE}}->{SIZE_FN_ARGS}->($d->{DATA}, $name, $varname);
1981 $typefamily{TYPEDEF} = {
1982 PUSH_FN_BODY => \&ParseTypedefPush,
1983 DECL => \&DeclTypedef,
1984 PULL_FN_BODY => \&ParseTypedefPull,
1985 PRINT_FN_BODY => \&ParseTypedefPrint,
1986 SIZE_FN_ARGS => \&ArgsTypedefNdrSize,
1987 SIZE_FN_BODY => \&ParseTypedefNdrSize,
1990 #####################################################################
1991 # parse a function - print side
1992 sub ParseFunctionPrint($$)
1994 my($self, $fn) = @_;
1997 $self->pidl_hdr("void ndr_print_$fn->{NAME}(struct ndr_print *$ndr, const char *name, int flags, const struct $fn->{NAME} *r);");
1999 return if has_property($fn, "noprint");
2001 $self->pidl("_PUBLIC_ void ndr_print_$fn->{NAME}(struct ndr_print *$ndr, const char *name, int flags, const struct $fn->{NAME} *r)");
2005 foreach my $e (@{$fn->{ELEMENTS}}) {
2006 $self->DeclareArrayVariables($e);
2009 $self->pidl("ndr_print_struct($ndr, name, \"$fn->{NAME}\");");
2010 $self->pidl("$ndr->depth++;");
2012 $self->pidl("if (flags & NDR_SET_VALUES) {");
2013 $self->pidl("\t$ndr->flags |= LIBNDR_PRINT_SET_VALUES;");
2016 $self->pidl("if (flags & NDR_IN) {");
2018 $self->pidl("ndr_print_struct($ndr, \"in\", \"$fn->{NAME}\");");
2019 $self->pidl("$ndr->depth++;");
2021 my $env = GenerateFunctionInEnv($fn);
2023 foreach my $e (@{$fn->{ELEMENTS}}) {
2024 if (grep(/in/,@{$e->{DIRECTION}})) {
2025 $self->ParseElementPrint($e, $ndr, $env->{$e->{NAME}}, $env);
2028 $self->pidl("$ndr->depth--;");
2032 $self->pidl("if (flags & NDR_OUT) {");
2034 $self->pidl("ndr_print_struct($ndr, \"out\", \"$fn->{NAME}\");");
2035 $self->pidl("$ndr->depth++;");
2037 $env = GenerateFunctionOutEnv($fn);
2038 foreach my $e (@{$fn->{ELEMENTS}}) {
2039 if (grep(/out/,@{$e->{DIRECTION}})) {
2040 $self->ParseElementPrint($e, $ndr, $env->{$e->{NAME}}, $env);
2043 if ($fn->{RETURN_TYPE}) {
2044 $self->pidl("ndr_print_$fn->{RETURN_TYPE}($ndr, \"result\", r->out.result);");
2046 $self->pidl("$ndr->depth--;");
2050 $self->pidl("$ndr->depth--;");
2056 #####################################################################
2058 sub ParseFunctionPush($$)
2060 my($self, $fn) = @_;
2063 $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;
2065 return if has_property($fn, "nopush");
2070 foreach my $e (@{$fn->{ELEMENTS}}) {
2071 $self->DeclareArrayVariables($e);
2074 $self->pidl("if (flags & NDR_IN) {");
2077 my $env = GenerateFunctionInEnv($fn);
2079 EnvSubstituteValue($env, $fn);
2081 foreach my $e (@{$fn->{ELEMENTS}}) {
2082 if (grep(/in/,@{$e->{DIRECTION}})) {
2083 $self->ParseElementPush($e, $ndr, $env, 1, 1);
2090 $self->pidl("if (flags & NDR_OUT) {");
2093 $env = GenerateFunctionOutEnv($fn);
2094 foreach my $e (@{$fn->{ELEMENTS}}) {
2095 if (grep(/out/,@{$e->{DIRECTION}})) {
2096 $self->ParseElementPush($e, $ndr, $env, 1, 1);
2100 if ($fn->{RETURN_TYPE}) {
2101 $self->pidl("NDR_CHECK(ndr_push_$fn->{RETURN_TYPE}($ndr, NDR_SCALARS, r->out.result));");
2106 $self->pidl("return NDR_ERR_SUCCESS;");
2112 sub AllocateArrayLevel($$$$$$)
2114 my ($self,$e,$l,$ndr,$var,$size) = @_;
2116 my $pl = GetPrevLevel($e, $l);
2117 if (defined($pl) and
2118 $pl->{TYPE} eq "POINTER" and
2119 $pl->{POINTER_TYPE} eq "ref"
2120 and not $l->{IS_ZERO_TERMINATED}) {
2121 $self->pidl("if ($ndr->flags & LIBNDR_FLAG_REF_ALLOC) {");
2122 $self->pidl("\tNDR_PULL_ALLOC_N($ndr, $var, $size);");
2124 if (grep(/in/,@{$e->{DIRECTION}}) and
2125 grep(/out/,@{$e->{DIRECTION}})) {
2126 $self->pidl("memcpy(r->out.$e->{NAME}, r->in.$e->{NAME}, $size * sizeof(*r->in.$e->{NAME}));");
2131 $self->pidl("NDR_PULL_ALLOC_N($ndr, $var, $size);");
2134 #####################################################################
2136 sub ParseFunctionPull($$)
2141 # pull function args
2142 $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;
2147 # declare any internal pointers we need
2148 foreach my $e (@{$fn->{ELEMENTS}}) {
2149 $self->DeclarePtrVariables($e);
2150 $self->DeclareArrayVariables($e);
2153 my %double_cases = ();
2154 foreach my $e (@{$fn->{ELEMENTS}}) {
2155 next if ($e->{TYPE} eq "EMPTY");
2156 next if ($double_cases{"$e->{NAME}"});
2157 $self->DeclareMemCtxVariables($e);
2158 $double_cases{"$e->{NAME}"} = 1;
2161 $self->pidl("if (flags & NDR_IN) {");
2164 # auto-init the out section of a structure. I originally argued that
2165 # this was a bad idea as it hides bugs, but coping correctly
2166 # with initialisation and not wiping ref vars is turning
2167 # out to be too tricky (tridge)
2168 foreach my $e (@{$fn->{ELEMENTS}}) {
2169 next unless grep(/out/, @{$e->{DIRECTION}});
2170 $self->pidl("ZERO_STRUCT(r->out);");
2175 my $env = GenerateFunctionInEnv($fn);
2177 foreach my $e (@{$fn->{ELEMENTS}}) {
2178 next unless (grep(/in/, @{$e->{DIRECTION}}));
2179 $self->ParseElementPull($e, $ndr, $env, 1, 1);
2182 # allocate the "simple" out ref variables. FIXME: Shouldn't this have it's
2183 # own flag rather than be in NDR_IN ?
2185 foreach my $e (@{$fn->{ELEMENTS}}) {
2186 next unless (grep(/out/, @{$e->{DIRECTION}}));
2187 next unless ($e->{LEVELS}[0]->{TYPE} eq "POINTER" and
2188 $e->{LEVELS}[0]->{POINTER_TYPE} eq "ref");
2189 next if (($e->{LEVELS}[1]->{TYPE} eq "DATA") and
2190 ($e->{LEVELS}[1]->{DATA_TYPE} eq "string"));
2191 next if (($e->{LEVELS}[1]->{TYPE} eq "ARRAY")
2192 and $e->{LEVELS}[1]->{IS_ZERO_TERMINATED});
2194 if ($e->{LEVELS}[1]->{TYPE} eq "ARRAY") {
2195 my $size = ParseExprExt($e->{LEVELS}[1]->{SIZE_IS}, $env, $e->{ORIGINAL},
2196 check_null_pointer($e, $env, sub { $self->pidl(shift); },
2197 "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for size_is()\");"),
2198 check_fully_dereferenced($e, $env));
2199 $self->pidl("NDR_PULL_ALLOC_N($ndr, r->out.$e->{NAME}, $size);");
2201 if (grep(/in/, @{$e->{DIRECTION}})) {
2202 $self->pidl("memcpy(r->out.$e->{NAME}, r->in.$e->{NAME}, ($size) * sizeof(*r->in.$e->{NAME}));");
2204 $self->pidl("memset(r->out.$e->{NAME}, 0, ($size) * sizeof(*r->out.$e->{NAME}));");
2207 $self->pidl("NDR_PULL_ALLOC($ndr, r->out.$e->{NAME});");
2209 if (grep(/in/, @{$e->{DIRECTION}})) {
2210 $self->pidl("*r->out.$e->{NAME} = *r->in.$e->{NAME};");
2212 $self->pidl("ZERO_STRUCTP(r->out.$e->{NAME});");
2217 $self->add_deferred();
2221 $self->pidl("if (flags & NDR_OUT) {");
2224 $env = GenerateFunctionOutEnv($fn);
2225 foreach my $e (@{$fn->{ELEMENTS}}) {
2226 next unless grep(/out/, @{$e->{DIRECTION}});
2227 $self->ParseElementPull($e, $ndr, $env, 1, 1);
2230 if ($fn->{RETURN_TYPE}) {
2231 $self->pidl("NDR_CHECK(ndr_pull_$fn->{RETURN_TYPE}($ndr, NDR_SCALARS, &r->out.result));");
2234 $self->add_deferred();
2238 $self->pidl("return NDR_ERR_SUCCESS;");
2244 sub AuthServiceStruct($$$)
2246 my ($self, $ifacename, $authservice) = @_;
2247 my @a = split /,/, $authservice;
2248 my $authservice_count = $#a + 1;
2250 $self->pidl("static const char * const $ifacename\_authservice_strings[] = {");
2251 foreach my $ap (@a) {
2252 $self->pidl("\t$ap, ");
2257 $self->pidl("static const struct ndr_interface_string_array $ifacename\_authservices = {");
2258 $self->pidl("\t.count\t= $authservice_count,");
2259 $self->pidl("\t.names\t= $ifacename\_authservice_strings");
2264 sub FunctionCallEntry($$)
2266 my ($self, $d) = @_;
2267 return 0 if not defined($d->{OPNUM});
2269 $self->pidl("\t\t\"$d->{NAME}\",");
2270 $self->pidl("\t\tsizeof(struct $d->{NAME}),");
2271 $self->pidl("\t\t(ndr_push_flags_fn_t) ndr_push_$d->{NAME},");
2272 $self->pidl("\t\t(ndr_pull_flags_fn_t) ndr_pull_$d->{NAME},");
2273 $self->pidl("\t\t(ndr_print_function_t) ndr_print_$d->{NAME},");
2274 $self->pidl("\t\t".($d->{ASYNC}?"true":"false").",");
2275 $self->pidl("\t},");
2279 #####################################################################
2280 # produce a function call table
2281 sub FunctionTable($$)
2283 my($self,$interface) = @_;
2285 my $uname = uc $interface->{NAME};
2287 return if ($#{$interface->{FUNCTIONS}}+1 == 0);
2288 return unless defined ($interface->{PROPERTIES}->{uuid});
2290 $self->pidl("static const struct ndr_interface_call $interface->{NAME}\_calls[] = {");
2292 foreach my $d (@{$interface->{INHERITED_FUNCTIONS}},@{$interface->{FUNCTIONS}}) {
2293 $count += $self->FunctionCallEntry($d);
2295 $self->pidl("\t{ NULL, 0, NULL, NULL, NULL, false }");
2299 $self->pidl("static const char * const $interface->{NAME}\_endpoint_strings[] = {");
2300 foreach my $ep (@{$interface->{ENDPOINTS}}) {
2301 $self->pidl("\t$ep, ");
2303 my $endpoint_count = $#{$interface->{ENDPOINTS}}+1;
2308 $self->pidl("static const struct ndr_interface_string_array $interface->{NAME}\_endpoints = {");
2309 $self->pidl("\t.count\t= $endpoint_count,");
2310 $self->pidl("\t.names\t= $interface->{NAME}\_endpoint_strings");
2314 if (! defined $interface->{PROPERTIES}->{authservice}) {
2315 $interface->{PROPERTIES}->{authservice} = "\"host\"";
2318 $self->AuthServiceStruct($interface->{NAME},
2319 $interface->{PROPERTIES}->{authservice});
2321 $self->pidl("\nconst struct ndr_interface_table ndr_table_$interface->{NAME} = {");
2322 $self->pidl("\t.name\t\t= \"$interface->{NAME}\",");
2323 $self->pidl("\t.syntax_id\t= {");
2324 $self->pidl("\t\t" . print_uuid($interface->{UUID}) .",");
2325 $self->pidl("\t\tNDR_$uname\_VERSION");
2326 $self->pidl("\t},");
2327 $self->pidl("\t.helpstring\t= NDR_$uname\_HELPSTRING,");
2328 $self->pidl("\t.num_calls\t= $count,");
2329 $self->pidl("\t.calls\t\t= $interface->{NAME}\_calls,");
2330 $self->pidl("\t.endpoints\t= &$interface->{NAME}\_endpoints,");
2331 $self->pidl("\t.authservices\t= &$interface->{NAME}\_authservices");
2337 #####################################################################
2338 # generate include statements for imported idl files
2343 foreach (@imports) {
2344 $_ = unmake_str($_);
2346 $self->pidl(choose_header("librpc/gen_ndr/ndr_$_\.h", "gen_ndr/ndr_$_.h"));
2350 #####################################################################
2351 # generate include statements for included header files
2356 foreach (@includes) {
2357 $self->pidl_hdr("#include $_");
2361 #####################################################################
2362 # generate prototypes and defines for the interface definitions
2363 # FIXME: these prototypes are for the DCE/RPC client functions, not the
2364 # NDR parser and so do not belong here, technically speaking
2365 sub HeaderInterface($$$)
2367 my($self,$interface,$needed) = @_;
2371 if ($needed->{"compression"}) {
2372 $self->pidl(choose_header("librpc/ndr/ndr_compression.h", "ndr/compression.h"));
2375 if (has_property($interface, "object")) {
2376 $self->pidl(choose_header("librpc/gen_ndr/ndr_orpc.h", "ndr/orpc.h"));
2379 if (defined $interface->{PROPERTIES}->{helper}) {
2380 $self->HeaderInclude(split /,/, $interface->{PROPERTIES}->{helper});
2383 if (defined $interface->{PROPERTIES}->{uuid}) {
2384 my $name = uc $interface->{NAME};
2385 $self->pidl_hdr("#define NDR_$name\_UUID " .
2386 Parse::Pidl::Util::make_str(lc($interface->{PROPERTIES}->{uuid})));
2388 if(!defined $interface->{PROPERTIES}->{version}) { $interface->{PROPERTIES}->{version} = "0.0"; }
2389 $self->pidl_hdr("#define NDR_$name\_VERSION $interface->{PROPERTIES}->{version}");
2391 $self->pidl_hdr("#define NDR_$name\_NAME \"$interface->{NAME}\"");
2393 if(!defined $interface->{PROPERTIES}->{helpstring}) { $interface->{PROPERTIES}->{helpstring} = "NULL"; }
2394 $self->pidl_hdr("#define NDR_$name\_HELPSTRING $interface->{PROPERTIES}->{helpstring}");
2396 $self->pidl_hdr("extern const struct ndr_interface_table ndr_table_$interface->{NAME};");
2399 foreach (@{$interface->{FUNCTIONS}}) {
2400 next if has_property($_, "noopnum");
2401 next if grep(/^$_->{NAME}$/,@{$interface->{INHERITED_FUNCTIONS}});
2402 my $u_name = uc $_->{NAME};
2404 my $val = sprintf("0x%02x", $count);
2405 if (defined($interface->{BASE})) {
2406 $val .= " + NDR_" . uc $interface->{BASE} . "_CALL_COUNT";
2409 $self->pidl_hdr("#define NDR_$u_name ($val)");
2411 $self->pidl_hdr("");
2417 if (defined($interface->{BASE})) {
2418 $val .= " + NDR_" . uc $interface->{BASE} . "_CALL_COUNT";
2421 $self->pidl_hdr("#define NDR_" . uc $interface->{NAME} . "_CALL_COUNT ($val)");
2425 sub ParseTypePush($$$$$$)
2427 my ($self,$e, $ndr, $varname, $primitives, $deferred) = @_;
2429 # save the old relative_base_offset
2430 $self->pidl("uint32_t _save_relative_base_offset = ndr_push_get_relative_base_offset($ndr);") if defined(has_property($e, "relative_base"));
2431 $typefamily{$e->{TYPE}}->{PUSH_FN_BODY}->($self, $e, $ndr, $varname);
2432 # restore the old relative_base_offset
2433 $self->pidl("ndr_push_restore_relative_base_offset($ndr, _save_relative_base_offset);") if defined(has_property($e, "relative_base"));
2436 sub ParseTypePushFunction($$$)
2438 my ($self, $e, $varname) = @_;
2441 my $args = $typefamily{$e->{TYPE}}->{DECL}->($e, "push", $e->{NAME}, $varname);
2443 $self->fn_declare("push", $e, "enum ndr_err_code ".TypeFunctionName("ndr_push", $e)."(struct ndr_push *$ndr, int ndr_flags, $args)") or return;
2447 $self->ParseTypePush($e, $ndr, $varname, 1, 1);
2448 $self->pidl("return NDR_ERR_SUCCESS;");
2454 sub ParseTypePull($$$$$$)
2456 my ($self, $e, $ndr, $varname, $primitives, $deferred) = @_;
2458 # save the old relative_base_offset
2459 $self->pidl("uint32_t _save_relative_base_offset = ndr_pull_get_relative_base_offset($ndr);") if defined(has_property($e, "relative_base"));
2460 $typefamily{$e->{TYPE}}->{PULL_FN_BODY}->($self, $e, $ndr, $varname);
2461 # restore the old relative_base_offset
2462 $self->pidl("ndr_pull_restore_relative_base_offset($ndr, _save_relative_base_offset);") if defined(has_property($e, "relative_base"));
2465 sub ParseTypePullFunction($$)
2467 my ($self, $e, $varname) = @_;
2470 my $args = $typefamily{$e->{TYPE}}->{DECL}->($e, "pull", $e->{NAME}, $varname);
2472 $self->fn_declare("pull", $e, "enum ndr_err_code ".TypeFunctionName("ndr_pull", $e)."(struct ndr_pull *$ndr, int ndr_flags, $args)") or return;
2476 $self->ParseTypePull($e, $ndr, $varname, 1, 1);
2477 $self->pidl("return NDR_ERR_SUCCESS;");
2483 sub ParseTypePrint($$$$)
2485 my ($self, $e, $ndr, $varname) = @_;
2487 $typefamily{$e->{TYPE}}->{PRINT_FN_BODY}->($self, $e, $ndr, $e->{NAME}, $varname);
2490 sub ParseTypePrintFunction($$$)
2492 my ($self, $e, $varname) = @_;
2495 my $args = $typefamily{$e->{TYPE}}->{DECL}->($e, "print", $e->{NAME}, $varname);
2497 $self->pidl_hdr("void ".TypeFunctionName("ndr_print", $e)."(struct ndr_print *ndr, const char *name, $args);");
2499 return if (has_property($e, "noprint"));
2501 $self->pidl("_PUBLIC_ void ".TypeFunctionName("ndr_print", $e)."(struct ndr_print *$ndr, const char *name, $args)");
2504 $self->ParseTypePrint($e, $ndr, $varname);
2510 sub ParseTypeNdrSize($$)
2515 my $tf = $typefamily{$t->{TYPE}};
2516 my $args = $tf->{SIZE_FN_ARGS}->($t, $t->{NAME}, $varname);
2518 $self->fn_declare("size", $t, "size_t ndr_size_$t->{NAME}($args)") or return;
2522 $typefamily{$t->{TYPE}}->{SIZE_FN_BODY}->($self,$t, $t->{NAME}, $varname);
2528 #####################################################################
2529 # parse the interface definitions
2530 sub ParseInterface($$$)
2532 my($self,$interface,$needed) = @_;
2534 $self->pidl_hdr("#ifndef _HEADER_NDR_$interface->{NAME}");
2535 $self->pidl_hdr("#define _HEADER_NDR_$interface->{NAME}");
2537 $self->pidl_hdr("");
2539 $self->HeaderInterface($interface, $needed);
2542 foreach my $d (@{$interface->{TYPES}}) {
2543 next unless(typeHasBody($d));
2545 ($needed->{TypeFunctionName("ndr_push", $d)}) && $self->ParseTypePushFunction($d, "r");
2546 ($needed->{TypeFunctionName("ndr_pull", $d)}) && $self->ParseTypePullFunction($d, "r");
2547 ($needed->{TypeFunctionName("ndr_print", $d)}) && $self->ParseTypePrintFunction($d, "r");
2549 # Make sure we don't generate a function twice...
2550 $needed->{TypeFunctionName("ndr_push", $d)} =
2551 $needed->{TypeFunctionName("ndr_pull", $d)} =
2552 $needed->{TypeFunctionName("ndr_print", $d)} = 0;
2554 ($needed->{"ndr_size_$d->{NAME}"}) && $self->ParseTypeNdrSize($d);
2558 foreach my $d (@{$interface->{FUNCTIONS}}) {
2559 ($needed->{"ndr_push_$d->{NAME}"}) && $self->ParseFunctionPush($d);
2560 ($needed->{"ndr_pull_$d->{NAME}"}) && $self->ParseFunctionPull($d);
2561 ($needed->{"ndr_print_$d->{NAME}"}) && $self->ParseFunctionPrint($d);
2563 # Make sure we don't generate a function twice...
2564 $needed->{"ndr_push_$d->{NAME}"} = $needed->{"ndr_pull_$d->{NAME}"} =
2565 $needed->{"ndr_print_$d->{NAME}"} = 0;
2568 $self->FunctionTable($interface);
2570 $self->pidl_hdr("#endif /* _HEADER_NDR_$interface->{NAME} */");
2573 sub GenerateIncludes($)
2577 $self->pidl("#include \"includes.h\"");
2579 $self->pidl("#ifndef _GNU_SOURCE");
2580 $self->pidl("#define _GNU_SOURCE");
2581 $self->pidl("#endif");
2582 $self->pidl("#include <stdint.h>");
2583 $self->pidl("#include <stdlib.h>");
2584 $self->pidl("#include <stdio.h>");
2585 $self->pidl("#include <stdbool.h>");
2586 $self->pidl("#include <stdarg.h>");
2587 $self->pidl("#include <string.h>");
2591 #####################################################################
2592 # parse a parsed IDL structure back into an IDL file
2595 my($self, $ndr,$gen_header,$ndr_header) = @_;
2597 $self->pidl_hdr("/* header auto-generated by pidl */");
2598 $self->pidl_hdr("");
2599 $self->pidl_hdr(choose_header("librpc/ndr/libndr.h", "ndr.h"));
2600 $self->pidl_hdr("#include \"$gen_header\"") if ($gen_header);
2601 $self->pidl_hdr("");
2603 $self->pidl("/* parser auto-generated by pidl */");
2605 $self->GenerateIncludes();
2606 $self->pidl("#include \"$ndr_header\"") if ($ndr_header);
2612 ($_->{TYPE} eq "INTERFACE") && NeededInterface($_, \%needed);
2616 ($_->{TYPE} eq "INTERFACE") && $self->ParseInterface($_, \%needed);
2617 ($_->{TYPE} eq "IMPORT") && $self->HeaderImport(@{$_->{PATHS}});
2618 ($_->{TYPE} eq "INCLUDE") && $self->HeaderInclude(@{$_->{PATHS}});
2621 return ($self->{res_hdr}, $self->{res});
2624 sub NeededElement($$$)
2626 my ($e, $dir, $needed) = @_;
2628 return if ($e->{TYPE} eq "EMPTY");
2630 return if (ref($e->{TYPE}) eq "HASH" and
2631 not defined($e->{TYPE}->{NAME}));
2634 if (ref($e->{TYPE}) eq "HASH") {
2635 $t = $e->{TYPE}->{TYPE}."_".$e->{TYPE}->{NAME};
2640 if (ref($e->{REPRESENTATION_TYPE}) eq "HASH") {
2641 $rt = $e->{REPRESENTATION_TYPE}->{TYPE}."_".$e->{REPRESENTATION_TYPE}->{NAME};
2643 $rt = $e->{REPRESENTATION_TYPE};
2646 die ("$e->{NAME} $t, $rt FOO") unless ($rt ne "");
2649 if ($dir eq "print") {
2650 push(@fn, TypeFunctionName("ndr_print", $e->{REPRESENTATION_TYPE}));
2651 } elsif ($dir eq "pull") {
2652 push (@fn, TypeFunctionName("ndr_pull", $e->{TYPE}));
2653 push (@fn, "ndr_$t\_to_$rt")
2655 } elsif ($dir eq "push") {
2656 push (@fn, TypeFunctionName("ndr_push", $e->{TYPE}));
2657 push (@fn, "ndr_$rt\_to_$t")
2660 die("invalid direction `$dir'");
2664 unless (defined($needed->{$_})) {
2670 sub NeededFunction($$)
2672 my ($fn,$needed) = @_;
2673 $needed->{"ndr_pull_$fn->{NAME}"} = 1;
2674 $needed->{"ndr_push_$fn->{NAME}"} = 1;
2675 $needed->{"ndr_print_$fn->{NAME}"} = 1;
2676 foreach my $e (@{$fn->{ELEMENTS}}) {
2678 NeededElement($e, $_, $needed) foreach ("pull", "push", "print");
2684 sub NeededType($$$);
2685 my ($t,$needed,$req) = @_;
2687 NeededType($t->{DATA}, $needed, $req) if ($t->{TYPE} eq "TYPEDEF");
2689 if ($t->{TYPE} eq "STRUCT" or $t->{TYPE} eq "UNION") {
2690 return unless defined($t->{ELEMENTS});
2691 for my $e (@{$t->{ELEMENTS}}) {
2693 if (has_property($e, "compression")) {
2694 $needed->{"compression"} = 1;
2696 NeededElement($e, $req, $needed);
2697 NeededType($e->{TYPE}, $needed, $req) if (ref($e->{TYPE}) eq "HASH");
2702 #####################################################################
2703 # work out what parse functions are needed
2704 sub NeededInterface($$)
2706 my ($interface,$needed) = @_;
2707 NeededFunction($_, $needed) foreach (@{$interface->{FUNCTIONS}});
2708 foreach (reverse @{$interface->{TYPES}}) {
2709 if (has_property($_, "public")) {
2710 $needed->{TypeFunctionName("ndr_pull", $_)} = $needed->{TypeFunctionName("ndr_push", $_)} =
2711 $needed->{TypeFunctionName("ndr_print", $_)} = 1;
2714 NeededType($_, $needed, "pull") if ($needed->{TypeFunctionName("ndr_pull", $_)});
2715 NeededType($_, $needed, "push") if ($needed->{TypeFunctionName("ndr_push", $_)});
2716 NeededType($_, $needed, "print") if ($needed->{TypeFunctionName("ndr_print", $_)});
2717 if (has_property($_, "gensize")) {
2718 $needed->{"ndr_size_$_->{NAME}"} = 1;
2723 sub TypeFunctionName($$)
2725 my ($prefix, $t) = @_;
2727 return "$prefix\_$t->{NAME}" if (ref($t) eq "HASH" and
2728 $t->{TYPE} eq "TYPEDEF");
2729 return "$prefix\_$t->{TYPE}_$t->{NAME}" if (ref($t) eq "HASH");
2730 return "$prefix\_$t";