Rizin
unix-like reverse engineering framework and cli tools
checkFrame.c File Reference
#include "util.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include "lz4frame.h"
#include "lz4.h"
#include "xxhash.h"

Go to the source code of this file.

Classes

struct  cRess_t
 

Macros

#define LZ4F_STATIC_LINKING_ONLY
 
#define XXH_STATIC_LINKING_ONLY
 
#define KB   *(1U<<10)
 
#define MB   *(1U<<20)
 
#define GB   *(1U<<30)
 
#define DISPLAY(...)   fprintf(stderr, __VA_ARGS__)
 
#define DISPLAYLEVEL(l, ...)   if (displayLevel>=l) { DISPLAY(__VA_ARGS__); }
 
#define DEBUG   0
 
#define DEBUGOUTPUT(...)   if (DEBUG) DISPLAY(__VA_ARGS__);
 
#define EXM_THROW(error, ...)
 
#define MIN(a, b)   ( (a) < (b) ? (a) : (b) )
 
#define MAX(a, b)   ( (a) > (b) ? (a) : (b) )
 

Functions

static int createCResources (cRess_t *ress)
 
static void freeCResources (cRess_t ress)
 
int frameCheck (cRess_t ress, FILE *const srcFile, unsigned bsid, size_t blockSize)
 
int FUZ_usage (const char *programName)
 
int main (int argc, const char **argv)
 

Variables

static U32 no_prompt = 0
 
static U32 displayLevel = 2
 
static U32 use_pause = 0
 

Macro Definition Documentation

◆ DEBUG

#define DEBUG   0

Definition at line 63 of file checkFrame.c.

◆ DEBUGOUTPUT

#define DEBUGOUTPUT (   ...)    if (DEBUG) DISPLAY(__VA_ARGS__);

Definition at line 65 of file checkFrame.c.

◆ DISPLAY

#define DISPLAY (   ...)    fprintf(stderr, __VA_ARGS__)

Definition at line 56 of file checkFrame.c.

◆ DISPLAYLEVEL

#define DISPLAYLEVEL (   l,
  ... 
)    if (displayLevel>=l) { DISPLAY(__VA_ARGS__); }

Definition at line 57 of file checkFrame.c.

◆ EXM_THROW

#define EXM_THROW (   error,
  ... 
)
Value:
{ \
DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
DISPLAYLEVEL(1, "Error %i : ", error); \
DISPLAYLEVEL(1, __VA_ARGS__); \
DISPLAYLEVEL(1, " \n"); \
return(error); \
}
void error(const char *msg)
Definition: untgz.c:593

Definition at line 66 of file checkFrame.c.

◆ GB

#define GB   *(1U<<30)

Definition at line 50 of file checkFrame.c.

◆ KB

#define KB   *(1U<<10)

Definition at line 48 of file checkFrame.c.

◆ LZ4F_STATIC_LINKING_ONLY

#define LZ4F_STATIC_LINKING_ONLY

Definition at line 37 of file checkFrame.c.

◆ MAX

#define MAX (   a,
  b 
)    ( (a) > (b) ? (a) : (b) )

Definition at line 89 of file checkFrame.c.

◆ MB

#define MB   *(1U<<20)

Definition at line 49 of file checkFrame.c.

◆ MIN

#define MIN (   a,
  b 
)    ( (a) < (b) ? (a) : (b) )

Definition at line 88 of file checkFrame.c.

◆ XXH_STATIC_LINKING_ONLY

#define XXH_STATIC_LINKING_ONLY

Definition at line 41 of file checkFrame.c.

Function Documentation

◆ createCResources()

static int createCResources ( cRess_t ress)
static

Definition at line 99 of file checkFrame.c.

