Skip to content

Commit ecb3cfe

Browse files
committed
fixed tornado on uncompressible data
1 parent dba470a commit ecb3cfe

File tree

4 files changed

+27
-56
lines changed

4 files changed

+27
-56
lines changed

_lzbench/compressors.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -760,12 +760,12 @@ int64_t lzbench_snappy_decompress(char *inbuf, size_t insize, char *outbuf, size
760760

761761
int64_t lzbench_tornado_compress(char *inbuf, size_t insize, char *outbuf, size_t outsize, size_t level, size_t, size_t)
762762
{
763-
return tor_compress(level, (uint8_t*)inbuf, (uint8_t*)outbuf, insize);
763+
return tor_compress(level, (uint8_t*)inbuf, insize, (uint8_t*)outbuf, outsize);
764764
}
765765

766766
int64_t lzbench_tornado_decompress(char *inbuf, size_t insize, char *outbuf, size_t outsize, size_t, size_t, size_t)
767767
{
768-
return tor_decompress((uint8_t*)inbuf, (uint8_t*)outbuf, insize);
768+
return tor_decompress((uint8_t*)inbuf, insize, (uint8_t*)outbuf, outsize);
769769
}
770770

771771
#endif

tornado/license.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This code is provided on the GPL license.
2+
If you need a commercial license to use the code, please write to [email protected]

tornado/tor_test.cpp

+15-52
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ int compress_all_at_once = 0;
2828

2929
struct Results
3030
{
31-
uint32_t inlen, outlen;
32-
uint32_t inpos;
31+
uint32_t inpos, inlen, outpos, outlen;
3332
uint8_t *inbuf, *outbuf;
3433
};
3534

@@ -43,7 +42,7 @@ int ReadWriteCallback (const char *what, void *buf, int size, void *r_)
4342

4443
if (strequ(what,"init")) {
4544

46-
r.inpos = r.outlen = 0;
45+
r.inpos = r.outpos = 0;
4746
return FREEARC_OK;
4847

4948
} else if (strequ(what,"read")) {
@@ -56,8 +55,10 @@ int ReadWriteCallback (const char *what, void *buf, int size, void *r_)
5655

5756
} else if (strequ(what,"write") || strequ(what,"quasiwrite")) {
5857
if (strequ(what,"write")) {
59-
memcpy(r.outbuf+r.outlen, buf, size);
60-
r.outlen += size;
58+
if (r.outpos + size > r.outlen)
59+
return 0;
60+
memcpy(r.outbuf+r.outpos, buf, size);
61+
r.outpos += size;
6162
return size;
6263
}
6364

@@ -71,46 +72,6 @@ int ReadWriteCallback (const char *what, void *buf, int size, void *r_)
7172
}
7273

7374

74-
// #define CHUNK_SIZE 10240
75-
// GetTime(start_ticks);
76-
// ReadWriteCallback("init", NULL, 0, &r);
77-
// ArithCoder<EOB_CODE> ari(ReadWriteCallback, &r, CHUNK_SIZE, CHUNK_SIZE, 256);
78-
// for (int i=0; i<size; i++)
79-
// {
80-
// if (i%CHUNK_SIZE == 0)
81-
// ari.flush();
82-
// ari.encode(inbuf[i]);
83-
// }
84-
// outlen = r.outlen;
85-
// Print_Time("ArithCoder", &ticksPerSecond, &start_ticks, size, outlen);
86-
//
87-
//
88-
// GetTime(start_ticks);
89-
// ReadWriteCallback("init", NULL, 0, &r);
90-
// HuffmanEncoder<EOB_CODE> huff(ReadWriteCallback, &r, CHUNK_SIZE, CHUNK_SIZE, 256);
91-
// for (int i=0; i<size; i++)
92-
// {
93-
// if (i%CHUNK_SIZE == 0)
94-
// huff.flush();
95-
// huff.encode(inbuf[i]);
96-
// }
97-
// outlen = r.outlen;
98-
// Print_Time("HuffmanEncoder", &ticksPerSecond, &start_ticks, size, outlen);
99-
//
100-
//
101-
// GetTime(start_ticks);
102-
// ReadWriteCallback("init", NULL, 0, &r);
103-
// HuffmanEncoderOrder1<256, EOB_CODE> huff1(ReadWriteCallback, &r, CHUNK_SIZE, CHUNK_SIZE, 256);
104-
// for (int i=0; i<size; i++)
105-
// {
106-
// if (i%CHUNK_SIZE == 0)
107-
// huff1.flush();
108-
// huff1.encode(inbuf[i-1], inbuf[i]);
109-
// }
110-
// outlen = r.outlen;
111-
// Print_Time("HuffmanEncoderO1", &ticksPerSecond, &start_ticks, size, outlen);
112-
113-
11475

11576
PackMethod second_Tornado_method[] =
11677
// tables row hashsize matchfinder buffer parser hash3 shift update auxhash fast_bytes
@@ -124,14 +85,15 @@ PackMethod second_Tornado_method[] =
12485
, { 7, BITCODER, false, 1, 4*mb, NON_CACHING_MF, 32*mb, GREEDY, 0, 0, 999, 0, 0, 128 }
12586
};
12687

