Rizin
unix-like reverse engineering framework and cli tools
HCStreaming_ringBuffer.c File Reference
#include "lz4hc.h"
#include "lz4.h"
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

Go to the source code of this file.

Macros

#define GCC_VERSION   (__GNUC__ * 100 + __GNUC_MINOR__)
 
#define CMPBUFSIZE   (LZ4_COMPRESSBOUND(MESSAGE_MAX_BYTES))
 

Enumerations

enum  { MESSAGE_MAX_BYTES = 1024 , RING_BUFFER_BYTES = 1024 * 8 + MESSAGE_MAX_BYTES , DEC_BUFFER_BYTES = RING_BUFFER_BYTES + MESSAGE_MAX_BYTES }
 

Functions

size_t write_int32 (FILE *fp, int32_t i)
 
size_t write_bin (FILE *fp, const void *array, int arrayBytes)
 
size_t read_int32 (FILE *fp, int32_t *i)
 
size_t read_bin (FILE *fp, void *array, int arrayBytes)
 
void test_compress (FILE *outFp, FILE *inpFp)
 
void test_decompress (FILE *outFp, FILE *inpFp)
 
size_t compare (FILE *f0, FILE *f1)
 
int main (int argc, const char **argv)
 

Macro Definition Documentation

◆ CMPBUFSIZE

#define CMPBUFSIZE   (LZ4_COMPRESSBOUND(MESSAGE_MAX_BYTES))

◆ GCC_VERSION

#define GCC_VERSION   (__GNUC__ * 100 + __GNUC_MINOR__)

Definition at line 13 of file HCStreaming_ringBuffer.c.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
MESSAGE_MAX_BYTES 
RING_BUFFER_BYTES 
DEC_BUFFER_BYTES 

Definition at line 31 of file HCStreaming_ringBuffer.c.

31  {
32  MESSAGE_MAX_BYTES = 1024,
34  DEC_BUFFER_BYTES = RING_BUFFER_BYTES + MESSAGE_MAX_BYTES // Intentionally larger to test unsynchronized ring buffers
35 };
@ DEC_BUFFER_BYTES
@ RING_BUFFER_BYTES
@ MESSAGE_MAX_BYTES

Function Documentation

◆ compare()

size_t compare ( FILE *  f0,
FILE *  f1 
)

Definition at line 133 of file HCStreaming_ringBuffer.c.

134 {
135  size_t result = 1;
136 
137  for (;;) {
138  char b0[65536];
139  char b1[65536];
140  const size_t r0 = fread(b0, 1, sizeof(b0), f0);
141  const size_t r1 = fread(b1, 1, sizeof(b1), f1);
142 
143  if ((r0==0) && (r1==0)) return 0; // success
144 
145  if (r0 != r1) {
146  size_t smallest = r0;
147  if (r1<r0) smallest = r1;
148  result += smallest;
149  break;
150  }
151 
152  if (memcmp(b0, b1, r0)) {
153  unsigned errorPos = 0;
154  while ((errorPos < r0) && (b0[errorPos]==b1[errorPos])) errorPos++;
155  result += errorPos;
156  break;
157  }
158 
159  result += sizeof(b0);
160  }
161 
162  return result;
163 }

References b1, f0, f1, r0, and r1.

Referenced by main().

◆ main()

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

Definition at line 166 of file HCStreaming_ringBuffer.c.

