1 ###################################################
2 # Samba4 parser generator for IDL structures
3 # Copyright tridge@samba.org 2000-2003
4 # Copyright tpot@samba.org 2001
5 # released under the GNU GPL
13 # the list of needed functions
16 #####################################################################
17 # parse a properties list
18 sub ParseProperties($)
21 foreach my $d (@{$props}) {
22 if (ref($d) ne "HASH") {
25 foreach my $k (keys %{$d}) {
26 $res .= "[$k($d->{$k})] ";
32 ####################################################################
33 # work out the name of a size_is() variable
38 my($fn) = $e->{PARENT};
40 if (util::is_constant($size)) {
44 if ($size =~ /ndr->/) {
48 if ($fn->{TYPE} ne "FUNCTION") {
52 for my $e2 (@{$fn->{DATA}}) {
53 if ($e2->{NAME} eq $size) {
54 if (util::has_property($e2, "in")) {
57 if (util::has_property($e2, "out")) {
58 return "r->out.$size";
62 die "invalid variable in $size for element $e->{NAME} in $fn->{NAME}\n";
66 #####################################################################
67 # work out is a parse function should be declared static or not
71 if (util::has_property($e, "public")) {
78 #####################################################################
79 # work out the correct alignment for a structure
80 sub struct_alignment($)
84 for my $e (@{$s->{ELEMENTS}}) {
85 if ($align < util::type_align($e)) {
86 $align = util::type_align($e);
92 #####################################################################
93 # parse an array - push side
94 sub ParseArrayPush($$$)
97 my $var_prefix = shift;
98 my $ndr_flags = shift;
100 my $size = find_size_var($e, util::array_size($e));
102 if (defined $e->{CONFORMANT_SIZE}) {
103 # the conformant size has already been pushed
104 } elsif (!util::is_inline_array($e)) {
105 # we need to emit the array size
106 $res .= "\t\tNDR_CHECK(ndr_push_uint32(ndr, $size));\n";
109 if (my $length = util::has_property($e, "length_is")) {
110 $length = find_size_var($e, $length);
111 $res .= "\t\tNDR_CHECK(ndr_push_uint32(ndr, 0));\n";
112 $res .= "\t\tNDR_CHECK(ndr_push_uint32(ndr, $length));\n";
116 if (util::is_scalar_type($e->{TYPE})) {
117 $res .= "\t\tNDR_CHECK(ndr_push_array_$e->{TYPE}(ndr, $ndr_flags, $var_prefix$e->{NAME}, $size));\n";
119 $res .= "\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";
123 #####################################################################
125 sub ParseArrayPrint($$)
128 my $var_prefix = shift;
129 my $size = find_size_var($e, util::array_size($e));
130 my $length = util::has_property($e, "length_is");
132 if (defined $length) {
133 $size = find_size_var($e, $length);
136 if (util::is_scalar_type($e->{TYPE})) {
137 $res .= "\t\tndr_print_array_$e->{TYPE}(ndr, \"$e->{NAME}\", $var_prefix$e->{NAME}, $size);\n";
139 $res .= "\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";
143 #####################################################################
144 # parse an array - pull side
145 sub ParseArrayPull($$$)
148 my $var_prefix = shift;
149 my $ndr_flags = shift;
151 my $size = find_size_var($e, util::array_size($e));
152 my $alloc_size = $size;
154 # if this is a conformant array then we use that size to allocate, and make sure
155 # we allocate enough to pull the elements
156 if (defined $e->{CONFORMANT_SIZE}) {
157 $alloc_size = $e->{CONFORMANT_SIZE};
159 $res .= "\tif ($size > $alloc_size) {\n";
160 $res .= "\t\treturn ndr_pull_error(ndr, NDR_ERR_CONFORMANT_SIZE, \"Bad conformant size %u should be %u\", $alloc_size, $size);\n";
162 } elsif (!util::is_inline_array($e)) {
163 # non fixed arrays encode the size just before the array
165 $res .= "\t\tuint32 _array_size;\n";
166 $res .= "\t\tNDR_CHECK(ndr_pull_uint32(ndr, &_array_size));\n";
167 $res .= "\t\tif ($size > _array_size) {\n";
168 $res .= "\t\t\treturn ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, \"Bad array size %u should be %u\", _array_size, $size);\n";
173 if (util::need_alloc($e) && !util::is_fixed_array($e)) {
174 if (!util::is_inline_array($e) || $ndr_flags eq "NDR_SCALARS") {
175 $res .= "\t\tNDR_ALLOC_N_SIZE(ndr, $var_prefix$e->{NAME}, $alloc_size, sizeof($var_prefix$e->{NAME}\[0]));\n";
179 if (my $length = util::has_property($e, "length_is")) {
180 $length = find_size_var($e, $length);
181 $res .= "\t\tuint32 _offset, _length;\n";
182 $res .= "\t\tNDR_CHECK(ndr_pull_uint32(ndr, &_offset));\n";
183 $res .= "\t\tNDR_CHECK(ndr_pull_uint32(ndr, &_length));\n";
184 $res .= "\t\tif (_offset != 0) return ndr_pull_error(ndr, NDR_ERR_OFFSET, \"Bad array offset 0x%08x\", _offset);\n";
185 $res .= "\t\tif (_length > $size || _length != $length) return ndr_pull_error(ndr, NDR_ERR_LENGTH, \"Bad array length 0x%08x > size 0x%08x\", _offset, $size);\n";
189 if (util::is_scalar_type($e->{TYPE})) {
190 $res .= "\t\tNDR_CHECK(ndr_pull_array_$e->{TYPE}(ndr, $ndr_flags, $var_prefix$e->{NAME}, $size));\n";
192 $res .= "\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";
197 #####################################################################
198 # parse scalars in a structure element
199 sub ParseElementPushScalar($$$)
202 my($var_prefix) = shift;
203 my($ndr_flags) = shift;
204 my $cprefix = util::c_push_prefix($e);
206 if (util::has_property($e, "relative")) {
207 $res .= "\tNDR_CHECK(ndr_push_relative(ndr, NDR_SCALARS, $var_prefix$e->{NAME}, (ndr_push_const_fn_t) ndr_push_$e->{TYPE}));\n";
208 } elsif (util::is_inline_array($e)) {
209 ParseArrayPush($e, "r->", "NDR_SCALARS");
210 } elsif (my $value = util::has_property($e, "value")) {
211 $res .= "\tNDR_CHECK(ndr_push_$e->{TYPE}(ndr, $value));\n";
212 } elsif (defined $e->{VALUE}) {
213 $res .= "\tNDR_CHECK(ndr_push_$e->{TYPE}(ndr, $e->{VALUE}));\n";
214 } elsif (util::need_wire_pointer($e)) {
215 $res .= "\tNDR_CHECK(ndr_push_ptr(ndr, $var_prefix$e->{NAME}));\n";
216 } elsif (my $switch = util::has_property($e, "switch_is")) {
217 ParseElementPushSwitch($e, $var_prefix, $ndr_flags, $switch);
218 } elsif (util::is_builtin_type($e->{TYPE})) {
219 $res .= "\tNDR_CHECK(ndr_push_$e->{TYPE}(ndr, $cprefix$var_prefix$e->{NAME}));\n";
221 $res .= "\tNDR_CHECK(ndr_push_$e->{TYPE}(ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}));\n";
225 #####################################################################
226 # print scalars in a structure element
227 sub ParseElementPrintScalar($$)
230 my($var_prefix) = shift;
231 my $cprefix = util::c_push_prefix($e);
233 if (defined $e->{VALUE}) {
234 $res .= "\tndr_print_$e->{TYPE}(ndr, \"$e->{NAME}\", $e->{VALUE});\n";
235 } elsif (util::has_direct_buffers($e)) {
236 $res .= "\tndr_print_ptr(ndr, \"$e->{NAME}\", $var_prefix$e->{NAME});\n";
237 $res .= "\tndr->depth++;\n";
238 ParseElementPrintBuffer($e, $var_prefix);
239 $res .= "\tndr->depth--;\n";
240 } elsif (my $switch = util::has_property($e, "switch_is")) {
241 ParseElementPrintSwitch($e, $var_prefix, $switch);
243 $res .= "\tndr_print_$e->{TYPE}(ndr, \"$e->{NAME}\", $cprefix$var_prefix$e->{NAME});\n";
247 #####################################################################
248 # parse scalars in a structure element - pull size
249 sub ParseElementPullSwitch($$$$)
252 my($var_prefix) = shift;
253 my($ndr_flags) = shift;
255 my $switch_var = find_size_var($e, $switch);
257 my $cprefix = util::c_pull_prefix($e);
259 $res .= "\t{ uint16 _level = $switch_var;\n";
261 if (util::has_property($e, "subcontext")) {
262 $res .= "\tNDR_CHECK(ndr_pull_subcontext_union_fn(ndr, &_level, $cprefix$var_prefix$e->{NAME}, (ndr_pull_union_fn_t) ndr_pull_$e->{TYPE}));\n";
264 $res .= "\tNDR_CHECK(ndr_pull_$e->{TYPE}(ndr, $ndr_flags, &_level, $cprefix$var_prefix$e->{NAME}));\n";
268 $res .= "\tif ((($ndr_flags) & NDR_SCALARS) && (_level != $switch_var)) return ndr_pull_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value %u in $e->{NAME}\");\n";
272 #####################################################################
273 # push switch element
274 sub ParseElementPushSwitch($$$$)
277 my($var_prefix) = shift;
278 my($ndr_flags) = shift;
280 my $switch_var = find_size_var($e, $switch);
281 my $cprefix = util::c_push_prefix($e);
283 if (util::has_property($e, "subcontext")) {
284 $res .= "\tNDR_CHECK(ndr_push_subcontext_union_fn(ndr, $switch_var, $cprefix$var_prefix$e->{NAME}, (ndr_push_union_fn_t) ndr_pull_$e->{TYPE}));\n";
286 $res .= "\tNDR_CHECK(ndr_push_$e->{TYPE}(ndr, $ndr_flags, $switch_var, $cprefix$var_prefix$e->{NAME}));\n";
290 #####################################################################
291 # print scalars in a structure element
292 sub ParseElementPrintSwitch($$$)
295 my($var_prefix) = shift;
297 my $switch_var = find_size_var($e, $switch);
298 my $cprefix = util::c_push_prefix($e);
300 $res .= "\tndr_print_$e->{TYPE}(ndr, \"$e->{NAME}\", $switch_var, $cprefix$var_prefix$e->{NAME});\n";
304 #####################################################################
305 # parse scalars in a structure element - pull size
306 sub ParseElementPullScalar($$$)
309 my($var_prefix) = shift;
310 my($ndr_flags) = shift;
311 my $cprefix = util::c_pull_prefix($e);
313 if (util::has_property($e, "relative")) {
314 $res .= "\tNDR_CHECK(ndr_pull_relative(ndr, (const void **)&$var_prefix$e->{NAME}, sizeof(*$var_prefix$e->{NAME}), (ndr_pull_flags_fn_t)ndr_pull_$e->{TYPE}));\n";
315 } elsif (defined $e->{VALUE}) {
316 $res .= "\tNDR_CHECK(ndr_pull_$e->{TYPE}(ndr, $e->{VALUE}));\n";
317 } elsif (util::is_inline_array($e)) {
318 ParseArrayPull($e, "r->", "NDR_SCALARS");
319 } elsif (util::need_wire_pointer($e)) {
320 $res .= "\tNDR_CHECK(ndr_pull_uint32(ndr, &_ptr_$e->{NAME}));\n";
321 $res .= "\tif (_ptr_$e->{NAME}) {\n";
322 $res .= "\t\tNDR_ALLOC(ndr, $var_prefix$e->{NAME});\n";
323 $res .= "\t} else {\n";
324 $res .= "\t\t$var_prefix$e->{NAME} = NULL;\n";
326 } elsif (util::need_alloc($e)) {
327 # no scalar component
328 } elsif (my $switch = util::has_property($e, "switch_is")) {
329 ParseElementPullSwitch($e, $var_prefix, $ndr_flags, $switch);
330 } elsif (util::has_property($e, "subcontext")) {
331 if (util::is_builtin_type($e->{TYPE})) {
332 $res .= "\tNDR_CHECK(ndr_pull_subcontext_fn(ndr, $cprefix$var_prefix$e->{NAME}, (ndr_pull_fn_t) ndr_pull_$e->{TYPE}));\n";
334 $res .= "\tNDR_CHECK(ndr_pull_subcontext_flags_fn(ndr, $cprefix$var_prefix$e->{NAME}, (ndr_pull_flags_fn_t) ndr_pull_$e->{TYPE}));\n";
336 } elsif (util::is_builtin_type($e->{TYPE})) {
337 $res .= "\tNDR_CHECK(ndr_pull_$e->{TYPE}(ndr, $cprefix$var_prefix$e->{NAME}));\n";
339 $res .= "\tNDR_CHECK(ndr_pull_$e->{TYPE}(ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}));\n";
343 #####################################################################
344 # parse buffers in a structure element
345 sub ParseElementPushBuffer($$$)
348 my($var_prefix) = shift;
349 my($ndr_flags) = shift;
350 my $cprefix = util::c_push_prefix($e);
352 if (util::is_pure_scalar($e)) {
356 if (util::need_wire_pointer($e)) {
357 $res .= "\tif ($var_prefix$e->{NAME}) {\n";
360 if (util::has_property($e, "relative")) {
361 $res .= "\tNDR_CHECK(ndr_push_relative(ndr, NDR_BUFFERS, $cprefix$var_prefix$e->{NAME}, (ndr_push_const_fn_t) ndr_push_$e->{TYPE}));\n";
362 } elsif (util::is_inline_array($e)) {
363 ParseArrayPush($e, "r->", "NDR_BUFFERS");
364 } elsif (util::array_size($e)) {
365 ParseArrayPush($e, "r->", "NDR_SCALARS|NDR_BUFFERS");
366 } elsif (my $switch = util::has_property($e, "switch_is")) {
367 if ($e->{POINTERS}) {
368 ParseElementPushSwitch($e, $var_prefix, "NDR_BUFFERS|NDR_SCALARS", $switch);
370 ParseElementPushSwitch($e, $var_prefix, "NDR_BUFFERS", $switch);
372 } elsif (util::is_builtin_type($e->{TYPE})) {
373 $res .= "\t\tNDR_CHECK(ndr_push_$e->{TYPE}(ndr, $cprefix$var_prefix$e->{NAME}));\n";
374 } elsif ($e->{POINTERS}) {
375 $res .= "\t\tNDR_CHECK(ndr_push_$e->{TYPE}(ndr, NDR_SCALARS|NDR_BUFFERS, $cprefix$var_prefix$e->{NAME}));\n";
377 $res .= "\t\tNDR_CHECK(ndr_push_$e->{TYPE}(ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}));\n";
380 if (util::need_wire_pointer($e)) {
385 #####################################################################
386 # print buffers in a structure element
387 sub ParseElementPrintBuffer($$)
390 my($var_prefix) = shift;
391 my $cprefix = util::c_push_prefix($e);
393 if (util::need_wire_pointer($e)) {
394 $res .= "\tif ($var_prefix$e->{NAME}) {\n";
397 if (util::array_size($e)) {
398 ParseArrayPrint($e, $var_prefix);
399 } elsif (my $switch = util::has_property($e, "switch_is")) {
400 ParseElementPrintSwitch($e, $var_prefix, $switch);
402 $res .= "\t\tndr_print_$e->{TYPE}(ndr, \"$e->{NAME}\", $cprefix$var_prefix$e->{NAME});\n";
405 if (util::need_wire_pointer($e)) {
411 #####################################################################
412 # parse buffers in a structure element - pull side
413 sub ParseElementPullBuffer($$$)
416 my($var_prefix) = shift;
417 my($ndr_flags) = shift;
418 my $cprefix = util::c_pull_prefix($e);
420 if (util::is_pure_scalar($e)) {
424 if (util::has_property($e, "relative")) {
428 if (util::need_wire_pointer($e)) {
429 $res .= "\tif ($var_prefix$e->{NAME}) {\n";
432 if (util::is_inline_array($e)) {
433 ParseArrayPull($e, "r->", "NDR_BUFFERS");
434 } elsif (util::array_size($e)) {
435 ParseArrayPull($e, "r->", "NDR_SCALARS|NDR_BUFFERS");
436 } elsif (my $switch = util::has_property($e, "switch_is")) {
437 if ($e->{POINTERS}) {
438 ParseElementPullSwitch($e, $var_prefix, "NDR_SCALARS|NDR_BUFFERS", $switch);
440 ParseElementPullSwitch($e, $var_prefix, "NDR_BUFFERS", $switch);
442 } elsif (util::has_property($e, "subcontext")) {
443 if (util::is_builtin_type($e->{TYPE})) {
444 $res .= "\tNDR_CHECK(ndr_pull_subcontext_fn(ndr, $cprefix$var_prefix$e->{NAME}, (ndr_pull_fn_t) ndr_pull_$e->{TYPE}));\n";
446 $res .= "\tNDR_CHECK(ndr_pull_subcontext_flags_fn(ndr, $cprefix$var_prefix$e->{NAME}, (ndr_pull_flags_fn_t) ndr_pull_$e->{TYPE}));\n";
448 } elsif (util::is_builtin_type($e->{TYPE})) {
449 $res .= "\t\tNDR_CHECK(ndr_pull_$e->{TYPE}(ndr, $cprefix$var_prefix$e->{NAME}));\n";
450 } elsif ($e->{POINTERS}) {
451 $res .= "\t\tNDR_CHECK(ndr_pull_$e->{TYPE}(ndr, NDR_SCALARS|NDR_BUFFERS, $cprefix$var_prefix$e->{NAME}));\n";
453 $res .= "\t\tNDR_CHECK(ndr_pull_$e->{TYPE}(ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}));\n";
456 if (util::need_wire_pointer($e)) {
461 #####################################################################
463 sub ParseStructPush($)
468 if (! defined $struct->{ELEMENTS}) {
472 # see if the structure contains a conformant array. If it
473 # does, then it must be the last element of the structure, and
474 # we need to push the conformant length early, as it fits on
475 # the wire before the structure (and even before the structure
477 my $e = $struct->{ELEMENTS}[-1];
478 if (defined $e->{ARRAY_LEN} && $e->{ARRAY_LEN} eq "*") {
479 my $size = find_size_var($e, util::array_size($e));
480 $e->{CONFORMANT_SIZE} = $size;
482 $res .= "\tNDR_CHECK(ndr_push_uint32(ndr, $size));\n";
485 $res .= "\tif (!(ndr_flags & NDR_SCALARS)) goto buffers;\n";
487 $res .= "\tNDR_CHECK(ndr_push_struct_start(ndr));\n";
489 my $align = struct_alignment($struct);
490 $res .= "\tNDR_CHECK(ndr_push_align(ndr, $align));\n";
492 foreach my $e (@{$struct->{ELEMENTS}}) {
493 $e->{PARENT} = $struct;
494 ParseElementPushScalar($e, "r->", "NDR_SCALARS");
497 $res .= "\tndr_push_struct_end(ndr);\n";
499 $res .= "buffers:\n";
500 $res .= "\tif (!(ndr_flags & NDR_BUFFERS)) goto done;\n";
501 foreach my $e (@{$struct->{ELEMENTS}}) {
502 ParseElementPushBuffer($e, "r->", "NDR_BUFFERS");
508 #####################################################################
509 # generate a struct print function
510 sub ParseStructPrint($)
514 if (! defined $struct->{ELEMENTS}) {
518 $res .= "\tndr->depth++;\n";
519 foreach my $e (@{$struct->{ELEMENTS}}) {
520 ParseElementPrintScalar($e, "r->");
522 $res .= "\tndr->depth--;\n";
525 #####################################################################
526 # parse a struct - pull side
527 sub ParseStructPull($)
532 if (! defined $struct->{ELEMENTS}) {
536 # see if the structure contains a conformant array. If it
537 # does, then it must be the last element of the structure, and
538 # we need to pull the conformant length early, as it fits on
539 # the wire before the structure (and even before the structure
541 my $e = $struct->{ELEMENTS}[-1];
542 if (defined $e->{ARRAY_LEN} && $e->{ARRAY_LEN} eq "*") {
544 $res .= "\tuint32 _conformant_size;\n";
545 $conform_e->{CONFORMANT_SIZE} = "_conformant_size";
548 # declare any internal pointers we need
549 foreach my $e (@{$struct->{ELEMENTS}}) {
550 $e->{PARENT} = $struct;
551 if (util::need_wire_pointer($e)) {
552 $res .= "\tuint32 _ptr_$e->{NAME};\n";
556 $res .= "\tNDR_CHECK(ndr_pull_struct_start(ndr));\n";
558 if (defined $conform_e) {
559 $res .= "\tNDR_CHECK(ndr_pull_uint32(ndr, &$conform_e->{CONFORMANT_SIZE}));\n";
562 $res .= "\tif (!(ndr_flags & NDR_SCALARS)) goto buffers;\n";
564 my $align = struct_alignment($struct);
565 $res .= "\tNDR_CHECK(ndr_pull_align(ndr, $align));\n";
567 foreach my $e (@{$struct->{ELEMENTS}}) {
568 ParseElementPullScalar($e, "r->", "NDR_SCALARS");
571 $res .= "\tndr_pull_struct_end(ndr);\n";
573 $res .= "buffers:\n";
574 $res .= "\tif (!(ndr_flags & NDR_BUFFERS)) goto done;\n";
575 foreach my $e (@{$struct->{ELEMENTS}}) {
576 ParseElementPullBuffer($e, "r->", "NDR_BUFFERS");
583 #####################################################################
584 # parse a union - push side
585 sub ParseUnionPush($)
588 my $have_default = 0;
589 $res .= "\tif (!(ndr_flags & NDR_SCALARS)) goto buffers;\n";
591 $res .= "\tNDR_CHECK(ndr_push_struct_start(ndr));\n";
593 if (!util::has_property($e, "nodiscriminant")) {
594 $res .= "\tNDR_CHECK(ndr_push_uint16(ndr, level));\n";
596 $res .= "\tswitch (level) {\n";
597 foreach my $el (@{$e->{DATA}}) {
598 if ($el->{CASE} eq "default") {
599 $res .= "\tdefault:\n";
602 $res .= "\tcase $el->{CASE}:\n";
604 ParseElementPushScalar($el->{DATA}, "r->", "NDR_SCALARS");
605 $res .= "\tbreak;\n\n";
607 if (! $have_default) {
608 $res .= "\tdefault:\n";
609 $res .= "\t\treturn ndr_push_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);\n";
612 $res .= "\tndr_push_struct_end(ndr);\n";
613 $res .= "buffers:\n";
614 $res .= "\tif (!(ndr_flags & NDR_BUFFERS)) goto done;\n";
615 $res .= "\tswitch (level) {\n";
616 foreach my $el (@{$e->{DATA}}) {
617 if ($el->{CASE} eq "default") {
618 $res .= "\tdefault:\n";
620 $res .= "\tcase $el->{CASE}:\n";
622 ParseElementPushBuffer($el->{DATA}, "r->", "ndr_flags");
623 $res .= "\tbreak;\n\n";
625 if (! $have_default) {
626 $res .= "\tdefault:\n";
627 $res .= "\t\treturn ndr_push_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);\n";
633 #####################################################################
635 sub ParseUnionPrint($)
638 my $have_default = 0;
640 $res .= "\tswitch (level) {\n";
641 foreach my $el (@{$e->{DATA}}) {
642 if ($el->{CASE} eq "default") {
644 $res .= "\tdefault:\n";
646 $res .= "\tcase $el->{CASE}:\n";
648 ParseElementPrintScalar($el->{DATA}, "r->");
649 $res .= "\tbreak;\n\n";
651 if (! $have_default) {
652 $res .= "\tdefault:\n\t\tndr_print_bad_level(ndr, name, level);\n";
657 #####################################################################
658 # parse a union - pull side
659 sub ParseUnionPull($)
662 my $have_default = 0;
664 $res .= "\tif (!(ndr_flags & NDR_SCALARS)) goto buffers;\n";
666 $res .= "\tNDR_CHECK(ndr_pull_struct_start(ndr));\n";
668 if (!util::has_property($e, "nodiscriminant")) {
669 $res .= "\tNDR_CHECK(ndr_pull_uint16(ndr, level));\n";
671 $res .= "\tswitch (*level) {\n";
672 foreach my $el (@{$e->{DATA}}) {
673 if ($el->{CASE} eq "default") {
674 $res .= "\tdefault: {\n";
677 $res .= "\tcase $el->{CASE}: {\n";
679 my $e2 = $el->{DATA};
680 if ($e2->{POINTERS}) {
681 $res .= "\t\tuint32 _ptr_$e2->{NAME};\n";
683 ParseElementPullScalar($el->{DATA}, "r->", "NDR_SCALARS");
684 $res .= "\tbreak; }\n\n";
686 if (! $have_default) {
687 $res .= "\tdefault:\n";
688 $res .= "\t\treturn ndr_pull_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", *level);\n";
691 $res .= "\tndr_pull_struct_end(ndr);\n";
692 $res .= "buffers:\n";
693 $res .= "\tif (!(ndr_flags & NDR_BUFFERS)) goto done;\n";
694 $res .= "\tswitch (*level) {\n";
695 foreach my $el (@{$e->{DATA}}) {
696 if ($el->{CASE} eq "default") {
697 $res .= "\tdefault:\n";
699 $res .= "\tcase $el->{CASE}:\n";
701 ParseElementPullBuffer($el->{DATA}, "r->", "NDR_BUFFERS");
702 $res .= "\tbreak;\n\n";
704 if (! $have_default) {
705 $res .= "\tdefault:\n";
706 $res .= "\t\treturn ndr_pull_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", *level);\n";
712 #####################################################################
718 if (ref($data) eq "HASH") {
719 ($data->{TYPE} eq "STRUCT") &&
720 ParseStructPush($data);
721 ($data->{TYPE} eq "UNION") &&
722 ParseUnionPush($data);
726 #####################################################################
727 # generate a print function for a type
728 sub ParseTypePrint($)
732 if (ref($data) eq "HASH") {
733 ($data->{TYPE} eq "STRUCT") &&
734 ParseStructPrint($data);
735 ($data->{TYPE} eq "UNION") &&
736 ParseUnionPrint($data);
740 #####################################################################
746 if (ref($data) eq "HASH") {
747 ($data->{TYPE} eq "STRUCT") &&
748 ParseStructPull($data);
749 ($data->{TYPE} eq "UNION") &&
750 ParseUnionPull($data);
754 #####################################################################
755 # parse a typedef - push side
756 sub ParseTypedefPush($)
759 my $static = fn_prefix($e);
761 if (! $needed{"push_$e->{NAME}"}) {
762 # print "push_$e->{NAME} not needed\n";
766 if ($e->{DATA}->{TYPE} eq "STRUCT") {
767 $res .= "$static" . "NTSTATUS ndr_push_$e->{NAME}(struct ndr_push *ndr, int ndr_flags, struct $e->{NAME} *r)";
769 ParseTypePush($e->{DATA});
770 $res .= "\treturn NT_STATUS_OK;\n";
774 if ($e->{DATA}->{TYPE} eq "UNION") {
775 $res .= "$static" . "NTSTATUS ndr_push_$e->{NAME}(struct ndr_push *ndr, int ndr_flags, uint16 level, union $e->{NAME} *r)";
777 ParseTypePush($e->{DATA});
778 $res .= "\treturn NT_STATUS_OK;\n";
784 #####################################################################
785 # parse a typedef - pull side
786 sub ParseTypedefPull($)
789 my $static = fn_prefix($e);
791 if (! $needed{"pull_$e->{NAME}"}) {
792 # print "pull_$e->{NAME} not needed\n";
796 if ($e->{DATA}->{TYPE} eq "STRUCT") {
797 $res .= "$static" . "NTSTATUS ndr_pull_$e->{NAME}(struct ndr_pull *ndr, int ndr_flags, struct $e->{NAME} *r)";
799 ParseTypePull($e->{DATA});
800 $res .= "\treturn NT_STATUS_OK;\n";
804 if ($e->{DATA}->{TYPE} eq "UNION") {
805 $res .= "$static" . "NTSTATUS ndr_pull_$e->{NAME}(struct ndr_pull *ndr, int ndr_flags, uint16 *level, union $e->{NAME} *r)";
807 ParseTypePull($e->{DATA});
808 $res .= "\treturn NT_STATUS_OK;\n";
814 #####################################################################
815 # parse a typedef - print side
816 sub ParseTypedefPrint($)
820 if ($e->{DATA}->{TYPE} eq "STRUCT") {
821 $res .= "void ndr_print_$e->{NAME}(struct ndr_print *ndr, const char *name, struct $e->{NAME} *r)";
823 $res .= "\tndr_print_struct(ndr, name, \"$e->{NAME}\");\n";
824 ParseTypePrint($e->{DATA});
828 if ($e->{DATA}->{TYPE} eq "UNION") {
829 $res .= "void ndr_print_$e->{NAME}(struct ndr_print *ndr, const char *name, uint16 level, union $e->{NAME} *r)";
831 $res .= "\tndr_print_union(ndr, name, level, \"$e->{NAME}\");\n";
832 ParseTypePrint($e->{DATA});
837 #####################################################################
838 # parse a function - print side
839 sub ParseFunctionPrint($)
843 $res .= "void ndr_print_$fn->{NAME}(struct ndr_print *ndr, const char *name, int flags, struct $fn->{NAME} *r)";
845 $res .= "\tndr_print_struct(ndr, name, \"$fn->{NAME}\");\n";
846 $res .= "\tndr->depth++;\n";
848 $res .= "\tif (flags & NDR_IN) {\n";
849 $res .= "\t\tndr_print_struct(ndr, \"in\", \"$fn->{NAME}\");\n";
850 $res .= "\tndr->depth++;\n";
851 foreach my $e (@{$fn->{DATA}}) {
852 if (util::has_property($e, "in")) {
853 ParseElementPrintScalar($e, "r->in.");
856 $res .= "\tndr->depth--;\n";
859 $res .= "\tif (flags & NDR_OUT) {\n";
860 $res .= "\t\tndr_print_struct(ndr, \"out\", \"$fn->{NAME}\");\n";
861 $res .= "\tndr->depth++;\n";
862 foreach my $e (@{$fn->{DATA}}) {
863 if (util::has_property($e, "out")) {
864 ParseElementPrintScalar($e, "r->out.");
867 if ($fn->{RETURN_TYPE} && $fn->{RETURN_TYPE} ne "void") {
868 $res .= "\tndr_print_$fn->{RETURN_TYPE}(ndr, \"result\", &r->out.result);\n";
870 $res .= "\tndr->depth--;\n";
873 $res .= "\tndr->depth--;\n";
878 #####################################################################
880 sub ParseFunctionPush($)
882 my($function) = shift;
885 $res .= "NTSTATUS ndr_push_$function->{NAME}(struct ndr_push *ndr, struct $function->{NAME} *r)\n{\n";
887 foreach my $e (@{$function->{DATA}}) {
888 if (util::has_property($e, "in")) {
889 $e->{PARENT} = $function;
890 if (util::array_size($e)) {
891 if (util::need_wire_pointer($e)) {
892 $res .= "\tNDR_CHECK(ndr_push_ptr(ndr, r->in.$e->{NAME}));\n";
894 $res .= "\tif (r->in.$e->{NAME}) {\n";
895 ParseArrayPush($e, "r->in.", "NDR_SCALARS|NDR_BUFFERS");
898 ParseElementPushScalar($e, "r->in.", "NDR_SCALARS|NDR_BUFFERS");
899 if ($e->{POINTERS}) {
900 ParseElementPushBuffer($e, "r->in.", "NDR_SCALARS|NDR_BUFFERS");
906 $res .= "\n\treturn NT_STATUS_OK;\n}\n\n";
909 #####################################################################
911 sub ParseFunctionPull($)
916 $res .= "NTSTATUS ndr_pull_$fn->{NAME}(struct ndr_pull *ndr, struct $fn->{NAME} *r)\n{\n";
918 # declare any internal pointers we need
919 foreach my $e (@{$fn->{DATA}}) {
920 if (util::has_property($e, "out")) {
921 if (util::need_wire_pointer($e)) {
922 $res .= "\tuint32 _ptr_$e->{NAME};\n";
927 foreach my $e (@{$fn->{DATA}}) {
928 if (util::has_property($e, "out")) {
930 if (util::array_size($e)) {
931 if (util::need_wire_pointer($e)) {
932 $res .= "\tNDR_CHECK(ndr_pull_uint32(ndr, _ptr_$e->{NAME}));\n";
933 $res .= "\tif (_ptr_$e->{NAME}) {\n";
935 $res .= "\tif (r->out.$e->{NAME}) {\n";
937 ParseArrayPull($e, "r->out.", "NDR_SCALARS|NDR_BUFFERS");
940 ParseElementPullScalar($e, "r->out.", "NDR_SCALARS|NDR_BUFFERS");
941 if ($e->{POINTERS}) {
942 ParseElementPullBuffer($e, "r->out.", "NDR_SCALARS|NDR_BUFFERS");
948 if ($fn->{RETURN_TYPE} && $fn->{RETURN_TYPE} ne "void") {
949 $res .= "\tNDR_CHECK(ndr_pull_$fn->{RETURN_TYPE}(ndr, &r->out.result));\n";
953 $res .= "\n\treturn NT_STATUS_OK;\n}\n\n";
956 #####################################################################
957 # parse the interface definitions
958 sub ParseInterface($)
960 my($interface) = shift;
961 my($data) = $interface->{DATA};
962 foreach my $d (@{$data}) {
963 ($d->{TYPE} eq "TYPEDEF") &&
964 ParseTypedefPush($d);
965 ($d->{TYPE} eq "FUNCTION") &&
966 ParseFunctionPush($d);
968 foreach my $d (@{$data}) {
969 ($d->{TYPE} eq "TYPEDEF") &&
970 ParseTypedefPull($d);
971 ($d->{TYPE} eq "FUNCTION") &&
972 ParseFunctionPull($d);
974 foreach my $d (@{$data}) {
975 if ($d->{TYPE} eq "TYPEDEF" &&
976 !util::has_property($d->{DATA}, "noprint")) {
977 ParseTypedefPrint($d);
979 if ($d->{TYPE} eq "FUNCTION" &&
980 !util::has_property($d, "noprint")) {
981 ParseFunctionPrint($d);
986 sub NeededFunction($)
989 $needed{"pull_$fn->{NAME}"} = 1;
990 $needed{"push_$fn->{NAME}"} = 1;
991 foreach my $e (@{$fn->{DATA}}) {
992 if (util::has_property($e, "out")) {
993 $needed{"pull_$e->{TYPE}"} = 1;
995 if (util::has_property($e, "in")) {
996 $needed{"push_$e->{TYPE}"} = 1;
1001 sub NeededTypedef($)
1004 if (util::has_property($t->{DATA}, "public")) {
1005 $needed{"pull_$t->{NAME}"} = 1;
1006 $needed{"push_$t->{NAME}"} = 1;
1008 if ($t->{DATA}->{TYPE} eq "STRUCT") {
1009 for my $e (@{$t->{DATA}->{ELEMENTS}}) {
1010 if ($needed{"pull_$t->{NAME}"}) {
1011 $needed{"pull_$e->{TYPE}"} = 1;
1013 if ($needed{"push_$t->{NAME}"}) {
1014 $needed{"push_$e->{TYPE}"} = 1;
1018 if ($t->{DATA}->{TYPE} eq "UNION") {
1019 for my $e (@{$t->{DATA}->{DATA}}) {
1020 if ($needed{"pull_$t->{NAME}"}) {
1021 $needed{"pull_$e->{DATA}->{TYPE}"} = 1;
1023 if ($needed{"push_$t->{NAME}"}) {
1024 $needed{"push_$e->{DATA}->{TYPE}"} = 1;
1030 #####################################################################
1031 # work out what parse functions are needed
1034 my($interface) = shift;
1035 my($data) = $interface->{DATA};
1036 foreach my $d (@{$data}) {
1037 ($d->{TYPE} eq "FUNCTION") &&
1040 foreach my $d (reverse @{$data}) {
1041 ($d->{TYPE} eq "TYPEDEF") &&
1047 #####################################################################
1048 # parse a parsed IDL structure back into an IDL file
1052 $res = "/* parser auto-generated by pidl */\n\n";
1053 $res .= "#include \"includes.h\"\n\n";
1054 foreach my $x (@{$idl}) {
1055 if ($x->{TYPE} eq "INTERFACE") {