1package Compress::Raw::Lzma;
2
3use strict ;
4use warnings ;
5
6require 5.006 ;
7require Exporter;
8use AutoLoader;
9use Carp ;
10
11use bytes ;
12our ($VERSION, $XS_VERSION, @ISA, @EXPORT, $AUTOLOAD);
13
14$VERSION = '2.101';
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
24    LZMA_OK
25    LZMA_STREAM_END
26    LZMA_NO_CHECK
27    LZMA_UNSUPPORTED_CHECK
28    LZMA_GET_CHECK
29    LZMA_MEM_ERROR
30    LZMA_MEMLIMIT_ERROR
31    LZMA_FORMAT_ERROR
32    LZMA_OPTIONS_ERROR
33    LZMA_DATA_ERROR
34    LZMA_BUF_ERROR
35    LZMA_PROG_ERROR
36
37    LZMA_RUN
38    LZMA_SYNC_FLUSH
39    LZMA_FULL_FLUSH
40    LZMA_FINISH
41
42    LZMA_FILTER_X86
43    LZMA_FILTER_POWERPC
44    LZMA_FILTER_IA64
45    LZMA_FILTER_ARM
46    LZMA_FILTER_ARMTHUMB
47    LZMA_FILTER_SPARC
48
49
50    LZMA_BLOCK_HEADER_SIZE_MIN
51    LZMA_BLOCK_HEADER_SIZE_MAX
52
53    LZMA_CHECK_NONE
54    LZMA_CHECK_CRC32
55    LZMA_CHECK_CRC64
56    LZMA_CHECK_SHA256
57
58    LZMA_CHECK_ID_MAX
59    LZMA_CHECK_SIZE_MAX
60
61    LZMA_PRESET_DEFAULT
62    LZMA_PRESET_LEVEL_MASK
63    LZMA_PRESET_EXTREME
64
65    LZMA_TELL_NO_CHECK
66    LZMA_TELL_UNSUPPORTED_CHECK
67    LZMA_TELL_ANY_CHECK
68    LZMA_CONCATENATED
69
70
71    LZMA_FILTER_DELTA
72    LZMA_DELTA_DIST_MIN
73    LZMA_DELTA_DIST_MAX
74    LZMA_DELTA_TYPE_BYTE
75
76    LZMA_FILTERS_MAX
77
78    LZMA_FILTER_LZMA2
79
80    LZMA_MF_HC3
81    LZMA_MF_HC4
82    LZMA_MF_BT2
83    LZMA_MF_BT3
84    LZMA_MF_BT4
85
86    LZMA_MODE_FAST
87    LZMA_MODE_NORMAL
88
89    LZMA_DICT_SIZE_MIN
90    LZMA_DICT_SIZE_DEFAULT
91
92    LZMA_LCLP_MIN
93    LZMA_LCLP_MAX
94    LZMA_LC_DEFAULT
95
96    LZMA_LP_DEFAULT
97
98    LZMA_PB_MIN
99    LZMA_PB_MAX
100    LZMA_PB_DEFAULT
101
102    LZMA_STREAM_HEADER_SIZE
103
104    LZMA_BACKWARD_SIZE_MIN
105
106    LZMA_FILTER_SUBBLOCK
107
108    LZMA_SUBFILTER_NONE
109    LZMA_SUBFILTER_SET
110    LZMA_SUBFILTER_RUN
111    LZMA_SUBFILTER_FINISH
112
113    LZMA_SUBBLOCK_ALIGNMENT_MIN
114    LZMA_SUBBLOCK_ALIGNMENT_MAX
115    LZMA_SUBBLOCK_ALIGNMENT_DEFAULT
116
117    LZMA_SUBBLOCK_DATA_SIZE_MIN
118    LZMA_SUBBLOCK_DATA_SIZE_MAX
119    LZMA_SUBBLOCK_DATA_SIZE_DEFAULT
120
121    LZMA_SUBBLOCK_RLE_OFF
122    LZMA_SUBBLOCK_RLE_MIN
123    LZMA_SUBBLOCK_RLE_MAX
124
125    LZMA_VERSION
126    LZMA_VERSION_MAJOR
127    LZMA_VERSION_MINOR
128    LZMA_VERSION_PATCH
129    LZMA_VERSION_STABILITY
130
131    LZMA_VERSION_STABILITY_STRING
132    LZMA_VERSION_STRING
133    );
134
135    #LZMA_VLI_MAX
136    #LZMA_VLI_UNKNOWN
137    #LZMA_VLI_BYTES_MAX
138
139sub AUTOLOAD {
140    my($constname);
141    ($constname = $AUTOLOAD) =~ s/.*:://;
142    my ($error, $val) = constant($constname);
143    Carp::croak $error if $error;
144    no strict 'refs';
145    *{$AUTOLOAD} = sub { $val };
146    goto &{$AUTOLOAD};
147
148}
149
150use constant FLAG_APPEND             => 1 ;
151use constant FLAG_CRC                => 2 ;
152use constant FLAG_ADLER              => 4 ;
153use constant FLAG_CONSUME_INPUT      => 8 ;
154use constant FLAG_LIMIT_OUTPUT       => 16 ;
155
156eval {
157    require XSLoader;
158    XSLoader::load('Compress::Raw::Lzma', $XS_VERSION);
159    1;
160}
161or do {
162    require DynaLoader;
163    local @ISA = qw(DynaLoader);
164    bootstrap Compress::Raw::Lzma $XS_VERSION ;
165};
166
167use constant Parse_any      => 0x01;
168use constant Parse_unsigned => 0x02;
169use constant Parse_signed   => 0x04;
170use constant Parse_boolean  => 0x08;
171use constant Parse_string   => 0x10;
172use constant Parse_custom   => 0x12;
173
174use constant Parse_store_ref => 0x100 ;
175
176use constant OFF_PARSED     => 0 ;
177use constant OFF_TYPE       => 1 ;
178use constant OFF_DEFAULT    => 2 ;
179use constant OFF_FIXED      => 3 ;
180use constant OFF_FIRST_ONLY => 4 ;
181use constant OFF_STICKY     => 5 ;
182
183
184
185sub ParseParameters
186{
187    my $level = shift || 0 ;
188
189    my $sub = (caller($level + 1))[3] ;
190    #local $Carp::CarpLevel = 1 ;
191    my $p = new Compress::Raw::Lzma::Parameters() ;
192    $p->parse(@_)
193        or croak "$sub: $p->{Error}" ;
194
195    return $p;
196}
197
198
199sub Compress::Raw::Lzma::Parameters::new
200{
201    my $class = shift ;
202
203    my $obj = { Error => '',
204                Got   => {},
205              } ;
206
207    #return bless $obj, ref($class) || $class || __PACKAGE__ ;
208    return bless $obj, 'Compress::Raw::Lzma::Parameters' ;
209}
210
211sub Compress::Raw::Lzma::Parameters::setError
212{
213    my $self = shift ;
214    my $error = shift ;
215    my $retval = @_ ? shift : undef ;
216
217    $self->{Error} = $error ;
218    return $retval;
219}
220
221#sub getError
222#{
223#    my $self = shift ;
224#    return $self->{Error} ;
225#}
226
227sub Compress::Raw::Lzma::Parameters::parse
228{
229    my $self = shift ;
230
231    my $default = shift ;
232
233    my $got = $self->{Got} ;
234    my $firstTime = keys %{ $got } == 0 ;
235
236    my (@Bad) ;
237    my @entered = () ;
238
239    # Allow the options to be passed as a hash reference or
240    # as the complete hash.
241    if (@_ == 0) {
242        @entered = () ;
243    }
244    elsif (@_ == 1) {
245        my $href = $_[0] ;
246        return $self->setError("Expected even number of parameters, got 1")
247            if ! defined $href or ! ref $href or ref $href ne "HASH" ;
248
249        foreach my $key (keys %$href) {
250            push @entered, $key ;
251            push @entered, \$href->{$key} ;
252        }
253    }
254    else {
255        my $count = @_;
256        return $self->setError("Expected even number of parameters, got $count")
257            if $count % 2 != 0 ;
258
259        for my $i (0.. $count / 2 - 1) {
260            push @entered, $_[2* $i] ;
261            push @entered, \$_[2* $i+1] ;
262        }
263    }
264
265
266    while (my ($key, $v) = each %$default)
267    {
268        croak "need 4 params [@$v]"
269            if @$v != 4 ;
270
271        my ($first_only, $sticky, $type, $value) = @$v ;
272        my $x ;
273        $self->_checkType($key, \$value, $type, 0, \$x)
274            or return undef ;
275
276        $key = lc $key;
277
278        if ($firstTime || ! $sticky) {
279            $got->{$key} = [0, $type, $value, $x, $first_only, $sticky] ;
280        }
281
282        $got->{$key}[OFF_PARSED] = 0 ;
283    }
284
285    for my $i (0.. @entered / 2 - 1) {
286        my $key = $entered[2* $i] ;
287        my $value = $entered[2* $i+1] ;
288
289        #print "Key [$key] Value [$value]" ;
290        #print defined $$value ? "[$$value]\n" : "[undef]\n";
291
292        $key =~ s/^-// ;
293        my $canonkey = lc $key;
294
295        if ($got->{$canonkey} && ($firstTime ||
296                                  ! $got->{$canonkey}[OFF_FIRST_ONLY]  ))
297        {
298            my $type = $got->{$canonkey}[OFF_TYPE] ;
299            my $s ;
300            $self->_checkType($key, $value, $type, 1, \$s)
301                or return undef ;
302            #$value = $$value unless $type & Parse_store_ref ;
303            $value = $$value ;
304            $got->{$canonkey} = [1, $type, $value, $s] ;
305        }
306        else
307          { push (@Bad, $key) }
308    }
309
310    if (@Bad) {
311        my ($bad) = join(", ", @Bad) ;
312        return $self->setError("unknown key value(s) @Bad") ;
313    }
314
315    return 1;
316}
317
318sub Compress::Raw::Lzma::Parameters::_checkType
319{
320    my $self = shift ;
321
322    my $key   = shift ;
323    my $value = shift ;
324    my $type  = shift ;
325    my $validate  = shift ;
326    my $output  = shift;
327
328    #local $Carp::CarpLevel = $level ;
329    #print "PARSE $type $key $value $validate $sub\n" ;
330    if ( $type & Parse_store_ref)
331    {
332        #$value = $$value
333        #    if ref ${ $value } ;
334
335        $$output = $value ;
336        return 1;
337    }
338
339    $value = $$value ;
340
341    if ($type & Parse_any)
342    {
343        $$output = $value ;
344        return 1;
345    }
346    elsif ($type & Parse_unsigned)
347    {
348        return $self->setError("Parameter '$key' must be an unsigned int, got 'undef'")
349            if $validate && ! defined $value ;
350        return $self->setError("Parameter '$key' must be an unsigned int, got '$value'")
351            if $validate && $value !~ /^\d+$/;
352
353        $$output = defined $value ? $value : 0 ;
354        return 1;
355    }
356    elsif ($type & Parse_signed)
357    {
358        return $self->setError("Parameter '$key' must be a signed int, got 'undef'")
359            if $validate && ! defined $value ;
360        return $self->setError("Parameter '$key' must be a signed int, got '$value'")
361            if $validate && $value !~ /^-?\d+$/;
362
363        $$output = defined $value ? $value : 0 ;
364        return 1 ;
365    }
366    elsif ($type & Parse_boolean)
367    {
368        return $self->setError("Parameter '$key' must be an int, got '$value'")
369            if $validate && defined $value && $value !~ /^\d*$/;
370        $$output =  defined $value ? $value != 0 : 0 ;
371        return 1;
372    }
373    elsif ($type & Parse_string)
374    {
375        $$output = defined $value ? $value : "" ;
376        return 1;
377    }
378
379    $$output = $value ;
380    return 1;
381}
382
383
384
385sub Compress::Raw::Lzma::Parameters::parsed
386{
387    my $self = shift ;
388    my $name = shift ;
389
390    return $self->{Got}{lc $name}[OFF_PARSED] ;
391}
392
393sub Compress::Raw::Lzma::Parameters::value
394{
395    my $self = shift ;
396    my $name = shift ;
397
398    if (@_)
399    {
400        $self->{Got}{lc $name}[OFF_PARSED]  = 1;
401        $self->{Got}{lc $name}[OFF_DEFAULT] = $_[0] ;
402        $self->{Got}{lc $name}[OFF_FIXED]   = $_[0] ;
403    }
404
405    return $self->{Got}{lc $name}[OFF_FIXED] ;
406}
407
408
409sub Compress::Raw::Lzma::Encoder::STORABLE_freeze
410{
411    my $type = ref shift;
412    croak "Cannot freeze $type object\n";
413}
414
415sub Compress::Raw::Lzma::Encoder::STORABLE_thaw
416{
417    my $type = ref shift;
418    croak "Cannot thaw $type object\n";
419}
420
421
422@Compress::Raw::Lzma::EasyEncoder::ISA = qw(Compress::Raw::Lzma::Encoder);
423
424sub Compress::Raw::Lzma::EasyEncoder::new
425{
426    my $pkg = shift ;
427    my ($got) = ParseParameters(0,
428            {
429                'AppendOutput'  => [1, 1, Parse_boolean,  0],
430                'Bufsize'       => [1, 1, Parse_unsigned, 16 * 1024],
431
432                'Preset'        => [1, 1, Parse_unsigned, LZMA_PRESET_DEFAULT()],
433                'Extreme'       => [1, 1, Parse_boolean, 0],
434                'Check'         => [1, 1, Parse_unsigned, LZMA_CHECK_CRC32()],
435            }, @_) ;
436
437
438#    croak "Compress::Raw::Lzma::EasyEncoder::new: Bufsize must be >= 1, you specified " .
439#            $got->value('Bufsize')
440#        unless $got->value('Bufsize') >= 1;
441
442    my $flags = 0 ;
443    $flags |= FLAG_APPEND if $got->value('AppendOutput') ;
444
445    my $preset = $got->value('Preset');
446
447    if ($got->value('Extreme')) {
448        $preset |= LZMA_PRESET_EXTREME();
449    }
450
451    lzma_easy_encoder($pkg, $flags,
452                $got->value('Bufsize'),
453                $preset,
454                $got->value('Check')) ;
455
456}
457
458@Compress::Raw::Lzma::AloneEncoder::ISA = qw(Compress::Raw::Lzma::Encoder);
459
460sub Compress::Raw::Lzma::AloneEncoder::new
461{
462    my $pkg = shift ;
463    my ($got) = ParseParameters(0,
464            {
465                'AppendOutput'  => [1, 1, Parse_boolean,  0],
466                'Bufsize'       => [1, 1, Parse_unsigned, 16 * 1024],
467                'Filter'        => [1, 1, Parse_any, [] ],
468
469            }, @_) ;
470
471
472    my $flags = 0 ;
473    $flags |= FLAG_APPEND if $got->value('AppendOutput') ;
474
475    my $filters = Lzma::Filters::validateFilters(1, 0, $got->value('Filter')) ;
476    # TODO - check max of 1 filter & it is a reference to Lzma::Filter::Lzma1
477
478    lzma_alone_encoder($pkg, $flags,
479                       $got->value('Bufsize'),
480                       $filters);
481
482}
483
484@Compress::Raw::Lzma::StreamEncoder::ISA = qw(Compress::Raw::Lzma::Encoder);
485
486sub Compress::Raw::Lzma::StreamEncoder::new
487{
488    my $pkg = shift ;
489    my ($got) = ParseParameters(0,
490            {
491                'AppendOutput'  => [1, 1, Parse_boolean,  0],
492                'Bufsize'       => [1, 1, Parse_unsigned, 16 * 1024],
493                'Filter'        => [1, 1, Parse_any, [] ],
494                'Check'         => [1, 1, Parse_unsigned, LZMA_CHECK_CRC32()],
495
496            }, @_) ;
497
498
499    my $flags = 0 ;
500    $flags |= FLAG_APPEND if $got->value('AppendOutput') ;
501
502    my $filters = Lzma::Filters::validateFilters(1, 1, $got->value('Filter')) ;
503
504    lzma_stream_encoder($pkg, $flags,
505                        $got->value('Bufsize'),
506                        $filters,
507                        $got->value('Check'));
508
509}
510
511@Compress::Raw::Lzma::RawEncoder::ISA = qw(Compress::Raw::Lzma::Encoder);
512
513sub Compress::Raw::Lzma::RawEncoder::new
514{
515    my $pkg = shift ;
516    my ($got) = ParseParameters(0,
517            {
518                'ForZip'        => [1, 1, Parse_boolean,  0],
519                'AppendOutput'  => [1, 1, Parse_boolean,  0],
520                'Bufsize'       => [1, 1, Parse_unsigned, 16 * 1024],
521                'Filter'        => [1, 1, Parse_any, [] ],
522
523            }, @_) ;
524
525
526    my $flags = 0 ;
527    $flags |= FLAG_APPEND if $got->value('AppendOutput') ;
528
529    my $forZip = $got->value('ForZip');
530
531    my $filters = Lzma::Filters::validateFilters(1, ! $forZip, $got->value('Filter')) ;
532
533    lzma_raw_encoder($pkg, $flags,
534                        $got->value('Bufsize'),
535                        $filters,
536                        $forZip);
537
538}
539
540@Compress::Raw::Lzma::AutoDecoder::ISA = qw(Compress::Raw::Lzma::Decoder);
541
542sub Compress::Raw::Lzma::AutoDecoder::new
543{
544    my $pkg = shift ;
545    my ($got) = ParseParameters(0,
546                    {
547                        'AppendOutput'  => [1, 1, Parse_boolean,  0],
548                        'LimitOutput'   => [1, 1, Parse_boolean,  0],
549                        'ConsumeInput'  => [1, 1, Parse_boolean,  1],
550                        'Bufsize'       => [1, 1, Parse_unsigned, 16 * 1024],
551
552                        'MemLimit'      => [1, 1, Parse_unsigned, 128 *1024 *1024],
553
554            }, @_) ;
555
556
557    my $flags = 0 ;
558    $flags |= FLAG_APPEND if $got->value('AppendOutput') ;
559    $flags |= FLAG_CONSUME_INPUT if $got->value('ConsumeInput') ;
560    $flags |= FLAG_LIMIT_OUTPUT if $got->value('LimitOutput') ;
561
562    lzma_auto_decoder($pkg, $flags, $got->value('MemLimit'));
563}
564
565@Compress::Raw::Lzma::AloneDecoder::ISA = qw(Compress::Raw::Lzma::Decoder);
566
567sub Compress::Raw::Lzma::AloneDecoder::new
568{
569    my $pkg = shift ;
570    my ($got) = ParseParameters(0,
571                    {
572                        'AppendOutput'  => [1, 1, Parse_boolean,  0],
573                        'LimitOutput'   => [1, 1, Parse_boolean,  0],
574                        'ConsumeInput'  => [1, 1, Parse_boolean,  1],
575                        'Bufsize'       => [1, 1, Parse_unsigned, 16 * 1024],
576
577                        'MemLimit'      => [1, 1, Parse_unsigned, 128 *1024 *1024],
578
579            }, @_) ;
580
581
582    my $flags = 0 ;
583    $flags |= FLAG_APPEND if $got->value('AppendOutput') ;
584    $flags |= FLAG_CONSUME_INPUT if $got->value('ConsumeInput') ;
585    $flags |= FLAG_LIMIT_OUTPUT if $got->value('LimitOutput') ;
586
587    lzma_alone_decoder($pkg,
588                       $flags,
589                       $got->value('Bufsize'),
590                       $got->value('MemLimit'));
591}
592
593@Compress::Raw::Lzma::StreamDecoder::ISA = qw(Compress::Raw::Lzma::Decoder);
594
595sub Compress::Raw::Lzma::StreamDecoder::new
596{
597    my $pkg = shift ;
598    my ($got) = ParseParameters(0,
599                    {
600                        'AppendOutput'  => [1, 1, Parse_boolean,  0],
601                        'LimitOutput'   => [1, 1, Parse_boolean,  0],
602                        'ConsumeInput'  => [1, 1, Parse_boolean,  1],
603                        'Bufsize'       => [1, 1, Parse_unsigned, 16 * 1024],
604
605                        'MemLimit'      => [1, 1, Parse_unsigned, 128 *1024 *1024],
606                        'Flags'         => [1, 1, Parse_unsigned, 0],
607
608            }, @_) ;
609
610
611    my $flags = 0 ;
612    $flags |= FLAG_APPEND if $got->value('AppendOutput') ;
613    $flags |= FLAG_CONSUME_INPUT if $got->value('ConsumeInput') ;
614    $flags |= FLAG_LIMIT_OUTPUT if $got->value('LimitOutput') ;
615
616    lzma_stream_decoder($pkg,
617                        $flags,
618                        $got->value('Bufsize'),
619                        $got->value('MemLimit'),
620                        $got->value('Flags'));
621}
622
623@Compress::Raw::Lzma::RawDecoder::ISA = qw(Compress::Raw::Lzma::Decoder);
624
625sub Compress::Raw::Lzma::RawDecoder::new
626{
627    my $pkg = shift ;
628    my ($got) = ParseParameters(0,
629                    {
630                        'AppendOutput'  => [1, 1, Parse_boolean,  0],
631                        'LimitOutput'   => [1, 1, Parse_boolean,  0],
632                        'ConsumeInput'  => [1, 1, Parse_boolean,  1],
633                        'Bufsize'       => [1, 1, Parse_unsigned, 16 * 1024],
634                        'Filter'        => [1, 1, Parse_any, [] ],
635                        'Properties'    => [1, 1, Parse_any,  undef],
636            }, @_) ;
637
638
639    my $flags = 0 ;
640    $flags |= FLAG_APPEND if $got->value('AppendOutput') ;
641    $flags |= FLAG_CONSUME_INPUT if $got->value('ConsumeInput') ;
642    $flags |= FLAG_LIMIT_OUTPUT if $got->value('LimitOutput') ;
643
644    my $filters = Lzma::Filters::validateFilters(0, ! defined $got->value('Properties'),
645                            $got->value('Filter')) ;
646
647    lzma_raw_decoder($pkg,
648                        $flags,
649                        $got->value('Bufsize'),
650                        $filters,
651                        $got->value('Properties'));
652}
653
654# LZMA1/2
655#   Preset
656#   Dict
657#   Lc
658#   Lp
659#   Pb
660#   Mode LZMA_MODE_FAST, LZMA_MODE_NORMAL
661#   Nice
662#   Mf LZMA_MF_HC3 LZMA_MF_HC4 LZMA_MF_BT2 LZMA_MF_BT3 LZMA_MF_BT4
663#   Depth
664
665# BCJ
666#   LZMA_FILTER_X86
667#   LZMA_FILTER_POWERPC
668#   LZMA_FILTER_IA64
669#   LZMA_FILTER_ARM
670#   LZMA_FILTER_ARMTHUMB
671#   LZMA_FILTER_SPARC
672#
673#   BCJ => LZMA_FILTER_X86 -- this assumes offset is 0
674#   BCJ => [LZMA_FILTER_X86, offset]
675
676# Delta
677#    Dist 1 - 256, 1
678
679# Subblock
680#    Size
681#    RLE
682#    Align
683
684# Preset (0-9) LZMA_PRESET_EXTREME LZMA_PRESET_DEFAULT -- call lzma_lzma_preset
685
686# Memory
687
688# Check => LZMA_CHECK_NONE, LZMA_CHECK_CRC32, LZMA_CHECK_CRC64, LZMA_CHECK_SHA256
689
690# my $bool = lzma_check_is_supported(LZMA_CHECK_CRC32);
691# my $int = lzma_check_size(LZMA_CHECK_CRC32);
692# my $int = $lzma->lzma_get_check();
693
694
695
696
697#sub Compress::Raw::Lzma::new
698#{
699#    my $class = shift ;
700#    my ($ptr, $status) = _new(@_);
701#    return wantarray ? (undef, $status) : undef
702#        unless $ptr ;
703#    my $obj = bless [$ptr], $class ;
704#    return wantarray ? ($obj, $status) : $obj;
705#}
706#
707#package Compress::Raw::UnLzma ;
708#
709#sub Compress::Raw::UnLzma::new
710#{
711#    my $class = shift ;
712#    my ($ptr, $status) = _new(@_);
713#    return wantarray ? (undef, $status) : undef
714#        unless $ptr ;
715#    my $obj = bless [$ptr], $class ;
716#    return wantarray ? ($obj, $status) : $obj;
717#}
718
719
720sub Lzma::Filters::validateFilters
721{
722    use UNIVERSAL ;
723    use Scalar::Util qw(blessed );
724
725    my $encoding = shift; # not decoding
726    my $lzma2 = shift;
727
728    # my $objType = $lzma2 ? "Lzma::Filter::Lzma2"
729    #                      : "Lzma::Filter::Lzma" ;
730
731    my $objType =  "Lzma::Filter::Lzma" ;
732
733    # if only one, convert into an array reference
734    if (blessed $_[0] )  {
735        die "filter object $_[0] is not an $objType object"
736            unless UNIVERSAL::isa($_[0], $objType);
737
738            #$_[0] = [ $_[0] ] ;
739        return [ $_[0] ] ;
740    }
741
742    if (ref $_[0] ne 'ARRAY')
743      { die "$_[0] not Lzma::Filter object or ARRAY ref" }
744
745    my $filters = $_[0] ;
746    my $count = @$filters;
747
748    # check number of filters
749    die sprintf "Too many filters ($count), max is %d", LZMA_FILTERS_MAX()
750        if $count > LZMA_FILTERS_MAX();
751
752    # TODO - add more tests here
753    # Check that all filters inherit from Lzma::Filter
754    # check that filters are supported
755    # check memory requirements
756    # need exactly one lzma1/2 filter
757    # lzma1/2 is the last thing in the list
758    for (my $i = 0; $i <  @$filters ; ++$i)
759    {
760        my $filt = $filters->[$i];
761        die "filter is not an Lzma::Filter object"
762            unless UNIVERSAL::isa($filt, 'Lzma::Filter');
763        die "Lzma filter must be last"
764            if UNIVERSAL::isa($filt, 'Lzma::Filter::Lzma') && $i < $count -1 ;
765
766        #die "xxx" unless lzma_filter_encoder_is_supported($filt->id());
767    }
768
769    if (@$filters == 0)
770    {
771        push @$filters, $lzma2 ? Lzma::Filter::Lzma2()
772                               : Lzma::Filter::Lzma1();
773    }
774
775    return $filters;
776}
777
778#package Lzma::Filter;
779#package Lzma::Filter::Lzma;
780
781#our ($VERSION, @ISA, @EXPORT, $AUTOLOAD);
782@Lzma::Filter::Lzma::ISA = qw(Lzma::Filter);
783
784sub Lzma::Filter::Lzma::mk
785{
786    my $type = shift;
787
788    my $got = Compress::Raw::Lzma::ParseParameters(0,
789        {
790            'DictSize' => [1, 1, Parse_unsigned(), LZMA_DICT_SIZE_DEFAULT()],
791            'PresetDict' => [1, 1, Parse_string(), undef],
792            'Lc'    => [1, 1, Parse_unsigned(), LZMA_LC_DEFAULT()],
793            'Lp'    => [1, 1, Parse_unsigned(), LZMA_LP_DEFAULT()],
794            'Pb'    => [1, 1, Parse_unsigned(), LZMA_PB_DEFAULT()],
795            'Mode'  => [1, 1, Parse_unsigned(), LZMA_MODE_NORMAL()],
796            'Nice'  => [1, 1, Parse_unsigned(), 64],
797            'Mf'    => [1, 1, Parse_unsigned(), LZMA_MF_BT4()],
798            'Depth' => [1, 1, Parse_unsigned(), 0],
799        }, @_) ;
800
801    my $pkg = (caller(1))[3] ;
802
803    my $DictSize = $got->value('DictSize');
804    die "Dictsize $DictSize not in range 4KiB - 1536Mib"
805        if $DictSize < 1024 * 4 ||
806           $DictSize > 1024 * 1024 * 1536 ;
807
808    my $Lc = $got->value('Lc');
809    die "Lc $Lc not in range 0-4"
810        if $Lc < 0 || $Lc > 4;
811
812    my $Lp = $got->value('Lp');
813    die "Lp $Lp not in range 0-4"
814        if $Lp < 0 || $Lp > 4;
815
816    die "Lc + Lp must be <= 4"
817        if $Lc + $Lp > 4;
818
819    my $Pb = $got->value('Pb');
820    die "Pb $Pb not in range 0-4"
821        if $Pb < 0 || $Pb > 4;
822
823    my $Mode = $got->value('Mode');
824    die "Mode $Mode not LZMA_MODE_FAST or LZMA_MODE_NORMAL"
825        if $Mode != LZMA_MODE_FAST() && $Mode != LZMA_MODE_NORMAL();
826
827    my $Mf = $got->value('Mf');
828    die "Mf $Mf not valid"
829        if ! grep { $Mf == $_ }
830             ( LZMA_MF_HC3(),
831               LZMA_MF_HC4(),
832               LZMA_MF_BT2(),
833               LZMA_MF_BT3(),
834               LZMA_MF_BT4());
835
836    my $Nice = $got->value('Nice');
837    die "Nice $Nice not in range 2-273"
838        if $Nice < 2 || $Nice > 273;
839
840    my $obj = Lzma::Filter::Lzma::_mk($type,
841                            $DictSize,
842                            $Lc,
843                            $Lp,
844                            $Pb,
845                            $Mode,
846                            $Nice,
847                            $Mf,
848                            $got->value('Depth'),
849                            $got->value('PresetDict'),
850                        );
851
852    bless $obj, $pkg
853        if defined $obj;
854
855    $obj;
856}
857
858sub Lzma::Filter::Lzma::mkPreset
859{
860    my $type = shift;
861
862    my $preset = shift;
863    my $pkg = (caller(1))[3] ;
864
865    my $obj = Lzma::Filter::Lzma::_mkPreset($type, $preset);
866
867    bless $obj, $pkg
868        if defined $obj;
869
870    $obj;
871}
872
873@Lzma::Filter::Lzma1::ISA = qw(Lzma::Filter::Lzma);
874sub Lzma::Filter::Lzma1
875{
876    Lzma::Filter::Lzma::mk(0, @_);
877}
878
879@Lzma::Filter::Lzma1::Preset::ISA = qw(Lzma::Filter::Lzma);
880sub Lzma::Filter::Lzma1::Preset
881{
882    Lzma::Filter::Lzma::mkPreset(0, @_);
883}
884
885@Lzma::Filter::Lzma2::ISA = qw(Lzma::Filter::Lzma);
886sub Lzma::Filter::Lzma2
887{
888    Lzma::Filter::Lzma::mk(1, @_);
889}
890
891@Lzma::Filter::Lzma2::Preset::ISA = qw(Lzma::Filter::Lzma);
892sub Lzma::Filter::Lzma2::Preset
893{
894    Lzma::Filter::Lzma::mkPreset(1, @_);
895}
896
897@Lzma::Filter::BCJ::ISA = qw(Lzma::Filter);
898
899sub Lzma::Filter::BCJ::mk
900{
901    my $type = shift;
902    my $got = Compress::Raw::Lzma::ParseParameters(0,
903            {
904                'Offset' => [1, 1, Parse_unsigned(), 0],
905            }, @_) ;
906
907    my $pkg = (caller(1))[3] ;
908    my $obj = Lzma::Filter::BCJ::_mk($type, $got->value('Offset')) ;
909    bless $obj, $pkg
910        if defined $obj;
911
912    $obj;
913}
914
915@Lzma::Filter::X86::ISA = qw(Lzma::Filter::BCJ);
916
917sub Lzma::Filter::X86
918{
919    Lzma::Filter::BCJ::mk(LZMA_FILTER_X86(), @_);
920}
921
922@Lzma::Filter::PowerPC::ISA = qw(Lzma::Filter::BCJ);
923
924sub Lzma::Filter::PowerPC
925{
926    Lzma::Filter::BCJ::mk(LZMA_FILTER_POWERPC(), @_);
927}
928
929@Lzma::Filter::IA64::ISA = qw(Lzma::Filter::BCJ);
930
931sub Lzma::Filter::IA64
932{
933    Lzma::Filter::BCJ::mk(LZMA_FILTER_IA64(), @_);
934}
935
936@Lzma::Filter::ARM::ISA = qw(Lzma::Filter::BCJ);
937
938sub Lzma::Filter::ARM
939{
940    Lzma::Filter::BCJ::mk(LZMA_FILTER_ARM(), @_);
941}
942
943@Lzma::Filter::ARMThumb::ISA = qw(Lzma::Filter::BCJ);
944
945sub Lzma::Filter::ARMThumb
946{
947    Lzma::Filter::BCJ::mk(LZMA_FILTER_ARMTHUMB(), @_);
948}
949
950@Lzma::Filter::Sparc::ISA = qw(Lzma::Filter::BCJ);
951
952sub Lzma::Filter::Sparc
953{
954    Lzma::Filter::BCJ::mk(LZMA_FILTER_SPARC(), @_);
955}
956
957
958@Lzma::Filter::Delta::ISA = qw(Lzma::Filter);
959sub Lzma::Filter::Delta
960{
961    #my $pkg = shift ;
962    my ($got) = Compress::Raw::Lzma::ParseParameters(0,
963            {
964                'Type'   => [1, 1, Parse_unsigned,  LZMA_DELTA_TYPE_BYTE()],
965                'Distance' => [1, 1, Parse_unsigned, LZMA_DELTA_DIST_MIN()],
966            }, @_) ;
967
968    Lzma::Filter::Delta::_mk($got->value('Type'),
969                             $got->value('Distance')) ;
970}
971
972#package Lzma::Filter::SubBlock;
973
974
975package Compress::Raw::Lzma;
976
9771;
978
979__END__
980
981
982=head1 NAME
983
984Compress::Raw::Lzma - Low-Level Interface to lzma compression library
985
986=head1 SYNOPSIS
987
988    use Compress::Raw::Lzma ;
989
990    # Encoders
991    my ($lz, $status) = new Compress::Raw::Lzma::EasyEncoder [OPTS]
992        or die "Cannot create lzma object: $status\n";
993
994    my ($lz, $status) = new Compress::Raw::Lzma::AloneEncoder [OPTS]
995        or die "Cannot create lzma object: $status\n";
996
997    my ($lz, $status) = new Compress::Raw::Lzma::StreamEncoder [OPTS]
998        or die "Cannot create lzma object: $status\n";
999
1000    my ($lz, $status) = new Compress::Raw::Lzma::RawEncoder [OPTS]
1001        or die "Cannot create lzma object: $status\n";
1002
1003    $status = $lz->code($input, $output);
1004    $status = $lz->flush($output);
1005
1006    # Decoders
1007    my ($lz, $status) = new Compress::Raw::Lzma::AloneDecoder [OPTS]
1008        or die "Cannot create bunzip2 object: $status\n";
1009
1010    my ($lz, $status) = new Compress::Raw::Lzma::AutoDecoder [OPTS]
1011        or die "Cannot create bunzip2 object: $status\n";
1012
1013    my ($lz, $status) = new Compress::Raw::Lzma::StreamDecoder [OPTS]
1014        or die "Cannot create bunzip2 object: $status\n";
1015
1016    my ($lz, $status) = new Compress::Raw::Lzma::RawDecoder [OPTS]
1017        or die "Cannot create bunzip2 object: $status\n";
1018
1019    $status = $lz->code($input, $output);
1020
1021    my $version = Compress::Raw::Lzma::lzma_version_number();
1022    my $version = Compress::Raw::Lzma::lzma_version_string();
1023
1024=head1 DESCRIPTION
1025
1026C<Compress::Raw::Lzma> provides an interface to the in-memory
1027compression/uncompression functions from the lzma compression library.
1028
1029Although the primary purpose for the existence of C<Compress::Raw::Lzma> is
1030for use by the  C<IO::Compress::Lzma>, C<IO::Uncompress::UnLzma>,
1031C<IO::Compress::Xz> and C<IO::Uncompress::UnXz> modules, it can be used on
1032its own for simple compression/uncompression tasks.
1033
1034There are two functions, called C<code> and C<flush>, used in all the
1035compression and uncompression interfaces defined in this module. By default
1036both of these functions overwrites any data stored in its output buffer
1037parameter. If you want to compress/uncompress to a single buffer, and have
1038C<code> and C<flush> append to that buffer, enable the C<AppendOutput>
1039option when you create the compression/decompression object.
1040
1041=head1 Compression
1042
1043There are four compression interfaces available in this module.
1044
1045=over 5
1046
1047=item Compress::Raw::Lzma::EasyEncoder
1048=item Compress::Raw::Lzma::AloneEncoder
1049=item Compress::Raw::Lzma::StreamEncoder
1050=item Compress::Raw::Lzma::RawEncoder
1051
1052=back
1053
1054=head2 ($z, $status) = new Compress::Raw::Lzma::EasyEncoder [OPTS];
1055
1056Creates a new I<xz> compression object.
1057
1058If successful, it will return the initialised compression object, C<$z>
1059and a C<$status> of C<LZMA_OK> in a list context. In scalar context it
1060returns the deflation object, C<$z>, only.
1061
1062If not successful, the returned compression object, C<$z>, will be
1063I<undef> and C<$status> will hold the an I<lzma> error code.
1064
1065Below is a list of the valid options:
1066
1067=over 5
1068
1069=item B<< Preset => $preset >>
1070
1071Used to choose the compression preset.
1072
1073Valid values are 0-9 and C<LZMA_PRESET_DEFAULT>.
1074
10750 is the fastest compression with the lowest memory usage and the lowest
1076compression.
1077
10789 is the slowest compression with the highest memory usage but with the best
1079compression.
1080
1081Defaults to C<LZMA_PRESET_DEFAULT>.
1082
1083=item B<< Extreme => 0|1 >>
1084
1085Makes the compression a lot slower, but a small compression gain.
1086
1087Defaults to 0.
1088
1089=item B<< Check => $check >>
1090
1091Used to specify the integrity check used in the xz data stream.
1092Valid values are C<LZMA_CHECK_NONE>, C<LZMA_CHECK_CRC32>,
1093C<LZMA_CHECK_CRC64>, C<LZMA_CHECK_SHA256>.
1094
1095Defaults to C<LZMA_CHECK_CRC32>.
1096
1097=item B<< AppendOutput => 0|1 >>
1098
1099Controls whether the compressed data is appended to the output buffer in
1100the C<code> and C<flush> methods.
1101
1102Defaults to 0.
1103(Note in versions of this module prior to 2.072 the default value was
1104incorrectly documented as 1).
1105
1106=item B<< BufSize => $number >>
1107
1108Sets the initial size for the output buffer used by the C<$d-E<gt>code>
1109method. If the buffer has to be reallocated to increase the size, it will
1110grow in increments of C<Bufsize>.
1111
1112Defaults to 16k.
1113
1114=back
1115
1116=head2 ($z, $status) = new Compress::Raw::Lzma::AloneEncoder [OPTS];
1117
1118Creates a legacy I<lzma> compression object. This format is also know as
1119lzma_alone.
1120
1121If successful, it will return the initialised compression object, C<$z>
1122and a C<$status> of C<LZMA_OK> in a list context. In scalar context it
1123returns the deflation object, C<$z>, only.
1124
1125If not successful, the returned compression object, C<$z>, will be
1126I<undef> and C<$status> will hold the an I<lzma> error code.
1127
1128Below is a list of the valid options:
1129
1130=over 5
1131
1132=item B<< Filter => $filter >>
1133
1134The C< $filter > option must be an object of type C<Lzma::Filter::Lzma1>.
1135See L<Compress::Raw::Lzma/Lzma::Filter::Lzma> for a definition
1136of C<Lzma::Filter::Lzma1>.
1137
1138If this option is not present an C<Lzma::Filter::Lzma1> object with default
1139values will be used.
1140
1141=item B<< AppendOutput => 0|1 >>
1142
1143Controls whether the compressed data is appended to the output buffer in
1144the C<code> and C<flush> methods.
1145
1146Defaults to 0.
1147(Note in versions of this module prior to 2.072 the default value was
1148incorrectly documented as 1).
1149
1150=item B<< BufSize => $number >>
1151
1152Sets the initial size for the output buffer used by the C<$d-E<gt>code>
1153method. If the buffer has to be reallocated to increase the size, it will
1154grow in increments of C<Bufsize>.
1155
1156Defaults to 16k.
1157
1158=back
1159
1160=head2 ($z, $status) = new Compress::Raw::Lzma::StreamEncoder [OPTS];
1161
1162Creates a I<xz> compression object.
1163
1164If successful, it will return the initialised compression object, C<$z>
1165and a C<$status> of C<LZMA_OK> in a list context. In scalar context it
1166returns the deflation object, C<$z>, only.
1167
1168If not successful, the returned compression object, C<$z>, will be
1169I<undef> and C<$status> will hold the an I<lzma> error code.
1170
1171Below is a list of the valid options:
1172
1173=over 5
1174
1175=item B<< Filter => $filter >>
1176=item B<< Filter => [$filter1, $filter2,...] >>
1177
1178This option is used to change the bahaviour of the StreamEncoder by
1179applying between one and C<LZMA_FILTERS_MAX> filters to the data stream
1180during compression. See L</Filters> for more details on the available
1181filters.
1182
1183If this option is present it must either contain a single
1184C<Lzma::Filter::Lzma> filter object or an array reference containing between
1185one and C<LZMA_FILTERS_MAX> filter objects.
1186
1187If this option is not present an C<Lzma::Filter::Lzma2> object with default
1188values will be used.
1189
1190=item B<< Check => $check >>
1191
1192Used to specify the integrity check used in the xz data stream.
1193Valid values are C<LZMA_CHECK_NONE>, C<LZMA_CHECK_CRC32>,
1194C<LZMA_CHECK_CRC64>, C<LZMA_CHECK_SHA256>.
1195
1196Defaults to C<LZMA_CHECK_CRC32>.
1197
1198=item B<< AppendOutput => 0|1 >>
1199
1200Controls whether the compressed data is appended to the output buffer in
1201the C<code> and C<flush> methods.
1202
1203Defaults to 0.
1204(Note in versions of this module prior to 2.072 the default value was
1205incorrectly documented as 1).
1206
1207=item B<< BufSize => $number >>
1208
1209Sets the initial size for the output buffer used by the C<$d-E<gt>code>
1210method. If the buffer has to be reallocated to increase the size, it will
1211grow in increments of C<Bufsize>.
1212
1213Defaults to 16k.
1214
1215=back
1216
1217=head2 ($z, $status) = new Compress::Raw::Lzma::RawEncoder [OPTS];
1218
1219Low level access to lzma.
1220
1221If successful, it will return the initialised compression object, C<$z>
1222and a C<$status> of C<LZMA_OK> in a list context. In scalar context it
1223returns the deflation object, C<$z>, only.
1224
1225If not successful, the returned compression object, C<$z>, will be
1226I<undef> and C<$status> will hold the an I<lzma> error code.
1227
1228Below is a list of the valid options:
1229
1230=over 5
1231
1232=item B<< Filter => $filter >>
1233=item B<< Filter => [$filter1, $filter2,...] >>
1234
1235This option is used to change the bahaviour of the RawEncoder by
1236applying between one and C<LZMA_FILTERS_MAX> filters to the data stream
1237during compression. See L</Filters> for more details on the available
1238filters.
1239
1240If this option is present it must either contain a single
1241C<Lzma::Filter::Lzma> filter object or an array reference containing between
1242one and C<LZMA_FILTERS_MAX> filter objects.
1243
1244If this option is not present an C<Lzma::Filter::Lzma2> object with default
1245values will be used.
1246
1247=item B<< AppendOutput => 0|1 >>
1248
1249Controls whether the compressed data is appended to the output buffer in
1250the C<code> and C<flush> methods.
1251
1252Defaults to 0.
1253(Note in versions of this module prior to 2.072 the default value was
1254incorrectly documented as 1).
1255
1256=item B<< BufSize => $number >>
1257
1258Sets the initial size for the output buffer used by the C<$d-E<gt>code>
1259method. If the buffer has to be reallocated to increase the size, it will
1260grow in increments of C<Bufsize>.
1261
1262Defaults to 16k.
1263
1264=item B<< ForZip => 1/0 >>
1265
1266This boolean option is used to enable prefixing the compressed data stream
1267with an encoded copy of the filter properties.
1268
1269Defaults to 0.
1270
1271=back
1272
1273=head2 $status = $lz->code($input, $output);
1274
1275Reads the contents of C<$input>, compresses it and writes the compressed
1276data to C<$output>.
1277
1278Returns C<LZMA_OK> on success and an C<lzma> error code on failure.
1279
1280If C<appendOutput> is enabled in the constructor for the lzma object, the
1281compressed data will be appended to C<$output>. If not enabled, C<$output>
1282will be truncated before the compressed data is written to it.
1283
1284=head2 $status = $lz->flush($output, LZMA_FINISH);
1285
1286Flushes any pending compressed data to C<$output>. By default it terminates
1287the compressed data stream.
1288
1289Returns C<LZMA_OK> on success and an C<lzma> error code on failure.
1290
1291=head2 Example
1292
1293TODO
1294
1295=head1 Uncompression
1296
1297There are four uncompression interfaces available in this module.
1298
1299=over 5
1300
1301=item Compress::Raw::Lzma::AutoDecoder
1302=item Compress::Raw::Lzma::AloneDecoder
1303=item Compress::Raw::Lzma::StreamDecoder
1304=item Compress::Raw::Lzma::RawDecoder
1305
1306=back
1307
1308=head2 ($z, $status) = new Compress::Raw::Lzma::AutoDecoder [OPTS] ;
1309
1310Create an object that can uncompress any of the compressed data streams
1311that can be created by this module.
1312
1313If successful, it will return the initialised uncompression object, C<$z>
1314and a C<$status> of C<LZMA_OK> in a list context. In scalar context it
1315returns the deflation object, C<$z>, only.
1316
1317If not successful, the returned uncompression object, C<$z>, will be
1318I<undef> and C<$status> will hold the an I<lzma> error code.
1319
1320Below is a list of the valid options:
1321
1322=over 5
1323
1324=item B<-MemLimit>
1325
1326The number of bytes to use when uncompressing.
1327
1328Default is unlimited.
1329
1330=item B<-Bufsize>
1331
1332Sets the initial size for the output buffer used by the C<$i-E<gt>code>
1333method. If the output buffer in this method has to be reallocated to
1334increase the size, it will grow in increments of C<Bufsize>.
1335
1336Default is 16k.
1337
1338=item B<-AppendOutput>
1339
1340This option controls how data is written to the output buffer by the
1341C<$i-E<gt>code> method.
1342
1343If the option is set to false, the output buffer in the C<$i-E<gt>code>
1344method will be truncated before uncompressed data is written to it.
1345
1346If the option is set to true, uncompressed data will be appended to the
1347output buffer by the C<$i-E<gt>code> method.
1348
1349This option defaults to false.
1350
1351=item B<-ConsumeInput>
1352
1353If set to true, this option will remove compressed data from the input
1354buffer of the C<< $i->code >> method as the uncompression progresses.
1355
1356This option can be useful when you are processing compressed data that is
1357embedded in another file/buffer. In this case the data that immediately
1358follows the compressed stream will be left in the input buffer.
1359
1360This option defaults to true.
1361
1362=item B<-LimitOutput>
1363
1364The C<LimitOutput> option changes the behavior of the C<< $i->code >>
1365method so that the amount of memory used by the output buffer can be
1366limited.
1367
1368When C<LimitOutput> is used the size of the output buffer used will either
1369be the value of the C<Bufsize> option or the amount of memory already
1370allocated to C<$output>, whichever is larger. Predicting the output size
1371available is tricky, so don't rely on getting an exact output buffer size.
1372
1373When C<LimitOutout> is not specified C<< $i->code >> will use as much
1374memory as it takes to write all the uncompressed data it creates by
1375uncompressing the input buffer.
1376
1377If C<LimitOutput> is enabled, the C<ConsumeInput> option will also be
1378enabled.
1379
1380This option defaults to false.
1381
1382See L</The LimitOutput option> for a discussion on why C<LimitOutput> is
1383needed and how to use it.
1384
1385=back
1386
1387=head2 ($z, $status) = new Compress::Raw::Lzma::AloneDecoder [OPTS] ;
1388
1389Create an object that can uncompress an lzma_alone data stream.
1390
1391If successful, it will return the initialised uncompression object, C<$z>
1392and a C<$status> of C<LZMA_OK> in a list context. In scalar context it
1393returns the deflation object, C<$z>, only.
1394
1395If not successful, the returned uncompression object, C<$z>, will be
1396I<undef> and C<$status> will hold the an I<lzma> error code.
1397
1398Below is a list of the valid options:
1399
1400=over 5
1401
1402=item B<-MemLimit>
1403
1404The number of bytes to use when uncompressing.
1405
1406Default is unlimited.
1407
1408=item B<-Bufsize>
1409
1410Sets the initial size for the output buffer used by the C<$i-E<gt>code>
1411method. If the output buffer in this method has to be reallocated to
1412increase the size, it will grow in increments of C<Bufsize>.
1413
1414Default is 16k.
1415
1416=item B<-AppendOutput>
1417
1418This option controls how data is written to the output buffer by the
1419C<$i-E<gt>code> method.
1420
1421If the option is set to false, the output buffer in the C<$i-E<gt>code>
1422method will be truncated before uncompressed data is written to it.
1423
1424If the option is set to true, uncompressed data will be appended to the
1425output buffer by the C<$i-E<gt>code> method.
1426
1427This option defaults to false.
1428
1429=item B<-ConsumeInput>
1430
1431If set to true, this option will remove compressed data from the input
1432buffer of the C<< $i->code >> method as the uncompression progresses.
1433
1434This option can be useful when you are processing compressed data that is
1435embedded in another file/buffer. In this case the data that immediately
1436follows the compressed stream will be left in the input buffer.
1437
1438This option defaults to true.
1439
1440=item B<-LimitOutput>
1441
1442The C<LimitOutput> option changes the behavior of the C<< $i->code >>
1443method so that the amount of memory used by the output buffer can be
1444limited.
1445
1446When C<LimitOutput> is used the size of the output buffer used will either
1447be the value of the C<Bufsize> option or the amount of memory already
1448allocated to C<$output>, whichever is larger. Predicting the output size
1449available is tricky, so don't rely on getting an exact output buffer size.
1450
1451When C<LimitOutout> is not specified C<< $i->code >> will use as much
1452memory as it takes to write all the uncompressed data it creates by
1453uncompressing the input buffer.
1454
1455If C<LimitOutput> is enabled, the C<ConsumeInput> option will also be
1456enabled.
1457
1458This option defaults to false.
1459
1460See L</The LimitOutput option> for a discussion on why C<LimitOutput> is
1461needed and how to use it.
1462
1463=back
1464
1465=head2 $status = $z->code($input, $output);
1466
1467Uncompresses C<$input> and writes the uncompressed data to C<$output>.
1468
1469Returns C<LZMA_OK> if the uncompression was successful, but the end of the
1470compressed data stream has not been reached. Returns C<LZMA_STREAM_END> on
1471successful uncompression and the end of the compression stream has been
1472reached.
1473
1474If C<consumeInput> is enabled in the constructor for the lzma object,
1475C<$input> will have all compressed data removed from it after
1476uncompression. On C<LZMA_OK> return this will mean that C<$input> will be an
1477empty string; when C<LZMA_STREAM_END> C<$input> will either be an empty
1478string or will contain whatever data immediately followed the compressed
1479data stream.
1480
1481If C<appendOutput> is enabled in the constructor for the lzma object,
1482the uncompressed data will be appended to C<$output>. If not enabled,
1483C<$output> will be truncated before the uncompressed data is written to it.
1484
1485=head1 Filters
1486
1487TODO - more here
1488
1489A number of the Lzma compression interfaces (namely
1490C<Compress::Raw::Lzma::StreamEncoder> &
1491C<Compress::Raw::Lzma::AloneEncoder>) and the raw lzma uncompression interface
1492make use of filters. These filters are used to change the behaviour of
1493compression (and raw uncompression).
1494
1495All Lzma Filters are sub-classed from the C<Lzma::Filter> base-class.
1496
1497=head2 Lzma::Filter::Lzma
1498
1499The C<Lzma::Filter::Lzma> class is used to... TODO - more here
1500
1501There are two subclasses of C<Lzma::Filter::Lzma>, namely
1502C<Lzma::Filter::Lzma1> and C<Lzma::Filter::Lzma2>.
1503
1504The former is typically used with C<Compress::Raw::Lzma::AloneEncoder>.
1505The latter with C<Compress::Raw::Lzma::StreamEncoder>.
1506
1507When using Lzma filters an C<Lzma::Filter::Lzma> I<must> be included and it
1508I<must> be the last filter in the chain. There can only be one
1509C<Lzma::Filter::Lzma> filter in any filter chain.
1510
1511The C<Lzma::Filter::Lzma> construction takes the following options.
1512
1513=over 5
1514
1515=item DictSize => $value
1516
1517Dictionary size in bytes. This controls
1518how many bytes of the recently processed
1519uncompressed data is kept in memory. The size of the dictionary must be at
1520least C<LZMA_DICT_SIZE_MIN>.
1521
1522Defaults to C<LZMA_DICT_SIZE_DEFAULT>.
1523
1524=item PresetDict => $dict
1525
1526Provide an initial dictionary. This value is used to initialize the LZ77 history window.
1527
1528This feature only works correctly with raw encoding and decoding.
1529You may not be able to decode other formats that have been encoded with a preset dictionary.
1530
1531C<$dict> should contain typical strings that occur in the files being compressed,
1532with the most probably strings near the end fo the preset dictionary.
1533
1534If C<$dict> is larger than C<DictSize>, only the last C<DictSize> bytes are processed.
1535
1536=item Lc => $value
1537
1538Number of literal context bits.
1539
1540How many of the highest bits of the previous uncompressed
1541eight-bit byte (also known as `literal') are taken into
1542account when predicting the bits of the next literal.
1543
1544C<$value> must be a number between C<LZMA_LCLP_MIN> and
1545C<LZMA_LCLP_MAX>.
1546
1547Note the sum of the C<Lc> and C<Lp> options cannot exceed 4.
1548
1549Defaults to C<LZMA_LC_DEFAULT>.
1550
1551=item Lp => $value
1552
1553Number of literal position bits.
1554
1555How many of the lowest bits of the current position (number
1556of bytes from the beginning of the uncompressed data) in the
1557uncompressed data is taken into account when predicting the
1558bits of the next literal (a single eight-bit byte).
1559
1560Defaults to C<LZMA_LP_DEFAULT>.
1561
1562=item Pb => $value
1563
1564Number of position bits
1565
1566How many of the lowest bits of the current position in the
1567uncompressed data is taken into account when estimating
1568probabilities of matches. A match is a sequence of bytes for
1569which a matching sequence is found from the dictionary and
1570thus can be stored as distance-length pair.
1571
1572C<$value> must be a number between C<LZMA_PB_MIN> and
1573C<LZMA_PB_MAX>.
1574
1575Defaults to C<LZMA_PB_DEFAULT>.
1576
1577=item Mode => $value
1578
1579The Compression Mode. Valid values are C<LZMA_MODE_FAST> and
1580C<LZMA_MODE_NORMAL>.
1581
1582Defaults to C<LZMA_MODE_NORMAL>.
1583
1584=item Nice => $value
1585
1586Nice length of a match
1587
1588Defaults to 64.
1589
1590=item Mf => $value
1591
1592Defines which Match Finder to use. Valid values are C<LZMA_MF_HC3>
1593C<LZMA_MF_HC4>, C<LZMA_MF_BT2> C<LZMA_MF_BT3> and C<LZMA_MF_BT4>.
1594
1595Defaults to C<LZMA_MF_BT4>.
1596
1597=item Depth => $value
1598
1599Maximum search depth in the match finder.
1600
1601Defaults to 0.
1602
1603=back
1604
1605=head2 Lzma::Filter::BCJ
1606
1607The sub-classes of C<Lzma::Filter::BCJ> are the
1608Branch/Call/Jump conversion filters. These filters are used to rewrite
1609executable binary code for a number of processor architectures.
1610None of these classes take any options.
1611
1612=over 5
1613
1614=item Lzma::Filter::X86
1615
1616Filter for x86 binaries.
1617
1618=item Lzma::Filter::PowerPC
1619
1620Filter for Big endian PowerPC binaries.
1621
1622=item Lzma::Filter::IA64
1623
1624Filter for IA64 (Itanium) binaries.
1625
1626=item Lzma::Filter::ARM
1627
1628Filter for ARM binaries.
1629
1630=item Lzma::Filter::ARMThumb
1631
1632Filter for ARMThumb binaries.
1633
1634=item Lzma::Filter::Sparc
1635
1636Filter for Sparc binaries.
1637
1638=back
1639
1640=head2 Lzma::Filter::Delta
1641
1642Usage is
1643
1644    Lzma::Filter::Delta [OPTS]
1645
1646=over 5
1647
1648=item Type => $type
1649
1650Defines the type of Delta calculation. The only available type (and
1651therefore the default) is
1652C<LZMA_DELTA_TYPE_BYTE>,
1653
1654=item Distance => $value
1655
1656Defines the Delta Distance. C<$value> must be a number between
1657C<LZMA_DELTA_DIST_MIN> and C<LZMA_DELTA_DIST_MAX>.
1658
1659Default is C<LZMA_DELTA_DIST_MIN>.
1660
1661=back
1662
1663=head1 Misc
1664
1665=head2 my $version = Compress::Raw::Lzma::lzma_version_number();
1666
1667Returns the version of the underlying lzma library this module is using at
1668run-time as a number.
1669
1670=head2 my $version = Compress::Raw::Lzma::lzma_version_string();
1671
1672Returns the version of the underlying lzma library this module is using at
1673run-time as a string.
1674
1675=head2 my $version = Compress::Raw::Lzma::LZMA_VERSION();
1676
1677Returns the version of the underlying lzma library this module was using at
1678compile-time as a number.
1679
1680=head2 my $version = Compress::Raw::Lzma::LZMA_VERSION_STRING();
1681
1682Returns the version of the underlying lzma library this module was using at
1683compile-time as a string.
1684
1685=head1 Constants
1686
1687The following lzma constants are exported by this module
1688
1689TODO - more here
1690
1691=head1 SUPPORT
1692
1693General feedback/questions/bug reports should be sent to
1694L<https://github.com/pmqs/Compress-Raw-Lzma/issues> (preferred) or
1695L<https://rt.cpan.org/Public/Dist/Display.html?Name=Compress-Raw-Lzma>.
1696
1697=head1 SEE ALSO
1698
1699L<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>
1700
1701L<IO::Compress::FAQ|IO::Compress::FAQ>
1702
1703L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
1704L<Archive::Tar|Archive::Tar>,
1705L<IO::Zlib|IO::Zlib>
1706
1707=head1 AUTHOR
1708
1709This module was written by Paul Marquess, C<pmqs@cpan.org>.
1710
1711=head1 MODIFICATION HISTORY
1712
1713See the Changes file.
1714
1715=head1 COPYRIGHT AND LICENSE
1716
1717Copyright (c) 2005-2021 Paul Marquess. All rights reserved.
1718
1719This program is free software; you can redistribute it and/or
1720modify it under the same terms as Perl itself.
1721