2 * Author : Paul Marquess, <pmqs@cpan.org>
3 * Created : 5th October 2005
6 * Copyright (c) 2005-2010 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.
12 #define PERL_NO_GET_CONTEXT
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 /* Set TRACE_DEFAULT to a non-zero value to enable tracing */
105 #define TRACE_DEFAULT 0
107 #if defined(NO_WRITEABLE_DATA) || TRACE_DEFAULT == 0
108 # define trace TRACE_DEFAULT
110 static int trace = TRACE_DEFAULT ;
113 /* Dodge PerlIO hiding of these functions. */
117 #define getInnerObject(x) (*av_fetch((AV*)SvRV(x), 0, FALSE))
119 #define getInnerObject(x) ((SV*)SvRV(sv))
123 void bz_internal_error(int errorcode)
125 croak("bz_internal_error %d\n", errorcode);
131 GetErrorString(int error_no)
133 GetErrorString(error_no)
137 return(char*) my_z_errmsg[4 - error_no];
142 DispHex(void * ptr, int length)
149 char * p = (char*)ptr;
151 for (i = 0; i < length; ++i) {
152 printf(" %02x", 0xFF & *(p+i));
159 DispStream(di_stream * s, const char * message)
161 DispStream(s, message)
163 const char * message;
172 #define EnDis(f) (s->flags & f ? "Enabled" : "Disabled")
174 printf("DispStream 0x%p", s) ;
176 printf(" - %s \n", message) ;
180 printf(" stream pointer is NULL\n");
183 printf(" stream 0x%p\n", &(s->stream));
184 printf(" opaque 0x%p\n", s->stream.opaque);
185 printf(" state 0x%p\n", s->stream.state );
186 printf(" next_in 0x%p", s->stream.next_in);
187 if (s->stream.next_in){
189 DispHex(s->stream.next_in, 4);
193 printf(" next_out 0x%p", s->stream.next_out);
194 if (s->stream.next_out){
196 DispHex(s->stream.next_out, 4);
200 printf(" avail_in %lu\n", (unsigned long)s->stream.avail_in);
201 printf(" avail_out %lu\n", (unsigned long)s->stream.avail_out);
202 printf(" bufsize %lu\n", (unsigned long)s->bufsize);
203 printf(" total_in_lo32 %u\n", s->stream.total_in_lo32);
204 printf(" total_in_hi32 %u\n", s->stream.total_in_hi32);
205 printf(" total_out_lo32 %u\n", s->stream.total_out_lo32);
206 printf(" total_out_hi32 %u\n", s->stream.total_out_hi32);
207 printf(" flags 0x%x\n", s->flags);
208 printf(" APPEND %s\n", EnDis(FLAG_APPEND_OUTPUT));
209 printf(" CONSUME %s\n", EnDis(FLAG_CONSUME_INPUT));
210 printf(" LIMIT %s\n", EnDis(FLAG_LIMIT_OUTPUT));
226 ZMALLOC(s, di_stream) ;
234 PostInitStream(di_stream * s, int flags)
236 PostInitStream(s, flags)
241 s->bufsize = 1024 * 16 ;
249 deRef(SV * sv, const char * string)
266 croak("%s: buffer parameter is not a SCALAR reference", string);
271 croak("%s: buffer parameter is a reference to a reference", string) ;
275 sv = sv_2mortal(newSVpv("", 0));
282 deRef_l(SV * sv, const char * string)
305 croak("%s: buffer parameter is not a SCALAR reference", string);
310 croak("%s: buffer parameter is a reference to a reference", string) ;
313 if (SvREADONLY(sv) && PL_curcop != &PL_compiling)
314 croak("%s: buffer parameter is read-only", string);
316 SvUPGRADE(sv, SVt_PV);
321 (void)SvPVbyte_force(sv, na) ;
327 #include "constants.h"
329 MODULE = Compress::Raw::Bzip2 PACKAGE = Compress::Raw::Bzip2 PREFIX = Zip_
334 INCLUDE: constants.xs
337 /* Check this version of bzip2 is == 1 */
338 if (BZ2_bzlibVersion()[0] != '1')
339 croak(COMPRESS_CLASS " needs bzip2 version 1.x, you have %s\n", BZ2_bzlibVersion()) ;
342 MODULE = Compress::Raw::Bzip2 PACKAGE = Compress::Raw::Bzip2
344 #define bzlibversion() BZ2_bzlibVersion()
349 new(className, appendOut=1, blockSize100k=1, workfactor=0, verbosity=0)
350 const char * className
361 warn("in Compress::Raw::Bzip2::_new(items=%d,appendOut=%d, blockSize100k=%d, workfactor=%d, verbosity=%d\n",
362 items, appendOut, blockSize100k, workfactor, verbosity);
364 if ((s = InitStream() )) {
366 err = BZ2_bzCompressInit ( &(s->stream),
378 flags |= FLAG_APPEND_OUTPUT;
379 PostInitStream(s, appendOut ? FLAG_APPEND_OUTPUT :0) ;
386 SV* obj = sv_setref_pv(sv_newmortal(), className, (void*)s);
391 SV* obj = sv_2mortal(newSViv(PTR2IV(s))) ;
394 if (GIMME == G_ARRAY) {
395 SV * sv = sv_2mortal(newSViv(err)) ;
396 setDUALstatus(sv, err);
401 MODULE = Compress::Raw::Bunzip2 PACKAGE = Compress::Raw::Bunzip2
404 new(className, appendOut=1 , consume=1, small=0, verbosity=0, limitOutput=0)
405 const char* className
417 warn("in _inflateInit(windowBits=%d, bufsize=%lu, dictionary=%lu\n",
418 windowBits, bufsize, (unsigned long)SvCUR(dictionary)) ;
420 if ((s = InitStream() )) {
422 err = BZ2_bzDecompressInit (&(s->stream), verbosity, small);
430 flags |= FLAG_APPEND_OUTPUT;
432 flags |= FLAG_CONSUME_INPUT;
434 flags |= (FLAG_LIMIT_OUTPUT|FLAG_CONSUME_INPUT);
435 PostInitStream(s, flags) ;
442 SV* obj = sv_setref_pv(sv_newmortal(), className, (void*)s);
447 SV* obj = sv_2mortal(newSViv(PTR2IV(s))) ;
450 if (GIMME == G_ARRAY) {
451 SV * sv = sv_2mortal(newSViv(err)) ;
452 setDUALstatus(sv, err);
459 MODULE = Compress::Raw::Bzip2 PACKAGE = Compress::Raw::Bzip2
462 DispStream(s, message=NULL)
463 Compress::Raw::Bzip2 s
467 bzdeflate (s, buf, output)
468 Compress::Raw::Bzip2 s
471 uInt cur_length = NO_INIT
472 uInt increment = NO_INIT
474 uInt bufinc = NO_INIT
475 STRLEN origlen = 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*)SvPV_nomg(buf, origlen) ;
488 s->stream.avail_in = origlen;
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*) SvPVX(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 s->stream.next_out = Sv_Grow(output, SvLEN(output) + bufinc) ;
510 cur_length += increment ;
511 s->stream.next_out += 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 += origlen - 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) != FLAG_APPEND_OUTPUT) {
562 SvCUR_set(output, 0);
563 /* sv_setpvn(output, "", 0); */
565 cur_length = SvCUR(output) ;
566 s->stream.next_out = (char*) SvPVX(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 s->stream.next_out = Sv_Grow(output, SvLEN(output) + bufinc) ;
574 cur_length += increment ;
575 s->stream.next_out += 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) != FLAG_APPEND_OUTPUT) {
623 SvCUR_set(output, 0);
624 /* sv_setpvn(output, "", 0); */
626 cur_length = SvCUR(output) ;
627 s->stream.next_out = (char*) SvPVX(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 s->stream.next_out = Sv_Grow(output, SvLEN(output) + bufinc) ;
635 cur_length += increment ;
636 s->stream.next_out += 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 uInt bufinc = NO_INIT
715 STRLEN na = NO_INIT ;
716 STRLEN origlen = NO_INIT
718 #ifdef UTF8_AVAILABLE
719 bool out_utf8 = FALSE;
723 /* If the buffer is a reference, dereference it */
724 buf = deRef(buf, "bzinflate") ;
726 if (s->flags & FLAG_CONSUME_INPUT) {
728 croak(UNCOMPRESS_CLASS "::bzinflate input parameter cannot be read-only when ConsumeInput is specified");
731 #ifdef UTF8_AVAILABLE
732 if (DO_UTF8(buf) && !sv_utf8_downgrade(buf, 1))
733 croak("Wide character in " UNCOMPRESS_CLASS "::bzinflate input parameter");
736 /* initialise the input buffer */
737 s->stream.next_in = (char*)SvPV_nomg(buf, origlen) ;
738 s->stream.avail_in = origlen;
740 /* and retrieve the output buffer */
741 output = deRef_l(output, "bzinflate") ;
742 #ifdef UTF8_AVAILABLE
745 if (DO_UTF8(output) && !sv_utf8_downgrade(output, 1))
746 croak("Wide character in " UNCOMPRESS_CLASS "::bzinflate output parameter");
748 if((s->flags & FLAG_APPEND_OUTPUT) != FLAG_APPEND_OUTPUT) {
749 SvCUR_set(output, 0);
752 /* Assume no output buffer - the code below will update if there is any available */
753 s->stream.avail_out = 0;
756 prefix_length = cur_length = SvCUR(output) ;
758 if (s->flags & FLAG_LIMIT_OUTPUT && SvLEN(output) - cur_length - 1 < bufinc)
760 Sv_Grow(output, bufinc + cur_length + 1) ;
763 /* Only setup the stream output pointers if there is spare
764 capacity in the outout SV
766 if (SvLEN(output) > cur_length + 1)
768 s->stream.next_out = (char*) SvPVX(output) + cur_length;
769 increment = SvLEN(output) - cur_length - 1;
770 s->stream.avail_out = increment;
774 s->bytesInflated = 0;
780 if (s->stream.avail_out == 0) {
781 /* out of space in the output buffer so make it bigger */
782 s->stream.next_out = Sv_Grow(output, SvLEN(output) + bufinc + 1) ;
783 cur_length += increment ;
784 s->stream.next_out += cur_length ;
786 s->stream.avail_out = increment;
790 /* DispStream(s, "pre"); */
791 RETVAL = BZ2_bzDecompress (&(s->stream));
794 printf("Status %d\n", RETVAL);
795 DispStream(s, "apres");
797 if (RETVAL != BZ_OK || s->flags & FLAG_LIMIT_OUTPUT)
800 if (s->stream.avail_out == 0)
803 if (s->stream.avail_in == 0) {
810 s->last_error = RETVAL ;
811 if (RETVAL == BZ_OK || RETVAL == BZ_STREAM_END) {
814 s->bytesInflated = cur_length + increment - s->stream.avail_out - prefix_length;
815 s->uncompressedBytes += s->bytesInflated ;
816 s->compressedBytes += origlen - s->stream.avail_in ;
819 SvCUR_set(output, prefix_length + s->bytesInflated) ;
820 *SvEND(output) = '\0';
821 #ifdef UTF8_AVAILABLE
823 sv_utf8_upgrade(output);
827 /* fix the input buffer */
828 if (s->flags & FLAG_CONSUME_INPUT) {
829 in = s->stream.avail_in ;
832 Move(s->stream.next_in, SvPVX(buf), in, char) ;
842 Compress::Raw::Bunzip2 s
844 RETVAL = s->bytesInflated;
851 Compress::Raw::Bunzip2 s
853 BZ2_bzDecompressEnd(&s->stream) ;
859 Compress::Raw::Bunzip2 s
861 RETVAL = s->last_error ;
867 Compress::Raw::Bunzip2 s
869 RETVAL = s->stream.total_in_lo32 ;
875 Compress::Raw::Bunzip2 s
877 RETVAL = s->stream.total_out_lo32 ;
883 Compress::Raw::Bunzip2 s
885 RETVAL = s->compressedBytes;
891 Compress::Raw::Bunzip2 s
893 RETVAL = s->uncompressedBytes;
897 MODULE = Compress::Raw::Bzip2 PACKAGE = Compress::Raw::Bzip2 PREFIX = Zip_