blib/lib/HTTP/Promise/Stream/UU.pm untested

filter: Statementstmt 0.0% 0 / 106 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 19 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 183 CC 17 SCAR 39.1
CC
17
Cov
0%
decode
132.0
encode
30.0
decode_uu
6.0
CRAP 50.1
1 ##----------------------------------------------------------------------------
2 ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Stream/UU.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2022/04/29
7 ## Modified 2023/09/08
8 ## All rights reserved.
9 ##
10 ##
11 ## This program is free software; you can redistribute it and/or modify it
12 ## under the same terms as Perl itself.
13 ##----------------------------------------------------------------------------
14 package HTTP::Promise::Stream::UU;
15 BEGIN
16 {
17 use strict;
18 use warnings;
19 warnings::register_categories( 'HTTP::Promise' );
20 use HTTP::Promise::Stream;
21 use parent -norequire, qw( HTTP::Promise::Stream::Generic );
22 use vars qw( @EXPORT_OK $VERSION $EXCEPTION_CLASS $UUError );
23 use constant {
24 DECODE_BUFFER_SIZE => 1024,
25 ENCODE_BUFFER_SIZE => 45,
26 };
27 our @EXPORT_OK = qw( decode_uu encode_uu );
28 our $EXCEPTION_CLASS = 'HTTP::Promise::Exception';
29 our $VERSION = 'v0.2.0';
30 };
31  
32 use strict;
33 use warnings;
34  
35 sub decode
36 {
37 my $self = shift( @_ );
38 my $from = shift( @_ );
39 my $to = shift( @_ );
40 my $opts = $self->_get_args_as_hash( @_ );
41 my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from );
42 my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 );
43 return( $self->pass_error ) if( !defined( $from_fh ) || !defined( $to_fh ) );
44 my( $n, $buff );
45 $buff = '';
46
47 my $getline = sub
48 {
49 if( length( $buff ) > 0 && $buff =~ s/^((?:[^\015\012]+)\015?\012)// )
50 {
51 return( $1 );
52 }
53 else
54 {
55 while( $n = $reader->( $buff, DECODE_BUFFER_SIZE, length( $buff ) ) )
56 {
57 if( $buff =~ s/^((?:[^\015\012]+)\015?\012)// )
58 {
59 return( $1 );
60 }
61 else
62 {
63 next;
64 }
65 }
66 return;
67 }
68 };
69
70 my( $mode, $fname );
71 my $done;
72 local $_;
73 # Credit: brian d foy <https://metacpan.org/dist/PerlPowerTools/view/bin/uudecode>
74 READ: while( defined( $_ = $getline->() ) )
75 {
76 next unless( ( $mode, $fname ) = /^begin[[:blank:]\h]+(\d+)[[:blank:]\h]+(\S+)/ );
77 $self->filename( $fname );
78 $self->mode( $mode );
79 $opts->{filename} = $fname;
80 $opts->{mode} = $mode;
81 $done = 0;
82 LINE: while( defined( $_ = $getline->() ) )
83 {
84 if( /^end$/ )
85 {
86 $done = 1;
87 last READ;
88 }
89 next LINE if( /[a-z]/ );
90 next LINE unless int( ( ( ( ord( $_ ) - 32 ) & 077 ) + 2 ) / 3 ) == int( length( $_ ) / 4 );
91 my $rv = $writer->( unpack( 'u', $_ ) );
92 return( $self->pass_error ) if( !defined( $rv ) );
93 }
94 }
95 return( $self->error( "No UU encoded data found." ) ) if( !defined( $done ) );
96 return( $self->error( "Missing end. input data stream may be truncated." ) ) if( !$done );
97 return( $self );
98 }
99  
100 sub decode_uu
101 {
102 my $s = __PACKAGE__->new;
103 my $rv = $s->decode( @_ );
104 if( !defined( $rv ) )
105 {
106 $UUError = $s->error;
107 return( $s->pass_error );
108 }
109 else
110 {
111 undef( $UUError );
112 return( $rv );
113 }
114 }
115  
116 sub encode
117 {
118 my $self = shift( @_ );
119 my $from = shift( @_ );
120 my $to = shift( @_ );
121 my $opts = $self->_get_args_as_hash( @_ );
122 my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from );
123 my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 );
124 return( $self->pass_error ) if( !defined( $from_fh ) || !defined( $to_fh ) );
125 my $fname = ( exists( $opts->{filename} ) && length( $opts->{filename} ) ) ? $opts->{filename} : 'unknown.bin';
126 my $mode = ( exists( $opts->{mode} ) && length( $opts->{mode} ) ) ? $opts->{mode} : 0644;
127 my( $n, $buff );
128
129 my $rv = $writer->( sprintf( "begin %03o $fname\n", $mode ) );
130 return( $self->pass_error ) if( !defined( $rv ) );
131 while( $n = $reader->( $buff, ENCODE_BUFFER_SIZE ) )
132 {
133 $writer->( pack( 'u', $buff ) );
134 }
135 defined( $writer->( "`\n" ) ) or return( $self->pass_error );
136 defined( $writer->( "end\n" ) ) or return( $self->pass_error );
137 return( $self );
138 }
139  
140 sub encode_uu
141 {
142 my $s = __PACKAGE__->new;
143 my $rv = $s->encode( @_ );
144 if( !defined( $rv ) )
145 {
146 $UUError = $s->error;
147 return( $s->pass_error );
148 }
149 else
150 {
151 undef( $UUError );
152 return( $rv );
153 }
154 }
155  
156 sub filename { return( shift->_set_get_scalar( 'filename', @_ ) ); }
157  
158 sub is_decoder_installed { return(1); }
159  
160 sub is_encoder_installed { return(1); }
161  
162 sub mode { return( shift->_set_get_number( 'mode', @_ ) ); }
163  
164 # NOTE: sub FREEZE is inherited
165  
166 # NOTE: sub STORABLE_freeze is inherited
167  
168 # NOTE: sub STORABLE_thaw is inherited
169  
170 # NOTE: sub THAW is inherited
171  
172 1;
173 # NOTE: POD
174 __END__
175  
176 =encoding utf-8
177  
178 =head1 NAME
179  
180 HTTP::Promise::Stream::UU - Stream Encoder for UU Encoding
181  
182 =head1 SYNOPSIS
183  
184 use HTTP::Promise::Stream::UU;
185 my $s = HTTP::Promise::Stream::UU->new ||
186 die( HTTP::Promise::Stream::UU->error, "\n" );
187 $s->encode( $input => $output, eol => "\n" ) ||
188 die( $s->error );
189 $s->decode( $input => $output ) || die( $s->error );
190 HTTP::Promise::Stream::UU::encode_uu( $input => $output, eol => "\n" ) ||
191 die( $HTTP::Promise::Stream::UU::UUError );
192 HTTP::Promise::Stream::UU::decode_uu( $input => $output, eol => "\n" ) ||
193 die( $HTTP::Promise::Stream::UU::UUError );
194  
195 =head1 VERSION
196  
197 v0.2.0
198  
199 =head1 DESCRIPTION
200  
201 This implements an encoding and decoding mechanism for UU encoding using either of the following on input and output:
202  
203 =over 4
204  
205 =item C<filepath>
206  
207 If the parameter is neither a scalar reference nor a file handle, it will be assumed to be a file path.
208  
209 =item C<file handle>
210  
211 This can be a native file handle, or an object oriented one as long as it implements the C<print> or C<write>, and C<read> methods. The C<read> method is expected to return the number of bytes read or C<undef> upon error. The C<print> and C<write> methods are expected to simply return true upon success and C<undef> upon error.
212  
213 Alternatively, those methods can die and those exceptions wil be caught.
214  
215 =item C<scalar reference>
216  
217 This can be a simple scalar reference, or an object scalar reference.
218  
219 =back
220  
221 =head1 CONSTRUCTOR
222  
223 =head2 new
224  
225 Creates a new L<HTTP::Promise::Stream::UU> object and returns it.
226  
227 =head1 METHODS
228  
229 =head2 decode
230  
231 This takes 2 arguments: an input and an output. Each one can be either a file path, a file handle, or a scalar reference.
232  
233 It will decode the UU encoded data and write the result into the output.
234  
235 It returns true upon success and sets an L<error|Module::Generic/error> and return C<undef> upon error.
236  
237 =head2 encode
238  
239 This takes 2 arguments: an input and an output. Each one can be either a file path, a file handle, or a scalar reference.
240  
241 It will encode the data into UU encoded data and write the result into the output.
242  
243 Possible options are:
244  
245 =over 4
246  
247 =item I<filename>
248  
249 The file name (not the file path) to be used for UU encoding.
250  
251 =item I<mode>
252  
253 The file octal permisions, like C<0644>. It defaults to C<0644> if nothing is provided.
254  
255 =back
256  
257 It returns true upon success and sets an L<error|Module::Generic/error> and return C<undef> upon error.
258  
259 =head1 CLASS FUNCTIONS
260  
261 The following class functions are available and can also be exported, such as:
262  
263 use HTTP::Promise::Stream::Brotli qw( decode_uu encode_uu );
264  
265 =head2 decode_uu
266  
267 This takes the same 2 arguments used in L</decode>: an input and an output. Each one can be either a file path, a file handle, or a scalar reference.
268  
269 It will decode the UU encoded data and write the result into the output.
270  
271 It returns true upon success, and upon error, it will set the error in the global variable C<$UUError> and return C<undef>
272  
273 my $decoded = HTTP::Promise::Stream::UU::decode_uu( $encoded );
274 die( "Something went wrong: $HTTP::Promise::Stream::UU::UUError\n" if( !defined( $decoded ) );
275 print( "Decoded data is: $decoded\n" );
276  
277 =head2 encode_uu
278  
279 This takes the same 2 arguments used in L</encode>: an input and an output. Each one can be either a file path, a file handle, or a scalar reference.
280  
281 It will encode the data into UU encoded data and write the result into the output.
282  
283 It returns true upon success, and upon error, it will set the error in the global variable C<$UUError> and return C<undef>
284  
285 my $encoded = HTTP::Promise::Stream::UU::encode_uu( $data );
286 die( "Something went wrong: $HTTP::Promise::Stream::UU::UUError\n" if( !defined( $encoded ) );
287 print( "Encoded data is: $encoded\n" );
288  
289 =head2 is_decoder_installed
290  
291 Always returns true, because decoding is done internally.
292  
293 =head2 is_encoder_installed
294  
295 Always returns true, because encoding is done internally.
296  
297 =head1 THREAD-SAFETY
298  
299 This module is thread-safe for all operations, as it operates on per-object state and uses thread-safe external libraries.
300  
301 =head1 AUTHOR
302  
303 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
304  
305 =head1 SEE ALSO
306  
307 L<Wikipedia page|https://en.wikipedia.org/wiki/Uuencoding>
308  
309 L<HTTP::Promise>, L<HTTP::Promise::Request>, L<HTTP::Promise::Response>, L<HTTP::Promise::Message>, L<HTTP::Promise::Entity>, L<HTTP::Promise::Headers>, L<HTTP::Promise::Body>, L<HTTP::Promise::Body::Form>, L<HTTP::Promise::Body::Form::Data>, L<HTTP::Promise::Body::Form::Field>, L<HTTP::Promise::Status>, L<HTTP::Promise::MIME>, L<HTTP::Promise::Parser>, L<HTTP::Promise::IO>, L<HTTP::Promise::Stream>, L<HTTP::Promise::Exception>
310  
311 =head1 COPYRIGHT & LICENSE
312  
313 Copyright(c) 2022 DEGUEST Pte. Ltd.
314  
315 All rights reserved.
316  
317 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
318  
319 =cut