100 {
101  ress->srcBufferSize = 4 MB;
102  ress->srcBuffer = malloc(ress->srcBufferSize);
103  ress->dstBufferSize = 4 MB;
104  ress->dstBuffer = malloc(ress->dstBufferSize);
105 
106  if (!ress->srcBuffer || !ress->dstBuffer) {
107  free(ress->srcBuffer);
108  free(ress->dstBuffer);
109  EXM_THROW(20, "Allocation error : not enough memory");
110  }
111 
113  free(ress->srcBuffer);
114  free(ress->dstBuffer);
115  EXM_THROW(21, "Unable to create decompression context");
116  }
117  return 0;
118 }
#define EXM_THROW(error,...)
Definition: checkFrame.c:66
#define MB
Definition: checkFrame.c:49
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
void * malloc(size_t size)
Definition: malloc.c:123
unsigned LZ4F_isError(LZ4F_errorCode_t code)
Definition: lz4frame.c:249
LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_dctx **LZ4F_decompressionContextPtr, unsigned versionNumber)
Definition: lz4frame.c:1069
#define LZ4F_VERSION
Definition: lz4frame.h:242
void * srcBuffer
Definition: lz4io.c:535
void * dstBuffer
Definition: lz4io.c:537
size_t dstBufferSize
Definition: lz4io.c:538
LZ4F_compressionContext_t ctx
Definition: lz4io.c:539
size_t srcBufferSize
Definition: lz4io.c:536

References cRess_t::ctx, cRess_t::dstBuffer, cRess_t::dstBufferSize, EXM_THROW, free(), LZ4F_createDecompressionContext(), LZ4F_isError(), LZ4F_VERSION, malloc(), MB, cRess_t::srcBuffer, and cRess_t::srcBufferSize.

Referenced by main().

◆ frameCheck()

int frameCheck ( cRess_t  ress,
FILE *const  srcFile,
unsigned  bsid,
size_t  blockSize 
)

Definition at line 128 of file checkFrame.c.

129 {
130  LZ4F_errorCode_t nextToLoad = 0;
131  size_t curblocksize = 0;
132  int partialBlock = 0;
133 
134  /* Main Loop */
135  for (;;) {
136  size_t readSize;
137  size_t pos = 0;
138  size_t decodedBytes = ress.dstBufferSize;
139  size_t remaining;
140  LZ4F_frameInfo_t frameInfo;
141 
142  /* Read input */
143  readSize = fread(ress.srcBuffer, 1, ress.srcBufferSize, srcFile);
144  if (!readSize) break; /* reached end of file or stream */
145 
146  while (pos < readSize) { /* still to read */
147  /* Decode Input (at least partially) */
148  if (!nextToLoad) {
149  /* LZ4F_decompress returned 0 : starting new frame */
150  curblocksize = 0;
151  remaining = readSize - pos;
152  nextToLoad = LZ4F_getFrameInfo(ress.ctx, &frameInfo, (char*)(ress.srcBuffer)+pos, &remaining);
153  if (LZ4F_isError(nextToLoad))
154  EXM_THROW(22, "Error getting frame info: %s",
155  LZ4F_getErrorName(nextToLoad));
156  if (frameInfo.blockSizeID != bsid)
157  EXM_THROW(23, "Block size ID %u != expected %u",
158  frameInfo.blockSizeID, bsid);
159  pos += remaining;
160  /* nextToLoad should be block header size */
161  remaining = nextToLoad;
162  decodedBytes = ress.dstBufferSize;
163  nextToLoad = LZ4F_decompress(ress.ctx, ress.dstBuffer, &decodedBytes, (char*)(ress.srcBuffer)+pos, &remaining, NULL);
164  if (LZ4F_isError(nextToLoad)) EXM_THROW(24, "Decompression error : %s", LZ4F_getErrorName(nextToLoad));
165  pos += remaining;
166  }
167  decodedBytes = ress.dstBufferSize;
168  /* nextToLoad should be just enough to cover the next block */
169  if (nextToLoad > (readSize - pos)) {
170  /* block is not fully contained in current buffer */
171  partialBlock = 1;
172  remaining = readSize - pos;
173  } else {
174  if (partialBlock) {
175  partialBlock = 0;
176  }
177  remaining = nextToLoad;
178  }
179  nextToLoad = LZ4F_decompress(ress.ctx, ress.dstBuffer, &decodedBytes, (char*)(ress.srcBuffer)+pos, &remaining, NULL);
180  if (LZ4F_isError(nextToLoad)) EXM_THROW(24, "Decompression error : %s", LZ4F_getErrorName(nextToLoad));
181  curblocksize += decodedBytes;
182  pos += remaining;
183  if (!partialBlock) {
184  /* detect small block due to end of frame; the final 4-byte frame checksum could be left in the buffer */
185  if ((curblocksize != 0) && (nextToLoad > 4)) {
186  if (curblocksize != blockSize)
187  EXM_THROW(25, "Block size %u != expected %u, pos %u\n",
188  (unsigned)curblocksize, (unsigned)blockSize, (unsigned)pos);
189  }
190  curblocksize = 0;
191  }
192  }
193  }
194  /* can be out because readSize == 0, which could be an fread() error */
195  if (ferror(srcFile)) EXM_THROW(26, "Read error");
196 
197  if (nextToLoad!=0) EXM_THROW(27, "Unfinished stream");
198 
199  return 0;
200 }
#define NULL
Definition: cris-opc.c:27
size_t LZ4F_decompress(LZ4F_dctx *dctx, void *dstBuffer, size_t *dstSizePtr, const void *srcBuffer, size_t *srcSizePtr, const LZ4F_decompressOptions_t *decompressOptionsPtr)
Definition: lz4frame.c:1384
LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_dctx *dctx, LZ4F_frameInfo_t *frameInfoPtr, const void *srcBuffer, size_t *srcSizePtr)
Definition: lz4frame.c:1253
const char * LZ4F_getErrorName(LZ4F_errorCode_t code)
Definition: lz4frame.c:254
size_t LZ4F_errorCode_t
Definition: lz4frame.h:103
LZ4F_blockSizeID_t blockSizeID
Definition: lz4frame.h:176
int pos
Definition: main.c:11

