r5360: Remove a couple of unused functions.
[Samba/aatanasov.git] / source / build / pidl / ndr.pm
blob149d5f93b983384a95b6aef1d2ef00b0e0f55699
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
8 package NdrParser;
10 use strict;
11 use needed;
13 # list of known types
14 my %typedefs;
16 my %type_alignments =
18 "char" => 1,
19 "int8" => 1,
20 "uint8" => 1,
21 "short" => 2,
22 "wchar_t" => 2,
23 "int16" => 2,
24 "uint16" => 2,
25 "long" => 4,
26 "int32" => 4,
27 "uint32" => 4,
28 "dlong" => 4,
29 "udlong" => 4,
30 "NTTIME" => 4,
31 "NTTIME_1sec" => 4,
32 "time_t" => 4,
33 "DATA_BLOB" => 4,
34 "error_status_t" => 4,
35 "WERROR" => 4,
36 "boolean32" => 4,
37 "unsigned32" => 4,
38 "ipv4address" => 4,
39 "hyper" => 8,
40 "NTTIME_hyper" => 8
43 foreach my $k (keys %type_alignments) {
44 $typedefs{$k} = {
45 NAME => $k,
46 TYPE => "TYPEDEF",
47 DATA => {
48 TYPE => "SCALAR",
49 ALIGN => $type_alignments{$k}
54 sub is_scalar_type($)
56 my $type = shift;
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));
62 return 0;
65 # determine if an element needs a reference pointer on the wire
66 # in its NDR representation
67 sub need_wire_pointer($)
69 my $e = shift;
70 if ($e->{POINTERS} &&
71 !util::has_property($e, "ref")) {
72 return $e->{POINTERS};
74 return undef;
77 # determine if an element is a pure scalar. pure scalars do not
78 # have a "buffers" section in NDR
79 sub is_pure_scalar($)
81 my $e = shift;
82 if (util::has_property($e, "ref")) {
83 return 1;
85 if (is_scalar_type($e->{TYPE}) &&
86 !$e->{POINTERS} &&
87 !util::array_size($e)) {
88 return 1;
90 return 0;
93 # see if a variable needs to be allocated by the NDR subsystem on pull
94 sub need_alloc($)
96 my $e = shift;
98 if (util::has_property($e, "ref")) {
99 return 0;
102 if ($e->{POINTERS} || util::array_size($e)) {
103 return 1;
106 return 0;
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
113 sub c_push_prefix($)
115 my $e = shift;
117 if ($e->{TYPE} =~ "string") {
118 return "";
121 if (is_scalar_type($e->{TYPE}) &&
122 $e->{POINTERS}) {
123 return "*";
125 if (!is_scalar_type($e->{TYPE}) &&
126 !$e->{POINTERS} &&
127 !util::array_size($e)) {
128 return "&";
130 return "";
134 # determine the C prefix used to refer to a variable when passing to a pull
135 # return '&' or ''
136 sub c_pull_prefix($)
138 my $e = shift;
140 if (!$e->{POINTERS} && !util::array_size($e)) {
141 return "&";
144 if ($e->{TYPE} =~ "string") {
145 return "&";
148 return "";
150 my $res = "";
151 sub pidl($)
153 $res .= shift;
156 #####################################################################
157 # parse a properties list
158 sub ParseProperties($)
160 my($props) = shift;
161 foreach my $d (@{$props}) {
162 if (ref($d) ne "HASH") {
163 pidl "[$d] ";
164 } else {
165 foreach my $k (keys %{$d}) {
166 pidl "[$k($d->{$k})] ";
172 ###################################
173 # find a sibling var in a structure
174 sub find_sibling($$)
176 my($e) = shift;
177 my($name) = shift;
178 my($fn) = $e->{PARENT};
180 if ($name =~ /\*(.*)/) {
181 $name = $1;
184 if ($fn->{TYPE} eq "FUNCTION") {
185 for my $e2 (@{$fn->{ELEMENTS}}) {
186 if ($e2->{NAME} eq $name) {
187 return $e2;
192 for my $e2 (@{$fn->{ELEMENTS}}) {
193 if ($e2->{NAME} eq $name) {
194 return $e2;
197 die "invalid sibling '$name'";
200 ####################################################################
201 # work out the name of a size_is() variable
202 sub find_size_var($$$)
204 my($e) = shift;
205 my($size) = shift;
206 my($var_prefix) = shift;
208 my($fn) = $e->{PARENT};
210 if (util::is_constant($size)) {
211 return $size;
214 if ($size =~ /ndr->|\(/) {
215 return $size;
218 my $prefix = "";
220 if ($size =~ /\*(.*)/) {
221 $size = $1;
222 $prefix = "*";
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($)
248 my $size = shift;
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($)
260 my $size = shift;
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
270 sub fn_prefix($)
272 my $fn = shift;
273 if ($fn->{TYPE} eq "TYPEDEF") {
274 if (util::has_property($fn, "public")) {
275 return "";
279 if ($fn->{TYPE} eq "FUNCTION") {
280 if (util::has_property($fn, "public")) {
281 return "";
284 return "static ";
288 ###################################################################
289 # setup any special flags for an element or structure
290 sub start_flags($)
292 my $e = shift;
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
302 sub end_flags($)
304 my $e = shift;
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
314 sub struct_alignment
316 my $s = shift;
318 my $align = 1;
319 for my $e (@{$s->{ELEMENTS}}) {
320 my $a = 1;
322 if (need_wire_pointer($e)) {
323 $a = 4;
324 } else {
325 $a = align_type($e->{TYPE});
328 $align = $a if ($align < $a);
331 return $align;
334 #####################################################################
335 # align a type
336 sub align_type
338 my $e = shift;
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";
343 return 4;
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") {
357 return $dt->{ALIGN};
360 die("Internal pidl error. Typedef has unknown data type $dt->{TYPE}!");
363 #####################################################################
364 # parse an array - push side
365 sub ParseArrayPush($$$)
367 my $e = shift;
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";
384 $size = $length;
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";
389 } else {
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 #####################################################################
395 # print an array
396 sub ParseArrayPrint($$)
398 my $e = shift;
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";
409 } else {
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($$)
418 my $e = shift;
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";
427 pidl "\t}\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";
435 pidl "\t}\n";
440 #####################################################################
441 # parse an array - pull side
442 sub ParseArrayPull($$$)
444 my $e = shift;
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";
458 pidl "\t}\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";
481 pidl "\t}\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";
493 } else {
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($$$)
503 my($e) = shift;
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");
509 start_flags($e);
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";
527 } else {
528 pidl "\tNDR_CHECK(ndr_push_$e->{TYPE}(ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}));\n";
531 end_flags($e);
534 #####################################################################
535 # print scalars in a structure element
536 sub ParseElementPrintScalar($$)
538 my($e) = shift;
539 my($var_prefix) = shift;
540 my $cprefix = c_push_prefix($e);
542 if (util::has_property($e, "noprint")) {
543 return;
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";
549 pidl "\t}\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);
561 } else {
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($$$$)
570 my($e) = shift;
571 my($var_prefix) = shift;
572 my($ndr_flags) = shift;
573 my $switch = 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";
596 } else {
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";
600 pidl "\t\t}\n";
601 if ($switch_var =~ /r->/) {
602 pidl "else { $switch_var = _level; }\n";
604 pidl "\t}\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";
611 pidl "\t}\n";
612 } else {
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($$$$)
623 my($e) = shift;
624 my($var_prefix) = shift;
625 my($ndr_flags) = shift;
626 my $switch = 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";
638 pidl "\t}\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";
645 pidl "\t}\n";
646 } else {
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($$$)
655 my($e) = shift;
656 my($var_prefix) = shift;
657 my $switch = 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($$$)
671 my($e) = shift;
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");
677 start_flags($e);
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";
688 pidl "\t} else {\n";
689 pidl "\t\t$var_prefix$e->{NAME} = NULL;\n";
690 pidl "\t}\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";
697 } else {
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";
706 end_flags($e);
709 #####################################################################
710 # parse buffers in a structure element
711 sub ParseElementPushBuffer($$$)
713 my($e) = shift;
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)) {
720 return;
723 start_flags($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);
739 } else {
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";
748 } else {
749 pidl "\t\tNDR_CHECK(ndr_push_$e->{TYPE}(ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}));\n";
752 if (need_wire_pointer($e)) {
753 pidl "\t}\n";
756 end_flags($e);
759 #####################################################################
760 # print buffers in a structure element
761 sub ParseElementPrintBuffer($$)
763 my($e) = shift;
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);
775 } else {
776 pidl "\t\tndr_print_$e->{TYPE}(ndr, \"$e->{NAME}\", $cprefix$var_prefix$e->{NAME});\n";
779 if (need_wire_pointer($e)) {
780 pidl "\t}\n";
785 #####################################################################
786 # parse buffers in a structure element - pull side
787 sub ParseElementPullBuffer($$$)
789 my($e) = shift;
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)) {
796 return;
799 start_flags($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);
817 } else {
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";
826 } else {
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";
834 pidl "\t}\n";
837 end_flags($e);
840 #####################################################################
841 # parse a struct
842 sub ParseStructPush($)
844 my($struct) = shift;
846 if (! defined $struct->{ELEMENTS}) {
847 return;
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
856 # alignment)
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");
881 pidl "buffers:\n";
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";
889 pidl "done:\n";
891 end_flags($struct);
895 #####################################################################
896 # generate a push function for an enum
897 sub ParseEnumPush($)
899 my($enum) = shift;
900 my($type_fn) = util::enum_type_fn($enum);
902 start_flags($enum);
904 pidl "\tNDR_CHECK(ndr_push_$type_fn(ndr, NDR_SCALARS, r));\n";
906 end_flags($enum);
909 #####################################################################
910 # generate a pull function for an enum
911 sub ParseEnumPull($)
913 my($enum) = shift;
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";
918 start_flags($enum);
919 pidl "\tNDR_CHECK(ndr_pull_$type_fn(ndr, NDR_SCALARS, &v));\n";
920 pidl "\t*r = v;\n";
922 end_flags($enum);
925 #####################################################################
926 # generate a print function for an enum
927 sub ParseEnumPrint($)
929 my($enum) = shift;
931 pidl "\tconst char *val = NULL;\n\n";
933 start_flags($enum);
935 pidl "\tswitch (r) {\n";
936 my $els = \@{$enum->{ELEMENTS}};
937 foreach my $i (0 .. $#{$els}) {
938 my $e = ${$els}[$i];
939 chomp $e;
940 if ($e =~ /^(.*)=/) {
941 $e = $1;
943 pidl "\t\tcase $e: val = \"$e\"; break;\n";
946 pidl "\t}\n\n\tndr_print_enum(ndr, name, \"$enum->{TYPE}\", val, r);\n";
948 end_flags($enum);
952 #####################################################################
953 # generate a push function for a bitmap
954 sub ParseBitmapPush($)
956 my($bitmap) = shift;
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";
963 end_flags($bitmap);
966 #####################################################################
967 # generate a pull function for an bitmap
968 sub ParseBitmapPull($)
970 my($bitmap) = shift;
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";
977 pidl "\t*r = v;\n";
979 end_flags($bitmap);
982 #####################################################################
983 # generate a print function for an bitmap
984 sub ParseBitmapPrintElement($$)
986 my($e) = shift;
987 my($bitmap) = shift;
988 my($type_decl) = util::bitmap_type_decl($bitmap);
989 my($type_fn) = util::bitmap_type_fn($bitmap);
990 my($name) = $bitmap->{PARENT}->{NAME};
991 my($flag);
993 if ($e =~ /^(\w+) .*$/) {
994 $flag = "$1";
995 } else {
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";
1020 end_flags($bitmap);
1023 #####################################################################
1024 # generate a struct print function
1025 sub ParseStructPrint($)
1027 my($struct) = shift;
1029 if (! defined $struct->{ELEMENTS}) {
1030 return;
1033 start_flags($struct);
1035 pidl "\tndr->depth++;\n";
1036 foreach my $e (@{$struct->{ELEMENTS}}) {
1037 ParseElementPrintScalar($e, "r->");
1039 pidl "\tndr->depth--;\n";
1041 end_flags($struct);
1044 #####################################################################
1045 # parse a struct - pull side
1046 sub ParseStructPull($)
1048 my($struct) = shift;
1049 my $conform_e;
1051 if (! defined $struct->{ELEMENTS}) {
1052 return;
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
1059 # alignment)
1060 my $e = $struct->{ELEMENTS}[-1];
1061 if (defined $e->{ARRAY_LEN} && $e->{ARRAY_LEN} eq "*") {
1062 $conform_e = $e;
1065 if (defined $e->{TYPE} && $e->{TYPE} eq "string"
1066 && util::property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
1067 $conform_e = $e;
1070 if (defined $conform_e) {
1071 $conform_e = $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");
1100 pidl "buffers:\n";
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";
1112 pidl "done:\n";
1114 end_flags($struct);
1117 #####################################################################
1118 # calculate size of ndr struct
1119 sub ParseStructNdrSize($)
1121 my $t = shift;
1122 my $static = fn_prefix($t);
1123 my $sizevar;
1125 pidl "size_t ndr_size_$t->{NAME}(const struct $t->{NAME} *r, int flags)\n";
1126 pidl "{\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";
1131 pidl "}\n\n";
1134 #####################################################################
1135 # calculate size of ndr struct
1136 sub ParseUnionNdrSize($)
1138 my $t = shift;
1139 my $static = fn_prefix($t);
1140 my $sizevar;
1142 pidl "size_t ndr_size_$t->{NAME}(const union $t->{NAME} *r, int level, int flags)\n";
1143 pidl "{\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";
1148 pidl "}\n\n";
1151 #####################################################################
1152 # parse a union - push side
1153 sub ParseUnionPush($)
1155 my $e = shift;
1156 my $have_default = 0;
1158 start_flags($e);
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";
1171 $have_default = 1;
1172 } else {
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";
1184 pidl "\t}\n";
1185 pidl "buffers:\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";
1191 } else {
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";
1203 pidl "\t}\n";
1204 pidl "\tndr_push_struct_end(ndr);\n";
1205 pidl "done:\n";
1206 end_flags($e);
1209 #####################################################################
1210 # print a union
1211 sub ParseUnionPrint($)
1213 my $e = shift;
1214 my $have_default = 0;
1216 start_flags($e);
1218 pidl "\tswitch (level) {\n";
1219 foreach my $el (@{$e->{ELEMENTS}}) {
1220 if (util::has_property($el, "default")) {
1221 $have_default = 1;
1222 pidl "\tdefault:\n";
1223 } else {
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";
1234 pidl "\t}\n";
1236 end_flags($e);
1239 #####################################################################
1240 # parse a union - pull side
1241 sub ParseUnionPull($)
1243 my $e = shift;
1244 my $have_default = 0;
1246 start_flags($e);
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";
1259 $have_default = 1;
1260 } else {
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";
1275 pidl "\t}\n";
1276 pidl "buffers:\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";
1282 } else {
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";
1294 pidl "\t}\n";
1295 pidl "\tndr_pull_struct_end(ndr);\n";
1296 pidl "done:\n";
1297 end_flags($e);
1301 #####################################################################
1302 # parse a type
1303 sub ParseTypePush($)
1305 my($data) = shift;
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($)
1321 my($data) = shift;
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 #####################################################################
1334 # parse a type
1335 sub ParseTypePull($)
1337 my($data) = shift;
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($)
1353 my($e) = shift;
1354 my $static = fn_prefix($e);
1356 if (! needed::is_needed("push_$e->{NAME}")) {
1357 # print "push_$e->{NAME} not needed\n";
1358 return;
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)";
1367 pidl "\n{\n";
1368 ParseTypePush($e->{DATA});
1369 pidl "\treturn NT_STATUS_OK;\n";
1370 pidl "}\n\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)";
1375 pidl "\n{\n";
1376 ParseTypePush($e->{DATA});
1377 pidl "\treturn NT_STATUS_OK;\n";
1378 pidl "}\n\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)";
1383 pidl "\n{\n";
1384 ParseTypePush($e->{DATA});
1385 pidl "\treturn NT_STATUS_OK;\n";
1386 pidl "}\n\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)";
1392 pidl "\n{\n";
1393 ParseTypePush($e->{DATA});
1394 pidl "\treturn NT_STATUS_OK;\n";
1395 pidl "}\n\n";
1400 #####################################################################
1401 # parse a typedef - pull side
1402 sub ParseTypedefPull($)
1404 my($e) = shift;
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";
1413 return;
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)";
1418 pidl "\n{\n";
1419 ParseTypePull($e->{DATA});
1420 pidl "\treturn NT_STATUS_OK;\n";
1421 pidl "}\n\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)";
1426 pidl "\n{\n";
1427 ParseTypePull($e->{DATA});
1428 pidl "\treturn NT_STATUS_OK;\n";
1429 pidl "}\n\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)";
1434 pidl "\n{\n";
1435 ParseTypePull($e->{DATA});
1436 pidl "\treturn NT_STATUS_OK;\n";
1437 pidl "}\n\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)";
1443 pidl "\n{\n";
1444 ParseTypePull($e->{DATA});
1445 pidl "\treturn NT_STATUS_OK;\n";
1446 pidl "}\n\n";
1451 #####################################################################
1452 # parse a typedef - print side
1453 sub ParseTypedefPrint($)
1455 my($e) = shift;
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)";
1463 pidl "\n{\n";
1464 pidl "\tndr_print_struct(ndr, name, \"$e->{NAME}\");\n";
1465 ParseTypePrint($e->{DATA});
1466 pidl "}\n\n";
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)";
1471 pidl "\n{\n";
1472 pidl "\tndr_print_union(ndr, name, level, \"$e->{NAME}\");\n";
1473 ParseTypePrint($e->{DATA});
1474 pidl "}\n\n";
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)";
1479 pidl "\n{\n";
1480 ParseTypePrint($e->{DATA});
1481 pidl "}\n\n";
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)";
1487 pidl "\n{\n";
1488 ParseTypePrint($e->{DATA});
1489 pidl "}\n\n";
1493 #####################################################################
1494 ## calculate the size of a structure
1495 sub ParseTypedefNdrSize($)
1497 my($t) = shift;
1498 if (! needed::is_needed("ndr_size_$t->{NAME}")) {
1499 return;
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($)
1513 my($fn) = shift;
1515 pidl "void ndr_print_$fn->{NAME}(struct ndr_print *ndr, const char *name, int flags, struct $fn->{NAME} *r)";
1516 pidl "\n{\n";
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";
1522 pidl "\t}\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";
1534 pidl "\t}\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") {
1545 my $cprefix = "&";
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";
1550 pidl "\t}\n";
1552 pidl "\tndr->depth--;\n";
1553 pidl "}\n\n";
1557 #####################################################################
1558 # parse a function element
1559 sub ParseFunctionElementPush($$)
1561 my $e = shift;
1562 my $inout = shift;
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");
1569 pidl "\t}\n";
1570 } else {
1571 ParseArrayPush($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1573 } else {
1574 ParseElementPushScalar($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1576 if ($e->{POINTERS}) {
1577 ParseElementPushBuffer($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1582 #####################################################################
1583 # parse a function
1584 sub ParseFunctionPush($)
1586 my($fn) = shift;
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";
1612 pidl "\ndone:\n";
1613 pidl "\n\treturn NT_STATUS_OK;\n}\n\n";
1616 #####################################################################
1617 # parse a function element
1618 sub ParseFunctionElementPull($$)
1620 my $e = shift;
1621 my $inout = shift;
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";
1630 } else {
1631 pidl "\t{\n";
1633 ParseArrayPull($e, "r->$inout.", "NDR_SCALARS|NDR_BUFFERS");
1634 pidl "\t}\n";
1635 } else {
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";
1639 pidl "\t}\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($)
1657 my $e = shift;
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
1662 # the in side
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";
1669 } else {
1670 pidl "\tZERO_STRUCTP(r->out.$e->{NAME});\n";
1672 return;
1675 # its an array
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";
1681 } else {
1682 pidl "\tmemset(r->out.$e->{NAME}, 0, $size * sizeof(*r->out.$e->{NAME}));\n";
1687 #####################################################################
1688 # parse a function
1689 sub ParseFunctionPull($)
1691 my($fn) = shift;
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";
1713 last;
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";
1753 pidl "\ndone:\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};
1763 my $count = 0;
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") {
1775 pidl "\t{\n";
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";
1781 pidl "\t},\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) {
1796 pidl "\t$ep, \n";
1798 pidl "};\n\n";
1800 pidl "static const struct dcerpc_endpoint_list $interface->{NAME}\_endpoints = {\n";
1801 pidl "\t$endpoint_count, $interface->{NAME}\_endpoint_strings\n";
1802 pidl "};\n\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";
1809 pidl "\t$count,\n";
1810 pidl "\t$interface->{NAME}\_calls,\n";
1811 pidl "\t&$interface->{NAME}\_endpoints\n";
1812 pidl "};\n\n";
1814 pidl "static NTSTATUS dcerpc_ndr_$interface->{NAME}_init(void)\n";
1815 pidl "{\n";
1816 pidl "\treturn librpc_register_interface(&dcerpc_table_$interface->{NAME});\n";
1817 pidl "}\n\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;
1836 # Push functions
1837 foreach my $d (@{$data}) {
1838 ($d->{TYPE} eq "TYPEDEF") &&
1839 ParseTypedefPush($d);
1840 ($d->{TYPE} eq "FUNCTION") &&
1841 ParseFunctionPush($d);
1844 # Pull functions
1845 foreach my $d (@{$data}) {
1846 ($d->{TYPE} eq "TYPEDEF") &&
1847 ParseTypedefPull($d);
1848 ($d->{TYPE} eq "FUNCTION") &&
1849 ParseFunctionPull($d);
1852 # Print functions
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);
1864 # Size functions
1865 foreach my $d (@{$data}) {
1866 ($d->{TYPE} eq "TYPEDEF") &&
1867 ParseTypedefNdrSize($d);
1870 FunctionTable($interface);
1873 sub RegistrationFunction($$)
1875 my $idl = shift;
1876 my $filename = shift;
1878 $filename =~ /.*\/ndr_(.*).c/;
1879 my $basename = $1;
1880 pidl "NTSTATUS dcerpc_$basename\_init(void)\n";
1881 pidl "{\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};
1887 my $count = 0;
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";
1897 pidl "\t}\n\n";
1899 pidl "\treturn status;\n";
1900 pidl "}\n\n";
1903 #####################################################################
1904 # parse a parsed IDL structure back into an IDL file
1905 sub Parse($$)
1907 my($idl) = shift;
1908 my($filename) = shift;
1909 my $h_filename = $filename;
1910 $res = "";
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);
1923 ParseInterface($x);
1927 RegistrationFunction($idl, $filename);
1929 return $res;