/*
  NrrdIO: C library for NRRD file IO (with optional compressions)
  Copyright (C) 2009--2026  University of Chicago
  Copyright (C) 2005--2008  Gordon Kindlmann
  Copyright (C) 1998--2004  University of Utah

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any
  damages arising from the use of this software.

  Permission is granted to anyone to use this software for any
  purpose, including commercial applications, and to alter it and
  redistribute it freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must
     not claim that you wrote the original software. If you use this
     software in a product, an acknowledgment in the product
     documentation would be appreciated but is not required.

  2. Altered source versions must be plainly marked as such, and must
     not be misrepresented as being the original software.

  3. This notice may not be removed or altered from any source distribution.
*/
/*
  This file is a modified version of the 'gzio.c' and 'zutil.h' source
  files from the zlib 1.1.4 distribution.

  zlib.h -- interface of the 'zlib' general purpose compression library
  version 1.1.4, March 11th, 2002

  Copyright (C) 1995-2002 Jean-loup Gailly and Mark Adler

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

  Jean-loup Gailly        Mark Adler
  jloup@gzip.org          madler@alumni.caltech.edu

  The data format used by the zlib library is described by RFCs (Request for
  Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
  (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
*/

#if TEEM_ZLIB

#  include "NrrdIO.h"
#  include "privateNrrd.h"

#  ifdef _WIN32 /* Window 95 & Windows NT */
#    define N_OS_CODE 0x0b
#  endif

#  if defined(MACOS) || defined(TARGET_OS_MAC) || defined(__APPLE_CC__)
#    define N_OS_CODE 0x07
#  endif

#  ifndef N_OS_CODE
#    define N_OS_CODE 0x03 /* assume Unix */
#  endif

/* default memLevel */
#  if MAX_MEM_LEVEL >= 8
#    define N_DEF_MEM_LEVEL 8
#  else
#    define N_DEF_MEM_LEVEL MAX_MEM_LEVEL
#  endif

/* stream buffer size */
#  define N_Z_BUFSIZE 16 * 1024

/* gzip flag byte */
#  define N_ASCII_FLAG  0x01 /* bit 0 set: file probably ascii text */
#  define N_HEAD_CRC    0x02 /* bit 1 set: header CRC present */
#  define N_EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
#  define N_ORIG_NAME   0x08 /* bit 3 set: original file name present */
#  define N_COMMENT     0x10 /* bit 4 set: file comment present */
#  define N_RESERVED    0xE0 /* bits 5..7: reserved */

typedef struct GzStream_t {
  z_stream stream;
  int z_err;       /* error code for last stream operation */
  int z_eof;       /* set if end of input file */
  FILE *file;      /* .gz file */
  Byte *inbuf;     /* input buffer */
  Byte *outbuf;    /* output buffer */
  uLong crc;       /* crc32 of uncompressed data */
  char *msg;       /* error message */
  int transparent; /* 1 if input file is not a .gz file */
  char mode;       /* 'w' or 'r' */
  long startpos;   /* start of compressed data in file (header skipped) */
} GzStream_t;

static int gzMagic[2] = {0x1f, 0x8b}; /* gzip magic header */

/* zlib error messages */
static const char *gzErrMsg[10] = {"need dictionary",      /* Z_NEED_DICT       2  */
                                   "stream end",           /* Z_STREAM_END      1  */
                                   "",                     /* Z_OK              0  */
                                   "file error",           /* Z_ERRNO         (-1) */
                                   "stream error",         /* Z_STREAM_ERROR  (-2) */
                                   "data error",           /* Z_DATA_ERROR    (-3) */
                                   "insufficient memory",  /* Z_MEM_ERROR     (-4) */
                                   "buffer error",         /* Z_BUF_ERROR     (-5) */
                                   "incompatible version", /* Z_VERSION_ERROR (-6) */
                                   ""};

#  define N_GZ_ERR_MSG(err) gzErrMsg[Z_NEED_DICT - (err)]