References LZ4F_frameInfo_t::blockSizeID, cRess_t::ctx, cRess_t::dstBuffer, cRess_t::dstBufferSize, EXM_THROW, LZ4F_decompress(), LZ4F_getErrorName(), LZ4F_getFrameInfo(), LZ4F_isError(), NULL, pos, cRess_t::srcBuffer, and cRess_t::srcBufferSize.

Referenced by main().

◆ freeCResources()

static void freeCResources ( cRess_t  ress)
static

Definition at line 120 of file checkFrame.c.

121 {
122  free(ress.srcBuffer);
123  free(ress.dstBuffer);
124 
125  (void) LZ4F_freeDecompressionContext(ress.ctx);
126 }
LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_dctx *dctx)
Definition: lz4frame.c:1082

References cRess_t::ctx, cRess_t::dstBuffer, free(), LZ4F_freeDecompressionContext(), and cRess_t::srcBuffer.

Referenced by main().

◆ FUZ_usage()

int FUZ_usage ( const char *  programName)

Definition at line 202 of file checkFrame.c.

203 {
204  DISPLAY( "Usage :\n");
205  DISPLAY( " %s [args] filename\n", programName);
206  DISPLAY( "\n");
207  DISPLAY( "Arguments :\n");
208  DISPLAY( " -b# : expected blocksizeID [4-7] (required)\n");
209  DISPLAY( " -B# : expected blocksize [32-4194304] (required)\n");
210  DISPLAY( " -v : verbose\n");
211  DISPLAY( " -h : display help and exit\n");
212  return 0;
213 }
#define DISPLAY(...)
Definition: checkFrame.c:56

References DISPLAY.

Referenced by main().

◆ main()

int main ( int  argc,
const char **  argv 
)

Definition at line 216 of file checkFrame.c.

