File Coverage

blib/lib/WDDX/Binary.pm
Criterion Covered Total %
statement 12 59 20.3
branch 0 12 0.0
condition n/a
subroutine 4 20 20.0
pod n/a
total 16 91 17.5


line stmt bran cond sub pod time code
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
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     1   1902 use strict;
  1         2  
  1         25  
16 1     1   5 use Carp;
  1         2  
  1         47  
17 1     1   707 use MIME::Base64;
  1         718  
  1         598  
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             sub new {
35 0     0     my( $class, $value ) = @_;
36            
37 0 0         croak "You must supply a value when creating a new $class object\n"
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             sub type {
50 0     0     return "binary";
51             }
52              
53              
54             sub as_packet {
55 0     0     my( $self ) = @_;
56 0           my $output = $WDDX::PACKET_HEADER .
57             $self->_serialize .
58             $WDDX::PACKET_FOOTER;
59             }
60              
61              
62             sub as_scalar {
63 0     0     my( $self ) = @_;
64 0           return $self->_deserialize;
65             }
66              
67              
68             sub as_javascript {
69 0     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             sub is_parser {
80 0     0     return 0;
81             }
82              
83              
84             sub _serialize {
85 0     0     my( $self ) = @_;
86 0           my $length = length $self->{value};
87 0           my $val = $self->_encode;
88 0           my $output = "$val";
89            
90 0           return $output;
91             }
92              
93              
94             sub _deserialize {
95 0     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             sub _decode {
102 0     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             sub _encode {
108 0     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     1   7 use MIME::Base64;
  1         2  
  1         357  
120              
121              
122             sub new {
123 0     0     return bless { value => "" }, shift;
124             }
125              
126              
127             sub start_tag {
128 0     0     my( $self, $element, $attribs ) = @_;
129            
130 0 0         if ( $element eq "binary" ) {
131 0 0         $self->{'length'} =
132             defined( $attribs->{'length'} ) ? $attribs->{'length'} : undef;
133             }
134             else {
135 0           die "<$element> not allowed within element\n";
136             }
137            
138 0           return $self;
139             }
140              
141              
142             sub end_tag {
143 0     0     my( $self, $element ) = @_;
144            
145 0 0         if ( $element eq "binary" ) {
146 0           $self = new WDDX::Binary( $self->_decode );
147             }
148             else {
149 0           die " not allowed within element\n";
150             }
151 0           return $self;
152             }
153              
154              
155             sub append_data {
156 0     0     my( $self, $data ) = @_;
157 0           $self->{value} .= $data;
158             }
159              
160              
161             sub is_parser {
162 0     0     return 1;
163             }
164              
165              
166             # This is a separate sub to facilitate adding other encodings in the future
167             sub _decode {
168 0     0     my( $self ) = @_;
169            
170 0           my $decoded = decode_base64( $self->{value} );
171            
172 0 0         if ( defined $self->{'length'} ) {
173 0           my $declared = $self->{'length'};
174 0           my $read = length $decoded;
175 0 0         if ( $declared != $read ) {
176 0           die "Declared length of element ($declared) does not " .
177             "match length read ($read)\n";
178             }
179             }
180            
181 0           return $decoded;
182             }