/* some forward declarations for things in this file */
static void gzCheckHeader(GzStream_t *s);
static int gzDestroy(GzStream_t *s);
static int gzDoFlush(gzFile file, int flush);
static void gzPutLong(FILE *file, uLong x);
static uLong gzGetLong(GzStream_t *s);

/*
** nrrd__GzOpen()
**
** Opens a gzip (.gz) file for reading or writing. The mode parameter
** is like in fopen ("rb" or "wb"). The file represented by the FILE* pointer
** should be open already with the same mode.  The mode parameter can also be
** used to specify the compression level "[0-9]" and strategy "[f|h]".
**
** The compression level must be between 0 and 9: 1 gives best speed,
** 9 gives best compression, 0 gives no compression at all (the input data
** is simply copied a block at a time). The default level is 6.
**
** The strategy parameter is used to tune the compression algorithm. Use
** "f" for data produced by a filter (or predictor), or "h" to force Huffman
** encoding only (no string match). Filtered data consists mostly of small
** values with a somewhat random distribution. In this case, the compression
** algorithm is tuned to compress them better. The effect of "f" is to force
** more Huffman coding and less string matching; it is somewhat intermediate
** between the default and Huffman. The strategy parameter only affects the
** compression ratio but not the correctness of the compressed output even
** if it is not set appropriately.
**
** The complete syntax for the mode parameter is: "(r|w[a])[0-9][f|h]".
**
** Returns Z_NULL if the file could not be opened or if there was
** insufficient memory to allocate the (de)compression state; errno
** can be checked to distinguish the two cases (if errno is zero, the
** zlib error is Z_MEM_ERROR).
*/
gzFile /* Biff: (private) (gzFile)Z_NULL */
nrrd__GzOpen(FILE *fd, const char *mode) {
  static const char me[] = "nrrd__GzOpen";
  int error;
  int level = Z_DEFAULT_COMPRESSION; /* compression level */
  int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */
  const char *p = mode;
  GzStream_t *s;
  char fmode[AIR_STRLEN_MED + 1]; /* copy of mode, without the compression level */
  char *m = fmode;

  if (!mode) {
    biffAddf(NRRD, "%s: no file mode specified", me);
    return (gzFile)Z_NULL;
  }
  /* allocate stream struct */
  s = (GzStream_t *)calloc(1, sizeof(GzStream_t));
  if (!s) {
    biffAddf(NRRD, "%s: failed to allocate stream buffer", me);
    return (gzFile)Z_NULL;
  }
  /* initialize stream struct */
  s->stream.zalloc = (alloc_func)0;
  s->stream.zfree = (free_func)0;
  s->stream.opaque = (voidpf)0;
  s->stream.next_in = s->inbuf = Z_NULL;
  s->stream.next_out = s->outbuf = Z_NULL;
  s->stream.avail_in = s->stream.avail_out = 0;
  s->file = NULL;
  s->z_err = Z_OK;
  s->z_eof = 0;
  s->crc = crc32(0L, Z_NULL, 0);
  s->msg = NULL;
  s->transparent = 0;
  /* parse mode flag */
  s->mode = '\0';
  do {
    if (*p == 'r') s->mode = 'r';
    if (*p == 'w' || *p == 'a') s->mode = 'w';
    if (*p >= '0' && *p <= '9') {
      level = *p - '0';
    } else if (*p == 'f') {
      strategy = Z_FILTERED;
    } else if (*p == 'h') {
      strategy = Z_HUFFMAN_ONLY;
    } else {
      *m++ = *p; /* copy the mode */
    }
  } while (*p++ && m != fmode + sizeof(fmode));
  if (s->mode == '\0') {
    biffAddf(NRRD, "%s: invalid file mode", me);
    gzDestroy(s);
    return (gzFile)Z_NULL;
  }

  if (s->mode == 'w') {
    error = deflateInit2(&(s->stream), level, Z_DEFLATED, -MAX_WBITS, N_DEF_MEM_LEVEL,
                         strategy);
    /* windowBits is passed < 0 to suppress zlib header */

    s->stream.next_out = s->outbuf = (Byte *)calloc(1, N_Z_BUFSIZE);
    if (error != Z_OK || s->outbuf == Z_NULL) {
      biffAddf(NRRD, "%s: stream init failed", me);
      gzDestroy(s);
      return (gzFile)Z_NULL;
    }
  } else {
    s->stream.next_in = s->inbuf = (Byte *)calloc(1, N_Z_BUFSIZE);

    error = inflateInit2(&(s->stream), -MAX_WBITS);
    /* windowBits is passed < 0 to tell that there is no zlib header.
     * Note that in this case inflate *requires* an extra "dummy" byte
     * after the compressed stream in order to complete decompression and
     * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
     * present after the compressed stream.
     */
    if (error != Z_OK || s->inbuf == Z_NULL) {
      biffAddf(NRRD, "%s: stream init failed", me);
      gzDestroy(s);
      return (gzFile)Z_NULL;
    }
  }
  s->stream.avail_out = N_Z_BUFSIZE;
  errno = 0;
  s->file = fd;
  if (s->file == NULL) {
    biffAddf(NRRD, "%s: null file pointer", me);
    gzDestroy(s);
    return (gzFile)Z_NULL;
  }
  if (s->mode == 'w') {
    /* Write a very simple .gz header: */
    fprintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gzMagic[0], gzMagic[1], Z_DEFLATED,
            0 /*flags*/, 0, 0, 0, 0 /*time*/, 0 /*xflags*/, N_OS_CODE);
    s->startpos = 10L;
    /* We use 10L instead of ftell(s->file) to because ftell causes an
     * fflush on some systems. This version of the library doesn't use
     * startpos anyway in write mode, so this initialization is not
     * necessary.
     */
  } else {
    gzCheckHeader(s); /* skip the .gz header */
    s->startpos = (ftell(s->file) - s->stream.avail_in);
  }
  return (gzFile)s;
}

