/* ** nio->byteSkip < 0 functionality contributed by Katharina Quintus */ static int _nrrdEncodingGzip_read(FILE *file, void *_data, size_t elNum, Nrrd *nrrd, NrrdIoState *nio) { static const char me[]="_nrrdEncodingGzip_read"; #if TEEM_ZLIB size_t sizeData, sizeRed; int error; long int bi; unsigned int didread, sizeChunk, maxChunk; char *data; gzFile gzfin; airPtrPtrUnion appu; sizeData = nrrdElementSize(nrrd)*elNum; /* Create the gzFile for reading in the gzipped data. */ if ((gzfin = _nrrdGzOpen(file, "rb")) == Z_NULL) { /* there was a problem */ biffAddf(NRRD, "%s: error opening gzFile", me); return 1; } /* keeps track of how many bytes have been successfully read in */ sizeRed = 0; /* zlib can only handle data sizes up to UINT_MAX ==> if there's more than UINT_MAX bytes to read in, we read in in chunks. However, we wrap a value _nrrdZlibMaxChunk around UINT_MAX for testing purposes. Given how sizeChunk is used below, we also cap chunk size at _nrrdZlibMaxChunk/2 to prevent overflow. */ maxChunk = _nrrdZlibMaxChunk/2; sizeChunk = AIR_CAST(unsigned int, AIR_MIN(sizeData, maxChunk)); if (nio->byteSkip < 0) { /* We don't know the size of the size to skip before the data, so decompress the data first into a temporary memory buffer. Then the byteskipping is then just memcpy-ing the appropriate region of memory from "buff" into the given "_data" pointer */ char *buff; airArray *buffArr; long backwards; /* setting the airArray increment to twice the chunk size means that for headers that are small compared to the data, the airArray never actually has to reallocate. The unit is 1 because we are managing the reading in terms of bytes (sizeof(char)==1 by definition) */ buff = NULL; appu.c = &buff; buffArr = airArrayNew(appu.v, NULL, 1, 2*sizeChunk); airArrayLenSet(buffArr, sizeChunk); if (!( buffArr && buffArr->data )) { biffAddf(NRRD, "%s: couldn't initialize airArray\n", me); return 1; } /* we keep reading in chunks as long as there hasn't been an error, and we haven't hit EOF (EOF signified by read == 0). Unlike the code below (for positive byteskip), we are obligated to read until the bitter end, and can't update sizeChunk to encompass only the required data. */ while (!(error = _nrrdGzRead(gzfin, buff + sizeRed, sizeChunk, &didread)) && didread > 0) { sizeRed += didread; if (didread >= sizeChunk) { /* we were able to read as much data as we requested, maybe there is more, so we need to make our temp buffer bigger */ unsigned int newlen = buffArr->len + sizeChunk; if (newlen < buffArr->len) { biffAddf(NRRD, "%s: array size will exceed uint capacity", me); return 1; } airArrayLenSet(buffArr, newlen); if (!buffArr->data) { biffAddf(NRRD, "%s: couldn't re-allocate data buffer", me); return 1; } } } if (error) { biffAddf(NRRD, "%s: error reading from gzFile", me); return 1; } /* backwards is (positive) number of bytes AFTER data that we ignore */ backwards = -nio->byteSkip - 1; if (sizeRed < sizeData + AIR_CAST(size_t, backwards)) { char stmp1[AIR_STRLEN_SMALL], stmp2[AIR_STRLEN_SMALL]; biffAddf(NRRD, "%s: expected %s bytes but received only %s", me, airSprintSize_t(stmp1, sizeData + AIR_CAST(size_t, backwards)), airSprintSize_t(stmp2, sizeRed)); return 1; } /* also handles nio->byteSkip == -N-1 signifying extra N bytes at end */ memcpy(_data, buff + sizeRed - sizeData - backwards, sizeData); airArrayNuke(buffArr); } else { /* no negative byteskip: after byteskipping, we can read directly into given data buffer */ if (nio->byteSkip > 0) { for (bi=0; bi<nio->byteSkip; bi++) { unsigned char b; /* Check to see if a single byte was able to be read. */ if (_nrrdGzRead(gzfin, &b, 1, &didread) != 0 || didread != 1) { biffAddf(NRRD, "%s: hit an error skipping byte %ld of %ld", me, bi, nio->byteSkip); return 1; } } } /* Pointer to chunks as we read them. */ data = AIR_CAST(char *, _data); while (!(error = _nrrdGzRead(gzfin, data, sizeChunk, &didread)) && didread > 0) { /* Increment the data pointer to the next available chunk. */ data += didread; sizeRed += didread; /* We only want to read as much data as we need, so we need to check to make sure that we don't request data that might be there but that we don't want. This will reduce sizeChunk when we get to the last block (which may be smaller than the original sizeChunk). */ if (sizeData >= sizeRed && sizeData - sizeRed < sizeChunk) { sizeChunk = AIR_CAST(unsigned int, sizeData - sizeRed); } } if (error) { biffAddf(NRRD, "%s: error reading from gzFile", me); return 1; } /* Check to see if we got out as much as we thought we should. */ if (sizeRed != sizeData) { char stmp1[AIR_STRLEN_SMALL], stmp2[AIR_STRLEN_SMALL]; biffAddf(NRRD, "%s: expected %s bytes but received %s", me, airSprintSize_t(stmp1, sizeData), airSprintSize_t(stmp2, sizeRed)); return 1; } }
int _nrrdEncodingGzip_write(FILE *file, const void *_data, size_t elNum, const Nrrd *nrrd, NrrdIoState *nio) { char me[]="_nrrdEncodingGzip_write", err[BIFF_STRLEN]; #if TEEM_ZLIB size_t bsize, total_written, block_size; int fmt_i=0, error; char *data, fmt[4]; gzFile gzfout; unsigned int wrote; bsize = nrrdElementSize(nrrd)*elNum; /* Set format string based on the NrrdIoState parameters. */ fmt[fmt_i++] = 'w'; if (0 <= nio->zlibLevel && nio->zlibLevel <= 9) fmt[fmt_i++] = '0' + nio->zlibLevel; switch (nio->zlibStrategy) { case nrrdZlibStrategyHuffman: fmt[fmt_i++] = 'h'; break; case nrrdZlibStrategyFiltered: fmt[fmt_i++] = 'f'; break; case nrrdZlibStrategyDefault: default: break; } fmt[fmt_i] = 0; /* Create the gzFile for writing in the gzipped data. */ if ((gzfout = _nrrdGzOpen(file, fmt)) == Z_NULL) { /* there was a problem */ sprintf(err, "%s: error opening gzFile", me); biffAdd(NRRD, err); return 1; } /* zlib can handle data sizes up to UINT_MAX, so we can't just pass in the bsize, because it might be too large for an unsigned int. Therefore it must be read in chunks if the bsize is larger than UINT_MAX. */ if (bsize <= UINT_MAX) { block_size = bsize; } else { block_size = UINT_MAX; } /* This counter will help us to make sure that we write as much data as we think we should. */ total_written = 0; /* Pointer to the blocks as we write them. */ data = (char *)_data; /* Ok, now we can begin writing. */ while ((error = _nrrdGzWrite(gzfout, data, block_size, &wrote)) == 0 && wrote > 0) { /* Increment the data pointer to the next available spot. */ data += wrote; total_written += wrote; /* We only want to write as much data as we need, so we need to check to make sure that we don't write more data than is there. This will reduce block_size when we get to the last block (which may be smaller than block_size). */ if (bsize >= total_written && (unsigned int)(bsize - total_written) < block_size) block_size = bsize - total_written; } /* Check if we stopped because of an error. */ if (error != 0) { sprintf(err, "%s: error reading from gzFile", me); biffAdd(NRRD, err); return 1; } /* Close the gzFile. Since _nrrdGzClose does not close the FILE* we will not encounter problems when dataFile is closed later. */ if (_nrrdGzClose(gzfout) != 0) { sprintf(err, "%s: error closing gzFile", me); biffAdd(NRRD, err); return 1; } /* Check to see if we got out as much as we thought we should. */ if (total_written != bsize) { sprintf(err, "%s: expected to write " _AIR_SIZE_T_CNV " bytes, but only " "wrote " _AIR_SIZE_T_CNV, me, bsize, total_written); biffAdd(NRRD, err); return 1; } return 0; #else AIR_UNUSED(file); AIR_UNUSED(_data); AIR_UNUSED(elNum); AIR_UNUSED(nrrd); AIR_UNUSED(nio); sprintf(err, "%s: sorry, this nrrd not compiled with zlib " "(needed for gzip) enabled", me); biffAdd(NRRD, err); return 1; #endif }
int _nrrdEncodingGzip_read(FILE *file, void *_data, size_t elNum, Nrrd *nrrd, NrrdIoState *nio) { char me[]="_nrrdEncodingGzip_read", err[BIFF_STRLEN]; #if TEEM_ZLIB size_t bsize, total_read, block_size; int i, error; unsigned int read; char *data; gzFile gzfin; bsize = nrrdElementSize(nrrd)*elNum; /* Create the gzFile for reading in the gzipped data. */ if ((gzfin = _nrrdGzOpen(file, "rb")) == Z_NULL) { /* there was a problem */ sprintf(err, "%s: error opening gzFile", me); biffAdd(NRRD, err); return 1; } /* Here is where we do the byte skipping. */ for(i = 0; i < nio->byteSkip; i++) { unsigned char b; /* Check to see if a single byte was able to be read. */ if (_nrrdGzRead(gzfin, &b, 1, &read) != 0 || read != 1) { sprintf(err, "%s: hit an error skipping byte %d of %d", me, i, nio->byteSkip); biffAdd(NRRD, err); return 1; } } /* zlib can handle data sizes up to UINT_MAX, so we can't just pass in the size, because it might be too large for an unsigned int. Therefore it must be read in chunks if the size is larger than UINT_MAX. */ if (bsize <= UINT_MAX) { block_size = bsize; } else { block_size = UINT_MAX; } /* This counter will help us to make sure that we read as much data as we think we should. */ total_read = 0; /* Pointer to the blocks as we read them. */ data = (char *)_data; /* Ok, now we can begin reading. */ while ((error = _nrrdGzRead(gzfin, data, block_size, &read)) == 0 && read > 0) { /* Increment the data pointer to the next available spot. */ data += read; total_read += read; /* We only want to read as much data as we need, so we need to check to make sure that we don't request data that might be there but that we don't want. This will reduce block_size when we get to the last block (which may be smaller than block_size). */ if (bsize >= total_read && bsize - total_read < block_size) { block_size = bsize - total_read; } } /* Check if we stopped because of an error. */ if (error != 0) { sprintf(err, "%s: error reading from gzFile", me); biffAdd(NRRD, err); return 1; } /* Close the gzFile. Since _nrrdGzClose does not close the FILE* we will not encounter problems when dataFile is closed later. */ if (_nrrdGzClose(gzfin) != 0) { sprintf(err, "%s: error closing gzFile", me); biffAdd(NRRD, err); return 1; } /* Check to see if we got out as much as we thought we should. */ if (total_read != bsize) { sprintf(err, "%s: expected " _AIR_SIZE_T_CNV " bytes and received " _AIR_SIZE_T_CNV " bytes", me, bsize, total_read); biffAdd(NRRD, err); return 1; } return 0; #else AIR_UNUSED(file); AIR_UNUSED(_data); AIR_UNUSED(elNum); AIR_UNUSED(nrrd); AIR_UNUSED(nio); sprintf(err, "%s: sorry, this nrrd not compiled with gzip enabled", me); biffAdd(NRRD, err); return 1; #endif }
/* ** nio->byteSkip < 0 functionality contributed by Katharina Quintus */ int _nrrdEncodingGzip_read(FILE *file, void *_data, size_t elNum, Nrrd *nrrd, NrrdIoState *nio) { char me[]="_nrrdEncodingGzip_read", err[BIFF_STRLEN]; #if TEEM_ZLIB size_t sizeData, sizeRed, sizeChunk; int error; long int bi; unsigned int read; char *data; gzFile gzfin; ptrHack hack; sizeData = nrrdElementSize(nrrd)*elNum; /* Create the gzFile for reading in the gzipped data. */ if ((gzfin = _nrrdGzOpen(file, "rb")) == Z_NULL) { /* there was a problem */ sprintf(err, "%s: error opening gzFile", me); biffAdd(NRRD, err); return 1; } /* keeps track of how many bytes have been successfully read in */ sizeRed = 0; /* zlib can only handle data sizes up to UINT_MAX ==> if there's more than UINT_MAX bytes to read in, we read in in chunks */ sizeChunk = AIR_MIN(sizeData, UINT_MAX); if (nio->byteSkip < 0) { /* We don't know the size of the size to skip before the data, so decompress the data first into a temporary memory buffer. Then the byteskipping is then just memcpy-ing the appropriate region of memory from "buff" into the given "_data" pointer */ char *buff; airArray *buffArr; /* setting the airArray increment to twice the chunk size means that for headers that are small compared to the data, the airArray never actually has to reallocate. The unit is 1 because we are managing the reading in terms of bytes (sizeof(char)==1 by definition) */ buff = NULL; hack.c = &buff; buffArr = airArrayNew(hack.v, NULL, 1, 2*sizeChunk); airArrayLenSet(buffArr, sizeChunk); if (!( buffArr && buffArr->data )) { sprintf(err, "%s: couldn't initialize airArray\n", me); biffAdd(NRRD, err); return 1; } /* we keep reading in chunks as long as there hasn't been an error, and we haven't hit EOF (EOF signified by read == 0). Unlike the code below (for positive byteskip), we are obligated to read until the bitter end, and can't update sizeChunk to encompass only the required data. Cast on third arg ok because of AIR_MIN use above */ while (!(error = _nrrdGzRead(gzfin, buff + sizeRed, AIR_CAST(unsigned int, sizeChunk), &read)) && read > 0) { sizeRed += read; if (read >= sizeChunk) { /* we were able to read as much data as we requested, maybe there is more, so we need to make our temp buffer bigger */ airArrayLenIncr(buffArr, sizeChunk); if (!buffArr->data) { sprintf(err, "%s: couldn't re-allocate data buffer", me); biffAdd(NRRD, err); return 1; } } } if (error) { sprintf(err, "%s: error reading from gzFile", me); biffAdd(NRRD, err); return 1; } if (sizeRed < sizeData + (-nio->byteSkip - 1)) { sprintf(err, "%s: expected " _AIR_SIZE_T_CNV " bytes and received only " _AIR_SIZE_T_CNV " bytes", me, AIR_CAST(size_t, sizeData + (-nio->byteSkip - 1)), sizeRed); biffAdd(NRRD, err); return 1; } /* also handles nio->byteSkip == -N-1 signifying extra N bytes at end */ memcpy(_data, buff + sizeRed - sizeData - (-nio->byteSkip - 1), sizeData); airArrayNuke(buffArr); } else {