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

Go to the source code of this file.

Macros

#define CMPBUFSIZE   (LZ4_COMPRESSBOUND(MESSAGE_MAX_BYTES))
 

Enumerations

enum  { MESSAGE_MAX_BYTES = 1024 , RING_BUFFER_BYTES = 1024 * 8 + MESSAGE_MAX_BYTES , DECODE_RING_BUFFER = 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)
 
int compare (FILE *f0, FILE *f1)
 
int main (int argc, char **argv)
 

Macro Definition Documentation

◆ CMPBUFSIZE

#define CMPBUFSIZE   (LZ4_COMPRESSBOUND(MESSAGE_MAX_BYTES))

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
MESSAGE_MAX_BYTES 
RING_BUFFER_BYTES 
DECODE_RING_BUFFER 

Definition at line 24 of file blockStreaming_ringBuffer.c.

24  {
25  MESSAGE_MAX_BYTES = 1024,
27  DECODE_RING_BUFFER = RING_BUFFER_BYTES + MESSAGE_MAX_BYTES /* Intentionally larger, to test unsynchronized ring buffers */
28 };
@ DECODE_RING_BUFFER

Function Documentation

◆ compare()

int compare ( FILE *  f0,
FILE *  f1 
)

Definition at line 114 of file blockStreaming_ringBuffer.c.

115 {
116  int result = 0;
117 
118  while (0 == result) {
119  char b0[65536];
120  char b1[65536];
121  const size_t r0 = fread(b0, 1, sizeof(b0), f0);
122  const size_t r1 = fread(b1, 1, sizeof(b1), f1);
123 
124  result = (int) r0 - (int) r1;
125 
126  if (0 == r0 || 0 == r1) break;
127 
128  if (0 == result) result = memcmp(b0, b1, r0);
129  }
130 
131  return result;
132 }
static int
Definition: sfsocketcall.h:114

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

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 135 of file blockStreaming_ringBuffer.c.

136 {
137  char inpFilename[256] = { 0 };
138  char lz4Filename[256] = { 0 };
139  char decFilename[256] = { 0 };
140 
141  if (argc < 2) {
142  printf("Please specify input filename\n");
143  return 0;
144  }
145 
146  snprintf(inpFilename, 256, "%s", argv[1]);
147  snprintf(lz4Filename, 256, "%s.lz4s-%d", argv[1], 0);
148  snprintf(decFilename, 256, "%s.lz4s-%d.dec", argv[1], 0);
149 
150  printf("inp = [%s]\n", inpFilename);
151  printf("lz4 = [%s]\n", lz4Filename);
152  printf("dec = [%s]\n", decFilename);
153 
154  // compress
155  { FILE* const inpFp = fopen(inpFilename, "rb");
156  FILE* const outFp = fopen(lz4Filename, "wb");
157 
158  test_compress(outFp, inpFp);
159 
160  fclose(outFp);
161  fclose(inpFp);
162  }
163 
164  // decompress
165  { FILE* const inpFp = fopen(lz4Filename, "rb");
166  FILE* const outFp = fopen(decFilename, "wb");
167 
168  test_decompress(outFp, inpFp);
169 
170  fclose(outFp);
171  fclose(inpFp);
172  }
173 
174  // verify
175  { FILE* const inpFp = fopen(inpFilename, "rb");
176  FILE* const decFp = fopen(decFilename, "rb");
177 
178  const int cmp = compare(inpFp, decFp);
179  if (0 == cmp) {
180  printf("Verify : OK\n");
181  } else {
182  printf("Verify : NG\n");
183  }
184 
185  fclose(decFp);
186  fclose(inpFp);
187  }
188 
189  return 0;
190 }
static RzILOpEffect * cmp(cs_insn *insn, bool is_thumb)
Definition: arm_il32.c:942
int compare(FILE *f0, FILE *f1)
void test_compress(FILE *outFp, FILE *inpFp)
void test_decompress(FILE *outFp, FILE *inpFp)
_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 43 of file blockStreaming_ringBuffer.c.