/*
** nrrd__GzClose()
**
** Flushes all pending output if necessary, closes the compressed file
** and deallocates the (de)compression state.
*/
int /* Biff: (private) 1 */
nrrd__GzClose(gzFile file) {
  static const char me[] = "nrrd__GzClose";
  int error;
  GzStream_t *s = (GzStream_t *)file;

  if (s == NULL) {
    biffAddf(NRRD, "%s: invalid stream", me);
    return 1;
  }
  if (s->mode == 'w') {
    error = gzDoFlush(file, Z_FINISH);
    if (error != Z_OK) {
      biffAddf(NRRD, "%s: failed to flush pending data", me);
      gzDestroy((GzStream_t *)file);
      return 1; /* gzDestroy's return does not communicate our known error */
    }
    gzPutLong(s->file, s->crc);
    gzPutLong(s->file, s->stream.total_in);
  }
  if (gzDestroy((GzStream_t *)file)) {
    biffAddf(NRRD, "%s: failed to clean up", me);
    return 1;
  }
  return 0;
}

/*
** nrrd__GzRead()
**
** Reads the given number of uncompressed bytes from the compressed file.
** Returns the number of bytes actually read (0 for end of file).
*/
int /* Biff: (private) 1 */
nrrd__GzRead(gzFile file, void *buf, unsigned int len, unsigned int *didread) {
  static const char me[] = "nrrd__GzRead";
  GzStream_t *s = (GzStream_t *)file;
  Bytef *start = (Bytef *)buf; /* starting point for crc computation */
  Byte *next_out;              /* == stream.next_out but not forced far (for MSDOS) */

  if (s == NULL || s->mode != 'r') {
    biffAddf(NRRD, "%s: invalid stream or file mode", me);
    *didread = 0;
    return 1;
  }

  if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) {
    biffAddf(NRRD, "%s: data read error", me);
    *didread = 0;
    return 1;
  }

  if (s->z_err == Z_STREAM_END) {
    *didread = 0;
    return 0; /* EOF */
  }

  next_out = (Byte *)buf;
  s->stream.next_out = (Bytef *)buf;
  s->stream.avail_out = len;

  while (s->stream.avail_out != 0) {

    if (s->transparent) {
      /* Copy first the lookahead bytes: */
      uInt n = s->stream.avail_in;
      if (n > s->stream.avail_out) n = s->stream.avail_out;
      if (n > 0) {
        memcpy(s->stream.next_out, s->stream.next_in, n);
        next_out += n;
        s->stream.next_out = next_out;
        s->stream.next_in += n;
        s->stream.avail_out -= n;
        s->stream.avail_in -= n;
      }
      if (s->stream.avail_out > 0) {
        s->stream.avail_out -= (uInt)fread(next_out, 1, s->stream.avail_out, s->file);
      }
      len -= s->stream.avail_out;
      s->stream.total_in += len;
      s->stream.total_out += len;
      if (len == 0) s->z_eof = 1;
      *didread = len;
      return 0;
    }
    if (s->stream.avail_in == 0 && !s->z_eof) {

      errno = 0;
      s->stream.avail_in = (uInt)fread(s->inbuf, 1, N_Z_BUFSIZE, s->file);
      if (s->stream.avail_in == 0) {
        s->z_eof = 1;
        if (ferror(s->file)) {
          s->z_err = Z_ERRNO;
          break;
        }
      }
      s->stream.next_in = s->inbuf;
    }
    s->z_err = inflate(&(s->stream), Z_NO_FLUSH);

    if (s->z_err == Z_STREAM_END) {
      /* Check CRC and original size */
      s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
      start = s->stream.next_out;

      if (gzGetLong(s) != s->crc) {
        s->z_err = Z_DATA_ERROR;
      } else {
        (void)gzGetLong(s);
        /* The uncompressed length returned by above getlong() may
         * be different from s->stream.total_out) in case of
         * concatenated .gz files. Check for such files:
         */
        gzCheckHeader(s);
        if (s->z_err == Z_OK) {
          uLong total_in = s->stream.total_in;
          uLong total_out = s->stream.total_out;

          inflateReset(&(s->stream));
          s->stream.total_in = total_in;
          s->stream.total_out = total_out;
          s->crc = crc32(0L, Z_NULL, 0);
        }
      }
    }
    if (s->z_err != Z_OK || s->z_eof) break;
  }
  s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));

  *didread = len - s->stream.avail_out;
  return 0;
}

