1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file test_block_header.c
4 /// \brief Tests Block Header coders
6 // Copyright (C) 2007 Lasse Collin
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 ///////////////////////////////////////////////////////////////////////////////
26 known_options = (lzma_options_block){
31 .uncompressed_size = 0,
32 .compressed_reserve = LZMA_VLI_BYTES_MAX,
33 .uncompressed_reserve = LZMA_VLI_BYTES_MAX,
36 memcpy(known_options.filters, filters_powerpc,
37 sizeof(filters_powerpc));
42 expect(!decode_ret(1, LZMA_HEADER_ERROR));
46 expect(decode_ret(21, LZMA_HEADER_ERROR));
53 static uint8_t buf[LZMA_BLOCK_HEADER_SIZE_MAX];
54 static lzma_options_block known_options;
55 static lzma_options_block decoded_options;
57 static lzma_options_filter filters_none[1] = {
59 .id = LZMA_VLI_VALUE_UNKNOWN,
64 static lzma_options_filter filters_one[2] = {
66 .id = LZMA_FILTER_LZMA,
67 .options = (void *)(&lzma_preset_lzma[0]),
69 .id = LZMA_VLI_VALUE_UNKNOWN,
74 static lzma_options_filter filters_four[5] = {
76 .id = LZMA_FILTER_X86,
79 .id = LZMA_FILTER_X86,
82 .id = LZMA_FILTER_X86,
85 .id = LZMA_FILTER_LZMA,
86 .options = (void *)(&lzma_preset_lzma[0]),
88 .id = LZMA_VLI_VALUE_UNKNOWN,
93 static lzma_options_filter filters_five[6] = {
95 .id = LZMA_FILTER_X86,
98 .id = LZMA_FILTER_X86,
101 .id = LZMA_FILTER_X86,
104 .id = LZMA_FILTER_X86,
107 .id = LZMA_FILTER_LZMA,
108 .options = (void *)(&lzma_preset_lzma[0]),
110 .id = LZMA_VLI_VALUE_UNKNOWN,
118 expect(lzma_block_header_encode(&known_options, buf) == LZMA_OK);
120 lzma_options_filter filters[LZMA_BLOCK_FILTERS_MAX + 1];
121 memcrap(filters, sizeof(filters));
122 memcrap(&decoded_options, sizeof(decoded_options));
124 decoded_options.header_size = known_options.header_size;
125 decoded_options.check = known_options.check;
126 decoded_options.filters = filters;
127 expect(lzma_block_header_decode(&decoded_options, NULL, buf)
130 expect(known_options.compressed_size
131 == decoded_options.compressed_size);
132 expect(known_options.uncompressed_size
133 == decoded_options.uncompressed_size);
135 for (size_t i = 0; known_options.filters[i].id
136 != LZMA_VLI_VALUE_UNKNOWN; ++i)
137 expect(known_options.filters[i].id == filters[i].id);
139 for (size_t i = 0; i < LZMA_BLOCK_FILTERS_MAX; ++i)
140 free(decoded_options.filters[i].options);
147 known_options = (lzma_options_block){
148 .check = LZMA_CHECK_NONE,
149 .compressed_size = LZMA_VLI_VALUE_UNKNOWN,
150 .uncompressed_size = LZMA_VLI_VALUE_UNKNOWN,
154 expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
156 known_options.filters = filters_none;
157 expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
159 known_options.filters = filters_five;
160 expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
162 known_options.filters = filters_one;
163 expect(lzma_block_header_size(&known_options) == LZMA_OK);
165 known_options.check = 999; // Some invalid value, which gets ignored.
166 expect(lzma_block_header_size(&known_options) == LZMA_OK);
168 known_options.compressed_size = 5; // Not a multiple of four.
169 expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
171 known_options.compressed_size = 0; // Cannot be zero.
172 expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
174 known_options.compressed_size = LZMA_VLI_VALUE_UNKNOWN;
175 known_options.uncompressed_size = 0;
176 expect(lzma_block_header_size(&known_options) == LZMA_OK);
178 known_options.uncompressed_size = LZMA_VLI_VALUE_MAX + 1;
179 expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
186 known_options = (lzma_options_block){
187 .check = LZMA_CHECK_CRC32,
188 .compressed_size = LZMA_VLI_VALUE_UNKNOWN,
189 .uncompressed_size = LZMA_VLI_VALUE_UNKNOWN,
190 .filters = filters_four,
193 expect(lzma_block_header_size(&known_options) == LZMA_OK);
196 known_options.compressed_size = 123456;
197 known_options.uncompressed_size = 234567;
198 expect(lzma_block_header_size(&known_options) == LZMA_OK);
201 // We can make the sizes smaller while keeping the header size
203 known_options.compressed_size = 12;
204 known_options.uncompressed_size = 23;
212 known_options = (lzma_options_block){
213 .check = LZMA_CHECK_CRC32,
214 .compressed_size = LZMA_VLI_VALUE_UNKNOWN,
215 .uncompressed_size = LZMA_VLI_VALUE_UNKNOWN,
216 .filters = filters_one,
219 expect(lzma_block_header_size(&known_options) == LZMA_OK);
220 known_options.header_size += 4;
221 expect(lzma_block_header_encode(&known_options, buf) == LZMA_OK);
223 lzma_options_filter filters[LZMA_BLOCK_FILTERS_MAX + 1];
224 decoded_options.header_size = known_options.header_size;
225 decoded_options.check = known_options.check;
226 decoded_options.filters = filters;
230 expect(lzma_block_header_decode(&decoded_options, NULL, buf)
235 buf[known_options.header_size - 1] ^= 1;
236 expect(lzma_block_header_decode(&decoded_options, NULL, buf)
238 buf[known_options.header_size - 1] ^= 1;
240 // Unsupported filter
241 // NOTE: This may need updating when new IDs become supported.
243 integer_write_32(buf + known_options.header_size - 4,
244 lzma_crc32(buf, known_options.header_size - 4, 0));
245 expect(lzma_block_header_decode(&decoded_options, NULL, buf)
246 == LZMA_HEADER_ERROR);
250 buf[known_options.header_size - 4 - 1] ^= 1;
251 integer_write_32(buf + known_options.header_size - 4,
252 lzma_crc32(buf, known_options.header_size - 4, 0));
253 expect(lzma_block_header_decode(&decoded_options, NULL, buf)
254 == LZMA_HEADER_ERROR);
255 buf[known_options.header_size - 4 - 1] ^= 1;