r6159: Move some more general ndr stuff (alignment calculations) to ndr.pm
[Samba/gebeck_regimport.git] / source4 / build / pidl / ndr_parser.pm
blob0ec24b3526664629a67a6a8d6f82f6992b01ffcb
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
8 package NdrParser;
10 use strict;
11 use needed;
12 use typelist;
13 use ndr;
15 # list of known types
16 my %typefamily;
18 sub get_typefamily($)
20 my $n = shift;
21 return $typefamily{$n};
24 # determine if an element needs a "buffers" section in NDR
25 sub need_buffers_section($)
27 my $e = shift;
28 if (!Ndr::can_contain_deferred($e) &&
29 !util::array_size($e)) {
30 return 0;
32 return 1;
35 # see if a variable needs to be allocated by the NDR subsystem on pull
36 sub need_alloc($)
38 my $e = shift;
40 return 0 if (util::has_property($e, "ref") && $e->{PARENT}->{TYPE} eq "FUNCTION");
41 return 1 if ($e->{POINTERS} || util::array_size($e));
42 return 0;
45 # Prefix to get the actual value of a variable
46 sub c_ptr_prefix($)
48 my $e = shift;
49 my $pointers = "";
50 foreach my $i (Ndr::need_wire_pointer($e)..$e->{POINTERS}-1) { $pointers.="*"; }
51 return $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
57 sub c_push_prefix($)
59 my $e = shift;
61 my $ret = "";
63 if ($e->{TYPE} =~ "string") {
64 $ret = "";
65 } elsif (Ndr::is_scalar_type($e->{TYPE}) and $e->{POINTERS} and
66 !util::array_size($e)) {
67 $ret .="*";
68 } elsif (!Ndr::is_scalar_type($e->{TYPE}) &&
69 !$e->{POINTERS} &&
70 !util::array_size($e)) {
71 return "&";
74 foreach my $i (2..$e->{POINTERS}) { $ret.="*"; }
76 return $ret;
79 # determine the C prefix used to refer to a variable when passing to a pull
80 # return '&' or ''
81 sub c_pull_prefix($)
83 my $e = shift;
85 if (!$e->{POINTERS} && !util::array_size($e)) {
86 return "&";
89 if ($e->{TYPE} =~ "string") {
90 return "&";
93 my $ret = "";
94 foreach my $i (2..$e->{POINTERS}) { $ret.="*"; }
95 return $ret;
97 my $res = "";
98 my $tabs = "";
99 sub pidl($)
101 my $d = shift;
102 if ($d) {
103 $res .= $tabs;
104 $res .= $d;
106 $res .="\n";
109 sub indent()
111 $tabs .= "\t";
114 sub deindent()
116 $tabs = substr($tabs, 0, -1);
119 ####################################################################
120 # work out the name of a size_is() variable
121 sub ParseExpr($$$)
123 my($e) = shift;
124 my($size) = shift;
125 my($var_prefix) = shift;
127 my($fn) = $e->{PARENT};
129 return $size if (util::is_constant($size));
131 return $size if ($size =~ /ndr->|\(/);
133 my $prefix = "";
135 if ($size =~ /\*(.*)/) {
136 $size = $1;
137 $prefix = "*";
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($)
167 my $size = shift;
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($)
179 my $size = shift;
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
188 sub fn_prefix($)
190 my $fn = shift;
192 if ($fn->{TYPE} eq "TYPEDEF" or
193 $fn->{TYPE} eq "FUNCTION") {
194 return "" if (util::has_property($fn, "public"));
197 return "static ";
200 ###################################################################
201 # setup any special flags for an element or structure
202 sub start_flags($)
204 my $e = shift;
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
214 sub end_flags($)
216 my $e = shift;
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($$$)
227 my $e = shift;
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($$$$)
243 my $e = shift;
244 my $ndr = shift;
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));";
261 $size = $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));";
266 } else {
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 #####################################################################
272 # print an array
273 sub ParseArrayPrint($$)
275 my $e = shift;
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);";
286 } else {
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($$)
295 my $e = shift;
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}) {";
301 indent;
302 check_null_pointer($size);
303 pidl "NDR_CHECK(ndr_check_array_size(ndr, (void*)&$var_prefix$e->{NAME}, $size));";
304 deindent;
305 pidl "}";
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}) {";
312 indent;
313 check_null_pointer($length);
314 pidl "NDR_CHECK(ndr_check_array_length(ndr, (void*)&$var_prefix$e->{NAME}, $length));";
315 deindent;
316 pidl "}"
320 sub ParseArrayPullPreceding($$$)
322 my $e = shift;
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($$$$)
336 my $e = shift;
337 my $ndr = shift;
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);
343 my $size = $length;
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) {";
369 indent;
370 pidl "return ndr_pull_error($ndr, NDR_ERR_CONFORMANT_SIZE, \"Bad conformant size %u should be %u\", $size, $length);";
371 deindent;
372 pidl "}";
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);";
386 pidl "}";
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));";
392 } else {
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($)
399 my $e = shift;
400 my $compression = util::has_property($e, "compression");
401 my ($alg, $clen, $dlen) = split(/ /, $compression);
403 return $alg;
406 sub compression_clen($)
408 my $e = shift;
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($)
417 my $e = shift;
418 my $compression = util::has_property($e, "compression");
419 my ($alg, $clen, $dlen) = split(/ /, $compression);
421 return ParseExpr($e, $dlen, "r->");
424 sub ParseCompressionPushStart($$$)
426 my $e = shift;
427 my $subndr = shift;
428 my $ndr_flags = shift;
429 my $comndr = $subndr."_compressed";
431 pidl "{";
432 indent;
433 pidl "struct ndr_push *$comndr;";
434 pidl "";
435 pidl "$comndr = ndr_push_init_ctx($subndr);";
436 pidl "if (!$comndr) return NT_STATUS_NO_MEMORY;";
437 pidl "$comndr->flags = $subndr->flags;";
438 pidl "";
440 return $comndr;
443 sub ParseCompressionPushEnd($$)
445 my $e = shift;
446 my $subndr = shift;
447 my $comndr = $subndr."_compressed";
448 my $alg = compression_alg($e);
450 pidl "NDR_CHECK(ndr_push_compression($subndr, $comndr, $alg));";
451 deindent;
452 pidl "}";
455 sub ParseCompressionPullStart($$$)
457 my $e = shift;
458 my $subndr = shift;
459 my $ndr_flags = shift;
460 my $comndr = $subndr."_compressed";
461 my $alg = compression_alg($e);
462 my $dlen = compression_dlen($e);
464 pidl "{";
465 indent;
466 pidl "struct ndr_pull *$comndr;";
467 pidl "NDR_ALLOC($subndr, $comndr);";
468 pidl "NDR_CHECK(ndr_pull_compression($subndr, $comndr, $alg, $dlen));";
470 return $comndr;
473 sub ParseCompressionPullEnd($$)
475 my $e = shift;
476 my $subndr = shift;
477 my $comndr = $subndr."_compressed";
479 deindent;
480 pidl "}";
483 sub ParseSubcontextPushStart($$)
485 my $e = shift;
486 my $ndr_flags = shift;
487 my $compression = util::has_property($e, "compression");
488 my $retndr;
490 pidl "if (($ndr_flags) & NDR_SCALARS) {";
491 indent;
492 pidl "struct ndr_push *_ndr_$e->{NAME};";
493 pidl "";
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;";
497 pidl "";
499 $retndr = "_ndr_$e->{NAME}";
501 if (defined $compression) {
502 $retndr = ParseCompressionPushStart($e, $retndr, "NDR_SCALARS");
505 return $retndr
508 sub ParseSubcontextPushEnd($)
510 my $e = shift;
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)) {
521 $size_is = "-1";
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));";
526 deindent;
527 pidl "}";
530 sub ParseSubcontextPullStart($$)
532 my $e = shift;
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)) {
540 $size_is = "-1";
543 pidl "if (($ndr_flags) & NDR_SCALARS) {";
544 indent;
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);
553 return $retndr;
556 sub ParseSubcontextPullEnd($)
558 my $e = shift;
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);
568 my $advance;
569 if (defined ($size_is)) {
570 $advance = "$size_is";
571 } elsif ($header_size) {
572 $advance = "$subndr->data_size";
573 } else {
574 $advance = "$subndr->offset";
576 pidl "NDR_CHECK(ndr_pull_advance(ndr, $advance));";
577 deindent;
578 pidl "}";
581 #####################################################################
582 # parse scalars in a structure element
583 sub ParseElementPushScalar($$$)
585 my($e) = shift;
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");
591 my $ndr = "ndr";
592 my $subndr = undef;
594 start_flags($e);
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");
602 $ndr = $subndr;
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
611 } else {
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);
623 end_flags($e);
626 #####################################################################
627 # parse a pointer in a struct element or function
628 sub ParsePtrPush($$)
630 my $e = shift;
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}));";
637 } else {
638 pidl "NDR_CHECK(ndr_push_unique_ptr(ndr, $var_prefix$e->{NAME}));";
642 #####################################################################
643 # print scalars in a structure element
644 sub ParseElementPrint($$)
646 my($e) = shift;
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;";
656 pidl "}";
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}) {";
667 indent;
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});";
678 } else {
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)) {
684 deindent;
685 pidl "}";
687 pidl "ndr->depth--;";
691 #####################################################################
692 # parse scalars in a structure element - pull size
693 sub ParseSwitchPull($$$$$)
695 my($e) = shift;
696 my $ndr = shift;
697 my($var_prefix) = shift;
698 my($ndr_flags) = shift;
699 my $switch = 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($$$$$)
716 my($e) = shift;
717 my $ndr = shift;
718 my($var_prefix) = shift;
719 my($ndr_flags) = shift;
720 my $switch = 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($$$)
734 my($e) = shift;
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");
740 my $ndr = "ndr";
741 my $subndr = undef;
743 start_flags($e);
745 if (defined $sub_size && $e->{POINTERS} == 0) {
746 $subndr = ParseSubcontextPullStart($e, $ndr_flags);
747 $ndr = $subndr;
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)) {
756 } else {
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\");";
768 pidl "}";
771 if (defined $sub_size && $e->{POINTERS} == 0) {
772 ParseSubcontextPullEnd($e);
775 end_flags($e);
778 #####################################################################
779 # parse a pointer in a struct element or function
780 sub ParsePtrPull($$)
782 my($e) = shift;
783 my($var_prefix) = shift;
785 if (util::has_property($e, "ref")) {
786 pidl "NDR_CHECK(ndr_pull_ref_ptr(ndr, &_ptr_$e->{NAME}));";
787 } else {
788 pidl "NDR_CHECK(ndr_pull_unique_ptr(ndr, &_ptr_$e->{NAME}));";
790 pidl "if (_ptr_$e->{NAME}) {";
791 indent;
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}));";
796 deindent;
797 pidl "} else {";
798 pidl "\t$var_prefix$e->{NAME} = NULL;";
799 pidl "}";
802 #####################################################################
803 # parse buffers in a structure element
804 sub ParseElementPushBuffer($$)
806 my($e) = shift;
807 my($var_prefix) = shift;
808 my $cprefix = c_push_prefix($e);
809 my $sub_size = util::has_property($e, "subcontext");
810 my $ndr = "ndr";
811 my $subndr = undef;
813 return unless (need_buffers_section($e));
815 start_flags($e);
817 my $pointers = c_ptr_prefix($e);
818 for my $i (1..Ndr::need_wire_pointer($e)) {
819 if ($i > 1) {
820 ParsePtrPush($e,$pointers.$var_prefix);
822 pidl "if ($pointers$var_prefix$e->{NAME}) {";
823 indent;
824 $pointers.="*";
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);
839 $ndr = $subndr;
840 $ndr_flags = "NDR_SCALARS|NDR_BUFFERS";
843 if (util::array_size($e)) {
844 ParseArrayPush($e, $ndr, "r->", $ndr_flags);
845 } else {
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)) {
858 deindent;
859 pidl "}";
862 end_flags($e);
865 #####################################################################
866 # parse buffers in a structure element - pull side
867 sub ParseElementPullBuffer($$)
869 my($e) = shift;
870 my($var_prefix) = shift;
871 my $cprefix = c_pull_prefix($e);
872 my $sub_size = util::has_property($e, "subcontext");
873 my $ndr = "ndr";
874 my $subndr = undef;
876 return unless (need_buffers_section($e));
878 start_flags($e);
880 my $pointers = c_ptr_prefix($e);
881 for my $i (1..Ndr::need_wire_pointer($e)) {
882 if ($i > 1) {
883 ParsePtrPull($e,$pointers.$var_prefix);
885 pidl "if ($pointers$var_prefix$e->{NAME}) {";
886 indent;
887 $pointers.="*";
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);
904 $ndr = $subndr;
905 $ndr_flags = "NDR_SCALARS|NDR_BUFFERS";
908 if (util::array_size($e)) {
909 ParseArrayPull($e, $ndr, "r->", $ndr_flags);
910 } else {
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)) {
927 deindent;
928 pidl "}";
931 end_flags($e);
934 #####################################################################
935 # parse a struct
936 sub ParseStructPush($)
938 my($struct) = shift;
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
948 # alignment)
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");
970 pidl "buffers:";
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);";
978 pidl "done:";
980 end_flags($struct);
983 #####################################################################
984 # generate a push function for an enum
985 sub ParseEnumPush($)
987 my($enum) = shift;
988 my($type_fn) = typelist::enum_type_fn($enum);
990 start_flags($enum);
992 pidl "NDR_CHECK(ndr_push_$type_fn(ndr, NDR_SCALARS, r));";
994 end_flags($enum);
997 #####################################################################
998 # generate a pull function for an enum
999 sub ParseEnumPull($)
1001 my($enum) = shift;
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;";
1006 start_flags($enum);
1007 pidl "NDR_CHECK(ndr_pull_$type_fn(ndr, NDR_SCALARS, &v));";
1008 pidl "*r = v;";
1010 end_flags($enum);
1013 #####################################################################
1014 # generate a print function for an enum
1015 sub ParseEnumPrint($)
1017 my($enum) = shift;
1019 pidl "const char *val = NULL;";
1020 pidl "";
1022 start_flags($enum);
1024 pidl "switch (r) {";
1025 indent;
1026 my $els = \@{$enum->{ELEMENTS}};
1027 foreach my $i (0 .. $#{$els}) {
1028 my $e = ${$els}[$i];
1029 chomp $e;
1030 if ($e =~ /^(.*)=/) {
1031 $e = $1;
1033 pidl "case $e: val = \"$e\"; break;";
1036 deindent;
1037 pidl "}";
1039 pidl "ndr_print_enum(ndr, name, \"$enum->{TYPE}\", val, r);";
1041 end_flags($enum);
1044 sub ArgsEnumPush($)
1046 my $e = shift;
1047 return "struct ndr_push *ndr, int ndr_flags, enum $e->{NAME} r";
1050 sub ArgsEnumPrint($)
1052 my $e = shift;
1053 return "struct ndr_print *ndr, const char *name, enum $e->{NAME} r";
1056 sub ArgsEnumPull($)
1058 my $e = shift;
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));";
1082 end_flags($bitmap);
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));";
1096 pidl "*r = v;";
1098 end_flags($bitmap);
1101 #####################################################################
1102 # generate a print function for an bitmap
1103 sub ParseBitmapPrintElement($$)
1105 my($e) = shift;
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};
1110 my($flag);
1112 if ($e =~ /^(\w+) .*$/) {
1113 $flag = "$1";
1114 } else {
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--;";
1139 end_flags($bitmap);
1142 sub ArgsBitmapPush($)
1144 my $e = shift;
1145 my $type_decl = typelist::mapType($e->{DATA});
1146 return "struct ndr_push *ndr, int ndr_flags, $type_decl r";
1149 sub ArgsBitmapPrint($)
1151 my $e = shift;
1152 my $type_decl = typelist::mapType($e->{DATA});
1153 return "struct ndr_print *ndr, const char *name, $type_decl r";
1156 sub ArgsBitmapPull($)
1158 my $e = shift;
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--;";
1191 end_flags($struct);
1194 #####################################################################
1195 # parse a struct - pull side
1196 sub ParseStructPull($)
1198 my($struct) = shift;
1199 my $conform_e;
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
1207 # alignment)
1208 my $e = $struct->{ELEMENTS}[-1];
1209 if (Ndr::is_conformant_array($e) and Ndr::is_surrounding_array($e)) {
1210 $conform_e = $e;
1213 if (defined $e->{TYPE} && $e->{TYPE} eq "string"
1214 && util::property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
1215 $conform_e = $e;
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");
1242 pidl "buffers:\n";
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);";
1254 pidl "done:";
1256 end_flags($struct);
1259 #####################################################################
1260 # calculate size of ndr struct
1261 sub ParseStructNdrSize($)
1263 my $t = shift;
1264 my $static = fn_prefix($t);
1265 my $sizevar;
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($)
1275 my $e = shift;
1276 return "struct ndr_push *ndr, int ndr_flags, struct $e->{NAME} *r";
1279 sub ArgsStructPrint($)
1281 my $e = shift;
1282 return "struct ndr_print *ndr, const char *name, struct $e->{NAME} *r";
1285 sub ArgsStructPull($)
1287 my $e = shift;
1288 return "struct ndr_pull *ndr, int ndr_flags, struct $e->{NAME} *r";
1291 sub ArgsStructNdrSize($)
1293 my $d = shift;
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($)
1312 my $t = shift;
1313 my $static = fn_prefix($t);
1314 my $sizevar;
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($)
1326 my $e = shift;
1327 my $have_default = 0;
1329 pidl "int level;";
1331 start_flags($e);
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) {";
1349 indent;
1350 foreach my $el (@{$e->{ELEMENTS}}) {
1351 if (util::has_property($el, "default")) {
1352 pidl "default:";
1353 $have_default = 1;
1354 } else {
1355 pidl "case $el->{PROPERTIES}->{case}:";
1358 if ($el->{TYPE} ne "EMPTY") {
1359 indent;
1360 ParseElementPushScalar($el, "r->", "NDR_SCALARS");
1361 deindent;
1363 pidl "break;";
1364 pidl "";
1366 if (! $have_default) {
1367 pidl "default:";
1368 pidl "\treturn ndr_push_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);";
1370 deindent;
1371 pidl "}";
1372 pidl "buffers:";
1373 pidl "if (!(ndr_flags & NDR_BUFFERS)) goto done;";
1374 pidl "switch (level) {";
1375 indent;
1376 foreach my $el (@{$e->{ELEMENTS}}) {
1377 if (util::has_property($el, "default")) {
1378 pidl "default:";
1379 } else {
1380 pidl "case $el->{PROPERTIES}->{case}:";
1382 if ($el->{TYPE} ne "EMPTY") {
1383 indent;
1384 ParseElementPushBuffer($el, "r->");
1385 deindent;
1387 pidl "break;";
1388 pidl "";
1390 if (! $have_default) {
1391 pidl "default:";
1392 pidl "\treturn ndr_push_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);";
1394 deindent;
1395 pidl "}";
1396 pidl "ndr_push_struct_end(ndr);";
1397 pidl "done:";
1398 end_flags($e);
1401 #####################################################################
1402 # print a union
1403 sub ParseUnionPrint($)
1405 my $e = shift;
1406 my $have_default = 0;
1407 my($name) = $e->{PARENT}->{NAME};
1409 pidl "ndr_print_union(ndr, name, level, \"$name\");";
1410 start_flags($e);
1412 pidl "switch (level) {";
1413 indent;
1414 foreach my $el (@{$e->{ELEMENTS}}) {
1415 if (util::has_property($el, "default")) {
1416 $have_default = 1;
1417 pidl "default:";
1418 } else {
1419 pidl "case $el->{PROPERTIES}->{case}:";
1421 if ($el->{TYPE} ne "EMPTY") {
1422 indent;
1423 ParseElementPrint($el, "r->");
1424 deindent;
1426 pidl "break;";
1427 pidl "";
1429 if (! $have_default) {
1430 pidl "default:";
1431 pidl "\tndr_print_bad_level(ndr, name, level);";
1433 deindent;
1434 pidl "}";
1436 end_flags($e);
1439 #####################################################################
1440 # parse a union - pull side
1441 sub ParseUnionPull($)
1443 my $e = shift;
1444 my $have_default = 0;
1445 my $switch_type = util::has_property($e, "switch_type");
1446 $switch_type = "uint32" unless defined($switch_type);
1448 pidl "int level;";
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;";
1456 start_flags($e);
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);";
1466 pidl "}";
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) {";
1475 indent;
1476 foreach my $el (@{$e->{ELEMENTS}}) {
1477 if (util::has_property($el, "default")) {
1478 pidl "default: {";
1479 $have_default = 1;
1480 } else {
1481 pidl "case $el->{PROPERTIES}->{case}: {";
1483 if ($el->{TYPE} ne "EMPTY") {
1484 indent;
1485 if ($el->{POINTERS}) {
1486 pidl "uint32_t _ptr_$el->{NAME};";
1488 ParseElementPullScalar($el, "r->", "NDR_SCALARS");
1489 deindent;
1491 pidl "break; }";
1492 pidl "";
1494 if (! $have_default) {
1495 pidl "default:";
1496 pidl "\treturn ndr_pull_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);";
1498 deindent;
1499 pidl "}";
1500 pidl "buffers:";
1501 pidl "if (!(ndr_flags & NDR_BUFFERS)) goto done;";
1502 pidl "switch (level) {";
1503 indent;
1504 foreach my $el (@{$e->{ELEMENTS}}) {
1505 if (util::has_property($el, "default")) {
1506 pidl "default:";
1507 } else {
1508 pidl "case $el->{PROPERTIES}->{case}:";
1510 if ($el->{TYPE} ne "EMPTY") {
1511 indent;
1512 ParseElementPullBuffer($el, "r->");
1513 deindent;
1515 pidl "break;";
1516 pidl "";
1518 if (! $have_default) {
1519 pidl "default:";
1520 pidl "\treturn ndr_pull_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);";
1522 deindent;
1523 pidl "}";
1524 pidl "ndr_pull_struct_end(ndr);";
1525 pidl "done:";
1526 end_flags($e);
1529 sub ArgsUnionPush($)
1531 my $e = shift;
1532 return "struct ndr_push *ndr, int ndr_flags, union $e->{NAME} *r";
1535 sub ArgsUnionPrint($)
1537 my $e = shift;
1538 return "struct ndr_print *ndr, const char *name, int level, union $e->{NAME} *r";
1541 sub ArgsUnionPull($)
1543 my $e = shift;
1544 return "struct ndr_pull *ndr, int ndr_flags, union $e->{NAME} *r";
1547 sub ArgsUnionNdrSize($)
1549 my $d = shift;
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($)
1568 my($e) = shift;
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)";
1576 pidl "{";
1577 indent;
1578 $typefamily{$e->{DATA}->{TYPE}}->{PUSH_FN_BODY}->($e->{DATA});
1579 pidl "return NT_STATUS_OK;";
1580 deindent;
1581 pidl "}";
1582 pidl "";;
1586 #####################################################################
1587 # parse a typedef - pull side
1588 sub ParseTypedefPull($)
1590 my($e) = shift;
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)";
1599 pidl "{";
1600 indent;
1601 $typefamily{$e->{DATA}->{TYPE}}->{PULL_FN_BODY}->($e->{DATA});
1602 pidl "return NT_STATUS_OK;";
1603 deindent;
1604 pidl "}";
1605 pidl "";
1608 #####################################################################
1609 # parse a typedef - print side
1610 sub ParseTypedefPrint($)
1612 my($e) = shift;
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)";
1619 pidl "{";
1620 indent;
1621 $typefamily{$e->{DATA}->{TYPE}}->{PRINT_FN_BODY}->($e->{DATA});
1622 deindent;
1623 pidl "}";
1626 #####################################################################
1627 ## calculate the size of a structure
1628 sub ParseTypedefNdrSize($)
1630 my($t) = shift;
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)";
1638 pidl "{";
1639 indent;
1640 $typefamily{$t->{DATA}->{TYPE}}->{SIZE_FN_BODY}->($t);
1641 deindent;
1642 pidl "}";
1643 pidl "";
1646 #####################################################################
1647 # parse a function - print side
1648 sub ParseFunctionPrint($)
1650 my($fn) = shift;
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)";
1655 pidl "{";
1656 indent;
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;";
1662 pidl "}";
1664 pidl "if (flags & NDR_IN) {";
1665 indent;
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--;";
1675 deindent;
1676 pidl "}";
1678 pidl "if (flags & NDR_OUT) {";
1679 indent;
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") {
1688 my $cprefix = "&";
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--;";
1693 deindent;
1694 pidl "}";
1696 pidl "ndr->depth--;";
1697 deindent;
1698 pidl "}";
1699 pidl "";
1702 #####################################################################
1703 # parse a function element
1704 sub ParseFunctionElementPush($$)
1706 my $e = shift;
1707 my $inout = shift;
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}) {";
1713 indent;
1714 ParseArrayPush($e, "ndr", "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1715 deindent;
1716 pidl "}";
1717 } else {
1718 ParseArrayPush($e, "ndr", "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1720 } else {
1721 ParseElementPushScalar($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1722 if (Ndr::need_wire_pointer($e)) {
1723 ParseElementPushBuffer($e, "r->$inout.");
1728 #####################################################################
1729 # parse a function
1730 sub ParseFunctionPush($)
1732 my($fn) = shift;
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)";
1738 pidl "{";
1739 indent;
1741 pidl "if (!(flags & NDR_IN)) goto ndr_out;";
1742 pidl "";
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");
1753 pidl "ndr_out:";
1754 pidl "if (!(flags & NDR_OUT)) goto done;";
1755 pidl "";
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));";
1770 pidl "done:";
1771 pidl "return NT_STATUS_OK;";
1772 deindent;
1773 pidl "}";
1774 pidl "";
1777 #####################################################################
1778 # parse a function element
1779 sub ParseFunctionElementPull($$)
1781 my $e = shift;
1782 my $inout = shift;
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}) {";
1789 indent;
1790 } elsif ($inout eq "out" && util::has_property($e, "ref")) {
1791 pidl "if (r->$inout.$e->{NAME}) {";
1792 indent;
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"))) {
1798 deindent;
1799 pidl "}";
1801 } else {
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});";
1805 pidl "}";
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($)
1823 my $e = shift;
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
1828 # the in side
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};";
1835 } else {
1836 pidl "ZERO_STRUCTP(r->out.$e->{NAME});";
1838 return;
1841 # its an array
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}));";
1847 } else {
1848 pidl "memset(r->out.$e->{NAME}, 0, $size * sizeof(*r->out.$e->{NAME}));";
1852 #####################################################################
1853 # parse a function
1854 sub ParseFunctionPull($)
1856 my($fn) = shift;
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)";
1863 pidl "{";
1864 indent;
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;";
1874 pidl "";
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);";
1883 pidl "";
1884 last;
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.");
1905 pidl "ndr_out:";
1906 pidl "if (!(flags & NDR_OUT)) goto done;";
1907 pidl "";
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));";
1925 pidl "done:";
1926 pidl "";
1927 pidl "return NT_STATUS_OK;";
1928 deindent;
1929 pidl "}";
1930 pidl "";
1933 #####################################################################
1934 # produce a function call table
1935 sub FunctionTable($)
1937 my($interface) = shift;
1938 my($data) = $interface->{INHERITED_DATA};
1939 my $count = 0;
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") {
1951 pidl "\t{";
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}";
1957 pidl "\t},";
1960 pidl "\t{ NULL, 0, NULL, NULL, NULL }";
1961 pidl "};";
1962 pidl "";
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) {
1974 pidl "\t$ep, ";
1976 pidl "};";
1977 pidl "";
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";
1982 pidl "};";
1983 pidl "";
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) {
1994 pidl "\t$ap, ";
1996 pidl "};";
1997 pidl "";
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";
2002 pidl "};";
2003 pidl "";
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";
2014 pidl "};";
2015 pidl "";
2017 pidl "static NTSTATUS dcerpc_ndr_$interface->{NAME}_init(void)";
2018 pidl "{";
2019 pidl "\treturn librpc_register_interface(&dcerpc_table_$interface->{NAME});";
2020 pidl "}";
2021 pidl "";
2024 #####################################################################
2025 # parse the interface definitions
2026 sub ParseInterface($)
2028 my($interface) = shift;
2029 my($data) = $interface->{DATA};
2031 # Push functions
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($$)
2054 my $idl = shift;
2055 my $filename = shift;
2057 $filename =~ /.*\/ndr_(.*).c/;
2058 my $basename = $1;
2059 pidl "NTSTATUS dcerpc_$basename\_init(void)";
2060 pidl "{";
2061 indent;
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};
2067 my $count = 0;
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;";
2077 pidl "}";
2078 pidl "";
2080 pidl "return status;";
2081 deindent;
2082 pidl "}";
2083 pidl "";
2086 sub CheckPointerTypes($$)
2088 my $s = shift;
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($)
2106 my $x = shift;
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"
2132 sub Load($)
2134 my $idl = shift;
2136 foreach my $x (@{$idl}) {
2137 LoadInterface($x);
2141 #####################################################################
2142 # parse a parsed IDL structure back into an IDL file
2143 sub Parse($$)
2145 my($idl) = shift;
2146 my($filename) = shift;
2147 my $h_filename = $filename;
2148 $res = "";
2150 Load($idl);
2152 if ($h_filename =~ /(.*)\.c/) {
2153 $h_filename = "$1.h";
2156 pidl "/* parser auto-generated by pidl */";
2157 pidl "";
2158 pidl "#include \"includes.h\"";
2159 pidl "#include \"$h_filename\"";
2160 pidl "";
2162 foreach my $x (@{$idl}) {
2163 if ($x->{TYPE} eq "INTERFACE") {
2164 needed::BuildNeeded($x);
2165 ParseInterface($x);
2169 RegistrationFunction($idl, $filename);
2171 return $res;