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
53 #define FLAG_LIMIT_OUTPUT 16
58 uLong compressedBytes ;
59 uLong uncompressedBytes ;
63 typedef di_stream * deflateStream ;
64 typedef di_stream * Compress__Raw__Bzip2 ;
66 typedef di_stream * inflateStream ;
67 typedef di_stream * Compress__Raw__Bunzip2 ;
69 #define COMPRESS_CLASS "Compress::Raw::Bzip2"
70 #define UNCOMPRESS_CLASS "Compress::Raw::Bunzip2"
72 #define ZMALLOC(to, typ) ((to = (typ *)safemalloc(sizeof(typ))), \
76 /* static const char * const my_z_errmsg[] = { */
77 static const char my_z_errmsg[][32] = {
78 "End of Stream", /* BZ_STREAM_END 4 */
79 "Finish OK", /* BZ_FINISH_OK 3 */
80 "Flush OK", /* BZ_FLUSH_OK 2 */
81 "Run OK", /* BZ_RUN_OK 1 */
83 "Sequence Error", /* BZ_SEQUENCE_ERROR (-1) */
84 "Param Error", /* BZ_PARAM_ERROR (-2) */
85 "Memory Error", /* BZ_MEM_ERROR (-3) */
86 "Data Error", /* BZ_DATA_ERROR (-4) */
87 "Magic Error", /* BZ_DATA_ERROR_MAGIC (-5) */
88 "IO Error", /* BZ_IO_ERROR (-6) */
89 "Unexpected EOF", /* BZ_UNEXPECTED_EOF (-7) */
90 "Output Buffer Full", /* BZ_OUTBUFF_FULL (-8) */
91 "Config Error", /* BZ_CONFIG_ERROR (-9) */
94 #define setDUALstatus(var, err) \
95 sv_setnv(var, (double)err) ; \
96 sv_setpv(var, ((err) ? GetErrorString(err) : "")) ; \
100 #if defined(__SYMBIAN32__)
101 # define NO_WRITEABLE_DATA
104 #define TRACE_DEFAULT 0
106 #ifdef NO_WRITEABLE_DATA
107 # define trace TRACE_DEFAULT
109 static int trace = TRACE_DEFAULT ;
112 /* Dodge PerlIO hiding of these functions. */
116 #define getInnerObject(x) (*av_fetch((AV*)SvRV(x), 0, FALSE))
118 #define getInnerObject(x) ((SV*)SvRV(sv))
122 void bz_internal_error(int errorcode)
124 croak("bz_internal_error %d\n", errorcode);
130 GetErrorString(int error_no)
132 GetErrorString(error_no)
140 if (error_no == BZ_ERRNO) {
141 errstr = Strerror(errno) ;
145 errstr = (char*) my_z_errmsg[4 - error_no];
152 DispHex(void * ptr, int length)
159 char * p = (char*)ptr;
161 for (i = 0; i < length; ++i) {
162 printf(" %02x", 0xFF & *(p+i));
169 DispStream(di_stream * s, char * message)
171 DispStream(s, message)
182 #define EnDis(f) (s->flags & f ? "Enabled" : "Disabled")
184 printf("DispStream 0x%p", s) ;
186 printf(" - %s \n", message) ;
190 printf(" stream pointer is NULL\n");
193 printf(" stream 0x%p\n", &(s->stream));
194 printf(" opaque 0x%p\n", s->stream.opaque);
195 printf(" state 0x%p\n", s->stream.state );
196 printf(" next_in 0x%p", s->stream.next_in);
197 if (s->stream.next_in){
199 DispHex(s->stream.next_in, 4);
203 printf(" next_out 0x%p", s->stream.next_out);
204 if (s->stream.next_out){
206 DispHex(s->stream.next_out, 4);
210 printf(" avail_in %lu\n", (unsigned long)s->stream.avail_in);
211 printf(" avail_out %lu\n", (unsigned long)s->stream.avail_out);
212 printf(" bufsize %lu\n", (unsigned long)s->bufsize);
213 printf(" total_in_lo32 %u\n", s->stream.total_in_lo32);
214 printf(" total_in_hi32 %u\n", s->stream.total_in_hi32);
215 printf(" total_out_lo32 %u\n", s->stream.total_out_lo32);
216 printf(" total_out_hi32 %u\n", s->stream.total_out_hi32);
217 printf(" flags 0x%x\n", s->flags);
218 printf(" APPEND %s\n", EnDis(FLAG_APPEND_OUTPUT));
219 printf(" CONSUME %s\n", EnDis(FLAG_CONSUME_INPUT));
220 printf(" LIMIT %s\n", EnDis(FLAG_LIMIT_OUTPUT));
236 ZMALLOC(s, di_stream) ;
244 PostInitStream(di_stream * s, int flags)
246 PostInitStream(s, flags)
251 s->bufsize = 1024 * 16 ;
259 deRef(SV * sv, const char * string)
276 croak("%s: buffer parameter is not a SCALAR reference", string);
281 croak("%s: buffer parameter is a reference to a reference", string) ;
293 deRef_l(SV * sv, const char * string)
315 croak("%s: buffer parameter is not a SCALAR reference", string);
320 croak("%s: buffer parameter is a reference to a reference", string) ;
323 if (SvREADONLY(sv) && PL_curcop != &PL_compiling)
324 croak("%s: buffer parameter is read-only", string);
326 SvUPGRADE(sv, SVt_PV);
338 #include "constants.h"
340 MODULE = Compress::Raw::Bzip2 PACKAGE = Compress::Raw::Bzip2 PREFIX = Zip_
345 INCLUDE: constants.xs
348 #ifndef NO_WRITEABLE_DATA
349 trace = TRACE_DEFAULT ;
351 /* Check this version of bzip2 is == 1 */
352 if (BZ2_bzlibVersion()[0] != '1')
353 croak(COMPRESS_CLASS " needs bzip2 version 1.x, you have %s\n", BZ2_bzlibVersion()) ;
356 MODULE = Compress::Raw::Bzip2 PACKAGE = Compress::Raw::Bzip2
358 #define bzlibversion() BZ2_bzlibVersion()
363 new(className, appendOut=1, blockSize100k=1, workfactor=0, verbosity=0)
364 const char * className
375 warn("in Compress::Raw::Bzip2::_new(items=%d,appendOut=%d, blockSize100k=%d, workfactor=%d, verbosity=%d\n",
376 items, appendOut, blockSize100k, workfactor, verbosity);
378 if ((s = InitStream() )) {
380 err = BZ2_bzCompressInit ( &(s->stream),
392 flags |= FLAG_APPEND_OUTPUT;
393 PostInitStream(s, appendOut ? FLAG_APPEND_OUTPUT :0) ;
400 SV* obj = sv_setref_pv(sv_newmortal(), className, (void*)s);
405 SV* obj = sv_2mortal(newSViv(PTR2IV(s))) ;
408 if (GIMME == G_ARRAY) {
409 SV * sv = sv_2mortal(newSViv(err)) ;
410 setDUALstatus(sv, err);
415 MODULE = Compress::Raw::Bunzip2 PACKAGE = Compress::Raw::Bunzip2
418 new(className, appendOut=1 , consume=1, small=0, verbosity=0, limitOutput=0)
419 const char* className
431 warn("in _inflateInit(windowBits=%d, bufsize=%lu, dictionary=%lu\n",
432 windowBits, bufsize, (unsigned long)SvCUR(dictionary)) ;
434 if ((s = InitStream() )) {
436 err = BZ2_bzDecompressInit (&(s->stream), verbosity, small);
444 flags |= FLAG_APPEND_OUTPUT;
446 flags |= FLAG_CONSUME_INPUT;
448 flags |= (FLAG_LIMIT_OUTPUT|FLAG_CONSUME_INPUT);
449 PostInitStream(s, flags) ;
456 SV* obj = sv_setref_pv(sv_newmortal(), className, (void*)s);
461 SV* obj = sv_2mortal(newSViv(PTR2IV(s))) ;
464 if (GIMME == G_ARRAY) {
465 SV * sv = sv_2mortal(newSViv(err)) ;
466 setDUALstatus(sv, err);
473 MODULE = Compress::Raw::Bzip2 PACKAGE = Compress::Raw::Bzip2
476 DispStream(s, message=NULL)
477 Compress::Raw::Bzip2 s
481 bzdeflate (s, buf, output)
482 Compress::Raw::Bzip2 s
485 uInt cur_length = NO_INIT
486 uInt increment = NO_INIT
488 uInt bufinc = NO_INIT
492 /* If the input buffer is a reference, dereference it */
493 buf = deRef(buf, "deflate") ;
495 /* initialise the input buffer */
496 #ifdef UTF8_AVAILABLE
497 if (DO_UTF8(buf) && !sv_utf8_downgrade(buf, 1))
498 croak("Wide character in " COMPRESS_CLASS "::bzdeflate input parameter");
500 s->stream.next_in = (char*)SvPVbyte_nolen(buf) ;
501 s->stream.avail_in = SvCUR(buf) ;
503 /* and retrieve the output buffer */
504 output = deRef_l(output, "deflate") ;
505 #ifdef UTF8_AVAILABLE
506 if (DO_UTF8(output) && !sv_utf8_downgrade(output, 1))
507 croak("Wide character in " COMPRESS_CLASS "::bzdeflate output parameter");
510 if((s->flags & FLAG_APPEND_OUTPUT) != FLAG_APPEND_OUTPUT) {
511 SvCUR_set(output, 0);
512 /* sv_setpvn(output, "", 0); */
514 cur_length = SvCUR(output) ;
515 s->stream.next_out = (char*) SvPVbyte_nolen(output) + cur_length;
516 increment = SvLEN(output) - cur_length;
517 s->stream.avail_out = increment;
518 while (s->stream.avail_in != 0) {
520 if (s->stream.avail_out == 0) {
521 /* out of space in the output buffer so make it bigger */
522 Sv_Grow(output, SvLEN(output) + bufinc) ;
523 cur_length += increment ;
524 s->stream.next_out = (char*) SvPVbyte_nolen(output) + cur_length ;
526 s->stream.avail_out = increment;
530 RETVAL = BZ2_bzCompress(&(s->stream), BZ_RUN);
531 if (RETVAL != BZ_RUN_OK)
535 s->compressedBytes += cur_length + increment - s->stream.avail_out ;
536 s->uncompressedBytes += SvCUR(buf) - s->stream.avail_in ;
538 s->last_error = RETVAL ;
539 if (RETVAL == BZ_RUN_OK) {
541 SvCUR_set(output, cur_length + increment - s->stream.avail_out) ;
550 Compress::Raw::Bzip2 s
552 BZ2_bzCompressEnd(&s->stream) ;
558 Compress::Raw::Bzip2 s
560 uInt cur_length = NO_INIT
561 uInt increment = NO_INIT
562 uInt bufinc = NO_INIT
566 s->stream.avail_in = 0; /* should be zero already anyway */
568 /* retrieve the output buffer */
569 output = deRef_l(output, "close") ;
570 #ifdef UTF8_AVAILABLE
571 if (DO_UTF8(output) && !sv_utf8_downgrade(output, 1))
572 croak("Wide character in " COMPRESS_CLASS "::bzclose input parameter");
574 if(! s->flags & FLAG_APPEND_OUTPUT) {
575 SvCUR_set(output, 0);
576 /* sv_setpvn(output, "", 0); */
578 cur_length = SvCUR(output) ;
579 s->stream.next_out = (char*) SvPVbyte_nolen(output) + cur_length;
580 increment = SvLEN(output) - cur_length;
581 s->stream.avail_out = increment;
584 if (s->stream.avail_out == 0) {
585 /* consumed all the available output, so extend it */
586 Sv_Grow(output, SvLEN(output) + bufinc) ;
587 cur_length += increment ;
588 s->stream.next_out = (char*) SvPVbyte_nolen(output) + cur_length ;
590 s->stream.avail_out = increment;
593 RETVAL = BZ2_bzCompress(&(s->stream), BZ_FINISH);
595 /* deflate has finished flushing only when it hasn't used up
596 * all the available space in the output buffer:
598 /* if (s->stream.avail_out != 0 || RETVAL < 0 ) */
599 if (RETVAL == BZ_STREAM_END || RETVAL < 0 )
603 /* RETVAL = (RETVAL == BZ_STREAM_END ? BZ_OK : RETVAL) ; */
604 s->last_error = RETVAL ;
606 s->compressedBytes += cur_length + increment - s->stream.avail_out ;
608 if (RETVAL == BZ_STREAM_END) {
610 SvCUR_set(output, cur_length + increment - s->stream.avail_out) ;
619 Compress::Raw::Bzip2 s
621 uInt cur_length = NO_INIT
622 uInt increment = NO_INIT
623 uInt bufinc = NO_INIT
627 s->stream.avail_in = 0; /* should be zero already anyway */
629 /* retrieve the output buffer */
630 output = deRef_l(output, "close") ;
631 #ifdef UTF8_AVAILABLE
632 if (DO_UTF8(output) && !sv_utf8_downgrade(output, 1))
633 croak("Wide character in " COMPRESS_CLASS "::bzflush input parameter");
635 if(! s->flags & FLAG_APPEND_OUTPUT) {
636 SvCUR_set(output, 0);
637 /* sv_setpvn(output, "", 0); */
639 cur_length = SvCUR(output) ;
640 s->stream.next_out = (char*) SvPVbyte_nolen(output) + cur_length;
641 increment = SvLEN(output) - cur_length;
642 s->stream.avail_out = increment;
645 if (s->stream.avail_out == 0) {
646 /* consumed all the available output, so extend it */
647 Sv_Grow(output, SvLEN(output) + bufinc) ;
648 cur_length += increment ;
649 s->stream.next_out = (char*) SvPVbyte_nolen(output) + cur_length ;
651 s->stream.avail_out = increment;
654 RETVAL = BZ2_bzCompress(&(s->stream), BZ_FLUSH);
656 if (RETVAL == BZ_RUN_OK || RETVAL < 0)
659 /* deflate has finished flushing only when it hasn't used up
660 * all the available space in the output buffer:
662 /* RETVAL == if (s->stream.avail_out != 0 || RETVAL < 0 )
666 /* RETVAL = (RETVAL == BZ_STREAM_END ? BZ_OK : RETVAL) ; */
667 s->last_error = RETVAL ;
669 s->compressedBytes += cur_length + increment - s->stream.avail_out ;
671 if (RETVAL == BZ_RUN_OK) {
673 SvCUR_set(output, cur_length + increment - s->stream.avail_out) ;
681 Compress::Raw::Bzip2 s
683 RETVAL = s->stream.total_in_lo32 ;
689 Compress::Raw::Bzip2 s
691 RETVAL = s->stream.total_out_lo32 ;
697 Compress::Raw::Bzip2 s
699 RETVAL = s->compressedBytes;
705 Compress::Raw::Bzip2 s
707 RETVAL = s->uncompressedBytes;
712 MODULE = Compress::Raw::Bunzip2 PACKAGE = Compress::Raw::Bunzip2
715 DispStream(s, message=NULL)
716 Compress::Raw::Bunzip2 s
720 bzinflate (s, buf, output)
721 Compress::Raw::Bunzip2 s
725 uInt prefix_length = 0;
727 STRLEN stmp = NO_INIT
728 uInt bufinc = NO_INIT
730 #ifdef UTF8_AVAILABLE
731 bool out_utf8 = FALSE;
735 /* If the buffer is a reference, dereference it */
736 buf = deRef(buf, "bzinflate") ;
738 if (s->flags & FLAG_CONSUME_INPUT && SvREADONLY(buf))
739 croak(UNCOMPRESS_CLASS "::bzinflate input parameter cannot be read-only when ConsumeInput is specified");
740 #ifdef UTF8_AVAILABLE
741 if (DO_UTF8(buf) && !sv_utf8_downgrade(buf, 1))
742 croak("Wide character in " UNCOMPRESS_CLASS "::bzinflate input parameter");
745 /* initialise the input buffer */
746 s->stream.next_in = (char*)SvPVbyte_force(buf, stmp) ;
747 s->stream.avail_in = SvCUR(buf);
749 /* and retrieve the output buffer */
750 output = deRef_l(output, "bzinflate") ;
751 #ifdef UTF8_AVAILABLE
754 if (DO_UTF8(output) && !sv_utf8_downgrade(output, 1))
755 croak("Wide character in " UNCOMPRESS_CLASS "::bzinflate output parameter");
757 if((s->flags & FLAG_APPEND_OUTPUT) != FLAG_APPEND_OUTPUT) {
758 SvCUR_set(output, 0);
761 /* Assume no output buffer - the code below will update if there is any available */
762 s->stream.avail_out = 0;
765 prefix_length = cur_length = SvCUR(output) ;
767 if (s->flags & FLAG_LIMIT_OUTPUT && SvLEN(output) - cur_length - 1 < bufinc)
769 Sv_Grow(output, bufinc + cur_length + 1) ;
772 /* Only setup the stream output pointers if there is spare
773 capacity in the outout SV
775 if (SvLEN(output) > cur_length + 1)
777 s->stream.next_out = (char*) SvPVbyte_nolen(output) + cur_length;
778 increment = SvLEN(output) - cur_length - 1;
779 s->stream.avail_out = increment;
783 s->bytesInflated = 0;
789 if (s->stream.avail_out == 0) {
790 /* out of space in the output buffer so make it bigger */
791 Sv_Grow(output, SvLEN(output) + bufinc + 1) ;
792 cur_length += increment ;
793 s->stream.next_out = (char*) SvPVbyte_nolen(output) + cur_length ;
795 s->stream.avail_out = increment;
799 /* DispStream(s, "pre"); */
800 RETVAL = BZ2_bzDecompress (&(s->stream));
802 /* DispStream(s, "apres"); */
803 if (RETVAL != BZ_OK || s->flags & FLAG_LIMIT_OUTPUT)
806 if (s->stream.avail_out == 0)
809 if (s->stream.avail_in == 0) {
816 s->last_error = RETVAL ;
817 if (RETVAL == BZ_OK || RETVAL == BZ_STREAM_END) {
820 s->bytesInflated = cur_length + increment - s->stream.avail_out - prefix_length;
821 s->uncompressedBytes += s->bytesInflated ;
822 s->compressedBytes += SvCUR(buf) - s->stream.avail_in ;
825 SvCUR_set(output, prefix_length + s->bytesInflated) ;
826 *SvEND(output) = '\0';
827 #ifdef UTF8_AVAILABLE
829 sv_utf8_upgrade(output);
833 /* fix the input buffer */
834 if (s->flags & FLAG_CONSUME_INPUT) {
835 in = s->stream.avail_in ;
838 Move(s->stream.next_in, SvPVbyte_nolen(buf), in, char) ;
848 Compress::Raw::Bunzip2 s
850 RETVAL = s->bytesInflated;
857 Compress::Raw::Bunzip2 s
859 BZ2_bzDecompressEnd(&s->stream) ;
865 Compress::Raw::Bunzip2 s
867 RETVAL = s->last_error ;
873 Compress::Raw::Bunzip2 s
875 RETVAL = s->stream.total_in_lo32 ;
881 Compress::Raw::Bunzip2 s
883 RETVAL = s->stream.total_out_lo32 ;
889 Compress::Raw::Bunzip2 s
891 RETVAL = s->compressedBytes;
897 Compress::Raw::Bunzip2 s
899 RETVAL = s->uncompressedBytes;
903 MODULE = Compress::Raw::Bzip2 PACKAGE = Compress::Raw::Bzip2 PREFIX = Zip_