diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-10 20:34:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-10 20:34:10 +0000 |
commit | e4ba6dbc3f1e76890b22773807ea37fe8fa2b1bc (patch) | |
tree | 68cb5ef9081156392f1dd62a00c6ccc1451b93df /tools/pidl/lib/Parse/Pidl | |
parent | Initial commit. (diff) | |
download | wireshark-e4ba6dbc3f1e76890b22773807ea37fe8fa2b1bc.tar.xz wireshark-e4ba6dbc3f1e76890b22773807ea37fe8fa2b1bc.zip |
Adding upstream version 4.2.2.upstream/4.2.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
25 files changed, 18092 insertions, 0 deletions
diff --git a/tools/pidl/lib/Parse/Pidl.pm b/tools/pidl/lib/Parse/Pidl.pm new file mode 100644 index 00000000..40e36739 --- /dev/null +++ b/tools/pidl/lib/Parse/Pidl.pm @@ -0,0 +1,44 @@ +################################################### +# package to parse IDL files and generate code for +# rpc functions in Samba +# Copyright tridge@samba.org 2000-2003 +# Copyright jelmer@samba.org 2005 +# released under the GNU GPL + +package Parse::Pidl; + +require Exporter; +@ISA = qw(Exporter); +@EXPORT_OK = qw(warning error fatal $VERSION); + +use strict; + +use vars qw ( $VERSION ); + +$VERSION = '0.02'; + +sub warning +{ + my ($l,$m) = @_; + if ($l) { + print STDERR "$l->{FILE}:$l->{LINE}: "; + } + print STDERR "warning: $m\n"; +} + +sub error +{ + my ($l,$m) = @_; + if ($l) { + print STDERR "$l->{FILE}:$l->{LINE}: "; + } + print STDERR "error: $m\n"; +} + +sub fatal($$) +{ + my ($e,$s) = @_; + die("$e->{FILE}:$e->{LINE}: $s\n"); +} + +1; diff --git a/tools/pidl/lib/Parse/Pidl/CUtil.pm b/tools/pidl/lib/Parse/Pidl/CUtil.pm new file mode 100644 index 00000000..9deb6ee1 --- /dev/null +++ b/tools/pidl/lib/Parse/Pidl/CUtil.pm @@ -0,0 +1,52 @@ +################################################### +# C utility functions for pidl +# Copyright jelmer@samba.org 2005-2007 +# released under the GNU GPL +package Parse::Pidl::CUtil; + +require Exporter; +@ISA = qw(Exporter); +@EXPORT = qw(get_pointer_to get_value_of get_array_element); +use vars qw($VERSION); +$VERSION = '0.01'; + +use strict; + +sub get_pointer_to($) +{ + my $var_name = shift; + + if ($var_name =~ /^\*(.*)$/) { + return $1; + } elsif ($var_name =~ /^\&(.*)$/) { + return "&($var_name)"; + } else { + return "&$var_name"; + } +} + +sub get_value_of($) +{ + my $var_name = shift; + + if ($var_name =~ /^\&(.*)$/) { + return $1; + } else { + return "*$var_name"; + } +} + +sub get_array_element($$) +{ + my ($var_name, $idx) = @_; + + if ($var_name =~ /^\*.*$/) { + $var_name = "($var_name)"; + } elsif ($var_name =~ /^\&.*$/) { + $var_name = "($var_name)"; + } + + return "$var_name"."[$idx]"; +} + +1; diff --git a/tools/pidl/lib/Parse/Pidl/Compat.pm b/tools/pidl/lib/Parse/Pidl/Compat.pm new file mode 100644 index 00000000..b8abcb88 --- /dev/null +++ b/tools/pidl/lib/Parse/Pidl/Compat.pm @@ -0,0 +1,168 @@ +################################################### +# IDL Compatibility checker +# Copyright jelmer@samba.org 2005 +# released under the GNU GPL + +package Parse::Pidl::Compat; + +use Parse::Pidl qw(warning); +use Parse::Pidl::Util qw(has_property); +use strict; + +use vars qw($VERSION); +$VERSION = '0.01'; + +my %supported_properties = ( + # interface + "helpstring" => ["INTERFACE", "FUNCTION"], + "version" => ["INTERFACE"], + "uuid" => ["INTERFACE"], + "endpoint" => ["INTERFACE"], + "pointer_default" => ["INTERFACE"], + "no_srv_register" => ["INTERFACE"], + + # dcom + "object" => ["INTERFACE"], + "local" => ["INTERFACE", "FUNCTION"], + "iid_is" => ["ELEMENT"], + "call_as" => ["FUNCTION"], + "idempotent" => ["FUNCTION"], + + # function + "in" => ["ELEMENT"], + "out" => ["ELEMENT"], + + # pointer + "ref" => ["ELEMENT"], + "ptr" => ["ELEMENT"], + "unique" => ["ELEMENT"], + "ignore" => ["ELEMENT"], + + "value" => ["ELEMENT"], + + # generic + "public" => ["FUNCTION", "TYPEDEF"], + "nopush" => ["FUNCTION", "TYPEDEF"], + "nopull" => ["FUNCTION", "TYPEDEF"], + "noprint" => ["FUNCTION", "TYPEDEF"], + "nopython" => ["FUNCTION", "TYPEDEF"], + + # union + "switch_is" => ["ELEMENT"], + "switch_type" => ["ELEMENT", "TYPEDEF"], + "case" => ["ELEMENT"], + "default" => ["ELEMENT"], + + # subcontext + "subcontext" => ["ELEMENT"], + "subcontext_size" => ["ELEMENT"], + + # enum + "enum16bit" => ["TYPEDEF"], + "v1_enum" => ["TYPEDEF"], + + # bitmap + "bitmap8bit" => ["TYPEDEF"], + "bitmap16bit" => ["TYPEDEF"], + "bitmap32bit" => ["TYPEDEF"], + "bitmap64bit" => ["TYPEDEF"], + + # array + "range" => ["ELEMENT"], + "size_is" => ["ELEMENT"], + "string" => ["ELEMENT"], + "noheader" => ["ELEMENT"], + "charset" => ["ELEMENT"], + "length_is" => ["ELEMENT"], +); + +sub CheckTypedef($) +{ + my ($td) = @_; + + if (has_property($td, "nodiscriminant")) { + warning($td, "nodiscriminant property not supported"); + } + + if ($td->{TYPE} eq "BITMAP") { + warning($td, "converting bitmap to scalar"); + #FIXME + } + + if (has_property($td, "gensize")) { + warning($td, "ignoring gensize() property. "); + } + + if (has_property($td, "enum8bit") and has_property($td, "enum16bit")) { + warning($td, "8 and 16 bit enums not supported, converting to scalar"); + #FIXME + } + + StripProperties($td); +} + +sub CheckElement($) +{ + my $e = shift; + + if (has_property($e, "noheader")) { + warning($e, "noheader property not supported"); + return; + } + + if (has_property($e, "subcontext")) { + warning($e, "converting subcontext to byte array"); + #FIXME + } + + if (has_property($e, "compression")) { + warning($e, "compression() property not supported"); + } + + if (has_property($e, "sptr")) { + warning($e, "sptr() pointer property not supported"); + } + + if (has_property($e, "relative")) { + warning($e, "relative() pointer property not supported"); + } + + if (has_property($e, "relative_short")) { + warning($e, "relative_short() pointer property not supported"); + } + + if (has_property($e, "flag")) { + warning($e, "ignoring flag() property"); + } + + if (has_property($e, "value")) { + warning($e, "ignoring value() property"); + } +} + +sub CheckFunction($) +{ + my $fn = shift; + + if (has_property($fn, "noopnum")) { + warning($fn, "noopnum not converted. Opcodes will be out of sync."); + } +} + +sub CheckInterface($) +{ + my $if = shift; + +} + +sub Check($) +{ + my $pidl = shift; + my $nidl = []; + + foreach (@{$pidl}) { + push (@$nidl, CheckInterface($_)) if ($_->{TYPE} eq "INTERFACE"); + } +} + +1; diff --git a/tools/pidl/lib/Parse/Pidl/Dump.pm b/tools/pidl/lib/Parse/Pidl/Dump.pm new file mode 100644 index 00000000..4e623db6 --- /dev/null +++ b/tools/pidl/lib/Parse/Pidl/Dump.pm @@ -0,0 +1,294 @@ +################################################### +# dump function for IDL structures +# Copyright tridge@samba.org 2000 +# Copyright jelmer@samba.org 2005 +# released under the GNU GPL + +=pod + +=head1 NAME + +Parse::Pidl::Dump - Dump support + +=head1 DESCRIPTION + +This module provides functions that can generate IDL code from +internal pidl data structures. + +=cut + +package Parse::Pidl::Dump; + +use Exporter; + +use vars qw($VERSION); +$VERSION = '0.01'; +@ISA = qw(Exporter); +@EXPORT_OK = qw(DumpType DumpTypedef DumpStruct DumpEnum DumpBitmap DumpUnion DumpFunction); + +use strict; +use Parse::Pidl::Util qw(has_property); + +my($res); + +##################################################################### +# dump a properties list +sub DumpProperties($) +{ + my($props) = shift; + my $res = ""; + + foreach my $d ($props) { + foreach my $k (sort(keys %{$d})) { + if ($k eq "in") { + $res .= "[in] "; + next; + } + if ($k eq "out") { + $res .= "[out] "; + next; + } + if ($k eq "ref") { + $res .= "[ref] "; + next; + } + $res .= "[$k($d->{$k})] "; + } + } + return $res; +} + +##################################################################### +# dump a structure element +sub DumpElement($) +{ + my($element) = shift; + my $res = ""; + + (defined $element->{PROPERTIES}) && + ($res .= DumpProperties($element->{PROPERTIES})); + $res .= DumpType($element->{TYPE}); + $res .= " "; + for my $i (1..$element->{POINTERS}) { + $res .= "*"; + } + $res .= "$element->{NAME}"; + foreach (@{$element->{ARRAY_LEN}}) { + $res .= "[$_]"; + } + + return $res; +} + +##################################################################### +# dump a struct +sub DumpStruct($) +{ + my($struct) = shift; + my($res); + + $res .= "struct "; + if ($struct->{NAME}) { + $res.="$struct->{NAME} "; + } + + $res.="{\n"; + if (defined $struct->{ELEMENTS}) { + foreach (@{$struct->{ELEMENTS}}) { + $res .= "\t" . DumpElement($_) . ";\n"; + } + } + $res .= "}"; + + return $res; +} + + +##################################################################### +# dump a struct +sub DumpEnum($) +{ + my($enum) = shift; + my($res); + + $res .= "enum {\n"; + + foreach (@{$enum->{ELEMENTS}}) { + if (/^([A-Za-z0-9_]+)[ \t]*\((.*)\)$/) { + $res .= "\t$1 = $2,\n"; + } else { + $res .= "\t$_,\n"; + } + } + + $res.= "}"; + + return $res; +} + +##################################################################### +# dump a struct +sub DumpBitmap($) +{ + my($bitmap) = shift; + my($res); + + $res .= "bitmap {\n"; + + foreach (@{$bitmap->{ELEMENTS}}) { + if (/^([A-Za-z0-9_]+)[ \t]*\((.*)\)$/) { + $res .= "\t$1 = $2,\n"; + } else { + die ("Bitmap $bitmap->{NAME} has field $_ without proper value"); + } + } + + $res.= "}"; + + return $res; +} + + +##################################################################### +# dump a union element +sub DumpUnionElement($) +{ + my($element) = shift; + my($res); + + if (has_property($element, "default")) { + $res .= "[default] ;\n"; + } else { + $res .= "[case($element->{PROPERTIES}->{case})] "; + $res .= DumpElement($element), if defined($element); + $res .= ";\n"; + } + + return $res; +} + +##################################################################### +# dump a union +sub DumpUnion($) +{ + my($union) = shift; + my($res); + + (defined $union->{PROPERTIES}) && + ($res .= DumpProperties($union->{PROPERTIES})); + $res .= "union {\n"; + foreach my $e (@{$union->{ELEMENTS}}) { + $res .= DumpUnionElement($e); + } + $res .= "}"; + + return $res; +} + +##################################################################### +# dump a type +sub DumpType($) +{ + my($data) = shift; + + if (ref($data) eq "HASH") { + return DumpStruct($data) if ($data->{TYPE} eq "STRUCT"); + return DumpUnion($data) if ($data->{TYPE} eq "UNION"); + return DumpEnum($data) if ($data->{TYPE} eq "ENUM"); + return DumpBitmap($data) if ($data->{TYPE} eq "BITMAP"); + } else { + return $data; + } +} + +##################################################################### +# dump a typedef +sub DumpTypedef($) +{ + my($typedef) = shift; + my($res); + + $res .= "typedef "; + $res .= DumpType($typedef->{DATA}); + $res .= " $typedef->{NAME};\n\n"; + + return $res; +} + +##################################################################### +# dump a typedef +sub DumpFunction($) +{ + my($function) = shift; + my($first) = 1; + my($res); + + $res .= DumpType($function->{RETURN_TYPE}); + $res .= " $function->{NAME}(\n"; + for my $d (@{$function->{ELEMENTS}}) { + unless ($first) { $res .= ",\n"; } $first = 0; + $res .= DumpElement($d); + } + $res .= "\n);\n\n"; + + return $res; +} + +##################################################################### +# dump a module header +sub DumpInterfaceProperties($) +{ + my($header) = shift; + my($data) = $header->{DATA}; + my($first) = 1; + my($res); + + $res .= "[\n"; + foreach my $k (sort(keys %{$data})) { + $first || ($res .= ",\n"); $first = 0; + $res .= "$k($data->{$k})"; + } + $res .= "\n]\n"; + + return $res; +} + +##################################################################### +# dump the interface definitions +sub DumpInterface($) +{ + my($interface) = shift; + my($data) = $interface->{DATA}; + my($res); + + $res .= DumpInterfaceProperties($interface->{PROPERTIES}); + + $res .= "interface $interface->{NAME}\n{\n"; + foreach my $d (@{$data}) { + ($d->{TYPE} eq "TYPEDEF") && + ($res .= DumpTypedef($d)); + ($d->{TYPE} eq "FUNCTION") && + ($res .= DumpFunction($d)); + } + $res .= "}\n"; + + return $res; +} + + +##################################################################### +# dump a parsed IDL structure back into an IDL file +sub Dump($) +{ + my($idl) = shift; + my($res); + + $res = "/* Dumped by pidl */\n\n"; + foreach my $x (@{$idl}) { + ($x->{TYPE} eq "INTERFACE") && + ($res .= DumpInterface($x)); + } + return $res; +} + +1; diff --git a/tools/pidl/lib/Parse/Pidl/Expr.pm b/tools/pidl/lib/Parse/Pidl/Expr.pm new file mode 100644 index 00000000..24581d29 --- /dev/null +++ b/tools/pidl/lib/Parse/Pidl/Expr.pm @@ -0,0 +1,1444 @@ +#################################################################### +# +# This file was generated using Parse::Yapp version 1.05. +# +# Don't edit this file, use source file instead. +# +# ANY CHANGE MADE HERE WILL BE LOST ! +# +#################################################################### +package Parse::Pidl::Expr; +use vars qw ( @ISA ); +use strict; + +@ISA= qw ( Parse::Yapp::Driver ); +use Parse::Yapp::Driver; + + + +sub new { + my($class)=shift; + ref($class) + and $class=ref($class); + + my($self)=$class->SUPER::new( yyversion => '1.05', + yystates => +[ + {#State 0 + ACTIONS => { + "-" => 1, + "~" => 3, + "&" => 4, + 'NUM' => 5, + 'TEXT' => 6, + "(" => 7, + "!" => 8, + "*" => 9, + 'VAR' => 10 + }, + GOTOS => { + 'exp' => 2, + 'var' => 11, + 'func' => 12, + 'possible_pointer' => 13 + } + }, + {#State 1 + ACTIONS => { + "-" => 1, + "~" => 3, + "&" => 4, + 'TEXT' => 6, + 'NUM' => 5, + "(" => 7, + "!" => 8, + "*" => 9, + 'VAR' => 10 + }, + GOTOS => { + 'exp' => 14, + 'var' => 11, + 'func' => 12, + 'possible_pointer' => 13 + } + }, + {#State 2 + ACTIONS => { + '' => 16, + "-" => 15, + "<" => 17, + "+" => 18, + "%" => 19, + "==" => 20, + "^" => 21, + "*" => 22, + ">>" => 23, + "!=" => 24, + "?" => 25, + "||" => 26, + "&&" => 27, + "&" => 28, + "/" => 29, + "|" => 30, + "<<" => 32, + "=>" => 31, + "<=" => 33, + ">" => 34 + } + }, + {#State 3 + ACTIONS => { + "-" => 1, + "~" => 3, + "&" => 4, + 'TEXT' => 6, + 'NUM' => 5, + "!" => 8, + "(" => 7, + "*" => 9, + 'VAR' => 10 + }, + GOTOS => { + 'exp' => 35, + 'var' => 11, + 'func' => 12, + 'possible_pointer' => 13 + } + }, + {#State 4 + ACTIONS => { + "-" => 1, + "~" => 3, + "&" => 4, + 'TEXT' => 6, + 'NUM' => 5, + "!" => 8, + "(" => 7, + "*" => 9, + 'VAR' => 10 + }, + GOTOS => { + 'exp' => 36, + 'var' => 11, + 'func' => 12, + 'possible_pointer' => 13 + } + }, + {#State 5 + DEFAULT => -1 + }, + {#State 6 + DEFAULT => -2 + }, + {#State 7 + ACTIONS => { + "-" => 1, + "~" => 3, + "&" => 4, + 'TEXT' => 6, + 'NUM' => 5, + "!" => 8, + "(" => 7, + "*" => 9, + 'VAR' => 10 + }, + GOTOS => { + 'exp' => 38, + 'var' => 37, + 'func' => 12, + 'possible_pointer' => 13 + } + }, + {#State 8 + ACTIONS => { + "-" => 1, + "~" => 3, + "&" => 4, + 'TEXT' => 6, + 'NUM' => 5, + "!" => 8, + "(" => 7, + "*" => 9, + 'VAR' => 10 + }, + GOTOS => { + 'exp' => 39, + 'var' => 11, + 'func' => 12, + 'possible_pointer' => 13 + } + }, + {#State 9 + ACTIONS => { + "*" => 9, + 'VAR' => 41 + }, + GOTOS => { + 'possible_pointer' => 40 + } + }, + {#State 10 + ACTIONS => { + "(" => 42 + }, + DEFAULT => -30 + }, + {#State 11 + ACTIONS => { + "->" => 43, + "." => 44 + }, + DEFAULT => -4 + }, + {#State 12 + DEFAULT => -3 + }, + {#State 13 + DEFAULT => -32 + }, + {#State 14 + ACTIONS => { + "^" => 21, + "=>" => 31, + "<=" => 33 + }, + DEFAULT => -26 + }, + {#State 15 + ACTIONS => { + "-" => 1, + "~" => 3, + "&" => 4, + 'TEXT' => 6, + 'NUM' => 5, + "!" => 8, + "(" => 7, + "*" => 9, + 'VAR' => 10 + }, + GOTOS => { + 'exp' => 45, + 'var' => 11, + 'func' => 12, + 'possible_pointer' => 13 + } + }, + {#State 16 + DEFAULT => 0 + }, + {#State 17 + ACTIONS => { + "-" => 1, + "~" => 3, + "&" => 4, + 'TEXT' => 6, + 'NUM' => 5, + "!" => 8, + "(" => 7, + "*" => 9, + 'VAR' => 10 + }, + GOTOS => { + 'exp' => 46, + 'var' => 11, + 'func' => 12, + 'possible_pointer' => 13 + } + }, + {#State 18 + ACTIONS => { + "-" => 1, + "~" => 3, + "&" => 4, + 'TEXT' => 6, + 'NUM' => 5, + "!" => 8, + "(" => 7, + "*" => 9, + 'VAR' => 10 + }, + GOTOS => { + 'exp' => 47, + 'var' => 11, + 'func' => 12, + 'possible_pointer' => 13 + } + }, + {#State 19 + ACTIONS => { + "-" => 1, + "~" => 3, + "&" => 4, + 'TEXT' => 6, + 'NUM' => 5, + "!" => 8, + "(" => 7, + "*" => 9, + 'VAR' => 10 + }, + GOTOS => { + 'exp' => 48, + 'var' => 11, + 'func' => 12, + 'possible_pointer' => 13 + } + }, + {#State 20 + ACTIONS => { + "-" => 1, + "~" => 3, + "&" => 4, + 'TEXT' => 6, + 'NUM' => 5, + "!" => 8, + "(" => 7, + "*" => 9, + 'VAR' => 10 + }, + GOTOS => { + 'exp' => 49, + 'var' => 11, + 'func' => 12, + 'possible_pointer' => 13 + } + }, + {#State 21 + ACTIONS => { + "-" => 1, + "~" => 3, + "&" => 4, + 'TEXT' => 6, + 'NUM' => 5, + "!" => 8, + "(" => 7, + "*" => 9, + 'VAR' => 10 + }, + GOTOS => { + 'exp' => 50, + 'var' => 11, + 'func' => 12, + 'possible_pointer' => 13 + } + }, + {#State 22 + ACTIONS => { + "-" => 1, + "~" => 3, + "&" => 4, + 'TEXT' => 6, + 'NUM' => 5, + "!" => 8, + "(" => 7, + "*" => 9, + 'VAR' => 10 + }, + GOTOS => { + 'exp' => 51, + 'var' => 11, + 'func' => 12, + 'possible_pointer' => 13 + } + }, + {#State 23 + ACTIONS => { + "-" => 1, + "~" => 3, + "&" => 4, + 'TEXT' => 6, + 'NUM' => 5, + "!" => 8, + "(" => 7, + "*" => 9, + 'VAR' => 10 + }, + GOTOS => { + 'exp' => 52, + 'var' => 11, + 'func' => 12, + 'possible_pointer' => 13 + } + }, + {#State 24 + ACTIONS => { + "-" => 1, + "~" => 3, + "&" => 4, + 'TEXT' => 6, + 'NUM' => 5, + "!" => 8, + "(" => 7, + "*" => 9, + 'VAR' => 10 + }, + GOTOS => { + 'exp' => 53, + 'var' => 11, + 'func' => 12, + 'possible_pointer' => 13 + } + }, + {#State 25 + ACTIONS => { + "-" => 1, + "~" => 3, + "&" => 4, + 'TEXT' => 6, + 'NUM' => 5, + "!" => 8, + "(" => 7, + "*" => 9, + 'VAR' => 10 + }, + GOTOS => { + 'exp' => 54, + 'var' => 11, + 'func' => 12, + 'possible_pointer' => 13 + } + }, + {#State 26 + ACTIONS => { + "-" => 1, + "~" => 3, + "&" => 4, + 'TEXT' => 6, + 'NUM' => 5, + "!" => 8, + "(" => 7, + "*" => 9, + 'VAR' => 10 + }, + GOTOS => { + 'exp' => 55, + 'var' => 11, + 'func' => 12, + 'possible_pointer' => 13 + } + }, + {#State 27 + ACTIONS => { + "-" => 1, + "~" => 3, + "&" => 4, + 'TEXT' => 6, + 'NUM' => 5, + "!" => 8, + "(" => 7, + "*" => 9, + 'VAR' => 10 + }, + GOTOS => { + 'exp' => 56, + 'var' => 11, + 'func' => 12, + 'possible_pointer' => 13 + } + }, + {#State 28 + ACTIONS => { + "-" => 1, + "~" => 3, + "&" => 4, + 'TEXT' => 6, + 'NUM' => 5, + "!" => 8, + "(" => 7, + "*" => 9, + 'VAR' => 10 + }, + GOTOS => { + 'exp' => 57, + 'var' => 11, + 'func' => 12, + 'possible_pointer' => 13 + } + }, + {#State 29 + ACTIONS => { + "-" => 1, + "~" => 3, + "&" => 4, + 'TEXT' => 6, + 'NUM' => 5, + "!" => 8, + "(" => 7, + "*" => 9, + 'VAR' => 10 + }, + GOTOS => { + 'exp' => 58, + 'var' => 11, + 'func' => 12, + 'possible_pointer' => 13 + } + }, + {#State 30 + ACTIONS => { + "-" => 1, + "~" => 3, + "&" => 4, + 'TEXT' => 6, + 'NUM' => 5, + "!" => 8, + "(" => 7, + "*" => 9, + 'VAR' => 10 + }, + GOTOS => { + 'exp' => 59, + 'var' => 11, + 'func' => 12, + 'possible_pointer' => 13 + } + }, + {#State 31 + ACTIONS => { + "-" => 1, + "~" => 3, + "&" => 4, + 'TEXT' => 6, + 'NUM' => 5, + "!" => 8, + "(" => 7, + "*" => 9, + 'VAR' => 10 + }, + GOTOS => { + 'exp' => 60, + 'var' => 11, + 'func' => 12, + 'possible_pointer' => 13 + } + }, + {#State 32 + ACTIONS => { + "-" => 1, + "~" => 3, + "&" => 4, + 'TEXT' => 6, + 'NUM' => 5, + "!" => 8, + "(" => 7, + "*" => 9, + 'VAR' => 10 + }, + GOTOS => { + 'exp' => 61, + 'var' => 11, + 'func' => 12, + 'possible_pointer' => 13 + } + }, + {#State 33 + ACTIONS => { + "-" => 1, + "~" => 3, + "&" => 4, + 'TEXT' => 6, + 'NUM' => 5, + "!" => 8, + "(" => 7, + "*" => 9, + 'VAR' => 10 + }, + GOTOS => { + 'exp' => 62, + 'var' => 11, + 'func' => 12, + 'possible_pointer' => 13 + } + }, + {#State 34 + ACTIONS => { + "-" => 1, + "~" => 3, + "&" => 4, + 'TEXT' => 6, + 'NUM' => 5, + "!" => 8, + "(" => 7, + "*" => 9, + 'VAR' => 10 + }, + GOTOS => { + 'exp' => 63, + 'var' => 11, + 'func' => 12, + 'possible_pointer' => 13 + } + }, + {#State 35 + ACTIONS => { + "^" => 21, + "=>" => 31, + "<=" => 33 + }, + DEFAULT => -5 + }, + {#State 36 + ACTIONS => { + "^" => 21, + "=>" => 31, + "<=" => 33 + }, + DEFAULT => -27 + }, + {#State 37 + ACTIONS => { + ")" => 64, + "->" => 43, + "." => 44 + }, + DEFAULT => -4 + }, + {#State 38 + ACTIONS => { + "-" => 15, + "<" => 17, + "+" => 18, + "%" => 19, + "==" => 20, + "^" => 21, + "*" => 22, + ")" => 65, + ">>" => 23, + "!=" => 24, + "?" => 25, + "&&" => 27, + "||" => 26, + "&" => 28, + "/" => 29, + "|" => 30, + "=>" => 31, + "<<" => 32, + "<=" => 33, + ">" => 34 + } + }, + {#State 39 + ACTIONS => { + "-" => 15, + "<" => 17, + "+" => 18, + "%" => 19, + "==" => 20, + "^" => 21, + "*" => 22, + ">>" => 23, + "!=" => 24, + "?" => 25, + "&&" => 27, + "||" => 26, + "&" => 28, + "/" => 29, + "|" => 30, + "=>" => 31, + "<<" => 32, + "<=" => 33, + ">" => 34 + }, + DEFAULT => -24 + }, + {#State 40 + DEFAULT => -31 + }, + {#State 41 + DEFAULT => -30 + }, + {#State 42 + ACTIONS => { + "-" => 1, + "~" => 3, + "&" => 4, + 'TEXT' => 6, + 'NUM' => 5, + "!" => 8, + "(" => 7, + "*" => 9, + 'VAR' => 10 + }, + DEFAULT => -37, + GOTOS => { + 'exp' => 69, + 'var' => 11, + 'args' => 66, + 'func' => 12, + 'opt_args' => 70, + 'exp_or_possible_pointer' => 67, + 'possible_pointer' => 68 + } + }, + {#State 43 + ACTIONS => { + 'VAR' => 71 + } + }, + {#State 44 + ACTIONS => { + 'VAR' => 72 + } + }, + {#State 45 + ACTIONS => { + "<" => 17, + "==" => 20, + "^" => 21, + ">>" => 23, + "!=" => 24, + "?" => 25, + "&&" => 27, + "||" => 26, + "&" => 28, + "|" => 30, + "=>" => 31, + "<<" => 32, + "<=" => 33, + ">" => 34 + }, + DEFAULT => -7 + }, + {#State 46 + ACTIONS => { + "==" => 20, + "^" => 21, + "!=" => 24, + "?" => 25, + "&&" => 27, + "||" => 26, + "&" => 28, + "|" => 30, + "=>" => 31, + "<=" => 33 + }, + DEFAULT => -10 + }, + {#State 47 + ACTIONS => { + "<" => 17, + "==" => 20, + "^" => 21, + ">>" => 23, + "!=" => 24, + "?" => 25, + "&&" => 27, + "||" => 26, + "&" => 28, + "|" => 30, + "=>" => 31, + "<<" => 32, + "<=" => 33, + ">" => 34 + }, + DEFAULT => -6 + }, + {#State 48 + ACTIONS => { + "-" => 15, + "<" => 17, + "+" => 18, + "==" => 20, + "^" => 21, + ">>" => 23, + "!=" => 24, + "?" => 25, + "&&" => 27, + "||" => 26, + "&" => 28, + "|" => 30, + "=>" => 31, + "<<" => 32, + "<=" => 33, + ">" => 34 + }, + DEFAULT => -9 + }, + {#State 49 + ACTIONS => { + "^" => 21, + "?" => 25, + "&&" => 27, + "||" => 26, + "&" => 28, + "|" => 30, + "=>" => 31, + "<=" => 33 + }, + DEFAULT => -13 + }, + {#State 50 + ACTIONS => { + "-" => 15, + "<" => 17, + "+" => 18, + "%" => 19, + "==" => 20, + "^" => 21, + "*" => 22, + ">>" => 23, + "!=" => 24, + "?" => 25, + "&&" => 27, + "||" => 26, + "&" => 28, + "/" => 29, + "|" => 30, + "=>" => 31, + "<<" => 32, + "<=" => 33, + ">" => 34 + }, + DEFAULT => -28 + }, + {#State 51 + ACTIONS => { + "-" => 15, + "<" => 17, + "+" => 18, + "==" => 20, + "^" => 21, + ">>" => 23, + "!=" => 24, + "?" => 25, + "&&" => 27, + "||" => 26, + "&" => 28, + "|" => 30, + "=>" => 31, + "<<" => 32, + "<=" => 33, + ">" => 34 + }, + DEFAULT => -8 + }, + {#State 52 + ACTIONS => { + "<" => 17, + "==" => 20, + "^" => 21, + "!=" => 24, + "?" => 25, + "&&" => 27, + "||" => 26, + "&" => 28, + "|" => 30, + "=>" => 31, + "<=" => 33, + ">" => 34 + }, + DEFAULT => -17 + }, + {#State 53 + ACTIONS => { + "^" => 21, + "?" => 25, + "&&" => 27, + "||" => 26, + "&" => 28, + "|" => 30, + "=>" => 31, + "<=" => 33 + }, + DEFAULT => -18 + }, + {#State 54 + ACTIONS => { + ":" => 73, + "-" => 15, + "<" => 17, + "+" => 18, + "%" => 19, + "==" => 20, + "^" => 21, + "*" => 22, + ">>" => 23, + "!=" => 24, + "?" => 25, + "&&" => 27, + "||" => 26, + "&" => 28, + "/" => 29, + "|" => 30, + "=>" => 31, + "<<" => 32, + "<=" => 33, + ">" => 34 + } + }, + {#State 55 + ACTIONS => { + "^" => 21, + "?" => 25, + "=>" => 31, + "<=" => 33 + }, + DEFAULT => -19 + }, + {#State 56 + ACTIONS => { + "^" => 21, + "?" => 25, + "||" => 26, + "=>" => 31, + "<=" => 33 + }, + DEFAULT => -20 + }, + {#State 57 + ACTIONS => { + "^" => 21, + "?" => 25, + "&&" => 27, + "||" => 26, + "|" => 30, + "=>" => 31, + "<=" => 33 + }, + DEFAULT => -21 + }, + {#State 58 + ACTIONS => { + "-" => 15, + "<" => 17, + "+" => 18, + "==" => 20, + "^" => 21, + ">>" => 23, + "!=" => 24, + "?" => 25, + "&&" => 27, + "||" => 26, + "&" => 28, + "|" => 30, + "=>" => 31, + "<<" => 32, + "<=" => 33, + ">" => 34 + }, + DEFAULT => -25 + }, + {#State 59 + ACTIONS => { + "^" => 21, + "?" => 25, + "&&" => 27, + "||" => 26, + "=>" => 31, + "<=" => 33 + }, + DEFAULT => -12 + }, + {#State 60 + ACTIONS => { + "-" => 15, + "<" => 17, + "+" => 18, + "%" => 19, + "==" => 20, + "^" => 21, + "*" => 22, + ">>" => 23, + "!=" => 24, + "?" => 25, + "&&" => 27, + "||" => 26, + "&" => 28, + "/" => 29, + "|" => 30, + "=>" => 31, + "<<" => 32, + "<=" => 33, + ">" => 34 + }, + DEFAULT => -15 + }, + {#State 61 + ACTIONS => { + "<" => 17, + "==" => 20, + "^" => 21, + "!=" => 24, + "?" => 25, + "&&" => 27, + "||" => 26, + "&" => 28, + "|" => 30, + "=>" => 31, + "<=" => 33, + ">" => 34 + }, + DEFAULT => -16 + }, + {#State 62 + ACTIONS => { + "-" => 15, + "<" => 17, + "+" => 18, + "%" => 19, + "==" => 20, + "^" => 21, + "*" => 22, + ">>" => 23, + "!=" => 24, + "?" => 25, + "&&" => 27, + "||" => 26, + "&" => 28, + "/" => 29, + "|" => 30, + "=>" => 31, + "<<" => 32, + "<=" => 33, + ">" => 34 + }, + DEFAULT => -14 + }, + {#State 63 + ACTIONS => { + "==" => 20, + "^" => 21, + "!=" => 24, + "?" => 25, + "&&" => 27, + "||" => 26, + "&" => 28, + "|" => 30, + "=>" => 31, + "<=" => 33 + }, + DEFAULT => -11 + }, + {#State 64 + DEFAULT => -34 + }, + {#State 65 + DEFAULT => -29 + }, + {#State 66 + DEFAULT => -38 + }, + {#State 67 + ACTIONS => { + "," => 74 + }, + DEFAULT => -41 + }, + {#State 68 + DEFAULT => -32 + }, + {#State 69 + ACTIONS => { + "-" => 15, + "<" => 17, + "+" => 18, + "%" => 19, + "==" => 20, + "^" => 21, + "*" => 22, + ">>" => 23, + "!=" => 24, + "?" => 25, + "&&" => 27, + "||" => 26, + "&" => 28, + "/" => 29, + "|" => 30, + "=>" => 31, + "<<" => 32, + "<=" => 33, + ">" => 34 + }, + DEFAULT => -39 + }, + {#State 70 + ACTIONS => { + ")" => 75 + } + }, + {#State 71 + DEFAULT => -35 + }, + {#State 72 + DEFAULT => -33 + }, + {#State 73 + ACTIONS => { + "-" => 1, + "~" => 3, + "&" => 4, + 'TEXT' => 6, + 'NUM' => 5, + "!" => 8, + "(" => 7, + "*" => 9, + 'VAR' => 10 + }, + GOTOS => { + 'exp' => 76, + 'var' => 11, + 'func' => 12, + 'possible_pointer' => 13 + } + }, + {#State 74 + ACTIONS => { + "-" => 1, + "~" => 3, + "&" => 4, + 'TEXT' => 6, + 'NUM' => 5, + "!" => 8, + "(" => 7, + "*" => 9, + 'VAR' => 10 + }, + GOTOS => { + 'exp' => 69, + 'var' => 11, + 'args' => 77, + 'func' => 12, + 'exp_or_possible_pointer' => 67, + 'possible_pointer' => 68 + } + }, + {#State 75 + DEFAULT => -36 + }, + {#State 76 + ACTIONS => { + "^" => 21, + "=>" => 31, + "<=" => 33 + }, + DEFAULT => -22 + }, + {#State 77 + DEFAULT => -42 + } +], + yyrules => +[ + [#Rule 0 + '$start', 2, undef + ], + [#Rule 1 + 'exp', 1, undef + ], + [#Rule 2 + 'exp', 1, +sub +#line 24 "expr.yp" +{ "\"$_[1]\"" } + ], + [#Rule 3 + 'exp', 1, undef + ], + [#Rule 4 + 'exp', 1, undef + ], + [#Rule 5 + 'exp', 2, +sub +#line 30 "expr.yp" +{ "~$_[2]" } + ], + [#Rule 6 + 'exp', 3, +sub +#line 32 "expr.yp" +{ "$_[1] + $_[3]" } + ], + [#Rule 7 + 'exp', 3, +sub +#line 34 "expr.yp" +{ "$_[1] - $_[3]" } + ], + [#Rule 8 + 'exp', 3, +sub +#line 36 "expr.yp" +{ "$_[1] * $_[3]" } + ], + [#Rule 9 + 'exp', 3, +sub +#line 38 "expr.yp" +{ "$_[1] % $_[3]" } + ], + [#Rule 10 + 'exp', 3, +sub +#line 40 "expr.yp" +{ "$_[1] < $_[3]" } + ], + [#Rule 11 + 'exp', 3, +sub +#line 42 "expr.yp" +{ "$_[1] > $_[3]" } + ], + [#Rule 12 + 'exp', 3, +sub +#line 44 "expr.yp" +{ "$_[1] | $_[3]" } + ], + [#Rule 13 + 'exp', 3, +sub +#line 46 "expr.yp" +{ "$_[1] == $_[3]" } + ], + [#Rule 14 + 'exp', 3, +sub +#line 48 "expr.yp" +{ "$_[1] <= $_[3]" } + ], + [#Rule 15 + 'exp', 3, +sub +#line 50 "expr.yp" +{ "$_[1] => $_[3]" } + ], + [#Rule 16 + 'exp', 3, +sub +#line 52 "expr.yp" +{ "$_[1] << $_[3]" } + ], + [#Rule 17 + 'exp', 3, +sub +#line 54 "expr.yp" +{ "$_[1] >> $_[3]" } + ], + [#Rule 18 + 'exp', 3, +sub +#line 56 "expr.yp" +{ "$_[1] != $_[3]" } + ], + [#Rule 19 + 'exp', 3, +sub +#line 58 "expr.yp" +{ "$_[1] || $_[3]" } + ], + [#Rule 20 + 'exp', 3, +sub +#line 60 "expr.yp" +{ "$_[1] && $_[3]" } + ], + [#Rule 21 + 'exp', 3, +sub +#line 62 "expr.yp" +{ "$_[1] & $_[3]" } + ], + [#Rule 22 + 'exp', 5, +sub +#line 64 "expr.yp" +{ "$_[1]?$_[3]:$_[5]" } + ], + [#Rule 23 + 'exp', 2, +sub +#line 66 "expr.yp" +{ "~$_[1]" } + ], + [#Rule 24 + 'exp', 2, +sub +#line 68 "expr.yp" +{ "not $_[1]" } + ], + [#Rule 25 + 'exp', 3, +sub +#line 70 "expr.yp" +{ "$_[1] / $_[3]" } + ], + [#Rule 26 + 'exp', 2, +sub +#line 72 "expr.yp" +{ "-$_[2]" } + ], + [#Rule 27 + 'exp', 2, +sub +#line 74 "expr.yp" +{ "&$_[2]" } + ], + [#Rule 28 + 'exp', 3, +sub +#line 76 "expr.yp" +{ "$_[1]^$_[3]" } + ], + [#Rule 29 + 'exp', 3, +sub +#line 78 "expr.yp" +{ "($_[2])" } + ], + [#Rule 30 + 'possible_pointer', 1, +sub +#line 82 "expr.yp" +{ $_[0]->_Lookup($_[1]) } + ], + [#Rule 31 + 'possible_pointer', 2, +sub +#line 84 "expr.yp" +{ $_[0]->_Dereference($_[2]); "*$_[2]" } + ], + [#Rule 32 + 'var', 1, +sub +#line 88 "expr.yp" +{ $_[0]->_Use($_[1]) } + ], + [#Rule 33 + 'var', 3, +sub +#line 90 "expr.yp" +{ $_[0]->_Use("$_[1].$_[3]") } + ], + [#Rule 34 + 'var', 3, +sub +#line 92 "expr.yp" +{ "($_[2])" } + ], + [#Rule 35 + 'var', 3, +sub +#line 94 "expr.yp" +{ $_[0]->_Use("*$_[1]"); $_[1]."->".$_[3] } + ], + [#Rule 36 + 'func', 4, +sub +#line 99 "expr.yp" +{ "$_[1]($_[3])" } + ], + [#Rule 37 + 'opt_args', 0, +sub +#line 104 "expr.yp" +{ "" } + ], + [#Rule 38 + 'opt_args', 1, undef + ], + [#Rule 39 + 'exp_or_possible_pointer', 1, undef + ], + [#Rule 40 + 'exp_or_possible_pointer', 1, undef + ], + [#Rule 41 + 'args', 1, undef + ], + [#Rule 42 + 'args', 3, +sub +#line 118 "expr.yp" +{ "$_[1], $_[3]" } + ] +], + @_); + bless($self,$class); +} + +#line 121 "expr.yp" + + +package Parse::Pidl::Expr; + +sub _Lexer { + my($parser)=shift; + + $parser->YYData->{INPUT}=~s/^[ \t]//; + + for ($parser->YYData->{INPUT}) { + if (s/^(0x[0-9A-Fa-f]+)//) { + $parser->YYData->{LAST_TOKEN} = $1; + return('NUM',$1); + } + if (s/^([0-9]+(?:\.[0-9]+)?)//) { + $parser->YYData->{LAST_TOKEN} = $1; + return('NUM',$1); + } + if (s/^([A-Za-z_][A-Za-z0-9_]*)//) { + $parser->YYData->{LAST_TOKEN} = $1; + return('VAR',$1); + } + if (s/^\"(.*?)\"//) { + $parser->YYData->{LAST_TOKEN} = $1; + return('TEXT',$1); + } + if (s/^(==|!=|<=|>=|->|\|\||<<|>>|&&)//s) { + $parser->YYData->{LAST_TOKEN} = $1; + return($1,$1); + } + if (s/^(.)//s) { + $parser->YYData->{LAST_TOKEN} = $1; + return($1,$1); + } + } +} + +sub _Use($$) +{ + my ($self, $x) = @_; + if (defined($self->YYData->{USE})) { + return $self->YYData->{USE}->($x); + } + return $x; +} + +sub _Lookup($$) +{ + my ($self, $x) = @_; + return $self->YYData->{LOOKUP}->($x); +} + +sub _Dereference($$) +{ + my ($self, $x) = @_; + if (defined($self->YYData->{DEREFERENCE})) { + $self->YYData->{DEREFERENCE}->($x); + } +} + +sub _Error($) +{ + my ($self) = @_; + if (defined($self->YYData->{LAST_TOKEN})) { + $self->YYData->{ERROR}->("Parse error in `".$self->YYData->{FULL_INPUT}."' near `". $self->YYData->{LAST_TOKEN} . "'"); + } else { + $self->YYData->{ERROR}->("Parse error in `".$self->YYData->{FULL_INPUT}."'"); + } +} + +sub Run { + my($self, $data, $error, $lookup, $deref, $use) = @_; + + $self->YYData->{FULL_INPUT} = $data; + $self->YYData->{INPUT} = $data; + $self->YYData->{LOOKUP} = $lookup; + $self->YYData->{DEREFERENCE} = $deref; + $self->YYData->{ERROR} = $error; + $self->YYData->{USE} = $use; + + return $self->YYParse( yylex => \&_Lexer, yyerror => \&_Error); +} + +1; diff --git a/tools/pidl/lib/Parse/Pidl/IDL.pm b/tools/pidl/lib/Parse/Pidl/IDL.pm new file mode 100644 index 00000000..6927c892 --- /dev/null +++ b/tools/pidl/lib/Parse/Pidl/IDL.pm @@ -0,0 +1,2664 @@ +#################################################################### +# +# This file was generated using Parse::Yapp version 1.05. +# +# Don't edit this file, use source file instead. +# +# ANY CHANGE MADE HERE WILL BE LOST ! +# +#################################################################### +package Parse::Pidl::IDL; +use vars qw ( @ISA ); +use strict; + +@ISA= qw ( Parse::Yapp::Driver ); +use Parse::Yapp::Driver; + + + +sub new { + my($class)=shift; + ref($class) + and $class=ref($class); + + my($self)=$class->SUPER::new( yyversion => '1.05', + yystates => +[ + {#State 0 + DEFAULT => -1, + GOTOS => { + 'idl' => 1 + } + }, + {#State 1 + ACTIONS => { + '' => 2, + "cpp_quote" => 3, + "importlib" => 4, + "import" => 7, + "include" => 13 + }, + DEFAULT => -89, + GOTOS => { + 'cpp_quote' => 11, + 'importlib' => 10, + 'interface' => 9, + 'include' => 5, + 'coclass' => 12, + 'import' => 8, + 'property_list' => 6 + } + }, + {#State 2 + DEFAULT => 0 + }, + {#State 3 + ACTIONS => { + "(" => 14 + } + }, + {#State 4 + ACTIONS => { + 'TEXT' => 16 + }, + GOTOS => { + 'commalist' => 15, + 'text' => 17 + } + }, + {#State 5 + DEFAULT => -5 + }, + {#State 6 + ACTIONS => { + "coclass" => 18, + "[" => 20, + "interface" => 19 + } + }, + {#State 7 + ACTIONS => { + 'TEXT' => 16 + }, + GOTOS => { + 'commalist' => 21, + 'text' => 17 + } + }, + {#State 8 + DEFAULT => -4 + }, + {#State 9 + DEFAULT => -2 + }, + {#State 10 + DEFAULT => -6 + }, + {#State 11 + DEFAULT => -7 + }, + {#State 12 + DEFAULT => -3 + }, + {#State 13 + ACTIONS => { + 'TEXT' => 16 + }, + GOTOS => { + 'commalist' => 22, + 'text' => 17 + } + }, + {#State 14 + ACTIONS => { + 'TEXT' => 16 + }, + GOTOS => { + 'text' => 23 + } + }, + {#State 15 + ACTIONS => { + ";" => 24, + "," => 25 + } + }, + {#State 16 + DEFAULT => -120 + }, + {#State 17 + DEFAULT => -11 + }, + {#State 18 + ACTIONS => { + 'IDENTIFIER' => 26 + }, + GOTOS => { + 'identifier' => 27 + } + }, + {#State 19 + ACTIONS => { + 'IDENTIFIER' => 26 + }, + GOTOS => { + 'identifier' => 28 + } + }, + {#State 20 + ACTIONS => { + 'IDENTIFIER' => 26 + }, + GOTOS => { + 'identifier' => 30, + 'property' => 31, + 'properties' => 29 + } + }, + {#State 21 + ACTIONS => { + ";" => 32, + "," => 25 + } + }, + {#State 22 + ACTIONS => { + ";" => 33, + "," => 25 + } + }, + {#State 23 + ACTIONS => { + ")" => 34 + } + }, + {#State 24 + DEFAULT => -10 + }, + {#State 25 + ACTIONS => { + 'TEXT' => 16 + }, + GOTOS => { + 'text' => 35 + } + }, + {#State 26 + DEFAULT => -116 + }, + {#State 27 + ACTIONS => { + "{" => 36 + } + }, + {#State 28 + ACTIONS => { + ":" => 37 + }, + DEFAULT => -17, + GOTOS => { + 'base_interface' => 38 + } + }, + {#State 29 + ACTIONS => { + "," => 39, + "]" => 40 + } + }, + {#State 30 + ACTIONS => { + "(" => 41 + }, + DEFAULT => -93 + }, + {#State 31 + DEFAULT => -91 + }, + {#State 32 + DEFAULT => -8 + }, + {#State 33 + DEFAULT => -9 + }, + {#State 34 + DEFAULT => -19 + }, + {#State 35 + DEFAULT => -12 + }, + {#State 36 + DEFAULT => -14, + GOTOS => { + 'interface_names' => 42 + } + }, + {#State 37 + ACTIONS => { + 'IDENTIFIER' => 26 + }, + GOTOS => { + 'identifier' => 43 + } + }, + {#State 38 + ACTIONS => { + "{" => 44 + } + }, + {#State 39 + ACTIONS => { + 'IDENTIFIER' => 26 + }, + GOTOS => { + 'identifier' => 30, + 'property' => 45 + } + }, + {#State 40 + DEFAULT => -90 + }, + {#State 41 + ACTIONS => { + 'CONSTANT' => 48, + 'TEXT' => 16, + 'IDENTIFIER' => 26 + }, + DEFAULT => -97, + GOTOS => { + 'identifier' => 50, + 'text' => 51, + 'anytext' => 46, + 'constant' => 47, + 'commalisttext' => 49 + } + }, + {#State 42 + ACTIONS => { + "}" => 52, + "interface" => 53 + } + }, + {#State 43 + DEFAULT => -18 + }, + {#State 44 + ACTIONS => { + "const" => 64 + }, + DEFAULT => -89, + GOTOS => { + 'typedecl' => 54, + 'function' => 55, + 'pipe' => 56, + 'definitions' => 58, + 'bitmap' => 57, + 'definition' => 61, + 'property_list' => 60, + 'usertype' => 59, + 'const' => 63, + 'struct' => 62, + 'typedef' => 66, + 'enum' => 65, + 'union' => 67 + } + }, + {#State 45 + DEFAULT => -92 + }, + {#State 46 + ACTIONS => { + "-" => 69, + ":" => 68, + "<" => 71, + "+" => 73, + "~" => 72, + "*" => 80, + "?" => 70, + "{" => 74, + "&" => 75, + "/" => 76, + "=" => 77, + "(" => 78, + "|" => 79, + "." => 81, + ">" => 82 + }, + DEFAULT => -95 + }, + {#State 47 + DEFAULT => -99 + }, + {#State 48 + DEFAULT => -119 + }, + {#State 49 + ACTIONS => { + "," => 83, + ")" => 84 + } + }, + {#State 50 + DEFAULT => -98 + }, + {#State 51 + DEFAULT => -100 + }, + {#State 52 + ACTIONS => { + ";" => 86 + }, + DEFAULT => -121, + GOTOS => { + 'optional_semicolon' => 85 + } + }, + {#State 53 + ACTIONS => { + 'IDENTIFIER' => 26 + }, + GOTOS => { + 'identifier' => 87 + } + }, + {#State 54 + DEFAULT => -25 + }, + {#State 55 + DEFAULT => -22 + }, + {#State 56 + DEFAULT => -34 + }, + {#State 57 + DEFAULT => -33 + }, + {#State 58 + ACTIONS => { + "}" => 88, + "const" => 64 + }, + DEFAULT => -89, + GOTOS => { + 'typedecl' => 54, + 'function' => 55, + 'pipe' => 56, + 'bitmap' => 57, + 'definition' => 89, + 'property_list' => 60, + 'usertype' => 59, + 'const' => 63, + 'struct' => 62, + 'typedef' => 66, + 'enum' => 65, + 'union' => 67 + } + }, + {#State 59 + ACTIONS => { + ";" => 90 + } + }, + {#State 60 + ACTIONS => { + "typedef" => 91, + 'IDENTIFIER' => 26, + "signed" => 100, + "union" => 92, + "enum" => 101, + "bitmap" => 102, + 'void' => 93, + "pipe" => 103, + "unsigned" => 104, + "[" => 20, + "struct" => 98 + }, + GOTOS => { + 'existingtype' => 99, + 'pipe' => 56, + 'bitmap' => 57, + 'usertype' => 95, + 'property_list' => 94, + 'identifier' => 96, + 'struct' => 62, + 'enum' => 65, + 'type' => 105, + 'union' => 67, + 'sign' => 97 + } + }, + {#State 61 + DEFAULT => -20 + }, + {#State 62 + DEFAULT => -30 + }, + {#State 63 + DEFAULT => -23 + }, + {#State 64 + ACTIONS => { + 'IDENTIFIER' => 26 + }, + GOTOS => { + 'identifier' => 106 + } + }, + {#State 65 + DEFAULT => -32 + }, + {#State 66 + DEFAULT => -24 + }, + {#State 67 + DEFAULT => -31 + }, + {#State 68 + ACTIONS => { + 'CONSTANT' => 48, + 'TEXT' => 16, + 'IDENTIFIER' => 26 + }, + DEFAULT => -97, + GOTOS => { + 'identifier' => 50, + 'anytext' => 107, + 'text' => 51, + 'constant' => 47 + } + }, + {#State 69 + ACTIONS => { + 'CONSTANT' => 48, + 'TEXT' => 16, + 'IDENTIFIER' => 26 + }, + DEFAULT => -97, + GOTOS => { + 'identifier' => 50, + 'anytext' => 108, + 'text' => 51, + 'constant' => 47 + } + }, + {#State 70 + ACTIONS => { + 'CONSTANT' => 48, + 'TEXT' => 16, + 'IDENTIFIER' => 26 + }, + DEFAULT => -97, + GOTOS => { + 'identifier' => 50, + 'anytext' => 109, + 'text' => 51, + 'constant' => 47 + } + }, + {#State 71 + ACTIONS => { + 'CONSTANT' => 48, + 'TEXT' => 16, + 'IDENTIFIER' => 26 + }, + DEFAULT => -97, + GOTOS => { + 'identifier' => 50, + 'anytext' => 110, + 'text' => 51, + 'constant' => 47 + } + }, + {#State 72 + ACTIONS => { + 'CONSTANT' => 48, + 'TEXT' => 16, + 'IDENTIFIER' => 26 + }, + DEFAULT => -97, + GOTOS => { + 'identifier' => 50, + 'anytext' => 111, + 'text' => 51, + 'constant' => 47 + } + }, + {#State 73 + ACTIONS => { + 'CONSTANT' => 48, + 'TEXT' => 16, + 'IDENTIFIER' => 26 + }, + DEFAULT => -97, + GOTOS => { + 'identifier' => 50, + 'anytext' => 112, + 'text' => 51, + 'constant' => 47 + } + }, + {#State 74 + ACTIONS => { + 'CONSTANT' => 48, + 'TEXT' => 16, + 'IDENTIFIER' => 26 + }, + DEFAULT => -97, + GOTOS => { + 'identifier' => 50, + 'anytext' => 46, + 'text' => 51, + 'constant' => 47, + 'commalisttext' => 113 + } + }, + {#State 75 + ACTIONS => { + 'CONSTANT' => 48, + 'TEXT' => 16, + 'IDENTIFIER' => 26 + }, + DEFAULT => -97, + GOTOS => { + 'identifier' => 50, + 'anytext' => 114, + 'text' => 51, + 'constant' => 47 + } + }, + {#State 76 + ACTIONS => { + 'CONSTANT' => 48, + 'TEXT' => 16, + 'IDENTIFIER' => 26 + }, + DEFAULT => -97, + GOTOS => { + 'identifier' => 50, + 'anytext' => 115, + 'text' => 51, + 'constant' => 47 + } + }, + {#State 77 + ACTIONS => { + 'CONSTANT' => 48, + 'TEXT' => 16, + 'IDENTIFIER' => 26 + }, + DEFAULT => -97, + GOTOS => { + 'identifier' => 50, + 'anytext' => 116, + 'text' => 51, + 'constant' => 47 + } + }, + {#State 78 + ACTIONS => { + 'CONSTANT' => 48, + 'TEXT' => 16, + 'IDENTIFIER' => 26 + }, + DEFAULT => -97, + GOTOS => { + 'identifier' => 50, + 'anytext' => 46, + 'text' => 51, + 'constant' => 47, + 'commalisttext' => 117 + } + }, + {#State 79 + ACTIONS => { + 'CONSTANT' => 48, + 'TEXT' => 16, + 'IDENTIFIER' => 26 + }, + DEFAULT => -97, + GOTOS => { + 'identifier' => 50, + 'anytext' => 118, + 'text' => 51, + 'constant' => 47 + } + }, + {#State 80 + ACTIONS => { + 'CONSTANT' => 48, + 'TEXT' => 16, + 'IDENTIFIER' => 26 + }, + DEFAULT => -97, + GOTOS => { + 'identifier' => 50, + 'anytext' => 119, + 'text' => 51, + 'constant' => 47 + } + }, + {#State 81 + ACTIONS => { + 'CONSTANT' => 48, + 'TEXT' => 16, + 'IDENTIFIER' => 26 + }, + DEFAULT => -97, + GOTOS => { + 'identifier' => 50, + 'anytext' => 120, + 'text' => 51, + 'constant' => 47 + } + }, + {#State 82 + ACTIONS => { + 'CONSTANT' => 48, + 'TEXT' => 16, + 'IDENTIFIER' => 26 + }, + DEFAULT => -97, + GOTOS => { + 'identifier' => 50, + 'anytext' => 121, + 'text' => 51, + 'constant' => 47 + } + }, + {#State 83 + ACTIONS => { + 'CONSTANT' => 48, + 'TEXT' => 16, + 'IDENTIFIER' => 26 + }, + DEFAULT => -97, + GOTOS => { + 'identifier' => 50, + 'anytext' => 122, + 'text' => 51, + 'constant' => 47 + } + }, + {#State 84 + DEFAULT => -94 + }, + {#State 85 + DEFAULT => -13 + }, + {#State 86 + DEFAULT => -122 + }, + {#State 87 + ACTIONS => { + ";" => 123 + } + }, + {#State 88 + ACTIONS => { + ";" => 86 + }, + DEFAULT => -121, + GOTOS => { + 'optional_semicolon' => 124 + } + }, + {#State 89 + DEFAULT => -21 + }, + {#State 90 + DEFAULT => -35 + }, + {#State 91 + ACTIONS => { + 'IDENTIFIER' => 26, + "signed" => 100, + 'void' => 93, + "unsigned" => 104 + }, + DEFAULT => -89, + GOTOS => { + 'existingtype' => 99, + 'pipe' => 56, + 'bitmap' => 57, + 'usertype' => 95, + 'property_list' => 94, + 'identifier' => 96, + 'struct' => 62, + 'enum' => 65, + 'type' => 125, + 'union' => 67, + 'sign' => 97 + } + }, + {#State 92 + ACTIONS => { + 'IDENTIFIER' => 126 + }, + DEFAULT => -117, + GOTOS => { + 'optional_identifier' => 127 + } + }, + {#State 93 + DEFAULT => -42 + }, + {#State 94 + ACTIONS => { + "pipe" => 103, + "union" => 92, + "enum" => 101, + "bitmap" => 102, + "[" => 20, + "struct" => 98 + } + }, + {#State 95 + DEFAULT => -40 + }, + {#State 96 + DEFAULT => -39 + }, + {#State 97 + ACTIONS => { + 'IDENTIFIER' => 26 + }, + GOTOS => { + 'identifier' => 128 + } + }, + {#State 98 + ACTIONS => { + 'IDENTIFIER' => 126 + }, + DEFAULT => -117, + GOTOS => { + 'optional_identifier' => 129 + } + }, + {#State 99 + DEFAULT => -41 + }, + {#State 100 + DEFAULT => -36 + }, + {#State 101 + ACTIONS => { + 'IDENTIFIER' => 126 + }, + DEFAULT => -117, + GOTOS => { + 'optional_identifier' => 130 + } + }, + {#State 102 + ACTIONS => { + 'IDENTIFIER' => 126 + }, + DEFAULT => -117, + GOTOS => { + 'optional_identifier' => 131 + } + }, + {#State 103 + ACTIONS => { + 'IDENTIFIER' => 26, + "signed" => 100, + 'void' => 93, + "unsigned" => 104 + }, + DEFAULT => -89, + GOTOS => { + 'existingtype' => 99, + 'pipe' => 56, + 'bitmap' => 57, + 'usertype' => 95, + 'property_list' => 94, + 'identifier' => 96, + 'struct' => 62, + 'enum' => 65, + 'type' => 132, + 'union' => 67, + 'sign' => 97 + } + }, + {#State 104 + DEFAULT => -37 + }, + {#State 105 + ACTIONS => { + 'IDENTIFIER' => 26 + }, + GOTOS => { + 'identifier' => 133 + } + }, + {#State 106 + DEFAULT => -75, + GOTOS => { + 'pointers' => 134 + } + }, + {#State 107 + ACTIONS => { + "-" => 69, + ":" => 68, + "<" => 71, + "+" => 73, + "~" => 72, + "*" => 80, + "?" => 70, + "{" => 74, + "&" => 75, + "/" => 76, + "=" => 77, + "(" => 78, + "|" => 79, + "." => 81, + ">" => 82 + }, + DEFAULT => -110 + }, + {#State 108 + ACTIONS => { + ":" => 68, + "<" => 71, + "~" => 72, + "?" => 70, + "{" => 74, + "=" => 77 + }, + DEFAULT => -101 + }, + {#State 109 + ACTIONS => { + "-" => 69, + ":" => 68, + "<" => 71, + "+" => 73, + "~" => 72, + "*" => 80, + "?" => 70, + "{" => 74, + "&" => 75, + "/" => 76, + "=" => 77, + "(" => 78, + "|" => 79, + "." => 81, + ">" => 82 + }, + DEFAULT => -109 + }, + {#State 110 + ACTIONS => { + "-" => 69, + ":" => 68, + "<" => 71, + "+" => 73, + "~" => 72, + "*" => 80, + "?" => 70, + "{" => 74, + "&" => 75, + "/" => 76, + "=" => 77, + "(" => 78, + "|" => 79, + "." => 81, + ">" => 82 + }, + DEFAULT => -105 + }, + {#State 111 + ACTIONS => { + "-" => 69, + ":" => 68, + "<" => 71, + "+" => 73, + "~" => 72, + "*" => 80, + "?" => 70, + "{" => 74, + "&" => 75, + "/" => 76, + "=" => 77, + "(" => 78, + "|" => 79, + "." => 81, + ">" => 82 + }, + DEFAULT => -113 + }, + {#State 112 + ACTIONS => { + ":" => 68, + "<" => 71, + "~" => 72, + "?" => 70, + "{" => 74, + "=" => 77 + }, + DEFAULT => -112 + }, + {#State 113 + ACTIONS => { + "}" => 135, + "," => 83 + } + }, + {#State 114 + ACTIONS => { + ":" => 68, + "<" => 71, + "~" => 72, + "?" => 70, + "{" => 74, + "=" => 77 + }, + DEFAULT => -107 + }, + {#State 115 + ACTIONS => { + ":" => 68, + "<" => 71, + "~" => 72, + "?" => 70, + "{" => 74, + "=" => 77 + }, + DEFAULT => -108 + }, + {#State 116 + ACTIONS => { + "-" => 69, + ":" => 68, + "<" => 71, + "+" => 73, + "~" => 72, + "*" => 80, + "?" => 70, + "{" => 74, + "&" => 75, + "/" => 76, + "=" => 77, + "(" => 78, + "|" => 79, + "." => 81, + ">" => 82 + }, + DEFAULT => -111 + }, + {#State 117 + ACTIONS => { + "," => 83, + ")" => 136 + } + }, + {#State 118 + ACTIONS => { + ":" => 68, + "<" => 71, + "~" => 72, + "?" => 70, + "{" => 74, + "=" => 77 + }, + DEFAULT => -106 + }, + {#State 119 + ACTIONS => { + ":" => 68, + "<" => 71, + "~" => 72, + "?" => 70, + "{" => 74, + "=" => 77 + }, + DEFAULT => -103 + }, + {#State 120 + ACTIONS => { + ":" => 68, + "<" => 71, + "~" => 72, + "?" => 70, + "{" => 74, + "=" => 77 + }, + DEFAULT => -102 + }, + {#State 121 + ACTIONS => { + ":" => 68, + "<" => 71, + "~" => 72, + "?" => 70, + "{" => 74, + "=" => 77 + }, + DEFAULT => -104 + }, + {#State 122 + ACTIONS => { + "-" => 69, + ":" => 68, + "<" => 71, + "+" => 73, + "~" => 72, + "*" => 80, + "?" => 70, + "{" => 74, + "&" => 75, + "/" => 76, + "=" => 77, + "(" => 78, + "|" => 79, + "." => 81, + ">" => 82 + }, + DEFAULT => -96 + }, + {#State 123 + DEFAULT => -15 + }, + {#State 124 + DEFAULT => -16 + }, + {#State 125 + DEFAULT => -75, + GOTOS => { + 'pointers' => 137 + } + }, + {#State 126 + DEFAULT => -118 + }, + {#State 127 + ACTIONS => { + "{" => 139 + }, + DEFAULT => -71, + GOTOS => { + 'union_body' => 140, + 'opt_union_body' => 138 + } + }, + {#State 128 + DEFAULT => -38 + }, + {#State 129 + ACTIONS => { + "{" => 142 + }, + DEFAULT => -61, + GOTOS => { + 'struct_body' => 141, + 'opt_struct_body' => 143 + } + }, + {#State 130 + ACTIONS => { + "{" => 144 + }, + DEFAULT => -44, + GOTOS => { + 'opt_enum_body' => 146, + 'enum_body' => 145 + } + }, + {#State 131 + ACTIONS => { + "{" => 148 + }, + DEFAULT => -52, + GOTOS => { + 'bitmap_body' => 149, + 'opt_bitmap_body' => 147 + } + }, + {#State 132 + DEFAULT => -77 + }, + {#State 133 + ACTIONS => { + "(" => 150 + } + }, + {#State 134 + ACTIONS => { + 'IDENTIFIER' => 26, + "*" => 152 + }, + GOTOS => { + 'identifier' => 151 + } + }, + {#State 135 + ACTIONS => { + 'CONSTANT' => 48, + 'TEXT' => 16, + 'IDENTIFIER' => 26 + }, + DEFAULT => -97, + GOTOS => { + 'identifier' => 50, + 'anytext' => 153, + 'text' => 51, + 'constant' => 47 + } + }, + {#State 136 + ACTIONS => { + 'CONSTANT' => 48, + 'TEXT' => 16, + 'IDENTIFIER' => 26 + }, + DEFAULT => -97, + GOTOS => { + 'identifier' => 50, + 'anytext' => 154, + 'text' => 51, + 'constant' => 47 + } + }, + {#State 137 + ACTIONS => { + 'IDENTIFIER' => 26, + "*" => 152 + }, + GOTOS => { + 'identifier' => 155 + } + }, + {#State 138 + DEFAULT => -73 + }, + {#State 139 + DEFAULT => -68, + GOTOS => { + 'union_elements' => 156 + } + }, + {#State 140 + DEFAULT => -72 + }, + {#State 141 + DEFAULT => -62 + }, + {#State 142 + DEFAULT => -78, + GOTOS => { + 'element_list1' => 157 + } + }, + {#State 143 + DEFAULT => -63 + }, + {#State 144 + ACTIONS => { + 'IDENTIFIER' => 26 + }, + GOTOS => { + 'identifier' => 158, + 'enum_element' => 159, + 'enum_elements' => 160 + } + }, + {#State 145 + DEFAULT => -45 + }, + {#State 146 + DEFAULT => -46 + }, + {#State 147 + DEFAULT => -54 + }, + {#State 148 + ACTIONS => { + 'IDENTIFIER' => 26 + }, + DEFAULT => -57, + GOTOS => { + 'identifier' => 163, + 'bitmap_element' => 162, + 'bitmap_elements' => 161, + 'opt_bitmap_elements' => 164 + } + }, + {#State 149 + DEFAULT => -53 + }, + {#State 150 + ACTIONS => { + "," => -82, + "void" => 168, + "const" => 166, + ")" => -82 + }, + DEFAULT => -80, + GOTOS => { + 'optional_const' => 165, + 'element_list2' => 167 + } + }, + {#State 151 + ACTIONS => { + "[" => 169, + "=" => 171 + }, + GOTOS => { + 'array_len' => 170 + } + }, + {#State 152 + DEFAULT => -76 + }, + {#State 153 + ACTIONS => { + "-" => 69, + ":" => 68, + "<" => 71, + "+" => 73, + "~" => 72, + "*" => 80, + "?" => 70, + "{" => 74, + "&" => 75, + "/" => 76, + "=" => 77, + "(" => 78, + "|" => 79, + "." => 81, + ">" => 82 + }, + DEFAULT => -115 + }, + {#State 154 + ACTIONS => { + ":" => 68, + "<" => 71, + "~" => 72, + "?" => 70, + "{" => 74, + "=" => 77 + }, + DEFAULT => -114 + }, + {#State 155 + ACTIONS => { + "[" => 169 + }, + DEFAULT => -86, + GOTOS => { + 'array_len' => 172 + } + }, + {#State 156 + ACTIONS => { + "}" => 173 + }, + DEFAULT => -89, + GOTOS => { + 'optional_base_element' => 175, + 'property_list' => 174 + } + }, + {#State 157 + ACTIONS => { + "}" => 176 + }, + DEFAULT => -89, + GOTOS => { + 'base_element' => 177, + 'property_list' => 178 + } + }, + {#State 158 + ACTIONS => { + "=" => 179 + }, + DEFAULT => -49 + }, + {#State 159 + DEFAULT => -47 + }, + {#State 160 + ACTIONS => { + "}" => 180, + "," => 181 + } + }, + {#State 161 + ACTIONS => { + "," => 182 + }, + DEFAULT => -58 + }, + {#State 162 + DEFAULT => -55 + }, + {#State 163 + ACTIONS => { + "=" => 183 + } + }, + {#State 164 + ACTIONS => { + "}" => 184 + } + }, + {#State 165 + DEFAULT => -89, + GOTOS => { + 'base_element' => 185, + 'property_list' => 178 + } + }, + {#State 166 + DEFAULT => -81 + }, + {#State 167 + ACTIONS => { + "," => 186, + ")" => 187 + } + }, + {#State 168 + DEFAULT => -83 + }, + {#State 169 + ACTIONS => { + 'CONSTANT' => 48, + 'TEXT' => 16, + "]" => 188, + 'IDENTIFIER' => 26 + }, + DEFAULT => -97, + GOTOS => { + 'identifier' => 50, + 'anytext' => 189, + 'text' => 51, + 'constant' => 47 + } + }, + {#State 170 + ACTIONS => { + "=" => 190 + } + }, + {#State 171 + ACTIONS => { + 'CONSTANT' => 48, + 'TEXT' => 16, + 'IDENTIFIER' => 26 + }, + DEFAULT => -97, + GOTOS => { + 'identifier' => 50, + 'anytext' => 191, + 'text' => 51, + 'constant' => 47 + } + }, + {#State 172 + ACTIONS => { + ";" => 192 + } + }, + {#State 173 + DEFAULT => -70 + }, + {#State 174 + ACTIONS => { + "[" => 20 + }, + DEFAULT => -89, + GOTOS => { + 'base_or_empty' => 193, + 'base_element' => 194, + 'empty_element' => 195, + 'property_list' => 196 + } + }, + {#State 175 + DEFAULT => -69 + }, + {#State 176 + DEFAULT => -60 + }, + {#State 177 + ACTIONS => { + ";" => 197 + } + }, + {#State 178 + ACTIONS => { + 'IDENTIFIER' => 26, + "signed" => 100, + 'void' => 93, + "unsigned" => 104, + "[" => 20 + }, + DEFAULT => -89, + GOTOS => { + 'existingtype' => 99, + 'pipe' => 56, + 'bitmap' => 57, + 'usertype' => 95, + 'property_list' => 94, + 'identifier' => 96, + 'struct' => 62, + 'enum' => 65, + 'type' => 198, + 'union' => 67, + 'sign' => 97 + } + }, + {#State 179 + ACTIONS => { + 'CONSTANT' => 48, + 'TEXT' => 16, + 'IDENTIFIER' => 26 + }, + DEFAULT => -97, + GOTOS => { + 'identifier' => 50, + 'anytext' => 199, + 'text' => 51, + 'constant' => 47 + } + }, + {#State 180 + DEFAULT => -43 + }, + {#State 181 + ACTIONS => { + 'IDENTIFIER' => 26 + }, + GOTOS => { + 'identifier' => 158, + 'enum_element' => 200 + } + }, + {#State 182 + ACTIONS => { + 'IDENTIFIER' => 26 + }, + GOTOS => { + 'identifier' => 163, + 'bitmap_element' => 201 + } + }, + {#State 183 + ACTIONS => { + 'CONSTANT' => 48, + 'TEXT' => 16, + 'IDENTIFIER' => 26 + }, + DEFAULT => -97, + GOTOS => { + 'identifier' => 50, + 'anytext' => 202, + 'text' => 51, + 'constant' => 47 + } + }, + {#State 184 + DEFAULT => -51 + }, + {#State 185 + DEFAULT => -84 + }, + {#State 186 + ACTIONS => { + "const" => 166 + }, + DEFAULT => -80, + GOTOS => { + 'optional_const' => 203 + } + }, + {#State 187 + ACTIONS => { + ";" => 204 + } + }, + {#State 188 + ACTIONS => { + "[" => 169 + }, + DEFAULT => -86, + GOTOS => { + 'array_len' => 205 + } + }, + {#State 189 + ACTIONS => { + "-" => 69, + ":" => 68, + "?" => 70, + "<" => 71, + "+" => 73, + "~" => 72, + "&" => 75, + "{" => 74, + "/" => 76, + "=" => 77, + "|" => 79, + "(" => 78, + "*" => 80, + "." => 81, + "]" => 206, + ">" => 82 + } + }, + {#State 190 + ACTIONS => { + 'CONSTANT' => 48, + 'TEXT' => 16, + 'IDENTIFIER' => 26 + }, + DEFAULT => -97, + GOTOS => { + 'identifier' => 50, + 'anytext' => 207, + 'text' => 51, + 'constant' => 47 + } + }, + {#State 191 + ACTIONS => { + "-" => 69, + ":" => 68, + "?" => 70, + "<" => 71, + ";" => 208, + "+" => 73, + "~" => 72, + "&" => 75, + "{" => 74, + "/" => 76, + "=" => 77, + "|" => 79, + "(" => 78, + "*" => 80, + "." => 81, + ">" => 82 + } + }, + {#State 192 + DEFAULT => -29 + }, + {#State 193 + DEFAULT => -67 + }, + {#State 194 + ACTIONS => { + ";" => 209 + } + }, + {#State 195 + DEFAULT => -66 + }, + {#State 196 + ACTIONS => { + 'IDENTIFIER' => 26, + "signed" => 100, + ";" => 210, + 'void' => 93, + "unsigned" => 104, + "[" => 20 + }, + DEFAULT => -89, + GOTOS => { + 'existingtype' => 99, + 'pipe' => 56, + 'bitmap' => 57, + 'usertype' => 95, + 'property_list' => 94, + 'identifier' => 96, + 'struct' => 62, + 'enum' => 65, + 'type' => 198, + 'union' => 67, + 'sign' => 97 + } + }, + {#State 197 + DEFAULT => -79 + }, + {#State 198 + DEFAULT => -75, + GOTOS => { + 'pointers' => 211 + } + }, + {#State 199 + ACTIONS => { + "-" => 69, + ":" => 68, + "<" => 71, + "+" => 73, + "~" => 72, + "*" => 80, + "?" => 70, + "{" => 74, + "&" => 75, + "/" => 76, + "=" => 77, + "(" => 78, + "|" => 79, + "." => 81, + ">" => 82 + }, + DEFAULT => -50 + }, + {#State 200 + DEFAULT => -48 + }, + {#State 201 + DEFAULT => -56 + }, + {#State 202 + ACTIONS => { + "-" => 69, + ":" => 68, + "<" => 71, + "+" => 73, + "~" => 72, + "*" => 80, + "?" => 70, + "{" => 74, + "&" => 75, + "/" => 76, + "=" => 77, + "(" => 78, + "|" => 79, + "." => 81, + ">" => 82 + }, + DEFAULT => -59 + }, + {#State 203 + DEFAULT => -89, + GOTOS => { + 'base_element' => 212, + 'property_list' => 178 + } + }, + {#State 204 + DEFAULT => -28 + }, + {#State 205 + DEFAULT => -87 + }, + {#State 206 + ACTIONS => { + "[" => 169 + }, + DEFAULT => -86, + GOTOS => { + 'array_len' => 213 + } + }, + {#State 207 + ACTIONS => { + "-" => 69, + ":" => 68, + "?" => 70, + "<" => 71, + ";" => 214, + "+" => 73, + "~" => 72, + "&" => 75, + "{" => 74, + "/" => 76, + "=" => 77, + "|" => 79, + "(" => 78, + "*" => 80, + "." => 81, + ">" => 82 + } + }, + {#State 208 + DEFAULT => -26 + }, + {#State 209 + DEFAULT => -65 + }, + {#State 210 + DEFAULT => -64 + }, + {#State 211 + ACTIONS => { + 'IDENTIFIER' => 26, + "*" => 152 + }, + GOTOS => { + 'identifier' => 215 + } + }, + {#State 212 + DEFAULT => -85 + }, + {#State 213 + DEFAULT => -88 + }, + {#State 214 + DEFAULT => -27 + }, + {#State 215 + ACTIONS => { + "[" => 169 + }, + DEFAULT => -86, + GOTOS => { + 'array_len' => 216 + } + }, + {#State 216 + DEFAULT => -74 + } +], + yyrules => +[ + [#Rule 0 + '$start', 2, undef + ], + [#Rule 1 + 'idl', 0, undef + ], + [#Rule 2 + 'idl', 2, +sub +#line 20 "idl.yp" +{ push(@{$_[1]}, $_[2]); $_[1] } + ], + [#Rule 3 + 'idl', 2, +sub +#line 22 "idl.yp" +{ push(@{$_[1]}, $_[2]); $_[1] } + ], + [#Rule 4 + 'idl', 2, +sub +#line 24 "idl.yp" +{ push(@{$_[1]}, $_[2]); $_[1] } + ], + [#Rule 5 + 'idl', 2, +sub +#line 26 "idl.yp" +{ push(@{$_[1]}, $_[2]); $_[1] } + ], + [#Rule 6 + 'idl', 2, +sub +#line 28 "idl.yp" +{ push(@{$_[1]}, $_[2]); $_[1] } + ], + [#Rule 7 + 'idl', 2, +sub +#line 30 "idl.yp" +{ push(@{$_[1]}, $_[2]); $_[1] } + ], + [#Rule 8 + 'import', 3, +sub +#line 35 "idl.yp" +{{ + "TYPE" => "IMPORT", + "PATHS" => $_[2], + "FILE" => $_[0]->YYData->{FILE}, + "LINE" => $_[0]->YYData->{LINE}, + }} + ], + [#Rule 9 + 'include', 3, +sub +#line 45 "idl.yp" +{{ + "TYPE" => "INCLUDE", + "PATHS" => $_[2], + "FILE" => $_[0]->YYData->{FILE}, + "LINE" => $_[0]->YYData->{LINE}, + }} + ], + [#Rule 10 + 'importlib', 3, +sub +#line 55 "idl.yp" +{{ + "TYPE" => "IMPORTLIB", + "PATHS" => $_[2], + "FILE" => $_[0]->YYData->{FILE}, + "LINE" => $_[0]->YYData->{LINE}, + }} + ], + [#Rule 11 + 'commalist', 1, +sub +#line 64 "idl.yp" +{ [ $_[1] ] } + ], + [#Rule 12 + 'commalist', 3, +sub +#line 66 "idl.yp" +{ push(@{$_[1]}, $_[3]); $_[1] } + ], + [#Rule 13 + 'coclass', 7, +sub +#line 71 "idl.yp" +{{ + "TYPE" => "COCLASS", + "PROPERTIES" => $_[1], + "NAME" => $_[3], + "DATA" => $_[5], + "FILE" => $_[0]->YYData->{FILE}, + "LINE" => $_[0]->YYData->{LINE}, + }} + ], + [#Rule 14 + 'interface_names', 0, undef + ], + [#Rule 15 + 'interface_names', 4, +sub +#line 84 "idl.yp" +{ push(@{$_[1]}, $_[2]); $_[1] } + ], + [#Rule 16 + 'interface', 8, +sub +#line 89 "idl.yp" +{{ + "TYPE" => "INTERFACE", + "PROPERTIES" => $_[1], + "NAME" => $_[3], + "BASE" => $_[4], + "DATA" => $_[6], + "FILE" => $_[0]->YYData->{FILE}, + "LINE" => $_[0]->YYData->{LINE}, + }} + ], + [#Rule 17 + 'base_interface', 0, undef + ], + [#Rule 18 + 'base_interface', 2, +sub +#line 103 "idl.yp" +{ $_[2] } + ], + [#Rule 19 + 'cpp_quote', 4, +sub +#line 109 "idl.yp" +{{ + "TYPE" => "CPP_QUOTE", + "DATA" => $_[3], + "FILE" => $_[0]->YYData->{FILE}, + "LINE" => $_[0]->YYData->{LINE}, + }} + ], + [#Rule 20 + 'definitions', 1, +sub +#line 118 "idl.yp" +{ [ $_[1] ] } + ], + [#Rule 21 + 'definitions', 2, +sub +#line 120 "idl.yp" +{ push(@{$_[1]}, $_[2]); $_[1] } + ], + [#Rule 22 + 'definition', 1, undef + ], + [#Rule 23 + 'definition', 1, undef + ], + [#Rule 24 + 'definition', 1, undef + ], + [#Rule 25 + 'definition', 1, undef + ], + [#Rule 26 + 'const', 7, +sub +#line 135 "idl.yp" +{{ + "TYPE" => "CONST", + "DTYPE" => $_[2], + "POINTERS" => $_[3], + "NAME" => $_[4], + "VALUE" => $_[6], + "FILE" => $_[0]->YYData->{FILE}, + "LINE" => $_[0]->YYData->{LINE}, + }} + ], + [#Rule 27 + 'const', 8, +sub +#line 146 "idl.yp" +{{ + "TYPE" => "CONST", + "DTYPE" => $_[2], + "POINTERS" => $_[3], + "NAME" => $_[4], + "ARRAY_LEN" => $_[5], + "VALUE" => $_[7], + "FILE" => $_[0]->YYData->{FILE}, + "LINE" => $_[0]->YYData->{LINE}, + }} + ], + [#Rule 28 + 'function', 7, +sub +#line 160 "idl.yp" +{{ + "TYPE" => "FUNCTION", + "NAME" => $_[3], + "RETURN_TYPE" => $_[2], + "PROPERTIES" => $_[1], + "ELEMENTS" => $_[5], + "FILE" => $_[0]->YYData->{FILE}, + "LINE" => $_[0]->YYData->{LINE}, + }} + ], + [#Rule 29 + 'typedef', 7, +sub +#line 173 "idl.yp" +{{ + "TYPE" => "TYPEDEF", + "PROPERTIES" => $_[1], + "NAME" => $_[5], + "DATA" => $_[3], + "POINTERS" => $_[4], + "ARRAY_LEN" => $_[6], + "FILE" => $_[0]->YYData->{FILE}, + "LINE" => $_[0]->YYData->{LINE}, + }} + ], + [#Rule 30 + 'usertype', 1, undef + ], + [#Rule 31 + 'usertype', 1, undef + ], + [#Rule 32 + 'usertype', 1, undef + ], + [#Rule 33 + 'usertype', 1, undef + ], + [#Rule 34 + 'usertype', 1, undef + ], + [#Rule 35 + 'typedecl', 2, +sub +#line 198 "idl.yp" +{ $_[1] } + ], + [#Rule 36 + 'sign', 1, undef + ], + [#Rule 37 + 'sign', 1, undef + ], + [#Rule 38 + 'existingtype', 2, +sub +#line 208 "idl.yp" +{ ($_[1]?$_[1]:"signed") ." $_[2]" } + ], + [#Rule 39 + 'existingtype', 1, undef + ], + [#Rule 40 + 'type', 1, undef + ], + [#Rule 41 + 'type', 1, undef + ], + [#Rule 42 + 'type', 1, +sub +#line 218 "idl.yp" +{ "void" } + ], + [#Rule 43 + 'enum_body', 3, +sub +#line 222 "idl.yp" +{ $_[2] } + ], + [#Rule 44 + 'opt_enum_body', 0, undef + ], + [#Rule 45 + 'opt_enum_body', 1, undef + ], + [#Rule 46 + 'enum', 4, +sub +#line 233 "idl.yp" +{{ + "TYPE" => "ENUM", + "PROPERTIES" => $_[1], + "NAME" => $_[3], + "ELEMENTS" => $_[4], + "FILE" => $_[0]->YYData->{FILE}, + "LINE" => $_[0]->YYData->{LINE}, + }} + ], + [#Rule 47 + 'enum_elements', 1, +sub +#line 244 "idl.yp" +{ [ $_[1] ] } + ], + [#Rule 48 + 'enum_elements', 3, +sub +#line 246 "idl.yp" +{ push(@{$_[1]}, $_[3]); $_[1] } + ], + [#Rule 49 + 'enum_element', 1, undef + ], + [#Rule 50 + 'enum_element', 3, +sub +#line 252 "idl.yp" +{ "$_[1]$_[2]$_[3]" } + ], + [#Rule 51 + 'bitmap_body', 3, +sub +#line 256 "idl.yp" +{ $_[2] } + ], + [#Rule 52 + 'opt_bitmap_body', 0, undef + ], + [#Rule 53 + 'opt_bitmap_body', 1, undef + ], + [#Rule 54 + 'bitmap', 4, +sub +#line 267 "idl.yp" +{{ + "TYPE" => "BITMAP", + "PROPERTIES" => $_[1], + "NAME" => $_[3], + "ELEMENTS" => $_[4], + "FILE" => $_[0]->YYData->{FILE}, + "LINE" => $_[0]->YYData->{LINE}, + }} + ], + [#Rule 55 + 'bitmap_elements', 1, +sub +#line 278 "idl.yp" +{ [ $_[1] ] } + ], + [#Rule 56 + 'bitmap_elements', 3, +sub +#line 280 "idl.yp" +{ push(@{$_[1]}, $_[3]); $_[1] } + ], + [#Rule 57 + 'opt_bitmap_elements', 0, undef + ], + [#Rule 58 + 'opt_bitmap_elements', 1, undef + ], + [#Rule 59 + 'bitmap_element', 3, +sub +#line 290 "idl.yp" +{ "$_[1] ( $_[3] )" } + ], + [#Rule 60 + 'struct_body', 3, +sub +#line 294 "idl.yp" +{ $_[2] } + ], + [#Rule 61 + 'opt_struct_body', 0, undef + ], + [#Rule 62 + 'opt_struct_body', 1, undef + ], + [#Rule 63 + 'struct', 4, +sub +#line 305 "idl.yp" +{{ + "TYPE" => "STRUCT", + "PROPERTIES" => $_[1], + "NAME" => $_[3], + "ELEMENTS" => $_[4], + "FILE" => $_[0]->YYData->{FILE}, + "LINE" => $_[0]->YYData->{LINE}, + }} + ], + [#Rule 64 + 'empty_element', 2, +sub +#line 317 "idl.yp" +{{ + "NAME" => "", + "TYPE" => "EMPTY", + "PROPERTIES" => $_[1], + "POINTERS" => 0, + "ARRAY_LEN" => [], + "FILE" => $_[0]->YYData->{FILE}, + "LINE" => $_[0]->YYData->{LINE}, + }} + ], + [#Rule 65 + 'base_or_empty', 2, undef + ], + [#Rule 66 + 'base_or_empty', 1, undef + ], + [#Rule 67 + 'optional_base_element', 2, +sub +#line 334 "idl.yp" +{ $_[2]->{PROPERTIES} = FlattenHash([$_[1],$_[2]->{PROPERTIES}]); $_[2] } + ], + [#Rule 68 + 'union_elements', 0, undef + ], + [#Rule 69 + 'union_elements', 2, +sub +#line 340 "idl.yp" +{ push(@{$_[1]}, $_[2]); $_[1] } + ], + [#Rule 70 + 'union_body', 3, +sub +#line 344 "idl.yp" +{ $_[2] } + ], + [#Rule 71 + 'opt_union_body', 0, undef + ], + [#Rule 72 + 'opt_union_body', 1, undef + ], + [#Rule 73 + 'union', 4, +sub +#line 355 "idl.yp" +{{ + "TYPE" => "UNION", + "PROPERTIES" => $_[1], + "NAME" => $_[3], + "ELEMENTS" => $_[4], + "FILE" => $_[0]->YYData->{FILE}, + "LINE" => $_[0]->YYData->{LINE}, + }} + ], + [#Rule 74 + 'base_element', 5, +sub +#line 367 "idl.yp" +{{ + "NAME" => $_[4], + "TYPE" => $_[2], + "PROPERTIES" => $_[1], + "POINTERS" => $_[3], + "ARRAY_LEN" => $_[5], + "FILE" => $_[0]->YYData->{FILE}, + "LINE" => $_[0]->YYData->{LINE}, + }} + ], + [#Rule 75 + 'pointers', 0, +sub +#line 380 "idl.yp" +{ 0 } + ], + [#Rule 76 + 'pointers', 2, +sub +#line 382 "idl.yp" +{ $_[1]+1 } + ], + [#Rule 77 + 'pipe', 3, +sub +#line 387 "idl.yp" +{{ + "TYPE" => "PIPE", + "PROPERTIES" => $_[1], + "NAME" => undef, + "DATA" => { + "TYPE" => "STRUCT", + "PROPERTIES" => $_[1], + "NAME" => undef, + "ELEMENTS" => [{ + "NAME" => "count", + "PROPERTIES" => $_[1], + "POINTERS" => 0, + "ARRAY_LEN" => [], + "TYPE" => "uint3264", + "FILE" => $_[0]->YYData->{FILE}, + "LINE" => $_[0]->YYData->{LINE}, + },{ + "NAME" => "array", + "PROPERTIES" => $_[1], + "POINTERS" => 0, + "ARRAY_LEN" => [ "count" ], + "TYPE" => $_[3], + "FILE" => $_[0]->YYData->{FILE}, + "LINE" => $_[0]->YYData->{LINE}, + }], + "FILE" => $_[0]->YYData->{FILE}, + "LINE" => $_[0]->YYData->{LINE}, + }, + "FILE" => $_[0]->YYData->{FILE}, + "LINE" => $_[0]->YYData->{LINE}, + }} + ], + [#Rule 78 + 'element_list1', 0, +sub +#line 422 "idl.yp" +{ [] } + ], + [#Rule 79 + 'element_list1', 3, +sub +#line 424 "idl.yp" +{ push(@{$_[1]}, $_[2]); $_[1] } + ], + [#Rule 80 + 'optional_const', 0, undef + ], + [#Rule 81 + 'optional_const', 1, undef + ], + [#Rule 82 + 'element_list2', 0, undef + ], + [#Rule 83 + 'element_list2', 1, undef + ], + [#Rule 84 + 'element_list2', 2, +sub +#line 438 "idl.yp" +{ [ $_[2] ] } + ], + [#Rule 85 + 'element_list2', 4, +sub +#line 440 "idl.yp" +{ push(@{$_[1]}, $_[4]); $_[1] } + ], + [#Rule 86 + 'array_len', 0, undef + ], + [#Rule 87 + 'array_len', 3, +sub +#line 446 "idl.yp" +{ push(@{$_[3]}, "*"); $_[3] } + ], + [#Rule 88 + 'array_len', 4, +sub +#line 448 "idl.yp" +{ push(@{$_[4]}, "$_[2]"); $_[4] } + ], + [#Rule 89 + 'property_list', 0, undef + ], + [#Rule 90 + 'property_list', 4, +sub +#line 454 "idl.yp" +{ FlattenHash([$_[1],$_[3]]); } + ], + [#Rule 91 + 'properties', 1, +sub +#line 458 "idl.yp" +{ $_[1] } + ], + [#Rule 92 + 'properties', 3, +sub +#line 460 "idl.yp" +{ FlattenHash([$_[1], $_[3]]); } + ], + [#Rule 93 + 'property', 1, +sub +#line 464 "idl.yp" +{{ "$_[1]" => "1" }} + ], + [#Rule 94 + 'property', 4, +sub +#line 466 "idl.yp" +{{ "$_[1]" => "$_[3]" }} + ], + [#Rule 95 + 'commalisttext', 1, undef + ], + [#Rule 96 + 'commalisttext', 3, +sub +#line 472 "idl.yp" +{ "$_[1],$_[3]" } + ], + [#Rule 97 + 'anytext', 0, +sub +#line 477 "idl.yp" +{ "" } + ], + [#Rule 98 + 'anytext', 1, undef + ], + [#Rule 99 + 'anytext', 1, undef + ], + [#Rule 100 + 'anytext', 1, undef + ], + [#Rule 101 + 'anytext', 3, +sub +#line 485 "idl.yp" +{ "$_[1]$_[2]$_[3]" } + ], + [#Rule 102 + 'anytext', 3, +sub +#line 487 "idl.yp" +{ "$_[1]$_[2]$_[3]" } + ], + [#Rule 103 + 'anytext', 3, +sub +#line 489 "idl.yp" +{ "$_[1]$_[2]$_[3]" } + ], + [#Rule 104 + 'anytext', 3, +sub +#line 491 "idl.yp" +{ "$_[1]$_[2]$_[3]" } + ], + [#Rule 105 + 'anytext', 3, +sub +#line 493 "idl.yp" +{ "$_[1]$_[2]$_[3]" } + ], + [#Rule 106 + 'anytext', 3, +sub +#line 495 "idl.yp" +{ "$_[1]$_[2]$_[3]" } + ], + [#Rule 107 + 'anytext', 3, +sub +#line 497 "idl.yp" +{ "$_[1]$_[2]$_[3]" } + ], + [#Rule 108 + 'anytext', 3, +sub +#line 499 "idl.yp" +{ "$_[1]$_[2]$_[3]" } + ], + [#Rule 109 + 'anytext', 3, +sub +#line 501 "idl.yp" +{ "$_[1]$_[2]$_[3]" } + ], + [#Rule 110 + 'anytext', 3, +sub +#line 503 "idl.yp" +{ "$_[1]$_[2]$_[3]" } + ], + [#Rule 111 + 'anytext', 3, +sub +#line 505 "idl.yp" +{ "$_[1]$_[2]$_[3]" } + ], + [#Rule 112 + 'anytext', 3, +sub +#line 507 "idl.yp" +{ "$_[1]$_[2]$_[3]" } + ], + [#Rule 113 + 'anytext', 3, +sub +#line 509 "idl.yp" +{ "$_[1]$_[2]$_[3]" } + ], + [#Rule 114 + 'anytext', 5, +sub +#line 511 "idl.yp" +{ "$_[1]$_[2]$_[3]$_[4]$_[5]" } + ], + [#Rule 115 + 'anytext', 5, +sub +#line 513 "idl.yp" +{ "$_[1]$_[2]$_[3]$_[4]$_[5]" } + ], + [#Rule 116 + 'identifier', 1, undef + ], + [#Rule 117 + 'optional_identifier', 0, undef + ], + [#Rule 118 + 'optional_identifier', 1, undef + ], + [#Rule 119 + 'constant', 1, undef + ], + [#Rule 120 + 'text', 1, +sub +#line 531 "idl.yp" +{ "\"$_[1]\"" } + ], + [#Rule 121 + 'optional_semicolon', 0, undef + ], + [#Rule 122 + 'optional_semicolon', 1, undef + ] +], + @_); + bless($self,$class); +} + +#line 543 "idl.yp" + + +use Parse::Pidl qw(error); + +##################################################################### +# flatten an array of hashes into a single hash +sub FlattenHash($) +{ + my $a = shift; + my %b; + for my $d (@{$a}) { + for my $k (keys %{$d}) { + $b{$k} = $d->{$k}; + } + } + return \%b; +} + +##################################################################### +# traverse a perl data structure removing any empty arrays or +# hashes and any hash elements that map to undef +sub CleanData($) +{ + sub CleanData($); + my($v) = shift; + + return undef if (not defined($v)); + + if (ref($v) eq "ARRAY") { + foreach my $i (0 .. $#{$v}) { + CleanData($v->[$i]); + } + # this removes any undefined elements from the array + @{$v} = grep { defined $_ } @{$v}; + } elsif (ref($v) eq "HASH") { + foreach my $x (keys %{$v}) { + CleanData($v->{$x}); + if (!defined $v->{$x}) { + delete($v->{$x}); + next; + } + } + } + + return $v; +} + +sub _Error { + if (exists $_[0]->YYData->{ERRMSG}) { + error($_[0]->YYData, $_[0]->YYData->{ERRMSG}); + delete $_[0]->YYData->{ERRMSG}; + return; + } + + my $last_token = $_[0]->YYData->{LAST_TOKEN}; + + error($_[0]->YYData, "Syntax error near '$last_token'"); +} + +sub _Lexer($) +{ + my($parser)=shift; + + $parser->YYData->{INPUT} or return('',undef); + +again: + $parser->YYData->{INPUT} =~ s/^[ \t]*//; + + for ($parser->YYData->{INPUT}) { + if (/^\#/) { + # Linemarker format is described at + # http://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html + if (s/^\# (\d+) \"(.*?)\"(( \d+){1,4}|)//) { + $parser->YYData->{LINE} = $1-1; + $parser->YYData->{FILE} = $2; + goto again; + } + if (s/^\#line (\d+) \"(.*?)\"( \d+|)//) { + $parser->YYData->{LINE} = $1-1; + $parser->YYData->{FILE} = $2; + goto again; + } + if (s/^(\#.*)$//m) { + goto again; + } + } + if (s/^(\n)//) { + $parser->YYData->{LINE}++; + goto again; + } + if (s/^\"(.*?)\"//) { + $parser->YYData->{LAST_TOKEN} = $1; + return('TEXT',$1); + } + if (s/^(\d+)(\W|$)/$2/) { + $parser->YYData->{LAST_TOKEN} = $1; + return('CONSTANT',$1); + } + if (s/^([\w_]+)//) { + $parser->YYData->{LAST_TOKEN} = $1; + if ($1 =~ + /^(coclass|interface|import|importlib + |include|cpp_quote|typedef + |union|struct|enum|bitmap|pipe + |void|const|unsigned|signed)$/x) { + return $1; + } + return('IDENTIFIER',$1); + } + if (s/^(.)//s) { + $parser->YYData->{LAST_TOKEN} = $1; + return($1,$1); + } + } +} + +sub parse_string +{ + my ($data,$filename) = @_; + + my $self = new Parse::Pidl::IDL; + + $self->YYData->{FILE} = $filename; + $self->YYData->{INPUT} = $data; + $self->YYData->{LINE} = 0; + $self->YYData->{LAST_TOKEN} = "NONE"; + + my $idl = $self->YYParse( yylex => \&_Lexer, yyerror => \&_Error ); + + return CleanData($idl); +} + +sub parse_file($$) +{ + my ($filename,$incdirs) = @_; + + my $saved_delim = $/; + undef $/; + my $cpp = $ENV{CPP}; + my $options = ""; + if (! defined $cpp) { + if (defined $ENV{CC}) { + $cpp = "$ENV{CC}"; + $options = "-E"; + } else { + $cpp = "cpp"; + } + } + my $includes = join('',map { " -I$_" } @$incdirs); + my $data = `$cpp $options -D__PIDL__$includes -xc "$filename"`; + $/ = $saved_delim; + + return parse_string($data, $filename); +} + +1; diff --git a/tools/pidl/lib/Parse/Pidl/NDR.pm b/tools/pidl/lib/Parse/Pidl/NDR.pm new file mode 100644 index 00000000..003156e3 --- /dev/null +++ b/tools/pidl/lib/Parse/Pidl/NDR.pm @@ -0,0 +1,1472 @@ +################################################### +# Samba4 NDR info tree generator +# Copyright tridge@samba.org 2000-2003 +# Copyright tpot@samba.org 2001 +# Copyright jelmer@samba.org 2004-2006 +# released under the GNU GPL + +=pod + +=head1 NAME + +Parse::Pidl::NDR - NDR parsing information generator + +=head1 DESCRIPTION + +Return a table describing the order in which the parts of an element +should be parsed +Possible level types: + - POINTER + - ARRAY + - SUBCONTEXT + - SWITCH + - DATA + +=head1 AUTHOR + +Jelmer Vernooij <jelmer@samba.org> + +=cut + +package Parse::Pidl::NDR; + +require Exporter; +use vars qw($VERSION); +$VERSION = '0.01'; +@ISA = qw(Exporter); +@EXPORT = qw(GetPrevLevel GetNextLevel ContainsDeferred ContainsPipe ContainsString); +@EXPORT_OK = qw(GetElementLevelTable ParseElement ReturnTypeElement ValidElement align_type mapToScalar ParseType can_contain_deferred is_charset_array); + +use strict; +use Parse::Pidl qw(warning fatal); +use Parse::Pidl::Typelist qw(hasType getType typeIs expandAlias mapScalarType is_fixed_size_scalar); +use Parse::Pidl::Util qw(has_property property_matches); + +# Alignment of the built-in scalar types +my $scalar_alignment = { + 'void' => 0, + 'char' => 1, + 'int8' => 1, + 'uint8' => 1, + 'int16' => 2, + 'uint16' => 2, + 'int1632' => 3, + 'uint1632' => 3, + 'int32' => 4, + 'uint32' => 4, + 'int3264' => 5, + 'uint3264' => 5, + 'hyper' => 8, + 'double' => 8, + 'pointer' => 8, + 'dlong' => 4, + 'udlong' => 4, + 'udlongr' => 4, + 'DATA_BLOB' => 4, + 'string' => 4, + 'string_array' => 4, #??? + 'time_t' => 4, + 'uid_t' => 8, + 'gid_t' => 8, + 'NTTIME' => 4, + 'NTTIME_1sec' => 4, + 'NTTIME_hyper' => 8, + 'WERROR' => 4, + 'NTSTATUS' => 4, + 'COMRESULT' => 4, + 'dns_string' => 4, + 'nbt_string' => 4, + 'wrepl_nbt_name' => 4, + 'ipv4address' => 4, + 'ipv6address' => 4, #16? + 'dnsp_name' => 1, + 'dnsp_string' => 1 +}; + +sub GetElementLevelTable($$$) +{ + my ($e, $pointer_default, $ms_union) = @_; + + my $order = []; + my $is_deferred = 0; + my @bracket_array = (); + my @length_is = (); + my @size_is = (); + my $pointer_idx = 0; + + if (has_property($e, "size_is")) { + @size_is = split /,/, has_property($e, "size_is"); + } + + if (has_property($e, "length_is")) { + @length_is = split /,/, has_property($e, "length_is"); + } + + if (defined($e->{ARRAY_LEN})) { + @bracket_array = @{$e->{ARRAY_LEN}}; + } + + if (has_property($e, "out")) { + my $needptrs = 1; + + if (has_property($e, "string") and not has_property($e, "in")) { $needptrs++; } + if ($#bracket_array >= 0) { $needptrs = 0; } + + warning($e, "[out] argument `$e->{NAME}' not a pointer") if ($needptrs > $e->{POINTERS}); + } + + my $allow_pipe = ($e->{PARENT}->{TYPE} eq "FUNCTION"); + my $is_pipe = typeIs($e->{TYPE}, "PIPE"); + + if ($is_pipe) { + if (not $allow_pipe) { + fatal($e, "argument `$e->{NAME}' is a pipe and not allowed on $e->{PARENT}->{TYPE}"); + } + + if ($e->{POINTERS} > 1) { + fatal($e, "$e->{POINTERS} are not allowed on pipe element $e->{NAME}"); + } + + if ($e->{POINTERS} < 0) { + fatal($e, "pipe element $e->{NAME} needs pointer"); + } + + if ($e->{POINTERS} == 1 and pointer_type($e) ne "ref") { + fatal($e, "pointer should be 'ref' on pipe element $e->{NAME}"); + } + + if (scalar(@size_is) > 0) { + fatal($e, "size_is() on pipe element"); + } + + if (scalar(@length_is) > 0) { + fatal($e, "length_is() on pipe element"); + } + + if (scalar(@bracket_array) > 0) { + fatal($e, "brackets on pipe element"); + } + + if (defined(has_property($e, "subcontext"))) { + fatal($e, "subcontext on pipe element"); + } + + if (has_property($e, "switch_is")) { + fatal($e, "switch_is on pipe element"); + } + + if (can_contain_deferred($e->{TYPE})) { + fatal($e, "$e->{TYPE} can_contain_deferred - not allowed on pipe element"); + } + } + + # Parse the [][][][] style array stuff + for my $i (0 .. $#bracket_array) { + my $d = $bracket_array[$#bracket_array - $i]; + my $size = $d; + my $length = $d; + my $is_surrounding = 0; + my $is_varying = 0; + my $is_conformant = 0; + my $is_string = 0; + my $is_fixed = 0; + my $is_inline = 0; + my $is_to_null = 0; + + if ($d eq "*") { + $is_conformant = 1; + if ($size = shift @size_is) { + if ($e->{POINTERS} < 1 and has_property($e, "string")) { + $is_string = 1; + delete($e->{PROPERTIES}->{string}); + } + } elsif ((scalar(@size_is) == 0) and has_property($e, "string")) { + $is_string = 1; + delete($e->{PROPERTIES}->{string}); + } else { + fatal($e, "Must specify size_is() for conformant array!") + } + + if (($length = shift @length_is) or $is_string) { + $is_varying = 1; + } else { + $length = $size; + } + + if ($e == $e->{PARENT}->{ELEMENTS}[-1] + and $e->{PARENT}->{TYPE} ne "FUNCTION") { + $is_surrounding = 1; + } + } + + $is_fixed = 1 if (not $is_conformant and Parse::Pidl::Util::is_constant($size)); + $is_inline = 1 if (not $is_conformant and not Parse::Pidl::Util::is_constant($size)); + + if ($i == 0 and $is_fixed and has_property($e, "string")) { + $is_fixed = 0; + $is_varying = 1; + $is_string = 1; + delete($e->{PROPERTIES}->{string}); + } + + if (has_property($e, "to_null")) { + $is_to_null = 1; + } + + push (@$order, { + TYPE => "ARRAY", + SIZE_IS => $size, + LENGTH_IS => $length, + IS_DEFERRED => $is_deferred, + IS_SURROUNDING => $is_surrounding, + IS_ZERO_TERMINATED => $is_string, + IS_VARYING => $is_varying, + IS_CONFORMANT => $is_conformant, + IS_FIXED => $is_fixed, + IS_INLINE => $is_inline, + IS_TO_NULL => $is_to_null + }); + } + + # Next, all the pointers + foreach my $i (1..$e->{POINTERS}) { + my $level = "EMBEDDED"; + # Top level "ref" pointers do not have a referrent identifier + $level = "TOP" if ($i == 1 and $e->{PARENT}->{TYPE} eq "FUNCTION"); + + my $pt; + # + # Only the first level gets the pointer type from the + # pointer property, the others get them from + # the pointer_default() interface property + # + # see http://msdn2.microsoft.com/en-us/library/aa378984(VS.85).aspx + # (Here they talk about the rightmost pointer, but testing shows + # they mean the leftmost pointer.) + # + # --metze + # + $pt = pointer_type($e); + if ($i > 1) { + $is_deferred = 1 if ($pt ne "ref" and $e->{PARENT}->{TYPE} eq "FUNCTION"); + $pt = $pointer_default; + } + + push (@$order, { + TYPE => "POINTER", + POINTER_TYPE => $pt, + POINTER_INDEX => $pointer_idx, + IS_DEFERRED => "$is_deferred", + LEVEL => $level + }); + + warning($e, "top-level \[out\] pointer `$e->{NAME}' is not a \[ref\] pointer") + if ($i == 1 and $pt ne "ref" and + $e->{PARENT}->{TYPE} eq "FUNCTION" and + not has_property($e, "in")); + + $pointer_idx++; + + # everything that follows will be deferred + $is_deferred = 1 if ($level ne "TOP"); + + my $array_size = shift @size_is; + my $array_length; + my $is_varying; + my $is_conformant; + my $is_string = 0; + if ($array_size) { + $is_conformant = 1; + if ($array_length = shift @length_is) { + $is_varying = 1; + } else { + $array_length = $array_size; + $is_varying =0; + } + } + + if (scalar(@size_is) == 0 and has_property($e, "string") and + $i == $e->{POINTERS}) { + $is_string = 1; + $is_varying = $is_conformant = has_property($e, "noheader")?0:1; + delete($e->{PROPERTIES}->{string}); + } + + if ($array_size or $is_string) { + push (@$order, { + TYPE => "ARRAY", + SIZE_IS => $array_size, + LENGTH_IS => $array_length, + IS_DEFERRED => $is_deferred, + IS_SURROUNDING => 0, + IS_ZERO_TERMINATED => $is_string, + IS_VARYING => $is_varying, + IS_CONFORMANT => $is_conformant, + IS_FIXED => 0, + IS_INLINE => 0 + }); + + $is_deferred = 0; + } + } + + if ($is_pipe) { + push (@$order, { + TYPE => "PIPE", + IS_DEFERRED => 0, + CONTAINS_DEFERRED => 0, + }); + + my $i = 0; + foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; } + + return $order; + } + + if (defined(has_property($e, "subcontext"))) { + my $hdr_size = has_property($e, "subcontext"); + my $subsize = has_property($e, "subcontext_size"); + if (not defined($subsize)) { + $subsize = -1; + } + + push (@$order, { + TYPE => "SUBCONTEXT", + HEADER_SIZE => $hdr_size, + SUBCONTEXT_SIZE => $subsize, + IS_DEFERRED => $is_deferred, + COMPRESSION => has_property($e, "compression"), + }); + } + + if (my $switch = has_property($e, "switch_is")) { + push (@$order, { + TYPE => "SWITCH", + SWITCH_IS => $switch, + IS_DEFERRED => $is_deferred + }); + } + + if (scalar(@size_is) > 0) { + fatal($e, "size_is() on non-array element"); + } + + if (scalar(@length_is) > 0) { + fatal($e, "length_is() on non-array element"); + } + + if (has_property($e, "string")) { + fatal($e, "string() attribute on non-array element"); + } + + push (@$order, { + TYPE => "DATA", + DATA_TYPE => $e->{TYPE}, + IS_DEFERRED => $is_deferred, + CONTAINS_DEFERRED => can_contain_deferred($e->{TYPE}), + IS_SURROUNDING => 0 #FIXME + }); + + my $i = 0; + foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; } + + return $order; +} + +sub GetTypedefLevelTable($$$$) +{ + my ($e, $data, $pointer_default, $ms_union) = @_; + + my $order = []; + + push (@$order, { + TYPE => "TYPEDEF" + }); + + my $i = 0; + foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; } + + return $order; +} + +##################################################################### +# see if a type contains any deferred data +sub can_contain_deferred($) +{ + sub can_contain_deferred($); + my ($type) = @_; + + return 1 unless (hasType($type)); # assume the worst + + $type = getType($type); + + return 0 if (Parse::Pidl::Typelist::is_scalar($type)); + + return can_contain_deferred($type->{DATA}) if ($type->{TYPE} eq "TYPEDEF"); + + return 0 unless defined($type->{ELEMENTS}); + + foreach (@{$type->{ELEMENTS}}) { + return 1 if ($_->{POINTERS}); + return 1 if (can_contain_deferred ($_->{TYPE})); + } + + return 0; +} + +sub pointer_type($) +{ + my $e = shift; + + return undef unless $e->{POINTERS}; + + return "ref" if (has_property($e, "ref")); + return "full" if (has_property($e, "ptr")); + return "sptr" if (has_property($e, "sptr")); + return "unique" if (has_property($e, "unique")); + return "relative" if (has_property($e, "relative")); + return "relative_short" if (has_property($e, "relative_short")); + return "ignore" if (has_property($e, "ignore")); + + return undef; +} + +##################################################################### +# work out the correct alignment for a structure or union +sub find_largest_alignment($) +{ + my $s = shift; + + my $align = 1; + for my $e (@{$s->{ELEMENTS}}) { + my $a = 1; + + if ($e->{POINTERS}) { + # this is a hack for NDR64 + # the NDR layer translates this into + # an alignment of 4 for NDR and 8 for NDR64 + $a = 5; + } elsif (has_property($e, "subcontext")) { + $a = 1; + } elsif (has_property($e, "transmit_as")) { + $a = align_type($e->{PROPERTIES}->{transmit_as}); + } else { + $a = align_type($e->{TYPE}); + } + + $align = $a if ($align < $a); + } + + return $align; +} + +##################################################################### +# align a type +sub align_type($) +{ + sub align_type($); + my ($e) = @_; + + if (ref($e) eq "HASH" and $e->{TYPE} eq "SCALAR") { + return $scalar_alignment->{$e->{NAME}}; + } + + return 0 if ($e eq "EMPTY"); + + unless (hasType($e)) { + # it must be an external type - all we can do is guess + # warning($e, "assuming alignment of unknown type '$e' is 4"); + return 4; + } + + my $dt = getType($e); + + if ($dt->{TYPE} eq "TYPEDEF") { + return align_type($dt->{DATA}); + } elsif ($dt->{TYPE} eq "CONFORMANCE") { + return $dt->{DATA}->{ALIGN}; + } elsif ($dt->{TYPE} eq "ENUM") { + return align_type(Parse::Pidl::Typelist::enum_type_fn($dt)); + } elsif ($dt->{TYPE} eq "BITMAP") { + return align_type(Parse::Pidl::Typelist::bitmap_type_fn($dt)); + } elsif (($dt->{TYPE} eq "STRUCT") or ($dt->{TYPE} eq "UNION")) { + # Struct/union without body: assume 4 + return 4 unless (defined($dt->{ELEMENTS})); + return find_largest_alignment($dt); + } elsif (($dt->{TYPE} eq "PIPE")) { + return 5; + } + + die("Unknown data type type $dt->{TYPE}"); +} + +sub ParseElement($$$) +{ + my ($e, $pointer_default, $ms_union) = @_; + + $e->{TYPE} = expandAlias($e->{TYPE}); + + if (ref($e->{TYPE}) eq "HASH") { + $e->{TYPE} = ParseType($e->{TYPE}, $pointer_default, $ms_union); + } + + return { + NAME => $e->{NAME}, + TYPE => $e->{TYPE}, + PROPERTIES => $e->{PROPERTIES}, + LEVELS => GetElementLevelTable($e, $pointer_default, $ms_union), + REPRESENTATION_TYPE => ($e->{PROPERTIES}->{represent_as} or $e->{TYPE}), + ALIGN => align_type($e->{TYPE}), + ORIGINAL => $e + }; +} + +sub ParseStruct($$$) +{ + my ($struct, $pointer_default, $ms_union) = @_; + my @elements = (); + my $surrounding = undef; + + return { + TYPE => "STRUCT", + NAME => $struct->{NAME}, + SURROUNDING_ELEMENT => undef, + ELEMENTS => undef, + PROPERTIES => $struct->{PROPERTIES}, + ORIGINAL => $struct, + ALIGN => undef + } unless defined($struct->{ELEMENTS}); + + CheckPointerTypes($struct, $pointer_default); + + foreach my $x (@{$struct->{ELEMENTS}}) + { + my $e = ParseElement($x, $pointer_default, $ms_union); + if ($x != $struct->{ELEMENTS}[-1] and + $e->{LEVELS}[0]->{IS_SURROUNDING}) { + fatal($x, "conformant member not at end of struct"); + } + push @elements, $e; + } + + my $e = $elements[-1]; + if (defined($e) and defined($e->{LEVELS}[0]->{IS_SURROUNDING}) and + $e->{LEVELS}[0]->{IS_SURROUNDING}) { + $surrounding = $e; + } + + if (defined $e->{TYPE} && $e->{TYPE} eq "string" + && property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) { + $surrounding = $struct->{ELEMENTS}[-1]; + } + + my $align = undef; + if ($struct->{NAME}) { + $align = align_type($struct->{NAME}); + } + + return { + TYPE => "STRUCT", + NAME => $struct->{NAME}, + SURROUNDING_ELEMENT => $surrounding, + ELEMENTS => \@elements, + PROPERTIES => $struct->{PROPERTIES}, + ORIGINAL => $struct, + ALIGN => $align + }; +} + +sub ParseUnion($$) +{ + my ($e, $pointer_default, $ms_union) = @_; + my @elements = (); + my $is_ms_union = $ms_union; + $is_ms_union = 1 if has_property($e, "ms_union"); + my $hasdefault = 0; + my $switch_type = has_property($e, "switch_type"); + unless (defined($switch_type)) { $switch_type = "uint32"; } + if (has_property($e, "nodiscriminant")) { $switch_type = undef; } + + return { + TYPE => "UNION", + NAME => $e->{NAME}, + SWITCH_TYPE => $switch_type, + ELEMENTS => undef, + PROPERTIES => $e->{PROPERTIES}, + HAS_DEFAULT => $hasdefault, + IS_MS_UNION => $is_ms_union, + ORIGINAL => $e, + ALIGN => undef + } unless defined($e->{ELEMENTS}); + + CheckPointerTypes($e, $pointer_default); + + foreach my $x (@{$e->{ELEMENTS}}) + { + my $t; + if ($x->{TYPE} eq "EMPTY") { + $t = { TYPE => "EMPTY" }; + } else { + $t = ParseElement($x, $pointer_default, $ms_union); + } + if (has_property($x, "default")) { + $t->{CASE} = "default"; + $hasdefault = 1; + } elsif (defined($x->{PROPERTIES}->{case})) { + $t->{CASE} = "case $x->{PROPERTIES}->{case}"; + } else { + die("Union element $x->{NAME} has neither default nor case property"); + } + push @elements, $t; + } + + my $align = undef; + if ($e->{NAME}) { + $align = align_type($e->{NAME}); + } + + return { + TYPE => "UNION", + NAME => $e->{NAME}, + SWITCH_TYPE => $switch_type, + ELEMENTS => \@elements, + PROPERTIES => $e->{PROPERTIES}, + HAS_DEFAULT => $hasdefault, + IS_MS_UNION => $is_ms_union, + ORIGINAL => $e, + ALIGN => $align + }; +} + +sub ParseEnum($$) +{ + my ($e, $pointer_default, $ms_union) = @_; + + return { + TYPE => "ENUM", + NAME => $e->{NAME}, + BASE_TYPE => Parse::Pidl::Typelist::enum_type_fn($e), + ELEMENTS => $e->{ELEMENTS}, + PROPERTIES => $e->{PROPERTIES}, + ORIGINAL => $e + }; +} + +sub ParseBitmap($$$) +{ + my ($e, $pointer_default, $ms_union) = @_; + + return { + TYPE => "BITMAP", + NAME => $e->{NAME}, + BASE_TYPE => Parse::Pidl::Typelist::bitmap_type_fn($e), + ELEMENTS => $e->{ELEMENTS}, + PROPERTIES => $e->{PROPERTIES}, + ORIGINAL => $e + }; +} + +sub ParsePipe($$$) +{ + my ($pipe, $pointer_default, $ms_union) = @_; + + my $pname = $pipe->{NAME}; + $pname = $pipe->{PARENT}->{NAME} unless defined $pname; + + if (not defined($pipe->{PROPERTIES}) + and defined($pipe->{PARENT}->{PROPERTIES})) { + $pipe->{PROPERTIES} = $pipe->{PARENT}->{PROPERTIES}; + } + + if (ref($pipe->{DATA}) eq "HASH") { + if (not defined($pipe->{DATA}->{PROPERTIES}) + and defined($pipe->{PROPERTIES})) { + $pipe->{DATA}->{PROPERTIES} = $pipe->{PROPERTIES}; + } + } + + my $struct = ParseStruct($pipe->{DATA}, $pointer_default, $ms_union); + $struct->{ALIGN} = 5; + $struct->{NAME} = "$pname\_chunk"; + + # 'count' is element [0] and 'array' [1] + my $e = $struct->{ELEMENTS}[1]; + # level [0] is of type "ARRAY" + my $l = $e->{LEVELS}[1]; + + # here we check that pipe elements have a fixed size type + while (defined($l)) { + my $cl = $l; + $l = GetNextLevel($e, $cl); + if ($cl->{TYPE} ne "DATA") { + fatal($pipe, el_name($pipe) . ": pipe contains non DATA level"); + } + + # for now we only support scalars + next if is_fixed_size_scalar($cl->{DATA_TYPE}); + + fatal($pipe, el_name($pipe) . ": pipe contains non fixed size type[$cl->{DATA_TYPE}]"); + } + + return { + TYPE => "PIPE", + NAME => $pipe->{NAME}, + DATA => $struct, + PROPERTIES => $pipe->{PROPERTIES}, + ORIGINAL => $pipe, + }; +} + +sub ParseType($$$) +{ + my ($d, $pointer_default, $ms_union) = @_; + + my $data = { + STRUCT => \&ParseStruct, + UNION => \&ParseUnion, + ENUM => \&ParseEnum, + BITMAP => \&ParseBitmap, + TYPEDEF => \&ParseTypedef, + PIPE => \&ParsePipe, + }->{$d->{TYPE}}->($d, $pointer_default, $ms_union); + + return $data; +} + +sub ParseTypedef($$) +{ + my ($d, $pointer_default, $ms_union) = @_; + + my $data; + + if (ref($d->{DATA}) eq "HASH") { + if (defined($d->{DATA}->{PROPERTIES}) + and not defined($d->{PROPERTIES})) { + $d->{PROPERTIES} = $d->{DATA}->{PROPERTIES}; + } + + $data = ParseType($d->{DATA}, $pointer_default, $ms_union); + $data->{ALIGN} = align_type($d->{NAME}); + } else { + $data = getType($d->{DATA}); + } + + return { + NAME => $d->{NAME}, + TYPE => $d->{TYPE}, + PROPERTIES => $d->{PROPERTIES}, + LEVELS => GetTypedefLevelTable($d, $data, $pointer_default, $ms_union), + DATA => $data, + ORIGINAL => $d + }; +} + +sub ParseConst($$) +{ + my ($ndr,$d) = @_; + + return $d; +} + +sub ParseFunction($$$$) +{ + my ($ndr,$d,$opnum,$ms_union) = @_; + my @elements = (); + my $rettype = undef; + my $thisopnum = undef; + + CheckPointerTypes($d, "ref"); + + if (not defined($d->{PROPERTIES}{noopnum})) { + $thisopnum = ${$opnum}; + ${$opnum}++; + } + + foreach my $x (@{$d->{ELEMENTS}}) { + my $e = ParseElement($x, $ndr->{PROPERTIES}->{pointer_default}, $ms_union); + push (@{$e->{DIRECTION}}, "in") if (has_property($x, "in")); + push (@{$e->{DIRECTION}}, "out") if (has_property($x, "out")); + + push (@elements, $e); + } + + if ($d->{RETURN_TYPE} ne "void") { + $rettype = expandAlias($d->{RETURN_TYPE}); + } + + return { + NAME => $d->{NAME}, + TYPE => "FUNCTION", + OPNUM => $thisopnum, + RETURN_TYPE => $rettype, + PROPERTIES => $d->{PROPERTIES}, + ELEMENTS => \@elements, + ORIGINAL => $d + }; +} + +sub ReturnTypeElement($) +{ + my ($fn) = @_; + + return undef unless defined($fn->{RETURN_TYPE}); + + my $e = { + "NAME" => "result", + "TYPE" => $fn->{RETURN_TYPE}, + "PROPERTIES" => undef, + "POINTERS" => 0, + "ARRAY_LEN" => [], + "FILE" => $fn->{FILE}, + "LINE" => $fn->{LINE}, + }; + + return ParseElement($e, 0, 0); +} + +sub CheckPointerTypes($$) +{ + my ($s,$default) = @_; + + return unless defined($s->{ELEMENTS}); + + foreach my $e (@{$s->{ELEMENTS}}) { + if ($e->{POINTERS} and not defined(pointer_type($e))) { + $e->{PROPERTIES}->{$default} = '1'; + } + } +} + +sub FindNestedTypes($$) +{ + sub FindNestedTypes($$); + my ($l, $t) = @_; + + return unless defined($t->{ELEMENTS}); + return if ($t->{TYPE} eq "ENUM"); + return if ($t->{TYPE} eq "BITMAP"); + + foreach (@{$t->{ELEMENTS}}) { + if (ref($_->{TYPE}) eq "HASH") { + push (@$l, $_->{TYPE}) if (defined($_->{TYPE}->{NAME})); + FindNestedTypes($l, $_->{TYPE}); + } + } +} + +sub ParseInterface($) +{ + my $idl = shift; + my @types = (); + my @consts = (); + my @functions = (); + my @endpoints; + my $opnum = 0; + my $version; + my $ms_union = 0; + $ms_union = 1 if has_property($idl, "ms_union"); + + if (not has_property($idl, "pointer_default")) { + # MIDL defaults to "ptr" in DCE compatible mode (/osf) + # and "unique" in Microsoft Extensions mode (default) + $idl->{PROPERTIES}->{pointer_default} = "unique"; + } + + foreach my $d (@{$idl->{DATA}}) { + if ($d->{TYPE} eq "FUNCTION") { + push (@functions, ParseFunction($idl, $d, \$opnum, $ms_union)); + } elsif ($d->{TYPE} eq "CONST") { + push (@consts, ParseConst($idl, $d)); + } else { + push (@types, ParseType($d, $idl->{PROPERTIES}->{pointer_default}, $ms_union)); + FindNestedTypes(\@types, $d); + } + } + + $version = "0.0"; + + if(defined $idl->{PROPERTIES}->{version}) { + my @if_version = split(/\./, $idl->{PROPERTIES}->{version}); + if ($if_version[0] == $idl->{PROPERTIES}->{version}) { + $version = $idl->{PROPERTIES}->{version}; + } else { + $version = $if_version[1] << 16 | $if_version[0]; + } + } + + # If no endpoint is set, default to the interface name as a named pipe + if (!defined $idl->{PROPERTIES}->{endpoint}) { + push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME} . "]\""; + } else { + @endpoints = split /,/, $idl->{PROPERTIES}->{endpoint}; + } + + return { + NAME => $idl->{NAME}, + UUID => lc(has_property($idl, "uuid")), + VERSION => $version, + TYPE => "INTERFACE", + PROPERTIES => $idl->{PROPERTIES}, + FUNCTIONS => \@functions, + CONSTS => \@consts, + TYPES => \@types, + ENDPOINTS => \@endpoints, + ORIGINAL => $idl + }; +} + +# Convert a IDL tree to a NDR tree +# Gives a result tree describing all that's necessary for easily generating +# NDR parsers / generators +sub Parse($) +{ + my $idl = shift; + + return undef unless (defined($idl)); + + Parse::Pidl::NDR::Validate($idl); + + my @ndr = (); + + foreach (@{$idl}) { + ($_->{TYPE} eq "CPP_QUOTE") && push(@ndr, $_); + ($_->{TYPE} eq "INTERFACE") && push(@ndr, ParseInterface($_)); + ($_->{TYPE} eq "IMPORT") && push(@ndr, $_); + } + + return \@ndr; +} + +sub GetNextLevel($$) +{ + my $e = shift; + my $fl = shift; + + my $seen = 0; + + foreach my $l (@{$e->{LEVELS}}) { + return $l if ($seen); + ($seen = 1) if ($l == $fl); + } + + return undef; +} + +sub GetPrevLevel($$) +{ + my ($e,$fl) = @_; + my $prev = undef; + + foreach my $l (@{$e->{LEVELS}}) { + (return $prev) if ($l == $fl); + $prev = $l; + } + + return undef; +} + +sub ContainsString($) +{ + my ($e) = @_; + + if (property_matches($e, "flag", ".*STR_NULLTERM.*")) { + return 1; + } + if (exists($e->{LEVELS}) and $e->{LEVELS}->[0]->{TYPE} eq "ARRAY" and + ($e->{LEVELS}->[0]->{IS_FIXED} or $e->{LEVELS}->[0]->{IS_INLINE}) and + has_property($e, "charset")) + { + return 1; + } + + foreach my $l (@{$e->{LEVELS}}) { + return 1 if ($l->{TYPE} eq "ARRAY" and $l->{IS_ZERO_TERMINATED}); + } + if (property_matches($e, "charset", ".*DOS.*")) { + return 1; + } + + return 0; +} + +sub ContainsDeferred($$) +{ + my ($e,$l) = @_; + + return 1 if ($l->{CONTAINS_DEFERRED}); + + while ($l = GetNextLevel($e,$l)) + { + return 1 if ($l->{IS_DEFERRED}); + return 1 if ($l->{CONTAINS_DEFERRED}); + } + + return 0; +} + +sub ContainsPipe($$) +{ + my ($e,$l) = @_; + + return 1 if ($l->{TYPE} eq "PIPE"); + + while ($l = GetNextLevel($e,$l)) + { + return 1 if ($l->{TYPE} eq "PIPE"); + } + + return 0; +} + +sub el_name($) +{ + my $e = shift; + my $name = "<ANONYMOUS>"; + + $name = $e->{NAME} if defined($e->{NAME}); + + if (defined($e->{PARENT}) and defined($e->{PARENT}->{NAME})) { + return "$e->{PARENT}->{NAME}.$name"; + } + + if (defined($e->{PARENT}) and + defined($e->{PARENT}->{PARENT}) and + defined($e->{PARENT}->{PARENT}->{NAME})) { + return "$e->{PARENT}->{PARENT}->{NAME}.$name"; + } + + return $name; +} + +################################### +# find a sibling var in a structure +sub find_sibling($$) +{ + my($e,$name) = @_; + my($fn) = $e->{PARENT}; + + if ($name =~ /\*(.*)/) { + $name = $1; + } + + for my $e2 (@{$fn->{ELEMENTS}}) { + return $e2 if ($e2->{NAME} eq $name); + } + + return undef; +} + +my %property_list = ( + # interface + "helpstring" => ["INTERFACE", "FUNCTION"], + "version" => ["INTERFACE"], + "uuid" => ["INTERFACE"], + "endpoint" => ["INTERFACE"], + "pointer_default" => ["INTERFACE"], + "helper" => ["INTERFACE"], + "pyhelper" => ["INTERFACE"], + "authservice" => ["INTERFACE"], + "restricted" => ["INTERFACE"], + "no_srv_register" => ["INTERFACE"], + + # dcom + "object" => ["INTERFACE"], + "local" => ["INTERFACE", "FUNCTION"], + "iid_is" => ["ELEMENT"], + "call_as" => ["FUNCTION"], + "idempotent" => ["FUNCTION"], + + # function + "noopnum" => ["FUNCTION"], + "in" => ["ELEMENT"], + "out" => ["ELEMENT"], + + # pointer + "ref" => ["ELEMENT", "TYPEDEF"], + "ptr" => ["ELEMENT", "TYPEDEF"], + "unique" => ["ELEMENT", "TYPEDEF"], + "ignore" => ["ELEMENT"], + "relative" => ["ELEMENT", "TYPEDEF"], + "relative_short" => ["ELEMENT", "TYPEDEF"], + "null_is_ffffffff" => ["ELEMENT"], + "relative_base" => ["TYPEDEF", "STRUCT", "UNION"], + + "gensize" => ["TYPEDEF", "STRUCT", "UNION"], + "value" => ["ELEMENT"], + "flag" => ["ELEMENT", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "PIPE"], + + # generic + "public" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "PIPE"], + "nopush" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "PIPE"], + "nopull" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "PIPE"], + "nosize" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"], + "noprint" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "ELEMENT", "PIPE"], + "nopython" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"], + "todo" => ["FUNCTION"], + "skip" => ["ELEMENT"], + "skip_noinit" => ["ELEMENT"], + + # union + "switch_is" => ["ELEMENT"], + "switch_type" => ["ELEMENT", "UNION"], + "nodiscriminant" => ["UNION"], + "ms_union" => ["INTERFACE", "UNION"], + "case" => ["ELEMENT"], + "default" => ["ELEMENT"], + + "represent_as" => ["ELEMENT"], + "transmit_as" => ["ELEMENT"], + + # subcontext + "subcontext" => ["ELEMENT"], + "subcontext_size" => ["ELEMENT"], + "compression" => ["ELEMENT"], + + # enum + "enum8bit" => ["ENUM"], + "enum16bit" => ["ENUM"], + "v1_enum" => ["ENUM"], + + # bitmap + "bitmap8bit" => ["BITMAP"], + "bitmap16bit" => ["BITMAP"], + "bitmap32bit" => ["BITMAP"], + "bitmap64bit" => ["BITMAP"], + + # array + "range" => ["ELEMENT", "PIPE"], + "size_is" => ["ELEMENT"], + "string" => ["ELEMENT"], + "noheader" => ["ELEMENT"], + "charset" => ["ELEMENT"], + "length_is" => ["ELEMENT"], + "to_null" => ["ELEMENT"], +); + +##################################################################### +# check for unknown properties +sub ValidProperties($$) +{ + my ($e,$t) = @_; + + return unless defined $e->{PROPERTIES}; + + foreach my $key (keys %{$e->{PROPERTIES}}) { + warning($e, el_name($e) . ": unknown property '$key'") + unless defined($property_list{$key}); + + fatal($e, el_name($e) . ": property '$key' not allowed on '$t'") + unless grep(/^$t$/, @{$property_list{$key}}); + } +} + +sub mapToScalar($) +{ + sub mapToScalar($); + my $t = shift; + return $t->{NAME} if (ref($t) eq "HASH" and $t->{TYPE} eq "SCALAR"); + my $ti = getType($t); + + if (not defined ($ti)) { + return undef; + } elsif ($ti->{TYPE} eq "TYPEDEF") { + return mapToScalar($ti->{DATA}); + } elsif ($ti->{TYPE} eq "ENUM") { + return Parse::Pidl::Typelist::enum_type_fn($ti); + } elsif ($ti->{TYPE} eq "BITMAP") { + return Parse::Pidl::Typelist::bitmap_type_fn($ti); + } + + return undef; +} + +##################################################################### +# validate an element +sub ValidElement($) +{ + my $e = shift; + + ValidProperties($e,"ELEMENT"); + + # Check whether switches are used correctly. + if (my $switch = has_property($e, "switch_is")) { + my $e2 = find_sibling($e, $switch); + my $type = getType($e->{TYPE}); + + if (defined($type) and $type->{DATA}->{TYPE} ne "UNION") { + fatal($e, el_name($e) . ": switch_is() used on non-union type $e->{TYPE} which is a $type->{DATA}->{TYPE}"); + } + + if (not has_property($type->{DATA}, "nodiscriminant") and defined($e2)) { + my $discriminator_type = has_property($type->{DATA}, "switch_type"); + $discriminator_type = "uint32" unless defined ($discriminator_type); + + my $t1 = mapScalarType(mapToScalar($discriminator_type)); + + if (not defined($t1)) { + fatal($e, el_name($e) . ": unable to map discriminator type '$discriminator_type' to scalar"); + } + + my $t2 = mapScalarType(mapToScalar($e2->{TYPE})); + if (not defined($t2)) { + fatal($e, el_name($e) . ": unable to map variable used for switch_is() to scalar"); + } + + if ($t1 ne $t2) { + warning($e, el_name($e) . ": switch_is() is of type $e2->{TYPE} ($t2), while discriminator type for union $type->{NAME} is $discriminator_type ($t1)"); + } + } + } + + if (has_property($e, "subcontext") and has_property($e, "represent_as")) { + fatal($e, el_name($e) . " : subcontext() and represent_as() can not be used on the same element"); + } + + if (has_property($e, "subcontext") and has_property($e, "transmit_as")) { + fatal($e, el_name($e) . " : subcontext() and transmit_as() can not be used on the same element"); + } + + if (has_property($e, "represent_as") and has_property($e, "transmit_as")) { + fatal($e, el_name($e) . " : represent_as() and transmit_as() can not be used on the same element"); + } + + if (has_property($e, "represent_as") and has_property($e, "value")) { + fatal($e, el_name($e) . " : represent_as() and value() can not be used on the same element"); + } + + if (has_property($e, "subcontext")) { + warning($e, "subcontext() is deprecated. Use represent_as() or transmit_as() instead"); + } + + if (defined (has_property($e, "subcontext_size")) and not defined(has_property($e, "subcontext"))) { + fatal($e, el_name($e) . " : subcontext_size() on non-subcontext element"); + } + + if (defined (has_property($e, "compression")) and not defined(has_property($e, "subcontext"))) { + fatal($e, el_name($e) . " : compression() on non-subcontext element"); + } + + if (!$e->{POINTERS} && ( + has_property($e, "ptr") or + has_property($e, "unique") or + has_property($e, "relative") or + has_property($e, "relative_short") or + has_property($e, "ref"))) { + fatal($e, el_name($e) . " : pointer properties on non-pointer element\n"); + } +} + +##################################################################### +# validate an enum +sub ValidEnum($) +{ + my ($enum) = @_; + + ValidProperties($enum, "ENUM"); +} + +##################################################################### +# validate a bitmap +sub ValidBitmap($) +{ + my ($bitmap) = @_; + + ValidProperties($bitmap, "BITMAP"); +} + +##################################################################### +# validate a struct +sub ValidStruct($) +{ + my($struct) = shift; + + ValidProperties($struct, "STRUCT"); + + return unless defined($struct->{ELEMENTS}); + + foreach my $e (@{$struct->{ELEMENTS}}) { + $e->{PARENT} = $struct; + ValidElement($e); + } +} + +##################################################################### +# parse a union +sub ValidUnion($) +{ + my($union) = shift; + + ValidProperties($union,"UNION"); + + if (has_property($union->{PARENT}, "nodiscriminant") and + has_property($union->{PARENT}, "switch_type")) { + fatal($union->{PARENT}, $union->{PARENT}->{NAME} . ": switch_type(" . $union->{PARENT}->{PROPERTIES}->{switch_type} . ") on union without discriminant"); + } + + return unless defined($union->{ELEMENTS}); + + foreach my $e (@{$union->{ELEMENTS}}) { + $e->{PARENT} = $union; + + if (defined($e->{PROPERTIES}->{default}) and + defined($e->{PROPERTIES}->{case})) { + fatal($e, "Union member $e->{NAME} can not have both default and case properties!"); + } + + unless (defined ($e->{PROPERTIES}->{default}) or + defined ($e->{PROPERTIES}->{case})) { + fatal($e, "Union member $e->{NAME} must have default or case property"); + } + + if (has_property($e, "ref")) { + fatal($e, el_name($e) . ": embedded ref pointers are not supported yet\n"); + } + + + ValidElement($e); + } +} + +##################################################################### +# validate a pipe +sub ValidPipe($) +{ + my ($pipe) = @_; + my $struct = $pipe->{DATA}; + + ValidProperties($pipe, "PIPE"); + + $struct->{PARENT} = $pipe; + + $struct->{FILE} = $pipe->{FILE} unless defined($struct->{FILE}); + $struct->{LINE} = $pipe->{LINE} unless defined($struct->{LINE}); + + ValidType($struct); +} + +##################################################################### +# parse a typedef +sub ValidTypedef($) +{ + my($typedef) = shift; + my $data = $typedef->{DATA}; + + ValidProperties($typedef, "TYPEDEF"); + + return unless (ref($data) eq "HASH"); + + $data->{PARENT} = $typedef; + + $data->{FILE} = $typedef->{FILE} unless defined($data->{FILE}); + $data->{LINE} = $typedef->{LINE} unless defined($data->{LINE}); + + ValidType($data); +} + +##################################################################### +# validate a function +sub ValidFunction($) +{ + my($fn) = shift; + + ValidProperties($fn,"FUNCTION"); + + foreach my $e (@{$fn->{ELEMENTS}}) { + $e->{PARENT} = $fn; + if (has_property($e, "ref") && !$e->{POINTERS}) { + fatal($e, "[ref] variables must be pointers ($fn->{NAME}/$e->{NAME})"); + } + ValidElement($e); + } +} + +##################################################################### +# validate a type +sub ValidType($) +{ + my ($t) = @_; + + { + TYPEDEF => \&ValidTypedef, + STRUCT => \&ValidStruct, + UNION => \&ValidUnion, + ENUM => \&ValidEnum, + BITMAP => \&ValidBitmap, + PIPE => \&ValidPipe + }->{$t->{TYPE}}->($t); +} + +##################################################################### +# parse the interface definitions +sub ValidInterface($) +{ + my($interface) = shift; + my($data) = $interface->{DATA}; + + if (has_property($interface, "helper")) { + warning($interface, "helper() is pidl-specific and deprecated. Use `include' instead"); + } + + ValidProperties($interface,"INTERFACE"); + + if (has_property($interface, "pointer_default")) { + if (not grep (/$interface->{PROPERTIES}->{pointer_default}/, + ("ref", "unique", "ptr"))) { + fatal($interface, "Unknown default pointer type `$interface->{PROPERTIES}->{pointer_default}'"); + } + } + + if (has_property($interface, "object")) { + if (has_property($interface, "version") && + $interface->{PROPERTIES}->{version} != 0) { + fatal($interface, "Object interfaces must have version 0.0 ($interface->{NAME})"); + } + + if (!defined($interface->{BASE}) && + not ($interface->{NAME} eq "IUnknown")) { + fatal($interface, "Object interfaces must all derive from IUnknown ($interface->{NAME})"); + } + } + + foreach my $d (@{$data}) { + ($d->{TYPE} eq "FUNCTION") && ValidFunction($d); + ($d->{TYPE} eq "TYPEDEF" or + $d->{TYPE} eq "STRUCT" or + $d->{TYPE} eq "UNION" or + $d->{TYPE} eq "ENUM" or + $d->{TYPE} eq "BITMAP" or + $d->{TYPE} eq "PIPE") && ValidType($d); + } + +} + +##################################################################### +# Validate an IDL structure +sub Validate($) +{ + my($idl) = shift; + + foreach my $x (@{$idl}) { + ($x->{TYPE} eq "INTERFACE") && + ValidInterface($x); + ($x->{TYPE} eq "IMPORTLIB") && + fatal($x, "importlib() not supported"); + } +} + +sub is_charset_array($$) +{ + my ($e,$l) = @_; + + return 0 if ($l->{TYPE} ne "ARRAY"); + + my $nl = GetNextLevel($e,$l); + + return 0 unless ($nl->{TYPE} eq "DATA"); + + return has_property($e, "charset"); +} + + + +1; diff --git a/tools/pidl/lib/Parse/Pidl/ODL.pm b/tools/pidl/lib/Parse/Pidl/ODL.pm new file mode 100644 index 00000000..14e77fa9 --- /dev/null +++ b/tools/pidl/lib/Parse/Pidl/ODL.pm @@ -0,0 +1,130 @@ +########################################## +# Converts ODL stuctures to IDL structures +# (C) 2004-2005, 2008 Jelmer Vernooij <jelmer@samba.org> + +package Parse::Pidl::ODL; + +use Parse::Pidl qw(error); +use Parse::Pidl::IDL; +use Parse::Pidl::Util qw(has_property unmake_str); +use Parse::Pidl::Typelist qw(hasType getType); +use File::Basename; +use strict; + +use vars qw($VERSION); +$VERSION = '0.01'; + +sub FunctionAddObjArgs($) +{ + my $e = shift; + + unshift(@{$e->{ELEMENTS}}, { + 'NAME' => 'ORPCthis', + 'POINTERS' => 0, + 'PROPERTIES' => { 'in' => '1' }, + 'TYPE' => 'ORPCTHIS', + 'FILE' => $e->{FILE}, + 'LINE' => $e->{LINE} + }); + unshift(@{$e->{ELEMENTS}}, { + 'NAME' => 'ORPCthat', + 'POINTERS' => 1, + 'PROPERTIES' => { 'out' => '1', 'ref' => '1' }, + 'TYPE' => 'ORPCTHAT', + 'FILE' => $e->{FILE}, + 'LINE' => $e->{LINE} + }); +} + +sub ReplaceInterfacePointers($) +{ + my ($e) = @_; + foreach my $x (@{$e->{ELEMENTS}}) { + next unless (hasType($x->{TYPE})); + next unless getType($x->{TYPE})->{DATA}->{TYPE} eq "INTERFACE"; + + $x->{TYPE} = "MInterfacePointer"; + } +} + +# Add ORPC specific bits to an interface. +sub ODL2IDL +{ + my ($odl, $basedir, $opt_incdirs) = (@_); + my $addedorpc = 0; + my $interfaces = {}; + + foreach my $x (@$odl) { + if ($x->{TYPE} eq "IMPORT") { + foreach my $idl_file (@{$x->{PATHS}}) { + $idl_file = unmake_str($idl_file); + my $idl_path = undef; + foreach ($basedir, @$opt_incdirs) { + if (-f "$_/$idl_file") { + $idl_path = "$_/$idl_file"; + last; + } + } + unless ($idl_path) { + error($x, "Unable to open include file `$idl_file'"); + next; + } + my $podl = Parse::Pidl::IDL::parse_file($idl_path, $opt_incdirs); + if (defined($podl)) { + require Parse::Pidl::Typelist; + my $basename = basename($idl_path, ".idl"); + + Parse::Pidl::Typelist::LoadIdl($podl, $basename); + my $pidl = ODL2IDL($podl, $basedir, $opt_incdirs); + + foreach my $y (@$pidl) { + if ($y->{TYPE} eq "INTERFACE") { + $interfaces->{$y->{NAME}} = $y; + } + } + } else { + error($x, "Failed to parse $idl_path"); + } + } + } + + if ($x->{TYPE} eq "INTERFACE") { + $interfaces->{$x->{NAME}} = $x; + # Add [in] ORPCTHIS *this, [out] ORPCTHAT *that + # and replace interfacepointers with MInterfacePointer + # for 'object' interfaces + if (has_property($x, "object")) { + foreach my $e (@{$x->{DATA}}) { + ($e->{TYPE} eq "FUNCTION") && FunctionAddObjArgs($e); + ReplaceInterfacePointers($e); + } + $addedorpc = 1; + } + + if ($x->{BASE}) { + my $base = $interfaces->{$x->{BASE}}; + + unless (defined($base)) { + error($x, "Undefined base interface `$x->{BASE}'"); + } else { + foreach my $fn (reverse @{$base->{DATA}}) { + next unless ($fn->{TYPE} eq "FUNCTION"); + push (@{$x->{INHERITED_FUNCTIONS}}, $fn); + } + } + } + } + } + + unshift (@$odl, { + TYPE => "IMPORT", + PATHS => [ "\"orpc.idl\"" ], + FILE => undef, + LINE => undef + }) if ($addedorpc); + + + return $odl; +} + +1; diff --git a/tools/pidl/lib/Parse/Pidl/Samba3/ClientNDR.pm b/tools/pidl/lib/Parse/Pidl/Samba3/ClientNDR.pm new file mode 100644 index 00000000..6acf1c5a --- /dev/null +++ b/tools/pidl/lib/Parse/Pidl/Samba3/ClientNDR.pm @@ -0,0 +1,409 @@ +################################################### +# Samba3 client generator for IDL structures +# on top of Samba4 style NDR functions +# Copyright jelmer@samba.org 2005-2006 +# Copyright gd@samba.org 2008 +# released under the GNU GPL + +package Parse::Pidl::Samba3::ClientNDR; + +use Exporter; +@ISA = qw(Exporter); +@EXPORT_OK = qw(ParseFunction $res $res_hdr); + +use strict; +use Parse::Pidl qw(fatal warning error); +use Parse::Pidl::Util qw(has_property ParseExpr genpad); +use Parse::Pidl::NDR qw(ContainsPipe); +use Parse::Pidl::Typelist qw(mapTypeName); +use Parse::Pidl::Samba4 qw(DeclLong); +use Parse::Pidl::Samba4::Header qw(GenerateFunctionInEnv GenerateFunctionOutEnv); + +use vars qw($VERSION); +$VERSION = '0.01'; + +sub indent($) { my ($self) = @_; $self->{tabs}.="\t"; } +sub deindent($) { my ($self) = @_; $self->{tabs} = substr($self->{tabs}, 1); } +sub pidl($$) { my ($self,$txt) = @_; $self->{res} .= $txt ? "$self->{tabs}$txt\n" : "\n"; } +sub pidl_hdr($$) { my ($self, $txt) = @_; $self->{res_hdr} .= "$txt\n"; } +sub fn_declare($$) { my ($self,$n) = @_; $self->pidl($n); $self->pidl_hdr("$n;"); } + +sub new($) +{ + my ($class) = shift; + my $self = { res => "", res_hdr => "", tabs => "" }; + bless($self, $class); +} + +sub ElementDirection($) +{ + my ($e) = @_; + + return "[in,out]" if (has_property($e, "in") and has_property($e, "out")); + return "[in]" if (has_property($e, "in")); + return "[out]" if (has_property($e, "out")); + return "[in,out]"; +} + +sub HeaderProperties($$) +{ + my($props,$ignores) = @_; + my $ret = ""; + + foreach my $d (sort(keys %{$props})) { + next if (grep(/^$d$/, @$ignores)); + if($props->{$d} ne "1") { + $ret.= "$d($props->{$d}),"; + } else { + $ret.="$d,"; + } + } + + if ($ret) { + return "[" . substr($ret, 0, -1) . "]"; + } +} + +sub ParseInvalidResponse($$) +{ + my ($self, $type) = @_; + + if ($type eq "sync") { + $self->pidl("return NT_STATUS_INVALID_NETWORK_RESPONSE;"); + } elsif ($type eq "async") { + $self->pidl("tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);"); + $self->pidl("return;"); + } else { + die("ParseInvalidResponse($type)"); + } +} + +sub ParseFunctionAsyncState($$$) +{ + my ($self, $if, $fn) = @_; + + my $state_str = "struct rpccli_$fn->{NAME}_state"; + my $done_fn = "rpccli_$fn->{NAME}_done"; + + $self->pidl("$state_str {"); + $self->indent; + $self->pidl("TALLOC_CTX *out_mem_ctx;"); + if (defined($fn->{RETURN_TYPE})) { + $self->pidl(mapTypeName($fn->{RETURN_TYPE}). " result;"); + } + $self->deindent; + $self->pidl("};"); + $self->pidl(""); + $self->pidl("static void $done_fn(struct tevent_req *subreq);"); + $self->pidl(""); +} + +sub ParseFunctionAsyncSend($$$) +{ + my ($self, $if, $fn) = @_; + + my $fn_args = ""; + my $uif = uc($if); + my $ufn = "NDR_".uc($fn->{NAME}); + my $state_str = "struct rpccli_$fn->{NAME}_state"; + my $done_fn = "rpccli_$fn->{NAME}_done"; + my $out_mem_ctx = "rpccli_$fn->{NAME}_out_memory"; + my $fn_str = "struct tevent_req *rpccli_$fn->{NAME}_send"; + my $pad = genpad($fn_str); + + $fn_args .= "TALLOC_CTX *mem_ctx"; + $fn_args .= ",\n" . $pad . "struct tevent_context *ev"; + $fn_args .= ",\n" . $pad . "struct rpc_pipe_client *cli"; + + foreach (@{$fn->{ELEMENTS}}) { + my $dir = ElementDirection($_); + my $prop = HeaderProperties($_->{PROPERTIES}, ["in", "out"]); + $fn_args .= ",\n" . $pad . DeclLong($_, "_") . " /* $dir $prop */"; + } + + $self->fn_declare("$fn_str($fn_args)"); + $self->pidl("{"); + $self->indent; + $self->pidl("struct tevent_req *req;"); + $self->pidl("$state_str *state;"); + $self->pidl("struct tevent_req *subreq;"); + $self->pidl(""); + $self->pidl("req = tevent_req_create(mem_ctx, &state,"); + $self->pidl("\t\t\t$state_str);"); + $self->pidl("if (req == NULL) {"); + $self->indent; + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl("state->out_mem_ctx = NULL;"); + $self->pidl(""); + + my $out_params = 0; + foreach (@{$fn->{ELEMENTS}}) { + if (grep(/out/, @{$_->{DIRECTION}})) { + $out_params++; + } + } + + if ($out_params > 0) { + $self->pidl("state->out_mem_ctx = talloc_named_const(state, 0,"); + $self->pidl("\t\t \"$out_mem_ctx\");"); + $self->pidl("if (tevent_req_nomem(state->out_mem_ctx, req)) {"); + $self->indent; + $self->pidl("return tevent_req_post(req, ev);"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + } + + $fn_str = "subreq = dcerpc_$fn->{NAME}_send"; + $pad = "\t" . genpad($fn_str); + $fn_args = "state,\n" . $pad . "ev,\n" . $pad . "cli->binding_handle"; + foreach (@{$fn->{ELEMENTS}}) { + $fn_args .= ",\n" . $pad . "_". $_->{NAME}; + } + + $self->pidl("$fn_str($fn_args);"); + $self->pidl("if (tevent_req_nomem(subreq, req)) {"); + $self->indent; + $self->pidl("return tevent_req_post(req, ev);"); + $self->deindent; + $self->pidl("}"); + $self->pidl("tevent_req_set_callback(subreq, $done_fn, req);"); + $self->pidl("return req;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); +} + +sub ParseFunctionAsyncDone($$$) +{ + my ($self, $if, $fn) = @_; + + my $state_str = "struct rpccli_$fn->{NAME}_state"; + my $done_fn = "rpccli_$fn->{NAME}_done"; + + $self->pidl("static void $done_fn(struct tevent_req *subreq)"); + $self->pidl("{"); + $self->indent; + $self->pidl("struct tevent_req *req = tevent_req_callback_data("); + $self->pidl("\tsubreq, struct tevent_req);"); + $self->pidl("$state_str *state = tevent_req_data("); + $self->pidl("\treq, $state_str);"); + $self->pidl("NTSTATUS status;"); + $self->pidl("TALLOC_CTX *mem_ctx;"); + $self->pidl(""); + + $self->pidl("if (state->out_mem_ctx) {"); + $self->indent; + $self->pidl("mem_ctx = state->out_mem_ctx;"); + $self->deindent; + $self->pidl("} else {"); + $self->indent; + $self->pidl("mem_ctx = state;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + my $fn_str = "status = dcerpc_$fn->{NAME}_recv"; + my $pad = "\t" . genpad($fn_str); + my $fn_args = "subreq,\n" . $pad . "mem_ctx"; + if (defined($fn->{RETURN_TYPE})) { + $fn_args .= ",\n" . $pad . "&state->result"; + } + + $self->pidl("$fn_str($fn_args);"); + $self->pidl("TALLOC_FREE(subreq);"); + $self->pidl("if (!NT_STATUS_IS_OK(status)) {"); + $self->indent; + $self->pidl("tevent_req_nterror(req, status);"); + $self->pidl("return;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $self->pidl("tevent_req_done(req);"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); +} + +sub ParseFunctionAsyncRecv($$$) +{ + my ($self, $if, $fn) = @_; + + my $fn_args = ""; + my $state_str = "struct rpccli_$fn->{NAME}_state"; + my $fn_str = "NTSTATUS rpccli_$fn->{NAME}_recv"; + my $pad = genpad($fn_str); + + $fn_args .= "struct tevent_req *req,\n" . $pad . "TALLOC_CTX *mem_ctx"; + + if (defined($fn->{RETURN_TYPE})) { + $fn_args .= ",\n" . $pad . "$fn->{RETURN_TYPE} *result"; + } + + $self->fn_declare("$fn_str($fn_args)"); + $self->pidl("{"); + $self->indent; + $self->pidl("$state_str *state = tevent_req_data("); + $self->pidl("\treq, $state_str);"); + $self->pidl("NTSTATUS status;"); + $self->pidl(""); + $self->pidl("if (tevent_req_is_nterror(req, &status)) {"); + $self->indent; + $self->pidl("tevent_req_received(req);"); + $self->pidl("return status;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $self->pidl("/* Steal possible out parameters to the callers context */"); + $self->pidl("talloc_steal(mem_ctx, state->out_mem_ctx);"); + $self->pidl(""); + + if (defined($fn->{RETURN_TYPE})) { + $self->pidl("/* Return result */"); + $self->pidl("*result = state->result;"); + $self->pidl(""); + } + + $self->pidl("tevent_req_received(req);"); + $self->pidl("return NT_STATUS_OK;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); +} + +sub ParseFunctionSync($$$) +{ + my ($self, $if, $fn) = @_; + + my $fn_args = ""; + my $uif = uc($if); + my $ufn = "NDR_".uc($fn->{NAME}); + my $fn_str = "NTSTATUS rpccli_$fn->{NAME}"; + my $pad = genpad($fn_str); + + $fn_args .= "struct rpc_pipe_client *cli,\n" . $pad . "TALLOC_CTX *mem_ctx"; + + foreach (@{$fn->{ELEMENTS}}) { + my $dir = ElementDirection($_); + my $prop = HeaderProperties($_->{PROPERTIES}, ["in", "out"]); + $fn_args .= ",\n" . $pad . DeclLong($_, "_") . " /* $dir $prop */"; + } + + if (defined($fn->{RETURN_TYPE}) && ($fn->{RETURN_TYPE} eq "WERROR")) { + $fn_args .= ",\n" . $pad . "WERROR *werror"; + } + + $self->fn_declare("$fn_str($fn_args)"); + $self->pidl("{"); + $self->indent; + if (defined($fn->{RETURN_TYPE})) { + $self->pidl(mapTypeName($fn->{RETURN_TYPE})." result;"); + } + $self->pidl("NTSTATUS status;"); + $self->pidl(""); + + $fn_str = "status = dcerpc_$fn->{NAME}"; + $pad = "\t" . genpad($fn_str); + $fn_args = "cli->binding_handle,\n" . $pad . "mem_ctx"; + foreach (@{$fn->{ELEMENTS}}) { + $fn_args .= ",\n" . $pad . "_". $_->{NAME}; + } + if (defined($fn->{RETURN_TYPE})) { + $fn_args .= ",\n" . $pad . "&result"; + } + + $self->pidl("$fn_str($fn_args);"); + $self->pidl("if (!NT_STATUS_IS_OK(status)) {"); + $self->indent; + $self->pidl("return status;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $self->pidl("/* Return result */"); + if (not $fn->{RETURN_TYPE}) { + $self->pidl("return NT_STATUS_OK;"); + } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS") { + $self->pidl("return result;"); + } elsif ($fn->{RETURN_TYPE} eq "WERROR") { + $self->pidl("if (werror) {"); + $self->indent; + $self->pidl("*werror = result;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + $self->pidl("return werror_to_ntstatus(result);"); + } else { + warning($fn->{ORIGINAL}, "Unable to convert $fn->{RETURN_TYPE} to NTSTATUS"); + $self->pidl("return NT_STATUS_OK;"); + } + + $self->deindent; + $self->pidl("}"); + $self->pidl(""); +} + +sub ParseFunction($$$) +{ + my ($self, $if, $fn) = @_; + + $self->ParseFunctionAsyncState($if, $fn); + $self->ParseFunctionAsyncSend($if, $fn); + $self->ParseFunctionAsyncDone($if, $fn); + $self->ParseFunctionAsyncRecv($if, $fn); + + $self->ParseFunctionSync($if, $fn); +} + +sub ParseInterface($$) +{ + my ($self, $if) = @_; + + my $uif = uc($if->{NAME}); + + $self->pidl_hdr("#ifndef __CLI_$uif\__"); + $self->pidl_hdr("#define __CLI_$uif\__"); + foreach my $fn (@{$if->{FUNCTIONS}}) { + next if has_property($fn, "noopnum"); + next if has_property($fn, "todo"); + + my $skip = 0; + foreach my $e (@{$fn->{ELEMENTS}}) { + if (ContainsPipe($e, $e->{LEVELS}[0])) { + $skip = 1; + last; + } + } + next if $skip; + + $self->ParseFunction($if->{NAME}, $fn); + } + $self->pidl_hdr("#endif /* __CLI_$uif\__ */"); +} + +sub Parse($$$$) +{ + my($self,$ndr,$header,$c_header) = @_; + + $self->pidl("/*"); + $self->pidl(" * Unix SMB/CIFS implementation."); + $self->pidl(" * client auto-generated by pidl. DO NOT MODIFY!"); + $self->pidl(" */"); + $self->pidl(""); + $self->pidl("#include \"includes.h\""); + $self->pidl("#include \"$header\""); + $self->pidl_hdr("#include \"$c_header\""); + $self->pidl(""); + + foreach (@$ndr) { + $self->ParseInterface($_) if ($_->{TYPE} eq "INTERFACE"); + } + + return ($self->{res}, $self->{res_hdr}); +} + +1; diff --git a/tools/pidl/lib/Parse/Pidl/Samba3/ServerNDR.pm b/tools/pidl/lib/Parse/Pidl/Samba3/ServerNDR.pm new file mode 100644 index 00000000..c87d17a5 --- /dev/null +++ b/tools/pidl/lib/Parse/Pidl/Samba3/ServerNDR.pm @@ -0,0 +1,322 @@ +################################################### +# Samba3 server generator for IDL structures +# on top of Samba4 style NDR functions +# Copyright jelmer@samba.org 2005-2006 +# released under the GNU GPL + +package Parse::Pidl::Samba3::ServerNDR; + +use Exporter; +@ISA = qw(Exporter); +@EXPORT_OK = qw(DeclLevel); + +use strict; +use Parse::Pidl qw(warning error fatal); +use Parse::Pidl::Typelist qw(mapTypeName scalar_is_reference); +use Parse::Pidl::Util qw(ParseExpr has_property is_constant); +use Parse::Pidl::NDR qw(GetNextLevel ContainsPipe); +use Parse::Pidl::Samba4 qw(ElementStars DeclLong); +use Parse::Pidl::Samba4::Header qw(GenerateFunctionOutEnv); + +use vars qw($VERSION); +$VERSION = '0.01'; + +my $res; +my $res_hdr; +my $tabs = ""; +sub pidl_reset() { $res=""; $res_hdr="", $tabs=""; } +sub pidl_return() { my $s = $res; my $h = $res_hdr; pidl_reset(); return ($s, $h) } +sub indent() { $tabs.="\t"; } +sub deindent() { $tabs = substr($tabs, 1); } +sub pidl($) { my ($txt) = @_; $res .= $txt?$tabs.(shift)."\n":"\n"; } +sub pidl_hdr($) { $res_hdr .= (shift)."\n"; } +sub fn_declare($) { my ($n) = @_; pidl $n; pidl_hdr "$n;"; } + +sub DeclLevel($$) +{ + my ($e, $l) = @_; + my $res = ""; + + if (has_property($e, "charset")) { + $res .= "const char"; + } else { + $res .= mapTypeName($e->{TYPE}); + } + + my $stars = ElementStars($e, $l); + + $res .= " ".$stars unless ($stars eq ""); + + return $res; +} + +sub AllocOutVar($$$$$$$) +{ + my ($e, $mem_ctx, $name, $env, $check, $cleanup, $return) = @_; + + my $l = $e->{LEVELS}[0]; + + # we skip pointer to arrays + if ($l->{TYPE} eq "POINTER") { + my $nl = GetNextLevel($e, $l); + $l = $nl if ($nl->{TYPE} eq "ARRAY"); + } elsif + + # we don't support multi-dimentional arrays yet + ($l->{TYPE} eq "ARRAY") { + my $nl = GetNextLevel($e, $l); + if ($nl->{TYPE} eq "ARRAY") { + fatal($e->{ORIGINAL},"multi-dimentional [out] arrays are not supported!"); + } + } else { + # neither pointer nor array, no need to alloc something. + return; + } + + if ($l->{TYPE} eq "ARRAY") { + unless(defined($l->{SIZE_IS})) { + error($e->{ORIGINAL}, "No size known for array `$e->{NAME}'"); + pidl "#error No size known for array `$e->{NAME}'"; + } else { + my $size = ParseExpr($l->{SIZE_IS}, $env, $e); + pidl "$name = talloc_zero_array($mem_ctx, " . DeclLevel($e, 1) . ", $size);"; + } + } else { + pidl "$name = talloc_zero($mem_ctx, " . DeclLevel($e, 1) . ");"; + } + + pidl "if (" . $check->($name) . ") {"; + indent; + pidl $cleanup->($name) if defined($cleanup); + pidl $return->($name) if defined($return); + deindent; + pidl "}"; + pidl ""; +} + +sub CallWithStruct($$$$$$) +{ + my ($pipes_struct, $mem_ctx, $fn, $check, $cleanup, $return) = @_; + my $env = GenerateFunctionOutEnv($fn); + my $hasout = 0; + foreach (@{$fn->{ELEMENTS}}) { + if (grep(/out/, @{$_->{DIRECTION}})) { $hasout = 1; } + } + + pidl "ZERO_STRUCT(r->out);" if ($hasout); + + foreach (@{$fn->{ELEMENTS}}) { + my @dir = @{$_->{DIRECTION}}; + if (grep(/in/, @dir) and grep(/out/, @dir)) { + pidl "r->out.$_->{NAME} = r->in.$_->{NAME};"; + } + } + + foreach (@{$fn->{ELEMENTS}}) { + next if ContainsPipe($_, $_->{LEVELS}[0]); + my @dir = @{$_->{DIRECTION}}; + if (grep(/in/, @dir) and grep(/out/, @dir)) { + # noop + } elsif (grep(/out/, @dir) and not + has_property($_, "represent_as")) { + AllocOutVar($_, $mem_ctx, "r->out.$_->{NAME}", $env, + $check, $cleanup, $return); + } + } + + my $proto = "_$fn->{NAME}(struct pipes_struct *p, struct $fn->{NAME} *r)"; + my $ret = "_$fn->{NAME}($pipes_struct, r)"; + + if ($fn->{RETURN_TYPE}) { + $ret = "r->out.result = $ret"; + $proto = mapTypeName($fn->{RETURN_TYPE})." $proto"; + } else { + $proto = "void $proto"; + } + + pidl_hdr "$proto;"; + pidl "$ret;"; +} + +sub ParseFunction($$) +{ + my ($if,$fn) = @_; + + my $op = "NDR_".uc($fn->{NAME}); + + pidl "static bool api_$fn->{NAME}(struct pipes_struct *p)"; + pidl "{"; + indent; + pidl "const struct ndr_interface_call *call;"; + pidl "struct ndr_pull *pull;"; + pidl "struct ndr_push *push;"; + pidl "enum ndr_err_code ndr_err;"; + pidl "struct $fn->{NAME} *r;"; + pidl ""; + pidl "call = &ndr_table_$if->{NAME}.calls[$op];"; + pidl ""; + pidl "r = talloc(talloc_tos(), struct $fn->{NAME});"; + pidl "if (r == NULL) {"; + pidl "\treturn false;"; + pidl "}"; + pidl ""; + pidl "pull = ndr_pull_init_blob(&p->in_data.data, r);"; + pidl "if (pull == NULL) {"; + pidl "\ttalloc_free(r);"; + pidl "\treturn false;"; + pidl "}"; + pidl ""; + pidl "pull->flags |= LIBNDR_FLAG_REF_ALLOC;"; + pidl "if (p->endian) {"; + pidl "\tpull->flags |= LIBNDR_FLAG_BIGENDIAN;"; + pidl "}"; + pidl "ndr_err = call->ndr_pull(pull, NDR_IN, r);"; + pidl "if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {"; + pidl "\ttalloc_free(r);"; + pidl "\treturn false;"; + pidl "}"; + pidl ""; + pidl "if (DEBUGLEVEL >= 10) {"; + pidl "\tNDR_PRINT_FUNCTION_DEBUG($fn->{NAME}, NDR_IN, r);"; + pidl "}"; + pidl ""; + + CallWithStruct("p", "r", $fn, + sub ($) { + my ($name) = @_; + return "${name} == NULL"; + }, + sub ($) { + my ($name) = @_; + return "talloc_free(r);"; + }, + sub ($) { + my ($name) = @_; + return "return false;"; + } + ); + + pidl ""; + pidl "if (p->fault_state) {"; + pidl "\ttalloc_free(r);"; + pidl "\t/* Return true here, srv_pipe_hnd.c will take care */"; + pidl "\treturn true;"; + pidl "}"; + pidl ""; + pidl "if (DEBUGLEVEL >= 10) {"; + pidl "\tNDR_PRINT_FUNCTION_DEBUG($fn->{NAME}, NDR_OUT | NDR_SET_VALUES, r);"; + pidl "}"; + pidl ""; + pidl "push = ndr_push_init_ctx(r);"; + pidl "if (push == NULL) {"; + pidl "\ttalloc_free(r);"; + pidl "\treturn false;"; + pidl "}"; + pidl ""; + pidl "/*"; + pidl " * carry over the pointer count to the reply in case we are"; + pidl " * using full pointer. See NDR specification for full pointers"; + pidl " */"; + pidl "push->ptr_count = pull->ptr_count;"; + pidl ""; + pidl "ndr_err = call->ndr_push(push, NDR_OUT, r);"; + pidl "if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {"; + pidl "\ttalloc_free(r);"; + pidl "\treturn false;"; + pidl "}"; + pidl ""; + pidl "p->out_data.rdata = ndr_push_blob(push);"; + pidl "talloc_steal(p->mem_ctx, p->out_data.rdata.data);"; + pidl ""; + pidl "talloc_free(r);"; + pidl ""; + pidl "return true;"; + deindent; + pidl "}"; + pidl ""; +} + +sub ParseInterface($) +{ + my $if = shift; + + my $uif = uc($if->{NAME}); + + pidl_hdr "#ifndef __SRV_$uif\__"; + pidl_hdr "#define __SRV_$uif\__"; + + foreach (@{$if->{FUNCTIONS}}) { + next if ($_->{PROPERTIES}{noopnum}); + ParseFunction($if, $_); + } + + pidl ""; + pidl "/* Tables */"; + pidl "static const struct api_struct api_$if->{NAME}_cmds[] = "; + pidl "{"; + indent; + + foreach (@{$if->{FUNCTIONS}}) { + next if ($_->{PROPERTIES}{noopnum}); + pidl "{\"" . uc($_->{NAME}) . "\", NDR_" . uc($_->{NAME}) . ", api_$_->{NAME}},"; + } + + deindent; + pidl "};"; + + pidl ""; + + pidl_hdr "const struct api_struct *$if->{NAME}_get_pipe_fns(int *n_fns);"; + pidl "const struct api_struct *$if->{NAME}_get_pipe_fns(int *n_fns)"; + pidl "{"; + indent; + pidl "*n_fns = sizeof(api_$if->{NAME}_cmds) / sizeof(struct api_struct);"; + pidl "return api_$if->{NAME}_cmds;"; + deindent; + pidl "}"; + pidl ""; + + if (not has_property($if, "no_srv_register")) { + pidl_hdr "struct rpc_srv_callbacks;"; + pidl_hdr "NTSTATUS rpc_$if->{NAME}_init(const struct rpc_srv_callbacks *rpc_srv_cb);"; + pidl "NTSTATUS rpc_$if->{NAME}_init(const struct rpc_srv_callbacks *rpc_srv_cb)"; + pidl "{"; + pidl "\treturn rpc_srv_register(SMB_RPC_INTERFACE_VERSION, \"$if->{NAME}\", \"$if->{NAME}\", \&ndr_table_$if->{NAME}, api_$if->{NAME}_cmds, sizeof(api_$if->{NAME}_cmds) / sizeof(struct api_struct), rpc_srv_cb);"; + pidl "}"; + + pidl ""; + + pidl_hdr "NTSTATUS rpc_$if->{NAME}_shutdown(void);"; + pidl "NTSTATUS rpc_$if->{NAME}_shutdown(void)"; + pidl "{"; + pidl "\treturn rpc_srv_unregister(\&ndr_table_$if->{NAME});"; + pidl "}"; + } + pidl_hdr "#endif /* __SRV_$uif\__ */"; +} + +sub Parse($$$) +{ + my($ndr,$header,$ndr_header) = @_; + + pidl_reset(); + + pidl "/*"; + pidl " * Unix SMB/CIFS implementation."; + pidl " * server auto-generated by pidl. DO NOT MODIFY!"; + pidl " */"; + pidl ""; + pidl "#include \"includes.h\""; + pidl "#include \"ntdomain.h\""; + pidl "#include \"$header\""; + pidl_hdr "#include \"$ndr_header\""; + pidl ""; + + foreach (@$ndr) { + ParseInterface($_) if ($_->{TYPE} eq "INTERFACE"); + } + + return pidl_return(); +} + +1; diff --git a/tools/pidl/lib/Parse/Pidl/Samba4.pm b/tools/pidl/lib/Parse/Pidl/Samba4.pm new file mode 100644 index 00000000..b720ab90 --- /dev/null +++ b/tools/pidl/lib/Parse/Pidl/Samba4.pm @@ -0,0 +1,133 @@ +################################################### +# Common Samba4 functions +# Copyright jelmer@samba.org 2006 +# released under the GNU GPL + +package Parse::Pidl::Samba4; + +require Exporter; +@ISA = qw(Exporter); +@EXPORT = qw(is_intree choose_header NumStars ElementStars ArrayBrackets DeclLong ArrayDynamicallyAllocated); + +use Parse::Pidl::Util qw(has_property is_constant); +use Parse::Pidl::NDR qw(GetNextLevel); +use Parse::Pidl::Typelist qw(mapTypeName scalar_is_reference); +use Parse::Pidl qw(fatal error); +use strict; + +use vars qw($VERSION); +$VERSION = '0.01'; + + +# return true if we are using pidl within the samba source tree. This changes +# the names of include files, as some include files (such as ntstatus.h) have +# different paths when installed to the patch in the source tree +sub is_intree() +{ + my $srcdir = $ENV{srcdir}; + $srcdir = $srcdir ? "$srcdir/" : ""; + return 1 if (-f "${srcdir}kdc/kdc.c"); + return 1 if (-d "${srcdir}source4"); + return 1 if (-f "${srcdir}include/smb.h"); + return 0; +} + +# Return an #include line depending on whether this build is an in-tree +# build or not. +sub choose_header($$) +{ + my ($in,$out) = @_; + return "#include \"$in\"" if (is_intree()); + return "#include <$out>"; +} + +sub ArrayDynamicallyAllocated($$) +{ + my ($e, $l) = @_; + die("Not an array") unless ($l->{TYPE} eq "ARRAY"); + return 0 if ($l->{IS_FIXED} and not has_property($e, "charset")); + return 1; +} + +sub NumStars($;$) +{ + my ($e, $d) = @_; + $d = 0 unless defined($d); + my $n = 0; + + foreach my $l (@{$e->{LEVELS}}) { + next unless ($l->{TYPE} eq "POINTER"); + + my $nl = GetNextLevel($e, $l); + next if (defined($nl) and $nl->{TYPE} eq "ARRAY"); + + $n++; + } + + if ($n >= 1) { + $n-- if (scalar_is_reference($e->{TYPE})); + } + + foreach my $l (@{$e->{LEVELS}}) { + next unless ($l->{TYPE} eq "ARRAY"); + next unless (ArrayDynamicallyAllocated($e, $l)); + $n++; + } + + error($e->{ORIGINAL}, "Too few pointers $n < $d") if ($n < $d); + + $n -= $d; + + return $n; +} + +sub ElementStars($;$) +{ + my ($e, $d) = @_; + my $res = ""; + my $n = 0; + + $n = NumStars($e, $d); + $res .= "*" foreach (1..$n); + + return $res; +} + +sub ArrayBrackets($) +{ + my ($e) = @_; + my $res = ""; + + foreach my $l (@{$e->{LEVELS}}) { + next unless ($l->{TYPE} eq "ARRAY"); + next if ArrayDynamicallyAllocated($e, $l); + $res .= "[$l->{SIZE_IS}]"; + } + + return $res; +} + +sub DeclLong($;$) +{ + my ($e, $p) = @_; + my $res = ""; + $p = "" unless defined($p); + + if (has_property($e, "represent_as")) { + $res .= mapTypeName($e->{PROPERTIES}->{represent_as})." "; + } else { + if (has_property($e, "charset")) { + $res .= "const char "; + } else { + $res .= mapTypeName($e->{TYPE})." "; + } + + $res .= ElementStars($e); + } + $res .= $p.$e->{NAME}; + $res .= ArrayBrackets($e); + + return $res; +} + +1; diff --git a/tools/pidl/lib/Parse/Pidl/Samba4/COM/Header.pm b/tools/pidl/lib/Parse/Pidl/Samba4/COM/Header.pm new file mode 100644 index 00000000..de7d4547 --- /dev/null +++ b/tools/pidl/lib/Parse/Pidl/Samba4/COM/Header.pm @@ -0,0 +1,160 @@ +# COM Header generation +# (C) 2005 Jelmer Vernooij <jelmer@samba.org> + +package Parse::Pidl::Samba4::COM::Header; + +use Parse::Pidl::Typelist qw(mapTypeName); +use Parse::Pidl::Util qw(has_property is_constant); + +use vars qw($VERSION); +$VERSION = '0.01'; + +use strict; + +sub GetArgumentProtoList($) +{ + my $f = shift; + my $res = ""; + + foreach my $a (@{$f->{ELEMENTS}}) { + + $res .= ", " . mapTypeName($a->{TYPE}) . " "; + + my $l = $a->{POINTERS}; + $l-- if (Parse::Pidl::Typelist::scalar_is_reference($a->{TYPE})); + foreach my $i (1..$l) { + $res .= "*"; + } + + if (defined $a->{ARRAY_LEN}[0] && !is_constant($a->{ARRAY_LEN}[0]) && + !$a->{POINTERS}) { + $res .= "*"; + } + $res .= $a->{NAME}; + if (defined $a->{ARRAY_LEN}[0] && is_constant($a->{ARRAY_LEN}[0])) { + $res .= "[$a->{ARRAY_LEN}[0]]"; + } + } + + return $res; +} + +sub GetArgumentList($) +{ + my $f = shift; + my $res = ""; + + foreach (@{$f->{ELEMENTS}}) { $res .= ", $_->{NAME}"; } + + return $res; +} + +##################################################################### +# generate vtable structure for COM interface +sub HeaderVTable($) +{ + my $interface = shift; + my $res; + $res .= "#define " . uc($interface->{NAME}) . "_METHODS \\\n"; + if (defined($interface->{BASE})) { + $res .= "\t" . uc($interface->{BASE} . "_METHODS") . "\\\n"; + } + + my $data = $interface->{DATA}; + foreach my $d (@{$data}) { + $res .= "\t" . mapTypeName($d->{RETURN_TYPE}) . " (*$d->{NAME}) (struct $interface->{NAME} *d, TALLOC_CTX *mem_ctx" . GetArgumentProtoList($d) . ");\\\n" if ($d->{TYPE} eq "FUNCTION"); + } + $res .= "\n"; + $res .= "struct $interface->{NAME}_vtable {\n"; + $res .= "\tstruct GUID iid;\n"; + $res .= "\t" . uc($interface->{NAME}) . "_METHODS\n"; + $res .= "};\n\n"; + + return $res; +} + +sub ParseInterface($) +{ + my $if = shift; + my $res; + + $res .= "\n#ifndef _$if->{NAME}_\n"; + $res .= "#define _$if->{NAME}_\n"; + + $res .="\n\n/* $if->{NAME} */\n"; + + $res .="#define COM_" . uc($if->{NAME}) . "_UUID $if->{PROPERTIES}->{uuid}\n\n"; + + $res .="struct $if->{NAME}_vtable;\n\n"; + + $res .="struct $if->{NAME} { + struct OBJREF obj; + struct com_context *ctx; + struct $if->{NAME}_vtable *vtable; + void *object_data; +};\n\n"; + + $res.=HeaderVTable($if); + + foreach my $d (@{$if->{DATA}}) { + next if ($d->{TYPE} ne "FUNCTION"); + + $res .= "#define $if->{NAME}_$d->{NAME}(interface, mem_ctx" . GetArgumentList($d) . ") "; + + $res .= "((interface)->vtable->$d->{NAME}(interface, mem_ctx" . GetArgumentList($d) . "))"; + + $res .="\n"; + } + + $res .= "#endif\n"; + + return $res; +} + +sub ParseCoClass($) +{ + my ($c) = @_; + my $res = ""; + $res .= "#define CLSID_" . uc($c->{NAME}) . " $c->{PROPERTIES}->{uuid}\n"; + if (has_property($c, "progid")) { + $res .= "#define PROGID_" . uc($c->{NAME}) . " $c->{PROPERTIES}->{progid}\n"; + } + $res .= "\n"; + return $res; +} + +sub Parse($$) +{ + my ($idl,$ndr_header) = @_; + my $res = ""; + my $has_obj = 0; + + $res .= "#include \"librpc/gen_ndr/orpc.h\"\n" . + "#include \"$ndr_header\"\n\n"; + + foreach (@{$idl}) + { + if ($_->{TYPE} eq "INTERFACE" && has_property($_, "object")) { + $res .="struct $_->{NAME};\n"; + $has_obj = 1; + } + } + + foreach (@{$idl}) + { + if ($_->{TYPE} eq "INTERFACE" && has_property($_, "object")) { + $res.=ParseInterface($_); + $has_obj = 1; + } + + if ($_->{TYPE} eq "COCLASS") { + $res.=ParseCoClass($_); + $has_obj = 1; + } + } + + return $res if ($has_obj); + return undef; +} + +1; diff --git a/tools/pidl/lib/Parse/Pidl/Samba4/COM/Proxy.pm b/tools/pidl/lib/Parse/Pidl/Samba4/COM/Proxy.pm new file mode 100644 index 00000000..35e6e3f0 --- /dev/null +++ b/tools/pidl/lib/Parse/Pidl/Samba4/COM/Proxy.pm @@ -0,0 +1,225 @@ +################################################### +# DCOM parser for Samba +# Basically the glue between COM and DCE/RPC with NDR +# Copyright jelmer@samba.org 2003-2005 +# released under the GNU GPL + +package Parse::Pidl::Samba4::COM::Proxy; + +use Parse::Pidl::Samba4::COM::Header; +use Parse::Pidl::Typelist qw(mapTypeName); +use Parse::Pidl::Util qw(has_property); + +use vars qw($VERSION); +$VERSION = '0.01'; + +use strict; + +my($res); + +sub ParseVTable($$) +{ + my ($interface, $name) = @_; + + # Generate the vtable + $res .="\tstruct $interface->{NAME}_vtable $name = {"; + + if (defined($interface->{BASE})) { + $res .= "\n\t\t{},"; + } + + my $data = $interface->{DATA}; + + foreach my $d (@{$data}) { + if ($d->{TYPE} eq "FUNCTION") { + $res .= "\n\t\tdcom_proxy_$interface->{NAME}_$d->{NAME}"; + $res .= ","; + } + } + + $res .= "\n\t};\n\n"; +} + +sub ParseRegFunc($) +{ + my $interface = shift; + + $res .= "static NTSTATUS dcom_proxy_$interface->{NAME}_init(TALLOC_CTX *ctx) +{ + struct $interface->{NAME}_vtable *proxy_vtable = talloc(ctx, struct $interface->{NAME}_vtable); +"; + + if (defined($interface->{BASE})) { + $res.= " + struct GUID base_iid; + const void *base_vtable; + + base_iid = ndr_table_$interface->{BASE}.syntax_id.uuid; + + base_vtable = dcom_proxy_vtable_by_iid(&base_iid); + if (base_vtable == NULL) { + DEBUG(0, (\"No proxy registered for base interface '$interface->{BASE}'\\n\")); + return NT_STATUS_FOOBAR; + } + + memcpy(&proxy_vtable, base_vtable, sizeof(struct $interface->{BASE}_vtable)); + +"; + } + foreach my $x (@{$interface->{DATA}}) { + next unless ($x->{TYPE} eq "FUNCTION"); + + $res .= "\tproxy_vtable->$x->{NAME} = dcom_proxy_$interface->{NAME}_$x->{NAME};\n"; + } + + $res.= " + proxy_vtable->iid = ndr_table_$interface->{NAME}.syntax_id.uuid; + + return dcom_register_proxy(ctx, (struct IUnknown_vtable *)proxy_vtable); +}\n\n"; +} + +##################################################################### +# parse a function +sub ParseFunction($$) +{ + my ($interface, $fn) = @_; + my $name = $fn->{NAME}; + my $uname = uc $name; + + my $tn = mapTypeName($fn->{RETURN_TYPE}); + + $res.=" +static $tn dcom_proxy_$interface->{NAME}_$name(struct $interface->{NAME} *d, TALLOC_CTX *mem_ctx" . Parse::Pidl::Samba4::COM::Header::GetArgumentProtoList($fn) . ") +{ + struct dcerpc_pipe *p; + NTSTATUS status = dcom_get_pipe(d, &p); + struct $name r; + struct rpc_request *req; + + if (NT_STATUS_IS_ERR(status)) { + return status; + } + + ZERO_STRUCT(r.in.ORPCthis); + r.in.ORPCthis.version.MajorVersion = COM_MAJOR_VERSION; + r.in.ORPCthis.version.MinorVersion = COM_MINOR_VERSION; +"; + + # Put arguments into r + foreach my $a (@{$fn->{ELEMENTS}}) { + next unless (has_property($a, "in")); + if (Parse::Pidl::Typelist::typeIs($a->{TYPE}, "INTERFACE")) { + $res .="\tNDR_CHECK(dcom_OBJREF_from_IUnknown(mem_ctx, &r.in.$a->{NAME}.obj, $a->{NAME}));\n"; + } else { + $res .= "\tr.in.$a->{NAME} = $a->{NAME};\n"; + } + } + + $res .=" + if (p->conn->flags & DCERPC_DEBUG_PRINT_IN) { + NDR_PRINT_IN_DEBUG($name, &r); + } + + status = dcerpc_ndr_request(p, &d->ipid, &ndr_table_$interface->{NAME}, NDR_$uname, mem_ctx, &r); + + if (NT_STATUS_IS_OK(status) && (p->conn->flags & DCERPC_DEBUG_PRINT_OUT)) { + NDR_PRINT_OUT_DEBUG($name, r); + } + +"; + + # Put r info back into arguments + foreach my $a (@{$fn->{ELEMENTS}}) { + next unless (has_property($a, "out")); + + if (Parse::Pidl::Typelist::typeIs($a->{TYPE}, "INTERFACE")) { + $res .="\tNDR_CHECK(dcom_IUnknown_from_OBJREF(d->ctx, &$a->{NAME}, r.out.$a->{NAME}.obj));\n"; + } else { + $res .= "\t*$a->{NAME} = r.out.$a->{NAME};\n"; + } + + } + + if ($fn->{RETURN_TYPE} eq "NTSTATUS") { + $res .= "\tif (NT_STATUS_IS_OK(status)) status = r.out.result;\n"; + } + + $res .= + " + return r.out.result; +}\n\n"; +} + +##################################################################### +# parse the interface definitions +sub ParseInterface($) +{ + my($interface) = shift; + my($data) = $interface->{DATA}; + $res = "/* DCOM proxy for $interface->{NAME} generated by pidl */\n\n"; + foreach my $d (@{$data}) { + ($d->{TYPE} eq "FUNCTION") && + ParseFunction($interface, $d); + } + + ParseRegFunc($interface); +} + +sub RegistrationFunction($$) +{ + my $idl = shift; + my $basename = shift; + + my $res = "\n\nNTSTATUS dcom_$basename\_init(void)\n"; + $res .= "{\n"; + $res .="\tNTSTATUS status = NT_STATUS_OK;\n"; + foreach my $interface (@{$idl}) { + next if $interface->{TYPE} ne "INTERFACE"; + next if not has_property($interface, "object"); + + my $data = $interface->{DATA}; + my $count = 0; + foreach my $d (@{$data}) { + if ($d->{TYPE} eq "FUNCTION") { $count++; } + } + + next if ($count == 0); + + $res .= "\tstatus = dcom_$interface->{NAME}_init();\n"; + $res .= "\tif (NT_STATUS_IS_ERR(status)) {\n"; + $res .= "\t\treturn status;\n"; + $res .= "\t}\n\n"; + } + $res .= "\treturn status;\n"; + $res .= "}\n\n"; + + return $res; +} + +sub Parse($$) +{ + my ($pidl,$comh_filename) = @_; + my $res = ""; + my $has_obj = 0; + + $res .= "#include \"includes.h\"\n" . + "#include \"lib/com/dcom/dcom.h\"\n" . + "#include \"$comh_filename\"\n" . + "#include \"librpc/rpc/dcerpc.h\"\n"; + + foreach (@{$pidl}) { + next if ($_->{TYPE} ne "INTERFACE"); + next if has_property($_, "local"); + next unless has_property($_, "object"); + + $res .= ParseInterface($_); + + $has_obj = 1; + } + + return $res if ($has_obj); + return undef; +} + +1; diff --git a/tools/pidl/lib/Parse/Pidl/Samba4/COM/Stub.pm b/tools/pidl/lib/Parse/Pidl/Samba4/COM/Stub.pm new file mode 100644 index 00000000..239f5baa --- /dev/null +++ b/tools/pidl/lib/Parse/Pidl/Samba4/COM/Stub.pm @@ -0,0 +1,327 @@ +################################################### +# DCOM stub boilerplate generator +# Copyright jelmer@samba.org 2004-2005 +# Copyright tridge@samba.org 2003 +# Copyright metze@samba.org 2004 +# released under the GNU GPL + +package Parse::Pidl::Samba4::COM::Stub; + +use Parse::Pidl::Util qw(has_property); +use strict; + +use vars qw($VERSION); +$VERSION = '0.01'; + +my($res); + +sub pidl($) +{ + $res .= shift; +} + +##################################################### +# generate the switch statement for function dispatch +sub gen_dispatch_switch($) +{ + my $data = shift; + + my $count = 0; + foreach my $d (@{$data}) { + next if ($d->{TYPE} ne "FUNCTION"); + + pidl "\tcase $count: {\n"; + if ($d->{RETURN_TYPE} && $d->{RETURN_TYPE} ne "void") { + pidl "\t\tNTSTATUS result;\n"; + } + pidl "\t\tstruct $d->{NAME} *r2 = r;\n"; + pidl "\t\tif (DEBUGLEVEL > 10) {\n"; + pidl "\t\t\tNDR_PRINT_FUNCTION_DEBUG($d->{NAME}, NDR_IN, r2);\n"; + pidl "\t\t}\n"; + if ($d->{RETURN_TYPE} && $d->{RETURN_TYPE} ne "void") { + pidl "\t\tresult = vtable->$d->{NAME}(iface, mem_ctx, r2);\n"; + } else { + pidl "\t\tvtable->$d->{NAME}(iface, mem_ctx, r2);\n"; + } + pidl "\t\tif (dce_call->state_flags & DCESRV_CALL_STATE_FLAG_ASYNC) {\n"; + pidl "\t\t\tDEBUG(5,(\"function $d->{NAME} will reply async\\n\"));\n"; + pidl "\t\t}\n"; + pidl "\t\tbreak;\n\t}\n"; + $count++; + } +} + +##################################################### +# generate the switch statement for function reply +sub gen_reply_switch($) +{ + my $data = shift; + + my $count = 0; + foreach my $d (@{$data}) { + next if ($d->{TYPE} ne "FUNCTION"); + + pidl "\tcase $count: {\n"; + pidl "\t\tstruct $d->{NAME} *r2 = r;\n"; + pidl "\t\tif (dce_call->state_flags & DCESRV_CALL_STATE_FLAG_ASYNC) {\n"; + pidl "\t\t\tDEBUG(5,(\"function $d->{NAME} replied async\\n\"));\n"; + pidl "\t\t}\n"; + pidl "\t\tif (DEBUGLEVEL > 10 && dce_call->fault_code == 0) {\n"; + pidl "\t\t\tNDR_PRINT_FUNCTION_DEBUG($d->{NAME}, NDR_OUT | NDR_SET_VALUES, r2);\n"; + pidl "\t\t}\n"; + pidl "\t\tif (dce_call->fault_code != 0) {\n"; + pidl "\t\t\tDEBUG(2,(\"dcerpc_fault %s in $d->{NAME}\\n\", dcerpc_errstr(mem_ctx, dce_call->fault_code)));\n"; + pidl "\t\t}\n"; + pidl "\t\tbreak;\n\t}\n"; + $count++; + } +} + +##################################################################### +# produce boilerplate code for a interface +sub Boilerplate_Iface($) +{ + my($interface) = shift; + my($data) = $interface->{DATA}; + my $name = $interface->{NAME}; + my $uname = uc $name; + my $uuid = Parse::Pidl::Util::make_str($interface->{PROPERTIES}->{uuid}); + my $if_version = $interface->{PROPERTIES}->{version}; + + pidl " +static NTSTATUS $name\__op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface, uint32_t if_version) +{ +#ifdef DCESRV_INTERFACE_$uname\_BIND + return DCESRV_INTERFACE_$uname\_BIND(dce_call,iface); +#else + return NT_STATUS_OK; +#endif +} + +static void $name\__op_unbind(struct dcesrv_connection_context *context, const struct dcesrv_interface *iface) +{ +#ifdef DCESRV_INTERFACE_$uname\_UNBIND + DCESRV_INTERFACE_$uname\_UNBIND(context, iface); +#else + return; +#endif +} + +static NTSTATUS $name\__op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r) +{ + NTSTATUS status; + uint16_t opnum = dce_call->pkt.u.request.opnum; + + dce_call->fault_code = 0; + + if (opnum >= dcerpc_table_$name.num_calls) { + dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR; + return NT_STATUS_NET_WRITE_FAULT; + } + + *r = talloc_size(mem_ctx, dcerpc_table_$name.calls[opnum].struct_size); + NT_STATUS_HAVE_NO_MEMORY(*r); + + /* unravel the NDR for the packet */ + status = dcerpc_table_$name.calls[opnum].ndr_pull(pull, NDR_IN, *r); + if (!NT_STATUS_IS_OK(status)) { + dcerpc_log_packet(&dcerpc_table_$name, opnum, NDR_IN, + &dce_call->pkt.u.request.stub_and_verifier); + dce_call->fault_code = DCERPC_FAULT_NDR; + return NT_STATUS_NET_WRITE_FAULT; + } + + return NT_STATUS_OK; +} + +static NTSTATUS $name\__op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r) +{ + uint16_t opnum = dce_call->pkt.u.request.opnum; + struct GUID ipid = dce_call->pkt.u.request.object.object; + struct dcom_interface_p *iface = dcom_get_local_iface_p(&ipid); + const struct dcom_$name\_vtable *vtable = iface->vtable; + + switch (opnum) { +"; + gen_dispatch_switch($data); + +pidl " + default: + dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR; + break; + } + + if (dce_call->fault_code != 0) { + dcerpc_log_packet(&dcerpc_table_$name, opnum, NDR_IN, + &dce_call->pkt.u.request.stub_and_verifier); + return NT_STATUS_NET_WRITE_FAULT; + } + + return NT_STATUS_OK; +} + +static NTSTATUS $name\__op_reply(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r) +{ + uint16_t opnum = dce_call->pkt.u.request.opnum; + + switch (opnum) { +"; + gen_reply_switch($data); + +pidl " + default: + dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR; + break; + } + + if (dce_call->fault_code != 0) { + dcerpc_log_packet(&dcerpc_table_$name, opnum, NDR_IN, + &dce_call->pkt.u.request.stub_and_verifier); + return NT_STATUS_NET_WRITE_FAULT; + } + + return NT_STATUS_OK; +} + +static NTSTATUS $name\__op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r) +{ + NTSTATUS status; + uint16_t opnum = dce_call->pkt.u.request.opnum; + + status = dcerpc_table_$name.calls[opnum].ndr_push(push, NDR_OUT, r); + if (!NT_STATUS_IS_OK(status)) { + dce_call->fault_code = DCERPC_FAULT_NDR; + return NT_STATUS_NET_WRITE_FAULT; + } + + return NT_STATUS_OK; +} + +static const struct dcesrv_interface $name\_interface = { + .name = \"$name\", + .uuid = $uuid, + .if_version = $if_version, + .bind = $name\__op_bind, + .unbind = $name\__op_unbind, + .ndr_pull = $name\__op_ndr_pull, + .dispatch = $name\__op_dispatch, + .reply = $name\__op_reply, + .ndr_push = $name\__op_ndr_push +}; + +"; +} + +##################################################################### +# produce boilerplate code for an endpoint server +sub Boilerplate_Ep_Server($) +{ + my($interface) = shift; + my $name = $interface->{NAME}; + my $uname = uc $name; + + pidl " +static NTSTATUS $name\__op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server) +{ + int i; + + for (i=0;i<dcerpc_table_$name.endpoints->count;i++) { + NTSTATUS ret; + const char *name = dcerpc_table_$name.endpoints->names[i]; + + ret = dcesrv_interface_register(dce_ctx, name, &$name\_interface, NULL); + if (!NT_STATUS_IS_OK(ret)) { + DEBUG(1,(\"$name\_op_init_server: failed to register endpoint \'%s\'\\n\",name)); + return ret; + } + } + + return NT_STATUS_OK; +} + +static BOOL $name\__op_interface_by_uuid(struct dcesrv_interface *iface, const char *uuid, uint32_t if_version) +{ + if (dcerpc_table_$name.if_version == if_version && + strcmp(dcerpc_table_$name.uuid, uuid)==0) { + memcpy(iface,&dcerpc_table_$name, sizeof(*iface)); + return True; + } + + return False; +} + +static BOOL $name\__op_interface_by_name(struct dcesrv_interface *iface, const char *name) +{ + if (strcmp(dcerpc_table_$name.name, name)==0) { + memcpy(iface,&dcerpc_table_$name, sizeof(*iface)); + return True; + } + + return False; +} + +NTSTATUS dcerpc_server_$name\_init(void) +{ + NTSTATUS ret; + struct dcesrv_endpoint_server ep_server; + + /* fill in our name */ + ep_server.name = \"$name\"; + + /* fill in all the operations */ + ep_server.init_server = $name\__op_init_server; + + ep_server.interface_by_uuid = $name\__op_interface_by_uuid; + ep_server.interface_by_name = $name\__op_interface_by_name; + + /* register ourselves with the DCERPC subsystem. */ + ret = dcerpc_register_ep_server(&ep_server); + + if (!NT_STATUS_IS_OK(ret)) { + DEBUG(0,(\"Failed to register \'$name\' endpoint server!\\n\")); + return ret; + } + + return ret; +} + +"; +} + +##################################################################### +# dcom interface stub from a parsed IDL structure +sub ParseInterface($) +{ + my($interface) = shift; + + return "" if has_property($interface, "local"); + + my($data) = $interface->{DATA}; + my $count = 0; + + $res = ""; + + if (!defined $interface->{PROPERTIES}->{uuid}) { + return $res; + } + + if (!defined $interface->{PROPERTIES}->{version}) { + $interface->{PROPERTIES}->{version} = "0.0"; + } + + foreach my $d (@{$data}) { + if ($d->{TYPE} eq "FUNCTION") { $count++; } + } + + if ($count == 0) { + return $res; + } + + $res = "/* dcom interface stub generated by pidl */\n\n"; + Boilerplate_Iface($interface); + Boilerplate_Ep_Server($interface); + + return $res; +} + +1; diff --git a/tools/pidl/lib/Parse/Pidl/Samba4/Header.pm b/tools/pidl/lib/Parse/Pidl/Samba4/Header.pm new file mode 100644 index 00000000..e9b7bee0 --- /dev/null +++ b/tools/pidl/lib/Parse/Pidl/Samba4/Header.pm @@ -0,0 +1,537 @@ +################################################### +# create C header files for an IDL structure +# Copyright tridge@samba.org 2000 +# Copyright jelmer@samba.org 2005 +# released under the GNU GPL + +package Parse::Pidl::Samba4::Header; +require Exporter; + +@ISA = qw(Exporter); +@EXPORT_OK = qw(GenerateFunctionInEnv GenerateFunctionOutEnv EnvSubstituteValue GenerateStructEnv); + +use strict; +use Parse::Pidl qw(fatal); +use Parse::Pidl::Typelist qw(mapTypeName scalar_is_reference); +use Parse::Pidl::Util qw(has_property is_constant unmake_str ParseExpr); +use Parse::Pidl::Samba4 qw(is_intree ElementStars ArrayBrackets choose_header); + +use vars qw($VERSION); +$VERSION = '0.01'; + +my($res); +my($tab_depth); + +sub pidl($) { $res .= shift; } + +sub tabs() +{ + my $res = ""; + $res .="\t" foreach (1..$tab_depth); + return $res; +} + +##################################################################### +# parse a properties list +sub HeaderProperties($$) +{ + my($props,$ignores) = @_; + my $ret = ""; + + foreach my $d (sort(keys %{$props})) { + next if (grep(/^$d$/, @$ignores)); + if($props->{$d} ne "1") { + $ret.= "$d($props->{$d}),"; + } else { + $ret.="$d,"; + } + } + + if ($ret) { + pidl "/* [" . substr($ret, 0, -1) . "] */"; + } +} + +##################################################################### +# parse a structure element +sub HeaderElement($) +{ + my($element) = shift; + + pidl tabs(); + if (has_property($element, "represent_as")) { + pidl mapTypeName($element->{PROPERTIES}->{represent_as})." "; + } else { + if (ref($element->{TYPE}) eq "HASH") { + HeaderType($element, $element->{TYPE}, $element->{TYPE}->{NAME}); + } else { + HeaderType($element, $element->{TYPE}, ""); + } + pidl " ".ElementStars($element); + } + pidl $element->{NAME}; + pidl ArrayBrackets($element); + + pidl ";"; + if (defined $element->{PROPERTIES}) { + HeaderProperties($element->{PROPERTIES}, ["in", "out"]); + } + pidl "\n"; +} + +##################################################################### +# parse a struct +sub HeaderStruct($$;$) +{ + my($struct,$name,$tail) = @_; + pidl "struct $name"; + pidl $tail if defined($tail) and not defined($struct->{ELEMENTS}); + return if (not defined($struct->{ELEMENTS})); + pidl " {\n"; + $tab_depth++; + my $el_count=0; + foreach (@{$struct->{ELEMENTS}}) { + HeaderElement($_); + $el_count++; + } + if ($el_count == 0) { + # some compilers can't handle empty structures + pidl tabs()."char _empty_;\n"; + } + $tab_depth--; + pidl tabs()."}"; + if (defined $struct->{PROPERTIES}) { + HeaderProperties($struct->{PROPERTIES}, []); + } + pidl $tail if defined($tail); +} + +##################################################################### +# parse a enum +sub HeaderEnum($$;$) +{ + my($enum,$name,$tail) = @_; + my $first = 1; + + pidl "enum $name"; + if (defined($enum->{ELEMENTS})) { + pidl "\n#ifndef USE_UINT_ENUMS\n"; + pidl " {\n"; + $tab_depth++; + foreach my $e (@{$enum->{ELEMENTS}}) { + my @enum_els = (); + unless ($first) { pidl ",\n"; } + $first = 0; + pidl tabs(); + @enum_els = split(/=/, $e); + if (@enum_els == 2) { + pidl $enum_els[0]; + pidl "=(int)"; + pidl "("; + pidl $enum_els[1]; + pidl ")"; + } else { + pidl $e; + } + } + pidl "\n"; + $tab_depth--; + pidl "}"; + pidl "\n"; + pidl "#else\n"; + my $count = 0; + my $with_val = 0; + my $without_val = 0; + pidl " { __do_not_use_enum_$name=0x7FFFFFFF}\n"; + foreach my $e (@{$enum->{ELEMENTS}}) { + my $t = "$e"; + my $name; + my $value; + if ($t =~ /(.*)=(.*)/) { + $name = $1; + $value = $2; + $with_val = 1; + fatal($e->{ORIGINAL}, "you can't mix enum member with values and without values!") + unless ($without_val == 0); + } else { + $name = $t; + $value = $count++; + $without_val = 1; + fatal($e->{ORIGINAL}, "you can't mix enum member with values and without values!") + unless ($with_val == 0); + } + pidl "#define $name ( $value )\n"; + } + pidl "#endif\n"; + } + pidl $tail if defined($tail); +} + +##################################################################### +# parse a bitmap +sub HeaderBitmap($$) +{ + my($bitmap,$name) = @_; + + return unless defined($bitmap->{ELEMENTS}); + + pidl "/* bitmap $name */\n"; + pidl "#define $_\n" foreach (@{$bitmap->{ELEMENTS}}); + pidl "\n"; +} + +##################################################################### +# parse a union +sub HeaderUnion($$;$) +{ + my($union,$name,$tail) = @_; + my %done = (); + + pidl "union $name"; + pidl $tail if defined($tail) and not defined($union->{ELEMENTS}); + return if (not defined($union->{ELEMENTS})); + pidl " {\n"; + $tab_depth++; + my $needed = 0; + foreach my $e (@{$union->{ELEMENTS}}) { + if ($e->{TYPE} ne "EMPTY") { + if (! defined $done{$e->{NAME}}) { + HeaderElement($e); + } + $done{$e->{NAME}} = 1; + $needed++; + } + } + if (!$needed) { + # sigh - some compilers don't like empty structures + pidl tabs()."int _dummy_element;\n"; + } + $tab_depth--; + pidl "}"; + + if (defined $union->{PROPERTIES}) { + HeaderProperties($union->{PROPERTIES}, []); + } + pidl $tail if defined($tail); +} + +##################################################################### +# parse a pipe +sub HeaderPipe($$;$) +{ + my($pipe,$name,$tail) = @_; + + my $struct = $pipe->{DATA}; + my $e = $struct->{ELEMENTS}[1]; + + pidl "struct $name;\n"; + pidl "struct $struct->{NAME} {\n"; + $tab_depth++; + pidl tabs()."uint32_t count;\n"; + pidl tabs().mapTypeName($e->{TYPE})." *array;\n"; + $tab_depth--; + pidl "}"; + + if (defined $struct->{PROPERTIES}) { + HeaderProperties($struct->{PROPERTIES}, []); + } + + pidl $tail if defined($tail); +} + +##################################################################### +# parse a type +sub HeaderType($$$;$) +{ + my($e,$data,$name,$tail) = @_; + if (ref($data) eq "HASH") { + ($data->{TYPE} eq "ENUM") && HeaderEnum($data, $name, $tail); + ($data->{TYPE} eq "BITMAP") && HeaderBitmap($data, $name); + ($data->{TYPE} eq "STRUCT") && HeaderStruct($data, $name, $tail); + ($data->{TYPE} eq "UNION") && HeaderUnion($data, $name, $tail); + ($data->{TYPE} eq "PIPE") && HeaderPipe($data, $name, $tail); + return; + } + + if (has_property($e, "charset")) { + pidl "const char"; + } else { + pidl mapTypeName($e->{TYPE}); + } + pidl $tail if defined($tail); +} + +##################################################################### +# parse a typedef +sub HeaderTypedef($;$) +{ + my($typedef,$tail) = @_; + # Don't print empty "enum foo;", since some compilers don't like it. + return if ($typedef->{DATA}->{TYPE} eq "ENUM" and not defined($typedef->{DATA}->{ELEMENTS})); + HeaderType($typedef, $typedef->{DATA}, $typedef->{NAME}, $tail) if defined ($typedef->{DATA}); +} + +##################################################################### +# parse a const +sub HeaderConst($) +{ + my($const) = shift; + if (!defined($const->{ARRAY_LEN}[0])) { + pidl "#define $const->{NAME}\t( $const->{VALUE} )\n"; + } else { + pidl "#define $const->{NAME}\t $const->{VALUE}\n"; + } +} + +sub ElementDirection($) +{ + my ($e) = @_; + + return "inout" if (has_property($e, "in") and has_property($e, "out")); + return "in" if (has_property($e, "in")); + return "out" if (has_property($e, "out")); + return "inout"; +} + +##################################################################### +# parse a function +sub HeaderFunctionInOut($$) +{ + my($fn,$prop) = @_; + + return unless defined($fn->{ELEMENTS}); + + foreach my $e (@{$fn->{ELEMENTS}}) { + HeaderElement($e) if (ElementDirection($e) eq $prop); + } +} + +##################################################################### +# determine if we need an "in" or "out" section +sub HeaderFunctionInOut_needed($$) +{ + my($fn,$prop) = @_; + + return 1 if ($prop eq "out" && defined($fn->{RETURN_TYPE})); + + return undef unless defined($fn->{ELEMENTS}); + + foreach my $e (@{$fn->{ELEMENTS}}) { + return 1 if (ElementDirection($e) eq $prop); + } + + return undef; +} + +my %headerstructs; + +##################################################################### +# parse a function +sub HeaderFunction($) +{ + my($fn) = shift; + + return if ($headerstructs{$fn->{NAME}}); + + $headerstructs{$fn->{NAME}} = 1; + + pidl "\nstruct $fn->{NAME} {\n"; + $tab_depth++; + my $needed = 0; + + if (HeaderFunctionInOut_needed($fn, "in") or + HeaderFunctionInOut_needed($fn, "inout")) { + pidl tabs()."struct {\n"; + $tab_depth++; + HeaderFunctionInOut($fn, "in"); + HeaderFunctionInOut($fn, "inout"); + $tab_depth--; + pidl tabs()."} in;\n\n"; + $needed++; + } + + if (HeaderFunctionInOut_needed($fn, "out") or + HeaderFunctionInOut_needed($fn, "inout")) { + pidl tabs()."struct {\n"; + $tab_depth++; + HeaderFunctionInOut($fn, "out"); + HeaderFunctionInOut($fn, "inout"); + if (defined($fn->{RETURN_TYPE})) { + pidl tabs().mapTypeName($fn->{RETURN_TYPE}) . " result;\n"; + } + $tab_depth--; + pidl tabs()."} out;\n\n"; + $needed++; + } + + if (!$needed) { + # sigh - some compilers don't like empty structures + pidl tabs()."int _dummy_element;\n"; + } + + $tab_depth--; + pidl "};\n\n"; +} + +sub HeaderImport +{ + my @imports = @_; + foreach my $import (@imports) { + $import = unmake_str($import); + $import =~ s/\.idl$//; + pidl choose_header("librpc/gen_ndr/$import\.h", "gen_ndr/$import.h") . "\n"; + } +} + +sub HeaderInclude +{ + my @includes = @_; + foreach (@includes) { + pidl "#include $_\n"; + } +} + +##################################################################### +# parse the interface definitions +sub HeaderInterface($) +{ + my($interface) = shift; + + pidl "#ifndef _HEADER_$interface->{NAME}\n"; + pidl "#define _HEADER_$interface->{NAME}\n\n"; + + foreach my $c (@{$interface->{CONSTS}}) { + HeaderConst($c); + } + + foreach my $t (@{$interface->{TYPES}}) { + HeaderTypedef($t, ";\n\n") if ($t->{TYPE} eq "TYPEDEF"); + HeaderStruct($t, $t->{NAME}, ";\n\n") if ($t->{TYPE} eq "STRUCT"); + HeaderUnion($t, $t->{NAME}, ";\n\n") if ($t->{TYPE} eq "UNION"); + HeaderEnum($t, $t->{NAME}, ";\n\n") if ($t->{TYPE} eq "ENUM"); + HeaderBitmap($t, $t->{NAME}) if ($t->{TYPE} eq "BITMAP"); + HeaderPipe($t, $t->{NAME}, "\n\n") if ($t->{TYPE} eq "PIPE"); + } + + foreach my $fn (@{$interface->{FUNCTIONS}}) { + HeaderFunction($fn); + } + + pidl "#endif /* _HEADER_$interface->{NAME} */\n"; +} + +sub HeaderQuote($) +{ + my($quote) = shift; + + pidl unmake_str($quote->{DATA}) . "\n"; +} + +##################################################################### +# parse a parsed IDL into a C header +sub Parse($) +{ + my($ndr) = shift; + $tab_depth = 0; + + $res = ""; + %headerstructs = (); + pidl "/* header auto-generated by pidl */\n\n"; + + my $ifacename = ""; + + # work out a unique interface name + foreach (@{$ndr}) { + if ($_->{TYPE} eq "INTERFACE") { + $ifacename = $_->{NAME}; + last; + } + } + + pidl "#ifndef _PIDL_HEADER_$ifacename\n"; + pidl "#define _PIDL_HEADER_$ifacename\n\n"; + + if (!is_intree()) { + pidl "#include <util/data_blob.h>\n"; + } + pidl "#include <stdint.h>\n"; + pidl "\n"; + # FIXME: Include this only if NTSTATUS was actually used + pidl choose_header("libcli/util/ntstatus.h", "core/ntstatus.h") . "\n"; + pidl "\n"; + + foreach (@{$ndr}) { + ($_->{TYPE} eq "CPP_QUOTE") && HeaderQuote($_); + ($_->{TYPE} eq "INTERFACE") && HeaderInterface($_); + ($_->{TYPE} eq "IMPORT") && HeaderImport(@{$_->{PATHS}}); + ($_->{TYPE} eq "INCLUDE") && HeaderInclude(@{$_->{PATHS}}); + } + + pidl "#endif /* _PIDL_HEADER_$ifacename */\n"; + + return $res; +} + +sub GenerateStructEnv($$) +{ + my ($x, $v) = @_; + my %env; + + foreach my $e (@{$x->{ELEMENTS}}) { + $env{$e->{NAME}} = "$v->$e->{NAME}"; + } + + $env{"this"} = $v; + + return \%env; +} + +sub EnvSubstituteValue($$) +{ + my ($env,$s) = @_; + + # Substitute the value() values in the env + foreach my $e (@{$s->{ELEMENTS}}) { + next unless (defined(my $v = has_property($e, "value"))); + + $env->{$e->{NAME}} = ParseExpr($v, $env, $e); + } + + return $env; +} + +sub GenerateFunctionInEnv($;$) +{ + my ($fn, $base) = @_; + my %env; + + $base = "r->" unless defined($base); + + foreach my $e (@{$fn->{ELEMENTS}}) { + if (grep (/in/, @{$e->{DIRECTION}})) { + $env{$e->{NAME}} = $base."in.$e->{NAME}"; + } + } + + return \%env; +} + +sub GenerateFunctionOutEnv($;$) +{ + my ($fn, $base) = @_; + my %env; + + $base = "r->" unless defined($base); + + foreach my $e (@{$fn->{ELEMENTS}}) { + if (grep (/out/, @{$e->{DIRECTION}})) { + $env{$e->{NAME}} = $base."out.$e->{NAME}"; + } elsif (grep (/in/, @{$e->{DIRECTION}})) { + $env{$e->{NAME}} = $base."in.$e->{NAME}"; + } + } + + return \%env; +} + +1; diff --git a/tools/pidl/lib/Parse/Pidl/Samba4/NDR/Client.pm b/tools/pidl/lib/Parse/Pidl/Samba4/NDR/Client.pm new file mode 100644 index 00000000..040cd5a4 --- /dev/null +++ b/tools/pidl/lib/Parse/Pidl/Samba4/NDR/Client.pm @@ -0,0 +1,884 @@ +################################################### +# client calls generator +# Copyright tridge@samba.org 2003 +# Copyright jelmer@samba.org 2005-2006 +# released under the GNU GPL + +package Parse::Pidl::Samba4::NDR::Client; + +use Exporter; +@ISA = qw(Exporter); +@EXPORT_OK = qw(Parse); + +use Parse::Pidl qw(fatal warning error); +use Parse::Pidl::Util qw(has_property ParseExpr genpad); +use Parse::Pidl::NDR qw(ContainsPipe); +use Parse::Pidl::Typelist qw(mapTypeName); +use Parse::Pidl::Samba4 qw(choose_header is_intree DeclLong); +use Parse::Pidl::Samba4::Header qw(GenerateFunctionInEnv GenerateFunctionOutEnv); + +use vars qw($VERSION); +$VERSION = '0.01'; + +use strict; + +sub indent($) { my ($self) = @_; $self->{tabs}.="\t"; } +sub deindent($) { my ($self) = @_; $self->{tabs} = substr($self->{tabs}, 1); } +sub pidl($$) { my ($self,$txt) = @_; $self->{res} .= $txt ? "$self->{tabs}$txt\n" : "\n"; } +sub pidl_hdr($$) { my ($self, $txt) = @_; $self->{res_hdr} .= "$txt\n"; } +sub pidl_both($$) { my ($self, $txt) = @_; $self->{hdr} .= "$txt\n"; $self->{res_hdr} .= "$txt\n"; } +sub fn_declare($$) { my ($self,$n) = @_; $self->pidl($n); $self->pidl_hdr("$n;"); } + +sub new($) +{ + my ($class) = shift; + my $self = { res => "", res_hdr => "", tabs => "" }; + bless($self, $class); +} + +sub ParseFunctionHasPipes($$) +{ + my ($self, $fn) = @_; + + foreach my $e (@{$fn->{ELEMENTS}}) { + return 1 if ContainsPipe($e, $e->{LEVELS}[0]); + } + + return 0; +} + +sub ParseFunction_r_State($$$$) +{ + my ($self, $if, $fn, $name) = @_; + my $uname = uc $name; + + $self->pidl("struct dcerpc_$name\_r_state {"); + $self->indent; + $self->pidl("TALLOC_CTX *out_mem_ctx;"); + $self->deindent; + $self->pidl("};"); + $self->pidl(""); + $self->pidl("static void dcerpc_$name\_r_done(struct tevent_req *subreq);"); + $self->pidl(""); +} + +sub ParseFunction_r_Send($$$$) +{ + my ($self, $if, $fn, $name) = @_; + my $uname = uc $name; + + my $proto = "struct tevent_req *dcerpc_$name\_r_send(TALLOC_CTX *mem_ctx,\n"; + $proto .= "\tstruct tevent_context *ev,\n", + $proto .= "\tstruct dcerpc_binding_handle *h,\n", + $proto .= "\tstruct $name *r)"; + + $self->fn_declare($proto); + + $self->pidl("{"); + $self->indent; + + $self->pidl("struct tevent_req *req;"); + $self->pidl("struct dcerpc_$name\_r_state *state;"); + $self->pidl("struct tevent_req *subreq;"); + $self->pidl(""); + + $self->pidl("req = tevent_req_create(mem_ctx, &state,"); + $self->pidl("\t\t\tstruct dcerpc_$name\_r_state);"); + $self->pidl("if (req == NULL) {"); + $self->indent; + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + my $out_params = 0; + foreach my $e (@{$fn->{ELEMENTS}}) { + next unless grep(/out/, @{$e->{DIRECTION}}); + next if ContainsPipe($e, $e->{LEVELS}[0]); + $out_params++; + + } + + my $submem; + if ($out_params > 0) { + $self->pidl("state->out_mem_ctx = talloc_new(state);"); + $self->pidl("if (tevent_req_nomem(state->out_mem_ctx, req)) {"); + $self->indent; + $self->pidl("return tevent_req_post(req, ev);"); + $self->deindent; + $self->pidl("}"); + $submem = "state->out_mem_ctx"; + } else { + $self->pidl("state->out_mem_ctx = NULL;"); + $submem = "state"; + } + $self->pidl(""); + + $self->pidl("subreq = dcerpc_binding_handle_call_send(state, ev, h,"); + $self->pidl("\t\tNULL, &ndr_table_$if->{NAME},"); + $self->pidl("\t\tNDR_$uname, $submem, r);"); + $self->pidl("if (tevent_req_nomem(subreq, req)) {"); + $self->indent; + $self->pidl("return tevent_req_post(req, ev);"); + $self->deindent; + $self->pidl("}"); + $self->pidl("tevent_req_set_callback(subreq, dcerpc_$name\_r_done, req);"); + $self->pidl(""); + + $self->pidl("return req;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); +} + +sub ParseFunction_r_Done($$$$) +{ + my ($self, $if, $fn, $name) = @_; + my $uname = uc $name; + + my $proto = "static void dcerpc_$name\_r_done(struct tevent_req *subreq)"; + + $self->pidl("$proto"); + $self->pidl("{"); + $self->indent; + + $self->pidl("struct tevent_req *req ="); + $self->pidl("\ttevent_req_callback_data(subreq,"); + $self->pidl("\tstruct tevent_req);"); + $self->pidl("NTSTATUS status;"); + $self->pidl(""); + + $self->pidl("status = dcerpc_binding_handle_call_recv(subreq);"); + $self->pidl("TALLOC_FREE(subreq);"); + $self->pidl("if (tevent_req_nterror(req, status)) {"); + $self->indent; + $self->pidl("return;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $self->pidl("tevent_req_done(req);"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); +} + +sub ParseFunction_r_Recv($$$$) +{ + my ($self, $if, $fn, $name) = @_; + my $uname = uc $name; + + my $proto = "NTSTATUS dcerpc_$name\_r_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)"; + + $self->fn_declare($proto); + + $self->pidl("{"); + $self->indent; + + $self->pidl("struct dcerpc_$name\_r_state *state ="); + $self->pidl("\ttevent_req_data(req,"); + $self->pidl("\tstruct dcerpc_$name\_r_state);"); + $self->pidl("NTSTATUS status;"); + $self->pidl(""); + + $self->pidl("if (tevent_req_is_nterror(req, &status)) {"); + $self->indent; + $self->pidl("tevent_req_received(req);"); + $self->pidl("return status;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $self->pidl("talloc_steal(mem_ctx, state->out_mem_ctx);"); + $self->pidl(""); + + $self->pidl("tevent_req_received(req);"); + $self->pidl("return NT_STATUS_OK;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); +} + +sub ParseFunction_r_Sync($$$$) +{ + my ($self, $if, $fn, $name) = @_; + my $uname = uc $name; + + if ($self->ParseFunctionHasPipes($fn)) { + $self->pidl_both("/*"); + $self->pidl_both(" * The following function is skipped because"); + $self->pidl_both(" * it uses pipes:"); + $self->pidl_both(" *"); + $self->pidl_both(" * dcerpc_$name\_r()"); + $self->pidl_both(" */"); + $self->pidl_both(""); + return; + } + + my $proto = "NTSTATUS dcerpc_$name\_r(struct dcerpc_binding_handle *h, TALLOC_CTX *mem_ctx, struct $name *r)"; + + $self->fn_declare($proto); + + $self->pidl("{"); + $self->indent; + $self->pidl("NTSTATUS status;"); + $self->pidl(""); + + $self->pidl("status = dcerpc_binding_handle_call(h,"); + $self->pidl("\t\tNULL, &ndr_table_$if->{NAME},"); + $self->pidl("\t\tNDR_$uname, mem_ctx, r);"); + $self->pidl(""); + $self->pidl("return status;"); + + $self->deindent; + $self->pidl("}"); + $self->pidl(""); +} + +sub ElementDirection($) +{ + my ($e) = @_; + + return "[in,out]" if (has_property($e, "in") and has_property($e, "out")); + return "[in]" if (has_property($e, "in")); + return "[out]" if (has_property($e, "out")); + return "[in,out]"; +} + +sub HeaderProperties($$) +{ + my($props,$ignores) = @_; + my $ret = ""; + + foreach my $d (sort(keys %{$props})) { + next if (grep(/^$d$/, @$ignores)); + if($props->{$d} ne "1") { + $ret.= "$d($props->{$d}),"; + } else { + $ret.="$d,"; + } + } + + if ($ret) { + return "[" . substr($ret, 0, -1) . "]"; + } +} + +sub ParseCopyArgument($$$$$) +{ + my ($self, $fn, $e, $r, $i) = @_; + my $l = $e->{LEVELS}[0]; + + if ($l->{TYPE} eq "ARRAY" and $l->{IS_FIXED} == 1) { + $self->pidl("memcpy(${r}$e->{NAME}, ${i}$e->{NAME}, sizeof(${r}$e->{NAME}));"); + } else { + $self->pidl("${r}$e->{NAME} = ${i}$e->{NAME};"); + } +} + +sub ParseInvalidResponse($$) +{ + my ($self, $type) = @_; + + if ($type eq "sync") { + $self->pidl("return NT_STATUS_INVALID_NETWORK_RESPONSE;"); + } elsif ($type eq "async") { + $self->pidl("tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);"); + $self->pidl("return;"); + } else { + die("ParseInvalidResponse($type)"); + } +} + +sub ParseOutputArgument($$$$$$) +{ + my ($self, $fn, $e, $r, $o, $invalid_response_type) = @_; + my $level = 0; + + if ($e->{LEVELS}[0]->{TYPE} ne "POINTER" and $e->{LEVELS}[0]->{TYPE} ne "ARRAY") { + fatal($e->{ORIGINAL}, "[out] argument is not a pointer or array"); + return; + } + + if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") { + $level = 1; + if ($e->{LEVELS}[0]->{POINTER_TYPE} ne "ref") { + $self->pidl("if ($o$e->{NAME} && ${r}out.$e->{NAME}) {"); + $self->indent; + } + } + + if ($e->{LEVELS}[$level]->{TYPE} eq "ARRAY") { + # This is a call to GenerateFunctionInEnv intentionally. + # Since the data is being copied into a user-provided data + # structure, the user should be able to know the size beforehand + # to allocate a structure of the right size. + my $in_env = GenerateFunctionInEnv($fn, $r); + my $out_env = GenerateFunctionOutEnv($fn, $r); + my $l = $e->{LEVELS}[$level]; + + my $in_var = undef; + if (grep(/in/, @{$e->{DIRECTION}})) { + $in_var = ParseExpr($e->{NAME}, $in_env, $e->{ORIGINAL}); + } + my $out_var = ParseExpr($e->{NAME}, $out_env, $e->{ORIGINAL}); + + my $in_size_is = undef; + my $out_size_is = undef; + my $out_length_is = undef; + + my $avail_len = undef; + my $needed_len = undef; + + $self->pidl("{"); + $self->indent; + my $copy_len_var = "_copy_len_$e->{NAME}"; + $self->pidl("size_t $copy_len_var;"); + + if (not defined($l->{SIZE_IS})) { + if (not $l->{IS_ZERO_TERMINATED}) { + fatal($e->{ORIGINAL}, "no size known for [out] array `$e->{NAME}'"); + } + if (has_property($e, "charset")) { + $avail_len = "ndr_charset_length($in_var, CH_UNIX)"; + $needed_len = "ndr_charset_length($out_var, CH_UNIX)"; + } else { + $avail_len = "ndr_string_length($in_var, sizeof(*$in_var))"; + $needed_len = "ndr_string_length($out_var, sizeof(*$out_var))"; + } + $in_size_is = ""; + $out_size_is = ""; + $out_length_is = ""; + } else { + $in_size_is = ParseExpr($l->{SIZE_IS}, $in_env, $e->{ORIGINAL}); + $out_size_is = ParseExpr($l->{SIZE_IS}, $out_env, $e->{ORIGINAL}); + $out_length_is = $out_size_is; + if (defined($l->{LENGTH_IS})) { + $out_length_is = ParseExpr($l->{LENGTH_IS}, $out_env, $e->{ORIGINAL}); + } + if (has_property($e, "charset")) { + if (defined($in_var)) { + $avail_len = "ndr_charset_length($in_var, CH_UNIX)"; + } else { + $avail_len = $out_length_is; + } + $needed_len = "ndr_charset_length($out_var, CH_UNIX)"; + } + } + + if ($out_size_is ne $in_size_is) { + $self->pidl("if (($out_size_is) > ($in_size_is)) {"); + $self->indent; + $self->ParseInvalidResponse($invalid_response_type); + $self->deindent; + $self->pidl("}"); + } + if ($out_length_is ne $out_size_is) { + $self->pidl("if (($out_length_is) > ($out_size_is)) {"); + $self->indent; + $self->ParseInvalidResponse($invalid_response_type); + $self->deindent; + $self->pidl("}"); + } + if (defined($needed_len)) { + $self->pidl("$copy_len_var = $needed_len;"); + $self->pidl("if ($copy_len_var > $avail_len) {"); + $self->indent; + $self->ParseInvalidResponse($invalid_response_type); + $self->deindent; + $self->pidl("}"); + } else { + $self->pidl("$copy_len_var = $out_length_is;"); + } + + my $dest_ptr = "$o$e->{NAME}"; + my $elem_size = "sizeof(*$dest_ptr)"; + $self->pidl("if ($dest_ptr != $out_var) {"); + $self->indent; + if (has_property($e, "charset")) { + $dest_ptr = "discard_const_p(uint8_t *, $dest_ptr)"; + } + $self->pidl("memcpy($dest_ptr, $out_var, $copy_len_var * $elem_size);"); + $self->deindent; + $self->pidl("}"); + + $self->deindent; + $self->pidl("}"); + } else { + $self->pidl("*$o$e->{NAME} = *${r}out.$e->{NAME};"); + } + + if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") { + if ($e->{LEVELS}[0]->{POINTER_TYPE} ne "ref") { + $self->deindent; + $self->pidl("}"); + } + } +} + +sub ParseFunction_State($$$$) +{ + my ($self, $if, $fn, $name) = @_; + + my $state_str = "struct dcerpc_$name\_state"; + my $done_fn = "dcerpc_$name\_done"; + + $self->pidl("$state_str {"); + $self->indent; + $self->pidl("struct $name orig;"); + $self->pidl("struct $name tmp;"); + $self->pidl("TALLOC_CTX *out_mem_ctx;"); + $self->deindent; + $self->pidl("};"); + $self->pidl(""); + $self->pidl("static void $done_fn(struct tevent_req *subreq);"); + $self->pidl(""); +} + +sub ParseFunction_Send($$$$) +{ + my ($self, $if, $fn, $name) = @_; + + my $fn_args = ""; + my $state_str = "struct dcerpc_$name\_state"; + my $done_fn = "dcerpc_$name\_done"; + my $out_mem_ctx = "dcerpc_$name\_out_memory"; + my $fn_str = "struct tevent_req *dcerpc_$name\_send"; + my $pad = genpad($fn_str); + + $fn_args .= "TALLOC_CTX *mem_ctx"; + $fn_args .= ",\n" . $pad . "struct tevent_context *ev"; + $fn_args .= ",\n" . $pad . "struct dcerpc_binding_handle *h"; + + foreach (@{$fn->{ELEMENTS}}) { + my $dir = ElementDirection($_); + my $prop = HeaderProperties($_->{PROPERTIES}, ["in", "out"]); + $fn_args .= ",\n" . $pad . DeclLong($_, "_") . " /* $dir $prop */"; + } + + $self->fn_declare("$fn_str($fn_args)"); + $self->pidl("{"); + $self->indent; + $self->pidl("struct tevent_req *req;"); + $self->pidl("$state_str *state;"); + $self->pidl("struct tevent_req *subreq;"); + $self->pidl(""); + $self->pidl("req = tevent_req_create(mem_ctx, &state,"); + $self->pidl("\t\t\t$state_str);"); + $self->pidl("if (req == NULL) {"); + $self->indent; + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl("state->out_mem_ctx = NULL;"); + $self->pidl(""); + + $self->pidl("/* In parameters */"); + foreach my $e (@{$fn->{ELEMENTS}}) { + next unless (grep(/in/, @{$e->{DIRECTION}})); + + $self->ParseCopyArgument($fn, $e, "state->orig.in.", "_"); + } + $self->pidl(""); + + my $out_params = 0; + $self->pidl("/* Out parameters */"); + foreach my $e (@{$fn->{ELEMENTS}}) { + next unless grep(/out/, @{$e->{DIRECTION}}); + + $self->ParseCopyArgument($fn, $e, "state->orig.out.", "_"); + + next if ContainsPipe($e, $e->{LEVELS}[0]); + + $out_params++; + } + $self->pidl(""); + + if (defined($fn->{RETURN_TYPE})) { + $self->pidl("/* Result */"); + $self->pidl("ZERO_STRUCT(state->orig.out.result);"); + $self->pidl(""); + } + + if ($out_params > 0) { + $self->pidl("state->out_mem_ctx = talloc_named_const(state, 0,"); + $self->pidl("\t\t \"$out_mem_ctx\");"); + $self->pidl("if (tevent_req_nomem(state->out_mem_ctx, req)) {"); + $self->indent; + $self->pidl("return tevent_req_post(req, ev);"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + } + + $self->pidl("/* make a temporary copy, that we pass to the dispatch function */"); + $self->pidl("state->tmp = state->orig;"); + $self->pidl(""); + + $self->pidl("subreq = dcerpc_$name\_r_send(state, ev, h, &state->tmp);"); + $self->pidl("if (tevent_req_nomem(subreq, req)) {"); + $self->indent; + $self->pidl("return tevent_req_post(req, ev);"); + $self->deindent; + $self->pidl("}"); + $self->pidl("tevent_req_set_callback(subreq, $done_fn, req);"); + $self->pidl("return req;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); +} + +sub ParseFunction_Done($$$$) +{ + my ($self, $if, $fn, $name) = @_; + + my $state_str = "struct dcerpc_$name\_state"; + my $done_fn = "dcerpc_$name\_done"; + + $self->pidl("static void $done_fn(struct tevent_req *subreq)"); + $self->pidl("{"); + $self->indent; + $self->pidl("struct tevent_req *req = tevent_req_callback_data("); + $self->pidl("\tsubreq, struct tevent_req);"); + $self->pidl("$state_str *state = tevent_req_data("); + $self->pidl("\treq, $state_str);"); + $self->pidl("NTSTATUS status;"); + $self->pidl("TALLOC_CTX *mem_ctx;"); + $self->pidl(""); + + $self->pidl("if (state->out_mem_ctx) {"); + $self->indent; + $self->pidl("mem_ctx = state->out_mem_ctx;"); + $self->deindent; + $self->pidl("} else {"); + $self->indent; + $self->pidl("mem_ctx = state;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $self->pidl("status = dcerpc_$name\_r_recv(subreq, mem_ctx);"); + $self->pidl("TALLOC_FREE(subreq);"); + $self->pidl("if (tevent_req_nterror(req, status)) {"); + $self->indent; + $self->pidl("return;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $self->pidl("/* Copy out parameters */"); + foreach my $e (@{$fn->{ELEMENTS}}) { + next if ContainsPipe($e, $e->{LEVELS}[0]); + next unless (grep(/out/, @{$e->{DIRECTION}})); + + $self->ParseOutputArgument($fn, $e, + "state->tmp.", + "state->orig.out.", + "async"); + } + $self->pidl(""); + + if (defined($fn->{RETURN_TYPE})) { + $self->pidl("/* Copy result */"); + $self->pidl("state->orig.out.result = state->tmp.out.result;"); + $self->pidl(""); + } + + $self->pidl("/* Reset temporary structure */"); + $self->pidl("ZERO_STRUCT(state->tmp);"); + $self->pidl(""); + + $self->pidl("tevent_req_done(req);"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); +} + +sub ParseFunction_Recv($$$$) +{ + my ($self, $if, $fn, $name) = @_; + + my $fn_args = ""; + my $state_str = "struct dcerpc_$name\_state"; + my $fn_str = "NTSTATUS dcerpc_$name\_recv"; + my $pad = genpad($fn_str); + + $fn_args .= "struct tevent_req *req,\n" . $pad . "TALLOC_CTX *mem_ctx"; + + if (defined($fn->{RETURN_TYPE})) { + $fn_args .= ",\n" . $pad . mapTypeName($fn->{RETURN_TYPE}). " *result"; + } + + $self->fn_declare("$fn_str($fn_args)"); + $self->pidl("{"); + $self->indent; + $self->pidl("$state_str *state = tevent_req_data("); + $self->pidl("\treq, $state_str);"); + $self->pidl("NTSTATUS status;"); + $self->pidl(""); + $self->pidl("if (tevent_req_is_nterror(req, &status)) {"); + $self->indent; + $self->pidl("tevent_req_received(req);"); + $self->pidl("return status;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $self->pidl("/* Steal possible out parameters to the callers context */"); + $self->pidl("talloc_steal(mem_ctx, state->out_mem_ctx);"); + $self->pidl(""); + + if (defined($fn->{RETURN_TYPE})) { + $self->pidl("/* Return result */"); + $self->pidl("*result = state->orig.out.result;"); + $self->pidl(""); + } + + $self->pidl("tevent_req_received(req);"); + $self->pidl("return NT_STATUS_OK;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); +} + +sub ParseFunction_Sync($$$$) +{ + my ($self, $if, $fn, $name) = @_; + + if ($self->ParseFunctionHasPipes($fn)) { + $self->pidl_both("/*"); + $self->pidl_both(" * The following function is skipped because"); + $self->pidl_both(" * it uses pipes:"); + $self->pidl_both(" *"); + $self->pidl_both(" * dcerpc_$name()"); + $self->pidl_both(" */"); + $self->pidl_both(""); + return; + } + + my $uname = uc $name; + my $fn_args = ""; + my $fn_str = "NTSTATUS dcerpc_$name"; + my $pad = genpad($fn_str); + + $fn_args .= "struct dcerpc_binding_handle *h,\n" . $pad . "TALLOC_CTX *mem_ctx"; + + foreach (@{$fn->{ELEMENTS}}) { + my $dir = ElementDirection($_); + my $prop = HeaderProperties($_->{PROPERTIES}, ["in", "out"]); + $fn_args .= ",\n" . $pad . DeclLong($_, "_") . " /* $dir $prop */"; + } + + if (defined($fn->{RETURN_TYPE})) { + $fn_args .= ",\n" . $pad . mapTypeName($fn->{RETURN_TYPE}). " *result"; + } + + $self->fn_declare("$fn_str($fn_args)"); + $self->pidl("{"); + $self->indent; + $self->pidl("struct $name r;"); + $self->pidl("NTSTATUS status;"); + $self->pidl(""); + + $self->pidl("/* In parameters */"); + foreach my $e (@{$fn->{ELEMENTS}}) { + next unless (grep(/in/, @{$e->{DIRECTION}})); + + $self->ParseCopyArgument($fn, $e, "r.in.", "_"); + } + $self->pidl(""); + + $self->pidl("/* Out parameters */"); + foreach my $e (@{$fn->{ELEMENTS}}) { + next unless grep(/out/, @{$e->{DIRECTION}}); + + $self->ParseCopyArgument($fn, $e, "r.out.", "_"); + } + $self->pidl(""); + + if (defined($fn->{RETURN_TYPE})) { + $self->pidl("/* Result */"); + $self->pidl("ZERO_STRUCT(r.out.result);"); + $self->pidl(""); + } + + $self->pidl("status = dcerpc_$name\_r(h, mem_ctx, &r);"); + $self->pidl("if (!NT_STATUS_IS_OK(status)) {"); + $self->indent; + $self->pidl("return status;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $self->pidl("/* Return variables */"); + foreach my $e (@{$fn->{ELEMENTS}}) { + next if ContainsPipe($e, $e->{LEVELS}[0]); + next unless (grep(/out/, @{$e->{DIRECTION}})); + + $self->ParseOutputArgument($fn, $e, "r.", "_", "sync"); + } + $self->pidl(""); + + $self->pidl("/* Return result */"); + if ($fn->{RETURN_TYPE}) { + $self->pidl("*result = r.out.result;"); + } + $self->pidl(""); + + $self->pidl("return NT_STATUS_OK;"); + + $self->deindent; + $self->pidl("}"); + $self->pidl(""); +} + +##################################################################### +# parse a function +sub ParseFunction($$$) +{ + my ($self, $if, $fn) = @_; + + if ($self->ParseFunctionHasPipes($fn)) { + $self->pidl_both("/*"); + $self->pidl_both(" * The following function is skipped because"); + $self->pidl_both(" * it uses pipes:"); + $self->pidl_both(" *"); + $self->pidl_both(" * dcerpc_$fn->{NAME}_r_send()"); + $self->pidl_both(" * dcerpc_$fn->{NAME}_r_recv()"); + $self->pidl_both(" * dcerpc_$fn->{NAME}_r()"); + $self->pidl_both(" *"); + $self->pidl_both(" * dcerpc_$fn->{NAME}_send()"); + $self->pidl_both(" * dcerpc_$fn->{NAME}_recv()"); + $self->pidl_both(" * dcerpc_$fn->{NAME}()"); + $self->pidl_both(" */"); + $self->pidl_both(""); + warning($fn->{ORIGINAL}, "$fn->{NAME}: dcerpc client does not support pipe yet"); + return; + } + + $self->ParseFunction_r_State($if, $fn, $fn->{NAME}); + $self->ParseFunction_r_Send($if, $fn, $fn->{NAME}); + $self->ParseFunction_r_Done($if, $fn, $fn->{NAME}); + $self->ParseFunction_r_Recv($if, $fn, $fn->{NAME}); + $self->ParseFunction_r_Sync($if, $fn, $fn->{NAME}); + + foreach my $e (@{$fn->{ELEMENTS}}) { + next unless (grep(/out/, @{$e->{DIRECTION}})); + + my $reason = "is not a pointer or array"; + + # TODO: make this fatal at NDR level + if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") { + if ($e->{LEVELS}[1]->{TYPE} eq "DATA" and + $e->{LEVELS}[1]->{DATA_TYPE} eq "string") { + $reason = "is a pointer to type 'string'"; + } elsif ($e->{LEVELS}[1]->{TYPE} eq "ARRAY" and + $e->{LEVELS}[1]->{IS_ZERO_TERMINATED}) { + next; + } elsif ($e->{LEVELS}[1]->{TYPE} eq "ARRAY" and + not defined($e->{LEVELS}[1]->{SIZE_IS})) { + $reason = "is a pointer to an unsized array"; + } else { + next; + } + } + if ($e->{LEVELS}[0]->{TYPE} eq "ARRAY") { + if (not defined($e->{LEVELS}[0]->{SIZE_IS})) { + $reason = "is an unsized array"; + } else { + next; + } + } + + $self->pidl_both("/*"); + $self->pidl_both(" * The following functions are skipped because"); + $self->pidl_both(" * an [out] argument $e->{NAME} $reason:"); + $self->pidl_both(" *"); + $self->pidl_both(" * dcerpc_$fn->{NAME}_send()"); + $self->pidl_both(" * dcerpc_$fn->{NAME}_recv()"); + $self->pidl_both(" * dcerpc_$fn->{NAME}()"); + $self->pidl_both(" */"); + $self->pidl_both(""); + + error($e->{ORIGINAL}, "$fn->{NAME}: [out] argument '$e->{NAME}' $reason, skip client functions"); + return; + } + + $self->ParseFunction_State($if, $fn, $fn->{NAME}); + $self->ParseFunction_Send($if, $fn, $fn->{NAME}); + $self->ParseFunction_Done($if, $fn, $fn->{NAME}); + $self->ParseFunction_Recv($if, $fn, $fn->{NAME}); + $self->ParseFunction_Sync($if, $fn, $fn->{NAME}); + + $self->pidl_hdr(""); +} + +my %done; + +##################################################################### +# parse the interface definitions +sub ParseInterface($$) +{ + my ($self, $if) = @_; + my $ifu = uc($if->{NAME}); + + $self->pidl_hdr("#ifndef _HEADER_RPC_$if->{NAME}"); + $self->pidl_hdr("#define _HEADER_RPC_$if->{NAME}"); + $self->pidl_hdr(""); + + if (defined $if->{PROPERTIES}->{uuid}) { + $self->pidl_hdr("extern const struct ndr_interface_table ndr_table_$if->{NAME};"); + $self->pidl_hdr(""); + } + + $self->pidl("/* $if->{NAME} - client functions generated by pidl */"); + $self->pidl(""); + + foreach my $fn (@{$if->{FUNCTIONS}}) { + next if defined($done{$fn->{NAME}}); + next if has_property($fn, "noopnum"); + next if has_property($fn, "todo"); + $self->ParseFunction($if, $fn); + $done{$fn->{NAME}} = 1; + } + + $self->pidl_hdr("#endif /* _HEADER_RPC_$if->{NAME} */"); +} + +sub Parse($$$$$$) +{ + my($self,$ndr,$header,$ndr_header,$client_header) = @_; + + $self->pidl("/* client functions auto-generated by pidl */"); + $self->pidl(""); + if (is_intree()) { + $self->pidl("#include \"includes.h\""); + } else { + $self->pidl("#ifndef _GNU_SOURCE"); + $self->pidl("#define _GNU_SOURCE"); + $self->pidl("#endif"); + $self->pidl("#include <stdio.h>"); + $self->pidl("#include <stdbool.h>"); + $self->pidl("#include <stdlib.h>"); + $self->pidl("#include <stdint.h>"); + $self->pidl("#include <stdarg.h>"); + $self->pidl("#include <string.h>"); + $self->pidl("#include <core/ntstatus.h>"); + } + $self->pidl("#include <tevent.h>"); + $self->pidl(choose_header("lib/util/tevent_ntstatus.h", "util/tevent_ntstatus.h").""); + $self->pidl("#include \"$ndr_header\""); + $self->pidl("#include \"$client_header\""); + $self->pidl(""); + + $self->pidl_hdr(choose_header("librpc/rpc/dcerpc.h", "dcerpc.h").""); + $self->pidl_hdr("#include \"$header\""); + + foreach my $x (@{$ndr}) { + ($x->{TYPE} eq "INTERFACE") && $self->ParseInterface($x); + } + + return ($self->{res},$self->{res_hdr}); +} + +1; diff --git a/tools/pidl/lib/Parse/Pidl/Samba4/NDR/Parser.pm b/tools/pidl/lib/Parse/Pidl/Samba4/NDR/Parser.pm new file mode 100644 index 00000000..cfcd29e2 --- /dev/null +++ b/tools/pidl/lib/Parse/Pidl/Samba4/NDR/Parser.pm @@ -0,0 +1,3224 @@ +################################################### +# Samba4 NDR parser generator for IDL structures +# Copyright tridge@samba.org 2000-2003 +# Copyright tpot@samba.org 2001 +# Copyright jelmer@samba.org 2004-2006 +# released under the GNU GPL + +package Parse::Pidl::Samba4::NDR::Parser; + +require Exporter; +@ISA = qw(Exporter); +@EXPORT_OK = qw(check_null_pointer NeededFunction NeededElement NeededType $res NeededInterface TypeFunctionName ParseElementPrint); + +use strict; +use Parse::Pidl::Typelist qw(hasType getType mapTypeName typeHasBody); +use Parse::Pidl::Util qw(has_property ParseExpr ParseExprExt print_uuid unmake_str); +use Parse::Pidl::CUtil qw(get_pointer_to get_value_of get_array_element); +use Parse::Pidl::NDR qw(GetPrevLevel GetNextLevel ContainsDeferred ContainsPipe is_charset_array); +use Parse::Pidl::Samba4 qw(is_intree choose_header ArrayDynamicallyAllocated); +use Parse::Pidl::Samba4::Header qw(GenerateFunctionInEnv GenerateFunctionOutEnv EnvSubstituteValue GenerateStructEnv); +use Parse::Pidl qw(warning); + +use vars qw($VERSION); +$VERSION = '0.01'; + +# list of known types +my %typefamily; + +sub new($$) { + my ($class) = @_; + my $self = { res => "", res_hdr => "", deferred => [], tabs => "", defer_tabs => "" }; + bless($self, $class); +} + +sub get_typefamily($) +{ + my $n = shift; + return $typefamily{$n}; +} + +sub append_prefix($$) +{ + my ($e, $var_name) = @_; + my $pointers = 0; + my $arrays = 0; + + foreach my $l (@{$e->{LEVELS}}) { + if ($l->{TYPE} eq "POINTER") { + $pointers++; + } elsif ($l->{TYPE} eq "ARRAY") { + $arrays++; + if (($pointers == 0) and + (not $l->{IS_FIXED}) and + (not $l->{IS_INLINE})) { + return get_value_of($var_name); + } + } elsif ($l->{TYPE} eq "DATA") { + if (Parse::Pidl::Typelist::scalar_is_reference($l->{DATA_TYPE})) { + return get_value_of($var_name) unless ($pointers or $arrays); + } + } + } + + return $var_name; +} + +sub has_fast_array($$) +{ + my ($e,$l) = @_; + + return 0 if ($l->{TYPE} ne "ARRAY"); + + my $nl = GetNextLevel($e,$l); + return 0 unless ($nl->{TYPE} eq "DATA"); + return 0 unless (hasType($nl->{DATA_TYPE})); + + my $t = getType($nl->{DATA_TYPE}); + + # Only uint8 and string have fast array functions at the moment + return ($t->{NAME} eq "uint8") or ($t->{NAME} eq "string"); +} + + +#################################### +# pidl() is our basic output routine +sub pidl($$) +{ + my ($self, $d) = @_; + if ($d) { + $self->{res} .= $self->{tabs}; + $self->{res} .= $d; + } + $self->{res} .="\n"; +} + +sub pidl_hdr($$) { my ($self, $d) = @_; $self->{res_hdr} .= "$d\n"; } + +#################################### +# defer() is like pidl(), but adds to +# a deferred buffer which is then added to the +# output buffer at the end of the structure/union/function +# This is needed to cope with code that must be pushed back +# to the end of a block of elements +sub defer_indent($) { my ($self) = @_; $self->{defer_tabs}.="\t"; } +sub defer_deindent($) { my ($self) = @_; $self->{defer_tabs}=substr($self->{defer_tabs}, 0, -1); } + +sub defer($$) +{ + my ($self, $d) = @_; + if ($d) { + push(@{$self->{deferred}}, $self->{defer_tabs}.$d); + } +} + +######################################## +# add the deferred content to the current +# output +sub add_deferred($) +{ + my ($self) = @_; + $self->pidl($_) foreach (@{$self->{deferred}}); + $self->{deferred} = []; + $self->{defer_tabs} = ""; +} + +sub indent($) +{ + my ($self) = @_; + $self->{tabs} .= "\t"; +} + +sub deindent($) +{ + my ($self) = @_; + $self->{tabs} = substr($self->{tabs}, 0, -1); +} + +##################################################################### +# declare a function public or static, depending on its attributes +sub fn_declare($$$$) +{ + my ($self,$type,$fn,$decl) = @_; + + if (has_property($fn, "no$type")) { + $self->pidl_hdr("$decl;"); + return 0; + } + + if (has_property($fn, "public")) { + $self->pidl_hdr("$decl;"); + $self->pidl("_PUBLIC_ $decl"); + } else { + $self->pidl("static $decl"); + } + + return 1; +} + +################################################################### +# setup any special flags for an element or structure +sub start_flags($$$) +{ + my ($self, $e, $ndr) = @_; + my $flags = has_property($e, "flag"); + if (defined $flags) { + $self->pidl("{"); + $self->indent; + $self->pidl("uint32_t _flags_save_$e->{TYPE} = $ndr->flags;"); + $self->pidl("ndr_set_flags(&$ndr->flags, $flags);"); + } +} + +################################################################### +# end any special flags for an element or structure +sub end_flags($$$) +{ + my ($self, $e, $ndr) = @_; + my $flags = has_property($e, "flag"); + if (defined $flags) { + $self->pidl("$ndr->flags = _flags_save_$e->{TYPE};"); + $self->deindent; + $self->pidl("}"); + } +} + +##################################################################### +# parse the data of an array - push side +sub ParseArrayPushHeader($$$$$$) +{ + my ($self,$e,$l,$ndr,$var_name,$env) = @_; + + my $size; + my $length; + + if ($l->{IS_ZERO_TERMINATED}) { + if (has_property($e, "charset")) { + $size = $length = "ndr_charset_length($var_name, CH_$e->{PROPERTIES}->{charset})"; + } else { + $size = $length = "ndr_string_length($var_name, sizeof(*$var_name))"; + } + if (defined($l->{SIZE_IS})) { + $size = ParseExpr($l->{SIZE_IS}, $env, $e); + } + if (defined($l->{LENGTH_IS})) { + $length = ParseExpr($l->{LENGTH_IS}, $env, $e); + } + } else { + $size = ParseExpr($l->{SIZE_IS}, $env, $e); + $length = ParseExpr($l->{LENGTH_IS}, $env, $e); + } + + if ((!$l->{IS_SURROUNDING}) and $l->{IS_CONFORMANT}) { + $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, $size));"); + } + + if ($l->{IS_VARYING}) { + $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, 0));"); # array offset + $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, $length));"); + } + + return $length; +} + +sub check_fully_dereferenced($$) +{ + my ($element, $env) = @_; + + return sub ($) { + my $origvar = shift; + my $check = 0; + + # Figure out the number of pointers in $ptr + my $expandedvar = $origvar; + $expandedvar =~ s/^(\**)//; + my $ptr = $1; + + my $var = undef; + foreach (keys %$env) { + if ($env->{$_} eq $expandedvar) { + $var = $_; + last; + } + } + + return($origvar) unless (defined($var)); + my $e; + foreach (@{$element->{PARENT}->{ELEMENTS}}) { + if ($_->{NAME} eq $var) { + $e = $_; + last; + } + } + + $e or die("Environment doesn't match siblings"); + + # See if pointer at pointer level $level + # needs to be checked. + my $nump = 0; + foreach (@{$e->{LEVELS}}) { + if ($_->{TYPE} eq "POINTER") { + $nump = $_->{POINTER_INDEX}+1; + } + } + warning($element->{ORIGINAL}, "Got pointer for `$e->{NAME}', expected fully dereferenced variable") if ($nump > length($ptr)); + return ($origvar); + } +} + +sub check_null_pointer($$$$) +{ + my ($element, $env, $print_fn, $return) = @_; + + return sub ($) { + my $expandedvar = shift; + my $check = 0; + + # Figure out the number of pointers in $ptr + $expandedvar =~ s/^(\**)//; + my $ptr = $1; + + my $var = undef; + foreach (keys %$env) { + if ($env->{$_} eq $expandedvar) { + $var = $_; + last; + } + } + + if (defined($var)) { + my $e; + # lookup ptr in $e + foreach (@{$element->{PARENT}->{ELEMENTS}}) { + if ($_->{NAME} eq $var) { + $e = $_; + last; + } + } + + $e or die("Environment doesn't match siblings"); + + # See if pointer at pointer level $level + # needs to be checked. + foreach my $l (@{$e->{LEVELS}}) { + if ($l->{TYPE} eq "POINTER" and + $l->{POINTER_INDEX} == length($ptr)) { + # No need to check ref pointers + $check = ($l->{POINTER_TYPE} ne "ref"); + last; + } + + if ($l->{TYPE} eq "DATA") { + warning($element, "too much dereferences for `$var'"); + } + } + } else { + warning($element, "unknown dereferenced expression `$expandedvar'"); + $check = 1; + } + + $print_fn->("if ($ptr$expandedvar == NULL) $return") if $check; + } +} + +sub is_deferred_switch_non_empty($) +{ + # 1 if there needs to be a deferred branch in an ndr_pull/push, + # 0 otherwise. + my ($e) = @_; + my $have_default = 0; + foreach my $el (@{$e->{ELEMENTS}}) { + if ($el->{CASE} eq "default") { + $have_default = 1; + } + if ($el->{TYPE} ne "EMPTY") { + if (ContainsDeferred($el, $el->{LEVELS}[0])) { + return 1; + } + } + } + return ! $have_default; +} + +sub ParseArrayPullGetSize($$$$$$) +{ + my ($self,$e,$l,$ndr,$var_name,$env) = @_; + + my $size; + + if ($l->{IS_CONFORMANT}) { + $size = "ndr_get_array_size($ndr, " . get_pointer_to($var_name) . ")"; + } elsif ($l->{IS_ZERO_TERMINATED} and $l->{SIZE_IS} == 0 and $l->{LENGTH_IS} == 0) { # Noheader arrays + $size = "ndr_get_string_size($ndr, sizeof(*$var_name))"; + } else { + $size = ParseExprExt($l->{SIZE_IS}, $env, $e->{ORIGINAL}, + check_null_pointer($e, $env, sub { $self->pidl(shift); }, + "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for size_is()\");"), + check_fully_dereferenced($e, $env)); + } + + $self->pidl("size_$e->{NAME}_$l->{LEVEL_INDEX} = $size;"); + my $array_size = "size_$e->{NAME}_$l->{LEVEL_INDEX}"; + + if (my $range = has_property($e, "range")) { + my ($low, $high) = split(/,/, $range, 2); + if ($low < 0) { + warning(0, "$low is invalid for the range of an array size"); + } + if ($low == 0) { + $self->pidl("if ($array_size > $high) {"); + } else { + $self->pidl("if ($array_size < $low || $array_size > $high) {"); + } + $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_RANGE, \"value out of range\");"); + $self->pidl("}"); + } + + return $array_size; +} + +##################################################################### +# parse an array - pull side +sub ParseArrayPullGetLength($$$$$$;$) +{ + my ($self,$e,$l,$ndr,$var_name,$env,$array_size) = @_; + + if (not defined($array_size)) { + $array_size = $self->ParseArrayPullGetSize($e, $l, $ndr, $var_name, $env); + } + + if (not $l->{IS_VARYING}) { + return $array_size; + } + + my $length = "ndr_get_array_length($ndr, " . get_pointer_to($var_name) .")"; + $self->pidl("length_$e->{NAME}_$l->{LEVEL_INDEX} = $length;"); + my $array_length = "length_$e->{NAME}_$l->{LEVEL_INDEX}"; + + if (my $range = has_property($e, "range")) { + my ($low, $high) = split(/,/, $range, 2); + if ($low < 0) { + warning(0, "$low is invalid for the range of an array size"); + } + if ($low == 0) { + $self->pidl("if ($array_length > $high) {"); + } else { + $self->pidl("if ($array_length < $low || $array_length > $high) {"); + } + $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_RANGE, \"value out of range\");"); + $self->pidl("}"); + } + + return $array_length; +} + +##################################################################### +# parse an array - pull side +sub ParseArrayPullHeader($$$$$$) +{ + my ($self,$e,$l,$ndr,$var_name,$env) = @_; + + if ((!$l->{IS_SURROUNDING}) and $l->{IS_CONFORMANT}) { + $self->pidl("NDR_CHECK(ndr_pull_array_size($ndr, " . get_pointer_to($var_name) . "));"); + } + + if ($l->{IS_VARYING}) { + $self->pidl("NDR_CHECK(ndr_pull_array_length($ndr, " . get_pointer_to($var_name) . "));"); + } + + my $array_size = $self->ParseArrayPullGetSize($e, $l, $ndr, $var_name, $env); + my $array_length = $self->ParseArrayPullGetLength($e, $l, $ndr, $var_name, $env, $array_size); + + if ($array_length ne $array_size) { + $self->pidl("if ($array_length > $array_size) {"); + $self->indent; + $self->pidl("return ndr_pull_error($ndr, NDR_ERR_ARRAY_SIZE, \"Bad array size %u should exceed array length %u\", $array_size, $array_length);"); + $self->deindent; + $self->pidl("}"); + } + + if ($l->{IS_CONFORMANT} and (defined($l->{SIZE_IS}) or not $l->{IS_ZERO_TERMINATED})) { + $self->defer("if ($var_name) {"); + $self->defer_indent; + my $size = ParseExprExt($l->{SIZE_IS}, $env, $e->{ORIGINAL}, + check_null_pointer($e, $env, sub { $self->defer(shift); }, + "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for size_is()\");"), + check_fully_dereferenced($e, $env)); + $self->defer("NDR_CHECK(ndr_check_array_size($ndr, (void*)" . get_pointer_to($var_name) . ", $size));"); + $self->defer_deindent; + $self->defer("}"); + } + + if ($l->{IS_VARYING} and (defined($l->{LENGTH_IS}) or not $l->{IS_ZERO_TERMINATED})) { + $self->defer("if ($var_name) {"); + $self->defer_indent; + my $length = ParseExprExt($l->{LENGTH_IS}, $env, $e->{ORIGINAL}, + check_null_pointer($e, $env, sub { $self->defer(shift); }, + "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for length_is()\");"), + check_fully_dereferenced($e, $env)); + $self->defer("NDR_CHECK(ndr_check_array_length($ndr, (void*)" . get_pointer_to($var_name) . ", $length));"); + $self->defer_deindent; + $self->defer("}"); + } + + if (ArrayDynamicallyAllocated($e,$l) and not is_charset_array($e,$l)) { + $self->AllocateArrayLevel($e,$l,$ndr,$var_name,$array_size); + } + + return $array_length; +} + +sub compression_alg($$) +{ + my ($e, $l) = @_; + my ($alg, $clen, $dlen) = split(/,/, $l->{COMPRESSION}); + + return $alg; +} + +sub compression_clen($$$) +{ + my ($e, $l, $env) = @_; + my ($alg, $clen, $dlen) = split(/,/, $l->{COMPRESSION}); + + return ParseExpr($clen, $env, $e->{ORIGINAL}); +} + +sub compression_dlen($$$) +{ + my ($e,$l,$env) = @_; + my ($alg, $clen, $dlen) = split(/,/, $l->{COMPRESSION}); + + return ParseExpr($dlen, $env, $e->{ORIGINAL}); +} + +sub ParseCompressionPushStart($$$$$) +{ + my ($self,$e,$l,$ndr,$env) = @_; + my $comndr = "$ndr\_compressed"; + my $alg = compression_alg($e, $l); + my $dlen = compression_dlen($e, $l, $env); + + $self->pidl("{"); + $self->indent; + $self->pidl("struct ndr_push *$comndr;"); + $self->pidl("NDR_CHECK(ndr_push_compression_start($ndr, &$comndr, $alg, $dlen));"); + + return $comndr; +} + +sub ParseCompressionPushEnd($$$$$) +{ + my ($self,$e,$l,$ndr,$env) = @_; + my $comndr = "$ndr\_compressed"; + my $alg = compression_alg($e, $l); + my $dlen = compression_dlen($e, $l, $env); + + $self->pidl("NDR_CHECK(ndr_push_compression_end($ndr, $comndr, $alg, $dlen));"); + $self->deindent; + $self->pidl("}"); +} + +sub ParseCompressionPullStart($$$$$) +{ + my ($self,$e,$l,$ndr,$env) = @_; + my $comndr = "$ndr\_compressed"; + my $alg = compression_alg($e, $l); + my $dlen = compression_dlen($e, $l, $env); + my $clen = compression_clen($e, $l, $env); + + $self->pidl("{"); + $self->indent; + $self->pidl("struct ndr_pull *$comndr;"); + $self->pidl("NDR_CHECK(ndr_pull_compression_start($ndr, &$comndr, $alg, $dlen, $clen));"); + + return $comndr; +} + +sub ParseCompressionPullEnd($$$$$) +{ + my ($self,$e,$l,$ndr,$env) = @_; + my $comndr = "$ndr\_compressed"; + my $alg = compression_alg($e, $l); + my $dlen = compression_dlen($e, $l, $env); + + $self->pidl("NDR_CHECK(ndr_pull_compression_end($ndr, $comndr, $alg, $dlen));"); + $self->deindent; + $self->pidl("}"); +} + +sub ParseSubcontextPushStart($$$$$) +{ + my ($self,$e,$l,$ndr,$env) = @_; + my $subndr = "_ndr_$e->{NAME}"; + my $subcontext_size = ParseExpr($l->{SUBCONTEXT_SIZE}, $env, $e->{ORIGINAL}); + + $self->pidl("{"); + $self->indent; + $self->pidl("struct ndr_push *$subndr;"); + $self->pidl("NDR_CHECK(ndr_push_subcontext_start($ndr, &$subndr, $l->{HEADER_SIZE}, $subcontext_size));"); + + if (defined $l->{COMPRESSION}) { + $subndr = $self->ParseCompressionPushStart($e, $l, $subndr, $env); + } + + return $subndr; +} + +sub ParseSubcontextPushEnd($$$$$) +{ + my ($self,$e,$l,$ndr,$env) = @_; + my $subndr = "_ndr_$e->{NAME}"; + my $subcontext_size = ParseExpr($l->{SUBCONTEXT_SIZE}, $env, $e->{ORIGINAL}); + + if (defined $l->{COMPRESSION}) { + $self->ParseCompressionPushEnd($e, $l, $subndr, $env); + } + + $self->pidl("NDR_CHECK(ndr_push_subcontext_end($ndr, $subndr, $l->{HEADER_SIZE}, $subcontext_size));"); + $self->deindent; + $self->pidl("}"); +} + +sub ParseSubcontextPullStart($$$$$) +{ + my ($self,$e,$l,$ndr,$env) = @_; + my $subndr = "_ndr_$e->{NAME}"; + my $subcontext_size = ParseExpr($l->{SUBCONTEXT_SIZE}, $env, $e->{ORIGINAL}); + + $self->pidl("{"); + $self->indent; + $self->pidl("struct ndr_pull *$subndr;"); + $self->pidl("NDR_CHECK(ndr_pull_subcontext_start($ndr, &$subndr, $l->{HEADER_SIZE}, $subcontext_size));"); + + if (defined $l->{COMPRESSION}) { + $subndr = $self->ParseCompressionPullStart($e, $l, $subndr, $env); + } + + return $subndr; +} + +sub ParseSubcontextPullEnd($$$$$) +{ + my ($self,$e,$l,$ndr,$env) = @_; + my $subndr = "_ndr_$e->{NAME}"; + my $subcontext_size = ParseExpr($l->{SUBCONTEXT_SIZE}, $env, $e->{ORIGINAL}); + + if (defined $l->{COMPRESSION}) { + $self->ParseCompressionPullEnd($e, $l, $subndr, $env); + } + + $self->pidl("NDR_CHECK(ndr_pull_subcontext_end($ndr, $subndr, $l->{HEADER_SIZE}, $subcontext_size));"); + $self->deindent; + $self->pidl("}"); +} + +sub ParseElementPushLevel +{ + my ($self,$e,$l,$ndr,$var_name,$env,$primitives,$deferred) = @_; + + my $ndr_flags = CalcNdrFlags($l, $primitives, $deferred); + + if ($l->{TYPE} eq "ARRAY" and ($l->{IS_CONFORMANT} or $l->{IS_VARYING})) { + $var_name = get_pointer_to($var_name); + } + + if (defined($ndr_flags)) { + if ($l->{TYPE} eq "SUBCONTEXT") { + my $subndr = $self->ParseSubcontextPushStart($e, $l, $ndr, $env); + $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $subndr, $var_name, $env, 1, 1); + $self->ParseSubcontextPushEnd($e, $l, $ndr, $env); + } elsif ($l->{TYPE} eq "POINTER") { + $self->ParsePtrPush($e, $l, $ndr, $var_name); + } elsif ($l->{TYPE} eq "ARRAY") { + my $length = $self->ParseArrayPushHeader($e, $l, $ndr, $var_name, $env); + + my $nl = GetNextLevel($e, $l); + + # Allow speedups for arrays of scalar types + if (is_charset_array($e,$l)) { + if ($l->{IS_TO_NULL}) { + $self->pidl("NDR_CHECK(ndr_push_charset_to_null($ndr, $ndr_flags, $var_name, $length, sizeof(" . mapTypeName($nl->{DATA_TYPE}) . "), CH_$e->{PROPERTIES}->{charset}));"); + } else { + $self->pidl("NDR_CHECK(ndr_push_charset($ndr, $ndr_flags, $var_name, $length, sizeof(" . mapTypeName($nl->{DATA_TYPE}) . "), CH_$e->{PROPERTIES}->{charset}));"); + } + return; + } elsif (has_fast_array($e,$l)) { + $self->pidl("NDR_CHECK(ndr_push_array_$nl->{DATA_TYPE}($ndr, $ndr_flags, $var_name, $length));"); + return; + } + } elsif ($l->{TYPE} eq "SWITCH") { + $self->ParseSwitchPush($e, $l, $ndr, $var_name, $env); + } elsif ($l->{TYPE} eq "DATA") { + $self->ParseDataPush($e, $l, $ndr, $var_name, $primitives, $deferred); + } elsif ($l->{TYPE} eq "TYPEDEF") { + $typefamily{$e->{DATA}->{TYPE}}->{PUSH_FN_BODY}->($self, $e->{DATA}, $ndr, $var_name); + } + } + + if ($l->{TYPE} eq "POINTER" and $l->{POINTER_TYPE} eq "ignore") { + $self->pidl("/* [ignore] '$e->{NAME}' */"); + } elsif ($l->{TYPE} eq "POINTER" and $deferred) { + my $rel_var_name = $var_name; + if ($l->{POINTER_TYPE} ne "ref") { + $self->pidl("if ($var_name) {"); + $self->indent; + if ($l->{POINTER_TYPE} eq "relative") { + $self->pidl("NDR_CHECK(ndr_push_relative_ptr2_start($ndr, $rel_var_name));"); + } + if ($l->{POINTER_TYPE} eq "relative_short") { + $self->pidl("NDR_CHECK(ndr_push_short_relative_ptr2($ndr, $var_name));"); + } + } + $var_name = get_value_of($var_name); + $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, 1, 1); + + if ($l->{POINTER_TYPE} ne "ref") { + if ($l->{POINTER_TYPE} eq "relative") { + $self->pidl("NDR_CHECK(ndr_push_relative_ptr2_end($ndr, $rel_var_name));"); + } + $self->deindent; + $self->pidl("}"); + } + } elsif ($l->{TYPE} eq "ARRAY" and not has_fast_array($e,$l) and + not is_charset_array($e, $l)) { + my $length = ParseExpr($l->{LENGTH_IS}, $env, $e->{ORIGINAL}); + my $counter = "cntr_$e->{NAME}_$l->{LEVEL_INDEX}"; + + my $array_pointless = ($length eq "0"); + + if ($array_pointless) { + warning($e->{ORIGINAL}, "pointless array `$e->{NAME}' will always have size 0"); + } + + $var_name = get_array_element($var_name, $counter); + + if ((($primitives and not $l->{IS_DEFERRED}) or ($deferred and $l->{IS_DEFERRED})) and not $array_pointless) { + $self->pidl("for ($counter = 0; $counter < ($length); $counter++) {"); + $self->indent; + $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, 1, 0); + $self->deindent; + $self->pidl("}"); + } + + if ($deferred and ContainsDeferred($e, $l) and not $array_pointless) { + $self->pidl("for ($counter = 0; $counter < ($length); $counter++) {"); + $self->indent; + $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, 0, 1); + $self->deindent; + $self->pidl("}"); + } + } elsif ($l->{TYPE} eq "SWITCH") { + $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, $primitives, $deferred); + } +} + +##################################################################### +# parse scalars in a structure element +sub ParseElementPush($$$$$$) +{ + my ($self,$e,$ndr,$env,$primitives,$deferred) = @_; + my $subndr = undef; + + my $var_name = $env->{$e->{NAME}}; + + if (has_property($e, "skip") or has_property($e, "skip_noinit")) { + $self->pidl("/* [skip] '$var_name' */"); + return; + } + + return if ContainsPipe($e, $e->{LEVELS}[0]); + + return unless $primitives or ($deferred and ContainsDeferred($e, $e->{LEVELS}[0])); + + # Representation type is different from transmit_as + if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) { + $self->pidl("{"); + $self->indent; + my $transmit_name = "_transmit_$e->{NAME}"; + $self->pidl(mapTypeName($e->{TYPE}) ." $transmit_name;"); + $self->pidl("NDR_CHECK(ndr_$e->{REPRESENTATION_TYPE}_to_$e->{TYPE}($var_name, " . get_pointer_to($transmit_name) . "));"); + $var_name = $transmit_name; + } + + $var_name = append_prefix($e, $var_name); + + $self->start_flags($e, $ndr); + + if (defined(my $value = has_property($e, "value"))) { + $var_name = ParseExpr($value, $env, $e->{ORIGINAL}); + } + + $self->ParseElementPushLevel($e, $e->{LEVELS}[0], $ndr, $var_name, $env, $primitives, $deferred); + + $self->end_flags($e, $ndr); + + if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) { + $self->deindent; + $self->pidl("}"); + } +} + +##################################################################### +# parse a pointer in a struct element or function +sub ParsePtrPush($$$$$) +{ + my ($self,$e,$l,$ndr,$var_name) = @_; + + if ($l->{POINTER_TYPE} eq "ref") { + if ($l->{LEVEL_INDEX} > 0) { + $self->pidl("if ($var_name == NULL) {"); + $self->indent; + $self->pidl("return ndr_push_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL [ref] pointer\");"); + $self->deindent; + $self->pidl("}"); + } + if ($l->{LEVEL} eq "EMBEDDED") { + $self->pidl("NDR_CHECK(ndr_push_ref_ptr(ndr)); /* $var_name */"); + } + } elsif ($l->{POINTER_TYPE} eq "relative") { + $self->pidl("NDR_CHECK(ndr_push_relative_ptr1($ndr, $var_name));"); + } elsif ($l->{POINTER_TYPE} eq "relative_short") { + $self->pidl("NDR_CHECK(ndr_push_short_relative_ptr1($ndr, $var_name));"); + } elsif ($l->{POINTER_TYPE} eq "unique") { + $self->pidl("NDR_CHECK(ndr_push_unique_ptr($ndr, $var_name));"); + } elsif ($l->{POINTER_TYPE} eq "full") { + $self->pidl("NDR_CHECK(ndr_push_full_ptr($ndr, $var_name));"); + } elsif ($l->{POINTER_TYPE} eq "ignore") { + # We don't want this pointer to appear on the wire at all + $self->pidl("NDR_CHECK(ndr_push_uint3264(ndr, NDR_SCALARS, 0));"); + } else { + die("Unhandled pointer type $l->{POINTER_TYPE}"); + } +} + +sub need_pointer_to($$$) +{ + my ($e, $l, $scalar_only) = @_; + + my $t; + if (ref($l->{DATA_TYPE})) { + $t = "$l->{DATA_TYPE}->{TYPE}_$l->{DATA_TYPE}->{NAME}"; + } else { + $t = $l->{DATA_TYPE}; + } + + if (not Parse::Pidl::Typelist::is_scalar($t)) { + return 1 if $scalar_only; + } + + my $arrays = 0; + + foreach my $tl (@{$e->{LEVELS}}) { + last if $l == $tl; + if ($tl->{TYPE} eq "ARRAY") { + $arrays++; + } + } + + if (Parse::Pidl::Typelist::scalar_is_reference($t)) { + return 1 unless $arrays; + } + + return 0; +} + +sub ParseDataPrint($$$$$) +{ + my ($self, $e, $l, $ndr, $var_name) = @_; + + if (not ref($l->{DATA_TYPE}) or defined($l->{DATA_TYPE}->{NAME})) { + + if (need_pointer_to($e, $l, 1)) { + $var_name = get_pointer_to($var_name); + } + + $self->pidl(TypeFunctionName("ndr_print", $l->{DATA_TYPE})."($ndr, \"$e->{NAME}\", $var_name);"); + } else { + $self->ParseTypePrint($l->{DATA_TYPE}, $ndr, $var_name); + } +} + +##################################################################### +# print scalars in a structure element +sub ParseElementPrint($$$$$) +{ + my($self, $e, $ndr, $var_name, $env) = @_; + + return if (has_property($e, "noprint")); + my $cur_depth = 0; + my $ignore_depth = 0xFFFF; + + $self->start_flags($e, $ndr); + if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) { + $self->pidl("ndr_print_$e->{REPRESENTATION_TYPE}($ndr, \"$e->{NAME}\", $var_name);"); + $self->end_flags($e, $ndr); + return; + } + + $var_name = append_prefix($e, $var_name); + + if (defined(my $value = has_property($e, "value"))) { + $var_name = "($ndr->flags & LIBNDR_PRINT_SET_VALUES)?" . ParseExpr($value,$env, $e->{ORIGINAL}) . ":$var_name"; + } + + foreach my $l (@{$e->{LEVELS}}) { + $cur_depth += 1; + + if ($cur_depth > $ignore_depth) { + next; + } + + if ($l->{TYPE} eq "POINTER") { + $self->pidl("ndr_print_ptr($ndr, \"$e->{NAME}\", $var_name);"); + if ($l->{POINTER_TYPE} eq "ignore") { + $self->pidl("/* [ignore] '$e->{NAME}' */"); + $ignore_depth = $cur_depth; + last; + } + $self->pidl("$ndr->depth++;"); + if ($l->{POINTER_TYPE} ne "ref") { + $self->pidl("if ($var_name) {"); + $self->indent; + } + $var_name = get_value_of($var_name); + } elsif ($l->{TYPE} eq "ARRAY") { + my $length; + + if ($l->{IS_CONFORMANT} or $l->{IS_VARYING}) { + $var_name = get_pointer_to($var_name); + } + + if ($l->{IS_ZERO_TERMINATED} and not defined($l->{LENGTH_IS})) { + $length = "ndr_string_length($var_name, sizeof(*$var_name))"; + } else { + $length = ParseExprExt($l->{LENGTH_IS}, $env, $e->{ORIGINAL}, + check_null_pointer($e, $env, sub { $self->pidl(shift); }, "return;"), check_fully_dereferenced($e, $env)); + } + + if (is_charset_array($e,$l)) { + $self->pidl("ndr_print_string($ndr, \"$e->{NAME}\", $var_name);"); + last; + } elsif (has_fast_array($e, $l)) { + my $nl = GetNextLevel($e, $l); + $self->pidl("ndr_print_array_$nl->{DATA_TYPE}($ndr, \"$e->{NAME}\", $var_name, $length);"); + last; + } else { + my $counter = "cntr_$e->{NAME}_$l->{LEVEL_INDEX}"; + + $self->pidl("$ndr->print($ndr, \"\%s: ARRAY(\%d)\", \"$e->{NAME}\", (int)$length);"); + $self->pidl("$ndr->depth++;"); + $self->pidl("for ($counter = 0; $counter < ($length); $counter++) {"); + $self->indent; + + $var_name = get_array_element($var_name, $counter); + } + } elsif ($l->{TYPE} eq "DATA") { + $self->ParseDataPrint($e, $l, $ndr, $var_name); + } elsif ($l->{TYPE} eq "SWITCH") { + my $switch_var = ParseExprExt($l->{SWITCH_IS}, $env, $e->{ORIGINAL}, + check_null_pointer($e, $env, sub { $self->pidl(shift); }, "return;"), check_fully_dereferenced($e, $env)); + $self->pidl("ndr_print_set_switch_value($ndr, " . get_pointer_to($var_name) . ", $switch_var);"); + } + } + + foreach my $l (reverse @{$e->{LEVELS}}) { + $cur_depth -= 1; + + if ($cur_depth > $ignore_depth) { + next; + } + + if ($l->{TYPE} eq "POINTER") { + if ($l->{POINTER_TYPE} eq "ignore") { + next; + } + + if ($l->{POINTER_TYPE} ne "ref") { + $self->deindent; + $self->pidl("}"); + } + $self->pidl("$ndr->depth--;"); + } elsif (($l->{TYPE} eq "ARRAY") + and not is_charset_array($e,$l) + and not has_fast_array($e,$l)) { + $self->deindent; + $self->pidl("}"); + $self->pidl("$ndr->depth--;"); + } + } + + $self->end_flags($e, $ndr); +} + +##################################################################### +# parse scalars in a structure element - pull size +sub ParseSwitchPull($$$$$$) +{ + my($self,$e,$l,$ndr,$var_name,$env) = @_; + my $switch_var = ParseExprExt($l->{SWITCH_IS}, $env, $e->{ORIGINAL}, + check_null_pointer($e, $env, sub { $self->pidl(shift); }, + "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for switch_is()\");"), + check_fully_dereferenced($e, $env)); + + $var_name = get_pointer_to($var_name); + $self->pidl("NDR_CHECK(ndr_pull_set_switch_value($ndr, $var_name, $switch_var));"); +} + +##################################################################### +# push switch element +sub ParseSwitchPush($$$$$$) +{ + my($self,$e,$l,$ndr,$var_name,$env) = @_; + my $switch_var = ParseExprExt($l->{SWITCH_IS}, $env, $e->{ORIGINAL}, + check_null_pointer($e, $env, sub { $self->pidl(shift); }, + "return ndr_push_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for switch_is()\");"), + check_fully_dereferenced($e, $env)); + + $var_name = get_pointer_to($var_name); + $self->pidl("NDR_CHECK(ndr_push_set_switch_value($ndr, $var_name, $switch_var));"); +} + +sub ParseDataPull($$$$$$$) +{ + my ($self,$e,$l,$ndr,$var_name,$primitives,$deferred) = @_; + + if (not ref($l->{DATA_TYPE}) or defined($l->{DATA_TYPE}->{NAME})) { + + my $ndr_flags = CalcNdrFlags($l, $primitives, $deferred); + + if (need_pointer_to($e, $l, 0)) { + $var_name = get_pointer_to($var_name); + } + + $var_name = get_pointer_to($var_name); + + $self->pidl("NDR_CHECK(".TypeFunctionName("ndr_pull", $l->{DATA_TYPE})."($ndr, $ndr_flags, $var_name));"); + + my $pl = GetPrevLevel($e, $l); + + my $range = has_property($e, "range"); + if ($range and $pl->{TYPE} ne "ARRAY") { + $var_name = get_value_of($var_name); + my $signed = Parse::Pidl::Typelist::is_signed($l->{DATA_TYPE}); + my ($low, $high) = split(/,/, $range, 2); + if ($low < 0 and not $signed) { + warning(0, "$low is invalid for the range of an unsigned type"); + } + if ($low == 0 and not $signed) { + $self->pidl("if ($var_name > $high) {"); + } else { + $self->pidl("if ($var_name < $low || $var_name > $high) {"); + } + $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_RANGE, \"value out of range\");"); + $self->pidl("}"); + } + } else { + $self->ParseTypePull($l->{DATA_TYPE}, $ndr, $var_name, $primitives, $deferred); + } +} + +sub ParseDataPush($$$$$$$) +{ + my ($self,$e,$l,$ndr,$var_name,$primitives,$deferred) = @_; + + if (not ref($l->{DATA_TYPE}) or defined($l->{DATA_TYPE}->{NAME})) { + + my $ndr_flags = CalcNdrFlags($l, $primitives, $deferred); + + # strings are passed by value rather than reference + if (need_pointer_to($e, $l, 1)) { + $var_name = get_pointer_to($var_name); + } + + $self->pidl("NDR_CHECK(".TypeFunctionName("ndr_push", $l->{DATA_TYPE})."($ndr, $ndr_flags, $var_name));"); + } else { + $self->ParseTypePush($l->{DATA_TYPE}, $ndr, $var_name, $primitives, $deferred); + } +} + +sub CalcNdrFlags($$$) +{ + my ($l,$primitives,$deferred) = @_; + + my $scalars = 0; + my $buffers = 0; + + # Add NDR_SCALARS if this one is deferred + # and deferreds may be pushed + $scalars = 1 if ($l->{IS_DEFERRED} and $deferred); + + # Add NDR_SCALARS if this one is not deferred and + # primitives may be pushed + $scalars = 1 if (!$l->{IS_DEFERRED} and $primitives); + + # Add NDR_BUFFERS if this one contains deferred stuff + # and deferreds may be pushed + $buffers = 1 if ($l->{CONTAINS_DEFERRED} and $deferred); + + return "NDR_SCALARS|NDR_BUFFERS" if ($scalars and $buffers); + return "NDR_SCALARS" if ($scalars); + return "NDR_BUFFERS" if ($buffers); + return undef; +} + +sub ParseMemCtxPullFlags($$$$) +{ + my ($self, $e, $l) = @_; + + return undef unless ($l->{TYPE} eq "POINTER" or $l->{TYPE} eq "ARRAY"); + return undef if (($l->{TYPE} eq "POINTER") and ($l->{POINTER_TYPE} eq "ignore")); + + return undef unless ($l->{TYPE} ne "ARRAY" or ArrayDynamicallyAllocated($e,$l)); + return undef if has_fast_array($e, $l); + return undef if is_charset_array($e, $l); + + my $mem_flags = "0"; + + if (($l->{TYPE} eq "POINTER") and ($l->{POINTER_TYPE} eq "ref")) { + my $nl = GetNextLevel($e, $l); + return undef if ($nl->{TYPE} eq "PIPE"); + return undef if ($nl->{TYPE} eq "ARRAY"); + return undef if (($nl->{TYPE} eq "DATA") and ($nl->{DATA_TYPE} eq "string")); + + if ($l->{LEVEL} eq "TOP") { + $mem_flags = "LIBNDR_FLAG_REF_ALLOC"; + } + } + + return $mem_flags; +} + +sub ParseMemCtxPullStart($$$$$) +{ + my ($self, $e, $l, $ndr, $ptr_name) = @_; + + my $mem_r_ctx = "_mem_save_$e->{NAME}_$l->{LEVEL_INDEX}"; + my $mem_c_ctx = $ptr_name; + my $mem_c_flags = $self->ParseMemCtxPullFlags($e, $l); + + return unless defined($mem_c_flags); + + $self->pidl("$mem_r_ctx = NDR_PULL_GET_MEM_CTX($ndr);"); + $self->pidl("NDR_PULL_SET_MEM_CTX($ndr, $mem_c_ctx, $mem_c_flags);"); +} + +sub ParseMemCtxPullEnd($$$$) +{ + my ($self, $e, $l, $ndr) = @_; + + my $mem_r_ctx = "_mem_save_$e->{NAME}_$l->{LEVEL_INDEX}"; + my $mem_r_flags = $self->ParseMemCtxPullFlags($e, $l); + + return unless defined($mem_r_flags); + + $self->pidl("NDR_PULL_SET_MEM_CTX($ndr, $mem_r_ctx, $mem_r_flags);"); +} + +sub CheckStringTerminator($$$$$) +{ + my ($self,$ndr,$e,$l,$length) = @_; + my $nl = GetNextLevel($e, $l); + + # Make sure last element is zero! + $self->pidl("NDR_CHECK(ndr_check_string_terminator($ndr, $length, sizeof($nl->{DATA_TYPE}_t)));"); +} + +sub ParseElementPullLevel +{ + my($self,$e,$l,$ndr,$var_name,$env,$primitives,$deferred) = @_; + + my $ndr_flags = CalcNdrFlags($l, $primitives, $deferred); + my $array_length = undef; + + if (has_property($e, "skip") or has_property($e, "skip_noinit")) { + $self->pidl("/* [skip] '$var_name' */"); + if (not has_property($e, "skip_noinit")) { + $self->pidl("ZERO_STRUCT($var_name);"); + } + return; + } + + if ($l->{TYPE} eq "ARRAY" and ($l->{IS_VARYING} or $l->{IS_CONFORMANT})) { + $var_name = get_pointer_to($var_name); + } + + # Only pull something if there's actually something to be pulled + if (defined($ndr_flags)) { + if ($l->{TYPE} eq "SUBCONTEXT") { + my $subndr = $self->ParseSubcontextPullStart($e, $l, $ndr, $env); + $self->ParseElementPullLevel($e, GetNextLevel($e,$l), $subndr, $var_name, $env, 1, 1); + $self->ParseSubcontextPullEnd($e, $l, $ndr, $env); + } elsif ($l->{TYPE} eq "ARRAY") { + my $length = $self->ParseArrayPullHeader($e, $l, $ndr, $var_name, $env); + $array_length = $length; + + my $nl = GetNextLevel($e, $l); + + if (is_charset_array($e,$l)) { + if ($l->{IS_ZERO_TERMINATED}) { + $self->CheckStringTerminator($ndr, $e, $l, $length); + } + if ($l->{IS_TO_NULL}) { + $self->pidl("NDR_CHECK(ndr_pull_charset_to_null($ndr, $ndr_flags, ".get_pointer_to($var_name).", $length, sizeof(" . mapTypeName($nl->{DATA_TYPE}) . "), CH_$e->{PROPERTIES}->{charset}));"); + } else { + $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}));"); + } + return; + } elsif (has_fast_array($e, $l)) { + if ($l->{IS_ZERO_TERMINATED}) { + $self->CheckStringTerminator($ndr,$e,$l,$length); + } + $self->pidl("NDR_CHECK(ndr_pull_array_$nl->{DATA_TYPE}($ndr, $ndr_flags, $var_name, $length));"); + return; + } + } elsif ($l->{TYPE} eq "POINTER") { + $self->ParsePtrPull($e, $l, $ndr, $var_name); + } elsif ($l->{TYPE} eq "SWITCH") { + $self->ParseSwitchPull($e, $l, $ndr, $var_name, $env); + } elsif ($l->{TYPE} eq "DATA") { + $self->ParseDataPull($e, $l, $ndr, $var_name, $primitives, $deferred); + } elsif ($l->{TYPE} eq "TYPEDEF") { + $typefamily{$e->{DATA}->{TYPE}}->{PULL_FN_BODY}->($self, $e->{DATA}, $ndr, $var_name); + } + } + + # add additional constructions + if ($l->{TYPE} eq "POINTER" and $l->{POINTER_TYPE} eq "ignore") { + $self->pidl("/* [ignore] '$e->{NAME}' */"); + } elsif ($l->{TYPE} eq "POINTER" and $deferred) { + if ($l->{POINTER_TYPE} ne "ref") { + $self->pidl("if ($var_name) {"); + $self->indent; + + if ($l->{POINTER_TYPE} eq "relative" or $l->{POINTER_TYPE} eq "relative_short") { + $self->pidl("uint32_t _relative_save_offset;"); + $self->pidl("_relative_save_offset = $ndr->offset;"); + $self->pidl("NDR_CHECK(ndr_pull_relative_ptr2($ndr, $var_name));"); + } + } + + $self->ParseMemCtxPullStart($e, $l, $ndr, $var_name); + + $var_name = get_value_of($var_name); + $self->ParseElementPullLevel($e, GetNextLevel($e,$l), $ndr, $var_name, $env, 1, 1); + + $self->ParseMemCtxPullEnd($e, $l, $ndr); + + if ($l->{POINTER_TYPE} ne "ref") { + if ($l->{POINTER_TYPE} eq "relative" or $l->{POINTER_TYPE} eq "relative_short") { + $self->pidl("if ($ndr->offset > $ndr->relative_highest_offset) {"); + $self->indent; + $self->pidl("$ndr->relative_highest_offset = $ndr->offset;"); + $self->deindent; + $self->pidl("}"); + $self->pidl("$ndr->offset = _relative_save_offset;"); + } + $self->deindent; + $self->pidl("}"); + } + } elsif ($l->{TYPE} eq "ARRAY" and + not has_fast_array($e,$l) and not is_charset_array($e, $l)) { + my $length = $array_length; + my $counter = "cntr_$e->{NAME}_$l->{LEVEL_INDEX}"; + my $array_name = $var_name; + + if (not defined($length)) { + $length = $self->ParseArrayPullGetLength($e, $l, $ndr, $var_name, $env); + } + + $var_name = get_array_element($var_name, $counter); + + $self->ParseMemCtxPullStart($e, $l, $ndr, $array_name); + + if (($primitives and not $l->{IS_DEFERRED}) or ($deferred and $l->{IS_DEFERRED})) { + my $nl = GetNextLevel($e,$l); + + if ($l->{IS_ZERO_TERMINATED}) { + $self->CheckStringTerminator($ndr,$e,$l,$length); + } + + $self->pidl("for ($counter = 0; $counter < ($length); $counter++) {"); + $self->indent; + $self->ParseElementPullLevel($e, $nl, $ndr, $var_name, $env, 1, 0); + $self->deindent; + $self->pidl("}"); + } + + if ($deferred and ContainsDeferred($e, $l)) { + $self->pidl("for ($counter = 0; $counter < ($length); $counter++) {"); + $self->indent; + $self->ParseElementPullLevel($e,GetNextLevel($e,$l), $ndr, $var_name, $env, 0, 1); + $self->deindent; + $self->pidl("}"); + } + + $self->ParseMemCtxPullEnd($e, $l, $ndr); + + } elsif ($l->{TYPE} eq "SWITCH") { + $self->ParseElementPullLevel($e, GetNextLevel($e,$l), $ndr, $var_name, $env, $primitives, $deferred); + } +} + +##################################################################### +# parse scalars in a structure element - pull size +sub ParseElementPull($$$$$$) +{ + my($self,$e,$ndr,$env,$primitives,$deferred) = @_; + + my $var_name = $env->{$e->{NAME}}; + my $represent_name; + my $transmit_name; + + return if ContainsPipe($e, $e->{LEVELS}[0]); + + return unless $primitives or ($deferred and ContainsDeferred($e, $e->{LEVELS}[0])); + + if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) { + $self->pidl("{"); + $self->indent; + $represent_name = $var_name; + $transmit_name = "_transmit_$e->{NAME}"; + $var_name = $transmit_name; + $self->pidl(mapTypeName($e->{TYPE})." $var_name;"); + } + + $var_name = append_prefix($e, $var_name); + + $self->start_flags($e, $ndr); + + $self->ParseElementPullLevel($e,$e->{LEVELS}[0],$ndr,$var_name,$env,$primitives,$deferred); + + $self->end_flags($e, $ndr); + + # Representation type is different from transmit_as + if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) { + $self->pidl("NDR_CHECK(ndr_$e->{TYPE}_to_$e->{REPRESENTATION_TYPE}($transmit_name, ".get_pointer_to($represent_name)."));"); + $self->deindent; + $self->pidl("}"); + } +} + +##################################################################### +# parse a pointer in a struct element or function +sub ParsePtrPull($$$$$) +{ + my($self, $e,$l,$ndr,$var_name) = @_; + + my $nl = GetNextLevel($e, $l); + my $next_is_array = ($nl->{TYPE} eq "ARRAY"); + my $next_is_string = (($nl->{TYPE} eq "DATA") and + ($nl->{DATA_TYPE} eq "string")); + + if ($l->{POINTER_TYPE} eq "ref" and $l->{LEVEL} eq "TOP") { + + if (!$next_is_array and !$next_is_string) { + $self->pidl("if ($ndr->flags & LIBNDR_FLAG_REF_ALLOC) {"); + $self->pidl("\tNDR_PULL_ALLOC($ndr, $var_name);"); + $self->pidl("}"); + } + + return; + } elsif ($l->{POINTER_TYPE} eq "ref" and $l->{LEVEL} eq "EMBEDDED") { + $self->pidl("NDR_CHECK(ndr_pull_ref_ptr($ndr, &_ptr_$e->{NAME}));"); + } elsif (($l->{POINTER_TYPE} eq "unique") or + ($l->{POINTER_TYPE} eq "relative") or + ($l->{POINTER_TYPE} eq "full")) { + $self->pidl("NDR_CHECK(ndr_pull_generic_ptr($ndr, &_ptr_$e->{NAME}));"); + } elsif ($l->{POINTER_TYPE} eq "relative_short") { + $self->pidl("NDR_CHECK(ndr_pull_relative_ptr_short($ndr, &_ptr_$e->{NAME}));"); + } elsif ($l->{POINTER_TYPE} eq "ignore") { + #We want to consume the pointer bytes, but ignore the pointer value + $self->pidl("NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &_ptr_$e->{NAME}));"); + $self->pidl("_ptr_$e->{NAME} = 0;"); + } else { + die("Unhandled pointer type $l->{POINTER_TYPE}"); + } + + $self->pidl("if (_ptr_$e->{NAME}) {"); + $self->indent; + + if ($l->{POINTER_TYPE} eq "ignore") { + # Don't do anything, we don't want to do the + # allocation, as we forced it to NULL just above, and + # we may not know the declared type anyway. + } else { + # Don't do this for arrays, they're allocated at the actual level + # of the array + unless ($next_is_array or $next_is_string) { + $self->pidl("NDR_PULL_ALLOC($ndr, $var_name);"); + } else { + # FIXME: Yes, this is nasty. + # We allocate an array twice + # - once just to indicate that it's there, + # - then the real allocation... + $self->pidl("NDR_PULL_ALLOC($ndr, $var_name);"); + } + } + + #$self->pidl("memset($var_name, 0, sizeof($var_name));"); + if ($l->{POINTER_TYPE} eq "relative" or $l->{POINTER_TYPE} eq "relative_short") { + $self->pidl("NDR_CHECK(ndr_pull_relative_ptr1($ndr, $var_name, _ptr_$e->{NAME}));"); + } + $self->deindent; + $self->pidl("} else {"); + $self->pidl("\t$var_name = NULL;"); + $self->pidl("}"); +} + +sub CheckRefPtrs($$$$) +{ + my ($self,$e,$ndr,$env) = @_; + + return if ContainsPipe($e, $e->{LEVELS}[0]); + return if ($e->{LEVELS}[0]->{TYPE} ne "POINTER"); + return if ($e->{LEVELS}[0]->{POINTER_TYPE} ne "ref"); + + my $var_name = $env->{$e->{NAME}}; + $var_name = append_prefix($e, $var_name); + + $self->pidl("if ($var_name == NULL) {"); + $self->indent; + $self->pidl("return ndr_push_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL [ref] pointer\");"); + $self->deindent; + $self->pidl("}"); +} + +sub ParseStructPushPrimitives($$$$$) +{ + my ($self, $struct, $ndr, $varname, $env) = @_; + + $self->CheckRefPtrs($_, $ndr, $env) foreach (@{$struct->{ELEMENTS}}); + + # see if the structure contains a conformant array. If it + # does, then it must be the last element of the structure, and + # we need to push the conformant length early, as it fits on + # the wire before the structure (and even before the structure + # alignment) + if (defined($struct->{SURROUNDING_ELEMENT})) { + my $e = $struct->{SURROUNDING_ELEMENT}; + + if (defined($e->{LEVELS}[0]) and + $e->{LEVELS}[0]->{TYPE} eq "ARRAY") { + my $size; + + if ($e->{LEVELS}[0]->{IS_ZERO_TERMINATED}) { + if (has_property($e, "charset")) { + $size = "ndr_charset_length($varname->$e->{NAME}, CH_$e->{PROPERTIES}->{charset})"; + } else { + $size = "ndr_string_length($varname->$e->{NAME}, sizeof(*$varname->$e->{NAME}))"; + } + if (defined($e->{LEVELS}[0]->{SIZE_IS})) { + $size = ParseExpr($e->{LEVELS}[0]->{SIZE_IS}, $env, $e->{ORIGINAL}); + } + } else { + $size = ParseExpr($e->{LEVELS}[0]->{SIZE_IS}, $env, $e->{ORIGINAL}); + } + + $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, $size));"); + } else { + $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, ndr_string_array_size($ndr, $varname->$e->{NAME})));"); + } + } + + $self->pidl("NDR_CHECK(ndr_push_align($ndr, $struct->{ALIGN}));"); + + if (defined($struct->{PROPERTIES}{relative_base})) { + # set the current offset as base for relative pointers + # and store it based on the toplevel struct/union + $self->pidl("NDR_CHECK(ndr_push_setup_relative_base_offset1($ndr, $varname, $ndr->offset));"); + } + + $self->ParseElementPush($_, $ndr, $env, 1, 0) foreach (@{$struct->{ELEMENTS}}); + + $self->pidl("NDR_CHECK(ndr_push_trailer_align($ndr, $struct->{ALIGN}));"); +} + +sub ParseStructPushDeferred($$$$) +{ + my ($self, $struct, $ndr, $varname, $env) = @_; + if (defined($struct->{PROPERTIES}{relative_base})) { + # retrieve the current offset as base for relative pointers + # based on the toplevel struct/union + $self->pidl("NDR_CHECK(ndr_push_setup_relative_base_offset2($ndr, $varname));"); + } + $self->ParseElementPush($_, $ndr, $env, 0, 1) foreach (@{$struct->{ELEMENTS}}); +} + +##################################################################### +# parse a struct +sub ParseStructPush($$$$) +{ + my ($self, $struct, $ndr, $varname) = @_; + + return unless defined($struct->{ELEMENTS}); + + my $env = GenerateStructEnv($struct, $varname); + + EnvSubstituteValue($env, $struct); + + $self->DeclareArrayVariablesNoZero($_, $env) foreach (@{$struct->{ELEMENTS}}); + + $self->start_flags($struct, $ndr); + + $self->pidl("NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);"); + $self->pidl("if (ndr_flags & NDR_SCALARS) {"); + $self->indent; + $self->ParseStructPushPrimitives($struct, $ndr, $varname, $env); + $self->deindent; + $self->pidl("}"); + + $self->pidl("if (ndr_flags & NDR_BUFFERS) {"); + $self->indent; + $self->ParseStructPushDeferred($struct, $ndr, $varname, $env); + $self->deindent; + $self->pidl("}"); + + $self->end_flags($struct, $ndr); +} + +##################################################################### +# generate a push function for an enum +sub ParseEnumPush($$$$) +{ + my($self,$enum,$ndr,$varname) = @_; + my($type_fn) = $enum->{BASE_TYPE}; + + $self->start_flags($enum, $ndr); + $self->pidl("NDR_CHECK(ndr_push_enum_$type_fn($ndr, NDR_SCALARS, $varname));"); + $self->end_flags($enum, $ndr); +} + +##################################################################### +# generate a pull function for an enum +sub ParseEnumPull($$$$) +{ + my($self,$enum,$ndr,$varname) = @_; + my($type_fn) = $enum->{BASE_TYPE}; + my($type_v_decl) = mapTypeName($type_fn); + + $self->pidl("$type_v_decl v;"); + $self->start_flags($enum, $ndr); + $self->pidl("NDR_CHECK(ndr_pull_enum_$type_fn($ndr, NDR_SCALARS, &v));"); + $self->pidl("*$varname = v;"); + + $self->end_flags($enum, $ndr); +} + +##################################################################### +# generate a print function for an enum +sub ParseEnumPrint($$$$$) +{ + my($self,$enum,$ndr,$name,$varname) = @_; + + $self->pidl("const char *val = NULL;"); + $self->pidl(""); + + $self->start_flags($enum, $ndr); + + $self->pidl("switch ($varname) {"); + $self->indent; + my $els = \@{$enum->{ELEMENTS}}; + foreach my $i (0 .. $#{$els}) { + my $e = ${$els}[$i]; + chomp $e; + if ($e =~ /^(.*)=/) { + $e = $1; + } + $self->pidl("case $e: val = \"$e\"; break;"); + } + + $self->deindent; + $self->pidl("}"); + + $self->pidl("ndr_print_enum($ndr, name, \"$enum->{TYPE}\", val, $varname);"); + + $self->end_flags($enum, $ndr); +} + +sub DeclEnum($$$$) +{ + my ($e,$t,$name,$varname) = @_; + return "enum $name " . + ($t eq "pull"?"*":"") . $varname; +} + +$typefamily{ENUM} = { + DECL => \&DeclEnum, + PUSH_FN_BODY => \&ParseEnumPush, + PULL_FN_BODY => \&ParseEnumPull, + PRINT_FN_BODY => \&ParseEnumPrint, +}; + +##################################################################### +# generate a push function for a bitmap +sub ParseBitmapPush($$$$) +{ + my($self,$bitmap,$ndr,$varname) = @_; + my($type_fn) = $bitmap->{BASE_TYPE}; + + $self->start_flags($bitmap, $ndr); + + $self->pidl("NDR_CHECK(ndr_push_$type_fn($ndr, NDR_SCALARS, $varname));"); + + $self->end_flags($bitmap, $ndr); +} + +##################################################################### +# generate a pull function for an bitmap +sub ParseBitmapPull($$$$) +{ + my($self,$bitmap,$ndr,$varname) = @_; + my $type_fn = $bitmap->{BASE_TYPE}; + my($type_decl) = mapTypeName($bitmap->{BASE_TYPE}); + + $self->pidl("$type_decl v;"); + $self->start_flags($bitmap, $ndr); + $self->pidl("NDR_CHECK(ndr_pull_$type_fn($ndr, NDR_SCALARS, &v));"); + $self->pidl("*$varname = v;"); + + $self->end_flags($bitmap, $ndr); +} + +##################################################################### +# generate a print function for an bitmap +sub ParseBitmapPrintElement($$$$$$) +{ + my($self,$e,$bitmap,$ndr,$name,$varname) = @_; + my($type_decl) = mapTypeName($bitmap->{BASE_TYPE}); + my($type_fn) = $bitmap->{BASE_TYPE}; + my($flag); + + if ($e =~ /^(\w+) .*$/) { + $flag = "$1"; + } else { + die "Bitmap: \"$name\" invalid Flag: \"$e\""; + } + + $self->pidl("ndr_print_bitmap_flag($ndr, sizeof($type_decl), \"$flag\", $flag, $varname);"); +} + +##################################################################### +# generate a print function for an bitmap +sub ParseBitmapPrint($$$$$) +{ + my($self,$bitmap,$ndr,$name,$varname) = @_; + my($type_decl) = mapTypeName($bitmap->{TYPE}); + my($type_fn) = $bitmap->{BASE_TYPE}; + + $self->start_flags($bitmap, $ndr); + + $self->pidl("ndr_print_$type_fn($ndr, name, $varname);"); + + $self->pidl("$ndr->depth++;"); + foreach my $e (@{$bitmap->{ELEMENTS}}) { + $self->ParseBitmapPrintElement($e, $bitmap, $ndr, $name, $varname); + } + $self->pidl("$ndr->depth--;"); + + $self->end_flags($bitmap, $ndr); +} + +sub DeclBitmap($$$$) +{ + my ($e,$t,$name,$varname) = @_; + return mapTypeName(Parse::Pidl::Typelist::bitmap_type_fn($e)) . + ($t eq "pull"?" *":" ") . $varname; +} + +$typefamily{BITMAP} = { + DECL => \&DeclBitmap, + PUSH_FN_BODY => \&ParseBitmapPush, + PULL_FN_BODY => \&ParseBitmapPull, + PRINT_FN_BODY => \&ParseBitmapPrint, +}; + +##################################################################### +# generate a struct print function +sub ParseStructPrint($$$$$) +{ + my($self,$struct,$ndr,$name,$varname) = @_; + + return unless defined $struct->{ELEMENTS}; + + my $env = GenerateStructEnv($struct, $varname); + + $self->DeclareArrayVariables($_) foreach (@{$struct->{ELEMENTS}}); + + $self->pidl("ndr_print_struct($ndr, name, \"$name\");"); + $self->pidl("if (r == NULL) { ndr_print_null($ndr); return; }"); + + $self->start_flags($struct, $ndr); + + $self->pidl("$ndr->depth++;"); + + $self->ParseElementPrint($_, $ndr, $env->{$_->{NAME}}, $env) + foreach (@{$struct->{ELEMENTS}}); + $self->pidl("$ndr->depth--;"); + + $self->end_flags($struct, $ndr); +} + +sub DeclarePtrVariables($$) +{ + my ($self,$e) = @_; + + if (has_property($e, "skip") or has_property($e, "skip_noinit")) { + return; + } + + foreach my $l (@{$e->{LEVELS}}) { + my $size = 32; + if ($l->{TYPE} eq "POINTER" and + not ($l->{POINTER_TYPE} eq "ref" and $l->{LEVEL} eq "TOP")) { + if ($l->{POINTER_TYPE} eq "relative_short") { + $size = 16; + } + $self->pidl("uint${size}_t _ptr_$e->{NAME};"); + last; + } + } +} + +sub DeclareArrayVariables($$;$) +{ + my ($self,$e,$pull) = @_; + + if (has_property($e, "skip") or has_property($e, "skip_noinit")) { + return; + } + + foreach my $l (@{$e->{LEVELS}}) { + next if ($l->{TYPE} ne "ARRAY"); + if (defined($pull)) { + $self->pidl("uint32_t size_$e->{NAME}_$l->{LEVEL_INDEX} = 0;"); + if ($l->{IS_VARYING}) { + $self->pidl("uint32_t length_$e->{NAME}_$l->{LEVEL_INDEX} = 0;"); + } + } + next if has_fast_array($e,$l); + next if is_charset_array($e,$l); + $self->pidl("uint32_t cntr_$e->{NAME}_$l->{LEVEL_INDEX};"); + } +} + +sub DeclareArrayVariablesNoZero($$$) +{ + my ($self,$e,$env) = @_; + + if (has_property($e, "skip") or has_property($e, "skip_noinit")) { + return; + } + + foreach my $l (@{$e->{LEVELS}}) { + next if ($l->{TYPE} ne "ARRAY"); + next if has_fast_array($e,$l); + next if is_charset_array($e,$l); + my $length = ParseExpr($l->{LENGTH_IS}, $env, $e->{ORIGINAL}); + if ($length eq "0") { + warning($e->{ORIGINAL}, "pointless array cntr: 'cntr_$e->{NAME}_$l->{LEVEL_INDEX}': length=$length"); + } else { + $self->pidl("uint32_t cntr_$e->{NAME}_$l->{LEVEL_INDEX};"); + } + } +} + +sub DeclareMemCtxVariables($$) +{ + my ($self,$e) = @_; + + if (has_property($e, "skip") or has_property($e, "skip_noinit")) { + return; + } + + foreach my $l (@{$e->{LEVELS}}) { + my $mem_flags = $self->ParseMemCtxPullFlags($e, $l); + + if (($l->{TYPE} eq "POINTER") and ($l->{POINTER_TYPE} eq "ignore")) { + last; + } + + if (defined($mem_flags)) { + $self->pidl("TALLOC_CTX *_mem_save_$e->{NAME}_$l->{LEVEL_INDEX} = NULL;"); + } + } +} + +sub ParseStructPullPrimitives($$$$$) +{ + my($self,$struct,$ndr,$varname,$env) = @_; + + if (defined $struct->{SURROUNDING_ELEMENT}) { + $self->pidl("NDR_CHECK(ndr_pull_array_size($ndr, &$varname->$struct->{SURROUNDING_ELEMENT}->{NAME}));"); + } + + $self->pidl("NDR_CHECK(ndr_pull_align($ndr, $struct->{ALIGN}));"); + + if (defined($struct->{PROPERTIES}{relative_base})) { + # set the current offset as base for relative pointers + # and store it based on the toplevel struct/union + $self->pidl("NDR_CHECK(ndr_pull_setup_relative_base_offset1($ndr, $varname, $ndr->offset));"); + } + + $self->ParseElementPull($_, $ndr, $env, 1, 0) foreach (@{$struct->{ELEMENTS}}); + + $self->add_deferred(); + + $self->pidl("NDR_CHECK(ndr_pull_trailer_align($ndr, $struct->{ALIGN}));"); +} + +sub ParseStructPullDeferred($$$$$) +{ + my ($self,$struct,$ndr,$varname,$env) = @_; + + if (defined($struct->{PROPERTIES}{relative_base})) { + # retrieve the current offset as base for relative pointers + # based on the toplevel struct/union + $self->pidl("NDR_CHECK(ndr_pull_setup_relative_base_offset2($ndr, $varname));"); + } + foreach my $e (@{$struct->{ELEMENTS}}) { + $self->ParseElementPull($e, $ndr, $env, 0, 1); + } + + $self->add_deferred(); +} + +##################################################################### +# parse a struct - pull side +sub ParseStructPull($$$$) +{ + my($self,$struct,$ndr,$varname) = @_; + + return unless defined $struct->{ELEMENTS}; + + # declare any internal pointers we need + foreach my $e (@{$struct->{ELEMENTS}}) { + $self->DeclarePtrVariables($e); + $self->DeclareArrayVariables($e, "pull"); + $self->DeclareMemCtxVariables($e); + } + + $self->start_flags($struct, $ndr); + + my $env = GenerateStructEnv($struct, $varname); + + $self->pidl("NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);"); + $self->pidl("if (ndr_flags & NDR_SCALARS) {"); + $self->indent; + $self->ParseStructPullPrimitives($struct,$ndr,$varname,$env); + $self->deindent; + $self->pidl("}"); + $self->pidl("if (ndr_flags & NDR_BUFFERS) {"); + $self->indent; + $self->ParseStructPullDeferred($struct,$ndr,$varname,$env); + $self->deindent; + $self->pidl("}"); + + $self->end_flags($struct, $ndr); +} + +##################################################################### +# calculate size of ndr struct +sub ParseStructNdrSize($$$$) +{ + my ($self,$t, $name, $varname) = @_; + my $sizevar; + + if (my $flags = has_property($t, "flag")) { + $self->pidl("flags |= $flags;"); + } + $self->pidl("return ndr_size_struct($varname, flags, (ndr_push_flags_fn_t)ndr_push_$name);"); +} + +sub DeclStruct($$$$) +{ + my ($e,$t,$name,$varname) = @_; + return ($t ne "pull"?"const ":"") . "struct $name *$varname"; +} + +sub ArgsStructNdrSize($$$) +{ + my ($d, $name, $varname) = @_; + return "const struct $name *$varname, int flags"; +} + +$typefamily{STRUCT} = { + PUSH_FN_BODY => \&ParseStructPush, + DECL => \&DeclStruct, + PULL_FN_BODY => \&ParseStructPull, + PRINT_FN_BODY => \&ParseStructPrint, + SIZE_FN_BODY => \&ParseStructNdrSize, + SIZE_FN_ARGS => \&ArgsStructNdrSize, +}; + +##################################################################### +# calculate size of ndr struct +sub ParseUnionNdrSize($$$) +{ + my ($self, $t, $name, $varname) = @_; + my $sizevar; + + if (my $flags = has_property($t, "flag")) { + $self->pidl("flags |= $flags;"); + } + + $self->pidl("return ndr_size_union($varname, flags, level, (ndr_push_flags_fn_t)ndr_push_$name);"); +} + +sub ParseUnionPushPrimitives($$$$) +{ + my ($self, $e, $ndr ,$varname) = @_; + + my $have_default = 0; + + $self->pidl("uint32_t level = ndr_push_get_switch_value($ndr, $varname);"); + + if (defined($e->{SWITCH_TYPE})) { + if (defined($e->{ALIGN})) { + $self->pidl("NDR_CHECK(ndr_push_union_align($ndr, $e->{ALIGN}));"); + } + + $self->pidl("NDR_CHECK(ndr_push_$e->{SWITCH_TYPE}($ndr, NDR_SCALARS, level));"); + } + + if (defined($e->{ALIGN})) { + if ($e->{IS_MS_UNION}) { + $self->pidl("/* ms_union is always aligned to the largest union arm*/"); + $self->pidl("NDR_CHECK(ndr_push_align($ndr, $e->{ALIGN}));"); + } else { + $self->pidl("NDR_CHECK(ndr_push_union_align($ndr, $e->{ALIGN}));"); + } + } + + $self->pidl("switch (level) {"); + $self->indent; + foreach my $el (@{$e->{ELEMENTS}}) { + if ($el->{CASE} eq "default") { + $have_default = 1; + } + $self->pidl("$el->{CASE}: {"); + + if ($el->{TYPE} ne "EMPTY") { + $self->indent; + if (defined($e->{PROPERTIES}{relative_base})) { + $self->pidl("NDR_CHECK(ndr_push_align($ndr, $el->{ALIGN}));"); + # set the current offset as base for relative pointers + # and store it based on the toplevel struct/union + $self->pidl("NDR_CHECK(ndr_push_setup_relative_base_offset1($ndr, $varname, $ndr->offset));"); + } + $self->DeclareArrayVariables($el); + my $el_env = {$el->{NAME} => "$varname->$el->{NAME}"}; + $self->CheckRefPtrs($el, $ndr, $el_env); + $self->ParseElementPush($el, $ndr, $el_env, 1, 0); + $self->deindent; + } + $self->pidl("break; }"); + $self->pidl(""); + } + if (! $have_default) { + $self->pidl("default:"); + $self->pidl("\treturn ndr_push_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u at \%s\", level, __location__);"); + } + $self->deindent; + $self->pidl("}"); +} + +sub ParseUnionPushDeferred($$$$) +{ + my ($self,$e,$ndr,$varname) = @_; + + my $have_default = 0; + + $self->pidl("uint32_t level = ndr_push_get_switch_value($ndr, $varname);"); + if (defined($e->{PROPERTIES}{relative_base})) { + # retrieve the current offset as base for relative pointers + # based on the toplevel struct/union + $self->pidl("NDR_CHECK(ndr_push_setup_relative_base_offset2($ndr, $varname));"); + } + $self->pidl("switch (level) {"); + $self->indent; + foreach my $el (@{$e->{ELEMENTS}}) { + if ($el->{CASE} eq "default") { + $have_default = 1; + } + + $self->pidl("$el->{CASE}:"); + if ($el->{TYPE} ne "EMPTY") { + $self->indent; + $self->ParseElementPush($el, $ndr, {$el->{NAME} => "$varname->$el->{NAME}"}, 0, 1); + $self->deindent; + } + $self->pidl("break;"); + $self->pidl(""); + } + if (! $have_default) { + $self->pidl("default:"); + $self->pidl("\treturn ndr_push_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u at \%s\", level, __location__);"); + } + $self->deindent; + $self->pidl("}"); +} + +##################################################################### +# parse a union - push side +sub ParseUnionPush($$$$) +{ + my ($self,$e,$ndr,$varname) = @_; + my $have_default = 0; + + $self->start_flags($e, $ndr); + + $self->pidl("NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);"); + $self->pidl("if (ndr_flags & NDR_SCALARS) {"); + $self->indent; + $self->ParseUnionPushPrimitives($e, $ndr, $varname); + $self->deindent; + $self->pidl("}"); + if (is_deferred_switch_non_empty($e)) { + $self->pidl("if (ndr_flags & NDR_BUFFERS) {"); + $self->indent; + $self->ParseUnionPushDeferred($e, $ndr, $varname); + $self->deindent; + $self->pidl("}"); + } + $self->end_flags($e, $ndr); +} + +##################################################################### +# print a union +sub ParseUnionPrint($$$$$) +{ + my ($self,$e,$ndr,$name,$varname) = @_; + my $have_default = 0; + + $self->pidl("uint32_t level;"); + foreach my $el (@{$e->{ELEMENTS}}) { + $self->DeclareArrayVariables($el); + } + + $self->start_flags($e, $ndr); + + $self->pidl("level = ndr_print_get_switch_value($ndr, $varname);"); + + $self->pidl("ndr_print_union($ndr, name, level, \"$name\");"); + + $self->pidl("switch (level) {"); + $self->indent; + foreach my $el (@{$e->{ELEMENTS}}) { + if ($el->{CASE} eq "default") { + $have_default = 1; + } + $self->pidl("$el->{CASE}:"); + if ($el->{TYPE} ne "EMPTY") { + $self->indent; + $self->ParseElementPrint($el, $ndr, "$varname->$el->{NAME}", {}); + $self->deindent; + } + $self->pidl("break;"); + $self->pidl(""); + } + if (! $have_default) { + $self->pidl("default:"); + $self->pidl("\tndr_print_bad_level($ndr, name, level);"); + } + $self->deindent; + $self->pidl("}"); + + $self->end_flags($e, $ndr); +} + +sub ParseUnionPullPrimitives($$$$$) +{ + my ($self,$e,$ndr,$varname,$switch_type) = @_; + my $have_default = 0; + + + if (defined($switch_type)) { + if (defined($e->{ALIGN})) { + $self->pidl("NDR_CHECK(ndr_pull_union_align($ndr, $e->{ALIGN}));"); + } + + $self->pidl("NDR_CHECK(ndr_pull_$switch_type($ndr, NDR_SCALARS, &_level));"); + $self->pidl("if (_level != level) {"); + $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value %u for $varname at \%s\", _level, __location__);"); + $self->pidl("}"); + } + + if (defined($e->{ALIGN})) { + if ($e->{IS_MS_UNION}) { + $self->pidl("/* ms_union is always aligned to the largest union arm*/"); + $self->pidl("NDR_CHECK(ndr_pull_align($ndr, $e->{ALIGN}));"); + } else { + $self->pidl("NDR_CHECK(ndr_pull_union_align($ndr, $e->{ALIGN}));"); + } + } + + $self->pidl("switch (level) {"); + $self->indent; + foreach my $el (@{$e->{ELEMENTS}}) { + if ($el->{CASE} eq "default") { + $have_default = 1; + } + $self->pidl("$el->{CASE}: {"); + + if ($el->{TYPE} ne "EMPTY") { + $self->indent; + if (defined($e->{PROPERTIES}{relative_base})) { + $self->pidl("NDR_CHECK(ndr_pull_align($ndr, $el->{ALIGN}));"); + # set the current offset as base for relative pointers + # and store it based on the toplevel struct/union + $self->pidl("NDR_CHECK(ndr_pull_setup_relative_base_offset1($ndr, $varname, $ndr->offset));"); + } + $self->ParseElementPull($el, $ndr, {$el->{NAME} => "$varname->$el->{NAME}"}, 1, 0); + $self->deindent; + } + $self->pidl("break; }"); + $self->pidl(""); + } + if (! $have_default) { + $self->pidl("default:"); + $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u at \%s\", level, __location__);"); + } + $self->deindent; + $self->pidl("}"); +} + +sub ParseUnionPullDeferred($$$$) +{ + my ($self,$e,$ndr,$varname) = @_; + my $have_default = 0; + + if (defined($e->{PROPERTIES}{relative_base})) { + # retrieve the current offset as base for relative pointers + # based on the toplevel struct/union + $self->pidl("NDR_CHECK(ndr_pull_setup_relative_base_offset2($ndr, $varname));"); + } + $self->pidl("switch (level) {"); + $self->indent; + foreach my $el (@{$e->{ELEMENTS}}) { + if ($el->{CASE} eq "default") { + $have_default = 1; + } + + $self->pidl("$el->{CASE}:"); + if ($el->{TYPE} ne "EMPTY") { + $self->indent; + $self->ParseElementPull($el, $ndr, {$el->{NAME} => "$varname->$el->{NAME}"}, 0, 1); + $self->deindent; + } + $self->pidl("break;"); + $self->pidl(""); + } + if (! $have_default) { + $self->pidl("default:"); + $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u at \%s\", level, __location__);"); + } + $self->deindent; + $self->pidl("}"); + + +} + +##################################################################### +# parse a union - pull side +sub ParseUnionPull($$$$) +{ + my ($self,$e,$ndr,$varname) = @_; + my $switch_type = $e->{SWITCH_TYPE}; + my $needs_deferred_switch = is_deferred_switch_non_empty($e); + $self->pidl("uint32_t level;"); + if (defined($switch_type)) { + if (Parse::Pidl::Typelist::typeIs($switch_type, "ENUM")) { + $switch_type = Parse::Pidl::Typelist::enum_type_fn(getType($switch_type)->{DATA}); + } + $self->pidl(mapTypeName($switch_type) . " _level;"); + } + + my %double_cases = (); + foreach my $el (@{$e->{ELEMENTS}}) { + next if ($el->{TYPE} eq "EMPTY"); + next if ($double_cases{"$el->{NAME}"}); + $self->DeclareMemCtxVariables($el); + $self->DeclarePtrVariables($el); + $self->DeclareArrayVariables($el, "pull"); + $double_cases{"$el->{NAME}"} = 1; + } + + $self->start_flags($e, $ndr); + + $self->pidl("NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);"); + $self->pidl("if (ndr_flags & NDR_SCALARS) {"); + $self->indent; + if (! $needs_deferred_switch) { + $self->pidl("/* This token is not used again */"); + $self->pidl("level = ndr_pull_steal_switch_value($ndr, $varname);"); + } else { + $self->pidl("level = ndr_pull_get_switch_value($ndr, $varname);"); + } + $self->ParseUnionPullPrimitives($e,$ndr,$varname,$switch_type); + $self->deindent; + $self->pidl("}"); + if ($needs_deferred_switch) { + $self->pidl("if (ndr_flags & NDR_BUFFERS) {"); + $self->indent; + $self->pidl("/* The token is not needed after this. */"); + $self->pidl("level = ndr_pull_steal_switch_value($ndr, $varname);"); + $self->ParseUnionPullDeferred($e,$ndr,$varname); + $self->deindent; + $self->pidl("}"); + } + $self->add_deferred(); + + $self->end_flags($e, $ndr); +} + +sub DeclUnion($$$$) +{ + my ($e,$t,$name,$varname) = @_; + return ($t ne "pull"?"const ":"") . "union $name *$varname"; +} + +sub ArgsUnionNdrSize($$) +{ + my ($d,$name) = @_; + return "const union $name *r, uint32_t level, int flags"; +} + +$typefamily{UNION} = { + PUSH_FN_BODY => \&ParseUnionPush, + DECL => \&DeclUnion, + PULL_FN_BODY => \&ParseUnionPull, + PRINT_FN_BODY => \&ParseUnionPrint, + SIZE_FN_ARGS => \&ArgsUnionNdrSize, + SIZE_FN_BODY => \&ParseUnionNdrSize, +}; + +##################################################################### +# parse a typedef - push side +sub ParseTypedefPush($$$$) +{ + my($self,$e,$ndr,$varname) = @_; + + my $env; + + $env->{$e->{NAME}} = $varname; + + $self->ParseElementPushLevel($e, $e->{LEVELS}[0], $ndr, $varname, $env, 1, 1); +} + +##################################################################### +# parse a typedef - pull side +sub ParseTypedefPull($$$$) +{ + my($self,$e,$ndr,$varname) = @_; + + my $env; + + $env->{$e->{NAME}} = $varname; + + $self->ParseElementPullLevel($e, $e->{LEVELS}[0], $ndr, $varname, $env, 1, 1); +} + +##################################################################### +# parse a typedef - print side +sub ParseTypedefPrint($$$$$) +{ + my($self,$e,$ndr,$name,$varname) = @_; + + $typefamily{$e->{DATA}->{TYPE}}->{PRINT_FN_BODY}->($self, $e->{DATA}, $ndr, $name, $varname); +} + +##################################################################### +## calculate the size of a structure +sub ParseTypedefNdrSize($$$$) +{ + my($self,$t,$name,$varname) = @_; + + $typefamily{$t->{DATA}->{TYPE}}->{SIZE_FN_BODY}->($self, $t->{DATA}, $name, $varname); +} + +sub DeclTypedef($$$$) +{ + my ($e, $t, $name, $varname) = @_; + + return $typefamily{$e->{DATA}->{TYPE}}->{DECL}->($e->{DATA}, $t, $name, $varname); +} + +sub ArgsTypedefNdrSize($$$) +{ + my ($d, $name, $varname) = @_; + return $typefamily{$d->{DATA}->{TYPE}}->{SIZE_FN_ARGS}->($d->{DATA}, $name, $varname); +} + +$typefamily{TYPEDEF} = { + PUSH_FN_BODY => \&ParseTypedefPush, + DECL => \&DeclTypedef, + PULL_FN_BODY => \&ParseTypedefPull, + PRINT_FN_BODY => \&ParseTypedefPrint, + SIZE_FN_ARGS => \&ArgsTypedefNdrSize, + SIZE_FN_BODY => \&ParseTypedefNdrSize, +}; + +sub ParsePipePushChunk($$) +{ + my ($self, $t) = @_; + + my $pipe = $t; + $pipe = $t->{DATA} if ($t->{TYPE} eq "TYPEDEF"); + my $struct = $pipe->{DATA}; + + my $name = "$struct->{NAME}"; + my $ndr = "ndr"; + my $varname = "r"; + + my $args = $typefamily{$struct->{TYPE}}->{DECL}->($struct, "push", $name, $varname); + + $self->fn_declare("push", $struct, "enum ndr_err_code ndr_push_$name(struct ndr_push *$ndr, int ndr_flags, $args)") or return; + + return if has_property($t, "nopush"); + + $self->pidl("{"); + $self->indent; + + $self->ParseStructPush($struct, $ndr, $varname); + $self->pidl(""); + + $self->pidl("NDR_CHECK(ndr_push_pipe_chunk_trailer(ndr, ndr_flags, $varname->count));"); + $self->pidl(""); + + $self->pidl("return NDR_ERR_SUCCESS;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); +} + +sub ParsePipePullChunk($$) +{ + my ($self, $t) = @_; + + my $pipe = $t; + $pipe = $t->{DATA} if ($t->{TYPE} eq "TYPEDEF"); + my $struct = $pipe->{DATA}; + + my $name = "$struct->{NAME}"; + my $ndr = "ndr"; + my $varname = "r"; + + my $args = $typefamily{$struct->{TYPE}}->{DECL}->($struct, "pull", $name, $varname); + + $self->fn_declare("pull", $struct, "enum ndr_err_code ndr_pull_$name(struct ndr_pull *$ndr, int ndr_flags, $args)") or return; + + return if has_property($struct, "nopull"); + + $self->pidl("{"); + $self->indent; + + $self->ParseStructPull($struct, $ndr, $varname); + $self->pidl(""); + + $self->pidl("NDR_CHECK(ndr_check_pipe_chunk_trailer($ndr, ndr_flags, $varname->count));"); + $self->pidl(""); + + $self->pidl("return NDR_ERR_SUCCESS;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); +} + +sub ParsePipePrintChunk($$) +{ + my ($self, $t) = @_; + + my $pipe = $t; + $pipe = $t->{DATA} if ($t->{TYPE} eq "TYPEDEF"); + my $struct = $pipe->{DATA}; + + my $name = "$struct->{NAME}"; + my $ndr = "ndr"; + my $varname = "r"; + + my $args = $typefamily{$struct->{TYPE}}->{DECL}->($struct, "print", $name, $varname); + + $self->pidl_hdr("void ndr_print_$name(struct ndr_print *ndr, const char *name, $args);"); + + return if (has_property($t, "noprint")); + + $self->pidl("_PUBLIC_ void ndr_print_$name(struct ndr_print *$ndr, const char *name, $args)"); + $self->pidl("{"); + $self->indent; + $self->ParseTypePrint($struct, $ndr, $varname); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); +} + +##################################################################### +# parse a function - print side +sub ParseFunctionPrint($$) +{ + my($self, $fn) = @_; + my $ndr = "ndr"; + + $self->pidl_hdr("void ndr_print_$fn->{NAME}(struct ndr_print *$ndr, const char *name, int flags, const struct $fn->{NAME} *r);"); + + return if has_property($fn, "noprint"); + + $self->pidl("_PUBLIC_ void ndr_print_$fn->{NAME}(struct ndr_print *$ndr, const char *name, int flags, const struct $fn->{NAME} *r)"); + $self->pidl("{"); + $self->indent; + + foreach my $e (@{$fn->{ELEMENTS}}) { + $self->DeclareArrayVariables($e); + } + + $self->pidl("ndr_print_struct($ndr, name, \"$fn->{NAME}\");"); + $self->pidl("if (r == NULL) { ndr_print_null($ndr); return; }"); + $self->pidl("$ndr->depth++;"); + + $self->pidl("if (flags & NDR_SET_VALUES) {"); + $self->pidl("\t$ndr->flags |= LIBNDR_PRINT_SET_VALUES;"); + $self->pidl("}"); + + $self->pidl("if (flags & NDR_IN) {"); + $self->indent; + $self->pidl("ndr_print_struct($ndr, \"in\", \"$fn->{NAME}\");"); + $self->pidl("$ndr->depth++;"); + + my $env = GenerateFunctionInEnv($fn); + + foreach my $e (@{$fn->{ELEMENTS}}) { + if (grep(/in/,@{$e->{DIRECTION}})) { + $self->ParseElementPrint($e, $ndr, $env->{$e->{NAME}}, $env); + } + } + $self->pidl("$ndr->depth--;"); + $self->deindent; + $self->pidl("}"); + + $self->pidl("if (flags & NDR_OUT) {"); + $self->indent; + $self->pidl("ndr_print_struct($ndr, \"out\", \"$fn->{NAME}\");"); + $self->pidl("$ndr->depth++;"); + + $env = GenerateFunctionOutEnv($fn); + foreach my $e (@{$fn->{ELEMENTS}}) { + if (grep(/out/,@{$e->{DIRECTION}})) { + $self->ParseElementPrint($e, $ndr, $env->{$e->{NAME}}, $env); + } + } + if ($fn->{RETURN_TYPE}) { + $self->pidl("ndr_print_$fn->{RETURN_TYPE}($ndr, \"result\", r->out.result);"); + } + $self->pidl("$ndr->depth--;"); + $self->deindent; + $self->pidl("}"); + + $self->pidl("$ndr->depth--;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); +} + +##################################################################### +# parse a function +sub ParseFunctionPush($$) +{ + my($self, $fn) = @_; + my $ndr = "ndr"; + + $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; + + return if has_property($fn, "nopush"); + + $self->pidl("{"); + $self->indent; + + foreach my $e (@{$fn->{ELEMENTS}}) { + $self->DeclareArrayVariables($e); + } + + $self->pidl("NDR_PUSH_CHECK_FN_FLAGS(ndr, flags);"); + + $self->pidl("if (flags & NDR_IN) {"); + $self->indent; + + my $env = GenerateFunctionInEnv($fn); + + EnvSubstituteValue($env, $fn); + + foreach my $e (@{$fn->{ELEMENTS}}) { + if (grep(/in/,@{$e->{DIRECTION}})) { + $self->CheckRefPtrs($e, $ndr, $env); + } + } + + foreach my $e (@{$fn->{ELEMENTS}}) { + if (grep(/in/,@{$e->{DIRECTION}})) { + $self->ParseElementPush($e, $ndr, $env, 1, 1); + } + } + + $self->deindent; + $self->pidl("}"); + + $self->pidl("if (flags & NDR_OUT) {"); + $self->indent; + + $env = GenerateFunctionOutEnv($fn); + EnvSubstituteValue($env, $fn); + + foreach my $e (@{$fn->{ELEMENTS}}) { + if (grep(/out/,@{$e->{DIRECTION}})) { + $self->CheckRefPtrs($e, $ndr, $env); + } + } + + foreach my $e (@{$fn->{ELEMENTS}}) { + if (grep(/out/,@{$e->{DIRECTION}})) { + $self->ParseElementPush($e, $ndr, $env, 1, 1); + } + } + + if ($fn->{RETURN_TYPE}) { + $self->pidl("NDR_CHECK(ndr_push_$fn->{RETURN_TYPE}($ndr, NDR_SCALARS, r->out.result));"); + } + + $self->deindent; + $self->pidl("}"); + $self->pidl("return NDR_ERR_SUCCESS;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); +} + +sub AllocateArrayLevel($$$$$$) +{ + my ($self,$e,$l,$ndr,$var,$size) = @_; + + my $pl = GetPrevLevel($e, $l); + if (defined($pl) and + $pl->{TYPE} eq "POINTER" and + $pl->{POINTER_TYPE} eq "ref" + and not $l->{IS_ZERO_TERMINATED}) { + $self->pidl("if ($ndr->flags & LIBNDR_FLAG_REF_ALLOC) {"); + $self->pidl("\tNDR_PULL_ALLOC_N($ndr, $var, $size);"); + $self->pidl("}"); + if (grep(/in/,@{$e->{DIRECTION}}) and + grep(/out/,@{$e->{DIRECTION}})) { + $self->pidl("memcpy(r->out.$e->{NAME}, r->in.$e->{NAME}, ($size) * sizeof(*r->in.$e->{NAME}));"); + } + return; + } + + $self->pidl("NDR_PULL_ALLOC_N($ndr, $var, $size);"); +} + +##################################################################### +# parse a function +sub ParseFunctionPull($$) +{ + my($self,$fn) = @_; + my $ndr = "ndr"; + + # pull function args + $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; + + $self->pidl("{"); + $self->indent; + + # declare any internal pointers we need + foreach my $e (@{$fn->{ELEMENTS}}) { + $self->DeclarePtrVariables($e); + $self->DeclareArrayVariables($e, "pull"); + } + + my %double_cases = (); + foreach my $e (@{$fn->{ELEMENTS}}) { + next if ($e->{TYPE} eq "EMPTY"); + next if ($double_cases{"$e->{NAME}"}); + $self->DeclareMemCtxVariables($e); + $double_cases{"$e->{NAME}"} = 1; + } + + $self->pidl("NDR_PULL_CHECK_FN_FLAGS(ndr, flags);"); + + $self->pidl("if (flags & NDR_IN) {"); + $self->indent; + + # auto-init the out section of a structure. I originally argued that + # this was a bad idea as it hides bugs, but coping correctly + # with initialisation and not wiping ref vars is turning + # out to be too tricky (tridge) + foreach my $e (@{$fn->{ELEMENTS}}) { + next unless grep(/out/, @{$e->{DIRECTION}}); + $self->pidl("ZERO_STRUCT(r->out);"); + $self->pidl(""); + last; + } + + my $env = GenerateFunctionInEnv($fn); + + foreach my $e (@{$fn->{ELEMENTS}}) { + next unless (grep(/in/, @{$e->{DIRECTION}})); + $self->ParseElementPull($e, $ndr, $env, 1, 1); + } + + # allocate the "simple" out ref variables. FIXME: Shouldn't this have it's + # own flag rather than be in NDR_IN ? + + foreach my $e (@{$fn->{ELEMENTS}}) { + next unless (grep(/out/, @{$e->{DIRECTION}})); + next unless ($e->{LEVELS}[0]->{TYPE} eq "POINTER" and + $e->{LEVELS}[0]->{POINTER_TYPE} eq "ref"); + next if (($e->{LEVELS}[1]->{TYPE} eq "DATA") and + ($e->{LEVELS}[1]->{DATA_TYPE} eq "string")); + next if ($e->{LEVELS}[1]->{TYPE} eq "PIPE"); + next if (($e->{LEVELS}[1]->{TYPE} eq "ARRAY") + and $e->{LEVELS}[1]->{IS_ZERO_TERMINATED}); + + if ($e->{LEVELS}[1]->{TYPE} eq "ARRAY") { + my $size = ParseExprExt($e->{LEVELS}[1]->{SIZE_IS}, $env, $e->{ORIGINAL}, + check_null_pointer($e, $env, sub { $self->pidl(shift); }, + "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for size_is()\");"), + check_fully_dereferenced($e, $env)); + $self->pidl("NDR_PULL_ALLOC_N($ndr, r->out.$e->{NAME}, $size);"); + + if (grep(/in/, @{$e->{DIRECTION}})) { + $self->pidl("memcpy(r->out.$e->{NAME}, r->in.$e->{NAME}, ($size) * sizeof(*r->in.$e->{NAME}));"); + } else { + $self->pidl("memset(r->out.$e->{NAME}, 0, ($size) * sizeof(*r->out.$e->{NAME}));"); + } + } elsif ($e->{LEVELS}[1]->{TYPE} eq "ARRAY") { + if (grep(/in/, @{$e->{DIRECTION}})) { + $self->pidl("r->out.$e->{NAME} = r->in.$e->{NAME};"); + } else { + $self->pidl("r->out.$e->{NAME} = NULL;"); + } + } else { + $self->pidl("NDR_PULL_ALLOC($ndr, r->out.$e->{NAME});"); + + if (grep(/in/, @{$e->{DIRECTION}})) { + $self->pidl("*r->out.$e->{NAME} = *r->in.$e->{NAME};"); + } else { + $self->pidl("ZERO_STRUCTP(r->out.$e->{NAME});"); + } + } + } + + $self->add_deferred(); + $self->deindent; + $self->pidl("}"); + + $self->pidl("if (flags & NDR_OUT) {"); + $self->indent; + + $env = GenerateFunctionOutEnv($fn); + foreach my $e (@{$fn->{ELEMENTS}}) { + next unless grep(/out/, @{$e->{DIRECTION}}); + $self->ParseElementPull($e, $ndr, $env, 1, 1); + } + + if ($fn->{RETURN_TYPE}) { + $self->pidl("NDR_CHECK(ndr_pull_$fn->{RETURN_TYPE}($ndr, NDR_SCALARS, &r->out.result));"); + } + + $self->add_deferred(); + $self->deindent; + $self->pidl("}"); + + $self->pidl("return NDR_ERR_SUCCESS;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); +} + +sub AuthServiceStruct($$$) +{ + my ($self, $ifacename, $authservice) = @_; + my @a = split /,/, $authservice; + my $authservice_count = $#a + 1; + + $self->pidl("static const char * const $ifacename\_authservice_strings[] = {"); + foreach my $ap (@a) { + $self->pidl("\t$ap, "); + } + $self->pidl("};"); + $self->pidl(""); + + $self->pidl("static const struct ndr_interface_string_array $ifacename\_authservices = {"); + $self->pidl("\t.count\t= $authservice_count,"); + $self->pidl("\t.names\t= $ifacename\_authservice_strings"); + $self->pidl("};"); + $self->pidl(""); +} + +sub ParseGeneratePipeArray($$$) +{ + my ($self, $fn, $direction) = @_; + + $self->pidl("static const struct ndr_interface_call_pipe $fn->{NAME}\_$direction\_pipes[] = {"); + $self->indent; + + foreach my $e (@{$fn->{ELEMENTS}}) { + next unless ContainsPipe($e, $e->{LEVELS}[0]); + next unless (grep(/$direction/, @{$e->{DIRECTION}})); + + my $cname = "$e->{TYPE}_chunk"; + + $self->pidl("{"); + $self->indent; + $self->pidl("\"$direction.$e->{NAME}\","); + $self->pidl("\"$cname\","); + $self->pidl("sizeof(struct $cname),"); + $self->pidl("(ndr_push_flags_fn_t) ndr_push_$cname,"); + $self->pidl("(ndr_pull_flags_fn_t) ndr_pull_$cname,"); + $self->pidl("(ndr_print_fn_t) ndr_print_$cname,"); + $self->deindent; + $self->pidl("},"); + } + $self->pidl("{ NULL, NULL, 0, NULL, NULL, NULL }"); + $self->deindent; + $self->pidl("};"); + $self->pidl(""); +} + +sub FunctionCallPipes($$) +{ + my ($self, $d) = @_; + return if not defined($d->{OPNUM}); + + my $in_pipes = 0; + my $out_pipes = 0; + + foreach my $e (@{$d->{ELEMENTS}}) { + next unless ContainsPipe($e, $e->{LEVELS}[0]); + + if (grep(/in/, @{$e->{DIRECTION}})) { + $in_pipes++; + } + if (grep(/out/, @{$e->{DIRECTION}})) { + $out_pipes++; + } + } + + if ($in_pipes) { + $self->ParseGeneratePipeArray($d, "in"); + } + + if ($out_pipes) { + $self->ParseGeneratePipeArray($d, "out"); + } +} + +sub FunctionCallEntry($$) +{ + my ($self, $d) = @_; + return 0 if not defined($d->{OPNUM}); + + my $in_pipes = 0; + my $out_pipes = 0; + + foreach my $e (@{$d->{ELEMENTS}}) { + next unless ContainsPipe($e, $e->{LEVELS}[0]); + + if (grep(/in/, @{$e->{DIRECTION}})) { + $in_pipes++; + } + if (grep(/out/, @{$e->{DIRECTION}})) { + $out_pipes++; + } + } + + my $in_pipes_ptr = "NULL"; + my $out_pipes_ptr = "NULL"; + + if ($in_pipes) { + $in_pipes_ptr = "$d->{NAME}_in_pipes"; + } + + if ($out_pipes) { + $out_pipes_ptr = "$d->{NAME}_out_pipes"; + } + + $self->pidl("\t{"); + $self->pidl("\t\t\"$d->{NAME}\","); + $self->pidl("\t\tsizeof(struct $d->{NAME}),"); + $self->pidl("\t\t(ndr_push_flags_fn_t) ndr_push_$d->{NAME},"); + $self->pidl("\t\t(ndr_pull_flags_fn_t) ndr_pull_$d->{NAME},"); + $self->pidl("\t\t(ndr_print_function_t) ndr_print_$d->{NAME},"); + $self->pidl("\t\t{ $in_pipes, $in_pipes_ptr },"); + $self->pidl("\t\t{ $out_pipes, $out_pipes_ptr },"); + $self->pidl("\t},"); + return 1; +} + +##################################################################### +# produce a function call table +sub FunctionTable($$) +{ + my($self,$interface) = @_; + my $count = 0; + my $uname = uc $interface->{NAME}; + + return if ($#{$interface->{FUNCTIONS}}+1 == 0); + return unless defined ($interface->{PROPERTIES}->{uuid}); + + foreach my $d (@{$interface->{INHERITED_FUNCTIONS}},@{$interface->{FUNCTIONS}}) { + $self->FunctionCallPipes($d); + } + + $self->pidl("static const struct ndr_interface_call $interface->{NAME}\_calls[] = {"); + + foreach my $d (@{$interface->{INHERITED_FUNCTIONS}},@{$interface->{FUNCTIONS}}) { + $count += $self->FunctionCallEntry($d); + } + $self->pidl("\t{ NULL, 0, NULL, NULL, NULL }"); + $self->pidl("};"); + $self->pidl(""); + + $self->pidl("static const char * const $interface->{NAME}\_endpoint_strings[] = {"); + foreach my $ep (@{$interface->{ENDPOINTS}}) { + $self->pidl("\t$ep, "); + } + my $endpoint_count = $#{$interface->{ENDPOINTS}}+1; + + $self->pidl("};"); + $self->pidl(""); + + $self->pidl("static const struct ndr_interface_string_array $interface->{NAME}\_endpoints = {"); + $self->pidl("\t.count\t= $endpoint_count,"); + $self->pidl("\t.names\t= $interface->{NAME}\_endpoint_strings"); + $self->pidl("};"); + $self->pidl(""); + + if (! defined $interface->{PROPERTIES}->{authservice}) { + $interface->{PROPERTIES}->{authservice} = "\"host\""; + } + + $self->AuthServiceStruct($interface->{NAME}, + $interface->{PROPERTIES}->{authservice}); + + $self->pidl("\nconst struct ndr_interface_table ndr_table_$interface->{NAME} = {"); + $self->pidl("\t.name\t\t= \"$interface->{NAME}\","); + $self->pidl("\t.syntax_id\t= {"); + $self->pidl("\t\t" . print_uuid($interface->{UUID}) .","); + $self->pidl("\t\tNDR_$uname\_VERSION"); + $self->pidl("\t},"); + $self->pidl("\t.helpstring\t= NDR_$uname\_HELPSTRING,"); + $self->pidl("\t.num_calls\t= $count,"); + $self->pidl("\t.calls\t\t= $interface->{NAME}\_calls,"); + $self->pidl("\t.endpoints\t= &$interface->{NAME}\_endpoints,"); + $self->pidl("\t.authservices\t= &$interface->{NAME}\_authservices"); + $self->pidl("};"); + $self->pidl(""); + +} + +##################################################################### +# generate include statements for imported idl files +sub HeaderImport +{ + my $self = shift; + my @imports = @_; + foreach (@imports) { + $_ = unmake_str($_); + s/\.idl$//; + $self->pidl(choose_header("librpc/gen_ndr/ndr_$_\.h", "gen_ndr/ndr_$_.h")); + } +} + +##################################################################### +# generate include statements for included header files +sub HeaderInclude +{ + my $self = shift; + my @includes = @_; + foreach (@includes) { + $self->pidl_hdr("#include $_"); + } +} + +##################################################################### +# generate prototypes and defines for the interface definitions +# FIXME: these prototypes are for the DCE/RPC client functions, not the +# NDR parser and so do not belong here, technically speaking +sub HeaderInterface($$$) +{ + my($self,$interface,$needed) = @_; + + my $count = 0; + + if ($needed->{"compression"}) { + $self->pidl(choose_header("librpc/ndr/ndr_compression.h", "ndr/compression.h")); + } + + if (has_property($interface, "object")) { + $self->pidl(choose_header("librpc/gen_ndr/ndr_orpc.h", "ndr/orpc.h")); + } + + if (defined $interface->{PROPERTIES}->{helper}) { + $self->HeaderInclude(split /,/, $interface->{PROPERTIES}->{helper}); + } + + if (defined $interface->{PROPERTIES}->{uuid}) { + my $name = uc $interface->{NAME}; + $self->pidl_hdr("#define NDR_$name\_UUID " . + Parse::Pidl::Util::make_str(lc($interface->{UUID}))); + + $self->pidl_hdr("#define NDR_$name\_VERSION $interface->{VERSION}"); + + $self->pidl_hdr("#define NDR_$name\_NAME \"$interface->{NAME}\""); + + if(!defined $interface->{PROPERTIES}->{helpstring}) { $interface->{PROPERTIES}->{helpstring} = "NULL"; } + $self->pidl_hdr("#define NDR_$name\_HELPSTRING $interface->{PROPERTIES}->{helpstring}"); + + $self->pidl_hdr("extern const struct ndr_interface_table ndr_table_$interface->{NAME};"); + } + + foreach (@{$interface->{FUNCTIONS}}) { + next if has_property($_, "noopnum"); + next if grep(/^$_->{NAME}$/,@{$interface->{INHERITED_FUNCTIONS}}); + my $u_name = uc $_->{NAME}; + + my $val = sprintf("0x%02x", $count); + if (defined($interface->{BASE})) { + $val .= " + NDR_" . uc $interface->{BASE} . "_CALL_COUNT"; + } + + $self->pidl_hdr("#define NDR_$u_name ($val)"); + + $self->pidl_hdr(""); + $count++; + } + + my $val = $count; + + if (defined($interface->{BASE})) { + $val .= " + NDR_" . uc $interface->{BASE} . "_CALL_COUNT"; + } + + $self->pidl_hdr("#define NDR_" . uc $interface->{NAME} . "_CALL_COUNT ($val)"); + +} + +sub ParseTypePush($$$$$$) +{ + my ($self,$e, $ndr, $varname, $primitives, $deferred) = @_; + + # save the old relative_base_offset + $self->pidl("uint32_t _save_relative_base_offset = ndr_push_get_relative_base_offset($ndr);") if defined(has_property($e, "relative_base")); + $typefamily{$e->{TYPE}}->{PUSH_FN_BODY}->($self, $e, $ndr, $varname); + # restore the old relative_base_offset + $self->pidl("ndr_push_restore_relative_base_offset($ndr, _save_relative_base_offset);") if defined(has_property($e, "relative_base")); +} + +sub ParseTypePushFunction($$$) +{ + my ($self, $e, $varname) = @_; + my $ndr = "ndr"; + + my $args = $typefamily{$e->{TYPE}}->{DECL}->($e, "push", $e->{NAME}, $varname); + + $self->fn_declare("push", $e, "enum ndr_err_code ".TypeFunctionName("ndr_push", $e)."(struct ndr_push *$ndr, int ndr_flags, $args)") or return; + + $self->pidl("{"); + $self->indent; + $self->ParseTypePush($e, $ndr, $varname, 1, 1); + $self->pidl("return NDR_ERR_SUCCESS;"); + $self->deindent; + $self->pidl("}"); + $self->pidl("");; +} + +sub ParseTypePull($$$$$$) +{ + my ($self, $e, $ndr, $varname, $primitives, $deferred) = @_; + + # save the old relative_base_offset + $self->pidl("uint32_t _save_relative_base_offset = ndr_pull_get_relative_base_offset($ndr);") if defined(has_property($e, "relative_base")); + $typefamily{$e->{TYPE}}->{PULL_FN_BODY}->($self, $e, $ndr, $varname); + # restore the old relative_base_offset + $self->pidl("ndr_pull_restore_relative_base_offset($ndr, _save_relative_base_offset);") if defined(has_property($e, "relative_base")); +} + +sub ParseTypePullFunction($$) +{ + my ($self, $e, $varname) = @_; + my $ndr = "ndr"; + + my $args = $typefamily{$e->{TYPE}}->{DECL}->($e, "pull", $e->{NAME}, $varname); + + $self->fn_declare("pull", $e, "enum ndr_err_code ".TypeFunctionName("ndr_pull", $e)."(struct ndr_pull *$ndr, int ndr_flags, $args)") or return; + + $self->pidl("{"); + $self->indent; + $self->ParseTypePull($e, $ndr, $varname, 1, 1); + $self->pidl("return NDR_ERR_SUCCESS;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); +} + +sub ParseTypePrint($$$$) +{ + my ($self, $e, $ndr, $varname) = @_; + + $typefamily{$e->{TYPE}}->{PRINT_FN_BODY}->($self, $e, $ndr, $e->{NAME}, $varname); +} + +sub ParseTypePrintFunction($$$) +{ + my ($self, $e, $varname) = @_; + my $ndr = "ndr"; + + my $args = $typefamily{$e->{TYPE}}->{DECL}->($e, "print", $e->{NAME}, $varname); + + $self->pidl_hdr("void ".TypeFunctionName("ndr_print", $e)."(struct ndr_print *ndr, const char *name, $args);"); + + return if (has_property($e, "noprint")); + + $self->pidl("_PUBLIC_ void ".TypeFunctionName("ndr_print", $e)."(struct ndr_print *$ndr, const char *name, $args)"); + $self->pidl("{"); + $self->indent; + $self->ParseTypePrint($e, $ndr, $varname); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); +} + +sub ParseTypeNdrSize($$) +{ + my ($self,$t) = @_; + + my $varname = "r"; + my $tf = $typefamily{$t->{TYPE}}; + my $args = $tf->{SIZE_FN_ARGS}->($t, $t->{NAME}, $varname); + + $self->fn_declare("size", $t, "size_t ndr_size_$t->{NAME}($args)") or return; + + $self->pidl("{"); + $self->indent; + $typefamily{$t->{TYPE}}->{SIZE_FN_BODY}->($self,$t, $t->{NAME}, $varname); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); +} + +##################################################################### +# parse the interface definitions +sub ParseInterface($$$) +{ + my($self,$interface,$needed) = @_; + + $self->pidl_hdr("#ifndef _HEADER_NDR_$interface->{NAME}"); + $self->pidl_hdr("#define _HEADER_NDR_$interface->{NAME}"); + + $self->pidl_hdr(""); + + $self->HeaderInterface($interface, $needed); + + # Typedefs + foreach my $d (@{$interface->{TYPES}}) { + if (Parse::Pidl::Typelist::typeIs($d, "PIPE")) { + ($needed->{TypeFunctionName("ndr_push", $d)}) && + $self->ParsePipePushChunk($d); + ($needed->{TypeFunctionName("ndr_pull", $d)}) && + $self->ParsePipePullChunk($d); + ($needed->{TypeFunctionName("ndr_print", $d)}) && + $self->ParsePipePrintChunk($d); + + $needed->{TypeFunctionName("ndr_pull", $d)} = 0; + $needed->{TypeFunctionName("ndr_push", $d)} = 0; + $needed->{TypeFunctionName("ndr_print", $d)} = 0; + next; + } + + next unless(typeHasBody($d)); + + ($needed->{TypeFunctionName("ndr_push", $d)}) && $self->ParseTypePushFunction($d, "r"); + ($needed->{TypeFunctionName("ndr_pull", $d)}) && $self->ParseTypePullFunction($d, "r"); + ($needed->{TypeFunctionName("ndr_print", $d)}) && $self->ParseTypePrintFunction($d, "r"); + + # Make sure we don't generate a function twice... + $needed->{TypeFunctionName("ndr_push", $d)} = + $needed->{TypeFunctionName("ndr_pull", $d)} = + $needed->{TypeFunctionName("ndr_print", $d)} = 0; + + ($needed->{"ndr_size_$d->{NAME}"}) && $self->ParseTypeNdrSize($d); + } + + # Functions + foreach my $d (@{$interface->{FUNCTIONS}}) { + ($needed->{"ndr_push_$d->{NAME}"}) && $self->ParseFunctionPush($d); + ($needed->{"ndr_pull_$d->{NAME}"}) && $self->ParseFunctionPull($d); + ($needed->{"ndr_print_$d->{NAME}"}) && $self->ParseFunctionPrint($d); + } + + $self->FunctionTable($interface); + + $self->pidl_hdr("#endif /* _HEADER_NDR_$interface->{NAME} */"); +} + +sub GenerateIncludes($) +{ + my ($self) = @_; + if (is_intree()) { + $self->pidl("#include \"includes.h\""); + } else { + $self->pidl("#ifndef _GNU_SOURCE"); + $self->pidl("#define _GNU_SOURCE"); + $self->pidl("#endif"); + $self->pidl("#include <stdint.h>"); + $self->pidl("#include <stdlib.h>"); + $self->pidl("#include <stdio.h>"); + $self->pidl("#include <stdbool.h>"); + $self->pidl("#include <stdarg.h>"); + $self->pidl("#include <string.h>"); + } +} + +##################################################################### +# parse a parsed IDL structure back into an IDL file +sub Parse($$$$) +{ + my($self, $ndr,$gen_header,$ndr_header) = @_; + + $self->pidl_hdr("/* header auto-generated by pidl */"); + $self->pidl_hdr(""); + $self->pidl_hdr(choose_header("librpc/ndr/libndr.h", "ndr.h")); + $self->pidl_hdr("#include \"$gen_header\"") if ($gen_header); + $self->pidl_hdr(""); + + $self->pidl("/* parser auto-generated by pidl */"); + $self->pidl(""); + $self->GenerateIncludes(); + $self->pidl("#include \"$ndr_header\"") if ($ndr_header); + $self->pidl(""); + + my %needed = (); + + foreach (@{$ndr}) { + ($_->{TYPE} eq "INTERFACE") && NeededInterface($_, \%needed); + } + + foreach (@{$ndr}) { + ($_->{TYPE} eq "INTERFACE") && $self->ParseInterface($_, \%needed); + ($_->{TYPE} eq "IMPORT") && $self->HeaderImport(@{$_->{PATHS}}); + ($_->{TYPE} eq "INCLUDE") && $self->HeaderInclude(@{$_->{PATHS}}); + } + + return ($self->{res_hdr}, $self->{res}); +} + +sub NeededElement($$$) +{ + my ($e, $dir, $needed) = @_; + + return if ($e->{TYPE} eq "EMPTY"); + + return if (ref($e->{TYPE}) eq "HASH" and + not defined($e->{TYPE}->{NAME})); + + my ($t, $rt); + if (ref($e->{TYPE}) eq "HASH") { + $t = $e->{TYPE}->{TYPE}."_".$e->{TYPE}->{NAME}; + } else { + $t = $e->{TYPE}; + } + + if (ref($e->{REPRESENTATION_TYPE}) eq "HASH") { + $rt = $e->{REPRESENTATION_TYPE}->{TYPE}."_".$e->{REPRESENTATION_TYPE}->{NAME}; + } else { + $rt = $e->{REPRESENTATION_TYPE}; + } + + die ("$e->{NAME} $t, $rt FOO") unless ($rt ne ""); + + my @fn = (); + if ($dir eq "print") { + push(@fn, TypeFunctionName("ndr_print", $e->{REPRESENTATION_TYPE})); + } elsif ($dir eq "pull") { + push (@fn, TypeFunctionName("ndr_pull", $e->{TYPE})); + push (@fn, "ndr_$t\_to_$rt") + if ($rt ne $t); + } elsif ($dir eq "push") { + push (@fn, TypeFunctionName("ndr_push", $e->{TYPE})); + push (@fn, "ndr_$rt\_to_$t") + if ($rt ne $t); + } else { + die("invalid direction `$dir'"); + } + + foreach (@fn) { + unless (defined($needed->{$_})) { + $needed->{$_} = 1; + } + } +} + +sub NeededFunction($$) +{ + my ($fn,$needed) = @_; + $needed->{"ndr_pull_$fn->{NAME}"} = 1; + $needed->{"ndr_push_$fn->{NAME}"} = 1; + $needed->{"ndr_print_$fn->{NAME}"} = 1; + foreach my $e (@{$fn->{ELEMENTS}}) { + $e->{PARENT} = $fn; + NeededElement($e, $_, $needed) foreach ("pull", "push", "print"); + } +} + +sub NeededType($$$) +{ + sub NeededType($$$); + my ($t,$needed,$req) = @_; + + NeededType($t->{DATA}, $needed, $req) if ($t->{TYPE} eq "TYPEDEF"); + NeededType($t->{DATA}, $needed, $req) if ($t->{TYPE} eq "PIPE"); + + if ($t->{TYPE} eq "STRUCT" or $t->{TYPE} eq "UNION") { + return unless defined($t->{ELEMENTS}); + for my $e (@{$t->{ELEMENTS}}) { + $e->{PARENT} = $t; + if (has_property($e, "compression")) { + $needed->{"compression"} = 1; + } + NeededElement($e, $req, $needed); + NeededType($e->{TYPE}, $needed, $req) if (ref($e->{TYPE}) eq "HASH"); + } + } +} + +##################################################################### +# work out what parse functions are needed +sub NeededInterface($$) +{ + my ($interface,$needed) = @_; + NeededFunction($_, $needed) foreach (@{$interface->{FUNCTIONS}}); + foreach (reverse @{$interface->{TYPES}}) { + + if (has_property($_, "public")) { + $needed->{TypeFunctionName("ndr_pull", $_)} = $needed->{TypeFunctionName("ndr_push", $_)} = + $needed->{TypeFunctionName("ndr_print", $_)} = 1; + } + + NeededType($_, $needed, "pull") if ($needed->{TypeFunctionName("ndr_pull", $_)}); + NeededType($_, $needed, "push") if ($needed->{TypeFunctionName("ndr_push", $_)}); + NeededType($_, $needed, "print") if ($needed->{TypeFunctionName("ndr_print", $_)}); + if (has_property($_, "gensize")) { + $needed->{"ndr_size_$_->{NAME}"} = 1; + } + } +} + +sub TypeFunctionName($$) +{ + my ($prefix, $t) = @_; + + return "$prefix\_$t->{NAME}" if (ref($t) eq "HASH" and + $t->{TYPE} eq "TYPEDEF"); + return "$prefix\_$t->{TYPE}_$t->{NAME}" if (ref($t) eq "HASH"); + return "$prefix\_$t"; +} + +1; diff --git a/tools/pidl/lib/Parse/Pidl/Samba4/NDR/Server.pm b/tools/pidl/lib/Parse/Pidl/Samba4/NDR/Server.pm new file mode 100644 index 00000000..ad36f000 --- /dev/null +++ b/tools/pidl/lib/Parse/Pidl/Samba4/NDR/Server.pm @@ -0,0 +1,342 @@ +################################################### +# server boilerplate generator +# Copyright tridge@samba.org 2003 +# Copyright metze@samba.org 2004 +# released under the GNU GPL + +package Parse::Pidl::Samba4::NDR::Server; + +use strict; +use Parse::Pidl::Util; + +use vars qw($VERSION); +$VERSION = '0.01'; + +my($res); + +sub pidl($) +{ + $res .= shift; +} + + +##################################################### +# generate the switch statement for function dispatch +sub gen_dispatch_switch($) +{ + my $interface = shift; + + foreach my $fn (@{$interface->{FUNCTIONS}}) { + next if not defined($fn->{OPNUM}); + + pidl "\tcase $fn->{OPNUM}: {\n"; + pidl "\t\tstruct $fn->{NAME} *r2 = (struct $fn->{NAME} *)r;\n"; + pidl "\t\tif (DEBUGLEVEL >= 10) {\n"; + pidl "\t\t\tNDR_PRINT_FUNCTION_DEBUG($fn->{NAME}, NDR_IN, r2);\n"; + pidl "\t\t}\n"; + if ($fn->{RETURN_TYPE} && $fn->{RETURN_TYPE} ne "void") { + pidl "\t\tr2->out.result = dcesrv_$fn->{NAME}(dce_call, mem_ctx, r2);\n"; + } else { + pidl "\t\tdcesrv_$fn->{NAME}(dce_call, mem_ctx, r2);\n"; + } + pidl "\t\tif (dce_call->state_flags & DCESRV_CALL_STATE_FLAG_ASYNC) {\n"; + pidl "\t\t\tDEBUG(5,(\"function $fn->{NAME} will reply async\\n\"));\n"; + pidl "\t\t}\n"; + pidl "\t\tbreak;\n\t}\n"; + } +} + +##################################################### +# generate the switch statement for function reply +sub gen_reply_switch($) +{ + my $interface = shift; + + foreach my $fn (@{$interface->{FUNCTIONS}}) { + next if not defined($fn->{OPNUM}); + + pidl "\tcase $fn->{OPNUM}: {\n"; + pidl "\t\tstruct $fn->{NAME} *r2 = (struct $fn->{NAME} *)r;\n"; + pidl "\t\tif (dce_call->state_flags & DCESRV_CALL_STATE_FLAG_ASYNC) {\n"; + pidl "\t\t\tDEBUG(5,(\"function $fn->{NAME} replied async\\n\"));\n"; + pidl "\t\t}\n"; + pidl "\t\tif (DEBUGLEVEL >= 10 && dce_call->fault_code == 0) {\n"; + pidl "\t\t\tNDR_PRINT_FUNCTION_DEBUG($fn->{NAME}, NDR_OUT | NDR_SET_VALUES, r2);\n"; + pidl "\t\t}\n"; + pidl "\t\tif (dce_call->fault_code != 0) {\n"; + pidl "\t\t\tDEBUG(2,(\"dcerpc_fault %s in $fn->{NAME}\\n\", dcerpc_errstr(mem_ctx, dce_call->fault_code)));\n"; + pidl "\t\t}\n"; + pidl "\t\tbreak;\n\t}\n"; + } +} + +##################################################################### +# produce boilerplate code for a interface +sub Boilerplate_Iface($) +{ + my($interface) = shift; + my $name = $interface->{NAME}; + my $uname = uc $name; + my $uuid = lc($interface->{UUID}); + my $if_version = $interface->{VERSION}; + + pidl " +static NTSTATUS $name\__op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface, uint32_t if_version) +{ +#ifdef DCESRV_INTERFACE_$uname\_BIND + return DCESRV_INTERFACE_$uname\_BIND(dce_call,iface); +#else + return NT_STATUS_OK; +#endif +} + +static void $name\__op_unbind(struct dcesrv_connection_context *context, const struct dcesrv_interface *iface) +{ +#ifdef DCESRV_INTERFACE_$uname\_UNBIND + DCESRV_INTERFACE_$uname\_UNBIND(context, iface); +#else + return; +#endif +} + +static NTSTATUS $name\__op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r) +{ + enum ndr_err_code ndr_err; + uint16_t opnum = dce_call->pkt.u.request.opnum; + + dce_call->fault_code = 0; + + if (opnum >= ndr_table_$name.num_calls) { + dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR; + return NT_STATUS_NET_WRITE_FAULT; + } + + *r = talloc_named(mem_ctx, + ndr_table_$name.calls[opnum].struct_size, + \"struct %s\", + ndr_table_$name.calls[opnum].name); + NT_STATUS_HAVE_NO_MEMORY(*r); + + /* unravel the NDR for the packet */ + ndr_err = ndr_table_$name.calls[opnum].ndr_pull(pull, NDR_IN, *r); + if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) { + dcerpc_log_packet(dce_call->conn->packet_log_dir, + &ndr_table_$name, opnum, NDR_IN, + &dce_call->pkt.u.request.stub_and_verifier); + dce_call->fault_code = DCERPC_FAULT_NDR; + return NT_STATUS_NET_WRITE_FAULT; + } + + return NT_STATUS_OK; +} + +static NTSTATUS $name\__op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r) +{ + uint16_t opnum = dce_call->pkt.u.request.opnum; + + switch (opnum) { +"; + gen_dispatch_switch($interface); + +pidl " + default: + dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR; + break; + } + + if (dce_call->fault_code != 0) { + dcerpc_log_packet(dce_call->conn->packet_log_dir, + &ndr_table_$name, opnum, NDR_IN, + &dce_call->pkt.u.request.stub_and_verifier); + return NT_STATUS_NET_WRITE_FAULT; + } + + return NT_STATUS_OK; +} + +static NTSTATUS $name\__op_reply(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r) +{ + uint16_t opnum = dce_call->pkt.u.request.opnum; + + switch (opnum) { +"; + gen_reply_switch($interface); + +pidl " + default: + dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR; + break; + } + + if (dce_call->fault_code != 0) { + dcerpc_log_packet(dce_call->conn->packet_log_dir, + &ndr_table_$name, opnum, NDR_IN, + &dce_call->pkt.u.request.stub_and_verifier); + return NT_STATUS_NET_WRITE_FAULT; + } + + return NT_STATUS_OK; +} + +static NTSTATUS $name\__op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r) +{ + enum ndr_err_code ndr_err; + uint16_t opnum = dce_call->pkt.u.request.opnum; + + ndr_err = ndr_table_$name.calls[opnum].ndr_push(push, NDR_OUT, r); + if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) { + dce_call->fault_code = DCERPC_FAULT_NDR; + return NT_STATUS_NET_WRITE_FAULT; + } + + return NT_STATUS_OK; +} + +static const struct dcesrv_interface dcesrv\_$name\_interface = { + .name = \"$name\", + .syntax_id = {".print_uuid($uuid).",$if_version}, + .bind = $name\__op_bind, + .unbind = $name\__op_unbind, + .ndr_pull = $name\__op_ndr_pull, + .dispatch = $name\__op_dispatch, + .reply = $name\__op_reply, + .ndr_push = $name\__op_ndr_push, +#ifdef DCESRV_INTERFACE_$uname\_FLAGS + .flags = DCESRV_INTERFACE_$uname\_FLAGS +#else + .flags = 0 +#endif +}; + +"; +} + +##################################################################### +# produce boilerplate code for an endpoint server +sub Boilerplate_Ep_Server($) +{ + my($interface) = shift; + my $name = $interface->{NAME}; + my $uname = uc $name; + + pidl " +static NTSTATUS $name\__op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server) +{ + int i; + + for (i=0;i<ndr_table_$name.endpoints->count;i++) { + NTSTATUS ret; + const char *name = ndr_table_$name.endpoints->names[i]; + + ret = dcesrv_interface_register(dce_ctx, name, &dcesrv_$name\_interface, NULL); + if (!NT_STATUS_IS_OK(ret)) { + DEBUG(1,(\"$name\_op_init_server: failed to register endpoint \'%s\'\\n\",name)); + return ret; + } + } + + return NT_STATUS_OK; +} + +static bool $name\__op_interface_by_uuid(struct dcesrv_interface *iface, const struct GUID *uuid, uint32_t if_version) +{ + if (dcesrv_$name\_interface.syntax_id.if_version == if_version && + GUID_equal(\&dcesrv\_$name\_interface.syntax_id.uuid, uuid)) { + memcpy(iface,&dcesrv\_$name\_interface, sizeof(*iface)); + return true; + } + + return false; +} + +static bool $name\__op_interface_by_name(struct dcesrv_interface *iface, const char *name) +{ + if (strcmp(dcesrv_$name\_interface.name, name)==0) { + memcpy(iface, &dcesrv_$name\_interface, sizeof(*iface)); + return true; + } + + return false; +} + +NTSTATUS dcerpc_server_$name\_init(TALLOC_CTX *ctx) +{ + NTSTATUS ret; + static const struct dcesrv_endpoint_server ep_server = { + /* fill in our name */ + .name = \"$name\", + + /* fill in all the operations */ +#ifdef DCESRV_INTERFACE_$uname\_INIT_SERVER + .init_server = DCESRV_INTERFACE_$uname\_INIT_SERVER, +#else + .init_server = $name\__op_init_server, +#endif + .interface_by_uuid = $name\__op_interface_by_uuid, + .interface_by_name = $name\__op_interface_by_name + }; + /* register ourselves with the DCERPC subsystem. */ + ret = dcerpc_register_ep_server(&ep_server); + + if (!NT_STATUS_IS_OK(ret)) { + DEBUG(0,(\"Failed to register \'$name\' endpoint server!\\n\")); + return ret; + } + + return ret; +} + +"; +} + +##################################################################### +# dcerpc server boilerplate from a parsed IDL structure +sub ParseInterface($) +{ + my($interface) = shift; + my $count = 0; + + $res .= "NTSTATUS dcerpc_server_$interface->{NAME}\_init(TALLOC_CTX *);\n"; + $res .= "\n"; + + if (!defined $interface->{PROPERTIES}->{uuid}) { + return $res; + } + + if (!defined $interface->{PROPERTIES}->{version}) { + $interface->{PROPERTIES}->{version} = "0.0"; + } + + foreach my $fn (@{$interface->{FUNCTIONS}}) { + if (defined($fn->{OPNUM})) { $count++; } + } + + if ($count == 0) { + return $res; + } + + $res .= "/* $interface->{NAME} - dcerpc server boilerplate generated by pidl */\n\n"; + Boilerplate_Iface($interface); + Boilerplate_Ep_Server($interface); + + return $res; +} + +sub Parse($$) +{ + my($ndr,$header) = @_; + + $res = ""; + $res .= "/* server functions auto-generated by pidl */\n"; + $res .= "#include \"$header\"\n"; + $res .= "#include <util/debug.h>\n"; + $res .= "\n"; + + foreach my $x (@{$ndr}) { + ParseInterface($x) if ($x->{TYPE} eq "INTERFACE" and not defined($x->{PROPERTIES}{object})); + } + + return $res; +} + +1; diff --git a/tools/pidl/lib/Parse/Pidl/Samba4/Python.pm b/tools/pidl/lib/Parse/Pidl/Samba4/Python.pm new file mode 100644 index 00000000..f418ac48 --- /dev/null +++ b/tools/pidl/lib/Parse/Pidl/Samba4/Python.pm @@ -0,0 +1,2425 @@ +################################################### +# Python function wrapper generator +# Copyright jelmer@samba.org 2007-2008 +# released under the GNU GPL + +package Parse::Pidl::Samba4::Python; + +use Exporter; +@ISA = qw(Exporter); + +use strict; +use Parse::Pidl qw(warning fatal error); +use Parse::Pidl::Typelist qw(hasType resolveType getType mapTypeName expandAlias bitmap_type_fn enum_type_fn); +use Parse::Pidl::Util qw(has_property ParseExpr unmake_str); +use Parse::Pidl::NDR qw(ReturnTypeElement GetPrevLevel GetNextLevel ContainsDeferred ContainsPipe is_charset_array); +use Parse::Pidl::CUtil qw(get_value_of get_pointer_to); +use Parse::Pidl::Samba4 qw(ArrayDynamicallyAllocated); +use Parse::Pidl::Samba4::Header qw(GenerateFunctionInEnv GenerateFunctionOutEnv EnvSubstituteValue GenerateStructEnv); + +use vars qw($VERSION); +$VERSION = '0.01'; + +sub new($) { + my ($class) = @_; + my $self = { res => "", res_hdr => "", tabs => "", + constants => [], constants_uniq => {}, + module_methods => [], + module_objects => [], module_objects_uniq => {}, + ready_types => [], + module_imports => [], module_imports_uniq => {}, + type_imports => [], type_imports_uniq => {}, + patch_type_calls => [], prereadycode => [], + postreadycode => []}; + bless($self, $class); +} + +sub pidl_hdr ($$) +{ + my $self = shift; + $self->{res_hdr} .= shift; +} + +sub pidl($$) +{ + my ($self, $d) = @_; + if ($d) { + if ((!($d =~ /^#/))) { + $self->{res} .= $self->{tabs}; + } + $self->{res} .= $d; + } + $self->{res} .= "\n"; +} + +sub indent($) +{ + my ($self) = @_; + $self->{tabs} .= "\t"; +} + +sub deindent($) +{ + my ($self) = @_; + $self->{tabs} = substr($self->{tabs}, 0, -1); +} + +sub PrettifyTypeName($$) +{ + my ($name, $basename) = @_; + + $basename =~ s/^.*\.([^.]+)$/\1/; + + $name =~ s/^$basename\_//; + + + return $name; +} + +sub Import +{ + my $self = shift; + my @imports = @_; + foreach (@imports) { + $_ = unmake_str($_); + s/\.idl$//; + $self->pidl_hdr("#include \"librpc/gen_ndr/$_\.h\"\n"); + $self->register_module_import("samba.dcerpc.$_"); + } +} + +sub Const($$) +{ + my ($self, $const) = @_; + $self->register_constant($const->{NAME}, $const->{DTYPE}, $const->{VALUE}); +} + +sub register_constant($$$$) +{ + my ($self, $name, $type, $value) = @_; + + unless (defined $self->{constants_uniq}->{$name}) { + my $h = {"key" => $name, "val" => [$type, $value]}; + push @{$self->{constants}}, $h; + $self->{constants_uniq}->{$name} = $h; + } +} + +sub EnumAndBitmapConsts($$$) +{ + my ($self, $name, $d) = @_; + + return unless (defined($d->{ELEMENTS})); + + foreach my $e (@{$d->{ELEMENTS}}) { + $e =~ /^([A-Za-z0-9_]+)/; + my $cname = $1; + + $self->register_constant($cname, $d, $cname); + } +} + +sub FromUnionToPythonFunction($$$$) +{ + my ($self, $mem_ctx, $type, $switch, $name) = @_; + + $self->pidl("PyObject *ret;"); + $self->pidl(""); + + $self->pidl("switch ($switch) {"); + $self->indent; + + foreach my $e (@{$type->{ELEMENTS}}) { + $self->pidl("$e->{CASE}:"); + + $self->indent; + + if ($e->{NAME}) { + $self->ConvertObjectToPython($mem_ctx, {}, $e, "$name->$e->{NAME}", "ret", "return NULL;"); + } else { + $self->pidl("ret = Py_None;"); + $self->pidl("Py_INCREF(ret);"); + } + + $self->pidl("return ret;"); + $self->pidl(""); + + $self->deindent; + } + + $self->deindent; + $self->pidl("}"); + + $self->pidl("PyErr_SetString(PyExc_TypeError, \"unknown union level\");"); + $self->pidl("return NULL;"); +} + +sub FromPythonToUnionFunction($$$$$) +{ + my ($self, $type, $typename, $switch, $mem_ctx, $name) = @_; + + my $has_default = 0; + + $self->pidl("$typename *ret = talloc_zero($mem_ctx, $typename);"); + + $self->pidl("switch ($switch) {"); + $self->indent; + + foreach my $e (@{$type->{ELEMENTS}}) { + $self->pidl("$e->{CASE}:"); + if ($e->{CASE} eq "default") { $has_default = 1; } + $self->indent; + if ($e->{NAME}) { + $self->ConvertObjectFromPython({}, $mem_ctx, $e, $name, "ret->$e->{NAME}", "talloc_free(ret); return NULL;"); + } + $self->pidl("break;"); + $self->deindent; + $self->pidl(""); + } + + if (!$has_default) { + $self->pidl("default:"); + $self->indent; + $self->pidl("PyErr_SetString(PyExc_TypeError, \"invalid union level value\");"); + $self->pidl("talloc_free(ret);"); + $self->pidl("ret = NULL;"); + $self->deindent; + } + + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + $self->pidl("return ret;"); +} + +sub PythonElementGetSet($$$$$$) { + my ($self, $name, $cname, $ename, $e, $env) = @_; + + my $varname = "object->$ename"; + $self->pidl("static PyObject *py_$name\_get_$e->{NAME}(PyObject *obj, void *closure)"); + $self->pidl("{"); + $self->indent; + $self->pidl("$cname *object = ($cname *)pytalloc_get_ptr(obj);"); + $self->pidl("PyObject *py_$e->{NAME};"); + $self->ConvertObjectToPython("pytalloc_get_mem_ctx(obj)", $env, $e, $varname, "py_$e->{NAME}", "return NULL;"); + $self->pidl("return py_$e->{NAME};"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $self->pidl("static int py_$name\_set_$e->{NAME}(PyObject *py_obj, PyObject *value, void *closure)"); + $self->pidl("{"); + $self->indent; + $self->pidl("$cname *object = ($cname *)pytalloc_get_ptr(py_obj);"); + my $mem_ctx = "pytalloc_get_mem_ctx(py_obj)"; + my $l = $e->{LEVELS}[0]; + my $nl = GetNextLevel($e, $l); + if ($l->{TYPE} eq "POINTER" and + not ($nl->{TYPE} eq "ARRAY" and ($nl->{IS_FIXED} or is_charset_array($e, $nl))) and + not ($nl->{TYPE} eq "DATA" and Parse::Pidl::Typelist::scalar_is_reference($nl->{DATA_TYPE}))) { + $self->pidl("talloc_unlink($mem_ctx, discard_const($varname));"); + } + $self->ConvertObjectFromPython($env, $mem_ctx, $e, "value", $varname, "return -1;"); + $self->pidl("return 0;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); +} + +sub PythonStruct($$$$$$) +{ + my ($self, $modulename, $prettyname, $name, $cname, $d) = @_; + + my $env = GenerateStructEnv($d, "object"); + + $self->pidl(""); + + my $getsetters = "NULL"; + + if ($#{$d->{ELEMENTS}} > -1) { + foreach my $e (@{$d->{ELEMENTS}}) { + $self->PythonElementGetSet($name, $cname, $e->{NAME}, $e, $env); + } + + $getsetters = "py_$name\_getsetters"; + $self->pidl("static PyGetSetDef ".$getsetters."[] = {"); + $self->indent; + foreach my $e (@{$d->{ELEMENTS}}) { + my $etype = ""; + if (ref($e->{TYPE}) eq "HASH") { + $etype = $e->{TYPE}->{NAME}; + } else { + $etype = $e->{TYPE}; + } + $self->pidl("{"); + $self->indent; + $self->pidl(".name = discard_const_p(char, \"$e->{NAME}\"),"); + $self->pidl(".get = py_$name\_get_$e->{NAME},"); + $self->pidl(".set = py_$name\_set_$e->{NAME},"); + $self->pidl(".doc = discard_const_p(char, \"PIDL-generated element of base type $etype\")"); + $self->deindent; + $self->pidl("},"); + } + $self->pidl("{ .name = NULL }"); + $self->deindent; + $self->pidl("};"); + $self->pidl(""); + } + + $self->pidl("static PyObject *py_$name\_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)"); + $self->pidl("{"); + $self->indent; + $self->pidl("return pytalloc_new($cname, type);"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + my $py_methods = "NULL"; + + # If the struct is not public there ndr_pull/ndr_push functions will + # be static so not callable from here + if (has_property($d, "public")) { + $self->pidl("static PyObject *py_$name\_ndr_pack(PyObject *py_obj)"); + $self->pidl("{"); + $self->indent; + $self->pidl("$cname *object = ($cname *)pytalloc_get_ptr(py_obj);"); + $self->pidl("PyObject *ret = NULL;"); + $self->pidl("DATA_BLOB blob;"); + $self->pidl("enum ndr_err_code err;"); + $self->pidl("TALLOC_CTX *tmp_ctx = talloc_new(pytalloc_get_mem_ctx(py_obj));"); + $self->pidl("if (tmp_ctx == NULL) {"); + $self->indent; + $self->pidl("PyErr_SetNdrError(NDR_ERR_ALLOC);"); + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl("err = ndr_push_struct_blob(&blob, tmp_ctx, object, (ndr_push_flags_fn_t)ndr_push_$name);"); + $self->pidl("if (!NDR_ERR_CODE_IS_SUCCESS(err)) {"); + $self->indent; + $self->pidl("TALLOC_FREE(tmp_ctx);"); + $self->pidl("PyErr_SetNdrError(err);"); + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + $self->pidl("ret = PyBytes_FromStringAndSize((char *)blob.data, blob.length);"); + $self->pidl("TALLOC_FREE(tmp_ctx);"); + $self->pidl("return ret;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $self->pidl("static PyObject *py_$name\_ndr_unpack(PyObject *py_obj, PyObject *args, PyObject *kwargs)"); + $self->pidl("{"); + $self->indent; + $self->pidl("$cname *object = ($cname *)pytalloc_get_ptr(py_obj);"); + $self->pidl("DATA_BLOB blob;"); + $self->pidl("Py_ssize_t blob_length = 0;"); + $self->pidl("enum ndr_err_code err;"); + $self->pidl("const char * const kwnames[] = { \"data_blob\", \"allow_remaining\", NULL };"); + $self->pidl("PyObject *allow_remaining_obj = NULL;"); + $self->pidl("bool allow_remaining = false;"); + $self->pidl(""); + $self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, PYARG_BYTES_LEN \"|O:__ndr_unpack__\","); + $self->indent; + $self->pidl("discard_const_p(char *, kwnames),"); + $self->pidl("&blob.data, &blob_length,"); + $self->pidl("&allow_remaining_obj)) {"); + $self->deindent; + $self->indent; + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl("blob.length = blob_length;"); + $self->pidl(""); + $self->pidl("if (allow_remaining_obj && PyObject_IsTrue(allow_remaining_obj)) {"); + $self->indent; + $self->pidl("allow_remaining = true;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + $self->pidl("if (allow_remaining) {"); + $self->indent; + $self->pidl("err = ndr_pull_struct_blob(&blob, pytalloc_get_mem_ctx(py_obj), object, (ndr_pull_flags_fn_t)ndr_pull_$name);"); + $self->deindent; + $self->pidl("} else {"); + $self->indent; + $self->pidl("err = ndr_pull_struct_blob_all(&blob, pytalloc_get_mem_ctx(py_obj), object, (ndr_pull_flags_fn_t)ndr_pull_$name);"); + $self->deindent; + $self->pidl("}"); + $self->pidl("if (!NDR_ERR_CODE_IS_SUCCESS(err)) {"); + $self->indent; + $self->pidl("PyErr_SetNdrError(err);"); + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + $self->pidl("Py_RETURN_NONE;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $self->pidl("static PyObject *py_$name\_ndr_print(PyObject *py_obj)"); + $self->pidl("{"); + $self->indent; + $self->pidl("$cname *object = ($cname *)pytalloc_get_ptr(py_obj);"); + $self->pidl("PyObject *ret;"); + $self->pidl("char *retstr;"); + $self->pidl(""); + $self->pidl("retstr = ndr_print_struct_string(pytalloc_get_mem_ctx(py_obj), (ndr_print_fn_t)ndr_print_$name, \"$name\", object);"); + $self->pidl("ret = PyStr_FromString(retstr);"); + $self->pidl("talloc_free(retstr);"); + $self->pidl(""); + $self->pidl("return ret;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $py_methods = "py_$name\_methods"; + $self->pidl("static PyMethodDef $py_methods\[] = {"); + $self->indent; + $self->pidl("{ \"__ndr_pack__\", (PyCFunction)py_$name\_ndr_pack, METH_NOARGS, \"S.ndr_pack(object) -> blob\\nNDR pack\" },"); + $self->pidl("{ \"__ndr_unpack__\", (PyCFunction)py_$name\_ndr_unpack, METH_VARARGS|METH_KEYWORDS, \"S.ndr_unpack(class, blob, allow_remaining=False) -> None\\nNDR unpack\" },"); + $self->pidl("{ \"__ndr_print__\", (PyCFunction)py_$name\_ndr_print, METH_NOARGS, \"S.ndr_print(object) -> None\\nNDR print\" },"); + $self->pidl("{ NULL, NULL, 0, NULL }"); + $self->deindent; + $self->pidl("};"); + $self->pidl(""); + } + + $self->pidl_hdr("static PyTypeObject $name\_Type;\n"); + $self->pidl(""); + my $docstring = $self->DocString($d, $name); + my $typeobject = "$name\_Type"; + $self->pidl("static PyTypeObject $typeobject = {"); + $self->indent; + $self->pidl("PyVarObject_HEAD_INIT(NULL, 0)"); + $self->pidl(".tp_name = \"$modulename.$prettyname\","); + $self->pidl(".tp_getset = $getsetters,"); + if ($docstring) { + $self->pidl(".tp_doc = $docstring,"); + } + $self->pidl(".tp_methods = $py_methods,"); + $self->pidl(".tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,"); + $self->pidl(".tp_new = py_$name\_new,"); + $self->deindent; + $self->pidl("};"); + + $self->pidl(""); + + my $talloc_typename = $self->import_type_variable("talloc", "BaseObject"); + $self->register_module_prereadycode(["$name\_Type.tp_base = $talloc_typename;", + "$name\_Type.tp_basicsize = pytalloc_BaseObject_size();", + ""]); + + return "&$typeobject"; +} + +sub PythonFunctionStruct($$$$) +{ + my ($self, $modulename, $fn, $iface, $prettyname) = @_; + + my $inenv = GenerateFunctionInEnv($fn, "object->"); + my $outenv = GenerateFunctionOutEnv($fn, "object->"); + + my $name = "$fn->{NAME}"; + my $cname = "struct $name"; + + $self->pidl(""); + + my $getsetters = "NULL"; + + foreach my $e (@{$fn->{ELEMENTS}}) { + if (grep(/in/,@{$e->{DIRECTION}})) { + my $inname = "$name\_in"; + my $ename = "in.$e->{NAME}"; + $self->PythonElementGetSet($inname, $cname, $ename, $e, $inenv); + } + if (grep(/out/,@{$e->{DIRECTION}})) { + my $outname = "$name\_out"; + my $ename = "out.$e->{NAME}"; + $self->PythonElementGetSet($outname, $cname, $ename, $e, $outenv); + } + } + + if (defined($fn->{RETURN_TYPE})) { + my $e = ReturnTypeElement($fn); + my $ename = "out.result"; + $self->PythonElementGetSet($name, $cname, $ename, $e, $outenv); + } + + $getsetters = "py_$name\_getsetters"; + $self->pidl("static PyGetSetDef ".$getsetters."[] = {"); + $self->indent; + foreach my $e (@{$fn->{ELEMENTS}}) { + if (grep(/in/,@{$e->{DIRECTION}})) { + $self->pidl("{"); + $self->indent; + $self->pidl(".name = discard_const_p(char, \"in_$e->{NAME}\"),"); + $self->pidl(".get = py_$name\_in_get_$e->{NAME},"); + $self->pidl(".set = py_$name\_in_set_$e->{NAME},"); + $self->pidl(".doc = discard_const_p(char, \"PIDL-generated element of base type $e->{TYPE}\")"); + $self->deindent; + $self->pidl("},"); + } + if (grep(/out/,@{$e->{DIRECTION}})) { + $self->pidl("{"); + $self->indent; + $self->pidl(".name = discard_const_p(char, \"out_$e->{NAME}\"),"); + $self->pidl(".get = py_$name\_out_get_$e->{NAME},"); + $self->pidl(".set = py_$name\_out_set_$e->{NAME},"); + $self->pidl(".doc = discard_const_p(char, \"PIDL-generated element of base type $e->{TYPE}\")"); + $self->deindent; + $self->pidl("},"); + } + } + if (defined($fn->{RETURN_TYPE})) { + $self->pidl("{"); + $self->indent; + $self->pidl(".name = discard_const_p(char, \"result\"),"); + $self->pidl(".get = py_$name\_get_result,"); + $self->pidl(".set = py_$name\_set_result,"); + $self->pidl(".doc = discard_const_p(char, \"PIDL-generated element of type $fn->{RETURN_TYPE}\")"); + $self->deindent; + $self->pidl("},"); + } + $self->pidl("{ .name = NULL }"); + $self->deindent; + $self->pidl("};"); + $self->pidl(""); + + $self->pidl("static PyObject *py_$name\_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)"); + $self->pidl("{"); + $self->indent; + $self->pidl("return pytalloc_new($cname, type);"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + my $py_methods = "NULL"; + + my $ndr_call = "const struct ndr_interface_call *call = NULL;"; + my $object_ptr = "$cname *object = ($cname *)pytalloc_get_ptr(py_obj);"; + + $self->pidl("static PyObject *py_$name\_ndr_opnum(PyTypeObject *type)"); + $self->pidl("{"); + $self->indent; + $self->pidl(""); + $self->pidl(""); + $self->pidl("return PyInt_FromLong($fn->{OPNUM});"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $self->pidl("static PyObject *py_$name\_ndr_pack(PyObject *py_obj, int ndr_inout_flags, uint32_t ndr_push_flags)"); + $self->pidl("{"); + $self->indent; + $self->pidl("$ndr_call"); + $self->pidl("$object_ptr"); + $self->pidl("PyObject *ret = NULL;"); + $self->pidl("struct ndr_push *push = NULL;"); + $self->pidl("DATA_BLOB blob;"); + $self->pidl("enum ndr_err_code err;"); + $self->pidl(""); + $self->pidl("if (ndr_table_$iface\.num_calls < " . ($fn->{OPNUM}+1) . + ") {"); + $self->indent; + $self->pidl("PyErr_SetString(PyExc_TypeError, \"Internal Error, ndr_interface_call missing for py_$name\_ndr_pack\");"); + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl("call = &ndr_table_$iface\.calls[$fn->{OPNUM}];"); + $self->pidl(""); + $self->pidl("push = ndr_push_init_ctx(pytalloc_get_mem_ctx(py_obj));"); + $self->pidl("if (push == NULL) {"); + $self->indent; + $self->pidl("PyErr_SetNdrError(NDR_ERR_ALLOC);"); + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + $self->pidl("push->flags |= ndr_push_flags;"); + $self->pidl(""); + $self->pidl("err = call->ndr_push(push, ndr_inout_flags, object);"); + $self->pidl("if (!NDR_ERR_CODE_IS_SUCCESS(err)) {"); + $self->indent; + $self->pidl("TALLOC_FREE(push);"); + $self->pidl("PyErr_SetNdrError(err);"); + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl("blob = ndr_push_blob(push);"); + $self->pidl("ret = PyBytes_FromStringAndSize((char *)blob.data, blob.length);"); + $self->pidl("TALLOC_FREE(push);"); + $self->pidl("return ret;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $self->pidl("static PyObject *py_$name\_ndr_pack_in(PyObject *py_obj, PyObject *args, PyObject *kwargs)"); + $self->pidl("{"); + $self->indent; + $self->pidl("const char * const kwnames[] = { \"bigendian\", \"ndr64\", NULL };"); + $self->pidl("PyObject *bigendian_obj = NULL;"); + $self->pidl("PyObject *ndr64_obj = NULL;"); + $self->pidl("uint32_t ndr_push_flags = 0;"); + $self->pidl(""); + $self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, \"|OO:__ndr_pack_in__\","); + $self->indent; + $self->pidl("discard_const_p(char *, kwnames),"); + $self->pidl("&bigendian_obj,"); + $self->pidl("&ndr64_obj)) {"); + $self->deindent; + $self->indent; + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + $self->pidl("if (bigendian_obj && PyObject_IsTrue(bigendian_obj)) {"); + $self->indent; + $self->pidl("ndr_push_flags |= LIBNDR_FLAG_BIGENDIAN;"); + $self->deindent; + $self->pidl("}"); + $self->pidl("if (ndr64_obj && PyObject_IsTrue(ndr64_obj)) {"); + $self->indent; + $self->pidl("ndr_push_flags |= LIBNDR_FLAG_NDR64;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + $self->pidl("return py_$name\_ndr_pack(py_obj, NDR_IN, ndr_push_flags);"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $self->pidl("static PyObject *py_$name\_ndr_pack_out(PyObject *py_obj, PyObject *args, PyObject *kwargs)"); + $self->pidl("{"); + $self->indent; + $self->pidl("const char * const kwnames[] = { \"bigendian\", \"ndr64\", NULL };"); + $self->pidl("PyObject *bigendian_obj = NULL;"); + $self->pidl("PyObject *ndr64_obj = NULL;"); + $self->pidl("uint32_t ndr_push_flags = 0;"); + $self->pidl(""); + $self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, \"|OO:__ndr_pack_out__\","); + $self->indent; + $self->pidl("discard_const_p(char *, kwnames),"); + $self->pidl("&bigendian_obj,"); + $self->pidl("&ndr64_obj)) {"); + $self->deindent; + $self->indent; + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + $self->pidl("if (bigendian_obj && PyObject_IsTrue(bigendian_obj)) {"); + $self->indent; + $self->pidl("ndr_push_flags |= LIBNDR_FLAG_BIGENDIAN;"); + $self->deindent; + $self->pidl("}"); + $self->pidl("if (ndr64_obj && PyObject_IsTrue(ndr64_obj)) {"); + $self->indent; + $self->pidl("ndr_push_flags |= LIBNDR_FLAG_NDR64;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + $self->pidl("return py_$name\_ndr_pack(py_obj, NDR_OUT, ndr_push_flags);"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $self->pidl("static PyObject *py_$name\_ndr_unpack(PyObject *py_obj, const DATA_BLOB *blob, int ndr_inout_flags, uint32_t ndr_pull_flags, bool allow_remaining)"); + $self->pidl("{"); + $self->indent; + $self->pidl("$ndr_call"); + $self->pidl("$object_ptr"); + $self->pidl("struct ndr_pull *pull = NULL;"); + $self->pidl("enum ndr_err_code err;"); + $self->pidl(""); + $self->pidl("if (ndr_table_$iface\.num_calls < " . ($fn->{OPNUM}+1) . + ") {"); + $self->indent; + $self->pidl("PyErr_SetString(PyExc_TypeError, \"Internal Error, ndr_interface_call missing for py_$name\_ndr_unpack\");"); + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl("call = &ndr_table_$iface\.calls[$fn->{OPNUM}];"); + $self->pidl(""); + $self->pidl("pull = ndr_pull_init_blob(blob, object);"); + $self->pidl("if (pull == NULL) {"); + $self->indent; + $self->pidl("PyErr_SetNdrError(NDR_ERR_ALLOC);"); + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + $self->pidl("pull->flags |= ndr_pull_flags;"); + $self->pidl(""); + $self->pidl("err = call->ndr_pull(pull, ndr_inout_flags, object);"); + $self->pidl("if (!NDR_ERR_CODE_IS_SUCCESS(err)) {"); + $self->indent; + $self->pidl("TALLOC_FREE(pull);"); + $self->pidl("PyErr_SetNdrError(err);"); + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl("if (!allow_remaining) {"); + $self->indent; + $self->pidl("uint32_t highest_ofs;"); + $self->pidl(""); + $self->pidl("if (pull->offset > pull->relative_highest_offset) {"); + $self->indent; + $self->pidl("highest_ofs = pull->offset;"); + $self->deindent; + $self->pidl("} else {"); + $self->indent; + $self->pidl("highest_ofs = pull->relative_highest_offset;"); + $self->deindent; + $self->pidl("}"); + $self->pidl("if (highest_ofs < pull->data_size) {"); + $self->indent; + $self->pidl("err = ndr_pull_error(pull, NDR_ERR_UNREAD_BYTES,"); + $self->indent; + $self->pidl("\"not all bytes consumed ofs[%u] size[%u]\","); + $self->pidl("highest_ofs, pull->data_size);"); + $self->deindent; + $self->pidl("TALLOC_FREE(pull);"); + $self->pidl("PyErr_SetNdrError(err);"); + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + $self->pidl("TALLOC_FREE(pull);"); + $self->pidl("Py_RETURN_NONE;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $self->pidl("static PyObject *py_$name\_ndr_unpack_in(PyObject *py_obj, PyObject *args, PyObject *kwargs)"); + $self->pidl("{"); + $self->indent; + $self->pidl("DATA_BLOB blob;"); + $self->pidl("Py_ssize_t blob_length = 0;"); + $self->pidl("const char * const kwnames[] = { \"data_blob\", \"bigendian\", \"ndr64\", \"allow_remaining\", NULL };"); + $self->pidl("PyObject *bigendian_obj = NULL;"); + $self->pidl("PyObject *ndr64_obj = NULL;"); + $self->pidl("uint32_t ndr_pull_flags = LIBNDR_FLAG_REF_ALLOC;"); + $self->pidl("PyObject *allow_remaining_obj = NULL;"); + $self->pidl("bool allow_remaining = false;"); + $self->pidl(""); + $self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, PYARG_BYTES_LEN \"|OOO:__ndr_unpack_in__\","); + $self->indent; + $self->pidl("discard_const_p(char *, kwnames),"); + $self->pidl("&blob.data, &blob_length,"); + $self->pidl("&bigendian_obj,"); + $self->pidl("&ndr64_obj,"); + $self->pidl("&allow_remaining_obj)) {"); + $self->deindent; + $self->indent; + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl("blob.length = blob_length;"); + $self->pidl(""); + $self->pidl("if (bigendian_obj && PyObject_IsTrue(bigendian_obj)) {"); + $self->indent; + $self->pidl("ndr_pull_flags |= LIBNDR_FLAG_BIGENDIAN;"); + $self->deindent; + $self->pidl("}"); + $self->pidl("if (ndr64_obj && PyObject_IsTrue(ndr64_obj)) {"); + $self->indent; + $self->pidl("ndr_pull_flags |= LIBNDR_FLAG_NDR64;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + $self->pidl("if (allow_remaining_obj && PyObject_IsTrue(allow_remaining_obj)) {"); + $self->indent; + $self->pidl("allow_remaining = true;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + $self->pidl("return py_$name\_ndr_unpack(py_obj, &blob, NDR_IN, ndr_pull_flags, allow_remaining);"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $self->pidl("static PyObject *py_$name\_ndr_unpack_out(PyObject *py_obj, PyObject *args, PyObject *kwargs)"); + $self->pidl("{"); + $self->indent; + $self->pidl("DATA_BLOB blob;"); + $self->pidl("Py_ssize_t blob_length = 0;"); + $self->pidl("const char * const kwnames[] = { \"data_blob\", \"bigendian\", \"ndr64\", \"allow_remaining\", NULL };"); + $self->pidl("PyObject *bigendian_obj = NULL;"); + $self->pidl("PyObject *ndr64_obj = NULL;"); + $self->pidl("uint32_t ndr_pull_flags = LIBNDR_FLAG_REF_ALLOC;"); + $self->pidl("PyObject *allow_remaining_obj = NULL;"); + $self->pidl("bool allow_remaining = false;"); + $self->pidl(""); + $self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, PYARG_BYTES_LEN \"|OOO:__ndr_unpack_out__\","); + $self->indent; + $self->pidl("discard_const_p(char *, kwnames),"); + $self->pidl("&blob.data, &blob_length,"); + $self->pidl("&bigendian_obj,"); + $self->pidl("&ndr64_obj,"); + $self->pidl("&allow_remaining_obj)) {"); + $self->deindent; + $self->indent; + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl("blob.length = blob_length;"); + $self->pidl(""); + $self->pidl("if (bigendian_obj && PyObject_IsTrue(bigendian_obj)) {"); + $self->indent; + $self->pidl("ndr_pull_flags |= LIBNDR_FLAG_BIGENDIAN;"); + $self->deindent; + $self->pidl("}"); + $self->pidl("if (ndr64_obj && PyObject_IsTrue(ndr64_obj)) {"); + $self->indent; + $self->pidl("ndr_pull_flags |= LIBNDR_FLAG_NDR64;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + $self->pidl("if (allow_remaining_obj && PyObject_IsTrue(allow_remaining_obj)) {"); + $self->indent; + $self->pidl("allow_remaining = true;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + $self->pidl("return py_$name\_ndr_unpack(py_obj, &blob, NDR_OUT, ndr_pull_flags, allow_remaining);"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $self->pidl("static PyObject *py_$name\_ndr_print(PyObject *py_obj, const char *name, int ndr_inout_flags)"); + $self->pidl("{"); + $self->indent; + $self->pidl("$ndr_call"); + $self->pidl("$object_ptr"); + $self->pidl("PyObject *ret;"); + $self->pidl("char *retstr;"); + $self->pidl(""); + $self->pidl("if (ndr_table_$iface\.num_calls < " . ($fn->{OPNUM}+1) . + ") {"); + $self->indent; + $self->pidl("PyErr_SetString(PyExc_TypeError, \"Internal Error, ndr_interface_call missing for py_$name\_ndr_print\");"); + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl("call = &ndr_table_$iface\.calls[$fn->{OPNUM}];"); + $self->pidl(""); + $self->pidl("retstr = ndr_print_function_string(pytalloc_get_mem_ctx(py_obj), call->ndr_print, name, ndr_inout_flags, object);"); + $self->pidl("ret = PyStr_FromString(retstr);"); + $self->pidl("TALLOC_FREE(retstr);"); + $self->pidl(""); + $self->pidl("return ret;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $self->pidl("static PyObject *py_$name\_ndr_print_in(PyObject *py_obj)"); + $self->pidl("{"); + $self->indent; + $self->pidl("return py_$name\_ndr_print(py_obj, \"$name\_in\", NDR_IN);"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $self->pidl("static PyObject *py_$name\_ndr_print_out(PyObject *py_obj)"); + $self->pidl("{"); + $self->indent; + $self->pidl("return py_$name\_ndr_print(py_obj, \"$name\_out\", NDR_OUT);"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $py_methods = "py_$name\_methods"; + $self->pidl("static PyMethodDef $py_methods\[] = {"); + $self->indent; + $self->pidl("{ \"opnum\", (PyCFunction)py_$name\_ndr_opnum, METH_NOARGS|METH_CLASS,"); + $self->indent; + $self->pidl("\"$modulename.$prettyname.opnum() -> ".sprintf("%d (0x%02x)", $fn->{OPNUM}, $fn->{OPNUM})." \" },"); + $self->deindent; + $self->pidl("{ \"__ndr_pack_in__\", (PyCFunction)py_$name\_ndr_pack_in, METH_VARARGS|METH_KEYWORDS,"); + $self->indent; + $self->pidl("\"S.ndr_pack_in(object, bigendian=False, ndr64=False) -> blob\\nNDR pack input\" },"); + $self->deindent; + $self->pidl("{ \"__ndr_pack_out__\", (PyCFunction)py_$name\_ndr_pack_out, METH_VARARGS|METH_KEYWORDS,"); + $self->indent; + $self->pidl("\"S.ndr_pack_out(object, bigendian=False, ndr64=False) -> blob\\nNDR pack output\" },"); + $self->deindent; + $self->pidl("{ \"__ndr_unpack_in__\", (PyCFunction)py_$name\_ndr_unpack_in, METH_VARARGS|METH_KEYWORDS,"); + $self->indent; + $self->pidl("\"S.ndr_unpack_in(class, blob, bigendian=False, ndr64=False, allow_remaining=False) -> None\\nNDR unpack input\" },"); + $self->deindent; + $self->pidl("{ \"__ndr_unpack_out__\", (PyCFunction)py_$name\_ndr_unpack_out, METH_VARARGS|METH_KEYWORDS,"); + $self->indent; + $self->pidl("\"S.ndr_unpack_out(class, blob, bigendian=False, ndr64=False, allow_remaining=False) -> None\\nNDR unpack output\" },"); + $self->deindent; + $self->pidl("{ \"__ndr_print_in__\", (PyCFunction)py_$name\_ndr_print_in, METH_NOARGS, \"S.ndr_print_in(object) -> None\\nNDR print input\" },"); + $self->pidl("{ \"__ndr_print_out__\", (PyCFunction)py_$name\_ndr_print_out, METH_NOARGS, \"S.ndr_print_out(object) -> None\\nNDR print output\" },"); + $self->pidl("{ NULL, NULL, 0, NULL }"); + $self->deindent; + $self->pidl("};"); + $self->pidl(""); + + $self->pidl_hdr("static PyTypeObject $name\_Type;\n"); + $self->pidl(""); + my $docstring = $self->DocString($fn, $name); + my $typeobject = "$name\_Type"; + $self->pidl("static PyTypeObject $typeobject = {"); + $self->indent; + $self->pidl("PyVarObject_HEAD_INIT(NULL, 0)"); + $self->pidl(".tp_name = \"$modulename.$prettyname\","); + $self->pidl(".tp_getset = $getsetters,"); + if ($docstring) { + $self->pidl(".tp_doc = $docstring,"); + } + $self->pidl(".tp_methods = $py_methods,"); + $self->pidl(".tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,"); + $self->pidl(".tp_new = py_$name\_new,"); + $self->deindent; + $self->pidl("};"); + + $self->pidl(""); + + my $talloc_typename = $self->import_type_variable("talloc", "BaseObject"); + $self->register_module_prereadycode(["$name\_Type.tp_base = $talloc_typename;", + "$name\_Type.tp_basicsize = pytalloc_BaseObject_size();", + ""]); + + return "&$typeobject"; +} + +sub get_metadata_var($) +{ + my ($e) = @_; + sub get_var($) { my $x = shift; $x =~ s/\*//g; return $x; } + + if (has_property($e, "length_is")) { + return get_var($e->{PROPERTIES}->{length_is}); + } elsif (has_property($e, "size_is")) { + return get_var($e->{PROPERTIES}->{size_is}); + } + + return undef; +} + +sub find_metadata_args($) +{ + my ($fn) = @_; + my $metadata_args = { in => {}, out => {} }; + + # Determine arguments that are metadata for other arguments (size_is/length_is) + foreach my $e (@{$fn->{ELEMENTS}}) { + foreach my $dir (@{$e->{DIRECTION}}) { + my $main = get_metadata_var($e); + if ($main) { + $metadata_args->{$dir}->{$main} = $e->{NAME}; + } + } + } + + return $metadata_args; +} + +sub PythonFunctionUnpackOut($$$) +{ + my ($self, $fn, $fnname) = @_; + + my $outfnname = "unpack_$fnname\_args_out"; + my $signature = ""; + + my $metadata_args = find_metadata_args($fn); + + my $env = GenerateFunctionOutEnv($fn, "r->"); + my $result_size = 0; + + $self->pidl("static PyObject *$outfnname(struct $fn->{NAME} *r)"); + $self->pidl("{"); + $self->indent; + $self->pidl("PyObject *result;"); + foreach my $e (@{$fn->{ELEMENTS}}) { + next unless (grep(/out/,@{$e->{DIRECTION}})); + next if (($metadata_args->{in}->{$e->{NAME}} and grep(/in/, @{$e->{DIRECTION}})) or + ($metadata_args->{out}->{$e->{NAME}}) and grep(/out/, @{$e->{DIRECTION}})); + $self->pidl("PyObject *py_$e->{NAME};"); + $result_size++; + } + + if ($fn->{RETURN_TYPE}) { + $result_size++ unless ($fn->{RETURN_TYPE} eq "WERROR" or $fn->{RETURN_TYPE} eq "NTSTATUS"); + } + + my $i = 0; + + if ($result_size > 1) { + $self->pidl("result = PyTuple_New($result_size);"); + $signature .= "("; + } elsif ($result_size == 0) { + $self->pidl("result = Py_None;"); + $self->pidl("Py_INCREF(result);"); + $signature .= "None"; + } + + foreach my $e (@{$fn->{ELEMENTS}}) { + next if ($metadata_args->{out}->{$e->{NAME}}); + my $py_name = "py_$e->{NAME}"; + if (grep(/out/,@{$e->{DIRECTION}})) { + $self->ConvertObjectToPython("r", $env, $e, "r->out.$e->{NAME}", $py_name, "return NULL;"); + if ($result_size > 1) { + $self->pidl("PyTuple_SetItem(result, $i, $py_name);"); + $i++; + $signature .= "$e->{NAME}, "; + } else { + $self->pidl("result = $py_name;"); + $signature .= $e->{NAME}; + } + } + } + + if (defined($fn->{RETURN_TYPE}) and $fn->{RETURN_TYPE} eq "NTSTATUS") { + $self->handle_ntstatus("r->out.result", "NULL", undef); + } elsif (defined($fn->{RETURN_TYPE}) and $fn->{RETURN_TYPE} eq "WERROR") { + $self->handle_werror("r->out.result", "NULL", undef); + } elsif (defined($fn->{RETURN_TYPE})) { + my $conv = $self->ConvertObjectToPythonData("r", $fn->{RETURN_TYPE}, "r->out.result", $fn); + if ($result_size > 1) { + $self->pidl("PyTuple_SetItem(result, $i, $conv);"); + } else { + $self->pidl("result = $conv;"); + } + $signature .= "result"; + } + + if (substr($signature, -2) eq ", ") { + $signature = substr($signature, 0, -2); + } + if ($result_size > 1) { + $signature .= ")"; + } + + $self->pidl("return result;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + return ($outfnname, $signature); +} + +sub PythonFunctionPackIn($$$) +{ + my ($self, $fn, $fnname) = @_; + my $metadata_args = find_metadata_args($fn); + + my $infnname = "pack_$fnname\_args_in"; + + $self->pidl("static bool $infnname(PyObject *args, PyObject *kwargs, struct $fn->{NAME} *r)"); + $self->pidl("{"); + $self->indent; + my $args_format = ""; + my $args_string = ""; + my $args_names = ""; + my $signature = ""; + + foreach my $e (@{$fn->{ELEMENTS}}) { + next unless (grep(/in/,@{$e->{DIRECTION}})); + next if (($metadata_args->{in}->{$e->{NAME}} and grep(/in/, @{$e->{DIRECTION}})) or + ($metadata_args->{out}->{$e->{NAME}}) and grep(/out/, @{$e->{DIRECTION}})); + $self->pidl("PyObject *py_$e->{NAME};"); + $args_format .= "O"; + $args_string .= ", &py_$e->{NAME}"; + $args_names .= "\"$e->{NAME}\", "; + $signature .= "$e->{NAME}, "; + } + if (substr($signature, -2) eq ", ") { + $signature = substr($signature, 0, -2); + } + $self->pidl("const char *kwnames[] = {"); + $self->indent; + $self->pidl($args_names . "NULL"); + $self->deindent; + $self->pidl("};"); + + $self->pidl(""); + $self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, \"$args_format:$fn->{NAME}\", discard_const_p(char *, kwnames)$args_string)) {"); + $self->indent; + $self->pidl("return false;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + my $env = GenerateFunctionInEnv($fn, "r->"); + + my $fail = "return false;"; + foreach my $e (@{$fn->{ELEMENTS}}) { + next unless (grep(/in/,@{$e->{DIRECTION}})); + if ($metadata_args->{in}->{$e->{NAME}}) { + my $py_var = "py_".$metadata_args->{in}->{$e->{NAME}}; + $self->pidl("PY_CHECK_TYPE(&PyList_Type, $py_var, $fail);"); + my $val = "PyList_GET_SIZE($py_var)"; + if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") { + $self->pidl("r->in.$e->{NAME} = talloc_ptrtype(r, r->in.$e->{NAME});"); + $self->pidl("if (r->in.$e->{NAME} == NULL) {"); + $self->indent; + $self->pidl("PyErr_NoMemory();"); + $self->pidl($fail); + $self->deindent; + $self->pidl("}"); + $self->pidl("*r->in.$e->{NAME} = $val;"); + } else { + $self->pidl("r->in.$e->{NAME} = $val;"); + } + } else { + $self->ConvertObjectFromPython($env, "r", $e, "py_$e->{NAME}", "r->in.$e->{NAME}", $fail); + } + } + $self->pidl("return true;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + return ($infnname, $signature); +} + +sub PythonFunction($$$) +{ + my ($self, $fn, $iface, $prettyname) = @_; + + my $fnname = "py_$fn->{NAME}"; + my $docstring = $self->DocString($fn, $fn->{NAME}); + + my ($infn, $insignature) = $self->PythonFunctionPackIn($fn, $fnname); + my ($outfn, $outsignature) = $self->PythonFunctionUnpackOut($fn, $fnname); + my $signature = "S.$prettyname($insignature) -> $outsignature"; + if ($docstring) { + $docstring = "\"$signature\\n\\n\"$docstring"; + } else { + $docstring = "\"$signature\""; + } + + return ($infn, $outfn, $docstring); +} + +sub handle_werror($$$$) +{ + my ($self, $var, $retval, $mem_ctx) = @_; + + $self->pidl("if (!W_ERROR_IS_OK($var)) {"); + $self->indent; + $self->pidl("PyErr_SetWERROR($var);"); + $self->pidl("talloc_free($mem_ctx);") if ($mem_ctx); + $self->pidl("return $retval;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); +} + +sub handle_ntstatus($$$$) +{ + my ($self, $var, $retval, $mem_ctx) = @_; + + $self->pidl("if (NT_STATUS_IS_ERR($var)) {"); + $self->indent; + $self->pidl("PyErr_SetNTSTATUS($var);"); + $self->pidl("talloc_free($mem_ctx);") if ($mem_ctx); + $self->pidl("return $retval;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); +} + +sub PythonType($$$$) +{ + my ($self, $modulename, $d, $interface, $basename) = @_; + + my $actual_ctype = $d; + if ($actual_ctype->{TYPE} eq "TYPEDEF") { + $actual_ctype = $actual_ctype->{DATA}; + } + + if ($actual_ctype->{TYPE} eq "STRUCT") { + my $typeobject; + my $fn_name = PrettifyTypeName($d->{NAME}, $basename); + + if ($d->{TYPE} eq "STRUCT") { + $typeobject = $self->PythonStruct($modulename, $fn_name, $d->{NAME}, mapTypeName($d), $d); + } else { + $typeobject = $self->PythonStruct($modulename, $fn_name, $d->{NAME}, mapTypeName($d), $d->{DATA}); + } + + $self->register_module_typeobject($fn_name, $typeobject, $d->{ORIGINAL}); + } + + if ($d->{TYPE} eq "ENUM" or $d->{TYPE} eq "BITMAP") { + $self->EnumAndBitmapConsts($d->{NAME}, $d); + } + + if ($d->{TYPE} eq "TYPEDEF" and ($d->{DATA}->{TYPE} eq "ENUM" or $d->{DATA}->{TYPE} eq "BITMAP")) { + $self->EnumAndBitmapConsts($d->{NAME}, $d->{DATA}); + } + + if ($actual_ctype->{TYPE} eq "UNION" and defined($actual_ctype->{ELEMENTS})) { + my $prettyname = PrettifyTypeName($d->{NAME}, $basename); + my $typeobject = "$d->{NAME}\_Type"; + my $docstring = $self->DocString($d, $d->{NAME}); + my $cname = "union $d->{NAME}"; + + $self->pidl("static PyObject *py_import_$d->{NAME}(TALLOC_CTX *mem_ctx, int level, " .mapTypeName($d) . " *in)"); + $self->pidl("{"); + $self->indent; + $self->FromUnionToPythonFunction("mem_ctx", $actual_ctype, "level", "in") if ($actual_ctype->{TYPE} eq "UNION"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $self->pidl("static ".mapTypeName($d) . " *py_export_$d->{NAME}(TALLOC_CTX *mem_ctx, int level, PyObject *in)"); + $self->pidl("{"); + $self->indent; + $self->FromPythonToUnionFunction($actual_ctype, mapTypeName($d), "level", "mem_ctx", "in") if ($actual_ctype->{TYPE} eq "UNION"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + my $getsetters = "NULL"; + my $py_methods = "NULL"; + my $typename = mapTypeName($d); + + $self->pidl("static PyObject *py_$d->{NAME}\_import(PyTypeObject *type, PyObject *args, PyObject *kwargs)"); + $self->pidl("{"); + $self->indent; + $self->pidl("const char * const kwnames[] = { \"mem_ctx\", \"level\", \"in\", NULL };"); + $self->pidl("PyObject *mem_ctx_obj = NULL;"); + $self->pidl("TALLOC_CTX *mem_ctx = NULL;"); + $self->pidl("int level = 0;"); + $self->pidl("PyObject *in_obj = NULL;"); + $self->pidl("$typename *in = NULL;"); + $self->pidl(""); + $self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, \"OiO:import\","); + $self->indent; + $self->pidl("discard_const_p(char *, kwnames),"); + $self->pidl("&mem_ctx_obj,"); + $self->pidl("&level,"); + $self->pidl("&in_obj)) {"); + $self->deindent; + $self->indent; + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl("mem_ctx = pytalloc_get_ptr(mem_ctx_obj);"); + $self->pidl("if (mem_ctx == NULL) {"); + $self->indent; + $self->pidl("PyErr_SetString(PyExc_TypeError, \"mem_ctx is NULL)!\");"); + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl("in = ($typename *)pytalloc_get_ptr(in_obj);"); + $self->pidl("if (in == NULL) {"); + $self->indent; + $self->pidl("PyErr_Format(PyExc_TypeError, \"in needs to be a pointer to $typename!\");"); + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + $self->pidl("return py_import_$d->{NAME}(mem_ctx, level, in);"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $self->pidl("static PyObject *py_$d->{NAME}\_export(PyTypeObject *type, PyObject *args, PyObject *kwargs)"); + $self->pidl("{"); + $self->indent; + $self->pidl("const char * const kwnames[] = { \"mem_ctx\", \"level\", \"in\", NULL };"); + $self->pidl("PyObject *mem_ctx_obj = NULL;"); + $self->pidl("TALLOC_CTX *mem_ctx = NULL;"); + $self->pidl("int level = 0;"); + $self->pidl("PyObject *in = NULL;"); + $self->pidl("$typename *out = NULL;"); + $self->pidl(""); + $self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, \"OiO:import\","); + $self->indent; + $self->pidl("discard_const_p(char *, kwnames),"); + $self->pidl("&mem_ctx_obj,"); + $self->pidl("&level,"); + $self->pidl("&in)) {"); + $self->deindent; + $self->indent; + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl("mem_ctx = pytalloc_get_ptr(mem_ctx_obj);"); + $self->pidl("if (mem_ctx == NULL) {"); + $self->indent; + $self->pidl("PyErr_SetString(PyExc_TypeError, \"mem_ctx is NULL)!\");"); + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + $self->pidl("out = py_export_$d->{NAME}(mem_ctx, level, in);"); + $self->pidl("if (out == NULL) {"); + $self->indent; + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + $self->pidl("return pytalloc_GenericObject_reference(out);"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $py_methods = "py_$d->{NAME}_methods"; + $self->pidl("static PyMethodDef $py_methods\[] = {"); + $self->indent; + $self->pidl("{ \"__import__\", (PyCFunction)py_$d->{NAME}\_import,"); + $self->indent; + $self->pidl("METH_VARARGS|METH_KEYWORDS|METH_CLASS,"); + $self->pidl("\"T.__import__(mem_ctx, level, in) => ret.\" },"); + $self->deindent; + $self->pidl("{ \"__export__\", (PyCFunction)py_$d->{NAME}\_export,"); + $self->indent; + $self->pidl("METH_VARARGS|METH_KEYWORDS|METH_CLASS,"); + $self->pidl("\"T.__export__(mem_ctx, level, in) => ret.\" },"); + $self->deindent; + $self->pidl("{ NULL, NULL, 0, NULL }"); + $self->deindent; + $self->pidl("};"); + $self->pidl(""); + + $self->pidl("static PyObject *py_$d->{NAME}\_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)"); + $self->pidl("{"); + $self->indent; + $self->pidl("PyErr_Format(PyExc_TypeError, \"New %s Objects are not supported\", type->tp_name);"); + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $self->pidl(""); + $self->pidl_hdr("static PyTypeObject $typeobject;\n"); + $self->pidl("static PyTypeObject $typeobject = {"); + $self->indent; + $self->pidl("PyVarObject_HEAD_INIT(NULL, 0)"); + $self->pidl(".tp_name = \"$modulename.$prettyname\","); + $self->pidl(".tp_getset = $getsetters,"); + if ($docstring) { + $self->pidl(".tp_doc = $docstring,"); + } + $self->pidl(".tp_methods = $py_methods,"); + $self->pidl(".tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,"); + $self->pidl(".tp_new = py_$d->{NAME}\_new,"); + $self->deindent; + $self->pidl("};"); + + $self->pidl(""); + + my $talloc_typename = $self->import_type_variable("talloc", "BaseObject"); + $self->register_module_prereadycode(["$typeobject.tp_base = $talloc_typename;", + "$typeobject.tp_basicsize = pytalloc_BaseObject_size();", + ""]); + + $self->register_module_typeobject($prettyname, "&$typeobject", $d->{ORIGINAL}); + } +} + +sub DocString($$$) +{ + my ($self, $d, $name) = @_; + if (has_property($d, "helpstring")) { + my $docstring = uc("py_doc_$name"); + $self->pidl("#define $docstring ".has_property($d, "helpstring")); + return $docstring; + } + + return undef; +} + +sub Interface($$$) +{ + my($self,$interface,$basename) = @_; + + if (has_property($interface, "pyhelper")) { + $self->pidl("#include \"".unmake_str($interface->{PROPERTIES}->{pyhelper})."\"\n"); + } + + $self->Const($_) foreach (@{$interface->{CONSTS}}); + + foreach my $d (@{$interface->{TYPES}}) { + next if has_property($d, "nopython"); + + $self->PythonType($basename, $d, $interface, $basename); + } + + if (defined $interface->{PROPERTIES}->{uuid}) { + $self->pidl_hdr("static PyTypeObject $interface->{NAME}_InterfaceType;\n"); + $self->pidl(""); + + my @fns = (); + + foreach my $d (@{$interface->{FUNCTIONS}}) { + next if has_property($d, "noopnum"); + next if has_property($d, "nopython"); + next if has_property($d, "todo"); + + my $skip = 0; + foreach my $e (@{$d->{ELEMENTS}}) { + if (ContainsPipe($e, $e->{LEVELS}[0])) { + $skip = 1; + last; + } + } + next if $skip; + + my $prettyname = $d->{NAME}; + + $prettyname =~ s/^$interface->{NAME}_//; + $prettyname =~ s/^$basename\_//; + + my $typeobject = $self->PythonFunctionStruct($basename, $d, $interface->{NAME}, $prettyname); + $self->register_module_typeobject($prettyname, $typeobject, $d->{ORIGINAL}); + + my ($infn, $outfn, $fndocstring) = $self->PythonFunction($d, $interface->{NAME}, $prettyname); + + push (@fns, [$infn, $outfn, "dcerpc_$d->{NAME}_r", $prettyname, $fndocstring, $d->{OPNUM}]); + } + + $self->pidl("const struct PyNdrRpcMethodDef py_ndr_$interface->{NAME}\_methods[] = {"); + $self->indent; + foreach my $d (@fns) { + my ($infn, $outfn, $callfn, $prettyname, $docstring, $opnum) = @$d; + $self->pidl("{ \"$prettyname\", $docstring, (py_dcerpc_call_fn)$callfn, (py_data_pack_fn)$infn, (py_data_unpack_fn)$outfn, $opnum, &ndr_table_$interface->{NAME} },"); + } + $self->pidl("{ NULL }"); + $self->deindent; + $self->pidl("};"); + $self->pidl(""); + + $self->pidl("static PyObject *interface_$interface->{NAME}_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)"); + $self->pidl("{"); + $self->indent; + $self->pidl("return py_dcerpc_interface_init_helper(type, args, kwargs, &ndr_table_$interface->{NAME});"); + $self->deindent; + $self->pidl("}"); + + $self->pidl(""); + + my $signature = +"\"$interface->{NAME}(binding, lp_ctx=None, credentials=None) -> connection\\n\" +\"\\n\" +\"binding should be a DCE/RPC binding string (for example: ncacn_ip_tcp:127.0.0.1)\\n\" +\"lp_ctx should be a path to a smb.conf file or a param.LoadParm object\\n\" +\"credentials should be a credentials.Credentials object.\\n\\n\""; + + my $docstring = $self->DocString($interface, $interface->{NAME}); + + if ($docstring) { + $docstring = "$signature$docstring"; + } else { + $docstring = $signature; + } + + my $if_typename = "$interface->{NAME}_InterfaceType"; + + $self->pidl("static PyTypeObject $if_typename = {"); + $self->indent; + $self->pidl("PyVarObject_HEAD_INIT(NULL, 0)"); + $self->pidl(".tp_name = \"$basename.$interface->{NAME}\","); + $self->pidl(".tp_basicsize = sizeof(dcerpc_InterfaceObject),"); + $self->pidl(".tp_doc = $docstring,"); + $self->pidl(".tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,"); + $self->pidl(".tp_new = interface_$interface->{NAME}_new,"); + $self->deindent; + $self->pidl("};"); + + $self->pidl(""); + + $self->register_module_typeobject($interface->{NAME}, "&$if_typename", $interface->{ORIGINAL}); + my $dcerpc_typename = $self->import_type_variable("samba.dcerpc.base", "ClientConnection"); + $self->register_module_prereadycode(["$if_typename.tp_base = $dcerpc_typename;", ""]); + $self->register_module_postreadycode(["if (!PyInterface_AddNdrRpcMethods(&$if_typename, py_ndr_$interface->{NAME}\_methods))", "\treturn NULL;", ""]); + + + $self->pidl("static PyObject *syntax_$interface->{NAME}_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)"); + $self->pidl("{"); + $self->indent; + $self->pidl("return py_dcerpc_syntax_init_helper(type, args, kwargs, &ndr_table_$interface->{NAME}.syntax_id);"); + $self->deindent; + $self->pidl("}"); + + $self->pidl(""); + + my $signature = "\"$interface->{NAME}_abstract_syntax()\\n\""; + + my $docstring = $self->DocString($interface, $interface->{NAME}."_syntax"); + + if ($docstring) { + $docstring = "$signature$docstring"; + } else { + $docstring = $signature; + } + + my $syntax_typename = "$interface->{NAME}_SyntaxType"; + + $self->pidl("static PyTypeObject $syntax_typename = {"); + $self->indent; + $self->pidl("PyVarObject_HEAD_INIT(NULL, 0)"); + $self->pidl(".tp_name = \"$basename.$interface->{NAME}_abstract_syntax\","); + $self->pidl(".tp_doc = $docstring,"); + $self->pidl(".tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,"); + $self->pidl(".tp_new = syntax_$interface->{NAME}_new,"); + $self->deindent; + $self->pidl("};"); + + $self->pidl(""); + + $self->register_module_typeobject("$interface->{NAME}_abstract_syntax", "&$syntax_typename", $interface->{ORIGINAL}); + if (not defined($self->existing_module_object("abstract_syntax"))) { + # Only the first syntax gets registered with the legacy + # "abstract_syntax" name + $self->register_module_typeobject("abstract_syntax", "&$syntax_typename", $interface->{ORIGINAL}); + } + my $ndr_typename = $self->import_type_variable("samba.dcerpc.misc", "ndr_syntax_id"); + $self->register_module_prereadycode(["$syntax_typename.tp_base = $ndr_typename;", + "$syntax_typename.tp_basicsize = pytalloc_BaseObject_size();", + ""]); + } + + $self->pidl_hdr("\n"); +} + +sub register_module_method($$$$$) +{ + my ($self, $fn_name, $pyfn_name, $flags, $doc) = @_; + + push (@{$self->{module_methods}}, [$fn_name, $pyfn_name, $flags, $doc]) +} + +sub register_module_typeobject($$$$) +{ + my ($self, $name, $py_name, $location) = @_; + + $self->register_module_object($name, "(PyObject *)(void *)$py_name", $location); + + $self->check_ready_type($py_name); + + $self->register_patch_type_call($name, $py_name); +} + +sub check_ready_type($$) +{ + my ($self, $py_name) = @_; + push (@{$self->{ready_types}}, $py_name) unless (grep(/^$py_name$/,@{$self->{ready_types}})); +} + +sub register_module_import($$) +{ + my ($self, $module_path) = @_; + + my $var_name = $module_path; + $var_name =~ s/\./_/g; + $var_name = "dep_$var_name"; + + unless (defined $self->{module_imports_uniq}->{$var_name}) { + my $h = { "key" => $var_name, "val" => $module_path}; + push @{$self->{module_imports}}, $h; + $self->{module_imports_uniq}->{$var_name} = $h; + } + return $var_name; +} + +sub import_type_variable($$$) +{ + my ($self, $module, $name) = @_; + + $self->register_module_import($module); + unless (defined $self->{type_imports_uniq}->{$name}) { + my $h = { "key" => $name, "val" => $module}; + push @{$self->{type_imports}}, $h; + $self->{type_imports_uniq}->{$name} = $h; + } + return "$name\_Type"; +} + +sub use_type_variable($$) +{ + my ($self, $orig_ctype) = @_; + # FIXME: Have a global lookup table for types that look different on the + # wire than they are named in C? + if ($orig_ctype->{NAME} eq "dom_sid2" or + $orig_ctype->{NAME} eq "dom_sid28" or + $orig_ctype->{NAME} eq "dom_sid0") { + $orig_ctype->{NAME} = "dom_sid"; + } + if ($orig_ctype->{NAME} eq "spoolss_security_descriptor") { + $orig_ctype->{NAME} = "security_descriptor"; + } + + my $ctype = resolveType($orig_ctype); + unless (defined($ctype->{BASEFILE})) { + return undef; + } + # If this is an external type, make sure we do the right imports. + if (($ctype->{BASEFILE} ne $self->{BASENAME})) { + return $self->import_type_variable("samba.dcerpc.$ctype->{BASEFILE}", $ctype->{NAME}); + } + return "&$ctype->{NAME}_Type"; +} + +sub register_patch_type_call($$$) +{ + my ($self, $typename, $cvar) = @_; + + push(@{$self->{patch_type_calls}}, [$typename, $cvar]); + +} + +sub register_module_prereadycode($$) +{ + my ($self, $code) = @_; + + push (@{$self->{prereadycode}}, @$code); +} + +sub register_module_postreadycode($$) +{ + my ($self, $code) = @_; + + push (@{$self->{postreadycode}}, @$code); +} + +sub existing_module_object($$) +{ + my ($self, $name) = @_; + + if (defined($self->{module_object_uniq}->{$name})) { + return $self->{module_object_uniq}->{$name}; + } + + return undef; +} + +sub register_module_object($$$$) +{ + my ($self, $name, $py_name, $location) = @_; + + my $existing = $self->existing_module_object($name); + fatal($location, "module_object($name, $py_name) registered twice! $existing.") if defined($existing); + + push (@{$self->{module_objects}}, [$name, $py_name]); + $self->{module_object_uniq}->{$name} = $py_name; +} + +sub assign($$$) +{ + my ($self, $dest, $src) = @_; + if ($dest =~ /^\&/ and $src eq "NULL") { + $self->pidl("memset($dest, 0, sizeof(" . get_value_of($dest) . "));"); + } elsif ($dest =~ /^\&/) { + my $destvar = get_value_of($dest); + $self->pidl("$destvar = *$src;"); + } else { + $self->pidl("$dest = $src;"); + } +} + +sub ConvertStringFromPythonData($$$$$) +{ + my ($self, $mem_ctx, $py_var, $target, $fail) = @_; + + $self->pidl("{"); + $self->indent; + $self->pidl("const char *test_str;"); + $self->pidl("const char *talloc_str;"); + $self->pidl("PyObject *unicode = NULL;"); + $self->pidl("if (PyUnicode_Check($py_var)) {"); + $self->indent; + # FIXME: Use Unix charset setting rather than utf-8 + $self->pidl("unicode = PyUnicode_AsEncodedString($py_var, \"utf-8\", \"ignore\");"); + $self->pidl("if (unicode == NULL) {"); + $self->indent; + $self->pidl("PyErr_NoMemory();"); + $self->pidl("$fail"); + $self->deindent; + $self->pidl("}"); + + $self->pidl("test_str = PyBytes_AS_STRING(unicode);"); + $self->deindent; + $self->pidl("} else if (PyBytes_Check($py_var)) {"); + $self->indent; + $self->pidl("test_str = PyBytes_AS_STRING($py_var);"); + $self->deindent; + $self->pidl("} else {"); + $self->indent; + $self->pidl("PyErr_Format(PyExc_TypeError, \"Expected string or unicode object, got %s\", Py_TYPE($py_var)->tp_name);"); + $self->pidl("$fail"); + $self->deindent; + $self->pidl("}"); + $self->pidl("talloc_str = talloc_strdup($mem_ctx, test_str);"); + $self->pidl("if (unicode != NULL) {"); + $self->indent; + $self->pidl("Py_DECREF(unicode);"); + $self->deindent; + $self->pidl("}"); + $self->pidl("if (talloc_str == NULL) {"); + $self->indent; + $self->pidl("PyErr_NoMemory();"); + $self->pidl("$fail"); + $self->deindent; + $self->pidl("}"); + $self->pidl("$target = talloc_str;"); + $self->deindent; + $self->pidl("}"); +} + +sub ConvertObjectFromPythonData($$$$$$;$$) +{ + my ($self, $mem_ctx, $cvar, $ctype, $target, $fail, $location, $switch) = @_; + + fatal($location, "undef type for $cvar") unless(defined($ctype)); + + $ctype = resolveType($ctype); + + my $actual_ctype = $ctype; + if ($actual_ctype->{TYPE} eq "TYPEDEF") { + $actual_ctype = $actual_ctype->{DATA}; + } + + # We need to cover ENUMs, BITMAPS and SCALAR values here, as + # all could otherwise be assigned invalid integer values + my $ctype_alias = ""; + my $uint_max = ""; + if ($actual_ctype->{TYPE} eq "ENUM") { + # Importantly, ENUM values are unsigned in pidl, and + # typically map to uint32 + $ctype_alias = enum_type_fn($actual_ctype); + } elsif ($actual_ctype->{TYPE} eq "BITMAP") { + $ctype_alias = bitmap_type_fn($actual_ctype); + } elsif ($actual_ctype->{TYPE} eq "SCALAR") { + $ctype_alias = expandAlias($actual_ctype->{NAME}); + } + + # This is the unsigned Python Integer -> C integer validation + # case. The signed case is below. + if ($ctype_alias =~ /^(uint[0-9]*|hyper|udlong|udlongr + |NTTIME_hyper|NTTIME|NTTIME_1sec + |uid_t|gid_t)$/x) { + $self->pidl("{"); + $self->indent; + $self->pidl("const unsigned long long uint_max = ndr_sizeof2uintmax(sizeof($target));"); + $self->pidl("if (PyLong_Check($cvar)) {"); + $self->indent; + $self->pidl("unsigned long long test_var;"); + $self->pidl("test_var = PyLong_AsUnsignedLongLong($cvar);"); + $self->pidl("if (PyErr_Occurred() != NULL) {"); + $self->indent; + $self->pidl($fail); + $self->deindent; + $self->pidl("}"); + $self->pidl("if (test_var > uint_max) {"); + $self->indent; + $self->pidl("PyErr_Format(PyExc_OverflowError, \"Expected type %s or %s within range 0 - %llu, got %llu\",\\"); + $self->pidl(" PyInt_Type.tp_name, PyLong_Type.tp_name, uint_max, test_var);"); + $self->pidl($fail); + $self->deindent; + $self->pidl("}"); + $self->pidl("$target = test_var;"); + $self->deindent; + $self->pidl("} else if (PyInt_Check($cvar)) {"); + $self->indent; + $self->pidl("long test_var;"); + $self->pidl("test_var = PyInt_AsLong($cvar);"); + $self->pidl("if (test_var < 0 || test_var > uint_max) {"); + $self->indent; + $self->pidl("PyErr_Format(PyExc_OverflowError, \"Expected type %s or %s within range 0 - %llu, got %ld\",\\"); + $self->pidl(" PyInt_Type.tp_name, PyLong_Type.tp_name, uint_max, test_var);"); + $self->pidl($fail); + $self->deindent; + $self->pidl("}"); + $self->pidl("$target = test_var;"); + $self->deindent; + $self->pidl("} else {"); + $self->indent; + $self->pidl("PyErr_Format(PyExc_TypeError, \"Expected type %s or %s\",\\"); + $self->pidl(" PyInt_Type.tp_name, PyLong_Type.tp_name);"); + $self->pidl($fail); + $self->deindent; + $self->pidl("}"); + $self->deindent; + $self->pidl("}"); + return; + } + + # Confirm the signed python integer fits in the C type + # correctly. It is subtly different from the unsigned case + # above, so while it looks like a duplicate, it is not + # actually a duplicate. + if ($ctype_alias =~ /^(dlong|char|int[0-9]*|time_t)$/x) { + $self->pidl("{"); + $self->indent; + $self->pidl("const long long int_max = ndr_sizeof2intmax(sizeof($target));"); + $self->pidl("const long long int_min = -int_max - 1;"); + $self->pidl("if (PyLong_Check($cvar)) {"); + $self->indent; + $self->pidl("long long test_var;"); + $self->pidl("test_var = PyLong_AsLongLong($cvar);"); + $self->pidl("if (PyErr_Occurred() != NULL) {"); + $self->indent; + $self->pidl($fail); + $self->deindent; + $self->pidl("}"); + $self->pidl("if (test_var < int_min || test_var > int_max) {"); + $self->indent; + $self->pidl("PyErr_Format(PyExc_OverflowError, \"Expected type %s or %s within range %lld - %lld, got %lld\",\\"); + $self->pidl(" PyInt_Type.tp_name, PyLong_Type.tp_name, int_min, int_max, test_var);"); + $self->pidl($fail); + $self->deindent; + $self->pidl("}"); + $self->pidl("$target = test_var;"); + $self->deindent; + $self->pidl("} else if (PyInt_Check($cvar)) {"); + $self->indent; + $self->pidl("long test_var;"); + $self->pidl("test_var = PyInt_AsLong($cvar);"); + $self->pidl("if (test_var < int_min || test_var > int_max) {"); + $self->indent; + $self->pidl("PyErr_Format(PyExc_OverflowError, \"Expected type %s or %s within range %lld - %lld, got %ld\",\\"); + $self->pidl(" PyInt_Type.tp_name, PyLong_Type.tp_name, int_min, int_max, test_var);"); + $self->pidl($fail); + $self->deindent; + $self->pidl("}"); + $self->pidl("$target = test_var;"); + $self->deindent; + $self->pidl("} else {"); + $self->indent; + $self->pidl("PyErr_Format(PyExc_TypeError, \"Expected type %s or %s\",\\"); + $self->pidl(" PyInt_Type.tp_name, PyLong_Type.tp_name);"); + $self->pidl($fail); + $self->deindent; + $self->pidl("}"); + $self->deindent; + $self->pidl("}"); + return; + } + + if ($actual_ctype->{TYPE} eq "STRUCT" or $actual_ctype->{TYPE} eq "INTERFACE") { + my $ctype_name = $self->use_type_variable($ctype); + unless (defined ($ctype_name)) { + error($location, "Unable to determine origin of type `" . mapTypeName($ctype) . "'"); + $self->pidl("PyErr_SetString(PyExc_TypeError, \"Can not convert C Type " . mapTypeName($ctype) . " from Python\");"); + return; + } + $self->pidl("PY_CHECK_TYPE($ctype_name, $cvar, $fail);"); + $self->pidl("if (talloc_reference($mem_ctx, pytalloc_get_mem_ctx($cvar)) == NULL) {"); + $self->indent; + $self->pidl("PyErr_NoMemory();"); + $self->pidl("$fail"); + $self->deindent; + $self->pidl("}"); + $self->assign($target, "(".mapTypeName($ctype)." *)pytalloc_get_ptr($cvar)"); + return; + } + + if ($actual_ctype->{TYPE} eq "UNION") { + my $ctype_name = $self->use_type_variable($ctype); + unless (defined ($ctype_name)) { + error($location, "Unable to determine origin of type `" . mapTypeName($ctype) . "'"); + $self->pidl("PyErr_SetString(PyExc_TypeError, \"Can not convert C Type " . mapTypeName($ctype) . " from Python\");"); + return; + } + my $export = "pyrpc_export_union($ctype_name, $mem_ctx, $switch, $cvar, \"".mapTypeName($ctype)."\")"; + $self->assign($target, "(".mapTypeName($ctype)." *)$export"); + return; + } + + if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "DATA_BLOB") { + $self->pidl("$target = data_blob_talloc($mem_ctx, PyBytes_AS_STRING($cvar), PyBytes_GET_SIZE($cvar));"); + return; + } + + if ($actual_ctype->{TYPE} eq "SCALAR" and + ($actual_ctype->{NAME} eq "string" + or $actual_ctype->{NAME} eq "nbt_string" + or $actual_ctype->{NAME} eq "nbt_name" + or $actual_ctype->{NAME} eq "wrepl_nbt_name" + or $actual_ctype->{NAME} eq "dns_string" + or $actual_ctype->{NAME} eq "dnsp_string" + or $actual_ctype->{NAME} eq "dns_name" + or $actual_ctype->{NAME} eq "ipv4address" + or $actual_ctype->{NAME} eq "ipv6address" + or $actual_ctype->{NAME} eq "dnsp_name")) { + $self->ConvertStringFromPythonData($mem_ctx, $cvar, $target, $fail); + return; + } + + if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "NTSTATUS") { + $self->pidl("$target = NT_STATUS(PyInt_AsLong($cvar));"); + return; + } + + if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "WERROR") { + $self->pidl("$target = W_ERROR(PyInt_AsLong($cvar));"); + return; + } + + if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "HRESULT") { + $self->pidl("$target = HRES_ERROR(PyInt_AsLong($cvar));"); + return; + } + + if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "string_array") { + $self->pidl("$target = pytalloc_get_ptr($cvar);"); + return; + } + + if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "pointer") { + $self->assign($target, "pytalloc_get_ptr($cvar)"); + return; + } + + fatal($location, "unknown type `$actual_ctype->{TYPE}' for ".mapTypeName($ctype) . ": $cvar"); + +} + +sub ConvertObjectFromPythonLevel($$$$$$$$$) +{ + my ($self, $env, $mem_ctx, $py_var, $e, $l, $var_name, $fail, $recurse) = @_; + my $nl = GetNextLevel($e, $l); + if ($nl and $nl->{TYPE} eq "SUBCONTEXT") { + $nl = GetNextLevel($e, $nl); + } + my $pl = GetPrevLevel($e, $l); + if ($pl and $pl->{TYPE} eq "SUBCONTEXT") { + $pl = GetPrevLevel($e, $pl); + } + + if ($recurse == 0) { + $self->pidl("if ($py_var == NULL) {"); + $self->indent; + $self->pidl("PyErr_Format(PyExc_AttributeError, \"Cannot delete NDR object: " . + mapTypeName($var_name) . "\");"); + $self->pidl($fail); + $self->deindent; + $self->pidl("}"); + } + $recurse = $recurse + 1; + + if ($l->{TYPE} eq "POINTER") { + if ($l->{POINTER_TYPE} ne "ref") { + $self->pidl("if ($py_var == Py_None) {"); + $self->indent; + $self->pidl("$var_name = NULL;"); + $self->deindent; + $self->pidl("} else {"); + $self->indent; + } + # if we want to handle more than one level of pointer in python interfaces + # then this is where we would need to allocate it + if ($l->{POINTER_TYPE} eq "ref") { + $self->pidl("$var_name = talloc_ptrtype($mem_ctx, $var_name);"); + $self->pidl("if ($var_name == NULL) {"); + $self->indent; + $self->pidl("PyErr_NoMemory();"); + $self->pidl($fail); + $self->deindent; + $self->pidl("}"); + } elsif ($nl->{TYPE} eq "DATA" and Parse::Pidl::Typelist::is_scalar($nl->{DATA_TYPE}) + and not Parse::Pidl::Typelist::scalar_is_reference($nl->{DATA_TYPE})) { + $self->pidl("$var_name = talloc_ptrtype($mem_ctx, $var_name);"); + $self->pidl("if ($var_name == NULL) {"); + $self->indent; + $self->pidl("PyErr_NoMemory();"); + $self->pidl($fail); + $self->deindent; + $self->pidl("}"); + } else { + $self->pidl("$var_name = NULL;"); + } + unless ($nl->{TYPE} eq "DATA" and Parse::Pidl::Typelist::scalar_is_reference($nl->{DATA_TYPE})) { + $var_name = get_value_of($var_name); + } + $self->ConvertObjectFromPythonLevel($env, $mem_ctx, $py_var, $e, $nl, $var_name, $fail, $recurse); + if ($l->{POINTER_TYPE} ne "ref") { + $self->deindent; + $self->pidl("}"); + } + } elsif ($l->{TYPE} eq "ARRAY") { + if ($pl && $pl->{TYPE} eq "POINTER") { + $var_name = get_pointer_to($var_name); + } + + if (is_charset_array($e, $l)) { + $self->ConvertStringFromPythonData($mem_ctx, $py_var, $var_name, $fail); + } else { + my $counter = "$e->{NAME}_cntr_$l->{LEVEL_INDEX}"; + $self->pidl("PY_CHECK_TYPE(&PyList_Type, $py_var, $fail);"); + $self->pidl("{"); + $self->indent; + $self->pidl("int $counter;"); + if (ArrayDynamicallyAllocated($e, $l)) { + $self->pidl("$var_name = talloc_array_ptrtype($mem_ctx, $var_name, PyList_GET_SIZE($py_var));"); + $self->pidl("if (!$var_name) { $fail; }"); + $self->pidl("talloc_set_name_const($var_name, \"ARRAY: $var_name\");"); + } else { + $self->pidl("if (ARRAY_SIZE($var_name) != PyList_GET_SIZE($py_var)) {"); + $self->indent; + $self->pidl("PyErr_Format(PyExc_TypeError, \"Expected list of type %s, length %zu, got %zd\", Py_TYPE($py_var)->tp_name, ARRAY_SIZE($var_name), PyList_GET_SIZE($py_var));"); + $self->pidl("$fail"); + $self->deindent; + $self->pidl("}"); + } + $self->pidl("for ($counter = 0; $counter < PyList_GET_SIZE($py_var); $counter++) {"); + $self->indent; + $self->ConvertObjectFromPythonLevel($env, $var_name, "PyList_GET_ITEM($py_var, $counter)", $e, $nl, $var_name."[$counter]", $fail, 0); + $self->deindent; + $self->pidl("}"); + $self->deindent; + $self->pidl("}"); + } + } elsif ($l->{TYPE} eq "DATA") { + if (not Parse::Pidl::Typelist::is_scalar($l->{DATA_TYPE})) { + $var_name = get_pointer_to($var_name); + } + $self->ConvertObjectFromPythonData($mem_ctx, $py_var, $l->{DATA_TYPE}, $var_name, $fail, $e->{ORIGINAL}); + } elsif ($l->{TYPE} eq "SWITCH") { + $var_name = get_pointer_to($var_name); + my $switch = ParseExpr($l->{SWITCH_IS}, $env, $e); + my $switch_ptr = "$e->{NAME}_switch_$l->{LEVEL_INDEX}"; + $self->pidl("{"); + $self->indent; + my $union_type = mapTypeName($nl->{DATA_TYPE}); + $self->pidl("$union_type *$switch_ptr;"); + $self->ConvertObjectFromPythonData($mem_ctx, $py_var, $nl->{DATA_TYPE}, $switch_ptr, $fail, $e->{ORIGINAL}, $switch); + $self->fail_on_null($switch_ptr, $fail); + $self->assign($var_name, "$switch_ptr"); + $self->deindent; + $self->pidl("}"); + } elsif ($l->{TYPE} eq "SUBCONTEXT") { + $self->ConvertObjectFromPythonLevel($env, $mem_ctx, $py_var, $e, $nl, $var_name, $fail, $recurse); + } else { + fatal($e->{ORIGINAL}, "unknown level type $l->{TYPE}"); + } +} + +sub ConvertObjectFromPython($$$$$$$) +{ + my ($self, $env, $mem_ctx, $ctype, $cvar, $target, $fail) = @_; + my $recurse = 0; + + $self->ConvertObjectFromPythonLevel($env, $mem_ctx, $cvar, $ctype, $ctype->{LEVELS}[0], $target, $fail, $recurse); +} + +sub ConvertScalarToPython($$$$) +{ + my ($self, $ctypename, $cvar, $mem_ctx) = @_; + + die("expected string for $cvar, not $ctypename") if (ref($ctypename) eq "HASH"); + + $ctypename = expandAlias($ctypename); + + if ($ctypename =~ /^(int64|dlong)$/) { + return "ndr_PyLong_FromLongLong($cvar)"; + } + + if ($ctypename =~ /^(uint64|hyper|NTTIME_hyper|NTTIME|NTTIME_1sec|udlong|udlongr|uid_t|gid_t)$/) { + return "ndr_PyLong_FromUnsignedLongLong($cvar)"; + } + + if ($ctypename =~ /^(char|int|int8|int16|int32|time_t)$/) { + return "PyInt_FromLong($cvar)"; + } + + # Needed to ensure unsigned values in a 32 or 16 bit enum is + # cast correctly to a uint32_t, not sign extended to a a + # possibly 64 bit unsigned long. (enums are signed in C, + # unsigned in NDR) + if ($ctypename =~ /^(uint32|uint3264)$/) { + return "ndr_PyLong_FromUnsignedLongLong((uint32_t)$cvar)"; + } + + if ($ctypename =~ /^(uint|uint8|uint16|uint1632)$/) { + return "PyInt_FromLong((uint16_t)$cvar)"; + } + + if ($ctypename eq "DATA_BLOB") { + return "PyBytes_FromStringAndSize((char *)($cvar).data, ($cvar).length)"; + } + + if ($ctypename eq "NTSTATUS") { + return "PyErr_FromNTSTATUS($cvar)"; + } + + if ($ctypename eq "WERROR") { + return "PyErr_FromWERROR($cvar)"; + } + + if ($ctypename eq "HRESULT") { + return "PyErr_FromHRESULT($cvar)"; + } + + if (($ctypename eq "string" or $ctypename eq "nbt_string" or $ctypename eq "nbt_name" or $ctypename eq "wrepl_nbt_name")) { + return "PyString_FromStringOrNULL($cvar)"; + } + + if (($ctypename eq "dns_string" or $ctypename eq "dns_name")) { + return "PyString_FromStringOrNULL($cvar)"; + } + + # Not yet supported + if ($ctypename eq "string_array") { + return "pytalloc_GenericObject_reference_ex($mem_ctx, $cvar)"; + } + if ($ctypename eq "ipv4address") { return "PyString_FromStringOrNULL($cvar)"; } + if ($ctypename eq "ipv6address") { return "PyString_FromStringOrNULL($cvar)"; } + if ($ctypename eq "dnsp_name") { return "PyString_FromStringOrNULL($cvar)"; } + if ($ctypename eq "dnsp_string") { return "PyString_FromStringOrNULL($cvar)"; } + if ($ctypename eq "pointer") { + return "pytalloc_GenericObject_reference_ex($mem_ctx, $cvar)"; + } + + die("Unknown scalar type $ctypename"); +} + +sub ConvertObjectToPythonData($$$$$;$$) +{ + my ($self, $mem_ctx, $ctype, $cvar, $location, $switch) = @_; + + die("undef type for $cvar") unless(defined($ctype)); + + $ctype = resolveType($ctype); + + my $actual_ctype = $ctype; + if ($actual_ctype->{TYPE} eq "TYPEDEF") { + $actual_ctype = $actual_ctype->{DATA}; + } + + if ($actual_ctype->{TYPE} eq "ENUM") { + return $self->ConvertScalarToPython(Parse::Pidl::Typelist::enum_type_fn($actual_ctype), $cvar, $mem_ctx); + } elsif ($actual_ctype->{TYPE} eq "BITMAP") { + return $self->ConvertScalarToPython(Parse::Pidl::Typelist::bitmap_type_fn($actual_ctype), $cvar, $mem_ctx); + } elsif ($actual_ctype->{TYPE} eq "SCALAR") { + return $self->ConvertScalarToPython($actual_ctype->{NAME}, $cvar, $mem_ctx); + } elsif ($actual_ctype->{TYPE} eq "UNION") { + my $ctype_name = $self->use_type_variable($ctype); + unless (defined($ctype_name)) { + error($location, "Unable to determine origin of type `" . mapTypeName($ctype) . "'"); + return "NULL"; # FIXME! + } + return "pyrpc_import_union($ctype_name, $mem_ctx, $switch, $cvar, \"".mapTypeName($ctype)."\")"; + } elsif ($actual_ctype->{TYPE} eq "STRUCT" or $actual_ctype->{TYPE} eq "INTERFACE") { + my $ctype_name = $self->use_type_variable($ctype); + unless (defined($ctype_name)) { + error($location, "Unable to determine origin of type `" . mapTypeName($ctype) . "'"); + return "NULL"; # FIXME! + } + return "pytalloc_reference_ex($ctype_name, $mem_ctx, $cvar)"; + } + + fatal($location, "unknown type $actual_ctype->{TYPE} for ".mapTypeName($ctype) . ": $cvar"); +} + +sub fail_on_null($$$) +{ + my ($self, $var, $fail) = @_; + $self->pidl("if ($var == NULL) {"); + $self->indent; + $self->pidl($fail); + $self->deindent; + $self->pidl("}"); +} + +sub ConvertObjectToPythonLevel($$$$$$$) +{ + my ($self, $mem_ctx, $env, $e, $l, $var_name, $py_var, $fail, $recurse) = @_; + my $nl = GetNextLevel($e, $l); + if ($nl and $nl->{TYPE} eq "SUBCONTEXT") { + $nl = GetNextLevel($e, $nl); + } + my $pl = GetPrevLevel($e, $l); + if ($pl and $pl->{TYPE} eq "SUBCONTEXT") { + $pl = GetPrevLevel($e, $pl); + } + + if ($l->{TYPE} eq "POINTER") { + if ($l->{POINTER_TYPE} ne "ref") { + if ($recurse == 0) { + $self->pidl("if ($var_name == NULL) {"); + $self->indent; + $self->pidl("$py_var = Py_None;"); + $self->pidl("Py_INCREF($py_var);"); + $self->deindent; + $self->pidl("} else {"); + $self->indent; + } else { + $self->pidl("{"); + $self->indent; + } + $recurse = $recurse + 1; + } + my $var_name2 = $var_name; + my $recurse2 = $recurse; + unless ($nl->{TYPE} eq "DATA" and Parse::Pidl::Typelist::scalar_is_reference($nl->{DATA_TYPE})) { + $var_name2 = get_value_of($var_name); + $recurse2 = 0; + } + $self->ConvertObjectToPythonLevel($var_name, $env, $e, $nl, $var_name2, $py_var, $fail, $recurse2); + if ($l->{POINTER_TYPE} ne "ref") { + $self->deindent; + $self->pidl("}"); + } + } elsif ($l->{TYPE} eq "ARRAY") { + if ($pl && $pl->{TYPE} eq "POINTER") { + $var_name = get_pointer_to($var_name); + } + + if (is_charset_array($e, $l)) { + # FIXME: Use Unix charset setting rather than utf-8 + $self->pidl("if ($var_name == NULL) {"); + $self->indent; + $self->pidl("$py_var = Py_None;"); + $self->pidl("Py_INCREF($py_var);"); + $self->deindent; + $self->pidl("} else {"); + $self->indent; + $self->pidl("$py_var = PyUnicode_Decode($var_name, strlen($var_name), \"utf-8\", \"ignore\");"); + $self->deindent; + $self->pidl("}"); + } else { + die("No SIZE_IS for array $var_name") unless (defined($l->{SIZE_IS})); + my $length = $l->{SIZE_IS}; + if (defined($l->{LENGTH_IS})) { + $length = $l->{LENGTH_IS}; + } + + $length = ParseExpr($length, $env, $e); + $self->pidl("$py_var = PyList_New($length);"); + $self->fail_on_null($py_var, $fail); + $self->pidl("{"); + $self->indent; + my $counter = "$e->{NAME}_cntr_$l->{LEVEL_INDEX}"; + $self->pidl("int $counter;"); + $self->pidl("for ($counter = 0; $counter < ($length); $counter++) {"); + $self->indent; + my $member_var = "py_$e->{NAME}_$l->{LEVEL_INDEX}"; + $self->pidl("PyObject *$member_var;"); + $self->ConvertObjectToPythonLevel($var_name, $env, $e, $nl, $var_name."[$counter]", $member_var, $fail, $recurse); + $self->pidl("PyList_SetItem($py_var, $counter, $member_var);"); + $self->deindent; + $self->pidl("}"); + $self->deindent; + $self->pidl("}"); + } + } elsif ($l->{TYPE} eq "SWITCH") { + $var_name = get_pointer_to($var_name); + my $switch = ParseExpr($l->{SWITCH_IS}, $env, $e); + my $conv = $self->ConvertObjectToPythonData($mem_ctx, $nl->{DATA_TYPE}, $var_name, $e->{ORIGINAL}, $switch); + $self->pidl("$py_var = $conv;"); + $self->fail_on_null($py_var, $fail); + + } elsif ($l->{TYPE} eq "DATA") { + if (not Parse::Pidl::Typelist::is_scalar($l->{DATA_TYPE})) { + $var_name = get_pointer_to($var_name); + } + my $conv = $self->ConvertObjectToPythonData($mem_ctx, $l->{DATA_TYPE}, $var_name, $e->{ORIGINAL}); + $self->pidl("$py_var = $conv;"); + } elsif ($l->{TYPE} eq "SUBCONTEXT") { + $self->ConvertObjectToPythonLevel($mem_ctx, $env, $e, $nl, $var_name, $py_var, $fail, $recurse); + } else { + fatal($e->{ORIGINAL}, "Unknown level type $l->{TYPE} $var_name"); + } +} + +sub ConvertObjectToPython($$$$$$) +{ + my ($self, $mem_ctx, $env, $ctype, $cvar, $py_var, $fail) = @_; + my $recurse = 0; + + $self->ConvertObjectToPythonLevel($mem_ctx, $env, $ctype, $ctype->{LEVELS}[0], $cvar, $py_var, $fail, $recurse); +} + +sub Parse($$$$$) +{ + my($self,$basename,$ndr,$ndr_hdr,$hdr) = @_; + + $self->{BASENAME} = $basename; + + $self->pidl_hdr(" +/* Python wrapper functions auto-generated by pidl */ +#define PY_SSIZE_T_CLEAN 1 /* We use Py_ssize_t for PyArg_ParseTupleAndKeywords */ +#include <Python.h> +#include \"python/py3compat.h\" +#include \"includes.h\" +#include <pytalloc.h> +#include \"librpc/rpc/pyrpc.h\" +#include \"librpc/rpc/pyrpc_util.h\" +#include \"$hdr\" +#include \"$ndr_hdr\" + +/* + * These functions are here to ensure they can be optimized out by + * the compiler based on the constant input values + */ + +static inline unsigned long long ndr_sizeof2uintmax(size_t var_size) +{ + switch (var_size) { + case 8: + return UINT64_MAX; + case 4: + return UINT32_MAX; + case 2: + return UINT16_MAX; + case 1: + return UINT8_MAX; + } + + return 0; +} + +static inline long long ndr_sizeof2intmax(size_t var_size) +{ + switch (var_size) { + case 8: + return INT64_MAX; + case 4: + return INT32_MAX; + case 2: + return INT16_MAX; + case 1: + return INT8_MAX; + } + + return 0; +} + +static inline PyObject *ndr_PyLong_FromLongLong(long long v) +{ + if (v > LONG_MAX || v < LONG_MIN) { + return PyLong_FromLongLong(v); + } else { + return PyInt_FromLong(v); + } +} + +static inline PyObject *ndr_PyLong_FromUnsignedLongLong(unsigned long long v) +{ + if (v > LONG_MAX) { + return PyLong_FromUnsignedLongLong(v); + } else { + return PyInt_FromLong(v); + } +} + +"); + + foreach my $x (@$ndr) { + ($x->{TYPE} eq "IMPORT") && $self->Import(@{$x->{PATHS}}); + ($x->{TYPE} eq "INTERFACE") && $self->Interface($x, $basename); + } + + $self->pidl("static PyMethodDef $basename\_methods[] = {"); + $self->indent; + foreach (@{$self->{module_methods}}) { + my ($fn_name, $pyfn_name, $flags, $doc) = @$_; + $self->pidl("{ \"$fn_name\", (PyCFunction)$pyfn_name, $flags, $doc },"); + } + + $self->pidl("{ NULL, NULL, 0, NULL }"); + $self->deindent; + $self->pidl("};"); + + $self->pidl(""); + + $self->pidl("static struct PyModuleDef moduledef = {"); + $self->indent; + $self->pidl("PyModuleDef_HEAD_INIT,"); + $self->pidl(".m_name = \"$basename\","); + $self->pidl(".m_doc = \"$basename DCE/RPC\","); + $self->pidl(".m_size = -1,"); + $self->pidl(".m_methods = $basename\_methods,"); + $self->deindent; + $self->pidl("};"); + + $self->pidl("MODULE_INIT_FUNC($basename)"); + $self->pidl("{"); + $self->indent; + $self->pidl("PyObject *m;"); + foreach my $h (@{$self->{module_imports}}) { + $self->pidl("PyObject *$h->{'key'};"); + } + $self->pidl(""); + + foreach my $h (@{$self->{module_imports}}) { + my $var_name = $h->{'key'}; + my $module_path = $h->{'val'}; + $self->pidl("$var_name = PyImport_ImportModule(\"$module_path\");"); + $self->pidl("if ($var_name == NULL)"); + $self->pidl("\treturn NULL;"); + $self->pidl(""); + } + + foreach my $h (@{$self->{type_imports}}) { + my $type_var = "$h->{'key'}\_Type"; + my $module_path = $h->{'val'}; + $self->pidl_hdr("static PyTypeObject *$type_var;\n"); + my $pretty_name = PrettifyTypeName($h->{'key'}, $module_path); + my $module_var = "dep_$module_path"; + $module_var =~ s/\./_/g; + $self->pidl("$type_var = (PyTypeObject *)PyObject_GetAttrString($module_var, \"$pretty_name\");"); + $self->pidl("if ($type_var == NULL)"); + $self->pidl("\treturn NULL;"); + $self->pidl(""); + } + + $self->pidl($_) foreach (@{$self->{prereadycode}}); + + foreach (@{$self->{ready_types}}) { + $self->pidl("if (PyType_Ready($_) < 0)"); + $self->pidl("\treturn NULL;"); + } + + $self->pidl($_) foreach (@{$self->{postreadycode}}); + + foreach (@{$self->{patch_type_calls}}) { + my ($typename, $cvar) = @$_; + $self->pidl("#ifdef PY_".uc($typename)."_PATCH"); + $self->pidl("PY_".uc($typename)."_PATCH($cvar);"); + $self->pidl("#endif"); + } + + $self->pidl(""); + + $self->pidl("m = PyModule_Create(&moduledef);"); + $self->pidl("if (m == NULL)"); + $self->pidl("\treturn NULL;"); + $self->pidl(""); + foreach my $h (@{$self->{constants}}) { + my $pretty_name = PrettifyTypeName($h->{'key'}, $basename); + my $py_obj; + my ($ctype, $cvar) = @{$h->{'val'}}; + if ($cvar =~ /^[0-9]+$/ or $cvar =~ /^0x[0-9a-fA-F]+$/) { + $py_obj = "ndr_PyLong_FromUnsignedLongLong($cvar)"; + } elsif ($cvar =~ /^".*"$/) { + $py_obj = "PyStr_FromString($cvar)"; + } else { + $py_obj = $self->ConvertObjectToPythonData("NULL", expandAlias($ctype), $cvar, undef); + } + + $self->pidl("PyModule_AddObject(m, \"$pretty_name\", $py_obj);"); + } + + foreach (@{$self->{module_objects}}) { + my ($object_name, $c_name) = @$_; + $self->pidl("Py_INCREF($c_name);"); + $self->pidl("PyModule_AddObject(m, \"$object_name\", $c_name);"); + } + + $self->pidl("#ifdef PY_MOD_".uc($basename)."_PATCH"); + $self->pidl("PY_MOD_".uc($basename)."_PATCH(m);"); + $self->pidl("#endif"); + + $self->pidl("return m;"); + $self->pidl(""); + $self->deindent; + $self->pidl("}"); + return ($self->{res_hdr} . $self->{res}); +} + +1; diff --git a/tools/pidl/lib/Parse/Pidl/Samba4/TDR.pm b/tools/pidl/lib/Parse/Pidl/Samba4/TDR.pm new file mode 100644 index 00000000..c0749304 --- /dev/null +++ b/tools/pidl/lib/Parse/Pidl/Samba4/TDR.pm @@ -0,0 +1,283 @@ +################################################### +# Trivial Parser Generator +# Copyright jelmer@samba.org 2005-2007 +# released under the GNU GPL + +package Parse::Pidl::Samba4::TDR; +use Parse::Pidl qw(fatal); +use Parse::Pidl::Util qw(has_property ParseExpr is_constant); +use Parse::Pidl::Samba4 qw(is_intree choose_header); +use Parse::Pidl::Typelist qw(mapTypeName); + +use Exporter; +@ISA = qw(Exporter); +@EXPORT_OK = qw(ParserType $ret $ret_hdr); + +use vars qw($VERSION); +$VERSION = '0.01'; + +use strict; + +sub new($) { + my ($class) = shift; + my $self = { ret => "", ret_hdr => "", tabs => "" }; + bless($self, $class); +} + +sub indent($) { my $self = shift; $self->{tabs}.="\t"; } +sub deindent($) { my $self = shift; $self->{tabs} = substr($self->{tabs}, 1); } +sub pidl($$) { my $self = shift; $self->{ret} .= $self->{tabs}.(shift)."\n"; } +sub pidl_hdr($$) { my $self = shift; $self->{ret_hdr} .= (shift)."\n"; } +sub typearg($) { + my $t = shift; + return(", const char *name") if ($t eq "print"); + return(", TALLOC_CTX *mem_ctx") if ($t eq "pull"); + return(""); +} + +sub fn_declare($$$) +{ + my ($self, $p, $d) = @_; + if ($p) { + $self->pidl($d); $self->pidl_hdr("$d;"); + } else { + $self->pidl("static $d"); + } +} + +sub ContainsArray($) +{ + my $e = shift; + foreach (@{$e->{ELEMENTS}}) { + next if (has_property($_, "charset") and + scalar(@{$_->{ARRAY_LEN}}) == 1); + return 1 if (defined($_->{ARRAY_LEN}) and + scalar(@{$_->{ARRAY_LEN}}) > 0); + } + return 0; +} + +sub ParserElement($$$$) +{ + my ($self, $e,$t,$env) = @_; + my $switch = ""; + my $array = ""; + my $name = ""; + my $mem_ctx = "mem_ctx"; + + fatal($e,"Pointers not supported in TDR") if ($e->{POINTERS} > 0); + fatal($e,"size_is() not supported in TDR") if (has_property($e, "size_is")); + fatal($e,"length_is() not supported in TDR") if (has_property($e, "length_is")); + + if ($t eq "print") { + $name = ", \"$e->{NAME}\"$array"; + } + + if (has_property($e, "flag")) { + $self->pidl("{"); + $self->indent; + $self->pidl("uint32_t saved_flags = tdr->flags;"); + $self->pidl("tdr->flags |= $e->{PROPERTIES}->{flag};"); + } + + if (has_property($e, "charset")) { + fatal($e,"charset() on non-array element") unless (defined($e->{ARRAY_LEN}) and scalar(@{$e->{ARRAY_LEN}}) > 0); + + my $len = ParseExpr(@{$e->{ARRAY_LEN}}[0], $env, $e); + if ($len eq "*") { $len = "-1"; } + $name = ", mem_ctx" if ($t eq "pull"); + $self->pidl("TDR_CHECK(tdr_$t\_charset(tdr$name, &v->$e->{NAME}, $len, sizeof($e->{TYPE}_t), CH_$e->{PROPERTIES}->{charset}));"); + return; + } + + if (has_property($e, "switch_is")) { + $switch = ", " . ParseExpr($e->{PROPERTIES}->{switch_is}, $env, $e); + } + + if (defined($e->{ARRAY_LEN}) and scalar(@{$e->{ARRAY_LEN}}) > 0) { + my $len = ParseExpr($e->{ARRAY_LEN}[0], $env, $e); + + if ($t eq "pull" and not is_constant($len)) { + $self->pidl("TDR_ALLOC(mem_ctx, v->$e->{NAME}, $len);"); + $mem_ctx = "v->$e->{NAME}"; + } + + $self->pidl("for (i = 0; i < $len; i++) {"); + $self->indent; + $array = "[i]"; + } + + if ($t eq "pull") { + $name = ", $mem_ctx"; + } + + if (has_property($e, "value") && $t eq "push") { + $self->pidl("v->$e->{NAME} = ".ParseExpr($e->{PROPERTIES}->{value}, $env, $e).";"); + } + + $self->pidl("TDR_CHECK(tdr_$t\_$e->{TYPE}(tdr$name$switch, &v->$e->{NAME}$array));"); + + if ($array) { $self->deindent; $self->pidl("}"); } + + if (has_property($e, "flag")) { + $self->pidl("tdr->flags = saved_flags;"); + $self->deindent; + $self->pidl("}"); + } +} + +sub ParserStruct($$$$$) +{ + my ($self, $e,$t,$p) = @_; + + $self->fn_declare($p,"NTSTATUS tdr_$t\_$e->{NAME} (struct tdr_$t *tdr".typearg($t).", struct $e->{NAME} *v)"); + $self->pidl("{"); $self->indent; + $self->pidl("int i;") if (ContainsArray($e)); + + if ($t eq "print") { + $self->pidl("tdr->print(tdr, \"\%-25s: struct $e->{NAME}\", name);"); + $self->pidl("tdr->level++;"); + } + + my %env = map { $_->{NAME} => "v->$_->{NAME}" } @{$e->{ELEMENTS}}; + $env{"this"} = "v"; + $self->ParserElement($_, $t, \%env) foreach (@{$e->{ELEMENTS}}); + + if ($t eq "print") { + $self->pidl("tdr->level--;"); + } + + $self->pidl("return NT_STATUS_OK;"); + + $self->deindent; $self->pidl("}"); +} + +sub ParserUnion($$$$) +{ + my ($self, $e,$t,$p) = @_; + + $self->fn_declare($p,"NTSTATUS tdr_$t\_$e->{NAME}(struct tdr_$t *tdr".typearg($t).", int level, union $e->{NAME} *v)"); + $self->pidl("{"); $self->indent; + $self->pidl("int i;") if (ContainsArray($e)); + + if ($t eq "print") { + $self->pidl("tdr->print(tdr, \"\%-25s: union $e->{NAME}\", name);"); + $self->pidl("tdr->level++;"); + } + + $self->pidl("switch (level) {"); $self->indent; + foreach (@{$e->{ELEMENTS}}) { + if (has_property($_, "case")) { + $self->pidl("case " . $_->{PROPERTIES}->{case} . ":"); + } elsif (has_property($_, "default")) { + $self->pidl("default:"); + } + $self->indent; $self->ParserElement($_, $t, {}); $self->deindent; + $self->pidl("break;"); + } + $self->deindent; $self->pidl("}"); + + if ($t eq "print") { + $self->pidl("tdr->level--;"); + } + + $self->pidl("return NT_STATUS_OK;\n"); + $self->deindent; $self->pidl("}"); +} + +sub ParserBitmap($$$$) +{ + my ($self,$e,$t,$p) = @_; + return if ($p); + $self->pidl("#define tdr_$t\_$e->{NAME} tdr_$t\_" . Parse::Pidl::Typelist::bitmap_type_fn($e)); +} + +sub ParserEnum($$$$) +{ + my ($self,$e,$t,$p) = @_; + my $bt = Parse::Pidl::Typelist::enum_type_fn($e); + my $mt = mapTypeName($bt); + + $self->fn_declare($p, "NTSTATUS tdr_$t\_$e->{NAME} (struct tdr_$t *tdr".typearg($t).", enum $e->{NAME} *v)"); + $self->pidl("{"); + if ($t eq "pull") { + $self->pidl("\t$mt r;"); + $self->pidl("\tTDR_CHECK(tdr_$t\_$bt(tdr, mem_ctx, \&r));"); + $self->pidl("\t*v = r;"); + } elsif ($t eq "push") { + $self->pidl("\tTDR_CHECK(tdr_$t\_$bt(tdr, ($mt *)v));"); + } elsif ($t eq "print") { + $self->pidl("\t/* FIXME */"); + } + $self->pidl("\treturn NT_STATUS_OK;"); + $self->pidl("}"); +} + +sub ParserTypedef($$$$) +{ + my ($self, $e,$t,$p) = @_; + + $self->ParserType($e->{DATA},$t); +} + +sub ParserType($$$) +{ + my ($self, $e,$t) = @_; + + return if (has_property($e, "no$t")); + + my $handlers = { + STRUCT => \&ParserStruct, UNION => \&ParserUnion, + ENUM => \&ParserEnum, BITMAP => \&ParserBitmap, + TYPEDEF => \&ParserTypedef + }; + + $handlers->{$e->{TYPE}}->($self, $e, $t, has_property($e, "public")) + if (defined($handlers->{$e->{TYPE}})); + + $self->pidl(""); +} + +sub ParserInterface($$) +{ + my ($self,$x) = @_; + + $self->pidl_hdr("#ifndef __TDR_$x->{NAME}_HEADER__"); + $self->pidl_hdr("#define __TDR_$x->{NAME}_HEADER__"); + + foreach (@{$x->{DATA}}) { + $self->ParserType($_, "pull"); + $self->ParserType($_, "push"); + $self->ParserType($_, "print"); + } + + $self->pidl_hdr("#endif /* __TDR_$x->{NAME}_HEADER__ */"); +} + +sub Parser($$$$) +{ + my ($self,$idl,$hdrname,$baseheader) = @_; + $self->pidl("/* autogenerated by pidl */"); + if (is_intree()) { + $self->pidl("#include \"includes.h\""); + } else { + $self->pidl("#include <stdio.h>"); + $self->pidl("#include <stdbool.h>"); + $self->pidl("#include <stdlib.h>"); + $self->pidl("#include <stdint.h>"); + $self->pidl("#include <stdarg.h>"); + $self->pidl("#include <string.h>"); + $self->pidl("#include <core/ntstatus.h>"); + } + $self->pidl("#include \"$hdrname\""); + $self->pidl(""); + $self->pidl_hdr("/* autogenerated by pidl */"); + $self->pidl_hdr("#include \"$baseheader\""); + $self->pidl_hdr(choose_header("lib/tdr/tdr.h", "tdr.h")); + $self->pidl_hdr(""); + + foreach (@$idl) { $self->ParserInterface($_) if ($_->{TYPE} eq "INTERFACE"); } + return ($self->{ret_hdr}, $self->{ret}); +} + +1; diff --git a/tools/pidl/lib/Parse/Pidl/Samba4/Template.pm b/tools/pidl/lib/Parse/Pidl/Samba4/Template.pm new file mode 100644 index 00000000..175bb124 --- /dev/null +++ b/tools/pidl/lib/Parse/Pidl/Samba4/Template.pm @@ -0,0 +1,92 @@ +################################################### +# server template function generator +# Copyright tridge@samba.org 2003 +# released under the GNU GPL + +package Parse::Pidl::Samba4::Template; + +use vars qw($VERSION); +$VERSION = '0.01'; + +use Parse::Pidl::Util qw(genpad); + +use strict; + +my($res); + +##################################################################### +# produce boilerplate code for a interface +sub Template($) +{ + my($interface) = shift; + my($data) = $interface->{DATA}; + my $name = $interface->{NAME}; + + $res .= +"/* + Unix SMB/CIFS implementation. + + endpoint server for the $name pipe + + Copyright (C) YOUR NAME HERE YEAR + + SPDX-License-Identifier: GPL-3.0-or-later +*/ + +#include \"includes.h\" +#include \"rpc_server/dcerpc_server.h\" +#include \"librpc/gen_ndr/ndr_$name.h\" +#include \"rpc_server/common/common.h\" + +"; + + foreach my $d (@{$data}) { + if ($d->{TYPE} eq "FUNCTION") { + my $fname = $d->{NAME}; + my $pad = genpad("static $d->{RETURN_TYPE} dcesrv_$fname"); + $res .= +" +/* + $fname +*/ + +static $d->{RETURN_TYPE} dcesrv_$fname(struct dcesrv_call_state *dce_call, +$pad"."TALLOC_CTX *mem_ctx, +$pad"."struct $fname *r) +{ +"; + + if ($d->{RETURN_TYPE} eq "void") { + $res .= "\tDCESRV_FAULT_VOID(DCERPC_FAULT_OP_RNG_ERROR);\n"; + } else { + $res .= "\tDCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);\n"; + } + + $res .= "} + +"; + } + } + + $res .= +" +/* include the generated boilerplate */ +#include \"librpc/gen_ndr/ndr_$name\_s.c\" +" +} + + +##################################################################### +# parse a parsed IDL structure back into an IDL file +sub Parse($) +{ + my($idl) = shift; + $res = ""; + foreach my $x (@{$idl}) { + ($x->{TYPE} eq "INTERFACE") && + Template($x); + } + return $res; +} + +1; diff --git a/tools/pidl/lib/Parse/Pidl/Typelist.pm b/tools/pidl/lib/Parse/Pidl/Typelist.pm new file mode 100644 index 00000000..774554f0 --- /dev/null +++ b/tools/pidl/lib/Parse/Pidl/Typelist.pm @@ -0,0 +1,354 @@ +################################################### +# Samba4 parser generator for IDL structures +# Copyright jelmer@samba.org 2005 +# released under the GNU GPL + +package Parse::Pidl::Typelist; + +require Exporter; +@ISA = qw(Exporter); +@EXPORT_OK = qw(hasType getType resolveType mapTypeName scalar_is_reference expandAlias + mapScalarType addType typeIs is_signed is_scalar enum_type_fn + bitmap_type_fn mapType typeHasBody is_fixed_size_scalar +); +use vars qw($VERSION); +$VERSION = '0.01'; + +use Parse::Pidl::Util qw(has_property); +use strict; + +my %types = (); + +my @reference_scalars = ( + "string", "string_array", "nbt_string", "dns_string", + "wrepl_nbt_name", "dnsp_name", "dnsp_string", + "ipv4address", "ipv6address" +); + +my @non_fixed_size_scalars = ( + "string", "string_array", "nbt_string", "dns_string", + "wrepl_nbt_name", "dnsp_name", "dnsp_string" +); + +# a list of known scalar types +my %scalars = ( + "void" => "void", + "char" => "char", + "int8" => "int8_t", + "uint8" => "uint8_t", + "int16" => "int16_t", + "uint16" => "uint16_t", + "int1632" => "int16_t", + "uint1632" => "uint16_t", + "int32" => "int32_t", + "uint32" => "uint32_t", + "int3264" => "int32_t", + "uint3264" => "uint32_t", + "hyper" => "uint64_t", + "dlong" => "int64_t", + "udlong" => "uint64_t", + "udlongr" => "uint64_t", + "double" => "double", + "pointer" => "void*", + "DATA_BLOB" => "DATA_BLOB", + "string" => "const char *", + "string_array" => "const char **", + "time_t" => "time_t", + "uid_t" => "uid_t", + "gid_t" => "gid_t", + "NTTIME" => "NTTIME", + "NTTIME_1sec" => "NTTIME", + "NTTIME_hyper" => "NTTIME", + "WERROR" => "WERROR", + "HRESULT" => "HRESULT", + "NTSTATUS" => "NTSTATUS", + "COMRESULT" => "COMRESULT", + "dns_string" => "const char *", + "nbt_string" => "const char *", + "wrepl_nbt_name"=> "struct nbt_name *", + "ipv4address" => "const char *", + "ipv6address" => "const char *", + "dnsp_name" => "const char *", + "dnsp_string" => "const char *", +); + +my %aliases = ( + "error_status_t" => "uint32", + "boolean8" => "uint8", + "boolean32" => "uint32", + "DWORD" => "uint32", + "uint" => "uint32", + "int" => "int32", + "WORD" => "uint16", + "char" => "uint8", + "long" => "int32", + "short" => "int16", + "HYPER_T" => "hyper", + "mode_t" => "uint32", +); + +sub expandAlias($) +{ + my $name = shift; + + return $aliases{$name} if defined($aliases{$name}); + + return $name; +} + +# map from a IDL type to a C header type +sub mapScalarType($) +{ + my $name = shift; + + # it's a bug when a type is not in the list + # of known scalars or has no mapping + return $scalars{$name} if defined($scalars{$name}); + + die("Unknown scalar type $name"); +} + +sub addType($) +{ + my $t = shift; + $types{$t->{NAME}} = $t; +} + +sub resolveType($) +{ + my ($ctype) = @_; + + if (not hasType($ctype)) { + # assume struct typedef + return { TYPE => "TYPEDEF", NAME => $ctype, DATA => { TYPE => "STRUCT" } }; + } else { + return getType($ctype); + } + + return $ctype; +} + +sub getType($) +{ + my $t = shift; + return ($t) if (ref($t) eq "HASH" and not defined($t->{NAME})); + return undef if not hasType($t); + return $types{$t->{NAME}} if (ref($t) eq "HASH"); + return $types{$t}; +} + +sub typeIs($$); +sub typeIs($$) +{ + my ($t,$tt) = @_; + + if (ref($t) eq "HASH") { + return 1 if ($t->{TYPE} eq "TYPEDEF" and $t->{DATA}->{TYPE} eq $tt); + return 1 if ($t->{TYPE} eq $tt); + return 0; + } + if (hasType($t) and getType($t)->{TYPE} eq "TYPEDEF") { + return typeIs(getType($t)->{DATA}, $tt); + } + return 0; +} + +sub hasType($) +{ + my $t = shift; + if (ref($t) eq "HASH") { + return 1 if (not defined($t->{NAME})); + return 1 if (defined($types{$t->{NAME}}) and + $types{$t->{NAME}}->{TYPE} eq $t->{TYPE}); + return 0; + } + return 1 if defined($types{$t}); + return 0; +} + +sub is_signed($) +{ + my $t = shift; + + return ($t eq "int8" + or $t eq "int16" + or $t eq "int32" + or $t eq "dlong" + or $t eq "int" + or $t eq "long" + or $t eq "short"); +} + +sub is_scalar($) +{ + sub is_scalar($); + my $type = shift; + + return 1 if (ref($type) eq "HASH" and + ($type->{TYPE} eq "SCALAR" or $type->{TYPE} eq "ENUM" or + $type->{TYPE} eq "BITMAP")); + + if (my $dt = getType($type)) { + return is_scalar($dt->{DATA}) if ($dt->{TYPE} eq "TYPEDEF"); + return 1 if ($dt->{TYPE} eq "SCALAR" or $dt->{TYPE} eq "ENUM" or + $dt->{TYPE} eq "BITMAP"); + } + + return 0; +} + +sub is_fixed_size_scalar($) +{ + my $name = shift; + + return 0 unless is_scalar($name); + return 0 if (grep(/^$name$/, @non_fixed_size_scalars)); + return 1; +} + +sub scalar_is_reference($) +{ + my $name = shift; + + return 1 if (grep(/^$name$/, @reference_scalars)); + return 0; +} + +sub RegisterScalars() +{ + foreach (keys %scalars) { + addType({ + NAME => $_, + TYPE => "TYPEDEF", + BASEFILE => "<builtin>", + DATA => { + TYPE => "SCALAR", + NAME => $_ + } + } + ); + } +} + +sub enum_type_fn($) +{ + my $enum = shift; + $enum->{TYPE} eq "ENUM" or die("not an enum"); + + # for typedef enum { } we need to check $enum->{PARENT} + if (has_property($enum, "enum8bit")) { + return "uint8"; + } elsif (has_property($enum, "enum16bit")) { + return "uint16"; + } elsif (has_property($enum, "v1_enum")) { + return "uint32"; + } elsif (has_property($enum->{PARENT}, "enum8bit")) { + return "uint8"; + } elsif (has_property($enum->{PARENT}, "enum16bit")) { + return "uint16"; + } elsif (has_property($enum->{PARENT}, "v1_enum")) { + return "uint32"; + } + return "uint1632"; +} + +sub bitmap_type_fn($) +{ + my $bitmap = shift; + + $bitmap->{TYPE} eq "BITMAP" or die("not a bitmap"); + + if (has_property($bitmap, "bitmap8bit")) { + return "uint8"; + } elsif (has_property($bitmap, "bitmap16bit")) { + return "uint16"; + } elsif (has_property($bitmap, "bitmap64bit")) { + return "hyper"; + } + return "uint32"; +} + +sub typeHasBody($) +{ + sub typeHasBody($); + my ($e) = @_; + + if ($e->{TYPE} eq "TYPEDEF") { + return 0 unless(defined($e->{DATA})); + return typeHasBody($e->{DATA}); + } + + return defined($e->{ELEMENTS}); +} + +sub mapType($$) +{ + sub mapType($$); + my ($t, $n) = @_; + + return mapType($t->{DATA}, $n) if ($t->{TYPE} eq "TYPEDEF"); + return mapScalarType($n) if ($t->{TYPE} eq "SCALAR"); + return "enum $n" if ($t->{TYPE} eq "ENUM"); + return "struct $n" if ($t->{TYPE} eq "STRUCT" or $t->{TYPE} eq "INTERFACE"); + return "union $n" if ($t->{TYPE} eq "UNION"); + return mapScalarType(bitmap_type_fn($t)) if ($t->{TYPE} eq "BITMAP"); + return "struct $n" if ($t->{TYPE} eq "PIPE"); + die("Unknown type $t->{TYPE}"); +} + +sub mapTypeName($) +{ + my $t = shift; + return "void" unless defined($t); + my $dt; + $t = expandAlias($t); + + if ($dt = getType($t)) { + return mapType($dt, $dt->{NAME}); + } elsif (ref($t) eq "HASH" and defined($t->{NAME})) { + return mapType($t, $t->{NAME}); + } else { + # Best guess + return "struct $t"; + } + +} + +sub LoadIdl($;$) +{ + my $idl = shift; + my $basename = shift; + + foreach my $x (@{$idl}) { + next if $x->{TYPE} ne "INTERFACE"; + + # DCOM interfaces can be types as well + addType({ + NAME => $x->{NAME}, + TYPE => "TYPEDEF", + DATA => $x, + BASEFILE => $basename, + }) if (has_property($x, "object")); + + foreach my $y (@{$x->{DATA}}) { + if ($y->{TYPE} eq "TYPEDEF" + or $y->{TYPE} eq "UNION" + or $y->{TYPE} eq "STRUCT" + or $y->{TYPE} eq "ENUM" + or $y->{TYPE} eq "BITMAP" + or $y->{TYPE} eq "PIPE") { + $y->{BASEFILE} = $basename; + addType($y); + } + } + } +} + +sub GenerateTypeLib() +{ + return Parse::Pidl::Util::MyDumper(\%types); +} + +RegisterScalars(); + +1; diff --git a/tools/pidl/lib/Parse/Pidl/Util.pm b/tools/pidl/lib/Parse/Pidl/Util.pm new file mode 100644 index 00000000..83e23937 --- /dev/null +++ b/tools/pidl/lib/Parse/Pidl/Util.pm @@ -0,0 +1,197 @@ +################################################### +# utility functions to support pidl +# Copyright tridge@samba.org 2000 +# released under the GNU GPL +package Parse::Pidl::Util; + +require Exporter; +@ISA = qw(Exporter); +@EXPORT = qw(has_property property_matches ParseExpr ParseExprExt is_constant make_str unmake_str print_uuid MyDumper genpad); +use vars qw($VERSION); +$VERSION = '0.01'; + +use strict; + +use Parse::Pidl::Expr; +use Parse::Pidl qw(error); + +=head1 NAME + +Parse::Pidl::Util - Generic utility functions for pidl + +=head1 SYNOPSIS + +use Parse::Pidl::Util; + +=head1 DESCRIPTION + +Simple module that contains a couple of trivial helper functions +used throughout the various pidl modules. + +=head1 FUNCTIONS + +=over 4 + +=cut + +=item B<MyDumper> +a dumper wrapper to prevent dependence on the Data::Dumper module +unless we actually need it + +=cut + +sub MyDumper($) +{ + require Data::Dumper; + $Data::Dumper::Sortkeys = 1; + my $s = shift; + return Data::Dumper::Dumper($s); +} + +=item B<has_property> +see if a pidl property list contains a given property + +=cut +sub has_property($$) +{ + my($e, $p) = @_; + + return undef if (not defined($e->{PROPERTIES})); + + return $e->{PROPERTIES}->{$p}; +} + +=item B<property_matches> +see if a pidl property matches a value + +=cut +sub property_matches($$$) +{ + my($e,$p,$v) = @_; + + if (!defined has_property($e, $p)) { + return undef; + } + + if ($e->{PROPERTIES}->{$p} =~ /$v/) { + return 1; + } + + return undef; +} + +=item B<is_constant> +return 1 if the string is a C constant + +=cut +sub is_constant($) +{ + my $s = shift; + return 1 if ($s =~ /^\d+$/); + return 1 if ($s =~ /^0x[0-9A-Fa-f]+$/); + return 0; +} + +=item B<make_str> +return a "" quoted string, unless already quoted + +=cut +sub make_str($) +{ + my $str = shift; + if (substr($str, 0, 1) eq "\"") { + return $str; + } + return "\"$str\""; +} + +=item B<unmake_str> +unquote a "" quoted string + +=cut +sub unmake_str($) +{ + my $str = shift; + + $str =~ s/^\"(.*)\"$/$1/; + + return $str; +} + +=item B<print_uuid> +Print C representation of a UUID. + +=cut +sub print_uuid($) +{ + my ($uuid) = @_; + $uuid =~ s/"//g; + my ($time_low,$time_mid,$time_hi,$clock_seq,$node) = split /-/, $uuid; + return undef if not defined($node); + + my @clock_seq = $clock_seq =~ /(..)/g; + my @node = $node =~ /(..)/g; + + return "{0x$time_low,0x$time_mid,0x$time_hi," . + "{".join(',', map {"0x$_"} @clock_seq)."}," . + "{".join(',', map {"0x$_"} @node)."}}"; +} + +=item B<ParseExpr> +Interpret an IDL expression, substituting particular variables. + +=cut +sub ParseExpr($$$) +{ + my($expr, $varlist, $e) = @_; + + my $x = new Parse::Pidl::Expr(); + + return $x->Run($expr, sub { my $x = shift; error($e, $x); }, + # Lookup fn + sub { my $x = shift; + return($varlist->{$x}) if (defined($varlist->{$x})); + return $x; + }, + undef, undef); +} + +=item B<ParseExprExt> +Interpret an IDL expression, substituting particular variables. Can call +callbacks when pointers are being dereferenced or variables are being used. + +=cut +sub ParseExprExt($$$$$) +{ + my($expr, $varlist, $e, $deref, $use) = @_; + + my $x = new Parse::Pidl::Expr(); + + return $x->Run($expr, sub { my $x = shift; error($e, $x); }, + # Lookup fn + sub { my $x = shift; + return($varlist->{$x}) if (defined($varlist->{$x})); + return $x; + }, + $deref, $use); +} + +=item B<genpad> +return an empty string consisting of tabs and spaces suitable for proper indent +of C-functions. + +=cut +sub genpad($) +{ + my ($s) = @_; + my $nt = int((length($s)+1)/8); + my $lt = ($nt*8)-1; + my $ns = (length($s)-$lt); + return "\t"x($nt)." "x($ns); +} + +=back + +=cut + +1; diff --git a/tools/pidl/lib/Parse/Pidl/Wireshark/Conformance.pm b/tools/pidl/lib/Parse/Pidl/Wireshark/Conformance.pm new file mode 100644 index 00000000..01a8c473 --- /dev/null +++ b/tools/pidl/lib/Parse/Pidl/Wireshark/Conformance.pm @@ -0,0 +1,509 @@ +################################################### +# parse an Wireshark conformance file +# Copyright jelmer@samba.org 2005 +# released under the GNU GPL + +=pod + +=head1 NAME + +Parse::Pidl::Wireshark::Conformance - Conformance file parser for Wireshark + +=head1 DESCRIPTION + +This module supports parsing Wireshark conformance files (*.cnf). + +=head1 FILE FORMAT + +Pidl needs additional data for Wireshark output. This data is read from +so-called conformance files. This section describes the format of these +files. + +Conformance files are simple text files with a single command on each line. +Empty lines and lines starting with a '#' character are ignored. +Arguments to commands are separated by spaces. + +The following commands are currently supported: + +=over 4 + +=item I<TYPE> name dissector ft_type base_type mask valsstring alignment + +Register new data type with specified name, what dissector function to call +and what properties to give header fields for elements of this type. + +=item I<NOEMIT> type + +Suppress emitting a dissect_type function for the specified type + +=item I<PARAM_VALUE> type param + +Set parameter to specify to dissector function for given type. + +=item I<HF_FIELD> hf title filter ft_type base_type valsstring mask description + +Generate a custom header field with specified properties. + +=item I<HF_RENAME> old_hf_name new_hf_name + +Force the use of new_hf_name when the parser generator was going to +use old_hf_name. + +This can be used in conjunction with HF_FIELD in order to make more than +one element use the same filter name. + +=item I<ETT_FIELD> ett + +Register a custom ett field + +=item I<STRIP_PREFIX> prefix + +Remove the specified prefix from all function names (if present). + +=item I<PROTOCOL> longname shortname filtername + +Change the short-, long- and filter-name for the current interface in +Wireshark. + +=item I<FIELD_DESCRIPTION> field desc + +Change description for the specified header field. `field' is the hf name of the field. + +=item I<IMPORT> dissector code... + +Code to insert when generating the specified dissector. @HF@ and +@PARAM@ will be substituted. + +=item I<INCLUDE> filename + +Include conformance data from the specified filename in the dissector. + +=item I<TFS> hf_name "true string" "false string" + +Override the text shown when a bitmap boolean value is enabled or disabled. + +=item I<MANUAL> fn_name + +Force pidl to not generate a particular function but allow the user +to write a function manually. This can be used to remove the function +for only one level for a particular element rather than all the functions and +ett/hf variables for a particular element as the NOEMIT command does. + +=item I<CODE START>/I<CODE END> +Begin and end a section of code to be put directly into the generated +source file for the dissector. + +=item I<HEADER START>/I<HEADER END> +Begin and end a section of code to be put directly into the generated +header file for the dissector. + +=back + +=head1 EXAMPLE + + INFO_KEY OpenKey.Ke + +=cut + +package Parse::Pidl::Wireshark::Conformance; + +require Exporter; +use vars qw($VERSION); +$VERSION = '0.01'; + +@ISA = qw(Exporter); +@EXPORT_OK = qw(ReadConformance ReadConformanceFH valid_ft_type valid_base_type); + +use strict; + +use Parse::Pidl qw(fatal warning error); +use Parse::Pidl::Util qw(has_property); +use Parse::Pidl::Typelist qw(addType); + +sub handle_type($$$$$$$$$$) +{ + my ($pos,$data,$name,$dissectorname,$ft_type,$base_type,$mask,$valsstring,$alignment) = @_; + + unless(defined($alignment)) { + error($pos, "incomplete TYPE command"); + return; + } + + unless ($dissectorname =~ /.*dissect_.*/) { + warning($pos, "dissector name does not contain `dissect'"); + } + + unless(valid_ft_type($ft_type)) { + warning($pos, "invalid FT_TYPE `$ft_type'"); + } + + unless (valid_base_type($base_type)) { + warning($pos, "invalid BASE_TYPE `$base_type'"); + } + + $dissectorname =~ s/^\"(.*)\"$/$1/g; + + if (not ($dissectorname =~ /;$/)) { + warning($pos, "missing semicolon"); + } + + $data->{types}->{$name} = { + NAME => $name, + POS => $pos, + USED => 0, + DISSECTOR_NAME => $dissectorname, + FT_TYPE => $ft_type, + BASE_TYPE => $base_type, + MASK => $mask, + VALSSTRING => $valsstring, + ALIGNMENT => $alignment + }; + + addType({ + NAME => $name, + TYPE => "CONFORMANCE", + BASEFILE => "conformance file", + DATA => { + NAME => $name, + TYPE => "CONFORMANCE", + ALIGN => $alignment + } + }); +} + +sub handle_tfs($$$$$) +{ + my ($pos,$data,$hf,$trues,$falses) = @_; + + unless(defined($falses)) { + error($pos, "incomplete TFS command"); + return; + } + + $data->{tfs}->{$hf} = { + TRUE_STRING => $trues, + FALSE_STRING => $falses + }; +} + +sub handle_hf_rename($$$$) +{ + my ($pos,$data,$old,$new) = @_; + + unless(defined($new)) { + warning($pos, "incomplete HF_RENAME command"); + return; + } + + $data->{hf_renames}->{$old} = { + OLDNAME => $old, + NEWNAME => $new, + POS => $pos, + USED => 0 + }; +} + +sub handle_param_value($$$$) +{ + my ($pos,$data,$dissector_name,$value) = @_; + + unless(defined($value)) { + error($pos, "incomplete PARAM_VALUE command"); + return; + } + + $data->{dissectorparams}->{$dissector_name} = { + DISSECTOR => $dissector_name, + PARAM => $value, + POS => $pos, + USED => 0 + }; +} + +sub valid_base_type($) +{ + my $t = shift; + return 0 unless($t =~ /^BASE_.*/); + return 1; +} + +sub valid_ft_type($) +{ + my $t = shift; + return 0 unless($t =~ /^FT_.*/); + return 1; +} + +sub handle_hf_field($$$$$$$$$$) +{ + my ($pos,$data,$index,$name,$filter,$ft_type,$base_type,$valsstring,$mask,$blurb) = @_; + + unless(defined($blurb)) { + error($pos, "incomplete HF_FIELD command"); + return; + } + + unless(valid_ft_type($ft_type)) { + warning($pos, "invalid FT_TYPE `$ft_type'"); + } + + unless(valid_base_type($base_type)) { + warning($pos, "invalid BASE_TYPE `$base_type'"); + } + + $data->{header_fields}->{$index} = { + INDEX => $index, + POS => $pos, + USED => 0, + NAME => $name, + FILTER => $filter, + FT_TYPE => $ft_type, + BASE_TYPE => $base_type, + VALSSTRING => $valsstring, + MASK => $mask, + BLURB => $blurb + }; +} + +sub handle_strip_prefix($$$) +{ + my ($pos,$data,$x) = @_; + + push (@{$data->{strip_prefixes}}, $x); +} + +sub handle_noemit($$$) +{ + my ($pos,$data,$type) = @_; + + if (defined($type)) { + $data->{noemit}->{$type} = 1; + } else { + $data->{noemit_dissector} = 1; + } +} + +sub handle_manual($$$) +{ + my ($pos,$data,$fn) = @_; + + unless(defined($fn)) { + warning($pos, "incomplete MANUAL command"); + return; + } + + $data->{manual}->{$fn} = 1; +} + +sub handle_protocol($$$$$$) +{ + my ($pos, $data, $name, $longname, $shortname, $filtername) = @_; + + $data->{protocols}->{$name} = { + LONGNAME => $longname, + SHORTNAME => $shortname, + FILTERNAME => $filtername + }; +} + +sub handle_fielddescription($$$$) +{ + my ($pos,$data,$field,$desc) = @_; + + unless(defined($desc)) { + warning($pos, "incomplete FIELD_DESCRIPTION command"); + return; + } + + $data->{fielddescription}->{$field} = { + DESCRIPTION => $desc, + POS => $pos, + USED => 0 + }; +} + +sub handle_import +{ + my $pos = shift @_; + my $data = shift @_; + my $dissectorname = shift @_; + + unless(defined($dissectorname)) { + error($pos, "no dissectorname specified"); + return; + } + + $data->{imports}->{$dissectorname} = { + NAME => $dissectorname, + DATA => join(' ', @_), + USED => 0, + POS => $pos + }; +} + +sub handle_ett_field +{ + my $pos = shift @_; + my $data = shift @_; + my $ett = shift @_; + + unless(defined($ett)) { + error($pos, "incomplete ETT_FIELD command"); + return; + } + + push (@{$data->{ett}}, $ett); +} + +sub handle_include +{ + my $pos = shift @_; + my $data = shift @_; + my $fn = shift @_; + + unless(defined($fn)) { + error($pos, "incomplete INCLUDE command"); + return; + } + + ReadConformance($fn, $data); +} + +my %field_handlers = ( + TYPE => \&handle_type, + NOEMIT => \&handle_noemit, + MANUAL => \&handle_manual, + PARAM_VALUE => \&handle_param_value, + HF_FIELD => \&handle_hf_field, + HF_RENAME => \&handle_hf_rename, + ETT_FIELD => \&handle_ett_field, + TFS => \&handle_tfs, + STRIP_PREFIX => \&handle_strip_prefix, + PROTOCOL => \&handle_protocol, + FIELD_DESCRIPTION => \&handle_fielddescription, + IMPORT => \&handle_import, + INCLUDE => \&handle_include +); + +sub ReadConformance($$) +{ + my ($f,$data) = @_; + my $ret; + + open(IN,"<$f") or return undef; + + $ret = ReadConformanceFH(*IN, $data, $f); + + close(IN); + + return $ret; +} + +sub ReadConformanceFH($$$) +{ + my ($fh,$data,$f) = @_; + + my $incodeblock = 0; + my $inheaderblock = 0; + + my $ln = 0; + + foreach (<$fh>) { + $ln++; + next if (/^#.*$/); + next if (/^$/); + + s/[\r\n]//g; + + if ($_ eq "CODE START") { + if ($incodeblock) { + warning({ FILE => $f, LINE => $ln }, + "CODE START inside CODE section"); + } + if ($inheaderblock) { + error({ FILE => $f, LINE => $ln }, + "CODE START inside HEADER section"); + return undef; + } + $incodeblock = 1; + next; + } elsif ($_ eq "CODE END") { + if (!$incodeblock) { + warning({ FILE => $f, LINE => $ln }, + "CODE END outside CODE section"); + } + if ($inheaderblock) { + error({ FILE => $f, LINE => $ln }, + "CODE END inside HEADER section"); + return undef; + } + $incodeblock = 0; + next; + } elsif ($incodeblock) { + if (exists $data->{override}) { + $data->{override}.="$_\n"; + } else { + $data->{override} = "$_\n"; + } + next; + } elsif ($_ eq "HEADER START") { + if ($inheaderblock) { + warning({ FILE => $f, LINE => $ln }, + "HEADER START inside HEADER section"); + } + if ($incodeblock) { + error({ FILE => $f, LINE => $ln }, + "HEADER START inside CODE section"); + return undef; + } + $inheaderblock = 1; + next; + } elsif ($_ eq "HEADER END") { + if (!$inheaderblock) { + warning({ FILE => $f, LINE => $ln }, + "HEADER END outside HEADER section"); + } + if ($incodeblock) { + error({ FILE => $f, LINE => $ln }, + "CODE END inside HEADER section"); + return undef; + } + $inheaderblock = 0; + next; + } elsif ($inheaderblock) { + if (exists $data->{header}) { + $data->{header}.="$_\n"; + } else { + $data->{header} = "$_\n"; + } + next; + } + + my @fields = /([^ "]+|"[^"]+")/g; + + my $cmd = $fields[0]; + + shift @fields; + + my $pos = { FILE => $f, LINE => $ln }; + + next unless(defined($cmd)); + + if (not defined($field_handlers{$cmd})) { + warning($pos, "Unknown command `$cmd'"); + next; + } + + $field_handlers{$cmd}($pos, $data, @fields); + } + + if ($incodeblock) { + warning({ FILE => $f, LINE => $ln }, + "Expecting CODE END"); + return undef; + } + + return 1; +} + +1; diff --git a/tools/pidl/lib/Parse/Pidl/Wireshark/NDR.pm b/tools/pidl/lib/Parse/Pidl/Wireshark/NDR.pm new file mode 100644 index 00000000..ada8dd66 --- /dev/null +++ b/tools/pidl/lib/Parse/Pidl/Wireshark/NDR.pm @@ -0,0 +1,1401 @@ +################################################## +# Wireshark NDR parser generator for IDL structures +# Copyright tridge@samba.org 2000-2003 +# Copyright tpot@samba.org 2001,2005 +# Copyright jelmer@samba.org 2004-2007 +# Portions based on idl2eth.c by Ronnie Sahlberg +# released under the GNU GPL + +=pod + +=head1 NAME + +Parse::Pidl::Wireshark::NDR - Parser generator for Wireshark + +=cut + +package Parse::Pidl::Wireshark::NDR; + +use Exporter; +@ISA = qw(Exporter); +@EXPORT_OK = qw(field2name %res PrintIdl StripPrefixes RegisterInterfaceHandoff register_hf_field CheckUsed ProcessImport ProcessInclude find_type DumpEttList DumpEttDeclaration DumpHfList DumpHfDeclaration DumpFunctionTable register_type register_ett); + +use strict; +use Parse::Pidl qw(error warning); +use Parse::Pidl::Typelist qw(getType); +use Parse::Pidl::Util qw(has_property property_matches make_str); +use Parse::Pidl::NDR qw(ContainsString GetNextLevel); +use Parse::Pidl::Dump qw(DumpType DumpFunction); +use Parse::Pidl::Wireshark::Conformance qw(ReadConformance); +use File::Basename; + +use vars qw($VERSION); +$VERSION = '0.01'; + +my %return_types = (); +my %dissector_used = (); + +my %ptrtype_mappings = ( + "unique" => "NDR_POINTER_UNIQUE", + "ref" => "NDR_POINTER_REF", + "ptr" => "NDR_POINTER_PTR" +); + +sub StripPrefixes($$) +{ + my ($s, $prefixes) = @_; + + foreach (@$prefixes) { + $s =~ s/^$_\_//g; + } + + return $s; +} + +# Convert a IDL structure field name (e.g access_mask) to a prettier +# string like 'Access Mask'. + +sub field2name($) +{ + my($field) = shift; + + $field =~ s/^(_)*//g; # Remove any starting underscores + $field =~ s/_/ /g; # Replace underscores with spaces + $field =~ s/(\w+)/\u$1/g; # Capitalise each word + + return $field; +} + +sub new($) +{ + my ($class) = @_; + my $self = {res => {hdr => "", def => "", code => ""}, tabs => "", cur_fn => undef, + hf_used => {}, ett => [], conformance => undef + + }; + bless($self, $class); +} + +sub pidl_fn_start($$) +{ + my ($self, $fn) = @_; + $self->{cur_fn} = $fn; +} +sub pidl_fn_end($$) +{ + my ($self, $fn) = @_; + die("Inconsistent state: $fn != $self->{cur_fn}") if ($fn ne $self->{cur_fn}); + $self->{cur_fn} = undef; +} + +sub pidl_code($$) +{ + my ($self, $d) = @_; + return if (defined($self->{cur_fn}) and defined($self->{conformance}->{manual}->{$self->{cur_fn}})); + + if ($d) { + $self->{res}->{code} .= $self->{tabs}; + $self->{res}->{code} .= $d; + } + $self->{res}->{code} .="\n"; +} + +sub pidl_hdr($$) { my ($self,$x) = @_; $self->{res}->{hdr} .= "$x\n"; } +sub pidl_def($$) { my ($self,$x) = @_; $self->{res}->{def} .= "$x\n"; } + +sub indent($) +{ + my ($self) = @_; + $self->{tabs} .= "\t"; +} + +sub deindent($) +{ + my ($self) = @_; + $self->{tabs} = substr($self->{tabs}, 0, -1); +} + +sub PrintIdl($$) +{ + my ($self, $idl) = @_; + + foreach (split /\n/, $idl) { + $self->pidl_code("/* IDL: $_ */"); + } + + $self->pidl_code(""); +} + +##################################################################### +# parse the interface definitions +sub Interface($$) +{ + my($self, $interface) = @_; + $self->Const($_,$interface->{NAME}) foreach (@{$interface->{CONSTS}}); + $self->Type($_, $_->{NAME}, $interface->{NAME}) foreach (@{$interface->{TYPES}}); + $self->Function($_,$interface->{NAME}) foreach (@{$interface->{FUNCTIONS}}); +} + +sub Enum($$$$) +{ + my ($self, $e,$name,$ifname) = @_; + my $valsstring = "$ifname\_$name\_vals"; + my $dissectorname = "$ifname\_dissect\_enum\_".StripPrefixes($name, $self->{conformance}->{strip_prefixes}); + + return if (defined($self->{conformance}->{noemit}->{StripPrefixes($name, $self->{conformance}->{strip_prefixes})})); + + foreach (@{$e->{ELEMENTS}}) { + if (/([^=]*)=(.*)/) { + $self->pidl_hdr("#define $1 ($2)"); + } + } + + $self->pidl_hdr("extern const value_string $valsstring\[];"); + $self->pidl_hdr("int $dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, dcerpc_info* di _U_, guint8 *drep _U_, int hf_index _U_, g$e->{BASE_TYPE} *param _U_);"); + + $self->pidl_def("const value_string ".$valsstring."[] = {"); + foreach (@{$e->{ELEMENTS}}) { + next unless (/([^=]*)=(.*)/); + $self->pidl_def("\t{ $1, \"$1\" },"); + } + + $self->pidl_def("{ 0, NULL }"); + $self->pidl_def("};"); + + $self->pidl_fn_start($dissectorname); + $self->pidl_code("int"); + $self->pidl_code("$dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, dcerpc_info* di _U_, guint8 *drep _U_, int hf_index _U_, g$e->{BASE_TYPE} *param _U_)"); + $self->pidl_code("{"); + $self->indent; + $self->pidl_code("g$e->{BASE_TYPE} parameter=0;"); + $self->pidl_code("if (param) {"); + $self->indent; + $self->pidl_code("parameter = *param;"); + $self->deindent; + $self->pidl_code("}"); + $self->pidl_code("offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, tree, di, drep, hf_index, ¶meter);"); + $self->pidl_code("if (param) {"); + $self->indent; + $self->pidl_code("*param = parameter;"); + $self->deindent; + $self->pidl_code("}"); + $self->pidl_code("return offset;"); + $self->deindent; + $self->pidl_code("}\n"); + $self->pidl_fn_end($dissectorname); + + my $enum_size = $e->{BASE_TYPE}; + $enum_size =~ s/uint//g; + $self->register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);", "FT_UINT$enum_size", "BASE_DEC", "0", "VALS($valsstring)", $enum_size / 8); +} + +sub Pipe($$$$) +{ + my ($self,$e,$name,$ifname) = @_; + error($e->{ORIGINAL}, "Pipe not yet supported"); + return; +} + +sub Bitmap($$$$) +{ + my ($self,$e,$name,$ifname) = @_; + my $dissectorname = "$ifname\_dissect\_bitmap\_".StripPrefixes($name, $self->{conformance}->{strip_prefixes}); + my $element_count = 0; + my $total_ev = 0; + + $self->register_ett("ett_$ifname\_$name"); + + $self->pidl_hdr("int $dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, dcerpc_info* di _U_, guint8 *drep _U_, int hf_index _U_, guint32 param _U_);"); + + $self->pidl_fn_start($dissectorname); + $self->pidl_code("int"); + $self->pidl_code("$dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *parent_tree _U_, dcerpc_info* di _U_, guint8 *drep _U_, int hf_index _U_, guint32 param _U_)"); + $self->pidl_code("{"); + $self->indent; + foreach (@{$e->{ELEMENTS}}) { + next unless (/([^ ]*) (.*)/); + $element_count++; + } + if ($element_count > 0) { + $self->pidl_code("proto_item *item;"); + $self->pidl_code("static int * const $ifname\_$name\_fields[] = {"); + $self->indent; + foreach (@{$e->{ELEMENTS}}) { + next unless (/([^ ]*) (.*)/); + my ($en,$ev) = ($1,$2); + my $hf_bitname = "hf_$ifname\_$name\_$1"; + + $ev =~ s/[()\s]//g; + if (hex($ev) != 0) { + $total_ev += hex($ev); + $self->pidl_code("&$hf_bitname,"); + } + } + $self->pidl_code("NULL"); + $self->deindent; + $self->pidl_code("};"); + } + + $self->pidl_code("g$e->{BASE_TYPE} flags;"); + if ($e->{ALIGN} > 1) { + $self->pidl_code("ALIGN_TO_$e->{ALIGN}_BYTES;"); + } + + $self->pidl_code(""); + + if ($element_count > 0) { + $self->pidl_code("item = proto_tree_add_bitmask_with_flags(parent_tree, tvb, offset, hf_index,"); + $self->pidl_code("\t\t\tett_$ifname\_$name, $ifname\_$name\_fields, DREP_ENC_INTEGER(drep), BMT_NO_FALSE);"); + $self->pidl_code(""); + + $self->pidl_code("offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, parent_tree, di, drep, -1, &flags);"); + $self->pidl_code(""); + + $self->pidl_code("if (!flags)"); + $self->pidl_code("\tproto_item_append_text(item, \": (No values set)\");\n"); + } else { + $self->pidl_code("proto_tree_add_item(parent_tree, hf_index, tvb, offset, $e->{ALIGN}, DREP_ENC_INTEGER(drep));"); + $self->pidl_code(""); + + $self->pidl_code("offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, parent_tree, di, drep, -1, &flags);"); + $self->pidl_code(""); + } + + foreach (@{$e->{ELEMENTS}}) { + next unless (/([^ ]*) (.*)/); + my ($en,$ev) = ($1,$2); + my $hf_bitname = "hf_$ifname\_$name\_$en"; + my $filtername = "$ifname\.$name\.$en"; + + $self->{hf_used}->{$hf_bitname} = 1; + + $ev =~ s/[()\s]//g; + if (hex($ev) != 0) { + $self->register_hf_field($hf_bitname, field2name($en), $filtername, "FT_BOOLEAN", $e->{ALIGN} * 8, "TFS(&$name\_$en\_tfs)", "( $ev )", ""); + + $self->pidl_def("static const true_false_string $name\_$en\_tfs = {"); + if (defined($self->{conformance}->{tfs}->{$hf_bitname})) { + $self->pidl_def(" $self->{conformance}->{tfs}->{$hf_bitname}->{TRUE_STRING},"); + $self->pidl_def(" $self->{conformance}->{tfs}->{$hf_bitname}->{FALSE_STRING},"); + $self->{conformance}->{tfs}->{$hf_bitname}->{USED} = 1; + } else { + $self->pidl_def(" \"$en is SET\","); + $self->pidl_def(" \"$en is NOT SET\","); + } + $self->pidl_def("};"); + } + } + + if ($element_count > 0) { + my $total_ev_hex = sprintf("0x%08x", $total_ev); + $self->pidl_code("if (flags & (~$total_ev_hex)) {"); + $self->pidl_code("\tflags &= (~$total_ev_hex);"); + $self->pidl_code("\tproto_item_append_text(item, \"Unknown bitmap value 0x%x\", flags);"); + $self->pidl_code("}\n"); + } + $self->pidl_code("return offset;"); + $self->deindent; + $self->pidl_code("}\n"); + $self->pidl_fn_end($dissectorname); + + my $size = $e->{BASE_TYPE}; + $size =~ s/uint//g; + $self->register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);", "FT_UINT$size", "BASE_HEX", "0", "NULL", $size/8); +} + +sub ElementLevel($$$$$$$$) +{ + my ($self,$e,$l,$hf,$myname,$pn,$ifname,$param) = @_; + + if (defined($self->{conformance}->{dissectorparams}->{$myname})) { + $param = $self->{conformance}->{dissectorparams}->{$myname}->{PARAM}; + } + + if ($l->{TYPE} eq "POINTER") { + my $type; + if ($l->{LEVEL} eq "TOP") { + $type = "toplevel"; + } elsif ($l->{LEVEL} eq "EMBEDDED") { + $type = "embedded"; + } + $self->pidl_code("offset = dissect_ndr_$type\_pointer(tvb, offset, pinfo, tree, di, drep, $myname\_, $ptrtype_mappings{$l->{POINTER_TYPE}}, \"Pointer to ".field2name(StripPrefixes($e->{NAME}, $self->{conformance}->{strip_prefixes})) . " ($e->{TYPE})\",$hf);"); + } elsif ($l->{TYPE} eq "ARRAY") { + if ($l->{IS_INLINE}) { + error($e->{ORIGINAL}, "Inline arrays not supported"); + } elsif ($l->{IS_FIXED}) { + $self->pidl_code("int i;"); + $self->pidl_code("for (i = 0; i < $l->{SIZE_IS}; i++)"); + $self->pidl_code("\toffset = $myname\_(tvb, offset, pinfo, tree, di, drep);"); + } else { + my $type = ""; + $type .= "c" if ($l->{IS_CONFORMANT}); + $type .= "v" if ($l->{IS_VARYING}); + + unless ($l->{IS_ZERO_TERMINATED}) { + $self->pidl_code("offset = dissect_ndr_u" . $type . "array(tvb, offset, pinfo, tree, di, drep, $myname\_);"); + } else { + my $nl = GetNextLevel($e,$l); + $self->pidl_code("char *data;"); + $self->pidl_code(""); + $self->pidl_code("offset = dissect_ndr_$type" . "string(tvb, offset, pinfo, tree, di, drep, sizeof(g$nl->{DATA_TYPE}), $hf, FALSE, &data);"); + $self->pidl_code("proto_item_append_text(tree, \": %s\", data);"); + } + } + } elsif ($l->{TYPE} eq "DATA") { + if ($l->{DATA_TYPE} eq "string") { + my $bs = 2; # Byte size defaults to that of UCS2 + + + ($bs = 1) if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_ASCII.*")); + + if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*") and property_matches($e, "flag", ".*LIBNDR_FLAG_STR_LEN4.*")) { + $self->pidl_code("char *data;\n"); + $self->pidl_code("offset = dissect_ndr_cvstring(tvb, offset, pinfo, tree, di, drep, $bs, $hf, FALSE, &data);"); + $self->pidl_code("proto_item_append_text(tree, \": %s\", data);"); + } elsif (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*")) { + $self->pidl_code("offset = dissect_ndr_vstring(tvb, offset, pinfo, tree, di, drep, $bs, $hf, FALSE, NULL);"); + } elsif (property_matches($e, "flag", ".*STR_NULLTERM.*")) { + if ($bs == 2) { + $self->pidl_code("offset = dissect_null_term_wstring(tvb, offset, pinfo, tree, drep, $hf , 0);") + } else { + $self->pidl_code("offset = dissect_null_term_string(tvb, offset, pinfo, tree, drep, $hf , 0);") + } + } else { + warn("Unable to handle string with flags $e->{PROPERTIES}->{flag}"); + } + } elsif ($l->{DATA_TYPE} eq "DATA_BLOB") { + my $remain = 0; + $remain = 1 if (property_matches($e->{ORIGINAL}, "flag", ".*LIBNDR_FLAG_REMAINING.*")); + $self->pidl_code("offset = dissect_ndr_datablob(tvb, offset, pinfo, tree, di, drep, $hf, $remain);"); + } else { + my $call; + + if ($self->{conformance}->{imports}->{$l->{DATA_TYPE}}) { + $call = $self->{conformance}->{imports}->{$l->{DATA_TYPE}}->{DATA}; + $self->{conformance}->{imports}->{$l->{DATA_TYPE}}->{USED} = 1; + } elsif (defined($self->{conformance}->{imports}->{"$pn.$e->{NAME}"})) { + $call = $self->{conformance}->{imports}->{"$pn.$e->{NAME}"}->{DATA}; + $self->{conformance}->{imports}->{"$pn.$e->{NAME}"}->{USED} = 1; + } elsif (defined($self->{conformance}->{types}->{$l->{DATA_TYPE}})) { + $call= $self->{conformance}->{types}->{$l->{DATA_TYPE}}->{DISSECTOR_NAME}; + $self->{conformance}->{types}->{$l->{DATA_TYPE}}->{USED} = 1; + } else { + my $t; + if (ref($l->{DATA_TYPE}) eq "HASH" ) { + $t = "$l->{DATA_TYPE}->{TYPE}_$l->{DATA_TYPE}->{NAME}"; + } else { + $t = $l->{DATA_TYPE}; + } + + $self->pidl_code("offset = $ifname\_dissect_struct_" . $t . "(tvb,offset,pinfo,tree,di,drep,$hf,$param);"); + + return; + } + + $call =~ s/\@HF\@/$hf/g; + $call =~ s/\@PARAM\@/$param/g; + $self->pidl_code($call); + } + } elsif ($_->{TYPE} eq "SUBCONTEXT") { + my $varswitch; + if (has_property($e, "switch_is")) { + $varswitch = $e->{PROPERTIES}->{switch_is}; + } + my $num_bits = ($l->{HEADER_SIZE}*8); + my $hf2 = $self->register_hf_field($hf."_", "Subcontext length", "$ifname.$pn.$_->{NAME}subcontext", "FT_UINT$num_bits", "BASE_HEX", "NULL", 0, ""); + $num_bits = 3264 if ($num_bits == 32); + $self->{hf_used}->{$hf2} = 1; + $self->pidl_code("guint$num_bits size;"); + $self->pidl_code("int conformant = di->conformant_run;"); + $self->pidl_code("tvbuff_t *subtvb;"); + $self->pidl_code(""); + # We need to be able to dissect the length of the context in every case + # and conformant run skips the dissections of scalars ... + $self->pidl_code("if (!conformant) {"); + $self->indent; + $self->pidl_code("guint32 saved_flags = di->call_data->flags;"); + $self->pidl_code("offset = dissect_ndr_uint$num_bits(tvb, offset, pinfo, tree, di, drep, $hf2, &size);"); + # This is a subcontext, there is normally no such thing as + # 64 bit NDR is subcontext so we clear the flag so that we can + # continue to dissect handmarshalled stuff with pidl + $self->pidl_code("di->call_data->flags &= ~DCERPC_IS_NDR64;"); + + $self->pidl_code("subtvb = tvb_new_subset_length_caplen(tvb, offset, (const gint)size, -1);"); + if ($param ne 0) { + $self->pidl_code("$myname\_(subtvb, 0, pinfo, tree, di, drep, $param);"); + } else { + $self->pidl_code("$myname\_(subtvb, 0, pinfo, tree, di, drep);"); + } + $self->pidl_code("offset += (int)size;"); + $self->pidl_code("di->call_data->flags = saved_flags;"); + $self->deindent; + $self->pidl_code("}"); + } elsif ($_->{TYPE} eq "PIPE") { + error($e->{ORIGINAL}, "Type PIPE not yet supported"); + } else { + die("Unknown type `$_->{TYPE}'"); + } +} + +sub SwitchType($$;$) +{ + my ($e, $type, $nodiscriminant) = @_; + + my $switch_dt = getType($type); + my $switch_type = undef; + if ($switch_dt->{DATA}->{TYPE} eq "ENUM") { + $switch_type = Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA}); + } elsif ($switch_dt->{DATA}->{TYPE} eq "BITMAP") { + $switch_type = Parse::Pidl::Typelist::bitmap_type_fn($switch_dt->{DATA}); + } elsif ($switch_dt->{DATA}->{TYPE} eq "SCALAR") { + if (defined $e->{SWITCH_TYPE}) { + $switch_type = "$e->{SWITCH_TYPE}"; + } else { + $switch_type = "$switch_dt->{DATA}->{NAME}"; + } + } elsif (not defined $e->{SWITCH_TYPE}) { + $switch_type = $nodiscriminant; + } + + return $switch_type +} + +sub Element($$$$$$) +{ + my ($self,$e,$pn,$ifname,$isoruseswitch,%switchvars) = @_; + + my $dissectorname = "$ifname\_dissect\_element\_".StripPrefixes($pn, $self->{conformance}->{strip_prefixes})."\_".StripPrefixes($e->{NAME}, $self->{conformance}->{strip_prefixes}); + + my ($call_code, $moreparam); + my $param = 0; + if (defined $isoruseswitch) { + my $type = $isoruseswitch->[0]; + my $name = $isoruseswitch->[1]; + + my $switch_dt = getType($type); + my $switch_raw_type = SwitchType($e, $type, "uint32"); + if (not defined($switch_raw_type)) { + die("Unknown type[$type]\n"); + } + my $switch_type = "g${switch_raw_type}"; + + if ($name ne "") { + $moreparam = ", $switch_type *".$name; + } else { + $moreparam = ""; + } + if (($e->{PROPERTIES}->{switch_is} eq "") && ($switchvars{$name}) && + #not a "native" type + (!($type =~ /^uint(8|16|1632|32|3264|64)/))) { + $param = $name; + } elsif ( $switch_dt->{DATA}->{TYPE} eq "ENUM") { + $param = $name; + } elsif ($name ne "") { + $param = "*".$name; + } + + if ($name ne "") { + $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, di, drep, &$name);"; + } else { + $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, di, drep);"; + } + } else { + $moreparam = ""; + $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, di, drep);"; + } + + + my $type = $self->find_type($e->{TYPE}); + + if (not defined($type)) { + # default settings + $type = { + MASK => 0, + VALSSTRING => "NULL", + FT_TYPE => "FT_NONE", + BASE_TYPE => "BASE_NONE" + }; + } + + if (ContainsString($e)) { + $type = { + MASK => 0, + VALSSTRING => "NULL", + FT_TYPE => "FT_STRING", + BASE_TYPE => "BASE_NONE" + }; + } + if (property_matches($e, "flag", ".*LIBNDR_FLAG_ALIGN.*")) { + my $align_flag = $e->{PROPERTIES}->{flag}; + if ($align_flag =~ m/LIBNDR_FLAG_ALIGN(\d+)/) { + $call_code = "ALIGN_TO_$1_BYTES; ".$call_code; + } + } + + my $hf = $self->register_hf_field("hf_$ifname\_$pn\_$e->{NAME}", field2name($e->{NAME}), "$ifname.$pn.$e->{NAME}", $type->{FT_TYPE}, $type->{BASE_TYPE}, $type->{VALSSTRING}, $type->{MASK}, ""); + $self->{hf_used}->{$hf} = 1; + + my $eltname = StripPrefixes($pn, $self->{conformance}->{strip_prefixes}) . ".$e->{NAME}"; + if (defined($self->{conformance}->{noemit}->{$eltname})) { + return $call_code; + } + + my $add = ""; + + my $oldparam = undef; + foreach (@{$e->{LEVELS}}) { + if (defined $_->{SWITCH_IS}) { + $oldparam = $param; + if (($param ne "0") && (!($param =~ /\*/))) { + $param = "*$param"; + } + } + next if ($_->{TYPE} eq "SWITCH"); + next if (defined($self->{conformance}->{noemit}->{"$dissectorname$add"})); + $self->pidl_def("static int $dissectorname$add(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, dcerpc_info* di _U_, guint8 *drep _U_$moreparam);"); + $self->pidl_fn_start("$dissectorname$add"); + $self->pidl_code("static int"); + $self->pidl_code("$dissectorname$add(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, dcerpc_info* di _U_, guint8 *drep _U_$moreparam)"); + $self->pidl_code("{"); + $self->indent; + + $self->ElementLevel($e,$_,$hf,$dissectorname.$add,$pn,$ifname,$param); + if (defined $oldparam) { + $param = $oldparam; + } + + $self->pidl_code(""); + $self->pidl_code("return offset;"); + $self->deindent; + $self->pidl_code("}\n"); + $self->pidl_fn_end("$dissectorname$add"); + $add.="_"; + last if ($_->{TYPE} eq "ARRAY" and $_->{IS_ZERO_TERMINATED}); + } + + return $call_code; +} + +sub Function($$$) +{ + my ($self, $fn,$ifname) = @_; + + my %dissectornames; + + foreach (@{$fn->{ELEMENTS}}) { + $dissectornames{$_->{NAME}} = $self->Element($_, $fn->{NAME}, $ifname, undef, undef) if not defined($dissectornames{$_->{NAME}}); + } + + my $fn_name = $_->{NAME}; + $fn_name =~ s/^${ifname}_//; + + $self->PrintIdl(DumpFunction($fn->{ORIGINAL})); + $self->pidl_fn_start("$ifname\_dissect\_$fn_name\_response"); + $self->pidl_code("static int"); + $self->pidl_code("$ifname\_dissect\_${fn_name}_response(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, dcerpc_info* di _U_, guint8 *drep _U_)"); + $self->pidl_code("{"); + $self->indent; + if ( not defined($fn->{RETURN_TYPE})) { + } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS" or $fn->{RETURN_TYPE} eq "WERROR" or $fn->{RETURN_TYPE} eq "HRESULT") + { + $self->pidl_code("guint32 status;\n"); + } elsif (my $type = getType($fn->{RETURN_TYPE})) { + if ($type->{DATA}->{TYPE} eq "ENUM") { + $self->pidl_code("g".Parse::Pidl::Typelist::enum_type_fn($type->{DATA}) . " status;\n"); + } elsif ($type->{DATA}->{TYPE} eq "SCALAR") { + $self->pidl_code("g$fn->{RETURN_TYPE} status;\n"); + } else { + error($fn, "return type `$fn->{RETURN_TYPE}' not yet supported"); + } + } else { + error($fn, "unknown return type `$fn->{RETURN_TYPE}'"); + } + + $self->pidl_code("di->dcerpc_procedure_name=\"${fn_name}\";"); + foreach (@{$fn->{ELEMENTS}}) { + if (grep(/out/,@{$_->{DIRECTION}})) { + $self->pidl_code("$dissectornames{$_->{NAME}}"); + $self->pidl_code("offset = dissect_deferred_pointers(pinfo, tvb, offset, di, drep);"); + $self->pidl_code(""); + } + } + + if (not defined($fn->{RETURN_TYPE})) { + } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS") { + $self->pidl_code("offset = dissect_ntstatus(tvb, offset, pinfo, tree, di, drep, hf\_$ifname\_status, &status);\n"); + $self->pidl_code("if (status != 0)"); + $self->pidl_code("\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, NT_errors, \"Unknown NT status 0x%08x\"));\n"); + $return_types{$ifname}->{"status"} = ["NTSTATUS", "NT Error"]; + } elsif ($fn->{RETURN_TYPE} eq "WERROR") { + $self->pidl_code("offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, di, drep, hf\_$ifname\_werror, &status);\n"); + $self->pidl_code("if (status != 0)"); + $self->pidl_code("\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, WERR_errors, \"Unknown DOS error 0x%08x\"));\n"); + + $return_types{$ifname}->{"werror"} = ["WERROR", "Windows Error"]; + } elsif ($fn->{RETURN_TYPE} eq "HRESULT") { + $self->pidl_code("offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, di, drep, hf\_$ifname\_hresult, &status);\n"); + $self->pidl_code("if (status != 0)"); + $self->pidl_code("\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, HRES_errors, \"Unknown HRES error 0x%08x\"));\n"); + $return_types{$ifname}->{"hresult"} = ["HRESULT", "HRES Windows Error"]; + } elsif (my $type = getType($fn->{RETURN_TYPE})) { + if ($type->{DATA}->{TYPE} eq "ENUM") { + my $return_type = "g".Parse::Pidl::Typelist::enum_type_fn($type->{DATA}); + my $return_dissect = "dissect_ndr_" .Parse::Pidl::Typelist::enum_type_fn($type->{DATA}); + + $self->pidl_code("offset = $return_dissect(tvb, offset, pinfo, tree, di, drep, hf\_$ifname\_$fn->{RETURN_TYPE}_status, &status);"); + $self->pidl_code("if (status != 0)"); + $self->pidl_code("\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Status: %s\", val_to_str(status, $ifname\_$fn->{RETURN_TYPE}\_vals, \"Unknown " . $fn->{RETURN_TYPE} . " error 0x%08x\"));\n"); + $return_types{$ifname}->{$fn->{RETURN_TYPE}."_status"} = [$fn->{RETURN_TYPE}, $fn->{RETURN_TYPE}]; + } elsif ($type->{DATA}->{TYPE} eq "SCALAR") { + $self->pidl_code("offset = dissect_ndr_$fn->{RETURN_TYPE}(tvb, offset, pinfo, tree, di, drep, hf\_$ifname\_$fn->{RETURN_TYPE}_status, &status);"); + $self->pidl_code("if (status != 0)"); + $self->pidl_code("\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Status: %d\", status);\n"); + $return_types{$ifname}->{$fn->{RETURN_TYPE}."_status"} = [$fn->{RETURN_TYPE}, $fn->{RETURN_TYPE}]; + } + } + + $self->pidl_code("return offset;"); + $self->deindent; + $self->pidl_code("}\n"); + $self->pidl_fn_end("$ifname\_dissect\_$fn_name\_response"); + + $self->pidl_fn_start("$ifname\_dissect\_$fn_name\_request"); + $self->pidl_code("static int"); + $self->pidl_code("$ifname\_dissect\_${fn_name}_request(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, dcerpc_info* di _U_, guint8 *drep _U_)"); + $self->pidl_code("{"); + $self->indent; + $self->pidl_code("di->dcerpc_procedure_name=\"${fn_name}\";"); + foreach (@{$fn->{ELEMENTS}}) { + if (grep(/in/,@{$_->{DIRECTION}})) { + $self->pidl_code("$dissectornames{$_->{NAME}}"); + $self->pidl_code("offset = dissect_deferred_pointers(pinfo, tvb, offset, di, drep);"); + } + + } + + $self->pidl_code("return offset;"); + $self->deindent; + $self->pidl_code("}\n"); + $self->pidl_fn_end("$ifname\_dissect\_$fn_name\_request"); +} + +sub Struct($$$$) +{ + my ($self,$e,$name,$ifname) = @_; + my $dissectorname = "$ifname\_dissect\_struct\_".StripPrefixes($name, $self->{conformance}->{strip_prefixes}); + + return if (defined($self->{conformance}->{noemit}->{StripPrefixes($name, $self->{conformance}->{strip_prefixes})})); + + $self->register_ett("ett_$ifname\_$name"); + + my $res = ""; + my $varswitchs = {}; + # will contain the switch var declaration; + my $vars = []; + my %switch_hash; + foreach (@{$e->{ELEMENTS}}) { + if (has_property($_, "switch_is")) { + $varswitchs->{$_->{PROPERTIES}->{switch_is}} = []; + $switch_hash{ $_->{PROPERTIES}->{switch_is}} = $_->{PROPERTIES}->{switch_is}; + } + } + foreach (@{$e->{ELEMENTS}}) { + my $switch_info = undef; + + my $v = $_->{NAME}; + if (scalar(grep {/^$v$/} keys(%$varswitchs)) == 1) { + # This element is one of the switch attribute + my $switch_raw_type = SwitchType($e, $_->{TYPE}, "uint32"); + if (not defined($switch_raw_type)) { + die("Unknown type[$_->{TYPE}]\n"); + } + my $switch_type = "g${switch_raw_type}"; + + if ($switch_type ne "") { + push @$vars, "$switch_type $v = 0;"; + } + $switch_info = [ $_->{TYPE}, $v ]; + $varswitchs->{$v} = $switch_info; + } + + if (has_property($_, "switch_is")) { + my $varswitch = $_->{PROPERTIES}->{switch_is}; + $switch_info = $varswitchs->{$varswitch}; + } + + $res.="\t".$self->Element($_, $name, $ifname, $switch_info, %switch_hash)."\n\n"; + } + + my $doalign = undef; + if ($e->{ALIGN} > 1 and not property_matches($e, "flag", ".*LIBNDR_FLAG_NOALIGN.*")) { + $doalign = 1; + } elsif (property_matches($e, "flag", ".*LIBNDR_FLAG_NOALIGN.*")) { + $doalign = 0; + } + + $self->pidl_hdr("int $dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *parent_tree _U_, dcerpc_info* di _U_, guint8 *drep _U_, int hf_index _U_, guint32 param _U_);"); + + $self->pidl_fn_start($dissectorname); + $self->pidl_code("int"); + $self->pidl_code("$dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *parent_tree _U_, dcerpc_info* di _U_, guint8 *drep _U_, int hf_index _U_, guint32 param _U_)"); + $self->pidl_code("{"); + $self->indent; + $self->pidl_code($_) foreach (@$vars); + $self->pidl_code("proto_item *item = NULL;"); + if($res) { + $self->pidl_code("proto_tree *tree = NULL;"); + } + if (defined($doalign) and $doalign == 0) { + $self->pidl_code("gboolean oldalign = di->no_align;"); + } + $self->pidl_code("int old_offset;"); + $self->pidl_code(""); + + if (defined($doalign)) { + if ($doalign == 1) { + $self->pidl_code("ALIGN_TO_$e->{ALIGN}_BYTES;"); + } + if ($doalign == 0) { + $self->pidl_code("di->no_align = TRUE;"); + } + $self->pidl_code(""); + } + + $self->pidl_code("old_offset = offset;"); + $self->pidl_code(""); + $self->pidl_code("if (parent_tree) {"); + $self->indent; + $self->pidl_code("item = proto_tree_add_item(parent_tree, hf_index, tvb, offset, -1, ENC_NA);"); + if($res) { + $self->pidl_code("tree = proto_item_add_subtree(item, ett_$ifname\_$name);"); + } + $self->deindent; + $self->pidl_code("}"); + $self->pidl_code(""); + + $self->deindent; + $self->pidl_code("$res"); + $self->indent; + + $self->pidl_code("proto_item_set_len(item, offset-old_offset);\n"); + if (defined($doalign) and $doalign == 1) { + $self->pidl_code(""); + $self->pidl_code("if (di->call_data->flags & DCERPC_IS_NDR64) {"); + $self->indent; + $self->pidl_code("ALIGN_TO_$e->{ALIGN}_BYTES;"); + $self->deindent; + $self->pidl_code("}"); + } + if (defined($doalign) and $doalign == 0) { + $self->pidl_code(""); + $self->pidl_code("di->no_align = oldalign;"); + } + $self->pidl_code(""); + $self->pidl_code("return offset;"); + $self->deindent; + $self->pidl_code("}\n"); + $self->pidl_fn_end($dissectorname); + + $self->register_type($name, "offset = $dissectorname(tvb,offset,pinfo,tree,di,drep,\@HF\@,\@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0); +} + +sub Union($$$$) +{ + my ($self,$e,$name,$ifname) = @_; + + my $dissectorname = "$ifname\_dissect_".StripPrefixes($name, $self->{conformance}->{strip_prefixes}); + + return if (defined($self->{conformance}->{noemit}->{StripPrefixes($name, $self->{conformance}->{strip_prefixes})})); + + $self->register_ett("ett_$ifname\_$name"); + + my $res = ""; + foreach (@{$e->{ELEMENTS}}) { + $res.="\n\t\t$_->{CASE}:\n"; + if ($_->{TYPE} ne "EMPTY") { + $res.="\t\t\t".$self->Element($_, $name, $ifname, undef, undef)."\n"; + } + $res.="\t\tbreak;\n"; + } + + my $switch_type = undef; + my $switch_dissect = undef; + my $switch_raw_type = SwitchType($e, $e->{SWITCH_TYPE}); + if (defined($switch_raw_type)) { + $switch_type = "g${switch_raw_type}"; + $switch_dissect = "dissect_ndr_${switch_raw_type}"; + } + + $self->pidl_fn_start($dissectorname); + $self->pidl_code("static int"); + $self->pidl_code("$dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *parent_tree _U_, dcerpc_info* di _U_, guint8 *drep _U_, int hf_index _U_, guint32 param _U_)"); + $self->pidl_code("{"); + $self->indent; + $self->pidl_code("proto_item *item = NULL;"); + $self->pidl_code("proto_tree *tree = NULL;"); + $self->pidl_code("int old_offset;"); + if (!defined $switch_type) { + $self->pidl_code("guint32 level = param;"); + } else { + $self->pidl_code("$switch_type level;"); + } + $self->pidl_code(""); + + $self->pidl_code("old_offset = offset;"); + $self->pidl_code("if (parent_tree) {"); + $self->indent; + $self->pidl_code("tree = proto_tree_add_subtree(parent_tree, tvb, offset, -1, ett_$ifname\_$name, &item, \"$name\");"); + $self->deindent; + $self->pidl_code("}"); + + $self->pidl_code(""); + + if (defined $switch_type) { + $self->pidl_code("offset = $switch_dissect(tvb, offset, pinfo, tree, di, drep, hf_index, &level);"); + + if ($e->{ALIGN} > 1) { + $self->pidl_code("ALIGN_TO_$e->{ALIGN}_BYTES;"); + $self->pidl_code(""); + } + } + + + $self->pidl_code("switch(level) {$res\t}"); + $self->pidl_code("proto_item_set_len(item, offset-old_offset);\n"); + $self->pidl_code(""); + + $self->pidl_code("return offset;"); + $self->deindent; + $self->pidl_code("}"); + $self->pidl_fn_end($dissectorname); + + $self->register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0); +} + +sub Const($$$) +{ + my ($self,$const,$ifname) = @_; + + if (!defined($const->{ARRAY_LEN}[0])) { + $self->pidl_hdr("#define $const->{NAME}\t( $const->{VALUE} )\n"); + } else { + $self->pidl_hdr("#define $const->{NAME}\t $const->{VALUE}\n"); + } +} + +sub Typedef($$$$) +{ + my ($self,$e,$name,$ifname) = @_; + + $self->Type($e->{DATA}, $name, $ifname); +} + +sub Type($$$$) +{ + my ($self, $e, $name, $ifname) = @_; + + $self->PrintIdl(DumpType($e->{ORIGINAL})); + { + ENUM => \&Enum, + STRUCT => \&Struct, + UNION => \&Union, + BITMAP => \&Bitmap, + TYPEDEF => \&Typedef, + PIPE => \&Pipe + }->{$e->{TYPE}}->($self, $e, $name, $ifname); +} + +sub RegisterInterface($$) +{ + my ($self, $x) = @_; + + $self->pidl_fn_start("proto_register_dcerpc_$x->{NAME}"); + $self->pidl_code("void proto_register_dcerpc_$x->{NAME}(void)"); + $self->pidl_code("{"); + $self->indent; + + $self->{res}->{headers} .= "void proto_register_dcerpc_$x->{NAME}(void);\n"; + + $self->{res}->{code}.=$self->DumpHfList()."\n"; + $self->{res}->{code}.="\n".DumpEttList($self->{ett})."\n"; + + if (defined($x->{UUID})) { + # These can be changed to non-pidl_code names if the old + # dissectors in epan/dissectors are deleted. + + my $name = uc($x->{NAME}) . " (pidl)"; + my $short_name = uc($x->{NAME}); + my $filter_name = $x->{NAME}; + + if (has_property($x, "helpstring")) { + $name = $x->{PROPERTIES}->{helpstring}; + } + + if (defined($self->{conformance}->{protocols}->{$x->{NAME}})) { + $short_name = $self->{conformance}->{protocols}->{$x->{NAME}}->{SHORTNAME}; + $name = $self->{conformance}->{protocols}->{$x->{NAME}}->{LONGNAME}; + $filter_name = $self->{conformance}->{protocols}->{$x->{NAME}}->{FILTERNAME}; + } + + $self->pidl_code("proto_dcerpc_$x->{NAME} = proto_register_protocol(".make_str($name).", ".make_str($short_name).", ".make_str($filter_name).");"); + + $self->pidl_code("proto_register_field_array(proto_dcerpc_$x->{NAME}, hf, array_length (hf));"); + $self->pidl_code("proto_register_subtree_array(ett, array_length(ett));"); + } else { + $self->pidl_code("proto_dcerpc = proto_get_id_by_filter_name(\"dcerpc\");"); + $self->pidl_code("proto_register_field_array(proto_dcerpc, hf, array_length(hf));"); + $self->pidl_code("proto_register_subtree_array(ett, array_length(ett));"); + } + + $self->deindent; + $self->pidl_code("}\n"); + $self->pidl_fn_end("proto_register_dcerpc_$x->{NAME}"); +} + +sub RegisterInterfaceHandoff($$) +{ + my ($self,$x) = @_; + + if (defined($x->{UUID})) { + $self->pidl_fn_start("proto_reg_handoff_dcerpc_$x->{NAME}"); + $self->pidl_code("void proto_reg_handoff_dcerpc_$x->{NAME}(void)"); + $self->pidl_code("{"); + $self->indent; + $self->pidl_code("dcerpc_init_uuid(proto_dcerpc_$x->{NAME}, ett_dcerpc_$x->{NAME},"); + $self->pidl_code("\t&uuid_dcerpc_$x->{NAME}, ver_dcerpc_$x->{NAME},"); + $self->pidl_code("\t$x->{NAME}_dissectors, hf_$x->{NAME}_opnum);"); + $self->deindent; + $self->pidl_code("}"); + $self->pidl_fn_end("proto_reg_handoff_dcerpc_$x->{NAME}"); + + $self->{res}->{headers} .= "void proto_reg_handoff_dcerpc_$x->{NAME}(void);\n"; + + $self->{hf_used}->{"hf_$x->{NAME}_opnum"} = 1; + } +} + +sub ProcessInclude +{ + my $self = shift; + my @includes = @_; + foreach (@includes) { + $self->pidl_hdr("#include \"$_\""); + } + $self->pidl_hdr(""); +} + +sub ProcessImport +{ + my $self = shift; + my @imports = @_; + foreach (@imports) { + next if($_ eq "security"); + s/^\"//; + s/\.idl"?$//; + s/^.*\///; + $self->pidl_hdr("#include \"packet-dcerpc-$_\.h\""); + } + $self->pidl_hdr(""); +} + +sub ProcessInterface($$) +{ + my ($self, $x) = @_; + + push(@{$self->{conformance}->{strip_prefixes}}, $x->{NAME}); + + my $define = "__PACKET_DCERPC_" . uc($_->{NAME}) . "_H"; + $self->pidl_hdr("#ifndef $define"); + $self->pidl_hdr("#define $define"); + $self->pidl_hdr(""); + + $self->pidl_def("static gint proto_dcerpc_$x->{NAME} = -1;"); + $self->register_ett("ett_dcerpc_$x->{NAME}"); + $self->register_hf_field("hf_$x->{NAME}_opnum", "Operation", "$x->{NAME}.opnum", "FT_UINT16", "BASE_DEC", "NULL", 0, ""); + + if (defined($x->{UUID})) { + my $if_uuid = $x->{UUID}; + + $self->pidl_def("/* Version information */\n\n"); + + $self->pidl_def("static e_guid_t uuid_dcerpc_$x->{NAME} = {"); + $self->pidl_def("\t0x" . substr($if_uuid, 1, 8) + . ", 0x" . substr($if_uuid, 10, 4) + . ", 0x" . substr($if_uuid, 15, 4) . ","); + $self->pidl_def("\t{ 0x" . substr($if_uuid, 20, 2) + . ", 0x" . substr($if_uuid, 22, 2) + . ", 0x" . substr($if_uuid, 25, 2) + . ", 0x" . substr($if_uuid, 27, 2) + . ", 0x" . substr($if_uuid, 29, 2) + . ", 0x" . substr($if_uuid, 31, 2) + . ", 0x" . substr($if_uuid, 33, 2) + . ", 0x" . substr($if_uuid, 35, 2) . " }"); + $self->pidl_def("};"); + + my $maj = 0x0000FFFF & $x->{VERSION}; + $maj =~ s/\.(.*)$//g; + $self->pidl_def("static guint16 ver_dcerpc_$x->{NAME} = $maj;"); + $self->pidl_def(""); + } + + $return_types{$x->{NAME}} = {}; + + $self->Interface($x); + $self->pidl_code("\n".DumpFunctionTable($x)); + + foreach (sort(keys %{$return_types{$x->{NAME}}})) { + my ($type, $desc) = @{$return_types{$x->{NAME}}->{$_}}; + my $dt = $self->find_type($type); + $dt or die("Unable to find information about return type `$type'"); + $self->register_hf_field("hf_$x->{NAME}_$_", $desc, "$x->{NAME}.$_", $dt->{FT_TYPE}, "BASE_HEX", $dt->{VALSSTRING}, 0, ""); + $self->{hf_used}->{"hf_$x->{NAME}_$_"} = 1; + } + + $self->RegisterInterface($x); + $self->RegisterInterfaceHandoff($x); + + if (exists ($self->{conformance}->{header})) { + $self->pidl_hdr($self->{conformance}->{header}); + } + + $self->pidl_hdr("#endif /* $define */"); +} + +sub find_type($$) +{ + my ($self, $n) = @_; + + return $self->{conformance}->{types}->{$n}; +} + +sub register_type($$$$$$$$) +{ + my ($self, $type,$call,$ft,$base,$mask,$vals,$length) = @_; + + return if (defined($self->{conformance}->{types}->{$type})); + + $self->{conformance}->{types}->{$type} = { + NAME => $type, + DISSECTOR_NAME => $call, + FT_TYPE => $ft, + BASE_TYPE => $base, + MASK => $mask, + VALSSTRING => $vals, + ALIGNMENT => $length + }; +} + +# Loads the default types +sub Initialize($$) +{ + my ($self, $cnf_file) = @_; + + $self->{conformance} = { + imports => {}, + header_fields=> {} + }; + + ReadConformance($cnf_file, $self->{conformance}) or print STDERR "warning: No conformance file `$cnf_file'\n"; + + foreach my $bytes (qw(1 2 4 8)) { + my $bits = $bytes * 8; + $self->register_type("uint$bits", "offset = PIDL_dissect_uint$bits(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);", "FT_UINT$bits", "BASE_DEC", 0, "NULL", $bytes); + $self->register_type("int$bits", "offset = PIDL_dissect_uint$bits(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);", "FT_INT$bits", "BASE_DEC", 0, "NULL", $bytes); + } + + $self->register_type("uint3264", "offset = dissect_ndr_uint3264(tvb, offset, pinfo, tree, di, drep, \@HF\@, NULL);", "FT_UINT32", "BASE_DEC", 0, "NULL", 8); + $self->register_type("hyper", "offset = dissect_ndr_uint64(tvb, offset, pinfo, tree, di, drep, \@HF\@, NULL);", "FT_UINT64", "BASE_DEC", 0, "NULL", 8); + $self->register_type("udlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, di, drep, \@HF\@, NULL);", "FT_UINT64", "BASE_DEC", 0, "NULL", 4); + $self->register_type("bool8", "offset = PIDL_dissect_uint8(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);","FT_INT8", "BASE_DEC", 0, "NULL", 1); + $self->register_type("char", "offset = PIDL_dissect_uint8(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);","FT_INT8", "BASE_DEC", 0, "NULL", 1); + $self->register_type("long", "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);","FT_INT32", "BASE_DEC", 0, "NULL", 4); + $self->register_type("dlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, di, drep, \@HF\@, NULL);","FT_INT64", "BASE_DEC", 0, "NULL", 8); + $self->register_type("GUID", "offset = dissect_ndr_uuid_t(tvb, offset, pinfo, tree, di, drep, \@HF\@, NULL);","FT_GUID", "BASE_NONE", 0, "NULL", 4); + $self->register_type("policy_handle", "offset = PIDL_dissect_policy_hnd(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);","FT_BYTES", "BASE_NONE", 0, "NULL", 4); + $self->register_type("NTTIME", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, di, drep, \@HF\@);","FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", 0, "NULL", 4); + $self->register_type("NTTIME_hyper", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, di, drep, \@HF\@);","FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", 0, "NULL", 4); + $self->register_type("time_t", "offset = dissect_ndr_time_t(tvb, offset, pinfo,tree, di, drep, \@HF\@, NULL);","FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", 0, "NULL", 4); + $self->register_type("NTTIME_1sec", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, di, drep, \@HF\@);", "FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", 0, "NULL", 4); + $self->register_type("dom_sid28", + "offset = dissect_ndr_nt_SID28(tvb, offset, pinfo, tree, di, drep, \@HF\@);", "FT_STRING", "BASE_NONE", 0, "NULL", 4); + $self->register_type("SID", + "offset = dissect_ndr_nt_SID_with_options(tvb, offset, pinfo, tree, di, drep, param, \@HF\@);","FT_STRING", "BASE_NONE", 0, "NULL", 4); + $self->register_type("WERROR", + "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);","FT_UINT32", "BASE_DEC", 0, "VALS(WERR_errors)", 4); + $self->register_type("NTSTATUS", + "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);","FT_UINT32", "BASE_DEC", 0, "VALS(NT_errors)", 4); + $self->register_type("HRESULT", + "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);","FT_UINT32", "BASE_DEC", 0, "VALS(HRES_errors)", 4); + $self->register_type("ipv6address", "proto_tree_add_item(tree, \@HF\@, tvb, offset, 16, ENC_NA); offset += 16;", "FT_IPv6", "BASE_NONE", 0, "NULL", 16); + $self->register_type("ipv4address", "proto_tree_add_item(tree, \@HF\@, tvb, offset, 4, ENC_BIG_ENDIAN); offset += 4;", "FT_IPv4", "BASE_NONE", 0, "NULL", 4); + +} + +##################################################################### +# Generate Wireshark parser and header code +sub Parse($$$$$) +{ + my($self,$ndr,$idl_file,$h_filename,$cnf_file) = @_; + + $self->Initialize($cnf_file); + + return (undef, undef) if defined($self->{conformance}->{noemit_dissector}); + + my $notice = +"/* DO NOT EDIT + This file was automatically generated by Pidl + from $idl_file and $cnf_file. + + Pidl is a perl based IDL compiler for DCE/RPC idl files. + It is maintained by the Samba team, not the Wireshark team. + Instructions on how to download and install Pidl can be + found at https://gitlab.com/wireshark/wireshark/-/wikis/Pidl +*/ + +"; + + $self->{res}->{headers} = "\n"; + $self->{res}->{headers} .= "#include \"config.h\"\n"; + + $self->{res}->{headers} .= "#include <glib.h>\n"; + $self->{res}->{headers} .= "#include <string.h>\n"; + $self->{res}->{headers} .= "#include <epan/packet.h>\n\n"; + + $self->{res}->{headers} .= "#include \"packet-dcerpc.h\"\n"; + $self->{res}->{headers} .= "#include \"packet-dcerpc-nt.h\"\n"; + $self->{res}->{headers} .= "#include \"packet-windows-common.h\"\n"; + + my $h_basename = basename($h_filename); + + $self->{res}->{headers} .= "#include \"$h_basename\"\n"; + $self->pidl_code(""); + + if (defined($self->{conformance}->{ett})) { + register_ett($self,$_) foreach(@{$self->{conformance}->{ett}}) + } + + # Wireshark protocol registration + + foreach (@$ndr) { + $self->ProcessInterface($_) if ($_->{TYPE} eq "INTERFACE"); + $self->ProcessImport(@{$_->{PATHS}}) if ($_->{TYPE} eq "IMPORT"); + $self->ProcessInclude(@{$_->{PATHS}}) if ($_->{TYPE} eq "INCLUDE"); + } + + $self->{res}->{ett} = DumpEttDeclaration($self->{ett}); + $self->{res}->{hf} = $self->DumpHfDeclaration(); + + my $parser = $notice; + $parser.= $self->{res}->{headers}; + $parser.=$self->{res}->{ett}; + $parser.=$self->{res}->{hf}; + $parser.=$self->{res}->{def}; + if (exists ($self->{conformance}->{override})) { + $parser.=$self->{conformance}->{override}; + } + $parser.=$self->{res}->{code}; + + my $header = $notice; + $header.=$self->{res}->{hdr}; + + $self->CheckUsed($self->{conformance}); + + return ($parser,$header); +} + +############################################################################### +# ETT +############################################################################### + +sub register_ett($$) +{ + my ($self, $name) = @_; + + push (@{$self->{ett}}, $name); +} + +sub DumpEttList +{ + my ($ett) = @_; + my $res = "\tstatic gint *ett[] = {\n"; + foreach (@$ett) { + $res .= "\t\t&$_,\n"; + } + + return "$res\t};\n"; +} + +sub DumpEttDeclaration +{ + my ($ett) = @_; + my $res = "\n/* Ett declarations */\n"; + foreach (@$ett) { + $res .= "static gint $_ = -1;\n"; + } + + return "$res\n"; +} + +############################################################################### +# HF +############################################################################### + +sub register_hf_field($$$$$$$$$) +{ + my ($self,$index,$name,$filter_name,$ft_type,$base_type,$valsstring,$mask,$blurb) = @_; + + if (defined ($self->{conformance}->{hf_renames}->{$index})) { + $self->{conformance}->{hf_renames}->{$index}->{USED} = 1; + return $self->{conformance}->{hf_renames}->{$index}->{NEWNAME}; + } + + $self->{conformance}->{header_fields}->{$index} = { + INDEX => $index, + NAME => $name, + FILTER => $filter_name, + FT_TYPE => $ft_type, + BASE_TYPE => $base_type, + VALSSTRING => $valsstring, + MASK => $mask, + BLURB => $blurb + }; + + if ((not defined($blurb) or $blurb eq "") and + defined($self->{conformance}->{fielddescription}->{$index})) { + $self->{conformance}->{header_fields}->{$index}->{BLURB} = + $self->{conformance}->{fielddescription}->{$index}->{DESCRIPTION}; + $self->{conformance}->{fielddescription}->{$index}->{USED} = 1; + } + + return $index; +} + +sub change_hf_field_type($$$$) +{ + my ($self,$index,$ft_type,$base_type) = @_; + if (defined ($self->{conformance}->{hf_renames}->{$index})) { + print "Field $index has been renamed to ".$self->{conformance}->{hf_renames}->{$index}->{NEWNAME}." you can't change it's type"; + return 0; + } + + if (!defined ($self->{conformance}->{header_fields}->{$index})) { + print "Field $index doesn't exists"; + return 0; + } + $self->{conformance}->{header_fields}->{$index}->{FT_TYPE} = $ft_type; + $self->{conformance}->{header_fields}->{$index}->{BASE_TYPE} = $base_type; + return 1; +} + +sub DumpHfDeclaration($) +{ + my ($self) = @_; + my $res = ""; + + $res = "\n/* Header field declarations */\n"; + + foreach (sort(keys %{$self->{conformance}->{header_fields}})) + { + $res .= "static gint $_ = -1;\n"; + } + + return "$res\n"; +} + +sub make_str_or_null($) +{ + my $str = shift; + if (substr($str, 0, 1) eq "\"") { + $str = substr($str, 1, length($str)-2); + } + $str =~ s/^\s*//; + $str =~ s/\s*$//; + if ($str eq "") { + return "NULL"; + } + return make_str($str); +} + +sub DumpHfList($) +{ + my ($self) = @_; + my $res = "\tstatic hf_register_info hf[] = {\n"; + + foreach (sort {$a->{INDEX} cmp $b->{INDEX}} values %{$self->{conformance}->{header_fields}}) + { + $res .= "\t{ &$_->{INDEX},\n". + "\t { ".make_str($_->{NAME}).", ".make_str($_->{FILTER}).", $_->{FT_TYPE}, $_->{BASE_TYPE}, $_->{VALSSTRING}, $_->{MASK}, ".make_str_or_null($_->{BLURB}).", HFILL }},\n"; + } + + return $res."\t};\n"; +} + + +############################################################################### +# Function table +############################################################################### + +sub DumpFunctionTable($) +{ + my $if = shift; + + my $res = "static dcerpc_sub_dissector $if->{NAME}\_dissectors[] = {\n"; + foreach (@{$if->{FUNCTIONS}}) { + my $fn_name = $_->{NAME}; + $fn_name =~ s/^$if->{NAME}_//; + $res.= "\t{ $_->{OPNUM}, \"$fn_name\",\n"; + $res.= "\t $if->{NAME}_dissect_${fn_name}_request, $if->{NAME}_dissect_${fn_name}_response},\n"; + } + + $res .= "\t{ 0, NULL, NULL, NULL }\n"; + + return "$res};\n"; +} + +sub CheckUsed($$) +{ + my ($self, $conformance) = @_; + foreach (values %{$conformance->{header_fields}}) { + if (not defined($self->{hf_used}->{$_->{INDEX}})) { + warning($_->{POS}, "hf field `$_->{INDEX}' not used"); + } + } + + foreach (values %{$conformance->{hf_renames}}) { + if (not $_->{USED}) { + warning($_->{POS}, "hf field `$_->{OLDNAME}' not used"); + } + } + + foreach (values %{$conformance->{dissectorparams}}) { + if (not $_->{USED}) { + warning($_->{POS}, "dissector param never used"); + } + } + + foreach (values %{$conformance->{imports}}) { + if (not $_->{USED}) { + warning($_->{POS}, "import never used"); + } + } + + foreach (values %{$conformance->{types}}) { + if (not $_->{USED} and defined($_->{POS})) { + warning($_->{POS}, "type never used"); + } + } + + foreach (values %{$conformance->{fielddescription}}) { + if (not $_->{USED}) { + warning($_->{POS}, "description never used"); + } + } + + foreach (values %{$conformance->{tfs}}) { + if (not $_->{USED}) { + warning($_->{POS}, "True/False description never used"); + } + } +} + +1; |