1
0

gzwrite.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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. /* write len bytes to buf, return the number of full items written */
  272. return len ? gz_write(state, buf, len) / size : 0;
  273. }
  274. /* -- see zlib.h -- */
  275. int ZEXPORT gzputc(file, c)
  276. gzFile file;
  277. int c;
  278. {
  279. unsigned have;
  280. unsigned char buf[1];
  281. gz_statep state;
  282. z_streamp strm;
  283. /* get internal structure */
  284. if (file == NULL)
  285. return -1;
  286. state = (gz_statep)file;
  287. strm = &(state->strm);
  288. /* check that we're writing and that there's no error */
  289. if (state->mode != GZ_WRITE || state->err != Z_OK)
  290. return -1;
  291. /* check for seek request */
  292. if (state->seek) {
  293. state->seek = 0;
  294. if (gz_zero(state, state->skip) == -1)
  295. return -1;
  296. }
  297. /* try writing to input buffer for speed (state->size == 0 if buffer not
  298. initialized) */
  299. if (state->size) {
  300. if (strm->avail_in == 0)
  301. strm->next_in = state->in;
  302. have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
  303. if (have < state->size) {
  304. state->in[have] = (unsigned char)c;
  305. strm->avail_in++;
  306. state->x.pos++;
  307. return c & 0xff;
  308. }
  309. }
  310. /* no room in buffer or not initialized, use gz_write() */
  311. buf[0] = (unsigned char)c;
  312. if (gz_write(state, buf, 1) != 1)
  313. return -1;
  314. return c & 0xff;
  315. }
  316. /* -- see zlib.h -- */
  317. int ZEXPORT gzputs(file, s)
  318. gzFile file;
  319. const char *s;
  320. {
  321. z_size_t len, put;
  322. gz_statep state;
  323. /* get internal structure */
  324. if (file == NULL)
  325. return -1;
  326. state = (gz_statep)file;
  327. /* check that we're writing and that there's no error */
  328. if (state->mode != GZ_WRITE || state->err != Z_OK)
  329. return -1;
  330. /* write string */
  331. len = strlen(s);
  332. if ((int)len < 0 || (unsigned)len != len) {
  333. gz_error(state, Z_STREAM_ERROR, "string length does not fit in int");
  334. return -1;
  335. }
  336. put = gz_write(state, s, len);
  337. return put < len ? -1 : (int)len;
  338. }
  339. #if defined(STDC) || defined(Z_HAVE_STDARG_H)
  340. #include <stdarg.h>
  341. /* -- see zlib.h -- */
  342. int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va)
  343. {
  344. int len;
  345. unsigned left;
  346. char *next;
  347. gz_statep state;
  348. z_streamp strm;
  349. /* get internal structure */
  350. if (file == NULL)
  351. return Z_STREAM_ERROR;
  352. state = (gz_statep)file;
  353. strm = &(state->strm);
  354. /* check that we're writing and that there's no error */
  355. if (state->mode != GZ_WRITE || state->err != Z_OK)
  356. return Z_STREAM_ERROR;
  357. /* make sure we have some buffer space */
  358. if (state->size == 0 && gz_init(state) == -1)
  359. return state->err;
  360. /* check for seek request */
  361. if (state->seek) {
  362. state->seek = 0;
  363. if (gz_zero(state, state->skip) == -1)
  364. return state->err;
  365. }
  366. /* do the printf() into the input buffer, put length in len -- the input
  367. buffer is double-sized just for this function, so there is guaranteed to
  368. be state->size bytes available after the current contents */
  369. if (strm->avail_in == 0)
  370. strm->next_in = state->in;
  371. next = (char *)(state->in + (strm->next_in - state->in) + strm->avail_in);
  372. next[state->size - 1] = 0;
  373. #ifdef NO_vsnprintf
  374. # ifdef HAS_vsprintf_void
  375. (void)vsprintf(next, format, va);
  376. for (len = 0; len < state->size; len++)
  377. if (next[len] == 0) break;
  378. # else
  379. len = vsprintf(next, format, va);
  380. # endif
  381. #else
  382. # ifdef HAS_vsnprintf_void
  383. (void)vsnprintf(next, state->size, format, va);
  384. len = strlen(next);
  385. # else
  386. len = vsnprintf(next, state->size, format, va);
  387. # endif
  388. #endif
  389. /* check that printf() results fit in buffer */
  390. if (len == 0 || (unsigned)len >= state->size || next[state->size - 1] != 0)
  391. return 0;
  392. /* update buffer and position, compress first half if past that */
  393. strm->avail_in += (unsigned)len;
  394. state->x.pos += len;
  395. if (strm->avail_in >= state->size) {
  396. left = strm->avail_in - state->size;
  397. strm->avail_in = state->size;
  398. if (gz_comp(state, Z_NO_FLUSH) == -1)
  399. return state->err;
  400. memmove(state->in, state->in + state->size, left);
  401. strm->next_in = state->in;
  402. strm->avail_in = left;
  403. }
  404. return len;
  405. }
  406. int ZEXPORTVA gzprintf(gzFile file, const char *format, ...)
  407. {
  408. va_list va;
  409. int ret;
  410. va_start(va, format);
  411. ret = gzvprintf(file, format, va);
  412. va_end(va);
  413. return ret;
  414. }
  415. #else /* !STDC && !Z_HAVE_STDARG_H */
  416. /* -- see zlib.h -- */
  417. int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
  418. a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
  419. gzFile file;
  420. const char *format;
  421. int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
  422. a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
  423. {
  424. unsigned len, left;
  425. char *next;
  426. gz_statep state;
  427. z_streamp strm;
  428. /* get internal structure */
  429. if (file == NULL)
  430. return Z_STREAM_ERROR;
  431. state = (gz_statep)file;
  432. strm = &(state->strm);
  433. /* check that can really pass pointer in ints */
  434. if (sizeof(int) != sizeof(void *))
  435. return Z_STREAM_ERROR;
  436. /* check that we're writing and that there's no error */
  437. if (state->mode != GZ_WRITE || state->err != Z_OK)
  438. return Z_STREAM_ERROR;
  439. /* make sure we have some buffer space */
  440. if (state->size == 0 && gz_init(state) == -1)
  441. return state->error;
  442. /* check for seek request */
  443. if (state->seek) {
  444. state->seek = 0;
  445. if (gz_zero(state, state->skip) == -1)
  446. return state->error;
  447. }
  448. /* do the printf() into the input buffer, put length in len -- the input
  449. buffer is double-sized just for this function, so there is guaranteed to
  450. be state->size bytes available after the current contents */
  451. if (strm->avail_in == 0)
  452. strm->next_in = state->in;
  453. next = (char *)(strm->next_in + strm->avail_in);
  454. next[state->size - 1] = 0;
  455. #ifdef NO_snprintf
  456. # ifdef HAS_sprintf_void
  457. sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12,
  458. a13, a14, a15, a16, a17, a18, a19, a20);
  459. for (len = 0; len < size; len++)
  460. if (next[len] == 0)
  461. break;
  462. # else
  463. len = sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11,
  464. a12, a13, a14, a15, a16, a17, a18, a19, a20);
  465. # endif
  466. #else
  467. # ifdef HAS_snprintf_void
  468. snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8, a9,
  469. a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
  470. len = strlen(next);
  471. # else
  472. len = snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8,
  473. a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
  474. # endif
  475. #endif
  476. /* check that printf() results fit in buffer */
  477. if (len == 0 || len >= state->size || next[state->size - 1] != 0)
  478. return 0;
  479. /* update buffer and position, compress first half if past that */
  480. strm->avail_in += len;
  481. state->x.pos += len;
  482. if (strm->avail_in >= state->size) {
  483. left = strm->avail_in - state->size;
  484. strm->avail_in = state->size;
  485. if (gz_comp(state, Z_NO_FLUSH) == -1)
  486. return state->err;
  487. memmove(state->in, state->in + state->size, left);
  488. strm->next_in = state->in;
  489. strm->avail_in = left;
  490. }
  491. return (int)len;
  492. }
  493. #endif
  494. /* -- see zlib.h -- */
  495. int ZEXPORT gzflush(file, flush)
  496. gzFile file;
  497. int flush;
  498. {
  499. gz_statep state;
  500. /* get internal structure */
  501. if (file == NULL)
  502. return Z_STREAM_ERROR;
  503. state = (gz_statep)file;
  504. /* check that we're writing and that there's no error */
  505. if (state->mode != GZ_WRITE || state->err != Z_OK)
  506. return Z_STREAM_ERROR;
  507. /* check flush parameter */
  508. if (flush < 0 || flush > Z_FINISH)
  509. return Z_STREAM_ERROR;
  510. /* check for seek request */
  511. if (state->seek) {
  512. state->seek = 0;
  513. if (gz_zero(state, state->skip) == -1)
  514. return state->err;
  515. }
  516. /* compress remaining data with requested flush */
  517. (void)gz_comp(state, flush);
  518. return state->err;
  519. }
  520. /* -- see zlib.h -- */
  521. int ZEXPORT gzsetparams(file, level, strategy)
  522. gzFile file;
  523. int level;
  524. int strategy;
  525. {
  526. gz_statep state;
  527. z_streamp strm;
  528. /* get internal structure */
  529. if (file == NULL)
  530. return Z_STREAM_ERROR;
  531. state = (gz_statep)file;
  532. strm = &(state->strm);
  533. /* check that we're writing and that there's no error */
  534. if (state->mode != GZ_WRITE || state->err != Z_OK)
  535. return Z_STREAM_ERROR;
  536. /* if no change is requested, then do nothing */
  537. if (level == state->level && strategy == state->strategy)
  538. return Z_OK;
  539. /* check for seek request */
  540. if (state->seek) {
  541. state->seek = 0;
  542. if (gz_zero(state, state->skip) == -1)
  543. return state->err;
  544. }
  545. /* change compression parameters for subsequent input */
  546. if (state->size) {
  547. /* flush previous input with previous parameters before changing */
  548. if (strm->avail_in && gz_comp(state, Z_BLOCK) == -1)
  549. return state->err;
  550. deflateParams(strm, level, strategy);
  551. }
  552. state->level = level;
  553. state->strategy = strategy;
  554. return Z_OK;
  555. }
  556. /* -- see zlib.h -- */
  557. int ZEXPORT gzclose_w(file)
  558. gzFile file;
  559. {
  560. int ret = Z_OK;
  561. gz_statep state;
  562. /* get internal structure */
  563. if (file == NULL)
  564. return Z_STREAM_ERROR;
  565. state = (gz_statep)file;
  566. /* check that we're writing */
  567. if (state->mode != GZ_WRITE)
  568. return Z_STREAM_ERROR;
  569. /* check for seek request */
  570. if (state->seek) {
  571. state->seek = 0;
  572. if (gz_zero(state, state->skip) == -1)
  573. ret = state->err;
  574. }
  575. /* flush, free memory, and close file */
  576. if (gz_comp(state, Z_FINISH) == -1)
  577. ret = state->err;
  578. if (state->size) {
  579. if (!state->direct) {
  580. (void)deflateEnd(&(state->strm));
  581. free(state->out);
  582. }
  583. free(state->in);
  584. }
  585. gz_error(state, Z_OK, NULL);
  586. free(state->path);
  587. if (close(state->fd) == -1)
  588. ret = Z_ERRNO;
  589. free(state);
  590. return ret;
  591. }