Skip to content

Commit 7ff7d49

Browse files
authored
Default to r2's own lz4 implementation - remove smallz4 ##util
1 parent e35e1f5 commit 7ff7d49

14 files changed

+91
-559
lines changed

Diff for: config-user.mk.acr

-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ LIBZIP=@LIBZIP@
111111
LIBXXHASH=@LIBXXHASH@
112112

113113
USE_SYSLZ4=@USE_SYSLZ4@
114-
USE_SMALLZ4=@USE_SMALLZ4@
115114

116115
ifeq ($(USE_SYSLZ4),1)
117116
LZ4_LDFLAGS=@LZ4_LDFLAGS@

Diff for: configure

+8-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/sh
2-
# This script was automatically generated by ACR v2.1.4
2+
# This script was automatically generated by ACR v2.2.0
33
# @author: pancake <nopcode.org>
44
# @url: http://www.nopcode.org
55
# @repo: git clone https://github.com/radare/acr
@@ -195,8 +195,7 @@ System types:
195195
--target=TARGET configure for building compilers for TARGET [HOST]
196196
EOF2
197197

198-
printf "
199-
Optional Features:
198+
printf "\nOptional Features:
200199
--disable-debugger disable native debugger features
201200
--with-libatomic force to use libatomic
202201
--with-sysmagic force to use system's magic
@@ -232,20 +231,16 @@ Optional Features:
232231
--with-libversion specify different libversion (LIBVERSION=xxx)
233232
--without-jemalloc build without jemalloc
234233
--with-checks-level value between 0 and 3 to enable different level of assert (see R_CHECKS_LEVEL) (R_CHECKS_LEVEL=2)
235-
--disable-debug-stuff disable the log messages and disable control-c
236-
"
237-
printf "
238-
Some influential environment variables:
234+
--disable-debug-stuff disable the log messages and disable control-c\n"
235+
printf "\nSome influential environment variables:
239236
CC C compiler command
240237
CFLAGS C compiler flags
241238
LDFLAGS linker flags, e.g. -L<lib dir> if you have libraries in a
242239
nonstandard directory <lib dir>
243240
CPPFLAGS C/C++ preprocessor flags, e.g. -I<include dir> if you have
244241
headers in a nonstandard directory <include dir>
245-
CPP C preprocessor
246-
"
247-
printf "
248-
Report bugs to: pancake <[email protected]>"
242+
CPP C preprocessor\n"
243+
printf "\nReport bugs to: pancake <[email protected]>"
249244
echo ""
250245
exit 0
251246
}
@@ -272,7 +267,7 @@ if [ "$QUIET" = 1 ]; then
272267
echo "5.9.9"
273268
exit 0
274269
fi
275-
echo "radare2-5.9.9 configuration script done with acr v2.1.4.
270+
echo "radare2-5.9.9 configuration script done with acr v2.2.0.
276271
The 'Free Software Foundation' message is only for autodetection.
277272
Originally written by pancake <nopcode.org>."
278273
exit 0
@@ -1052,7 +1047,7 @@ do_remove
10521047
if [ "$QUIET" = 0 ]; then
10531048
echo
10541049
echo "Final report:"
1055-
for A in BUILD CC CFLAGS DEBUGGER HAVE_ARC4RANDOM_UNIFORM HAVE_EXPLICIT_BZERO USE_NEW_ABI HAVE_EXPLICIT_MEMSET HAVE_FORK HAVE_GPERF HAVE_LIBUV HAVE_LIB_GMP WANT_DEBUGSTUFF HAVE_OPENSSL WANT_SSL_CRYPTO HAVE_PTRACE HOST LDFLAGS LIBVERSION PKGCONFIG PREFIX R_CHECKS_LEVEL TARGET USERCC USEROSTYPE USE_CAPSTONE USE_LIB_MAGIC NEW_IO_CACHE USE_SQSH USE_LIB_XXHASH USE_LIB_ZIP USE_PTRACE_WRAP USE_SYSLZ4 VERSION WANT_DYLINK USE_SMALLZ4 USE_LIB_ATOMIC HAVE___ATOMIC_FETCH_ADD_8 FORCE_LIB_ATOMIC WANT_QJS ; do
1050+
for A in BUILD CC CFLAGS DEBUGGER HAVE_ARC4RANDOM_UNIFORM HAVE_EXPLICIT_BZERO USE_NEW_ABI HAVE_EXPLICIT_MEMSET HAVE_FORK HAVE_GPERF HAVE_LIBUV HAVE_LIB_GMP WANT_DEBUGSTUFF HAVE_OPENSSL WANT_SSL_CRYPTO HAVE_PTRACE HOST LDFLAGS LIBVERSION PKGCONFIG PREFIX R_CHECKS_LEVEL TARGET USERCC USEROSTYPE USE_CAPSTONE USE_LIB_MAGIC NEW_IO_CACHE USE_SQSH USE_LIB_XXHASH USE_LIB_ZIP USE_PTRACE_WRAP USE_SYSLZ4 VERSION WANT_DYLINK USE_LIB_ATOMIC HAVE___ATOMIC_FETCH_ADD_8 FORCE_LIB_ATOMIC WANT_QJS ; do
10561051
eval VAL="\$${A}"
10571052
[ -z "${VAL}" ] && VAL="\"\""
10581053
echo " - ${A} = ${VAL}"

