Skip to content

Commit

Permalink
Add nx_gzread
Browse files Browse the repository at this point in the history
Adds a function to read from a compressed file.

Signed-off-by: Raphael Moreira Zinsly <rzinsly@linux.ibm.com>
  • Loading branch information
rzinsly committed Apr 13, 2022
1 parent 764fe41 commit 1e335c4
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 34 deletions.
1 change: 1 addition & 0 deletions lib/Versions
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ LIBNXZ_0.64.0 {
nx_gzclose;
nx_gzopen;
nx_gzdopen;
nx_gzread;
nx_gzwrite;
} LIBNXZ_0.63.0;

Expand Down
62 changes: 62 additions & 0 deletions lib/nx_gzlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,50 @@ gzFile nx_gzopen_(const char* path, int fd, const char *mode)
return file;
}

int nx_gzread(gzFile file, void *buf, unsigned len)
{
gz_statep state = (gz_statep) file;
z_streamp stream = &(state->strm);
int ret, err;
const uInt max = len > 10? 10 : 1;
uLong last_total_out = stream->total_out;
unsigned char* next_in = malloc(sizeof(char)*max);

if (next_in == NULL)
return Z_MEM_ERROR;

stream->next_out = buf;
stream->avail_out = len;
do {
ret = read(state->fd, next_in, sizeof(char)*max);
stream->next_in = next_in;
if (ret == 0){
/* Flush all at the end of the file. */
err = nx_inflate(stream, Z_FINISH);
break;
}

stream->avail_in = ret;
err = nx_inflate(stream, Z_NO_FLUSH);

if (err != Z_OK && err != Z_STREAM_END){
state->err = err;
free(next_in);
return 0;
}
} while (stream->avail_out != 0);

/* gzread should return when len bytes are wrote on buf, if we still
have unprocessed data on avail_in we need to save it on fifo_in. */
if (stream->avail_in > 0){
nx_streamp s = (nx_streamp) stream->state;
copy_data_to_fifo_in(s);
}

free(next_in);
return stream->total_out - last_total_out;
}

int nx_gzwrite (gzFile file, const void *buf, unsigned len)
{
gz_statep state = (gz_statep) file;
Expand Down Expand Up @@ -240,6 +284,24 @@ gzFile gzdopen(int fd, const char *mode)
return nx_gzdopen(fd, mode);
}

int gzread(gzFile file, void *buf, unsigned len)
{
int rc;

if (nx_config.mode.inflate == GZIP_AUTO) {
if(len <= DECOMPRESS_THRESHOLD)
rc = sw_gzread(file, buf, len);
else
rc = nx_gzread(file, buf, len);
}else if (nx_config.mode.inflate == GZIP_SW) {
rc = sw_gzread(file, buf, len);
}else {
rc = nx_gzread(file, buf, len);
}

return rc;
}

int gzwrite(gzFile file, const void *buf, unsigned len)
{
int rc;
Expand Down
34 changes: 0 additions & 34 deletions lib/nx_inflate.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,6 @@ do { if ((s)->cur_out > (s)->len_out/2) { \
(s)->cur_out = INF_HIS_LEN; } \
} while(0)

#define fifo_in_len_check(s) \
do { if ((s)->cur_in > (s)->len_in/2) { \
memmove((s)->fifo_in, (s)->fifo_in + (s)->cur_in, (s)->used_in); \
(s)->cur_in = 0; } \
} while(0)

static int nx_inflate_(nx_streamp s, int flush);

int nx_inflateResetKeep(z_streamp strm)
Expand Down Expand Up @@ -906,34 +900,6 @@ static int nx_amend_history_with_dict(nx_streamp s)
return nx_history_len;
}


