2 * Author : Paul Marquess, <pmqs@cpan.org>
3 * Created : 5th October 2005
6 * Copyright (c) 2005-2007 Paul Marquess. All rights reserved.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the same terms as Perl itself.
20 # define NEED_sv_2pv_nolen
24 #if PERL_REVISION == 5 && (PERL_VERSION < 8 || (PERL_VERSION == 8 && PERL_SUBVERSION < 4 ))
26 # ifdef SvPVbyte_force
27 # undef SvPVbyte_force
30 # define SvPVbyte_force(sv,lp) SvPV_force(sv,lp)
34 #ifndef SvPVbyte_nolen
35 # define SvPVbyte_nolen SvPV_nolen
39 #if PERL_REVISION == 5 && (PERL_VERSION >= 8 || (PERL_VERSION == 8 && PERL_SUBVERSION < 4 ))
40 # define UTF8_AVAILABLE
43 typedef int DualType ;
44 typedef int int_undef ;
46 typedef unsigned long uLong;
47 typedef unsigned int uInt;
49 typedef struct di_stream {
51 #define FLAG_APPEND_OUTPUT 1
52 #define FLAG_CONSUME_INPUT 8
57 uLong compressedBytes ;
58 uLong uncompressedBytes ;
62 typedef di_stream * deflateStream ;
63 typedef di_stream * Compress__Raw__Bzip2 ;
65 typedef di_stream * inflateStream ;
66 typedef di_stream * Compress__Raw__Bunzip2 ;
68 #define COMPRESS_CLASS "Compress::Raw::Bzip2"
69 #define UNCOMPRESS_CLASS "Compress::Raw::Bunzip2"
71 #define ZMALLOC(to, typ) ((to = (typ *)safemalloc(sizeof(typ))), \
75 /* static const char * const my_z_errmsg[] = { */
76 static const char my_z_errmsg[][32] = {
77 "End of Stream", /* BZ_STREAM_END 4 */
78 "Finish OK", /* BZ_FINISH_OK 3 */
79 "Flush OK", /* BZ_FLUSH_OK 2 */
80 "Run OK", /* BZ_RUN_OK 1 */
82 "Sequence Error", /* BZ_SEQUENCE_ERROR (-1) */
83 "Param Error", /* BZ_PARAM_ERROR (-2) */
84 "Memory Error", /* BZ_MEM_ERROR (-3) */
85 "Data Error", /* BZ_DATA_ERROR (-4) */
86 "Magic Error", /* BZ_DATA_ERROR_MAGIC (-5) */
87 "IO Error", /* BZ_IO_ERROR (-6) */
88 "Unexpected EOF", /* BZ_UNEXPECTED_EOF (-7) */
89 "Output Buffer Full", /* BZ_OUTBUFF_FULL (-8) */
90 "Config Error", /* BZ_CONFIG_ERROR (-9) */
93 #define setDUALstatus(var, err) \
94 sv_setnv(var, (double)err) ; \
95 sv_setpv(var, ((err) ? GetErrorString(err) : "")) ; \
99 #if defined(__SYMBIAN32__)
100 # define NO_WRITEABLE_DATA
103 #define TRACE_DEFAULT 0
105 #ifdef NO_WRITEABLE_DATA
106 # define trace TRACE_DEFAULT
108 static int trace = TRACE_DEFAULT ;
111 /* Dodge PerlIO hiding of these functions. */
115 #define getInnerObject(x) (*av_fetch((AV*)SvRV(x), 0, FALSE))
117 #define getInnerObject(x) ((SV*)SvRV(sv))
121 void bz_internal_error(int errorcode)
123 croak("bz_internal_error %d\n", errorcode);
129 GetErrorString(int error_no)
131 GetErrorString(error_no)
139 if (error_no == BZ_ERRNO) {
140 errstr = Strerror(errno) ;
144 errstr = (char*) my_z_errmsg[4 - error_no];
151 DispHex(void * ptr, int length)
158 char * p = (char*)ptr;
160 for (i = 0; i < length; ++i) {
161 printf(" %02x", 0xFF & *(p+i));
168 DispStream(di_stream * s, char * message)
170 DispStream(s, message)
181 #define EnDis(f) (s->flags & f ? "Enabled" : "Disabled")
183 printf("DispStream 0x%p", s) ;
185 printf("- %s \n", message) ;
189 printf(" stream pointer is NULL\n");
192 printf(" stream 0x%p\n", &(s->stream));
193 printf(" opaque 0x%p\n", s->stream.opaque);
194 printf(" next_in 0x%p", s->stream.next_in);
195 if (s->stream.next_in){
197 DispHex(s->stream.next_in, 4);
201 printf(" next_out 0x%p", s->stream.next_out);
202 if (s->stream.next_out){
204 DispHex(s->stream.next_out, 4);
208 printf(" avail_in %lu\n", (unsigned long)s->stream.avail_in);
209 printf(" avail_out %lu\n", (unsigned long)s->stream.avail_out);
210 printf(" bufsize %lu\n", (unsigned long)s->bufsize);
211 printf(" flags 0x%x\n", s->flags);
212 printf(" APPEND %s\n", EnDis(FLAG_APPEND_OUTPUT));
213 printf(" CONSUME %s\n", EnDis(FLAG_CONSUME_INPUT));
229 ZMALLOC(s, di_stream) ;
237 PostInitStream(di_stream * s, int flags)
239 PostInitStream(s, flags)
244 s->bufsize = 1024 * 16 ;
252 deRef(SV * sv, const char * string)
269 croak("%s: buffer parameter is not a SCALAR reference", string);
274 croak("%s: buffer parameter is a reference to a reference", string) ;
286 deRef_l(SV * sv, const char * string)
308 croak("%s: buffer parameter is not a SCALAR reference", string);
313 croak("%s: buffer parameter is a reference to a reference", string) ;
316 if (SvREADONLY(sv) && PL_curcop != &PL_compiling)
317 croak("%s: buffer parameter is read-only", string);
319 SvUPGRADE(sv, SVt_PV);
331 #include "constants.h"
333 MODULE = Compress::Raw::Bzip2 PACKAGE = Compress::Raw::Bzip2 PREFIX = Zip_
338 INCLUDE: constants.xs
341 /* Check this version of bzip2 is == 1 */
342 if (BZ2_bzlibVersion()[0] != '1')
343 croak(COMPRESS_CLASS " needs bzip2 version 1.x, you have %s\n", BZ2_bzlibVersion()) ;
346 MODULE = Compress::Raw::Bzip2 PACKAGE = Compress::Raw::Bzip2
348 #define bzlibversion() BZ2_bzlibVersion()
353 new(class, appendOut=1, blockSize100k=1, workfactor=0, verbosity=0)
365 warn("in Compress::Raw::Bzip2::_new(items=%d,appendOut=%d, blockSize100k=%d, workfactor=%d, verbosity=%d\n",
366 items, appendOut, blockSize100k, workfactor, verbosity);
368 if ((s = InitStream() )) {
370 err = BZ2_bzCompressInit ( &(s->stream),
382 flags |= FLAG_APPEND_OUTPUT;
383 PostInitStream(s, appendOut ? FLAG_APPEND_OUTPUT :0) ;
390 SV* obj = sv_setref_pv(sv_newmortal(), class, (void*)s);
395 SV* obj = sv_2mortal(newSViv(PTR2IV(s))) ;
398 if (GIMME == G_ARRAY) {
399 SV * sv = sv_2mortal(newSViv(err)) ;
400 setDUALstatus(sv, err);
405 MODULE = Compress::Raw::Bunzip2 PACKAGE = Compress::Raw::Bunzip2
408 new(class, appendOut=1 , consume=1, small=0, verbosity=0)
420 warn("in _inflateInit(windowBits=%d, bufsize=%lu, dictionary=%lu\n",
421 windowBits, bufsize, (unsigned long)SvCUR(dictionary)) ;
423 if ((s = InitStream() )) {
425 err = BZ2_bzDecompressInit (&(s->stream), verbosity, small);
433 flags |= FLAG_APPEND_OUTPUT;
435 flags |= FLAG_CONSUME_INPUT;
436 PostInitStream(s, flags) ;
443 SV* obj = sv_setref_pv(sv_newmortal(), class, (void*)s);
448 SV* obj = sv_2mortal(newSViv(PTR2IV(s))) ;
451 if (GIMME == G_ARRAY) {
452 SV * sv = sv_2mortal(newSViv(err)) ;
453 setDUALstatus(sv, err);
460 MODULE = Compress::Raw::Bzip2 PACKAGE = Compress::Raw::Bzip2
463 DispStream(s, message=NULL)
464 Compress::Raw::Bzip2 s
468 bzdeflate (s, buf, output)
469 Compress::Raw::Bzip2 s
472 uInt cur_length = NO_INIT
473 uInt increment = NO_INIT
475 uInt bufinc = NO_INIT
479 /* If the input buffer is a reference, dereference it */
480 buf = deRef(buf, "deflate") ;
482 /* initialise the input buffer */
483 #ifdef UTF8_AVAILABLE
484 if (DO_UTF8(buf) && !sv_utf8_downgrade(buf, 1))
485 croak("Wide character in " COMPRESS_CLASS "::bzdeflate input parameter");
487 s->stream.next_in = (char*)SvPVbyte_nolen(buf) ;
488 s->stream.avail_in = SvCUR(buf) ;
490 /* and retrieve the output buffer */
491 output = deRef_l(output, "deflate") ;
492 #ifdef UTF8_AVAILABLE
493 if (DO_UTF8(output) && !sv_utf8_downgrade(output, 1))
494 croak("Wide character in " COMPRESS_CLASS "::bzdeflate output parameter");
497 if((s->flags & FLAG_APPEND_OUTPUT) != FLAG_APPEND_OUTPUT) {
498 SvCUR_set(output, 0);
499 /* sv_setpvn(output, "", 0); */
501 cur_length = SvCUR(output) ;
502 s->stream.next_out = (char*) SvPVbyte_nolen(output) + cur_length;
503 increment = SvLEN(output) - cur_length;
504 s->stream.avail_out = increment;
505 while (s->stream.avail_in != 0) {
507 if (s->stream.avail_out == 0) {
508 /* out of space in the output buffer so make it bigger */
509 Sv_Grow(output, SvLEN(output) + bufinc) ;
510 cur_length += increment ;
511 s->stream.next_out = (char*) SvPVbyte_nolen(output) + cur_length ;
513 s->stream.avail_out = increment;
517 RETVAL = BZ2_bzCompress(&(s->stream), BZ_RUN);
518 if (RETVAL != BZ_RUN_OK)
522 s->compressedBytes += cur_length + increment - s->stream.avail_out ;
523 s->uncompressedBytes += SvCUR(buf) - s->stream.avail_in ;
525 s->last_error = RETVAL ;
526 if (RETVAL == BZ_RUN_OK) {
528 SvCUR_set(output, cur_length + increment - s->stream.avail_out) ;
537 Compress::Raw::Bzip2 s
539 BZ2_bzCompressEnd(&s->stream) ;
545 Compress::Raw::Bzip2 s
547 uInt cur_length = NO_INIT
548 uInt increment = NO_INIT
549 uInt bufinc = NO_INIT
553 s->stream.avail_in = 0; /* should be zero already anyway */
555 /* retrieve the output buffer */
556 output = deRef_l(output, "close") ;
557 #ifdef UTF8_AVAILABLE
558 if (DO_UTF8(output) && !sv_utf8_downgrade(output, 1))
559 croak("Wide character in " COMPRESS_CLASS "::bzclose input parameter");
561 if(! s->flags & FLAG_APPEND_OUTPUT) {
562 SvCUR_set(output, 0);
563 /* sv_setpvn(output, "", 0); */
565 cur_length = SvCUR(output) ;
566 s->stream.next_out = (char*) SvPVbyte_nolen(output) + cur_length;
567 increment = SvLEN(output) - cur_length;
568 s->stream.avail_out = increment;
571 if (s->stream.avail_out == 0) {
572 /* consumed all the available output, so extend it */
573 Sv_Grow(output, SvLEN(output) + bufinc) ;
574 cur_length += increment ;
575 s->stream.next_out = (char*) SvPVbyte_nolen(output) + cur_length ;
577 s->stream.avail_out = increment;
580 RETVAL = BZ2_bzCompress(&(s->stream), BZ_FINISH);
582 /* deflate has finished flushing only when it hasn't used up
583 * all the available space in the output buffer:
585 /* if (s->stream.avail_out != 0 || RETVAL < 0 ) */
586 if (RETVAL == BZ_STREAM_END || RETVAL < 0 )
590 /* RETVAL = (RETVAL == BZ_STREAM_END ? BZ_OK : RETVAL) ; */
591 s->last_error = RETVAL ;
593 s->compressedBytes += cur_length + increment - s->stream.avail_out ;
595 if (RETVAL == BZ_STREAM_END) {
597 SvCUR_set(output, cur_length + increment - s->stream.avail_out) ;
606 Compress::Raw::Bzip2 s
608 uInt cur_length = NO_INIT
609 uInt increment = NO_INIT
610 uInt bufinc = NO_INIT
614 s->stream.avail_in = 0; /* should be zero already anyway */
616 /* retrieve the output buffer */
617 output = deRef_l(output, "close") ;
618 #ifdef UTF8_AVAILABLE
619 if (DO_UTF8(output) && !sv_utf8_downgrade(output, 1))
620 croak("Wide character in " COMPRESS_CLASS "::bzflush input parameter");
622 if(! s->flags & FLAG_APPEND_OUTPUT) {
623 SvCUR_set(output, 0);
624 /* sv_setpvn(output, "", 0); */
626 cur_length = SvCUR(output) ;
627 s->stream.next_out = (char*) SvPVbyte_nolen(output) + cur_length;
628 increment = SvLEN(output) - cur_length;
629 s->stream.avail_out = increment;
632 if (s->stream.avail_out == 0) {
633 /* consumed all the available output, so extend it */
634 Sv_Grow(output, SvLEN(output) + bufinc) ;
635 cur_length += increment ;
636 s->stream.next_out = (char*) SvPVbyte_nolen(output) + cur_length ;
638 s->stream.avail_out = increment;
641 RETVAL = BZ2_bzCompress(&(s->stream), BZ_FLUSH);
643 if (RETVAL == BZ_RUN_OK || RETVAL < 0)
646 /* deflate has finished flushing only when it hasn't used up
647 * all the available space in the output buffer:
649 /* RETVAL == if (s->stream.avail_out != 0 || RETVAL < 0 )
653 /* RETVAL = (RETVAL == BZ_STREAM_END ? BZ_OK : RETVAL) ; */
654 s->last_error = RETVAL ;
656 s->compressedBytes += cur_length + increment - s->stream.avail_out ;
658 if (RETVAL == BZ_RUN_OK) {
660 SvCUR_set(output, cur_length + increment - s->stream.avail_out) ;
668 Compress::Raw::Bzip2 s
670 RETVAL = s->stream.total_in_lo32 ;
676 Compress::Raw::Bzip2 s
678 RETVAL = s->stream.total_out_lo32 ;
684 Compress::Raw::Bzip2 s
686 RETVAL = s->compressedBytes;
692 Compress::Raw::Bzip2 s
694 RETVAL = s->uncompressedBytes;
699 MODULE = Compress::Raw::Bunzip2 PACKAGE = Compress::Raw::Bunzip2
702 DispStream(s, message=NULL)
703 Compress::Raw::Bunzip2 s
707 bzinflate (s, buf, output)
708 Compress::Raw::Bunzip2 s
712 uInt prefix_length = 0;
714 STRLEN stmp = NO_INIT
715 uInt bufinc = NO_INIT
717 #ifdef UTF8_AVAILABLE
718 bool out_utf8 = FALSE;
722 /* If the buffer is a reference, dereference it */
723 buf = deRef(buf, "inflate") ;
725 if (s->flags & FLAG_CONSUME_INPUT && SvREADONLY(buf))
726 croak(UNCOMPRESS_CLASS "::bzinflate input parameter cannot be read-only when ConsumeInput is specified");
727 #ifdef UTF8_AVAILABLE
728 if (DO_UTF8(buf) && !sv_utf8_downgrade(buf, 1))
729 croak("Wide character in " UNCOMPRESS_CLASS "::bzinflate input parameter");
732 /* initialise the input buffer */
733 s->stream.next_in = (char*)SvPVbyte_force(buf, stmp) ;
734 s->stream.avail_in = SvCUR(buf);
736 /* and retrieve the output buffer */
737 output = deRef_l(output, "inflate") ;
738 #ifdef UTF8_AVAILABLE
741 if (DO_UTF8(output) && !sv_utf8_downgrade(output, 1))
742 croak("Wide character in " UNCOMPRESS_CLASS "::bzinflate output parameter");
744 if((s->flags & FLAG_APPEND_OUTPUT) != FLAG_APPEND_OUTPUT) {
745 SvCUR_set(output, 0);
748 prefix_length = cur_length = SvCUR(output) ;
749 s->stream.next_out = (char*) SvPVbyte_nolen(output) + cur_length;
750 increment = SvLEN(output) - cur_length - 1;
751 s->stream.avail_out = increment;
754 s->stream.avail_out = 0;
756 s->bytesInflated = 0;
760 if (s->stream.avail_out == 0) {
761 /* out of space in the output buffer so make it bigger */
762 Sv_Grow(output, SvLEN(output) + bufinc) ;
763 cur_length += increment ;
764 s->stream.next_out = (char*) SvPVbyte_nolen(output) + cur_length ;
766 s->stream.avail_out = increment;
770 RETVAL = BZ2_bzDecompress (&(s->stream));
775 if (s->stream.avail_out == 0)
778 if (s->stream.avail_in == 0) {
785 s->last_error = RETVAL ;
786 if (RETVAL == BZ_OK || RETVAL == BZ_STREAM_END) {
789 s->bytesInflated = cur_length + increment - s->stream.avail_out - prefix_length;
790 s->uncompressedBytes += s->bytesInflated ;
791 s->compressedBytes += SvCUR(buf) - s->stream.avail_in ;
794 SvCUR_set(output, prefix_length + s->bytesInflated) ;
795 *SvEND(output) = '\0';
796 #ifdef UTF8_AVAILABLE
798 sv_utf8_upgrade(output);
802 /* fix the input buffer */
803 if (s->flags & FLAG_CONSUME_INPUT) {
804 in = s->stream.avail_in ;
807 Move(s->stream.next_in, SvPVbyte_nolen(buf), in, char) ;
817 Compress::Raw::Bunzip2 s
819 RETVAL = s->bytesInflated;
826 Compress::Raw::Bunzip2 s
828 BZ2_bzDecompressEnd(&s->stream) ;
834 Compress::Raw::Bunzip2 s
836 RETVAL = s->last_error ;
842 Compress::Raw::Bunzip2 s
844 RETVAL = s->stream.total_in_lo32 ;
850 Compress::Raw::Bunzip2 s
852 RETVAL = s->stream.total_out_lo32 ;
858 Compress::Raw::Bunzip2 s
860 RETVAL = s->compressedBytes;
866 Compress::Raw::Bunzip2 s
868 RETVAL = s->uncompressedBytes;
872 MODULE = Compress::Raw::Bzip2 PACKAGE = Compress::Raw::Bzip2 PREFIX = Zip_