2
0

gzwrite.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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(
  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(
  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(
  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(
  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 = 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 = 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(
  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(
  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(
  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(
  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 = 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 (
  407. gzFile file,
  408. const char *format,
  409. int a1,
  410. int a2,
  411. int a3,
  412. int a4,
  413. int a5,
  414. int a6,
  415. int a7,
  416. int a8,
  417. int a9,
  418. int a10,
  419. int a11,
  420. int a12,
  421. int a13,
  422. int a14,
  423. int a15,
  424. int a16,
  425. int a17,
  426. int a18,
  427. int a19,
  428. int a20)
  429. {
  430. unsigned len, left;
  431. char *next;
  432. gz_statep state;
  433. z_streamp strm;
  434. /* get internal structure */
  435. if (file == NULL)
  436. return Z_STREAM_ERROR;
  437. state = (gz_statep)file;
  438. strm = &(state->strm);
  439. /* check that can really pass pointer in ints */
  440. if (sizeof(int) != sizeof(void *))
  441. return Z_STREAM_ERROR;
  442. /* check that we're writing and that there's no error */
  443. if (state->mode != GZ_WRITE || state->err != Z_OK)
  444. return Z_STREAM_ERROR;
  445. /* make sure we have some buffer space */
  446. if (state->size == 0 && gz_init(state) == -1)
  447. return state->error;
  448. /* check for seek request */
  449. if (state->seek) {
  450. state->seek = 0;
  451. if (gz_zero(state, state->skip) == -1)
  452. return state->error;
  453. }
  454. /* do the printf() into the input buffer, put length in len -- the input
  455. buffer is double-sized just for this function, so there is guaranteed to
  456. be state->size bytes available after the current contents */
  457. if (strm->avail_in == 0)
  458. strm->next_in = state->in;
  459. next = (char *)(strm->next_in + strm->avail_in);
  460. next[state->size - 1] = 0;
  461. #ifdef NO_snprintf
  462. # ifdef HAS_sprintf_void
  463. sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12,
  464. a13, a14, a15, a16, a17, a18, a19, a20);
  465. for (len = 0; len < size; len++)
  466. if (next[len] == 0)
  467. break;
  468. # else
  469. len = sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11,
  470. a12, a13, a14, a15, a16, a17, a18, a19, a20);
  471. # endif
  472. #else
  473. # ifdef HAS_snprintf_void
  474. snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8, a9,
  475. a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
  476. len = strlen(next);
  477. # else
  478. len = snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8,
  479. a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
  480. # endif
  481. #endif
  482. /* check that printf() results fit in buffer */
  483. if (len == 0 || len >= state->size || next[state->size - 1] != 0)
  484. return 0;
  485. /* update buffer and position, compress first half if past that */
  486. strm->avail_in += len;
  487. state->x.pos += len;
  488. if (strm->avail_in >= state->size) {
  489. left = strm->avail_in - state->size;
  490. strm->avail_in = state->size;
  491. if (gz_comp(state, Z_NO_FLUSH) == -1)
  492. return state->err;
  493. memcpy(state->in, state->in + state->size, left);
  494. strm->next_in = state->in;
  495. strm->avail_in = left;
  496. }
  497. return (int)len;
  498. }
  499. #endif
  500. /* -- see zlib.h -- */
  501. int ZEXPORT gzflush(
  502. gzFile file,
  503. int flush)
  504. {
  505. gz_statep state;
  506. /* get internal structure */
  507. if (file == NULL)
  508. return Z_STREAM_ERROR;
  509. state = (gz_statep)file;
  510. /* check that we're writing and that there's no error */
  511. if (state->mode != GZ_WRITE || state->err != Z_OK)
  512. return Z_STREAM_ERROR;
  513. /* check flush parameter */
  514. if (flush < 0 || flush > Z_FINISH)
  515. return Z_STREAM_ERROR;
  516. /* check for seek request */
  517. if (state->seek) {
  518. state->seek = 0;
  519. if (gz_zero(state, state->skip) == -1)
  520. return state->err;
  521. }
  522. /* compress remaining data with requested flush */
  523. (void)gz_comp(state, flush);
  524. return state->err;
  525. }
  526. /* -- see zlib.h -- */
  527. int ZEXPORT gzsetparams(
  528. gzFile file,
  529. int level,
  530. int strategy)
  531. {
  532. gz_statep state;
  533. z_streamp strm;
  534. /* get internal structure */
  535. if (file == NULL)
  536. return Z_STREAM_ERROR;
  537. state = (gz_statep)file;
  538. strm = &(state->strm);
  539. /* check that we're writing and that there's no error */
  540. if (state->mode != GZ_WRITE || state->err != Z_OK)
  541. return Z_STREAM_ERROR;
  542. /* if no change is requested, then do nothing */
  543. if (level == state->level && strategy == state->strategy)
  544. return Z_OK;
  545. /* check for seek request */
  546. if (state->seek) {
  547. state->seek = 0;
  548. if (gz_zero(state, state->skip) == -1)
  549. return state->err;
  550. }
  551. /* change compression parameters for subsequent input */
  552. if (state->size) {
  553. /* flush previous input with previous parameters before changing */
  554. if (strm->avail_in && gz_comp(state, Z_BLOCK) == -1)
  555. return state->err;
  556. deflateParams(strm, level, strategy);
  557. }
  558. state->level = level;
  559. state->strategy = strategy;
  560. return Z_OK;
  561. }
  562. /* -- see zlib.h -- */
  563. int ZEXPORT gzclose_w(
  564. gzFile file)
  565. {
  566. int ret = Z_OK;
  567. gz_statep state;
  568. /* get internal structure */
  569. if (file == NULL)
  570. return Z_STREAM_ERROR;
  571. state = (gz_statep)file;
  572. /* check that we're writing */
  573. if (state->mode != GZ_WRITE)
  574. return Z_STREAM_ERROR;
  575. /* check for seek request */
  576. if (state->seek) {
  577. state->seek = 0;
  578. if (gz_zero(state, state->skip) == -1)
  579. ret = state->err;
  580. }
  581. /* flush, free memory, and close file */
  582. if (gz_comp(state, Z_FINISH) == -1)
  583. ret = state->err;
  584. if (state->size) {
  585. if (!state->direct) {
  586. (void)deflateEnd(&(state->strm));
  587. free(state->out);
  588. }
  589. free(state->in);
  590. }
  591. gz_error(state, Z_OK, NULL);
  592. free(state->path);
  593. if (close(state->fd) == -1)
  594. ret = Z_ERRNO;
  595. free(state);
  596. return ret;
  597. }