1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
/*
checkFrame - verify frame headers
Copyright (C) Yann Collet 2014-2016
GPL v2 License
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
You can contact the author at :
- LZ4 homepage : http://www.lz4.org
- LZ4 source repository : https://github.com/lz4/lz4
*/
/*-************************************
* Compiler specific
**************************************/
#ifdef _MSC_VER /* Visual Studio */
# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
# pragma warning(disable : 4146) /* disable: C4146: minus unsigned expression */
#endif
/*-************************************
* Includes
**************************************/
#include "util.h" /* U32 */
#include <stdlib.h> /* malloc, free */
#include <stdio.h> /* fprintf */
#include <string.h> /* strcmp */
#include <time.h> /* clock_t, clock(), CLOCKS_PER_SEC */
#include <assert.h>
#include "lz4frame.h" /* include multiple times to test correctness/safety */
#include "lz4frame.h"
#define LZ4F_STATIC_LINKING_ONLY
#include "lz4frame.h"
#include "lz4frame.h"
#include "lz4.h" /* LZ4_VERSION_STRING */
#define XXH_STATIC_LINKING_ONLY
#include "xxhash.h" /* XXH64 */
/*-************************************
* Constants
**************************************/
#define KB *(1U<<10)
#define MB *(1U<<20)
#define GB *(1U<<30)
/*-************************************
* Macros
**************************************/
#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
#define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); }
/**************************************
* Exceptions
***************************************/
#ifndef DEBUG
# define DEBUG 0
#endif
#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__);
#define EXM_THROW(error, ...) \
{ \
DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
DISPLAYLEVEL(1, "Error %i : ", error); \
DISPLAYLEVEL(1, __VA_ARGS__); \
DISPLAYLEVEL(1, " \n"); \
return(error); \
}
/*-***************************************
* Local Parameters
*****************************************/
static U32 no_prompt = 0;
static U32 displayLevel = 2;
static U32 use_pause = 0;
/*-*******************************************************
* Fuzzer functions
*********************************************************/
#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
#define MAX(a,b) ( (a) > (b) ? (a) : (b) )
int frameCheck(FILE* const srcFile, unsigned bsid, size_t blockSize)
{
LZ4F_decompressionContext_t dctx = NULL;
const size_t srcBufferSize = 4 MB;
void* const srcBuffer = malloc(srcBufferSize);
const size_t dstBufferSize = 4 MB;
void* const dstBuffer = malloc(dstBufferSize);
LZ4F_errorCode_t nextToLoad = 0;
size_t curblocksize = 0;
int partialBlock = 0;
if (!srcBuffer || !dstBuffer) EXM_THROW(20, "Allocation error : not enough memory");
if (LZ4F_isError( LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION) )) {
EXM_THROW(21, "Unable to create decompression context");
}
/* Main Loop */
for (;;) {
size_t readSize;
size_t pos = 0;
size_t decodedBytes = dstBufferSize;
size_t remaining;
LZ4F_frameInfo_t frameInfo;
/* Read input */
readSize = fread(srcBuffer, 1, srcBufferSize, srcFile);
if (!readSize) break; /* reached end of file or stream */
while (pos < readSize) { /* still to read */
/* Decode Input (at least partially) */
if (!nextToLoad) {
/* LZ4F_decompress returned 0 : starting new frame */
curblocksize = 0;
remaining = readSize - pos;
nextToLoad = LZ4F_getFrameInfo(dctx, &frameInfo, (char*)(srcBuffer)+pos, &remaining);
if (LZ4F_isError(nextToLoad)) EXM_THROW(22, "Error getting frame info: %s", LZ4F_getErrorName(nextToLoad)); /* XXX */
if (frameInfo.blockSizeID != bsid) EXM_THROW(23, "Block size ID %u != expected %u", frameInfo.blockSizeID, bsid);
pos += remaining;
/* nextToLoad should be block header size */
remaining = nextToLoad;
decodedBytes = dstBufferSize;
nextToLoad = LZ4F_decompress(dctx, dstBuffer, &decodedBytes, (char*)(srcBuffer)+pos, &remaining, NULL);
if (LZ4F_isError(nextToLoad)) EXM_THROW(24, "Decompression error : %s", LZ4F_getErrorName(nextToLoad));
pos += remaining;
}
decodedBytes = dstBufferSize;
/* nextToLoad should be just enough to cover the next block */
if (nextToLoad > (readSize - pos)) {
/* block is not fully contained in current buffer */
partialBlock = 1;
remaining = readSize - pos;
} else {
if (partialBlock) {
partialBlock = 0;
}
remaining = nextToLoad;
}
nextToLoad = LZ4F_decompress(dctx, dstBuffer, &decodedBytes, (char*)(srcBuffer)+pos, &remaining, NULL);
if (LZ4F_isError(nextToLoad)) EXM_THROW(24, "Decompression error : %s", LZ4F_getErrorName(nextToLoad));
curblocksize += decodedBytes;
pos += remaining;
if (!partialBlock) {
/* detect small block due to end of frame; the final 4-byte frame checksum could be left in the buffer */
if ((curblocksize != 0) && (nextToLoad > 4)) {
if (curblocksize != blockSize)
EXM_THROW(25, "Block size %zu != expected %zu, pos %zu\n", curblocksize, blockSize, pos);
}
curblocksize = 0;
}
}
}
/* can be out because readSize == 0, which could be an fread() error */
if (ferror(srcFile)) EXM_THROW(26, "Read error");
if (nextToLoad!=0) EXM_THROW(27, "Unfinished stream");
return 0;
}
int FUZ_usage(const char* programName)
{
DISPLAY( "Usage :\n");
DISPLAY( " %s [args] filename\n", programName);
DISPLAY( "\n");
DISPLAY( "Arguments :\n");
DISPLAY( " -b# : expected blocksizeID [4-7] (required)\n");
DISPLAY( " -B# : expected blocksize [32-4194304] (required)\n");
DISPLAY( " -v : verbose\n");
DISPLAY( " -h : display help and exit\n");
return 0;
}
int main(int argc, const char** argv)
{
int argNb;
int err;
int bsid=0;
size_t blockSize=0;
const char* const programName = argv[0];
/* Check command line */
for (argNb=1; argNb<argc; argNb++) {
const char* argument = argv[argNb];
if(!argument) continue; /* Protection if argument empty */
/* Decode command (note : aggregated short commands are allowed) */
if (argument[0]=='-') {
if (!strcmp(argument, "--no-prompt")) {
no_prompt=1;
displayLevel=1;
continue;
}
argument++;
while (*argument!=0) {
switch(*argument)
{
case 'h':
return FUZ_usage(programName);
case 'v':
argument++;
displayLevel++;
break;
case 'q':
argument++;
displayLevel--;
break;
case 'p': /* pause at the end */
argument++;
use_pause = 1;
break;
case 'b':
argument++;
bsid=0;
while ((*argument>='0') && (*argument<='9')) {
bsid *= 10;
bsid += *argument - '0';
argument++;
}
break;
case 'B':
argument++;
blockSize=0;
while ((*argument>='0') && (*argument<='9')) {
blockSize *= 10;
blockSize += *argument - '0';
argument++;
}
break;
default:
;
return FUZ_usage(programName);
}
}
} else {
FILE *srcFile;
if (bsid == 0 || blockSize == 0)
return FUZ_usage(programName);
DISPLAY("Starting frame checker (%i-bits, %s)\n", (int)(sizeof(size_t)*8), LZ4_VERSION_STRING);
srcFile = fopen(argument, "rb");
if ( srcFile==NULL ) EXM_THROW(1, "%s: %s \n", argument, strerror(errno));
err = frameCheck(srcFile, bsid, blockSize);
fclose(srcFile);
return (err);
}
}
return 0;
}
|