aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Adler <madler@alumni.caltech.edu>2025-05-11 14:49:04 -0700
committerMark Adler <git@madler.net>2025-12-06 17:39:14 -0800
commit1ab1026a20282383d9cd2282f81461655bea4028 (patch)
tree511ef0996f9beb4c83ba15e72d7cc62d6252bb56
parent473f78532aed8f5a8d55cd6e7b305160db9cf742 (diff)
downloadzlib-1ab1026a20282383d9cd2282f81461655bea4028.tar.gz
zlib-1ab1026a20282383d9cd2282f81461655bea4028.tar.bz2
zlib-1ab1026a20282383d9cd2282f81461655bea4028.zip
Add a "G" option to force gzip, disabling transparency in gzread().
If the input is not a gzip stream with this option, an error will be returned.
-rw-r--r--gzlib.c20
-rw-r--r--gzread.c10
-rw-r--r--zlib.h6
3 files changed, 31 insertions, 5 deletions
diff --git a/gzlib.c b/gzlib.c
index 2f9cdd4..4c1aa83 100644
--- a/gzlib.c
+++ b/gzlib.c
@@ -153,6 +153,9 @@ local gzFile gz_open(const void *path, int fd, const char *mode) {
153 case 'F': 153 case 'F':
154 state->strategy = Z_FIXED; 154 state->strategy = Z_FIXED;
155 break; 155 break;
156 case 'G':
157 state->direct = -1;
158 break;
156 case 'T': 159 case 'T':
157 state->direct = 1; 160 state->direct = 1;
158 break; 161 break;
@@ -168,14 +171,25 @@ local gzFile gz_open(const void *path, int fd, const char *mode) {
168 return NULL; 171 return NULL;
169 } 172 }
170 173
171 /* can't force transparent read */ 174 /* direct is 0, 1 if "T", or -1 if "G" (last "G" or "T" wins) */
172 if (state->mode == GZ_READ) { 175 if (state->mode == GZ_READ) {
173 if (state->direct) { 176 if (state->direct == 1) {
177 /* can't force a transparent read */
174 free(state); 178 free(state);
175 return NULL; 179 return NULL;
176 } 180 }
177 state->direct = 1; /* for empty file */ 181 if (state->direct == 0)
182 /* default when reading is auto-detect of gzip vs. transparent --
183 start with a transparent assumption in case of an empty file */
184 state->direct = 1;
185 }
186 else if (state->direct == -1) {
187 /* "G" has no meaning when writing -- disallow it */
188 free(state);
189 return NULL;
178 } 190 }
191 /* if reading, direct == 1 for auto-detect, -1 for gzip only; if writing or
192 appending, direct == 0 for gzip, 1 for transparent (copy in to out) */
179 193
180 /* save the path name for error messages */ 194 /* save the path name for error messages */
181#ifdef WIDECHAR 195#ifdef WIDECHAR
diff --git a/gzread.c b/gzread.c
index 5edecbf..6fefe89 100644
--- a/gzread.c
+++ b/gzread.c
@@ -106,6 +106,14 @@ local int gz_look(gz_statep state) {
106 } 106 }
107 } 107 }
108 108
109 /* if transparent reading is disabled, simply read as gzip */
110 if (state->direct == -1) {
111 inflateReset(strm);
112 state->how = GZIP;
113 state->direct = 0;
114 return 0;
115 }
116
109 /* get at least the magic bytes in the input buffer */ 117 /* get at least the magic bytes in the input buffer */
110 if (strm->avail_in < 2) { 118 if (strm->avail_in < 2) {
111 if (gz_avail(state) == -1) 119 if (gz_avail(state) == -1)
@@ -567,7 +575,7 @@ int ZEXPORT gzdirect(gzFile file) {
567 (void)gz_look(state); 575 (void)gz_look(state);
568 576
569 /* return 1 if transparent, 0 if processing a gzip stream */ 577 /* return 1 if transparent, 0 if processing a gzip stream */
570 return state->direct; 578 return state->direct == 1;
571} 579}
572 580
573/* -- see zlib.h -- */ 581/* -- see zlib.h -- */
diff --git a/zlib.h b/zlib.h
index 85d7999..a2db7b2 100644
--- a/zlib.h
+++ b/zlib.h
@@ -1343,7 +1343,11 @@ ZEXTERN gzFile ZEXPORT gzopen(const char *path, const char *mode);
1343 'R' for run-length encoding as in "wb1R", or 'F' for fixed code compression 1343 'R' for run-length encoding as in "wb1R", or 'F' for fixed code compression
1344 as in "wb9F". (See the description of deflateInit2 for more information 1344 as in "wb9F". (See the description of deflateInit2 for more information
1345 about the strategy parameter.) 'T' will request transparent writing or 1345 about the strategy parameter.) 'T' will request transparent writing or
1346 appending with no compression and not using the gzip format. 1346 appending with no compression and not using the gzip format. 'T' cannot be
1347 used to force transparent reading. Transparent reading is automatically
1348 performed if there is no gzip header at the start. Transparent reading can
1349 be disabled with the 'G' option, which will instead return an error if there
1350 is no gzip header.
1347 1351
1348 "a" can be used instead of "w" to request that the gzip stream that will 1352 "a" can be used instead of "w" to request that the gzip stream that will
1349 be written be appended to the file. "+" will result in an error, since 1353 be written be appended to the file. "+" will result in an error, since