/*
** nrrd__GzWrite()
**
** Writes the given number of uncompressed bytes into the compressed file.
** Returns the number of bytes actually written (0 in case of error).
*/
int /* Biff: (private) 1 */
nrrd__GzWrite(gzFile file, const void *buf, unsigned int len, unsigned int *written) {
  static const char me[] = "nrrd__GzWrite";
  GzStream_t *s = (GzStream_t *)file;
  void *nonconstbuf;

  if (s == NULL || s->mode != 'w') {
    biffAddf(NRRD, "%s: invalid stream or file mode", me);
    *written = 0;
    return 1;
  }

  /* If you google for "const correct zlib" or "zlib.h is not
     const-correct" you'll find zlib mailing list discussions of how
     zlib doesn't have all the consts that it should, and various code
     examples of using multiple casts to hide the problem. Here's a
     slow way that doesn't use mere casting to make the const go away */
  memcpy(&nonconstbuf, &buf, sizeof(void *));
  s->stream.next_in = (Bytef *)nonconstbuf;
  s->stream.avail_in = len;

  while (s->stream.avail_in != 0) {
    if (s->stream.avail_out == 0) {
      s->stream.next_out = s->outbuf;
      if (fwrite(s->outbuf, 1, N_Z_BUFSIZE, s->file) != N_Z_BUFSIZE) {
        s->z_err = Z_ERRNO;
        biffAddf(NRRD, "%s: failed to fwrite to file", me);
        /* earlier code had a "break" here instead of return, which seemed to
        permit the function returning 0 instead of 1; so doing the return here
        and not trying to record any additional state in s */
        return 1;
      }
      s->stream.avail_out = N_Z_BUFSIZE;
    }
    s->z_err = deflate(&(s->stream), Z_NO_FLUSH);
    if (s->z_err != Z_OK) {
      /* earlier code had a mere "break" here, which definitely meant that
      the error was not reflected in the final return (always returned 0),
      so adding info to biff (and also not trying to record anything in s)
      */
      biffAddf(NRRD, "%s: s->z_err (%d) != Z_OK (%d)", me, s->z_err, Z_OK);
      return 1;
    }
  }
  s->crc = crc32(s->crc, (const Bytef *)buf, len);

  *written = len - s->stream.avail_in;
  return 0;
}