Diff for: configure.acr

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ REPORT
336336
HAVE_EXPLICIT_MEMSET HAVE_FORK HAVE_GPERF HAVE_LIBUV HAVE_LIB_GMP WANT_DEBUGSTUFF
337337
HAVE_OPENSSL WANT_SSL_CRYPTO HAVE_PTRACE HOST LDFLAGS LIBVERSION PKGCONFIG PREFIX
338338
R_CHECKS_LEVEL TARGET USERCC USEROSTYPE USE_CAPSTONE USE_LIB_MAGIC NEW_IO_CACHE USE_SQSH
339-
USE_LIB_XXHASH USE_LIB_ZIP USE_PTRACE_WRAP USE_SYSLZ4 VERSION WANT_DYLINK USE_SMALLZ4
339+
USE_LIB_XXHASH USE_LIB_ZIP USE_PTRACE_WRAP USE_SYSLZ4 VERSION WANT_DYLINK
340340
USE_LIB_ATOMIC HAVE___ATOMIC_FETCH_ADD_8 FORCE_LIB_ATOMIC WANT_QJS
341341
;
342342

Diff for: libr/bin/meson.build

+2-1
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,9 @@ r_bin_sources += r_bin_d_sources
156156
r_bin_inc = [platform_inc, include_directories('mangling', 'format')]
157157

