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
21 return $typefamily{$n};
24 # determine if an element needs a "buffers" section in NDR
25 sub need_buffers_section
($)
28 if (!Ndr
::can_contain_deferred
($e) &&
29 !util
::array_size
($e)) {
35 # see if a variable needs to be allocated by the NDR subsystem on pull
40 return 0 if (util
::has_property
($e, "ref") && $e->{PARENT
}->{TYPE
} eq "FUNCTION");
41 return 1 if ($e->{POINTERS
} || util
::array_size
($e));
45 # Prefix to get the actual value of a variable
50 foreach my $i (Ndr
::need_wire_pointer
($e)..$e->{POINTERS
}-1) { $pointers.="*"; }
54 # determine the C prefix used to refer to a variable when passing to a push
55 # function. This will be '*' for pointers to scalar types, '' for scalar
56 # types and normal pointers and '&' for pass-by-reference structures
63 if ($e->{TYPE
} =~ "string") {
65 } elsif (Ndr
::is_scalar_type
($e->{TYPE
}) and $e->{POINTERS
} and
66 !util
::array_size
($e)) {
68 } elsif (!Ndr
::is_scalar_type
($e->{TYPE
}) &&
70 !util
::array_size
($e)) {
74 foreach my $i (2..$e->{POINTERS
}) { $ret.="*"; }
79 # determine the C prefix used to refer to a variable when passing to a pull
85 if (!$e->{POINTERS
} && !util
::array_size
($e)) {
89 if ($e->{TYPE
} =~ "string") {
94 foreach my $i (2..$e->{POINTERS
}) { $ret.="*"; }
116 $tabs = substr($tabs, 0, -1);
119 ####################################################################
120 # work out the name of a size_is() variable
125 my($var_prefix) = shift;
127 my($fn) = $e->{PARENT
};
129 return $size if (util
::is_constant
($size));
131 return $size if ($size =~ /ndr->|\(/);
135 if ($size =~ /\*(.*)/) {
140 if ($fn->{TYPE
} ne "FUNCTION") {
141 return $prefix . "r->$size";
144 my $e2 = util
::find_sibling
($e, $size);
146 die("Invalid sibling '$size'") unless defined($e2);
148 if (util
::has_property
($e2, "in") && util
::has_property
($e2, "out")) {
149 return $prefix . "$var_prefix$size";
152 if (util
::has_property
($e2, "in")) {
153 return $prefix . "r->in.$size";
156 if (util
::has_property
($e2, "out")) {
157 return $prefix . "r->out.$size";
160 die "invalid variable in $size for element $e->{NAME} in $fn->{NAME}\n";
163 #####################################################################
164 # check that a variable we get from ParseExpr isn't a null pointer
165 sub check_null_pointer
($)
168 if ($size =~ /^\*/) {
169 my $size2 = substr($size, 1);
170 pidl
"if ($size2 == NULL) return NT_STATUS_INVALID_PARAMETER_MIX;";
174 #####################################################################
175 # check that a variable we get from ParseExpr isn't a null pointer
176 # void return varient
177 sub check_null_pointer_void
($)
180 if ($size =~ /^\*/) {
181 my $size2 = substr($size, 1);
182 pidl
"if ($size2 == NULL) return;";
186 #####################################################################
187 # work out is a parse function should be declared static or not
192 if ($fn->{TYPE
} eq "TYPEDEF" or
193 $fn->{TYPE
} eq "FUNCTION") {
194 return "" if (util
::has_property
($fn, "public"));
200 ###################################################################
201 # setup any special flags for an element or structure
205 my $flags = util
::has_property
($e, "flag");
206 if (defined $flags) {
207 pidl
"{ uint32_t _flags_save_$e->{TYPE} = ndr->flags;";
208 pidl
"ndr_set_flags(&ndr->flags, $flags);";
212 ###################################################################
213 # end any special flags for an element or structure
217 my $flags = util
::has_property
($e, "flag");
218 if (defined $flags) {
219 pidl
"ndr->flags = _flags_save_$e->{TYPE};\n\t}";
223 #####################################################################
224 # parse array preceding data - push side
225 sub ParseArrayPushPreceding
($$$)
228 my $var_prefix = shift;
229 my $ndr_flags = shift;
231 my $size = ParseExpr
($e, util
::array_size
($e), $var_prefix);
233 if (!Ndr
::is_inline_array
($e)) {
234 # we need to emit the array size
235 pidl
"NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, $size));";
239 #####################################################################
240 # parse the data of an array - push side
241 sub ParseArrayPush
($$$$)
245 my $var_prefix = shift;
246 my $ndr_flags = shift;
247 my $cprefix = c_push_prefix
($e);
249 my $size = ParseExpr
($e, util
::array_size
($e), $var_prefix);
251 # See whether the array size has been pushed yet
252 if (!Ndr
::is_surrounding_array
($e)) {
253 ParseArrayPushPreceding
($e, $var_prefix, $ndr_flags);
256 if (Ndr
::is_varying_array
($e)) {
257 my $length = util
::has_property
($e, "length_is");
258 $length = ParseExpr
($e, $length, $var_prefix);
259 pidl
"NDR_CHECK(ndr_push_uint32($ndr, NDR_SCALARS, 0));";
260 pidl
"NDR_CHECK(ndr_push_uint32($ndr, NDR_SCALARS, $length));";
264 if (Ndr
::is_scalar_type
($e->{TYPE
})) {
265 pidl
"NDR_CHECK(ndr_push_array_$e->{TYPE}($ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}, $size));";
267 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}));";
271 #####################################################################
273 sub ParseArrayPrint
($$)
276 my $var_prefix = shift;
277 my $size = ParseExpr
($e, util
::array_size
($e), $var_prefix);
278 my $cprefix = c_push_prefix
($e);
280 if (Ndr
::is_varying_array
($e)) {
281 $size = ParseExpr
($e, util
::has_property
($e, "length_is"), $var_prefix);
284 if (Ndr
::is_scalar_type
($e->{TYPE
})) {
285 pidl
"ndr_print_array_$e->{TYPE}(ndr, \"$e->{NAME}\", $cprefix$var_prefix$e->{NAME}, $size);";
287 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});";
291 #####################################################################
292 # check the size_is and length_is constraints
293 sub CheckArraySizes
($$)
296 my $var_prefix = shift;
298 if (Ndr
::is_conformant_array
($e)) {
299 my $size = ParseExpr
($e, util
::array_size
($e), $var_prefix);
300 pidl
"if ($var_prefix$e->{NAME}) {";
302 check_null_pointer
($size);
303 pidl
"NDR_CHECK(ndr_check_array_size(ndr, (void*)&$var_prefix$e->{NAME}, $size));";
308 if (Ndr
::is_varying_array
($e)) {
309 my $length = util
::has_property
($e, "length_is");
310 $length = ParseExpr
($e, $length, $var_prefix);
311 pidl
"if ($var_prefix$e->{NAME}) {";
313 check_null_pointer
($length);
314 pidl
"NDR_CHECK(ndr_check_array_length(ndr, (void*)&$var_prefix$e->{NAME}, $length));";
320 sub ParseArrayPullPreceding
($$$)
323 my $var_prefix = shift;
324 my $ndr_flags = shift;
326 if (!Ndr
::is_inline_array
($e)) {
327 # non fixed arrays encode the size just before the array
328 pidl
"NDR_CHECK(ndr_pull_array_size(ndr, &$var_prefix$e->{NAME}));";
332 #####################################################################
333 # parse an array - pull side
334 sub ParseArrayPull
($$$$)
338 my $var_prefix = shift;
339 my $ndr_flags = shift;
341 my $cprefix = c_pull_prefix
($e);
342 my $length = ParseExpr
($e, util
::array_size
($e), $var_prefix);
345 if (Ndr
::is_conformant_array
($e)) {
346 $length = $size = "ndr_get_array_size($ndr, &$var_prefix$e->{NAME})";
349 # if this is a conformant array then we use that size to allocate, and make sure
350 # we allocate enough to pull the elements
351 if (!Ndr
::is_inline_array
($e) and not Ndr
::is_surrounding_array
($e)) {
352 if ($var_prefix =~ /^r->out/ && $length =~ /^\*r->in/) {
353 my $length2 = substr($length, 1);
354 pidl
"if (ndr->flags & LIBNDR_FLAG_REF_ALLOC) { NDR_ALLOC($ndr, $length2); }";
357 ParseArrayPullPreceding
($e, $var_prefix, $ndr_flags);
360 if (Ndr
::is_varying_array
($e)) {
361 pidl
"NDR_CHECK(ndr_pull_array_length($ndr, &$var_prefix$e->{NAME}));";
362 $length = "ndr_get_array_length($ndr, &$var_prefix$e->{NAME})";
365 check_null_pointer
($length);
367 if ($length ne $size) {
368 pidl
"if ($length > $size) {";
370 pidl
"return ndr_pull_error($ndr, NDR_ERR_CONFORMANT_SIZE, \"Bad conformant size %u should be %u\", $size, $length);";
375 if ((need_alloc
($e) && !Ndr
::is_fixed_array
($e)) ||
376 ($var_prefix eq "r->in." && util
::has_property
($e, "ref"))) {
377 if (!Ndr
::is_inline_array
($e) || $ndr_flags eq "NDR_SCALARS") {
378 pidl
"NDR_ALLOC_N($ndr, $var_prefix$e->{NAME}, $size);";
382 if (($var_prefix eq "r->out." && util
::has_property
($e, "ref"))) {
383 if (!Ndr
::is_inline_array
($e) || $ndr_flags eq "NDR_SCALARS") {
384 pidl
"if ($ndr->flags & LIBNDR_FLAG_REF_ALLOC) {";
385 pidl
"\tNDR_ALLOC_N($ndr, $var_prefix$e->{NAME}, $size);";
390 if (Ndr
::is_scalar_type
($e->{TYPE
})) {
391 pidl
"NDR_CHECK(ndr_pull_array_$e->{TYPE}($ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}, $length));";
393 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}));";
397 sub compression_alg
($)
400 my $compression = util
::has_property
($e, "compression");
401 my ($alg, $clen, $dlen) = split(/ /, $compression);
406 sub compression_clen
($)
409 my $compression = util
::has_property
($e, "compression");
410 my ($alg, $clen, $dlen) = split(/ /, $compression);
412 return ParseExpr
($e, $clen, "r->");
415 sub compression_dlen
($)
418 my $compression = util
::has_property
($e, "compression");
419 my ($alg, $clen, $dlen) = split(/ /, $compression);
421 return ParseExpr
($e, $dlen, "r->");
424 sub ParseCompressionPushStart
($$$)
428 my $ndr_flags = shift;
429 my $comndr = $subndr."_compressed";
433 pidl
"struct ndr_push *$comndr;";
435 pidl
"$comndr = ndr_push_init_ctx($subndr);";
436 pidl
"if (!$comndr) return NT_STATUS_NO_MEMORY;";
437 pidl
"$comndr->flags = $subndr->flags;";
443 sub ParseCompressionPushEnd
($$)
447 my $comndr = $subndr."_compressed";
448 my $alg = compression_alg
($e);
450 pidl
"NDR_CHECK(ndr_push_compression($subndr, $comndr, $alg));";
455 sub ParseCompressionPullStart
($$$)
459 my $ndr_flags = shift;
460 my $comndr = $subndr."_compressed";
461 my $alg = compression_alg
($e);
462 my $dlen = compression_dlen
($e);
466 pidl
"struct ndr_pull *$comndr;";
467 pidl
"NDR_ALLOC($subndr, $comndr);";
468 pidl
"NDR_CHECK(ndr_pull_compression($subndr, $comndr, $alg, $dlen));";
473 sub ParseCompressionPullEnd
($$)
477 my $comndr = $subndr."_compressed";
483 sub ParseSubcontextPushStart
($$)
486 my $ndr_flags = shift;
487 my $compression = util
::has_property
($e, "compression");
490 pidl
"if (($ndr_flags) & NDR_SCALARS) {";
492 pidl
"struct ndr_push *_ndr_$e->{NAME};";
494 pidl
"_ndr_$e->{NAME} = ndr_push_init_ctx(ndr);";
495 pidl
"if (!_ndr_$e->{NAME}) return NT_STATUS_NO_MEMORY;";
496 pidl
"_ndr_$e->{NAME}->flags = ndr->flags;";
499 $retndr = "_ndr_$e->{NAME}";
501 if (defined $compression) {
502 $retndr = ParseCompressionPushStart
($e, $retndr, "NDR_SCALARS");
508 sub ParseSubcontextPushEnd
($)
511 my $header_size = util
::has_property
($e, "subcontext");
512 my $size_is = util
::has_property
($e, "subcontext_size");
513 my $compression = util
::has_property
($e, "compression");
514 my $ndr = "_ndr_$e->{NAME}";
516 if (defined $compression) {
517 ParseCompressionPushEnd
($e, $ndr);
520 if (not defined($size_is)) {
524 pidl
"NDR_CHECK(ndr_push_subcontext_header(ndr, $header_size, $size_is, $ndr));";
525 pidl
"NDR_CHECK(ndr_push_bytes(ndr, $ndr->data, $ndr->offset));";
530 sub ParseSubcontextPullStart
($$)
533 my $ndr_flags = shift;
534 my $header_size = util
::has_property
($e, "subcontext");
535 my $size_is = util
::has_property
($e, "subcontext_size");
536 my $retndr = "_ndr_$e->{NAME}";
537 my $compression = util
::has_property
($e, "compression");
539 if (not defined($size_is)) {
543 pidl
"if (($ndr_flags) & NDR_SCALARS) {";
545 pidl
"struct ndr_pull *$retndr;";
546 pidl
"NDR_ALLOC(ndr, $retndr);";
547 pidl
"NDR_CHECK(ndr_pull_subcontext_header(ndr, $header_size, $size_is, $retndr));";
549 if (defined $compression) {
550 $retndr = ParseCompressionPullStart
($e, $retndr, $ndr_flags);
556 sub ParseSubcontextPullEnd
($)
559 my $header_size = util
::has_property
($e, "subcontext");
560 my $size_is = util
::has_property
($e, "subcontext_size");
561 my $subndr = "_ndr_$e->{NAME}";
562 my $compression = util
::has_property
($e, "compression");
564 if (defined $compression) {
565 ParseCompressionPullEnd
($e, $subndr);
569 if (defined ($size_is)) {
570 $advance = "$size_is";
571 } elsif ($header_size) {
572 $advance = "$subndr->data_size";
574 $advance = "$subndr->offset";
576 pidl
"NDR_CHECK(ndr_pull_advance(ndr, $advance));";
581 #####################################################################
582 # parse scalars in a structure element
583 sub ParseElementPushScalar
($$$)
586 my($var_prefix) = shift;
587 my($ndr_flags) = shift;
588 my $cprefix = c_push_prefix
($e);
589 my $ptr_prefix = c_ptr_prefix
($e);
590 my $sub_size = util
::has_property
($e, "subcontext");
596 if (my $value = util
::has_property
($e, "value")) {
597 pidl
"$cprefix$var_prefix$e->{NAME} = $value;";
600 if (defined $sub_size and $e->{POINTERS
} == 0) {
601 $subndr = ParseSubcontextPushStart
($e, "NDR_SCALARS");
605 if (Ndr
::need_wire_pointer
($e)) {
606 ParsePtrPush
($e, $ptr_prefix.$var_prefix);
607 } elsif (Ndr
::is_inline_array
($e)) {
608 ParseArrayPush
($e, $ndr, "r->", "NDR_SCALARS");
609 } elsif (need_alloc
($e)) {
610 # no scalar component
612 if (my $switch = util
::has_property
($e, "switch_is")) {
613 ParseSwitchPush
($e, $ndr, $var_prefix, $ndr_flags, $switch);
616 pidl
"NDR_CHECK(ndr_push_$e->{TYPE}($ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}));";
619 if (defined $sub_size and $e->{POINTERS
} == 0) {
620 ParseSubcontextPushEnd
($e);
626 #####################################################################
627 # parse a pointer in a struct element or function
631 my $var_prefix = shift;
633 if (util
::has_property
($e, "ref")) {
634 pidl
"NDR_CHECK(ndr_push_ref_ptr(ndr, $var_prefix$e->{NAME}));";
635 } elsif (util
::has_property
($e, "relative")) {
636 pidl
"NDR_CHECK(ndr_push_relative_ptr1(ndr, $var_prefix$e->{NAME}));";
638 pidl
"NDR_CHECK(ndr_push_unique_ptr(ndr, $var_prefix$e->{NAME}));";
642 #####################################################################
643 # print scalars in a structure element
644 sub ParseElementPrint
($$)
647 my($var_prefix) = shift;
648 my $cprefix = c_push_prefix
($e);
649 my $ptr_prefix = c_ptr_prefix
($e);
651 return if (util
::has_property
($e, "noprint"));
653 if (my $value = util
::has_property
($e, "value")) {
654 pidl
"if (ndr->flags & LIBNDR_PRINT_SET_VALUES) {";
655 pidl
"\t$cprefix$var_prefix$e->{NAME} = $value;";
659 my $l = $e->{POINTERS
};
660 $l++ if (util
::array_size
($e) and $l == 0 and !Ndr
::is_fixed_array
($e));
662 foreach my $i (1..$l) {
663 pidl
"ndr_print_ptr(ndr, \"$e->{NAME}\", $var_prefix$e->{NAME});";
664 pidl
"ndr->depth++;";
665 if ($i > $l-Ndr
::need_wire_pointer
($e)) {
666 pidl
"if ($ptr_prefix$var_prefix$e->{NAME}) {";
671 if (util
::array_size
($e)) {
672 ParseArrayPrint
($e, $var_prefix)
673 } elsif (my $switch = util
::has_property
($e, "switch_is")) {
674 my $switch_var = ParseExpr
($e, $switch, $var_prefix);
675 check_null_pointer_void
($switch_var);
677 pidl
"ndr_print_$e->{TYPE}(ndr, \"$e->{NAME}\", $switch_var, $cprefix$var_prefix$e->{NAME});";
679 pidl
"ndr_print_$e->{TYPE}(ndr, \"$e->{NAME}\", $cprefix$var_prefix$e->{NAME});";
682 foreach my $i (1..$l) {
683 if ($i > $l-Ndr
::need_wire_pointer
($e)) {
687 pidl
"ndr->depth--;";
691 #####################################################################
692 # parse scalars in a structure element - pull size
693 sub ParseSwitchPull
($$$$$)
697 my($var_prefix) = shift;
698 my($ndr_flags) = shift;
700 my $switch_var = ParseExpr
($e, $switch, $var_prefix);
702 my $cprefix = c_pull_prefix
($e);
704 my $utype = typelist
::getType
($e->{TYPE
});
706 check_null_pointer
($switch_var);
708 pidl
"NDR_CHECK(ndr_pull_set_switch_value($ndr, $cprefix$var_prefix$e->{NAME}, $switch_var));";
712 #####################################################################
713 # push switch element
714 sub ParseSwitchPush
($$$$$)
718 my($var_prefix) = shift;
719 my($ndr_flags) = shift;
721 my $switch_var = ParseExpr
($e, $switch, $var_prefix);
722 my $cprefix = c_push_prefix
($e);
724 check_null_pointer
($switch_var);
726 pidl
"NDR_CHECK(ndr_push_set_switch_value($ndr, $cprefix$var_prefix$e->{NAME}, $switch_var));";
730 #####################################################################
731 # parse scalars in a structure element - pull size
732 sub ParseElementPullScalar
($$$)
735 my($var_prefix) = shift;
736 my($ndr_flags) = shift;
737 my $cprefix = c_pull_prefix
($e);
738 my $ptr_prefix = c_ptr_prefix
($e);
739 my $sub_size = util
::has_property
($e, "subcontext");
745 if (defined $sub_size && $e->{POINTERS
} == 0) {
746 $subndr = ParseSubcontextPullStart
($e, $ndr_flags);
748 $ndr_flags = "NDR_SCALARS|NDR_BUFFERS";
751 if (Ndr
::is_inline_array
($e)) {
752 ParseArrayPull
($e, $ndr, "r->", "NDR_SCALARS");
753 } elsif (Ndr
::need_wire_pointer
($e)) {
754 ParsePtrPull
($e, $ptr_prefix.$var_prefix);
755 } elsif (Ndr
::is_surrounding_array
($e)) {
757 if (my $switch = util
::has_property
($e, "switch_is")) {
758 ParseSwitchPull
($e, $ndr, $var_prefix, $ndr_flags, $switch);
761 pidl
"NDR_CHECK(ndr_pull_$e->{TYPE}($ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}));";
764 if (my $range = util
::has_property
($e, "range")) {
765 my ($low, $high) = split(/ /, $range, 2);
766 pidl
"if ($var_prefix$e->{NAME} < $low || $var_prefix$e->{NAME} > $high) {";
767 pidl
"\treturn ndr_pull_error($ndr, NDR_ERR_RANGE, \"value out of range\");";
771 if (defined $sub_size && $e->{POINTERS
} == 0) {
772 ParseSubcontextPullEnd
($e);
778 #####################################################################
779 # parse a pointer in a struct element or function
783 my($var_prefix) = shift;
785 if (util
::has_property
($e, "ref")) {
786 pidl
"NDR_CHECK(ndr_pull_ref_ptr(ndr, &_ptr_$e->{NAME}));";
788 pidl
"NDR_CHECK(ndr_pull_unique_ptr(ndr, &_ptr_$e->{NAME}));";
790 pidl
"if (_ptr_$e->{NAME}) {";
792 pidl
"NDR_ALLOC(ndr, $var_prefix$e->{NAME});";
793 if (util
::has_property
($e, "relative")) {
794 pidl
"NDR_CHECK(ndr_pull_relative_ptr1(ndr, $var_prefix$e->{NAME}, _ptr_$e->{NAME}));";
798 pidl
"\t$var_prefix$e->{NAME} = NULL;";
802 #####################################################################
803 # parse buffers in a structure element
804 sub ParseElementPushBuffer
($$)
807 my($var_prefix) = shift;
808 my $cprefix = c_push_prefix
($e);
809 my $sub_size = util
::has_property
($e, "subcontext");
813 return unless (need_buffers_section
($e));
817 my $pointers = c_ptr_prefix
($e);
818 for my $i (1..Ndr
::need_wire_pointer
($e)) {
820 ParsePtrPush
($e,$pointers.$var_prefix);
822 pidl
"if ($pointers$var_prefix$e->{NAME}) {";
827 if (util
::has_property
($e, "relative")) {
828 pidl
"NDR_CHECK(ndr_push_relative_ptr2(ndr, $var_prefix$e->{NAME}));";
831 my $ndr_flags = "NDR_BUFFERS";
832 if ($e->{POINTERS
} || (util
::array_size
($e) && !Ndr
::is_inline_array
($e)))
834 $ndr_flags="NDR_SCALARS|$ndr_flags"
837 if (defined $sub_size) {
838 $subndr = ParseSubcontextPushStart
($e, $ndr_flags);
840 $ndr_flags = "NDR_SCALARS|NDR_BUFFERS";
843 if (util
::array_size
($e)) {
844 ParseArrayPush
($e, $ndr, "r->", $ndr_flags);
846 if (my $switch = util
::has_property
($e, "switch_is")) {
847 ParseSwitchPush
($e, $ndr, $var_prefix, $ndr_flags, $switch);
850 pidl
"NDR_CHECK(ndr_push_$e->{TYPE}($ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}));";
853 if (defined $sub_size) {
854 ParseSubcontextPushEnd
($e);
857 for my $i (1..Ndr
::need_wire_pointer
($e)) {
865 #####################################################################
866 # parse buffers in a structure element - pull side
867 sub ParseElementPullBuffer
($$)
870 my($var_prefix) = shift;
871 my $cprefix = c_pull_prefix
($e);
872 my $sub_size = util
::has_property
($e, "subcontext");
876 return unless (need_buffers_section
($e));
880 my $pointers = c_ptr_prefix
($e);
881 for my $i (1..Ndr
::need_wire_pointer
($e)) {
883 ParsePtrPull
($e,$pointers.$var_prefix);
885 pidl
"if ($pointers$var_prefix$e->{NAME}) {";
890 if (util
::has_property
($e, "relative")) {
891 pidl
"struct ndr_pull_save _relative_save;";
892 pidl
"ndr_pull_save(ndr, &_relative_save);";
893 pidl
"NDR_CHECK(ndr_pull_relative_ptr2(ndr, $var_prefix$e->{NAME}));";
896 my $ndr_flags = "NDR_BUFFERS";
897 if ($e->{POINTERS
} || (util
::array_size
($e) && !Ndr
::is_inline_array
($e)))
899 $ndr_flags="NDR_SCALARS|$ndr_flags"
902 if (defined $sub_size) {
903 $subndr = ParseSubcontextPullStart
($e, $ndr_flags);
905 $ndr_flags = "NDR_SCALARS|NDR_BUFFERS";
908 if (util
::array_size
($e)) {
909 ParseArrayPull
($e, $ndr, "r->", $ndr_flags);
911 if (my $switch = util
::has_property
($e, "switch_is")) {
912 ParseSwitchPull
($e, $ndr, $var_prefix, $ndr_flags, $switch);
915 pidl
"NDR_CHECK(ndr_pull_$e->{TYPE}($ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}));";
918 if (defined $sub_size) {
919 ParseSubcontextPullEnd
($e);
922 if (util
::has_property
($e, "relative")) {
923 pidl
"ndr_pull_restore(ndr, &_relative_save);";
926 for my $i (1..Ndr
::need_wire_pointer
($e)) {
934 #####################################################################
936 sub ParseStructPush
($)
940 return unless defined($struct->{ELEMENTS
});
942 start_flags
($struct);
944 # see if the structure contains a conformant array. If it
945 # does, then it must be the last element of the structure, and
946 # we need to push the conformant length early, as it fits on
947 # the wire before the structure (and even before the structure
949 my $e = $struct->{ELEMENTS
}[-1];
950 if (Ndr
::is_conformant_array
($e) and Ndr
::is_surrounding_array
($e)) {
951 ParseArrayPushPreceding
($e, "r->", "NDR_SCALARS");
954 if (defined $e->{TYPE
} && $e->{TYPE
} eq "string"
955 && util
::property_matches
($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
956 pidl
"NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, ndr_string_array_size(ndr, r->$e->{NAME})));";
959 pidl
"if (!(ndr_flags & NDR_SCALARS)) goto buffers;";
961 pidl
"NDR_CHECK(ndr_push_struct_start(ndr));";
963 my $align = Ndr
::find_largest_alignment
($struct);
964 pidl
"NDR_CHECK(ndr_push_align(ndr, $align));";
966 foreach my $e (@
{$struct->{ELEMENTS
}}) {
967 ParseElementPushScalar
($e, "r->", "NDR_SCALARS");
971 pidl
"if (!(ndr_flags & NDR_BUFFERS)) goto done;";
972 foreach my $e (@
{$struct->{ELEMENTS
}}) {
973 ParseElementPushBuffer
($e, "r->");
976 pidl
"ndr_push_struct_end(ndr);";
983 #####################################################################
984 # generate a push function for an enum
988 my($type_fn) = typelist
::enum_type_fn
($enum);
992 pidl
"NDR_CHECK(ndr_push_$type_fn(ndr, NDR_SCALARS, r));";
997 #####################################################################
998 # generate a pull function for an enum
1002 my($type_fn) = typelist
::enum_type_fn
($enum);
1003 my($type_v_decl) = typelist
::mapScalarType
(typelist
::enum_type_fn
($enum));
1005 pidl
"$type_v_decl v;";
1007 pidl
"NDR_CHECK(ndr_pull_$type_fn(ndr, NDR_SCALARS, &v));";
1013 #####################################################################
1014 # generate a print function for an enum
1015 sub ParseEnumPrint
($)
1019 pidl
"const char *val = NULL;";
1024 pidl
"switch (r) {";
1026 my $els = \@
{$enum->{ELEMENTS
}};
1027 foreach my $i (0 .. $#{$els}) {
1028 my $e = ${$els}[$i];
1030 if ($e =~ /^(.*)=/) {
1033 pidl
"case $e: val = \"$e\"; break;";
1039 pidl
"ndr_print_enum(ndr, name, \"$enum->{TYPE}\", val, r);";
1047 return "struct ndr_push *ndr, int ndr_flags, enum $e->{NAME} r";
1050 sub ArgsEnumPrint
($)
1053 return "struct ndr_print *ndr, const char *name, enum $e->{NAME} r";
1059 return "struct ndr_pull *ndr, int ndr_flags, enum $e->{NAME} *r";
1062 $typefamily{ENUM
} = {
1063 PUSH_FN_BODY
=> \
&ParseEnumPush
,
1064 PUSH_FN_ARGS
=> \
&ArgsEnumPush
,
1065 PULL_FN_BODY
=> \
&ParseEnumPull
,
1066 PULL_FN_ARGS
=> \
&ArgsEnumPull
,
1067 PRINT_FN_BODY
=> \
&ParseEnumPrint
,
1068 PRINT_FN_ARGS
=> \
&ArgsEnumPrint
,
1071 #####################################################################
1072 # generate a push function for a bitmap
1073 sub ParseBitmapPush
($)
1075 my($bitmap) = shift;
1076 my($type_fn) = typelist
::bitmap_type_fn
($bitmap);
1078 start_flags
($bitmap);
1080 pidl
"NDR_CHECK(ndr_push_$type_fn(ndr, NDR_SCALARS, r));";
1085 #####################################################################
1086 # generate a pull function for an bitmap
1087 sub ParseBitmapPull
($)
1089 my($bitmap) = shift;
1090 my($type_fn) = typelist
::bitmap_type_fn
($bitmap);
1091 my($type_decl) = typelist
::mapType
($bitmap);
1093 pidl
"$type_decl v;";
1094 start_flags
($bitmap);
1095 pidl
"NDR_CHECK(ndr_pull_$type_fn(ndr, NDR_SCALARS, &v));";
1101 #####################################################################
1102 # generate a print function for an bitmap
1103 sub ParseBitmapPrintElement
($$)
1106 my($bitmap) = shift;
1107 my($type_decl) = typelist
::mapType
($bitmap);
1108 my($type_fn) = typelist
::bitmap_type_fn
($bitmap);
1109 my($name) = $bitmap->{PARENT
}->{NAME
};
1112 if ($e =~ /^(\w+) .*$/) {
1115 die "Bitmap: \"$name\" invalid Flag: \"$e\"";
1118 pidl
"ndr_print_bitmap_flag(ndr, sizeof($type_decl), \"$flag\", $flag, r);";
1121 #####################################################################
1122 # generate a print function for an bitmap
1123 sub ParseBitmapPrint
($)
1125 my($bitmap) = shift;
1126 my($type_decl) = typelist
::mapType
($bitmap);
1127 my($type_fn) = typelist
::bitmap_type_fn
($bitmap);
1129 start_flags
($bitmap);
1131 pidl
"ndr_print_$type_fn(ndr, name, r);";
1133 pidl
"ndr->depth++;";
1134 foreach my $e (@
{$bitmap->{ELEMENTS
}}) {
1135 ParseBitmapPrintElement
($e, $bitmap);
1137 pidl
"ndr->depth--;";
1142 sub ArgsBitmapPush
($)
1145 my $type_decl = typelist
::mapType
($e->{DATA
});
1146 return "struct ndr_push *ndr, int ndr_flags, $type_decl r";
1149 sub ArgsBitmapPrint
($)
1152 my $type_decl = typelist
::mapType
($e->{DATA
});
1153 return "struct ndr_print *ndr, const char *name, $type_decl r";
1156 sub ArgsBitmapPull
($)
1159 my $type_decl = typelist
::mapType
($e->{DATA
});
1160 return "struct ndr_pull *ndr, int ndr_flags, $type_decl *r";
1163 $typefamily{BITMAP
} = {
1164 PUSH_FN_BODY
=> \
&ParseBitmapPush
,
1165 PUSH_FN_ARGS
=> \
&ArgsBitmapPush
,
1166 PULL_FN_BODY
=> \
&ParseBitmapPull
,
1167 PULL_FN_ARGS
=> \
&ArgsBitmapPull
,
1168 PRINT_FN_BODY
=> \
&ParseBitmapPrint
,
1169 PRINT_FN_ARGS
=> \
&ArgsBitmapPrint
,
1172 #####################################################################
1173 # generate a struct print function
1174 sub ParseStructPrint
($)
1176 my($struct) = shift;
1177 my($name) = $struct->{PARENT
}->{NAME
};
1179 return unless defined $struct->{ELEMENTS
};
1181 pidl
"ndr_print_struct(ndr, name, \"$name\");";
1183 start_flags
($struct);
1185 pidl
"ndr->depth++;";
1186 foreach my $e (@
{$struct->{ELEMENTS
}}) {
1187 ParseElementPrint
($e, "r->");
1189 pidl
"ndr->depth--;";
1194 #####################################################################
1195 # parse a struct - pull side
1196 sub ParseStructPull
($)
1198 my($struct) = shift;
1201 return unless defined $struct->{ELEMENTS
};
1203 # see if the structure contains a conformant array. If it
1204 # does, then it must be the last element of the structure, and
1205 # we need to pull the conformant length early, as it fits on
1206 # the wire before the structure (and even before the structure
1208 my $e = $struct->{ELEMENTS
}[-1];
1209 if (Ndr
::is_conformant_array
($e) and Ndr
::is_surrounding_array
($e)) {
1213 if (defined $e->{TYPE
} && $e->{TYPE
} eq "string"
1214 && util
::property_matches
($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
1218 # declare any internal pointers we need
1219 foreach my $e (@
{$struct->{ELEMENTS
}}) {
1220 if (Ndr
::need_wire_pointer
($e)) {
1221 pidl
"uint32_t _ptr_$e->{NAME};";
1225 start_flags
($struct);
1227 pidl
"if (!(ndr_flags & NDR_SCALARS)) goto buffers;";
1229 pidl
"NDR_CHECK(ndr_pull_struct_start(ndr));";
1231 if (defined $conform_e) {
1232 ParseArrayPullPreceding
($conform_e, "r->", "NDR_SCALARS");
1235 my $align = Ndr
::find_largest_alignment
($struct);
1236 pidl
"NDR_CHECK(ndr_pull_align(ndr, $align));";
1238 foreach my $e (@
{$struct->{ELEMENTS
}}) {
1239 ParseElementPullScalar
($e, "r->", "NDR_SCALARS");
1243 pidl
"if (!(ndr_flags & NDR_BUFFERS)) goto done;";
1244 foreach my $e (@
{$struct->{ELEMENTS
}}) {
1245 ParseElementPullBuffer
($e, "r->");
1248 foreach my $e (@
{$struct->{ELEMENTS
}}) {
1249 CheckArraySizes
($e, "r->");
1252 pidl
"ndr_pull_struct_end(ndr);";
1259 #####################################################################
1260 # calculate size of ndr struct
1261 sub ParseStructNdrSize
($)
1264 my $static = fn_prefix
($t);
1267 if (my $flags = util
::has_property
($t, "flag")) {
1268 pidl
"flags |= $flags;";
1270 pidl
"return ndr_size_struct(r, flags, (ndr_push_flags_fn_t)ndr_push_$t->{NAME});";
1273 sub ArgsStructPush
($)
1276 return "struct ndr_push *ndr, int ndr_flags, struct $e->{NAME} *r";
1279 sub ArgsStructPrint
($)
1282 return "struct ndr_print *ndr, const char *name, struct $e->{NAME} *r";
1285 sub ArgsStructPull
($)
1288 return "struct ndr_pull *ndr, int ndr_flags, struct $e->{NAME} *r";
1291 sub ArgsStructNdrSize
($)
1294 return "const struct $d->{NAME} *r, int flags";
1297 $typefamily{STRUCT
} = {
1298 PUSH_FN_BODY
=> \
&ParseStructPush
,
1299 PUSH_FN_ARGS
=> \
&ArgsStructPush
,
1300 PULL_FN_BODY
=> \
&ParseStructPull
,
1301 PULL_FN_ARGS
=> \
&ArgsStructPull
,
1302 PRINT_FN_BODY
=> \
&ParseStructPrint
,
1303 PRINT_FN_ARGS
=> \
&ArgsStructPrint
,
1304 SIZE_FN_BODY
=> \
&ParseStructNdrSize
,
1305 SIZE_FN_ARGS
=> \
&ArgsStructNdrSize
,
1308 #####################################################################
1309 # calculate size of ndr struct
1310 sub ParseUnionNdrSize
($)
1313 my $static = fn_prefix
($t);
1316 if (my $flags = util
::has_property
($t, "flag")) {
1317 pidl
"flags |= $flags;";
1319 pidl
"return ndr_size_union(r, flags, level, (ndr_push_union_fn_t)ndr_push_$t->{NAME});";
1322 #####################################################################
1323 # parse a union - push side
1324 sub ParseUnionPush
($)
1327 my $have_default = 0;
1333 pidl
"level = ndr_push_get_switch_value(ndr, r);";
1335 pidl
"if (!(ndr_flags & NDR_SCALARS)) goto buffers;";
1337 if (!util
::has_property
($e, "nodiscriminant")) {
1338 my $switch_type = util
::has_property
($e, "switch_type");
1339 $switch_type = "uint32" unless (defined ($switch_type));
1340 pidl
"NDR_CHECK(ndr_push_$switch_type(ndr, NDR_SCALARS, level));";
1343 pidl
"NDR_CHECK(ndr_push_struct_start(ndr));";
1345 # my $align = union_alignment($e);
1346 # pidl "NDR_CHECK(ndr_push_align(ndr, $align));";
1348 pidl
"switch (level) {";
1350 foreach my $el (@
{$e->{ELEMENTS
}}) {
1351 if (util
::has_property
($el, "default")) {
1355 pidl
"case $el->{PROPERTIES}->{case}:";
1358 if ($el->{TYPE
} ne "EMPTY") {
1360 ParseElementPushScalar
($el, "r->", "NDR_SCALARS");
1366 if (! $have_default) {
1368 pidl
"\treturn ndr_push_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);";
1373 pidl
"if (!(ndr_flags & NDR_BUFFERS)) goto done;";
1374 pidl
"switch (level) {";
1376 foreach my $el (@
{$e->{ELEMENTS
}}) {
1377 if (util
::has_property
($el, "default")) {
1380 pidl
"case $el->{PROPERTIES}->{case}:";
1382 if ($el->{TYPE
} ne "EMPTY") {
1384 ParseElementPushBuffer
($el, "r->");
1390 if (! $have_default) {
1392 pidl
"\treturn ndr_push_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);";
1396 pidl
"ndr_push_struct_end(ndr);";
1401 #####################################################################
1403 sub ParseUnionPrint
($)
1406 my $have_default = 0;
1407 my($name) = $e->{PARENT
}->{NAME
};
1409 pidl
"ndr_print_union(ndr, name, level, \"$name\");";
1412 pidl
"switch (level) {";
1414 foreach my $el (@
{$e->{ELEMENTS
}}) {
1415 if (util
::has_property
($el, "default")) {
1419 pidl
"case $el->{PROPERTIES}->{case}:";
1421 if ($el->{TYPE
} ne "EMPTY") {
1423 ParseElementPrint
($el, "r->");
1429 if (! $have_default) {
1431 pidl
"\tndr_print_bad_level(ndr, name, level);";
1439 #####################################################################
1440 # parse a union - pull side
1441 sub ParseUnionPull
($)
1444 my $have_default = 0;
1445 my $switch_type = util
::has_property
($e, "switch_type");
1446 $switch_type = "uint32" unless defined($switch_type);
1449 if (!util
::has_property
($e, "nodiscriminant")) {
1450 if (typelist
::typeIs
($switch_type, "ENUM")) {
1451 $switch_type = typelist
::enum_type_fn
(typelist
::getType
($switch_type));
1453 pidl typelist
::mapScalarType
($switch_type) . " _level;";
1458 pidl
"level = ndr_pull_get_switch_value(ndr, r);";
1460 pidl
"if (!(ndr_flags & NDR_SCALARS)) goto buffers;";
1462 if (!util
::has_property
($e, "nodiscriminant")) {
1463 pidl
"NDR_CHECK(ndr_pull_$switch_type(ndr, NDR_SCALARS, &_level));";
1464 pidl
"if (_level != level) {";
1465 pidl
"\treturn ndr_pull_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value %u for $e->{PARENT}->{NAME}\", _level);";
1469 pidl
"NDR_CHECK(ndr_pull_struct_start(ndr));";
1471 # my $align = union_alignment($e);
1472 # pidl "\tNDR_CHECK(ndr_pull_align(ndr, $align));\n";
1474 pidl
"switch (level) {";
1476 foreach my $el (@
{$e->{ELEMENTS
}}) {
1477 if (util
::has_property
($el, "default")) {
1481 pidl
"case $el->{PROPERTIES}->{case}: {";
1483 if ($el->{TYPE
} ne "EMPTY") {
1485 if ($el->{POINTERS
}) {
1486 pidl
"uint32_t _ptr_$el->{NAME};";
1488 ParseElementPullScalar
($el, "r->", "NDR_SCALARS");
1494 if (! $have_default) {
1496 pidl
"\treturn ndr_pull_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);";
1501 pidl
"if (!(ndr_flags & NDR_BUFFERS)) goto done;";
1502 pidl
"switch (level) {";
1504 foreach my $el (@
{$e->{ELEMENTS
}}) {
1505 if (util
::has_property
($el, "default")) {
1508 pidl
"case $el->{PROPERTIES}->{case}:";
1510 if ($el->{TYPE
} ne "EMPTY") {
1512 ParseElementPullBuffer
($el, "r->");
1518 if (! $have_default) {
1520 pidl
"\treturn ndr_pull_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);";
1524 pidl
"ndr_pull_struct_end(ndr);";
1529 sub ArgsUnionPush
($)
1532 return "struct ndr_push *ndr, int ndr_flags, union $e->{NAME} *r";
1535 sub ArgsUnionPrint
($)
1538 return "struct ndr_print *ndr, const char *name, int level, union $e->{NAME} *r";
1541 sub ArgsUnionPull
($)
1544 return "struct ndr_pull *ndr, int ndr_flags, union $e->{NAME} *r";
1547 sub ArgsUnionNdrSize
($)
1550 return "const union $d->{NAME} *r, uint32_t level, int flags";
1553 $typefamily{UNION
} = {
1554 PUSH_FN_BODY
=> \
&ParseUnionPush
,
1555 PUSH_FN_ARGS
=> \
&ArgsUnionPush
,
1556 PULL_FN_BODY
=> \
&ParseUnionPull
,
1557 PULL_FN_ARGS
=> \
&ArgsUnionPull
,
1558 PRINT_FN_BODY
=> \
&ParseUnionPrint
,
1559 PRINT_FN_ARGS
=> \
&ArgsUnionPrint
,
1560 SIZE_FN_ARGS
=> \
&ArgsUnionNdrSize
,
1561 SIZE_FN_BODY
=> \
&ParseUnionNdrSize
,
1564 #####################################################################
1565 # parse a typedef - push side
1566 sub ParseTypedefPush
($)
1569 my $static = fn_prefix
($e);
1571 return unless needed
::is_needed
("push_$e->{NAME}");
1573 my $args = $typefamily{$e->{DATA
}->{TYPE
}}->{PUSH_FN_ARGS
}->($e);
1574 pidl
$static . "NTSTATUS ndr_push_$e->{NAME}($args)";
1578 $typefamily{$e->{DATA
}->{TYPE
}}->{PUSH_FN_BODY
}->($e->{DATA
});
1579 pidl
"return NT_STATUS_OK;";
1586 #####################################################################
1587 # parse a typedef - pull side
1588 sub ParseTypedefPull
($)
1591 my $static = fn_prefix
($e);
1593 return unless needed
::is_needed
("pull_$e->{NAME}");
1595 my $args = $typefamily{$e->{DATA
}->{TYPE
}}->{PULL_FN_ARGS
}->($e);
1597 pidl
$static . "NTSTATUS ndr_pull_$e->{NAME}($args)";
1601 $typefamily{$e->{DATA
}->{TYPE
}}->{PULL_FN_BODY
}->($e->{DATA
});
1602 pidl
"return NT_STATUS_OK;";
1608 #####################################################################
1609 # parse a typedef - print side
1610 sub ParseTypedefPrint
($)
1614 my $args = $typefamily{$e->{DATA
}->{TYPE
}}->{PRINT_FN_ARGS
}->($e);
1616 return unless !util
::has_property
($e, "noprint");
1618 pidl
"void ndr_print_$e->{NAME}($args)";
1621 $typefamily{$e->{DATA
}->{TYPE
}}->{PRINT_FN_BODY
}->($e->{DATA
});
1626 #####################################################################
1627 ## calculate the size of a structure
1628 sub ParseTypedefNdrSize
($)
1632 return unless needed
::is_needed
("ndr_size_$t->{NAME}");
1634 my $tf = $typefamily{$t->{DATA
}->{TYPE
}};
1635 my $args = $tf->{SIZE_FN_ARGS
}->($t);
1637 pidl
"size_t ndr_size_$t->{NAME}($args)";
1640 $typefamily{$t->{DATA
}->{TYPE
}}->{SIZE_FN_BODY
}->($t);
1646 #####################################################################
1647 # parse a function - print side
1648 sub ParseFunctionPrint
($)
1652 return unless !util
::has_property
($fn, "noprint");
1654 pidl
"void ndr_print_$fn->{NAME}(struct ndr_print *ndr, const char *name, int flags, struct $fn->{NAME} *r)";
1657 pidl
"ndr_print_struct(ndr, name, \"$fn->{NAME}\");";
1658 pidl
"ndr->depth++;";
1660 pidl
"if (flags & NDR_SET_VALUES) {";
1661 pidl
"\tndr->flags |= LIBNDR_PRINT_SET_VALUES;";
1664 pidl
"if (flags & NDR_IN) {";
1666 pidl
"ndr_print_struct(ndr, \"in\", \"$fn->{NAME}\");";
1667 pidl
"ndr->depth++;";
1669 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1670 if (util
::has_property
($e, "in")) {
1671 ParseElementPrint
($e, "r->in.");
1674 pidl
"ndr->depth--;";
1678 pidl
"if (flags & NDR_OUT) {";
1680 pidl
"ndr_print_struct(ndr, \"out\", \"$fn->{NAME}\");";
1681 pidl
"ndr->depth++;";
1682 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1683 if (util
::has_property
($e, "out")) {
1684 ParseElementPrint
($e, "r->out.");
1687 if ($fn->{RETURN_TYPE
} && $fn->{RETURN_TYPE
} ne "void") {
1689 $cprefix = "" if (Ndr
::is_scalar_type
($fn->{RETURN_TYPE
})) ; # FIXME: Should really use util::c_push_prefix here
1690 pidl
"ndr_print_$fn->{RETURN_TYPE}(ndr, \"result\", $cprefix"."r->out.result);";
1692 pidl
"ndr->depth--;";
1696 pidl
"ndr->depth--;";
1702 #####################################################################
1703 # parse a function element
1704 sub ParseFunctionElementPush
($$)
1709 if (util
::array_size
($e)) {
1710 if (Ndr
::need_wire_pointer
($e)) {
1711 ParsePtrPush
($e, "r->$inout.");
1712 pidl
"if (r->$inout.$e->{NAME}) {";
1714 ParseArrayPush
($e, "ndr", "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1718 ParseArrayPush
($e, "ndr", "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1721 ParseElementPushScalar
($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1722 if (Ndr
::need_wire_pointer
($e)) {
1723 ParseElementPushBuffer
($e, "r->$inout.");
1728 #####################################################################
1730 sub ParseFunctionPush
($)
1733 my $static = fn_prefix
($fn);
1735 return unless !util
::has_property
($fn, "nopush");
1737 pidl
$static . "NTSTATUS ndr_push_$fn->{NAME}(struct ndr_push *ndr, int flags, struct $fn->{NAME} *r)";
1741 pidl
"if (!(flags & NDR_IN)) goto ndr_out;";
1744 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1745 if (util
::has_property
($e, "in")) {
1746 if (util
::has_property
($e, "ref")) {
1747 check_null_pointer
("*r->in.$e->{NAME}");
1749 ParseFunctionElementPush
($e, "in");
1754 pidl
"if (!(flags & NDR_OUT)) goto done;";
1757 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1758 if (util
::has_property
($e, "out")) {
1759 if (util
::has_property
($e, "ref")) {
1760 check_null_pointer
("*r->out.$e->{NAME}");
1762 ParseFunctionElementPush
($e, "out");
1766 if ($fn->{RETURN_TYPE
} && $fn->{RETURN_TYPE
} ne "void") {
1767 pidl
"NDR_CHECK(ndr_push_$fn->{RETURN_TYPE}(ndr, NDR_SCALARS, r->out.result));";
1771 pidl
"return NT_STATUS_OK;";
1777 #####################################################################
1778 # parse a function element
1779 sub ParseFunctionElementPull
($$)
1784 if (util
::array_size
($e)) {
1785 if (Ndr
::need_wire_pointer
($e)) {
1786 pidl
"NDR_CHECK(ndr_pull_unique_ptr(ndr, &_ptr_$e->{NAME}));";
1787 pidl
"r->$inout.$e->{NAME} = NULL;";
1788 pidl
"if (_ptr_$e->{NAME}) {";
1790 } elsif ($inout eq "out" && util
::has_property
($e, "ref")) {
1791 pidl
"if (r->$inout.$e->{NAME}) {";
1795 ParseArrayPull
($e, "ndr", "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1797 if (Ndr
::need_wire_pointer
($e) or ($inout eq "out" and util
::has_property
($e, "ref"))) {
1802 if ($inout eq "out" && util
::has_property
($e, "ref")) {
1803 pidl
"if (ndr->flags & LIBNDR_FLAG_REF_ALLOC) {";
1804 pidl
"\tNDR_ALLOC(ndr, r->out.$e->{NAME});";
1808 if ($inout eq "in" && util
::has_property
($e, "ref")) {
1809 pidl
"NDR_ALLOC(ndr, r->in.$e->{NAME});";
1812 ParseElementPullScalar
($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1813 if (Ndr
::need_wire_pointer
($e)) {
1814 ParseElementPullBuffer
($e, "r->$inout.");
1819 ############################################################
1820 # allocate ref variables
1821 sub AllocateRefVars
($)
1824 my $asize = util
::array_size
($e);
1826 # note that if the variable is also an "in"
1827 # variable then we copy the initial value from
1830 if (!defined $asize) {
1831 # its a simple variable
1832 pidl
"NDR_ALLOC(ndr, r->out.$e->{NAME});";
1833 if (util
::has_property
($e, "in")) {
1834 pidl
"*r->out.$e->{NAME} = *r->in.$e->{NAME};";
1836 pidl
"ZERO_STRUCTP(r->out.$e->{NAME});";
1842 my $size = ParseExpr
($e, $asize, "r->out.");
1843 check_null_pointer
($size);
1844 pidl
"NDR_ALLOC_N(ndr, r->out.$e->{NAME}, $size);";
1845 if (util
::has_property
($e, "in")) {
1846 pidl
"memcpy(r->out.$e->{NAME},r->in.$e->{NAME},$size * sizeof(*r->in.$e->{NAME}));";
1848 pidl
"memset(r->out.$e->{NAME}, 0, $size * sizeof(*r->out.$e->{NAME}));";
1852 #####################################################################
1854 sub ParseFunctionPull
($)
1857 my $static = fn_prefix
($fn);
1859 return unless !util
::has_property
($fn, "nopull");
1861 # pull function args
1862 pidl
$static . "NTSTATUS ndr_pull_$fn->{NAME}(struct ndr_pull *ndr, int flags, struct $fn->{NAME} *r)";
1866 # declare any internal pointers we need
1867 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1868 if (Ndr
::need_wire_pointer
($e)) {
1869 pidl
"uint32_t _ptr_$e->{NAME};";
1873 pidl
"if (!(flags & NDR_IN)) goto ndr_out;";
1876 # auto-init the out section of a structure. I originally argued that
1877 # this was a bad idea as it hides bugs, but coping correctly
1878 # with initialisation and not wiping ref vars is turning
1879 # out to be too tricky (tridge)
1880 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1881 if (util
::has_property
($e, "out")) {
1882 pidl
"ZERO_STRUCT(r->out);";
1888 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1889 if (util
::has_property
($e, "in")) {
1890 ParseFunctionElementPull
($e, "in");
1892 # we need to allocate any reference output variables, so that
1893 # a dcerpc backend can be sure they are non-null
1894 if (util
::has_property
($e, "out") && util
::has_property
($e, "ref")) {
1895 AllocateRefVars
($e);
1899 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1900 if (util
::has_property
($e, "in")) {
1901 CheckArraySizes
($e, "r->in.");
1906 pidl
"if (!(flags & NDR_OUT)) goto done;";
1909 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1910 if (util
::has_property
($e, "out")) {
1911 ParseFunctionElementPull
($e, "out");
1915 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1916 if (util
::has_property
($e, "out")) {
1917 CheckArraySizes
($e, "r->out.");
1921 if ($fn->{RETURN_TYPE
} && $fn->{RETURN_TYPE
} ne "void") {
1922 pidl
"NDR_CHECK(ndr_pull_$fn->{RETURN_TYPE}(ndr, NDR_SCALARS, &r->out.result));";
1927 pidl
"return NT_STATUS_OK;";
1933 #####################################################################
1934 # produce a function call table
1935 sub FunctionTable
($)
1937 my($interface) = shift;
1938 my($data) = $interface->{INHERITED_DATA
};
1940 my $uname = uc $interface->{NAME
};
1942 foreach my $d (@
{$data}) {
1943 if ($d->{TYPE
} eq "FUNCTION") { $count++; }
1946 return if ($count == 0);
1948 pidl
"static const struct dcerpc_interface_call $interface->{NAME}\_calls[] = {";
1949 foreach my $d (@
{$data}) {
1950 if ($d->{TYPE
} eq "FUNCTION") {
1952 pidl
"\t\t\"$d->{NAME}\",";
1953 pidl
"\t\tsizeof(struct $d->{NAME}),";
1954 pidl
"\t\t(ndr_push_flags_fn_t) ndr_push_$d->{NAME},";
1955 pidl
"\t\t(ndr_pull_flags_fn_t) ndr_pull_$d->{NAME},";
1956 pidl
"\t\t(ndr_print_function_t) ndr_print_$d->{NAME}";
1960 pidl
"\t{ NULL, 0, NULL, NULL, NULL }";
1964 # If no endpoint is set, default to the interface name as a named pipe
1965 if (! defined $interface->{PROPERTIES
}->{endpoint
}) {
1966 $interface->{PROPERTIES
}->{endpoint
} = "\"ncacn_np:[\\\\pipe\\\\" . $interface->{NAME
} . "]\"";
1969 my @e = split / /, $interface->{PROPERTIES
}->{endpoint
};
1970 my $endpoint_count = $#e + 1;
1972 pidl
"static const char * const $interface->{NAME}\_endpoint_strings[] = {";
1973 foreach my $ep (@e) {
1979 pidl
"static const struct dcerpc_endpoint_list $interface->{NAME}\_endpoints = {";
1980 pidl
"\t.count\t= $endpoint_count,";
1981 pidl
"\t.names\t= $interface->{NAME}\_endpoint_strings";
1985 if (! defined $interface->{PROPERTIES
}->{authservice
}) {
1986 $interface->{PROPERTIES
}->{authservice
} = "\"host\"";
1989 my @a = split / /, $interface->{PROPERTIES
}->{authservice
};
1990 my $authservice_count = $#a + 1;
1992 pidl
"static const char * const $interface->{NAME}\_authservice_strings[] = {";
1993 foreach my $ap (@a) {
1999 pidl
"static const struct dcerpc_authservice_list $interface->{NAME}\_authservices = {";
2000 pidl
"\t.count\t= $endpoint_count,";
2001 pidl
"\t.names\t= $interface->{NAME}\_authservice_strings";
2005 pidl
"\nconst struct dcerpc_interface_table dcerpc_table_$interface->{NAME} = {";
2006 pidl
"\t.name\t\t= \"$interface->{NAME}\",";
2007 pidl
"\t.uuid\t\t= DCERPC_$uname\_UUID,";
2008 pidl
"\t.if_version\t= DCERPC_$uname\_VERSION,";
2009 pidl
"\t.helpstring\t= DCERPC_$uname\_HELPSTRING,";
2010 pidl
"\t.num_calls\t= $count,";
2011 pidl
"\t.calls\t\t= $interface->{NAME}\_calls,";
2012 pidl
"\t.endpoints\t= &$interface->{NAME}\_endpoints,";
2013 pidl
"\t.authservices\t= &$interface->{NAME}\_authservices";
2017 pidl
"static NTSTATUS dcerpc_ndr_$interface->{NAME}_init(void)";
2019 pidl
"\treturn librpc_register_interface(&dcerpc_table_$interface->{NAME});";
2024 #####################################################################
2025 # parse the interface definitions
2026 sub ParseInterface
($)
2028 my($interface) = shift;
2029 my($data) = $interface->{DATA
};
2032 foreach my $d (@
{$data}) {
2033 if ($d->{TYPE
} eq "TYPEDEF") {
2034 ParseTypedefPush
($d);
2035 ParseTypedefPull
($d);
2036 ParseTypedefPrint
($d);
2037 ParseTypedefNdrSize
($d);
2041 foreach my $d (@
{$data}) {
2042 if ($d->{TYPE
} eq "FUNCTION") {
2043 ParseFunctionPush
($d);
2044 ParseFunctionPull
($d);
2045 ParseFunctionPrint
($d);
2049 FunctionTable
($interface);
2052 sub RegistrationFunction
($$)
2055 my $filename = shift;
2057 $filename =~ /.*\/ndr_
(.*).c
/;
2059 pidl
"NTSTATUS dcerpc_$basename\_init(void)";
2062 pidl
"NTSTATUS status = NT_STATUS_OK;";
2063 foreach my $interface (@
{$idl}) {
2064 next if $interface->{TYPE
} ne "INTERFACE";
2066 my $data = $interface->{INHERITED_DATA
};
2068 foreach my $d (@
{$data}) {
2069 if ($d->{TYPE
} eq "FUNCTION") { $count++; }
2072 next if ($count == 0);
2074 pidl
"status = dcerpc_ndr_$interface->{NAME}_init();";
2075 pidl
"if (NT_STATUS_IS_ERR(status)) {";
2076 pidl
"\treturn status;";
2080 pidl
"return status;";
2086 sub CheckPointerTypes
($$)
2089 my $default = shift;
2091 foreach my $e (@
{$s->{ELEMENTS
}}) {
2092 if ($e->{POINTERS
}) {
2093 if (not defined(Ndr
::pointer_type
($e))) {
2094 $e->{PROPERTIES
}->{$default} = 1;
2097 if (Ndr
::pointer_type
($e) eq "ptr") {
2098 print "Warning: ptr is not supported by pidl yet\n";
2104 sub LoadInterface
($)
2108 if (not util
::has_property
($x, "pointer_default")) {
2109 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
2110 # and "unique" in Microsoft Extensions mode (default)
2111 $x->{PROPERTIES
}->{pointer_default
} = "unique";
2114 foreach my $d (@
{$x->{DATA
}}) {
2115 if (($d->{TYPE
} eq "DECLARE") or ($d->{TYPE
} eq "TYPEDEF")) {
2116 if ($d->{DATA
}->{TYPE
} eq "STRUCT" or $d->{DATA
}->{TYPE
} eq "UNION") {
2117 CheckPointerTypes
($d->{DATA
}, $x->{PROPERTIES
}->{pointer_default
});
2120 if (defined($d->{PROPERTIES
}) && !defined($d->{DATA
}->{PROPERTIES
})) {
2121 $d->{DATA
}->{PROPERTIES
} = $d->{PROPERTIES
};
2124 if ($d->{TYPE
} eq "FUNCTION") {
2125 CheckPointerTypes
($d,
2126 $x->{PROPERTIES
}->{pointer_default
} # MIDL defaults to "ref"
2136 foreach my $x (@
{$idl}) {
2141 #####################################################################
2142 # parse a parsed IDL structure back into an IDL file
2146 my($filename) = shift;
2147 my $h_filename = $filename;
2152 if ($h_filename =~ /(.*)\.c/) {
2153 $h_filename = "$1.h";
2156 pidl
"/* parser auto-generated by pidl */";
2158 pidl
"#include \"includes.h\"";
2159 pidl
"#include \"$h_filename\"";
2162 foreach my $x (@
{$idl}) {
2163 if ($x->{TYPE
} eq "INTERFACE") {
2164 needed
::BuildNeeded
($x);
2169 RegistrationFunction
($idl, $filename);