1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file test_block_header.c
4 /// \brief Tests Block Header coders
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
16 static uint8_t buf
[LZMA_BLOCK_HEADER_SIZE_MAX
];
17 static lzma_block known_options
;
18 static lzma_block decoded_options
;
20 static lzma_options_lzma opt_lzma
;
22 static lzma_filter filters_none
[1] = {
24 .id
= LZMA_VLI_UNKNOWN
,
29 static lzma_filter filters_one
[2] = {
31 .id
= LZMA_FILTER_LZMA2
,
34 .id
= LZMA_VLI_UNKNOWN
,
39 static lzma_filter filters_four
[5] = {
41 .id
= LZMA_FILTER_X86
,
44 .id
= LZMA_FILTER_X86
,
47 .id
= LZMA_FILTER_X86
,
50 .id
= LZMA_FILTER_LZMA2
,
53 .id
= LZMA_VLI_UNKNOWN
,
58 static lzma_filter filters_five
[6] = {
60 .id
= LZMA_FILTER_X86
,
63 .id
= LZMA_FILTER_X86
,
66 .id
= LZMA_FILTER_X86
,
69 .id
= LZMA_FILTER_X86
,
72 .id
= LZMA_FILTER_LZMA2
,
75 .id
= LZMA_VLI_UNKNOWN
,
83 expect(lzma_block_header_encode(&known_options
, buf
) == LZMA_OK
);
85 lzma_filter filters
[LZMA_FILTERS_MAX
+ 1];
86 memcrap(filters
, sizeof(filters
));
87 memcrap(&decoded_options
, sizeof(decoded_options
));
89 decoded_options
.header_size
= known_options
.header_size
;
90 decoded_options
.check
= known_options
.check
;
91 decoded_options
.filters
= filters
;
92 expect(lzma_block_header_decode(&decoded_options
, NULL
, buf
)
95 expect(known_options
.compressed_size
96 == decoded_options
.compressed_size
);
97 expect(known_options
.uncompressed_size
98 == decoded_options
.uncompressed_size
);
100 for (size_t i
= 0; known_options
.filters
[i
].id
101 != LZMA_VLI_UNKNOWN
; ++i
)
102 expect(known_options
.filters
[i
].id
== filters
[i
].id
);
104 for (size_t i
= 0; i
< LZMA_FILTERS_MAX
; ++i
)
105 free(decoded_options
.filters
[i
].options
);
112 known_options
= (lzma_block
){
113 .check
= LZMA_CHECK_NONE
,
114 .compressed_size
= LZMA_VLI_UNKNOWN
,
115 .uncompressed_size
= LZMA_VLI_UNKNOWN
,
119 expect(lzma_block_header_size(&known_options
) == LZMA_PROG_ERROR
);
121 known_options
.filters
= filters_none
;
122 expect(lzma_block_header_size(&known_options
) == LZMA_PROG_ERROR
);
124 known_options
.filters
= filters_five
;
125 expect(lzma_block_header_size(&known_options
) == LZMA_PROG_ERROR
);
127 known_options
.filters
= filters_one
;
128 expect(lzma_block_header_size(&known_options
) == LZMA_OK
);
130 known_options
.check
= 999; // Some invalid value, which gets ignored.
131 expect(lzma_block_header_size(&known_options
) == LZMA_OK
);
133 known_options
.compressed_size
= 5;
134 expect(lzma_block_header_size(&known_options
) == LZMA_OK
);
136 known_options
.compressed_size
= 0; // Cannot be zero.
137 expect(lzma_block_header_size(&known_options
) == LZMA_PROG_ERROR
);
139 // LZMA_VLI_MAX is too big to keep the total size of the Block
140 // a valid VLI, but lzma_block_header_size() is not meant
141 // to validate it. (lzma_block_header_encode() must validate it.)
142 known_options
.compressed_size
= LZMA_VLI_MAX
;
143 expect(lzma_block_header_size(&known_options
) == LZMA_OK
);
145 known_options
.compressed_size
= LZMA_VLI_UNKNOWN
;
146 known_options
.uncompressed_size
= 0;
147 expect(lzma_block_header_size(&known_options
) == LZMA_OK
);
149 known_options
.uncompressed_size
= LZMA_VLI_MAX
+ 1;
150 expect(lzma_block_header_size(&known_options
) == LZMA_PROG_ERROR
);
157 known_options
= (lzma_block
){
158 .check
= LZMA_CHECK_CRC32
,
159 .compressed_size
= LZMA_VLI_UNKNOWN
,
160 .uncompressed_size
= LZMA_VLI_UNKNOWN
,
161 .filters
= filters_four
,
164 expect(lzma_block_header_size(&known_options
) == LZMA_OK
);
167 known_options
.compressed_size
= 123456;
168 known_options
.uncompressed_size
= 234567;
169 expect(lzma_block_header_size(&known_options
) == LZMA_OK
);
172 // We can make the sizes smaller while keeping the header size
174 known_options
.compressed_size
= 12;
175 known_options
.uncompressed_size
= 23;
183 known_options
= (lzma_block
){
184 .check
= LZMA_CHECK_CRC32
,
185 .compressed_size
= LZMA_VLI_UNKNOWN
,
186 .uncompressed_size
= LZMA_VLI_UNKNOWN
,
187 .filters
= filters_one
,
190 expect(lzma_block_header_size(&known_options
) == LZMA_OK
);
191 known_options
.header_size
+= 4;
192 expect(lzma_block_header_encode(&known_options
, buf
) == LZMA_OK
);
194 lzma_filter filters
[LZMA_FILTERS_MAX
+ 1];
195 decoded_options
.header_size
= known_options
.header_size
;
196 decoded_options
.check
= known_options
.check
;
197 decoded_options
.filters
= filters
;
201 expect(lzma_block_header_decode(&decoded_options
, NULL
, buf
)
206 buf
[known_options
.header_size
- 1] ^= 1;
207 expect(lzma_block_header_decode(&decoded_options
, NULL
, buf
)
209 buf
[known_options
.header_size
- 1] ^= 1;
211 // Unsupported filter
212 // NOTE: This may need updating when new IDs become supported.
214 unaligned_write32le(buf
+ known_options
.header_size
- 4,
215 lzma_crc32(buf
, known_options
.header_size
- 4, 0));
216 expect(lzma_block_header_decode(&decoded_options
, NULL
, buf
)
217 == LZMA_OPTIONS_ERROR
);
221 buf
[known_options
.header_size
- 4 - 1] ^= 1;
222 unaligned_write32le(buf
+ known_options
.header_size
- 4,
223 lzma_crc32(buf
, known_options
.header_size
- 4, 0));
224 expect(lzma_block_header_decode(&decoded_options
, NULL
, buf
)
225 == LZMA_OPTIONS_ERROR
);
226 buf
[known_options
.header_size
- 4 - 1] ^= 1;
233 succeed(lzma_lzma_preset(&opt_lzma
, 1));