Пример #1
0
/*
** _nrrdGzGetLong()
**
** Reads a long in LSB order from the given _NrrdGzStream.
** Sets z_err in case of error.
*/
static uLong
_nrrdGzGetLong(_NrrdGzStream *s) {
  uLong x = (uLong)_nrrdGzGetByte(s);
  int c;

  x += ((uLong)_nrrdGzGetByte(s))<<8;
  x += ((uLong)_nrrdGzGetByte(s))<<16;
  c = _nrrdGzGetByte(s);
  if (c == EOF) s->z_err = Z_DATA_ERROR;
  x += ((uLong)c)<<24;
  return x;
}
Пример #2
0
/*
******** _nrrdGzCheckHeader()
**
** Checks the gzip header of a _NrrdGzStream 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
_nrrdGzCheckHeader(_NrrdGzStream *s) {
  char me[]="_nrrdGzCheckHeader", err[BIFF_STRLEN];
  int method; /* method byte */
  int flags;  /* flags byte */
  uInt len;
  int c;

  /* Check the gzip magic header */
  for (len = 0; len < 2; len++) {
    c = _nrrdGzGetByte(s);
    if (c != _nrrdGzMagic[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 = _nrrdGzGetByte(s);
  flags = _nrrdGzGetByte(s);
  if (method != Z_DEFLATED || (flags & _NRRD_RESERVED) != 0) {
    sprintf(err, "%s: gzip compression method is not deflate", me);
    biffAdd(NRRD, err);
    s->z_err = Z_DATA_ERROR;
    return;
  }

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

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