gzwrite.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. #ifdef Q_OS_LINUX
  12. int __cdecl write(int _Filehandle,const void *_Buf,unsigned int _MaxCharCount);
  13. int __cdecl close(int _FileHandle);
  14. #endif
  15. /* Initialize state for writing a gzip file. Mark initialization by setting
  16. state->size to non-zero. Return -1 on a memory allocation failure, or 0 on
  17. success. */
  18. local int gz_init(state)
  19. gz_statep state;
  20. {
  21. int ret;
  22. z_streamp strm = &(state->strm);
  23. /* allocate input buffer (double size for gzprintf) */
  24. state->in = (unsigned char *)malloc(state->want << 1);
  25. if (state->in == NULL) {
  26. gz_error(state, Z_MEM_ERROR, "out of memory");
  27. return -1;
  28. }
  29. /* only need output buffer and deflate state if compressing */
  30. if (!state->direct) {
  31. /* allocate output buffer */
  32. state->out = (unsigned char *)malloc(state->want);
  33. if (state->out == NULL) {
  34. free(state->in);
  35. gz_error(state, Z_MEM_ERROR, "out of memory");
  36. return -1;
  37. }
  38. /* allocate deflate memory, set up for gzip compression */
  39. strm->zalloc = Z_NULL;
  40. strm->zfree = Z_NULL;
  41. strm->opaque = Z_NULL;
  42. ret = deflateInit2(strm, state->level, Z_DEFLATED,
  43. MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
  44. if (ret != Z_OK) {
  45. free(state->out);
  46. free(state->in);
  47. gz_error(state, Z_MEM_ERROR, "out of memory");
  48. return -1;
  49. }
  50. strm->next_in = NULL;
  51. }
  52. /* mark state as initialized */
  53. state->size = state->want;
  54. /* initialize write buffer if compressing */
  55. if (!state->direct) {
  56. strm->avail_out = state->size;
  57. strm->next_out = state->out;
  58. state->x.next = strm->next_out;
  59. }
  60. return 0;
  61. }
  62. /* Compress whatever is at avail_in and next_in and write to the output file.
  63. Return -1 if there is an error writing to the output file or if gz_init()
  64. fails to allocate memory, otherwise 0. flush is assumed to be a valid
  65. deflate() flush value. If flush is Z_FINISH, then the deflate() state is
  66. reset to start a new gzip stream. If gz->direct is true, then simply write
  67. to the output file without compressing, and ignore flush. */
  68. local int gz_comp(state, flush)
  69. gz_statep state;
  70. int flush;
  71. {
  72. int ret, writ;
  73. unsigned have, put, max = ((unsigned)-1 >> 2) + 1;
  74. z_streamp strm = &(state->strm);
  75. /* allocate memory if this is the first time through */
  76. if (state->size == 0 && gz_init(state) == -1)
  77. return -1;
  78. /* write directly if requested */
  79. if (state->direct) {
  80. while (strm->avail_in) {
  81. put = strm->avail_in > max ? max : strm->avail_in;
  82. writ = write(state->fd, strm->next_in, put);
  83. if (writ < 0) {
  84. gz_error(state, Z_ERRNO, zstrerror());
  85. return -1;
  86. }
  87. strm->avail_in -= (unsigned)writ;
  88. strm->next_in += writ;
  89. }
  90. return 0;
  91. }
  92. /* run deflate() on provided input until it produces no more output */
  93. ret = Z_OK;
  94. do {
  95. /* write out current buffer contents if full, or if flushing, but if
  96. doing Z_FINISH then don't write until we get to Z_STREAM_END */
  97. if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
  98. (flush != Z_FINISH || ret == Z_STREAM_END))) {
  99. while (strm->next_out > state->x.next) {
  100. put = strm->next_out - state->x.next > (int)max ? max :
  101. (unsigned)(strm->next_out - state->x.next);
  102. writ = write(state->fd, state->x.next, put);
  103. if (writ < 0) {
  104. gz_error(state, Z_ERRNO, zstrerror());
  105. return -1;
  106. }
  107. state->x.next += writ;
  108. }
  109. if (strm->avail_out == 0) {
  110. strm->avail_out = state->size;
  111. strm->next_out = state->out;
  112. state->x.next = state->out;
  113. }
  114. }
  115. /* compress */
  116. have = strm->avail_out;
  117. ret = deflate(strm, flush);
  118. if (ret == Z_STREAM_ERROR) {
  119. gz_error(state, Z_STREAM_ERROR,
  120. "internal error: deflate stream corrupt");
  121. return -1;
  122. }
  123. have -= strm->avail_out;
  124. } while (have);
  125. /* if that completed a deflate stream, allow another to start */
  126. if (flush == Z_FINISH)
  127. deflateReset(strm);
  128. /* all done, no errors */
  129. return 0;
  130. }
  131. /* Compress len zeros to output. Return -1 on a write error or memory
  132. allocation failure by gz_comp(), or 0 on success. */
  133. local int gz_zero(state, len)
  134. gz_statep state;
  135. z_off64_t len;
  136. {
  137. int first;
  138. unsigned n;
  139. z_streamp strm = &(state->strm);
  140. /* consume whatever's left in the input buffer */
  141. if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
  142. return -1;
  143. /* compress len zeros (len guaranteed > 0) */
  144. first = 1;
  145. while (len) {
  146. n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
  147. (unsigned)len : state->size;
  148. if (first) {
  149. memset(state->in, 0, n);
  150. first = 0;
  151. }
  152. strm->avail_in = n;
  153. strm->next_in = state->in;
  154. state->x.pos += n;
  155. if (gz_comp(state, Z_NO_FLUSH) == -1)
  156. return -1;
  157. len -= n;
  158. }
  159. return 0;
  160. }
  161. /* Write len bytes from buf to file. Return the number of bytes written. If
  162. the returned value is less than len, then there was an error. */
  163. local z_size_t gz_write(state, buf, len)
  164. gz_statep state;
  165. voidpc buf;
  166. z_size_t len;
  167. {
  168. z_size_t put = len;
  169. /* if len is zero, avoid unnecessary operations */
  170. if (len == 0)
  171. return 0;
  172. /* allocate memory if this is the first time through */
  173. if (state->size == 0 && gz_init(state) == -1)
  174. return 0;
  175. /* check for seek request */
  176. if (state->seek) {
  177. state->seek = 0;
  178. if (gz_zero(state, state->skip) == -1)
  179. return 0;
  180. }
  181. /* for small len, copy to input buffer, otherwise compress directly */
  182. if (len < state->size) {
  183. /* copy to input buffer, compress when full */
  184. do {
  185. unsigned have, copy;
  186. if (state->strm.avail_in == 0)
  187. state->strm.next_in = state->in;
  188. have = (unsigned)((state->strm.next_in + state->strm.avail_in) -
  189. state->in);
  190. copy = state->size - have;
  191. if (copy > len)
  192. copy = len;
  193. memcpy(state->in + have, buf, copy);
  194. state->strm.avail_in += copy;
  195. state->x.pos += copy;
  196. buf = (const char *)buf + copy;
  197. len -= copy;
  198. if (len && gz_comp(state, Z_NO_FLUSH) == -1)
  199. return 0;
  200. } while (len);
  201. }
  202. else {
  203. /* consume whatever's left in the input buffer */
  204. if (state->strm.avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
  205. return 0;
  206. /* directly compress user buffer to file */
  207. state->strm.next_in = (z_const Bytef *)buf;
  208. do {
  209. unsigned n = (unsigned)-1;
  210. if (n > len)
  211. n = len;
  212. state->strm.avail_in = n;
  213. state->x.pos += n;
  214. if (gz_comp(state, Z_NO_FLUSH) == -1)
  215. return 0;
  216. len -= n;
  217. } while (len);
  218. }
  219. /* input was all buffered or compressed */
  220. return put;
  221. }
  222. /* -- see zlib.h -- */
  223. int ZEXPORT gzwrite(file, buf, len)
  224. gzFile file;
  225. voidpc buf;
  226. unsigned len;
  227. {
  228. gz_statep state;
  229. /* get internal structure */
  230. if (file == NULL)
  231. return 0;
  232. state = (gz_statep)file;
  233. /* check that we're writing and that there's no error */
  234. if (state->mode != GZ_WRITE || state->err != Z_OK)
  235. return 0;
  236. /* since an int is returned, make sure len fits in one, otherwise return
  237. with an error (this avoids a flaw in the interface) */
  238. if ((int)len < 0) {
  239. gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
  240. return 0;
  241. }
  242. /* write len bytes from buf (the return value will fit in an int) */
  243. return (int)gz_write(state, buf, len);
  244. }
  245. /* -- see zlib.h -- */
  246. z_size_t ZEXPORT gzfwrite(buf, size, nitems, file)
  247. voidpc buf;
  248. z_size_t size;
  249. z_size_t nitems;
  250. gzFile file;
  251. {
  252. z_size_t len;
  253. gz_statep state;
  254. /* get internal structure */
  255. if (file == NULL)
  256. return 0;
  257. state = (gz_statep)file;
  258. /* check that we're writing and that there's no error */
  259. if (state->mode != GZ_WRITE || 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 = (gz_statep)file;
  283. strm = &(state->strm);
  284. /* check that we're writing and that there's no error */
  285. if (state->mode != GZ_WRITE || state->err != Z_OK)
  286. return -1;
  287. /* check for seek request */
  288. if (state->seek) {
  289. state->seek = 0;
  290. if (gz_zero(state, state->skip) == -1)
  291. return -1;
  292. }
  293. /* try writing to input buffer for speed (state->size == 0 if buffer not
  294. initialized) */
  295. if (state->size) {
  296. if (strm->avail_in == 0)
  297. strm->next_in = state->in;
  298. have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
  299. if (have < state->size) {
  300. state->in[have] = (unsigned char)c;
  301. strm->avail_in++;
  302. 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 = (gz_statep)file;
  324. /* check that we're writing and that there's no error */
  325. if (state->mode != GZ_WRITE || state->err != Z_OK)
  326. return -1;
  327. /* write string */
  328. len = strlen(str);
  329. ret = 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 = (gz_statep)file;
  346. strm = &(state->strm);
  347. /* check that we're writing and that there's no error */
  348. if (state->mode != GZ_WRITE || state->err != Z_OK)
  349. return Z_STREAM_ERROR;
  350. /* make sure we have some buffer space */
  351. if (state->size == 0 && gz_init(state) == -1)
  352. return state->err;
  353. /* check for seek request */
  354. if (state->seek) {
  355. state->seek = 0;
  356. if (gz_zero(state, state->skip) == -1)
  357. return 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->size bytes available after the current contents */
  362. if (strm->avail_in == 0)
  363. strm->next_in = state->in;
  364. next = (char *)(state->in + (strm->next_in - state->in) + strm->avail_in);
  365. next[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->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->size, format, va);
  377. len = strlen(next);
  378. # else
  379. len = vsnprintf(next, state->size, format, va);
  380. # endif
  381. #endif
  382. /* check that printf() results fit in buffer */
  383. if (len == 0 || (unsigned)len >= state->size || next[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->x.pos += len;
  388. if (strm->avail_in >= state->size) {
  389. left = strm->avail_in - state->size;
  390. strm->avail_in = state->size;
  391. if (gz_comp(state, Z_NO_FLUSH) == -1)
  392. return state->err;
  393. memcpy(state->in, state->in + state->size, left);
  394. strm->next_in = 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->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->mode != GZ_WRITE || state->err != Z_OK)
  431. return Z_STREAM_ERROR;
  432. /* make sure we have some buffer space */
  433. if (state->size == 0 && gz_init(state) == -1)
  434. return state->error;
  435. /* check for seek request */
  436. if (state->seek) {
  437. state->seek = 0;
  438. if (gz_zero(state, state->skip) == -1)
  439. return 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->size bytes available after the current contents */
  444. if (strm->avail_in == 0)
  445. strm->next_in = state->in;
  446. next = (char *)(strm->next_in + strm->avail_in);
  447. next[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->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->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->size || next[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->x.pos += len;
  475. if (strm->avail_in >= state->size) {
  476. left = strm->avail_in - state->size;
  477. strm->avail_in = state->size;
  478. if (gz_comp(state, Z_NO_FLUSH) == -1)
  479. return state->err;
  480. memcpy(state->in, state->in + state->size, left);
  481. strm->next_in = 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 = (gz_statep)file;
  497. /* check that we're writing and that there's no error */
  498. if (state->mode != GZ_WRITE || 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->seek) {
  505. state->seek = 0;
  506. if (gz_zero(state, state->skip) == -1)
  507. return state->err;
  508. }
  509. /* compress remaining data with requested flush */
  510. (void)gz_comp(state, flush);
  511. return 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 = (gz_statep)file;
  525. strm = &(state->strm);
  526. /* check that we're writing and that there's no error */
  527. if (state->mode != GZ_WRITE || state->err != Z_OK)
  528. return Z_STREAM_ERROR;
  529. /* if no change is requested, then do nothing */
  530. if (level == state->level && strategy == state->strategy)
  531. return Z_OK;
  532. /* check for seek request */
  533. if (state->seek) {
  534. state->seek = 0;
  535. if (gz_zero(state, state->skip) == -1)
  536. return state->err;
  537. }
  538. /* change compression parameters for subsequent input */
  539. if (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->err;
  543. deflateParams(strm, level, strategy);
  544. }
  545. state->level = level;
  546. 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 = (gz_statep)file;
  559. /* check that we're writing */
  560. if (state->mode != GZ_WRITE)
  561. return Z_STREAM_ERROR;
  562. /* check for seek request */
  563. if (state->seek) {
  564. state->seek = 0;
  565. if (gz_zero(state, state->skip) == -1)
  566. ret = state->err;
  567. }
  568. /* flush, free memory, and close file */
  569. if (gz_comp(state, Z_FINISH) == -1)
  570. ret = state->err;
  571. if (state->size) {
  572. if (!state->direct) {
  573. (void)deflateEnd(&(state->strm));
  574. free(state->out);
  575. }
  576. free(state->in);
  577. }
  578. gz_error(state, Z_OK, NULL);
  579. free(state->path);
  580. if (close(state->fd) == -1)
  581. ret = Z_ERRNO;
  582. free(state);
  583. return ret;
  584. }