static int copy_data_to_fifo_in(nx_streamp s) {
uint32_t free_space, read_sz;

if (s->fifo_in == NULL) {
s->len_in = nx_config.cache_threshold * 2;
if (NULL == (s->fifo_in = nx_alloc_buffer(s->len_in, nx_config.page_sz, 0))) {
prt_err("nx_alloc_buffer for inflate fifo_in\n");
return Z_MEM_ERROR;
}
}

/* reset fifo head to reduce unnecessary wrap arounds */
s->cur_in = (s->used_in == 0) ? 0 : s->cur_in;
fifo_in_len_check(s);
free_space = s->len_in - s->cur_in - s->used_in;

read_sz = NX_MIN(free_space, s->avail_in);
if (read_sz > 0) {
/* copy from next_in to the offset cur_in + used_in */
memcpy(s->fifo_in + s->cur_in + s->used_in, s->next_in, read_sz);
update_stream_in(s, read_sz);
s->used_in = s->used_in + read_sz;
}

return Z_OK;
}

/** \brief Reset DDE to initial values.
*
* @param s nx_streamp to be processed.
Expand Down
34 changes: 34 additions & 0 deletions lib/nx_zlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,12 @@ static inline int use_nx_deflate(z_streamp strm)
#define fifo_used_first_offset(cur) (cur)
#define fifo_used_last_offset(cur) (0)

#define fifo_in_len_check(s) \
do { if ((s)->cur_in > (s)->len_in/2) { \
memmove((s)->fifo_in, (s)->fifo_in + (s)->cur_in, (s)->used_in); \
(s)->cur_in = 0; } \
} while(0)

/* for appending bytes in to the stream */
#define nx_put_byte(s,b) do { if ((s)->avail_out > 0) \
{ *((s)->next_out++) = (b); --(s)->avail_out; ++(s)->total_out; \
Expand Down Expand Up @@ -618,6 +624,7 @@ extern void *dht_copy(void *handle);
extern int nx_gzclose(gzFile file);
extern gzFile nx_gzopen(const char* path, const char *mode);
extern gzFile nx_gzdopen(int fd, const char *mode);
extern int nx_gzread(gzFile file, void *buf, unsigned len);
extern int nx_gzwrite(gzFile file, const void *buf, unsigned len);

/* sw_zlib.c */
Expand All @@ -642,6 +649,7 @@ extern int sw_uncompress2(Bytef *dest, uLongf *destLen, const Bytef *source, uLo
extern int sw_gzclose(gzFile file);
extern gzFile sw_gzopen(const char* path, const char *mode);
extern gzFile sw_gzdopen(int fd, const char *mode);
extern int sw_gzread(gzFile file, void *buf, unsigned len);
extern int sw_gzwrite(gzFile file, const void *buf, unsigned len);

extern int sw_inflateInit_(z_streamp strm, const char *version, int stream_size);
Expand All @@ -659,6 +667,32 @@ extern int sw_compress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong
extern int sw_compress2(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen, int level);
extern uLong sw_compressBound(uLong sourceLen);

static inline int copy_data_to_fifo_in(nx_streamp s) {
uint32_t free_space, read_sz;

if (s->fifo_in == NULL) {
s->len_in = nx_config.cache_threshold * 2;
if (NULL == (s->fifo_in = nx_alloc_buffer(s->len_in, nx_config.page_sz, 0))) {
prt_err("nx_alloc_buffer for inflate fifo_in\n");
return Z_MEM_ERROR;
}
}

/* reset fifo head to reduce unnecessary wrap arounds */
s->cur_in = (s->used_in == 0) ? 0 : s->cur_in;
fifo_in_len_check(s);
free_space = s->len_in - s->cur_in - s->used_in;

read_sz = NX_MIN(free_space, s->avail_in);
if (read_sz > 0) {
/* copy from next_in to the offset cur_in + used_in */
memcpy(s->fifo_in + s->cur_in + s->used_in, s->next_in, read_sz);
update_stream_in(s, read_sz);
s->used_in = s->used_in + read_sz;
}

return Z_OK;
}


#endif /* _NX_ZLIB_H */
8 changes: 8 additions & 0 deletions lib/sw_zlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ gzFile sw_gzdopen(int fd, const char *mode)
return (* p_gzdopen)(fd, mode);
}

static int (* p_gzread)(gzFile file, void *buf, unsigned len);
int sw_gzread(gzFile file, void *buf, unsigned len)
{
check_sym(p_gzread, Z_STREAM_ERROR);
return (* p_gzread)(file, buf, len);
}

static int (*p_gzwrite)(gzFile file, const void *buf, unsigned len);
int sw_gzwrite (gzFile file, const void *buf, unsigned len)
{
Expand Down Expand Up @@ -339,6 +346,7 @@ int sw_zlib_init(void)
register_sym(gzclose);
register_sym(gzopen);
register_sym(gzdopen);
register_sym(gzread);
register_sym(gzwrite);
register_sym(inflateInit_);
register_sym(inflateInit2_);
Expand Down
2 changes: 2 additions & 0 deletions libnxz.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ extern int nx_uncompress(unsigned char *dest, ulong *destLen,
extern int nx_gzclose(gzFile file);
extern gzFile nx_gzopen(const char* path, const char *mode);
extern gzFile nx_gzdopen(int fd, const char *mode);
extern int nx_gzread(FILE file, void *buf, unsigned len);
extern int nx_gzwrite(gzFile file, const void *buf, unsigned len);

extern int deflateInit_(void *strm, int level, const char* version,
Expand Down Expand Up @@ -186,6 +187,7 @@ extern int uncompress2(unsigned char *dest, ulong *destLen,
extern int gzclose(gzFile file);
extern gzFile gzopen(const char* path, const char *mode);
extern gzFile gzdopen(int fd, const char *mode);
extern int gzread(FILE file, void *buf, unsigned len);
extern int gzwrite(gzFile file, void *buf, unsigned len);

#endif /* _LIBNXZ_H */
14 changes: 14 additions & 0 deletions test/libnxz.abi
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<elf-symbol name='gzclose' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='gzdopen' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='gzopen' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='gzread' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='gzwrite' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='inflate' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='inflateCopy' version='ZLIB_1.2.0' is-default-version='yes' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
Expand Down Expand Up @@ -60,6 +61,7 @@
<elf-symbol name='nx_gzclose' version='LIBNXZ_0.64.0' is-default-version='yes' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='nx_gzdopen' version='LIBNXZ_0.64.0' is-default-version='yes' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='nx_gzopen' version='LIBNXZ_0.64.0' is-default-version='yes' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='nx_gzread' version='LIBNXZ_0.64.0' is-default-version='yes' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='nx_gzwrite' version='LIBNXZ_0.64.0' is-default-version='yes' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='nx_inflate' version='LIBNXZ_0.61.0' is-default-version='yes' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='nx_inflateCopy' version='LIBNXZ_0.61.0' is-default-version='yes' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
Expand Down Expand Up @@ -439,6 +441,12 @@
<parameter type-id='f0981eeb' name='len'/>
<return type-id='95e97e5e'/>
</function-decl>
<function-decl name='gzread' mangled-name='gzread' visibility='default' binding='global' size-in-bits='64' elf-symbol-id='gzread'>
<parameter type-id='45259bf7' name='file'/>
<parameter type-id='eaa32e2f' name='buf'/>
<parameter type-id='f0981eeb' name='len'/>
<return type-id='95e97e5e'/>
</function-decl>
<function-decl name='gzdopen' mangled-name='gzdopen' visibility='default' binding='global' size-in-bits='64' elf-symbol-id='gzdopen'>
<parameter type-id='95e97e5e' name='fd'/>
<parameter type-id='80f4b756' name='mode'/>
Expand All @@ -459,6 +467,12 @@
<parameter type-id='f0981eeb' name='len'/>
<return type-id='95e97e5e'/>
</function-decl>
<function-decl name='nx_gzread' mangled-name='nx_gzread' visibility='default' binding='global' size-in-bits='64' elf-symbol-id='nx_gzread@@LIBNXZ_0.64.0'>
<parameter type-id='45259bf7' name='file'/>
<parameter type-id='eaa32e2f' name='buf'/>
<parameter type-id='f0981eeb' name='len'/>
<return type-id='95e97e5e'/>
</function-decl>
<function-decl name='nx_gzopen' mangled-name='nx_gzopen' visibility='default' binding='global' size-in-bits='64' elf-symbol-id='nx_gzopen@@LIBNXZ_0.64.0'>
<parameter type-id='80f4b756' name='path'/>
<parameter type-id='80f4b756' name='mode'/>
Expand Down

0 comments on commit 1e335c4

Please sign in to comment.