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-2005
6 # released under the GNU GPL
20 return $typefamily{$n};
23 my %scalar_alignments =
42 "error_status_t" => 4,
52 $typefamily{SCALAR} = {
55 return $scalar_alignments{$t->{NAME}};
63 return 0 unless typelist::hasType($type);
65 if (my $dt = typelist::getType($type)->{DATA}->{TYPE}) {
66 return 1 if ($dt eq "SCALAR" or $dt eq "ENUM" or $dt eq "BITMAP");
76 return undef unless $e->{POINTERS};
78 return "ref" if (util::has_property($e, "ref"));
79 return "ptr" if (util::has_property($e, "ptr"));
80 return "unique" if (util::has_property($e, "unique"));
81 return "relative" if (util::has_property($e, "relative"));
82 return "ignore" if (util::has_property($e, "ignore"));
87 # return 1 if this is a fixed array
91 my $len = $e->{"ARRAY_LEN"};
92 return 1 if (defined $len && util::is_constant($len));
96 # return 1 if this is a conformant array
97 sub is_conformant_array($)
100 return 1 if (util::has_property($e, "size_is"));
104 # return 1 if this is a inline array
105 sub is_inline_array($)
108 my $len = $e->{"ARRAY_LEN"};
109 if (is_fixed_array($e) ||
110 defined $len && $len ne "*") {
116 # return 1 if this is a varying array
117 sub is_varying_array($)
120 return util::has_property($e, "length_is");
123 # return 1 if this is a surrounding array (sometimes
124 # referred to as an embedded array). Can only occur as
125 # the last element in a struct and can not contain any pointers.
126 sub is_surrounding_array($)
130 return ($e->{POINTERS} == 0
131 and defined $e->{ARRAY_LEN}
132 and $e->{ARRAY_LEN} eq "*"
133 and $e == $e->{PARENT}->{ELEMENTS}[-1]
134 and $e->{PARENT}->{TYPE} ne "FUNCTION");
141 return "conformant-varying" if (is_varying_array($e) and is_conformant_array($e));
142 return "conformant" if (is_varying_array($e));
143 return "varying" if (is_varying_array($e));
144 return "inline" if (is_inline_array($e));
145 return "fixed" if (is_fixed_array($e));
150 # determine if an element needs a reference pointer on the wire
151 # in its NDR representation
152 sub need_wire_pointer($)
156 my $n = $e->{POINTERS};
157 my $pt = pointer_type($e);
159 # Top level "ref" pointers do not have a referrent identifier
162 and $e->{PARENT}->{TYPE} eq "FUNCTION")
170 # determine if an element needs a "buffers" section in NDR
171 sub need_buffers_section($)
174 if (!can_contain_deferred($e) &&
175 !util::array_size($e)) {
181 # see if a variable needs to be allocated by the NDR subsystem on pull
186 return 0 if (util::has_property($e, "ref") && $e->{PARENT}->{TYPE} eq "FUNCTION");
187 return 1 if ($e->{POINTERS} || util::array_size($e));
191 # Prefix to get the actual value of a variable
196 foreach my $i (need_wire_pointer($e)..$e->{POINTERS}-1) { $pointers.="*"; }
200 # determine the C prefix used to refer to a variable when passing to a push
201 # function. This will be '*' for pointers to scalar types, '' for scalar
202 # types and normal pointers and '&' for pass-by-reference structures
209 if ($e->{TYPE} =~ "string") {
211 } elsif (is_scalar_type($e->{TYPE}) and $e->{POINTERS} and
212 !util::array_size($e)) {
214 } elsif (!is_scalar_type($e->{TYPE}) &&
216 !util::array_size($e)) {
220 foreach my $i (2..$e->{POINTERS}) { $ret.="*"; }
225 # determine the C prefix used to refer to a variable when passing to a pull
231 if (!$e->{POINTERS} && !util::array_size($e)) {
235 if ($e->{TYPE} =~ "string") {
240 foreach my $i (2..$e->{POINTERS}) { $ret.="*"; }
262 $tabs = substr($tabs, 0, -1);
265 ####################################################################
266 # work out the name of a size_is() variable
271 my($var_prefix) = shift;
273 my($fn) = $e->{PARENT};
275 return $size if (util::is_constant($size));
277 return $size if ($size =~ /ndr->|\(/);
281 if ($size =~ /\*(.*)/) {
286 if ($fn->{TYPE} ne "FUNCTION") {
287 return $prefix . "r->$size";
290 my $e2 = util::find_sibling($e, $size);
292 die("Invalid sibling '$size'") unless defined($e2);
294 if (util::has_property($e2, "in") && util::has_property($e2, "out")) {
295 return $prefix . "$var_prefix$size";
298 if (util::has_property($e2, "in")) {
299 return $prefix . "r->in.$size";
302 if (util::has_property($e2, "out")) {
303 return $prefix . "r->out.$size";
306 die "invalid variable in $size for element $e->{NAME} in $fn->{NAME}\n";
309 #####################################################################
310 # check that a variable we get from ParseExpr isn't a null pointer
311 sub check_null_pointer($)
314 if ($size =~ /^\*/) {
315 my $size2 = substr($size, 1);
316 pidl "if ($size2 == NULL) return NT_STATUS_INVALID_PARAMETER_MIX;";
320 #####################################################################
321 # check that a variable we get from ParseExpr isn't a null pointer
322 # void return varient
323 sub check_null_pointer_void($)
326 if ($size =~ /^\*/) {
327 my $size2 = substr($size, 1);
328 pidl "if ($size2 == NULL) return;";
332 #####################################################################
333 # work out is a parse function should be declared static or not
338 if ($fn->{TYPE} eq "TYPEDEF" or
339 $fn->{TYPE} eq "FUNCTION") {
340 return "" if (util::has_property($fn, "public"));
346 ###################################################################
347 # setup any special flags for an element or structure
351 my $flags = util::has_property($e, "flag");
352 if (defined $flags) {
353 pidl "{ uint32_t _flags_save_$e->{TYPE} = ndr->flags;";
354 pidl "ndr_set_flags(&ndr->flags, $flags);";
358 ###################################################################
359 # end any special flags for an element or structure
363 my $flags = util::has_property($e, "flag");
364 if (defined $flags) {
365 pidl "ndr->flags = _flags_save_$e->{TYPE};\n\t}";
369 #####################################################################
370 # work out the correct alignment for a structure or union
371 sub find_largest_alignment($)
376 for my $e (@{$s->{ELEMENTS}}) {
379 if (need_wire_pointer($e)) {
382 $a = align_type($e->{TYPE});
385 $align = $a if ($align < $a);
391 #####################################################################
397 unless (typelist::hasType($e)) {
398 # it must be an external type - all we can do is guess
399 # print "Warning: assuming alignment of unknown type '$e' is 4\n";
403 my $dt = typelist::getType($e)->{DATA};
405 my $tmp = $typefamily{$dt->{TYPE}}->{ALIGN}->($dt);
409 #####################################################################
410 # see if a type contains any deferred data
411 sub can_contain_deferred
415 return 1 if ($e->{POINTERS});
416 return 0 if (is_scalar_type($e->{TYPE}));
417 return 0 if (util::has_property($e, "subcontext"));
418 return 1 unless (typelist::hasType($e->{TYPE})); # assume the worst
420 my $type = typelist::getType($e->{TYPE});
422 foreach my $x (@{$type->{DATA}->{ELEMENTS}}) {
423 return 1 if (can_contain_deferred ($x));
429 #####################################################################
430 # parse array preceding data - push side
431 sub ParseArrayPushPreceding($$$)
434 my $var_prefix = shift;
435 my $ndr_flags = shift;
437 my $size = ParseExpr($e, util::array_size($e), $var_prefix);
439 if (!is_inline_array($e)) {
440 # we need to emit the array size
441 pidl "NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, $size));";
445 #####################################################################
446 # parse the data of an array - push side
447 sub ParseArrayPush($$$$)
451 my $var_prefix = shift;
452 my $ndr_flags = shift;
453 my $cprefix = c_push_prefix($e);
455 my $size = ParseExpr($e, util::array_size($e), $var_prefix);
457 # See whether the array size has been pushed yet
458 if (!is_surrounding_array($e)) {
459 ParseArrayPushPreceding($e, $var_prefix, $ndr_flags);
462 if (is_varying_array($e)) {
463 my $length = util::has_property($e, "length_is");
464 $length = ParseExpr($e, $length, $var_prefix);
465 pidl "NDR_CHECK(ndr_push_uint32($ndr, NDR_SCALARS, 0));";
466 pidl "NDR_CHECK(ndr_push_uint32($ndr, NDR_SCALARS, $length));";
470 if (is_scalar_type($e->{TYPE})) {
471 pidl "NDR_CHECK(ndr_push_array_$e->{TYPE}($ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}, $size));";
473 pidl "NDR_CHECK(ndr_push_array($ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}, sizeof($cprefix$var_prefix$e->{NAME}\[0]), $size, (ndr_push_flags_fn_t)ndr_push_$e->{TYPE}));";
477 #####################################################################
479 sub ParseArrayPrint($$)
482 my $var_prefix = shift;
483 my $size = ParseExpr($e, util::array_size($e), $var_prefix);
484 my $cprefix = c_push_prefix($e);
486 if (is_varying_array($e)) {
487 $size = ParseExpr($e, util::has_property($e, "length_is"), $var_prefix);
490 if (is_scalar_type($e->{TYPE})) {
491 pidl "ndr_print_array_$e->{TYPE}(ndr, \"$e->{NAME}\", $cprefix$var_prefix$e->{NAME}, $size);";
493 pidl "ndr_print_array(ndr, \"$e->{NAME}\", $cprefix$var_prefix$e->{NAME}, sizeof($cprefix$var_prefix$e->{NAME}\[0]), $size, (ndr_print_fn_t)ndr_print_$e->{TYPE});";
497 #####################################################################
498 # check the size_is and length_is constraints
499 sub CheckArraySizes($$)
502 my $var_prefix = shift;
504 if (is_conformant_array($e)) {
505 my $size = ParseExpr($e, util::array_size($e), $var_prefix);
506 pidl "if ($var_prefix$e->{NAME}) {";
508 check_null_pointer($size);
509 pidl "NDR_CHECK(ndr_check_array_size(ndr, (void*)&$var_prefix$e->{NAME}, $size));";
514 if (is_varying_array($e)) {
515 my $length = util::has_property($e, "length_is");
516 $length = ParseExpr($e, $length, $var_prefix);
517 pidl "if ($var_prefix$e->{NAME}) {";
519 check_null_pointer($length);
520 pidl "NDR_CHECK(ndr_check_array_length(ndr, (void*)&$var_prefix$e->{NAME}, $length));";
526 sub ParseArrayPullPreceding($$$)
529 my $var_prefix = shift;
530 my $ndr_flags = shift;
532 if (!is_inline_array($e)) {
533 # non fixed arrays encode the size just before the array
534 pidl "NDR_CHECK(ndr_pull_array_size(ndr, &$var_prefix$e->{NAME}));";
538 #####################################################################
539 # parse an array - pull side
540 sub ParseArrayPull($$$$)
544 my $var_prefix = shift;
545 my $ndr_flags = shift;
547 my $cprefix = c_pull_prefix($e);
548 my $length = ParseExpr($e, util::array_size($e), $var_prefix);
551 if (is_conformant_array($e)) {
552 $length = $size = "ndr_get_array_size($ndr, &$var_prefix$e->{NAME})";
555 # if this is a conformant array then we use that size to allocate, and make sure
556 # we allocate enough to pull the elements
557 if (!is_inline_array($e) and not is_surrounding_array($e)) {
558 if ($var_prefix =~ /^r->out/ && $length =~ /^\*r->in/) {
559 my $length2 = substr($length, 1);
560 pidl "if (ndr->flags & LIBNDR_FLAG_REF_ALLOC) { NDR_ALLOC($ndr, $length2); }";
563 ParseArrayPullPreceding($e, $var_prefix, $ndr_flags);
566 if (is_varying_array($e)) {
567 pidl "NDR_CHECK(ndr_pull_array_length($ndr, &$var_prefix$e->{NAME}));";
568 $length = "ndr_get_array_length($ndr, &$var_prefix$e->{NAME})";
571 check_null_pointer($length);
573 if ($length ne $size) {
574 pidl "if ($length > $size) {";
576 pidl "return ndr_pull_error($ndr, NDR_ERR_CONFORMANT_SIZE, \"Bad conformant size %u should be %u\", $size, $length);";
581 if ((need_alloc($e) && !is_fixed_array($e)) ||
582 ($var_prefix eq "r->in." && util::has_property($e, "ref"))) {
583 if (!is_inline_array($e) || $ndr_flags eq "NDR_SCALARS") {
584 pidl "NDR_ALLOC_N($ndr, $var_prefix$e->{NAME}, $size);";
588 if (($var_prefix eq "r->out." && util::has_property($e, "ref"))) {
589 if (!is_inline_array($e) || $ndr_flags eq "NDR_SCALARS") {
590 pidl "if ($ndr->flags & LIBNDR_FLAG_REF_ALLOC) {";
591 pidl "\tNDR_ALLOC_N($ndr, $var_prefix$e->{NAME}, $size);";
596 if (is_scalar_type($e->{TYPE})) {
597 pidl "NDR_CHECK(ndr_pull_array_$e->{TYPE}($ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}, $length));";
599 pidl "NDR_CHECK(ndr_pull_array($ndr, $ndr_flags, (void **)$cprefix$var_prefix$e->{NAME}, sizeof($cprefix$var_prefix$e->{NAME}\[0]), $length, (ndr_pull_flags_fn_t)ndr_pull_$e->{TYPE}));";
603 sub ParseSubcontextPushStart($$)
606 my $ndr_flags = shift;
608 pidl "if (($ndr_flags) & NDR_SCALARS) {";
610 pidl "struct ndr_push *_ndr_$e->{NAME};";
612 pidl "_ndr_$e->{NAME} = ndr_push_init_ctx(ndr);";
613 pidl "if (!_ndr_$e->{NAME}) return NT_STATUS_NO_MEMORY;";
614 pidl "_ndr_$e->{NAME}->flags = ndr->flags;";
617 return "_ndr_$e->{NAME}";
620 sub ParseSubcontextPushEnd($)
623 my $header_size = util::has_property($e, "subcontext");
624 my $size_is = util::has_property($e, "subcontext_size");
626 if (not defined($size_is)) {
630 pidl "NDR_CHECK(ndr_push_subcontext_header(ndr, $header_size, $size_is, _ndr_$e->{NAME}));";
631 pidl "NDR_CHECK(ndr_push_bytes(ndr, _ndr_$e->{NAME}->data, _ndr_$e->{NAME}->offset));";
636 sub ParseSubcontextPullStart($$)
639 my $ndr_flags = shift;
640 my $header_size = util::has_property($e, "subcontext");
641 my $size_is = util::has_property($e, "subcontext_size");
643 if (not defined($size_is)) {
647 pidl "if (($ndr_flags) & NDR_SCALARS) {";
649 pidl "struct ndr_pull *_ndr_$e->{NAME};";
650 pidl "NDR_ALLOC(ndr, _ndr_$e->{NAME});";
651 pidl "NDR_CHECK(ndr_pull_subcontext_header(ndr, $header_size, $size_is, _ndr_$e->{NAME}));";
652 return "_ndr_$e->{NAME}";
655 sub ParseSubcontextPullEnd($)
658 my $header_size = util::has_property($e, "subcontext");
659 my $size_is = util::has_property($e, "subcontext_size");
662 if (defined ($size_is)) {
663 $advance = "$size_is";
664 } elsif ($header_size) {
665 $advance = "_ndr_$e->{NAME}->data_size";
667 $advance = "_ndr_$e->{NAME}->offset";
669 pidl "NDR_CHECK(ndr_pull_advance(ndr, $advance));";
674 #####################################################################
675 # parse scalars in a structure element
676 sub ParseElementPushScalar($$$)
679 my($var_prefix) = shift;
680 my($ndr_flags) = shift;
681 my $cprefix = c_push_prefix($e);
682 my $ptr_prefix = c_ptr_prefix($e);
683 my $sub_size = util::has_property($e, "subcontext");
688 if (my $value = util::has_property($e, "value")) {
689 pidl "$cprefix$var_prefix$e->{NAME} = $value;";
692 if (defined $sub_size and $e->{POINTERS} == 0) {
693 $ndr = ParseSubcontextPushStart($e, "NDR_SCALARS");
696 if (need_wire_pointer($e)) {
697 ParsePtrPush($e, $ptr_prefix.$var_prefix);
698 } elsif (is_inline_array($e)) {
699 ParseArrayPush($e, $ndr, "r->", "NDR_SCALARS");
700 } elsif (need_alloc($e)) {
701 # no scalar component
703 if (my $switch = util::has_property($e, "switch_is")) {
704 ParseSwitchPush($e, $ndr, $var_prefix, $ndr_flags, $switch);
707 pidl "NDR_CHECK(ndr_push_$e->{TYPE}($ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}));";
710 if (defined $sub_size and $e->{POINTERS} == 0) {
711 ParseSubcontextPushEnd($e);
717 #####################################################################
718 # parse a pointer in a struct element or function
722 my $var_prefix = shift;
724 if (util::has_property($e, "ref")) {
725 pidl "NDR_CHECK(ndr_push_ref_ptr(ndr, $var_prefix$e->{NAME}));";
726 } elsif (util::has_property($e, "relative")) {
727 pidl "NDR_CHECK(ndr_push_relative_ptr1(ndr, $var_prefix$e->{NAME}));";
729 pidl "NDR_CHECK(ndr_push_unique_ptr(ndr, $var_prefix$e->{NAME}));";
733 #####################################################################
734 # print scalars in a structure element
735 sub ParseElementPrint($$)
738 my($var_prefix) = shift;
739 my $cprefix = c_push_prefix($e);
740 my $ptr_prefix = c_ptr_prefix($e);
742 return if (util::has_property($e, "noprint"));
744 if (my $value = util::has_property($e, "value")) {
745 pidl "if (ndr->flags & LIBNDR_PRINT_SET_VALUES) {";
746 pidl "\t$cprefix$var_prefix$e->{NAME} = $value;";
750 my $l = $e->{POINTERS};
751 $l++ if (util::array_size($e) and $l == 0 and !is_fixed_array($e));
753 foreach my $i (1..$l) {
754 pidl "ndr_print_ptr(ndr, \"$e->{NAME}\", $var_prefix$e->{NAME});";
755 pidl "ndr->depth++;";
756 if ($i > $l-need_wire_pointer($e)) {
757 pidl "if ($ptr_prefix$var_prefix$e->{NAME}) {";
762 if (util::array_size($e)) {
763 ParseArrayPrint($e, $var_prefix)
764 } elsif (my $switch = util::has_property($e, "switch_is")) {
765 my $switch_var = ParseExpr($e, $switch, $var_prefix);
766 check_null_pointer_void($switch_var);
768 pidl "ndr_print_$e->{TYPE}(ndr, \"$e->{NAME}\", $switch_var, $cprefix$var_prefix$e->{NAME});";
770 pidl "ndr_print_$e->{TYPE}(ndr, \"$e->{NAME}\", $cprefix$var_prefix$e->{NAME});";
773 foreach my $i (1..$l) {
774 if ($i > $l-need_wire_pointer($e)) {
778 pidl "ndr->depth--;";
782 #####################################################################
783 # parse scalars in a structure element - pull size
784 sub ParseSwitchPull($$$$$)
788 my($var_prefix) = shift;
789 my($ndr_flags) = shift;
791 my $switch_var = ParseExpr($e, $switch, $var_prefix);
793 my $cprefix = c_pull_prefix($e);
795 my $utype = typelist::getType($e->{TYPE});
797 check_null_pointer($switch_var);
799 pidl "NDR_CHECK(ndr_pull_set_switch_value($ndr, $cprefix$var_prefix$e->{NAME}, $switch_var));";
803 #####################################################################
804 # push switch element
805 sub ParseSwitchPush($$$$$)
809 my($var_prefix) = shift;
810 my($ndr_flags) = shift;
812 my $switch_var = ParseExpr($e, $switch, $var_prefix);
813 my $cprefix = c_push_prefix($e);
815 check_null_pointer($switch_var);
817 pidl "NDR_CHECK(ndr_push_set_switch_value($ndr, $cprefix$var_prefix$e->{NAME}, $switch_var));";
821 #####################################################################
822 # parse scalars in a structure element - pull size
823 sub ParseElementPullScalar($$$)
826 my($var_prefix) = shift;
827 my($ndr_flags) = shift;
828 my $cprefix = c_pull_prefix($e);
829 my $ptr_prefix = c_ptr_prefix($e);
830 my $sub_size = util::has_property($e, "subcontext");
835 if (defined $sub_size && $e->{POINTERS} == 0) {
836 $ndr = ParseSubcontextPullStart($e, $ndr_flags);
837 $ndr_flags = "NDR_SCALARS|NDR_BUFFERS";
840 if (is_inline_array($e)) {
841 ParseArrayPull($e, $ndr, "r->", "NDR_SCALARS");
842 } elsif (need_wire_pointer($e)) {
843 ParsePtrPull($e, $ptr_prefix.$var_prefix);
844 } elsif (is_surrounding_array($e)) {
846 if (my $switch = util::has_property($e, "switch_is")) {
847 ParseSwitchPull($e, $ndr, $var_prefix, $ndr_flags, $switch);
850 pidl "NDR_CHECK(ndr_pull_$e->{TYPE}($ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}));";
853 if (my $range = util::has_property($e, "range")) {
854 my ($low, $high) = split(/ /, $range, 2);
855 pidl "if ($var_prefix$e->{NAME} < $low || $var_prefix$e->{NAME} > $high) {";
856 pidl "\treturn ndr_pull_error($ndr, NDR_ERR_RANGE, \"value out of range\");";
860 if (defined $sub_size && $e->{POINTERS} == 0) {
861 ParseSubcontextPullEnd($e);
867 #####################################################################
868 # parse a pointer in a struct element or function
872 my($var_prefix) = shift;
874 if (util::has_property($e, "ref")) {
875 pidl "NDR_CHECK(ndr_pull_ref_ptr(ndr, &_ptr_$e->{NAME}));";
877 pidl "NDR_CHECK(ndr_pull_unique_ptr(ndr, &_ptr_$e->{NAME}));";
879 pidl "if (_ptr_$e->{NAME}) {";
881 pidl "NDR_ALLOC(ndr, $var_prefix$e->{NAME});";
882 if (util::has_property($e, "relative")) {
883 pidl "NDR_CHECK(ndr_pull_relative_ptr1(ndr, $var_prefix$e->{NAME}, _ptr_$e->{NAME}));";
887 pidl "\t$var_prefix$e->{NAME} = NULL;";
891 #####################################################################
892 # parse buffers in a structure element
893 sub ParseElementPushBuffer($$)
896 my($var_prefix) = shift;
897 my $cprefix = c_push_prefix($e);
898 my $sub_size = util::has_property($e, "subcontext");
901 return unless (need_buffers_section($e));
905 my $pointers = c_ptr_prefix($e);
906 for my $i (1..need_wire_pointer($e)) {
908 ParsePtrPush($e,$pointers.$var_prefix);
910 pidl "if ($pointers$var_prefix$e->{NAME}) {";
915 if (util::has_property($e, "relative")) {
916 pidl "NDR_CHECK(ndr_push_relative_ptr2(ndr, $var_prefix$e->{NAME}));";
919 my $ndr_flags = "NDR_BUFFERS";
920 if ($e->{POINTERS} || (util::array_size($e) && !is_inline_array($e)))
922 $ndr_flags="NDR_SCALARS|$ndr_flags"
925 if (defined $sub_size) {
926 $ndr = ParseSubcontextPushStart($e, $ndr_flags);
927 $ndr_flags = "NDR_SCALARS|NDR_BUFFERS";
930 if (util::array_size($e)) {
931 ParseArrayPush($e, $ndr, "r->", $ndr_flags);
933 if (my $switch = util::has_property($e, "switch_is")) {
934 ParseSwitchPush($e, $ndr, $var_prefix, $ndr_flags, $switch);
937 pidl "NDR_CHECK(ndr_push_$e->{TYPE}($ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}));";
940 if (defined $sub_size) {
941 ParseSubcontextPushEnd($e);
944 for my $i (1..need_wire_pointer($e)) {
952 #####################################################################
953 # parse buffers in a structure element - pull side
954 sub ParseElementPullBuffer($$)
957 my($var_prefix) = shift;
958 my $cprefix = c_pull_prefix($e);
959 my $sub_size = util::has_property($e, "subcontext");
962 return unless (need_buffers_section($e));
966 my $pointers = c_ptr_prefix($e);
967 for my $i (1..need_wire_pointer($e)) {
969 ParsePtrPull($e,$pointers.$var_prefix);
971 pidl "if ($pointers$var_prefix$e->{NAME}) {";
976 if (util::has_property($e, "relative")) {
977 pidl "struct ndr_pull_save _relative_save;";
978 pidl "ndr_pull_save(ndr, &_relative_save);";
979 pidl "NDR_CHECK(ndr_pull_relative_ptr2(ndr, $var_prefix$e->{NAME}));";
982 my $ndr_flags = "NDR_BUFFERS";
983 if ($e->{POINTERS} || (util::array_size($e) && !is_inline_array($e)))
985 $ndr_flags="NDR_SCALARS|$ndr_flags"
988 if (defined $sub_size) {
989 $ndr = ParseSubcontextPullStart($e, $ndr_flags);
990 $ndr_flags = "NDR_SCALARS|NDR_BUFFERS";
993 if (util::array_size($e)) {
994 ParseArrayPull($e, $ndr, "r->", $ndr_flags);
996 if (my $switch = util::has_property($e, "switch_is")) {
997 ParseSwitchPull($e, $ndr, $var_prefix, $ndr_flags, $switch);
1000 pidl "NDR_CHECK(ndr_pull_$e->{TYPE}($ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}));";
1003 if (defined $sub_size) {
1004 ParseSubcontextPullEnd($e);
1007 if (util::has_property($e, "relative")) {
1008 pidl "ndr_pull_restore(ndr, &_relative_save);";
1011 for my $i (1..need_wire_pointer($e)) {
1019 #####################################################################
1021 sub ParseStructPush($)
1023 my($struct) = shift;
1025 return unless defined($struct->{ELEMENTS});
1027 start_flags($struct);
1029 # see if the structure contains a conformant array. If it
1030 # does, then it must be the last element of the structure, and
1031 # we need to push the conformant length early, as it fits on
1032 # the wire before the structure (and even before the structure
1034 my $e = $struct->{ELEMENTS}[-1];
1035 if (is_conformant_array($e) and is_surrounding_array($e)) {
1036 ParseArrayPushPreceding($e, "r->", "NDR_SCALARS");
1039 if (defined $e->{TYPE} && $e->{TYPE} eq "string"
1040 && util::property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
1041 pidl "NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, ndr_string_array_size(ndr, r->$e->{NAME})));";
1044 pidl "if (!(ndr_flags & NDR_SCALARS)) goto buffers;";
1046 pidl "NDR_CHECK(ndr_push_struct_start(ndr));";
1048 my $align = find_largest_alignment($struct);
1049 pidl "NDR_CHECK(ndr_push_align(ndr, $align));";
1051 foreach my $e (@{$struct->{ELEMENTS}}) {
1052 ParseElementPushScalar($e, "r->", "NDR_SCALARS");
1056 pidl "if (!(ndr_flags & NDR_BUFFERS)) goto done;";
1057 foreach my $e (@{$struct->{ELEMENTS}}) {
1058 ParseElementPushBuffer($e, "r->");
1061 pidl "ndr_push_struct_end(ndr);";
1068 #####################################################################
1069 # generate a push function for an enum
1070 sub ParseEnumPush($)
1073 my($type_fn) = typelist::enum_type_fn($enum);
1077 pidl "NDR_CHECK(ndr_push_$type_fn(ndr, NDR_SCALARS, r));";
1082 #####################################################################
1083 # generate a pull function for an enum
1084 sub ParseEnumPull($)
1087 my($type_fn) = typelist::enum_type_fn($enum);
1088 my($type_v_decl) = typelist::mapScalarType(typelist::enum_type_fn($enum));
1090 pidl "$type_v_decl v;";
1092 pidl "NDR_CHECK(ndr_pull_$type_fn(ndr, NDR_SCALARS, &v));";
1098 #####################################################################
1099 # generate a print function for an enum
1100 sub ParseEnumPrint($)
1104 pidl "const char *val = NULL;";
1109 pidl "switch (r) {";
1111 my $els = \@{$enum->{ELEMENTS}};
1112 foreach my $i (0 .. $#{$els}) {
1113 my $e = ${$els}[$i];
1115 if ($e =~ /^(.*)=/) {
1118 pidl "case $e: val = \"$e\"; break;";
1124 pidl "ndr_print_enum(ndr, name, \"$enum->{TYPE}\", val, r);";
1132 return "struct ndr_push *ndr, int ndr_flags, enum $e->{NAME} r";
1135 sub ArgsEnumPrint($)
1138 return "struct ndr_print *ndr, const char *name, enum $e->{NAME} r";
1144 return "struct ndr_pull *ndr, int ndr_flags, enum $e->{NAME} *r";
1147 $typefamily{ENUM} = {
1148 PUSH_FN_BODY => \&ParseEnumPush,
1149 PUSH_FN_ARGS => \&ArgsEnumPush,
1150 PULL_FN_BODY => \&ParseEnumPull,
1151 PULL_FN_ARGS => \&ArgsEnumPull,
1152 PRINT_FN_BODY => \&ParseEnumPrint,
1153 PRINT_FN_ARGS => \&ArgsEnumPrint,
1154 ALIGN => sub { return align_type(typelist::enum_type_fn(shift)); }
1157 #####################################################################
1158 # generate a push function for a bitmap
1159 sub ParseBitmapPush($)
1161 my($bitmap) = shift;
1162 my($type_fn) = typelist::bitmap_type_fn($bitmap);
1164 start_flags($bitmap);
1166 pidl "NDR_CHECK(ndr_push_$type_fn(ndr, NDR_SCALARS, r));";
1171 #####################################################################
1172 # generate a pull function for an bitmap
1173 sub ParseBitmapPull($)
1175 my($bitmap) = shift;
1176 my($type_fn) = typelist::bitmap_type_fn($bitmap);
1177 my($type_decl) = typelist::mapType($bitmap);
1179 pidl "$type_decl v;";
1180 start_flags($bitmap);
1181 pidl "NDR_CHECK(ndr_pull_$type_fn(ndr, NDR_SCALARS, &v));";
1187 #####################################################################
1188 # generate a print function for an bitmap
1189 sub ParseBitmapPrintElement($$)
1192 my($bitmap) = shift;
1193 my($type_decl) = typelist::mapType($bitmap);
1194 my($type_fn) = typelist::bitmap_type_fn($bitmap);
1195 my($name) = $bitmap->{PARENT}->{NAME};
1198 if ($e =~ /^(\w+) .*$/) {
1201 die "Bitmap: \"$name\" invalid Flag: \"$e\"";
1204 pidl "ndr_print_bitmap_flag(ndr, sizeof($type_decl), \"$flag\", $flag, r);";
1207 #####################################################################
1208 # generate a print function for an bitmap
1209 sub ParseBitmapPrint($)
1211 my($bitmap) = shift;
1212 my($type_decl) = typelist::mapType($bitmap);
1213 my($type_fn) = typelist::bitmap_type_fn($bitmap);
1215 start_flags($bitmap);
1217 pidl "ndr_print_$type_fn(ndr, name, r);";
1219 pidl "ndr->depth++;";
1220 foreach my $e (@{$bitmap->{ELEMENTS}}) {
1221 ParseBitmapPrintElement($e, $bitmap);
1223 pidl "ndr->depth--;";
1228 sub ArgsBitmapPush($)
1231 my $type_decl = typelist::mapType($e->{DATA});
1232 return "struct ndr_push *ndr, int ndr_flags, $type_decl r";
1235 sub ArgsBitmapPrint($)
1238 my $type_decl = typelist::mapType($e->{DATA});
1239 return "struct ndr_print *ndr, const char *name, $type_decl r";
1242 sub ArgsBitmapPull($)
1245 my $type_decl = typelist::mapType($e->{DATA});
1246 return "struct ndr_pull *ndr, int ndr_flags, $type_decl *r";
1249 $typefamily{BITMAP} = {
1250 PUSH_FN_BODY => \&ParseBitmapPush,
1251 PUSH_FN_ARGS => \&ArgsBitmapPush,
1252 PULL_FN_BODY => \&ParseBitmapPull,
1253 PULL_FN_ARGS => \&ArgsBitmapPull,
1254 PRINT_FN_BODY => \&ParseBitmapPrint,
1255 PRINT_FN_ARGS => \&ArgsBitmapPrint,
1256 ALIGN => sub { return align_type(typelist::bitmap_type_fn(shift)); }
1259 #####################################################################
1260 # generate a struct print function
1261 sub ParseStructPrint($)
1263 my($struct) = shift;
1264 my($name) = $struct->{PARENT}->{NAME};
1266 return unless defined $struct->{ELEMENTS};
1268 pidl "ndr_print_struct(ndr, name, \"$name\");";
1270 start_flags($struct);
1272 pidl "ndr->depth++;";
1273 foreach my $e (@{$struct->{ELEMENTS}}) {
1274 ParseElementPrint($e, "r->");
1276 pidl "ndr->depth--;";
1281 #####################################################################
1282 # parse a struct - pull side
1283 sub ParseStructPull($)
1285 my($struct) = shift;
1288 return unless defined $struct->{ELEMENTS};
1290 # see if the structure contains a conformant array. If it
1291 # does, then it must be the last element of the structure, and
1292 # we need to pull the conformant length early, as it fits on
1293 # the wire before the structure (and even before the structure
1295 my $e = $struct->{ELEMENTS}[-1];
1296 if (is_conformant_array($e) and is_surrounding_array($e)) {
1300 if (defined $e->{TYPE} && $e->{TYPE} eq "string"
1301 && util::property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
1305 # declare any internal pointers we need
1306 foreach my $e (@{$struct->{ELEMENTS}}) {
1307 if (need_wire_pointer($e)) {
1308 pidl "uint32_t _ptr_$e->{NAME};";
1312 start_flags($struct);
1314 pidl "if (!(ndr_flags & NDR_SCALARS)) goto buffers;";
1316 pidl "NDR_CHECK(ndr_pull_struct_start(ndr));";
1318 if (defined $conform_e) {
1319 ParseArrayPullPreceding($conform_e, "r->", "NDR_SCALARS");
1322 my $align = find_largest_alignment($struct);
1323 pidl "NDR_CHECK(ndr_pull_align(ndr, $align));";
1325 foreach my $e (@{$struct->{ELEMENTS}}) {
1326 ParseElementPullScalar($e, "r->", "NDR_SCALARS");
1330 pidl "if (!(ndr_flags & NDR_BUFFERS)) goto done;";
1331 foreach my $e (@{$struct->{ELEMENTS}}) {
1332 ParseElementPullBuffer($e, "r->");
1335 foreach my $e (@{$struct->{ELEMENTS}}) {
1336 CheckArraySizes($e, "r->");
1339 pidl "ndr_pull_struct_end(ndr);";
1346 #####################################################################
1347 # calculate size of ndr struct
1348 sub ParseStructNdrSize($)
1351 my $static = fn_prefix($t);
1354 if (my $flags = util::has_property($t, "flag")) {
1355 pidl "flags |= $flags;";
1357 pidl "return ndr_size_struct(r, flags, (ndr_push_flags_fn_t)ndr_push_$t->{NAME});";
1360 sub ArgsStructPush($)
1363 return "struct ndr_push *ndr, int ndr_flags, struct $e->{NAME} *r";
1366 sub ArgsStructPrint($)
1369 return "struct ndr_print *ndr, const char *name, struct $e->{NAME} *r";
1372 sub ArgsStructPull($)
1375 return "struct ndr_pull *ndr, int ndr_flags, struct $e->{NAME} *r";
1378 sub ArgsStructNdrSize($)
1381 return "const struct $d->{NAME} *r, int flags";
1384 $typefamily{STRUCT} = {
1385 PUSH_FN_BODY => \&ParseStructPush,
1386 PUSH_FN_ARGS => \&ArgsStructPush,
1387 PULL_FN_BODY => \&ParseStructPull,
1388 PULL_FN_ARGS => \&ArgsStructPull,
1389 PRINT_FN_BODY => \&ParseStructPrint,
1390 PRINT_FN_ARGS => \&ArgsStructPrint,
1391 SIZE_FN_BODY => \&ParseStructNdrSize,
1392 SIZE_FN_ARGS => \&ArgsStructNdrSize,
1393 ALIGN => \&find_largest_alignment
1396 #####################################################################
1397 # calculate size of ndr struct
1398 sub ParseUnionNdrSize($)
1401 my $static = fn_prefix($t);
1404 if (my $flags = util::has_property($t, "flag")) {
1405 pidl "flags |= $flags;";
1407 pidl "return ndr_size_union(r, flags, level, (ndr_push_union_fn_t)ndr_push_$t->{NAME});";
1410 #####################################################################
1411 # parse a union - push side
1412 sub ParseUnionPush($)
1415 my $have_default = 0;
1421 pidl "level = ndr_push_get_switch_value(ndr, r);";
1423 pidl "if (!(ndr_flags & NDR_SCALARS)) goto buffers;";
1425 if (!util::has_property($e, "nodiscriminant")) {
1426 my $switch_type = util::has_property($e, "switch_type");
1427 $switch_type = "uint32" unless (defined ($switch_type));
1428 pidl "NDR_CHECK(ndr_push_$switch_type(ndr, NDR_SCALARS, level));";
1431 pidl "NDR_CHECK(ndr_push_struct_start(ndr));";
1433 # my $align = union_alignment($e);
1434 # pidl "NDR_CHECK(ndr_push_align(ndr, $align));";
1436 pidl "switch (level) {";
1438 foreach my $el (@{$e->{ELEMENTS}}) {
1439 if (util::has_property($el, "default")) {
1443 pidl "case $el->{PROPERTIES}->{case}:";
1446 if ($el->{TYPE} ne "EMPTY") {
1448 ParseElementPushScalar($el, "r->", "NDR_SCALARS");
1454 if (! $have_default) {
1456 pidl "\treturn ndr_push_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);";
1461 pidl "if (!(ndr_flags & NDR_BUFFERS)) goto done;";
1462 pidl "switch (level) {";
1464 foreach my $el (@{$e->{ELEMENTS}}) {
1465 if (util::has_property($el, "default")) {
1468 pidl "case $el->{PROPERTIES}->{case}:";
1470 if ($el->{TYPE} ne "EMPTY") {
1472 ParseElementPushBuffer($el, "r->");
1478 if (! $have_default) {
1480 pidl "\treturn ndr_push_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);";
1484 pidl "ndr_push_struct_end(ndr);";
1489 #####################################################################
1491 sub ParseUnionPrint($)
1494 my $have_default = 0;
1495 my($name) = $e->{PARENT}->{NAME};
1497 pidl "ndr_print_union(ndr, name, level, \"$name\");";
1500 pidl "switch (level) {";
1502 foreach my $el (@{$e->{ELEMENTS}}) {
1503 if (util::has_property($el, "default")) {
1507 pidl "case $el->{PROPERTIES}->{case}:";
1509 if ($el->{TYPE} ne "EMPTY") {
1511 ParseElementPrint($el, "r->");
1517 if (! $have_default) {
1519 pidl "\tndr_print_bad_level(ndr, name, level);";
1527 #####################################################################
1528 # parse a union - pull side
1529 sub ParseUnionPull($)
1532 my $have_default = 0;
1533 my $switch_type = util::has_property($e, "switch_type");
1534 $switch_type = "uint32" unless defined($switch_type);
1537 if (!util::has_property($e, "nodiscriminant")) {
1538 if (typelist::typeIs($switch_type, "ENUM")) {
1539 $switch_type = typelist::enum_type_fn(typelist::getType($switch_type));
1541 pidl typelist::mapScalarType($switch_type) . " _level;";
1546 pidl "level = ndr_pull_get_switch_value(ndr, r);";
1548 pidl "if (!(ndr_flags & NDR_SCALARS)) goto buffers;";
1550 if (!util::has_property($e, "nodiscriminant")) {
1551 pidl "NDR_CHECK(ndr_pull_$switch_type(ndr, NDR_SCALARS, &_level));";
1552 pidl "if (_level != level) {";
1553 pidl "\treturn ndr_pull_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value %u for $e->{PARENT}->{NAME}\", _level);";
1557 pidl "NDR_CHECK(ndr_pull_struct_start(ndr));";
1559 # my $align = union_alignment($e);
1560 # pidl "\tNDR_CHECK(ndr_pull_align(ndr, $align));\n";
1562 pidl "switch (level) {";
1564 foreach my $el (@{$e->{ELEMENTS}}) {
1565 if (util::has_property($el, "default")) {
1569 pidl "case $el->{PROPERTIES}->{case}: {";
1571 if ($el->{TYPE} ne "EMPTY") {
1573 if ($el->{POINTERS}) {
1574 pidl "uint32_t _ptr_$el->{NAME};";
1576 ParseElementPullScalar($el, "r->", "NDR_SCALARS");
1582 if (! $have_default) {
1584 pidl "\treturn ndr_pull_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);";
1589 pidl "if (!(ndr_flags & NDR_BUFFERS)) goto done;";
1590 pidl "switch (level) {";
1592 foreach my $el (@{$e->{ELEMENTS}}) {
1593 if (util::has_property($el, "default")) {
1596 pidl "case $el->{PROPERTIES}->{case}:";
1598 if ($el->{TYPE} ne "EMPTY") {
1600 ParseElementPullBuffer($el, "r->");
1606 if (! $have_default) {
1608 pidl "\treturn ndr_pull_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);";
1612 pidl "ndr_pull_struct_end(ndr);";
1617 sub ArgsUnionPush($)
1620 return "struct ndr_push *ndr, int ndr_flags, union $e->{NAME} *r";
1623 sub ArgsUnionPrint($)
1626 return "struct ndr_print *ndr, const char *name, int level, union $e->{NAME} *r";
1629 sub ArgsUnionPull($)
1632 return "struct ndr_pull *ndr, int ndr_flags, union $e->{NAME} *r";
1635 sub ArgsUnionNdrSize($)
1638 return "const union $d->{NAME} *r, uint32_t level, int flags";
1641 $typefamily{UNION} = {
1642 PUSH_FN_BODY => \&ParseUnionPush,
1643 PUSH_FN_ARGS => \&ArgsUnionPush,
1644 PULL_FN_BODY => \&ParseUnionPull,
1645 PULL_FN_ARGS => \&ArgsUnionPull,
1646 PRINT_FN_BODY => \&ParseUnionPrint,
1647 PRINT_FN_ARGS => \&ArgsUnionPrint,
1648 SIZE_FN_ARGS => \&ArgsUnionNdrSize,
1649 SIZE_FN_BODY => \&ParseUnionNdrSize,
1650 ALIGN => \&find_largest_alignment
1653 #####################################################################
1654 # parse a typedef - push side
1655 sub ParseTypedefPush($)
1658 my $static = fn_prefix($e);
1660 return unless needed::is_needed("push_$e->{NAME}");
1662 my $args = $typefamily{$e->{DATA}->{TYPE}}->{PUSH_FN_ARGS}->($e);
1663 pidl $static . "NTSTATUS ndr_push_$e->{NAME}($args)";
1667 $typefamily{$e->{DATA}->{TYPE}}->{PUSH_FN_BODY}->($e->{DATA});
1668 pidl "return NT_STATUS_OK;";
1675 #####################################################################
1676 # parse a typedef - pull side
1677 sub ParseTypedefPull($)
1680 my $static = fn_prefix($e);
1682 return unless needed::is_needed("pull_$e->{NAME}");
1684 my $args = $typefamily{$e->{DATA}->{TYPE}}->{PULL_FN_ARGS}->($e);
1686 pidl $static . "NTSTATUS ndr_pull_$e->{NAME}($args)";
1690 $typefamily{$e->{DATA}->{TYPE}}->{PULL_FN_BODY}->($e->{DATA});
1691 pidl "return NT_STATUS_OK;";
1697 #####################################################################
1698 # parse a typedef - print side
1699 sub ParseTypedefPrint($)
1703 my $args = $typefamily{$e->{DATA}->{TYPE}}->{PRINT_FN_ARGS}->($e);
1705 return unless !util::has_property($e, "noprint");
1707 pidl "void ndr_print_$e->{NAME}($args)";
1710 $typefamily{$e->{DATA}->{TYPE}}->{PRINT_FN_BODY}->($e->{DATA});
1715 #####################################################################
1716 ## calculate the size of a structure
1717 sub ParseTypedefNdrSize($)
1721 return unless needed::is_needed("ndr_size_$t->{NAME}");
1723 my $tf = $typefamily{$t->{DATA}->{TYPE}};
1724 my $args = $tf->{SIZE_FN_ARGS}->($t);
1726 pidl "size_t ndr_size_$t->{NAME}($args)";
1729 $typefamily{$t->{DATA}->{TYPE}}->{SIZE_FN_BODY}->($t);
1735 #####################################################################
1736 # parse a function - print side
1737 sub ParseFunctionPrint($)
1741 return unless !util::has_property($fn, "noprint");
1743 pidl "void ndr_print_$fn->{NAME}(struct ndr_print *ndr, const char *name, int flags, struct $fn->{NAME} *r)";
1746 pidl "ndr_print_struct(ndr, name, \"$fn->{NAME}\");";
1747 pidl "ndr->depth++;";
1749 pidl "if (flags & NDR_SET_VALUES) {";
1750 pidl "\tndr->flags |= LIBNDR_PRINT_SET_VALUES;";
1753 pidl "if (flags & NDR_IN) {";
1755 pidl "ndr_print_struct(ndr, \"in\", \"$fn->{NAME}\");";
1756 pidl "ndr->depth++;";
1758 foreach my $e (@{$fn->{ELEMENTS}}) {
1759 if (util::has_property($e, "in")) {
1760 ParseElementPrint($e, "r->in.");
1763 pidl "ndr->depth--;";
1767 pidl "if (flags & NDR_OUT) {";
1769 pidl "ndr_print_struct(ndr, \"out\", \"$fn->{NAME}\");";
1770 pidl "ndr->depth++;";
1771 foreach my $e (@{$fn->{ELEMENTS}}) {
1772 if (util::has_property($e, "out")) {
1773 ParseElementPrint($e, "r->out.");
1776 if ($fn->{RETURN_TYPE} && $fn->{RETURN_TYPE} ne "void") {
1778 $cprefix = "" if (is_scalar_type($fn->{RETURN_TYPE})) ; # FIXME: Should really use util::c_push_prefix here
1779 pidl "ndr_print_$fn->{RETURN_TYPE}(ndr, \"result\", $cprefix"."r->out.result);";
1781 pidl "ndr->depth--;";
1785 pidl "ndr->depth--;";
1791 #####################################################################
1792 # parse a function element
1793 sub ParseFunctionElementPush($$)
1798 if (util::array_size($e)) {
1799 if (need_wire_pointer($e)) {
1800 ParsePtrPush($e, "r->$inout.");
1801 pidl "if (r->$inout.$e->{NAME}) {";
1803 ParseArrayPush($e, "ndr", "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1807 ParseArrayPush($e, "ndr", "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1810 ParseElementPushScalar($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1811 if (need_wire_pointer($e)) {
1812 ParseElementPushBuffer($e, "r->$inout.");
1817 #####################################################################
1819 sub ParseFunctionPush($)
1822 my $static = fn_prefix($fn);
1824 return unless !util::has_property($fn, "nopush");
1826 pidl $static . "NTSTATUS ndr_push_$fn->{NAME}(struct ndr_push *ndr, int flags, struct $fn->{NAME} *r)";
1830 pidl "if (!(flags & NDR_IN)) goto ndr_out;";
1833 foreach my $e (@{$fn->{ELEMENTS}}) {
1834 if (util::has_property($e, "in")) {
1835 if (util::has_property($e, "ref")) {
1836 check_null_pointer("*r->in.$e->{NAME}");
1838 ParseFunctionElementPush($e, "in");
1843 pidl "if (!(flags & NDR_OUT)) goto done;";
1846 foreach my $e (@{$fn->{ELEMENTS}}) {
1847 if (util::has_property($e, "out")) {
1848 if (util::has_property($e, "ref")) {
1849 check_null_pointer("*r->out.$e->{NAME}");
1851 ParseFunctionElementPush($e, "out");
1855 if ($fn->{RETURN_TYPE} && $fn->{RETURN_TYPE} ne "void") {
1856 pidl "NDR_CHECK(ndr_push_$fn->{RETURN_TYPE}(ndr, NDR_SCALARS, r->out.result));";
1860 pidl "return NT_STATUS_OK;";
1866 #####################################################################
1867 # parse a function element
1868 sub ParseFunctionElementPull($$)
1873 if (util::array_size($e)) {
1874 if (need_wire_pointer($e)) {
1875 pidl "NDR_CHECK(ndr_pull_unique_ptr(ndr, &_ptr_$e->{NAME}));";
1876 pidl "r->$inout.$e->{NAME} = NULL;";
1877 pidl "if (_ptr_$e->{NAME}) {";
1879 } elsif ($inout eq "out" && util::has_property($e, "ref")) {
1880 pidl "if (r->$inout.$e->{NAME}) {";
1884 ParseArrayPull($e, "ndr", "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1886 if (need_wire_pointer($e) or ($inout eq "out" and util::has_property($e, "ref"))) {
1891 if ($inout eq "out" && util::has_property($e, "ref")) {
1892 pidl "if (ndr->flags & LIBNDR_FLAG_REF_ALLOC) {";
1893 pidl "\tNDR_ALLOC(ndr, r->out.$e->{NAME});";
1897 if ($inout eq "in" && util::has_property($e, "ref")) {
1898 pidl "NDR_ALLOC(ndr, r->in.$e->{NAME});";
1901 ParseElementPullScalar($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1902 if (need_wire_pointer($e)) {
1903 ParseElementPullBuffer($e, "r->$inout.");
1908 ############################################################
1909 # allocate ref variables
1910 sub AllocateRefVars($)
1913 my $asize = util::array_size($e);
1915 # note that if the variable is also an "in"
1916 # variable then we copy the initial value from
1919 if (!defined $asize) {
1920 # its a simple variable
1921 pidl "NDR_ALLOC(ndr, r->out.$e->{NAME});";
1922 if (util::has_property($e, "in")) {
1923 pidl "*r->out.$e->{NAME} = *r->in.$e->{NAME};";
1925 pidl "ZERO_STRUCTP(r->out.$e->{NAME});";
1931 my $size = ParseExpr($e, $asize, "r->out.");
1932 check_null_pointer($size);
1933 pidl "NDR_ALLOC_N(ndr, r->out.$e->{NAME}, $size);";
1934 if (util::has_property($e, "in")) {
1935 pidl "memcpy(r->out.$e->{NAME},r->in.$e->{NAME},$size * sizeof(*r->in.$e->{NAME}));";
1937 pidl "memset(r->out.$e->{NAME}, 0, $size * sizeof(*r->out.$e->{NAME}));";
1941 #####################################################################
1943 sub ParseFunctionPull($)
1946 my $static = fn_prefix($fn);
1948 return unless !util::has_property($fn, "nopull");
1950 # pull function args
1951 pidl $static . "NTSTATUS ndr_pull_$fn->{NAME}(struct ndr_pull *ndr, int flags, struct $fn->{NAME} *r)";
1955 # declare any internal pointers we need
1956 foreach my $e (@{$fn->{ELEMENTS}}) {
1957 if (need_wire_pointer($e)) {
1958 pidl "uint32_t _ptr_$e->{NAME};";
1962 pidl "if (!(flags & NDR_IN)) goto ndr_out;";
1965 # auto-init the out section of a structure. I originally argued that
1966 # this was a bad idea as it hides bugs, but coping correctly
1967 # with initialisation and not wiping ref vars is turning
1968 # out to be too tricky (tridge)
1969 foreach my $e (@{$fn->{ELEMENTS}}) {
1970 if (util::has_property($e, "out")) {
1971 pidl "ZERO_STRUCT(r->out);";
1977 foreach my $e (@{$fn->{ELEMENTS}}) {
1978 if (util::has_property($e, "in")) {
1979 ParseFunctionElementPull($e, "in");
1981 # we need to allocate any reference output variables, so that
1982 # a dcerpc backend can be sure they are non-null
1983 if (util::has_property($e, "out") && util::has_property($e, "ref")) {
1984 AllocateRefVars($e);
1988 foreach my $e (@{$fn->{ELEMENTS}}) {
1989 if (util::has_property($e, "in")) {
1990 CheckArraySizes($e, "r->in.");
1995 pidl "if (!(flags & NDR_OUT)) goto done;";
1998 foreach my $e (@{$fn->{ELEMENTS}}) {
1999 if (util::has_property($e, "out")) {
2000 ParseFunctionElementPull($e, "out");
2004 foreach my $e (@{$fn->{ELEMENTS}}) {
2005 if (util::has_property($e, "out")) {
2006 CheckArraySizes($e, "r->out.");
2010 if ($fn->{RETURN_TYPE} && $fn->{RETURN_TYPE} ne "void") {
2011 pidl "NDR_CHECK(ndr_pull_$fn->{RETURN_TYPE}(ndr, NDR_SCALARS, &r->out.result));";
2016 pidl "return NT_STATUS_OK;";
2022 #####################################################################
2023 # produce a function call table
2024 sub FunctionTable($)
2026 my($interface) = shift;
2027 my($data) = $interface->{INHERITED_DATA};
2029 my $uname = uc $interface->{NAME};
2031 foreach my $d (@{$data}) {
2032 if ($d->{TYPE} eq "FUNCTION") { $count++; }
2035 return if ($count == 0);
2037 pidl "static const struct dcerpc_interface_call $interface->{NAME}\_calls[] = {";
2038 foreach my $d (@{$data}) {
2039 if ($d->{TYPE} eq "FUNCTION") {
2041 pidl "\t\t\"$d->{NAME}\",";
2042 pidl "\t\tsizeof(struct $d->{NAME}),";
2043 pidl "\t\t(ndr_push_flags_fn_t) ndr_push_$d->{NAME},";
2044 pidl "\t\t(ndr_pull_flags_fn_t) ndr_pull_$d->{NAME},";
2045 pidl "\t\t(ndr_print_function_t) ndr_print_$d->{NAME}";
2049 pidl "\t{ NULL, 0, NULL, NULL, NULL }";
2053 # If no endpoint is set, default to the interface name as a named pipe
2054 if (! defined $interface->{PROPERTIES}->{endpoint}) {
2055 $interface->{PROPERTIES}->{endpoint} = "\"ncacn_np:[\\\\pipe\\\\" . $interface->{NAME} . "]\"";
2058 my @e = split / /, $interface->{PROPERTIES}->{endpoint};
2059 my $endpoint_count = $#e + 1;
2061 pidl "static const char * const $interface->{NAME}\_endpoint_strings[] = {";
2062 foreach my $ep (@e) {
2068 pidl "static const struct dcerpc_endpoint_list $interface->{NAME}\_endpoints = {";
2069 pidl "\t.count\t= $endpoint_count,";
2070 pidl "\t.names\t= $interface->{NAME}\_endpoint_strings";
2074 if (! defined $interface->{PROPERTIES}->{authservice}) {
2075 $interface->{PROPERTIES}->{authservice} = "\"host\"";
2078 my @a = split / /, $interface->{PROPERTIES}->{authservice};
2079 my $authservice_count = $#a + 1;
2081 pidl "static const char * const $interface->{NAME}\_authservice_strings[] = {";
2082 foreach my $ap (@a) {
2088 pidl "static const struct dcerpc_authservice_list $interface->{NAME}\_authservices = {";
2089 pidl "\t.count\t= $endpoint_count,";
2090 pidl "\t.names\t= $interface->{NAME}\_authservice_strings";
2094 pidl "\nconst struct dcerpc_interface_table dcerpc_table_$interface->{NAME} = {";
2095 pidl "\t.name\t\t= \"$interface->{NAME}\",";
2096 pidl "\t.uuid\t\t= DCERPC_$uname\_UUID,";
2097 pidl "\t.if_version\t= DCERPC_$uname\_VERSION,";
2098 pidl "\t.helpstring\t= DCERPC_$uname\_HELPSTRING,";
2099 pidl "\t.num_calls\t= $count,";
2100 pidl "\t.calls\t\t= $interface->{NAME}\_calls,";
2101 pidl "\t.endpoints\t= &$interface->{NAME}\_endpoints,";
2102 pidl "\t.authservices\t= &$interface->{NAME}\_authservices";
2106 pidl "static NTSTATUS dcerpc_ndr_$interface->{NAME}_init(void)";
2108 pidl "\treturn librpc_register_interface(&dcerpc_table_$interface->{NAME});";
2113 #####################################################################
2114 # parse the interface definitions
2115 sub ParseInterface($)
2117 my($interface) = shift;
2118 my($data) = $interface->{DATA};
2121 foreach my $d (@{$data}) {
2122 ($d->{TYPE} eq "TYPEDEF") &&
2123 ParseTypedefPush($d);
2124 ($d->{TYPE} eq "FUNCTION") &&
2125 ParseFunctionPush($d);
2129 foreach my $d (@{$data}) {
2130 ($d->{TYPE} eq "TYPEDEF") &&
2131 ParseTypedefPull($d);
2132 ($d->{TYPE} eq "FUNCTION") &&
2133 ParseFunctionPull($d);
2137 foreach my $d (@{$data}) {
2138 ($d->{TYPE} eq "TYPEDEF") &&
2139 ParseTypedefPrint($d);
2140 ($d->{TYPE} eq "FUNCTION") &&
2141 ParseFunctionPrint($d);
2145 foreach my $d (@{$data}) {
2146 ($d->{TYPE} eq "TYPEDEF") &&
2147 ParseTypedefNdrSize($d);
2150 FunctionTable($interface);
2153 sub RegistrationFunction($$)
2156 my $filename = shift;
2158 $filename =~ /.*\/ndr_(.*).c/;
2160 pidl "NTSTATUS dcerpc_$basename\_init(void)";
2163 pidl "NTSTATUS status = NT_STATUS_OK;";
2164 foreach my $interface (@{$idl}) {
2165 next if $interface->{TYPE} ne "INTERFACE";
2167 my $data = $interface->{INHERITED_DATA};
2169 foreach my $d (@{$data}) {
2170 if ($d->{TYPE} eq "FUNCTION") { $count++; }
2173 next if ($count == 0);
2175 pidl "status = dcerpc_ndr_$interface->{NAME}_init();";
2176 pidl "if (NT_STATUS_IS_ERR(status)) {";
2177 pidl "\treturn status;";
2181 pidl "return status;";
2187 sub CheckPointerTypes($$)
2190 my $default = shift;
2192 foreach my $e (@{$s->{ELEMENTS}}) {
2193 if ($e->{POINTERS}) {
2194 if (not defined(pointer_type($e))) {
2195 $e->{PROPERTIES}->{$default} = 1;
2198 if (pointer_type($e) eq "ptr") {
2199 print "Warning: ptr is not supported by pidl yet\n";
2205 sub LoadInterface($)
2209 if (not util::has_property($x, "pointer_default")) {
2210 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
2211 # and "unique" in Microsoft Extensions mode (default)
2212 $x->{PROPERTIES}->{pointer_default} = "unique";
2215 foreach my $d (@{$x->{DATA}}) {
2216 if (($d->{TYPE} eq "DECLARE") or ($d->{TYPE} eq "TYPEDEF")) {
2217 if ($d->{DATA}->{TYPE} eq "STRUCT" or $d->{DATA}->{TYPE} eq "UNION") {
2218 CheckPointerTypes($d->{DATA}, $x->{PROPERTIES}->{pointer_default});
2221 if (defined($d->{PROPERTIES}) && !defined($d->{DATA}->{PROPERTIES})) {
2222 $d->{DATA}->{PROPERTIES} = $d->{PROPERTIES};
2225 if ($d->{TYPE} eq "FUNCTION") {
2226 CheckPointerTypes($d,
2227 $x->{PROPERTIES}->{pointer_default} # MIDL defaults to "ref"
2237 foreach my $x (@{$idl}) {
2242 #####################################################################
2243 # parse a parsed IDL structure back into an IDL file
2247 my($filename) = shift;
2248 my $h_filename = $filename;
2253 if ($h_filename =~ /(.*)\.c/) {
2254 $h_filename = "$1.h";
2257 pidl "/* parser auto-generated by pidl */";
2259 pidl "#include \"includes.h\"";
2260 pidl "#include \"$h_filename\"";
2263 foreach my $x (@{$idl}) {
2264 if ($x->{TYPE} eq "INTERFACE") {
2265 needed::BuildNeeded($x);
2270 RegistrationFunction($idl, $filename);