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
26 return $typefamily{$n};
29 sub RegisterPrimitives
()
50 "error_status_t" => 4,
60 foreach my $k (keys %type_alignments) {
66 ALIGN
=> $type_alignments{$k}
76 if (my $dt = $typedefs{$type}->{DATA
}->{TYPE
}) {
77 return 1 if ($dt eq "SCALAR" or $dt eq "ENUM" or $dt eq "BITMAP");
87 return undef unless $e->{POINTERS
};
89 return "ref" if (util
::has_property
($e, "ref"));
90 return "ptr" if (util
::has_property
($e, "ptr"));
91 return "unique" if (util
::has_property
($e, "unique"));
92 return "relative" if (util
::has_property
($e, "relative"));
93 return "ignore" if (util
::has_property
($e, "ignore"));
98 # return 1 if this is a fixed array
102 my $len = $e->{"ARRAY_LEN"};
103 if (defined $len && util
::is_constant
($len)) {
109 # return 1 if this is a conformant array
110 sub is_conformant_array
($)
113 return defined($e->{CONFORMANT_SIZE
});
116 # return 1 if this is a inline array
117 sub is_inline_array
($)
120 my $len = $e->{"ARRAY_LEN"};
121 if (is_fixed_array
($e) ||
122 defined $len && $len ne "*") {
128 sub is_varying_array
($)
131 return 1 if (util
::has_property
($e, "length_is"));
139 return "conformant-varying" if (is_varying_array
($e) and is_conformant_array
($e));
140 return "conformant" if (is_varying_array
($e));
141 return "varying" if (is_varying_array
($e));
142 return "inline" if (is_inline_array
($e));
143 return "fixed" if (is_fixed_array
($e));
148 # determine if an element needs a reference pointer on the wire
149 # in its NDR representation
150 sub need_wire_pointer
($)
155 return 0 unless ($pt = pointer_type
($e));
164 # determine if an element is a pure scalar. pure scalars do not
165 # have a "buffers" section in NDR
166 sub is_pure_scalar
($)
169 if (util
::has_property
($e, "ref")) {
172 if (is_scalar_type
($e->{TYPE
}) &&
174 !util
::array_size
($e)) {
180 # see if a variable needs to be allocated by the NDR subsystem on pull
185 if (util
::has_property
($e, "ref")) {
189 if ($e->{POINTERS
} || util
::array_size
($e)) {
196 # determine the C prefix used to refer to a variable when passing to a push
197 # function. This will be '*' for pointers to scalar types, '' for scalar
198 # types and normal pointers and '&' for pass-by-reference structures
203 if ($e->{TYPE
} =~ "string") {
207 if (is_scalar_type
($e->{TYPE
}) &&
211 if (!is_scalar_type
($e->{TYPE
}) &&
213 !util
::array_size
($e)) {
219 # determine the C prefix used to refer to a variable when passing to a pull
225 if (!$e->{POINTERS
} && !util
::array_size
($e)) {
229 if ($e->{TYPE
} =~ "string") {
254 $tabs = substr($tabs, 0, -1);
257 ###################################
258 # find a sibling var in a structure
263 my($fn) = $e->{PARENT
};
265 if ($name =~ /\*(.*)/) {
269 for my $e2 (@
{$fn->{ELEMENTS
}}) {
270 return $e2 if ($e2->{NAME
} eq $name);
273 die "invalid sibling '$name'";
276 ####################################################################
277 # work out the name of a size_is() variable
282 my($var_prefix) = shift;
284 my($fn) = $e->{PARENT
};
286 return $size if (util
::is_constant
($size));
288 return $size if ($size =~ /ndr->|\(/);
292 if ($size =~ /\*(.*)/) {
297 if ($fn->{TYPE
} ne "FUNCTION") {
298 return $prefix . "r->$size";
301 my $e2 = find_sibling
($e, $size);
303 if (util
::has_property
($e2, "in") && util
::has_property
($e2, "out")) {
304 return $prefix . "$var_prefix$size";
307 if (util
::has_property
($e2, "in")) {
308 return $prefix . "r->in.$size";
311 if (util
::has_property
($e2, "out")) {
312 return $prefix . "r->out.$size";
315 die "invalid variable in $size for element $e->{NAME} in $fn->{NAME}\n";
318 #####################################################################
319 # check that a variable we get from ParseExpr isn't a null pointer
320 sub check_null_pointer
($)
323 if ($size =~ /^\*/) {
324 my $size2 = substr($size, 1);
325 pidl
"if ($size2 == NULL) return NT_STATUS_INVALID_PARAMETER_MIX;";
329 #####################################################################
330 # check that a variable we get from ParseExpr isn't a null pointer
331 # void return varient
332 sub check_null_pointer_void
($)
335 if ($size =~ /^\*/) {
336 my $size2 = substr($size, 1);
337 pidl
"if ($size2 == NULL) return;";
341 #####################################################################
342 # work out is a parse function should be declared static or not
346 if ($fn->{TYPE
} eq "TYPEDEF") {
347 if (util
::has_property
($fn, "public")) {
352 if ($fn->{TYPE
} eq "FUNCTION") {
353 if (util
::has_property
($fn, "public")) {
360 ###################################################################
361 # setup any special flags for an element or structure
365 my $flags = util
::has_property
($e, "flag");
366 if (defined $flags) {
367 pidl
"{ uint32_t _flags_save_$e->{TYPE} = ndr->flags;";
368 pidl
"ndr_set_flags(&ndr->flags, $flags);";
372 ###################################################################
373 # end any special flags for an element or structure
377 my $flags = util
::has_property
($e, "flag");
378 if (defined $flags) {
379 pidl
"ndr->flags = _flags_save_$e->{TYPE};\n\t}";
383 #####################################################################
384 # work out the correct alignment for a structure or union
385 sub find_largest_alignment
($)
390 for my $e (@
{$s->{ELEMENTS
}}) {
393 if (need_wire_pointer
($e)) {
396 $a = align_type
($e->{TYPE
});
399 $align = $a if ($align < $a);
405 #####################################################################
411 unless (defined($typedefs{$e}) && defined($typedefs{$e}->{DATA
}->{TYPE
})) {
412 # it must be an external type - all we can do is guess
413 # print "Warning: assuming alignment of unknown type '$e' is 4\n";
417 my $dt = $typedefs{$e}->{DATA
};
419 return $dt->{ALIGN
} if ($dt->{ALIGN
});
420 return $typefamily{$dt->{TYPE
}}->{ALIGN
}->($dt);
423 #####################################################################
424 # parse an array - push side
425 sub ParseArrayPush
($$$)
428 my $var_prefix = shift;
429 my $ndr_flags = shift;
431 my $size = ParseExpr
($e, util
::array_size
($e), $var_prefix);
433 if (is_conformant_array
($e) ) {
434 # the conformant size has already been pushed
435 } elsif (!is_inline_array
($e)) {
436 # we need to emit the array size
437 pidl
"NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, $size));";
440 if (my $length = util
::has_property
($e, "length_is")) {
441 $length = ParseExpr
($e, $length, $var_prefix);
442 pidl
"NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));";
443 pidl
"NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, $length));";
447 if (is_scalar_type
($e->{TYPE
})) {
448 pidl
"NDR_CHECK(ndr_push_array_$e->{TYPE}(ndr, $ndr_flags, $var_prefix$e->{NAME}, $size));";
450 pidl
"NDR_CHECK(ndr_push_array(ndr, $ndr_flags, $var_prefix$e->{NAME}, sizeof($var_prefix$e->{NAME}\[0]), $size, (ndr_push_flags_fn_t)ndr_push_$e->{TYPE}));";
454 #####################################################################
456 sub ParseArrayPrint
($$)
459 my $var_prefix = shift;
460 my $size = ParseExpr
($e, util
::array_size
($e), $var_prefix);
461 my $length = util
::has_property
($e, "length_is");
463 if (defined $length) {
464 $size = ParseExpr
($e, $length, $var_prefix);
467 if (is_scalar_type
($e->{TYPE
})) {
468 pidl
"ndr_print_array_$e->{TYPE}(ndr, \"$e->{NAME}\", $var_prefix$e->{NAME}, $size);";
470 pidl
"ndr_print_array(ndr, \"$e->{NAME}\", $var_prefix$e->{NAME}, sizeof($var_prefix$e->{NAME}\[0]), $size, (ndr_print_fn_t)ndr_print_$e->{TYPE});";
474 #####################################################################
475 # check the size_is and length_is constraints
476 sub CheckArraySizes
($$)
479 my $var_prefix = shift;
481 if (!is_conformant_array
($e) &&
482 util
::has_property
($e, "size_is")) {
483 my $size = ParseExpr
($e, util
::array_size
($e), $var_prefix);
484 pidl
"if ($var_prefix$e->{NAME}) {";
486 check_null_pointer
($size);
487 pidl
"NDR_CHECK(ndr_check_array_size(ndr, (void*)&$var_prefix$e->{NAME}, $size));";
492 if (my $length = util
::has_property
($e, "length_is")) {
493 $length = ParseExpr
($e, $length, $var_prefix);
494 pidl
"if ($var_prefix$e->{NAME}) {";
496 check_null_pointer
($length);
497 pidl
"NDR_CHECK(ndr_check_array_length(ndr, (void*)&$var_prefix$e->{NAME}, $length));";
503 #####################################################################
504 # parse an array - pull side
505 sub ParseArrayPull
($$$)
508 my $var_prefix = shift;
509 my $ndr_flags = shift;
511 my $size = ParseExpr
($e, util
::array_size
($e), $var_prefix);
512 my $alloc_size = $size;
514 # if this is a conformant array then we use that size to allocate, and make sure
515 # we allocate enough to pull the elements
516 if (is_conformant_array
($e)) {
517 $alloc_size = $e->{CONFORMANT_SIZE
};
518 check_null_pointer
($size);
519 pidl
"if ($size > $alloc_size) {";
521 pidl
"return ndr_pull_error(ndr, NDR_ERR_CONFORMANT_SIZE, \"Bad conformant size %u should be %u\", $alloc_size, $size);";
524 } elsif (!is_inline_array
($e)) {
525 if ($var_prefix =~ /^r->out/ && $size =~ /^\*r->in/) {
526 my $size2 = substr($size, 1);
527 pidl
"if (ndr->flags & LIBNDR_FLAG_REF_ALLOC) { NDR_ALLOC(ndr, $size2); }";
530 # non fixed arrays encode the size just before the array
531 pidl
"NDR_CHECK(ndr_pull_array_size(ndr, &$var_prefix$e->{NAME}));";
532 $alloc_size = "ndr_get_array_size(ndr, &$var_prefix$e->{NAME})";
535 if ((need_alloc
($e) && !is_fixed_array
($e)) ||
536 ($var_prefix eq "r->in." && util
::has_property
($e, "ref"))) {
537 if (!is_inline_array
($e) || $ndr_flags eq "NDR_SCALARS") {
538 pidl
"NDR_ALLOC_N(ndr, $var_prefix$e->{NAME}, $alloc_size);";
542 if (($var_prefix eq "r->out." && util
::has_property
($e, "ref"))) {
543 if (!is_inline_array
($e) || $ndr_flags eq "NDR_SCALARS") {
544 pidl
"if (ndr->flags & LIBNDR_FLAG_REF_ALLOC) {";
546 pidl
"NDR_ALLOC_N(ndr, $var_prefix$e->{NAME}, $alloc_size);";
552 if (my $length = util
::has_property
($e, "length_is")) {
553 pidl
"NDR_CHECK(ndr_pull_array_length(ndr, &$var_prefix$e->{NAME}));";
554 $size = "ndr_get_array_length(ndr, &$var_prefix$e->{NAME})";
557 check_null_pointer
($size);
558 if (is_scalar_type
($e->{TYPE
})) {
559 pidl
"NDR_CHECK(ndr_pull_array_$e->{TYPE}(ndr, $ndr_flags, $var_prefix$e->{NAME}, $size));";
561 pidl
"NDR_CHECK(ndr_pull_array(ndr, $ndr_flags, (void **)$var_prefix$e->{NAME}, sizeof($var_prefix$e->{NAME}\[0]), $size, (ndr_pull_flags_fn_t)ndr_pull_$e->{TYPE}));";
565 #####################################################################
566 # parse scalars in a structure element
567 sub ParseElementPushScalar
($$$)
570 my($var_prefix) = shift;
571 my($ndr_flags) = shift;
572 my $cprefix = c_push_prefix
($e);
573 my $sub_size = util
::has_property
($e, "subcontext");
577 if (my $value = util
::has_property
($e, "value")) {
578 pidl
"$cprefix$var_prefix$e->{NAME} = $value;";
581 if (util
::has_property
($e, "relative")) {
582 pidl
"NDR_CHECK(ndr_push_relative_ptr1(ndr, $var_prefix$e->{NAME}));";
583 } elsif (is_inline_array
($e)) {
584 ParseArrayPush
($e, "r->", "NDR_SCALARS");
585 } elsif (need_wire_pointer
($e)) {
586 ParseElementPushPtr
($e, $var_prefix, $ndr_flags);
587 } elsif (need_alloc
($e)) {
588 # no scalar component
589 } elsif (my $switch = util
::has_property
($e, "switch_is")) {
590 ParseElementPushSwitch
($e, $var_prefix, $ndr_flags, $switch);
591 } elsif (defined $sub_size) {
592 pidl
"NDR_CHECK(ndr_push_subcontext_flags_fn(ndr, $sub_size, $cprefix$var_prefix$e->{NAME}, (ndr_push_flags_fn_t) ndr_push_$e->{TYPE}));";
594 pidl
"NDR_CHECK(ndr_push_$e->{TYPE}(ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}));";
600 #####################################################################
601 # parse a pointer in a struct element or function
602 sub ParseElementPushPtr
($$$)
605 my $var_prefix = shift;
606 my $ndr_flags = shift;
608 pidl
"NDR_CHECK(ndr_push_unique_ptr(ndr, $var_prefix$e->{NAME}));";
612 #####################################################################
613 # print scalars in a structure element
614 sub ParseElementPrintScalar
($$)
617 my($var_prefix) = shift;
618 my $cprefix = c_push_prefix
($e);
620 if (util
::has_property
($e, "noprint")) {
624 if (my $value = util
::has_property
($e, "value")) {
625 pidl
"if (ndr->flags & LIBNDR_PRINT_SET_VALUES) {";
627 pidl
"$cprefix$var_prefix$e->{NAME} = $value;";
632 if (is_fixed_array
($e)) {
633 ParseElementPrintBuffer
($e, $var_prefix);
634 } elsif ($e->{POINTERS
} || util
::array_size
($e)) {
635 pidl
"ndr_print_ptr(ndr, \"$e->{NAME}\", $var_prefix$e->{NAME});";
636 pidl
"ndr->depth++;";
637 ParseElementPrintBuffer
($e, $var_prefix);
638 pidl
"ndr->depth--;";
639 } elsif (my $switch = util
::has_property
($e, "switch_is")) {
640 ParseElementPrintSwitch
($e, $var_prefix, $switch);
642 pidl
"ndr_print_$e->{TYPE}(ndr, \"$e->{NAME}\", $cprefix$var_prefix$e->{NAME});";
646 #####################################################################
647 # parse scalars in a structure element - pull size
648 sub ParseElementPullSwitch
($$$$)
651 my($var_prefix) = shift;
652 my($ndr_flags) = shift;
654 my $switch_var = ParseExpr
($e, $switch, $var_prefix);
656 my $cprefix = c_pull_prefix
($e);
658 my $utype = $typedefs{$e->{TYPE
}};
660 check_null_pointer
($switch_var);
662 if (!defined $utype ||
663 !util
::has_property
($utype, "nodiscriminant")) {
664 my $e2 = find_sibling
($e, $switch);
665 my $type_decl = util
::map_type
($e2->{TYPE
});
666 pidl
"if (($ndr_flags) & NDR_SCALARS) {";
668 if ($typedefs{$e2->{TYPE
}}->{DATA
}->{TYPE
} eq "ENUM") {
669 $type_decl = util
::enum_type_decl
($e2);
670 } elsif ($typedefs{$e2->{TYPE
}}->{DATA
}->{TYPE
} eq "BITMAP") {
671 $type_decl = util
::bitmap_type_decl
($e2);
673 pidl
"$type_decl _level;";
674 pidl
"NDR_CHECK(ndr_pull_$e2->{TYPE}(ndr, NDR_SCALARS, &_level));";
675 if ($switch_var =~ /r->in/) {
676 pidl
"if (!(ndr->flags & LIBNDR_FLAG_REF_ALLOC) && _level != $switch_var) {";
679 pidl
"if (_level != $switch_var) {";
682 pidl
"return ndr_pull_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value %u in $e->{NAME}\", _level);";
684 if ($switch_var =~ /r->/) {
685 pidl
"} else { $switch_var = _level; }";
693 my $sub_size = util
::has_property
($e, "subcontext");
694 if (defined $sub_size) {
695 pidl
"if (($ndr_flags) & NDR_SCALARS) {";
697 pidl
"NDR_CHECK(ndr_pull_subcontext_union_fn(ndr, $sub_size, $switch_var, $cprefix$var_prefix$e->{NAME}, (ndr_pull_union_fn_t) ndr_pull_$e->{TYPE}));";
701 pidl
"NDR_CHECK(ndr_pull_$e->{TYPE}(ndr, $ndr_flags, $switch_var, $cprefix$var_prefix$e->{NAME}));";
707 #####################################################################
708 # push switch element
709 sub ParseElementPushSwitch
($$$$)
712 my($var_prefix) = shift;
713 my($ndr_flags) = shift;
715 my $switch_var = ParseExpr
($e, $switch, $var_prefix);
716 my $cprefix = c_push_prefix
($e);
718 check_null_pointer
($switch_var);
720 my $utype = $typedefs{$e->{TYPE
}};
721 if (!defined $utype ||
722 !util
::has_property
($utype, "nodiscriminant")) {
723 my $e2 = find_sibling
($e, $switch);
724 pidl
"if (($ndr_flags) & NDR_SCALARS) {";
726 pidl
"NDR_CHECK(ndr_push_$e2->{TYPE}(ndr, NDR_SCALARS, $switch_var));";
731 my $sub_size = util
::has_property
($e, "subcontext");
732 if (defined $sub_size) {
733 pidl
"if(($ndr_flags) & NDR_SCALARS) {";
735 pidl
"NDR_CHECK(ndr_push_subcontext_union_fn(ndr, $sub_size, $switch_var, $cprefix$var_prefix$e->{NAME}, (ndr_push_union_fn_t) ndr_push_$e->{TYPE}));";
739 pidl
"NDR_CHECK(ndr_push_$e->{TYPE}(ndr, $ndr_flags, $switch_var, $cprefix$var_prefix$e->{NAME}));";
743 #####################################################################
744 # print scalars in a structure element
745 sub ParseElementPrintSwitch
($$$)
748 my($var_prefix) = shift;
750 my $switch_var = ParseExpr
($e, $switch, $var_prefix);
751 my $cprefix = c_push_prefix
($e);
753 check_null_pointer_void
($switch_var);
755 pidl
"ndr_print_$e->{TYPE}(ndr, \"$e->{NAME}\", $switch_var, $cprefix$var_prefix$e->{NAME});";
759 #####################################################################
760 # parse scalars in a structure element - pull size
761 sub ParseElementPullScalar
($$$)
764 my($var_prefix) = shift;
765 my($ndr_flags) = shift;
766 my $cprefix = c_pull_prefix
($e);
767 my $sub_size = util
::has_property
($e, "subcontext");
771 if (is_inline_array
($e)) {
772 ParseArrayPull
($e, "r->", "NDR_SCALARS");
773 } elsif (need_wire_pointer
($e)) {
774 ParseElementPullPtr
($e, $var_prefix, $ndr_flags);
775 } elsif (need_alloc
($e)) {
776 # no scalar component
777 } elsif (my $switch = util
::has_property
($e, "switch_is")) {
778 ParseElementPullSwitch
($e, $var_prefix, $ndr_flags, $switch);
779 } elsif (defined $sub_size) {
780 pidl
"NDR_CHECK(ndr_pull_subcontext_flags_fn(ndr, $sub_size, $cprefix$var_prefix$e->{NAME}, (ndr_pull_flags_fn_t) ndr_pull_$e->{TYPE}));";
782 pidl
"NDR_CHECK(ndr_pull_$e->{TYPE}(ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}));";
784 if (my $range = util
::has_property
($e, "range")) {
785 my ($low, $high) = split(/ /, $range, 2);
786 pidl
"if ($var_prefix$e->{NAME} < $low || $var_prefix$e->{NAME} > $high) {";
787 pidl
"\treturn ndr_pull_error(ndr, NDR_ERR_RANGE, \"value out of range\");";
794 #####################################################################
795 # parse a pointer in a struct element or function
796 sub ParseElementPullPtr
($$$)
799 my($var_prefix) = shift;
800 my($ndr_flags) = shift;
802 pidl
"NDR_CHECK(ndr_pull_unique_ptr(ndr, &_ptr_$e->{NAME}));";
803 pidl
"if (_ptr_$e->{NAME}) {";
805 pidl
"NDR_ALLOC(ndr, $var_prefix$e->{NAME});";
806 if (util
::has_property
($e, "relative")) {
807 pidl
"NDR_CHECK(ndr_pull_relative_ptr1(ndr, $var_prefix$e->{NAME}, _ptr_$e->{NAME}));";
811 pidl
"\t$var_prefix$e->{NAME} = NULL;";
815 #####################################################################
816 # parse buffers in a structure element
817 sub ParseElementPushBuffer
($$$)
820 my($var_prefix) = shift;
821 my($ndr_flags) = shift;
822 my $cprefix = c_push_prefix
($e);
823 my $sub_size = util
::has_property
($e, "subcontext");
825 if (is_pure_scalar
($e)) {
831 if (need_wire_pointer
($e)) {
832 pidl
"if ($var_prefix$e->{NAME}) {";
834 if (util
::has_property
($e, "relative")) {
835 pidl
"NDR_CHECK(ndr_push_relative_ptr2(ndr, $var_prefix$e->{NAME}));";
839 if (is_inline_array
($e)) {
840 ParseArrayPush
($e, "r->", "NDR_BUFFERS");
841 } elsif (util
::array_size
($e)) {
842 ParseArrayPush
($e, "r->", "NDR_SCALARS|NDR_BUFFERS");
843 } elsif (my $switch = util
::has_property
($e, "switch_is")) {
844 if ($e->{POINTERS
}) {
845 ParseElementPushSwitch
($e, $var_prefix, "NDR_BUFFERS|NDR_SCALARS", $switch);
847 ParseElementPushSwitch
($e, $var_prefix, "NDR_BUFFERS", $switch);
849 } elsif (defined $sub_size) {
850 if ($e->{POINTERS
}) {
851 pidl
"NDR_CHECK(ndr_push_subcontext_flags_fn(ndr, $sub_size, $cprefix$var_prefix$e->{NAME}, (ndr_push_flags_fn_t) ndr_push_$e->{TYPE}));";
853 } elsif ($e->{POINTERS
}) {
854 pidl
"NDR_CHECK(ndr_push_$e->{TYPE}(ndr, NDR_SCALARS|NDR_BUFFERS, $cprefix$var_prefix$e->{NAME}));";
856 pidl
"NDR_CHECK(ndr_push_$e->{TYPE}(ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}));";
859 if (need_wire_pointer
($e)) {
867 #####################################################################
868 # print buffers in a structure element
869 sub ParseElementPrintBuffer
($$)
872 my($var_prefix) = shift;
873 my $cprefix = c_push_prefix
($e);
875 if (need_wire_pointer
($e)) {
876 pidl
"if ($var_prefix$e->{NAME}) {";
880 if (util
::array_size
($e)) {
881 ParseArrayPrint
($e, $var_prefix)
882 } elsif (my $switch = util
::has_property
($e, "switch_is")) {
883 ParseElementPrintSwitch
($e, $var_prefix, $switch);
885 pidl
"ndr_print_$e->{TYPE}(ndr, \"$e->{NAME}\", $cprefix$var_prefix$e->{NAME});";
888 if (need_wire_pointer
($e)) {
894 #####################################################################
895 # parse buffers in a structure element - pull side
896 sub ParseElementPullBuffer
($$$)
899 my($var_prefix) = shift;
900 my($ndr_flags) = shift;
901 my $cprefix = c_pull_prefix
($e);
902 my $sub_size = util
::has_property
($e, "subcontext");
904 if (is_pure_scalar
($e)) {
910 if (need_wire_pointer
($e)) {
911 pidl
"if ($var_prefix$e->{NAME}) {";
913 if (util
::has_property
($e, "relative")) {
914 pidl
"struct ndr_pull_save _relative_save;";
915 pidl
"ndr_pull_save(ndr, &_relative_save);";
916 pidl
"NDR_CHECK(ndr_pull_relative_ptr2(ndr, $var_prefix$e->{NAME}));";
920 if (is_inline_array
($e)) {
921 ParseArrayPull
($e, "r->", "NDR_BUFFERS");
922 } elsif (util
::array_size
($e)) {
923 ParseArrayPull
($e, "r->", "NDR_SCALARS|NDR_BUFFERS");
924 } elsif (my $switch = util
::has_property
($e, "switch_is")) {
925 if ($e->{POINTERS
}) {
926 ParseElementPullSwitch
($e, $var_prefix, "NDR_SCALARS|NDR_BUFFERS", $switch);
928 ParseElementPullSwitch
($e, $var_prefix, "NDR_BUFFERS", $switch);
930 } elsif (defined $sub_size) {
931 if ($e->{POINTERS
}) {
932 pidl
"NDR_CHECK(ndr_pull_subcontext_flags_fn(ndr, $sub_size, $cprefix$var_prefix$e->{NAME}, (ndr_pull_flags_fn_t) ndr_pull_$e->{TYPE}));";
934 } elsif ($e->{POINTERS
}) {
935 pidl
"NDR_CHECK(ndr_pull_$e->{TYPE}(ndr, NDR_SCALARS|NDR_BUFFERS, $cprefix$var_prefix$e->{NAME}));";
937 pidl
"NDR_CHECK(ndr_pull_$e->{TYPE}(ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}));";
940 if (need_wire_pointer
($e)) {
941 if (util
::has_property
($e, "relative")) {
942 pidl
"ndr_pull_restore(ndr, &_relative_save);";
951 #####################################################################
953 sub ParseStructPush
($)
957 return unless defined($struct->{ELEMENTS
});
959 start_flags
($struct);
961 # see if the structure contains a conformant array. If it
962 # does, then it must be the last element of the structure, and
963 # we need to push the conformant length early, as it fits on
964 # the wire before the structure (and even before the structure
966 my $e = $struct->{ELEMENTS
}[-1];
967 if (defined $e->{ARRAY_LEN
} && $e->{ARRAY_LEN
} eq "*") {
968 my $size = ParseExpr
($e, util
::array_size
($e), "r->");
969 $e->{CONFORMANT_SIZE
} = $size;
970 check_null_pointer
($size);
971 pidl
"NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, $size));";
974 if (defined $e->{TYPE
} && $e->{TYPE
} eq "string"
975 && util
::property_matches
($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
976 pidl
"NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, ndr_string_array_size(ndr, r->$e->{NAME})));";
979 pidl
"if (!(ndr_flags & NDR_SCALARS)) goto buffers;";
981 pidl
"NDR_CHECK(ndr_push_struct_start(ndr));";
983 my $align = find_largest_alignment
($struct);
984 pidl
"NDR_CHECK(ndr_push_align(ndr, $align));";
986 foreach my $e (@
{$struct->{ELEMENTS
}}) {
987 ParseElementPushScalar
($e, "r->", "NDR_SCALARS");
991 pidl
"if (!(ndr_flags & NDR_BUFFERS)) goto done;";
992 foreach my $e (@
{$struct->{ELEMENTS
}}) {
993 ParseElementPushBuffer
($e, "r->", "NDR_BUFFERS");
996 pidl
"ndr_push_struct_end(ndr);";
1003 #####################################################################
1004 # generate a push function for an enum
1005 sub ParseEnumPush
($)
1008 my($type_fn) = util
::enum_type_fn
($enum);
1012 pidl
"NDR_CHECK(ndr_push_$type_fn(ndr, NDR_SCALARS, r));";
1017 #####################################################################
1018 # generate a pull function for an enum
1019 sub ParseEnumPull
($)
1022 my($type_fn) = util
::enum_type_fn
($enum);
1023 my($type_v_decl) = util
::map_type
(util
::enum_type_fn
($enum));
1025 pidl
"$type_v_decl v;";
1027 pidl
"NDR_CHECK(ndr_pull_$type_fn(ndr, NDR_SCALARS, &v));";
1033 #####################################################################
1034 # generate a print function for an enum
1035 sub ParseEnumPrint
($)
1039 pidl
"const char *val = NULL;";
1044 pidl
"switch (r) {";
1046 my $els = \@
{$enum->{ELEMENTS
}};
1047 foreach my $i (0 .. $#{$els}) {
1048 my $e = ${$els}[$i];
1050 if ($e =~ /^(.*)=/) {
1053 pidl
"case $e: val = \"$e\"; break;";
1059 pidl
"ndr_print_enum(ndr, name, \"$enum->{TYPE}\", val, r);";
1067 return "struct ndr_push *ndr, int ndr_flags, enum $e->{NAME} r";
1070 sub ArgsEnumPrint
($)
1073 return "struct ndr_print *ndr, const char *name, enum $e->{NAME} r";
1079 return "struct ndr_pull *ndr, int ndr_flags, enum $e->{NAME} *r";
1082 $typefamily{ENUM
} = {
1083 PUSH_FN_BODY
=> \
&ParseEnumPush
,
1084 PUSH_FN_ARGS
=> \
&ArgsEnumPush
,
1085 PULL_FN_BODY
=> \
&ParseEnumPull
,
1086 PULL_FN_ARGS
=> \
&ArgsEnumPull
,
1087 PRINT_FN_BODY
=> \
&ParseEnumPrint
,
1088 PRINT_FN_ARGS
=> \
&ArgsEnumPrint
,
1089 ALIGN
=> sub { return align_type
(util
::enum_type_fn
(shift)); }
1092 #####################################################################
1093 # generate a push function for a bitmap
1094 sub ParseBitmapPush
($)
1096 my($bitmap) = shift;
1097 my($type_fn) = util
::bitmap_type_fn
($bitmap);
1099 start_flags
($bitmap);
1101 pidl
"NDR_CHECK(ndr_push_$type_fn(ndr, NDR_SCALARS, r));";
1106 #####################################################################
1107 # generate a pull function for an bitmap
1108 sub ParseBitmapPull
($)
1110 my($bitmap) = shift;
1111 my($type_fn) = util
::bitmap_type_fn
($bitmap);
1112 my($type_decl) = util
::bitmap_type_decl
($bitmap);
1114 pidl
"$type_decl v;";
1115 start_flags
($bitmap);
1116 pidl
"NDR_CHECK(ndr_pull_$type_fn(ndr, NDR_SCALARS, &v));";
1122 #####################################################################
1123 # generate a print function for an bitmap
1124 sub ParseBitmapPrintElement
($$)
1127 my($bitmap) = shift;
1128 my($type_decl) = util
::bitmap_type_decl
($bitmap);
1129 my($type_fn) = util
::bitmap_type_fn
($bitmap);
1130 my($name) = $bitmap->{PARENT
}->{NAME
};
1133 if ($e =~ /^(\w+) .*$/) {
1136 die "Bitmap: \"$name\" invalid Flag: \"$e\"";
1139 pidl
"ndr_print_bitmap_flag(ndr, sizeof($type_decl), \"$flag\", $flag, r);";
1142 #####################################################################
1143 # generate a print function for an bitmap
1144 sub ParseBitmapPrint
($)
1146 my($bitmap) = shift;
1147 my($type_decl) = util
::bitmap_type_decl
($bitmap);
1148 my($type_fn) = util
::bitmap_type_fn
($bitmap);
1150 start_flags
($bitmap);
1152 pidl
"ndr_print_$type_fn(ndr, name, r);";
1154 pidl
"ndr->depth++;";
1155 foreach my $e (@
{$bitmap->{ELEMENTS
}}) {
1156 ParseBitmapPrintElement
($e, $bitmap);
1158 pidl
"ndr->depth--;";
1163 sub ArgsBitmapPush
($)
1166 my $type_decl = util
::bitmap_type_decl
($e->{DATA
});
1167 return "struct ndr_push *ndr, int ndr_flags, $type_decl r";
1170 sub ArgsBitmapPrint
($)
1173 my $type_decl = util
::bitmap_type_decl
($e->{DATA
});
1174 return "struct ndr_print *ndr, const char *name, $type_decl r";
1177 sub ArgsBitmapPull
($)
1180 my $type_decl = util
::bitmap_type_decl
($e->{DATA
});
1181 return "struct ndr_pull *ndr, int ndr_flags, $type_decl *r";
1184 $typefamily{BITMAP
} = {
1185 PUSH_FN_BODY
=> \
&ParseBitmapPush
,
1186 PUSH_FN_ARGS
=> \
&ArgsBitmapPush
,
1187 PULL_FN_BODY
=> \
&ParseBitmapPull
,
1188 PULL_FN_ARGS
=> \
&ArgsBitmapPull
,
1189 PRINT_FN_BODY
=> \
&ParseBitmapPrint
,
1190 PRINT_FN_ARGS
=> \
&ArgsBitmapPrint
,
1191 ALIGN
=> sub { return align_type
(util
::bitmap_type_fn
(shift)); }
1194 #####################################################################
1195 # generate a struct print function
1196 sub ParseStructPrint
($)
1198 my($struct) = shift;
1199 my($name) = $struct->{PARENT
}->{NAME
};
1201 return unless defined $struct->{ELEMENTS
};
1203 pidl
"ndr_print_struct(ndr, name, \"$name\");";
1205 start_flags
($struct);
1207 pidl
"ndr->depth++;";
1208 foreach my $e (@
{$struct->{ELEMENTS
}}) {
1209 ParseElementPrintScalar
($e, "r->");
1211 pidl
"ndr->depth--;";
1216 #####################################################################
1217 # parse a struct - pull side
1218 sub ParseStructPull
($)
1220 my($struct) = shift;
1223 return unless defined $struct->{ELEMENTS
};
1225 # see if the structure contains a conformant array. If it
1226 # does, then it must be the last element of the structure, and
1227 # we need to pull the conformant length early, as it fits on
1228 # the wire before the structure (and even before the structure
1230 my $e = $struct->{ELEMENTS
}[-1];
1231 if (defined $e->{ARRAY_LEN
} && $e->{ARRAY_LEN
} eq "*") {
1235 if (defined $e->{TYPE
} && $e->{TYPE
} eq "string"
1236 && util
::property_matches
($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
1240 if (defined $conform_e) {
1242 pidl
"uint32_t _conformant_size;";
1243 $conform_e->{CONFORMANT_SIZE
} = "_conformant_size";
1246 # declare any internal pointers we need
1247 foreach my $e (@
{$struct->{ELEMENTS
}}) {
1248 if (need_wire_pointer
($e)) {
1249 pidl
"uint32_t _ptr_$e->{NAME};";
1253 start_flags
($struct);
1255 pidl
"if (!(ndr_flags & NDR_SCALARS)) goto buffers;";
1257 pidl
"NDR_CHECK(ndr_pull_struct_start(ndr));";
1259 if (defined $conform_e) {
1260 pidl
"NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &$conform_e->{CONFORMANT_SIZE}));";
1263 my $align = find_largest_alignment
($struct);
1264 pidl
"NDR_CHECK(ndr_pull_align(ndr, $align));";
1266 foreach my $e (@
{$struct->{ELEMENTS
}}) {
1267 ParseElementPullScalar
($e, "r->", "NDR_SCALARS");
1271 pidl
"if (!(ndr_flags & NDR_BUFFERS)) goto done;";
1272 foreach my $e (@
{$struct->{ELEMENTS
}}) {
1273 ParseElementPullBuffer
($e, "r->", "NDR_BUFFERS");
1276 foreach my $e (@
{$struct->{ELEMENTS
}}) {
1277 CheckArraySizes
($e, "r->");
1280 pidl
"ndr_pull_struct_end(ndr);";
1287 #####################################################################
1288 # calculate size of ndr struct
1289 sub ParseStructNdrSize
($)
1292 my $static = fn_prefix
($t);
1295 pidl
"size_t ndr_size_$t->{NAME}(const struct $t->{NAME} *r, int flags)";
1298 if (my $flags = util
::has_property
($t, "flag")) {
1299 pidl
"flags |= $flags;";
1301 pidl
"return ndr_size_struct(r, flags, (ndr_push_flags_fn_t)ndr_push_$t->{NAME});";
1307 sub ArgsStructPush
($)
1310 return "struct ndr_push *ndr, int ndr_flags, struct $e->{NAME} *r";
1313 sub ArgsStructPrint
($)
1316 return "struct ndr_print *ndr, const char *name, struct $e->{NAME} *r";
1319 sub ArgsStructPull
($)
1322 return "struct ndr_pull *ndr, int ndr_flags, struct $e->{NAME} *r";
1325 $typefamily{STRUCT
} = {
1326 PUSH_FN_BODY
=> \
&ParseStructPush
,
1327 PUSH_FN_ARGS
=> \
&ArgsStructPush
,
1328 PULL_FN_BODY
=> \
&ParseStructPull
,
1329 PULL_FN_ARGS
=> \
&ArgsStructPull
,
1330 PRINT_FN_BODY
=> \
&ParseStructPrint
,
1331 PRINT_FN_ARGS
=> \
&ArgsStructPrint
,
1332 SIZE_FN
=> \
&ParseStructNdrSize
,
1333 ALIGN
=> \
&find_largest_alignment
1336 #####################################################################
1337 # calculate size of ndr struct
1338 sub ParseUnionNdrSize
($)
1341 my $static = fn_prefix
($t);
1344 pidl
"size_t ndr_size_$t->{NAME}(const union $t->{NAME} *r, int level, int flags)";
1347 if (my $flags = util
::has_property
($t, "flag")) {
1348 pidl
"flags |= $flags;";
1350 pidl
"return ndr_size_union(r, flags, level, (ndr_push_union_fn_t)ndr_push_$t->{NAME});";
1356 #####################################################################
1357 # parse a union - push side
1358 sub ParseUnionPush
($)
1361 my $have_default = 0;
1365 pidl
"if (!(ndr_flags & NDR_SCALARS)) goto buffers;";
1367 pidl
"NDR_CHECK(ndr_push_struct_start(ndr));";
1369 # my $align = union_alignment($e);
1370 # pidl "NDR_CHECK(ndr_push_align(ndr, $align));";
1372 pidl
"switch (level) {";
1374 foreach my $el (@
{$e->{ELEMENTS
}}) {
1375 if (util
::has_property
($el, "default")) {
1379 pidl
"case $el->{PROPERTIES}->{case}:";
1382 if ($el->{TYPE
} ne "EMPTY") {
1384 ParseElementPushScalar
($el, "r->", "NDR_SCALARS");
1390 if (! $have_default) {
1392 pidl
"\treturn ndr_push_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);";
1397 pidl
"if (!(ndr_flags & NDR_BUFFERS)) goto done;";
1398 pidl
"switch (level) {";
1400 foreach my $el (@
{$e->{ELEMENTS
}}) {
1401 if (util
::has_property
($el, "default")) {
1404 pidl
"case $el->{PROPERTIES}->{case}:";
1406 if ($el->{TYPE
} ne "EMPTY") {
1408 ParseElementPushBuffer
($el, "r->", "NDR_BUFFERS");
1414 if (! $have_default) {
1416 pidl
"\treturn ndr_push_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);";
1420 pidl
"ndr_push_struct_end(ndr);";
1425 #####################################################################
1427 sub ParseUnionPrint
($)
1430 my $have_default = 0;
1431 my($name) = $e->{PARENT
}->{NAME
};
1433 pidl
"ndr_print_union(ndr, name, level, \"$name\");";
1436 pidl
"switch (level) {";
1438 foreach my $el (@
{$e->{ELEMENTS
}}) {
1439 if (util
::has_property
($el, "default")) {
1443 pidl
"case $el->{PROPERTIES}->{case}:";
1445 if ($el->{TYPE
} ne "EMPTY") {
1447 ParseElementPrintScalar
($el, "r->");
1453 if (! $have_default) {
1455 pidl
"\tndr_print_bad_level(ndr, name, level);";
1463 #####################################################################
1464 # parse a union - pull side
1465 sub ParseUnionPull
($)
1468 my $have_default = 0;
1472 pidl
"if (!(ndr_flags & NDR_SCALARS)) goto buffers;";
1474 pidl
"NDR_CHECK(ndr_pull_struct_start(ndr));";
1476 # my $align = union_alignment($e);
1477 # pidl "\tNDR_CHECK(ndr_pull_align(ndr, $align));\n";
1479 pidl
"switch (level) {";
1481 foreach my $el (@
{$e->{ELEMENTS
}}) {
1482 if (util
::has_property
($el, "default")) {
1486 pidl
"case $el->{PROPERTIES}->{case}: {";
1488 if ($el->{TYPE
} ne "EMPTY") {
1490 if ($el->{POINTERS
}) {
1491 pidl
"uint32_t _ptr_$el->{NAME};";
1493 ParseElementPullScalar
($el, "r->", "NDR_SCALARS");
1499 if (! $have_default) {
1501 pidl
"\treturn ndr_pull_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);";
1506 pidl
"if (!(ndr_flags & NDR_BUFFERS)) goto done;";
1507 pidl
"switch (level) {";
1509 foreach my $el (@
{$e->{ELEMENTS
}}) {
1510 if (util
::has_property
($el, "default")) {
1513 pidl
"case $el->{PROPERTIES}->{case}:";
1515 if ($el->{TYPE
} ne "EMPTY") {
1517 ParseElementPullBuffer
($el, "r->", "NDR_BUFFERS");
1523 if (! $have_default) {
1525 pidl
"\treturn ndr_pull_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);";
1529 pidl
"ndr_pull_struct_end(ndr);";
1534 sub ArgsUnionPush
($)
1537 return "struct ndr_push *ndr, int ndr_flags, int level, union $e->{NAME} *r";
1540 sub ArgsUnionPrint
($)
1543 return "struct ndr_print *ndr, const char *name, int level, union $e->{NAME} *r";
1546 sub ArgsUnionPull
($)
1549 return "struct ndr_pull *ndr, int ndr_flags, int level, union $e->{NAME} *r";
1552 $typefamily{UNION
} = {
1553 PUSH_FN_BODY
=> \
&ParseUnionPush
,
1554 PUSH_FN_ARGS
=> \
&ArgsUnionPush
,
1555 PULL_FN_BODY
=> \
&ParseUnionPull
,
1556 PULL_FN_ARGS
=> \
&ArgsUnionPull
,
1557 PRINT_FN_BODY
=> \
&ParseUnionPrint
,
1558 PRINT_FN_ARGS
=> \
&ArgsUnionPrint
,
1559 SIZE_FN
=> \
&ParseUnionNdrSize
,
1560 ALIGN
=> \
&find_largest_alignment
1563 #####################################################################
1564 # parse a typedef - push side
1565 sub ParseTypedefPush
($)
1568 my $static = fn_prefix
($e);
1570 return unless needed
::is_needed
("push_$e->{NAME}");
1572 my $args = $typefamily{$e->{DATA
}->{TYPE
}}->{PUSH_FN_ARGS
}->($e);
1573 pidl
$static . "NTSTATUS ndr_push_$e->{NAME}($args)";
1577 $typefamily{$e->{DATA
}->{TYPE
}}->{PUSH_FN_BODY
}->($e->{DATA
});
1578 pidl
"return NT_STATUS_OK;";
1585 #####################################################################
1586 # parse a typedef - pull side
1587 sub ParseTypedefPull
($)
1590 my $static = fn_prefix
($e);
1592 return unless needed
::is_needed
("pull_$e->{NAME}");
1594 my $args = $typefamily{$e->{DATA
}->{TYPE
}}->{PULL_FN_ARGS
}->($e);
1596 pidl
$static . "NTSTATUS ndr_pull_$e->{NAME}($args)";
1600 $typefamily{$e->{DATA
}->{TYPE
}}->{PULL_FN_BODY
}->($e->{DATA
});
1601 pidl
"return NT_STATUS_OK;";
1607 #####################################################################
1608 # parse a typedef - print side
1609 sub ParseTypedefPrint
($)
1613 my $args = $typefamily{$e->{DATA
}->{TYPE
}}->{PRINT_FN_ARGS
}->($e);
1615 pidl
"void ndr_print_$e->{NAME}($args)";
1618 $typefamily{$e->{DATA
}->{TYPE
}}->{PRINT_FN_BODY
}->($e->{DATA
});
1623 #####################################################################
1624 ## calculate the size of a structure
1625 sub ParseTypedefNdrSize
($)
1629 return unless needed
::is_needed
("ndr_size_$t->{NAME}");
1631 $typefamily{$t->{DATA
}->{TYPE
}}->{SIZE_FN
}->($t);
1634 #####################################################################
1635 # parse a function - print side
1636 sub ParseFunctionPrint
($)
1640 pidl
"void ndr_print_$fn->{NAME}(struct ndr_print *ndr, const char *name, int flags, struct $fn->{NAME} *r)";
1643 pidl
"ndr_print_struct(ndr, name, \"$fn->{NAME}\");";
1644 pidl
"ndr->depth++;";
1646 pidl
"if (flags & NDR_SET_VALUES) {";
1647 pidl
"\tndr->flags |= LIBNDR_PRINT_SET_VALUES;";
1650 pidl
"if (flags & NDR_IN) {";
1652 pidl
"ndr_print_struct(ndr, \"in\", \"$fn->{NAME}\");";
1653 pidl
"ndr->depth++;";
1655 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1656 if (util
::has_property
($e, "in")) {
1657 ParseElementPrintScalar
($e, "r->in.");
1660 pidl
"ndr->depth--;";
1664 pidl
"if (flags & NDR_OUT) {";
1666 pidl
"ndr_print_struct(ndr, \"out\", \"$fn->{NAME}\");";
1667 pidl
"ndr->depth++;";
1668 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1669 if (util
::has_property
($e, "out")) {
1670 ParseElementPrintScalar
($e, "r->out.");
1673 if ($fn->{RETURN_TYPE
} && $fn->{RETURN_TYPE
} ne "void") {
1675 $cprefix = "" if (is_scalar_type
($fn->{RETURN_TYPE
})) ; # FIXME: Should really use util::c_push_prefix here
1676 pidl
"ndr_print_$fn->{RETURN_TYPE}(ndr, \"result\", $cprefix"."r->out.result);";
1678 pidl
"ndr->depth--;";
1682 pidl
"ndr->depth--;";
1688 #####################################################################
1689 # parse a function element
1690 sub ParseFunctionElementPush
($$)
1695 if (util
::array_size
($e)) {
1696 if (need_wire_pointer
($e)) {
1697 pidl
"NDR_CHECK(ndr_push_unique_ptr(ndr, r->$inout.$e->{NAME}));";
1698 pidl
"if (r->$inout.$e->{NAME}) {";
1700 ParseArrayPush
($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1704 ParseArrayPush
($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1707 ParseElementPushScalar
($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1709 if ($e->{POINTERS
}) {
1710 ParseElementPushBuffer
($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1715 #####################################################################
1717 sub ParseFunctionPush
($)
1720 my $static = fn_prefix
($fn);
1722 pidl
$static . "NTSTATUS ndr_push_$fn->{NAME}(struct ndr_push *ndr, int flags, struct $fn->{NAME} *r)";
1726 pidl
"if (!(flags & NDR_IN)) goto ndr_out;";
1729 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1730 if (util
::has_property
($e, "in")) {
1731 ParseFunctionElementPush
($e, "in");
1736 pidl
"if (!(flags & NDR_OUT)) goto done;";
1739 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1740 if (util
::has_property
($e, "out")) {
1741 ParseFunctionElementPush
($e, "out");
1745 if ($fn->{RETURN_TYPE
} && $fn->{RETURN_TYPE
} ne "void") {
1746 pidl
"NDR_CHECK(ndr_push_$fn->{RETURN_TYPE}(ndr, NDR_SCALARS, r->out.result));";
1750 pidl
"return NT_STATUS_OK;";
1756 #####################################################################
1757 # parse a function element
1758 sub ParseFunctionElementPull
($$)
1763 if (util
::array_size
($e)) {
1764 if (need_wire_pointer
($e)) {
1765 pidl
"NDR_CHECK(ndr_pull_unique_ptr(ndr, &_ptr_$e->{NAME}));";
1766 pidl
"r->$inout.$e->{NAME} = NULL;";
1767 pidl
"if (_ptr_$e->{NAME}) {";
1769 } elsif ($inout eq "out" && util
::has_property
($e, "ref")) {
1770 pidl
"if (r->$inout.$e->{NAME}) {";
1776 ParseArrayPull
($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1780 if ($inout eq "out" && util
::has_property
($e, "ref")) {
1781 pidl
"if (ndr->flags & LIBNDR_FLAG_REF_ALLOC) {";
1782 pidl
"\tNDR_ALLOC(ndr, r->out.$e->{NAME});";
1785 if ($inout eq "in" && util
::has_property
($e, "ref")) {
1786 pidl
"NDR_ALLOC(ndr, r->in.$e->{NAME});";
1789 ParseElementPullScalar
($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1790 if ($e->{POINTERS
}) {
1791 ParseElementPullBuffer
($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1796 ############################################################
1797 # allocate ref variables
1798 sub AllocateRefVars
($)
1801 my $asize = util
::array_size
($e);
1803 # note that if the variable is also an "in"
1804 # variable then we copy the initial value from
1807 if (!defined $asize) {
1808 # its a simple variable
1809 pidl
"NDR_ALLOC(ndr, r->out.$e->{NAME});";
1810 if (util
::has_property
($e, "in")) {
1811 pidl
"*r->out.$e->{NAME} = *r->in.$e->{NAME};";
1813 pidl
"ZERO_STRUCTP(r->out.$e->{NAME});";
1819 my $size = ParseExpr
($e, $asize, "r->out.");
1820 check_null_pointer
($size);
1821 pidl
"NDR_ALLOC_N(ndr, r->out.$e->{NAME}, $size);";
1822 if (util
::has_property
($e, "in")) {
1823 pidl
"memcpy(r->out.$e->{NAME},r->in.$e->{NAME},$size * sizeof(*r->in.$e->{NAME}));";
1825 pidl
"memset(r->out.$e->{NAME}, 0, $size * sizeof(*r->out.$e->{NAME}));";
1829 #####################################################################
1831 sub ParseFunctionPull
($)
1834 my $static = fn_prefix
($fn);
1836 # pull function args
1837 pidl
$static . "NTSTATUS ndr_pull_$fn->{NAME}(struct ndr_pull *ndr, int flags, struct $fn->{NAME} *r)";
1841 # declare any internal pointers we need
1842 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1843 if (need_wire_pointer
($e)) {
1844 pidl
"uint32_t _ptr_$e->{NAME};";
1848 pidl
"if (!(flags & NDR_IN)) goto ndr_out;";
1851 # auto-init the out section of a structure. I originally argued that
1852 # this was a bad idea as it hides bugs, but coping correctly
1853 # with initialisation and not wiping ref vars is turning
1854 # out to be too tricky (tridge)
1855 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1856 if (util
::has_property
($e, "out")) {
1857 pidl
"ZERO_STRUCT(r->out);";
1863 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1864 if (util
::has_property
($e, "in")) {
1865 ParseFunctionElementPull
($e, "in");
1867 # we need to allocate any reference output variables, so that
1868 # a dcerpc backend can be sure they are non-null
1869 if (util
::has_property
($e, "out") && util
::has_property
($e, "ref")) {
1870 AllocateRefVars
($e);
1874 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1875 if (util
::has_property
($e, "in")) {
1876 CheckArraySizes
($e, "r->in.");
1881 pidl
"if (!(flags & NDR_OUT)) goto done;";
1884 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1885 if (util
::has_property
($e, "out")) {
1886 ParseFunctionElementPull
($e, "out");
1890 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1891 if (util
::has_property
($e, "out")) {
1892 CheckArraySizes
($e, "r->out.");
1896 if ($fn->{RETURN_TYPE
} && $fn->{RETURN_TYPE
} ne "void") {
1897 pidl
"NDR_CHECK(ndr_pull_$fn->{RETURN_TYPE}(ndr, NDR_SCALARS, &r->out.result));";
1902 pidl
"return NT_STATUS_OK;";
1908 #####################################################################
1909 # produce a function call table
1910 sub FunctionTable
($)
1912 my($interface) = shift;
1913 my($data) = $interface->{INHERITED_DATA
};
1915 my $uname = uc $interface->{NAME
};
1917 foreach my $d (@
{$data}) {
1918 if ($d->{TYPE
} eq "FUNCTION") { $count++; }
1921 return if ($count == 0);
1923 pidl
"static const struct dcerpc_interface_call $interface->{NAME}\_calls[] = {";
1924 foreach my $d (@
{$data}) {
1925 if ($d->{TYPE
} eq "FUNCTION") {
1927 pidl
"\t\t\"$d->{NAME}\",";
1928 pidl
"\t\tsizeof(struct $d->{NAME}),";
1929 pidl
"\t\t(ndr_push_flags_fn_t) ndr_push_$d->{NAME},";
1930 pidl
"\t\t(ndr_pull_flags_fn_t) ndr_pull_$d->{NAME},";
1931 pidl
"\t\t(ndr_print_function_t) ndr_print_$d->{NAME}";
1935 pidl
"\t{ NULL, 0, NULL, NULL, NULL }";
1939 # If no endpoint is set, default to the interface name as a named pipe
1940 if (! defined $interface->{PROPERTIES
}->{endpoint
}) {
1941 $interface->{PROPERTIES
}->{endpoint
} = "\"ncacn_np:[\\\\pipe\\\\" . $interface->{NAME
} . "]\"";
1944 my @e = split / /, $interface->{PROPERTIES
}->{endpoint
};
1945 my $endpoint_count = $#e + 1;
1947 pidl
"static const char * const $interface->{NAME}\_endpoint_strings[] = {";
1948 foreach my $ep (@e) {
1954 pidl
"static const struct dcerpc_endpoint_list $interface->{NAME}\_endpoints = {";
1955 pidl
"\t$endpoint_count, $interface->{NAME}\_endpoint_strings";
1959 pidl
"\nconst struct dcerpc_interface_table dcerpc_table_$interface->{NAME} = {";
1960 pidl
"\t\"$interface->{NAME}\",";
1961 pidl
"\tDCERPC_$uname\_UUID,";
1962 pidl
"\tDCERPC_$uname\_VERSION,";
1963 pidl
"\tDCERPC_$uname\_HELPSTRING,";
1965 pidl
"\t$interface->{NAME}\_calls,";
1966 pidl
"\t&$interface->{NAME}\_endpoints";
1970 pidl
"static NTSTATUS dcerpc_ndr_$interface->{NAME}_init(void)";
1972 pidl
"\treturn librpc_register_interface(&dcerpc_table_$interface->{NAME});";
1977 #####################################################################
1978 # parse the interface definitions
1979 sub ParseInterface
($)
1981 my($interface) = shift;
1982 my($data) = $interface->{DATA
};
1985 foreach my $d (@
{$data}) {
1986 ($d->{TYPE
} eq "TYPEDEF") &&
1987 ParseTypedefPush
($d);
1988 ($d->{TYPE
} eq "FUNCTION") &&
1989 ParseFunctionPush
($d);
1993 foreach my $d (@
{$data}) {
1994 ($d->{TYPE
} eq "TYPEDEF") &&
1995 ParseTypedefPull
($d);
1996 ($d->{TYPE
} eq "FUNCTION") &&
1997 ParseFunctionPull
($d);
2001 foreach my $d (@
{$data}) {
2002 if ($d->{TYPE
} eq "TYPEDEF" &&
2003 !util
::has_property
($d, "noprint")) {
2004 ParseTypedefPrint
($d);
2006 if ($d->{TYPE
} eq "FUNCTION" &&
2007 !util
::has_property
($d, "noprint")) {
2008 ParseFunctionPrint
($d);
2013 foreach my $d (@
{$data}) {
2014 ($d->{TYPE
} eq "TYPEDEF") &&
2015 ParseTypedefNdrSize
($d);
2018 FunctionTable
($interface);
2021 sub RegistrationFunction
($$)
2024 my $filename = shift;
2026 $filename =~ /.*\/ndr_
(.*).c
/;
2028 pidl
"NTSTATUS dcerpc_$basename\_init(void)";
2031 pidl
"NTSTATUS status = NT_STATUS_OK;";
2032 foreach my $interface (@
{$idl}) {
2033 next if $interface->{TYPE
} ne "INTERFACE";
2035 my $data = $interface->{INHERITED_DATA
};
2037 foreach my $d (@
{$data}) {
2038 if ($d->{TYPE
} eq "FUNCTION") { $count++; }
2041 next if ($count == 0);
2043 pidl
"status = dcerpc_ndr_$interface->{NAME}_init();";
2044 pidl
"if (NT_STATUS_IS_ERR(status)) {";
2045 pidl
"\treturn status;";
2049 pidl
"return status;";
2055 sub CheckPointerTypes
($$)
2058 my $default = shift;
2060 foreach my $e (@
{$s->{ELEMENTS
}}) {
2061 if ($e->{POINTERS
}) {
2062 if (not defined(pointer_type
($e))) {
2063 $e->{PROPERTIES
}->{$default} = 1;
2066 if (pointer_type
($e) eq "ptr") {
2067 print "Warning: ptr is not supported by pidl yet\n";
2073 sub LoadInterface
($)
2077 if (not util
::has_property
($x, "pointer_default")) {
2078 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
2079 # and "unique" in Microsoft Extensions mode (default)
2080 $x->{PROPERTIES
}->{pointer_default
} = "unique";
2083 foreach my $d (@
{$x->{DATA
}}) {
2084 if (($d->{TYPE
} eq "DECLARE") or ($d->{TYPE
} eq "TYPEDEF")) {
2085 $typedefs{$d->{NAME
}} = $d;
2086 if ($d->{DATA
}->{TYPE
} eq "STRUCT" or $d->{DATA
}->{TYPE
} eq "UNION") {
2087 CheckPointerTypes
($d->{DATA
}, $x->{PROPERTIES
}->{pointer_default
});
2090 if (defined($d->{PROPERTIES
}) && !defined($d->{DATA
}->{PROPERTIES
})) {
2091 $d->{DATA
}->{PROPERTIES
} = $d->{PROPERTIES
};
2094 if ($d->{TYPE
} eq "FUNCTION") {
2095 CheckPointerTypes
($d,
2096 $x->{PROPERTIES
}->{pointer_default
} # MIDL defaults to "ref"
2102 # Add ORPC specific bits to an interface.
2103 sub InterfaceORPC
($)
2106 # Add [in] ORPCTHIS *this, [out] ORPCTHAT *that
2107 # for 'object' interfaces
2108 if (util
::has_property
($x, "object")) {
2109 foreach my $e (@
{$x->{DATA
}}) {
2110 if($e->{TYPE
} eq "FUNCTION") {
2111 $e->{PROPERTIES
}->{object
} = 1;
2112 unshift(@
{$e->{ELEMENTS
}},
2113 { 'NAME' => 'ORPCthis',
2115 'PROPERTIES' => { 'in' => '1' },
2116 'TYPE' => 'ORPCTHIS'
2118 unshift(@
{$e->{ELEMENTS
}},
2119 { 'NAME' => 'ORPCthat',
2121 'PROPERTIES' => { 'out' => '1' },
2122 'TYPE' => 'ORPCTHAT'
2133 foreach my $x (@
{$idl}) {
2138 #####################################################################
2139 # parse a parsed IDL structure back into an IDL file
2143 my($filename) = shift;
2144 my $h_filename = $filename;
2149 if ($h_filename =~ /(.*)\.c/) {
2150 $h_filename = "$1.h";
2153 pidl
"/* parser auto-generated by pidl */";
2155 pidl
"#include \"includes.h\"";
2156 pidl
"#include \"$h_filename\"";
2159 foreach my $x (@
{$idl}) {
2160 if ($x->{TYPE
} eq "INTERFACE") {
2161 needed
::BuildNeeded
($x);
2166 RegistrationFunction
($idl, $filename);
2171 RegisterPrimitives
();