Skip to content

Commit

Permalink
Read multiple bytes instead of byte-by-byte in minizip unzip.c.
Browse files Browse the repository at this point in the history
Use a single ZREAD64 call in the unz64local_getShort/Long/Long64
implementation, rather than read it byte by byte.
  • Loading branch information
eugenegff authored and madler committed Aug 3, 2023
1 parent aa154e3 commit be7aa11
Showing 1 changed file with 38 additions and 96 deletions.
134 changes: 38 additions & 96 deletions contrib/minizip/unzip.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,129 +197,71 @@ typedef struct
#include "crypt.h"
#endif


/* ===========================================================================
Read a byte from a gz_stream; update next_in and avail_in. Return EOF
for end of file.
IN assertion: the stream s has been successfully opened for reading.
Reads a long in LSB order from the given gz_stream. Sets
*/


local int unz64local_getByte(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, int *pi) {
unsigned char c;
int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,&c,1);
if (err==1)
local int unz64local_getShort(const zlib_filefunc64_32_def* pzlib_filefunc_def,
voidpf filestream,
uLong *pX) {
unsigned char c[2];
int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,c,2);
if (err==2)
{
*pi = (int)c;
*pX = c[0] | ((uLong)c[1] << 8);
return UNZ_OK;
}
else
{
*pX = 0;
if (ZERROR64(*pzlib_filefunc_def,filestream))
return UNZ_ERRNO;
else
return UNZ_EOF;
}
}


/* ===========================================================================
Reads a long in LSB order from the given gz_stream. Sets
*/

local int unz64local_getShort(const zlib_filefunc64_32_def* pzlib_filefunc_def,
voidpf filestream,
uLong *pX) {
uLong x ;
int i = 0;
int err;

err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
x = (uLong)i;

if (err==UNZ_OK)
err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
x |= ((uLong)i)<<8;

if (err==UNZ_OK)
*pX = x;
else
*pX = 0;
return err;
}

local int unz64local_getLong(const zlib_filefunc64_32_def* pzlib_filefunc_def,
voidpf filestream,
uLong *pX) {
uLong x ;
int i = 0;
int err;

err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
x = (uLong)i;

if (err==UNZ_OK)
err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
x |= ((uLong)i)<<8;

if (err==UNZ_OK)
err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
x |= ((uLong)i)<<16;

if (err==UNZ_OK)
err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
x += ((uLong)i)<<24;

if (err==UNZ_OK)
*pX = x;
unsigned char c[4];
int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,c,4);
if (err==4)
{
*pX = c[0] | ((uLong)c[1] << 8) | ((uLong)c[2] << 16) | ((uLong)c[3] << 24);
return UNZ_OK;
}
else
{
*pX = 0;
return err;
if (ZERROR64(*pzlib_filefunc_def,filestream))
return UNZ_ERRNO;
else
return UNZ_EOF;
}
}


local int unz64local_getLong64(const zlib_filefunc64_32_def* pzlib_filefunc_def,
voidpf filestream,
ZPOS64_T *pX) {
ZPOS64_T x ;
int i = 0;
int err;

err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
x = (ZPOS64_T)i;

if (err==UNZ_OK)
err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
x |= ((ZPOS64_T)i)<<8;

if (err==UNZ_OK)
err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
x |= ((ZPOS64_T)i)<<16;

if (err==UNZ_OK)
err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
x |= ((ZPOS64_T)i)<<24;

if (err==UNZ_OK)
err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
x |= ((ZPOS64_T)i)<<32;

if (err==UNZ_OK)
err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
x |= ((ZPOS64_T)i)<<40;

if (err==UNZ_OK)
err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
x |= ((ZPOS64_T)i)<<48;

if (err==UNZ_OK)
err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
x |= ((ZPOS64_T)i)<<56;

if (err==UNZ_OK)
*pX = x;
unsigned char c[8];
int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,c,8);
if (err==8)
{
*pX = c[0] | ((ZPOS64_T)c[1] << 8) | ((ZPOS64_T)c[2] << 16) | ((ZPOS64_T)c[3] << 24)
| ((ZPOS64_T)c[4] << 32) | ((ZPOS64_T)c[5] << 40) | ((ZPOS64_T)c[6] << 48) | ((ZPOS64_T)c[7] << 56);
return UNZ_OK;
}
else
{
*pX = 0;
return err;
if (ZERROR64(*pzlib_filefunc_def,filestream))
return UNZ_ERRNO;
else
return UNZ_EOF;
}
}

/* My own strcmpi / strcasecmp */
Expand Down Expand Up @@ -1108,7 +1050,7 @@ extern int ZEXPORT unzGetCurrentFileInfo(unzFile file,
pfile_info->internal_fa = file_info64.internal_fa;
pfile_info->external_fa = file_info64.external_fa;

pfile_info->tmu_date = file_info64.tmu_date,
pfile_info->tmu_date = file_info64.tmu_date;


pfile_info->compressed_size = (uLong)file_info64.compressed_size;
Expand Down

0 comments on commit be7aa11

Please sign in to comment.