| 1 | #!/usr/bin/perl -w | ||||||||||
| 2 | # | ||||||||||
| 3 | # $Id: Binary.pm,v 1.1.1.1 2003/10/28 16:04:37 andy Exp $ | ||||||||||
| 4 | # | ||||||||||
| 5 | # This code is copyright 1999-2000 by Scott Guelich <scott@scripted.com> | ||||||||||
| 6 | # and is distributed according to the same conditions as Perl itself | ||||||||||
| 7 | # Please visit http://www.scripted.com/wddx/ for more information | ||||||||||
| 8 | # | ||||||||||
| 9 | |||||||||||
| 10 | package WDDX::Binary; | ||||||||||
| 11 | |||||||||||
| 12 | # Auto-inserted by build scripts | ||||||||||
| 13 | $VERSION = "1.01"; | ||||||||||
| 14 | |||||||||||
| 15 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 1 | ▶ | use MIME::Base64; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | require WDDX; | ||||||||||
| 20 | |||||||||||
| 21 | { my $i_hate_the_w_flag_sometimes = [ | ||||||||||
| 22 | $WDDX::PACKET_HEADER, | ||||||||||
| 23 | $WDDX::PACKET_FOOTER, | ||||||||||
| 24 | $WDDX::Binary::VERSION | ||||||||||
| 25 | ] } | ||||||||||
| 26 | |||||||||||
| 27 | 1; | ||||||||||
| 28 | |||||||||||
| 29 | |||||||||||
| 30 | #/----------------------------------------------------------------------- | ||||||||||
| 31 | # Public Methods | ||||||||||
| 32 | # | ||||||||||
| 33 | |||||||||||
| 34 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
| |||||||||||
| 35 | 0 | my( $class, $value ) = @_; | |||||||||
| 36 | |||||||||||
| 37 | 0 | ▶ | croak "You must supply a value when creating a new $class object\n" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | unless defined $value; | ||||||||||
| 39 | |||||||||||
| 40 | 0 | my $self = { | |||||||||
| 41 | value => $value, | ||||||||||
| 42 | }; | ||||||||||
| 43 | |||||||||||
| 44 | 0 | bless $self, $class; | |||||||||
| 45 | 0 | return $self; | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | |||||||||||
| 49 | 0 | ▶ | sub type { | ||||||||
|
Subroutine
type: not called CC=1 SCAR=6.9
| |||||||||||
| 50 | 0 | return "binary"; | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | |||||||||||
| 54 | 0 | ▶ | sub as_packet { | ||||||||
|
Subroutine
as_packet: not called CC=1 SCAR=6.9
| |||||||||||
| 55 | 0 | my( $self ) = @_; | |||||||||
| 56 | 0 | my $output = $WDDX::PACKET_HEADER . | |||||||||
| 57 | $self->_serialize . | ||||||||||
| 58 | $WDDX::PACKET_FOOTER; | ||||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | |||||||||||
| 62 | 0 | ▶ | sub as_scalar { | ||||||||
|
Subroutine
as_scalar: not called CC=1 SCAR=6.9
| |||||||||||
| 63 | 0 | my( $self ) = @_; | |||||||||
| 64 | 0 | return $self->_deserialize; | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | |||||||||||
| 68 | 0 | ▶ | sub as_javascript { | ||||||||
|
Subroutine
as_javascript: not called CC=1 SCAR=6.9
| |||||||||||
| 69 | 0 | my( $self, $js_var ) = @_; | |||||||||
| 70 | 0 | my $val = $self->_encode; | |||||||||
| 71 | 0 | return "$js_var=new WddxBinary( \"$val\" );"; | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | |||||||||||
| 75 | #/----------------------------------------------------------------------- | ||||||||||
| 76 | # Private Methods | ||||||||||
| 77 | # | ||||||||||
| 78 | |||||||||||
| 79 | 0 | ▶ | sub is_parser { | ||||||||
|
Subroutine
is_parser: not called CC=1 SCAR=6.9
| |||||||||||
| 80 | 0 | return 0; | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | |||||||||||
| 84 | 0 | ▶ | sub _serialize { | ||||||||
|
Subroutine
_serialize: not called CC=1 SCAR=6.9
| |||||||||||
| 85 | 0 | my( $self ) = @_; | |||||||||
| 86 | 0 | my $length = length $self->{value}; | |||||||||
| 87 | 0 | my $val = $self->_encode; | |||||||||
| 88 | 0 | my $output = "<binary length='$length'>$val</binary>"; | |||||||||
| 89 | |||||||||||
| 90 | 0 | return $output; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | |||||||||||
| 94 | 0 | ▶ | sub _deserialize { | ||||||||
|
Subroutine
_deserialize: not called CC=1 SCAR=6.9
| |||||||||||
| 95 | 0 | my( $self ) = @_; | |||||||||
| 96 | 0 | return $self->{value}; | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | |||||||||||
| 100 | # This is a separate sub to facilitate adding other encodings in the future | ||||||||||
| 101 | 0 | ▶ | sub _decode { | ||||||||
|
Subroutine
_decode: not called CC=1 SCAR=6.9
| |||||||||||
| 102 | 0 | my( $self ) = @_; | |||||||||
| 103 | 0 | return decode_base64( $self->{value} ); | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | # This is a separate sub to facilitate adding other encodings in the future | ||||||||||
| 107 | 0 | ▶ | sub _encode { | ||||||||
|
Subroutine
_encode: not called CC=1 SCAR=6.9
| |||||||||||
| 108 | 0 | my( $self ) = @_; | |||||||||
| 109 | 0 | return encode_base64( $self->{value} ); | |||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | |||||||||||
| 113 | #/----------------------------------------------------------------------- | ||||||||||
| 114 | # Parsing Code | ||||||||||
| 115 | # | ||||||||||
| 116 | |||||||||||
| 117 | package WDDX::Binary::Parser; | ||||||||||
| 118 | |||||||||||
| 119 | 1 | ▶ | use MIME::Base64; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 120 | |||||||||||
| 121 | |||||||||||
| 122 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
| |||||||||||
| 123 | 0 | return bless { value => "" }, shift; | |||||||||
| 124 | } | ||||||||||
| 125 | |||||||||||
| 126 | |||||||||||
| 127 | 0 | ▶ | sub start_tag { | ||||||||
|
Subroutine
start_tag: not called CC=3 SCAR=24.8
| |||||||||||
| 128 | 0 | my( $self, $element, $attribs ) = @_; | |||||||||
| 129 | |||||||||||
| 130 | 0 | ▶ | if ( $element eq "binary" ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 131 | $self->{'length'} = | ||||||||||
| 132 | 0 | ▶ | defined( $attribs->{'length'} ) ? $attribs->{'length'} : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 133 | } | ||||||||||
| 134 | else { | ||||||||||
| 135 | 0 | die "<$element> not allowed within <binary> element\n"; | |||||||||
| 136 | } | ||||||||||
| 137 | |||||||||||
| 138 | 0 | return $self; | |||||||||
| 139 | } | ||||||||||
| 140 | |||||||||||
| 141 | |||||||||||
| 142 | 0 | ▶ | sub end_tag { | ||||||||
|
Subroutine
end_tag: not called CC=2 SCAR=17.9
| |||||||||||
| 143 | 0 | my( $self, $element ) = @_; | |||||||||
| 144 | |||||||||||
| 145 | 0 | ▶ | if ( $element eq "binary" ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 146 | 0 | $self = new WDDX::Binary( $self->_decode ); | |||||||||
| 147 | } | ||||||||||
| 148 | else { | ||||||||||
| 149 | 0 | die "</$element> not allowed within <binary> element\n"; | |||||||||
| 150 | } | ||||||||||
| 151 | 0 | return $self; | |||||||||
| 152 | } | ||||||||||
| 153 | |||||||||||
| 154 | |||||||||||
| 155 | 0 | ▶ | sub append_data { | ||||||||
|
Subroutine
append_data: not called CC=1 SCAR=6.9
| |||||||||||
| 156 | 0 | my( $self, $data ) = @_; | |||||||||
| 157 | 0 | $self->{value} .= $data; | |||||||||
| 158 | } | ||||||||||
| 159 | |||||||||||
| 160 | |||||||||||
| 161 | 0 | ▶ | sub is_parser { | ||||||||
|
Subroutine
is_parser: not called CC=1 SCAR=6.9
| |||||||||||
| 162 | 0 | return 1; | |||||||||
| 163 | } | ||||||||||
| 164 | |||||||||||
| 165 | |||||||||||
| 166 | # This is a separate sub to facilitate adding other encodings in the future | ||||||||||
| 167 | 0 | ▶ | sub _decode { | ||||||||
|
Subroutine
_decode: not called CC=3 SCAR=24.8
| |||||||||||
| 168 | 0 | my( $self ) = @_; | |||||||||
| 169 | |||||||||||
| 170 | 0 | my $decoded = decode_base64( $self->{value} ); | |||||||||
| 171 | |||||||||||
| 172 | 0 | ▶ | if ( defined $self->{'length'} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 173 | 0 | my $declared = $self->{'length'}; | |||||||||
| 174 | 0 | my $read = length $decoded; | |||||||||
| 175 | 0 | ▶ | if ( $declared != $read ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 176 | 0 | die "Declared length of <binary> element ($declared) does not " . | |||||||||
| 177 | "match length read ($read)\n"; | ||||||||||
| 178 | } | ||||||||||
| 179 | } | ||||||||||
| 180 | |||||||||||
| 181 | 0 | return $decoded; | |||||||||
| 182 | } | ||||||||||