gzwrite.c 19 KB

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