Skip to content

Commit

Permalink
inflate
Browse files Browse the repository at this point in the history
  • Loading branch information
tbeu committed May 31, 2020
1 parent 1ed0690 commit b696f82
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
38 changes: 20 additions & 18 deletions src/inflate.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ size_t
InflateSkip(mat_t *mat, z_streamp z, int nBytes)
{
mat_uint8_t comp_buf[READ_BLOCK_SIZE], uncomp_buf[READ_BLOCK_SIZE];
int n, err, cnt = 0;
int n, err, cnt = 0;
size_t bytesread = 0;

if ( nBytes < 1 )
Expand Down Expand Up @@ -82,11 +82,11 @@ InflateSkip(mat_t *mat, z_streamp z, int nBytes)
while ( cnt < nBytes ) {
if ( !z->avail_in ) {
size_t nbytes = fread(comp_buf, 1, n, (FILE*)mat->fp);
bytesread += nbytes;
z->avail_in = (uInt)nbytes;
if ( 0 == nbytes ) {
break;
}
bytesread += nbytes;
z->avail_in = (uInt)nbytes;
z->next_in = comp_buf;
}
err = inflate(z,Z_FULL_FLUSH);
Expand All @@ -105,8 +105,8 @@ InflateSkip(mat_t *mat, z_streamp z, int nBytes)
}

if ( z->avail_in ) {
long offset = -(long)z->avail_in;
(void)fseek((FILE*)mat->fp,offset,SEEK_CUR);
const long offset = -(long)z->avail_in;
(void)fseek((FILE*)mat->fp, offset, SEEK_CUR);
bytesread -= z->avail_in;
z->avail_in = 0;
}
Expand Down Expand Up @@ -203,18 +203,19 @@ InflateRankDims(mat_t *mat, z_streamp z, void *buf, size_t nBytes, mat_uint32_t*

/** @brief Inflates the data
*
* buf must hold at least @c nBytes bytes
* @ingroup mat_internal
* @param mat Pointer to the MAT file
* @param z zlib compression stream
* @param buf Pointer to store the variables name
* @param nBytes Number of characters in the name
* @param buf Pointer to store the uncompressed data
* @param nBytes Number of uncompressed bytes to inflate
* @return Number of bytes read from the file
*/
size_t
Inflate(mat_t *mat, z_streamp z, void *buf, unsigned int nBytes)
{
mat_uint8_t comp_buf[32];
int err;
mat_uint8_t comp_buf[4];
int err;
size_t bytesread = 0;

if ( buf == NULL )
Expand All @@ -238,11 +239,11 @@ Inflate(mat_t *mat, z_streamp z, void *buf, unsigned int nBytes)
}
while ( z->avail_out && !z->avail_in ) {
size_t nbytes = fread(comp_buf, 1, 1, (FILE*)mat->fp);
bytesread += nbytes;
z->avail_in = (uInt)nbytes;
if ( 0 == nbytes ) {
break;
}
bytesread += nbytes;
z->avail_in = (uInt)nbytes;
z->next_in = comp_buf;
err = inflate(z,Z_NO_FLUSH);
if ( err != Z_OK ) {
Expand All @@ -252,7 +253,8 @@ Inflate(mat_t *mat, z_streamp z, void *buf, unsigned int nBytes)
}

if ( z->avail_in ) {
(void)fseek((FILE*)mat->fp,-(int)z->avail_in,SEEK_CUR);
const long offset = -(long)z->avail_in;
(void)fseek((FILE*)mat->fp, offset, SEEK_CUR);
bytesread -= z->avail_in;
z->avail_in = 0;
}
Expand All @@ -271,8 +273,8 @@ Inflate(mat_t *mat, z_streamp z, void *buf, unsigned int nBytes)
* @ingroup mat_internal
* @param mat Pointer to the MAT file
* @param z zlib compression stream
* @param buf Pointer to store the data type
* @param nBytes Number of bytes to inflate
* @param buf Pointer to store the uncompressed data
* @param nBytes Number of uncompressed bytes to inflate
* @return Number of bytes read from the file
*/
size_t
Expand Down Expand Up @@ -317,24 +319,24 @@ InflateData(mat_t *mat, z_streamp z, void *buf, unsigned int nBytes)
} else {
nbytes = fread(comp_buf, 1, nBytes - bytesread, (FILE*)mat->fp);
}
bytesread += nbytes;
z->avail_in = (uInt)nbytes;
if ( 0 == nbytes ) {
break;
}
bytesread += nbytes;
z->avail_in = (uInt)nbytes;
z->next_in = comp_buf;
err = inflate(z,Z_FULL_FLUSH);
if ( err == Z_STREAM_END ) {
break;
} else if ( err != Z_OK && err != Z_BUF_ERROR ) {
} else if ( err != Z_OK ) {
Mat_Critical("InflateData: inflate returned %s",zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
break;
}
}

if ( z->avail_in ) {
const long offset = -(long)z->avail_in;
(void)fseek((FILE*)mat->fp,offset,SEEK_CUR);
(void)fseek((FILE*)mat->fp, offset, SEEK_CUR);
bytesread -= z->avail_in;
z->avail_in = 0;
}
Expand Down
14 changes: 7 additions & 7 deletions src/mat.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,19 +313,19 @@ int SafeAdd(size_t* res, size_t a, size_t b)
* @retval 0 on success
*/
int
SafeRead(void* buf, size_t size, size_t count, FILE* fp, size_t* read_bytes) {
const size_t read_result = fread(buf, size, count, fp);
int err = read_result != count;
if ( NULL != read_bytes ) {
*read_bytes += read_result*size;
SafeRead(void* buf, size_t size, size_t count, FILE* fp, size_t* bytesread) {
const size_t readcount = fread(buf, size, count, fp);
int err = readcount != count;
if ( NULL != bytesread ) {
*bytesread += readcount*size;
}
if ( err && feof(fp) && 0 == read_result) {
if ( err && feof(fp) && 0 == readcount) {
err = 0;
}
if ( err ) {
Mat_Critical("Read beyond EOF error: Read %"
SIZE_T_FMTSTR " bytes, expected %"
SIZE_T_FMTSTR " bytes", read_result*size, count*size);
SIZE_T_FMTSTR " bytes", readcount*size, count*size);
}
return err;
}
Expand Down

0 comments on commit b696f82

Please sign in to comment.