File Coverage

blib/lib/XML/Smart/Base64.pm
Criterion Covered Total %
statement 119 125 95.2
branch 19 28 67.8
condition n/a
subroutine 11 11 100.0
pod 0 2 0.0
total 149 166 89.7


line stmt bran cond sub pod time code
1             #############################################################################
2             ## Name: Base64.pm
3             ## Purpose: XML::Smart::Base64
4             ## Author: Graciliano M. P.
5             ## Modified by:
6             ## Created: 25/5/2003
7             ## RCS-ID:
8             ## Copyright: (c) 2003 Graciliano M. P.
9             ## Licence: This program is free software; you can redistribute it and/or
10             ## modify it under the same terms as Perl itself
11            
12            
13             #############################################################################
14            
15            
16             ##
17             ## Modified by Harish to fix bugs in xml creation and to errors more readable.
18             ## Tue Nov 1 21:18:43 IST 2011
19            
20            
21             ############################################################################
22            
23            
24             package XML::Smart::Base64 ;
25            
26 4     4   26 use strict ;
  4         7  
  4         171  
27 4     4   22 use warnings ;
  4         9  
  4         162  
28            
29 4     4   22 use Carp ;
  4         6  
  4         309  
30            
31 4     4   21 use XML::Smart::Shared qw( _unset_sig_warn _reset_sig_warn ) ;
  4         7  
  4         5138  
32            
33             our $VERSION = '1.3' ;
34            
35            
36             my ($BASE64_PM) ;
37 4     4   9054 eval("use MIME::Base64 ()") ;
  4         3547  
  4         53  
