gzwrite.c 19 KB

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