New r_inflate_lz4 API to reuse LZ4 across all libs ##api

This commit is contained in:
Sergi Àlvarez i Capilla 2022-02-10 18:09:24 +01:00 committed by pancake
parent a6309160f4
commit 08bf7bc8c7
7 changed files with 51 additions and 13 deletions

View File

@ -1,4 +1,4 @@
/* radare2 - LGPL - Copyright 2017-2018 - rkx1209 */
/* radare2 - LGPL - Copyright 2017-2022 - rkx1209 */
#include <r_types.h>
#include <r_util.h>
@ -7,11 +7,6 @@
#include <r_io.h>
#include <r_cons.h>
#include "nxo/nxo.h"
#ifdef R_MESON_VERSION
#include <lz4.h>
#else
#include "../../../shlr/lz4/lz4.c"
#endif
#define NSO_OFF(x) r_offsetof (NSOHeader, x)
#define NSO_OFFSET_MODMEMOFF r_offsetof (NXOStart, mod_memoffset)
@ -40,7 +35,15 @@ static uint32_t decompress(const ut8 *cbuf, ut8 *obuf, int32_t csize, int32_t us
if (csize < 0 || usize < 0 || !cbuf || !obuf) {
return -1;
}
return LZ4_decompress_safe ((const char*)cbuf, (char*)obuf, (uint32_t) csize, (uint32_t) usize);
int consumed, osize;
ut8 *out = r_inflate_lz4 (cbuf, (int)csize, &consumed, &osize);
if (out) {
// osize should be the same as usize
memcpy (obuf, out, R_MIN (usize, osize));
free (out);
return usize;
}
return 0;
}
static ut64 baddr(RBinFile *bf) {

View File

@ -1,5 +1,4 @@
OBJ_NSO=bin_nso.o ../format/nxo/nxo.o
#include $(SHLR)/lz4/deps.mk
STATIC_OBJ+=${OBJ_NSO}
TARGET_NSO=bin_nso.${EXT_SO}

View File

@ -39,6 +39,7 @@ R_API char *r_file_abspath_rel(const char *cwd, const char *file);
R_API char *r_file_abspath(const char *file);
R_API ut8 *r_inflate(const ut8 *src, int srcLen, int *srcConsumed, int *dstLen);
R_API ut8 *r_inflate_raw(const ut8 *src, int srcLen, int *srcConsumed, int *dstLen);
R_API ut8 *r_inflate_lz4(const ut8 *src, int srcLen, int *consumed, int *dstLen);
R_API ut8 *r_file_gzslurp(const char *str, int *outlen, int origonfail);
R_API char *r_stdin_slurp(int *sz);
R_API char *r_file_slurp(const char *str, R_NULLABLE size_t *usz);

View File

@ -3,7 +3,6 @@ include ../config.mk
NAME=r_util
CFLAGS+=-DR2_PLUGIN_INCORE -I$(TOP)/shlr
PCLIBS=@LIBZIP@ @DL_LIBS@
OBJS=mem.o unum.o str.o hex.o file.o range.o charset.o
OBJS+=prof.o cache.o sys.o buf.o sys_w32.o ubase64.o base85.o base91.o
OBJS+=list.o flist.o chmod.o graph.o event.o alloc.o donut.o print_code.o
@ -24,6 +23,8 @@ OBJS+=d/ebcdic37.o
OBJS+=d/iso8859_1.o
endif
OBJS+=$(SHLR)/lz4/lz4.o
ifeq (${HAVE_LIB_GMP},1)
OBJS+=big_gmp.o
else

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009-2021 - ret2libc, pancake */
/* radare - LGPL - Copyright 2009-2022 - ret2libc, pancake */
#include <r_types.h>
#include <r_util.h>
@ -298,7 +298,6 @@ R_API bool r_buf_append_bytes(RBuffer *b, const ut8 *buf, ut64 length) {
if (r_buf_seek (b, 0, R_BUF_END) < 0) {
return false;
}
return r_buf_write (b, buf, length) >= 0;
}

View File

@ -1,6 +1,7 @@
subdir('d')
r_util_sources = [
'../../shlr/lz4/lz4.c',
'ascii_table.c',
'assert.c',
'w32.c',

View File

@ -1,8 +1,8 @@
/* radare - LGPL - Copyright 2014-2015 - pancake */
/* radare - LGPL - Copyright 2014-2022 - pancake */
#include <r_util.h>
#include <zlib.h>
#include "../../../shlr/lz4/lz4.h"
// set a maximum output buffer of 50MB
#define MAXOUT 50000000
@ -83,6 +83,40 @@ static ut8 *r_inflatew(const ut8 *src, int srcLen, int *consumed, int *dstLen, i
return NULL;
}
R_API ut8 *r_inflate_lz4(const ut8 *src, int srcLen, int *consumed, int *dstLen) {
ut32 osz = srcLen * 5;
ut8 *obuf = calloc (srcLen, 5);
if (!obuf) {
return NULL;
}
int res = LZ4_decompress_safe ((const char*)src, (char*)obuf, (uint32_t) srcLen, (uint32_t) osz);
if (res < 1) {
int mul = srcLen / -res;
int nosz = osz * (5 * (mul + 1));
if (nosz < osz) {
free (obuf);
return NULL;
}
ut8 *nbuf = realloc (obuf, nosz);
if (!nbuf) {
free (obuf);
return NULL;
}
obuf = nbuf;
osz = nosz;
}
res = LZ4_decompress_safe ((const char*)src, (char*)obuf, (uint32_t) srcLen, (uint32_t) osz);
if (res > 0) {
*dstLen = res;
*consumed = srcLen;
return obuf;
}
*dstLen = 0;
*consumed = 0;
free (obuf);
return NULL;
}
R_API ut8 *r_inflate(const ut8 *src, int srcLen, int *consumed, int *dstLen) {
return r_inflatew (src, srcLen, consumed, dstLen, MAX_WBITS + 32);
}