38             if ( defined &MIME::Base64::encode_base64 ) { $BASE64_PM = 1 ;}
39            
40            
41            
42            
43             #################
44             # ENCODE_BASE64 #
45             #################
46            
47             sub encode_base64 {
48            
49 38     38 0 124 my $value = $_[0] ;
50            
51 38 50       141 if( $BASE64_PM ) {
52            
53 38         73 eval {
54 38         162 _unset_sig_warn() ;
55 38         750 my $encoded = MIME::Base64::encode_base64( $value ) ;
56 24         118 my $decoded = MIME::Base64::decode_base64( $encoded) ;
57 24         75 _reset_sig_warn() ;
58            
59 24         70 my $tmp_decoded = $decoded ;
60 24         73 $tmp_decoded =~ s/\n//g ;
61            
62 24         67 my $tmp_value = $value ;
63 24         68 $tmp_value =~ s/\n//g ;
64            
65 24 50       90 return $encoded if( $tmp_decoded eq $tmp_value ) ;
66             };
67            
68             }
69            
70             {
71 38         87 my $encoded ;
  38         69  
72             my $decoded ;
73 0         0 my $tmp_value ;
74 0         0 my $tmp_decoded ;
75 38         74 eval {
76 38         102 _unset_sig_warn() ;
77 38         119 $encoded = _encode_base64_pure_perl( $value ) ;
78 38         130 $decoded = _decode_base64_pure_perl( $encoded ) ;
79 38         182 _reset_sig_warn() ;
80            
81 38         137 $tmp_decoded = $decoded ;
82 38         2136 $tmp_decoded =~ s/\n//g ;
83            
84 38         154 $tmp_value = $value ;
85 38         3113 $tmp_value =~ s/\n//g ;
86 38 50       116 } ; unless( $@ ) {
87 38 100       211 return $encoded if( $tmp_decoded eq $tmp_value ) ;
88             }
89             }
90            
91             {
92 14         20 _unset_sig_warn() ;
  14         57  
93 14         47 my $encoded = _encode_ord_special( $value ) ;
94 14         67 my $decoded = _decode_ord_special( $encoded ) ;
95 14         100 _reset_sig_warn() ;
96            
97 14         4824 my $tmp_decoded = $decoded ;
98 14         13894 $tmp_decoded =~ s/\n//g ;
99            
100 14         129 my $tmp_value = $value ;
101 14         3275 $tmp_value =~ s/\n//g ;
102            
103 14 50       382 return $encoded if( $tmp_decoded eq $tmp_value ) ;
104             }
105            
106            
107            
108 0         0 croak( "Error Encoding\n" ) ;
109            
110             }
111            
112             ############################
113             # _ENCODE_BASE64_PURE_PERL #
114             ############################
115            
116             sub _encode_base64_pure_perl {
117 64     64   116 my $res = "";
118 64         103 my $eol = $_[1];
119 64 50       194 $eol = "\n" unless defined $eol;
120 64         1586 pos($_[0]) = 0; # ensure start at the beginning
121 64         386 while ($_[0] =~ /(.{1,45})/gs) {
122 15685         21405 my $text = $1 ;
123 15685         58578 $res .= substr( pack('u', $text ), 1 ) ;
124 15685         46959 chop($res);
125             }
126 64         1600 $res =~ tr|` -_|AA-Za-z0-9+/|; # `# help emacs
127             # fix padding at the end
128 64         237 my $padding = (3 - length($_[0]) % 3) % 3;
129 64 100       1563 $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
  48         191  
130             # break encoded string into lines of no more than 76 characters each
131 64 50       169 if (length $eol) {
132 64         13932 $res =~ s/(.{1,76})/$1$eol/g;
133             }
134 64         1052 $res;
135             }
136            
137            
138            
139             ############################
140             # _ENCODE_ORD_SPECIAL #
141             ############################
142            
143            
144             sub _encode_ord_special {
145            
146 28     28   92 my $value = shift ;
147            
148 28         148389 my @chars = split( //, $value ) ;
149 28         26649 my @ords ;
150 28         90 foreach my $char ( @chars ) {
151 554698         629188 push @ords, ord( $char ) ;
152             }
153            
154 28         244209 return join( "|", @ords ) ;
155            
156             }
157            
158            
159             ############################
160             # _DECODE_ORD_SPECIAL #
161             ############################
162            
163            
164             sub _decode_ord_special {
165            
166 28     28   63 my $value = shift ;
167 28         151688 my @ords = split( /\|/, $value ) ;
168 28         17845 my @chars ;
169 28         93 foreach my $ord ( @ords ) {
170 554698         1078703 push @chars, chr( $ord ) ;
171             }
172            
173 28         97049 return join( "", @chars ) ;
174            
175             }
176            
177             #################
178             # DECODE_BASE64 #
179             #################
180            
181             sub decode_base64 {
182            
183 26     26 0 61 my $value = $_[0] ;
184            
185 26 50       117 if( $BASE64_PM ) {
186            
187 26         47 eval {
188 26         86 _unset_sig_warn() ;
189 26         3835 my $decoded = MIME::Base64::decode_base64( $value ) ;
190 26         1691 my $encoded = MIME::Base64::encode_base64( $decoded ) ;
191 26         93 _reset_sig_warn() ;
192            
193 26         83 my $tmp_value = $value ;
194 26         3015 $tmp_value =~ s/\n//g ;
195            
196 26         48 my $tmp_encoded = $encoded ;
197 26         2930 $tmp_encoded =~ s/\n//g ;
198            
199 26 100       114 return $decoded if( $tmp_encoded eq $tmp_value ) ;
200             };
201            
202             }
203            
204             {
205            
206 26         42 my $decoded ;
  26         43  
207             my $encoded ;
208 0         0 my $tmp_value ;
209 0         0 my $tmp_encoded ;
210 26         38 eval {
211 26         70 $decoded = _decode_base64_pure_perl( $value ) ;
212 26         111 $encoded = _encode_base64_pure_perl( $decoded ) ;
213            
214 26         63 $tmp_value = $value ;
215 26         2930 $tmp_value =~ s/\n//g ;
216            
217 26         54 $tmp_encoded = $encoded ;
218 26         2762 $tmp_encoded =~ s/\n//g ;
219 26 50       76 } ; unless( $@ ) {
220 26 100       166 return $decoded if( $tmp_encoded eq $tmp_value ) ;
221             }
222            
223             }
224            
225             {
226            
227 14         20 my $decoded = _decode_ord_special( $value ) ;
  14         51  
228 14         72 my $encoded = _encode_ord_special( $decoded ) ;
229            
230 14         74 my $tmp_value = $value ;
231 14         3494 $tmp_value =~ s/\n//g ;
232            
233 14         54 my $tmp_encoded = $encoded ;
234 14         1203 $tmp_encoded =~ s/\n//g ;
235            
236 14 50       1384 return $decoded if( $tmp_encoded eq $tmp_value ) ;
237            
238             }
239            
240 0         0 croak "Error Decoding $value\n" ;
241            
242             }
243            
244            
245             ############################
246             # _DECODE_BASE64_PURE_PERL #
247             ############################
248            
249             sub _decode_base64_pure_perl {
250 64     64   317 local($^W) = 0 ;
251 64         217 my $str = shift ;
252 64         111 my $res = "";
253            
254 64         12336 $str =~ tr|A-Za-z0-9+=/||cd; # remove non-base64 chars
255 64 100       1416 if (length($str) % 4) {
256             #require Carp;
257             #Carp::carp("Length of base64 data not a multiple of 4")
258             }
259 64         3225 $str =~ s/=+$//; # remove padding
260 64         5117 $str =~ tr|A-Za-z0-9+/| -_|; # convert to uuencoded format
261 64         494 while ($str =~ /(.{1,60})/gs) {
262 15680         27883 my $len = chr(32 + length($1)*3/4); # compute length byte
263 15680         72239 $res .= unpack("u", $len . $1 ); # uudecode
264             }
265 64         992 $res;
266             }
267            
268             #######
269             # END #
270             #######
271            
272             1;
273