127-
128-
uint32_t tor_compress(uint8_t method, uint8_t* inbuf, uint8_t* outbuf, uint32_t size)
88+
89+
uint32_t tor_compress(uint8_t method, uint8_t* inbuf, uint32_t inlen, uint8_t* outbuf, uint32_t outlen)
12990
{
13091
PackMethod m;
13192
static Results r;
13293
r.inbuf = inbuf;
13394
r.outbuf = outbuf;
134-
r.inlen = size;
95+
r.inlen = inlen;
96+
r.outlen = outlen;
13597

13698
ReadWriteCallback ("init", NULL, 0, &r);
13799
if (method >= 20)
@@ -142,18 +104,19 @@ uint32_t tor_compress(uint8_t method, uint8_t* inbuf, uint8_t* outbuf, uint32_t
142104
if (r.inlen >= 0)
143105
m.buffer = mymin (m.buffer, r.inlen+LOOKAHEAD*2);
144106
int result = tor_compress (m, ReadWriteCallback, &r, NULL, -1);
145-
return r.outlen;
107+
return r.outpos;
146108
}
147109

148-
uint32_t tor_decompress(uint8_t* inbuf, uint8_t* outbuf, uint32_t size)
110+
uint32_t tor_decompress(uint8_t* inbuf, uint32_t inlen, uint8_t* outbuf, uint32_t outlen)
149111
{
150112
static Results r;
151113
r.inbuf = inbuf;
152114
r.outbuf = outbuf;
153-
r.inlen = size;
115+
r.inlen = inlen;
116+
r.outlen = outlen;
154117

155118
ReadWriteCallback ("init", NULL, 0, &r);
156119
int result = tor_decompress(ReadWriteCallback, &r, NULL, -1);
157120
ReadWriteCallback ("done", NULL, 0, &r);
158-
return r.outlen;
121+
return r.outpos;
159122
}

tornado/tor_test.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1-
uint32_t tor_compress(uint8_t method, uint8_t* inbuf, uint8_t* outbuf, uint32_t size);
2-
uint32_t tor_decompress(uint8_t* inbuf, uint8_t* outbuf, uint32_t size);
1+
#ifndef TOR_TEST_H
2+
#define TOR_TEST_H
3+
4+
uint32_t tor_decompress(uint8_t* inbuf, uint32_t inlen, uint8_t* outbuf, uint32_t outlen);
5+
uint32_t tor_compress(uint8_t method, uint8_t* inbuf, uint32_t inlen, uint8_t* outbuf, uint32_t outlen);
6+
7+
#endif
8+
39

0 commit comments

Comments
 (0)