| 1 | use v5.14; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | |||
| 5 | package XDR::Gen; | ||
| 6 | $XDR::Gen::VERSION = '1.1.2'; | ||
| 7 | use Carp qw(croak confess); | ||
| 8 | use IO::Handle; | ||
| 9 | use List::Util qw(max); | ||
| 10 | use Scalar::Util qw(blessed reftype); | ||
| 11 | |||
| 12 | my %serializers = ( | ||
| 13 | # primitive types | ||
| 14 | 'bool' => \&_serializer_bool, | ||
| 15 | 'char' => \&_serializer_char, | ||
| 16 | 'short' => \&_serializer_short, | ||
| 17 | 'int' => \&_serializer_int, | ||
| 18 | 'long' => \&_serializer_long, | ||
| 19 | 'hyper' => \&_serializer_long, | ||
| 20 | 'float' => \&_serializer_float, | ||
| 21 | 'double' => \&_serializer_double, | ||
| 22 | 'quadruple' => '', # D> | ||
| 23 | 'string' => \&_serializer_string, | ||
| 24 | 'opaque' => \&_serializer_opaque, | ||
| 25 | 'void' => \&_serializer_void, | ||
| 26 | ); | ||
| 27 | |||
| 28 | my %deserializers = ( | ||
| 29 | # primitive types | ||
| 30 | 'bool' => \&_deserializer_bool, | ||
| 31 | 'char' => \&_deserializer_char, | ||
| 32 | 'short' => \&_deserializer_short, | ||
| 33 | 'int' => \&_deserializer_int, | ||
| 34 | 'long' => \&_deserializer_long, | ||
| 35 | 'hyper' => \&_deserializer_long, | ||
| 36 | 'float' => \&_deserializer_float, | ||
| 37 | 'double' => \&_deserializer_double, | ||
| 38 | 'quadruple' => '', # D> | ||
| 39 | 'string' => \&_deserializer_string, | ||
| 40 | 'opaque' => \&_deserializer_opaque, | ||
| 41 | 'void' => \&_deserializer_void, | ||
| 42 | ); | ||
| 43 | |||
| 44 | our $varnum = 0; | ||
| 45 | our %constants; | ||
| 46 | |||
| 47 | # add constant from ENUM element or CONST declaration | ||
| 48 | sub _add_constant { | ||
| 49 | my ($node) = @_; | ||
| 50 | $constants{$node->{name}->{content}} = $node->{value}->{content}; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub _resolve_to_number { | ||
| 54 | my ($datum) = @_; | ||
| 55 | while (exists $constants{$datum}) { | ||
| 56 | # Recursive defitions will loop endlessly?! | ||
| 57 | $datum = $constants{$datum}; | ||
| 58 | } | ||
| 59 | if ($datum =~ m/^0x[0-9a-f]+$/i) { | ||
| 60 | return hex($datum); | ||
| 61 | } | ||
| 62 | elsif ($datum =~ m/^0\d+$/) { | ||
| 63 | return oct($datum); | ||
| 64 | } | ||
| 65 | elsif ($datum =~ m/^-?\d+$/) { | ||
| 66 | return $datum; | ||
| 67 | } | ||
| 68 | croak "Value '$datum' does not resolve to a number"; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub _var { | ||
| 72 | if ($varnum) { | ||
| 73 | return $_[0] . $varnum++; | ||
| 74 | } | ||
| 75 | $varnum++; | ||
| 76 | return $_[0]; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub _indent { | ||
| 80 | chomp $_[0]; | ||
| 81 | return ($_[0] =~ s/\n(?!\s*(\n|$))/\n /gr); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub _assert_value_var { | ||
| 85 | confess q|Serializer error: missing i/o variable name| | ||
| 86 | unless defined $_[0]; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub _deserializer_bool { | ||
| 90 | my ($ast_node, $value) = @_; | ||
| 91 | _assert_value_var($value); | ||
| 92 | |||
| 93 | return <<~SERIAL; | ||
| 94 | # my (\$class, \$value, \$index, \$input) = \@_; | ||
| 95 | die "Input buffer too short" | ||
| 96 | if (\$input_length - \$_[2]) < 4; | ||
| 97 | $value = unpack("L>", substr( \$_[3], \$_[2] )); | ||
| 98 | die "Incorrect bool value $value (must be 0 or 1)" | ||
| 99 | unless $value == 0 or $value == 1; | ||
| 100 | \$_[2] += 4; | ||
| 101 | SERIAL | ||
| 102 | } | ||
| 103 | |||
| 104 | sub _serializer_bool { | ||
| 105 | my ($ast_node, $value) = @_; | ||
| 106 | _assert_value_var($value); | ||
| 107 | |||
| 108 | # Exploit Perl 5.36 booleanness by using (static) comparison results | ||
| 109 | $value //= '($_[1] ? (1==1) : (0==1))'; | ||
| 110 | return <<~SERIAL; | ||
| 111 | # my (\$class, \$value, \$index, \$output) = \@_; | ||
| 112 | # Allow <undef> to model a <false> value | ||
| 113 | substr( \$_[3], \$_[2] ) = pack("L>", $value); | ||
| 114 | \$_[2] += 4; | ||
| 115 | SERIAL | ||
| 116 | } | ||
| 117 | |||
| 118 | sub _deserializer_char { | ||
| 119 | my ($ast_node, $value) = @_; | ||
| 120 | my $signed = not $ast_node->{type}->{unsigned}; | ||
| 121 | _assert_value_var($value); | ||
| 122 | |||
| 123 | if ($signed) { | ||
| 124 | return <<~SERIAL; | ||
| 125 | # my (\$class, \$value, \$index, \$input) = \@_; | ||
| 126 | die "Input buffer too short" | ||
| 127 | if (\$input_length - \$_[2]) < 4; | ||
| 128 | $value = unpack("l>", substr( \$_[3], \$_[2] )); | ||
| 129 | \$_[2] += 4; | ||
| 130 | die "Out of bounds 'char': $value" | ||
| 131 | unless (-128 <= $value and $value < 128); | ||
| 132 | SERIAL | ||
| 133 | } | ||
| 134 | else { | ||
| 135 | return <<~SERIAL; | ||
| 136 | # my (\$class, \$value, \$index, \$input) = \@_; | ||
| 137 | die "Input buffer too short" | ||
| 138 | if (\$input_length - \$_[2]) < 4; | ||
| 139 | $value = unpack("L>", substr( \$_[3], \$_[2] )); | ||
| 140 | \$_[2] += 4; | ||
| 141 | die "Out of bounds 'unsigned char': $value" | ||
| 142 | unless (0 <= $value and $value <= 255); | ||
| 143 | SERIAL | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | sub _serializer_char { | ||
| 148 | my ($ast_node, $value) = @_; | ||
| 149 | my $signed = not $ast_node->{type}->{unsigned}; | ||
| 150 | _assert_value_var($value); | ||
| 151 | |||
| 152 | if ($signed) { | ||
| 153 | return <<~SERIAL; | ||
| 154 | # my (\$class, \$value, \$index, \$output) = \@_; | ||
| 155 | croak "Missing required input 'char' value" | ||
| 156 | unless defined $value; | ||
| 157 | die "Out of bounds 'char': $value" | ||
| 158 | unless (-128 <= $value and $value < 128); | ||
| 159 | die "Non-integer 'char' value given: $value" | ||
| 160 | unless int($value) == $value; | ||
| 161 | substr( \$_[3], \$_[2] ) = pack("l>", $value); | ||
| 162 | \$_[2] += 4; | ||
| 163 | SERIAL | ||
| 164 | } | ||
| 165 | else { | ||
| 166 | return <<~SERIAL; | ||
| 167 | # my (\$class, \$value, \$index, \$output) = \@_; | ||
| 168 | croak "Missing required input 'char' value" | ||
| 169 | unless defined $value; | ||
| 170 | die "Out of bounds 'unsigned char': $value" | ||
| 171 | unless (0 <= $value and $value <= 255); | ||
| 172 | die "Non-integer 'char' value given: $value" | ||
| 173 | unless int($value) == $value; | ||
| 174 | substr( \$_[3], \$_[2] ) = pack("L>", $value); | ||
| 175 | \$_[2] += 4; | ||
| 176 | SERIAL | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | sub _deserializer_short { | ||
| 181 | my ($ast_node, $value) = @_; | ||
| 182 | my $signed = not $ast_node->{type}->{unsigned}; | ||
| 183 | _assert_value_var($value); | ||
| 184 | |||
| 185 | if ($signed) { | ||
| 186 | return <<~SERIAL; | ||
| 187 | # my (\$class, \$value, \$index, \$input) = \@_; | ||
| 188 | die "Input buffer too short" | ||
| 189 | if (\$input_length - \$_[2]) < 4; | ||
| 190 | $value = unpack("l>", substr( \$_[3], \$_[2] )); | ||
| 191 | \$_[2] += 4; | ||
| 192 | die "Out of bounds 'short': $value" | ||
| 193 | unless (-32768 <= $value and $value < 32768); | ||
| 194 | SERIAL | ||
| 195 | } | ||
| 196 | else { | ||
| 197 | return <<~SERIAL; | ||
| 198 | # my (\$class, \$value, \$index, \$input) = \@_; | ||
| 199 | die "Input buffer too short" | ||
| 200 | if (\$input_length - \$_[2]) < 4; | ||
| 201 | $value = unpack("L>", substr( \$_[3], \$_[2] )); | ||
| 202 | \$_[2] += 4; | ||
| 203 | die "Out of bounds 'unsigned short': $value" | ||
| 204 | unless (0 <= $value and $value <= 65535); | ||
| 205 | SERIAL | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | sub _serializer_short { | ||
| 210 | my ($ast_node, $value) = @_; | ||
| 211 | my $signed = not $ast_node->{type}->{unsigned}; | ||
| 212 | _assert_value_var($value); | ||
| 213 | |||
| 214 | if ($signed) { | ||
| 215 | return <<~SERIAL; | ||
| 216 | # my (\$class, \$value, \$index, \$output) = \@_; | ||
| 217 | croak "Missing required input 'short' value" | ||
| 218 | unless defined $value; | ||
| 219 | die "Out of bounds 'short': $value" | ||
| 220 | unless (-32768 <= $value and $value < 32768); | ||
| 221 | die "Non-integer 'short' value given: $value" | ||
| 222 | unless int($value) == $value; | ||
| 223 | substr( \$_[3], \$_[2] ) = pack("l>", $value); | ||
| 224 | \$_[2] += 4; | ||
| 225 | SERIAL | ||
| 226 | } | ||
| 227 | else { | ||
| 228 | return <<~SERIAL; | ||
| 229 | # my (\$class, \$value, \$index, \$output) = \@_; | ||
| 230 | croak "Missing required input 'short' value" | ||
| 231 | unless defined $value; | ||
| 232 | die "Out of bounds 'unsigned short': $value" | ||
| 233 | unless (0 <= $value and $value <= 65535); | ||
| 234 | die "Non-integer 'short' value given: $value" | ||
| 235 | unless int($value) == $value; | ||
| 236 | substr( \$_[3], \$_[2] ) = pack("L>", $value); | ||
| 237 | \$_[2] += 4; | ||
| 238 | SERIAL | ||
| 239 | } | ||
| 240 | } | ||
| 241 | |||
| 242 | sub _deserializer_int { | ||
| 243 | my ($ast_node, $value) = @_; | ||
| 244 | my $signed = not $ast_node->{type}->{unsigned}; | ||
| 245 | _assert_value_var($value); | ||
| 246 | |||
| 247 | if ($signed) { | ||
| 248 | return <<~SERIAL; | ||
| 249 | # my (\$class, \$value, \$index, \$input) = \@_; | ||
| 250 | die "Input buffer too short" | ||
| 251 | if (\$input_length - \$_[2]) < 4; | ||
| 252 | $value = unpack("l>", substr( \$_[3], \$_[2] )); | ||
| 253 | \$_[2] += 4; | ||
| 254 | die "Out of bounds 'int': $value" | ||
| 255 | unless (-2147483648 <= $value and $value < 2147483648); | ||
| 256 | SERIAL | ||
| 257 | } | ||
| 258 | else { | ||
| 259 | return <<~SERIAL; | ||
| 260 | # my (\$class, \$value, \$index, \$input) = \@_; | ||
| 261 | die "Input buffer too short" | ||
| 262 | if (\$input_length - \$_[2]) < 4; | ||
| 263 | $value = unpack("L>", substr( \$_[3], \$_[2] )); | ||
| 264 | \$_[2] += 4; | ||
| 265 | die "Out of bounds 'unsigned int': $value" | ||
| 266 | unless (0 <= $value and $value <= 4294967295); | ||
| 267 | SERIAL | ||
| 268 | } | ||
| 269 | } | ||
| 270 | |||
| 271 | sub _serializer_int { | ||
| 272 | my ($ast_node, $value) = @_; | ||
| 273 | my $signed = not $ast_node->{type}->{unsigned}; | ||
| 274 | _assert_value_var($value); | ||
| 275 | |||
| 276 | if ($signed) { | ||
| 277 | return <<~SERIAL; | ||
| 278 | # my (\$class, \$value, \$index, \$output) = \@_; | ||
| 279 | croak "Missing required input 'int' value" | ||
| 280 | unless defined $value; | ||
| 281 | die "Out of bounds 'int': $value" | ||
| 282 | unless (-2147483648 <= $value and $value < 2147483648); | ||
| 283 | die "Non-integer 'int' value given: $value" | ||
| 284 | unless int($value) == $value; | ||
| 285 | substr( \$_[3], \$_[2] ) = pack("l>", $value); | ||
| 286 | \$_[2] += 4; | ||
| 287 | SERIAL | ||
| 288 | } | ||
| 289 | else { | ||
| 290 | return <<~SERIAL; | ||
| 291 | # my (\$class, \$value, \$index, \$output) = \@_; | ||
| 292 | croak "Missing required input 'unsigned int' value" | ||
| 293 | unless defined $value; | ||
| 294 | die "Out of bounds 'unsigned int': $value" | ||
| 295 | unless (0 <= $value and $value <= 4294967295); | ||
| 296 | die "Non-integer 'int' value given: $value" | ||
| 297 | unless int($value) == $value; | ||
| 298 | substr( \$_[3], \$_[2] ) = pack("L>", $value); | ||
| 299 | \$_[2] += 4; | ||
| 300 | SERIAL | ||
| 301 | } | ||
| 302 | } | ||
| 303 | |||
| 304 | sub _deserializer_long { | ||
| 305 | my ($ast_node, $value) = @_; | ||
| 306 | my $signed = not $ast_node->{type}->{unsigned}; | ||
| 307 | my $type_name = $ast_node->{type}->{name}; | ||
| 308 | _assert_value_var($value); | ||
| 309 | |||
| 310 | if ($signed) { | ||
| 311 | return <<~SERIAL; | ||
| 312 | # my (\$class, \$value, \$index, \$input) = \@_; | ||
| 313 | die "Input buffer too short" | ||
| 314 | if (\$input_length - \$_[2]) < 8; | ||
| 315 | $value = unpack("q>", substr( \$_[3], \$_[2] )); | ||
| 316 | \$_[2] += 8; | ||
| 317 | die "Out of bounds '$type_name': $value" | ||
| 318 | unless (-9223372036854775808 <= $value | ||
| 319 | and $value < 9223372036854775808); | ||
| 320 | SERIAL | ||
| 321 | } | ||
| 322 | else { | ||
| 323 | return <<~SERIAL; | ||
| 324 | # my (\$class, \$value, \$index, \$input) = \@_; | ||
| 325 | die "Input buffer too short" | ||
| 326 | if (\$input_length - \$_[2]) < 8; | ||
| 327 | $value = unpack("Q>", substr( \$_[3], \$_[2] )); | ||
| 328 | \$_[2] += 8; | ||
| 329 | die "Out of bounds 'unsigned $type_name': $value" | ||
| 330 | unless (0 <= $value | ||
| 331 | and $value <= 18446744073709551615); | ||
| 332 | SERIAL | ||
| 333 | } | ||
| 334 | } | ||
| 335 | |||
| 336 | sub _serializer_long { | ||
| 337 | my ($ast_node, $value) = @_; | ||
| 338 | my $signed = not $ast_node->{type}->{unsigned}; | ||
| 339 | my $type_name = $ast_node->{type}->{name}; | ||
| 340 | _assert_value_var($value); | ||
| 341 | |||
| 342 | if ($signed) { | ||
| 343 | return <<~SERIAL; | ||
| 344 | # my (\$class, \$value, \$index, \$output) = \@_; | ||
| 345 | croak "Missing required input 'long' value" | ||
| 346 | unless defined $value; | ||
| 347 | die "Out of bounds '$type_name': $value" | ||
| 348 | unless (-9223372036854775808 <= $value | ||
| 349 | and $value < 9223372036854775808); | ||
| 350 | die "Non-integer 'long' value given: $value" | ||
| 351 | unless int($value) == $value; | ||
| 352 | substr( \$_[3], \$_[2] ) = pack("q>", $value); | ||
| 353 | \$_[2] += 8; | ||
| 354 | SERIAL | ||
| 355 | } | ||
| 356 | else { | ||
| 357 | return <<~SERIAL; | ||
| 358 | # my (\$class, \$value, \$index, \$output) = \@_; | ||
| 359 | croak "Missing required input 'unsigned long' value" | ||
| 360 | unless defined $value; | ||
| 361 | die "Out of bounds 'unsigned $type_name': $value" | ||
| 362 | unless (0 <= $value | ||
| 363 | and $value <= 18446744073709551615); | ||
| 364 | die "Non-integer 'long' value given: $value" | ||
| 365 | unless int($value) == $value; | ||
| 366 | substr( \$_[3], \$_[2] ) = pack("Q>", $value); | ||
| 367 | \$_[2] += 8; | ||
| 368 | SERIAL | ||
| 369 | } | ||
| 370 | } | ||
| 371 | |||
| 372 | sub _deserializer_float { | ||
| 373 | my ($ast_node, $value) = @_; | ||
| 374 | _assert_value_var($value); | ||
| 375 | |||
| 376 | return <<~SERIAL; | ||
| 377 | # my (\$class, \$value, \$index, \$input) = \@_; | ||
| 378 | die "The platform doesn't support IEEE-754 floats" | ||
| 379 | unless \$Config{d_double_style_ieee754}; | ||
| 380 | die "Input buffer too short" | ||
| 381 | if (\$input_length - \$_[2]) < 4; | ||
| 382 | $value = unpack("f>", substr( \$_[3], \$_[2] )); | ||
| 383 | die "Incorrect bool value $value (must be 0 or 1)" | ||
| 384 | unless $value == 0 or $value == 1; | ||
| 385 | \$_[2] += 4; | ||
| 386 | SERIAL | ||
| 387 | } | ||
| 388 | |||
| 389 | sub _serializer_float { | ||
| 390 | my ($ast_node, $value) = @_; | ||
| 391 | _assert_value_var($value); | ||
| 392 | |||
| 393 | return <<~SERIAL; | ||
| 394 | # my (\$class, \$value, \$index, \$output) = \@_; | ||
| 395 | die "The platform doesn't support IEEE-754 floats" | ||
| 396 | unless \$Config{d_double_style_ieee754}; | ||
| 397 | croak "Missing required input 'float' value" | ||
| 398 | unless defined $value; | ||
| 399 | substr( \$_[3], \$_[2] ) = pack("f>", $value); | ||
| 400 | \$_[2] += 4; | ||
| 401 | SERIAL | ||
| 402 | } | ||
| 403 | |||
| 404 | sub _deserializer_double { | ||
| 405 | my ($ast_node, $value) = @_; | ||
| 406 | _assert_value_var($value); | ||
| 407 | |||
| 408 | return <<~SERIAL; | ||
| 409 | # my (\$class, \$value, \$index, \$input) = \@_; | ||
| 410 | die "The platform doesn't support IEEE-754 floats" | ||
| 411 | unless \$Config{d_double_style_ieee754}; | ||
| 412 | die "Input buffer too short" | ||
| 413 | if (\$input_length - \$_[2]) < 8; | ||
| 414 | $value = unpack("d>", substr( \$_[3], \$_[2] )); | ||
| 415 | die "Incorrect bool value $value (must be 0 or 1)" | ||
| 416 | unless $value == 0 or $value == 1; | ||
| 417 | \$_[2] += 8; | ||
| 418 | SERIAL | ||
| 419 | } | ||
| 420 | |||
| 421 | sub _serializer_double { | ||
| 422 | my ($ast_node, $value) = @_; | ||
| 423 | _assert_value_var($value); | ||
| 424 | |||
| 425 | return <<~SERIAL; | ||
| 426 | # my (\$class, \$value, \$index, \$output) = \@_; | ||
| 427 | die "The platform doesn't support IEEE-754 floats" | ||
| 428 | unless \$Config{d_double_style_ieee754}; | ||
| 429 | croak "Missing required input 'double' value" | ||
| 430 | unless defined $value; | ||
| 431 | substr( \$_[3], \$_[2] ) = pack("d>", $value); | ||
| 432 | \$_[2] += 8; | ||
| 433 | SERIAL | ||
| 434 | } | ||
| 435 | |||
| 436 | sub _deserializer_string { | ||
| 437 | my ($ast_node, $value) = @_; | ||
| 438 | my $max = _resolve_to_number($ast_node->{max}->{content} // 4294967295); | ||
| 439 | _assert_value_var($value); | ||
| 440 | |||
| 441 | return <<~SERIAL; | ||
| 442 | # my (\$class, \$value, \$index, \$input) = \@_; | ||
| 443 | do { | ||
| 444 | die "Input buffer too short" | ||
| 445 | if (\$input_length - \$_[2]) < 4; | ||
| 446 | my \$len = unpack("L>", substr( \$_[3], \$_[2] )); | ||
| 447 | \$_[2] += 4; | ||
| 448 | die "String too long (max: $max): \$len" | ||
| 449 | unless (\$len <= $max); | ||
| 450 | die "Input buffer too short" | ||
| 451 | if (\$input_length - \$_[2]) < \$len; | ||
| 452 | $value = substr( \$_[3], \$_[2], \$len ); | ||
| 453 | \$_[2] += \$len + ((4 - (\$len % 4)) % 4); # skip padding too | ||
| 454 | }; | ||
| 455 | SERIAL | ||
| 456 | } | ||
| 457 | |||
| 458 | sub _serializer_string { | ||
| 459 | my ($ast_node, $value) = @_; | ||
| 460 | my $max = _resolve_to_number($ast_node->{max}->{content} // 4294967295); | ||
| 461 | _assert_value_var($value); | ||
| 462 | |||
| 463 | return <<~SERIAL; | ||
| 464 | # my (\$class, \$value, \$index, \$output) = \@_; | ||
| 465 | do { | ||
| 466 | my \$len = length $value; | ||
| 467 | croak "Missing required input 'string' value" | ||
| 468 | unless defined $value; | ||
| 469 | die "String too long (max: $max): \$len" | ||
| 470 | unless (\$len <= $max); | ||
| 471 | |||
| 472 | substr( \$_[3], \$_[2] ) = pack("L>", \$len); | ||
| 473 | \$_[2] += 4; | ||
| 474 | substr( \$_[3], \$_[2] ) = $value; | ||
| 475 | \$_[2] += \$len; | ||
| 476 | if (my \$pad = ((4 - (\$len % 4)) % 4)) { | ||
| 477 | substr( \$_[3], \$_[2] ) = ("\\0" x \$pad); | ||
| 478 | \$_[2] += \$pad; | ||
| 479 | } | ||
| 480 | }; | ||
| 481 | SERIAL | ||
| 482 | } | ||
| 483 | |||
| 484 | sub __deserializer_bytes { | ||
| 485 | my ($is_array, $ast_node, $value) = @_; | ||
| 486 | my $type_name = ($is_array ? 'array' : 'opaque data'); | ||
| 487 | my $Type_name = ucfirst( $type_name ); | ||
| 488 | my $variable_length = $ast_node->{variable}; | ||
| 489 | _assert_value_var($value); | ||
| 490 | |||
| 491 | if ($variable_length) { | ||
| 492 | my $max = _resolve_to_number($ast_node->{max}->{content} // 4294967295); | ||
| 493 | |||
| 494 | return <<~SERIAL; | ||
| 495 | # my (\$class, \$value, \$index, \$input) = \@_; | ||
| 496 | do { | ||
| 497 | die "Input buffer too short" | ||
| 498 | if (\$input_length - \$_[2]) < 4; | ||
| 499 | my \$len = unpack("L>", substr( \$_[3], \$_[2] )); | ||
| 500 | \$_[2] += 4; | ||
| 501 | die "$Type_name too long (max: $max): \$len" | ||
| 502 | unless (\$len <= $max); | ||
| 503 | die "Input buffer too short" | ||
| 504 | if (\$input_length - \$_[2]) < \$len; | ||
| 505 | $value = substr( \$_[3], \$_[2], \$len ); | ||
| 506 | \$_[2] += \$len + ((4 - (\$len % 4)) % 4); # skip padding too | ||
| 507 | }; | ||
| 508 | SERIAL | ||
| 509 | } | ||
| 510 | else { | ||
| 511 | my $count = _resolve_to_number( $ast_node->{count}->{content} ); | ||
| 512 | my $pad = (4 - ($count % 4)) % 4; | ||
| 513 | my $padcode = $pad ? " + $pad" : ''; | ||
| 514 | |||
| 515 | return <<~SERIAL; | ||
| 516 | # my (\$class, \$value, \$index, \$input) = \@_; | ||
| 517 | die "Input buffer too short" | ||
| 518 | if (\$input_length - \$_[2]) < $count; | ||
| 519 | $value = substr( \$_[3], \$_[2], $count ); | ||
| 520 | \$_[2] += $count$padcode; | ||
| 521 | SERIAL | ||
| 522 | } | ||
| 523 | } | ||
| 524 | |||
| 525 | sub __serializer_bytes { | ||
| 526 | my ($is_array, $ast_node, $value) = @_; | ||
| 527 | my $type_name = ($is_array ? 'array' : 'opaque data'); | ||
| 528 | my $Type_name = ucfirst( $type_name ); | ||
| 529 | my $variable_length = $ast_node->{variable}; | ||
| 530 | _assert_value_var($value); | ||
| 531 | |||
| 532 | if ($variable_length) { | ||
| 533 | my $max = _resolve_to_number($ast_node->{max}->{content} // 4294967295); | ||
| 534 | |||
| 535 | return <<~SERIAL; | ||
| 536 | # my (\$class, \$value, \$index, \$output) = \@_; | ||
| 537 | croak "Missing required input '$type_name' value" | ||
| 538 | unless defined $value; | ||
| 539 | do { | ||
| 540 | my \$len = length $value; | ||
| 541 | die "$Type_name too long (max: $max): \$len" | ||
| 542 | unless (\$len <= $max); | ||
| 543 | |||
| 544 | substr( \$_[3], \$_[2] ) = pack("L>", \$len); | ||
| 545 | \$_[2] += 4; | ||
| 546 | substr( \$_[3], \$_[2] ) = $value; | ||
| 547 | \$_[2] += \$len; | ||
| 548 | if (my \$pad = ((4 - (\$len % 4)) % 4)) { | ||
| 549 | substr( \$_[3], \$_[2] ) = ("\\0" x \$pad); | ||
| 550 | \$_[2] += \$pad; | ||
| 551 | } | ||
| 552 | }; | ||
| 553 | SERIAL | ||
| 554 | } | ||
| 555 | else { # fixed length | ||
| 556 | my $count = _resolve_to_number( $ast_node->{count}->{content} ); | ||
| 557 | my $pad = (4 - ($count % 4)) % 4; | ||
| 558 | my $padstr = "\\0" x $pad; | ||
| 559 | my $padcode = $pad ? <<~PAD : ''; | ||
| 560 | |||
| 561 | substr( \$_[3], \$_[2] ) = "$padstr"; | ||
| 562 | \$_[2] += $pad; | ||
| 563 | PAD | ||
| 564 | return <<~SERIAL; | ||
| 565 | # my (\$class, \$value, \$index, \$output) = \@_; | ||
| 566 | croak "Missing required input '$type_name' value" | ||
| 567 | unless defined $value; | ||
| 568 | do { | ||
| 569 | my \$len = length $value; | ||
| 570 | die "$Type_name length mismatch (defined: $count): \$len" | ||
| 571 | if not \$len == $count; | ||
| 572 | |||
| 573 | substr( \$_[3], \$_[2] ) = $value; | ||
| 574 | \$_[2] += \$len;$padcode | ||
| 575 | }; | ||
| 576 | SERIAL | ||
| 577 | } | ||
| 578 | } | ||
| 579 | |||
| 580 | sub _deserializer_opaque { | ||
| 581 | return __deserializer_bytes( 0, @_ ); | ||
| 582 | } | ||
| 583 | |||
| 584 | sub _serializer_opaque { | ||
| 585 | return __serializer_bytes( 0, @_ ); | ||
| 586 | } | ||
| 587 | |||
| 588 | sub _deserializer_void { | ||
| 589 | return <<~SERIAL; | ||
| 590 | # VOID deserialization placeholder | ||
| 591 | SERIAL | ||
| 592 | } | ||
| 593 | |||
| 594 | sub _serializer_void { | ||
| 595 | return <<~SERIAL; | ||
| 596 | # VOID serialization placeholder | ||
| 597 | SERIAL | ||
| 598 | } | ||
| 599 | |||
| 600 | sub _deserializer_named { | ||
| 601 | my ($ast_node, $value, %args) = @_; | ||
| 602 | my $ref = $args{transform}->($ast_node->{type}->{name}->{content}); | ||
| 603 | _assert_value_var($value); | ||
| 604 | |||
| 605 | return <<~SERIAL; | ||
| 606 | # my (\$class, \$value, \$index, \$input) = \@_; | ||
| 607 | \$_[0]->deserialize_$ref( $value, \$_[2], \$_[3] ); | ||
| 608 | SERIAL | ||
| 609 | } | ||
| 610 | |||
| 611 | sub _serializer_named { | ||
| 612 | my ($ast_node, $value, %args) = @_; | ||
| 613 | my $ref = $args{transform}->($ast_node->{type}->{name}->{content}); | ||
| 614 | _assert_value_var($value); | ||
| 615 | |||
| 616 | return <<~SERIAL; | ||
| 617 | # my (\$class, \$value, \$index, \$output) = \@_; | ||
| 618 | \$_[0]->serialize_$ref( $value, \$_[2], \$_[3] ); | ||
| 619 | SERIAL | ||
| 620 | } | ||
| 621 | |||
| 622 | sub _postproc_deserializer_array_char { | ||
| 623 | my ($ast_node, $value) = @_; | ||
| 624 | my $decl_type = $ast_node->{type}; | ||
| 625 | my $pack_char = $decl_type->{unsigned} ? 'C' : 'c'; | ||
| 626 | |||
| 627 | return <<~SERIAL; | ||
| 628 | |||
| 629 | $value = pack('$pack_char*', \@{ $value }); | ||
| 630 | SERIAL | ||
| 631 | } | ||
| 632 | |||
| 633 | sub _postproc_deserializer_array { | ||
| 634 | my ($ast_node, $value) = @_; | ||
| 635 | if ($ast_node->{type}->{name} eq 'char') { | ||
| 636 | return _postproc_deserializer_array_char( @_ ); | ||
| 637 | } | ||
| 638 | return ''; | ||
| 639 | } | ||
| 640 | |||
| 641 | sub _deserializer_array { | ||
| 642 | my ($ast_node, $value, %args) = @_; | ||
| 643 | |||
| 644 | my $decl = $ast_node; | ||
| 645 | _assert_value_var($value); | ||
| 646 | |||
| 647 | local $varnum = $varnum; | ||
| 648 | my $len_var = _var( '$len' ); | ||
| 649 | my $iter_var = _var( '$i' ); | ||
| 650 | my $c = _indent( _indent( _deserializer_type( $decl, $value . "->[$iter_var\]", | ||
| 651 | %args ) ) ); | ||
| 652 | my $postproc = _postproc_deserializer_array( $ast_node, $value ); | ||
| 653 | if ($decl->{variable}) { | ||
| 654 | my $max = _resolve_to_number( $ast_node->{max}->{content} // 4294967295 ); | ||
| 655 | $postproc = _indent( $postproc ); | ||
| 656 | |||
| 657 | return <<~SERIAL; | ||
| 658 | # my (\$class, \$value, \$index, \$input) = \@_; | ||
| 659 | do { | ||
| 660 | die "Input buffer too short" | ||
| 661 | if (\$input_length - \$_[2]) < 4; | ||
| 662 | my $len_var = unpack("L>", substr( \$_[3], \$_[2] )); | ||
| 663 | \$_[2] += 4; | ||
| 664 | |||
| 665 | die "Array too long (max: $max): $len_var" | ||
| 666 | unless ($len_var <= $max); | ||
| 667 | $value = []; | ||
| 668 | for my $iter_var ( 0 .. ($len_var - 1) ) { | ||
| 669 | $c | ||
| 670 | }$postproc | ||
| 671 | }; | ||
| 672 | SERIAL | ||
| 673 | } | ||
| 674 | else { # fixed length | ||
| 675 | my $count = _resolve_to_number( $ast_node->{count}->{content} ); | ||
| 676 | |||
| 677 | return <<~SERIAL; | ||
| 678 | # my (\$class, \$value, \$index, \$input) = \@_; | ||
| 679 | $value = []; | ||
| 680 | for my $iter_var ( 0 .. ($count - 1) ) { | ||
| 681 | $c | ||
| 682 | }$postproc | ||
| 683 | SERIAL | ||
| 684 | } | ||
| 685 | } | ||
| 686 | |||
| 687 | sub _preproc_serializer_array_char { | ||
| 688 | my ($ast_node, $value) = @_; | ||
| 689 | my $decl_type = $ast_node->{type}; | ||
| 690 | my $pack_char = $decl_type->{unsigned} ? 'C' : 'c'; | ||
| 691 | |||
| 692 | return <<~SERIAL; | ||
| 693 | |||
| 694 | local $value = [ unpack('$pack_char*', $value) ]; | ||
| 695 | SERIAL | ||
| 696 | } | ||
| 697 | |||
| 698 | sub _preproc_serializer_array { | ||
| 699 | my ($ast_node, $value) = @_; | ||
| 700 | if ($ast_node->{type}->{name} eq 'char') { | ||
| 701 | return _preproc_serializer_array_char( @_ ); | ||
| 702 | } | ||
| 703 | return ''; | ||
| 704 | } | ||
| 705 | |||
| 706 | sub _serializer_array { | ||
| 707 | my ($ast_node, $value, %args) = @_; | ||
| 708 | |||
| 709 | my $decl = $ast_node; | ||
| 710 | _assert_value_var($value); | ||
| 711 | |||
| 712 | local $varnum = $varnum; | ||
| 713 | my $len_var = _var( '$len' ); | ||
| 714 | my $iter_var = _var( '$i' ); | ||
| 715 | my $c = _indent( _indent( _serializer_type( $decl, $value . "->[$iter_var\]", | ||
| 716 | %args ) ) ); | ||
| 717 | my $preproc = _indent( _preproc_serializer_array( $ast_node, $value ) ); | ||
| 718 | if ($decl->{variable}) { | ||
| 719 | my $max = _resolve_to_number( $ast_node->{max}->{content} // 4294967295 ); | ||
| 720 | |||
| 721 | return <<~SERIAL; | ||
| 722 | # my (\$class, \$value, \$index, \$output) = \@_; | ||
| 723 | croak "Missing required input 'array' value" | ||
| 724 | unless defined $value; | ||
| 725 | do {$preproc | ||
| 726 | my $len_var = scalar \@{ $value }; | ||
| 727 | die "Array too long (max: $max): $len_var" | ||
| 728 | unless ($len_var <= $max); | ||
| 729 | |||
| 730 | substr( \$_[3], \$_[2] ) = pack("L>", $len_var); | ||
| 731 | \$_[2] += 4; | ||
| 732 | for my $iter_var ( 0 .. ($len_var - 1) ) { | ||
| 733 | $c | ||
| 734 | } | ||
| 735 | }; | ||
| 736 | SERIAL | ||
| 737 | } | ||
| 738 | else { # fixed length | ||
| 739 | my $count = _resolve_to_number( $ast_node->{count}->{content} ); | ||
| 740 | return <<~SERIAL; | ||
| 741 | # my (\$class, \$value, \$index, \$output) = \@_; | ||
| 742 | croak "Missing required input 'array' value" | ||
| 743 | unless defined $value; | ||
| 744 | do {$preproc | ||
| 745 | my $len_var = scalar \@{ $value }; | ||
| 746 | die "Array length mismatch (defined: $count): $len_var" | ||
| 747 | if not $len_var == $count; | ||
| 748 | |||
| 749 | for my $iter_var ( 0 .. ($len_var - 1) ) { | ||
| 750 | $c | ||
| 751 | } | ||
| 752 | }; | ||
| 753 | SERIAL | ||
| 754 | } | ||
| 755 | |||
| 756 | } | ||
| 757 | |||
| 758 | sub _enum_bitvector { | ||
| 759 | my ($node) = @_; | ||
| 760 | my $bits = ''; | ||
| 761 | |||
| 762 | for my $element (@{ $node->{elements} }) { | ||
| 763 | vec($bits, $element->{value}->{content}, 1) = 1; | ||
| 764 | } | ||
| 765 | return $bits; | ||
| 766 | } | ||
| 767 | |||
| 768 | sub _deserializer_enum { | ||
| 769 | my ($node, $value) = @_; | ||
| 770 | _assert_value_var($value); | ||
| 771 | |||
| 772 | my $allowed_hex = unpack('H*', _enum_bitvector($node)); | ||
| 773 | my $state_var = _var('$m'); | ||
| 774 | return <<~SERIAL; | ||
| 775 | # my (\$class, \$value, \$index, \$input) = \@_; | ||
| 776 | die "Input buffer too short" | ||
| 777 | if (\$input_length - \$_[2]) < 4; | ||
| 778 | $value = unpack("l>", substr( \$_[3], \$_[2] ) ); | ||
| 779 | die "Out of range enum value supplied: $value" | ||
| 780 | unless vec(state $state_var = pack('H*', '$allowed_hex'), | ||
| 781 | $value, 1); | ||
| 782 | \$_[2] += 4; | ||
| 783 | SERIAL | ||
| 784 | } | ||
| 785 | |||
| 786 | sub _serializer_enum { | ||
| 787 | my ($node, $value) = @_; | ||
| 788 | _assert_value_var($value); | ||
| 789 | |||
| 790 | my $allowed_hex = unpack('H*', _enum_bitvector($node)); | ||
| 791 | my $state_var = _var('$m'); | ||
| 792 | return <<~SERIAL; | ||
| 793 | # my (\$class, \$value, \$index, \$output) = \@_; | ||
| 794 | croak "Missing required input 'enum' value" | ||
| 795 | unless defined $value; | ||
| 796 | die "Out of range enum value: $value" | ||
| 797 | unless vec(state $state_var = pack('H*', '$allowed_hex'), | ||
| 798 | $value, 1); | ||
| 799 | substr( \$_[3], \$_[2] ) = pack("l>", $value); | ||
| 800 | \$_[2] += 4; | ||
| 801 | SERIAL | ||
| 802 | } | ||
| 803 | |||
| 804 | sub _deserializer_struct { | ||
| 805 | my ($node, $value, %args) = @_; | ||
| 806 | my $decl = $node; | ||
| 807 | my @fragments = ("$value = {};"); | ||
| 808 | _assert_value_var($value); | ||
| 809 | |||
| 810 | local $varnum = $varnum; | ||
| 811 | for my $member (@{ $decl->{members} }) { | ||
| 812 | my $name = $member->{name}->{content}; | ||
| 813 | push @fragments, "# Deserializing field: '$name'"; | ||
| 814 | push @fragments, _deserializer_declaration( | ||
| 815 | $member->{declaration}, | ||
| 816 | $name ? $value . "->{$name}" : undef, | ||
| 817 | %args ); | ||
| 818 | } | ||
| 819 | return join("\n", @fragments); | ||
| 820 | } | ||
| 821 | |||
| 822 | sub _serializer_struct { | ||
| 823 | my ($node, $value, %args) = @_; | ||
| 824 | my $decl = $node; | ||
| 825 | my @fragments = (<<~SERIAL); | ||
| 826 | croak "Missing required input 'struct' value" | ||
| 827 | unless defined $value; | ||
| 828 | SERIAL | ||
| 829 | _assert_value_var($value); | ||
| 830 | |||
| 831 | local $varnum = $varnum; | ||
| 832 | for my $member (@{ $decl->{members} }) { | ||
| 833 | my $name = $member->{name}->{content}; | ||
| 834 | push @fragments, "# Serializing field: '$name'"; | ||
| 835 | push @fragments, qq|croak "Missing required input value '$name'"|; | ||
| 836 | # existance check, not definedness: "pointer"-type values may be 'undef' | ||
| 837 | push @fragments, qq| unless exists ${value}->{$name};|; | ||
| 838 | push @fragments, _serializer_declaration( | ||
| 839 | $member->{declaration}, | ||
| 840 | $name ? $value . "->{$name}" : undef, | ||
| 841 | %args ); | ||
| 842 | } | ||
| 843 | return join("\n", @fragments); | ||
| 844 | } | ||
| 845 | |||
| 846 | sub _deserializer_union { | ||
| 847 | my ($node, $value, %args) = @_; | ||
| 848 | my $switch = $node; | ||
| 849 | my @fragments = ("$value = {};"); | ||
| 850 | _assert_value_var($value); | ||
| 851 | |||
| 852 | # discriminator | ||
| 853 | local $varnum = $varnum; | ||
| 854 | my $var = _var('$d'); | ||
| 855 | do { | ||
| 856 | my $name = $switch->{discriminator}->{name}->{content}; | ||
| 857 | push @fragments, ( | ||
| 858 | "my $var;", | ||
| 859 | _deserializer_declaration( $switch->{discriminator}->{declaration}, | ||
| 860 | $var, %args ), | ||
| 861 | "$value\->{$name} = $var;"); | ||
| 862 | }; | ||
| 863 | my $members = $switch->{members}; | ||
| 864 | my $cond = 'if'; | ||
| 865 | for my $case (@{ $members->{cases} }) { | ||
| 866 | my $name = $case->{name}->{content}; | ||
| 867 | my $selector = $case->{value}->{content}; # "void" has no name | ||
| 868 | my $c = _indent( _deserializer_declaration( | ||
| 869 | $case->{declaration}, | ||
| 870 | $name ? $value . "->{$name}" : undef, | ||
| 871 | %args )); | ||
| 872 | push @fragments, <<~SERIAL; | ||
| 873 | # Deserializing member '$name' (discriminator == $selector) | ||
| 874 | $cond ($var == $selector) { | ||
| 875 | $c | ||
| 876 | } | ||
| 877 | SERIAL | ||
| 878 | |||
| 879 | $cond = 'elsif'; | ||
| 880 | } | ||
| 881 | if (my $default = $members->{default}) { | ||
| 882 | my $decl = $default->{declaration}; | ||
| 883 | my $name = $default->{name}->{content} // ''; # "void" has no name | ||
| 884 | my $c = _indent( _deserializer_declaration( | ||
| 885 | $decl, | ||
| 886 | $name ? $value . "->{$name}" : undef, | ||
| 887 | %args )); | ||
| 888 | push @fragments, <<~SERIAL; | ||
| 889 | else { | ||
| 890 | # Deserializing member '$name' (default discriminator) | ||
| 891 | $c | ||
| 892 | } | ||
| 893 | SERIAL | ||
| 894 | } | ||
| 895 | else { | ||
| 896 | push @fragments, <<~SERIAL; | ||
| 897 | else { | ||
| 898 | die "Unhandled union discriminator value ($var)"; | ||
| 899 | } | ||
| 900 | SERIAL | ||
| 901 | } | ||
| 902 | |||
| 903 | return join("\n", @fragments); | ||
| 904 | } | ||
| 905 | |||
| 906 | sub _serializer_union { | ||
| 907 | my ($node, $value, %args) = @_; | ||
| 908 | my $switch = $node; | ||
| 909 | my @fragments = (<<~SERIAL); | ||
| 910 | croak "Missing required input 'union' value" | ||
| 911 | unless defined $value; | ||
| 912 | SERIAL | ||
| 913 | _assert_value_var($value); | ||
| 914 | |||
| 915 | # discriminator | ||
| 916 | local $varnum = $varnum; | ||
| 917 | my $var = _var('$d'); | ||
| 918 | do { | ||
| 919 | my $name = $switch->{discriminator}->{name}->{content}; | ||
| 920 | push @fragments, "my $var = $value\->{$name};"; | ||
| 921 | push @fragments, _serializer_declaration( $switch->{discriminator}->{declaration}, | ||
| 922 | $var, | ||
| 923 | %args ); | ||
| 924 | }; | ||
| 925 | my $members = $switch->{members}; | ||
| 926 | my $cond = 'if'; | ||
| 927 | for my $case (@{ $members->{cases} }) { | ||
| 928 | my $name = $case->{name}->{content}; # "void" has no name | ||
| 929 | my $selector = $case->{value}->{content}; | ||
| 930 | my $c = _indent( _serializer_declaration( | ||
| 931 | $case->{declaration}, | ||
| 932 | $name ? $value . "->{$name}" : undef, | ||
| 933 | %args )); | ||
| 934 | push @fragments, <<~SERIAL; | ||
| 935 | # Serializing member '$name' (discriminator == $selector) | ||
| 936 | $cond ($var == $selector) { | ||
| 937 | $c | ||
| 938 | } | ||
| 939 | SERIAL | ||
| 940 | |||
| 941 | $cond = 'elsif'; | ||
| 942 | } | ||
| 943 | if (my $default = $members->{default}) { | ||
| 944 | my $decl = $default->{declaration}; | ||
| 945 | my $name = $default->{name}->{content} // ''; # "void" has no name | ||
| 946 | my $c = _indent( _serializer_declaration( | ||
| 947 | $decl, | ||
| 948 | $name ? $value . "->{$name}" : undef, | ||
| 949 | %args )); | ||
| 950 | push @fragments, <<~SERIAL; | ||
| 951 | else { | ||
| 952 | # Serializing member '$name' (default discriminator) | ||
| 953 | $c | ||
| 954 | } | ||
| 955 | SERIAL | ||
| 956 | } | ||
| 957 | else { | ||
| 958 | push @fragments, <<~SERIAL; | ||
| 959 | else { | ||
| 960 | die "Unhandled union discriminator value ($var)"; | ||
| 961 | } | ||
| 962 | SERIAL | ||
| 963 | } | ||
| 964 | |||
| 965 | return join("\n", @fragments); | ||
| 966 | } | ||
| 967 | |||
| 968 | sub _deserializer_type { | ||
| 969 | my ($node, $value, %args) = @_; | ||
| 970 | my $decl = $node; | ||
| 971 | my $decl_type = $decl->{type}; | ||
| 972 | my $spec = $decl_type->{spec}; | ||
| 973 | |||
| 974 | if ($spec eq 'primitive') { | ||
| 975 | if (my $d = $deserializers{$decl_type->{name}} | ||
| 976 | // $deserializers{$decl_type->{name}->{content}}) { | ||
| 977 | return $d->($node, $value, %args); | ||
| 978 | } | ||
| 979 | else { | ||
| 980 | croak "Unknown primitive type: $decl_type->{name}"; | ||
| 981 | } | ||
| 982 | } | ||
| 983 | elsif ($spec eq 'named') { | ||
| 984 | return _deserializer_named( $node, $value, %args ); | ||
| 985 | } | ||
| 986 | elsif ($spec eq 'enum') { | ||
| 987 | return _deserializer_enum( $decl_type->{declaration}, $value, %args ); | ||
| 988 | } | ||
| 989 | elsif ($spec eq 'struct') { | ||
| 990 | return _deserializer_struct( $decl_type->{declaration}, $value, %args ); | ||
| 991 | } | ||
| 992 | elsif ($spec eq 'union') { | ||
| 993 | return _deserializer_union( $decl_type->{declaration}, $value, %args ); | ||
| 994 | } | ||
| 995 | else { | ||
| 996 | croak "Unknown complex type: $spec"; | ||
| 997 | } | ||
| 998 | # unreachable | ||
| 999 | } | ||
| 1000 | |||
| 1001 | sub _serializer_type { | ||
| 1002 | my ($node, $value, %args) = @_; | ||
| 1003 | my $decl = $node; | ||
| 1004 | my $decl_type = $decl->{type}; | ||
| 1005 | my $spec = $decl_type->{spec}; | ||
| 1006 | |||
| 1007 | if ($spec eq 'primitive') { | ||
| 1008 | if (my $s = $serializers{$decl_type->{name}} | ||
| 1009 | // $serializers{$decl_type->{name}->{content}}) { | ||
| 1010 | return $s->($node, $value, %args); | ||
| 1011 | } | ||
| 1012 | else { | ||
| 1013 | croak "Unknown primitive type: $decl_type->{name}"; | ||
| 1014 | } | ||
| 1015 | } | ||
| 1016 | elsif ($spec eq 'named') { | ||
| 1017 | return _serializer_named( $node, $value, %args ); | ||
| 1018 | } | ||
| 1019 | elsif ($spec eq 'enum') { | ||
| 1020 | return _serializer_enum( $decl_type->{declaration}, $value, %args ); | ||
| 1021 | } | ||
| 1022 | elsif ($spec eq 'struct') { | ||
| 1023 | return _serializer_struct( $decl_type->{declaration}, $value, %args ); | ||
| 1024 | } | ||
| 1025 | elsif ($spec eq 'union') { | ||
| 1026 | return _serializer_union( $decl_type->{declaration}, $value, %args ); | ||
| 1027 | } | ||
| 1028 | else { | ||
| 1029 | croak "Unknown complex type: $spec"; | ||
| 1030 | } | ||
| 1031 | # unreachable | ||
| 1032 | } | ||
| 1033 | |||
| 1034 | sub _deserializer_pointer { | ||
| 1035 | my ($node, $value,%args) = @_; | ||
| 1036 | my $decl = $node; | ||
| 1037 | my $c = _indent( _indent(_deserializer_type( $node, $value, %args ))); | ||
| 1038 | my $bool_var = _var('$b'); | ||
| 1039 | my $b = _indent(_deserializer_bool( $node, $bool_var )); | ||
| 1040 | |||
| 1041 | return <<~SERIAL; | ||
| 1042 | # my (\$class, \$value, \$index, \$input) = \@_; | ||
| 1043 | do { | ||
| 1044 | my $bool_var; | ||
| 1045 | $b | ||
| 1046 | if ($bool_var) { | ||
| 1047 | $c | ||
| 1048 | } | ||
| 1049 | else { | ||
| 1050 | $value = undef; | ||
| 1051 | } | ||
| 1052 | }; | ||
| 1053 | SERIAL | ||
| 1054 | } | ||
| 1055 | |||
| 1056 | sub _serializer_pointer { | ||
| 1057 | my ($node, $value, %args) = @_; | ||
| 1058 | my $decl = $node; | ||
| 1059 | my $c = _indent(_serializer_type( $node, $value, %args )); | ||
| 1060 | my $c_false = _indent(_serializer_bool( $node, 0 )); | ||
| 1061 | my $c_true = _indent(_serializer_bool( $node, 1 )); | ||
| 1062 | |||
| 1063 | return <<~SERIAL; | ||
| 1064 | # my (\$class, \$value, \$index, \$output) = \@_; | ||
| 1065 | if (defined $value) { | ||
| 1066 | $c_true | ||
| 1067 | $c | ||
| 1068 | } | ||
| 1069 | else { | ||
| 1070 | $c_false | ||
| 1071 | } | ||
| 1072 | SERIAL | ||
| 1073 | } | ||
| 1074 | |||
| 1075 | sub _deserializer_declaration { | ||
| 1076 | my ($decl, $value, %args) = @_; | ||
| 1077 | |||
| 1078 | if ($decl->{pointer}) { | ||
| 1079 | return _deserializer_pointer( $decl, $value, %args ); | ||
| 1080 | } | ||
| 1081 | elsif ($decl->{array}) { | ||
| 1082 | return _deserializer_array( $decl, $value, %args ); | ||
| 1083 | } | ||
| 1084 | |||
| 1085 | return _deserializer_type( $decl, $value, %args ); | ||
| 1086 | } | ||
| 1087 | |||
| 1088 | sub _serializer_declaration { | ||
| 1089 | my ($decl, $value, %args) = @_; | ||
| 1090 | |||
| 1091 | if ($decl->{pointer}) { | ||
| 1092 | return _serializer_pointer( $decl, $value, %args ); | ||
| 1093 | } | ||
| 1094 | elsif ($decl->{array}) { | ||
| 1095 | return _serializer_array( $decl, $value, %args ); | ||
| 1096 | } | ||
| 1097 | |||
| 1098 | return _serializer_type( $decl, $value, %args ); | ||
| 1099 | } | ||
| 1100 | |||
| 1101 | sub _deserializer_definition { | ||
| 1102 | my ($node, %args) = @_; | ||
| 1103 | |||
| 1104 | local $varnum = $varnum; | ||
| 1105 | my $name = $args{transform}->( $node->{name}->{content} ); | ||
| 1106 | my $c = _indent( _deserializer_declaration( $node->{definition}, '$_[1]', %args ) ); | ||
| 1107 | return <<~SERIAL; | ||
| 1108 | # \@_: (\$class, \$value, \$index, \$input) = \@_; | ||
| 1109 | sub deserialize_$name { | ||
| 1110 | my \$input_length = length \$_[3]; | ||
| 1111 | $c | ||
| 1112 | } | ||
| 1113 | SERIAL | ||
| 1114 | } | ||
| 1115 | |||
| 1116 | sub _serializer_definition { | ||
| 1117 | my ($node, %args) = @_; | ||
| 1118 | |||
| 1119 | local $varnum = $varnum; | ||
| 1120 | my $name = $args{transform}->( $node->{name}->{content} ); | ||
| 1121 | my $c = _indent( _serializer_declaration( $node->{definition}, '$_[1]', %args ) ); | ||
| 1122 | return <<~SERIAL; | ||
| 1123 | # \@_: (\$class, \$value, \$index, \$output) = \@_; | ||
| 1124 | sub serialize_$name { | ||
| 1125 | $c | ||
| 1126 | } | ||
| 1127 | SERIAL | ||
| 1128 | } | ||
| 1129 | |||
| 1130 | sub _define_constant { | ||
| 1131 | my ($node, %args) = @_; | ||
| 1132 | |||
| 1133 | my $name = $args{transform}->( $node->{name}->{content} ); | ||
| 1134 | my $value = $node->{value}->{content}; | ||
| 1135 | my $resolved = _resolve_to_number( $value ); | ||
| 1136 | return <<~SERIAL; | ||
| 1137 | use constant $name => $resolved; # $value | ||
| 1138 | SERIAL | ||
| 1139 | } | ||
| 1140 | |||
| 1141 | sub _define_enum { | ||
| 1142 | my ($node, $names, %args) = @_; | ||
| 1143 | my ($name, @nesting) = @{ $names }; | ||
| 1144 | my $elements = $node->{type}->{declaration}->{elements}; | ||
| 1145 | $name = $args{transform}->( $name ) . join('.', @nesting); | ||
| 1146 | |||
| 1147 | my @fragments = ( | ||
| 1148 | "# Define elements from enum '$name'", | ||
| 1149 | "use constant {", | ||
| 1150 | ); | ||
| 1151 | my $w = max map { length $args{transform}->($_->{name}->{content}) } @{ $elements }; | ||
| 1152 | for my $element (@{ $elements }) { | ||
| 1153 | _add_constant( $element ); | ||
| 1154 | push @fragments, sprintf(" %-${w}s => %s,", | ||
| 1155 | $args{transform}->($element->{name}->{content}), | ||
| 1156 | $element->{value}->{content}); | ||
| 1157 | } | ||
| 1158 | push @fragments, "};"; | ||
| 1159 | return join("\n", @fragments) . "\n"; | ||
| 1160 | } | ||
| 1161 | |||
| 1162 | sub _serializer_nested_enums { | ||
| 1163 | my ($cb, $names, $node, %args) = @_; | ||
| 1164 | my $decl = $node->{type}->{declaration}; | ||
| 1165 | |||
| 1166 | if ($node->{type}->{spec} eq 'union') { | ||
| 1167 | do { # restrict "local" scope | ||
| 1168 | my $discriminator = $decl->{discriminator}; | ||
| 1169 | |||
| 1170 | _serializer_nested_enums( $cb, | ||
| 1171 | [ @{ $names }, $discriminator->{name}->{content} ], | ||
| 1172 | $discriminator->{declaration}, %args ); | ||
| 1173 | }; | ||
| 1174 | my $members = $decl->{members}; | ||
| 1175 | for my $case (@{ $members->{cases} }) { | ||
| 1176 | _serializer_nested_enums( $cb, | ||
| 1177 | [ @{ $names }, $case->{name}->{content} ], | ||
| 1178 | $case->{declaration}, %args ); | ||
| 1179 | } | ||
| 1180 | if (my $default = $members->{default}) { | ||
| 1181 | _serializer_nested_enums( $cb, | ||
| 1182 | [ @{ $names }, $default->{name}->{content} ], | ||
| 1183 | $default->{declaration}, %args ); | ||
| 1184 | } | ||
| 1185 | |||
| 1186 | return; | ||
| 1187 | } | ||
| 1188 | elsif ($node->{type}->{spec} eq 'struct') { | ||
| 1189 | my $members = $decl->{members}; | ||
| 1190 | for my $member (@{ $members }) { | ||
| 1191 | _serializer_nested_enums( $cb, | ||
| 1192 | [ @{ $names }, $member->{name}->{content} ], | ||
| 1193 | $member->{declaration}, %args ); | ||
| 1194 | } | ||
| 1195 | |||
| 1196 | return; | ||
| 1197 | } | ||
| 1198 | elsif ($node->{type}->{spec} eq 'enum') { | ||
| 1199 | $cb->( $node, _define_enum( $node, %args{transform} ), | ||
| 1200 | names => $names, | ||
| 1201 | type => 'enum', | ||
| 1202 | %args{transform} ); | ||
| 1203 | return; | ||
| 1204 | } | ||
| 1205 | |||
| 1206 | return; | ||
| 1207 | } | ||
| 1208 | |||
| 1209 | sub _iterate_toplevel_nodes { | ||
| 1210 | my ($class, $ast, $cb, %options) = @_; | ||
| 1211 | |||
| 1212 | for my $node (@{ $ast }) { | ||
| 1213 | if ($node->{def} eq 'passthrough') { | ||
| 1214 | next unless $options{include_passthrough}; | ||
| 1215 | }; | ||
| 1216 | if ($node->{def} eq 'preprocessor') { | ||
| 1217 | next unless $options{include_preprocessor}; | ||
| 1218 | } | ||
| 1219 | |||
| 1220 | if ($node->{def} eq 'const') { | ||
| 1221 | _add_constant( $node, transform => $options{transform} ); | ||
| 1222 | next if $options{exclude_constants}; | ||
| 1223 | |||
| 1224 | $cb->( $node, _define_constant( $node, transform => $options{transform} ), | ||
| 1225 | type => 'constant', transform => $options{transform} ); | ||
| 1226 | next; | ||
| 1227 | } | ||
| 1228 | |||
| 1229 | if ($node->{def} eq 'enum') { | ||
| 1230 | next if $options{exclude_enums}; | ||
| 1231 | |||
| 1232 | $cb->( $node->{definition}, | ||
| 1233 | _define_enum( $node->{definition}, | ||
| 1234 | [ $node->{name}->{content} ], | ||
| 1235 | transform => $options{transform} ), | ||
| 1236 | names => [ $node->{name}->{content} ], | ||
| 1237 | type => 'enum', | ||
| 1238 | transform => $options{transform} ); | ||
| 1239 | # fall through to create serializers too | ||
| 1240 | } | ||
| 1241 | elsif (not $options{exclude_enums} | ||
| 1242 | and ($node->{def} eq 'struct' | ||
| 1243 | or $node->{def} eq 'union')) { | ||
| 1244 | # iterate through structs and unions to find | ||
| 1245 | # enums declared inline to push the symbols into | ||
| 1246 | # the symbol table | ||
| 1247 | |||
| 1248 | _serializer_nested_enums( $cb, | ||
| 1249 | [ $node->{name}->{content} ], | ||
| 1250 | $node->{definition}, | ||
| 1251 | transform => $options{transform} ); | ||
| 1252 | } | ||
| 1253 | |||
| 1254 | $cb->( $node, _deserializer_definition( $node, transform => $options{transform} ), | ||
| 1255 | type => 'deserializer', transform => $options{transform} ); | ||
| 1256 | $cb->( $node, _serializer_definition( $node, transform => $options{transform} ), | ||
| 1257 | type => 'serializer', transform => $options{transform} ); | ||
| 1258 | } | ||
| 1259 | |||
| 1260 | return; | ||
| 1261 | } | ||
| 1262 | |||
| 1263 | sub generate { | ||
| 1264 | my ($class, $ast, $output, %options) = @_; | ||
| 1265 | my $cb; | ||
| 1266 | |||
| 1267 | local %constants = %{ $options{external_constants} // {} }; | ||
| 1268 | $output //= IO::Handle->new_from_fd(*STDOUT, 'w'); | ||
| 1269 | if (blessed $output and $output->can('print')) { | ||
| 1270 | $cb = sub { | ||
| 1271 | $output->print( $_[1] ); | ||
| 1272 | }; | ||
| 1273 | } | ||
| 1274 | elsif (ref $output and reftype $output eq 'SCALAR') { | ||
| 1275 | $cb = sub { | ||
| 1276 | ${ $output } .= $_[1]; | ||
| 1277 | }; | ||
| 1278 | } | ||
| 1279 | elsif (ref $output and reftype $output eq 'CODE') { | ||
| 1280 | $cb = $output; | ||
| 1281 | } | ||
| 1282 | else { | ||
| 1283 | croak 'Unsupported output method'; | ||
| 1284 | } | ||
| 1285 | |||
| 1286 | $cb->( undef, <<~'SERIAL', type => 'preamble' ); | ||
| 1287 | use v5.14; | ||
| 1288 | use warnings FATAL => 'uninitialized'; | ||
| 1289 | use Config; | ||
| 1290 | use Carp qw(croak); | ||
| 1291 | SERIAL | ||
| 1292 | $class->_iterate_toplevel_nodes( $ast, $cb, | ||
| 1293 | transform => sub { $_[0] }, | ||
| 1294 | %options ); | ||
| 1295 | $cb->( undef, '', type => 'postamble' ); | ||
| 1296 | } | ||
| 1297 | |||
| 1298 | |||
| 1299 | 1; | ||
| 1300 | |||
| 1301 | =head1 NAME | ||
| 1302 | |||
| 1303 | XDR::Gen - Generator for XDR (de)serializers | ||
| 1304 | |||
| 1305 | =head1 VERSION | ||
| 1306 | |||
| 1307 | version 1.1.2 | ||
| 1308 | |||
| 1309 | =head1 SYNOPSIS | ||
| 1310 | |||
| 1311 | use XDR::Gen; | ||
| 1312 | use XDR::Parse; | ||
| 1313 | |||
| 1314 | my $parser = XDR::Parse->new; | ||
| 1315 | my $definition = <<'DEF'; | ||
| 1316 | typedef string *optional_string; | ||
| 1317 | DEF | ||
| 1318 | |||
| 1319 | open my $fh, '<', \$definition; | ||
| 1320 | my $ast = $parser->parse( $fh ); | ||
| 1321 | |||
| 1322 | # print the generated serializers on STDOUT | ||
| 1323 | XDR::Gen->serializers( $ast ); | ||
| 1324 | |||
| 1325 | # or print to a specific file handle | ||
| 1326 | open my $oh, '>', '/dev/null'; | ||
| 1327 | XDR::Gen->serializers( $ast, $oh ); | ||
| 1328 | |||
| 1329 | =head1 DESCRIPTION | ||
| 1330 | |||
| 1331 | This module contains a generator for Perl code to serialize data following | ||
| 1332 | an XDR data definition. Each type defined in the data definition results | ||
| 1333 | in a serializer and a deserializer function. For defined constants and the | ||
| 1334 | elements of enums, constants are defined. | ||
| 1335 | |||
| 1336 | The generated conversion routines include validation of input coming from | ||
| 1337 | encoded input (in case of deserialization) or from the enclosing application | ||
| 1338 | (in case of serialization). | ||
| 1339 | |||
| 1340 | =head2 Data conversion | ||
| 1341 | |||
| 1342 | Except for data passed to a 'pointer' type (defined using C<*>) or boolean | ||
| 1343 | values, all values provided must be defined. In case of boolean values, an | ||
| 1344 | undefined value will be interpreted to indicate C<false>. In case of a | ||
| 1345 | pointer type, an undefined value will be serialized as "not provided", the | ||
| 1346 | same way a C C<NULL> pointer would have. | ||
| 1347 | |||
| 1348 | An exception to regular array conversion is the conversion of C<char> and | ||
| 1349 | C<unsigned char> arrays which are converted to Perl strings (strings of bytes). | ||
| 1350 | |||
| 1351 | =head1 FUNCTIONS | ||
| 1352 | |||
| 1353 | =head2 generate | ||
| 1354 | |||
| 1355 | XDR::Gen->generate( $ast, $output, [ %options ] ) | ||
| 1356 | |||
| 1357 | Generates the code for C<$ast>, sending it to C<$output>. The C<%options> | ||
| 1358 | determine the exact outputs being generated. | ||
| 1359 | |||
| 1360 | C<$output> can be one of: | ||
| 1361 | |||
| 1362 | =over 8 | ||
| 1363 | |||
| 1364 | =item * A blessed reference supporting C<print> | ||
| 1365 | |||
| 1366 | When a blessed reference is passed on which the function C<print> | ||
| 1367 | can be called, e.g. an L<IO::Handle> instance, or a custom object | ||
| 1368 | with a C<print> method. | ||
| 1369 | |||
| 1370 | =item * A code reference | ||
| 1371 | |||
| 1372 | When a code reference is passed in C<$output>, it is called as | ||
| 1373 | |||
| 1374 | $coderef->( $node, $definition, type => $type ); | ||
| 1375 | |||
| 1376 | with C<$node> the AST node for which the definition is generated, | ||
| 1377 | C<$definition> the source code fragment and C<$type> the type of | ||
| 1378 | source code having been generated. C<$type> can have these values: | ||
| 1379 | |||
| 1380 | =over 8 | ||
| 1381 | |||
| 1382 | =item * preamble | ||
| 1383 | |||
| 1384 | Code fragment to preceed the generated code. Defaults to | ||
| 1385 | |||
| 1386 | use v5.14; | ||
| 1387 | use warnings FATAL => 'uninitialized'; | ||
| 1388 | use Config; | ||
| 1389 | use Carp qw(croak); | ||
| 1390 | |||
| 1391 | If it's the intent to use the generated code as a stand-alone module, | ||
| 1392 | at the absolute minimum, a C<package> statement needs to be prepended | ||
| 1393 | to the preamble. | ||
| 1394 | |||
| 1395 | =item * postamble | ||
| 1396 | |||
| 1397 | Code fragment to follow the generated code. Default to an empty string. | ||
| 1398 | If it's the intent to use the generated code as a stand-alone module, | ||
| 1399 | at the absolute minimum, a C<1;> statement should be appended to this | ||
| 1400 | postamble. | ||
| 1401 | |||
| 1402 | =item * constant | ||
| 1403 | |||
| 1404 | A constant generated from a C<const> line in the input. | ||
| 1405 | |||
| 1406 | =item * enum | ||
| 1407 | |||
| 1408 | A value from an C<enum> definition; note that this definition may be | ||
| 1409 | part of a nested enum specification inside a struct or union definition. | ||
| 1410 | |||
| 1411 | =item * serializer | ||
| 1412 | |||
| 1413 | Code generated to serialize a type specified in the XDR syntax. | ||
| 1414 | |||
| 1415 | =item * deserializer | ||
| 1416 | |||
| 1417 | Code generated to deserialize a type specified in the XDR syntax. | ||
| 1418 | |||
| 1419 | =back | ||
| 1420 | |||
| 1421 | Note that the C<$node> does not need to be a top-level AST node, | ||
| 1422 | especially with enum types, which can be defined 'inline' as part | ||
| 1423 | of union or struct | ||
| 1424 | |||
| 1425 | =item * A scalar reference | ||
| 1426 | |||
| 1427 | When a scalar reference is passed, the code fragments are appended | ||
| 1428 | to the contents of the referenced variable. | ||
| 1429 | |||
| 1430 | =item * C<undef> | ||
| 1431 | |||
| 1432 | When C<$output> is undefined, the output is sent to STDOUT by default. | ||
| 1433 | |||
| 1434 | =back | ||
| 1435 | |||
| 1436 | Supported values for C<options>: | ||
| 1437 | |||
| 1438 | =over 8 | ||
| 1439 | |||
| 1440 | =item * transform | ||
| 1441 | |||
| 1442 | A function to transform identifiers; e.g., | ||
| 1443 | |||
| 1444 | sub { $_[0] =~ s/^PREFIX_//ir } | ||
| 1445 | |||
| 1446 | would strip C<PREFIX_> from C<PREFIX_IDENTI_FIER>, using C<IDENTI_FIER> | ||
| 1447 | in the generated code instead. | ||
| 1448 | |||
| 1449 | =item * exclude_constants | ||
| 1450 | |||
| 1451 | Prevent emitting code for constant definitions. | ||
| 1452 | |||
| 1453 | =item * exclude_enums | ||
| 1454 | |||
| 1455 | Prevent emitting code for enum elements. | ||
| 1456 | |||
| 1457 | =item * exclude_deserializers | ||
| 1458 | |||
| 1459 | Prevent emitting code for deserializer routines. | ||
| 1460 | |||
| 1461 | =item * exclude_serializers | ||
| 1462 | |||
| 1463 | Prevent emitting code for serializer routines. | ||
| 1464 | |||
| 1465 | =item * include_passthrough | ||
| 1466 | |||
| 1467 | Emit passthrough instructions (as-is). | ||
| 1468 | |||
| 1469 | =item * include_preprocessor | ||
| 1470 | |||
| 1471 | Emit preprocessor instructions (as-is). | ||
| 1472 | |||
| 1473 | =item * external_constants | ||
| 1474 | |||
| 1475 | XDR::Gen->generate( $ast, $fh, | ||
| 1476 | external_constants => { VIR_UUID_BUFLEN => 16 } ); | ||
| 1477 | |||
| 1478 | A hashref with constants defined outside of the scope of the xdr input | ||
| 1479 | file. C<rpcgen> depends on C<cpp> to resolve preprocessor macros; XDR::Gen | ||
| 1480 | does not, but allows values extracted by other means to be provided for use | ||
| 1481 | during code generation or in the generated code. External constants are | ||
| 1482 | resolved to their numeric value at code generation time by XDR::Gen. | ||
| 1483 | |||
| 1484 | =back | ||
| 1485 | |||
| 1486 | =head1 LICENSE | ||
| 1487 | |||
| 1488 | This distribution may be used under the same terms as Perl itself. | ||
| 1489 | |||
| 1490 | =head1 AUTHOR | ||
| 1491 | |||
| 1492 | =over 8 | ||
| 1493 | |||
| 1494 | =item * Erik Huelsmann | ||
| 1495 | |||
| 1496 | =back | ||
| 1497 | |||
| 1498 | =head1 SEE ALSO | ||
| 1499 | |||
| 1500 | L<XDR::Parse>, L<perlrpcgen> | ||
| 1501 | |||
| 1502 | =cut |