1
2package Compress::Raw::Bzip2;
3
4use strict ;
5use warnings ;
6
7require 5.006 ;
8require Exporter;
9use Carp ;
10
11use bytes ;
12our ($VERSION, $XS_VERSION, @ISA, @EXPORT, $AUTOLOAD);
13
14$VERSION = '2.093';
15$XS_VERSION = $VERSION;
16$VERSION = eval $VERSION;
17
18@ISA = qw(Exporter);
19# Items to export into callers namespace by default. Note: do not export
20# names by default without a very good reason. Use EXPORT_OK instead.
21# Do not simply export all your public functions/methods/constants.
22@EXPORT = qw(
23		BZ_RUN
24		BZ_FLUSH
25		BZ_FINISH
26
27		BZ_OK
28		BZ_RUN_OK
29		BZ_FLUSH_OK
30		BZ_FINISH_OK
31		BZ_STREAM_END
32		BZ_SEQUENCE_ERROR
33		BZ_PARAM_ERROR
34		BZ_MEM_ERROR
35		BZ_DATA_ERROR
36		BZ_DATA_ERROR_MAGIC
37		BZ_IO_ERROR
38		BZ_UNEXPECTED_EOF
39		BZ_OUTBUFF_FULL
40		BZ_CONFIG_ERROR
41
42    );
43
44sub AUTOLOAD {
45    my($constname);
46    ($constname = $AUTOLOAD) =~ s/.*:://;
47    my ($error, $val) = constant($constname);
48    Carp::croak $error if $error;
49    no strict 'refs';
50    *{$AUTOLOAD} = sub { $val };
51    goto &{$AUTOLOAD};
52
53}
54
55use constant FLAG_APPEND             => 1 ;
56use constant FLAG_CRC                => 2 ;
57use constant FLAG_ADLER              => 4 ;
58use constant FLAG_CONSUME_INPUT      => 8 ;
59
60eval {
61    require XSLoader;
62    XSLoader::load('Compress::Raw::Bzip2', $XS_VERSION);
63    1;
64}
65or do {
66    require DynaLoader;
67    local @ISA = qw(DynaLoader);
68    bootstrap Compress::Raw::Bzip2 $XS_VERSION ;
69};
70
71#sub Compress::Raw::Bzip2::new
72#{
73#    my $class = shift ;
74#    my ($ptr, $status) = _new(@_);
75#    return wantarray ? (undef, $status) : undef
76#        unless $ptr ;
77#    my $obj = bless [$ptr], $class ;
78#    return wantarray ? ($obj, $status) : $obj;
79#}
80#
81#package Compress::Raw::Bunzip2 ;
82#
83#sub Compress::Raw::Bunzip2::new
84#{
85#    my $class = shift ;
86#    my ($ptr, $status) = _new(@_);
87#    return wantarray ? (undef, $status) : undef
88#        unless $ptr ;
89#    my $obj = bless [$ptr], $class ;
90#    return wantarray ? ($obj, $status) : $obj;
91#}
92
93sub Compress::Raw::Bzip2::STORABLE_freeze
94{
95    my $type = ref shift;
96    croak "Cannot freeze $type object\n";
97}
98
99sub Compress::Raw::Bzip2::STORABLE_thaw
100{
101    my $type = ref shift;
102    croak "Cannot thaw $type object\n";
103}
104
105sub Compress::Raw::Bunzip2::STORABLE_freeze
106{
107    my $type = ref shift;
108    croak "Cannot freeze $type object\n";
109}
110
111sub Compress::Raw::Bunzip2::STORABLE_thaw
112{
113    my $type = ref shift;
114    croak "Cannot thaw $type object\n";
115}
116
117
118package Compress::Raw::Bzip2;
119
1201;
121
122__END__
123
124
125=head1 NAME
126
127Compress::Raw::Bzip2 - Low-Level Interface to bzip2 compression library
128
129=head1 SYNOPSIS
130
131    use Compress::Raw::Bzip2 ;
132
133    my ($bz, $status) = new Compress::Raw::Bzip2 [OPTS]
134        or die "Cannot create bzip2 object: $bzerno\n";
135
136    $status = $bz->bzdeflate($input, $output);
137    $status = $bz->bzflush($output);
138    $status = $bz->bzclose($output);
139
140    my ($bz, $status) = new Compress::Raw::Bunzip2 [OPTS]
141        or die "Cannot create bunzip2 object: $bzerno\n";
142
143    $status = $bz->bzinflate($input, $output);
144
145    my $version = Compress::Raw::Bzip2::bzlibversion();
146
147=head1 DESCRIPTION
148
149C<Compress::Raw::Bzip2> provides an interface to the in-memory
150compression/uncompression functions from the bzip2 compression library.
151
152Although the primary purpose for the existence of C<Compress::Raw::Bzip2>
153is for use by the  C<IO::Compress::Bzip2> and C<IO::Compress::Bunzip2>
154modules, it can be used on its own for simple compression/uncompression
155tasks.
156
157=head1 Compression
158
159=head2 ($z, $status) = new Compress::Raw::Bzip2 $appendOutput, $blockSize100k, $workfactor;
160
161Creates a new compression object.
162
163If successful, it will return the initialised compression object, C<$z>
164and a C<$status> of C<BZ_OK> in a list context. In scalar context it
165returns the deflation object, C<$z>, only.
166
167If not successful, the returned compression object, C<$z>, will be
168I<undef> and C<$status> will hold the a I<bzip2> error code.
169
170Below is a list of the valid options:
171
172=over 5
173
174=item B<$appendOutput>
175
176Controls whether the compressed data is appended to the output buffer in
177the C<bzdeflate>, C<bzflush> and C<bzclose> methods.
178
179Defaults to 1.
180
181=item B<$blockSize100k>
182
183To quote the bzip2 documentation
184
185    blockSize100k specifies the block size to be used for compression. It
186    should be a value between 1 and 9 inclusive, and the actual block size
187    used is 100000 x this figure. 9 gives the best compression but takes
188    most memory.
189
190Defaults to 1.
191
192=item B<$workfactor>
193
194To quote the bzip2 documentation
195
196    This parameter controls how the compression phase behaves when
197    presented with worst case, highly repetitive, input data. If
198    compression runs into difficulties caused by repetitive data, the
199    library switches from the standard sorting algorithm to a fallback
200    algorithm. The fallback is slower than the standard algorithm by
201    perhaps a factor of three, but always behaves reasonably, no matter how
202    bad the input.
203
204    Lower values of workFactor reduce the amount of effort the standard
205    algorithm will expend before resorting to the fallback. You should set
206    this parameter carefully; too low, and many inputs will be handled by
207    the fallback algorithm and so compress rather slowly, too high, and
208    your average-to-worst case compression times can become very large. The
209    default value of 30 gives reasonable behaviour over a wide range of
210    circumstances.
211
212    Allowable values range from 0 to 250 inclusive. 0 is a special case,
213    equivalent to using the default value of 30.
214
215Defaults to 0.
216
217=back
218
219=head2 $status = $bz->bzdeflate($input, $output);
220
221Reads the contents of C<$input>, compresses it and writes the compressed
222data to C<$output>.
223
224Returns C<BZ_RUN_OK> on success and a C<bzip2> error code on failure.
225
226If C<appendOutput> is enabled in the constructor for the bzip2 object, the
227compressed data will be appended to C<$output>. If not enabled, C<$output>
228will be truncated before the compressed data is written to it.
229
230=head2 $status = $bz->bzflush($output);
231
232Flushes any pending compressed data to C<$output>.
233
234Returns C<BZ_RUN_OK> on success and a C<bzip2> error code on failure.
235
236=head2 $status = $bz->bzclose($output);
237
238Terminates the compressed data stream and flushes any pending compressed
239data to C<$output>.
240
241Returns C<BZ_STREAM_END> on success and a C<bzip2> error code on failure.
242
243=head2 Example
244
245=head1 Uncompression
246
247=head2 ($z, $status) = new Compress::Raw::Bunzip2 $appendOutput, $consumeInput, $small, $verbosity, $limitOutput;
248
249If successful, it will return the initialised uncompression object, C<$z>
250and a C<$status> of C<BZ_OK> in a list context. In scalar context it
251returns the deflation object, C<$z>, only.
252
253If not successful, the returned uncompression object, C<$z>, will be
254I<undef> and C<$status> will hold the a I<bzip2> error code.
255
256Below is a list of the valid options:
257
258=over 5
259
260=item B<$appendOutput>
261
262Controls whether the compressed data is appended to the output buffer in the
263C<bzinflate>, C<bzflush> and C<bzclose> methods.
264
265Defaults to 1.
266
267=item B<$consumeInput>
268
269=item B<$small>
270
271To quote the bzip2 documentation
272
273    If small is nonzero, the library will use an alternative decompression
274    algorithm which uses less memory but at the cost of decompressing more
275    slowly (roughly speaking, half the speed, but the maximum memory
276    requirement drops to around 2300k).
277
278Defaults to 0.
279
280=item B<$limitOutput>
281
282The C<LimitOutput> option changes the behavior of the C<< $i->bzinflate >>
283method so that the amount of memory used by the output buffer can be
284limited.
285
286When C<LimitOutput> is used the size of the output buffer used will either
287be the 16k or the amount of memory already allocated to C<$output>,
288whichever is larger. Predicting the output size available is tricky, so
289don't rely on getting an exact output buffer size.
290
291When C<LimitOutout> is not specified C<< $i->bzinflate >> will use as much
292memory as it takes to write all the uncompressed data it creates by
293uncompressing the input buffer.
294
295If C<LimitOutput> is enabled, the C<ConsumeInput> option will also be
296enabled.
297
298This option defaults to false.
299
300=item B<$verbosity>
301
302This parameter is ignored.
303
304Defaults to 0.
305
306=back
307
308=head2 $status = $z->bzinflate($input, $output);
309
310Uncompresses C<$input> and writes the uncompressed data to C<$output>.
311
312Returns C<BZ_OK> if the uncompression was successful, but the end of the
313compressed data stream has not been reached. Returns C<BZ_STREAM_END> on
314successful uncompression and the end of the compression stream has been
315reached.
316
317If C<consumeInput> is enabled in the constructor for the bunzip2 object,
318C<$input> will have all compressed data removed from it after
319uncompression. On C<BZ_OK> return this will mean that C<$input> will be an
320empty string; when C<BZ_STREAM_END> C<$input> will either be an empty
321string or will contain whatever data immediately followed the compressed
322data stream.
323
324If C<appendOutput> is enabled in the constructor for the bunzip2 object,
325the uncompressed data will be appended to C<$output>. If not enabled,
326C<$output> will be truncated before the uncompressed data is written to it.
327
328=head1 Misc
329
330=head2 my $version = Compress::Raw::Bzip2::bzlibversion();
331
332Returns the version of the underlying bzip2 library.
333
334=head1 Constants
335
336The following bzip2 constants are exported by this module
337
338		BZ_RUN
339		BZ_FLUSH
340		BZ_FINISH
341
342		BZ_OK
343		BZ_RUN_OK
344		BZ_FLUSH_OK
345		BZ_FINISH_OK
346		BZ_STREAM_END
347		BZ_SEQUENCE_ERROR
348		BZ_PARAM_ERROR
349		BZ_MEM_ERROR
350		BZ_DATA_ERROR
351		BZ_DATA_ERROR_MAGIC
352		BZ_IO_ERROR
353		BZ_UNEXPECTED_EOF
354		BZ_OUTBUFF_FULL
355		BZ_CONFIG_ERROR
356
357=head1 SUPPORT
358
359General feedback/questions/bug reports should be sent to
360L<https://github.com/pmqs/Compress-Raw-Bzip2/issues> (preferred) or
361L<https://rt.cpan.org/Public/Dist/Display.html?Name=Compress-Raw-Bzip2>.
362
363=head1 SEE ALSO
364
365L<Compress::Zlib>, L<IO::Compress::Gzip>, L<IO::Uncompress::Gunzip>, L<IO::Compress::Deflate>, L<IO::Uncompress::Inflate>, L<IO::Compress::RawDeflate>, L<IO::Uncompress::RawInflate>, L<IO::Compress::Bzip2>, L<IO::Uncompress::Bunzip2>, L<IO::Compress::Lzma>, L<IO::Uncompress::UnLzma>, L<IO::Compress::Xz>, L<IO::Uncompress::UnXz>, L<IO::Compress::Lzip>, L<IO::Uncompress::UnLzip>, L<IO::Compress::Lzop>, L<IO::Uncompress::UnLzop>, L<IO::Compress::Lzf>, L<IO::Uncompress::UnLzf>, L<IO::Compress::Zstd>, L<IO::Uncompress::UnZstd>, L<IO::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress>
366
367L<IO::Compress::FAQ|IO::Compress::FAQ>
368
369L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
370L<Archive::Tar|Archive::Tar>,
371L<IO::Zlib|IO::Zlib>
372
373The primary site for the bzip2 program is L<https://sourceware.org/bzip2/>.
374
375See the module L<Compress::Bzip2|Compress::Bzip2>
376
377=head1 AUTHOR
378
379This module was written by Paul Marquess, C<pmqs@cpan.org>.
380
381=head1 MODIFICATION HISTORY
382
383See the Changes file.
384
385=head1 COPYRIGHT AND LICENSE
386
387Copyright (c) 2005-2019 Paul Marquess. All rights reserved.
388
389This program is free software; you can redistribute it and/or
390modify it under the same terms as Perl itself.
391
392