File Coverage

blib/lib/Data/BISON/yEnc.pm
Criterion Covered Total %
statement 39 39 100.0
branch 8 12 66.6
condition 11 15 73.3
subroutine 6 6 100.0
pod 2 2 100.0
total 66 74 89.1


line stmt bran cond sub pod time code
1             package Data::BISON::yEnc;
2              
3 5     5   33686 use strict;
  5         11  
  5         186  
4 5     5   29 use warnings;
  5         12  
  5         163  
5 5     5   27 use Carp;
  5         9  
  5         541  
6              
7 5     5   29 use base qw(Exporter);
  5         10  
  5         5395  
8              
9             our @EXPORT_OK = qw( encode_yEnc decode_yEnc );
10              
11             sub encode_yEnc {
12 1 50   1 1 1444 croak "encode_yEnc needs one argument" unless @_ == 1;
13              
14 1         4 my $data = shift;
15 1         94 my @data = map { ord $_ } split //, $data;
  584         848  
16 1         62 my @out = ();
17 1         8 while ( defined( my $byte = shift @data ) ) {
18 584         559 my $rep = ( $byte + 42 ) & 0xFF;
19 584 100 100     3553 if ( $rep == 0x00
      100        
      100        
20             || $rep == 0x0A
21             || $rep == 0x0D
22             || $rep == 0x3D ) {
23 8         10 push @out, 0x3D;
24 8         12 $rep = ( $rep + 64 ) & 0xFF;
25             }
26 584         1195 push @out, $rep;
27             }
28              
29 1         5 return join '', map { chr $_ } @out;
  592         840  
30             }
31              
32             sub decode_yEnc {
33 1 50   1 1 5289 croak "decode_yEnc needs one argument" unless @_ == 1;
34              
35 1         28 my $data = shift;
36 1         64 my @data = map { ord $_ } split //, $data;
  592         607  
37 1         40 my @out = ();
38 1         6 while ( defined( my $byte = shift @data ) ) {
39             next
40 584 50 33     2439 if $byte == 0x00
      33        
41             || $byte == 0x0A
42             || $byte == 0x0D;
43 584 100       756 if ( $byte == 0x3D ) {
44 8         9 my $next = shift @data;
45 8 50       12 croak "Escape character at end of data"
46             unless defined $next;
47 8         9 $byte = ( $next - 64 ) & 0xFF;
48             }
49 584         1061 push @out, ( $byte - 42 ) & 0xFF;
50             }
51              
52 1         6 return join '', map { chr $_ } @out;
  584         802  
53             }
54              
55             1;
56              
57             =head1 NAME
58              
59             Data::BISON::yEnc - Implements yEnc encode, decode for Data::BISON
60              
61             =head1 VERSION
62              
63             This document describes Data::BISON::yEnc version 0.0.3
64              
65             =head1 SYNOPSIS
66              
67             use Data::BISON::yEnc qw(encode_yEnc decode_yEnc);
68              
69             my $encoded = encode_yEnc( 'Some text or maybe binary data' );
70             print decode_yEnc( $encoded );
71            
72             =head1 DESCRIPTION
73              
74             yEnc is an encoding that allows arbitrary binary data to be encoded as
75             8 bit ASCII. The encoded data will not include the characters 0x00,
76             0x09, 0x0A or 0x0D so it can be safely reformatted without losing its
77             original meaning.
78              
79             The full yEnc specification describes an envelope scheme that allows
80             multiple binary files to be encoded a la MIME multipart or uuencode.
81             This implementation only performs encoding and decoding of binary data
82             into yEnc. See L for a more complete implementation.
83              
84             For more about yEnc see L.
85              
86             =head1 INTERFACE
87              
88             =over
89              
90             =item C<< encode_yEnc >>
91              
92             Encode arbitrary binary data using the yEnc encoding scheme.
93              
94             my $safe = encode_yEnc( $binary_data );
95              
96             =item C<< decode_yEnc >>
97              
98             Decode data that has been encoded using yEnc.
99              
100             my $data = decode_yEnc( $yenc );
101              
102             =back
103              
104             =head1 DIAGNOSTICS
105              
106             =over
107              
108             =item C<< encode_yEnc needs one argument >>
109              
110             encode_yEnc takes a single argument - a string containing the binary
111             data to be encoded.
112              
113             =item C<< decode_yEnc needs one argument >>
114              
115             decode_yEnc takes a single argument - a string containing the yEnc
116             encoded data to be decoded.
117              
118             =item C<< Escape character at end of data >>
119              
120             The yEnc escape character (0x3D) was found as the last character in the
121             data to be decoded. The escape character should always be followed by
122             the character it escapes.
123              
124             =back
125              
126             =head1 CONFIGURATION AND ENVIRONMENT
127              
128             Data::BISON::yEnc requires no configuration files or environment variables.
129              
130             =head1 DEPENDENCIES
131              
132             None.
133              
134             =head1 INCOMPATIBILITIES
135              
136             None reported.
137              
138             =head1 BUGS AND LIMITATIONS
139              
140             No bugs have been reported.
141              
142             Please report any bugs or feature requests to
143             C, or through the web interface at
144             L.
145              
146             =head1 AUTHOR
147              
148             Andy Armstrong C<< >>
149              
150             =head1 LICENCE AND COPYRIGHT
151              
152             Copyright (c) 2007, Andy Armstrong C<< >>. All rights reserved.
153              
154             This module is free software; you can redistribute it and/or
155             modify it under the same terms as Perl itself. See L.
156              
157             =head1 DISCLAIMER OF WARRANTY
158              
159             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
160             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
161             OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
162             PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
163             EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
164             WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
165             ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
166             YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
167             NECESSARY SERVICING, REPAIR, OR CORRECTION.
168              
169             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
170             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
171             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
172             LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
173             OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
174             THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
175             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
176             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
177             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
178             SUCH DAMAGES.