167 {
168  char inpFilename[256] = { 0 };
169  char lz4Filename[256] = { 0 };
170  char decFilename[256] = { 0 };
171  unsigned fileID = 1;
172  unsigned pause = 0;
173 
174 
175  if(argc < 2) {
176  printf("Please specify input filename\n");
177  return 0;
178  }
179 
180  if (!strcmp(argv[1], "-p")) { pause = 1; fileID = 2; }
181 
182  snprintf(inpFilename, 256, "%s", argv[fileID]);
183  snprintf(lz4Filename, 256, "%s.lz4s-%d", argv[fileID], 9);
184  snprintf(decFilename, 256, "%s.lz4s-%d.dec", argv[fileID], 9);
185 
186  printf("input = [%s]\n", inpFilename);
187  printf("lz4 = [%s]\n", lz4Filename);
188  printf("decoded = [%s]\n", decFilename);
189 
190  // compress
191  { FILE* const inpFp = fopen(inpFilename, "rb");
192  FILE* const outFp = fopen(lz4Filename, "wb");
193 
194  test_compress(outFp, inpFp);
195 
196  fclose(outFp);
197  fclose(inpFp);
198  }
199 
200  // decompress
201  { FILE* const inpFp = fopen(lz4Filename, "rb");
202  FILE* const outFp = fopen(decFilename, "wb");
203 
204  test_decompress(outFp, inpFp);
205 
206  fclose(outFp);
207  fclose(inpFp);
208  }
209 
210  // verify
211  { FILE* const inpFp = fopen(inpFilename, "rb");
212  FILE* const decFp = fopen(decFilename, "rb");
213 
214  const size_t cmp = compare(inpFp, decFp);
215  if(0 == cmp) {
216  printf("Verify : OK\n");
217  } else {
218  printf("Verify : NG : error at pos %u\n", (unsigned)cmp-1);
219  }
220 
221  fclose(decFp);
222  fclose(inpFp);
223  }
224 
225  if (pause) {
226  int unused;
227  printf("Press enter to continue ...\n");
228  unused = getchar(); (void)unused; /* silence static analyzer */
229  }
230 
231  return 0;
232 }
void test_compress(FILE *outFp, FILE *inpFp)
void test_decompress(FILE *outFp, FILE *inpFp)
size_t compare(FILE *f0, FILE *f1)
static RzILOpEffect * cmp(cs_insn *insn, bool is_thumb)
Definition: arm_il32.c:942
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
snprintf
Definition: kernel.h:364
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
string FILE
Definition: benchmark.py:21

References argv, cmp(), compare(), benchmark::FILE, printf(), snprintf, test_compress(), and test_decompress().

◆ read_bin()

size_t read_bin ( FILE *  fp,
void *  array,
int  arrayBytes 
)

Definition at line 51 of file HCStreaming_ringBuffer.c.

51  {
52  assert(arrayBytes >= 0);
53  return fread(array, 1, (size_t)arrayBytes, fp);
54 }
assert(limit<=UINT32_MAX/2)

References assert().

Referenced by test_compress(), and test_decompress().

◆ read_int32()

size_t read_int32 ( FILE *  fp,
int32_t i 
)

Definition at line 47 of file HCStreaming_ringBuffer.c.

47  {
48  return fread(i, sizeof(*i), 1, fp);
49 }
lzma_index ** i
Definition: index.h:629

References i.

Referenced by test_decompress().

◆ test_compress()

void test_compress ( FILE *  outFp,
FILE *  inpFp 
)

Definition at line 57 of file HCStreaming_ringBuffer.c.