158158
if not get_option('use_sys_lz4')
159-
library_cflags += ['-DUSE_SMALLZ4']
159+
library_cflags += ['-DUSE_RLZ4']
160160
endif
161+
161162
r_bin = library('r_bin', r_bin_sources,
162163
include_directories: r_bin_inc,
163164
c_args: ['-DR_API_BIN_ONLY=1'] + library_cflags,

Diff for: libr/include/r_util.h

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ int gettimeofday (struct timeval* p, void* tz);
9191

9292
R_API int r_lz4_compress(ut8 *obuf, ut8 *buf, size_t buf_size, const int max_chain);
9393
R_API ut8 *r_lz4_decompress(const ut8* input, size_t input_size, size_t *output_size);
94+
R_API int r_lz4_decompress_block(ut8 *g_buf, const int comp_len, int *pp, ut8 *obuf, int osz);
9495

9596
#ifdef __cplusplus
9697
extern "C" {

Diff for: libr/util/Makefile

+1-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ CFLAGS+=${LZ4_CFLAGS}
4444
PCLIBS+=@LZ4_LDFLAGS@
4545
LDFLAGS+=${LZ4_LDFLAGS}
4646
else
47-
CFLAGS+=-DUSE_SMALLZ4
48-
OBJS+=$(SHLR)/smallz4/smallz4cat.o
47+
CFLAGS+=-DUSE_RLZ4
4948
endif
5049

5150
OBJS+=big_gmp.o

Diff for: libr/util/meson.build

+2-1
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,9 @@ if get_option('sdb_cgen')
110110
r_util_sources += r_util_d_sources
111111
endif
112112

113+
lz4_dep = dependency('liblz4', required: false)
113114
if not get_option('use_sys_lz4')
114-
library_cflags += ['-DUSE_SMALLZ4']
115+
library_cflags += ['-DUSE_RLZ4']
115116
endif
116117

117118
r_util_deps = [ldl, mth, spp_dep, pth, utl, sdb_dep, lz4_dep, zlib_dep, platform_deps]

Diff for: libr/util/rlz4.c

+58-22
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
#include <r_util.h>
66

7-
// R2_600 - replace shlr/smallz4 with this code which also supports compressing
8-
97
#define BLOCK_SIZE (1024 * 8) /* 8K */
108
#define PADDING_LITERALS 5
119
#define WINDOW_BITS 10
@@ -18,7 +16,9 @@
1816

1917
#define LOAD_16(p) *(ut16*)&g_buf[(p)]
2018
#define LOAD_32(p) *(ut32*)&g_buf[(p)]
19+
#define LOAD_32_FROM(p, x) *(ut32 *)&x[(p)]
2120
#define COPY_32(d, s) *(ut32*)&g_buf[(d)] = LOAD_32((s))
21+
#define COPY_32_TO(d, s, x, y) *(ut32 *)&x[(d)] = LOAD_32_FROM (s, y)
2222

2323
#define HASH_BITS 12
2424
#define HASH_SIZE (1 << HASH_BITS)
@@ -142,10 +142,12 @@ static int lz4_compress(ut8 *g_buf, const int uc_length, int max_chain) {
142142
return op - BLOCK_SIZE;
143143
}
144144

145-
static int lz4_decompress(ut8 *g_buf, const int comp_len, int *pp) {
145+
R_API int r_lz4_decompress_block(ut8 *g_buf, const int comp_len, int *pp, ut8 *obuf, int osz) {
146146
int i, s, len, run, p = 0;
147-
int ip = BLOCK_SIZE;
147+
int ip = obuf? 0: BLOCK_SIZE;
148+
int maxLen = obuf? osz: BLOCK_SIZE;
148149
int ip_end = ip + comp_len;
150+
ut8 *dst = obuf? obuf: g_buf;
149151

150152
for (;;) {
151153
const int token = g_buf[ip++];
@@ -160,15 +162,15 @@ static int lz4_decompress(ut8 *g_buf, const int comp_len, int *pp) {
160162
}
161163
}
162164
}
163-
if ((p + run) > BLOCK_SIZE) {
165+
if ((p + run) > maxLen) {
164166
return -1;
165167
}
166168

167-
COPY_32 (p, ip);
168-
COPY_32 (p + 4, ip + 4);
169+
COPY_32_TO (p, ip, dst, g_buf);
170+
COPY_32_TO (p + 4, ip + 4, dst, g_buf);
169171
for (i = 8; i < run; i += 8) {
170-
COPY_32 (p + i, ip + i);
171-
COPY_32 (p + 4 + i, ip + 4 + i);
172+
COPY_32_TO (p + i, ip + i, dst, g_buf);
173+
COPY_32_TO (p + 4 + i, ip + 4 + i, dst, g_buf);
172174
}
173175
p += run;
174176
ip += run;
@@ -192,20 +194,20 @@ static int lz4_decompress(ut8 *g_buf, const int comp_len, int *pp) {
192194
}
193195
}
194196
}
195-
if ((p + len) > BLOCK_SIZE) {
197+
if ((p + len) > maxLen) {
196198
return -1;
197199
}
198200
if ((p - s) >= 4) {
199-
COPY_32 (p, s);
200-
COPY_32 (p + 4, s + 4);
201+
COPY_32_TO (p, s, dst, dst);
202+
COPY_32_TO (p + 4, s + 4, dst, dst);
201203
for (i = 8; i < len; i += 8) {
202-
COPY_32 (p + i, s + i);
203-
COPY_32 (p + 4 + i, s + 4 + i);
204+
COPY_32_TO (p + i, s + i, dst, dst);
205+
COPY_32_TO (p + 4 + i, s + 4 + i, dst, dst);
204206
}
205207
p += len;
206208
} else {
207209
while (len-- != 0) {
208-
g_buf[p++] = g_buf[s++];
210+
dst[p++] = dst[s++];
209211
}
210212
}
211213
}
@@ -217,24 +219,58 @@ R_API ut8 *r_lz4_decompress(const ut8* input, size_t input_size, size_t *output_
217219
R_RETURN_VAL_IF_FAIL (input && output_size, NULL);
218220
RBuffer *b = r_buf_new ();
219221
ut8 g_buf[(BLOCK_SIZE + BLOCK_SIZE + EXCESS) * sizeof (ut8)];
222+
bool is_legacy = true;
223+
bool has_block_checksum = false, has_content_size = false, has_dictionary_id = false, has_content_checksum = false;
220224
const ut8 *input_last = input + input_size;
221-
while (input + 4 < input_last) {
222-
if (!memcmp (input, "\x02\x21\x4c\x18", 4)) {
225+
226+
// Process the lz4 header
227+
if (!memcmp (input, "\x02\x21\x4c\x18", 4)) {
228+
input += 4;
229+
} else if (!memcmp (input, "\x04\x22\x4d\x18", 4)) {
230+
is_legacy = false;
231+
input += 4;
232+
ut8 flag = r_read_le8 (input);
233+
input += 2; // skip BD byte
234+
has_block_checksum = flag & 16;
235+
has_content_size = flag & 8;
236+
has_content_checksum = flag & 4;
237+
has_dictionary_id = flag & 1;
238+
if (has_content_size) {
239+
input += 8;
240+
}
241+
if (has_dictionary_id) {
223242
input += 4;
224-
continue;
225243
}
244+
input += 1; // skip header checksum
245+
}
246+
247+
const ut8 bytes_at_end_to_skip = has_content_checksum? 8: 4;
248+
while (input + bytes_at_end_to_skip < input_last) {
226249
ut32 comp_len = r_read_le32 (input);
250+
bool is_compressed = is_legacy || (comp_len & 0x80000000) == 0;
251+
if (!is_legacy) {
252+
comp_len &= 0x7FFFFFFF;
253+
}
227254
input += 4;
228255
int p;
229256
memcpy (g_buf + BLOCK_SIZE, input, comp_len);
230-
int error = lz4_decompress (g_buf, comp_len, &p);
231-
if (error != 0) {
232-
r_buf_free (b);
233-
return NULL;
257+
if (is_compressed) {
258+
int error = r_lz4_decompress_block (g_buf, comp_len, &p, NULL, 0);
259+
if (error != 0) {
260+
r_buf_free (b);
261+
return NULL;
262+
}
263+
} else {
264+
p = comp_len;
234265
}
235266
r_buf_write (b, g_buf, p);
236267
input += comp_len;
268+
269+
if (has_block_checksum) {
270+
input += 4;
271+
}
237272
}
273+
238274
ut64 osz;
239275
ut8 *res = r_buf_drain (b, &osz);
240276
if (output_size) {

Diff for: libr/util/zip.c

+18-53
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,11 @@
66
// set a maximum output buffer of 50MB
77
#define MAXOUT 50000000
88

9-
#if USE_SMALLZ4
10-
#include "../../../shlr/smallz4/smallz4cat.h"
11-
12-
struct UserPtr {
13-
const ut8 * input;
14-
ut64 inputPos;
15-
ut8 *output;
16-
ut64 outputPos;
17-
ut32 *outputSize;
18-
int error;
19-
};
20-
21-
static void smallz4Write(const unsigned char* data, unsigned int numBytes, void *userPtr) {
22-
struct UserPtr* user = (struct UserPtr*)userPtr;
23-
if (data != NULL && numBytes > 0) {
24-
if (*(user->outputSize) - user->outputPos < numBytes) {
25-
user->error = -1;
26-
return;
27-
}
28-
memcpy(user->output + user->outputPos, data, numBytes);
29-
user->outputPos += numBytes;
30-
}
31-
}
9+
#ifndef USE_RLZ4
10+
#define USE_RLZ4 0
11+
#endif
3212

33-
static ut8 smallz4GetByte(void *userPtr) {
34-
struct UserPtr* user = (struct UserPtr*)userPtr;
35-
return *(user->input + (user->inputPos++));
36-
}
37-
#else
13+
#if !USE_RLZ4
3814
#include <lz4.h>
3915
#endif
4016

@@ -116,28 +92,22 @@ static ut8 *inflatew(const ut8 *src, int srcLen, int *consumed, int *dstLen, int
11692
R_API ut8 *r_inflate_lz4(const ut8 *src, int srcLen, R_NULLABLE int *consumed, int *dstLen) {
11793
R_RETURN_VAL_IF_FAIL (src && dstLen, NULL);
11894
ut32 osz = srcLen * 5;
95+
int pp = 0;
96+
11997
ut8 *obuf = calloc (srcLen, 5);
12098
if (!obuf) {
12199
return NULL;
122100
}
123101

124-
#if USE_SMALLZ4
125-
struct UserPtr user = {
126-
.input = src,
127-
.inputPos = 0,
128-
.output = obuf,
129-
.outputPos = 0,
130-
.outputSize = &osz,
131-
.error = 0
132-
};
133-
int res = unlz4Block_userPtr (smallz4GetByte, smallz4Write, &user, srcLen, NULL, NULL);
134-
if (res < 1 || user.error != 0)
102+
#if USE_RLZ4
103+
int res = r_lz4_decompress_block ((ut8 *)src, srcLen, &pp, obuf, osz);
104+
if (res < 0)
135105
#else
136106
int res = LZ4_decompress_safe ((const char*)src, (char*)obuf, (uint32_t) srcLen, (uint32_t) osz);
137107
if (res < 1)
138108
#endif
139109
{
140-
const int mul = srcLen / -res;
110+
const int mul = USE_RLZ4? 1: srcLen / -res;
141111
const int nosz = osz * (5 * (mul + 1));
142112
if (nosz < osz) {
143113
free (obuf);
@@ -150,24 +120,19 @@ R_API ut8 *r_inflate_lz4(const ut8 *src, int srcLen, R_NULLABLE int *consumed, i
150120
}
151121
obuf = nbuf;
152122
osz = nosz;
153-
#if USE_SMALLZ4
154-
user.output = obuf;
155-
user.inputPos = 0;
156-
user.outputPos = 0;
157-
user.error = 0;
158-
res = unlz4Block_userPtr (smallz4GetByte, smallz4Write, &user, srcLen, NULL, NULL);
159-
}
160-
user.output = NULL;
161-
user.input = NULL;
123+
#if USE_RLZ4
124+
res = r_lz4_decompress_block ((ut8 *)src, srcLen, &pp, obuf, osz);
162125
#else
163-
}
164-
res = LZ4_decompress_safe ((const char*)src, (char*)obuf, (uint32_t) srcLen, (uint32_t) osz);
126+
res = LZ4_decompress_safe ((const char *)src, (char *)obuf, (uint32_t)srcLen, (uint32_t)osz);
165127
#endif
166-
if (res > 0) {
167-
*dstLen = res;
128+
}
129+
130+
if (USE_RLZ4? res == 0: res > 0) {
131+
*dstLen = USE_RLZ4? pp: res;
168132
*consumed = srcLen;
169133
return obuf;
170134
}
135+
171136
*dstLen = 0;
172137
*consumed = 0;
173138
free (obuf);

Diff for: meson.build

-1
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,6 @@ if get_option('local') and get_option('default_library') == 'shared'
586586
rpath_lib = '$ORIGIN'
587587
endif
588588

589-
subdir('shlr/smallz4')
590589
subdir('libr/util')
591590
subdir('libr/socket')
592591
subdir('libr/crypto')

Diff for: shlr/smallz4/Makefile

-10
This file was deleted.

0 commit comments

Comments
 (0)