gzwrite.c 16 KB

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