Rizin
unix-like reverse engineering framework and cli tools
tests.h
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 #ifndef LZMA_TESTS_H
14 #define LZMA_TESTS_H
15 
16 #include "sysdefs.h"
17 #include "tuklib_integer.h"
18 #include "lzma.h"
19 
20 #include <stdio.h>
21 
22 #define memcrap(buf, size) memset(buf, 0xFD, size)
23 
24 #define expect(test) ((test) ? 0 : (fprintf(stderr, "%s:%d: %s\n", \
25  __FILE__, __LINE__, #test), abort(), 0))
26 
27 #define succeed(test) expect(!(test))
28 
29 #define fail(test) expect(test)
30 
31 
32 static inline const char *
34 {
35  if ((unsigned int)(ret) > LZMA_PROG_ERROR)
36  return "UNKNOWN_ERROR";
37 
38  static const char *msgs[] = {
39  "LZMA_OK",
40  "LZMA_STREAM_END",
41  "LZMA_NO_CHECK",
42  "LZMA_UNSUPPORTED_CHECK",
43  "LZMA_GET_CHECK",
44  "LZMA_MEM_ERROR",
45  "LZMA_MEMLIMIT_ERROR",
46  "LZMA_FORMAT_ERROR",
47  "LZMA_OPTIONS_ERROR",
48  "LZMA_DATA_ERROR",
49  "LZMA_BUF_ERROR",
50  "LZMA_PROG_ERROR"
51  };
52 
53  return msgs[ret];
54 }
55 
56 
57 static inline bool
59  uint8_t *out, size_t out_size,
60  lzma_ret expected_ret, lzma_action finishing_action)
61 {
62  size_t in_left = in_size;
63  size_t out_left = out_size > 0 ? out_size + 1 : 0;
65  lzma_ret ret;
66 
67  strm->next_in = NULL;
68  strm->avail_in = 0;
69  strm->next_out = NULL;
70  strm->avail_out = 0;
71 
72  while (true) {
73  if (in_left > 0) {
74  if (--in_left == 0)
75  action = finishing_action;
76 
77  strm->next_in = in++;
78  strm->avail_in = 1;
79  }
80 
81  if (out_left > 0) {
82  --out_left;
83  strm->next_out = out++;
84  strm->avail_out = 1;
85  }
86 
87  ret = lzma_code(strm, action);
88  if (ret != LZMA_OK)
89  break;
90  }
91 
92  bool error = false;
93 
94  if (ret != expected_ret)
95  error = true;
96 
97  if (strm->total_in != in_size || strm->total_out != out_size)
98  error = true;
99 
100  return error;
101 }
102 
103 
104 static inline bool
106  lzma_ret expected_ret)
107 {
108  return coder_loop(strm, in, in_size, NULL, 0, expected_ret, LZMA_RUN);
109 }
110 
111 
112 static inline bool
114 {
115  return coder_loop(strm, in, in_size, NULL, 0,
117 }
118 
119 #endif
const lzma_allocator const uint8_t size_t in_size
Definition: block.h:527
const lzma_allocator const uint8_t * in
Definition: block.h:527
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
#define NULL
Definition: cris-opc.c:27
static lzma_stream strm
Definition: full_flush.c:20
The public API of liblzma data compression library.
unsigned char uint8_t
Definition: sftypes.h:31
Passing data to and from liblzma.
Definition: base.h:485
uint8_t * next_out
Definition: base.h:490
uint64_t total_in
Definition: base.h:488
size_t avail_out
Definition: base.h:491
const uint8_t * next_in
Definition: base.h:486
uint64_t total_out
Definition: base.h:492
size_t avail_in
Definition: base.h:487
Common includes, definitions, system-specific things etc.
static bool coder_loop(lzma_stream *strm, uint8_t *in, size_t in_size, uint8_t *out, size_t out_size, lzma_ret expected_ret, lzma_action finishing_action)
Definition: tests.h:58
static bool decoder_loop_ret(lzma_stream *strm, uint8_t *in, size_t in_size, lzma_ret expected_ret)
Definition: tests.h:105
static const char * lzma_ret_sym(lzma_ret ret)
Definition: tests.h:33
static bool decoder_loop(lzma_stream *strm, uint8_t *in, size_t in_size)
Definition: tests.h:113
Various integer and bit operations.
void error(const char *msg)
Definition: untgz.c:593
lzma_ret
Return values used by several functions in liblzma.
Definition: base.h:57
@ LZMA_PROG_ERROR
Programming error.
Definition: base.h:218
@ LZMA_STREAM_END
End of stream was reached.
Definition: base.h:63
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58
lzma_action
The ‘action’ argument for lzma_code()
Definition: base.h:250
@ LZMA_RUN
Continue coding.
Definition: base.h:251