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 ///////////////////////////////////////////////////////////////////////////////
23 static uint8_t buf[LZMA_BLOCK_HEADER_SIZE_MAX];
24 static lzma_block known_options;
25 static lzma_block decoded_options;
27 static lzma_options_lzma opt_lzma;
29 static lzma_filter filters_none[1] = {
31 .id = LZMA_VLI_UNKNOWN,
36 static lzma_filter filters_one[2] = {
38 .id = LZMA_FILTER_LZMA2,
41 .id = LZMA_VLI_UNKNOWN,
46 static lzma_filter filters_four[5] = {
48 .id = LZMA_FILTER_X86,
51 .id = LZMA_FILTER_X86,
54 .id = LZMA_FILTER_X86,
57 .id = LZMA_FILTER_LZMA2,
60 .id = LZMA_VLI_UNKNOWN,
65 static lzma_filter filters_five[6] = {
67 .id = LZMA_FILTER_X86,
70 .id = LZMA_FILTER_X86,
73 .id = LZMA_FILTER_X86,
76 .id = LZMA_FILTER_X86,
79 .id = LZMA_FILTER_LZMA2,
82 .id = LZMA_VLI_UNKNOWN,
90 expect(lzma_block_header_encode(&known_options, buf) == LZMA_OK);
92 lzma_filter filters[LZMA_FILTERS_MAX + 1];
93 memcrap(filters, sizeof(filters));
94 memcrap(&decoded_options, sizeof(decoded_options));
96 decoded_options.header_size = known_options.header_size;
97 decoded_options.check = known_options.check;
98 decoded_options.filters = filters;
99 expect(lzma_block_header_decode(&decoded_options, NULL, buf)
102 expect(known_options.compressed_size
103 == decoded_options.compressed_size);
104 expect(known_options.uncompressed_size
105 == decoded_options.uncompressed_size);
107 for (size_t i = 0; known_options.filters[i].id
108 != LZMA_VLI_UNKNOWN; ++i)
109 expect(known_options.filters[i].id == filters[i].id);
111 for (size_t i = 0; i < LZMA_FILTERS_MAX; ++i)
112 free(decoded_options.filters[i].options);
119 known_options = (lzma_block){
120 .check = LZMA_CHECK_NONE,
121 .compressed_size = LZMA_VLI_UNKNOWN,
122 .uncompressed_size = LZMA_VLI_UNKNOWN,
126 expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
128 known_options.filters = filters_none;
129 expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
131 known_options.filters = filters_five;
132 expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
134 known_options.filters = filters_one;
135 expect(lzma_block_header_size(&known_options) == LZMA_OK);
137 known_options.check = 999; // Some invalid value, which gets ignored.
138 expect(lzma_block_header_size(&known_options) == LZMA_OK);
140 known_options.compressed_size = 5;
141 expect(lzma_block_header_size(&known_options) == LZMA_OK);
143 known_options.compressed_size = 0; // Cannot be zero.
144 expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
146 // LZMA_VLI_MAX is too big to keep the total size of the Block
147 // a valid VLI, but lzma_block_header_size() is not meant
148 // to validate it. (lzma_block_header_encode() must validate it.)
149 known_options.compressed_size = LZMA_VLI_MAX;
150 expect(lzma_block_header_size(&known_options) == LZMA_OK);
152 known_options.compressed_size = LZMA_VLI_UNKNOWN;
153 known_options.uncompressed_size = 0;
154 expect(lzma_block_header_size(&known_options) == LZMA_OK);
156 known_options.uncompressed_size = LZMA_VLI_MAX + 1;
157 expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
164 known_options = (lzma_block){
165 .check = LZMA_CHECK_CRC32,
166 .compressed_size = LZMA_VLI_UNKNOWN,
167 .uncompressed_size = LZMA_VLI_UNKNOWN,
168 .filters = filters_four,
171 expect(lzma_block_header_size(&known_options) == LZMA_OK);
174 known_options.compressed_size = 123456;
175 known_options.uncompressed_size = 234567;
176 expect(lzma_block_header_size(&known_options) == LZMA_OK);
179 // We can make the sizes smaller while keeping the header size
181 known_options.compressed_size = 12;
182 known_options.uncompressed_size = 23;
190 known_options = (lzma_block){
191 .check = LZMA_CHECK_CRC32,
192 .compressed_size = LZMA_VLI_UNKNOWN,
193 .uncompressed_size = LZMA_VLI_UNKNOWN,
194 .filters = filters_one,
197 expect(lzma_block_header_size(&known_options) == LZMA_OK);
198 known_options.header_size += 4;
199 expect(lzma_block_header_encode(&known_options, buf) == LZMA_OK);
201 lzma_filter filters[LZMA_FILTERS_MAX + 1];
202 decoded_options.header_size = known_options.header_size;
203 decoded_options.check = known_options.check;
204 decoded_options.filters = filters;
208 expect(lzma_block_header_decode(&decoded_options, NULL, buf)
213 buf[known_options.header_size - 1] ^= 1;
214 expect(lzma_block_header_decode(&decoded_options, NULL, buf)
216 buf[known_options.header_size - 1] ^= 1;
218 // Unsupported filter
219 // NOTE: This may need updating when new IDs become supported.
221 integer_write_32(buf + known_options.header_size - 4,
222 lzma_crc32(buf, known_options.header_size - 4, 0));
223 expect(lzma_block_header_decode(&decoded_options, NULL, buf)
224 == LZMA_OPTIONS_ERROR);
228 buf[known_options.header_size - 4 - 1] ^= 1;
229 integer_write_32(buf + known_options.header_size - 4,
230 lzma_crc32(buf, known_options.header_size - 4, 0));
231 expect(lzma_block_header_decode(&decoded_options, NULL, buf)
232 == LZMA_OPTIONS_ERROR);
233 buf[known_options.header_size - 4 - 1] ^= 1;
240 succeed(lzma_lzma_preset(&opt_lzma, 1));