43  {
44  return fread(array, 1, arrayBytes, fp);
45 }

Referenced by test_compress(), and test_decompress().

◆ read_int32()

size_t read_int32 ( FILE *  fp,
int32_t i 
)

Definition at line 39 of file blockStreaming_ringBuffer.c.

39  {
40  return fread(i, sizeof(*i), 1, fp);
41 }
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 48 of file blockStreaming_ringBuffer.c.

49 {
50  LZ4_stream_t lz4Stream_body = { { 0 } };
51  LZ4_stream_t* lz4Stream = &lz4Stream_body;
52 
53  static char inpBuf[RING_BUFFER_BYTES];
54  int inpOffset = 0;
55 
56  for(;;) {
57  // Read random length ([1,MESSAGE_MAX_BYTES]) data to the ring buffer.
58  char* const inpPtr = &inpBuf[inpOffset];
59  const int randomLength = (rand() % MESSAGE_MAX_BYTES) + 1;
60  const int inpBytes = (int) read_bin(inpFp, inpPtr, randomLength);
61  if (0 == inpBytes) break;
62 
63  {
64 #define CMPBUFSIZE (LZ4_COMPRESSBOUND(MESSAGE_MAX_BYTES))
65  char cmpBuf[CMPBUFSIZE];
66  const int cmpBytes = LZ4_compress_fast_continue(lz4Stream, inpPtr, cmpBuf, inpBytes, CMPBUFSIZE, 0);
67  if(cmpBytes <= 0) break;
68  write_int32(outFp, cmpBytes);
69  write_bin(outFp, cmpBuf, cmpBytes);
70 
71  inpOffset += inpBytes;
72 
73  // Wraparound the ringbuffer offset
74  if(inpOffset >= RING_BUFFER_BYTES - MESSAGE_MAX_BYTES) inpOffset = 0;
75  }
76  }
77 
78  write_int32(outFp, 0);
79 }
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_fast_continue(LZ4_stream_t *LZ4_stream, const char *source, char *dest, int inputSize, int maxOutputSize, int acceleration)
Definition: lz4.c:1565

References CMPBUFSIZE, int, LZ4_compress_fast_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 82 of file blockStreaming_ringBuffer.c.

83 {
84  static char decBuf[DECODE_RING_BUFFER];
85  int decOffset = 0;
86  LZ4_streamDecode_t lz4StreamDecode_body = { { 0 } };
87  LZ4_streamDecode_t* lz4StreamDecode = &lz4StreamDecode_body;
88 
89  for(;;) {
90  int cmpBytes = 0;
91  char cmpBuf[CMPBUFSIZE];
92 
93  { const size_t r0 = read_int32(inpFp, &cmpBytes);
94  if(r0 != 1 || cmpBytes <= 0) break;
95 
96  const size_t r1 = read_bin(inpFp, cmpBuf, cmpBytes);
97  if(r1 != (size_t) cmpBytes) break;
98  }
99 
100  { char* const decPtr = &decBuf[decOffset];
101  const int decBytes = LZ4_decompress_safe_continue(
102  lz4StreamDecode, cmpBuf, decPtr, cmpBytes, MESSAGE_MAX_BYTES);
103  if(decBytes <= 0) break;
104  decOffset += decBytes;
105  write_bin(outFp, decPtr, decBytes);
106 
107  // Wraparound the ringbuffer offset
108  if(decOffset >= DECODE_RING_BUFFER - MESSAGE_MAX_BYTES) decOffset = 0;
109  }
110  }
111 }
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, DECODE_RING_BUFFER, 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 35 of file blockStreaming_ringBuffer.c.

35  {
36  return fwrite(array, 1, arrayBytes, fp);
37 }

Referenced by test_compress(), and test_decompress().

◆ write_int32()

size_t write_int32 ( FILE *  fp,
int32_t  i 
)

Definition at line 31 of file blockStreaming_ringBuffer.c.

31  {
32  return fwrite(&i, sizeof(i), 1, fp);
33 }

References i.

Referenced by test_compress().