Fixes more gunzip support in r_util

This commit is contained in:
pancake 2014-08-10 18:24:00 +02:00
parent 3d66ed5378
commit 504f006d5d
3 changed files with 22 additions and 5 deletions

View File

@ -454,6 +454,8 @@ R_API char *r_file_temp (const char *prefix);
R_API char *r_file_path(const char *bin);
R_API const char *r_file_basename (const char *path);
R_API char *r_file_abspath(const char *file);
R_API ut8 *r_gunzip(const ut8 *src, int srcLen, int *dstLen);
R_API ut8 *r_file_gzslurp(const char *str, int *outlen, int origonfail);
R_API char *r_file_slurp(const char *str, int *usz);
//R_API char *r_file_slurp_range(const char *str, ut64 off, ut64 sz);
R_API char *r_file_slurp_range(const char *str, ut64 off, int sz, int *osz);

View File

@ -165,13 +165,20 @@ R_API char *r_file_slurp(const char *str, int *usz) {
return ret;
}
R_API char *r_file_gzslurp(const char *str, int *outlen) {
R_API ut8 *r_file_gzslurp(const char *str, int *outlen, int origonfail) {
int sz;
char *in, *out;
ut8 *in, *out;
if (outlen) *outlen = 0;
in = r_file_slurp (str, &sz);
in = (ut8*)r_file_slurp (str, &sz);
if (!in) return NULL;
out = r_gunzip (in, sz, outlen);
if (!out && origonfail) {
// if uncompression fails, return orig buffer ?
if (outlen)
*outlen = sz;
in[sz] = 0;
return in;
}
free (in);
return out;
}

View File

@ -3,19 +3,27 @@
#include <r_util.h>
#include <zlib.h>
// TODO: r_gzip
// TODO: add r_gzip
R_API ut8 *r_gunzip(const void *src, int srcLen, int *dstLen) {
// avoid gzipbombs
#define MAXRETRIES 10
R_API ut8 *r_gunzip(const ut8 *src, int srcLen, int *dstLen) {
ut8 *dst = NULL, *dst2;
z_stream strm;
int tryLen = 1+(srcLen * 4);
int retries = 0;
// TODO: optimize this using an incremental method
retrygunzip:
free (dst);
if (++retries>MAXRETRIES)
return NULL;
if (tryLen<1)
return NULL;
memset (&strm, 0, sizeof (z_stream));
dst = malloc (tryLen+1);
if (!dst)
return NULL;
strm.total_in = strm.avail_in = srcLen;
strm.total_out = strm.avail_out = tryLen;
strm.next_in = (Bytef *) src;