| 1 | |
| 2 | package Compress::Raw::Bzip2; |
| 3 | |
| 4 | use strict ; |
| 5 | use warnings ; |
| 6 | |
| 7 | require 5.008 ; |
| 8 | require Exporter; |
| 9 | use Carp ; |
| 10 | |
| 11 | use bytes ; |
| 12 | our ($VERSION, $XS_VERSION, @ISA, @EXPORT, $AUTOLOAD); |
| 13 | |
| 14 | $VERSION = '2.224'; |
| 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 | |
| 44 | sub 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 | |
| 55 | use constant FLAG_APPEND => 1 ; |
| 56 | use constant FLAG_CRC => 2 ; |
| 57 | use constant FLAG_ADLER => 4 ; |
| 58 | use constant FLAG_CONSUME_INPUT => 8 ; |
| 59 | |
| 60 | eval { |
| 61 | require XSLoader; |
| 62 | XSLoader::load('Compress::Raw::Bzip2', $XS_VERSION); |
| 63 | 1; |
| 64 | } |
| 65 | or 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 | |
| 93 | sub Compress::Raw::Bzip2::STORABLE_freeze |
| 94 | { |
| 95 | my $type = ref shift; |
| 96 | croak "Cannot freeze $type object\n"; |
| 97 | } |
| 98 | |
| 99 | sub Compress::Raw::Bzip2::STORABLE_thaw |
| 100 | { |
| 101 | my $type = ref shift; |
| 102 | croak "Cannot thaw $type object\n"; |
| 103 | } |
| 104 | |
| 105 | sub Compress::Raw::Bunzip2::STORABLE_freeze |
| 106 | { |
| 107 | my $type = ref shift; |
| 108 | croak "Cannot freeze $type object\n"; |
| 109 | } |
| 110 | |
| 111 | sub Compress::Raw::Bunzip2::STORABLE_thaw |
| 112 | { |
| 113 | my $type = ref shift; |
| 114 | croak "Cannot thaw $type object\n"; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | package Compress::Raw::Bzip2; |
| 119 | |
| 120 | 1; |
| 121 | |
| 122 | __END__ |
| 123 | |
| 124 | |
| 125 | =head1 NAME |
| 126 | |
| 127 | Compress::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 | |
| 149 | C<Compress::Raw::Bzip2> provides an interface to the in-memory |
| 150 | compression/uncompression functions from the bzip2 compression library. |
| 151 | |
| 152 | Although the primary purpose for the existence of C<Compress::Raw::Bzip2> |
| 153 | is for use by the C<IO::Compress::Bzip2> and C<IO::Compress::Bunzip2> |
| 154 | modules, it can be used on its own for simple compression/uncompression |
| 155 | tasks. |
| 156 | |
| 157 | =head1 Compression |
| 158 | |
| 159 | =head2 ($z, $status) = new Compress::Raw::Bzip2 $appendOutput, $blockSize100k, $workfactor; |
| 160 | |
| 161 | Creates a new compression object. |
| 162 | |
| 163 | If successful, it will return the initialised compression object, C<$z> |
| 164 | and a C<$status> of C<BZ_OK> in a list context. In scalar context it |
| 165 | returns the deflation object, C<$z>, only. |
| 166 | |
| 167 | If not successful, the returned compression object, C<$z>, will be |
| 168 | I<undef> and C<$status> will hold the a I<bzip2> error code. |
| 169 | |
| 170 | Below is a list of the valid options: |
| 171 | |
| 172 | =over 5 |
| 173 | |
| 174 | =item B<$appendOutput> |
| 175 | |
| 176 | Controls whether the compressed data is appended to the output buffer in |
| 177 | the C<bzdeflate>, C<bzflush> and C<bzclose> methods. |
| 178 | |
| 179 | Defaults to 1. |
| 180 | |
| 181 | =item B<$blockSize100k> |
| 182 | |
| 183 | To 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 | |
| 190 | Defaults to 1. |
| 191 | |
| 192 | =item B<$workfactor> |
| 193 | |
| 194 | To 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 | |
| 215 | Defaults to 0. |
| 216 | |
| 217 | =back |
| 218 | |
| 219 | =head2 $status = $bz->bzdeflate($input, $output); |
| 220 | |
| 221 | Reads the contents of C<$input>, compresses it and writes the compressed |
| 222 | data to C<$output>. |
| 223 | |
| 224 | Returns C<BZ_RUN_OK> on success and a C<bzip2> error code on failure. |
| 225 | |
| 226 | If C<appendOutput> is enabled in the constructor for the bzip2 object, the |
| 227 | compressed data will be appended to C<$output>. If not enabled, C<$output> |
| 228 | will be truncated before the compressed data is written to it. |
| 229 | |
| 230 | =head2 $status = $bz->bzflush($output); |
| 231 | |
| 232 | Flushes any pending compressed data to C<$output>. |
| 233 | |
| 234 | Returns C<BZ_RUN_OK> on success and a C<bzip2> error code on failure. |
| 235 | |
| 236 | =head2 $status = $bz->bzclose($output); |
| 237 | |
| 238 | Terminates the compressed data stream and flushes any pending compressed |
| 239 | data to C<$output>. |
| 240 | |
| 241 | Returns 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 | |
| 249 | If successful, it will return the initialised uncompression object, C<$z> |
| 250 | and a C<$status> of C<BZ_OK> in a list context. In scalar context it |
| 251 | returns the deflation object, C<$z>, only. |
| 252 | |
| 253 | If not successful, the returned uncompression object, C<$z>, will be |
| 254 | I<undef> and C<$status> will hold the a I<bzip2> error code. |
| 255 | |
| 256 | Below is a list of the valid options: |
| 257 | |
| 258 | =over 5 |
| 259 | |
| 260 | =item B<$appendOutput> |
| 261 | |
| 262 | Controls whether the compressed data is appended to the output buffer in the |
| 263 | C<bzinflate>, C<bzflush> and C<bzclose> methods. |
| 264 | |
| 265 | Defaults to 1. |
| 266 | |
| 267 | =item B<$consumeInput> |
| 268 | |
| 269 | =item B<$small> |
| 270 | |
| 271 | To 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 | |
| 278 | Defaults to 0. |
| 279 | |
| 280 | =item B<$limitOutput> |
| 281 | |
| 282 | The C<LimitOutput> option changes the behavior of the C<< $i->bzinflate >> |
| 283 | method so that the amount of memory used by the output buffer can be |
| 284 | limited. |
| 285 | |
| 286 | When C<LimitOutput> is used the size of the output buffer used will either |
| 287 | be the 16k or the amount of memory already allocated to C<$output>, |
| 288 | whichever is larger. Predicting the output size available is tricky, so |
| 289 | don't rely on getting an exact output buffer size. |
| 290 | |
| 291 | When C<LimitOutout> is not specified C<< $i->bzinflate >> will use as much |
| 292 | memory as it takes to write all the uncompressed data it creates by |
| 293 | uncompressing the input buffer. |
| 294 | |
| 295 | If C<LimitOutput> is enabled, the C<ConsumeInput> option will also be |
| 296 | enabled. |
| 297 | |
| 298 | This option defaults to false. |
| 299 | |
| 300 | =item B<$verbosity> |
| 301 | |
| 302 | This parameter is ignored. |
| 303 | |
| 304 | Defaults to 0. |
| 305 | |
| 306 | =back |
| 307 | |
| 308 | =head2 $status = $z->bzinflate($input, $output); |
| 309 | |
| 310 | Uncompresses C<$input> and writes the uncompressed data to C<$output>. |
| 311 | |
| 312 | Returns C<BZ_OK> if the uncompression was successful, but the end of the |
| 313 | compressed data stream has not been reached. Returns C<BZ_STREAM_END> on |
| 314 | successful uncompression and the end of the compression stream has been |
| 315 | reached. |
| 316 | |
| 317 | If C<consumeInput> is enabled in the constructor for the bunzip2 object, |
| 318 | C<$input> will have all compressed data removed from it after |
| 319 | uncompression. On C<BZ_OK> return this will mean that C<$input> will be an |
| 320 | empty string; when C<BZ_STREAM_END> C<$input> will either be an empty |
| 321 | string or will contain whatever data immediately followed the compressed |
| 322 | data stream. |
| 323 | |
| 324 | If C<appendOutput> is enabled in the constructor for the bunzip2 object, |
| 325 | the uncompressed data will be appended to C<$output>. If not enabled, |
| 326 | C<$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 | |
| 332 | Returns the version of the underlying bzip2 library. |
| 333 | |
| 334 | =head1 Constants |
| 335 | |
| 336 | The 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 | |
| 359 | General feedback/questions/bug reports should be sent to |
| 360 | L<https://github.com/pmqs/Compress-Raw-Bzip2/issues> (preferred) or |
| 361 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=Compress-Raw-Bzip2>. |
| 362 | |
| 363 | =head1 SEE ALSO |
| 364 | |
| 365 | L<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 | |
| 367 | L<IO::Compress::FAQ|IO::Compress::FAQ> |
| 368 | |
| 369 | L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>, |
| 370 | L<Archive::Tar|Archive::Tar>, |
| 371 | L<IO::Zlib|IO::Zlib> |
| 372 | |
| 373 | The primary site for the bzip2 program is L<https://sourceware.org/bzip2/>. |
| 374 | |
| 375 | See the module L<Compress::Bzip2|Compress::Bzip2> |
| 376 | |
| 377 | =head1 AUTHOR |
| 378 | |
| 379 | This module was written by Paul Marquess, C<[email protected]>. |
| 380 | |
| 381 | =head1 MODIFICATION HISTORY |
| 382 | |
| 383 | See the Changes file. |
| 384 | |
| 385 | =head1 COPYRIGHT AND LICENSE |
| 386 | |
| 387 | Copyright (c) 2005-2026 Paul Marquess. All rights reserved. |
| 388 | |
| 389 | This program is free software; you can redistribute it and/or |
| 390 | modify it under the same terms as Perl itself. |