/contrib/cvs/src/zlib.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 760 lines · 515 code · 116 blank · 129 comment · 137 complexity · 7113565ded1d9989f23e19908500bfd5 MD5 · raw file

  1. /* zlib.c --- interface to the zlib compression library
  2. Ian Lance Taylor <ian@cygnus.com>
  3. This file is part of GNU CVS.
  4. GNU CVS is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details. */
  12. /* The routines in this file are the interface between the CVS
  13. client/server support and the zlib compression library. */
  14. #include <assert.h>
  15. #include "cvs.h"
  16. #include "buffer.h"
  17. #if defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT)
  18. #include "zlib.h"
  19. /* OS/2 doesn't have EIO. FIXME: this whole notion of turning
  20. a different error into EIO strikes me as pretty dubious. */
  21. #if !defined (EIO)
  22. #define EIO EBADPOS
  23. #endif
  24. /* The compression interface is built upon the buffer data structure.
  25. We provide a buffer type which compresses or decompresses the data
  26. which passes through it. An input buffer decompresses the data
  27. read from an underlying buffer, and an output buffer compresses the
  28. data before writing it to an underlying buffer. */
  29. /* This structure is the closure field of the buffer. */
  30. struct compress_buffer
  31. {
  32. /* The underlying buffer. */
  33. struct buffer *buf;
  34. /* The compression information. */
  35. z_stream zstr;
  36. };
  37. static void compress_error PROTO((int, int, z_stream *, const char *));
  38. static int compress_buffer_input PROTO((void *, char *, int, int, int *));
  39. static int compress_buffer_output PROTO((void *, const char *, int, int *));
  40. static int compress_buffer_flush PROTO((void *));
  41. static int compress_buffer_block PROTO((void *, int));
  42. static int compress_buffer_shutdown_input PROTO((struct buffer *));
  43. static int compress_buffer_shutdown_output PROTO((struct buffer *));
  44. /* Report an error from one of the zlib functions. */
  45. static void
  46. compress_error (status, zstatus, zstr, msg)
  47. int status;
  48. int zstatus;
  49. z_stream *zstr;
  50. const char *msg;
  51. {
  52. int hold_errno;
  53. const char *zmsg;
  54. char buf[100];
  55. hold_errno = errno;
  56. zmsg = zstr->msg;
  57. if (zmsg == NULL)
  58. {
  59. sprintf (buf, "error %d", zstatus);
  60. zmsg = buf;
  61. }
  62. error (status,
  63. zstatus == Z_ERRNO ? hold_errno : 0,
  64. "%s: %s", msg, zmsg);
  65. }
  66. /* Create a compression buffer. */
  67. struct buffer *
  68. compress_buffer_initialize (buf, input, level, memory)
  69. struct buffer *buf;
  70. int input;
  71. int level;
  72. void (*memory) PROTO((struct buffer *));
  73. {
  74. struct compress_buffer *n;
  75. int zstatus;
  76. n = (struct compress_buffer *) xmalloc (sizeof *n);
  77. memset (n, 0, sizeof *n);
  78. n->buf = buf;
  79. if (input)
  80. zstatus = inflateInit (&n->zstr);
  81. else
  82. zstatus = deflateInit (&n->zstr, level);
  83. if (zstatus != Z_OK)
  84. compress_error (1, zstatus, &n->zstr, "compression initialization");
  85. /* There may already be data buffered on BUF. For an output
  86. buffer, this is OK, because these routines will just use the
  87. buffer routines to append data to the (uncompressed) data
  88. already on BUF. An input buffer expects to handle a single
  89. buffer_data of buffered input to be uncompressed, so that is OK
  90. provided there is only one buffer. At present that is all
  91. there ever will be; if this changes, compress_buffer_input must
  92. be modified to handle multiple input buffers. */
  93. assert (! input || buf->data == NULL || buf->data->next == NULL);
  94. return buf_initialize (input ? compress_buffer_input : NULL,
  95. input ? NULL : compress_buffer_output,
  96. input ? NULL : compress_buffer_flush,
  97. compress_buffer_block,
  98. (input
  99. ? compress_buffer_shutdown_input
  100. : compress_buffer_shutdown_output),
  101. memory,
  102. n);
  103. }
  104. /* Input data from a compression buffer. */
  105. static int
  106. compress_buffer_input (closure, data, need, size, got)
  107. void *closure;
  108. char *data;
  109. int need;
  110. int size;
  111. int *got;
  112. {
  113. struct compress_buffer *cb = (struct compress_buffer *) closure;
  114. struct buffer_data *bd;
  115. if (cb->buf->input == NULL)
  116. abort ();
  117. /* We use a single buffer_data structure to buffer up data which
  118. the z_stream structure won't use yet. We can safely store this
  119. on cb->buf->data, because we never call the buffer routines on
  120. cb->buf; we only call the buffer input routine, since that
  121. gives us the semantics we want. As noted in
  122. compress_buffer_initialize, the buffer_data structure may
  123. already exist, and hold data which was already read and
  124. buffered before the decompression began. */
  125. bd = cb->buf->data;
  126. if (bd == NULL)
  127. {
  128. bd = ((struct buffer_data *) xmalloc (sizeof (struct buffer_data)));
  129. if (bd == NULL)
  130. return -2;
  131. bd->text = (char *) xmalloc (BUFFER_DATA_SIZE);
  132. if (bd->text == NULL)
  133. {
  134. free (bd);
  135. return -2;
  136. }
  137. bd->bufp = bd->text;
  138. bd->size = 0;
  139. cb->buf->data = bd;
  140. }
  141. cb->zstr.avail_out = size;
  142. cb->zstr.next_out = (Bytef *) data;
  143. while (1)
  144. {
  145. int zstatus, sofar, status, nread;
  146. /* First try to inflate any data we already have buffered up.
  147. This is useful even if we don't have any buffered data,
  148. because there may be data buffered inside the z_stream
  149. structure. */
  150. cb->zstr.avail_in = bd->size;
  151. cb->zstr.next_in = (Bytef *) bd->bufp;
  152. do
  153. {
  154. zstatus = inflate (&cb->zstr, Z_NO_FLUSH);
  155. if (zstatus == Z_STREAM_END)
  156. break;
  157. if (zstatus != Z_OK && zstatus != Z_BUF_ERROR)
  158. {
  159. compress_error (0, zstatus, &cb->zstr, "inflate");
  160. return EIO;
  161. }
  162. } while (cb->zstr.avail_in > 0
  163. && cb->zstr.avail_out > 0);
  164. bd->size = cb->zstr.avail_in;
  165. bd->bufp = (char *) cb->zstr.next_in;
  166. if (zstatus == Z_STREAM_END)
  167. return -1;
  168. /* If we have obtained NEED bytes, then return, unless NEED is
  169. zero and we haven't obtained anything at all. If NEED is
  170. zero, we will keep reading from the underlying buffer until
  171. we either can't read anything, or we have managed to
  172. inflate at least one byte. */
  173. sofar = size - cb->zstr.avail_out;
  174. if (sofar > 0 && sofar >= need)
  175. break;
  176. /* All our buffered data should have been processed at this
  177. point. */
  178. assert (bd->size == 0);
  179. /* This will work well in the server, because this call will
  180. do an unblocked read and fetch all the available data. In
  181. the client, this will read a single byte from the stdio
  182. stream, which will cause us to call inflate once per byte.
  183. It would be more efficient if we could make a call which
  184. would fetch all the available bytes, and at least one byte. */
  185. status = (*cb->buf->input) (cb->buf->closure, bd->text,
  186. need > 0 ? 1 : 0,
  187. BUFFER_DATA_SIZE, &nread);
  188. if (status != 0)
  189. return status;
  190. /* If we didn't read anything, then presumably the buffer is
  191. in nonblocking mode, and we should just get out now with
  192. whatever we've inflated. */
  193. if (nread == 0)
  194. {
  195. assert (need == 0);
  196. break;
  197. }
  198. bd->bufp = bd->text;
  199. bd->size = nread;
  200. }
  201. *got = size - cb->zstr.avail_out;
  202. return 0;
  203. }
  204. /* Output data to a compression buffer. */
  205. static int
  206. compress_buffer_output (closure, data, have, wrote)
  207. void *closure;
  208. const char *data;
  209. int have;
  210. int *wrote;
  211. {
  212. struct compress_buffer *cb = (struct compress_buffer *) closure;
  213. cb->zstr.avail_in = have;
  214. cb->zstr.next_in = (unsigned char *) data;
  215. while (cb->zstr.avail_in > 0)
  216. {
  217. char buffer[BUFFER_DATA_SIZE];
  218. int zstatus;
  219. cb->zstr.avail_out = BUFFER_DATA_SIZE;
  220. cb->zstr.next_out = (unsigned char *) buffer;
  221. zstatus = deflate (&cb->zstr, Z_NO_FLUSH);
  222. if (zstatus != Z_OK)
  223. {
  224. compress_error (0, zstatus, &cb->zstr, "deflate");
  225. return EIO;
  226. }
  227. if (cb->zstr.avail_out != BUFFER_DATA_SIZE)
  228. buf_output (cb->buf, buffer,
  229. BUFFER_DATA_SIZE - cb->zstr.avail_out);
  230. }
  231. *wrote = have;
  232. /* We will only be here because buf_send_output was called on the
  233. compression buffer. That means that we should now call
  234. buf_send_output on the underlying buffer. */
  235. return buf_send_output (cb->buf);
  236. }
  237. /* Flush a compression buffer. */
  238. static int
  239. compress_buffer_flush (closure)
  240. void *closure;
  241. {
  242. struct compress_buffer *cb = (struct compress_buffer *) closure;
  243. cb->zstr.avail_in = 0;
  244. cb->zstr.next_in = NULL;
  245. while (1)
  246. {
  247. char buffer[BUFFER_DATA_SIZE];
  248. int zstatus;
  249. cb->zstr.avail_out = BUFFER_DATA_SIZE;
  250. cb->zstr.next_out = (unsigned char *) buffer;
  251. zstatus = deflate (&cb->zstr, Z_SYNC_FLUSH);
  252. /* The deflate function will return Z_BUF_ERROR if it can't do
  253. anything, which in this case means that all data has been
  254. flushed. */
  255. if (zstatus == Z_BUF_ERROR)
  256. break;
  257. if (zstatus != Z_OK)
  258. {
  259. compress_error (0, zstatus, &cb->zstr, "deflate flush");
  260. return EIO;
  261. }
  262. if (cb->zstr.avail_out != BUFFER_DATA_SIZE)
  263. buf_output (cb->buf, buffer,
  264. BUFFER_DATA_SIZE - cb->zstr.avail_out);
  265. /* If the deflate function did not fill the output buffer,
  266. then all data has been flushed. */
  267. if (cb->zstr.avail_out > 0)
  268. break;
  269. }
  270. /* Now flush the underlying buffer. Note that if the original
  271. call to buf_flush passed 1 for the BLOCK argument, then the
  272. buffer will already have been set into blocking mode, so we
  273. should always pass 0 here. */
  274. return buf_flush (cb->buf, 0);
  275. }
  276. /* The block routine for a compression buffer. */
  277. static int
  278. compress_buffer_block (closure, block)
  279. void *closure;
  280. int block;
  281. {
  282. struct compress_buffer *cb = (struct compress_buffer *) closure;
  283. if (block)
  284. return set_block (cb->buf);
  285. else
  286. return set_nonblock (cb->buf);
  287. }
  288. /* Shut down an input buffer. */
  289. static int
  290. compress_buffer_shutdown_input (buf)
  291. struct buffer *buf;
  292. {
  293. struct compress_buffer *cb = (struct compress_buffer *) buf->closure;
  294. int zstatus;
  295. /* Don't make any attempt to pick up trailing data since we are shutting
  296. * down. If the client doesn't know we are shutting down, we might not
  297. * see the EOF we are expecting.
  298. */
  299. zstatus = inflateEnd (&cb->zstr);
  300. if (zstatus != Z_OK)
  301. {
  302. compress_error (0, zstatus, &cb->zstr, "inflateEnd");
  303. return EIO;
  304. }
  305. return buf_shutdown (cb->buf);
  306. }
  307. /* Shut down an output buffer. */
  308. static int
  309. compress_buffer_shutdown_output (buf)
  310. struct buffer *buf;
  311. {
  312. struct compress_buffer *cb = (struct compress_buffer *) buf->closure;
  313. int zstatus, status;
  314. do
  315. {
  316. char buffer[BUFFER_DATA_SIZE];
  317. cb->zstr.avail_out = BUFFER_DATA_SIZE;
  318. cb->zstr.next_out = (unsigned char *) buffer;
  319. zstatus = deflate (&cb->zstr, Z_FINISH);
  320. if (zstatus != Z_OK && zstatus != Z_STREAM_END)
  321. {
  322. compress_error (0, zstatus, &cb->zstr, "deflate finish");
  323. return EIO;
  324. }
  325. if (cb->zstr.avail_out != BUFFER_DATA_SIZE)
  326. buf_output (cb->buf, buffer,
  327. BUFFER_DATA_SIZE - cb->zstr.avail_out);
  328. } while (zstatus != Z_STREAM_END);
  329. zstatus = deflateEnd (&cb->zstr);
  330. if (zstatus != Z_OK)
  331. {
  332. compress_error (0, zstatus, &cb->zstr, "deflateEnd");
  333. return EIO;
  334. }
  335. status = buf_flush (cb->buf, 1);
  336. if (status != 0)
  337. return status;
  338. return buf_shutdown (cb->buf);
  339. }
  340. /* Here is our librarified gzip implementation. It is very minimal
  341. but attempts to be RFC1952 compliant. */
  342. /* GZIP ID byte values */
  343. #define GZIP_ID1 31
  344. #define GZIP_ID2 139
  345. /* Compression methods */
  346. #define GZIP_CDEFLATE 8
  347. /* Flags */
  348. #define GZIP_FTEXT 1
  349. #define GZIP_FHCRC 2
  350. #define GZIP_FEXTRA 4
  351. #define GZIP_FNAME 8
  352. #define GZIP_FCOMMENT 16
  353. /* BUF should contain SIZE bytes of gzipped data (RFC1952/RFC1951).
  354. We are to uncompress the data and write the result to the file
  355. descriptor FD. If something goes wrong, give a nonfatal error message
  356. mentioning FULLNAME as the name of the file for FD. Return 1 if
  357. it is an error we can't recover from. */
  358. int
  359. gunzip_and_write (fd, fullname, buf, size)
  360. int fd;
  361. char *fullname;
  362. unsigned char *buf;
  363. size_t size;
  364. {
  365. size_t pos;
  366. z_stream zstr;
  367. int zstatus;
  368. unsigned char outbuf[32768];
  369. unsigned long crc;
  370. if (size < 10)
  371. {
  372. error (0, 0, "gzipped data too small - lacks complete header");
  373. return 1;
  374. }
  375. if (buf[0] != GZIP_ID1 || buf[1] != GZIP_ID2)
  376. {
  377. error (0, 0, "gzipped data does not start with gzip identification");
  378. return 1;
  379. }
  380. if (buf[2] != GZIP_CDEFLATE)
  381. {
  382. error (0, 0, "only the deflate compression method is supported");
  383. return 1;
  384. }
  385. /* Skip over the fixed header, and then skip any of the variable-length
  386. fields. As we skip each field, we keep pos <= size. The checks
  387. on positions and lengths are really checks for malformed or
  388. incomplete gzip data. */
  389. pos = 10;
  390. if (buf[3] & GZIP_FEXTRA)
  391. {
  392. if (pos + 2 >= size)
  393. {
  394. error (0, 0, "%s lacks proper gzip XLEN field", fullname);
  395. return 1;
  396. }
  397. pos += buf[pos] + (buf[pos + 1] << 8) + 2;
  398. if (pos > size)
  399. {
  400. error (0, 0, "%s lacks proper gzip \"extra field\"", fullname);
  401. return 1;
  402. }
  403. }
  404. if (buf[3] & GZIP_FNAME)
  405. {
  406. unsigned char *p = memchr(buf + pos, '\0', size - pos);
  407. if (p == NULL)
  408. {
  409. error (0, 0, "%s has bad gzip filename field", fullname);
  410. return 1;
  411. }
  412. pos = p - buf + 1;
  413. }
  414. if (buf[3] & GZIP_FCOMMENT)
  415. {
  416. unsigned char *p = memchr(buf + pos, '\0', size - pos);
  417. if (p == NULL)
  418. {
  419. error (0, 0, "%s has bad gzip comment field", fullname);
  420. return 1;
  421. }
  422. pos = p - buf + 1;
  423. }
  424. if (buf[3] & GZIP_FHCRC)
  425. {
  426. pos += 2;
  427. if (pos > size)
  428. {
  429. error (0, 0, "%s has bad gzip CRC16 field", fullname);
  430. return 1;
  431. }
  432. }
  433. /* There could be no data to decompress - check and short circuit. */
  434. if (pos >= size)
  435. {
  436. error (0, 0, "gzip data incomplete for %s (no data)", fullname);
  437. return 1;
  438. }
  439. memset (&zstr, 0, sizeof zstr);
  440. /* Passing a negative argument tells zlib not to look for a zlib
  441. (RFC1950) header. This is an undocumented feature; I suppose if
  442. we wanted to be anal we could synthesize a header instead,
  443. but why bother? */
  444. zstatus = inflateInit2 (&zstr, -15);
  445. if (zstatus != Z_OK)
  446. compress_error (1, zstatus, &zstr, fullname);
  447. /* I don't see why we should have to include the 8 byte trailer in
  448. avail_in. But I see that zlib/gzio.c does, and it seemed to fix
  449. a fairly rare bug in which we'd get a Z_BUF_ERROR for no obvious
  450. reason. */
  451. zstr.avail_in = size - pos;
  452. zstr.next_in = buf + pos;
  453. crc = crc32 (0, NULL, 0);
  454. do
  455. {
  456. zstr.avail_out = sizeof (outbuf);
  457. zstr.next_out = outbuf;
  458. zstatus = inflate (&zstr, Z_NO_FLUSH);
  459. if (zstatus != Z_STREAM_END && zstatus != Z_OK)
  460. {
  461. compress_error (0, zstatus, &zstr, fullname);
  462. return 1;
  463. }
  464. if (write (fd, outbuf, sizeof (outbuf) - zstr.avail_out) < 0)
  465. {
  466. error (0, errno, "writing decompressed file %s", fullname);
  467. return 1;
  468. }
  469. crc = crc32 (crc, outbuf, sizeof (outbuf) - zstr.avail_out);
  470. } while (zstatus != Z_STREAM_END);
  471. zstatus = inflateEnd (&zstr);
  472. if (zstatus != Z_OK)
  473. compress_error (0, zstatus, &zstr, fullname);
  474. /* Check that there is still 8 trailer bytes remaining (CRC32
  475. and ISIZE). Check total decomp. data, plus header len (pos)
  476. against input buffer total size. */
  477. pos += zstr.total_in;
  478. if (size - pos != 8)
  479. {
  480. error (0, 0, "gzip data incomplete for %s (no trailer)", fullname);
  481. return 1;
  482. }
  483. if (crc != ((unsigned long)buf[pos]
  484. + ((unsigned long)buf[pos + 1] << 8)
  485. + ((unsigned long)buf[pos + 2] << 16)
  486. + ((unsigned long)buf[pos + 3] << 24)))
  487. {
  488. error (0, 0, "CRC error uncompressing %s", fullname);
  489. return 1;
  490. }
  491. if (zstr.total_out != ((unsigned long)buf[pos + 4]
  492. + ((unsigned long)buf[pos + 5] << 8)
  493. + ((unsigned long)buf[pos + 6] << 16)
  494. + ((unsigned long)buf[pos + 7] << 24)))
  495. {
  496. error (0, 0, "invalid length uncompressing %s", fullname);
  497. return 1;
  498. }
  499. return 0;
  500. }
  501. /* Read all of FD and put the gzipped data (RFC1952/RFC1951) into *BUF,
  502. replacing previous contents of *BUF. *BUF is xmalloc'd and *SIZE is
  503. its allocated size. Put the actual number of bytes of data in
  504. *LEN. If something goes wrong, give a nonfatal error mentioning
  505. FULLNAME as the name of the file for FD, and return 1 if we can't
  506. recover from it). LEVEL is the compression level (1-9). */
  507. int
  508. read_and_gzip (fd, fullname, buf, size, len, level)
  509. int fd;
  510. const char *fullname;
  511. unsigned char **buf;
  512. size_t *size;
  513. size_t *len;
  514. int level;
  515. {
  516. z_stream zstr;
  517. int zstatus;
  518. unsigned char inbuf[8192];
  519. int nread;
  520. unsigned long crc;
  521. if (*size < 1024)
  522. {
  523. unsigned char *newbuf;
  524. *size = 1024;
  525. newbuf = xrealloc (*buf, *size);
  526. if (newbuf == NULL)
  527. {
  528. error (0, 0, "out of memory");
  529. return 1;
  530. }
  531. *buf = newbuf;
  532. }
  533. (*buf)[0] = GZIP_ID1;
  534. (*buf)[1] = GZIP_ID2;
  535. (*buf)[2] = GZIP_CDEFLATE;
  536. (*buf)[3] = 0;
  537. (*buf)[4] = (*buf)[5] = (*buf)[6] = (*buf)[7] = 0;
  538. /* Could set this based on level, but why bother? */
  539. (*buf)[8] = 0;
  540. (*buf)[9] = 255;
  541. memset (&zstr, 0, sizeof zstr);
  542. zstatus = deflateInit2 (&zstr, level, Z_DEFLATED, -15, 8,
  543. Z_DEFAULT_STRATEGY);
  544. crc = crc32 (0, NULL, 0);
  545. if (zstatus != Z_OK)
  546. {
  547. compress_error (0, zstatus, &zstr, fullname);
  548. return 1;
  549. }
  550. /* Adjust for 10-byte output header (filled in above) */
  551. zstr.total_out = 10;
  552. zstr.avail_out = *size - 10;
  553. zstr.next_out = *buf + 10;
  554. while (1)
  555. {
  556. int finish = 0;
  557. nread = read (fd, inbuf, sizeof inbuf);
  558. if (nread < 0)
  559. {
  560. error (0, errno, "cannot read %s", fullname);
  561. return 1;
  562. }
  563. else if (nread == 0)
  564. /* End of file. */
  565. finish = 1;
  566. crc = crc32 (crc, inbuf, nread);
  567. zstr.next_in = inbuf;
  568. zstr.avail_in = nread;
  569. do
  570. {
  571. /* I don't see this documented anywhere, but deflate seems
  572. to tend to dump core sometimes if we pass it Z_FINISH and
  573. a small (e.g. 2147 byte) avail_out. So we insist on at
  574. least 4096 bytes (that is what zlib/gzio.c uses). */
  575. if (zstr.avail_out < 4096)
  576. {
  577. unsigned char *newbuf;
  578. assert(zstr.avail_out + zstr.total_out == *size);
  579. assert(zstr.next_out == *buf + zstr.total_out);
  580. *size *= 2;
  581. newbuf = xrealloc (*buf, *size);
  582. if (newbuf == NULL)
  583. {
  584. error (0, 0, "out of memory");
  585. return 1;
  586. }
  587. *buf = newbuf;
  588. zstr.next_out = *buf + zstr.total_out;
  589. zstr.avail_out = *size - zstr.total_out;
  590. assert(zstr.avail_out + zstr.total_out == *size);
  591. assert(zstr.next_out == *buf + zstr.total_out);
  592. }
  593. zstatus = deflate (&zstr, finish ? Z_FINISH : 0);
  594. if (zstatus == Z_STREAM_END)
  595. goto done;
  596. else if (zstatus != Z_OK)
  597. compress_error (0, zstatus, &zstr, fullname);
  598. } while (zstr.avail_out == 0);
  599. }
  600. done:
  601. /* Need to add the CRC information (8 bytes)
  602. to the end of the gzip'd output.
  603. Ensure there is enough space in the output buffer
  604. to do so. */
  605. if (zstr.avail_out < 8)
  606. {
  607. unsigned char *newbuf;
  608. assert(zstr.avail_out + zstr.total_out == *size);
  609. assert(zstr.next_out == *buf + zstr.total_out);
  610. *size += 8 - zstr.avail_out;
  611. newbuf = realloc (*buf, *size);
  612. if (newbuf == NULL)
  613. {
  614. error (0, 0, "out of memory");
  615. return 1;
  616. }
  617. *buf = newbuf;
  618. zstr.next_out = *buf + zstr.total_out;
  619. zstr.avail_out = *size - zstr.total_out;
  620. assert(zstr.avail_out + zstr.total_out == *size);
  621. assert(zstr.next_out == *buf + zstr.total_out);
  622. }
  623. *zstr.next_out++ = (unsigned char)(crc & 0xff);
  624. *zstr.next_out++ = (unsigned char)((crc >> 8) & 0xff);
  625. *zstr.next_out++ = (unsigned char)((crc >> 16) & 0xff);
  626. *zstr.next_out++ = (unsigned char)((crc >> 24) & 0xff);
  627. *zstr.next_out++ = (unsigned char)(zstr.total_in & 0xff);
  628. *zstr.next_out++ = (unsigned char)((zstr.total_in >> 8) & 0xff);
  629. *zstr.next_out++ = (unsigned char)((zstr.total_in >> 16) & 0xff);
  630. *zstr.next_out++ = (unsigned char)((zstr.total_in >> 24) & 0xff);
  631. zstr.total_out += 8;
  632. zstr.avail_out -= 8;
  633. assert(zstr.avail_out + zstr.total_out == *size);
  634. assert(zstr.next_out == *buf + zstr.total_out);
  635. *len = zstr.total_out;
  636. zstatus = deflateEnd (&zstr);
  637. if (zstatus != Z_OK)
  638. compress_error (0, zstatus, &zstr, fullname);
  639. return 0;
  640. }
  641. #endif /* defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT) */