+\f
+/* Null decompressor. */
+
+static int
+stored_read (struct zip_member *zm, void *buf, size_t n)
+{
+ size_t bytes_read = fread (buf, 1, n, zm->fp);
+ if (!bytes_read && !zm->error)
+ zm->error = get_stream_error (zm->fp, zm->file_name);
+ return bytes_read;
+}
+
+static char *
+stored_init (struct zip_member *zm UNUSED)
+{
+ return NULL;
+}
+
+static void
+stored_finish (struct zip_member *zm UNUSED)
+{
+ /* Nothing required */
+}
+
+static const struct decompressor stored_decompressor =
+ {stored_init, stored_read, stored_finish};
+\f
+/* Inflate decompressor. */
+
+#undef crc32
+#include <zlib.h>
+
+#define UCOMPSIZE 4096
+
+struct inflator
+{
+ z_stream zss;
+ int state;
+ unsigned char ucomp[UCOMPSIZE];
+ size_t bytes_uncomp;
+ size_t ucomp_bytes_read;
+
+ /* Two bitfields as defined by RFC1950 */
+ uint16_t cmf_flg ;
+};
+
+static void
+inflate_finish (struct zip_member *zm)
+{
+ struct inflator *inf = zm->aux;
+
+ inflateEnd (&inf->zss);
+
+ free (inf);
+}
+
+static char *
+inflate_init (struct zip_member *zm)
+{
+ int r;
+ struct inflator *inf = xzalloc (sizeof *inf);
+
+ uint16_t flg = 0 ;
+ uint16_t cmf = 0x8; /* Always 8 for inflate */
+
+ const uint16_t cinfo = 7; /* log_2(Window size) - 8 */
+
+ cmf |= cinfo << 4; /* Put cinfo into the high nibble */
+
+ /* make these into a 16 bit word */
+ inf->cmf_flg = (cmf << 8) | flg;
+
+ /* Set the check bits */
+ inf->cmf_flg += 31 - (inf->cmf_flg % 31);
+ assert (inf->cmf_flg % 31 == 0);
+
+ inf->zss.next_in = Z_NULL;
+ inf->zss.avail_in = 0;
+ inf->zss.zalloc = Z_NULL;
+ inf->zss.zfree = Z_NULL;
+ inf->zss.opaque = Z_NULL;
+ r = inflateInit (&inf->zss);
+
+ if (Z_OK != r)
+ return xasprintf (_("%s: cannot initialize inflator (%s)"),
+ zm->file_name, zError (r));
+
+ zm->aux = inf;
+
+ return NULL;
+}
+
+static int
+inflate_read (struct zip_member *zm, void *buf, size_t n)
+{
+ int r;
+ struct inflator *inf = zm->aux;
+
+ if (inf->zss.avail_in == 0)
+ {
+ int bytes_read;
+ int bytes_to_read;
+ int pad = 0;
+
+ if (inf->state == 0)
+ {
+ inf->ucomp[1] = inf->cmf_flg ;
+ inf->ucomp[0] = inf->cmf_flg >> 8 ;
+
+ pad = 2;
+ inf->state++;
+ }
+
+ bytes_to_read = zm->comp_size - inf->ucomp_bytes_read;
+
+ if (bytes_to_read == 0)
+ return 0;
+
+ if (bytes_to_read > UCOMPSIZE)
+ bytes_to_read = UCOMPSIZE;
+
+ bytes_read = fread (inf->ucomp + pad, 1, bytes_to_read - pad, zm->fp);
+ if (!bytes_read && !zm->error)
+ {
+ zm->error = get_stream_error (zm->fp, zm->file_name);
+ return -1;
+ }
+
+ inf->ucomp_bytes_read += bytes_read;
+
+ inf->zss.avail_in = bytes_read + pad;
+ inf->zss.next_in = inf->ucomp;
+ }
+ inf->zss.avail_out = n;
+ inf->zss.next_out = buf;
+
+ r = inflate (&inf->zss, Z_NO_FLUSH);
+ if (Z_OK == r)
+ {
+ return n - inf->zss.avail_out;
+ }
+
+ if (!zm->error)
+ zm->error = xasprintf (_("%s: error inflating \"%s\" (%s)"),
+ zm->file_name, zm->member_name, zError (r));
+
+ return -1;
+}
+
+static const struct decompressor inflate_decompressor =
+ {inflate_init, inflate_read, inflate_finish};