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