gzwrite.c 15 KB

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