2 * Author : Paul Marquess, <pmqs@cpan.org>
3 * Created : 30 January 2005
6 * Copyright (c) 1995-2005 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 /* Part of this code is based on the file gzio.c */
14 /* gzio.c -- IO on .gz files
15 * Copyright (C) 1995 Jean-loup Gailly.
16 * For conditions of distribution and use, see copyright notice in zlib.h
28 #include "patchlevel.h"
29 #define PERL_REVISION 5
30 #define PERL_VERSION PATCHLEVEL
31 #define PERL_SUBVERSION SUBVERSION
34 #if PERL_REVISION == 5 && (PERL_VERSION < 4 || (PERL_VERSION == 4 && PERL_SUBVERSION <= 75 ))
36 # define PL_sv_undef sv_undef
38 # define PL_curcop curcop
39 # define PL_compiling compiling
44 # define newSVuv newSViv
47 typedef struct di_stream {
53 bool deflateParams_out_valid ;
54 Bytef deflateParams_out_byte;
62 typedef di_stream * deflateStream ;
63 typedef di_stream * Compress__Zlib__deflateStream ;
64 typedef di_stream * inflateStream ;
65 typedef di_stream * Compress__Zlib__inflateStream ;
67 /* typedef gzFile Compress__Zlib__gzFile ; */
68 typedef struct gzType {
75 typedef gzType* Compress__Zlib__gzFile ;
79 #define GZERRNO "Compress::Zlib::gzerrno"
81 #define ZMALLOC(to, typ) ((to = (typ *)safemalloc(sizeof(typ))), \
84 #define adlerInitial adler32(0L, Z_NULL, 0)
85 #define crcInitial crc32(0L, Z_NULL, 0)
88 static char *my_z_errmsg[] = {
89 "need dictionary", /* Z_NEED_DICT 2 */
90 "stream end", /* Z_STREAM_END 1 */
92 "file error", /* Z_ERRNO (-1) */
93 "stream error", /* Z_STREAM_ERROR (-2) */
94 "data error", /* Z_DATA_ERROR (-3) */
95 "insufficient memory", /* Z_MEM_ERROR (-4) */
96 "buffer error", /* Z_BUF_ERROR (-5) */
97 "incompatible version",/* Z_VERSION_ERROR(-6) */
102 static int trace = 0 ;
106 SetGzErrorNo(int error_no)
108 SetGzErrorNo(error_no)
113 SV * gzerror_sv = perl_get_sv(GZERRNO, FALSE) ;
115 if (error_no == Z_ERRNO) {
117 errstr = Strerror(errno) ;
120 /* errstr = gzerror(fil, &error_no) ; */
121 errstr = (char*) my_z_errmsg[2 - error_no];
123 if (SvIV(gzerror_sv) != error_no) {
124 sv_setiv(gzerror_sv, error_no) ;
125 sv_setpv(gzerror_sv, errstr) ;
126 SvIOK_on(gzerror_sv) ;
133 SetGzError(gzFile file)
141 (void)gzerror(file, &error_no) ;
142 SetGzErrorNo(error_no) ;
147 DispHex(void * ptr, int length)
154 char * p = (char*)ptr;
156 for (i = 0; i < length; ++i) {
157 printf(" %02x", 0xFF & *(p+i));
164 DispStream(di_stream * s, char * message)
166 DispStream(s, message)
177 printf("DispStream 0x%p - %s \n", s, message) ;
180 printf(" stream pointer is NULL\n");
183 printf(" stream 0x%p\n", &(s->stream));
184 printf(" zalloc 0x%p\n", s->stream.zalloc);
185 printf(" zfree 0x%p\n", s->stream.zfree);
186 printf(" opaque 0x%p\n", s->stream.opaque);
188 printf(" msg %s\n", s->stream.msg);
191 printf(" next_in 0x%p", s->stream.next_in);
192 if (s->stream.next_in) {
194 DispHex(s->stream.next_in, 4);
198 printf(" next_out 0x%p", s->stream.next_out);
199 if (s->stream.next_out){
201 DispHex(s->stream.next_out, 4);
205 printf(" avail_in %ld\n", s->stream.avail_in);
206 printf(" avail_out %ld\n", s->stream.avail_out);
207 printf(" total_in %ld\n", s->stream.total_in);
208 printf(" total_out %ld\n", s->stream.total_out);
209 printf(" adler 0x%lx\n", s->stream.adler);
210 printf(" reserved 0x%lx\n", s->stream.reserved);
211 printf(" bufsize %ld\n", s->bufsize);
212 printf(" dictionary 0x%p\n", s->dictionary);
213 printf(" dict_adler 0x%ld\n", s->dict_adler);
222 InitStream(uLong bufsize)
230 ZMALLOC(s, di_stream) ;
233 s->bufsize = bufsize ;
234 s->bufinc = bufsize ;
245 gzreadline(Compress__Zlib__gzFile file, SV * output)
247 gzreadline(file, output)
248 Compress__Zlib__gzFile file ;
253 SV * store = file->buffer ;
256 char *out_ptr = SvPVX(store) ;
261 /* anything left from last time */
262 if ((n = SvCUR(store))) {
264 out_ptr = SvPVX(store) + file->offset ;
265 if ((p = ninstr(out_ptr, out_ptr + n - 1, nl, nl))) {
266 /* if (rschar != 0777 && */
267 /* p = ninstr(out_ptr, out_ptr + n - 1, rs, rs+rslen-1)) { */
269 sv_catpvn(output, out_ptr, p - out_ptr + 1);
271 file->offset += (p - out_ptr + 1) ;
272 n = n - (p - out_ptr + 1);
273 SvCUR_set(store, n) ;
274 return SvCUR(output);
276 else /* no EOL, so append the complete buffer */
277 sv_catpvn(output, out_ptr, n);
282 SvCUR_set(store, 0) ;
284 out_ptr = SvPVX(store) ;
286 n = gzread(file->gz, out_ptr, SIZE) ;
289 /* Either EOF or an error */
290 /* so return what we have so far else signal eof */
291 return (SvCUR(output)>0) ? SvCUR(output) : n ;
293 SvCUR_set(store, n) ;
299 deRef(SV * sv, char * string)
312 croak("%s: buffer parameter is not a SCALAR reference", string);
315 croak("%s: buffer parameter is a reference to a reference", string) ;
324 #include "constants.h"
326 MODULE = Compress::Zlib PACKAGE = Compress::Zlib PREFIX = Zip_
331 INCLUDE: constants.xs
334 /* Check this version of zlib is == 1 */
335 if (zlibVersion()[0] != '1')
336 croak("Compress::Zlib needs zlib version 1.x\n") ;
339 /* Create the $gzerror scalar */
340 SV * gzerror_sv = perl_get_sv(GZERRNO, GV_ADDMULTI) ;
341 sv_setiv(gzerror_sv, 0) ;
342 sv_setpv(gzerror_sv, "") ;
343 SvIOK_on(gzerror_sv) ;
347 #define Zip_zlib_version() (char*)zlib_version
353 DispStream(s, message=NULL)
354 Compress::Zlib::inflateStream s
357 Compress::Zlib::gzFile
363 gz = gzopen(path, mode) ;
365 ZMALLOC(RETVAL, gzType) ;
366 RETVAL->buffer = newSV(SIZE) ;
367 SvPOK_only(RETVAL->buffer) ;
368 SvCUR_set(RETVAL->buffer, 0) ;
371 RETVAL->closed = FALSE ;
376 SetGzErrorNo(errno ? Z_ERRNO : Z_MEM_ERROR) ;
382 Compress::Zlib::gzFile
383 gzdopen_(fh, mode, offset)
390 lseek(fh, offset, 0) ;
391 gz = gzdopen(fh, mode) ;
393 ZMALLOC(RETVAL, gzType) ;
394 RETVAL->buffer = newSV(SIZE) ;
395 SvPOK_only(RETVAL->buffer) ;
396 SvCUR_set(RETVAL->buffer, 0) ;
399 RETVAL->closed = FALSE ;
404 SetGzErrorNo(errno ? Z_ERRNO : Z_MEM_ERROR) ;
410 MODULE = Compress::Zlib PACKAGE = Compress::Zlib::gzFile PREFIX = Zip_
412 #define Zip_gzread(file, buf, len) gzread(file->gz, bufp, len)
415 Zip_gzread(file, buf, len=4096)
416 Compress::Zlib::gzFile file
423 if (SvREADONLY(buf) && PL_curcop != &PL_compiling)
424 croak("gzread: buffer parameter is read-only");
425 SvUPGRADE(buf, SVt_PV);
428 /* any left over from gzreadline ? */
429 if ((bufsize = SvCUR(file->buffer)) > 0) {
442 sv_catpvn(buf, SvPVX(file->buffer) + file->offset, movesize);
444 file->offset += movesize ;
445 SvCUR_set(file->buffer, bufsize - movesize) ;
449 bufp = (Byte*)SvGROW(buf, bufsize+len+1);
450 RETVAL = gzread(file->gz, ((Bytef*)bufp)+bufsize, len) ;
451 SetGzError(file->gz) ;
454 SvCUR_set(buf, RETVAL) ;
463 gzreadline(file, buf)
464 Compress::Zlib::gzFile file
468 if (SvREADONLY(buf) && PL_curcop != &PL_compiling)
469 croak("gzreadline: buffer parameter is read-only");
470 SvUPGRADE(buf, SVt_PV);
472 /* sv_setpvn(buf, "", SIZE) ; */
475 RETVAL = gzreadline(file, buf) ;
476 SetGzError(file->gz) ;
482 /* SvCUR(buf) = RETVAL; */
483 /* Don't need to explicitly terminate with '\0', because
484 sv_catpvn aready has */
487 #define Zip_gzwrite(file, buf) gzwrite(file->gz, buf, (unsigned)len)
489 Zip_gzwrite(file, buf)
490 Compress::Zlib::gzFile file
492 voidp buf = (voidp)SvPV(ST(1), len) ;
494 SetGzError(file->gz) ;
496 #define Zip_gzflush(file, flush) gzflush(file->gz, flush)
498 Zip_gzflush(file, flush)
499 Compress::Zlib::gzFile file
502 SetGzError(file->gz) ;
504 #define Zip_gzclose(file) gzclose(file->gz)
507 Compress::Zlib::gzFile file
509 file->closed = TRUE ;
510 SetGzErrorNo(RETVAL) ;
513 #define Zip_gzeof(file) gzeof(file->gz)
516 Compress::Zlib::gzFile file
519 croak("gzeof needs zlib 1.0.6 or better") ;
521 RETVAL = gzeof(file->gz);
527 #define Zip_gzsetparams(file,l,s) gzsetparams(file->gz,l,s)
529 Zip_gzsetparams(file, level, strategy)
530 Compress::Zlib::gzFile file
535 croak("gzsetparams needs zlib 1.0.6 or better") ;
537 RETVAL = gzsetparams(file->gz, level, strategy);
544 Compress::Zlib::gzFile file
548 SvREFCNT_dec(file->buffer) ;
549 safefree((char*)file) ;
551 #define Zip_gzerror(file) (char*)gzerror(file->gz, &errnum)
555 Compress::Zlib::gzFile file
558 sv_setiv(ST(0), errnum) ;
563 MODULE = Compress::Zlib PACKAGE = Compress::Zlib PREFIX = Zip_
566 #define Zip_adler32(buf, adler) adler32(adler, buf, (uInt)len)
569 Zip_adler32(buf, adler=adlerInitial)
570 uLong adler = NO_INIT
572 Bytef * buf = NO_INIT
575 /* If the buffer is a reference, dereference it */
576 sv = deRef(sv, "adler32") ;
577 buf = (Byte*)SvPV(sv, len) ;
580 adler = adlerInitial;
581 else if (SvOK(ST(1)))
582 adler = SvUV(ST(1)) ;
584 adler = adlerInitial;
586 #define Zip_crc32(buf, crc) crc32(crc, buf, (uInt)len)
589 Zip_crc32(buf, crc=crcInitial)
592 Bytef * buf = NO_INIT
595 /* If the buffer is a reference, dereference it */
596 sv = deRef(sv, "crc32") ;
597 buf = (Byte*)SvPV(sv, len) ;
601 else if (SvOK(ST(1)))
606 MODULE = Compress::Zlib PACKAGE = Compress::Zlib
609 _deflateInit(level, method, windowBits, memLevel, strategy, bufsize, dictionary)
623 warn("in _deflateInit(level=%d, method=%d, windowBits=%d, memLevel=%d, strategy=%d, bufsize=%d\n",
624 level, method, windowBits, memLevel, strategy, bufsize) ;
625 if ((s = InitStream(bufsize)) ) {
629 s->WindowBits = windowBits;
630 s->MemLevel = memLevel;
631 s->Strategy = strategy;
633 err = deflateInit2(&(s->stream), level,
634 method, windowBits, memLevel, strategy);
636 /* Check if a dictionary has been specified */
637 if (err == Z_OK && SvCUR(dictionary)) {
638 err = deflateSetDictionary(&(s->stream), (const Bytef*) SvPVX(dictionary),
640 s->dict_adler = s->stream.adler ;
652 XPUSHs(sv_setref_pv(sv_newmortal(),
653 "Compress::Zlib::deflateStream", (void*)s));
654 if (GIMME == G_ARRAY)
655 XPUSHs(sv_2mortal(newSViv(err))) ;
658 _inflateInit(windowBits, bufsize, dictionary)
668 warn("in _inflateInit(windowBits=%d, bufsize=%d, dictionary=%d\n",
669 windowBits, bufsize, SvCUR(dictionary)) ;
670 if ((s = InitStream(bufsize)) ) {
672 s->WindowBits = windowBits;
674 err = inflateInit2(&(s->stream), windowBits);
680 else if (SvCUR(dictionary)) {
681 /* Dictionary specified - take a copy for use in inflate */
682 s->dictionary = newSVsv(dictionary) ;
688 XPUSHs(sv_setref_pv(sv_newmortal(),
689 "Compress::Zlib::inflateStream", (void*)s));
690 if (GIMME == G_ARRAY)
691 XPUSHs(sv_2mortal(newSViv(err))) ;
695 MODULE = Compress::Zlib PACKAGE = Compress::Zlib::deflateStream
698 DispStream(s, message=NULL)
699 Compress::Zlib::deflateStream s
704 Compress::Zlib::deflateStream s
706 uLong outsize = NO_INIT
707 SV * output = NO_INIT
711 /* If the buffer is a reference, dereference it */
712 buf = deRef(buf, "deflate") ;
714 /* initialise the input buffer */
715 s->stream.next_in = (Bytef*)SvPV(buf, *(STRLEN*)&s->stream.avail_in) ;
716 /* s->stream.next_in = (Bytef*)SvPVX(buf); */
717 s->stream.avail_in = SvCUR(buf) ;
719 /* and the output buffer */
720 /* output = sv_2mortal(newSVpv("", s->bufinc)) ; */
721 output = sv_2mortal(newSV(s->bufinc)) ;
723 SvCUR_set(output, 0) ;
724 outsize = s->bufinc ;
725 s->stream.next_out = (Bytef*) SvPVX(output) ;
726 s->stream.avail_out = outsize;
728 /* Check for saved output from deflateParams */
729 if (s->deflateParams_out_valid) {
730 *(s->stream.next_out) = s->deflateParams_out_byte;
731 ++ s->stream.next_out;
732 -- s->stream.avail_out ;
733 s->deflateParams_out_valid = FALSE;
736 while (s->stream.avail_in != 0) {
738 if (s->stream.avail_out == 0) {
740 SvGROW(output, outsize + s->bufinc) ;
741 s->stream.next_out = (Bytef*) SvPVX(output) + outsize ;
742 outsize += s->bufinc ;
743 s->stream.avail_out = s->bufinc ;
745 err = deflate(&(s->stream), Z_NO_FLUSH);
752 SvCUR_set(output, outsize - s->stream.avail_out) ;
755 output = &PL_sv_undef ;
757 if (GIMME == G_ARRAY)
758 XPUSHs(sv_2mortal(newSViv(err))) ;
764 Compress::Zlib::deflateStream s
766 uLong outsize = NO_INIT
767 SV * output = NO_INIT
771 s->stream.avail_in = 0; /* should be zero already anyway */
773 /* output = sv_2mortal(newSVpv("", s->bufinc)) ; */
774 output = sv_2mortal(newSV(s->bufinc)) ;
776 SvCUR_set(output, 0) ;
777 outsize = s->bufinc ;
778 s->stream.next_out = (Bytef*) SvPVX(output) ;
779 s->stream.avail_out = outsize;
781 /* Check for saved output from deflateParams */
782 if (s->deflateParams_out_valid) {
783 *(s->stream.next_out) = s->deflateParams_out_byte;
784 ++ s->stream.next_out;
785 -- s->stream.avail_out ;
786 s->deflateParams_out_valid = FALSE;
790 if (s->stream.avail_out == 0) {
791 /* consumed all the available output, so extend it */
793 SvGROW(output, outsize + s->bufinc) ;
794 s->stream.next_out = (Bytef*)SvPVX(output) + outsize ;
795 outsize += s->bufinc ;
796 s->stream.avail_out = s->bufinc ;
798 err = deflate(&(s->stream), f);
800 /* deflate has finished flushing only when it hasn't used up
801 * all the available space in the output buffer:
803 if (s->stream.avail_out != 0 || err != Z_OK )
807 err = (err == Z_STREAM_END ? Z_OK : err) ;
811 SvCUR_set(output, outsize - s->stream.avail_out) ;
814 output = &PL_sv_undef ;
816 if (GIMME == G_ARRAY)
817 XPUSHs(sv_2mortal(newSViv(err))) ;
820 _deflateParams(s, flags, level, strategy, bufsize)
821 Compress::Zlib::deflateStream s
830 s->Strategy = strategy ;
832 s->bufsize = bufsize;
835 s->stream.avail_in = 0;
836 s->stream.next_out = &(s->deflateParams_out_byte) ;
837 s->stream.avail_out = 1;
838 RETVAL = deflateParams(&(s->stream), s->Level, s->Strategy);
839 s->deflateParams_out_valid =
840 (RETVAL == Z_OK && s->stream.avail_out == 0) ;
847 Compress::Zlib::deflateStream s
855 Compress::Zlib::deflateStream s
857 RETVAL = s->Strategy ;
863 Compress::Zlib::deflateStream s
865 deflateEnd(&s->stream) ;
867 SvREFCNT_dec(s->dictionary) ;
873 Compress::Zlib::deflateStream s
875 RETVAL = s->dict_adler ;
881 Compress::Zlib::deflateStream s
883 RETVAL = s->stream.total_in ;
889 Compress::Zlib::deflateStream s
891 RETVAL = s->stream.total_out ;
897 Compress::Zlib::deflateStream s
899 RETVAL = s->stream.msg;
904 MODULE = Compress::Zlib PACKAGE = Compress::Zlib::inflateStream
907 DispStream(s, message=NULL)
908 Compress::Zlib::inflateStream s
913 Compress::Zlib::inflateStream s
915 uLong outsize = NO_INIT
916 SV * output = NO_INIT
922 /* If the buffer is a reference, dereference it */
923 buf = deRef(buf, "inflate") ;
925 /* initialise the input buffer */
926 s->stream.next_in = (Bytef*)SvPVX(buf) ;
927 s->stream.avail_in = SvCUR(buf) ;
929 /* and the output buffer */
930 output = sv_2mortal(newSV(s->bufinc+1)) ;
932 SvCUR_set(output, 0) ;
933 outsize = s->bufinc ;
934 s->stream.next_out = (Bytef*) SvPVX(output) ;
935 s->stream.avail_out = outsize;
939 if (s->stream.avail_out == 0) {
941 SvGROW(output, outsize + s->bufinc+1) ;
942 s->stream.next_out = (Bytef*) SvPVX(output) + outsize ;
943 outsize += s->bufinc ;
944 s->stream.avail_out = s->bufinc ;
947 err = inflate(&(s->stream), Z_SYNC_FLUSH);
948 if (err == Z_BUF_ERROR) {
949 if (s->stream.avail_out == 0)
951 if (s->stream.avail_in == 0) {
957 if (err == Z_NEED_DICT && s->dictionary) {
958 s->dict_adler = s->stream.adler ;
959 err = inflateSetDictionary(&(s->stream),
960 (const Bytef*)SvPVX(s->dictionary),
961 SvCUR(s->dictionary));
968 if (err == Z_OK || err == Z_STREAM_END || err == Z_DATA_ERROR) {
972 SvCUR_set(output, outsize - s->stream.avail_out) ;
973 *SvEND(output) = '\0';
975 /* fix the input buffer */
977 in = s->stream.avail_in ;
980 Move(s->stream.next_in, SvPVX(buf), in, char) ;
986 output = &PL_sv_undef ;
988 if (GIMME == G_ARRAY)
989 XPUSHs(sv_2mortal(newSViv(err))) ;
993 Compress::Zlib::inflateStream s
997 /* If the buffer is a reference, dereference it */
998 buf = deRef(buf, "inflateSync") ;
1000 /* initialise the input buffer */
1001 s->stream.next_in = (Bytef*)SvPVX(buf) ;
1002 s->stream.avail_in = SvCUR(buf) ;
1004 /* inflateSync doesn't create any output */
1005 s->stream.next_out = (Bytef*) NULL;
1006 s->stream.avail_out = 0;
1008 RETVAL = inflateSync(&(s->stream));
1010 /* fix the input buffer */
1011 unsigned in = s->stream.avail_in ;
1013 SvCUR_set(buf, in) ;
1015 Move(s->stream.next_in, SvPVX(buf), in, char) ;
1024 Compress::Zlib::inflateStream s
1026 inflateEnd(&s->stream) ;
1028 SvREFCNT_dec(s->dictionary) ;
1034 Compress::Zlib::inflateStream s
1036 RETVAL = s->dict_adler ;
1042 Compress::Zlib::inflateStream s
1044 RETVAL = s->stream.total_in ;
1050 Compress::Zlib::inflateStream s
1052 RETVAL = s->stream.total_out ;
1058 Compress::Zlib::inflateStream s
1060 RETVAL = s->stream.msg;