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