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