This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
ignore PERL_XMLDUMP when tainting
[perl5.git] / cpan / Compress-Raw-Bzip2 / Bzip2.xs
CommitLineData
bdb7fd9f
RGS
1/* Filename: Bzip2.xs
2 * Author : Paul Marquess, <pmqs@cpan.org>
3 * Created : 5th October 2005
4 * Version : 2.000
5 *
9b5fd1d4 6 * Copyright (c) 2005-2010 Paul Marquess. All rights reserved.
bdb7fd9f
RGS
7 * This program is free software; you can redistribute it and/or
8 * modify it under the same terms as Perl itself.
9 *
10 */
11
12
13#include "EXTERN.h"
14#include "perl.h"
15#include "XSUB.h"
16
17#include "bzlib.h"
18
19#ifdef USE_PPPORT_H
20# define NEED_sv_2pv_nolen
21# include "ppport.h"
22#endif
23
24#if PERL_REVISION == 5 && (PERL_VERSION < 8 || (PERL_VERSION == 8 && PERL_SUBVERSION < 4 ))
25
26# ifdef SvPVbyte_force
27# undef SvPVbyte_force
28# endif
29
30# define SvPVbyte_force(sv,lp) SvPV_force(sv,lp)
31
32#endif
33
34#ifndef SvPVbyte_nolen
35# define SvPVbyte_nolen SvPV_nolen
36#endif
37
38
39#if PERL_REVISION == 5 && (PERL_VERSION >= 8 || (PERL_VERSION == 8 && PERL_SUBVERSION < 4 ))
40# define UTF8_AVAILABLE
41#endif
42
43typedef int DualType ;
44typedef int int_undef ;
45
46typedef unsigned long uLong;
47typedef unsigned int uInt;
48
49typedef struct di_stream {
50 int flags ;
51#define FLAG_APPEND_OUTPUT 1
52#define FLAG_CONSUME_INPUT 8
ea6efd2c 53#define FLAG_LIMIT_OUTPUT 16
bdb7fd9f
RGS
54 bz_stream stream;
55 uInt bufsize;
56 int last_error ;
57 uLong bytesInflated ;
58 uLong compressedBytes ;
59 uLong uncompressedBytes ;
60
61} di_stream;
62
63typedef di_stream * deflateStream ;
64typedef di_stream * Compress__Raw__Bzip2 ;
65
66typedef di_stream * inflateStream ;
67typedef di_stream * Compress__Raw__Bunzip2 ;
68
69#define COMPRESS_CLASS "Compress::Raw::Bzip2"
70#define UNCOMPRESS_CLASS "Compress::Raw::Bunzip2"
71
72#define ZMALLOC(to, typ) ((to = (typ *)safemalloc(sizeof(typ))), \
73 Zero(to,1,typ))
74
75
76/* static const char * const my_z_errmsg[] = { */
77static 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 */
82 "", /* BZ_OK 0 */
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) */
92 ""};
93
94#define setDUALstatus(var, err) \
95 sv_setnv(var, (double)err) ; \
96 sv_setpv(var, ((err) ? GetErrorString(err) : "")) ; \
97 SvNOK_on(var);
98
99
100#if defined(__SYMBIAN32__)
101# define NO_WRITEABLE_DATA
102#endif
103
104#define TRACE_DEFAULT 0
105
106#ifdef NO_WRITEABLE_DATA
107# define trace TRACE_DEFAULT
108#else
109 static int trace = TRACE_DEFAULT ;
110#endif
111
112/* Dodge PerlIO hiding of these functions. */
113#undef printf
114
115#if 1
116#define getInnerObject(x) (*av_fetch((AV*)SvRV(x), 0, FALSE))
117#else
118#define getInnerObject(x) ((SV*)SvRV(sv))
119#endif
120
121#ifdef BZ_NO_STDIO
122void bz_internal_error(int errorcode)
123{
124 croak("bz_internal_error %d\n", errorcode);
125}
126#endif
127
128static char *
129#ifdef CAN_PROTOTYPE
130GetErrorString(int error_no)
131#else
132GetErrorString(error_no)
133int error_no ;
134#endif
135{
136 dTHX;
137 char * errstr ;
138
139#if 0
140 if (error_no == BZ_ERRNO) {
141 errstr = Strerror(errno) ;
142 }
143 else
144#endif
145 errstr = (char*) my_z_errmsg[4 - error_no];
146
147 return errstr ;
148}
149
150static void
151#ifdef CAN_PROTOTYPE
152DispHex(void * ptr, int length)
153#else
154DispHex(ptr, length)
155 void * ptr;
156 int length;
157#endif
158{
159 char * p = (char*)ptr;
160 int i;
161 for (i = 0; i < length; ++i) {
162 printf(" %02x", 0xFF & *(p+i));
163 }
164}
165
166
167static void
168#ifdef CAN_PROTOTYPE
169DispStream(di_stream * s, char * message)
170#else
171DispStream(s, message)
172 di_stream * s;
173 char * message;
174#endif
175{
176
177#if 0
178 if (! trace)
179 return ;
180#endif
181
182#define EnDis(f) (s->flags & f ? "Enabled" : "Disabled")
183
184 printf("DispStream 0x%p", s) ;
185 if (message)
ea6efd2c 186 printf(" - %s \n", message) ;
bdb7fd9f
RGS
187 printf("\n") ;
188
189 if (!s) {
190 printf(" stream pointer is NULL\n");
191 }
192 else {
193 printf(" stream 0x%p\n", &(s->stream));
194 printf(" opaque 0x%p\n", s->stream.opaque);
ea6efd2c 195 printf(" state 0x%p\n", s->stream.state );
bdb7fd9f
RGS
196 printf(" next_in 0x%p", s->stream.next_in);
197 if (s->stream.next_in){
198 printf(" =>");
199 DispHex(s->stream.next_in, 4);
200 }
201 printf("\n");
202
203 printf(" next_out 0x%p", s->stream.next_out);
204 if (s->stream.next_out){
205 printf(" =>");
206 DispHex(s->stream.next_out, 4);
207 }
208 printf("\n");
209
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);
ea6efd2c
MB
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);
bdb7fd9f
RGS
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));
ea6efd2c 220 printf(" LIMIT %s\n", EnDis(FLAG_LIMIT_OUTPUT));
bdb7fd9f
RGS
221
222 printf("\n");
223
224 }
225}
226
227static di_stream *
228#ifdef CAN_PROTOTYPE
229InitStream(void)
230#else
231InitStream()
232#endif
233{
234 di_stream *s ;
235
236 ZMALLOC(s, di_stream) ;
237
238 return s ;
239
240}
241
242static void
243#ifdef CAN_PROTOTYPE
244PostInitStream(di_stream * s, int flags)
245#else
246PostInitStream(s, flags)
247 di_stream *s ;
248 int flags ;
249#endif
250{
251 s->bufsize = 1024 * 16 ;
252 s->last_error = 0 ;
253 s->flags = flags ;
254}
255
256
257static SV*
258#ifdef CAN_PROTOTYPE
259deRef(SV * sv, const char * string)
260#else
261deRef(sv, string)
262SV * sv ;
263char * string;
264#endif
265{
266 dTHX;
267 SvGETMAGIC(sv);
268
269 if (SvROK(sv)) {
270 sv = SvRV(sv) ;
271 SvGETMAGIC(sv);
272 switch(SvTYPE(sv)) {
273 case SVt_PVAV:
274 case SVt_PVHV:
275 case SVt_PVCV:
276 croak("%s: buffer parameter is not a SCALAR reference", string);
776304fb
PM
277 default:
278 break;
bdb7fd9f
RGS
279 }
280 if (SvROK(sv))
281 croak("%s: buffer parameter is a reference to a reference", string) ;
282 }
283
4bac9ae4
CS
284 if (!SvOK(sv))
285 sv = sv_2mortal(newSVpv("", 0));
bdb7fd9f
RGS
286
287 return sv ;
288}
289
290static SV*
291#ifdef CAN_PROTOTYPE
292deRef_l(SV * sv, const char * string)
293#else
294deRef_l(sv, string)
295SV * sv ;
296char * string ;
297#endif
298{
299 dTHX;
300 bool wipe = 0 ;
4bac9ae4 301 STRLEN na;
bdb7fd9f
RGS
302
303 SvGETMAGIC(sv);
304 wipe = ! SvOK(sv) ;
305
306 if (SvROK(sv)) {
307 sv = SvRV(sv) ;
308 SvGETMAGIC(sv);
309 wipe = ! SvOK(sv) ;
310
311 switch(SvTYPE(sv)) {
312 case SVt_PVAV:
313 case SVt_PVHV:
314 case SVt_PVCV:
315 croak("%s: buffer parameter is not a SCALAR reference", string);
776304fb
PM
316 default:
317 break;
bdb7fd9f
RGS
318 }
319 if (SvROK(sv))
320 croak("%s: buffer parameter is a reference to a reference", string) ;
321 }
322
323 if (SvREADONLY(sv) && PL_curcop != &PL_compiling)
324 croak("%s: buffer parameter is read-only", string);
325
4bac9ae4 326 SvUPGRADE(sv, SVt_PV) ;
bdb7fd9f 327 if (wipe)
4bac9ae4
CS
328 sv_setpv(sv, "") ;
329 else
330 (void)SvPVbyte_force(sv, na) ;
bdb7fd9f
RGS
331 return sv ;
332}
333
334
335#include "constants.h"
336
337MODULE = Compress::Raw::Bzip2 PACKAGE = Compress::Raw::Bzip2 PREFIX = Zip_
338
339REQUIRE: 1.924
340PROTOTYPES: DISABLE
341
342INCLUDE: constants.xs
343
344BOOT:
c14f59c3
PM
345#ifndef NO_WRITEABLE_DATA
346 trace = TRACE_DEFAULT ;
347#endif
bdb7fd9f
RGS
348 /* Check this version of bzip2 is == 1 */
349 if (BZ2_bzlibVersion()[0] != '1')
350 croak(COMPRESS_CLASS " needs bzip2 version 1.x, you have %s\n", BZ2_bzlibVersion()) ;
351
352
353MODULE = Compress::Raw::Bzip2 PACKAGE = Compress::Raw::Bzip2
354
355#define bzlibversion() BZ2_bzlibVersion()
356const char *
357bzlibversion()
358
359void
c14f59c3
PM
360new(className, appendOut=1, blockSize100k=1, workfactor=0, verbosity=0)
361 const char * className
bdb7fd9f
RGS
362 int appendOut
363 int blockSize100k
364 int workfactor
365 int verbosity
366 PPCODE:
367 {
368 int err ;
369 deflateStream s ;
370#if 0
371 /* if (trace) */
372 warn("in Compress::Raw::Bzip2::_new(items=%d,appendOut=%d, blockSize100k=%d, workfactor=%d, verbosity=%d\n",
373 items, appendOut, blockSize100k, workfactor, verbosity);
374#endif
375 if ((s = InitStream() )) {
376
377 err = BZ2_bzCompressInit ( &(s->stream),
378 blockSize100k,
379 verbosity,
380 workfactor );
381
382 if (err != BZ_OK) {
383 Safefree(s) ;
384 s = NULL ;
385 }
386 else {
387 int flags = 0 ;
388 if (appendOut)
389 flags |= FLAG_APPEND_OUTPUT;
390 PostInitStream(s, appendOut ? FLAG_APPEND_OUTPUT :0) ;
391 }
392 }
393 else
394 err = BZ_MEM_ERROR ;
395
396 {
c14f59c3 397 SV* obj = sv_setref_pv(sv_newmortal(), className, (void*)s);
bdb7fd9f
RGS
398 XPUSHs(obj);
399 }
400 if(0)
401 {
402 SV* obj = sv_2mortal(newSViv(PTR2IV(s))) ;
403 XPUSHs(obj);
404 }
405 if (GIMME == G_ARRAY) {
406 SV * sv = sv_2mortal(newSViv(err)) ;
407 setDUALstatus(sv, err);
408 XPUSHs(sv) ;
409 }
410 }
411
412MODULE = Compress::Raw::Bunzip2 PACKAGE = Compress::Raw::Bunzip2
413
414void
ea6efd2c 415new(className, appendOut=1 , consume=1, small=0, verbosity=0, limitOutput=0)
c14f59c3 416 const char* className
bdb7fd9f
RGS
417 int appendOut
418 int consume
419 int small
420 int verbosity
ea6efd2c 421 int limitOutput
bdb7fd9f
RGS
422 PPCODE:
423 {
424 int err = BZ_OK ;
425 inflateStream s ;
426#if 0
427 if (trace)
428 warn("in _inflateInit(windowBits=%d, bufsize=%lu, dictionary=%lu\n",
429 windowBits, bufsize, (unsigned long)SvCUR(dictionary)) ;
430#endif
431 if ((s = InitStream() )) {
432
433 err = BZ2_bzDecompressInit (&(s->stream), verbosity, small);
434 if (err != BZ_OK) {
435 Safefree(s) ;
436 s = NULL ;
437 }
438 if (s) {
439 int flags = 0;
440 if (appendOut)
441 flags |= FLAG_APPEND_OUTPUT;
442 if (consume)
443 flags |= FLAG_CONSUME_INPUT;
ea6efd2c
MB
444 if (limitOutput)
445 flags |= (FLAG_LIMIT_OUTPUT|FLAG_CONSUME_INPUT);
bdb7fd9f
RGS
446 PostInitStream(s, flags) ;
447 }
448 }
449 else
450 err = BZ_MEM_ERROR ;
451
452 {
c14f59c3 453 SV* obj = sv_setref_pv(sv_newmortal(), className, (void*)s);
bdb7fd9f
RGS
454 XPUSHs(obj);
455 }
456 if (0)
457 {
458 SV* obj = sv_2mortal(newSViv(PTR2IV(s))) ;
459 XPUSHs(obj);
460 }
461 if (GIMME == G_ARRAY) {
462 SV * sv = sv_2mortal(newSViv(err)) ;
463 setDUALstatus(sv, err);
464 XPUSHs(sv) ;
465 }
466 }
467
468
469
470MODULE = Compress::Raw::Bzip2 PACKAGE = Compress::Raw::Bzip2
471
472void
473DispStream(s, message=NULL)
474 Compress::Raw::Bzip2 s
475 char * message
476
477DualType
478bzdeflate (s, buf, output)
479 Compress::Raw::Bzip2 s
480 SV * buf
481 SV * output
482 uInt cur_length = NO_INIT
483 uInt increment = NO_INIT
484 int RETVAL = 0;
485 uInt bufinc = NO_INIT
486 CODE:
487 bufinc = s->bufsize;
488
489 /* If the input buffer is a reference, dereference it */
490 buf = deRef(buf, "deflate") ;
491
492 /* initialise the input buffer */
493#ifdef UTF8_AVAILABLE
494 if (DO_UTF8(buf) && !sv_utf8_downgrade(buf, 1))
495 croak("Wide character in " COMPRESS_CLASS "::bzdeflate input parameter");
4bac9ae4
CS
496#endif
497 s->stream.next_in = SvPV_nomg_nolen(buf);
498 s->stream.avail_in = SvCUR(buf);
bdb7fd9f
RGS
499
500 /* and retrieve the output buffer */
501 output = deRef_l(output, "deflate") ;
502#ifdef UTF8_AVAILABLE
503 if (DO_UTF8(output) && !sv_utf8_downgrade(output, 1))
504 croak("Wide character in " COMPRESS_CLASS "::bzdeflate output parameter");
505#endif
4bac9ae4 506 if((s->flags & FLAG_APPEND_OUTPUT) != FLAG_APPEND_OUTPUT)
bdb7fd9f 507 SvCUR_set(output, 0);
bdb7fd9f 508 cur_length = SvCUR(output) ;
4bac9ae4 509 s->stream.next_out = SvPVX(output) + cur_length;
bdb7fd9f
RGS
510 increment = SvLEN(output) - cur_length;
511 s->stream.avail_out = increment;
512 while (s->stream.avail_in != 0) {
513
514 if (s->stream.avail_out == 0) {
515 /* out of space in the output buffer so make it bigger */
516 Sv_Grow(output, SvLEN(output) + bufinc) ;
517 cur_length += increment ;
4bac9ae4 518 s->stream.next_out = SvPVX(output) + cur_length ;
bdb7fd9f
RGS
519 increment = bufinc ;
520 s->stream.avail_out = increment;
521 bufinc *= 2 ;
522 }
523
524 RETVAL = BZ2_bzCompress(&(s->stream), BZ_RUN);
525 if (RETVAL != BZ_RUN_OK)
526 break;
527 }
528
529 s->compressedBytes += cur_length + increment - s->stream.avail_out ;
530 s->uncompressedBytes += SvCUR(buf) - s->stream.avail_in ;
531
532 s->last_error = RETVAL ;
533 if (RETVAL == BZ_RUN_OK) {
534 SvPOK_only(output);
535 SvCUR_set(output, cur_length + increment - s->stream.avail_out) ;
536 SvSETMAGIC(output);
537 }
538 OUTPUT:
539 RETVAL
540
541
542void
543DESTROY(s)
544 Compress::Raw::Bzip2 s
545 CODE:
546 BZ2_bzCompressEnd(&s->stream) ;
547 Safefree(s) ;
548
549
550DualType
551bzclose(s, output)
552 Compress::Raw::Bzip2 s
553 SV * output
554 uInt cur_length = NO_INIT
555 uInt increment = NO_INIT
556 uInt bufinc = NO_INIT
557 CODE:
558 bufinc = s->bufsize;
559
560 s->stream.avail_in = 0; /* should be zero already anyway */
561
562 /* retrieve the output buffer */
563 output = deRef_l(output, "close") ;
564#ifdef UTF8_AVAILABLE
565 if (DO_UTF8(output) && !sv_utf8_downgrade(output, 1))
566 croak("Wide character in " COMPRESS_CLASS "::bzclose input parameter");
567#endif
4bac9ae4 568 if(! s->flags & FLAG_APPEND_OUTPUT)
bdb7fd9f 569 SvCUR_set(output, 0);
bdb7fd9f 570 cur_length = SvCUR(output) ;
4bac9ae4 571 s->stream.next_out = SvPVX(output) + cur_length;
bdb7fd9f
RGS
572 increment = SvLEN(output) - cur_length;
573 s->stream.avail_out = increment;
574
575 for (;;) {
576 if (s->stream.avail_out == 0) {
577 /* consumed all the available output, so extend it */
578 Sv_Grow(output, SvLEN(output) + bufinc) ;
579 cur_length += increment ;
4bac9ae4 580 s->stream.next_out = SvPVX(output) + cur_length ;
bdb7fd9f
RGS
581 increment = bufinc ;
582 s->stream.avail_out = increment;
583 bufinc *= 2 ;
584 }
585 RETVAL = BZ2_bzCompress(&(s->stream), BZ_FINISH);
586
587 /* deflate has finished flushing only when it hasn't used up
588 * all the available space in the output buffer:
589 */
590 /* if (s->stream.avail_out != 0 || RETVAL < 0 ) */
591 if (RETVAL == BZ_STREAM_END || RETVAL < 0 )
592 break;
593 }
594
595 /* RETVAL = (RETVAL == BZ_STREAM_END ? BZ_OK : RETVAL) ; */
596 s->last_error = RETVAL ;
597
598 s->compressedBytes += cur_length + increment - s->stream.avail_out ;
599
600 if (RETVAL == BZ_STREAM_END) {
601 SvPOK_only(output);
602 SvCUR_set(output, cur_length + increment - s->stream.avail_out) ;
603 SvSETMAGIC(output);
604 }
605 OUTPUT:
606 RETVAL
607
608
609DualType
610bzflush(s, output)
611 Compress::Raw::Bzip2 s
612 SV * output
613 uInt cur_length = NO_INIT
614 uInt increment = NO_INIT
615 uInt bufinc = NO_INIT
616 CODE:
617 bufinc = s->bufsize;
618
619 s->stream.avail_in = 0; /* should be zero already anyway */
620
621 /* retrieve the output buffer */
622 output = deRef_l(output, "close") ;
623#ifdef UTF8_AVAILABLE
624 if (DO_UTF8(output) && !sv_utf8_downgrade(output, 1))
625 croak("Wide character in " COMPRESS_CLASS "::bzflush input parameter");
626#endif
4bac9ae4 627 if(! s->flags & FLAG_APPEND_OUTPUT)
bdb7fd9f 628 SvCUR_set(output, 0);
bdb7fd9f 629 cur_length = SvCUR(output) ;
4bac9ae4 630 s->stream.next_out = SvPVX(output) + cur_length;
bdb7fd9f
RGS
631 increment = SvLEN(output) - cur_length;
632 s->stream.avail_out = increment;
633
634 for (;;) {
635 if (s->stream.avail_out == 0) {
636 /* consumed all the available output, so extend it */
637 Sv_Grow(output, SvLEN(output) + bufinc) ;
638 cur_length += increment ;
4bac9ae4 639 s->stream.next_out = SvPVX(output) + cur_length ;
bdb7fd9f
RGS
640 increment = bufinc ;
641 s->stream.avail_out = increment;
642 bufinc *= 2 ;
643 }
644 RETVAL = BZ2_bzCompress(&(s->stream), BZ_FLUSH);
645
646 if (RETVAL == BZ_RUN_OK || RETVAL < 0)
647 break;
648
649 /* deflate has finished flushing only when it hasn't used up
650 * all the available space in the output buffer:
651 */
652 /* RETVAL == if (s->stream.avail_out != 0 || RETVAL < 0 )
653 break; */
654 }
655
656 /* RETVAL = (RETVAL == BZ_STREAM_END ? BZ_OK : RETVAL) ; */
657 s->last_error = RETVAL ;
658
659 s->compressedBytes += cur_length + increment - s->stream.avail_out ;
660
661 if (RETVAL == BZ_RUN_OK) {
662 SvPOK_only(output);
663 SvCUR_set(output, cur_length + increment - s->stream.avail_out) ;
664 SvSETMAGIC(output);
665 }
666 OUTPUT:
667 RETVAL
668
669uLong
670total_in_lo32(s)
671 Compress::Raw::Bzip2 s
672 CODE:
673 RETVAL = s->stream.total_in_lo32 ;
674 OUTPUT:
675 RETVAL
676
677uLong
678total_out_lo32(s)
679 Compress::Raw::Bzip2 s
680 CODE:
681 RETVAL = s->stream.total_out_lo32 ;
682 OUTPUT:
683 RETVAL
684
685uLong
686compressedBytes(s)
687 Compress::Raw::Bzip2 s
688 CODE:
689 RETVAL = s->compressedBytes;
690 OUTPUT:
691 RETVAL
692
693uLong
694uncompressedBytes(s)
695 Compress::Raw::Bzip2 s
696 CODE:
697 RETVAL = s->uncompressedBytes;
698 OUTPUT:
699 RETVAL
700
701
702MODULE = Compress::Raw::Bunzip2 PACKAGE = Compress::Raw::Bunzip2
703
704void
705DispStream(s, message=NULL)
706 Compress::Raw::Bunzip2 s
707 char * message
708
709DualType
710bzinflate (s, buf, output)
711 Compress::Raw::Bunzip2 s
712 SV * buf
713 SV * output
714 uInt cur_length = 0;
715 uInt prefix_length = 0;
716 uInt increment = 0;
717 STRLEN stmp = NO_INIT
718 uInt bufinc = NO_INIT
719 PREINIT:
720#ifdef UTF8_AVAILABLE
721 bool out_utf8 = FALSE;
722#endif
723 CODE:
724 bufinc = s->bufsize;
725 /* If the buffer is a reference, dereference it */
ea6efd2c 726 buf = deRef(buf, "bzinflate") ;
bdb7fd9f
RGS
727
728 if (s->flags & FLAG_CONSUME_INPUT && SvREADONLY(buf))
729 croak(UNCOMPRESS_CLASS "::bzinflate input parameter cannot be read-only when ConsumeInput is specified");
730#ifdef UTF8_AVAILABLE
731 if (DO_UTF8(buf) && !sv_utf8_downgrade(buf, 1))
732 croak("Wide character in " UNCOMPRESS_CLASS "::bzinflate input parameter");
733#endif
734
735 /* initialise the input buffer */
4bac9ae4
CS
736 s->stream.next_in = SvPV_nomg_nolen(buf);
737 s->stream.avail_in = stmp = SvCUR(buf);
bdb7fd9f
RGS
738
739 /* and retrieve the output buffer */
ea6efd2c 740 output = deRef_l(output, "bzinflate") ;
bdb7fd9f
RGS
741#ifdef UTF8_AVAILABLE
742 if (DO_UTF8(output))
743 out_utf8 = TRUE ;
744 if (DO_UTF8(output) && !sv_utf8_downgrade(output, 1))
745 croak("Wide character in " UNCOMPRESS_CLASS "::bzinflate output parameter");
746#endif
747 if((s->flags & FLAG_APPEND_OUTPUT) != FLAG_APPEND_OUTPUT) {
748 SvCUR_set(output, 0);
749 }
80b215cb 750
ea6efd2c
MB
751 /* Assume no output buffer - the code below will update if there is any available */
752 s->stream.avail_out = 0;
753
754 if (SvLEN(output)) {
755 prefix_length = cur_length = SvCUR(output) ;
756
757 if (s->flags & FLAG_LIMIT_OUTPUT && SvLEN(output) - cur_length - 1 < bufinc)
758 {
759 Sv_Grow(output, bufinc + cur_length + 1) ;
760 }
761
762 /* Only setup the stream output pointers if there is spare
763 capacity in the outout SV
764 */
765 if (SvLEN(output) > cur_length + 1)
766 {
4bac9ae4 767 s->stream.next_out = SvPVX(output) + cur_length;
ea6efd2c
MB
768 increment = SvLEN(output) - cur_length - 1;
769 s->stream.avail_out = increment;
770 }
771 }
772
773 s->bytesInflated = 0;
774
775 RETVAL = BZ_OK;
bdb7fd9f
RGS
776
777 while (1) {
778
779 if (s->stream.avail_out == 0) {
780 /* out of space in the output buffer so make it bigger */
ea6efd2c 781 Sv_Grow(output, SvLEN(output) + bufinc + 1) ;
bdb7fd9f 782 cur_length += increment ;
4bac9ae4 783 s->stream.next_out = SvPVX(output) + cur_length ;
bdb7fd9f
RGS
784 increment = bufinc ;
785 s->stream.avail_out = increment;
786 bufinc *= 2 ;
787 }
788
dcfdccf9 789 /* DispStream(s, "pre"); */
bdb7fd9f
RGS
790 RETVAL = BZ2_bzDecompress (&(s->stream));
791
dcfdccf9 792 /* DispStream(s, "apres"); */
ea6efd2c 793 if (RETVAL != BZ_OK || s->flags & FLAG_LIMIT_OUTPUT)
bdb7fd9f
RGS
794 break ;
795
796 if (s->stream.avail_out == 0)
797 continue ;
798
799 if (s->stream.avail_in == 0) {
800 RETVAL = BZ_OK ;
801 break ;
802 }
803
804 }
805
806 s->last_error = RETVAL ;
807 if (RETVAL == BZ_OK || RETVAL == BZ_STREAM_END) {
808 unsigned in ;
809
810 s->bytesInflated = cur_length + increment - s->stream.avail_out - prefix_length;
811 s->uncompressedBytes += s->bytesInflated ;
812 s->compressedBytes += SvCUR(buf) - s->stream.avail_in ;
813
814 SvPOK_only(output);
815 SvCUR_set(output, prefix_length + s->bytesInflated) ;
4bac9ae4 816 *SvEND(output) = '\0' ;
bdb7fd9f
RGS
817#ifdef UTF8_AVAILABLE
818 if (out_utf8)
4bac9ae4 819 sv_utf8_upgrade(output) ;
bdb7fd9f 820#endif
4bac9ae4 821 SvSETMAGIC(output) ;
bdb7fd9f
RGS
822
823 /* fix the input buffer */
824 if (s->flags & FLAG_CONSUME_INPUT) {
825 in = s->stream.avail_in ;
826 SvCUR_set(buf, in) ;
827 if (in)
4bac9ae4
CS
828 Move(s->stream.next_in, SvPVX(buf), in, char) ;
829 *SvEND(buf) = '\0' ;
830 SvSETMAGIC(buf) ;
bdb7fd9f
RGS
831 }
832 }
833 OUTPUT:
834 RETVAL
835
836uLong
837inflateCount(s)
838 Compress::Raw::Bunzip2 s
839 CODE:
840 RETVAL = s->bytesInflated;
841 OUTPUT:
842 RETVAL
843
844
845void
846DESTROY(s)
847 Compress::Raw::Bunzip2 s
848 CODE:
849 BZ2_bzDecompressEnd(&s->stream) ;
850 Safefree(s) ;
851
852
853uLong
854status(s)
855 Compress::Raw::Bunzip2 s
856 CODE:
857 RETVAL = s->last_error ;
858 OUTPUT:
859 RETVAL
860
861uLong
862total_in_lo32(s)
863 Compress::Raw::Bunzip2 s
864 CODE:
865 RETVAL = s->stream.total_in_lo32 ;
866 OUTPUT:
867 RETVAL
868
869uLong
870total_out_lo32(s)
871 Compress::Raw::Bunzip2 s
872 CODE:
873 RETVAL = s->stream.total_out_lo32 ;
874 OUTPUT:
875 RETVAL
876
877uLong
878compressedBytes(s)
879 Compress::Raw::Bunzip2 s
880 CODE:
881 RETVAL = s->compressedBytes;
882 OUTPUT:
883 RETVAL
884
885uLong
886uncompressedBytes(s)
887 Compress::Raw::Bunzip2 s
888 CODE:
889 RETVAL = s->uncompressedBytes;
890 OUTPUT:
891 RETVAL
892
893MODULE = Compress::Raw::Bzip2 PACKAGE = Compress::Raw::Bzip2 PREFIX = Zip_