checkFrame.c 10 KB

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