Rizin
unix-like reverse engineering framework and cli tools
create_compress_files.c File Reference

Creates bunch of test files to be compressed. More...

#include "sysdefs.h"
#include <stdio.h>

Go to the source code of this file.

Macros

#define create_test(name)
 

Functions

static bool file_exists (const char *filename)
 
static FILE * file_create (const char *filename)
 
static void file_finish (FILE *file, const char *filename)
 
static void write_abc (FILE *file)
 
static void write_random (FILE *file)
 
static void write_text (FILE *file)
 
int main (void)
 

Detailed Description

Creates bunch of test files to be compressed.

Using a test file generator program saves space in the source code package considerably.

Definition in file create_compress_files.c.

Macro Definition Documentation

◆ create_test

#define create_test (   name)
Value:
do { \
if (!file_exists("compress_generated_" #name)) { \
FILE *file = file_create("compress_generated_" #name); \
write_ ## name(file); \
file_finish(file, "compress_generated_" #name); \
} \
} while (0)
static bool file_exists(const char *filename)
static FILE * file_create(const char *filename)
const char * name
Definition: op.c:541
Definition: gzappend.c:170
Definition: z80asm.h:102

Definition at line 21 of file create_compress_files.c.

Function Documentation

◆ file_create()

static FILE* file_create ( const char *  filename)
static

Definition at line 50 of file create_compress_files.c.

51 {
52  FILE *file = fopen(filename, "wb");
53 
54  if (file == NULL) {
55  perror(filename);
56  exit(1);
57  }
58 
59  return file;
60 }
#define NULL
Definition: cris-opc.c:27
const char * filename
Definition: ioapi.h:137
string FILE
Definition: benchmark.py:21
static int file
Definition: z80asm.c:58

References test-lz4-list::exit, file, benchmark::FILE, and NULL.

◆ file_exists()

static bool file_exists ( const char *  filename)
static

Definition at line 32 of file create_compress_files.c.

33 {
34  // Trying to be somewhat portable by avoiding stat().
35  FILE *file = fopen(filename, "rb");
36  bool ret;
37 
38  if (file != NULL) {
39  fclose(file);
40  ret = true;
41  } else {
42  ret = false;
43  }
44 
45  return ret;
46 }

References benchmark::FILE, and NULL.

◆ file_finish()

static void file_finish ( FILE *  file,
const char *  filename 
)
static

Definition at line 64 of file create_compress_files.c.

65 {
66  const bool ferror_fail = ferror(file);
67  const bool fclose_fail = fclose(file);
68 
69  if (ferror_fail || fclose_fail) {
70  perror(filename);
71  exit(1);
72  }
73 }

References test-lz4-list::exit.

◆ main()

int main ( void  )

Definition at line 152 of file create_compress_files.c.

153 {
154  create_test(abc);
155  create_test(random);
156  create_test(text);
157  return 0;
158 }
#define create_test(name)

References create_test, and create_tags_rz::text.

◆ write_abc()

static void write_abc ( FILE *  file)
static

Definition at line 79 of file create_compress_files.c.

80 {
81  for (size_t i = 0; i < 12345; ++i)
82  if (fwrite("abc\n", 4, 1, file) != 1)
83  exit(1);
84 }
lzma_index ** i
Definition: index.h:629

References test-lz4-list::exit, and i.

◆ write_random()

static void write_random ( FILE *  file)
static

Definition at line 90 of file create_compress_files.c.

91 {
92  uint32_t n = 5;
93 
94  for (size_t i = 0; i < 123456; ++i) {
95  n = 101771 * n + 71777;
96 
97  putc((uint8_t)(n), file);
98  putc((uint8_t)(n >> 8), file);
99  putc((uint8_t)(n >> 16), file);
100  putc((uint8_t)(n >> 24), file);
101  }
102 }
int n
Definition: mipsasm.c:19
unsigned int uint32_t
Definition: sftypes.h:29
unsigned char uint8_t
Definition: sftypes.h:31

References i, and n.

◆ write_text()

static void write_text ( FILE *  file)
static

Definition at line 107 of file create_compress_files.c.

108 {
109  static const char *lorem[] = {
110  "Lorem", "ipsum", "dolor", "sit", "amet,", "consectetur",
111  "adipisicing", "elit,", "sed", "do", "eiusmod", "tempor",
112  "incididunt", "ut", "labore", "et", "dolore", "magna",
113  "aliqua.", "Ut", "enim", "ad", "minim", "veniam,", "quis",
114  "nostrud", "exercitation", "ullamco", "laboris", "nisi",
115  "ut", "aliquip", "ex", "ea", "commodo", "consequat.",
116  "Duis", "aute", "irure", "dolor", "in", "reprehenderit",
117  "in", "voluptate", "velit", "esse", "cillum", "dolore",
118  "eu", "fugiat", "nulla", "pariatur.", "Excepteur", "sint",
119  "occaecat", "cupidatat", "non", "proident,", "sunt", "in",
120  "culpa", "qui", "officia", "deserunt", "mollit", "anim",
121  "id", "est", "laborum."
122  };
123 
124  // Let the first paragraph be the original text.
125  for (size_t w = 0; w < ARRAY_SIZE(lorem); ++w) {
126  fprintf(file, "%s ", lorem[w]);
127 
128  if (w % 7 == 6)
129  fprintf(file, "\n");
130  }
131 
132  // The rest shall be (hopefully) meaningless combinations of
133  // the same words.
134  uint32_t n = 29;
135 
136  for (size_t p = 0; p < 500; ++p) {
137  fprintf(file, "\n\n");
138 
139  for (size_t w = 0; w < ARRAY_SIZE(lorem); ++w) {
140  n = 101771 * n + 71777;
141 
142  fprintf(file, "%s ", lorem[n % ARRAY_SIZE(lorem)]);
143 
144  if (w % 7 == 6)
145  fprintf(file, "\n");
146  }
147  }
148 }
#define ARRAY_SIZE(a)
#define w
Definition: crypto_rc6.c:13
void * p
Definition: libc.cpp:67

References ARRAY_SIZE, n, p, and w.