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

Go to the source code of this file.

Enumerations

enum  { BLOCK_BYTES = 1024 * 8 }
 

Functions

size_t write_int (FILE *fp, int i)
 
size_t write_bin (FILE *fp, const void *array, size_t arrayBytes)
 
size_t read_int (FILE *fp, int *i)
 
size_t read_bin (FILE *fp, void *array, size_t arrayBytes)
 
void test_compress (FILE *outFp, FILE *inpFp)
 
void test_decompress (FILE *outFp, FILE *inpFp)
 
int compare (FILE *fp0, FILE *fp1)
 
int main (int argc, char *argv[])
 

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
BLOCK_BYTES 

Definition at line 16 of file blockStreaming_doubleBuffer.c.

16  {
17  BLOCK_BYTES = 1024 * 8,
18 // BLOCK_BYTES = 1024 * 64,
19 };

Function Documentation

◆ compare()

int compare ( FILE *  fp0,
FILE *  fp1 
)

Definition at line 115 of file blockStreaming_doubleBuffer.c.

116 {
117  int result = 0;
118 
119  while(0 == result) {
120  char b0[65536];
121  char b1[65536];
122  const size_t r0 = read_bin(fp0, b0, sizeof(b0));
123  const size_t r1 = read_bin(fp1, b1, sizeof(b1));
124 
125  result = (int) r0 - (int) r1;
126 
127  if(0 == r0 || 0 == r1) {
128  break;
129  }
130  if(0 == result) {
131  result = memcmp(b0, b1, r0);
132  }
133  }
134 
135  return result;
136 }
size_t read_bin(FILE *fp, void *array, size_t arrayBytes)
static int
Definition: sfsocketcall.h:114

References b1, int, r0, r1, and read_bin().

Referenced by main().

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 139 of file blockStreaming_doubleBuffer.c.

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

◆ read_bin()

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

Definition at line 34 of file blockStreaming_doubleBuffer.c.

34  {
35  return fread(array, 1, arrayBytes, fp);
36 }

Referenced by compare(), test_compress(), and test_decompress().

◆ read_int()

size_t read_int ( FILE *  fp,
int i 
)

Definition at line 30 of file blockStreaming_doubleBuffer.c.

30  {
31  return fread(i, sizeof(*i), 1, fp);
32 }
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 39 of file blockStreaming_doubleBuffer.c.

40 {
41  LZ4_stream_t lz4Stream_body;
42  LZ4_stream_t* lz4Stream = &lz4Stream_body;
43 
44  char inpBuf[2][BLOCK_BYTES];
45  int inpBufIndex = 0;
46 
47  LZ4_initStream(lz4Stream, sizeof (*lz4Stream));
48 
49  for(;;) {
50  char* const inpPtr = inpBuf[inpBufIndex];
51  const int inpBytes = (int) read_bin(inpFp, inpPtr, BLOCK_BYTES);
52  if(0 == inpBytes) {
53  break;
54  }
55 
56  {
57  char cmpBuf[LZ4_COMPRESSBOUND(BLOCK_BYTES)];
58  const int cmpBytes = LZ4_compress_fast_continue(
59  lz4Stream, inpPtr, cmpBuf, inpBytes, sizeof(cmpBuf), 1);
60  if(cmpBytes <= 0) {
61  break;
62  }
63  write_int(outFp, cmpBytes);
64  write_bin(outFp, cmpBuf, (size_t) cmpBytes);
65  }
66 
67  inpBufIndex = (inpBufIndex + 1) % 2;
68  }
69 
70  write_int(outFp, 0);
71 }
size_t write_int(FILE *fp, int i)
size_t write_bin(FILE *fp, const void *array, size_t arrayBytes)
LZ4_stream_t * LZ4_initStream(void *buffer, size_t size)
Definition: lz4.c:1443
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
#define LZ4_COMPRESSBOUND(isize)
Definition: lz4.h:171

References BLOCK_BYTES, int, LZ4_compress_fast_continue(), LZ4_COMPRESSBOUND, LZ4_initStream(), read_bin(), write_bin(), and write_int().

Referenced by main().

◆ test_decompress()

void test_decompress ( FILE *  outFp,
FILE *  inpFp 
)

Definition at line 74 of file blockStreaming_doubleBuffer.c.

75 {
76  LZ4_streamDecode_t lz4StreamDecode_body;
77  LZ4_streamDecode_t* lz4StreamDecode = &lz4StreamDecode_body;
78 
79  char decBuf[2][BLOCK_BYTES];
80  int decBufIndex = 0;
81 
82  LZ4_setStreamDecode(lz4StreamDecode, NULL, 0);
83 
84  for(;;) {
85  char cmpBuf[LZ4_COMPRESSBOUND(BLOCK_BYTES)];
86  int cmpBytes = 0;
87 
88  {
89  const size_t readCount0 = read_int(inpFp, &cmpBytes);
90  if(readCount0 != 1 || cmpBytes <= 0) {
91  break;
92  }
93 
94  const size_t readCount1 = read_bin(inpFp, cmpBuf, (size_t) cmpBytes);
95  if(readCount1 != (size_t) cmpBytes) {
96  break;
97  }
98  }
99 
100  {
101  char* const decPtr = decBuf[decBufIndex];
102  const int decBytes = LZ4_decompress_safe_continue(
103  lz4StreamDecode, cmpBuf, decPtr, cmpBytes, BLOCK_BYTES);
104  if(decBytes <= 0) {
105  break;
106  }
107  write_bin(outFp, decPtr, (size_t) decBytes);
108  }
109 
110  decBufIndex = (decBufIndex + 1) % 2;
111  }
112 }
size_t read_int(FILE *fp, int *i)
#define NULL
Definition: cris-opc.c:27
int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode, const char *dictionary, int dictSize)
Definition: lz4.c:2285
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 BLOCK_BYTES, LZ4_COMPRESSBOUND, LZ4_decompress_safe_continue(), LZ4_setStreamDecode(), NULL, read_bin(), read_int(), and write_bin().

Referenced by main().

◆ write_bin()

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

Definition at line 26 of file blockStreaming_doubleBuffer.c.

26  {
27  return fwrite(array, 1, arrayBytes, fp);
28 }

Referenced by test_compress(), and test_decompress().

◆ write_int()

size_t write_int ( FILE *  fp,
int  i 
)

Definition at line 22 of file blockStreaming_doubleBuffer.c.

22  {
23  return fwrite(&i, sizeof(i), 1, fp);
24 }

References i.

Referenced by test_compress().