gzwrite.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /* gzwrite.c -- zlib functions for writing gzip files
  2. * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 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 = (unsigned char *)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 = (unsigned char *)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. gz_statep state;
  153. z_streamp strm;
  154. /* get internal structure */
  155. if (file == NULL)
  156. return 0;
  157. state = (gz_statep)file;
  158. strm = &(state->strm);
  159. /* check that we're writing and that there's no error */
  160. if (state->mode != GZ_WRITE || state->err != Z_OK)
  161. return 0;
  162. /* since an int is returned, make sure len fits in one, otherwise return
  163. with an error (this avoids the flaw in the interface) */
  164. if ((int)len < 0) {
  165. gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
  166. return 0;
  167. }
  168. /* if len is zero, avoid unnecessary operations */
  169. if (len == 0)
  170. return 0;
  171. /* allocate memory if this is the first time through */
  172. if (state->size == 0 && gz_init(state) == -1)
  173. return 0;
  174. /* check for seek request */
  175. if (state->seek) {
  176. state->seek = 0;
  177. if (gz_zero(state, state->skip) == -1)
  178. return 0;
  179. }
  180. /* for small len, copy to input buffer, otherwise compress directly */
  181. if (len < state->size) {
  182. /* copy to input buffer, compress when full */
  183. do {
  184. unsigned have, copy;
  185. if (strm->avail_in == 0)
  186. strm->next_in = state->in;
  187. have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
  188. copy = state->size - have;
  189. if (copy > len)
  190. copy = len;
  191. memcpy(state->in + have, buf, copy);
  192. strm->avail_in += copy;
  193. state->x.pos += copy;
  194. buf = (const char *)buf + copy;
  195. len -= copy;
  196. if (len && gz_comp(state, Z_NO_FLUSH) == -1)
  197. return 0;
  198. } while (len);
  199. }
  200. else {
  201. /* consume whatever's left in the input buffer */
  202. if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
  203. return 0;
  204. /* directly compress user buffer to file */
  205. strm->avail_in = len;
  206. strm->next_in = (z_const Bytef *)buf;
  207. state->x.pos += len;
  208. if (gz_comp(state, Z_NO_FLUSH) == -1)
  209. return 0;
  210. }
  211. /* input was all buffered or compressed (put will fit in int) */
  212. return (int)put;
  213. }
  214. /* -- see zlib.h -- */
  215. int ZEXPORT gzputc(file, c)
  216. gzFile file;
  217. int c;
  218. {
  219. unsigned have;
  220. unsigned char buf[1];
  221. gz_statep state;
  222. z_streamp strm;
  223. /* get internal structure */
  224. if (file == NULL)
  225. return -1;
  226. state = (gz_statep)file;
  227. strm = &(state->strm);
  228. /* check that we're writing and that there's no error */
  229. if (state->mode != GZ_WRITE || state->err != Z_OK)
  230. return -1;
  231. /* check for seek request */
  232. if (state->seek) {
  233. state->seek = 0;
  234. if (gz_zero(state, state->skip) == -1)
  235. return -1;
  236. }
  237. /* try writing to input buffer for speed (state->size == 0 if buffer not
  238. initialized) */
  239. if (state->size) {
  240. if (strm->avail_in == 0)
  241. strm->next_in = state->in;
  242. have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
  243. if (have < state->size) {
  244. state->in[have] = c;
  245. strm->avail_in++;
  246. state->x.pos++;
  247. return c & 0xff;
  248. }
  249. }
  250. /* no room in buffer or not initialized, use gz_write() */
  251. buf[0] = c;
  252. if (gzwrite(file, buf, 1) != 1)
  253. return -1;
  254. return c & 0xff;
  255. }
  256. /* -- see zlib.h -- */
  257. int ZEXPORT gzputs(file, str)
  258. gzFile file;
  259. const char *str;
  260. {
  261. int ret;
  262. unsigned len;
  263. /* write string */
  264. len = (unsigned)strlen(str);
  265. ret = gzwrite(file, str, len);
  266. return ret == 0 && len != 0 ? -1 : ret;
  267. }
  268. #if defined(STDC) || defined(Z_HAVE_STDARG_H)
  269. #include <stdarg.h>
  270. /* -- see zlib.h -- */
  271. int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va)
  272. {
  273. int size, len;
  274. gz_statep state;
  275. z_streamp strm;
  276. /* get internal structure */
  277. if (file == NULL)
  278. return -1;
  279. state = (gz_statep)file;
  280. strm = &(state->strm);
  281. /* check that we're writing and that there's no error */
  282. if (state->mode != GZ_WRITE || state->err != Z_OK)
  283. return 0;
  284. /* make sure we have some buffer space */
  285. if (state->size == 0 && gz_init(state) == -1)
  286. return 0;
  287. /* check for seek request */
  288. if (state->seek) {
  289. state->seek = 0;
  290. if (gz_zero(state, state->skip) == -1)
  291. return 0;
  292. }
  293. /* consume whatever's left in the input buffer */
  294. if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
  295. return 0;
  296. /* do the printf() into the input buffer, put length in len */
  297. size = (int)(state->size);
  298. state->in[size - 1] = 0;
  299. #ifdef NO_vsnprintf
  300. # ifdef HAS_vsprintf_void
  301. (void)vsprintf((char *)(state->in), format, va);
  302. for (len = 0; len < size; len++)
  303. if (state->in[len] == 0) break;
  304. # else
  305. len = vsprintf((char *)(state->in), format, va);
  306. # endif
  307. #else
  308. # ifdef HAS_vsnprintf_void
  309. (void)vsnprintf((char *)(state->in), size, format, va);
  310. len = strlen((char *)(state->in));
  311. # else
  312. len = vsnprintf((char *)(state->in), size, format, 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. int ZEXPORTVA gzprintf(gzFile file, const char *format, ...)
  325. {
  326. va_list va;
  327. int ret;
  328. va_start(va, format);
  329. ret = gzvprintf(file, format, va);
  330. va_end(va);
  331. return ret;
  332. }
  333. #else /* !STDC && !Z_HAVE_STDARG_H */
  334. /* -- see zlib.h -- */
  335. int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
  336. a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
  337. gzFile file;
  338. const char *format;
  339. int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
  340. a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
  341. {
  342. int size, len;
  343. gz_statep state;
  344. z_streamp strm;
  345. /* get internal structure */
  346. if (file == NULL)
  347. return -1;
  348. state = (gz_statep)file;
  349. strm = &(state->strm);
  350. /* check that can really pass pointer in ints */
  351. if (sizeof(int) != sizeof(void *))
  352. return 0;
  353. /* check that we're writing and that there's no error */
  354. if (state->mode != GZ_WRITE || state->err != Z_OK)
  355. return 0;
  356. /* make sure we have some buffer space */
  357. if (state->size == 0 && gz_init(state) == -1)
  358. return 0;
  359. /* check for seek request */
  360. if (state->seek) {
  361. state->seek = 0;
  362. if (gz_zero(state, state->skip) == -1)
  363. return 0;
  364. }
  365. /* consume whatever's left in the input buffer */
  366. if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
  367. return 0;
  368. /* do the printf() into the input buffer, put length in len */
  369. size = (int)(state->size);
  370. state->in[size - 1] = 0;
  371. #ifdef NO_snprintf
  372. # ifdef HAS_sprintf_void
  373. sprintf((char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8,
  374. a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
  375. for (len = 0; len < size; len++)
  376. if (state->in[len] == 0) break;
  377. # else
  378. len = sprintf((char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8,
  379. a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
  380. # endif
  381. #else
  382. # ifdef HAS_snprintf_void
  383. snprintf((char *)(state->in), size, format, a1, a2, a3, a4, a5, a6, a7, a8,
  384. a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
  385. len = strlen((char *)(state->in));
  386. # else
  387. len = snprintf((char *)(state->in), size, format, a1, a2, a3, a4, a5, a6,
  388. a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18,
  389. a19, a20);
  390. # endif
  391. #endif
  392. /* check that printf() results fit in buffer */
  393. if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
  394. return 0;
  395. /* update buffer and position, defer compression until needed */
  396. strm->avail_in = (unsigned)len;
  397. strm->next_in = state->in;
  398. state->x.pos += len;
  399. return len;
  400. }
  401. #endif
  402. /* -- see zlib.h -- */
  403. int ZEXPORT gzflush(file, flush)
  404. gzFile file;
  405. int flush;
  406. {
  407. gz_statep state;
  408. /* get internal structure */
  409. if (file == NULL)
  410. return -1;
  411. state = (gz_statep)file;
  412. /* check that we're writing and that there's no error */
  413. if (state->mode != GZ_WRITE || state->err != Z_OK)
  414. return Z_STREAM_ERROR;
  415. /* check flush parameter */
  416. if (flush < 0 || flush > Z_FINISH)
  417. return Z_STREAM_ERROR;
  418. /* check for seek request */
  419. if (state->seek) {
  420. state->seek = 0;
  421. if (gz_zero(state, state->skip) == -1)
  422. return -1;
  423. }
  424. /* compress remaining data with requested flush */
  425. gz_comp(state, flush);
  426. return state->err;
  427. }
  428. /* -- see zlib.h -- */
  429. int ZEXPORT gzsetparams(file, level, strategy)
  430. gzFile file;
  431. int level;
  432. int strategy;
  433. {
  434. gz_statep state;
  435. z_streamp strm;
  436. /* get internal structure */
  437. if (file == NULL)
  438. return Z_STREAM_ERROR;
  439. state = (gz_statep)file;
  440. strm = &(state->strm);
  441. /* check that we're writing and that there's no error */
  442. if (state->mode != GZ_WRITE || state->err != Z_OK)
  443. return Z_STREAM_ERROR;
  444. /* if no change is requested, then do nothing */
  445. if (level == state->level && strategy == state->strategy)
  446. return Z_OK;
  447. /* check for seek request */
  448. if (state->seek) {
  449. state->seek = 0;
  450. if (gz_zero(state, state->skip) == -1)
  451. return -1;
  452. }
  453. /* change compression parameters for subsequent input */
  454. if (state->size) {
  455. /* flush previous input with previous parameters before changing */
  456. if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH) == -1)
  457. return state->err;
  458. deflateParams(strm, level, strategy);
  459. }
  460. state->level = level;
  461. state->strategy = strategy;
  462. return Z_OK;
  463. }
  464. /* -- see zlib.h -- */
  465. int ZEXPORT gzclose_w(file)
  466. gzFile file;
  467. {
  468. int ret = Z_OK;
  469. gz_statep state;
  470. /* get internal structure */
  471. if (file == NULL)
  472. return Z_STREAM_ERROR;
  473. state = (gz_statep)file;
  474. /* check that we're writing */
  475. if (state->mode != GZ_WRITE)
  476. return Z_STREAM_ERROR;
  477. /* check for seek request */
  478. if (state->seek) {
  479. state->seek = 0;
  480. if (gz_zero(state, state->skip) == -1)
  481. ret = state->err;
  482. }
  483. /* flush, free memory, and close file */
  484. if (gz_comp(state, Z_FINISH) == -1)
  485. ret = state->err;
  486. if (state->size) {
  487. if (!state->direct) {
  488. (void)deflateEnd(&(state->strm));
  489. free(state->out);
  490. }
  491. free(state->in);
  492. }
  493. gz_error(state, Z_OK, NULL);
  494. free(state->path);
  495. if (close(state->fd) == -1)
  496. ret = Z_ERRNO;
  497. free(state);
  498. return ret;
  499. }