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_BLOCK_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_BLOCK_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; // Not a multiple of four.
141 expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
143 known_options.compressed_size = 0; // Cannot be zero.
144 expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
146 known_options.compressed_size = LZMA_VLI_UNKNOWN;
147 known_options.uncompressed_size = 0;
148 expect(lzma_block_header_size(&known_options) == LZMA_OK);
150 known_options.uncompressed_size = LZMA_VLI_MAX + 1;
151 expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
158 known_options = (lzma_block){
159 .check = LZMA_CHECK_CRC32,
160 .compressed_size = LZMA_VLI_UNKNOWN,
161 .uncompressed_size = LZMA_VLI_UNKNOWN,
162 .filters = filters_four,
165 expect(lzma_block_header_size(&known_options) == LZMA_OK);
168 known_options.compressed_size = 123456;
169 known_options.uncompressed_size = 234567;
170 expect(lzma_block_header_size(&known_options) == LZMA_OK);
173 // We can make the sizes smaller while keeping the header size
175 known_options.compressed_size = 12;
176 known_options.uncompressed_size = 23;
184 known_options = (lzma_block){
185 .check = LZMA_CHECK_CRC32,
186 .compressed_size = LZMA_VLI_UNKNOWN,
187 .uncompressed_size = LZMA_VLI_UNKNOWN,
188 .filters = filters_one,
191 expect(lzma_block_header_size(&known_options) == LZMA_OK);
192 known_options.header_size += 4;
193 expect(lzma_block_header_encode(&known_options, buf) == LZMA_OK);
195 lzma_filter filters[LZMA_BLOCK_FILTERS_MAX + 1];
196 decoded_options.header_size = known_options.header_size;
197 decoded_options.check = known_options.check;
198 decoded_options.filters = filters;
202 expect(lzma_block_header_decode(&decoded_options, NULL, buf)
207 buf[known_options.header_size - 1] ^= 1;
208 expect(lzma_block_header_decode(&decoded_options, NULL, buf)
210 buf[known_options.header_size - 1] ^= 1;
212 // Unsupported filter
213 // NOTE: This may need updating when new IDs become supported.
215 integer_write_32(buf + known_options.header_size - 4,
216 lzma_crc32(buf, known_options.header_size - 4, 0));
217 expect(lzma_block_header_decode(&decoded_options, NULL, buf)
218 == LZMA_OPTIONS_ERROR);
222 buf[known_options.header_size - 4 - 1] ^= 1;
223 integer_write_32(buf + known_options.header_size - 4,
224 lzma_crc32(buf, known_options.header_size - 4, 0));
225 expect(lzma_block_header_decode(&decoded_options, NULL, buf)
226 == LZMA_OPTIONS_ERROR);
227 buf[known_options.header_size - 4 - 1] ^= 1;
235 succeed(lzma_lzma_preset(&opt_lzma, 0));