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
355 RETVAL = ZLIB_VERNUM ;
357 /* 1.1.4 => 0x1140 */
358 RETVAL = (ZLIB_VERSION[0] - '0') << 12 ;
359 RETVAL += (ZLIB_VERSION[2] - '0') << 8 ;
360 RETVAL += (ZLIB_VERSION[4] - '0') << 4 ;
368 DispStream(s, message=NULL)
369 Compress::Zlib::inflateStream s
372 Compress::Zlib::gzFile
378 gz = gzopen(path, mode) ;
380 ZMALLOC(RETVAL, gzType) ;
381 RETVAL->buffer = newSV(SIZE) ;
382 SvPOK_only(RETVAL->buffer) ;
383 SvCUR_set(RETVAL->buffer, 0) ;
386 RETVAL->closed = FALSE ;
391 SetGzErrorNo(errno ? Z_ERRNO : Z_MEM_ERROR) ;
397 Compress::Zlib::gzFile
398 gzdopen_(fh, mode, offset)
405 lseek(fh, offset, 0) ;
406 gz = gzdopen(fh, mode) ;
408 ZMALLOC(RETVAL, gzType) ;
409 RETVAL->buffer = newSV(SIZE) ;
410 SvPOK_only(RETVAL->buffer) ;
411 SvCUR_set(RETVAL->buffer, 0) ;
414 RETVAL->closed = FALSE ;
419 SetGzErrorNo(errno ? Z_ERRNO : Z_MEM_ERROR) ;
425 MODULE = Compress::Zlib PACKAGE = Compress::Zlib::gzFile PREFIX = Zip_
427 #define Zip_gzread(file, buf, len) gzread(file->gz, bufp, len)
430 Zip_gzread(file, buf, len=4096)
431 Compress::Zlib::gzFile file
438 if (SvREADONLY(buf) && PL_curcop != &PL_compiling)
439 croak("gzread: buffer parameter is read-only");
440 SvUPGRADE(buf, SVt_PV);
443 /* any left over from gzreadline ? */
444 if ((bufsize = SvCUR(file->buffer)) > 0) {
457 sv_catpvn(buf, SvPVX(file->buffer) + file->offset, movesize);
459 file->offset += movesize ;
460 SvCUR_set(file->buffer, bufsize - movesize) ;
464 bufp = (Byte*)SvGROW(buf, bufsize+len+1);
465 RETVAL = gzread(file->gz, ((Bytef*)bufp)+bufsize, len) ;
466 SetGzError(file->gz) ;
469 SvCUR_set(buf, RETVAL) ;
478 gzreadline(file, buf)
479 Compress::Zlib::gzFile file
483 if (SvREADONLY(buf) && PL_curcop != &PL_compiling)
484 croak("gzreadline: buffer parameter is read-only");
485 SvUPGRADE(buf, SVt_PV);
487 /* sv_setpvn(buf, "", SIZE) ; */
490 RETVAL = gzreadline(file, buf) ;
491 SetGzError(file->gz) ;
497 /* SvCUR(buf) = RETVAL; */
498 /* Don't need to explicitly terminate with '\0', because
499 sv_catpvn aready has */
502 #define Zip_gzwrite(file, buf) gzwrite(file->gz, buf, (unsigned)len)
504 Zip_gzwrite(file, buf)
505 Compress::Zlib::gzFile file
507 voidp buf = (voidp)SvPV(ST(1), len) ;
509 SetGzError(file->gz) ;
511 #define Zip_gzflush(file, flush) gzflush(file->gz, flush)
513 Zip_gzflush(file, flush)
514 Compress::Zlib::gzFile file
517 SetGzError(file->gz) ;
519 #define Zip_gzclose(file) file->closed ? 0 : gzclose(file->gz)
522 Compress::Zlib::gzFile file
524 file->closed = TRUE ;
525 SetGzErrorNo(RETVAL) ;
528 #define Zip_gzeof(file) gzeof(file->gz)
531 Compress::Zlib::gzFile file
534 croak("gzeof needs zlib 1.0.6 or better") ;
536 RETVAL = gzeof(file->gz);
542 #define Zip_gzsetparams(file,l,s) gzsetparams(file->gz,l,s)
544 Zip_gzsetparams(file, level, strategy)
545 Compress::Zlib::gzFile file
550 croak("gzsetparams needs zlib 1.0.6 or better") ;
552 RETVAL = gzsetparams(file->gz, level, strategy);
559 Compress::Zlib::gzFile file
563 SvREFCNT_dec(file->buffer) ;
564 safefree((char*)file) ;
566 #define Zip_gzerror(file) (char*)gzerror(file->gz, &errnum)
570 Compress::Zlib::gzFile file
573 sv_setiv(ST(0), errnum) ;
578 MODULE = Compress::Zlib PACKAGE = Compress::Zlib PREFIX = Zip_
581 #define Zip_adler32(buf, adler) adler32(adler, buf, (uInt)len)
584 Zip_adler32(buf, adler=adlerInitial)
585 uLong adler = NO_INIT
587 Bytef * buf = NO_INIT
590 /* If the buffer is a reference, dereference it */
591 sv = deRef(sv, "adler32") ;
592 buf = (Byte*)SvPV(sv, len) ;
595 adler = adlerInitial;
596 else if (SvOK(ST(1)))
597 adler = SvUV(ST(1)) ;
599 adler = adlerInitial;
601 #define Zip_crc32(buf, crc) crc32(crc, buf, (uInt)len)
604 Zip_crc32(buf, crc=crcInitial)
607 Bytef * buf = NO_INIT
610 /* If the buffer is a reference, dereference it */
611 sv = deRef(sv, "crc32") ;
612 buf = (Byte*)SvPV(sv, len) ;
616 else if (SvOK(ST(1)))
621 MODULE = Compress::Zlib PACKAGE = Compress::Zlib
624 _deflateInit(level, method, windowBits, memLevel, strategy, bufsize, dictionary)
638 warn("in _deflateInit(level=%d, method=%d, windowBits=%d, memLevel=%d, strategy=%d, bufsize=%d\n",
639 level, method, windowBits, memLevel, strategy, bufsize) ;
640 if ((s = InitStream(bufsize)) ) {
644 s->WindowBits = windowBits;
645 s->MemLevel = memLevel;
646 s->Strategy = strategy;
648 err = deflateInit2(&(s->stream), level,
649 method, windowBits, memLevel, strategy);
651 /* Check if a dictionary has been specified */
652 if (err == Z_OK && SvCUR(dictionary)) {
653 err = deflateSetDictionary(&(s->stream), (const Bytef*) SvPVX(dictionary),
655 s->dict_adler = s->stream.adler ;
667 XPUSHs(sv_setref_pv(sv_newmortal(),
668 "Compress::Zlib::deflateStream", (void*)s));
669 if (GIMME == G_ARRAY)
670 XPUSHs(sv_2mortal(newSViv(err))) ;
673 _inflateInit(windowBits, bufsize, dictionary)
683 warn("in _inflateInit(windowBits=%d, bufsize=%d, dictionary=%d\n",
684 windowBits, bufsize, SvCUR(dictionary)) ;
685 if ((s = InitStream(bufsize)) ) {
687 s->WindowBits = windowBits;
689 err = inflateInit2(&(s->stream), windowBits);
695 else if (SvCUR(dictionary)) {
696 /* Dictionary specified - take a copy for use in inflate */
697 s->dictionary = newSVsv(dictionary) ;
703 XPUSHs(sv_setref_pv(sv_newmortal(),
704 "Compress::Zlib::inflateStream", (void*)s));
705 if (GIMME == G_ARRAY)
706 XPUSHs(sv_2mortal(newSViv(err))) ;
710 MODULE = Compress::Zlib PACKAGE = Compress::Zlib::deflateStream
713 DispStream(s, message=NULL)
714 Compress::Zlib::deflateStream s
719 Compress::Zlib::deflateStream s
721 uLong outsize = NO_INIT
722 SV * output = NO_INIT
726 /* If the buffer is a reference, dereference it */
727 buf = deRef(buf, "deflate") ;
729 /* initialise the input buffer */
730 s->stream.next_in = (Bytef*)SvPV(buf, *(STRLEN*)&s->stream.avail_in) ;
731 /* s->stream.next_in = (Bytef*)SvPVX(buf); */
732 s->stream.avail_in = SvCUR(buf) ;
734 /* and the output buffer */
735 /* output = sv_2mortal(newSVpv("", s->bufinc)) ; */
736 output = sv_2mortal(newSV(s->bufinc)) ;
738 SvCUR_set(output, 0) ;
739 outsize = s->bufinc ;
740 s->stream.next_out = (Bytef*) SvPVX(output) ;
741 s->stream.avail_out = outsize;
743 /* Check for saved output from deflateParams */
744 if (s->deflateParams_out_valid) {
745 *(s->stream.next_out) = s->deflateParams_out_byte;
746 ++ s->stream.next_out;
747 -- s->stream.avail_out ;
748 s->deflateParams_out_valid = FALSE;
751 while (s->stream.avail_in != 0) {
753 if (s->stream.avail_out == 0) {
755 SvGROW(output, outsize + s->bufinc) ;
756 s->stream.next_out = (Bytef*) SvPVX(output) + outsize ;
757 outsize += s->bufinc ;
758 s->stream.avail_out = s->bufinc ;
760 err = deflate(&(s->stream), Z_NO_FLUSH);
767 SvCUR_set(output, outsize - s->stream.avail_out) ;
770 output = &PL_sv_undef ;
772 if (GIMME == G_ARRAY)
773 XPUSHs(sv_2mortal(newSViv(err))) ;
779 Compress::Zlib::deflateStream s
781 uLong outsize = NO_INIT
782 SV * output = NO_INIT
786 s->stream.avail_in = 0; /* should be zero already anyway */
788 /* output = sv_2mortal(newSVpv("", s->bufinc)) ; */
789 output = sv_2mortal(newSV(s->bufinc)) ;
791 SvCUR_set(output, 0) ;
792 outsize = s->bufinc ;
793 s->stream.next_out = (Bytef*) SvPVX(output) ;
794 s->stream.avail_out = outsize;
796 /* Check for saved output from deflateParams */
797 if (s->deflateParams_out_valid) {
798 *(s->stream.next_out) = s->deflateParams_out_byte;
799 ++ s->stream.next_out;
800 -- s->stream.avail_out ;
801 s->deflateParams_out_valid = FALSE;
805 if (s->stream.avail_out == 0) {
806 /* consumed all the available output, so extend it */
808 SvGROW(output, outsize + s->bufinc) ;
809 s->stream.next_out = (Bytef*)SvPVX(output) + outsize ;
810 outsize += s->bufinc ;
811 s->stream.avail_out = s->bufinc ;
813 err = deflate(&(s->stream), f);
815 /* deflate has finished flushing only when it hasn't used up
816 * all the available space in the output buffer:
818 if (s->stream.avail_out != 0 || err != Z_OK )
822 err = (err == Z_STREAM_END ? Z_OK : err) ;
826 SvCUR_set(output, outsize - s->stream.avail_out) ;
829 output = &PL_sv_undef ;
831 if (GIMME == G_ARRAY)
832 XPUSHs(sv_2mortal(newSViv(err))) ;
835 _deflateParams(s, flags, level, strategy, bufsize)
836 Compress::Zlib::deflateStream s
845 s->Strategy = strategy ;
847 s->bufsize = bufsize;
850 s->stream.avail_in = 0;
851 s->stream.next_out = &(s->deflateParams_out_byte) ;
852 s->stream.avail_out = 1;
853 RETVAL = deflateParams(&(s->stream), s->Level, s->Strategy);
854 s->deflateParams_out_valid =
855 (RETVAL == Z_OK && s->stream.avail_out == 0) ;
862 Compress::Zlib::deflateStream s
870 Compress::Zlib::deflateStream s
872 RETVAL = s->Strategy ;
878 Compress::Zlib::deflateStream s
880 deflateEnd(&s->stream) ;
882 SvREFCNT_dec(s->dictionary) ;
888 Compress::Zlib::deflateStream s
890 RETVAL = s->dict_adler ;
896 Compress::Zlib::deflateStream s
898 RETVAL = s->stream.total_in ;
904 Compress::Zlib::deflateStream s
906 RETVAL = s->stream.total_out ;
912 Compress::Zlib::deflateStream s
914 RETVAL = s->stream.msg;
919 MODULE = Compress::Zlib PACKAGE = Compress::Zlib::inflateStream
922 DispStream(s, message=NULL)
923 Compress::Zlib::inflateStream s
928 Compress::Zlib::inflateStream s
930 uLong outsize = NO_INIT
931 SV * output = NO_INIT
937 /* If the buffer is a reference, dereference it */
938 buf = deRef(buf, "inflate") ;
940 /* initialise the input buffer */
941 s->stream.next_in = (Bytef*)SvPVX(buf) ;
942 s->stream.avail_in = SvCUR(buf) ;
944 /* and the output buffer */
945 output = sv_2mortal(newSV(s->bufinc+1)) ;
947 SvCUR_set(output, 0) ;
948 outsize = s->bufinc ;
949 s->stream.next_out = (Bytef*) SvPVX(output) ;
950 s->stream.avail_out = outsize;
954 if (s->stream.avail_out == 0) {
956 SvGROW(output, outsize + s->bufinc+1) ;
957 s->stream.next_out = (Bytef*) SvPVX(output) + outsize ;
958 outsize += s->bufinc ;
959 s->stream.avail_out = s->bufinc ;
962 err = inflate(&(s->stream), Z_SYNC_FLUSH);
963 if (err == Z_BUF_ERROR) {
964 if (s->stream.avail_out == 0)
966 if (s->stream.avail_in == 0) {
972 if (err == Z_NEED_DICT && s->dictionary) {
973 s->dict_adler = s->stream.adler ;
974 err = inflateSetDictionary(&(s->stream),
975 (const Bytef*)SvPVX(s->dictionary),
976 SvCUR(s->dictionary));
983 if (err == Z_OK || err == Z_STREAM_END || err == Z_DATA_ERROR) {
987 SvCUR_set(output, outsize - s->stream.avail_out) ;
988 *SvEND(output) = '\0';
990 /* fix the input buffer */
992 in = s->stream.avail_in ;
995 Move(s->stream.next_in, SvPVX(buf), in, char) ;
1001 output = &PL_sv_undef ;
1003 if (GIMME == G_ARRAY)
1004 XPUSHs(sv_2mortal(newSViv(err))) ;
1007 inflateSync (s, buf)
1008 Compress::Zlib::inflateStream s
1012 /* If the buffer is a reference, dereference it */
1013 buf = deRef(buf, "inflateSync") ;
1015 /* initialise the input buffer */
1016 s->stream.next_in = (Bytef*)SvPVX(buf) ;
1017 s->stream.avail_in = SvCUR(buf) ;
1019 /* inflateSync doesn't create any output */
1020 s->stream.next_out = (Bytef*) NULL;
1021 s->stream.avail_out = 0;
1023 RETVAL = inflateSync(&(s->stream));
1025 /* fix the input buffer */
1026 unsigned in = s->stream.avail_in ;
1028 SvCUR_set(buf, in) ;
1030 Move(s->stream.next_in, SvPVX(buf), in, char) ;
1039 Compress::Zlib::inflateStream s
1041 inflateEnd(&s->stream) ;
1043 SvREFCNT_dec(s->dictionary) ;
1049 Compress::Zlib::inflateStream s
1051 RETVAL = s->dict_adler ;
1057 Compress::Zlib::inflateStream s
1059 RETVAL = s->stream.total_in ;
1065 Compress::Zlib::inflateStream s
1067 RETVAL = s->stream.total_out ;
1073 Compress::Zlib::inflateStream s
1075 RETVAL = s->stream.msg;