/*
** gzGetByte()
**
** Reads a byte from a GzStream_t. Updates next_in and avail_in.
** Returns EOF for end of file.
** IN assertion: the stream s has been sucessfully opened for reading.
*/
static int /* Biff: EOF */
gzGetByte(GzStream_t *s) {
  static const char me[] = "gzGetByte";

  if (s->z_eof) return EOF;
  if (s->stream.avail_in == 0) {
    errno = 0;
    s->stream.avail_in = (uInt)fread(s->inbuf, 1, N_Z_BUFSIZE, s->file);
    if (s->stream.avail_in == 0) {
      s->z_eof = 1;
      if (ferror(s->file)) {
        biffAddf(NRRD, "%s: failed to read from file", me);
        s->z_err = Z_ERRNO;
      }
      return EOF;
    }
    s->stream.next_in = s->inbuf;
  }
  s->stream.avail_in--;
  return *(s->stream.next_in)++;
}

/*
******** gzCheckHeader()
**
** Checks the gzip header of a GzStream_t opened for reading. Sets
** the stream mode to transparent if the gzip magic header is not
** present; sets s->err to Z_DATA_ERROR if the magic header is present
** but the rest of the header is incorrect.
** IN assertion: the stream s has already been created sucessfully;
** s->stream.avail_in is zero for the first time, but may be non-zero
** for concatenated .gz files.
*/
static void
gzCheckHeader(GzStream_t *s) {
  /* static const char me[] = "gzCheckHeader"; */
  int method; /* method byte */
  int flags;  /* flags byte */
  uInt len;
  int c;

  /* Check the gzip magic header */
  for (len = 0; len < 2; len++) {
    c = gzGetByte(s);
    if (c != gzMagic[len]) {
      if (len != 0) s->stream.avail_in++, s->stream.next_in--;
      if (c != EOF) {
        s->stream.avail_in++, s->stream.next_in--;
        s->transparent = 1;
      }
      s->z_err = s->stream.avail_in != 0 ? Z_OK : Z_STREAM_END;
      return;
    }
  }
  method = gzGetByte(s);
  flags = gzGetByte(s);
  if (method != Z_DEFLATED || (flags & N_RESERVED) != 0) {
    /* (GLK) this (long-standing) biff usage is not acted on by any callers */
    /* biffAddf(NRRD, "%s: gzip compression method is not deflate", me); */
    s->z_err = Z_DATA_ERROR;
    return;
  }

  /* Discard time, xflags and OS code: */
  for (len = 0; len < 6; len++)
    (void)gzGetByte(s);

  if ((flags & N_EXTRA_FIELD) != 0) { /* skip the extra field */
    len = (uInt)gzGetByte(s);
    len += ((uInt)gzGetByte(s)) << 8;
    /* len is garbage if EOF but the loop below will quit anyway */
    while (len-- != 0 && gzGetByte(s) != EOF)
      ;
  }
  if ((flags & N_ORIG_NAME) != 0) { /* skip the original file name */
    while ((c = gzGetByte(s)) != 0 && c != EOF)
      ;
  }
  if ((flags & N_COMMENT) != 0) { /* skip the .gz file comment */
    while ((c = gzGetByte(s)) != 0 && c != EOF)
      ;
  }
  if ((flags & N_HEAD_CRC) != 0) { /* skip the header crc */
    for (len = 0; len < 2; len++)
      (void)gzGetByte(s);
  }
  s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
  return;
}