217 {
218  int argNb;
219  unsigned bsid=0;
220  size_t blockSize=0;
221  const char* const programName = argv[0];
222 
223  /* Check command line */
224  for (argNb=1; argNb<argc; argNb++) {
225  const char* argument = argv[argNb];
226 
227  if(!argument) continue; /* Protection if argument empty */
228 
229  /* Decode command (note : aggregated short commands are allowed) */
230  if (argument[0]=='-') {
231  if (!strcmp(argument, "--no-prompt")) {
232  no_prompt=1;
233  displayLevel=1;
234  continue;
235  }
236  argument++;
237 
238  while (*argument!=0) {
239  switch(*argument)
240  {
241  case 'h':
242  return FUZ_usage(programName);
243  case 'v':
244  argument++;
245  displayLevel++;
246  break;
247  case 'q':
248  argument++;
249  displayLevel--;
250  break;
251  case 'p': /* pause at the end */
252  argument++;
253  use_pause = 1;
254  break;
255 
256  case 'b':
257  argument++;
258  bsid=0;
259  while ((*argument>='0') && (*argument<='9')) {
260  bsid *= 10;
261  bsid += (unsigned)(*argument - '0');
262  argument++;
263  }
264  break;
265 
266  case 'B':
267  argument++;
268  blockSize=0;
269  while ((*argument>='0') && (*argument<='9')) {
270  blockSize *= 10;
271  blockSize += (size_t)(*argument - '0');
272  argument++;
273  }
274  break;
275 
276  default:
277  ;
278  return FUZ_usage(programName);
279  }
280  }
281  } else {
282  int err;
283  FILE *srcFile;
284  cRess_t ress;
285  if (bsid == 0 || blockSize == 0)
286  return FUZ_usage(programName);
287  DISPLAY("Starting frame checker (%i-bits, %s)\n", (int)(sizeof(size_t)*8), LZ4_VERSION_STRING);
288  err = createCResources(&ress);
289  if (err) return (err);
290  srcFile = fopen(argument, "rb");
291  if ( srcFile==NULL ) {
292  freeCResources(ress);
293  EXM_THROW(1, "%s: %s \n", argument, strerror(errno));
294  }
295  assert (srcFile != NULL);
296  err = frameCheck(ress, srcFile, bsid, blockSize);
297  freeCResources(ress);
298  fclose(srcFile);
299  return (err);
300  }
301  }
302  return 0;
303 }
static bool err
Definition: armass.c:435
static U32 use_pause
Definition: checkFrame.c:82
int frameCheck(cRess_t ress, FILE *const srcFile, unsigned bsid, size_t blockSize)
Definition: checkFrame.c:128
static void freeCResources(cRess_t ress)
Definition: checkFrame.c:120
static U32 displayLevel
Definition: checkFrame.c:81
static int createCResources(cRess_t *ress)
Definition: checkFrame.c:99
int FUZ_usage(const char *programName)
Definition: checkFrame.c:202
static U32 no_prompt
Definition: checkFrame.c:80
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
static void struct sockaddr socklen_t static fromlen static backlog static fork char char char static envp int struct rusage static rusage struct utsname static buf struct sembuf unsigned
Definition: sflib.h:97
#define LZ4_VERSION_STRING
Definition: lz4.h:110
assert(limit<=UINT32_MAX/2)
string FILE
Definition: benchmark.py:21
int size_t
Definition: sftypes.h:40
Definition: lz4io.c:534

References argv, assert(), createCResources(), DISPLAY, displayLevel, err, EXM_THROW, benchmark::FILE, frameCheck(), freeCResources(), FUZ_usage(), LZ4_VERSION_STRING, no_prompt, NULL, unsigned, and use_pause.

Variable Documentation

◆ displayLevel

U32 displayLevel = 2
static

Definition at line 81 of file checkFrame.c.

Referenced by main().

◆ no_prompt

U32 no_prompt = 0
static

Definition at line 80 of file checkFrame.c.

Referenced by main().

◆ use_pause

U32 use_pause = 0
static

Definition at line 82 of file checkFrame.c.

Referenced by main().