pidl/NDR/Parser: simplify logic in DeclareArrayVariables*()
[Samba.git] / pidl / lib / Parse / Pidl / Samba4 / NDR / Parser.pm
blob31250014c39f96d17d4eb6b65d1d9477886ef4ad
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-2006
6 # released under the GNU GPL
8 package Parse::Pidl::Samba4::NDR::Parser;
10 require Exporter;
11 @ISA = qw(Exporter);
12 @EXPORT_OK = qw(check_null_pointer NeededFunction NeededElement NeededType $res NeededInterface TypeFunctionName ParseElementPrint);
14 use strict;
15 use Parse::Pidl::Typelist qw(hasType getType mapTypeName typeHasBody);
16 use Parse::Pidl::Util qw(has_property ParseExpr ParseExprExt print_uuid unmake_str);
17 use Parse::Pidl::CUtil qw(get_pointer_to get_value_of get_array_element);
18 use Parse::Pidl::NDR qw(GetPrevLevel GetNextLevel ContainsDeferred is_charset_array);
19 use Parse::Pidl::Samba4 qw(is_intree choose_header ArrayDynamicallyAllocated);
20 use Parse::Pidl::Samba4::Header qw(GenerateFunctionInEnv GenerateFunctionOutEnv EnvSubstituteValue GenerateStructEnv);
21 use Parse::Pidl qw(warning);
23 use vars qw($VERSION);
24 $VERSION = '0.01';
26 # list of known types
27 my %typefamily;
29 sub new($$) {
30 my ($class) = @_;
31 my $self = { res => "", res_hdr => "", deferred => [], tabs => "", defer_tabs => "" };
32 bless($self, $class);
35 sub get_typefamily($)
37 my $n = shift;
38 return $typefamily{$n};
41 sub append_prefix($$)
43 my ($e, $var_name) = @_;
44 my $pointers = 0;
45 my $arrays = 0;
47 foreach my $l (@{$e->{LEVELS}}) {
48 if ($l->{TYPE} eq "POINTER") {
49 $pointers++;
50 } elsif ($l->{TYPE} eq "ARRAY") {
51 $arrays++;
52 if (($pointers == 0) and
53 (not $l->{IS_FIXED}) and
54 (not $l->{IS_INLINE})) {
55 return get_value_of($var_name);
57 } elsif ($l->{TYPE} eq "DATA") {
58 if (Parse::Pidl::Typelist::scalar_is_reference($l->{DATA_TYPE})) {
59 return get_value_of($var_name) unless ($pointers or $arrays);
64 return $var_name;
67 sub has_fast_array($$)
69 my ($e,$l) = @_;
71 return 0 if ($l->{TYPE} ne "ARRAY");
73 my $nl = GetNextLevel($e,$l);
74 return 0 unless ($nl->{TYPE} eq "DATA");
75 return 0 unless (hasType($nl->{DATA_TYPE}));
77 my $t = getType($nl->{DATA_TYPE});
79 # Only uint8 and string have fast array functions at the moment
80 return ($t->{NAME} eq "uint8") or ($t->{NAME} eq "string");
84 ####################################
85 # pidl() is our basic output routine
86 sub pidl($$)
88 my ($self, $d) = @_;
89 if ($d) {
90 $self->{res} .= $self->{tabs};
91 $self->{res} .= $d;
93 $self->{res} .="\n";
96 sub pidl_hdr($$) { my ($self, $d) = @_; $self->{res_hdr} .= "$d\n"; }
98 ####################################
99 # defer() is like pidl(), but adds to
100 # a deferred buffer which is then added to the
101 # output buffer at the end of the structure/union/function
102 # This is needed to cope with code that must be pushed back
103 # to the end of a block of elements
104 sub defer_indent($) { my ($self) = @_; $self->{defer_tabs}.="\t"; }
105 sub defer_deindent($) { my ($self) = @_; $self->{defer_tabs}=substr($self->{defer_tabs}, 0, -1); }
107 sub defer($$)
109 my ($self, $d) = @_;
110 if ($d) {
111 push(@{$self->{deferred}}, $self->{defer_tabs}.$d);
115 ########################################
116 # add the deferred content to the current
117 # output
118 sub add_deferred($)
120 my ($self) = @_;
121 $self->pidl($_) foreach (@{$self->{deferred}});
122 $self->{deferred} = [];
123 $self->{defer_tabs} = "";
126 sub indent($)
128 my ($self) = @_;
129 $self->{tabs} .= "\t";
132 sub deindent($)
134 my ($self) = @_;
135 $self->{tabs} = substr($self->{tabs}, 0, -1);
138 #####################################################################
139 # declare a function public or static, depending on its attributes
140 sub fn_declare($$$$)
142 my ($self,$type,$fn,$decl) = @_;
144 if (has_property($fn, "no$type")) {
145 $self->pidl_hdr("$decl;");
146 return 0;
149 if (has_property($fn, "public")) {
150 $self->pidl_hdr("$decl;");
151 $self->pidl("_PUBLIC_ $decl");
152 } else {
153 $self->pidl("static $decl");
156 return 1;
159 ###################################################################
160 # setup any special flags for an element or structure
161 sub start_flags($$$)
163 my ($self, $e, $ndr) = @_;
164 my $flags = has_property($e, "flag");
165 if (defined $flags) {
166 $self->pidl("{");
167 $self->indent;
168 $self->pidl("uint32_t _flags_save_$e->{TYPE} = $ndr->flags;");
169 $self->pidl("ndr_set_flags(&$ndr->flags, $flags);");
173 ###################################################################
174 # end any special flags for an element or structure
175 sub end_flags($$$)
177 my ($self, $e, $ndr) = @_;
178 my $flags = has_property($e, "flag");
179 if (defined $flags) {
180 $self->pidl("$ndr->flags = _flags_save_$e->{TYPE};");
181 $self->deindent;
182 $self->pidl("}");
186 #####################################################################
187 # parse the data of an array - push side
188 sub ParseArrayPushHeader($$$$$$)
190 my ($self,$e,$l,$ndr,$var_name,$env) = @_;
192 my $size;
193 my $length;
195 if ($l->{IS_ZERO_TERMINATED}) {
196 if (has_property($e, "charset")) {
197 $size = $length = "ndr_charset_length($var_name, CH_$e->{PROPERTIES}->{charset})";
198 } else {
199 $size = $length = "ndr_string_length($var_name, sizeof(*$var_name))";
201 } else {
202 $size = ParseExpr($l->{SIZE_IS}, $env, $e);
203 $length = ParseExpr($l->{LENGTH_IS}, $env, $e);
206 if ((!$l->{IS_SURROUNDING}) and $l->{IS_CONFORMANT}) {
207 $self->pidl("NDR_CHECK(ndr_push_uint32($ndr, NDR_SCALARS, $size));");
210 if ($l->{IS_VARYING}) {
211 $self->pidl("NDR_CHECK(ndr_push_uint32($ndr, NDR_SCALARS, 0));"); # array offset
212 $self->pidl("NDR_CHECK(ndr_push_uint32($ndr, NDR_SCALARS, $length));");
215 return $length;
218 sub check_fully_dereferenced($$)
220 my ($element, $env) = @_;
222 return sub ($) {
223 my $origvar = shift;
224 my $check = 0;
226 # Figure out the number of pointers in $ptr
227 my $expandedvar = $origvar;
228 $expandedvar =~ s/^(\**)//;
229 my $ptr = $1;
231 my $var = undef;
232 foreach (keys %$env) {
233 if ($env->{$_} eq $expandedvar) {
234 $var = $_;
235 last;
239 return($origvar) unless (defined($var));
240 my $e;
241 foreach (@{$element->{PARENT}->{ELEMENTS}}) {
242 if ($_->{NAME} eq $var) {
243 $e = $_;
244 last;
248 $e or die("Environment doesn't match siblings");
250 # See if pointer at pointer level $level
251 # needs to be checked.
252 my $nump = 0;
253 foreach (@{$e->{LEVELS}}) {
254 if ($_->{TYPE} eq "POINTER") {
255 $nump = $_->{POINTER_INDEX}+1;
258 warning($element->{ORIGINAL}, "Got pointer for `$e->{NAME}', expected fully derefenced variable") if ($nump > length($ptr));
259 return ($origvar);
263 sub check_null_pointer($$$$)
265 my ($element, $env, $print_fn, $return) = @_;
267 return sub ($) {
268 my $expandedvar = shift;
269 my $check = 0;
271 # Figure out the number of pointers in $ptr
272 $expandedvar =~ s/^(\**)//;
273 my $ptr = $1;
275 my $var = undef;
276 foreach (keys %$env) {
277 if ($env->{$_} eq $expandedvar) {
278 $var = $_;
279 last;
283 if (defined($var)) {
284 my $e;
285 # lookup ptr in $e
286 foreach (@{$element->{PARENT}->{ELEMENTS}}) {
287 if ($_->{NAME} eq $var) {
288 $e = $_;
289 last;
293 $e or die("Environment doesn't match siblings");
295 # See if pointer at pointer level $level
296 # needs to be checked.
297 foreach my $l (@{$e->{LEVELS}}) {
298 if ($l->{TYPE} eq "POINTER" and
299 $l->{POINTER_INDEX} == length($ptr)) {
300 # No need to check ref pointers
301 $check = ($l->{POINTER_TYPE} ne "ref");
302 last;
305 if ($l->{TYPE} eq "DATA") {
306 warning($element, "too much dereferences for `$var'");
309 } else {
310 warning($element, "unknown dereferenced expression `$expandedvar'");
311 $check = 1;
314 $print_fn->("if ($ptr$expandedvar == NULL) $return") if $check;
318 #####################################################################
319 # parse an array - pull side
320 sub ParseArrayPullHeader($$$$$$)
322 my ($self,$e,$l,$ndr,$var_name,$env) = @_;
324 my $length;
325 my $size;
327 if ($l->{IS_CONFORMANT}) {
328 $length = $size = "ndr_get_array_size($ndr, " . get_pointer_to($var_name) . ")";
329 } elsif ($l->{IS_ZERO_TERMINATED} and $l->{SIZE_IS} == 0 and $l->{LENGTH_IS} == 0) { # Noheader arrays
330 $length = $size = "ndr_get_string_size($ndr, sizeof(*$var_name))";
331 } else {
332 $length = $size = ParseExprExt($l->{SIZE_IS}, $env, $e->{ORIGINAL},
333 check_null_pointer($e, $env, sub { $self->pidl(shift); },
334 "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for size_is()\");"),
335 check_fully_dereferenced($e, $env));
338 if ((!$l->{IS_SURROUNDING}) and $l->{IS_CONFORMANT}) {
339 $self->pidl("NDR_CHECK(ndr_pull_array_size($ndr, " . get_pointer_to($var_name) . "));");
342 if ($l->{IS_VARYING}) {
343 $self->pidl("NDR_CHECK(ndr_pull_array_length($ndr, " . get_pointer_to($var_name) . "));");
344 $length = "ndr_get_array_length($ndr, " . get_pointer_to($var_name) .")";
347 if ($length ne $size) {
348 $self->pidl("if ($length > $size) {");
349 $self->indent;
350 $self->pidl("return ndr_pull_error($ndr, NDR_ERR_ARRAY_SIZE, \"Bad array size %u should exceed array length %u\", $size, $length);");
351 $self->deindent;
352 $self->pidl("}");
355 if ($l->{IS_CONFORMANT} and not $l->{IS_ZERO_TERMINATED}) {
356 $self->defer("if ($var_name) {");
357 $self->defer_indent;
358 my $size = ParseExprExt($l->{SIZE_IS}, $env, $e->{ORIGINAL},
359 check_null_pointer($e, $env, sub { $self->defer(shift); },
360 "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for size_is()\");"),
361 check_fully_dereferenced($e, $env));
362 $self->defer("NDR_CHECK(ndr_check_array_size($ndr, (void*)" . get_pointer_to($var_name) . ", $size));");
363 $self->defer_deindent;
364 $self->defer("}");
367 if ($l->{IS_VARYING} and not $l->{IS_ZERO_TERMINATED}) {
368 $self->defer("if ($var_name) {");
369 $self->defer_indent;
370 my $length = ParseExprExt($l->{LENGTH_IS}, $env, $e->{ORIGINAL},
371 check_null_pointer($e, $env, sub { $self->defer(shift); },
372 "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for length_is()\");"),
373 check_fully_dereferenced($e, $env));
374 $self->defer("NDR_CHECK(ndr_check_array_length($ndr, (void*)" . get_pointer_to($var_name) . ", $length));");
375 $self->defer_deindent;
376 $self->defer("}");
379 if (ArrayDynamicallyAllocated($e,$l) and not is_charset_array($e,$l)) {
380 $self->AllocateArrayLevel($e,$l,$ndr,$var_name,$size);
383 return $length;
386 sub compression_alg($$)
388 my ($e, $l) = @_;
389 my ($alg, $clen, $dlen) = split(/,/, $l->{COMPRESSION});
391 return $alg;
394 sub compression_clen($$$)
396 my ($e, $l, $env) = @_;
397 my ($alg, $clen, $dlen) = split(/,/, $l->{COMPRESSION});
399 return ParseExpr($clen, $env, $e->{ORIGINAL});
402 sub compression_dlen($$$)
404 my ($e,$l,$env) = @_;
405 my ($alg, $clen, $dlen) = split(/,/, $l->{COMPRESSION});
407 return ParseExpr($dlen, $env, $e->{ORIGINAL});
410 sub ParseCompressionPushStart($$$$$)
412 my ($self,$e,$l,$ndr,$env) = @_;
413 my $comndr = "$ndr\_compressed";
414 my $alg = compression_alg($e, $l);
415 my $dlen = compression_dlen($e, $l, $env);
417 $self->pidl("{");
418 $self->indent;
419 $self->pidl("struct ndr_push *$comndr;");
420 $self->pidl("NDR_CHECK(ndr_push_compression_start($ndr, &$comndr, $alg, $dlen));");
422 return $comndr;
425 sub ParseCompressionPushEnd($$$$$)
427 my ($self,$e,$l,$ndr,$env) = @_;
428 my $comndr = "$ndr\_compressed";
429 my $alg = compression_alg($e, $l);
430 my $dlen = compression_dlen($e, $l, $env);
432 $self->pidl("NDR_CHECK(ndr_push_compression_end($ndr, $comndr, $alg, $dlen));");
433 $self->deindent;
434 $self->pidl("}");
437 sub ParseCompressionPullStart($$$$$)
439 my ($self,$e,$l,$ndr,$env) = @_;
440 my $comndr = "$ndr\_compressed";
441 my $alg = compression_alg($e, $l);
442 my $dlen = compression_dlen($e, $l, $env);
444 $self->pidl("{");
445 $self->indent;
446 $self->pidl("struct ndr_pull *$comndr;");
447 $self->pidl("NDR_CHECK(ndr_pull_compression_start($ndr, &$comndr, $alg, $dlen));");
449 return $comndr;
452 sub ParseCompressionPullEnd($$$$$)
454 my ($self,$e,$l,$ndr,$env) = @_;
455 my $comndr = "$ndr\_compressed";
456 my $alg = compression_alg($e, $l);
457 my $dlen = compression_dlen($e, $l, $env);
459 $self->pidl("NDR_CHECK(ndr_pull_compression_end($ndr, $comndr, $alg, $dlen));");
460 $self->deindent;
461 $self->pidl("}");
464 sub ParseSubcontextPushStart($$$$$)
466 my ($self,$e,$l,$ndr,$env) = @_;
467 my $subndr = "_ndr_$e->{NAME}";
468 my $subcontext_size = ParseExpr($l->{SUBCONTEXT_SIZE}, $env, $e->{ORIGINAL});
470 $self->pidl("{");
471 $self->indent;
472 $self->pidl("struct ndr_push *$subndr;");
473 $self->pidl("NDR_CHECK(ndr_push_subcontext_start($ndr, &$subndr, $l->{HEADER_SIZE}, $subcontext_size));");
475 if (defined $l->{COMPRESSION}) {
476 $subndr = $self->ParseCompressionPushStart($e, $l, $subndr, $env);
479 return $subndr;
482 sub ParseSubcontextPushEnd($$$$$)
484 my ($self,$e,$l,$ndr,$env) = @_;
485 my $subndr = "_ndr_$e->{NAME}";
486 my $subcontext_size = ParseExpr($l->{SUBCONTEXT_SIZE}, $env, $e->{ORIGINAL});
488 if (defined $l->{COMPRESSION}) {
489 $self->ParseCompressionPushEnd($e, $l, $subndr, $env);
492 $self->pidl("NDR_CHECK(ndr_push_subcontext_end($ndr, $subndr, $l->{HEADER_SIZE}, $subcontext_size));");
493 $self->deindent;
494 $self->pidl("}");
497 sub ParseSubcontextPullStart($$$$$)
499 my ($self,$e,$l,$ndr,$env) = @_;
500 my $subndr = "_ndr_$e->{NAME}";
501 my $subcontext_size = ParseExpr($l->{SUBCONTEXT_SIZE}, $env, $e->{ORIGINAL});
503 $self->pidl("{");
504 $self->indent;
505 $self->pidl("struct ndr_pull *$subndr;");
506 $self->pidl("NDR_CHECK(ndr_pull_subcontext_start($ndr, &$subndr, $l->{HEADER_SIZE}, $subcontext_size));");
508 if (defined $l->{COMPRESSION}) {
509 $subndr = $self->ParseCompressionPullStart($e, $l, $subndr, $env);
512 return $subndr;
515 sub ParseSubcontextPullEnd($$$$$)
517 my ($self,$e,$l,$ndr,$env) = @_;
518 my $subndr = "_ndr_$e->{NAME}";
519 my $subcontext_size = ParseExpr($l->{SUBCONTEXT_SIZE}, $env, $e->{ORIGINAL});
521 if (defined $l->{COMPRESSION}) {
522 $self->ParseCompressionPullEnd($e, $l, $subndr, $env);
525 $self->pidl("NDR_CHECK(ndr_pull_subcontext_end($ndr, $subndr, $l->{HEADER_SIZE}, $subcontext_size));");
526 $self->deindent;
527 $self->pidl("}");
530 sub ParseElementPushLevel
532 my ($self,$e,$l,$ndr,$var_name,$env,$primitives,$deferred) = @_;
534 my $ndr_flags = CalcNdrFlags($l, $primitives, $deferred);
536 if ($l->{TYPE} eq "ARRAY" and ($l->{IS_CONFORMANT} or $l->{IS_VARYING})) {
537 $var_name = get_pointer_to($var_name);
540 if (defined($ndr_flags)) {
541 if ($l->{TYPE} eq "SUBCONTEXT") {
542 my $subndr = $self->ParseSubcontextPushStart($e, $l, $ndr, $env);
543 $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $subndr, $var_name, $env, 1, 1);
544 $self->ParseSubcontextPushEnd($e, $l, $ndr, $env);
545 } elsif ($l->{TYPE} eq "POINTER") {
546 $self->ParsePtrPush($e, $l, $ndr, $var_name);
547 } elsif ($l->{TYPE} eq "ARRAY") {
548 my $length = $self->ParseArrayPushHeader($e, $l, $ndr, $var_name, $env);
550 my $nl = GetNextLevel($e, $l);
552 # Allow speedups for arrays of scalar types
553 if (is_charset_array($e,$l)) {
554 $self->pidl("NDR_CHECK(ndr_push_charset($ndr, $ndr_flags, $var_name, $length, sizeof(" . mapTypeName($nl->{DATA_TYPE}) . "), CH_$e->{PROPERTIES}->{charset}));");
555 return;
556 } elsif (has_fast_array($e,$l)) {
557 $self->pidl("NDR_CHECK(ndr_push_array_$nl->{DATA_TYPE}($ndr, $ndr_flags, $var_name, $length));");
558 return;
560 } elsif ($l->{TYPE} eq "SWITCH") {
561 $self->ParseSwitchPush($e, $l, $ndr, $var_name, $env);
562 } elsif ($l->{TYPE} eq "DATA") {
563 $self->ParseDataPush($e, $l, $ndr, $var_name, $primitives, $deferred);
564 } elsif ($l->{TYPE} eq "TYPEDEF") {
565 $typefamily{$e->{DATA}->{TYPE}}->{PUSH_FN_BODY}->($self, $e->{DATA}, $ndr, $var_name);
569 if ($l->{TYPE} eq "POINTER" and $deferred) {
570 my $rel_var_name = $var_name;
571 if ($l->{POINTER_TYPE} ne "ref") {
572 $self->pidl("if ($var_name) {");
573 $self->indent;
574 if ($l->{POINTER_TYPE} eq "relative") {
575 $self->pidl("NDR_CHECK(ndr_push_relative_ptr2_start($ndr, $rel_var_name));");
578 $var_name = get_value_of($var_name);
579 $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, 1, 1);
581 if ($l->{POINTER_TYPE} ne "ref") {
582 if ($l->{POINTER_TYPE} eq "relative") {
583 $self->pidl("NDR_CHECK(ndr_push_relative_ptr2_end($ndr, $rel_var_name));");
585 $self->deindent;
586 $self->pidl("}");
588 } elsif ($l->{TYPE} eq "ARRAY" and not has_fast_array($e,$l) and
589 not is_charset_array($e, $l)) {
590 my $length = ParseExpr($l->{LENGTH_IS}, $env, $e->{ORIGINAL});
591 my $counter = "cntr_$e->{NAME}_$l->{LEVEL_INDEX}";
593 my $array_pointless = ($length eq "0");
595 if ($array_pointless) {
596 warning($e->{ORIGINAL}, "pointless array `$e->{NAME}' will always have size 0");
599 $var_name = get_array_element($var_name, $counter);
601 if ((($primitives and not $l->{IS_DEFERRED}) or ($deferred and $l->{IS_DEFERRED})) and not $array_pointless) {
602 $self->pidl("for ($counter = 0; $counter < $length; $counter++) {");
603 $self->indent;
604 $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, 1, 0);
605 $self->deindent;
606 $self->pidl("}");
609 if ($deferred and ContainsDeferred($e, $l) and not $array_pointless) {
610 $self->pidl("for ($counter = 0; $counter < $length; $counter++) {");
611 $self->indent;
612 $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, 0, 1);
613 $self->deindent;
614 $self->pidl("}");
616 } elsif ($l->{TYPE} eq "SWITCH") {
617 $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, $primitives, $deferred);
621 #####################################################################
622 # parse scalars in a structure element
623 sub ParseElementPush($$$$$$)
625 my ($self,$e,$ndr,$env,$primitives,$deferred) = @_;
626 my $subndr = undef;
628 my $var_name = $env->{$e->{NAME}};
630 return unless $primitives or ($deferred and ContainsDeferred($e, $e->{LEVELS}[0]));
632 # Representation type is different from transmit_as
633 if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
634 $self->pidl("{");
635 $self->indent;
636 my $transmit_name = "_transmit_$e->{NAME}";
637 $self->pidl(mapTypeName($e->{TYPE}) ." $transmit_name;");
638 $self->pidl("NDR_CHECK(ndr_$e->{REPRESENTATION_TYPE}_to_$e->{TYPE}($var_name, " . get_pointer_to($transmit_name) . "));");
639 $var_name = $transmit_name;
642 $var_name = append_prefix($e, $var_name);
644 $self->start_flags($e, $ndr);
646 if (defined(my $value = has_property($e, "value"))) {
647 $var_name = ParseExpr($value, $env, $e->{ORIGINAL});
650 $self->ParseElementPushLevel($e, $e->{LEVELS}[0], $ndr, $var_name, $env, $primitives, $deferred);
652 $self->end_flags($e, $ndr);
654 if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
655 $self->deindent;
656 $self->pidl("}");
660 #####################################################################
661 # parse a pointer in a struct element or function
662 sub ParsePtrPush($$$$$)
664 my ($self,$e,$l,$ndr,$var_name) = @_;
666 if ($l->{POINTER_TYPE} eq "ref") {
667 $self->pidl("if ($var_name == NULL) {");
668 $self->indent;
669 $self->pidl("return ndr_push_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL [ref] pointer\");");
670 $self->deindent;
671 $self->pidl("}");
672 if ($l->{LEVEL} eq "EMBEDDED") {
673 $self->pidl("NDR_CHECK(ndr_push_ref_ptr(ndr));");
675 } elsif ($l->{POINTER_TYPE} eq "relative") {
676 $self->pidl("NDR_CHECK(ndr_push_relative_ptr1($ndr, $var_name));");
677 } elsif ($l->{POINTER_TYPE} eq "unique") {
678 $self->pidl("NDR_CHECK(ndr_push_unique_ptr($ndr, $var_name));");
679 } elsif ($l->{POINTER_TYPE} eq "full") {
680 $self->pidl("NDR_CHECK(ndr_push_full_ptr($ndr, $var_name));");
681 } else {
682 die("Unhandled pointer type $l->{POINTER_TYPE}");
686 sub need_pointer_to($$$)
688 my ($e, $l, $scalar_only) = @_;
690 my $t;
691 if (ref($l->{DATA_TYPE})) {
692 $t = "$l->{DATA_TYPE}->{TYPE}_$l->{DATA_TYPE}->{NAME}";
693 } else {
694 $t = $l->{DATA_TYPE};
697 if (not Parse::Pidl::Typelist::is_scalar($t)) {
698 return 1 if $scalar_only;
701 my $arrays = 0;
703 foreach my $tl (@{$e->{LEVELS}}) {
704 last if $l == $tl;
705 if ($tl->{TYPE} eq "ARRAY") {
706 $arrays++;
710 if (Parse::Pidl::Typelist::scalar_is_reference($t)) {
711 return 1 unless $arrays;
714 return 0;
717 sub ParseDataPrint($$$$$)
719 my ($self, $e, $l, $ndr, $var_name) = @_;
721 if (not ref($l->{DATA_TYPE}) or defined($l->{DATA_TYPE}->{NAME})) {
723 if (need_pointer_to($e, $l, 1)) {
724 $var_name = get_pointer_to($var_name);
727 $self->pidl(TypeFunctionName("ndr_print", $l->{DATA_TYPE})."($ndr, \"$e->{NAME}\", $var_name);");
728 } else {
729 $self->ParseTypePrint($l->{DATA_TYPE}, $ndr, $var_name);
733 #####################################################################
734 # print scalars in a structure element
735 sub ParseElementPrint($$$$$)
737 my($self, $e, $ndr, $var_name, $env) = @_;
739 return if (has_property($e, "noprint"));
741 if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
742 $self->pidl("ndr_print_$e->{REPRESENTATION_TYPE}($ndr, \"$e->{NAME}\", $var_name);");
743 return;
746 $var_name = append_prefix($e, $var_name);
748 if (defined(my $value = has_property($e, "value"))) {
749 $var_name = "($ndr->flags & LIBNDR_PRINT_SET_VALUES)?" . ParseExpr($value,$env, $e->{ORIGINAL}) . ":$var_name";
752 foreach my $l (@{$e->{LEVELS}}) {
753 if ($l->{TYPE} eq "POINTER") {
754 $self->pidl("ndr_print_ptr($ndr, \"$e->{NAME}\", $var_name);");
755 $self->pidl("$ndr->depth++;");
756 if ($l->{POINTER_TYPE} ne "ref") {
757 $self->pidl("if ($var_name) {");
758 $self->indent;
760 $var_name = get_value_of($var_name);
761 } elsif ($l->{TYPE} eq "ARRAY") {
762 my $length;
764 if ($l->{IS_CONFORMANT} or $l->{IS_VARYING}) {
765 $var_name = get_pointer_to($var_name);
768 if ($l->{IS_ZERO_TERMINATED}) {
769 $length = "ndr_string_length($var_name, sizeof(*$var_name))";
770 } else {
771 $length = ParseExprExt($l->{LENGTH_IS}, $env, $e->{ORIGINAL},
772 check_null_pointer($e, $env, sub { $self->pidl(shift); }, "return;"), check_fully_dereferenced($e, $env));
775 if (is_charset_array($e,$l)) {
776 $self->pidl("ndr_print_string($ndr, \"$e->{NAME}\", $var_name);");
777 last;
778 } elsif (has_fast_array($e, $l)) {
779 my $nl = GetNextLevel($e, $l);
780 $self->pidl("ndr_print_array_$nl->{DATA_TYPE}($ndr, \"$e->{NAME}\", $var_name, $length);");
781 last;
782 } else {
783 my $counter = "cntr_$e->{NAME}_$l->{LEVEL_INDEX}";
785 $self->pidl("$ndr->print($ndr, \"\%s: ARRAY(\%d)\", \"$e->{NAME}\", (int)$length);");
786 $self->pidl("$ndr->depth++;");
787 $self->pidl("for ($counter=0;$counter<$length;$counter++) {");
788 $self->indent;
789 $self->pidl("char *idx_$l->{LEVEL_INDEX}=NULL;");
790 $self->pidl("if (asprintf(&idx_$l->{LEVEL_INDEX}, \"[\%d]\", $counter) != -1) {");
791 $self->indent;
793 $var_name = get_array_element($var_name, $counter);
795 } elsif ($l->{TYPE} eq "DATA") {
796 $self->ParseDataPrint($e, $l, $ndr, $var_name);
797 } elsif ($l->{TYPE} eq "SWITCH") {
798 my $switch_var = ParseExprExt($l->{SWITCH_IS}, $env, $e->{ORIGINAL},
799 check_null_pointer($e, $env, sub { $self->pidl(shift); }, "return;"), check_fully_dereferenced($e, $env));
800 $self->pidl("ndr_print_set_switch_value($ndr, " . get_pointer_to($var_name) . ", $switch_var);");
804 foreach my $l (reverse @{$e->{LEVELS}}) {
805 if ($l->{TYPE} eq "POINTER") {
806 if ($l->{POINTER_TYPE} ne "ref") {
807 $self->deindent;
808 $self->pidl("}");
810 $self->pidl("$ndr->depth--;");
811 } elsif (($l->{TYPE} eq "ARRAY")
812 and not is_charset_array($e,$l)
813 and not has_fast_array($e,$l)) {
814 $self->pidl("free(idx_$l->{LEVEL_INDEX});");
815 $self->deindent;
816 $self->pidl("}");
817 $self->deindent;
818 $self->pidl("}");
819 $self->pidl("$ndr->depth--;");
824 #####################################################################
825 # parse scalars in a structure element - pull size
826 sub ParseSwitchPull($$$$$$)
828 my($self,$e,$l,$ndr,$var_name,$env) = @_;
829 my $switch_var = ParseExprExt($l->{SWITCH_IS}, $env, $e->{ORIGINAL},
830 check_null_pointer($e, $env, sub { $self->pidl(shift); },
831 "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for switch_is()\");"),
832 check_fully_dereferenced($e, $env));
834 $var_name = get_pointer_to($var_name);
835 $self->pidl("NDR_CHECK(ndr_pull_set_switch_value($ndr, $var_name, $switch_var));");
838 #####################################################################
839 # push switch element
840 sub ParseSwitchPush($$$$$$)
842 my($self,$e,$l,$ndr,$var_name,$env) = @_;
843 my $switch_var = ParseExprExt($l->{SWITCH_IS}, $env, $e->{ORIGINAL},
844 check_null_pointer($e, $env, sub { $self->pidl(shift); },
845 "return ndr_push_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for switch_is()\");"),
846 check_fully_dereferenced($e, $env));
848 $var_name = get_pointer_to($var_name);
849 $self->pidl("NDR_CHECK(ndr_push_set_switch_value($ndr, $var_name, $switch_var));");
852 sub ParseDataPull($$$$$$$)
854 my ($self,$e,$l,$ndr,$var_name,$primitives,$deferred) = @_;
856 if (not ref($l->{DATA_TYPE}) or defined($l->{DATA_TYPE}->{NAME})) {
858 my $ndr_flags = CalcNdrFlags($l, $primitives, $deferred);
860 if (need_pointer_to($e, $l, 0)) {
861 $var_name = get_pointer_to($var_name);
864 $var_name = get_pointer_to($var_name);
866 $self->pidl("NDR_CHECK(".TypeFunctionName("ndr_pull", $l->{DATA_TYPE})."($ndr, $ndr_flags, $var_name));");
868 my $pl = GetPrevLevel($e, $l);
870 my $range = has_property($e, "range");
871 if ($range and $pl->{TYPE} ne "ARRAY") {
872 $var_name = get_value_of($var_name);
873 my $signed = Parse::Pidl::Typelist::is_signed($l->{DATA_TYPE});
874 my ($low, $high) = split(/,/, $range, 2);
875 if ($low < 0 and not $signed) {
876 warning(0, "$low is invalid for the range of an unsigned type");
878 if ($low == 0 and not $signed) {
879 $self->pidl("if ($var_name > $high) {");
880 } else {
881 $self->pidl("if ($var_name < $low || $var_name > $high) {");
883 $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_RANGE, \"value out of range\");");
884 $self->pidl("}");
886 } else {
887 $self->ParseTypePull($l->{DATA_TYPE}, $ndr, $var_name, $primitives, $deferred);
891 sub ParseDataPush($$$$$$$)
893 my ($self,$e,$l,$ndr,$var_name,$primitives,$deferred) = @_;
895 if (not ref($l->{DATA_TYPE}) or defined($l->{DATA_TYPE}->{NAME})) {
897 my $ndr_flags = CalcNdrFlags($l, $primitives, $deferred);
899 # strings are passed by value rather than reference
900 if (need_pointer_to($e, $l, 1)) {
901 $var_name = get_pointer_to($var_name);
904 $self->pidl("NDR_CHECK(".TypeFunctionName("ndr_push", $l->{DATA_TYPE})."($ndr, $ndr_flags, $var_name));");
905 } else {
906 $self->ParseTypePush($l->{DATA_TYPE}, $ndr, $var_name, $primitives, $deferred);
910 sub CalcNdrFlags($$$)
912 my ($l,$primitives,$deferred) = @_;
914 my $scalars = 0;
915 my $buffers = 0;
917 # Add NDR_SCALARS if this one is deferred
918 # and deferreds may be pushed
919 $scalars = 1 if ($l->{IS_DEFERRED} and $deferred);
921 # Add NDR_SCALARS if this one is not deferred and
922 # primitives may be pushed
923 $scalars = 1 if (!$l->{IS_DEFERRED} and $primitives);
925 # Add NDR_BUFFERS if this one contains deferred stuff
926 # and deferreds may be pushed
927 $buffers = 1 if ($l->{CONTAINS_DEFERRED} and $deferred);
929 return "NDR_SCALARS|NDR_BUFFERS" if ($scalars and $buffers);
930 return "NDR_SCALARS" if ($scalars);
931 return "NDR_BUFFERS" if ($buffers);
932 return undef;
935 sub ParseMemCtxPullFlags($$$$)
937 my ($self, $e, $l) = @_;
939 return undef unless ($l->{TYPE} eq "POINTER" or $l->{TYPE} eq "ARRAY");
941 return undef unless ($l->{TYPE} ne "ARRAY" or ArrayDynamicallyAllocated($e,$l));
942 return undef if has_fast_array($e, $l);
943 return undef if is_charset_array($e, $l);
945 my $mem_flags = "0";
947 if (($l->{TYPE} eq "POINTER") and ($l->{POINTER_TYPE} eq "ref")) {
948 my $nl = GetNextLevel($e, $l);
949 my $next_is_array = ($nl->{TYPE} eq "ARRAY");
950 my $next_is_string = (($nl->{TYPE} eq "DATA") and
951 ($nl->{DATA_TYPE} eq "string"));
952 if ($next_is_array or $next_is_string) {
953 return undef;
954 } elsif ($l->{LEVEL} eq "TOP") {
955 $mem_flags = "LIBNDR_FLAG_REF_ALLOC";
959 return $mem_flags;
962 sub ParseMemCtxPullStart($$$$$)
964 my ($self, $e, $l, $ndr, $ptr_name) = @_;
966 my $mem_r_ctx = "_mem_save_$e->{NAME}_$l->{LEVEL_INDEX}";
967 my $mem_c_ctx = $ptr_name;
968 my $mem_c_flags = $self->ParseMemCtxPullFlags($e, $l);
970 return unless defined($mem_c_flags);
972 $self->pidl("$mem_r_ctx = NDR_PULL_GET_MEM_CTX($ndr);");
973 $self->pidl("NDR_PULL_SET_MEM_CTX($ndr, $mem_c_ctx, $mem_c_flags);");
976 sub ParseMemCtxPullEnd($$$$)
978 my ($self, $e, $l, $ndr) = @_;
980 my $mem_r_ctx = "_mem_save_$e->{NAME}_$l->{LEVEL_INDEX}";
981 my $mem_r_flags = $self->ParseMemCtxPullFlags($e, $l);
983 return unless defined($mem_r_flags);
985 $self->pidl("NDR_PULL_SET_MEM_CTX($ndr, $mem_r_ctx, $mem_r_flags);");
988 sub CheckStringTerminator($$$$$)
990 my ($self,$ndr,$e,$l,$length) = @_;
991 my $nl = GetNextLevel($e, $l);
993 # Make sure last element is zero!
994 $self->pidl("NDR_CHECK(ndr_check_string_terminator($ndr, $length, sizeof($nl->{DATA_TYPE}_t)));");
997 sub ParseElementPullLevel
999 my($self,$e,$l,$ndr,$var_name,$env,$primitives,$deferred) = @_;
1001 my $ndr_flags = CalcNdrFlags($l, $primitives, $deferred);
1003 if ($l->{TYPE} eq "ARRAY" and ($l->{IS_VARYING} or $l->{IS_CONFORMANT})) {
1004 $var_name = get_pointer_to($var_name);
1007 # Only pull something if there's actually something to be pulled
1008 if (defined($ndr_flags)) {
1009 if ($l->{TYPE} eq "SUBCONTEXT") {
1010 my $subndr = $self->ParseSubcontextPullStart($e, $l, $ndr, $env);
1011 $self->ParseElementPullLevel($e, GetNextLevel($e,$l), $subndr, $var_name, $env, 1, 1);
1012 $self->ParseSubcontextPullEnd($e, $l, $ndr, $env);
1013 } elsif ($l->{TYPE} eq "ARRAY") {
1014 my $length = $self->ParseArrayPullHeader($e, $l, $ndr, $var_name, $env);
1016 if (my $range = has_property($e, "range")) {
1017 my ($low, $high) = split(/,/, $range, 2);
1018 if ($low < 0) {
1019 warning(0, "$low is invalid for the range of an array size");
1021 if ($low == 0) {
1022 $self->pidl("if ($length > $high) {");
1023 } else {
1024 $self->pidl("if ($length < $low || $length > $high) {");
1026 $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_RANGE, \"value out of range\");");
1027 $self->pidl("}");
1030 my $nl = GetNextLevel($e, $l);
1032 if (is_charset_array($e,$l)) {
1033 if ($l->{IS_ZERO_TERMINATED}) {
1034 $self->CheckStringTerminator($ndr, $e, $l, $length);
1036 $self->pidl("NDR_CHECK(ndr_pull_charset($ndr, $ndr_flags, ".get_pointer_to($var_name).", $length, sizeof(" . mapTypeName($nl->{DATA_TYPE}) . "), CH_$e->{PROPERTIES}->{charset}));");
1037 return;
1038 } elsif (has_fast_array($e, $l)) {
1039 if ($l->{IS_ZERO_TERMINATED}) {
1040 $self->CheckStringTerminator($ndr,$e,$l,$length);
1042 $self->pidl("NDR_CHECK(ndr_pull_array_$nl->{DATA_TYPE}($ndr, $ndr_flags, $var_name, $length));");
1043 return;
1045 } elsif ($l->{TYPE} eq "POINTER") {
1046 $self->ParsePtrPull($e, $l, $ndr, $var_name);
1047 } elsif ($l->{TYPE} eq "SWITCH") {
1048 $self->ParseSwitchPull($e, $l, $ndr, $var_name, $env);
1049 } elsif ($l->{TYPE} eq "DATA") {
1050 $self->ParseDataPull($e, $l, $ndr, $var_name, $primitives, $deferred);
1051 } elsif ($l->{TYPE} eq "TYPEDEF") {
1052 $typefamily{$e->{DATA}->{TYPE}}->{PULL_FN_BODY}->($self, $e->{DATA}, $ndr, $var_name);
1056 # add additional constructions
1057 if ($l->{TYPE} eq "POINTER" and $deferred) {
1058 if ($l->{POINTER_TYPE} ne "ref") {
1059 $self->pidl("if ($var_name) {");
1060 $self->indent;
1062 if ($l->{POINTER_TYPE} eq "relative") {
1063 $self->pidl("uint32_t _relative_save_offset;");
1064 $self->pidl("_relative_save_offset = $ndr->offset;");
1065 $self->pidl("NDR_CHECK(ndr_pull_relative_ptr2($ndr, $var_name));");
1069 $self->ParseMemCtxPullStart($e, $l, $ndr, $var_name);
1071 $var_name = get_value_of($var_name);
1072 $self->ParseElementPullLevel($e, GetNextLevel($e,$l), $ndr, $var_name, $env, 1, 1);
1074 $self->ParseMemCtxPullEnd($e, $l, $ndr);
1076 if ($l->{POINTER_TYPE} ne "ref") {
1077 if ($l->{POINTER_TYPE} eq "relative") {
1078 $self->pidl("$ndr->offset = _relative_save_offset;");
1080 $self->deindent;
1081 $self->pidl("}");
1083 } elsif ($l->{TYPE} eq "ARRAY" and
1084 not has_fast_array($e,$l) and not is_charset_array($e, $l)) {
1085 my $length = ParseExpr($l->{LENGTH_IS}, $env, $e->{ORIGINAL});
1086 my $counter = "cntr_$e->{NAME}_$l->{LEVEL_INDEX}";
1087 my $array_name = $var_name;
1089 if ($l->{IS_VARYING}) {
1090 $length = "ndr_get_array_length($ndr, " . get_pointer_to($var_name) .")";
1093 if (my $range = has_property($e, "range")) {
1094 my ($low, $high) = split(/,/, $range, 2);
1095 if ($low < 0) {
1096 warning(0, "$low is invalid for the range of an array size");
1098 if ($low == 0) {
1099 $self->pidl("if ($length > $high) {");
1100 } else {
1101 $self->pidl("if ($length < $low || $length > $high) {");
1103 $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_RANGE, \"value out of range\");");
1104 $self->pidl("}");
1107 $var_name = get_array_element($var_name, $counter);
1109 $self->ParseMemCtxPullStart($e, $l, $ndr, $array_name);
1111 if (($primitives and not $l->{IS_DEFERRED}) or ($deferred and $l->{IS_DEFERRED})) {
1112 my $nl = GetNextLevel($e,$l);
1114 if ($l->{IS_ZERO_TERMINATED}) {
1115 $self->CheckStringTerminator($ndr,$e,$l,$length);
1118 $self->pidl("for ($counter = 0; $counter < $length; $counter++) {");
1119 $self->indent;
1120 $self->ParseElementPullLevel($e, $nl, $ndr, $var_name, $env, 1, 0);
1121 $self->deindent;
1122 $self->pidl("}");
1125 if ($deferred and ContainsDeferred($e, $l)) {
1126 $self->pidl("for ($counter = 0; $counter < $length; $counter++) {");
1127 $self->indent;
1128 $self->ParseElementPullLevel($e,GetNextLevel($e,$l), $ndr, $var_name, $env, 0, 1);
1129 $self->deindent;
1130 $self->pidl("}");
1133 $self->ParseMemCtxPullEnd($e, $l, $ndr);
1135 } elsif ($l->{TYPE} eq "SWITCH") {
1136 $self->ParseElementPullLevel($e, GetNextLevel($e,$l), $ndr, $var_name, $env, $primitives, $deferred);
1140 #####################################################################
1141 # parse scalars in a structure element - pull size
1142 sub ParseElementPull($$$$$$)
1144 my($self,$e,$ndr,$env,$primitives,$deferred) = @_;
1146 my $var_name = $env->{$e->{NAME}};
1147 my $represent_name;
1148 my $transmit_name;
1150 return unless $primitives or ($deferred and ContainsDeferred($e, $e->{LEVELS}[0]));
1152 if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
1153 $self->pidl("{");
1154 $self->indent;
1155 $represent_name = $var_name;
1156 $transmit_name = "_transmit_$e->{NAME}";
1157 $var_name = $transmit_name;
1158 $self->pidl(mapTypeName($e->{TYPE})." $var_name;");
1161 $var_name = append_prefix($e, $var_name);
1163 $self->start_flags($e, $ndr);
1165 $self->ParseElementPullLevel($e,$e->{LEVELS}[0],$ndr,$var_name,$env,$primitives,$deferred);
1167 $self->end_flags($e, $ndr);
1169 # Representation type is different from transmit_as
1170 if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
1171 $self->pidl("NDR_CHECK(ndr_$e->{TYPE}_to_$e->{REPRESENTATION_TYPE}($transmit_name, ".get_pointer_to($represent_name)."));");
1172 $self->deindent;
1173 $self->pidl("}");
1177 #####################################################################
1178 # parse a pointer in a struct element or function
1179 sub ParsePtrPull($$$$$)
1181 my($self, $e,$l,$ndr,$var_name) = @_;
1183 my $nl = GetNextLevel($e, $l);
1184 my $next_is_array = ($nl->{TYPE} eq "ARRAY");
1185 my $next_is_string = (($nl->{TYPE} eq "DATA") and
1186 ($nl->{DATA_TYPE} eq "string"));
1188 if ($l->{POINTER_TYPE} eq "ref" and $l->{LEVEL} eq "TOP") {
1190 if (!$next_is_array and !$next_is_string) {
1191 $self->pidl("if ($ndr->flags & LIBNDR_FLAG_REF_ALLOC) {");
1192 $self->pidl("\tNDR_PULL_ALLOC($ndr, $var_name);");
1193 $self->pidl("}");
1196 return;
1197 } elsif ($l->{POINTER_TYPE} eq "ref" and $l->{LEVEL} eq "EMBEDDED") {
1198 $self->pidl("NDR_CHECK(ndr_pull_ref_ptr($ndr, &_ptr_$e->{NAME}));");
1199 } elsif (($l->{POINTER_TYPE} eq "unique") or
1200 ($l->{POINTER_TYPE} eq "relative") or
1201 ($l->{POINTER_TYPE} eq "full")) {
1202 $self->pidl("NDR_CHECK(ndr_pull_generic_ptr($ndr, &_ptr_$e->{NAME}));");
1203 } else {
1204 die("Unhandled pointer type $l->{POINTER_TYPE}");
1207 $self->pidl("if (_ptr_$e->{NAME}) {");
1208 $self->indent;
1210 # Don't do this for arrays, they're allocated at the actual level
1211 # of the array
1212 unless ($next_is_array or $next_is_string) {
1213 $self->pidl("NDR_PULL_ALLOC($ndr, $var_name);");
1214 } else {
1215 # FIXME: Yes, this is nasty.
1216 # We allocate an array twice
1217 # - once just to indicate that it's there,
1218 # - then the real allocation...
1219 $self->pidl("NDR_PULL_ALLOC($ndr, $var_name);");
1222 #$self->pidl("memset($var_name, 0, sizeof($var_name));");
1223 if ($l->{POINTER_TYPE} eq "relative") {
1224 $self->pidl("NDR_CHECK(ndr_pull_relative_ptr1($ndr, $var_name, _ptr_$e->{NAME}));");
1226 $self->deindent;
1227 $self->pidl("} else {");
1228 $self->pidl("\t$var_name = NULL;");
1229 $self->pidl("}");
1232 sub ParseStructPushPrimitives($$$$$)
1234 my ($self, $struct, $ndr, $varname, $env) = @_;
1236 # see if the structure contains a conformant array. If it
1237 # does, then it must be the last element of the structure, and
1238 # we need to push the conformant length early, as it fits on
1239 # the wire before the structure (and even before the structure
1240 # alignment)
1241 if (defined($struct->{SURROUNDING_ELEMENT})) {
1242 my $e = $struct->{SURROUNDING_ELEMENT};
1244 if (defined($e->{LEVELS}[0]) and
1245 $e->{LEVELS}[0]->{TYPE} eq "ARRAY") {
1246 my $size;
1248 if ($e->{LEVELS}[0]->{IS_ZERO_TERMINATED}) {
1249 if (has_property($e, "charset")) {
1250 $size = "ndr_charset_length($varname->$e->{NAME}, CH_$e->{PROPERTIES}->{charset})";
1251 } else {
1252 $size = "ndr_string_length($varname->$e->{NAME}, sizeof(*$varname->$e->{NAME}))";
1254 } else {
1255 $size = ParseExpr($e->{LEVELS}[0]->{SIZE_IS}, $env, $e->{ORIGINAL});
1258 $self->pidl("NDR_CHECK(ndr_push_uint32($ndr, NDR_SCALARS, $size));");
1259 } else {
1260 $self->pidl("NDR_CHECK(ndr_push_uint32($ndr, NDR_SCALARS, ndr_string_array_size($ndr, $varname->$e->{NAME})));");
1264 $self->pidl("NDR_CHECK(ndr_push_align($ndr, $struct->{ALIGN}));");
1266 if (defined($struct->{PROPERTIES}{relative_base})) {
1267 # set the current offset as base for relative pointers
1268 # and store it based on the toplevel struct/union
1269 $self->pidl("NDR_CHECK(ndr_push_setup_relative_base_offset1($ndr, $varname, $ndr->offset));");
1272 $self->ParseElementPush($_, $ndr, $env, 1, 0) foreach (@{$struct->{ELEMENTS}});
1275 sub ParseStructPushDeferred($$$$)
1277 my ($self, $struct, $ndr, $varname, $env) = @_;
1278 if (defined($struct->{PROPERTIES}{relative_base})) {
1279 # retrieve the current offset as base for relative pointers
1280 # based on the toplevel struct/union
1281 $self->pidl("NDR_CHECK(ndr_push_setup_relative_base_offset2($ndr, $varname));");
1283 $self->ParseElementPush($_, $ndr, $env, 0, 1) foreach (@{$struct->{ELEMENTS}});
1286 #####################################################################
1287 # parse a struct
1288 sub ParseStructPush($$$$)
1290 my ($self, $struct, $ndr, $varname) = @_;
1292 return unless defined($struct->{ELEMENTS});
1294 my $env = GenerateStructEnv($struct, $varname);
1296 EnvSubstituteValue($env, $struct);
1298 $self->DeclareArrayVariablesNoZero($_, $env) foreach (@{$struct->{ELEMENTS}});
1300 $self->start_flags($struct, $ndr);
1302 $self->pidl("if (ndr_flags & NDR_SCALARS) {");
1303 $self->indent;
1304 $self->ParseStructPushPrimitives($struct, $ndr, $varname, $env);
1305 $self->deindent;
1306 $self->pidl("}");
1308 $self->pidl("if (ndr_flags & NDR_BUFFERS) {");
1309 $self->indent;
1310 $self->ParseStructPushDeferred($struct, $ndr, $varname, $env);
1311 $self->deindent;
1312 $self->pidl("}");
1314 $self->end_flags($struct, $ndr);
1317 #####################################################################
1318 # generate a push function for an enum
1319 sub ParseEnumPush($$$$)
1321 my($self,$enum,$ndr,$varname) = @_;
1322 my($type_fn) = $enum->{BASE_TYPE};
1324 $self->start_flags($enum, $ndr);
1325 $self->pidl("NDR_CHECK(ndr_push_$type_fn($ndr, NDR_SCALARS, $varname));");
1326 $self->end_flags($enum, $ndr);
1329 #####################################################################
1330 # generate a pull function for an enum
1331 sub ParseEnumPull($$$$)
1333 my($self,$enum,$ndr,$varname) = @_;
1334 my($type_fn) = $enum->{BASE_TYPE};
1335 my($type_v_decl) = mapTypeName($type_fn);
1337 $self->pidl("$type_v_decl v;");
1338 $self->start_flags($enum, $ndr);
1339 $self->pidl("NDR_CHECK(ndr_pull_$type_fn($ndr, NDR_SCALARS, &v));");
1340 $self->pidl("*$varname = v;");
1342 $self->end_flags($enum, $ndr);
1345 #####################################################################
1346 # generate a print function for an enum
1347 sub ParseEnumPrint($$$$$)
1349 my($self,$enum,$ndr,$name,$varname) = @_;
1351 $self->pidl("const char *val = NULL;");
1352 $self->pidl("");
1354 $self->start_flags($enum, $ndr);
1356 $self->pidl("switch ($varname) {");
1357 $self->indent;
1358 my $els = \@{$enum->{ELEMENTS}};
1359 foreach my $i (0 .. $#{$els}) {
1360 my $e = ${$els}[$i];
1361 chomp $e;
1362 if ($e =~ /^(.*)=/) {
1363 $e = $1;
1365 $self->pidl("case $e: val = \"$e\"; break;");
1368 $self->deindent;
1369 $self->pidl("}");
1371 $self->pidl("ndr_print_enum($ndr, name, \"$enum->{TYPE}\", val, $varname);");
1373 $self->end_flags($enum, $ndr);
1376 sub DeclEnum($$$$)
1378 my ($e,$t,$name,$varname) = @_;
1379 return "enum $name " .
1380 ($t eq "pull"?"*":"") . $varname;
1383 $typefamily{ENUM} = {
1384 DECL => \&DeclEnum,
1385 PUSH_FN_BODY => \&ParseEnumPush,
1386 PULL_FN_BODY => \&ParseEnumPull,
1387 PRINT_FN_BODY => \&ParseEnumPrint,
1390 #####################################################################
1391 # generate a push function for a bitmap
1392 sub ParseBitmapPush($$$$)
1394 my($self,$bitmap,$ndr,$varname) = @_;
1395 my($type_fn) = $bitmap->{BASE_TYPE};
1397 $self->start_flags($bitmap, $ndr);
1399 $self->pidl("NDR_CHECK(ndr_push_$type_fn($ndr, NDR_SCALARS, $varname));");
1401 $self->end_flags($bitmap, $ndr);
1404 #####################################################################
1405 # generate a pull function for an bitmap
1406 sub ParseBitmapPull($$$$)
1408 my($self,$bitmap,$ndr,$varname) = @_;
1409 my $type_fn = $bitmap->{BASE_TYPE};
1410 my($type_decl) = mapTypeName($bitmap->{BASE_TYPE});
1412 $self->pidl("$type_decl v;");
1413 $self->start_flags($bitmap, $ndr);
1414 $self->pidl("NDR_CHECK(ndr_pull_$type_fn($ndr, NDR_SCALARS, &v));");
1415 $self->pidl("*$varname = v;");
1417 $self->end_flags($bitmap, $ndr);
1420 #####################################################################
1421 # generate a print function for an bitmap
1422 sub ParseBitmapPrintElement($$$$$$)
1424 my($self,$e,$bitmap,$ndr,$name,$varname) = @_;
1425 my($type_decl) = mapTypeName($bitmap->{BASE_TYPE});
1426 my($type_fn) = $bitmap->{BASE_TYPE};
1427 my($flag);
1429 if ($e =~ /^(\w+) .*$/) {
1430 $flag = "$1";
1431 } else {
1432 die "Bitmap: \"$name\" invalid Flag: \"$e\"";
1435 $self->pidl("ndr_print_bitmap_flag($ndr, sizeof($type_decl), \"$flag\", $flag, $varname);");
1438 #####################################################################
1439 # generate a print function for an bitmap
1440 sub ParseBitmapPrint($$$$$)
1442 my($self,$bitmap,$ndr,$name,$varname) = @_;
1443 my($type_decl) = mapTypeName($bitmap->{TYPE});
1444 my($type_fn) = $bitmap->{BASE_TYPE};
1446 $self->start_flags($bitmap, $ndr);
1448 $self->pidl("ndr_print_$type_fn($ndr, name, $varname);");
1450 $self->pidl("$ndr->depth++;");
1451 foreach my $e (@{$bitmap->{ELEMENTS}}) {
1452 $self->ParseBitmapPrintElement($e, $bitmap, $ndr, $name, $varname);
1454 $self->pidl("$ndr->depth--;");
1456 $self->end_flags($bitmap, $ndr);
1459 sub DeclBitmap($$$$)
1461 my ($e,$t,$name,$varname) = @_;
1462 return mapTypeName(Parse::Pidl::Typelist::bitmap_type_fn($e)) .
1463 ($t eq "pull"?" *":" ") . $varname;
1466 $typefamily{BITMAP} = {
1467 DECL => \&DeclBitmap,
1468 PUSH_FN_BODY => \&ParseBitmapPush,
1469 PULL_FN_BODY => \&ParseBitmapPull,
1470 PRINT_FN_BODY => \&ParseBitmapPrint,
1473 #####################################################################
1474 # generate a struct print function
1475 sub ParseStructPrint($$$$$)
1477 my($self,$struct,$ndr,$name,$varname) = @_;
1479 return unless defined $struct->{ELEMENTS};
1481 my $env = GenerateStructEnv($struct, $varname);
1483 $self->DeclareArrayVariables($_) foreach (@{$struct->{ELEMENTS}});
1485 $self->pidl("ndr_print_struct($ndr, name, \"$name\");");
1487 $self->start_flags($struct, $ndr);
1489 $self->pidl("$ndr->depth++;");
1491 $self->ParseElementPrint($_, $ndr, $env->{$_->{NAME}}, $env)
1492 foreach (@{$struct->{ELEMENTS}});
1493 $self->pidl("$ndr->depth--;");
1495 $self->end_flags($struct, $ndr);
1498 sub DeclarePtrVariables($$)
1500 my ($self,$e) = @_;
1501 foreach my $l (@{$e->{LEVELS}}) {
1502 if ($l->{TYPE} eq "POINTER" and
1503 not ($l->{POINTER_TYPE} eq "ref" and $l->{LEVEL} eq "TOP")) {
1504 $self->pidl("uint32_t _ptr_$e->{NAME};");
1505 last;
1510 sub DeclareArrayVariables($$)
1512 my ($self,$e) = @_;
1514 foreach my $l (@{$e->{LEVELS}}) {
1515 next if ($l->{TYPE} ne "ARRAY");
1516 next if has_fast_array($e,$l);
1517 next if is_charset_array($e,$l);
1518 $self->pidl("uint32_t cntr_$e->{NAME}_$l->{LEVEL_INDEX};");
1522 sub DeclareArrayVariablesNoZero($$$)
1524 my ($self,$e,$env) = @_;
1526 foreach my $l (@{$e->{LEVELS}}) {
1527 next if ($l->{TYPE} ne "ARRAY");
1528 next if has_fast_array($e,$l);
1529 next if is_charset_array($e,$l);
1530 my $length = ParseExpr($l->{LENGTH_IS}, $env, $e->{ORIGINAL});
1531 if ($length eq "0") {
1532 warning($e->{ORIGINAL}, "pointless array cntr: 'cntr_$e->{NAME}_$l->{LEVEL_INDEX}': length=$length");
1533 } else {
1534 $self->pidl("uint32_t cntr_$e->{NAME}_$l->{LEVEL_INDEX};");
1539 sub DeclareMemCtxVariables($$)
1541 my ($self,$e) = @_;
1542 foreach my $l (@{$e->{LEVELS}}) {
1543 my $mem_flags = $self->ParseMemCtxPullFlags($e, $l);
1544 if (defined($mem_flags)) {
1545 $self->pidl("TALLOC_CTX *_mem_save_$e->{NAME}_$l->{LEVEL_INDEX};");
1550 sub ParseStructPullPrimitives($$$$$)
1552 my($self,$struct,$ndr,$varname,$env) = @_;
1554 if (defined $struct->{SURROUNDING_ELEMENT}) {
1555 $self->pidl("NDR_CHECK(ndr_pull_array_size($ndr, &$varname->$struct->{SURROUNDING_ELEMENT}->{NAME}));");
1558 $self->pidl("NDR_CHECK(ndr_pull_align($ndr, $struct->{ALIGN}));");
1560 if (defined($struct->{PROPERTIES}{relative_base})) {
1561 # set the current offset as base for relative pointers
1562 # and store it based on the toplevel struct/union
1563 $self->pidl("NDR_CHECK(ndr_pull_setup_relative_base_offset1($ndr, $varname, $ndr->offset));");
1566 $self->ParseElementPull($_, $ndr, $env, 1, 0) foreach (@{$struct->{ELEMENTS}});
1568 $self->add_deferred();
1571 sub ParseStructPullDeferred($$$$$)
1573 my ($self,$struct,$ndr,$varname,$env) = @_;
1575 if (defined($struct->{PROPERTIES}{relative_base})) {
1576 # retrieve the current offset as base for relative pointers
1577 # based on the toplevel struct/union
1578 $self->pidl("NDR_CHECK(ndr_pull_setup_relative_base_offset2($ndr, $varname));");
1580 foreach my $e (@{$struct->{ELEMENTS}}) {
1581 $self->ParseElementPull($e, $ndr, $env, 0, 1);
1584 $self->add_deferred();
1587 #####################################################################
1588 # parse a struct - pull side
1589 sub ParseStructPull($$$$)
1591 my($self,$struct,$ndr,$varname) = @_;
1593 return unless defined $struct->{ELEMENTS};
1595 # declare any internal pointers we need
1596 foreach my $e (@{$struct->{ELEMENTS}}) {
1597 $self->DeclarePtrVariables($e);
1598 $self->DeclareArrayVariables($e);
1599 $self->DeclareMemCtxVariables($e);
1602 $self->start_flags($struct, $ndr);
1604 my $env = GenerateStructEnv($struct, $varname);
1606 $self->pidl("if (ndr_flags & NDR_SCALARS) {");
1607 $self->indent;
1608 $self->ParseStructPullPrimitives($struct,$ndr,$varname,$env);
1609 $self->deindent;
1610 $self->pidl("}");
1611 $self->pidl("if (ndr_flags & NDR_BUFFERS) {");
1612 $self->indent;
1613 $self->ParseStructPullDeferred($struct,$ndr,$varname,$env);
1614 $self->deindent;
1615 $self->pidl("}");
1617 $self->end_flags($struct, $ndr);
1620 #####################################################################
1621 # calculate size of ndr struct
1622 sub ParseStructNdrSize($$$$)
1624 my ($self,$t, $name, $varname) = @_;
1625 my $sizevar;
1627 if (my $flags = has_property($t, "flag")) {
1628 $self->pidl("flags |= $flags;");
1630 $self->pidl("return ndr_size_struct($varname, flags, (ndr_push_flags_fn_t)ndr_push_$name, ic);");
1633 sub DeclStruct($$$$)
1635 my ($e,$t,$name,$varname) = @_;
1636 return ($t ne "pull"?"const ":"") . "struct $name *$varname";
1639 sub ArgsStructNdrSize($$$)
1641 my ($d, $name, $varname) = @_;
1642 return "const struct $name *$varname, struct smb_iconv_convenience *ic, int flags";
1645 $typefamily{STRUCT} = {
1646 PUSH_FN_BODY => \&ParseStructPush,
1647 DECL => \&DeclStruct,
1648 PULL_FN_BODY => \&ParseStructPull,
1649 PRINT_FN_BODY => \&ParseStructPrint,
1650 SIZE_FN_BODY => \&ParseStructNdrSize,
1651 SIZE_FN_ARGS => \&ArgsStructNdrSize,
1654 #####################################################################
1655 # calculate size of ndr struct
1656 sub ParseUnionNdrSize($$$)
1658 my ($self, $t, $name, $varname) = @_;
1659 my $sizevar;
1661 if (my $flags = has_property($t, "flag")) {
1662 $self->pidl("flags |= $flags;");
1665 $self->pidl("return ndr_size_union($varname, flags, level, (ndr_push_flags_fn_t)ndr_push_$name, ic);");
1668 sub ParseUnionPushPrimitives($$$$)
1670 my ($self, $e, $ndr ,$varname) = @_;
1672 my $have_default = 0;
1674 $self->pidl("int level = ndr_push_get_switch_value($ndr, $varname);");
1676 if (defined($e->{SWITCH_TYPE})) {
1677 $self->pidl("NDR_CHECK(ndr_push_$e->{SWITCH_TYPE}($ndr, NDR_SCALARS, level));");
1680 $self->pidl("switch (level) {");
1681 $self->indent;
1682 foreach my $el (@{$e->{ELEMENTS}}) {
1683 if ($el->{CASE} eq "default") {
1684 $have_default = 1;
1686 $self->pidl("$el->{CASE}: {");
1688 if ($el->{TYPE} ne "EMPTY") {
1689 $self->indent;
1690 if (defined($e->{PROPERTIES}{relative_base})) {
1691 $self->pidl("NDR_CHECK(ndr_push_align($ndr, $el->{ALIGN}));");
1692 # set the current offset as base for relative pointers
1693 # and store it based on the toplevel struct/union
1694 $self->pidl("NDR_CHECK(ndr_push_setup_relative_base_offset1($ndr, $varname, $ndr->offset));");
1696 $self->DeclareArrayVariables($el);
1697 $self->ParseElementPush($el, $ndr, {$el->{NAME} => "$varname->$el->{NAME}"}, 1, 0);
1698 $self->deindent;
1700 $self->pidl("break; }");
1701 $self->pidl("");
1703 if (! $have_default) {
1704 $self->pidl("default:");
1705 $self->pidl("\treturn ndr_push_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);");
1707 $self->deindent;
1708 $self->pidl("}");
1711 sub ParseUnionPushDeferred($$$$)
1713 my ($self,$e,$ndr,$varname) = @_;
1715 my $have_default = 0;
1717 $self->pidl("int level = ndr_push_get_switch_value($ndr, $varname);");
1718 if (defined($e->{PROPERTIES}{relative_base})) {
1719 # retrieve the current offset as base for relative pointers
1720 # based on the toplevel struct/union
1721 $self->pidl("NDR_CHECK(ndr_push_setup_relative_base_offset2($ndr, $varname));");
1723 $self->pidl("switch (level) {");
1724 $self->indent;
1725 foreach my $el (@{$e->{ELEMENTS}}) {
1726 if ($el->{CASE} eq "default") {
1727 $have_default = 1;
1730 $self->pidl("$el->{CASE}:");
1731 if ($el->{TYPE} ne "EMPTY") {
1732 $self->indent;
1733 $self->ParseElementPush($el, $ndr, {$el->{NAME} => "$varname->$el->{NAME}"}, 0, 1);
1734 $self->deindent;
1736 $self->pidl("break;");
1737 $self->pidl("");
1739 if (! $have_default) {
1740 $self->pidl("default:");
1741 $self->pidl("\treturn ndr_push_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);");
1743 $self->deindent;
1744 $self->pidl("}");
1747 #####################################################################
1748 # parse a union - push side
1749 sub ParseUnionPush($$$$)
1751 my ($self,$e,$ndr,$varname) = @_;
1752 my $have_default = 0;
1754 $self->start_flags($e, $ndr);
1756 $self->pidl("if (ndr_flags & NDR_SCALARS) {");
1757 $self->indent;
1758 $self->ParseUnionPushPrimitives($e, $ndr, $varname);
1759 $self->deindent;
1760 $self->pidl("}");
1761 $self->pidl("if (ndr_flags & NDR_BUFFERS) {");
1762 $self->indent;
1763 $self->ParseUnionPushDeferred($e, $ndr, $varname);
1764 $self->deindent;
1765 $self->pidl("}");
1766 $self->end_flags($e, $ndr);
1769 #####################################################################
1770 # print a union
1771 sub ParseUnionPrint($$$$$)
1773 my ($self,$e,$ndr,$name,$varname) = @_;
1774 my $have_default = 0;
1776 $self->pidl("int level;");
1777 foreach my $el (@{$e->{ELEMENTS}}) {
1778 $self->DeclareArrayVariables($el);
1781 $self->start_flags($e, $ndr);
1783 $self->pidl("level = ndr_print_get_switch_value($ndr, $varname);");
1785 $self->pidl("ndr_print_union($ndr, name, level, \"$name\");");
1787 $self->pidl("switch (level) {");
1788 $self->indent;
1789 foreach my $el (@{$e->{ELEMENTS}}) {
1790 if ($el->{CASE} eq "default") {
1791 $have_default = 1;
1793 $self->pidl("$el->{CASE}:");
1794 if ($el->{TYPE} ne "EMPTY") {
1795 $self->indent;
1796 $self->ParseElementPrint($el, $ndr, "$varname->$el->{NAME}", {});
1797 $self->deindent;
1799 $self->pidl("break;");
1800 $self->pidl("");
1802 if (! $have_default) {
1803 $self->pidl("default:");
1804 $self->pidl("\tndr_print_bad_level($ndr, name, level);");
1806 $self->deindent;
1807 $self->pidl("}");
1809 $self->end_flags($e, $ndr);
1812 sub ParseUnionPullPrimitives($$$$$)
1814 my ($self,$e,$ndr,$varname,$switch_type) = @_;
1815 my $have_default = 0;
1817 if (defined($switch_type)) {
1818 $self->pidl("NDR_CHECK(ndr_pull_$switch_type($ndr, NDR_SCALARS, &_level));");
1819 $self->pidl("if (_level != level) {");
1820 $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value %u for $varname\", _level);");
1821 $self->pidl("}");
1824 $self->pidl("switch (level) {");
1825 $self->indent;
1826 foreach my $el (@{$e->{ELEMENTS}}) {
1827 if ($el->{CASE} eq "default") {
1828 $have_default = 1;
1830 $self->pidl("$el->{CASE}: {");
1832 if ($el->{TYPE} ne "EMPTY") {
1833 $self->indent;
1834 if (defined($e->{PROPERTIES}{relative_base})) {
1835 $self->pidl("NDR_CHECK(ndr_pull_align($ndr, $el->{ALIGN}));");
1836 # set the current offset as base for relative pointers
1837 # and store it based on the toplevel struct/union
1838 $self->pidl("NDR_CHECK(ndr_pull_setup_relative_base_offset1($ndr, $varname, $ndr->offset));");
1840 $self->ParseElementPull($el, $ndr, {$el->{NAME} => "$varname->$el->{NAME}"}, 1, 0);
1841 $self->deindent;
1843 $self->pidl("break; }");
1844 $self->pidl("");
1846 if (! $have_default) {
1847 $self->pidl("default:");
1848 $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);");
1850 $self->deindent;
1851 $self->pidl("}");
1854 sub ParseUnionPullDeferred($$$$)
1856 my ($self,$e,$ndr,$varname) = @_;
1857 my $have_default = 0;
1859 if (defined($e->{PROPERTIES}{relative_base})) {
1860 # retrieve the current offset as base for relative pointers
1861 # based on the toplevel struct/union
1862 $self->pidl("NDR_CHECK(ndr_pull_setup_relative_base_offset2($ndr, $varname));");
1864 $self->pidl("switch (level) {");
1865 $self->indent;
1866 foreach my $el (@{$e->{ELEMENTS}}) {
1867 if ($el->{CASE} eq "default") {
1868 $have_default = 1;
1871 $self->pidl("$el->{CASE}:");
1872 if ($el->{TYPE} ne "EMPTY") {
1873 $self->indent;
1874 $self->ParseElementPull($el, $ndr, {$el->{NAME} => "$varname->$el->{NAME}"}, 0, 1);
1875 $self->deindent;
1877 $self->pidl("break;");
1878 $self->pidl("");
1880 if (! $have_default) {
1881 $self->pidl("default:");
1882 $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);");
1884 $self->deindent;
1885 $self->pidl("}");
1890 #####################################################################
1891 # parse a union - pull side
1892 sub ParseUnionPull($$$$)
1894 my ($self,$e,$ndr,$varname) = @_;
1895 my $switch_type = $e->{SWITCH_TYPE};
1897 $self->pidl("int level;");
1898 if (defined($switch_type)) {
1899 if (Parse::Pidl::Typelist::typeIs($switch_type, "ENUM")) {
1900 $switch_type = Parse::Pidl::Typelist::enum_type_fn(getType($switch_type)->{DATA});
1902 $self->pidl(mapTypeName($switch_type) . " _level;");
1905 my %double_cases = ();
1906 foreach my $el (@{$e->{ELEMENTS}}) {
1907 next if ($el->{TYPE} eq "EMPTY");
1908 next if ($double_cases{"$el->{NAME}"});
1909 $self->DeclareMemCtxVariables($el);
1910 $self->DeclarePtrVariables($el);
1911 $self->DeclareArrayVariables($el);
1912 $double_cases{"$el->{NAME}"} = 1;
1915 $self->start_flags($e, $ndr);
1917 $self->pidl("level = ndr_pull_get_switch_value($ndr, $varname);");
1919 $self->pidl("if (ndr_flags & NDR_SCALARS) {");
1920 $self->indent;
1921 $self->ParseUnionPullPrimitives($e,$ndr,$varname,$switch_type);
1922 $self->deindent;
1923 $self->pidl("}");
1925 $self->pidl("if (ndr_flags & NDR_BUFFERS) {");
1926 $self->indent;
1927 $self->ParseUnionPullDeferred($e,$ndr,$varname);
1928 $self->deindent;
1929 $self->pidl("}");
1931 $self->add_deferred();
1933 $self->end_flags($e, $ndr);
1936 sub DeclUnion($$$$)
1938 my ($e,$t,$name,$varname) = @_;
1939 return ($t ne "pull"?"const ":"") . "union $name *$varname";
1942 sub ArgsUnionNdrSize($$)
1944 my ($d,$name) = @_;
1945 return "const union $name *r, uint32_t level, struct smb_iconv_convenience *ic, int flags";
1948 $typefamily{UNION} = {
1949 PUSH_FN_BODY => \&ParseUnionPush,
1950 DECL => \&DeclUnion,
1951 PULL_FN_BODY => \&ParseUnionPull,
1952 PRINT_FN_BODY => \&ParseUnionPrint,
1953 SIZE_FN_ARGS => \&ArgsUnionNdrSize,
1954 SIZE_FN_BODY => \&ParseUnionNdrSize,
1957 #####################################################################
1958 # parse a typedef - push side
1959 sub ParseTypedefPush($$$$)
1961 my($self,$e,$ndr,$varname) = @_;
1963 my $env;
1965 $env->{$e->{NAME}} = $varname;
1967 $self->ParseElementPushLevel($e, $e->{LEVELS}[0], $ndr, $varname, $env, 1, 1);
1970 #####################################################################
1971 # parse a typedef - pull side
1972 sub ParseTypedefPull($$$$)
1974 my($self,$e,$ndr,$varname) = @_;
1976 my $env;
1978 $env->{$e->{NAME}} = $varname;
1980 $self->ParseElementPullLevel($e, $e->{LEVELS}[0], $ndr, $varname, $env, 1, 1);
1983 #####################################################################
1984 # parse a typedef - print side
1985 sub ParseTypedefPrint($$$$$)
1987 my($self,$e,$ndr,$name,$varname) = @_;
1989 $typefamily{$e->{DATA}->{TYPE}}->{PRINT_FN_BODY}->($self, $e->{DATA}, $ndr, $name, $varname);
1992 #####################################################################
1993 ## calculate the size of a structure
1994 sub ParseTypedefNdrSize($$$$)
1996 my($self,$t,$name,$varname) = @_;
1998 $typefamily{$t->{DATA}->{TYPE}}->{SIZE_FN_BODY}->($self, $t->{DATA}, $name, $varname);
2001 sub DeclTypedef($$$$)
2003 my ($e, $t, $name, $varname) = @_;
2005 return $typefamily{$e->{DATA}->{TYPE}}->{DECL}->($e->{DATA}, $t, $name, $varname);
2008 sub ArgsTypedefNdrSize($$$)
2010 my ($d, $name, $varname) = @_;
2011 return $typefamily{$d->{DATA}->{TYPE}}->{SIZE_FN_ARGS}->($d->{DATA}, $name, $varname);
2014 $typefamily{TYPEDEF} = {
2015 PUSH_FN_BODY => \&ParseTypedefPush,
2016 DECL => \&DeclTypedef,
2017 PULL_FN_BODY => \&ParseTypedefPull,
2018 PRINT_FN_BODY => \&ParseTypedefPrint,
2019 SIZE_FN_ARGS => \&ArgsTypedefNdrSize,
2020 SIZE_FN_BODY => \&ParseTypedefNdrSize,
2023 #####################################################################
2024 # parse a function - print side
2025 sub ParseFunctionPrint($$)
2027 my($self, $fn) = @_;
2028 my $ndr = "ndr";
2030 $self->pidl_hdr("void ndr_print_$fn->{NAME}(struct ndr_print *$ndr, const char *name, int flags, const struct $fn->{NAME} *r);");
2032 return if has_property($fn, "noprint");
2034 $self->pidl("_PUBLIC_ void ndr_print_$fn->{NAME}(struct ndr_print *$ndr, const char *name, int flags, const struct $fn->{NAME} *r)");
2035 $self->pidl("{");
2036 $self->indent;
2038 foreach my $e (@{$fn->{ELEMENTS}}) {
2039 $self->DeclareArrayVariables($e);
2042 $self->pidl("ndr_print_struct($ndr, name, \"$fn->{NAME}\");");
2043 $self->pidl("$ndr->depth++;");
2045 $self->pidl("if (flags & NDR_SET_VALUES) {");
2046 $self->pidl("\t$ndr->flags |= LIBNDR_PRINT_SET_VALUES;");
2047 $self->pidl("}");
2049 $self->pidl("if (flags & NDR_IN) {");
2050 $self->indent;
2051 $self->pidl("ndr_print_struct($ndr, \"in\", \"$fn->{NAME}\");");
2052 $self->pidl("$ndr->depth++;");
2054 my $env = GenerateFunctionInEnv($fn);
2056 foreach my $e (@{$fn->{ELEMENTS}}) {
2057 if (grep(/in/,@{$e->{DIRECTION}})) {
2058 $self->ParseElementPrint($e, $ndr, $env->{$e->{NAME}}, $env);
2061 $self->pidl("$ndr->depth--;");
2062 $self->deindent;
2063 $self->pidl("}");
2065 $self->pidl("if (flags & NDR_OUT) {");
2066 $self->indent;
2067 $self->pidl("ndr_print_struct($ndr, \"out\", \"$fn->{NAME}\");");
2068 $self->pidl("$ndr->depth++;");
2070 $env = GenerateFunctionOutEnv($fn);
2071 foreach my $e (@{$fn->{ELEMENTS}}) {
2072 if (grep(/out/,@{$e->{DIRECTION}})) {
2073 $self->ParseElementPrint($e, $ndr, $env->{$e->{NAME}}, $env);
2076 if ($fn->{RETURN_TYPE}) {
2077 $self->pidl("ndr_print_$fn->{RETURN_TYPE}($ndr, \"result\", r->out.result);");
2079 $self->pidl("$ndr->depth--;");
2080 $self->deindent;
2081 $self->pidl("}");
2083 $self->pidl("$ndr->depth--;");
2084 $self->deindent;
2085 $self->pidl("}");
2086 $self->pidl("");
2089 #####################################################################
2090 # parse a function
2091 sub ParseFunctionPush($$)
2093 my($self, $fn) = @_;
2094 my $ndr = "ndr";
2096 $self->fn_declare("push", $fn, "enum ndr_err_code ndr_push_$fn->{NAME}(struct ndr_push *$ndr, int flags, const struct $fn->{NAME} *r)") or return;
2098 return if has_property($fn, "nopush");
2100 $self->pidl("{");
2101 $self->indent;
2103 foreach my $e (@{$fn->{ELEMENTS}}) {
2104 $self->DeclareArrayVariables($e);
2107 $self->pidl("if (flags & NDR_IN) {");
2108 $self->indent;
2110 my $env = GenerateFunctionInEnv($fn);
2112 EnvSubstituteValue($env, $fn);
2114 foreach my $e (@{$fn->{ELEMENTS}}) {
2115 if (grep(/in/,@{$e->{DIRECTION}})) {
2116 $self->ParseElementPush($e, $ndr, $env, 1, 1);
2120 $self->deindent;
2121 $self->pidl("}");
2123 $self->pidl("if (flags & NDR_OUT) {");
2124 $self->indent;
2126 $env = GenerateFunctionOutEnv($fn);
2127 foreach my $e (@{$fn->{ELEMENTS}}) {
2128 if (grep(/out/,@{$e->{DIRECTION}})) {
2129 $self->ParseElementPush($e, $ndr, $env, 1, 1);
2133 if ($fn->{RETURN_TYPE}) {
2134 $self->pidl("NDR_CHECK(ndr_push_$fn->{RETURN_TYPE}($ndr, NDR_SCALARS, r->out.result));");
2137 $self->deindent;
2138 $self->pidl("}");
2139 $self->pidl("return NDR_ERR_SUCCESS;");
2140 $self->deindent;
2141 $self->pidl("}");
2142 $self->pidl("");
2145 sub AllocateArrayLevel($$$$$$)
2147 my ($self,$e,$l,$ndr,$var,$size) = @_;
2149 my $pl = GetPrevLevel($e, $l);
2150 if (defined($pl) and
2151 $pl->{TYPE} eq "POINTER" and
2152 $pl->{POINTER_TYPE} eq "ref"
2153 and not $l->{IS_ZERO_TERMINATED}) {
2154 $self->pidl("if ($ndr->flags & LIBNDR_FLAG_REF_ALLOC) {");
2155 $self->pidl("\tNDR_PULL_ALLOC_N($ndr, $var, $size);");
2156 $self->pidl("}");
2157 if (grep(/in/,@{$e->{DIRECTION}}) and
2158 grep(/out/,@{$e->{DIRECTION}})) {
2159 $self->pidl("memcpy(r->out.$e->{NAME}, r->in.$e->{NAME}, ($size) * sizeof(*r->in.$e->{NAME}));");
2161 return;
2164 $self->pidl("NDR_PULL_ALLOC_N($ndr, $var, $size);");
2167 #####################################################################
2168 # parse a function
2169 sub ParseFunctionPull($$)
2171 my($self,$fn) = @_;
2172 my $ndr = "ndr";
2174 # pull function args
2175 $self->fn_declare("pull", $fn, "enum ndr_err_code ndr_pull_$fn->{NAME}(struct ndr_pull *$ndr, int flags, struct $fn->{NAME} *r)") or return;
2177 $self->pidl("{");
2178 $self->indent;
2180 # declare any internal pointers we need
2181 foreach my $e (@{$fn->{ELEMENTS}}) {
2182 $self->DeclarePtrVariables($e);
2183 $self->DeclareArrayVariables($e);
2186 my %double_cases = ();
2187 foreach my $e (@{$fn->{ELEMENTS}}) {
2188 next if ($e->{TYPE} eq "EMPTY");
2189 next if ($double_cases{"$e->{NAME}"});
2190 $self->DeclareMemCtxVariables($e);
2191 $double_cases{"$e->{NAME}"} = 1;
2194 $self->pidl("if (flags & NDR_IN) {");
2195 $self->indent;
2197 # auto-init the out section of a structure. I originally argued that
2198 # this was a bad idea as it hides bugs, but coping correctly
2199 # with initialisation and not wiping ref vars is turning
2200 # out to be too tricky (tridge)
2201 foreach my $e (@{$fn->{ELEMENTS}}) {
2202 next unless grep(/out/, @{$e->{DIRECTION}});
2203 $self->pidl("ZERO_STRUCT(r->out);");
2204 $self->pidl("");
2205 last;
2208 my $env = GenerateFunctionInEnv($fn);
2210 foreach my $e (@{$fn->{ELEMENTS}}) {
2211 next unless (grep(/in/, @{$e->{DIRECTION}}));
2212 $self->ParseElementPull($e, $ndr, $env, 1, 1);
2215 # allocate the "simple" out ref variables. FIXME: Shouldn't this have it's
2216 # own flag rather than be in NDR_IN ?
2218 foreach my $e (@{$fn->{ELEMENTS}}) {
2219 next unless (grep(/out/, @{$e->{DIRECTION}}));
2220 next unless ($e->{LEVELS}[0]->{TYPE} eq "POINTER" and
2221 $e->{LEVELS}[0]->{POINTER_TYPE} eq "ref");
2222 next if (($e->{LEVELS}[1]->{TYPE} eq "DATA") and
2223 ($e->{LEVELS}[1]->{DATA_TYPE} eq "string"));
2224 next if (($e->{LEVELS}[1]->{TYPE} eq "ARRAY")
2225 and $e->{LEVELS}[1]->{IS_ZERO_TERMINATED});
2227 if ($e->{LEVELS}[1]->{TYPE} eq "ARRAY") {
2228 my $size = ParseExprExt($e->{LEVELS}[1]->{SIZE_IS}, $env, $e->{ORIGINAL},
2229 check_null_pointer($e, $env, sub { $self->pidl(shift); },
2230 "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for size_is()\");"),
2231 check_fully_dereferenced($e, $env));
2232 $self->pidl("NDR_PULL_ALLOC_N($ndr, r->out.$e->{NAME}, $size);");
2234 if (grep(/in/, @{$e->{DIRECTION}})) {
2235 $self->pidl("memcpy(r->out.$e->{NAME}, r->in.$e->{NAME}, ($size) * sizeof(*r->in.$e->{NAME}));");
2236 } else {
2237 $self->pidl("memset(r->out.$e->{NAME}, 0, ($size) * sizeof(*r->out.$e->{NAME}));");
2239 } else {
2240 $self->pidl("NDR_PULL_ALLOC($ndr, r->out.$e->{NAME});");
2242 if (grep(/in/, @{$e->{DIRECTION}})) {
2243 $self->pidl("*r->out.$e->{NAME} = *r->in.$e->{NAME};");
2244 } else {
2245 $self->pidl("ZERO_STRUCTP(r->out.$e->{NAME});");
2250 $self->add_deferred();
2251 $self->deindent;
2252 $self->pidl("}");
2254 $self->pidl("if (flags & NDR_OUT) {");
2255 $self->indent;
2257 $env = GenerateFunctionOutEnv($fn);
2258 foreach my $e (@{$fn->{ELEMENTS}}) {
2259 next unless grep(/out/, @{$e->{DIRECTION}});
2260 $self->ParseElementPull($e, $ndr, $env, 1, 1);
2263 if ($fn->{RETURN_TYPE}) {
2264 $self->pidl("NDR_CHECK(ndr_pull_$fn->{RETURN_TYPE}($ndr, NDR_SCALARS, &r->out.result));");
2267 $self->add_deferred();
2268 $self->deindent;
2269 $self->pidl("}");
2271 $self->pidl("return NDR_ERR_SUCCESS;");
2272 $self->deindent;
2273 $self->pidl("}");
2274 $self->pidl("");
2277 sub AuthServiceStruct($$$)
2279 my ($self, $ifacename, $authservice) = @_;
2280 my @a = split /,/, $authservice;
2281 my $authservice_count = $#a + 1;
2283 $self->pidl("static const char * const $ifacename\_authservice_strings[] = {");
2284 foreach my $ap (@a) {
2285 $self->pidl("\t$ap, ");
2287 $self->pidl("};");
2288 $self->pidl("");
2290 $self->pidl("static const struct ndr_interface_string_array $ifacename\_authservices = {");
2291 $self->pidl("\t.count\t= $authservice_count,");
2292 $self->pidl("\t.names\t= $ifacename\_authservice_strings");
2293 $self->pidl("};");
2294 $self->pidl("");
2297 sub FunctionCallEntry($$)
2299 my ($self, $d) = @_;
2300 return 0 if not defined($d->{OPNUM});
2301 $self->pidl("\t{");
2302 $self->pidl("\t\t\"$d->{NAME}\",");
2303 $self->pidl("\t\tsizeof(struct $d->{NAME}),");
2304 $self->pidl("\t\t(ndr_push_flags_fn_t) ndr_push_$d->{NAME},");
2305 $self->pidl("\t\t(ndr_pull_flags_fn_t) ndr_pull_$d->{NAME},");
2306 $self->pidl("\t\t(ndr_print_function_t) ndr_print_$d->{NAME},");
2307 $self->pidl("\t\t".($d->{ASYNC}?"true":"false").",");
2308 $self->pidl("\t},");
2309 return 1;
2312 #####################################################################
2313 # produce a function call table
2314 sub FunctionTable($$)
2316 my($self,$interface) = @_;
2317 my $count = 0;
2318 my $uname = uc $interface->{NAME};
2320 return if ($#{$interface->{FUNCTIONS}}+1 == 0);
2321 return unless defined ($interface->{PROPERTIES}->{uuid});
2323 $self->pidl("static const struct ndr_interface_call $interface->{NAME}\_calls[] = {");
2325 foreach my $d (@{$interface->{INHERITED_FUNCTIONS}},@{$interface->{FUNCTIONS}}) {
2326 $count += $self->FunctionCallEntry($d);
2328 $self->pidl("\t{ NULL, 0, NULL, NULL, NULL, false }");
2329 $self->pidl("};");
2330 $self->pidl("");
2332 $self->pidl("static const char * const $interface->{NAME}\_endpoint_strings[] = {");
2333 foreach my $ep (@{$interface->{ENDPOINTS}}) {
2334 $self->pidl("\t$ep, ");
2336 my $endpoint_count = $#{$interface->{ENDPOINTS}}+1;
2338 $self->pidl("};");
2339 $self->pidl("");
2341 $self->pidl("static const struct ndr_interface_string_array $interface->{NAME}\_endpoints = {");
2342 $self->pidl("\t.count\t= $endpoint_count,");
2343 $self->pidl("\t.names\t= $interface->{NAME}\_endpoint_strings");
2344 $self->pidl("};");
2345 $self->pidl("");
2347 if (! defined $interface->{PROPERTIES}->{authservice}) {
2348 $interface->{PROPERTIES}->{authservice} = "\"host\"";
2351 $self->AuthServiceStruct($interface->{NAME},
2352 $interface->{PROPERTIES}->{authservice});
2354 $self->pidl("\nconst struct ndr_interface_table ndr_table_$interface->{NAME} = {");
2355 $self->pidl("\t.name\t\t= \"$interface->{NAME}\",");
2356 $self->pidl("\t.syntax_id\t= {");
2357 $self->pidl("\t\t" . print_uuid($interface->{UUID}) .",");
2358 $self->pidl("\t\tNDR_$uname\_VERSION");
2359 $self->pidl("\t},");
2360 $self->pidl("\t.helpstring\t= NDR_$uname\_HELPSTRING,");
2361 $self->pidl("\t.num_calls\t= $count,");
2362 $self->pidl("\t.calls\t\t= $interface->{NAME}\_calls,");
2363 $self->pidl("\t.endpoints\t= &$interface->{NAME}\_endpoints,");
2364 $self->pidl("\t.authservices\t= &$interface->{NAME}\_authservices");
2365 $self->pidl("};");
2366 $self->pidl("");
2370 #####################################################################
2371 # generate include statements for imported idl files
2372 sub HeaderImport
2374 my $self = shift;
2375 my @imports = @_;
2376 foreach (@imports) {
2377 $_ = unmake_str($_);
2378 s/\.idl$//;
2379 $self->pidl(choose_header("librpc/gen_ndr/ndr_$_\.h", "gen_ndr/ndr_$_.h"));
2383 #####################################################################
2384 # generate include statements for included header files
2385 sub HeaderInclude
2387 my $self = shift;
2388 my @includes = @_;
2389 foreach (@includes) {
2390 $self->pidl_hdr("#include $_");
2394 #####################################################################
2395 # generate prototypes and defines for the interface definitions
2396 # FIXME: these prototypes are for the DCE/RPC client functions, not the
2397 # NDR parser and so do not belong here, technically speaking
2398 sub HeaderInterface($$$)
2400 my($self,$interface,$needed) = @_;
2402 my $count = 0;
2404 if ($needed->{"compression"}) {
2405 $self->pidl(choose_header("librpc/ndr/ndr_compression.h", "ndr/compression.h"));
2408 if (has_property($interface, "object")) {
2409 $self->pidl(choose_header("librpc/gen_ndr/ndr_orpc.h", "ndr/orpc.h"));
2412 if (defined $interface->{PROPERTIES}->{helper}) {
2413 $self->HeaderInclude(split /,/, $interface->{PROPERTIES}->{helper});
2416 if (defined $interface->{PROPERTIES}->{uuid}) {
2417 my $name = uc $interface->{NAME};
2418 $self->pidl_hdr("#define NDR_$name\_UUID " .
2419 Parse::Pidl::Util::make_str(lc($interface->{PROPERTIES}->{uuid})));
2421 if(!defined $interface->{PROPERTIES}->{version}) { $interface->{PROPERTIES}->{version} = "0.0"; }
2422 $self->pidl_hdr("#define NDR_$name\_VERSION $interface->{PROPERTIES}->{version}");
2424 $self->pidl_hdr("#define NDR_$name\_NAME \"$interface->{NAME}\"");
2426 if(!defined $interface->{PROPERTIES}->{helpstring}) { $interface->{PROPERTIES}->{helpstring} = "NULL"; }
2427 $self->pidl_hdr("#define NDR_$name\_HELPSTRING $interface->{PROPERTIES}->{helpstring}");
2429 $self->pidl_hdr("extern const struct ndr_interface_table ndr_table_$interface->{NAME};");
2432 foreach (@{$interface->{FUNCTIONS}}) {
2433 next if has_property($_, "noopnum");
2434 next if grep(/^$_->{NAME}$/,@{$interface->{INHERITED_FUNCTIONS}});
2435 my $u_name = uc $_->{NAME};
2437 my $val = sprintf("0x%02x", $count);
2438 if (defined($interface->{BASE})) {
2439 $val .= " + NDR_" . uc $interface->{BASE} . "_CALL_COUNT";
2442 $self->pidl_hdr("#define NDR_$u_name ($val)");
2444 $self->pidl_hdr("");
2445 $count++;
2448 my $val = $count;
2450 if (defined($interface->{BASE})) {
2451 $val .= " + NDR_" . uc $interface->{BASE} . "_CALL_COUNT";
2454 $self->pidl_hdr("#define NDR_" . uc $interface->{NAME} . "_CALL_COUNT ($val)");
2458 sub ParseTypePush($$$$$$)
2460 my ($self,$e, $ndr, $varname, $primitives, $deferred) = @_;
2462 # save the old relative_base_offset
2463 $self->pidl("uint32_t _save_relative_base_offset = ndr_push_get_relative_base_offset($ndr);") if defined(has_property($e, "relative_base"));
2464 $typefamily{$e->{TYPE}}->{PUSH_FN_BODY}->($self, $e, $ndr, $varname);
2465 # restore the old relative_base_offset
2466 $self->pidl("ndr_push_restore_relative_base_offset($ndr, _save_relative_base_offset);") if defined(has_property($e, "relative_base"));
2469 sub ParseTypePushFunction($$$)
2471 my ($self, $e, $varname) = @_;
2472 my $ndr = "ndr";
2474 my $args = $typefamily{$e->{TYPE}}->{DECL}->($e, "push", $e->{NAME}, $varname);
2476 $self->fn_declare("push", $e, "enum ndr_err_code ".TypeFunctionName("ndr_push", $e)."(struct ndr_push *$ndr, int ndr_flags, $args)") or return;
2478 $self->pidl("{");
2479 $self->indent;
2480 $self->ParseTypePush($e, $ndr, $varname, 1, 1);
2481 $self->pidl("return NDR_ERR_SUCCESS;");
2482 $self->deindent;
2483 $self->pidl("}");
2484 $self->pidl("");;
2487 sub ParseTypePull($$$$$$)
2489 my ($self, $e, $ndr, $varname, $primitives, $deferred) = @_;
2491 # save the old relative_base_offset
2492 $self->pidl("uint32_t _save_relative_base_offset = ndr_pull_get_relative_base_offset($ndr);") if defined(has_property($e, "relative_base"));
2493 $typefamily{$e->{TYPE}}->{PULL_FN_BODY}->($self, $e, $ndr, $varname);
2494 # restore the old relative_base_offset
2495 $self->pidl("ndr_pull_restore_relative_base_offset($ndr, _save_relative_base_offset);") if defined(has_property($e, "relative_base"));
2498 sub ParseTypePullFunction($$)
2500 my ($self, $e, $varname) = @_;
2501 my $ndr = "ndr";
2503 my $args = $typefamily{$e->{TYPE}}->{DECL}->($e, "pull", $e->{NAME}, $varname);
2505 $self->fn_declare("pull", $e, "enum ndr_err_code ".TypeFunctionName("ndr_pull", $e)."(struct ndr_pull *$ndr, int ndr_flags, $args)") or return;
2507 $self->pidl("{");
2508 $self->indent;
2509 $self->ParseTypePull($e, $ndr, $varname, 1, 1);
2510 $self->pidl("return NDR_ERR_SUCCESS;");
2511 $self->deindent;
2512 $self->pidl("}");
2513 $self->pidl("");
2516 sub ParseTypePrint($$$$)
2518 my ($self, $e, $ndr, $varname) = @_;
2520 $typefamily{$e->{TYPE}}->{PRINT_FN_BODY}->($self, $e, $ndr, $e->{NAME}, $varname);
2523 sub ParseTypePrintFunction($$$)
2525 my ($self, $e, $varname) = @_;
2526 my $ndr = "ndr";
2528 my $args = $typefamily{$e->{TYPE}}->{DECL}->($e, "print", $e->{NAME}, $varname);
2530 $self->pidl_hdr("void ".TypeFunctionName("ndr_print", $e)."(struct ndr_print *ndr, const char *name, $args);");
2532 return if (has_property($e, "noprint"));
2534 $self->pidl("_PUBLIC_ void ".TypeFunctionName("ndr_print", $e)."(struct ndr_print *$ndr, const char *name, $args)");
2535 $self->pidl("{");
2536 $self->indent;
2537 $self->ParseTypePrint($e, $ndr, $varname);
2538 $self->deindent;
2539 $self->pidl("}");
2540 $self->pidl("");
2543 sub ParseTypeNdrSize($$)
2545 my ($self,$t) = @_;
2547 my $varname = "r";
2548 my $tf = $typefamily{$t->{TYPE}};
2549 my $args = $tf->{SIZE_FN_ARGS}->($t, $t->{NAME}, $varname);
2551 $self->fn_declare("size", $t, "size_t ndr_size_$t->{NAME}($args)") or return;
2553 $self->pidl("{");
2554 $self->indent;
2555 $typefamily{$t->{TYPE}}->{SIZE_FN_BODY}->($self,$t, $t->{NAME}, $varname);
2556 $self->deindent;
2557 $self->pidl("}");
2558 $self->pidl("");
2561 #####################################################################
2562 # parse the interface definitions
2563 sub ParseInterface($$$)
2565 my($self,$interface,$needed) = @_;
2567 $self->pidl_hdr("#ifndef _HEADER_NDR_$interface->{NAME}");
2568 $self->pidl_hdr("#define _HEADER_NDR_$interface->{NAME}");
2570 $self->pidl_hdr("");
2572 $self->HeaderInterface($interface, $needed);
2574 # Typedefs
2575 foreach my $d (@{$interface->{TYPES}}) {
2576 next unless(typeHasBody($d));
2578 ($needed->{TypeFunctionName("ndr_push", $d)}) && $self->ParseTypePushFunction($d, "r");
2579 ($needed->{TypeFunctionName("ndr_pull", $d)}) && $self->ParseTypePullFunction($d, "r");
2580 ($needed->{TypeFunctionName("ndr_print", $d)}) && $self->ParseTypePrintFunction($d, "r");
2582 # Make sure we don't generate a function twice...
2583 $needed->{TypeFunctionName("ndr_push", $d)} =
2584 $needed->{TypeFunctionName("ndr_pull", $d)} =
2585 $needed->{TypeFunctionName("ndr_print", $d)} = 0;
2587 ($needed->{"ndr_size_$d->{NAME}"}) && $self->ParseTypeNdrSize($d);
2590 # Functions
2591 foreach my $d (@{$interface->{FUNCTIONS}}) {
2592 ($needed->{"ndr_push_$d->{NAME}"}) && $self->ParseFunctionPush($d);
2593 ($needed->{"ndr_pull_$d->{NAME}"}) && $self->ParseFunctionPull($d);
2594 ($needed->{"ndr_print_$d->{NAME}"}) && $self->ParseFunctionPrint($d);
2596 # Make sure we don't generate a function twice...
2597 $needed->{"ndr_push_$d->{NAME}"} = $needed->{"ndr_pull_$d->{NAME}"} =
2598 $needed->{"ndr_print_$d->{NAME}"} = 0;
2601 $self->FunctionTable($interface);
2603 $self->pidl_hdr("#endif /* _HEADER_NDR_$interface->{NAME} */");
2606 sub GenerateIncludes($)
2608 my ($self) = @_;
2609 if (is_intree()) {
2610 $self->pidl("#include \"includes.h\"");
2611 } else {
2612 $self->pidl("#ifndef _GNU_SOURCE");
2613 $self->pidl("#define _GNU_SOURCE");
2614 $self->pidl("#endif");
2615 $self->pidl("#include <stdint.h>");
2616 $self->pidl("#include <stdlib.h>");
2617 $self->pidl("#include <stdio.h>");
2618 $self->pidl("#include <stdbool.h>");
2619 $self->pidl("#include <stdarg.h>");
2620 $self->pidl("#include <string.h>");
2624 #####################################################################
2625 # parse a parsed IDL structure back into an IDL file
2626 sub Parse($$$$)
2628 my($self, $ndr,$gen_header,$ndr_header) = @_;
2630 $self->pidl_hdr("/* header auto-generated by pidl */");
2631 $self->pidl_hdr("");
2632 $self->pidl_hdr(choose_header("librpc/ndr/libndr.h", "ndr.h"));
2633 $self->pidl_hdr("#include \"$gen_header\"") if ($gen_header);
2634 $self->pidl_hdr("");
2636 $self->pidl("/* parser auto-generated by pidl */");
2637 $self->pidl("");
2638 $self->GenerateIncludes();
2639 $self->pidl("#include \"$ndr_header\"") if ($ndr_header);
2640 $self->pidl("");
2642 my %needed = ();
2644 foreach (@{$ndr}) {
2645 ($_->{TYPE} eq "INTERFACE") && NeededInterface($_, \%needed);
2648 foreach (@{$ndr}) {
2649 ($_->{TYPE} eq "INTERFACE") && $self->ParseInterface($_, \%needed);
2650 ($_->{TYPE} eq "IMPORT") && $self->HeaderImport(@{$_->{PATHS}});
2651 ($_->{TYPE} eq "INCLUDE") && $self->HeaderInclude(@{$_->{PATHS}});
2654 return ($self->{res_hdr}, $self->{res});
2657 sub NeededElement($$$)
2659 my ($e, $dir, $needed) = @_;
2661 return if ($e->{TYPE} eq "EMPTY");
2663 return if (ref($e->{TYPE}) eq "HASH" and
2664 not defined($e->{TYPE}->{NAME}));
2666 my ($t, $rt);
2667 if (ref($e->{TYPE}) eq "HASH") {
2668 $t = $e->{TYPE}->{TYPE}."_".$e->{TYPE}->{NAME};
2669 } else {
2670 $t = $e->{TYPE};
2673 if (ref($e->{REPRESENTATION_TYPE}) eq "HASH") {
2674 $rt = $e->{REPRESENTATION_TYPE}->{TYPE}."_".$e->{REPRESENTATION_TYPE}->{NAME};
2675 } else {
2676 $rt = $e->{REPRESENTATION_TYPE};
2679 die ("$e->{NAME} $t, $rt FOO") unless ($rt ne "");
2681 my @fn = ();
2682 if ($dir eq "print") {
2683 push(@fn, TypeFunctionName("ndr_print", $e->{REPRESENTATION_TYPE}));
2684 } elsif ($dir eq "pull") {
2685 push (@fn, TypeFunctionName("ndr_pull", $e->{TYPE}));
2686 push (@fn, "ndr_$t\_to_$rt")
2687 if ($rt ne $t);
2688 } elsif ($dir eq "push") {
2689 push (@fn, TypeFunctionName("ndr_push", $e->{TYPE}));
2690 push (@fn, "ndr_$rt\_to_$t")
2691 if ($rt ne $t);
2692 } else {
2693 die("invalid direction `$dir'");
2696 foreach (@fn) {
2697 unless (defined($needed->{$_})) {
2698 $needed->{$_} = 1;
2703 sub NeededFunction($$)
2705 my ($fn,$needed) = @_;
2706 $needed->{"ndr_pull_$fn->{NAME}"} = 1;
2707 $needed->{"ndr_push_$fn->{NAME}"} = 1;
2708 $needed->{"ndr_print_$fn->{NAME}"} = 1;
2709 foreach my $e (@{$fn->{ELEMENTS}}) {
2710 $e->{PARENT} = $fn;
2711 NeededElement($e, $_, $needed) foreach ("pull", "push", "print");
2715 sub NeededType($$$)
2717 sub NeededType($$$);
2718 my ($t,$needed,$req) = @_;
2720 NeededType($t->{DATA}, $needed, $req) if ($t->{TYPE} eq "TYPEDEF");
2722 if ($t->{TYPE} eq "STRUCT" or $t->{TYPE} eq "UNION") {
2723 return unless defined($t->{ELEMENTS});
2724 for my $e (@{$t->{ELEMENTS}}) {
2725 $e->{PARENT} = $t;
2726 if (has_property($e, "compression")) {
2727 $needed->{"compression"} = 1;
2729 NeededElement($e, $req, $needed);
2730 NeededType($e->{TYPE}, $needed, $req) if (ref($e->{TYPE}) eq "HASH");
2735 #####################################################################
2736 # work out what parse functions are needed
2737 sub NeededInterface($$)
2739 my ($interface,$needed) = @_;
2740 NeededFunction($_, $needed) foreach (@{$interface->{FUNCTIONS}});
2741 foreach (reverse @{$interface->{TYPES}}) {
2742 if (has_property($_, "public")) {
2743 $needed->{TypeFunctionName("ndr_pull", $_)} = $needed->{TypeFunctionName("ndr_push", $_)} =
2744 $needed->{TypeFunctionName("ndr_print", $_)} = 1;
2747 NeededType($_, $needed, "pull") if ($needed->{TypeFunctionName("ndr_pull", $_)});
2748 NeededType($_, $needed, "push") if ($needed->{TypeFunctionName("ndr_push", $_)});
2749 NeededType($_, $needed, "print") if ($needed->{TypeFunctionName("ndr_print", $_)});
2750 if (has_property($_, "gensize")) {
2751 $needed->{"ndr_size_$_->{NAME}"} = 1;
2756 sub TypeFunctionName($$)
2758 my ($prefix, $t) = @_;
2760 return "$prefix\_$t->{NAME}" if (ref($t) eq "HASH" and
2761 $t->{TYPE} eq "TYPEDEF");
2762 return "$prefix\_$t->{TYPE}_$t->{NAME}" if (ref($t) eq "HASH");
2763 return "$prefix\_$t";