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_options_block known_options;
25 static lzma_options_block decoded_options;
27 static lzma_options_filter filters_none[1] = {
29 .id = LZMA_VLI_VALUE_UNKNOWN,
34 static lzma_options_filter filters_one[2] = {
36 .id = LZMA_FILTER_LZMA,
37 .options = (void *)(&lzma_preset_lzma[0]),
39 .id = LZMA_VLI_VALUE_UNKNOWN,
44 static lzma_options_filter filters_four[5] = {
46 .id = LZMA_FILTER_X86,
49 .id = LZMA_FILTER_X86,
52 .id = LZMA_FILTER_X86,
55 .id = LZMA_FILTER_LZMA,
56 .options = (void *)(&lzma_preset_lzma[0]),
58 .id = LZMA_VLI_VALUE_UNKNOWN,
63 static lzma_options_filter filters_five[6] = {
65 .id = LZMA_FILTER_X86,
68 .id = LZMA_FILTER_X86,
71 .id = LZMA_FILTER_X86,
74 .id = LZMA_FILTER_X86,
77 .id = LZMA_FILTER_LZMA,
78 .options = (void *)(&lzma_preset_lzma[0]),
80 .id = LZMA_VLI_VALUE_UNKNOWN,
88 expect(lzma_block_header_encode(&known_options, buf) == LZMA_OK);
90 lzma_options_filter filters[LZMA_BLOCK_FILTERS_MAX + 1];
91 memcrap(filters, sizeof(filters));
92 memcrap(&decoded_options, sizeof(decoded_options));
94 decoded_options.header_size = known_options.header_size;
95 decoded_options.check = known_options.check;
96 decoded_options.filters = filters;
97 expect(lzma_block_header_decode(&decoded_options, NULL, buf)
100 expect(known_options.compressed_size
101 == decoded_options.compressed_size);
102 expect(known_options.uncompressed_size
103 == decoded_options.uncompressed_size);
105 for (size_t i = 0; known_options.filters[i].id
106 != LZMA_VLI_VALUE_UNKNOWN; ++i)
107 expect(known_options.filters[i].id == filters[i].id);
109 for (size_t i = 0; i < LZMA_BLOCK_FILTERS_MAX; ++i)
110 free(decoded_options.filters[i].options);
117 known_options = (lzma_options_block){
118 .check = LZMA_CHECK_NONE,
119 .compressed_size = LZMA_VLI_VALUE_UNKNOWN,
120 .uncompressed_size = LZMA_VLI_VALUE_UNKNOWN,
124 expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
126 known_options.filters = filters_none;
127 expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
129 known_options.filters = filters_five;
130 expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
132 known_options.filters = filters_one;
133 expect(lzma_block_header_size(&known_options) == LZMA_OK);
135 known_options.check = 999; // Some invalid value, which gets ignored.
136 expect(lzma_block_header_size(&known_options) == LZMA_OK);
138 known_options.compressed_size = 5; // Not a multiple of four.
139 expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
141 known_options.compressed_size = 0; // Cannot be zero.
142 expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
144 known_options.compressed_size = LZMA_VLI_VALUE_UNKNOWN;
145 known_options.uncompressed_size = 0;
146 expect(lzma_block_header_size(&known_options) == LZMA_OK);
148 known_options.uncompressed_size = LZMA_VLI_VALUE_MAX + 1;
149 expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
156 known_options = (lzma_options_block){
157 .check = LZMA_CHECK_CRC32,
158 .compressed_size = LZMA_VLI_VALUE_UNKNOWN,
159 .uncompressed_size = LZMA_VLI_VALUE_UNKNOWN,
160 .filters = filters_four,
163 expect(lzma_block_header_size(&known_options) == LZMA_OK);
166 known_options.compressed_size = 123456;
167 known_options.uncompressed_size = 234567;
168 expect(lzma_block_header_size(&known_options) == LZMA_OK);
171 // We can make the sizes smaller while keeping the header size
173 known_options.compressed_size = 12;
174 known_options.uncompressed_size = 23;
182 known_options = (lzma_options_block){
183 .check = LZMA_CHECK_CRC32,
184 .compressed_size = LZMA_VLI_VALUE_UNKNOWN,
185 .uncompressed_size = LZMA_VLI_VALUE_UNKNOWN,
186 .filters = filters_one,
189 expect(lzma_block_header_size(&known_options) == LZMA_OK);
190 known_options.header_size += 4;
191 expect(lzma_block_header_encode(&known_options, buf) == LZMA_OK);
193 lzma_options_filter filters[LZMA_BLOCK_FILTERS_MAX + 1];
194 decoded_options.header_size = known_options.header_size;
195 decoded_options.check = known_options.check;
196 decoded_options.filters = filters;
200 expect(lzma_block_header_decode(&decoded_options, NULL, buf)
205 buf[known_options.header_size - 1] ^= 1;
206 expect(lzma_block_header_decode(&decoded_options, NULL, buf)
208 buf[known_options.header_size - 1] ^= 1;
210 // Unsupported filter
211 // NOTE: This may need updating when new IDs become supported.
213 integer_write_32(buf + known_options.header_size - 4,
214 lzma_crc32(buf, known_options.header_size - 4, 0));
215 expect(lzma_block_header_decode(&decoded_options, NULL, buf)
216 == LZMA_HEADER_ERROR);
220 buf[known_options.header_size - 4 - 1] ^= 1;
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_HEADER_ERROR);
225 buf[known_options.header_size - 4 - 1] ^= 1;