Rizin
unix-like reverse engineering framework and cli tools
test_filter_flags.c
Go to the documentation of this file.
1 //
5 //
6 // Author: Lasse Collin
7 //
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
10 //
12 
13 #include "tests.h"
14 
15 
16 static uint8_t buffer[4096];
20 
21 
22 static bool
23 encode(uint32_t known_size)
24 {
25  memcrap(buffer, sizeof(buffer));
26 
27  uint32_t tmp;
28  if (lzma_filter_flags_size(&tmp, &known_flags) != LZMA_OK)
29  return true;
30 
31  if (tmp != known_size)
32  return true;
33 
34  size_t out_pos = 0;
35  if (lzma_filter_flags_encode(&known_flags,
36  buffer, &out_pos, known_size) != LZMA_OK)
37  return true;
38 
39  if (out_pos != known_size)
40  return true;
41 
42  return false;
43 }
44 
45 
46 static bool
47 decode_ret(uint32_t known_size, lzma_ret expected_ret)
48 {
50 
51  size_t pos = 0;
52  if (lzma_filter_flags_decode(&decoded_flags, NULL,
53  buffer, &pos, known_size) != expected_ret
54  || pos != known_size)
55  return true;
56 
57  return false;
58 }
59 
60 
61 static bool
62 decode(uint32_t known_size)
63 {
64  if (decode_ret(known_size, LZMA_OK))
65  return true;
66 
68  return true;
69 
70  return false;
71 }
72 
73 
74 #if defined(HAVE_ENCODER_X86) && defined(HAVE_DECODER_X86)
75 static void
76 test_bcj(void)
77 {
78  // Test 1
81 
82  expect(!encode(2));
83  expect(!decode(2));
85 
86  // Test 2
88  options.start_offset = 0;
90  expect(!encode(2));
91  expect(!decode(2));
93 
94  // Test 3
95  options.start_offset = 123456;
97  expect(!encode(6));
98  expect(!decode(6));
100 
102  expect(decoded->start_offset == options.start_offset);
103 
104  free(decoded);
105 }
106 #endif
107 
108 
109 #if defined(HAVE_ENCODER_DELTA) && defined(HAVE_DECODER_DELTA)
110 static void
111 test_delta(void)
112 {
113  // Test 1
116  expect(encode(99));
117 
118  // Test 2
120  .type = LZMA_DELTA_TYPE_BYTE,
121  .dist = 0
122  };
124  expect(encode(99));
125 
126  // Test 3
128  expect(!encode(3));
129  expect(!decode(3));
131  == options.dist);
132 
134 
135  // Test 4
137  expect(!encode(3));
138  expect(!decode(3));
140  == options.dist);
141 
143 
144  // Test 5
145  options.dist = LZMA_DELTA_DIST_MAX + 1;
146  expect(encode(99));
147 }
148 #endif
149 
150 /*
151 #ifdef HAVE_FILTER_LZMA
152 static void
153 validate_lzma(void)
154 {
155  const lzma_options_lzma *known = known_flags.options;
156  const lzma_options_lzma *decoded = decoded_flags.options;
157 
158  expect(known->dictionary_size <= decoded->dictionary_size);
159 
160  if (known->dictionary_size == 1)
161  expect(decoded->dictionary_size == 1);
162  else
163  expect(known->dictionary_size + known->dictionary_size / 2
164  > decoded->dictionary_size);
165 
166  expect(known->literal_context_bits == decoded->literal_context_bits);
167  expect(known->literal_pos_bits == decoded->literal_pos_bits);
168  expect(known->pos_bits == decoded->pos_bits);
169 }
170 
171 
172 static void
173 test_lzma(void)
174 {
175  // Test 1
176  known_flags.id = LZMA_FILTER_LZMA1;
177  known_flags.options = NULL;
178  expect(encode(99));
179 
180  // Test 2
181  lzma_options_lzma options = {
182  .dictionary_size = 0,
183  .literal_context_bits = 0,
184  .literal_pos_bits = 0,
185  .pos_bits = 0,
186  .preset_dictionary = NULL,
187  .preset_dictionary_size = 0,
188  .mode = LZMA_MODE_INVALID,
189  .fast_bytes = 0,
190  .match_finder = LZMA_MF_INVALID,
191  .match_finder_cycles = 0,
192  };
193 
194  // Test 3 (empty dictionary not allowed)
195  known_flags.options = &options;
196  expect(encode(99));
197 
198  // Test 4 (brute-force test some valid dictionary sizes)
199  options.dictionary_size = LZMA_DICTIONARY_SIZE_MIN;
200  while (options.dictionary_size != LZMA_DICTIONARY_SIZE_MAX) {
201  if (++options.dictionary_size == 5000)
202  options.dictionary_size = LZMA_DICTIONARY_SIZE_MAX - 5;
203 
204  expect(!encode(4));
205  expect(!decode(4));
206  validate_lzma();
207 
208  free(decoded_flags.options);
209  }
210 
211  // Test 5 (too big dictionary size)
212  options.dictionary_size = LZMA_DICTIONARY_SIZE_MAX + 1;
213  expect(encode(99));
214 
215  // Test 6 (brute-force test lc/lp/pb)
216  options.dictionary_size = LZMA_DICTIONARY_SIZE_MIN;
217  for (uint32_t lc = LZMA_LITERAL_CONTEXT_BITS_MIN;
218  lc <= LZMA_LITERAL_CONTEXT_BITS_MAX; ++lc) {
219  for (uint32_t lp = LZMA_LITERAL_POS_BITS_MIN;
220  lp <= LZMA_LITERAL_POS_BITS_MAX; ++lp) {
221  for (uint32_t pb = LZMA_POS_BITS_MIN;
222  pb <= LZMA_POS_BITS_MAX; ++pb) {
223  if (lc + lp > LZMA_LITERAL_BITS_MAX)
224  continue;
225 
226  options.literal_context_bits = lc;
227  options.literal_pos_bits = lp;
228  options.pos_bits = pb;
229 
230  expect(!encode(4));
231  expect(!decode(4));
232  validate_lzma();
233 
234  free(decoded_flags.options);
235  }
236  }
237  }
238 }
239 #endif
240 */
241 
242 int
243 main(void)
244 {
245 #if defined(HAVE_ENCODER_X86) && defined(HAVE_DECODER_X86)
246  test_bcj();
247 #endif
248 #if defined(HAVE_ENCODER_DELTA) && defined(HAVE_DECODER_DELTA)
249  test_delta();
250 #endif
251 // #ifdef HAVE_FILTER_LZMA
252 // test_lzma();
253 // #endif
254 
255  lzma_end(&strm);
256 
257  return 0;
258 }
#define LZMA_FILTER_X86
Definition: bcj.h:22
const lzma_allocator const uint8_t size_t uint8_t size_t * out_pos
Definition: block.h:528
#define NULL
Definition: cris-opc.c:27
@ LZMA_DELTA_TYPE_BYTE
Definition: delta.h:36
#define LZMA_DELTA_DIST_MIN
Definition: delta.h:60
#define LZMA_FILTER_DELTA
Filter ID.
Definition: delta.h:25
#define LZMA_DELTA_DIST_MAX
Definition: delta.h:61
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
static const char struct stat static buf struct stat static buf static vhangup int options
Definition: sflib.h:145
#define expect(expr, value)
Definition: lz4.c:170
unsigned int uint32_t
Definition: sftypes.h:29
unsigned char uint8_t
Definition: sftypes.h:31
Definition: buffer.h:15
Filter options.
Definition: filter.h:43
void * options
Pointer to filter-specific options structure.
Definition: filter.h:63
lzma_vli id
Filter ID.
Definition: filter.h:54
Options for BCJ filters.
Definition: bcj.h:73
uint32_t start_offset
Start offset for conversions.
Definition: bcj.h:88
Options for the Delta filter.
Definition: delta.h:45
Passing data to and from liblzma.
Definition: base.h:485
int pos
Definition: main.c:11
static lzma_stream strm
static bool decode_ret(uint32_t known_size, lzma_ret expected_ret)
static lzma_filter decoded_flags
static bool encode(uint32_t known_size)
int main(void)
static lzma_filter known_flags
static bool decode(uint32_t known_size)
Common definitions for test applications.
#define memcrap(buf, size)
Definition: tests.h:22
lzma_ret
Return values used by several functions in liblzma.
Definition: base.h:57
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58
#define LZMA_STREAM_INIT
Initialization for lzma_stream.
Definition: base.h:545