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
6 # released under the GNU GPL
34 "error_status_t" => 4,
43 foreach my $k (keys %type_alignments) {
49 ALIGN
=> $type_alignments{$k}
58 return 1 if (defined($typedefs{$type}) and $typedefs{$type}->{DATA
}->{TYPE
} eq "SCALAR");
59 return 1 if (util
::is_enum
($type));
60 return 1 if (util
::is_bitmap
($type));
65 # determine if an element needs a reference pointer on the wire
66 # in its NDR representation
67 sub need_wire_pointer
($)
71 !util
::has_property
($e, "ref")) {
72 return $e->{POINTERS
};
77 # determine if an element is a pure scalar. pure scalars do not
78 # have a "buffers" section in NDR
82 if (util
::has_property
($e, "ref")) {
85 if (is_scalar_type
($e->{TYPE
}) &&
87 !util
::array_size
($e)) {
93 # see if a variable needs to be allocated by the NDR subsystem on pull
98 if (util
::has_property
($e, "ref")) {
102 if ($e->{POINTERS
} || util
::array_size
($e)) {
110 # determine the C prefix used to refer to a variable when passing to a push
111 # function. This will be '*' for pointers to scalar types, '' for scalar
112 # types and normal pointers and '&' for pass-by-reference structures
117 if ($e->{TYPE
} =~ "string") {
121 if (is_scalar_type
($e->{TYPE
}) &&
125 if (!is_scalar_type
($e->{TYPE
}) &&
127 !util
::array_size
($e)) {
134 # determine the C prefix used to refer to a variable when passing to a pull
140 if (!$e->{POINTERS
} && !util
::array_size
($e)) {
144 if ($e->{TYPE
} =~ "string") {
156 #####################################################################
157 # parse a properties list
158 sub ParseProperties
($)
161 foreach my $d (@
{$props}) {
162 if (ref($d) ne "HASH") {
165 foreach my $k (keys %{$d}) {
166 pidl
"[$k($d->{$k})] ";
172 ###################################
173 # find a sibling var in a structure
178 my($fn) = $e->{PARENT
};
180 if ($name =~ /\*(.*)/) {
184 if ($fn->{TYPE
} eq "FUNCTION") {
185 for my $e2 (@
{$fn->{ELEMENTS
}}) {
186 if ($e2->{NAME
} eq $name) {
192 for my $e2 (@
{$fn->{ELEMENTS
}}) {
193 if ($e2->{NAME
} eq $name) {
197 die "invalid sibling '$name'";
200 ####################################################################
201 # work out the name of a size_is() variable
202 sub find_size_var
($$$)
206 my($var_prefix) = shift;
208 my($fn) = $e->{PARENT
};
210 if (util
::is_constant
($size)) {
214 if ($size =~ /ndr->|\(/) {
220 if ($size =~ /\*(.*)/) {
225 if ($fn->{TYPE
} ne "FUNCTION") {
226 return $prefix . "r->$size";
229 my $e2 = find_sibling
($e, $size);
231 if (util
::has_property
($e2, "in") && util
::has_property
($e2, "out")) {
232 return $prefix . "$var_prefix$size";
234 if (util
::has_property
($e2, "in")) {
235 return $prefix . "r->in.$size";
237 if (util
::has_property
($e2, "out")) {
238 return $prefix . "r->out.$size";
241 die "invalid variable in $size for element $e->{NAME} in $fn->{NAME}\n";
244 #####################################################################
245 # check that a variable we get from find_size_var isn't a null pointer
246 sub check_null_pointer
($)
249 if ($size =~ /^\*/) {
250 my $size2 = substr($size, 1);
251 pidl
"\tif ($size2 == NULL) return NT_STATUS_INVALID_PARAMETER_MIX;\n";
255 #####################################################################
256 # check that a variable we get from find_size_var isn't a null pointer
257 # void return varient
258 sub check_null_pointer_void
($)
261 if ($size =~ /^\*/) {
262 my $size2 = substr($size, 1);
263 pidl
"\tif ($size2 == NULL) return;\n";
268 #####################################################################
269 # work out is a parse function should be declared static or not
273 if ($fn->{TYPE
} eq "TYPEDEF") {
274 if (util
::has_property
($fn, "public")) {
279 if ($fn->{TYPE
} eq "FUNCTION") {
280 if (util
::has_property
($fn, "public")) {
288 ###################################################################
289 # setup any special flags for an element or structure
293 my $flags = util
::has_property
($e, "flag");
294 if (defined $flags) {
295 pidl
"\t{ uint32_t _flags_save_$e->{TYPE} = ndr->flags;\n";
296 pidl
"\tndr_set_flags(&ndr->flags, $flags);\n";
300 ###################################################################
301 # end any special flags for an element or structure
305 my $flags = util
::has_property
($e, "flag");
306 if (defined $flags) {
307 pidl
"\tndr->flags = _flags_save_$e->{TYPE};\n\t}\n";
312 #####################################################################
313 # work out the correct alignment for a structure or union
319 for my $e (@
{$s->{ELEMENTS
}}) {
322 if (need_wire_pointer
($e)) {
325 $a = align_type
($e->{TYPE
});
328 $align = $a if ($align < $a);
334 #####################################################################
340 unless (defined($typedefs{$e})) {
341 # it must be an external type - all we can do is guess
342 # print "Warning: assuming alignment of unknown type '$e' is 4\n";
346 my $dt = $typedefs{$e}->{DATA
};
348 if ($dt->{TYPE
} eq "STRUCT") {
349 return struct_alignment
($dt);
350 } elsif($dt->{TYPE
} eq "UNION") {
351 return struct_alignment
($dt);
352 } elsif ($dt->{TYPE
} eq "ENUM") {
353 return align_type
(util
::enum_type_fn
(util
::get_enum
($e)));
354 } elsif ($dt->{TYPE
} eq "BITMAP") {
355 return align_type
(util
::bitmap_type_fn
(util
::get_bitmap
($e)));
356 } elsif ($dt->{TYPE
} eq "SCALAR") {
360 die("Internal pidl error. Typedef has unknown data type $dt->{TYPE}!");
363 #####################################################################
364 # parse an array - push side
365 sub ParseArrayPush
($$$)
368 my $var_prefix = shift;
369 my $ndr_flags = shift;
371 my $size = find_size_var
($e, util
::array_size
($e), $var_prefix);
373 if (defined $e->{CONFORMANT_SIZE
}) {
374 # the conformant size has already been pushed
375 } elsif (!util
::is_inline_array
($e)) {
376 # we need to emit the array size
377 pidl
"\t\tNDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, $size));\n";
380 if (my $length = util
::has_property
($e, "length_is")) {
381 $length = find_size_var
($e, $length, $var_prefix);
382 pidl
"\t\tNDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));\n";
383 pidl
"\t\tNDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, $length));\n";
387 if (is_scalar_type
($e->{TYPE
})) {
388 pidl
"\t\tNDR_CHECK(ndr_push_array_$e->{TYPE}(ndr, $ndr_flags, $var_prefix$e->{NAME}, $size));\n";
390 pidl
"\t\tNDR_CHECK(ndr_push_array(ndr, $ndr_flags, $var_prefix$e->{NAME}, sizeof($var_prefix$e->{NAME}\[0]), $size, (ndr_push_flags_fn_t)ndr_push_$e->{TYPE}));\n";
394 #####################################################################
396 sub ParseArrayPrint
($$)
399 my $var_prefix = shift;
400 my $size = find_size_var
($e, util
::array_size
($e), $var_prefix);
401 my $length = util
::has_property
($e, "length_is");
403 if (defined $length) {
404 $size = find_size_var
($e, $length, $var_prefix);
407 if (is_scalar_type
($e->{TYPE
})) {
408 pidl
"\t\tndr_print_array_$e->{TYPE}(ndr, \"$e->{NAME}\", $var_prefix$e->{NAME}, $size);\n";
410 pidl
"\t\tndr_print_array(ndr, \"$e->{NAME}\", $var_prefix$e->{NAME}, sizeof($var_prefix$e->{NAME}\[0]), $size, (ndr_print_fn_t)ndr_print_$e->{TYPE});\n";
414 #####################################################################
415 # check the size_is and length_is constraints
416 sub CheckArraySizes
($$)
419 my $var_prefix = shift;
421 if (!defined $e->{CONFORMANT_SIZE
} &&
422 util
::has_property
($e, "size_is")) {
423 my $size = find_size_var
($e, util
::array_size
($e), $var_prefix);
424 pidl
"\tif ($var_prefix$e->{NAME}) {\n";
425 check_null_pointer
($size);
426 pidl
"\t\tNDR_CHECK(ndr_check_array_size(ndr, (void*)&$var_prefix$e->{NAME}, $size));\n";
430 if (my $length = util
::has_property
($e, "length_is")) {
431 $length = find_size_var
($e, $length, $var_prefix);
432 pidl
"\tif ($var_prefix$e->{NAME}) {\n";
433 check_null_pointer
($length);
434 pidl
"\t\tNDR_CHECK(ndr_check_array_length(ndr, (void*)&$var_prefix$e->{NAME}, $length));\n";
440 #####################################################################
441 # parse an array - pull side
442 sub ParseArrayPull
($$$)
445 my $var_prefix = shift;
446 my $ndr_flags = shift;
448 my $size = find_size_var
($e, util
::array_size
($e), $var_prefix);
449 my $alloc_size = $size;
451 # if this is a conformant array then we use that size to allocate, and make sure
452 # we allocate enough to pull the elements
453 if (defined $e->{CONFORMANT_SIZE
}) {
454 $alloc_size = $e->{CONFORMANT_SIZE
};
455 check_null_pointer
($size);
456 pidl
"\tif ($size > $alloc_size) {\n";
457 pidl
"\t\treturn ndr_pull_error(ndr, NDR_ERR_CONFORMANT_SIZE, \"Bad conformant size %u should be %u\", $alloc_size, $size);\n";
459 } elsif (!util
::is_inline_array
($e)) {
460 if ($var_prefix =~ /^r->out/ && $size =~ /^\*r->in/) {
461 my $size2 = substr($size, 1);
462 pidl
"if (ndr->flags & LIBNDR_FLAG_REF_ALLOC) { NDR_ALLOC(ndr, $size2); }\n";
465 # non fixed arrays encode the size just before the array
466 pidl
"\t\tNDR_CHECK(ndr_pull_array_size(ndr, &$var_prefix$e->{NAME}));\n";
467 $alloc_size = "ndr_get_array_size(ndr, &$var_prefix$e->{NAME})";
470 if ((need_alloc
($e) && !util
::is_fixed_array
($e)) ||
471 ($var_prefix eq "r->in." && util
::has_property
($e, "ref"))) {
472 if (!util
::is_inline_array
($e) || $ndr_flags eq "NDR_SCALARS") {
473 pidl
"\t\tNDR_ALLOC_N(ndr, $var_prefix$e->{NAME}, $alloc_size);\n";
477 if (($var_prefix eq "r->out." && util
::has_property
($e, "ref"))) {
478 if (!util
::is_inline_array
($e) || $ndr_flags eq "NDR_SCALARS") {
479 pidl
"\tif (ndr->flags & LIBNDR_FLAG_REF_ALLOC) {";
480 pidl
"\t\tNDR_ALLOC_N(ndr, $var_prefix$e->{NAME}, $alloc_size);\n";
485 if (my $length = util
::has_property
($e, "length_is")) {
486 pidl
"\t\tNDR_CHECK(ndr_pull_array_length(ndr, &$var_prefix$e->{NAME}));\n";
487 $size = "ndr_get_array_length(ndr, &$var_prefix$e->{NAME})";
490 check_null_pointer
($size);
491 if (is_scalar_type
($e->{TYPE
})) {
492 pidl
"\t\tNDR_CHECK(ndr_pull_array_$e->{TYPE}(ndr, $ndr_flags, $var_prefix$e->{NAME}, $size));\n";
494 pidl
"\t\tNDR_CHECK(ndr_pull_array(ndr, $ndr_flags, (void **)$var_prefix$e->{NAME}, sizeof($var_prefix$e->{NAME}\[0]), $size, (ndr_pull_flags_fn_t)ndr_pull_$e->{TYPE}));\n";
499 #####################################################################
500 # parse scalars in a structure element
501 sub ParseElementPushScalar
($$$)
504 my($var_prefix) = shift;
505 my($ndr_flags) = shift;
506 my $cprefix = c_push_prefix
($e);
507 my $sub_size = util
::has_property
($e, "subcontext");
511 if (my $value = util
::has_property
($e, "value")) {
512 pidl
"\t$cprefix$var_prefix$e->{NAME} = $value;\n";
515 if (util
::has_property
($e, "relative")) {
516 pidl
"\tNDR_CHECK(ndr_push_relative1(ndr, $var_prefix$e->{NAME}));\n";
517 } elsif (util
::is_inline_array
($e)) {
518 ParseArrayPush
($e, "r->", "NDR_SCALARS");
519 } elsif (need_wire_pointer
($e)) {
520 pidl
"\tNDR_CHECK(ndr_push_ptr(ndr, $var_prefix$e->{NAME}));\n";
521 } elsif (need_alloc
($e)) {
522 # no scalar component
523 } elsif (my $switch = util
::has_property
($e, "switch_is")) {
524 ParseElementPushSwitch
($e, $var_prefix, $ndr_flags, $switch);
525 } elsif (defined $sub_size) {
526 pidl
"\tNDR_CHECK(ndr_push_subcontext_flags_fn(ndr, $sub_size, $cprefix$var_prefix$e->{NAME}, (ndr_push_flags_fn_t) ndr_push_$e->{TYPE}));\n";
528 pidl
"\tNDR_CHECK(ndr_push_$e->{TYPE}(ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}));\n";
534 #####################################################################
535 # print scalars in a structure element
536 sub ParseElementPrintScalar
($$)
539 my($var_prefix) = shift;
540 my $cprefix = c_push_prefix
($e);
542 if (util
::has_property
($e, "noprint")) {
546 if (my $value = util
::has_property
($e, "value")) {
547 pidl
"\tif (ndr->flags & LIBNDR_PRINT_SET_VALUES) {\n";
548 pidl
"\t\t$cprefix$var_prefix$e->{NAME} = $value;\n";
552 if (util
::is_fixed_array
($e)) {
553 ParseElementPrintBuffer
($e, $var_prefix);
554 } elsif ($e->{POINTERS
} || util
::array_size
($e)) {
555 pidl
"\tndr_print_ptr(ndr, \"$e->{NAME}\", $var_prefix$e->{NAME});\n";
556 pidl
"\tndr->depth++;\n";
557 ParseElementPrintBuffer
($e, $var_prefix);
558 pidl
"\tndr->depth--;\n";
559 } elsif (my $switch = util
::has_property
($e, "switch_is")) {
560 ParseElementPrintSwitch
($e, $var_prefix, $switch);
562 pidl
"\tndr_print_$e->{TYPE}(ndr, \"$e->{NAME}\", $cprefix$var_prefix$e->{NAME});\n";
566 #####################################################################
567 # parse scalars in a structure element - pull size
568 sub ParseElementPullSwitch
($$$$)
571 my($var_prefix) = shift;
572 my($ndr_flags) = shift;
574 my $switch_var = find_size_var
($e, $switch, $var_prefix);
576 my $cprefix = c_pull_prefix
($e);
578 my $utype = $typedefs{$e->{TYPE
}};
580 check_null_pointer
($switch_var);
582 if (!defined $utype ||
583 !util
::has_property
($utype, "nodiscriminant")) {
584 my $e2 = find_sibling
($e, $switch);
585 my $type_decl = util
::map_type
($e2->{TYPE
});
586 pidl
"\tif (($ndr_flags) & NDR_SCALARS) {\n";
587 if (util
::is_enum
($e2->{TYPE
})) {
588 $type_decl = util
::enum_type_decl
($e2);
589 } elsif (util
::is_bitmap
($e2->{TYPE
})) {
590 $type_decl = util
::bitmap_type_decl
($e2);
592 pidl
"\t\t$type_decl _level;\n";
593 pidl
"\t\tNDR_CHECK(ndr_pull_$e2->{TYPE}(ndr, NDR_SCALARS, &_level));\n";
594 if ($switch_var =~ /r->in/) {
595 pidl
"\t\tif (!(ndr->flags & LIBNDR_FLAG_REF_ALLOC) && _level != $switch_var) {\n";
597 pidl
"\t\tif (_level != $switch_var) {\n";
599 pidl
"\t\t\treturn ndr_pull_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value %u in $e->{NAME}\", _level);\n";
601 if ($switch_var =~ /r->/) {
602 pidl
"else { $switch_var = _level; }\n";
607 my $sub_size = util
::has_property
($e, "subcontext");
608 if (defined $sub_size) {
609 pidl
"\tif (($ndr_flags) & NDR_SCALARS) {\n";
610 pidl
"\t\tNDR_CHECK(ndr_pull_subcontext_union_fn(ndr, $sub_size, $switch_var, $cprefix$var_prefix$e->{NAME}, (ndr_pull_union_fn_t) ndr_pull_$e->{TYPE}));\n";
613 pidl
"\tNDR_CHECK(ndr_pull_$e->{TYPE}(ndr, $ndr_flags, $switch_var, $cprefix$var_prefix$e->{NAME}));\n";
619 #####################################################################
620 # push switch element
621 sub ParseElementPushSwitch
($$$$)
624 my($var_prefix) = shift;
625 my($ndr_flags) = shift;
627 my $switch_var = find_size_var
($e, $switch, $var_prefix);
628 my $cprefix = c_push_prefix
($e);
630 check_null_pointer
($switch_var);
632 my $utype = $typedefs{$e->{TYPE
}};
633 if (!defined $utype ||
634 !util
::has_property
($utype, "nodiscriminant")) {
635 my $e2 = find_sibling
($e, $switch);
636 pidl
"\tif (($ndr_flags) & NDR_SCALARS) {\n";
637 pidl
"\t\tNDR_CHECK(ndr_push_$e2->{TYPE}(ndr, NDR_SCALARS, $switch_var));\n";
641 my $sub_size = util
::has_property
($e, "subcontext");
642 if (defined $sub_size) {
643 pidl
"\tif(($ndr_flags) & NDR_SCALARS) {\n";
644 pidl
"\t\tNDR_CHECK(ndr_push_subcontext_union_fn(ndr, $sub_size, $switch_var, $cprefix$var_prefix$e->{NAME}, (ndr_push_union_fn_t) ndr_push_$e->{TYPE}));\n";
647 pidl
"\tNDR_CHECK(ndr_push_$e->{TYPE}(ndr, $ndr_flags, $switch_var, $cprefix$var_prefix$e->{NAME}));\n";
651 #####################################################################
652 # print scalars in a structure element
653 sub ParseElementPrintSwitch
($$$)
656 my($var_prefix) = shift;
658 my $switch_var = find_size_var
($e, $switch, $var_prefix);
659 my $cprefix = c_push_prefix
($e);
661 check_null_pointer_void
($switch_var);
663 pidl
"\tndr_print_$e->{TYPE}(ndr, \"$e->{NAME}\", $switch_var, $cprefix$var_prefix$e->{NAME});\n";
667 #####################################################################
668 # parse scalars in a structure element - pull size
669 sub ParseElementPullScalar
($$$)
672 my($var_prefix) = shift;
673 my($ndr_flags) = shift;
674 my $cprefix = c_pull_prefix
($e);
675 my $sub_size = util
::has_property
($e, "subcontext");
679 if (util
::is_inline_array
($e)) {
680 ParseArrayPull
($e, "r->", "NDR_SCALARS");
681 } elsif (need_wire_pointer
($e)) {
682 pidl
"\tNDR_CHECK(ndr_pull_ptr(ndr, &_ptr_$e->{NAME}));\n";
683 pidl
"\tif (_ptr_$e->{NAME}) {\n";
684 pidl
"\t\tNDR_ALLOC(ndr, $var_prefix$e->{NAME});\n";
685 if (util
::has_property
($e, "relative")) {
686 pidl
"\t\tNDR_CHECK(ndr_pull_relative1(ndr, $var_prefix$e->{NAME}, _ptr_$e->{NAME}));\n";
689 pidl
"\t\t$var_prefix$e->{NAME} = NULL;\n";
691 } elsif (need_alloc
($e)) {
692 # no scalar component
693 } elsif (my $switch = util
::has_property
($e, "switch_is")) {
694 ParseElementPullSwitch
($e, $var_prefix, $ndr_flags, $switch);
695 } elsif (defined $sub_size) {
696 pidl
"\tNDR_CHECK(ndr_pull_subcontext_flags_fn(ndr, $sub_size, $cprefix$var_prefix$e->{NAME}, (ndr_pull_flags_fn_t) ndr_pull_$e->{TYPE}));\n";
698 pidl
"\tNDR_CHECK(ndr_pull_$e->{TYPE}(ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}));\n";
700 if (my $range = util
::has_property
($e, "range")) {
701 my ($low, $high) = split(/ /, $range, 2);
702 pidl
"\tif ($var_prefix$e->{NAME} < $low || $var_prefix$e->{NAME} > $high) {\n";
703 pidl
"\t\treturn ndr_pull_error(ndr, NDR_ERR_RANGE, \"value out of range\");\n\t}\n";
709 #####################################################################
710 # parse buffers in a structure element
711 sub ParseElementPushBuffer
($$$)
714 my($var_prefix) = shift;
715 my($ndr_flags) = shift;
716 my $cprefix = c_push_prefix
($e);
717 my $sub_size = util
::has_property
($e, "subcontext");
719 if (is_pure_scalar
($e)) {
725 if (need_wire_pointer
($e)) {
726 pidl
"\tif ($var_prefix$e->{NAME}) {\n";
727 if (util
::has_property
($e, "relative")) {
728 pidl
"\t\tNDR_CHECK(ndr_push_relative2(ndr, $var_prefix$e->{NAME}));\n";
732 if (util
::is_inline_array
($e)) {
733 ParseArrayPush
($e, "r->", "NDR_BUFFERS");
734 } elsif (util
::array_size
($e)) {
735 ParseArrayPush
($e, "r->", "NDR_SCALARS|NDR_BUFFERS");
736 } elsif (my $switch = util
::has_property
($e, "switch_is")) {
737 if ($e->{POINTERS
}) {
738 ParseElementPushSwitch
($e, $var_prefix, "NDR_BUFFERS|NDR_SCALARS", $switch);
740 ParseElementPushSwitch
($e, $var_prefix, "NDR_BUFFERS", $switch);
742 } elsif (defined $sub_size) {
743 if ($e->{POINTERS
}) {
744 pidl
"\tNDR_CHECK(ndr_push_subcontext_flags_fn(ndr, $sub_size, $cprefix$var_prefix$e->{NAME}, (ndr_push_flags_fn_t) ndr_push_$e->{TYPE}));\n";
746 } elsif ($e->{POINTERS
}) {
747 pidl
"\t\tNDR_CHECK(ndr_push_$e->{TYPE}(ndr, NDR_SCALARS|NDR_BUFFERS, $cprefix$var_prefix$e->{NAME}));\n";
749 pidl
"\t\tNDR_CHECK(ndr_push_$e->{TYPE}(ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}));\n";
752 if (need_wire_pointer
($e)) {
759 #####################################################################
760 # print buffers in a structure element
761 sub ParseElementPrintBuffer
($$)
764 my($var_prefix) = shift;
765 my $cprefix = c_push_prefix
($e);
767 if (need_wire_pointer
($e)) {
768 pidl
"\tif ($var_prefix$e->{NAME}) {\n";
771 if (util
::array_size
($e)) {
772 ParseArrayPrint
($e, $var_prefix)
773 } elsif (my $switch = util
::has_property
($e, "switch_is")) {
774 ParseElementPrintSwitch
($e, $var_prefix, $switch);
776 pidl
"\t\tndr_print_$e->{TYPE}(ndr, \"$e->{NAME}\", $cprefix$var_prefix$e->{NAME});\n";
779 if (need_wire_pointer
($e)) {
785 #####################################################################
786 # parse buffers in a structure element - pull side
787 sub ParseElementPullBuffer
($$$)
790 my($var_prefix) = shift;
791 my($ndr_flags) = shift;
792 my $cprefix = c_pull_prefix
($e);
793 my $sub_size = util
::has_property
($e, "subcontext");
795 if (is_pure_scalar
($e)) {
801 if (need_wire_pointer
($e)) {
802 pidl
"\tif ($var_prefix$e->{NAME}) {\n";
803 if (util
::has_property
($e, "relative")) {
804 pidl
"\t\tstruct ndr_pull_save _relative_save;\n";
805 pidl
"\t\tndr_pull_save(ndr, &_relative_save);\n";
806 pidl
"\t\tNDR_CHECK(ndr_pull_relative2(ndr, $var_prefix$e->{NAME}));\n";
810 if (util
::is_inline_array
($e)) {
811 ParseArrayPull
($e, "r->", "NDR_BUFFERS");
812 } elsif (util
::array_size
($e)) {
813 ParseArrayPull
($e, "r->", "NDR_SCALARS|NDR_BUFFERS");
814 } elsif (my $switch = util
::has_property
($e, "switch_is")) {
815 if ($e->{POINTERS
}) {
816 ParseElementPullSwitch
($e, $var_prefix, "NDR_SCALARS|NDR_BUFFERS", $switch);
818 ParseElementPullSwitch
($e, $var_prefix, "NDR_BUFFERS", $switch);
820 } elsif (defined $sub_size) {
821 if ($e->{POINTERS
}) {
822 pidl
"\tNDR_CHECK(ndr_pull_subcontext_flags_fn(ndr, $sub_size, $cprefix$var_prefix$e->{NAME}, (ndr_pull_flags_fn_t) ndr_pull_$e->{TYPE}));\n";
824 } elsif ($e->{POINTERS
}) {
825 pidl
"\t\tNDR_CHECK(ndr_pull_$e->{TYPE}(ndr, NDR_SCALARS|NDR_BUFFERS, $cprefix$var_prefix$e->{NAME}));\n";
827 pidl
"\t\tNDR_CHECK(ndr_pull_$e->{TYPE}(ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}));\n";
830 if (need_wire_pointer
($e)) {
831 if (util
::has_property
($e, "relative")) {
832 pidl
"\t\tndr_pull_restore(ndr, &_relative_save);\n";
840 #####################################################################
842 sub ParseStructPush
($)
846 if (! defined $struct->{ELEMENTS
}) {
850 start_flags
($struct);
852 # see if the structure contains a conformant array. If it
853 # does, then it must be the last element of the structure, and
854 # we need to push the conformant length early, as it fits on
855 # the wire before the structure (and even before the structure
857 my $e = $struct->{ELEMENTS
}[-1];
858 if (defined $e->{ARRAY_LEN
} && $e->{ARRAY_LEN
} eq "*") {
859 my $size = find_size_var
($e, util
::array_size
($e), "r->");
860 $e->{CONFORMANT_SIZE
} = $size;
861 check_null_pointer
($size);
862 pidl
"\tNDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, $size));\n";
865 if (defined $e->{TYPE
} && $e->{TYPE
} eq "string"
866 && util
::property_matches
($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
867 pidl
"\tNDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, ndr_string_array_size(ndr, r->$e->{NAME})));\n";
870 pidl
"\tif (!(ndr_flags & NDR_SCALARS)) goto buffers;\n";
872 pidl
"\tNDR_CHECK(ndr_push_struct_start(ndr));\n";
874 my $align = struct_alignment
($struct);
875 pidl
"\tNDR_CHECK(ndr_push_align(ndr, $align));\n";
877 foreach my $e (@
{$struct->{ELEMENTS
}}) {
878 ParseElementPushScalar
($e, "r->", "NDR_SCALARS");
882 pidl
"\tif (!(ndr_flags & NDR_BUFFERS)) goto done;\n";
883 foreach my $e (@
{$struct->{ELEMENTS
}}) {
884 ParseElementPushBuffer
($e, "r->", "NDR_BUFFERS");
887 pidl
"\tndr_push_struct_end(ndr);\n";
895 #####################################################################
896 # generate a push function for an enum
900 my($type_fn) = util
::enum_type_fn
($enum);
904 pidl
"\tNDR_CHECK(ndr_push_$type_fn(ndr, NDR_SCALARS, r));\n";
909 #####################################################################
910 # generate a pull function for an enum
914 my($type_fn) = util
::enum_type_fn
($enum);
915 my($type_v_decl) = util
::map_type
(util
::enum_type_fn
($enum));
917 pidl
"\t$type_v_decl v;\n";
919 pidl
"\tNDR_CHECK(ndr_pull_$type_fn(ndr, NDR_SCALARS, &v));\n";
925 #####################################################################
926 # generate a print function for an enum
927 sub ParseEnumPrint
($)
931 pidl
"\tconst char *val = NULL;\n\n";
935 pidl
"\tswitch (r) {\n";
936 my $els = \@
{$enum->{ELEMENTS
}};
937 foreach my $i (0 .. $#{$els}) {
940 if ($e =~ /^(.*)=/) {
943 pidl
"\t\tcase $e: val = \"$e\"; break;\n";
946 pidl
"\t}\n\n\tndr_print_enum(ndr, name, \"$enum->{TYPE}\", val, r);\n";
952 #####################################################################
953 # generate a push function for a bitmap
954 sub ParseBitmapPush
($)
957 my($type_fn) = util
::bitmap_type_fn
($bitmap);
959 start_flags
($bitmap);
961 pidl
"\tNDR_CHECK(ndr_push_$type_fn(ndr, NDR_SCALARS, r));\n";
966 #####################################################################
967 # generate a pull function for an bitmap
968 sub ParseBitmapPull
($)
971 my($type_fn) = util
::bitmap_type_fn
($bitmap);
972 my($type_decl) = util
::bitmap_type_decl
($bitmap);
974 pidl
"\t$type_decl v;\n";
975 start_flags
($bitmap);
976 pidl
"\tNDR_CHECK(ndr_pull_$type_fn(ndr, NDR_SCALARS, &v));\n";
982 #####################################################################
983 # generate a print function for an bitmap
984 sub ParseBitmapPrintElement
($$)
988 my($type_decl) = util
::bitmap_type_decl
($bitmap);
989 my($type_fn) = util
::bitmap_type_fn
($bitmap);
990 my($name) = $bitmap->{PARENT
}->{NAME
};
993 if ($e =~ /^(\w+) .*$/) {
996 die "Bitmap: \"$name\" invalid Flag: \"$e\"";
999 pidl
"\tndr_print_bitmap_flag(ndr, sizeof($type_decl), \"$flag\", $flag, r);\n";
1002 #####################################################################
1003 # generate a print function for an bitmap
1004 sub ParseBitmapPrint
($)
1006 my($bitmap) = shift;
1007 my($type_decl) = util
::bitmap_type_decl
($bitmap);
1008 my($type_fn) = util
::bitmap_type_fn
($bitmap);
1010 start_flags
($bitmap);
1012 pidl
"\tndr_print_$type_fn(ndr, name, r);\n";
1014 pidl
"\tndr->depth++;\n";
1015 foreach my $e (@
{$bitmap->{ELEMENTS
}}) {
1016 ParseBitmapPrintElement
($e, $bitmap);
1018 pidl
"\tndr->depth--;\n";
1023 #####################################################################
1024 # generate a struct print function
1025 sub ParseStructPrint
($)
1027 my($struct) = shift;
1029 if (! defined $struct->{ELEMENTS
}) {
1033 start_flags
($struct);
1035 pidl
"\tndr->depth++;\n";
1036 foreach my $e (@
{$struct->{ELEMENTS
}}) {
1037 ParseElementPrintScalar
($e, "r->");
1039 pidl
"\tndr->depth--;\n";
1044 #####################################################################
1045 # parse a struct - pull side
1046 sub ParseStructPull
($)
1048 my($struct) = shift;
1051 if (! defined $struct->{ELEMENTS
}) {
1055 # see if the structure contains a conformant array. If it
1056 # does, then it must be the last element of the structure, and
1057 # we need to pull the conformant length early, as it fits on
1058 # the wire before the structure (and even before the structure
1060 my $e = $struct->{ELEMENTS
}[-1];
1061 if (defined $e->{ARRAY_LEN
} && $e->{ARRAY_LEN
} eq "*") {
1065 if (defined $e->{TYPE
} && $e->{TYPE
} eq "string"
1066 && util
::property_matches
($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
1070 if (defined $conform_e) {
1072 pidl
"\tuint32_t _conformant_size;\n";
1073 $conform_e->{CONFORMANT_SIZE
} = "_conformant_size";
1076 # declare any internal pointers we need
1077 foreach my $e (@
{$struct->{ELEMENTS
}}) {
1078 if (need_wire_pointer
($e)) {
1079 pidl
"\tuint32_t _ptr_$e->{NAME};\n";
1083 start_flags
($struct);
1085 pidl
"\tif (!(ndr_flags & NDR_SCALARS)) goto buffers;\n";
1087 pidl
"\tNDR_CHECK(ndr_pull_struct_start(ndr));\n";
1089 if (defined $conform_e) {
1090 pidl
"\tNDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &$conform_e->{CONFORMANT_SIZE}));\n";
1093 my $align = struct_alignment
($struct);
1094 pidl
"\tNDR_CHECK(ndr_pull_align(ndr, $align));\n";
1096 foreach my $e (@
{$struct->{ELEMENTS
}}) {
1097 ParseElementPullScalar
($e, "r->", "NDR_SCALARS");
1101 pidl
"\tif (!(ndr_flags & NDR_BUFFERS)) goto done;\n";
1102 foreach my $e (@
{$struct->{ELEMENTS
}}) {
1103 ParseElementPullBuffer
($e, "r->", "NDR_BUFFERS");
1106 foreach my $e (@
{$struct->{ELEMENTS
}}) {
1107 CheckArraySizes
($e, "r->");
1110 pidl
"\tndr_pull_struct_end(ndr);\n";
1117 #####################################################################
1118 # calculate size of ndr struct
1119 sub ParseStructNdrSize
($)
1122 my $static = fn_prefix
($t);
1125 pidl
"size_t ndr_size_$t->{NAME}(const struct $t->{NAME} *r, int flags)\n";
1127 if (my $flags = util
::has_property
($t, "flag")) {
1128 pidl
"\tflags |= $flags;\n";
1130 pidl
"\treturn ndr_size_struct(r, flags, (ndr_push_flags_fn_t)ndr_push_$t->{NAME});\n";
1134 #####################################################################
1135 # calculate size of ndr struct
1136 sub ParseUnionNdrSize
($)
1139 my $static = fn_prefix
($t);
1142 pidl
"size_t ndr_size_$t->{NAME}(const union $t->{NAME} *r, int level, int flags)\n";
1144 if (my $flags = util
::has_property
($t, "flag")) {
1145 pidl
"\tflags |= $flags;\n";
1147 pidl
"\treturn ndr_size_union(r, flags, level, (ndr_push_union_fn_t)ndr_push_$t->{NAME});\n";
1151 #####################################################################
1152 # parse a union - push side
1153 sub ParseUnionPush
($)
1156 my $have_default = 0;
1160 pidl
"\tif (!(ndr_flags & NDR_SCALARS)) goto buffers;\n";
1162 pidl
"\tNDR_CHECK(ndr_push_struct_start(ndr));\n";
1164 # my $align = union_alignment($e);
1165 # pidl "\tNDR_CHECK(ndr_push_align(ndr, $align));\n";
1167 pidl
"\tswitch (level) {\n";
1168 foreach my $el (@
{$e->{ELEMENTS
}}) {
1169 if (util
::has_property
($el, "default")) {
1170 pidl
"\tdefault:\n";
1173 pidl
"\tcase $el->{PROPERTIES}->{case}:\n";
1175 if ($el->{TYPE
} ne "EMPTY") {
1176 ParseElementPushScalar
($el, "r->", "NDR_SCALARS");
1178 pidl
"\tbreak;\n\n";
1180 if (! $have_default) {
1181 pidl
"\tdefault:\n";
1182 pidl
"\t\treturn ndr_push_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);\n";
1186 pidl
"\tif (!(ndr_flags & NDR_BUFFERS)) goto done;\n";
1187 pidl
"\tswitch (level) {\n";
1188 foreach my $el (@
{$e->{ELEMENTS
}}) {
1189 if (util
::has_property
($el, "default")) {
1190 pidl
"\tdefault:\n";
1192 pidl
"\tcase $el->{PROPERTIES}->{case}:\n";
1194 if ($el->{TYPE
} ne "EMPTY") {
1195 ParseElementPushBuffer
($el, "r->", "NDR_BUFFERS");
1197 pidl
"\tbreak;\n\n";
1199 if (! $have_default) {
1200 pidl
"\tdefault:\n";
1201 pidl
"\t\treturn ndr_push_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);\n";
1204 pidl
"\tndr_push_struct_end(ndr);\n";
1209 #####################################################################
1211 sub ParseUnionPrint
($)
1214 my $have_default = 0;
1218 pidl
"\tswitch (level) {\n";
1219 foreach my $el (@
{$e->{ELEMENTS
}}) {
1220 if (util
::has_property
($el, "default")) {
1222 pidl
"\tdefault:\n";
1224 pidl
"\tcase $el->{PROPERTIES}->{case}:\n";
1226 if ($el->{TYPE
} ne "EMPTY") {
1227 ParseElementPrintScalar
($el, "r->");
1229 pidl
"\tbreak;\n\n";
1231 if (! $have_default) {
1232 pidl
"\tdefault:\n\t\tndr_print_bad_level(ndr, name, level);\n";
1239 #####################################################################
1240 # parse a union - pull side
1241 sub ParseUnionPull
($)
1244 my $have_default = 0;
1248 pidl
"\tif (!(ndr_flags & NDR_SCALARS)) goto buffers;\n";
1250 pidl
"\tNDR_CHECK(ndr_pull_struct_start(ndr));\n";
1252 # my $align = union_alignment($e);
1253 # pidl "\tNDR_CHECK(ndr_pull_align(ndr, $align));\n";
1255 pidl
"\tswitch (level) {\n";
1256 foreach my $el (@
{$e->{ELEMENTS
}}) {
1257 if (util
::has_property
($el, "default")) {
1258 pidl
"\tdefault: {\n";
1261 pidl
"\tcase $el->{PROPERTIES}->{case}: {\n";
1263 if ($el->{TYPE
} ne "EMPTY") {
1264 if ($el->{POINTERS
}) {
1265 pidl
"\t\tuint32_t _ptr_$el->{NAME};\n";
1267 ParseElementPullScalar
($el, "r->", "NDR_SCALARS");
1269 pidl
"\tbreak; }\n\n";
1271 if (! $have_default) {
1272 pidl
"\tdefault:\n";
1273 pidl
"\t\treturn ndr_pull_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);\n";
1277 pidl
"\tif (!(ndr_flags & NDR_BUFFERS)) goto done;\n";
1278 pidl
"\tswitch (level) {\n";
1279 foreach my $el (@
{$e->{ELEMENTS
}}) {
1280 if (util
::has_property
($el, "default")) {
1281 pidl
"\tdefault:\n";
1283 pidl
"\tcase $el->{PROPERTIES}->{case}:\n";
1285 if ($el->{TYPE
} ne "EMPTY") {
1286 ParseElementPullBuffer
($el, "r->", "NDR_BUFFERS");
1288 pidl
"\tbreak;\n\n";
1290 if (! $have_default) {
1291 pidl
"\tdefault:\n";
1292 pidl
"\t\treturn ndr_pull_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);\n";
1295 pidl
"\tndr_pull_struct_end(ndr);\n";
1301 #####################################################################
1303 sub ParseTypePush
($)
1307 ($data->{TYPE
} eq "STRUCT") &&
1308 ParseStructPush
($data);
1309 ($data->{TYPE
} eq "UNION") &&
1310 ParseUnionPush
($data);
1311 ($data->{TYPE
} eq "ENUM") &&
1312 ParseEnumPush
($data);
1313 ($data->{TYPE
} eq "BITMAP") &&
1314 ParseBitmapPush
($data);
1317 #####################################################################
1318 # generate a print function for a type
1319 sub ParseTypePrint
($)
1323 ($data->{TYPE
} eq "STRUCT") &&
1324 ParseStructPrint
($data);
1325 ($data->{TYPE
} eq "UNION") &&
1326 ParseUnionPrint
($data);
1327 ($data->{TYPE
} eq "ENUM") &&
1328 ParseEnumPrint
($data);
1329 ($data->{TYPE
} eq "BITMAP") &&
1330 ParseBitmapPrint
($data);
1333 #####################################################################
1335 sub ParseTypePull
($)
1339 ($data->{TYPE
} eq "STRUCT") &&
1340 ParseStructPull
($data);
1341 ($data->{TYPE
} eq "UNION") &&
1342 ParseUnionPull
($data);
1343 ($data->{TYPE
} eq "ENUM") &&
1344 ParseEnumPull
($data);
1345 ($data->{TYPE
} eq "BITMAP") &&
1346 ParseBitmapPull
($data);
1349 #####################################################################
1350 # parse a typedef - push side
1351 sub ParseTypedefPush
($)
1354 my $static = fn_prefix
($e);
1356 if (! needed
::is_needed
("push_$e->{NAME}")) {
1357 # print "push_$e->{NAME} not needed\n";
1361 if (defined($e->{PROPERTIES
}) && !defined($e->{DATA
}->{PROPERTIES
})) {
1362 $e->{DATA
}->{PROPERTIES
} = $e->{PROPERTIES
};
1365 if ($e->{DATA
}->{TYPE
} eq "STRUCT") {
1366 pidl
$static . "NTSTATUS ndr_push_$e->{NAME}(struct ndr_push *ndr, int ndr_flags, struct $e->{NAME} *r)";
1368 ParseTypePush
($e->{DATA
});
1369 pidl
"\treturn NT_STATUS_OK;\n";
1373 if ($e->{DATA
}->{TYPE
} eq "UNION") {
1374 pidl
$static . "NTSTATUS ndr_push_$e->{NAME}(struct ndr_push *ndr, int ndr_flags, int level, union $e->{NAME} *r)";
1376 ParseTypePush
($e->{DATA
});
1377 pidl
"\treturn NT_STATUS_OK;\n";
1381 if ($e->{DATA
}->{TYPE
} eq "ENUM") {
1382 pidl
$static . "NTSTATUS ndr_push_$e->{NAME}(struct ndr_push *ndr, int ndr_flags, enum $e->{NAME} r)";
1384 ParseTypePush
($e->{DATA
});
1385 pidl
"\treturn NT_STATUS_OK;\n";
1389 if ($e->{DATA
}->{TYPE
} eq "BITMAP") {
1390 my $type_decl = util
::bitmap_type_decl
($e->{DATA
});
1391 pidl
$static . "NTSTATUS ndr_push_$e->{NAME}(struct ndr_push *ndr, int ndr_flags, $type_decl r)";
1393 ParseTypePush
($e->{DATA
});
1394 pidl
"\treturn NT_STATUS_OK;\n";
1400 #####################################################################
1401 # parse a typedef - pull side
1402 sub ParseTypedefPull
($)
1405 my $static = fn_prefix
($e);
1407 if (defined($e->{PROPERTIES
}) && !defined($e->{DATA
}->{PROPERTIES
})) {
1408 $e->{DATA
}->{PROPERTIES
} = $e->{PROPERTIES
};
1411 if (! needed
::is_needed
("pull_$e->{NAME}")) {
1412 # print "pull_$e->{NAME} not needed\n";
1416 if ($e->{DATA
}->{TYPE
} eq "STRUCT") {
1417 pidl
$static . "NTSTATUS ndr_pull_$e->{NAME}(struct ndr_pull *ndr, int ndr_flags, struct $e->{NAME} *r)";
1419 ParseTypePull
($e->{DATA
});
1420 pidl
"\treturn NT_STATUS_OK;\n";
1424 if ($e->{DATA
}->{TYPE
} eq "UNION") {
1425 pidl
$static . "NTSTATUS ndr_pull_$e->{NAME}(struct ndr_pull *ndr, int ndr_flags, int level, union $e->{NAME} *r)";
1427 ParseTypePull
($e->{DATA
});
1428 pidl
"\treturn NT_STATUS_OK;\n";
1432 if ($e->{DATA
}->{TYPE
} eq "ENUM") {
1433 pidl
$static . "NTSTATUS ndr_pull_$e->{NAME}(struct ndr_pull *ndr, int ndr_flags, enum $e->{NAME} *r)";
1435 ParseTypePull
($e->{DATA
});
1436 pidl
"\treturn NT_STATUS_OK;\n";
1440 if ($e->{DATA
}->{TYPE
} eq "BITMAP") {
1441 my $type_decl = util
::bitmap_type_decl
($e->{DATA
});
1442 pidl
$static . "NTSTATUS ndr_pull_$e->{NAME}(struct ndr_pull *ndr, int ndr_flags, $type_decl *r)";
1444 ParseTypePull
($e->{DATA
});
1445 pidl
"\treturn NT_STATUS_OK;\n";
1451 #####################################################################
1452 # parse a typedef - print side
1453 sub ParseTypedefPrint
($)
1457 if (defined($e->{PROPERTIES
}) && !defined($e->{DATA
}->{PROPERTIES
})) {
1458 $e->{DATA
}->{PROPERTIES
} = $e->{PROPERTIES
};
1461 if ($e->{DATA
}->{TYPE
} eq "STRUCT") {
1462 pidl
"void ndr_print_$e->{NAME}(struct ndr_print *ndr, const char *name, struct $e->{NAME} *r)";
1464 pidl
"\tndr_print_struct(ndr, name, \"$e->{NAME}\");\n";
1465 ParseTypePrint
($e->{DATA
});
1469 if ($e->{DATA
}->{TYPE
} eq "UNION") {
1470 pidl
"void ndr_print_$e->{NAME}(struct ndr_print *ndr, const char *name, int level, union $e->{NAME} *r)";
1472 pidl
"\tndr_print_union(ndr, name, level, \"$e->{NAME}\");\n";
1473 ParseTypePrint
($e->{DATA
});
1477 if ($e->{DATA
}->{TYPE
} eq "ENUM") {
1478 pidl
"void ndr_print_$e->{NAME}(struct ndr_print *ndr, const char *name, enum $e->{NAME} r)";
1480 ParseTypePrint
($e->{DATA
});
1484 if ($e->{DATA
}->{TYPE
} eq "BITMAP") {
1485 my $type_decl = util
::bitmap_type_decl
($e->{DATA
});
1486 pidl
"void ndr_print_$e->{NAME}(struct ndr_print *ndr, const char *name, $type_decl r)";
1488 ParseTypePrint
($e->{DATA
});
1493 #####################################################################
1494 ## calculate the size of a structure
1495 sub ParseTypedefNdrSize
($)
1498 if (! needed
::is_needed
("ndr_size_$t->{NAME}")) {
1502 ($t->{DATA
}->{TYPE
} eq "STRUCT") &&
1503 ParseStructNdrSize
($t);
1505 ($t->{DATA
}->{TYPE
} eq "UNION") &&
1506 ParseUnionNdrSize
($t);
1509 #####################################################################
1510 # parse a function - print side
1511 sub ParseFunctionPrint
($)
1515 pidl
"void ndr_print_$fn->{NAME}(struct ndr_print *ndr, const char *name, int flags, struct $fn->{NAME} *r)";
1517 pidl
"\tndr_print_struct(ndr, name, \"$fn->{NAME}\");\n";
1518 pidl
"\tndr->depth++;\n";
1520 pidl
"\tif (flags & NDR_SET_VALUES) {\n";
1521 pidl
"\t\tndr->flags |= LIBNDR_PRINT_SET_VALUES;\n";
1524 pidl
"\tif (flags & NDR_IN) {\n";
1525 pidl
"\t\tndr_print_struct(ndr, \"in\", \"$fn->{NAME}\");\n";
1526 pidl
"\tndr->depth++;\n";
1528 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1529 if (util
::has_property
($e, "in")) {
1530 ParseElementPrintScalar
($e, "r->in.");
1533 pidl
"\tndr->depth--;\n";
1536 pidl
"\tif (flags & NDR_OUT) {\n";
1537 pidl
"\t\tndr_print_struct(ndr, \"out\", \"$fn->{NAME}\");\n";
1538 pidl
"\tndr->depth++;\n";
1539 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1540 if (util
::has_property
($e, "out")) {
1541 ParseElementPrintScalar
($e, "r->out.");
1544 if ($fn->{RETURN_TYPE
} && $fn->{RETURN_TYPE
} ne "void") {
1546 $cprefix = "" if (is_scalar_type
($fn->{RETURN_TYPE
})) ; # FIXME: Should really use util::c_push_prefix here
1547 pidl
"\tndr_print_$fn->{RETURN_TYPE}(ndr, \"result\", $cprefix"."r->out.result);\n";
1549 pidl
"\tndr->depth--;\n";
1552 pidl
"\tndr->depth--;\n";
1557 #####################################################################
1558 # parse a function element
1559 sub ParseFunctionElementPush
($$)
1564 if (util
::array_size
($e)) {
1565 if (need_wire_pointer
($e)) {
1566 pidl
"\tNDR_CHECK(ndr_push_ptr(ndr, r->$inout.$e->{NAME}));\n";
1567 pidl
"\tif (r->$inout.$e->{NAME}) {\n";
1568 ParseArrayPush
($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1571 ParseArrayPush
($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1574 ParseElementPushScalar
($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1576 if ($e->{POINTERS
}) {
1577 ParseElementPushBuffer
($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1582 #####################################################################
1584 sub ParseFunctionPush
($)
1587 my $static = fn_prefix
($fn);
1589 pidl
$static . "NTSTATUS ndr_push_$fn->{NAME}(struct ndr_push *ndr, int flags, struct $fn->{NAME} *r)\n{\n";
1591 pidl
"\n\tif (!(flags & NDR_IN)) goto ndr_out;\n\n";
1593 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1594 if (util
::has_property
($e, "in")) {
1595 ParseFunctionElementPush
($e, "in");
1599 pidl
"\nndr_out:\n";
1600 pidl
"\tif (!(flags & NDR_OUT)) goto done;\n\n";
1602 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1603 if (util
::has_property
($e, "out")) {
1604 ParseFunctionElementPush
($e, "out");
1608 if ($fn->{RETURN_TYPE
} && $fn->{RETURN_TYPE
} ne "void") {
1609 pidl
"\tNDR_CHECK(ndr_push_$fn->{RETURN_TYPE}(ndr, NDR_SCALARS, r->out.result));\n";
1613 pidl
"\n\treturn NT_STATUS_OK;\n}\n\n";
1616 #####################################################################
1617 # parse a function element
1618 sub ParseFunctionElementPull
($$)
1623 if (util
::array_size
($e)) {
1624 if (need_wire_pointer
($e)) {
1625 pidl
"\tNDR_CHECK(ndr_pull_ptr(ndr, &_ptr_$e->{NAME}));\n";
1626 pidl
"\tr->$inout.$e->{NAME} = NULL;\n";
1627 pidl
"\tif (_ptr_$e->{NAME}) {\n";
1628 } elsif ($inout eq "out" && util
::has_property
($e, "ref")) {
1629 pidl
"\tif (r->$inout.$e->{NAME}) {\n";
1633 ParseArrayPull
($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1636 if ($inout eq "out" && util
::has_property
($e, "ref")) {
1637 pidl
"\tif (ndr->flags & LIBNDR_FLAG_REF_ALLOC) {\n";
1638 pidl
"\tNDR_ALLOC(ndr, r->out.$e->{NAME});\n";
1641 if ($inout eq "in" && util
::has_property
($e, "ref")) {
1642 pidl
"\tNDR_ALLOC(ndr, r->in.$e->{NAME});\n";
1645 ParseElementPullScalar
($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1646 if ($e->{POINTERS
}) {
1647 ParseElementPullBuffer
($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1653 ############################################################
1654 # allocate ref variables
1655 sub AllocateRefVars
($)
1658 my $asize = util
::array_size
($e);
1660 # note that if the variable is also an "in"
1661 # variable then we copy the initial value from
1664 if (!defined $asize) {
1665 # its a simple variable
1666 pidl
"\tNDR_ALLOC(ndr, r->out.$e->{NAME});\n";
1667 if (util
::has_property
($e, "in")) {
1668 pidl
"\t*r->out.$e->{NAME} = *r->in.$e->{NAME};\n";
1670 pidl
"\tZERO_STRUCTP(r->out.$e->{NAME});\n";
1676 my $size = find_size_var
($e, $asize, "r->out.");
1677 check_null_pointer
($size);
1678 pidl
"\tNDR_ALLOC_N(ndr, r->out.$e->{NAME}, $size);\n";
1679 if (util
::has_property
($e, "in")) {
1680 pidl
"\tmemcpy(r->out.$e->{NAME},r->in.$e->{NAME},$size * sizeof(*r->in.$e->{NAME}));\n";
1682 pidl
"\tmemset(r->out.$e->{NAME}, 0, $size * sizeof(*r->out.$e->{NAME}));\n";
1687 #####################################################################
1689 sub ParseFunctionPull
($)
1692 my $static = fn_prefix
($fn);
1694 # pull function args
1695 pidl
$static . "NTSTATUS ndr_pull_$fn->{NAME}(struct ndr_pull *ndr, int flags, struct $fn->{NAME} *r)\n{\n";
1697 # declare any internal pointers we need
1698 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1699 if (need_wire_pointer
($e)) {
1700 pidl
"\tuint32_t _ptr_$e->{NAME};\n";
1704 pidl
"\n\tif (!(flags & NDR_IN)) goto ndr_out;\n\n";
1706 # auto-init the out section of a structure. I originally argued that
1707 # this was a bad idea as it hides bugs, but coping correctly
1708 # with initialisation and not wiping ref vars is turning
1709 # out to be too tricky (tridge)
1710 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1711 if (util
::has_property
($e, "out")) {
1712 pidl
"\tZERO_STRUCT(r->out);\n\n";
1717 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1718 if (util
::has_property
($e, "in")) {
1719 ParseFunctionElementPull
($e, "in");
1721 # we need to allocate any reference output variables, so that
1722 # a dcerpc backend can be sure they are non-null
1723 if (util
::has_property
($e, "out") && util
::has_property
($e, "ref")) {
1724 AllocateRefVars
($e);
1728 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1729 if (util
::has_property
($e, "in")) {
1730 CheckArraySizes
($e, "r->in.");
1734 pidl
"\nndr_out:\n";
1735 pidl
"\tif (!(flags & NDR_OUT)) goto done;\n\n";
1737 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1738 if (util
::has_property
($e, "out")) {
1739 ParseFunctionElementPull
($e, "out");
1743 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1744 if (util
::has_property
($e, "out")) {
1745 CheckArraySizes
($e, "r->out.");
1749 if ($fn->{RETURN_TYPE
} && $fn->{RETURN_TYPE
} ne "void") {
1750 pidl
"\tNDR_CHECK(ndr_pull_$fn->{RETURN_TYPE}(ndr, NDR_SCALARS, &r->out.result));\n";
1754 pidl
"\n\treturn NT_STATUS_OK;\n}\n\n";
1757 #####################################################################
1758 # produce a function call table
1759 sub FunctionTable
($)
1761 my($interface) = shift;
1762 my($data) = $interface->{INHERITED_DATA
};
1764 my $uname = uc $interface->{NAME
};
1766 foreach my $d (@
{$data}) {
1767 if ($d->{TYPE
} eq "FUNCTION") { $count++; }
1770 return if ($count == 0);
1772 pidl
"static const struct dcerpc_interface_call $interface->{NAME}\_calls[] = {\n";
1773 foreach my $d (@
{$data}) {
1774 if ($d->{TYPE
} eq "FUNCTION") {
1776 pidl
"\t\t\"$d->{NAME}\",\n";
1777 pidl
"\t\tsizeof(struct $d->{NAME}),\n";
1778 pidl
"\t\t(ndr_push_flags_fn_t) ndr_push_$d->{NAME},\n";
1779 pidl
"\t\t(ndr_pull_flags_fn_t) ndr_pull_$d->{NAME},\n";
1780 pidl
"\t\t(ndr_print_function_t) ndr_print_$d->{NAME}\n";
1784 pidl
"\t{ NULL, 0, NULL, NULL, NULL }\n};\n\n";
1786 # If no endpoint is set, default to the interface name as a named pipe
1787 if (! defined $interface->{PROPERTIES
}->{endpoint
}) {
1788 $interface->{PROPERTIES
}->{endpoint
} = "\"ncacn_np:[\\\\pipe\\\\" . $interface->{NAME
} . "]\"";
1791 my @e = split / /, $interface->{PROPERTIES
}->{endpoint
};
1792 my $endpoint_count = $#e + 1;
1794 pidl
"static const char * const $interface->{NAME}\_endpoint_strings[] = {\n";
1795 foreach my $ep (@e) {
1800 pidl
"static const struct dcerpc_endpoint_list $interface->{NAME}\_endpoints = {\n";
1801 pidl
"\t$endpoint_count, $interface->{NAME}\_endpoint_strings\n";
1804 pidl
"\nconst struct dcerpc_interface_table dcerpc_table_$interface->{NAME} = {\n";
1805 pidl
"\t\"$interface->{NAME}\",\n";
1806 pidl
"\tDCERPC_$uname\_UUID,\n";
1807 pidl
"\tDCERPC_$uname\_VERSION,\n";
1808 pidl
"\tDCERPC_$uname\_HELPSTRING,\n";
1810 pidl
"\t$interface->{NAME}\_calls,\n";
1811 pidl
"\t&$interface->{NAME}\_endpoints\n";
1814 pidl
"static NTSTATUS dcerpc_ndr_$interface->{NAME}_init(void)\n";
1816 pidl
"\treturn librpc_register_interface(&dcerpc_table_$interface->{NAME});\n";
1820 #####################################################################
1821 # parse the interface definitions
1822 sub ParseInterface
($)
1824 my($interface) = shift;
1825 my($data) = $interface->{DATA
};
1827 foreach my $d (@
{$data}) {
1828 if ($d->{TYPE
} eq "DECLARE") {
1829 $typedefs{$d->{NAME
}} = $d;
1831 if ($d->{TYPE
} eq "TYPEDEF") {
1832 $typedefs{$d->{NAME
}} = $d;
1837 foreach my $d (@
{$data}) {
1838 ($d->{TYPE
} eq "TYPEDEF") &&
1839 ParseTypedefPush
($d);
1840 ($d->{TYPE
} eq "FUNCTION") &&
1841 ParseFunctionPush
($d);
1845 foreach my $d (@
{$data}) {
1846 ($d->{TYPE
} eq "TYPEDEF") &&
1847 ParseTypedefPull
($d);
1848 ($d->{TYPE
} eq "FUNCTION") &&
1849 ParseFunctionPull
($d);
1853 foreach my $d (@
{$data}) {
1854 if ($d->{TYPE
} eq "TYPEDEF" &&
1855 !util
::has_property
($d, "noprint")) {
1856 ParseTypedefPrint
($d);
1858 if ($d->{TYPE
} eq "FUNCTION" &&
1859 !util
::has_property
($d, "noprint")) {
1860 ParseFunctionPrint
($d);
1865 foreach my $d (@
{$data}) {
1866 ($d->{TYPE
} eq "TYPEDEF") &&
1867 ParseTypedefNdrSize
($d);
1870 FunctionTable
($interface);
1873 sub RegistrationFunction
($$)
1876 my $filename = shift;
1878 $filename =~ /.*\/ndr_
(.*).c
/;
1880 pidl
"NTSTATUS dcerpc_$basename\_init(void)\n";
1882 pidl
"\tNTSTATUS status = NT_STATUS_OK;\n";
1883 foreach my $interface (@
{$idl}) {
1884 next if $interface->{TYPE
} ne "INTERFACE";
1886 my $data = $interface->{INHERITED_DATA
};
1888 foreach my $d (@
{$data}) {
1889 if ($d->{TYPE
} eq "FUNCTION") { $count++; }
1892 next if ($count == 0);
1894 pidl
"\tstatus = dcerpc_ndr_$interface->{NAME}_init();\n";
1895 pidl
"\tif (NT_STATUS_IS_ERR(status)) {\n";
1896 pidl
"\t\treturn status;\n";
1899 pidl
"\treturn status;\n";
1903 #####################################################################
1904 # parse a parsed IDL structure back into an IDL file
1908 my($filename) = shift;
1909 my $h_filename = $filename;
1912 if ($h_filename =~ /(.*)\.c/) {
1913 $h_filename = "$1.h";
1916 pidl
"/* parser auto-generated by pidl */\n\n";
1917 pidl
"#include \"includes.h\"\n";
1918 pidl
"#include \"$h_filename\"\n\n";
1920 foreach my $x (@
{$idl}) {
1921 if ($x->{TYPE
} eq "INTERFACE") {
1922 needed
::BuildNeeded
($x);
1927 RegistrationFunction
($idl, $filename);