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