gzwrite.c 20 KB

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