Add r_file_gzslurp() to read GZIPped files easily

This commit is contained in:
pancake 2014-08-10 18:13:12 +02:00
parent d7ea65f66a
commit 3d66ed5378
3 changed files with 87 additions and 1 deletions

View File

@ -9,7 +9,7 @@ OBJS+=list.o flist.o ht.o ht64.o mixed.o btree.o chmod.o graph.o
OBJS+=regex/regcomp.o regex/regerror.o regex/regexec.o uleb128.o
OBJS+=sandbox.o calc.o thread.o lock.o strpool.o bitmap.o strht.o
OBJS+=p_date.o p_format.o print.o p_seven.o slist.o randomart.o
OBJS+=utf8.o strbuf.o lib.o name.o log.o
OBJS+=utf8.o strbuf.o lib.o name.o log.o gzip.o
# DO NOT BUILD r_big api (not yet used and its buggy)
ifeq (1,0)
@ -24,6 +24,12 @@ else
endif
endif
ifeq ($(USE_LIB_ZIP),1)
LINK+=$(LIBZIP)
else
LINK+=$(SHLR)/zip/librz.a
endif
LDFLAGS+=${BN_LIBS}
LDFLAGS+=${TH_LIBS}
LDFLAGS+=${DL_LIBS}

View File

@ -165,6 +165,17 @@ R_API char *r_file_slurp(const char *str, int *usz) {
return ret;
}
R_API char *r_file_gzslurp(const char *str, int *outlen) {
int sz;
char *in, *out;
if (outlen) *outlen = 0;
in = r_file_slurp (str, &sz);
if (!in) return NULL;
out = r_gunzip (in, sz, outlen);
free (in);
return out;
}
R_API ut8 *r_file_slurp_hexpairs(const char *str, int *usz) {
ut8 *ret;
long sz;

69
libr/util/gzip.c Normal file
View File

@ -0,0 +1,69 @@
/* radare - LGPL - Copyright 2014 - pancake */
#include <r_util.h>
#include <zlib.h>
// TODO: r_gzip
R_API ut8 *r_gunzip(const void *src, int srcLen, int *dstLen) {
ut8 *dst = NULL, *dst2;
z_stream strm;
int tryLen = 1+(srcLen * 4);
// TODO: optimize this using an incremental method
retrygunzip:
free (dst);
if (tryLen<1)
return NULL;
memset (&strm, 0, sizeof (z_stream));
dst = malloc (tryLen+1);
strm.total_in = strm.avail_in = srcLen;
strm.total_out = strm.avail_out = tryLen;
strm.next_in = (Bytef *) src;
strm.next_out = (Bytef *) dst;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
int err = -1;
int ret = -1;
// ZLIB
//err = inflateInit2(&strm, (15 + 32)); //15 window bits, and the +32 tells zlib to to detect if using gzip or zlib
// GZIP
err = inflateInit2(&strm, 16+MAX_WBITS);
if (err == Z_OK) {
err = inflate(&strm, Z_FINISH);
if (err == Z_STREAM_END) {
ret = strm.total_out;
if (dstLen)
*dstLen = ret;
dst[ret] = 0;
dst2 = realloc (dst, ret+1);
if (dst2)
dst = dst2;
return dst;
} else {
inflateEnd(&strm);
if (err == Z_BUF_ERROR) {
tryLen *= 2;
goto retrygunzip;
}
return NULL;
}
} else {
inflateEnd(&strm);
if (err == Z_BUF_ERROR) {
tryLen *= 2;
goto retrygunzip;
}
return NULL;
}
inflateEnd(&strm);
if (err == Z_BUF_ERROR) {
tryLen *= 2;
goto retrygunzip;
}
return NULL;
}