/usr/share/perl5/XML_Codec.pm is in libopensrs-perl 3.0.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 | #
# XML_Codec
#
# - Encodes perl data structures into XML representation
# - Decodes XML into perl data structures
#
# Dependencies:
# XML:Parser module must be installed
#
# History
# 2000-06-05 vm First version, based on some routines in XML::Dumper
# 2000-06-16 vm Fixed xml escaping routine to ignore undefined values
# 2000-06-21 vm Removed uncessary call to xml_to_data, improving performance
package XML_Codec;
use strict;
use vars qw($_SPACER $_CRLF $can_utf8_off);
$_SPACER = " "; # indent character
$_CRLF = "\n";
use strict;
use vars qw($VAR1 $VERSION);
use XML::Parser;
if ($] >= 5.008) {
eval {
require Data::Structure::Util;
$can_utf8_off = 1;
};
if ($@){
die "You must install Data::Structure::Util for proper UTF8 to byte handling with your version of Perl $]"
}
}
$VERSION = '0.2';
use bytes;
#
# sub: new
#
# - constructor
#
sub new {
my ($class, %args) = @_;
my $option = $args{option};
my $self = {};
if ( defined($option) )
{
if ($option eq 'compress')
{
# use compression, turn off all spaces and newlines
# print "DEBUG--compression on\n";
$_SPACER = "";
$_CRLF = "";
}
}
return bless $self,$class;
}
#
# sub: data_to_xml
#
# wrapper for converting data structure to XML
# - we dont really need this, just calle data_to_xml_string directly!
#
sub data_to_xml
{
my ($obj,$ref) = @_;
return $obj->data_to_xml_string($ref);
}
#
# sub: data_to_xml_string
#
# converts a data structure to an string (xml)
#
sub data_to_xml_string {
my ($obj,$ref) = @_;
return(
$_SPACER x 2 . "<data_block>" .
&Tree2XML($ref, 3) .
$_CRLF . $_SPACER x 2 . "</data_block>"
);
}
#
# sub: Tree2XML
#
# takes a data structure reference and converts it to XML
# representation
#
sub Tree2XML {
my ($ref, $indent) = @_;
my $string = '';
# SCALAR REFERENCE
if (defined(ref($ref)) && (ref($ref) eq 'SCALAR')) {
$string .= $_CRLF . $_SPACER x $indent . "<dt_scalarref>" . "e_XML_chars($$ref) . "</dt_scalarref>";
}
# ARRAY REFERENCE
elsif (defined(ref($ref)) && (ref($ref) eq 'ARRAY')) {
$string .= $_CRLF . $_SPACER x $indent . "<dt_array>";
$indent++;
my $j = 0;
for (my $i=0; $i < @$ref; $i++) {
# don't encode objects that are DBI thingies because
# they are useless when decoded because they aren't
# "magic" anymore (i.e. no connection to DB, etc...)
if ( ref($ref->[$i]) =~ /^DBI/ )
{
next;
}
$string .= $_CRLF . $_SPACER x $indent . "<item key=\"$j\"";
if ( ! ( ref($ref->[$i]) =~ /^(ARRAY|SCALAR|HASH)$/ ||
ref($ref->[$i]) =~ /^$/ ) )
{
$string .= " class=\"". ref($ref->[$i]) ."\"";
}
$string .= ">";
if (ref($ref->[$i])) {
$string .= &Tree2XML($ref->[$i], $indent+1);
$string .= $_CRLF . $_SPACER x $indent . "</item>";
} else {
$string .= "e_XML_chars($ref->[$i]) . "</item>";
}
$j++;
}
$indent--;
$string .= $_CRLF . $_SPACER x $indent . "</dt_array>";
}
# HASH REFERENCE
# dmanley -- 2000/07/08 -- don't just check for HASH but for
# non blank too because that will account for object references
# which are normally hashes anyway.
#elsif (defined(ref($ref)) && (ref($ref) eq 'HASH')) {
elsif (defined(ref($ref)) &&
( (ref($ref) eq 'HASH') || ref($ref) ne '' )
) {
$string .= $_CRLF . $_SPACER x $indent . "<dt_assoc>";
$indent++;
foreach my $key (keys(%$ref)) {
# don't encode objects that are DBI thingies because
# they are useless when decoded because they aren't
# "magic" anymore (i.e. no connection to DB, etc...)
if ( ref($ref->{$key}) =~ /^DBI/ )
{
next;
}
$string .= $_CRLF . $_SPACER x $indent . "<item key=\"" . "e_XML_chars($key) . "\"";
if ( ! ( ref($ref->{$key}) =~ /^(ARRAY|SCALAR|HASH)$/ ||
ref($ref->{$key}) =~ /^$/ ) )
{
$string .= " class=\"". ref($ref->{$key}) ."\"";
}
$string .= ">";
if (ref($ref->{$key})) {
$string .= &Tree2XML($ref->{$key}, $indent+1);
$string .= $_CRLF . $_SPACER x $indent . "</item>";
} else {
$string .= "e_XML_chars($ref->{$key}) . "</item>";
}
}
$indent--;
$string .= $_CRLF . $_SPACER x $indent . "</dt_assoc>";
}
## SCALAR
else {
$string .= $_CRLF . $_SPACER x $indent . "<dt_scalar>" . "e_XML_chars($ref) . "</dt_scalar>";
}
return($string);
}
#
# sub: quote_XML_chars
#
# quote special XML characters
#
sub quote_XML_chars
{
my $n = shift;
if ( defined ( $n ) ) {
$n =~ s/&/&/g;
$n =~ s/</</g;
$n =~ s/>/>/g;
$n =~ s/'/'/g;
$n =~ s/"/"/g;
$n =~ s/([\x80-\xFF])/&UTF8_encode(ord($1))/ge;
$n =~ s/[\x00-\x08\x0b-\x0c\x0e-\x1f]//sg;
return($n);
}
#if ( defined ( $_[0] ) )
#{
# $_[0] =~ s/&/&/g;
# $_[0] =~ s/</</g;
# $_[0] =~ s/>/>/g;
# $_[0] =~ s/'/'/g;
# $_[0] =~ s/"/"/g;
# $_[0] =~ s/([\x80-\xFF])/&UTF8_encode(ord($1))/ge;
# $_[0] =~ s/[\x00-\x08\x0b-\x0c\x0e-\x1f]//sg;
# return($_[0]);
#}
}
# OpenSRS-grown encoder for high-end ASCII encoding.
# Allows encoding of high-end character (128-255) into "@#nnn;" where nnn is
# dec ascii representation.
sub XML_encode{
my $n = shift;
return "\@\#$n;";
}
#
# sub: UTF8_encode
#
# convert to UTF8 encoding
# NOTE: Not used at this time.
#
sub UTF8_encode
{
my $n = shift;
if ($n < 0x80) {
return chr ($n);
} elsif ($n < 0x800) {
return pack ("CC", (($n >> 6) | 0xc0), (($n & 0x3f) | 0x80));
} elsif ($n < 0x10000) {
return pack ("CCC", (($n >> 12) | 0xe0), ((($n >> 6) & 0x3f) | 0x80),
(($n & 0x3f) | 0x80));
} elsif ($n < 0x110000) {
return pack ("CCCC", (($n >> 18) | 0xf0), ((($n >> 12) & 0x3f) | 0x80),
((($n >> 6) & 0x3f) | 0x80), (($n & 0x3f) | 0x80));
}
return $n;
}
sub UTF8_decode
{
my ($str, $hex) = @_;
my $len = length ($str);
my $n;
if ($len == 2)
{
my @n = unpack "C2", $str;
$n = (($n[0] & 0x3f) << 6) + ($n[1] & 0x3f);
}
elsif ($len == 3)
{
my @n = unpack "C3", $str;
$n = (($n[0] & 0x1f) << 12) + (($n[1] & 0x3f) << 6) +
($n[2] & 0x3f);
}
elsif ($len == 4)
{
my @n = unpack "C4", $str;
$n = (($n[0] & 0x0f) << 18) + (($n[1] & 0x3f) << 12) +
(($n[2] & 0x3f) << 6) + ($n[3] & 0x3f);
}
elsif ($len == 1) # just to be complete...
{
$n = ord ($str);
}
else
{
die "bad value [$str] for UTF8_decode";
}
$hex ? sprintf ("&#x%x;", $n) : chr($n);
}
#
# sub: xml_to_data
#
# accepts and XML parse tree and converts it to a perl data structure
#
sub xml_to_data {
my ($obj,$tree) = @_;
## Skip enclosing "data_block" level
my $TopItem = $tree->[1];
my $ref = &Undump($TopItem);
return($ref);
}
#
# sub: Undump
#
# Takes a parse tree and recursively
# undumps it to create a data structure in memory.
# The top-level object is a scalar, a reference to a scalar, a hash, or an array.
# Hashes and arrays may themselves contain scalars, or references to
# scalars, or references to hashes or arrays.
# NOTE: One exception is that scalar values are never "undef" because
# there's currently no way to accurately represent undef in the dumped data.
#
sub Undump {
my ($Tree) = shift;
my $ref = undef;
my $FoundScalar;
my $i;
for ($i = 1; $i < $#$Tree; $i+=2)
{
if (lc($Tree->[$i]) eq 'dt_scalar')
{
# process scalar datatype
# Make a copy of the string
$ref = $Tree->[$i+1]->[2];
last;
}
if (lc($Tree->[$i]) eq 'dt_scalarref')
{
# process scalar reference datatype
# Make a ref to a copy of the string
$ref = \ $Tree->[$i+1]->[2];
last;
}
elsif (lc($Tree->[$i]) eq 'dt_assoc')
{
# process associative array (hash) datatype
$ref = {};
my $j;
for ($j = 1; $j < $#{$Tree->[$i+1]}; $j+=2)
{
next unless $Tree->[$i+1]->[$j] eq 'item';
my $ItemTree = $Tree->[$i+1]->[$j+1];
next unless defined(my $key = $ItemTree->[0]->{key});
$ref->{$key} = &Undump($ItemTree);
if ( exists $ItemTree->[0]->{class} )
{
bless( $ref->{$key}, $ItemTree->[0]->{class} );
}
}
last;
}
elsif (lc($Tree->[$i]) eq 'dt_array')
{
# process array datatype
$ref = [];
my $j;
for ($j = 1; $j < $#{$Tree->[$i+1]}; $j+=2)
{
next unless $Tree->[$i+1]->[$j] eq 'item';
my $ItemTree = $Tree->[$i+1]->[$j+1];
next unless defined(my $key = $ItemTree->[0]->{key});
$ref->[$key] = &Undump($ItemTree);
if ( exists $ItemTree->[0]->{class} )
{
bless( $ref->[$key], $ItemTree->[0]->{class} );
}
}
last;
}
elsif (lc($Tree->[$i]) eq '0')
{
# process scalar datatype
$FoundScalar = $Tree->[$i + 1] unless defined $FoundScalar;
}
else
{
# Unrecognized tag. Just move on.
}
}
# If $ref is not set at this point, it means we've just
# encountered a scalar value directly inside the item tag.
$ref = $FoundScalar unless defined($ref);
if ($ref) {
$ref =~ s/([\xC0-\xDF].|[\xE0-\xEF]..|[\xF0-\xFF]...)/$1 && &UTF8_decode($1)/egs if $ref;
if ($can_utf8_off){
Data::Structure::Util::_utf8_off($ref);
}
}
# print "-- $ref --";
# $ref =~ s/(\@\#(\d+))\;/chr($2)/ge;
done:
return ($ref);
}
#
# sub: decode
#
# Accepts and XML protocol string and decodes it into a perl reference
#
sub decode
{
my ($obj,$tree) = @_;
return($obj->xml_to_data($tree));
}
#
# sub: encode
#
# Accepts a perl reference and encodes it into an XML protocol string
#
sub encode
{
my ($obj,$ref) = @_;
return($obj->data_to_xml_string($ref));
}
1;
__END__
=head1 NAME
XML_Codec - Encoder/Decoder for Perl memory structures to XML
=head1 SYNOPSIS
use XML_Codec;
my $xml = new XML_Codec;
# encode
$xml_string = $xml->encode($hash_ref);
# decode
$hash_ref = $xml->decode($xml_string);
=head1 DESCRIPTION
XML_Coded is a module that encodes perl data structures into
XML representation and decodes the same XML representation into
perl data structures.
=head1 METHODS
=over 4
=item new
This is a class method, the constructor for XML_Codec. Options are passed as
keyword value pairs. Recognized options are :
=over 4
=item * option
Currently the only recognized option is "compress". This turns on XML compression by
removing irrelevant whitespace and newline characters.
=back
=item encode(HASH REF)
This method encodes a hash structure into an XML based representation. You must
pass a reference to a hash. Any arbitrary data structure is supported (e.g.
a hash or array, or a hash of array of hashes of hashes, and so on.)
The method returns a string.
=over 4
=item example:
example code
=back
=item decode(STRING)
This method decodes a string which contains an XML representation of a hash
structure (such as that returned by the L<"encode"> method) and turns
it back into a perl hash structure.
The method returns a reference to a hash.
=over 4
=item example:
example code
=back
=head1 AUTHOR
Victor Magdic, Tucows Inc. <vmagdic@tucows.com>
=back
=cut
|