58 {
59  LZ4_streamHC_t lz4Stream_body = { 0 };
60  LZ4_streamHC_t* lz4Stream = &lz4Stream_body;
61 
62  static char inpBuf[RING_BUFFER_BYTES];
63  int inpOffset = 0;
64 
65  for(;;) {
66  // Read random length ([1,MESSAGE_MAX_BYTES]) data to the ring buffer.
67  char* const inpPtr = &inpBuf[inpOffset];
68  const int randomLength = (rand() % MESSAGE_MAX_BYTES) + 1;
69  const int inpBytes = (int) read_bin(inpFp, inpPtr, randomLength);
70  if (0 == inpBytes) break;
71 
72 #define CMPBUFSIZE (LZ4_COMPRESSBOUND(MESSAGE_MAX_BYTES))
73  { char cmpBuf[CMPBUFSIZE];
74  const int cmpBytes = LZ4_compress_HC_continue(lz4Stream, inpPtr, cmpBuf, inpBytes, CMPBUFSIZE);
75 
76  if(cmpBytes <= 0) break;
77  write_int32(outFp, cmpBytes);
78  write_bin(outFp, cmpBuf, cmpBytes);
79 
80  inpOffset += inpBytes;
81 
82  // Wraparound the ringbuffer offset
83  if(inpOffset >= RING_BUFFER_BYTES - MESSAGE_MAX_BYTES)
84  inpOffset = 0;
85  }
86  }
87 
88  write_int32(outFp, 0);
89 }
size_t write_int32(FILE *fp, int32_t i)
size_t write_bin(FILE *fp, const void *array, int arrayBytes)
size_t read_bin(FILE *fp, void *array, int arrayBytes)
#define CMPBUFSIZE
int LZ4_compress_HC_continue(LZ4_streamHC_t *LZ4_streamHCPtr, const char *src, char *dst, int srcSize, int dstCapacity)
Definition: lz4hc.c:1138
static int
Definition: sfsocketcall.h:114

References CMPBUFSIZE, int, LZ4_compress_HC_continue(), MESSAGE_MAX_BYTES, read_bin(), RING_BUFFER_BYTES, write_bin(), and write_int32().

Referenced by main().

◆ test_decompress()

void test_decompress ( FILE *  outFp,
FILE *  inpFp 
)

Definition at line 92 of file HCStreaming_ringBuffer.c.

93 {
94  static char decBuf[DEC_BUFFER_BYTES];
95  int decOffset = 0;
96  LZ4_streamDecode_t lz4StreamDecode_body = { 0 };
97  LZ4_streamDecode_t* lz4StreamDecode = &lz4StreamDecode_body;
98 
99  for(;;) {
100  int cmpBytes = 0;
101  char cmpBuf[CMPBUFSIZE];
102 
103  { const size_t r0 = read_int32(inpFp, &cmpBytes);
104  size_t r1;
105  if(r0 != 1 || cmpBytes <= 0)
106  break;
107 
108  r1 = read_bin(inpFp, cmpBuf, cmpBytes);
109  if(r1 != (size_t) cmpBytes)
110  break;
111  }
112 
113  { char* const decPtr = &decBuf[decOffset];
114  const int decBytes = LZ4_decompress_safe_continue(
115  lz4StreamDecode, cmpBuf, decPtr, cmpBytes, MESSAGE_MAX_BYTES);
116  if(decBytes <= 0)
117  break;
118 
119  decOffset += decBytes;
120  write_bin(outFp, decPtr, decBytes);
121 
122  // Wraparound the ringbuffer offset
123  if(decOffset >= DEC_BUFFER_BYTES - MESSAGE_MAX_BYTES)
124  decOffset = 0;
125  }
126  }
127 }
size_t read_int32(FILE *fp, int32_t *i)
LZ4_FORCE_O2 int LZ4_decompress_safe_continue(LZ4_streamDecode_t *LZ4_streamDecode, const char *source, char *dest, int compressedSize, int maxOutputSize)
Definition: lz4.c:2322

References CMPBUFSIZE, DEC_BUFFER_BYTES, LZ4_decompress_safe_continue(), MESSAGE_MAX_BYTES, r0, r1, read_bin(), read_int32(), and write_bin().

Referenced by main().

◆ write_bin()

size_t write_bin ( FILE *  fp,
const void *  array,
int  arrayBytes 
)

Definition at line 42 of file HCStreaming_ringBuffer.c.

42  {
43  assert(arrayBytes >= 0);
44  return fwrite(array, 1, (size_t)arrayBytes, fp);
45 }

References assert().

Referenced by test_compress(), and test_decompress().

◆ write_int32()

size_t write_int32 ( FILE *  fp,
int32_t  i 
)

Definition at line 38 of file HCStreaming_ringBuffer.c.

38  {
39  return fwrite(&i, sizeof(i), 1, fp);
40 }

References i.

Referenced by test_compress().