1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
///////////////////////////////////////////////////////////////////////////////
//
/// \file block_header_encoder.c
/// \brief Encodes Block Header for .lzma files
//
// Copyright (C) 2007 Lasse Collin
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
///////////////////////////////////////////////////////////////////////////////
#include "common.h"
#include "check.h"
extern LZMA_API lzma_ret
lzma_block_header_size(lzma_options_block *options)
{
// Block Flags take two bytes.
size_t size = 2;
// Compressed Size
if (!lzma_vli_is_valid(options->compressed_size)) {
return LZMA_PROG_ERROR;
} else if (options->compressed_reserve != 0) {
// Make sure that the known Compressed Size fits into the
// reserved space. Note that lzma_vli_size() will return zero
// if options->compressed_size is LZMA_VLI_VALUE_UNKNOWN, so
// we don't need to handle that special case separately.
if (options->compressed_reserve > LZMA_VLI_BYTES_MAX
|| lzma_vli_size(options->compressed_size)
> (size_t)(options->compressed_reserve))
return LZMA_PROG_ERROR;
size += options->compressed_reserve;
} else if (options->compressed_size != LZMA_VLI_VALUE_UNKNOWN) {
// Compressed Size is known. We have already checked
// that is is a valid VLI, and since it isn't
// LZMA_VLI_VALUE_UNKNOWN, we can be sure that
// lzma_vli_size() will succeed.
size += lzma_vli_size(options->compressed_size);
}
// Uncompressed Size
if (!lzma_vli_is_valid(options->uncompressed_size)) {
return LZMA_PROG_ERROR;
} else if (options->uncompressed_reserve != 0) {
if (options->uncompressed_reserve > LZMA_VLI_BYTES_MAX
|| lzma_vli_size(options->uncompressed_size)
> (size_t)(options->uncompressed_reserve))
return LZMA_PROG_ERROR;
size += options->uncompressed_reserve;
} else if (options->uncompressed_size != LZMA_VLI_VALUE_UNKNOWN) {
size += lzma_vli_size(options->uncompressed_size);
}
// List of Filter Flags
for (size_t i = 0; options->filters[i].id != LZMA_VLI_VALUE_UNKNOWN;
++i) {
// Don't allow too many filters.
if (i == 7)
return LZMA_PROG_ERROR;
uint32_t tmp;
const lzma_ret ret = lzma_filter_flags_size(&tmp,
options->filters + i);
if (ret != LZMA_OK)
return ret;
size += tmp;
}
// CRC32
if (options->has_crc32)
size += 4;
// Padding
int32_t padding;
if (options->padding == LZMA_BLOCK_HEADER_PADDING_AUTO) {
const uint32_t preferred = lzma_alignment_output(
options->filters, 1);
const uint32_t unaligned = size + options->alignment;
padding = (int32_t)(unaligned % preferred);
if (padding != 0)
padding = preferred - padding;
} else if (options->padding >= LZMA_BLOCK_HEADER_PADDING_MIN
&& options->padding <= LZMA_BLOCK_HEADER_PADDING_MAX) {
padding = options->padding;
} else {
return LZMA_PROG_ERROR;
}
// All success. Copy the calculated values to the options structure.
options->padding = padding;
options->header_size = size + (size_t)(padding);
return LZMA_OK;
}
extern LZMA_API lzma_ret
lzma_block_header_encode(uint8_t *out, const lzma_options_block *options)
{
// We write the Block Flags later.
if (options->header_size < 2)
return LZMA_PROG_ERROR;
const size_t out_size = options->header_size;
size_t out_pos = 2;
// Compressed Size
if (options->compressed_size != LZMA_VLI_VALUE_UNKNOWN
|| options->compressed_reserve != 0) {
const lzma_vli size = options->compressed_size
!= LZMA_VLI_VALUE_UNKNOWN
? options->compressed_size : 0;
size_t vli_pos = 0;
if (lzma_vli_encode(
size, &vli_pos, options->compressed_reserve,
out, &out_pos, out_size) != LZMA_STREAM_END)
return LZMA_PROG_ERROR;
}
// Uncompressed Size
if (options->uncompressed_size != LZMA_VLI_VALUE_UNKNOWN
|| options->uncompressed_reserve != 0) {
const lzma_vli size = options->uncompressed_size
!= LZMA_VLI_VALUE_UNKNOWN
? options->uncompressed_size : 0;
size_t vli_pos = 0;
if (lzma_vli_encode(
size, &vli_pos, options->uncompressed_reserve,
out, &out_pos, out_size) != LZMA_STREAM_END)
return LZMA_PROG_ERROR;
}
// Filter Flags
size_t filter_count;
for (filter_count = 0; options->filters[filter_count].id
!= LZMA_VLI_VALUE_UNKNOWN; ++filter_count) {
// There can be at maximum of seven filters.
if (filter_count == 7)
return LZMA_PROG_ERROR;
const lzma_ret ret = lzma_filter_flags_encode(out, &out_pos,
out_size, options->filters + filter_count);
// FIXME: Don't return LZMA_BUF_ERROR.
if (ret != LZMA_OK)
return ret;
}
// Block Flags 1
out[0] = filter_count;
if (options->has_eopm)
out[0] |= 0x08;
else if (options->uncompressed_size == LZMA_VLI_VALUE_UNKNOWN)
return LZMA_PROG_ERROR;
if (options->compressed_size != LZMA_VLI_VALUE_UNKNOWN
|| options->compressed_reserve != 0)
out[0] |= 0x10;
if (options->uncompressed_size != LZMA_VLI_VALUE_UNKNOWN
|| options->uncompressed_reserve != 0)
out[0] |= 0x20;
if (options->is_metadata)
out[0] |= 0x80;
// Block Flags 2
if (options->padding < LZMA_BLOCK_HEADER_PADDING_MIN
|| options->padding > LZMA_BLOCK_HEADER_PADDING_MAX)
return LZMA_PROG_ERROR;
out[1] = (uint8_t)(options->padding);
// CRC32
if (options->has_crc32) {
if (out_size - out_pos < 4)
return LZMA_PROG_ERROR;
const uint32_t crc = lzma_crc32(out, out_pos, 0);
for (size_t i = 0; i < 4; ++i)
out[out_pos++] = crc >> (i * 8);
}
// Padding - the amount of available space must now match with
// the size of the Padding field.
if (out_size - out_pos != (size_t)(options->padding))
return LZMA_PROG_ERROR;
memzero(out + out_pos, (size_t)(options->padding));
return LZMA_OK;
}
|