/*
** gzDestroy()
**
** Cleans up then free the given GzStream_t. Returns a zlib error code.
** Try freeing in the reverse order of allocations.  FILE* s->file is not
** closed.  Because we didn't allocate it, we shouldn't delete it.
**
** Returns 1 (not an zlib error code) in case of error
*/
static int /* Biff: 1 */
gzDestroy(GzStream_t *s) {
  static const char me[] = "gzDestroy";
  int error = Z_OK;

  if (s == NULL) {
    biffAddf(NRRD, "%s: invalid stream", me);
    return 1;
  }
  s->msg = (char *)airFree(s->msg);
  if (s->stream.state != NULL) {
    if (s->mode == 'w') {
      error = deflateEnd(&(s->stream));
    } else if (s->mode == 'r') {
      error = inflateEnd(&(s->stream));
    }
  }
  if (error != Z_OK) {
    biffAddf(NRRD, "%s: %s (%d)", me, N_GZ_ERR_MSG(error), error);
    return 1;
  }
  if (s->z_err < 0) error = s->z_err; /* GLK is curious why */
  if (error != Z_OK) {
    biffAddf(NRRD, "%s: %s (%d)", me, N_GZ_ERR_MSG(error), error);
    return 1;
  }
  s->inbuf = (Byte *)airFree(s->inbuf);
  s->outbuf = (Byte *)airFree(s->outbuf);
  airFree(s);
  return 0;
}

/*
** gzDoFlush()
**
** Flushes all pending output into the compressed file. The parameter
** flush is the same as in the deflate() function.
*/
static int /* Biff: Z_STREAM_ERROR */
gzDoFlush(gzFile file, int flush) {
  static const char me[] = "gzDoFlush";
  uInt len;
  int done = 0;
  GzStream_t *s = (GzStream_t *)file;

  if (s == NULL || s->mode != 'w') {
    biffAddf(NRRD, "%s: invalid stream or file mode", me);
    return Z_STREAM_ERROR;
  }

  s->stream.avail_in = 0; /* should be zero already anyway */

  for (;;) {
    len = N_Z_BUFSIZE - s->stream.avail_out;

    if (len != 0) {
      if ((uInt)fwrite(s->outbuf, 1, len, s->file) != len) {
        s->z_err = Z_ERRNO;
        return Z_ERRNO;
      }
      s->stream.next_out = s->outbuf;
      s->stream.avail_out = N_Z_BUFSIZE;
    }
    if (done) break;
    s->z_err = deflate(&(s->stream), flush);

    /* Ignore the second of two consecutive flushes: */
    if (len == 0 && s->z_err == Z_BUF_ERROR) s->z_err = Z_OK;

    /* deflate has finished flushing only when it hasn't used up
     * all the available space in the output buffer:
     */
    done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END);

    if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break;
  }
  return s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
}

/*
** gzPutLong()
**
** Outputs a long in LSB order to the given file.
*/
static void
gzPutLong(FILE *file, uLong x) {
  int n;
  for (n = 0; n < 4; n++) {
    fputc((int)(x & 0xff), file);
    x >>= 8;
  }
}

/*
** gzGetLong()
**
** Reads a long in LSB order from the given GzStream_t.
** Sets z_err in case of error.
*/
static uLong
gzGetLong(GzStream_t *s) {
  uLong x = (uLong)gzGetByte(s);
  int c;

  x += ((uLong)gzGetByte(s)) << 8;
  x += ((uLong)gzGetByte(s)) << 16;
  c = gzGetByte(s);
  if (c == EOF) s->z_err = Z_DATA_ERROR;
  x += ((uLong)c) << 24;
  return x;
}

#endif /* TEEM_ZLIB */

/*
** random symbol to have in object file, even when Zlib not enabled
*/
int /* Biff: (private) nope */
nrrd__GzDummySymbol(void) {
  return 42;
}
