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

Tests BCJ decoding when the output size is known. More...

#include "tests.h"

Go to the source code of this file.

Functions

static void compress (void)
 
static void decompress (void)
 
static void decompress_empty (void)
 
int main (void)
 

Variables

static const uint8_t in [16] = "0123456789ABCDEF"
 Something to be compressed. More...
 
static uint8_t compressed [1024]
 in[] after compression More...
 
static size_t compressed_size = 0
 
static uint8_t out [sizeof(in)]
 Output buffer for decompressing compressed[]. More...
 

Detailed Description

Tests BCJ decoding when the output size is known.

These tests fail with XZ Utils 5.0.3 and earlier.

Definition in file test_bcj_exact_size.c.

Function Documentation

◆ compress()

static void compress ( void  )
static

Definition at line 30 of file test_bcj_exact_size.c.

31 {
32  // Compress with PowerPC BCJ and LZMA2. PowerPC BCJ is used because
33  // it has fixed 4-byte alignment which makes triggering the potential
34  // bug easy.
35  lzma_options_lzma opt_lzma2;
36  succeed(lzma_lzma_preset(&opt_lzma2, 0));
37 
38  lzma_filter filters[3] = {
39  { .id = LZMA_FILTER_POWERPC, .options = NULL },
40  { .id = LZMA_FILTER_LZMA2, .options = &opt_lzma2 },
41  { .id = LZMA_VLI_UNKNOWN, .options = NULL },
42  };
43 
44  expect(lzma_stream_buffer_encode(filters, LZMA_CHECK_CRC32, NULL,
45  in, sizeof(in),
47  == LZMA_OK);
48 }
@ LZMA_CHECK_CRC32
Definition: check.h:35
#define LZMA_FILTER_POWERPC
Definition: bcj.h:27
const lzma_filter * filters
Definition: container.h:315
#define NULL
Definition: cris-opc.c:27
#define expect(expr, value)
Definition: lz4.c:170
#define LZMA_FILTER_LZMA2
LZMA2 Filter ID.
Definition: lzma12.h:40
Filter options.
Definition: filter.h:43
lzma_vli id
Filter ID.
Definition: filter.h:54
Options specific to the LZMA1 and LZMA2 filters.
Definition: lzma12.h:185
static size_t compressed_size
static const uint8_t in[16]
Something to be compressed.
static uint8_t compressed[1024]
in[] after compression
#define succeed(test)
Definition: tests.h:27
#define LZMA_VLI_UNKNOWN
VLI value to denote that the value is unknown.
Definition: vli.h:39
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58

References compressed, compressed_size, expect, filters, lzma_filter::id, in, LZMA_CHECK_CRC32, LZMA_FILTER_LZMA2, LZMA_FILTER_POWERPC, LZMA_OK, LZMA_VLI_UNKNOWN, NULL, and succeed.

Referenced by main().

◆ decompress()

static void decompress ( void  )
static

Definition at line 52 of file test_bcj_exact_size.c.

53 {
55  expect(lzma_stream_decoder(&strm, 10 << 20, 0) == LZMA_OK);
56 
58  strm.next_out = out;
59 
60  while (true) {
62  strm.avail_in = 1;
63 
64  const lzma_ret ret = lzma_code(&strm, LZMA_RUN);
65  if (ret == LZMA_STREAM_END) {
67  expect(strm.total_out == sizeof(in));
68  lzma_end(&strm);
69  return;
70  }
71 
72  expect(ret == LZMA_OK);
73 
74  if (strm.total_out < sizeof(in))
75  strm.avail_out = 1;
76  }
77 }
static lzma_stream strm
Definition: full_flush.c:20
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
static uint8_t out[sizeof(in)]
Output buffer for decompressing compressed[].
lzma_ret
Return values used by several functions in liblzma.
Definition: base.h:57
@ LZMA_STREAM_END
End of stream was reached.
Definition: base.h:63
@ LZMA_RUN
Continue coding.
Definition: base.h:251
#define LZMA_STREAM_INIT
Initialization for lzma_stream.
Definition: base.h:545

References lzma_stream::avail_in, lzma_stream::avail_out, compressed, compressed_size, expect, in, LZMA_OK, LZMA_RUN, LZMA_STREAM_END, LZMA_STREAM_INIT, lzma_stream::next_in, lzma_stream::next_out, out, strm, lzma_stream::total_in, and lzma_stream::total_out.

Referenced by main().

◆ decompress_empty()

static void decompress_empty ( void  )
static

Definition at line 81 of file test_bcj_exact_size.c.

82 {
83  // An empty file with one Block using PowerPC BCJ and LZMA2.
84  static const uint8_t empty_bcj_lzma2[] = {
85  0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00, 0x00, 0x01,
86  0x69, 0x22, 0xDE, 0x36, 0x02, 0x01, 0x05, 0x00,
87  0x21, 0x01, 0x00, 0x00, 0x7F, 0xE0, 0xF1, 0xC8,
88  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
89  0x00, 0x01, 0x11, 0x00, 0x3B, 0x96, 0x5F, 0x73,
90  0x90, 0x42, 0x99, 0x0D, 0x01, 0x00, 0x00, 0x00,
91  0x00, 0x01, 0x59, 0x5A
92  };
93 
94  // Decompress without giving any output space.
95  uint64_t memlimit = 1 << 20;
96  size_t in_pos = 0;
97  size_t out_pos = 0;
98  expect(lzma_stream_buffer_decode(&memlimit, 0, NULL,
99  empty_bcj_lzma2, &in_pos, sizeof(empty_bcj_lzma2),
100  out, &out_pos, 0) == LZMA_OK);
101  expect(in_pos == sizeof(empty_bcj_lzma2));
102  expect(out_pos == 0);
103 }
const lzma_allocator const uint8_t size_t uint8_t size_t * out_pos
Definition: block.h:528
const lzma_allocator const uint8_t size_t * in_pos
Definition: block.h:579
uint64_t memlimit
Definition: container.h:537
unsigned long uint64_t
Definition: sftypes.h:28
unsigned char uint8_t
Definition: sftypes.h:31

References expect, in_pos, LZMA_OK, memlimit, NULL, out, and out_pos.

Referenced by main().

◆ main()

int main ( void  )

Definition at line 107 of file test_bcj_exact_size.c.

108 {
109  compress();
110  decompress();
112  return 0;
113 }
static void compress(void)
static void decompress(void)
static void decompress_empty(void)

References compress(), decompress(), and decompress_empty().

Variable Documentation

◆ compressed

uint8_t compressed[1024]
static

in[] after compression

Definition at line 22 of file test_bcj_exact_size.c.

Referenced by compress(), decompress(), and uncompressed_name().

◆ compressed_size

size_t compressed_size = 0
static

Definition at line 23 of file test_bcj_exact_size.c.

Referenced by compress(), and decompress().

◆ in

const uint8_t in[16] = "0123456789ABCDEF"
static

Something to be compressed.

Definition at line 19 of file test_bcj_exact_size.c.

Referenced by compress(), and decompress().

◆ out

uint8_t out[sizeof(in)]
static

Output buffer for decompressing compressed[].

Definition at line 26 of file test_bcj_exact_size.c.

Referenced by decompress(), and decompress_empty().