OLD | NEW |
1 /* gzwrite.c -- zlib functions for writing gzip files | 1 /* gzwrite.c -- zlib functions for writing gzip files |
2 * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 Mark Adler | 2 * Copyright (C) 2004-2017 Mark Adler |
3 * For conditions of distribution and use, see copyright notice in zlib.h | 3 * For conditions of distribution and use, see copyright notice in zlib.h |
4 */ | 4 */ |
5 | 5 |
6 #include "gzguts.h" | 6 #include "gzguts.h" |
7 | 7 |
8 /* Local functions */ | 8 /* Local functions */ |
9 local int gz_init OF((gz_statep)); | 9 local int gz_init OF((gz_statep)); |
10 local int gz_comp OF((gz_statep, int)); | 10 local int gz_comp OF((gz_statep, int)); |
11 local int gz_zero OF((gz_statep, z_off64_t)); | 11 local int gz_zero OF((gz_statep, z_off64_t)); |
| 12 local z_size_t gz_write OF((gz_statep, voidpc, z_size_t)); |
12 | 13 |
13 /* Initialize state for writing a gzip file. Mark initialization by setting | 14 /* Initialize state for writing a gzip file. Mark initialization by setting |
14 state->size to non-zero. Return -1 on failure or 0 on success. */ | 15 state->size to non-zero. Return -1 on a memory allocation failure, or 0 on |
| 16 success. */ |
15 local int gz_init(state) | 17 local int gz_init(state) |
16 gz_statep state; | 18 gz_statep state; |
17 { | 19 { |
18 int ret; | 20 int ret; |
19 z_streamp strm = &(state->strm); | 21 z_streamp strm = &(state->strm); |
20 | 22 |
21 /* allocate input buffer */ | 23 /* allocate input buffer (double size for gzprintf) */ |
22 state->in = (unsigned char *)malloc(state->want); | 24 state->in = (unsigned char *)malloc(state->want << 1); |
23 if (state->in == NULL) { | 25 if (state->in == NULL) { |
24 gz_error(state, Z_MEM_ERROR, "out of memory"); | 26 gz_error(state, Z_MEM_ERROR, "out of memory"); |
25 return -1; | 27 return -1; |
26 } | 28 } |
27 | 29 |
28 /* only need output buffer and deflate state if compressing */ | 30 /* only need output buffer and deflate state if compressing */ |
29 if (!state->direct) { | 31 if (!state->direct) { |
30 /* allocate output buffer */ | 32 /* allocate output buffer */ |
31 state->out = (unsigned char *)malloc(state->want); | 33 state->out = (unsigned char *)malloc(state->want); |
32 if (state->out == NULL) { | 34 if (state->out == NULL) { |
33 free(state->in); | 35 free(state->in); |
34 gz_error(state, Z_MEM_ERROR, "out of memory"); | 36 gz_error(state, Z_MEM_ERROR, "out of memory"); |
35 return -1; | 37 return -1; |
36 } | 38 } |
37 | 39 |
38 /* allocate deflate memory, set up for gzip compression */ | 40 /* allocate deflate memory, set up for gzip compression */ |
39 strm->zalloc = Z_NULL; | 41 strm->zalloc = Z_NULL; |
40 strm->zfree = Z_NULL; | 42 strm->zfree = Z_NULL; |
41 strm->opaque = Z_NULL; | 43 strm->opaque = Z_NULL; |
42 ret = deflateInit2(strm, state->level, Z_DEFLATED, | 44 ret = deflateInit2(strm, state->level, Z_DEFLATED, |
43 MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy); | 45 MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy); |
44 if (ret != Z_OK) { | 46 if (ret != Z_OK) { |
45 free(state->out); | 47 free(state->out); |
46 free(state->in); | 48 free(state->in); |
47 gz_error(state, Z_MEM_ERROR, "out of memory"); | 49 gz_error(state, Z_MEM_ERROR, "out of memory"); |
48 return -1; | 50 return -1; |
49 } | 51 } |
| 52 strm->next_in = NULL; |
50 } | 53 } |
51 | 54 |
52 /* mark state as initialized */ | 55 /* mark state as initialized */ |
53 state->size = state->want; | 56 state->size = state->want; |
54 | 57 |
55 /* initialize write buffer if compressing */ | 58 /* initialize write buffer if compressing */ |
56 if (!state->direct) { | 59 if (!state->direct) { |
57 strm->avail_out = state->size; | 60 strm->avail_out = state->size; |
58 strm->next_out = state->out; | 61 strm->next_out = state->out; |
59 state->x.next = strm->next_out; | 62 state->x.next = strm->next_out; |
60 } | 63 } |
61 return 0; | 64 return 0; |
62 } | 65 } |
63 | 66 |
64 /* Compress whatever is at avail_in and next_in and write to the output file. | 67 /* Compress whatever is at avail_in and next_in and write to the output file. |
65 Return -1 if there is an error writing to the output file, otherwise 0. | 68 Return -1 if there is an error writing to the output file or if gz_init() |
66 flush is assumed to be a valid deflate() flush value. If flush is Z_FINISH, | 69 fails to allocate memory, otherwise 0. flush is assumed to be a valid |
67 then the deflate() state is reset to start a new gzip stream. If gz->direct | 70 deflate() flush value. If flush is Z_FINISH, then the deflate() state is |
68 is true, then simply write to the output file without compressing, and | 71 reset to start a new gzip stream. If gz->direct is true, then simply write |
69 ignore flush. */ | 72 to the output file without compressing, and ignore flush. */ |
70 local int gz_comp(state, flush) | 73 local int gz_comp(state, flush) |
71 gz_statep state; | 74 gz_statep state; |
72 int flush; | 75 int flush; |
73 { | 76 { |
74 int ret, got; | 77 int ret, writ; |
75 unsigned have; | 78 unsigned have, put, max = ((unsigned)-1 >> 2) + 1; |
76 z_streamp strm = &(state->strm); | 79 z_streamp strm = &(state->strm); |
77 | 80 |
78 /* allocate memory if this is the first time through */ | 81 /* allocate memory if this is the first time through */ |
79 if (state->size == 0 && gz_init(state) == -1) | 82 if (state->size == 0 && gz_init(state) == -1) |
80 return -1; | 83 return -1; |
81 | 84 |
82 /* write directly if requested */ | 85 /* write directly if requested */ |
83 if (state->direct) { | 86 if (state->direct) { |
84 got = write(state->fd, strm->next_in, strm->avail_in); | 87 while (strm->avail_in) { |
85 if (got < 0 || (unsigned)got != strm->avail_in) { | 88 put = strm->avail_in > max ? max : strm->avail_in; |
86 gz_error(state, Z_ERRNO, zstrerror()); | 89 writ = write(state->fd, strm->next_in, put); |
87 return -1; | 90 if (writ < 0) { |
| 91 gz_error(state, Z_ERRNO, zstrerror()); |
| 92 return -1; |
| 93 } |
| 94 strm->avail_in -= (unsigned)writ; |
| 95 strm->next_in += writ; |
88 } | 96 } |
89 strm->avail_in = 0; | |
90 return 0; | 97 return 0; |
91 } | 98 } |
92 | 99 |
93 /* run deflate() on provided input until it produces no more output */ | 100 /* run deflate() on provided input until it produces no more output */ |
94 ret = Z_OK; | 101 ret = Z_OK; |
95 do { | 102 do { |
96 /* write out current buffer contents if full, or if flushing, but if | 103 /* write out current buffer contents if full, or if flushing, but if |
97 doing Z_FINISH then don't write until we get to Z_STREAM_END */ | 104 doing Z_FINISH then don't write until we get to Z_STREAM_END */ |
98 if (strm->avail_out == 0 || (flush != Z_NO_FLUSH && | 105 if (strm->avail_out == 0 || (flush != Z_NO_FLUSH && |
99 (flush != Z_FINISH || ret == Z_STREAM_END))) { | 106 (flush != Z_FINISH || ret == Z_STREAM_END))) { |
100 have = (unsigned)(strm->next_out - state->x.next); | 107 while (strm->next_out > state->x.next) { |
101 if (have && ((got = write(state->fd, state->x.next, have)) < 0 || | 108 put = strm->next_out - state->x.next > (int)max ? max : |
102 (unsigned)got != have)) { | 109 (unsigned)(strm->next_out - state->x.next); |
103 gz_error(state, Z_ERRNO, zstrerror()); | 110 writ = write(state->fd, state->x.next, put); |
104 return -1; | 111 if (writ < 0) { |
| 112 gz_error(state, Z_ERRNO, zstrerror()); |
| 113 return -1; |
| 114 } |
| 115 state->x.next += writ; |
105 } | 116 } |
106 if (strm->avail_out == 0) { | 117 if (strm->avail_out == 0) { |
107 strm->avail_out = state->size; | 118 strm->avail_out = state->size; |
108 strm->next_out = state->out; | 119 strm->next_out = state->out; |
| 120 state->x.next = state->out; |
109 } | 121 } |
110 state->x.next = strm->next_out; | |
111 } | 122 } |
112 | 123 |
113 /* compress */ | 124 /* compress */ |
114 have = strm->avail_out; | 125 have = strm->avail_out; |
115 ret = deflate(strm, flush); | 126 ret = deflate(strm, flush); |
116 if (ret == Z_STREAM_ERROR) { | 127 if (ret == Z_STREAM_ERROR) { |
117 gz_error(state, Z_STREAM_ERROR, | 128 gz_error(state, Z_STREAM_ERROR, |
118 "internal error: deflate stream corrupt"); | 129 "internal error: deflate stream corrupt"); |
119 return -1; | 130 return -1; |
120 } | 131 } |
121 have -= strm->avail_out; | 132 have -= strm->avail_out; |
122 } while (have); | 133 } while (have); |
123 | 134 |
124 /* if that completed a deflate stream, allow another to start */ | 135 /* if that completed a deflate stream, allow another to start */ |
125 if (flush == Z_FINISH) | 136 if (flush == Z_FINISH) |
126 deflateReset(strm); | 137 deflateReset(strm); |
127 | 138 |
128 /* all done, no errors */ | 139 /* all done, no errors */ |
129 return 0; | 140 return 0; |
130 } | 141 } |
131 | 142 |
132 /* Compress len zeros to output. Return -1 on error, 0 on success. */ | 143 /* Compress len zeros to output. Return -1 on a write error or memory |
| 144 allocation failure by gz_comp(), or 0 on success. */ |
133 local int gz_zero(state, len) | 145 local int gz_zero(state, len) |
134 gz_statep state; | 146 gz_statep state; |
135 z_off64_t len; | 147 z_off64_t len; |
136 { | 148 { |
137 int first; | 149 int first; |
138 unsigned n; | 150 unsigned n; |
139 z_streamp strm = &(state->strm); | 151 z_streamp strm = &(state->strm); |
140 | 152 |
141 /* consume whatever's left in the input buffer */ | 153 /* consume whatever's left in the input buffer */ |
142 if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) | 154 if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) |
(...skipping 11 matching lines...) Expand all Loading... |
154 strm->avail_in = n; | 166 strm->avail_in = n; |
155 strm->next_in = state->in; | 167 strm->next_in = state->in; |
156 state->x.pos += n; | 168 state->x.pos += n; |
157 if (gz_comp(state, Z_NO_FLUSH) == -1) | 169 if (gz_comp(state, Z_NO_FLUSH) == -1) |
158 return -1; | 170 return -1; |
159 len -= n; | 171 len -= n; |
160 } | 172 } |
161 return 0; | 173 return 0; |
162 } | 174 } |
163 | 175 |
164 /* -- see zlib.h -- */ | 176 /* Write len bytes from buf to file. Return the number of bytes written. If |
165 int ZEXPORT gzwrite(file, buf, len) | 177 the returned value is less than len, then there was an error. */ |
166 gzFile file; | 178 local z_size_t gz_write(state, buf, len) |
| 179 gz_statep state; |
167 voidpc buf; | 180 voidpc buf; |
168 unsigned len; | 181 z_size_t len; |
169 { | 182 { |
170 unsigned put = len; | 183 z_size_t put = len; |
171 gz_statep state; | |
172 z_streamp strm; | |
173 | |
174 /* get internal structure */ | |
175 if (file == NULL) | |
176 return 0; | |
177 state = (gz_statep)file; | |
178 strm = &(state->strm); | |
179 | |
180 /* check that we're writing and that there's no error */ | |
181 if (state->mode != GZ_WRITE || state->err != Z_OK) | |
182 return 0; | |
183 | |
184 /* since an int is returned, make sure len fits in one, otherwise return | |
185 with an error (this avoids the flaw in the interface) */ | |
186 if ((int)len < 0) { | |
187 gz_error(state, Z_DATA_ERROR, "requested length does not fit in int"); | |
188 return 0; | |
189 } | |
190 | 184 |
191 /* if len is zero, avoid unnecessary operations */ | 185 /* if len is zero, avoid unnecessary operations */ |
192 if (len == 0) | 186 if (len == 0) |
193 return 0; | 187 return 0; |
194 | 188 |
195 /* allocate memory if this is the first time through */ | 189 /* allocate memory if this is the first time through */ |
196 if (state->size == 0 && gz_init(state) == -1) | 190 if (state->size == 0 && gz_init(state) == -1) |
197 return 0; | 191 return 0; |
198 | 192 |
199 /* check for seek request */ | 193 /* check for seek request */ |
200 if (state->seek) { | 194 if (state->seek) { |
201 state->seek = 0; | 195 state->seek = 0; |
202 if (gz_zero(state, state->skip) == -1) | 196 if (gz_zero(state, state->skip) == -1) |
203 return 0; | 197 return 0; |
204 } | 198 } |
205 | 199 |
206 /* for small len, copy to input buffer, otherwise compress directly */ | 200 /* for small len, copy to input buffer, otherwise compress directly */ |
207 if (len < state->size) { | 201 if (len < state->size) { |
208 /* copy to input buffer, compress when full */ | 202 /* copy to input buffer, compress when full */ |
209 do { | 203 do { |
210 unsigned have, copy; | 204 unsigned have, copy; |
211 | 205 |
212 if (strm->avail_in == 0) | 206 if (state->strm.avail_in == 0) |
213 strm->next_in = state->in; | 207 state->strm.next_in = state->in; |
214 have = (unsigned)((strm->next_in + strm->avail_in) - state->in); | 208 have = (unsigned)((state->strm.next_in + state->strm.avail_in) - |
| 209 state->in); |
215 copy = state->size - have; | 210 copy = state->size - have; |
216 if (copy > len) | 211 if (copy > len) |
217 copy = len; | 212 copy = len; |
218 memcpy(state->in + have, buf, copy); | 213 memcpy(state->in + have, buf, copy); |
219 strm->avail_in += copy; | 214 state->strm.avail_in += copy; |
220 state->x.pos += copy; | 215 state->x.pos += copy; |
221 buf = (const char *)buf + copy; | 216 buf = (const char *)buf + copy; |
222 len -= copy; | 217 len -= copy; |
223 if (len && gz_comp(state, Z_NO_FLUSH) == -1) | 218 if (len && gz_comp(state, Z_NO_FLUSH) == -1) |
224 return 0; | 219 return 0; |
225 } while (len); | 220 } while (len); |
226 } | 221 } |
227 else { | 222 else { |
228 /* consume whatever's left in the input buffer */ | 223 /* consume whatever's left in the input buffer */ |
229 if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) | 224 if (state->strm.avail_in && gz_comp(state, Z_NO_FLUSH) == -1) |
230 return 0; | 225 return 0; |
231 | 226 |
232 /* directly compress user buffer to file */ | 227 /* directly compress user buffer to file */ |
233 strm->avail_in = len; | 228 state->strm.next_in = (z_const Bytef *)buf; |
234 strm->next_in = (z_const Bytef *)buf; | 229 do { |
235 state->x.pos += len; | 230 unsigned n = (unsigned)-1; |
236 if (gz_comp(state, Z_NO_FLUSH) == -1) | 231 if (n > len) |
237 return 0; | 232 n = len; |
| 233 state->strm.avail_in = n; |
| 234 state->x.pos += n; |
| 235 if (gz_comp(state, Z_NO_FLUSH) == -1) |
| 236 return 0; |
| 237 len -= n; |
| 238 } while (len); |
238 } | 239 } |
239 | 240 |
240 /* input was all buffered or compressed (put will fit in int) */ | 241 /* input was all buffered or compressed */ |
241 return (int)put; | 242 return put; |
242 } | 243 } |
243 | 244 |
244 /* -- see zlib.h -- */ | 245 /* -- see zlib.h -- */ |
| 246 int ZEXPORT gzwrite(file, buf, len) |
| 247 gzFile file; |
| 248 voidpc buf; |
| 249 unsigned len; |
| 250 { |
| 251 gz_statep state; |
| 252 |
| 253 /* get internal structure */ |
| 254 if (file == NULL) |
| 255 return 0; |
| 256 state = (gz_statep)file; |
| 257 |
| 258 /* check that we're writing and that there's no error */ |
| 259 if (state->mode != GZ_WRITE || state->err != Z_OK) |
| 260 return 0; |
| 261 |
| 262 /* since an int is returned, make sure len fits in one, otherwise return |
| 263 with an error (this avoids a flaw in the interface) */ |
| 264 if ((int)len < 0) { |
| 265 gz_error(state, Z_DATA_ERROR, "requested length does not fit in int"); |
| 266 return 0; |
| 267 } |
| 268 |
| 269 /* write len bytes from buf (the return value will fit in an int) */ |
| 270 return (int)gz_write(state, buf, len); |
| 271 } |
| 272 |
| 273 /* -- see zlib.h -- */ |
| 274 z_size_t ZEXPORT gzfwrite(buf, size, nitems, file) |
| 275 voidpc buf; |
| 276 z_size_t size; |
| 277 z_size_t nitems; |
| 278 gzFile file; |
| 279 { |
| 280 z_size_t len; |
| 281 gz_statep state; |
| 282 |
| 283 /* get internal structure */ |
| 284 if (file == NULL) |
| 285 return 0; |
| 286 state = (gz_statep)file; |
| 287 |
| 288 /* check that we're writing and that there's no error */ |
| 289 if (state->mode != GZ_WRITE || state->err != Z_OK) |
| 290 return 0; |
| 291 |
| 292 /* compute bytes to read -- error on overflow */ |
| 293 len = nitems * size; |
| 294 if (size && len / size != nitems) { |
| 295 gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t"); |
| 296 return 0; |
| 297 } |
| 298 |
| 299 /* write len bytes to buf, return the number of full items written */ |
| 300 return len ? gz_write(state, buf, len) / size : 0; |
| 301 } |
| 302 |
| 303 /* -- see zlib.h -- */ |
245 int ZEXPORT gzputc(file, c) | 304 int ZEXPORT gzputc(file, c) |
246 gzFile file; | 305 gzFile file; |
247 int c; | 306 int c; |
248 { | 307 { |
249 unsigned have; | 308 unsigned have; |
250 unsigned char buf[1]; | 309 unsigned char buf[1]; |
251 gz_statep state; | 310 gz_statep state; |
252 z_streamp strm; | 311 z_streamp strm; |
253 | 312 |
254 /* get internal structure */ | 313 /* get internal structure */ |
(...skipping 13 matching lines...) Expand all Loading... |
268 return -1; | 327 return -1; |
269 } | 328 } |
270 | 329 |
271 /* try writing to input buffer for speed (state->size == 0 if buffer not | 330 /* try writing to input buffer for speed (state->size == 0 if buffer not |
272 initialized) */ | 331 initialized) */ |
273 if (state->size) { | 332 if (state->size) { |
274 if (strm->avail_in == 0) | 333 if (strm->avail_in == 0) |
275 strm->next_in = state->in; | 334 strm->next_in = state->in; |
276 have = (unsigned)((strm->next_in + strm->avail_in) - state->in); | 335 have = (unsigned)((strm->next_in + strm->avail_in) - state->in); |
277 if (have < state->size) { | 336 if (have < state->size) { |
278 state->in[have] = c; | 337 state->in[have] = (unsigned char)c; |
279 strm->avail_in++; | 338 strm->avail_in++; |
280 state->x.pos++; | 339 state->x.pos++; |
281 return c & 0xff; | 340 return c & 0xff; |
282 } | 341 } |
283 } | 342 } |
284 | 343 |
285 /* no room in buffer or not initialized, use gz_write() */ | 344 /* no room in buffer or not initialized, use gz_write() */ |
286 buf[0] = c; | 345 buf[0] = (unsigned char)c; |
287 if (gzwrite(file, buf, 1) != 1) | 346 if (gz_write(state, buf, 1) != 1) |
288 return -1; | 347 return -1; |
289 return c & 0xff; | 348 return c & 0xff; |
290 } | 349 } |
291 | 350 |
292 /* -- see zlib.h -- */ | 351 /* -- see zlib.h -- */ |
293 int ZEXPORT gzputs(file, str) | 352 int ZEXPORT gzputs(file, str) |
294 gzFile file; | 353 gzFile file; |
295 const char *str; | 354 const char *str; |
296 { | 355 { |
297 int ret; | 356 int ret; |
298 unsigned len; | 357 z_size_t len; |
| 358 gz_statep state; |
| 359 |
| 360 /* get internal structure */ |
| 361 if (file == NULL) |
| 362 return -1; |
| 363 state = (gz_statep)file; |
| 364 |
| 365 /* check that we're writing and that there's no error */ |
| 366 if (state->mode != GZ_WRITE || state->err != Z_OK) |
| 367 return -1; |
299 | 368 |
300 /* write string */ | 369 /* write string */ |
301 len = (unsigned)strlen(str); | 370 len = strlen(str); |
302 ret = gzwrite(file, str, len); | 371 ret = gz_write(state, str, len); |
303 return ret == 0 && len != 0 ? -1 : ret; | 372 return ret == 0 && len != 0 ? -1 : ret; |
304 } | 373 } |
305 | 374 |
306 #if defined(STDC) || defined(Z_HAVE_STDARG_H) | 375 #if defined(STDC) || defined(Z_HAVE_STDARG_H) |
307 #include <stdarg.h> | 376 #include <stdarg.h> |
308 | 377 |
309 /* -- see zlib.h -- */ | 378 /* -- see zlib.h -- */ |
310 int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va) | 379 int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va) |
311 { | 380 { |
312 int size, len; | 381 int len; |
| 382 unsigned left; |
| 383 char *next; |
313 gz_statep state; | 384 gz_statep state; |
314 z_streamp strm; | 385 z_streamp strm; |
315 | 386 |
316 /* get internal structure */ | 387 /* get internal structure */ |
317 if (file == NULL) | 388 if (file == NULL) |
318 return -1; | 389 return Z_STREAM_ERROR; |
319 state = (gz_statep)file; | 390 state = (gz_statep)file; |
320 strm = &(state->strm); | 391 strm = &(state->strm); |
321 | 392 |
322 /* check that we're writing and that there's no error */ | 393 /* check that we're writing and that there's no error */ |
323 if (state->mode != GZ_WRITE || state->err != Z_OK) | 394 if (state->mode != GZ_WRITE || state->err != Z_OK) |
324 return 0; | 395 return Z_STREAM_ERROR; |
325 | 396 |
326 /* make sure we have some buffer space */ | 397 /* make sure we have some buffer space */ |
327 if (state->size == 0 && gz_init(state) == -1) | 398 if (state->size == 0 && gz_init(state) == -1) |
328 return 0; | 399 return state->err; |
329 | 400 |
330 /* check for seek request */ | 401 /* check for seek request */ |
331 if (state->seek) { | 402 if (state->seek) { |
332 state->seek = 0; | 403 state->seek = 0; |
333 if (gz_zero(state, state->skip) == -1) | 404 if (gz_zero(state, state->skip) == -1) |
334 return 0; | 405 return state->err; |
335 } | 406 } |
336 | 407 |
337 /* consume whatever's left in the input buffer */ | 408 /* do the printf() into the input buffer, put length in len -- the input |
338 if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) | 409 buffer is double-sized just for this function, so there is guaranteed to |
339 return 0; | 410 be state->size bytes available after the current contents */ |
340 | 411 if (strm->avail_in == 0) |
341 /* do the printf() into the input buffer, put length in len */ | 412 strm->next_in = state->in; |
342 size = (int)(state->size); | 413 next = (char *)(state->in + (strm->next_in - state->in) + strm->avail_in); |
343 state->in[size - 1] = 0; | 414 next[state->size - 1] = 0; |
344 #ifdef NO_vsnprintf | 415 #ifdef NO_vsnprintf |
345 # ifdef HAS_vsprintf_void | 416 # ifdef HAS_vsprintf_void |
346 (void)vsprintf((char *)(state->in), format, va); | 417 (void)vsprintf(next, format, va); |
347 for (len = 0; len < size; len++) | 418 for (len = 0; len < state->size; len++) |
348 if (state->in[len] == 0) break; | 419 if (next[len] == 0) break; |
349 # else | 420 # else |
350 len = vsprintf((char *)(state->in), format, va); | 421 len = vsprintf(next, format, va); |
351 # endif | 422 # endif |
352 #else | 423 #else |
353 # ifdef HAS_vsnprintf_void | 424 # ifdef HAS_vsnprintf_void |
354 (void)vsnprintf((char *)(state->in), size, format, va); | 425 (void)vsnprintf(next, state->size, format, va); |
355 len = strlen((char *)(state->in)); | 426 len = strlen(next); |
356 # else | 427 # else |
357 len = vsnprintf((char *)(state->in), size, format, va); | 428 len = vsnprintf(next, state->size, format, va); |
358 # endif | 429 # endif |
359 #endif | 430 #endif |
360 | 431 |
361 /* check that printf() results fit in buffer */ | 432 /* check that printf() results fit in buffer */ |
362 if (len <= 0 || len >= (int)size || state->in[size - 1] != 0) | 433 if (len == 0 || (unsigned)len >= state->size || next[state->size - 1] != 0) |
363 return 0; | 434 return 0; |
364 | 435 |
365 /* update buffer and position, defer compression until needed */ | 436 /* update buffer and position, compress first half if past that */ |
366 strm->avail_in = (unsigned)len; | 437 strm->avail_in += (unsigned)len; |
367 strm->next_in = state->in; | |
368 state->x.pos += len; | 438 state->x.pos += len; |
| 439 if (strm->avail_in >= state->size) { |
| 440 left = strm->avail_in - state->size; |
| 441 strm->avail_in = state->size; |
| 442 if (gz_comp(state, Z_NO_FLUSH) == -1) |
| 443 return state->err; |
| 444 memcpy(state->in, state->in + state->size, left); |
| 445 strm->next_in = state->in; |
| 446 strm->avail_in = left; |
| 447 } |
369 return len; | 448 return len; |
370 } | 449 } |
371 | 450 |
372 int ZEXPORTVA gzprintf(gzFile file, const char *format, ...) | 451 int ZEXPORTVA gzprintf(gzFile file, const char *format, ...) |
373 { | 452 { |
374 va_list va; | 453 va_list va; |
375 int ret; | 454 int ret; |
376 | 455 |
377 va_start(va, format); | 456 va_start(va, format); |
378 ret = gzvprintf(file, format, va); | 457 ret = gzvprintf(file, format, va); |
379 va_end(va); | 458 va_end(va); |
380 return ret; | 459 return ret; |
381 } | 460 } |
382 | 461 |
383 #else /* !STDC && !Z_HAVE_STDARG_H */ | 462 #else /* !STDC && !Z_HAVE_STDARG_H */ |
384 | 463 |
385 /* -- see zlib.h -- */ | 464 /* -- see zlib.h -- */ |
386 int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, | 465 int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, |
387 a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) | 466 a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) |
388 gzFile file; | 467 gzFile file; |
389 const char *format; | 468 const char *format; |
390 int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, | 469 int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, |
391 a11, a12, a13, a14, a15, a16, a17, a18, a19, a20; | 470 a11, a12, a13, a14, a15, a16, a17, a18, a19, a20; |
392 { | 471 { |
393 int size, len; | 472 unsigned len, left; |
| 473 char *next; |
394 gz_statep state; | 474 gz_statep state; |
395 z_streamp strm; | 475 z_streamp strm; |
396 | 476 |
397 /* get internal structure */ | 477 /* get internal structure */ |
398 if (file == NULL) | 478 if (file == NULL) |
399 return -1; | 479 return Z_STREAM_ERROR; |
400 state = (gz_statep)file; | 480 state = (gz_statep)file; |
401 strm = &(state->strm); | 481 strm = &(state->strm); |
402 | 482 |
403 /* check that can really pass pointer in ints */ | 483 /* check that can really pass pointer in ints */ |
404 if (sizeof(int) != sizeof(void *)) | 484 if (sizeof(int) != sizeof(void *)) |
405 return 0; | 485 return Z_STREAM_ERROR; |
406 | 486 |
407 /* check that we're writing and that there's no error */ | 487 /* check that we're writing and that there's no error */ |
408 if (state->mode != GZ_WRITE || state->err != Z_OK) | 488 if (state->mode != GZ_WRITE || state->err != Z_OK) |
409 return 0; | 489 return Z_STREAM_ERROR; |
410 | 490 |
411 /* make sure we have some buffer space */ | 491 /* make sure we have some buffer space */ |
412 if (state->size == 0 && gz_init(state) == -1) | 492 if (state->size == 0 && gz_init(state) == -1) |
413 return 0; | 493 return state->error; |
414 | 494 |
415 /* check for seek request */ | 495 /* check for seek request */ |
416 if (state->seek) { | 496 if (state->seek) { |
417 state->seek = 0; | 497 state->seek = 0; |
418 if (gz_zero(state, state->skip) == -1) | 498 if (gz_zero(state, state->skip) == -1) |
419 return 0; | 499 return state->error; |
420 } | 500 } |
421 | 501 |
422 /* consume whatever's left in the input buffer */ | 502 /* do the printf() into the input buffer, put length in len -- the input |
423 if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) | 503 buffer is double-sized just for this function, so there is guaranteed to |
424 return 0; | 504 be state->size bytes available after the current contents */ |
425 | 505 if (strm->avail_in == 0) |
426 /* do the printf() into the input buffer, put length in len */ | 506 strm->next_in = state->in; |
427 size = (int)(state->size); | 507 next = (char *)(strm->next_in + strm->avail_in); |
428 state->in[size - 1] = 0; | 508 next[state->size - 1] = 0; |
429 #ifdef NO_snprintf | 509 #ifdef NO_snprintf |
430 # ifdef HAS_sprintf_void | 510 # ifdef HAS_sprintf_void |
431 sprintf((char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8, | 511 sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, |
432 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); | 512 a13, a14, a15, a16, a17, a18, a19, a20); |
433 for (len = 0; len < size; len++) | 513 for (len = 0; len < size; len++) |
434 if (state->in[len] == 0) break; | 514 if (next[len] == 0) |
| 515 break; |
435 # else | 516 # else |
436 len = sprintf((char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8, | 517 len = sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, |
437 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); | 518 a12, a13, a14, a15, a16, a17, a18, a19, a20); |
438 # endif | 519 # endif |
439 #else | 520 #else |
440 # ifdef HAS_snprintf_void | 521 # ifdef HAS_snprintf_void |
441 snprintf((char *)(state->in), size, format, a1, a2, a3, a4, a5, a6, a7, a8, | 522 snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, |
442 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); | 523 a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); |
443 len = strlen((char *)(state->in)); | 524 len = strlen(next); |
444 # else | 525 # else |
445 len = snprintf((char *)(state->in), size, format, a1, a2, a3, a4, a5, a6, | 526 len = snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8, |
446 a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, | 527 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); |
447 a19, a20); | |
448 # endif | 528 # endif |
449 #endif | 529 #endif |
450 | 530 |
451 /* check that printf() results fit in buffer */ | 531 /* check that printf() results fit in buffer */ |
452 if (len <= 0 || len >= (int)size || state->in[size - 1] != 0) | 532 if (len == 0 || len >= state->size || next[state->size - 1] != 0) |
453 return 0; | 533 return 0; |
454 | 534 |
455 /* update buffer and position, defer compression until needed */ | 535 /* update buffer and position, compress first half if past that */ |
456 strm->avail_in = (unsigned)len; | 536 strm->avail_in += len; |
457 strm->next_in = state->in; | |
458 state->x.pos += len; | 537 state->x.pos += len; |
459 return len; | 538 if (strm->avail_in >= state->size) { |
| 539 left = strm->avail_in - state->size; |
| 540 strm->avail_in = state->size; |
| 541 if (gz_comp(state, Z_NO_FLUSH) == -1) |
| 542 return state->err; |
| 543 memcpy(state->in, state->in + state->size, left); |
| 544 strm->next_in = state->in; |
| 545 strm->avail_in = left; |
| 546 } |
| 547 return (int)len; |
460 } | 548 } |
461 | 549 |
462 #endif | 550 #endif |
463 | 551 |
464 /* -- see zlib.h -- */ | 552 /* -- see zlib.h -- */ |
465 int ZEXPORT gzflush(file, flush) | 553 int ZEXPORT gzflush(file, flush) |
466 gzFile file; | 554 gzFile file; |
467 int flush; | 555 int flush; |
468 { | 556 { |
469 gz_statep state; | 557 gz_statep state; |
470 | 558 |
471 /* get internal structure */ | 559 /* get internal structure */ |
472 if (file == NULL) | 560 if (file == NULL) |
473 return -1; | 561 return Z_STREAM_ERROR; |
474 state = (gz_statep)file; | 562 state = (gz_statep)file; |
475 | 563 |
476 /* check that we're writing and that there's no error */ | 564 /* check that we're writing and that there's no error */ |
477 if (state->mode != GZ_WRITE || state->err != Z_OK) | 565 if (state->mode != GZ_WRITE || state->err != Z_OK) |
478 return Z_STREAM_ERROR; | 566 return Z_STREAM_ERROR; |
479 | 567 |
480 /* check flush parameter */ | 568 /* check flush parameter */ |
481 if (flush < 0 || flush > Z_FINISH) | 569 if (flush < 0 || flush > Z_FINISH) |
482 return Z_STREAM_ERROR; | 570 return Z_STREAM_ERROR; |
483 | 571 |
484 /* check for seek request */ | 572 /* check for seek request */ |
485 if (state->seek) { | 573 if (state->seek) { |
486 state->seek = 0; | 574 state->seek = 0; |
487 if (gz_zero(state, state->skip) == -1) | 575 if (gz_zero(state, state->skip) == -1) |
488 return -1; | 576 return state->err; |
489 } | 577 } |
490 | 578 |
491 /* compress remaining data with requested flush */ | 579 /* compress remaining data with requested flush */ |
492 gz_comp(state, flush); | 580 (void)gz_comp(state, flush); |
493 return state->err; | 581 return state->err; |
494 } | 582 } |
495 | 583 |
496 /* -- see zlib.h -- */ | 584 /* -- see zlib.h -- */ |
497 int ZEXPORT gzsetparams(file, level, strategy) | 585 int ZEXPORT gzsetparams(file, level, strategy) |
498 gzFile file; | 586 gzFile file; |
499 int level; | 587 int level; |
500 int strategy; | 588 int strategy; |
501 { | 589 { |
502 gz_statep state; | 590 gz_statep state; |
(...skipping 10 matching lines...) Expand all Loading... |
513 return Z_STREAM_ERROR; | 601 return Z_STREAM_ERROR; |
514 | 602 |
515 /* if no change is requested, then do nothing */ | 603 /* if no change is requested, then do nothing */ |
516 if (level == state->level && strategy == state->strategy) | 604 if (level == state->level && strategy == state->strategy) |
517 return Z_OK; | 605 return Z_OK; |
518 | 606 |
519 /* check for seek request */ | 607 /* check for seek request */ |
520 if (state->seek) { | 608 if (state->seek) { |
521 state->seek = 0; | 609 state->seek = 0; |
522 if (gz_zero(state, state->skip) == -1) | 610 if (gz_zero(state, state->skip) == -1) |
523 return -1; | 611 return state->err; |
524 } | 612 } |
525 | 613 |
526 /* change compression parameters for subsequent input */ | 614 /* change compression parameters for subsequent input */ |
527 if (state->size) { | 615 if (state->size) { |
528 /* flush previous input with previous parameters before changing */ | 616 /* flush previous input with previous parameters before changing */ |
529 if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH) == -1) | 617 if (strm->avail_in && gz_comp(state, Z_BLOCK) == -1) |
530 return state->err; | 618 return state->err; |
531 deflateParams(strm, level, strategy); | 619 deflateParams(strm, level, strategy); |
532 } | 620 } |
533 state->level = level; | 621 state->level = level; |
534 state->strategy = strategy; | 622 state->strategy = strategy; |
535 return Z_OK; | 623 return Z_OK; |
536 } | 624 } |
537 | 625 |
538 /* -- see zlib.h -- */ | 626 /* -- see zlib.h -- */ |
539 int ZEXPORT gzclose_w(file) | 627 int ZEXPORT gzclose_w(file) |
(...skipping 28 matching lines...) Expand all Loading... |
568 } | 656 } |
569 free(state->in); | 657 free(state->in); |
570 } | 658 } |
571 gz_error(state, Z_OK, NULL); | 659 gz_error(state, Z_OK, NULL); |
572 free(state->path); | 660 free(state->path); |
573 if (close(state->fd) == -1) | 661 if (close(state->fd) == -1) |
574 ret = Z_ERRNO; | 662 ret = Z_ERRNO; |
575 free(state); | 663 free(state); |
576 return ret; | 664 return ret; |
577 } | 665 } |
OLD | NEW |