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
51 typedef struct di_stream {
57 bool deflateParams_out_valid ;
58 Bytef deflateParams_out_byte;
66 typedef di_stream * deflateStream ;
67 typedef di_stream * Compress__Zlib__deflateStream ;
68 typedef di_stream * inflateStream ;
69 typedef di_stream * Compress__Zlib__inflateStream ;
71 /* typedef gzFile Compress__Zlib__gzFile ; */
72 typedef struct gzType {
79 typedef gzType* Compress__Zlib__gzFile ;
83 #define GZERRNO "Compress::Zlib::gzerrno"
85 #define ZMALLOC(to, typ) ((to = (typ *)safemalloc(sizeof(typ))), \
88 #define adlerInitial adler32(0L, Z_NULL, 0)
89 #define crcInitial crc32(0L, Z_NULL, 0)
92 static const char * const my_z_errmsg[] = {
93 "need dictionary", /* Z_NEED_DICT 2 */
94 "stream end", /* Z_STREAM_END 1 */
96 "file error", /* Z_ERRNO (-1) */
97 "stream error", /* Z_STREAM_ERROR (-2) */
98 "data error", /* Z_DATA_ERROR (-3) */
99 "insufficient memory", /* Z_MEM_ERROR (-4) */
100 "buffer error", /* Z_BUF_ERROR (-5) */
101 "incompatible version",/* Z_VERSION_ERROR(-6) */
105 #if defined(__SYMBIAN32__)
106 # define NO_WRITEABLE_DATA
109 #define TRACE_DEFAULT 0
111 #ifdef NO_WRITEABLE_DATA
112 #define trace TRACE_DEFAULT
114 static int trace = TRACE_DEFAULT ;
117 /* Dodge PerlIO hiding of these functions. */
122 SetGzErrorNo(int error_no)
124 SetGzErrorNo(error_no)
130 SV * gzerror_sv = perl_get_sv(GZERRNO, FALSE) ;
132 if (error_no == Z_ERRNO) {
134 errstr = Strerror(errno) ;
137 /* errstr = gzerror(fil, &error_no) ; */
138 errstr = (char*) my_z_errmsg[2 - error_no];
140 if (SvIV(gzerror_sv) != error_no) {
141 sv_setiv(gzerror_sv, error_no) ;
142 sv_setpv(gzerror_sv, errstr) ;
143 SvIOK_on(gzerror_sv) ;
150 SetGzError(gzFile file)
158 (void)gzerror(file, &error_no) ;
159 SetGzErrorNo(error_no) ;
164 DispHex(void * ptr, int length)
171 char * p = (char*)ptr;
173 for (i = 0; i < length; ++i) {
174 printf(" %02x", 0xFF & *(p+i));
181 DispStream(di_stream * s, char * message)
183 DispStream(s, message)
194 printf("DispStream 0x%p - %s \n", s, message) ;
197 printf(" stream pointer is NULL\n");
200 printf(" stream 0x%p\n", &(s->stream));
201 printf(" zalloc 0x%p\n", s->stream.zalloc);
202 printf(" zfree 0x%p\n", s->stream.zfree);
203 printf(" opaque 0x%p\n", s->stream.opaque);
205 printf(" msg %s\n", s->stream.msg);
208 printf(" next_in 0x%p", s->stream.next_in);
209 if (s->stream.next_in) {
211 DispHex(s->stream.next_in, 4);
215 printf(" next_out 0x%p", s->stream.next_out);
216 if (s->stream.next_out){
218 DispHex(s->stream.next_out, 4);
222 printf(" avail_in %ld\n", s->stream.avail_in);
223 printf(" avail_out %ld\n", s->stream.avail_out);
224 printf(" total_in %ld\n", s->stream.total_in);
225 printf(" total_out %ld\n", s->stream.total_out);
226 printf(" adler 0x%lx\n", s->stream.adler);
227 printf(" reserved 0x%lx\n", s->stream.reserved);
228 printf(" bufsize %ld\n", s->bufsize);
229 printf(" dictionary 0x%p\n", s->dictionary);
230 printf(" dict_adler 0x%ld\n", s->dict_adler);
239 InitStream(uLong bufsize)
247 ZMALLOC(s, di_stream) ;
250 s->bufsize = bufsize ;
251 s->bufinc = bufsize ;
262 gzreadline(Compress__Zlib__gzFile file, SV * output)
264 gzreadline(file, output)
265 Compress__Zlib__gzFile file ;
270 SV * store = file->buffer ;
273 char *out_ptr = SvPVX(store) ;
278 /* anything left from last time */
279 if ((n = SvCUR(store))) {
281 out_ptr = SvPVX(store) + file->offset ;
282 if ((p = ninstr(out_ptr, out_ptr + n - 1, nl, nl))) {
283 /* if (rschar != 0777 && */
284 /* p = ninstr(out_ptr, out_ptr + n - 1, rs, rs+rslen-1)) { */
286 sv_catpvn(output, out_ptr, p - out_ptr + 1);
288 file->offset += (p - out_ptr + 1) ;
289 n = n - (p - out_ptr + 1);
290 SvCUR_set(store, n) ;
291 return SvCUR(output);
293 else /* no EOL, so append the complete buffer */
294 sv_catpvn(output, out_ptr, n);
299 SvCUR_set(store, 0) ;
301 out_ptr = SvPVX(store) ;
303 n = gzread(file->gz, out_ptr, SIZE) ;
306 /* Either EOF or an error */
307 /* so return what we have so far else signal eof */
308 return (SvCUR(output)>0) ? SvCUR(output) : n ;
310 SvCUR_set(store, n) ;
316 deRef(SV * sv, char * string)
330 croak("%s: buffer parameter is not a SCALAR reference", string);
333 croak("%s: buffer parameter is a reference to a reference", string) ;
342 #include "constants.h"
344 MODULE = Compress::Zlib PACKAGE = Compress::Zlib PREFIX = Zip_
349 INCLUDE: constants.xs
352 /* Check this version of zlib is == 1 */
353 if (zlibVersion()[0] != '1')
354 croak("Compress::Zlib needs zlib version 1.x\n") ;
357 /* Create the $gzerror scalar */
358 SV * gzerror_sv = perl_get_sv(GZERRNO, GV_ADDMULTI) ;
359 sv_setiv(gzerror_sv, 0) ;
360 sv_setpv(gzerror_sv, "") ;
361 SvIOK_on(gzerror_sv) ;
365 #define Zip_zlib_version() (char*)zlib_version
373 RETVAL = ZLIB_VERNUM ;
375 /* 1.1.4 => 0x1140 */
376 RETVAL = (ZLIB_VERSION[0] - '0') << 12 ;
377 RETVAL += (ZLIB_VERSION[2] - '0') << 8 ;
378 RETVAL += (ZLIB_VERSION[4] - '0') << 4 ;
386 DispStream(s, message=NULL)
387 Compress::Zlib::inflateStream s
390 Compress::Zlib::gzFile
396 gz = gzopen(path, mode) ;
398 ZMALLOC(RETVAL, gzType) ;
399 RETVAL->buffer = newSV(SIZE) ;
400 SvPOK_only(RETVAL->buffer) ;
401 SvCUR_set(RETVAL->buffer, 0) ;
404 RETVAL->closed = FALSE ;
409 SetGzErrorNo(errno ? Z_ERRNO : Z_MEM_ERROR) ;
415 Compress::Zlib::gzFile
416 gzdopen_(fh, mode, offset)
423 lseek(fh, offset, 0) ;
424 gz = gzdopen(fh, mode) ;
426 ZMALLOC(RETVAL, gzType) ;
427 RETVAL->buffer = newSV(SIZE) ;
428 SvPOK_only(RETVAL->buffer) ;
429 SvCUR_set(RETVAL->buffer, 0) ;
432 RETVAL->closed = FALSE ;
437 SetGzErrorNo(errno ? Z_ERRNO : Z_MEM_ERROR) ;
443 MODULE = Compress::Zlib PACKAGE = Compress::Zlib::gzFile PREFIX = Zip_
445 #define Zip_gzread(file, buf, len) gzread(file->gz, bufp, len)
448 Zip_gzread(file, buf, len=4096)
449 Compress::Zlib::gzFile file
456 if (SvREADONLY(buf) && PL_curcop != &PL_compiling)
457 croak("gzread: buffer parameter is read-only");
458 SvUPGRADE(buf, SVt_PV);
461 /* any left over from gzreadline ? */
462 if ((bufsize = SvCUR(file->buffer)) > 0) {
475 sv_catpvn(buf, SvPVX(file->buffer) + file->offset, movesize);
477 file->offset += movesize ;
478 SvCUR_set(file->buffer, bufsize - movesize) ;
482 bufp = (Byte*)SvGROW(buf, bufsize+len+1);
483 RETVAL = gzread(file->gz, ((Bytef*)bufp)+bufsize, len) ;
484 SetGzError(file->gz) ;
487 SvCUR_set(buf, RETVAL) ;
496 gzreadline(file, buf)
497 Compress::Zlib::gzFile file
501 if (SvREADONLY(buf) && PL_curcop != &PL_compiling)
502 croak("gzreadline: buffer parameter is read-only");
503 SvUPGRADE(buf, SVt_PV);
505 /* sv_setpvn(buf, "", SIZE) ; */
508 RETVAL = gzreadline(file, buf) ;
509 SetGzError(file->gz) ;
515 /* SvCUR(buf) = RETVAL; */
516 /* Don't need to explicitly terminate with '\0', because
517 sv_catpvn aready has */
520 #define Zip_gzwrite(file, buf) gzwrite(file->gz, buf, (unsigned)len)
522 Zip_gzwrite(file, buf)
523 Compress::Zlib::gzFile file
525 voidp buf = (voidp)SvPV(ST(1), len) ;
527 SetGzError(file->gz) ;
529 #define Zip_gzflush(file, flush) gzflush(file->gz, flush)
531 Zip_gzflush(file, flush)
532 Compress::Zlib::gzFile file
535 SetGzError(file->gz) ;
537 #define Zip_gzclose(file) file->closed ? 0 : gzclose(file->gz)
540 Compress::Zlib::gzFile file
542 file->closed = TRUE ;
543 SetGzErrorNo(RETVAL) ;
546 #define Zip_gzeof(file) gzeof(file->gz)
549 Compress::Zlib::gzFile file
552 croak("gzeof needs zlib 1.0.6 or better") ;
554 RETVAL = gzeof(file->gz);
560 #define Zip_gzsetparams(file,l,s) gzsetparams(file->gz,l,s)
562 Zip_gzsetparams(file, level, strategy)
563 Compress::Zlib::gzFile file
568 croak("gzsetparams needs zlib 1.0.6 or better") ;
570 RETVAL = gzsetparams(file->gz, level, strategy);
577 Compress::Zlib::gzFile file
581 SvREFCNT_dec(file->buffer) ;
582 safefree((char*)file) ;
584 #define Zip_gzerror(file) (char*)gzerror(file->gz, &errnum)
588 Compress::Zlib::gzFile file
591 sv_setiv(ST(0), errnum) ;
596 MODULE = Compress::Zlib PACKAGE = Compress::Zlib PREFIX = Zip_
599 #define Zip_adler32(buf, adler) adler32(adler, buf, (uInt)len)
602 Zip_adler32(buf, adler=adlerInitial)
603 uLong adler = NO_INIT
605 Bytef * buf = NO_INIT
608 /* If the buffer is a reference, dereference it */
609 sv = deRef(sv, "adler32") ;
610 buf = (Byte*)SvPV(sv, len) ;
613 adler = adlerInitial;
614 else if (SvOK(ST(1)))
615 adler = SvUV(ST(1)) ;
617 adler = adlerInitial;
619 #define Zip_crc32(buf, crc) crc32(crc, buf, (uInt)len)
622 Zip_crc32(buf, crc=crcInitial)
625 Bytef * buf = NO_INIT
628 /* If the buffer is a reference, dereference it */
629 sv = deRef(sv, "crc32") ;
630 buf = (Byte*)SvPV(sv, len) ;
634 else if (SvOK(ST(1)))
639 MODULE = Compress::Zlib PACKAGE = Compress::Zlib
642 _deflateInit(level, method, windowBits, memLevel, strategy, bufsize, dictionary)
656 warn("in _deflateInit(level=%d, method=%d, windowBits=%d, memLevel=%d, strategy=%d, bufsize=%d\n",
657 level, method, windowBits, memLevel, strategy, bufsize) ;
658 if ((s = InitStream(bufsize)) ) {
662 s->WindowBits = windowBits;
663 s->MemLevel = memLevel;
664 s->Strategy = strategy;
666 err = deflateInit2(&(s->stream), level,
667 method, windowBits, memLevel, strategy);
669 /* Check if a dictionary has been specified */
670 if (err == Z_OK && SvCUR(dictionary)) {
671 err = deflateSetDictionary(&(s->stream), (const Bytef*) SvPVX(dictionary),
673 s->dict_adler = s->stream.adler ;
685 XPUSHs(sv_setref_pv(sv_newmortal(),
686 "Compress::Zlib::deflateStream", (void*)s));
687 if (GIMME == G_ARRAY)
688 XPUSHs(sv_2mortal(newSViv(err))) ;
691 _inflateInit(windowBits, bufsize, dictionary)
701 warn("in _inflateInit(windowBits=%d, bufsize=%d, dictionary=%d\n",
702 windowBits, bufsize, SvCUR(dictionary)) ;
703 if ((s = InitStream(bufsize)) ) {
705 s->WindowBits = windowBits;
707 err = inflateInit2(&(s->stream), windowBits);
713 else if (SvCUR(dictionary)) {
714 /* Dictionary specified - take a copy for use in inflate */
715 s->dictionary = newSVsv(dictionary) ;
721 XPUSHs(sv_setref_pv(sv_newmortal(),
722 "Compress::Zlib::inflateStream", (void*)s));
723 if (GIMME == G_ARRAY)
724 XPUSHs(sv_2mortal(newSViv(err))) ;
728 MODULE = Compress::Zlib PACKAGE = Compress::Zlib::deflateStream
731 DispStream(s, message=NULL)
732 Compress::Zlib::deflateStream s
737 Compress::Zlib::deflateStream s
739 uLong outsize = NO_INIT
740 SV * output = NO_INIT
744 /* If the buffer is a reference, dereference it */
745 buf = deRef(buf, "deflate") ;
747 /* initialise the input buffer */
748 s->stream.next_in = (Bytef*)SvPV(buf, *(STRLEN*)&s->stream.avail_in) ;
749 /* s->stream.next_in = (Bytef*)SvPVX(buf); */
750 s->stream.avail_in = SvCUR(buf) ;
752 /* and the output buffer */
753 /* output = sv_2mortal(newSVpv("", s->bufinc)) ; */
754 output = sv_2mortal(newSV(s->bufinc)) ;
756 SvCUR_set(output, 0) ;
757 outsize = s->bufinc ;
758 s->stream.next_out = (Bytef*) SvPVX(output) ;
759 s->stream.avail_out = outsize;
761 /* Check for saved output from deflateParams */
762 if (s->deflateParams_out_valid) {
763 *(s->stream.next_out) = s->deflateParams_out_byte;
764 ++ s->stream.next_out;
765 -- s->stream.avail_out ;
766 s->deflateParams_out_valid = FALSE;
769 while (s->stream.avail_in != 0) {
771 if (s->stream.avail_out == 0) {
773 SvGROW(output, outsize + s->bufinc) ;
774 s->stream.next_out = (Bytef*) SvPVX(output) + outsize ;
775 outsize += s->bufinc ;
776 s->stream.avail_out = s->bufinc ;
778 err = deflate(&(s->stream), Z_NO_FLUSH);
785 SvCUR_set(output, outsize - s->stream.avail_out) ;
788 output = &PL_sv_undef ;
790 if (GIMME == G_ARRAY)
791 XPUSHs(sv_2mortal(newSViv(err))) ;
797 Compress::Zlib::deflateStream s
799 uLong outsize = NO_INIT
800 SV * output = NO_INIT
804 s->stream.avail_in = 0; /* should be zero already anyway */
806 /* output = sv_2mortal(newSVpv("", s->bufinc)) ; */
807 output = sv_2mortal(newSV(s->bufinc)) ;
809 SvCUR_set(output, 0) ;
810 outsize = s->bufinc ;
811 s->stream.next_out = (Bytef*) SvPVX(output) ;
812 s->stream.avail_out = outsize;
814 /* Check for saved output from deflateParams */
815 if (s->deflateParams_out_valid) {
816 *(s->stream.next_out) = s->deflateParams_out_byte;
817 ++ s->stream.next_out;
818 -- s->stream.avail_out ;
819 s->deflateParams_out_valid = FALSE;
823 if (s->stream.avail_out == 0) {
824 /* consumed all the available output, so extend it */
826 SvGROW(output, outsize + s->bufinc) ;
827 s->stream.next_out = (Bytef*)SvPVX(output) + outsize ;
828 outsize += s->bufinc ;
829 s->stream.avail_out = s->bufinc ;
831 err = deflate(&(s->stream), f);
833 /* deflate has finished flushing only when it hasn't used up
834 * all the available space in the output buffer:
836 if (s->stream.avail_out != 0 || err != Z_OK )
840 err = (err == Z_STREAM_END ? Z_OK : err) ;
844 SvCUR_set(output, outsize - s->stream.avail_out) ;
847 output = &PL_sv_undef ;
849 if (GIMME == G_ARRAY)
850 XPUSHs(sv_2mortal(newSViv(err))) ;
853 _deflateParams(s, flags, level, strategy, bufsize)
854 Compress::Zlib::deflateStream s
863 s->Strategy = strategy ;
865 s->bufsize = bufsize;
868 s->stream.avail_in = 0;
869 s->stream.next_out = &(s->deflateParams_out_byte) ;
870 s->stream.avail_out = 1;
871 RETVAL = deflateParams(&(s->stream), s->Level, s->Strategy);
872 s->deflateParams_out_valid =
873 (RETVAL == Z_OK && s->stream.avail_out == 0) ;
880 Compress::Zlib::deflateStream s
888 Compress::Zlib::deflateStream s
890 RETVAL = s->Strategy ;
896 Compress::Zlib::deflateStream s
898 deflateEnd(&s->stream) ;
900 SvREFCNT_dec(s->dictionary) ;
906 Compress::Zlib::deflateStream s
908 RETVAL = s->dict_adler ;
914 Compress::Zlib::deflateStream s
916 RETVAL = s->stream.total_in ;
922 Compress::Zlib::deflateStream s
924 RETVAL = s->stream.total_out ;
930 Compress::Zlib::deflateStream s
932 RETVAL = s->stream.msg;
937 MODULE = Compress::Zlib PACKAGE = Compress::Zlib::inflateStream
940 DispStream(s, message=NULL)
941 Compress::Zlib::inflateStream s
946 Compress::Zlib::inflateStream s
948 uLong outsize = NO_INIT
949 SV * output = NO_INIT
955 /* If the buffer is a reference, dereference it */
956 buf = deRef(buf, "inflate") ;
958 /* initialise the input buffer */
959 s->stream.next_in = (Bytef*)SvPVX(buf) ;
960 s->stream.avail_in = SvCUR(buf) ;
962 /* and the output buffer */
963 output = sv_2mortal(newSV(s->bufinc+1)) ;
965 SvCUR_set(output, 0) ;
966 outsize = s->bufinc ;
967 s->stream.next_out = (Bytef*) SvPVX(output) ;
968 s->stream.avail_out = outsize;
972 if (s->stream.avail_out == 0) {
974 SvGROW(output, outsize + s->bufinc+1) ;
975 s->stream.next_out = (Bytef*) SvPVX(output) + outsize ;
976 outsize += s->bufinc ;
977 s->stream.avail_out = s->bufinc ;
980 err = inflate(&(s->stream), Z_SYNC_FLUSH);
981 if (err == Z_BUF_ERROR) {
982 if (s->stream.avail_out == 0)
984 if (s->stream.avail_in == 0) {
990 if (err == Z_NEED_DICT && s->dictionary) {
991 s->dict_adler = s->stream.adler ;
992 err = inflateSetDictionary(&(s->stream),
993 (const Bytef*)SvPVX(s->dictionary),
994 SvCUR(s->dictionary));
1001 if (err == Z_OK || err == Z_STREAM_END || err == Z_DATA_ERROR) {
1005 SvCUR_set(output, outsize - s->stream.avail_out) ;
1006 *SvEND(output) = '\0';
1008 /* fix the input buffer */
1010 in = s->stream.avail_in ;
1011 SvCUR_set(buf, in) ;
1013 Move(s->stream.next_in, SvPVX(buf), in, char) ;
1019 output = &PL_sv_undef ;
1021 if (GIMME == G_ARRAY)
1022 XPUSHs(sv_2mortal(newSViv(err))) ;
1025 inflateSync (s, buf)
1026 Compress::Zlib::inflateStream s
1030 /* If the buffer is a reference, dereference it */
1031 buf = deRef(buf, "inflateSync") ;
1033 /* initialise the input buffer */
1034 s->stream.next_in = (Bytef*)SvPVX(buf) ;
1035 s->stream.avail_in = SvCUR(buf) ;
1037 /* inflateSync doesn't create any output */
1038 s->stream.next_out = (Bytef*) NULL;
1039 s->stream.avail_out = 0;
1041 RETVAL = inflateSync(&(s->stream));
1043 /* fix the input buffer */
1044 unsigned in = s->stream.avail_in ;
1046 SvCUR_set(buf, in) ;
1048 Move(s->stream.next_in, SvPVX(buf), in, char) ;
1057 Compress::Zlib::inflateStream s
1059 inflateEnd(&s->stream) ;
1061 SvREFCNT_dec(s->dictionary) ;
1067 Compress::Zlib::inflateStream s
1069 RETVAL = s->dict_adler ;
1075 Compress::Zlib::inflateStream s
1077 RETVAL = s->stream.total_in ;
1083 Compress::Zlib::inflateStream s
1085 RETVAL = s->stream.total_out ;
1091 Compress::Zlib::inflateStream s
1093 RETVAL = s->stream.msg;