1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file block_header_encoder.c
4 /// \brief Encodes Block Header for .lzma files
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 ///////////////////////////////////////////////////////////////////////////////
24 extern LZMA_API lzma_ret
25 lzma_block_header_size(lzma_options_block *options)
27 // Block Header Size + Block Flags + CRC32.
28 size_t size = 1 + 1 + 4;
31 if (options->compressed_size != LZMA_VLI_VALUE_UNKNOWN) {
32 if (options->compressed_size > LZMA_VLI_VALUE_MAX / 4 - 1
33 || options->compressed_size == 0
34 || (options->compressed_size & 3))
35 return LZMA_PROG_ERROR;
37 size += lzma_vli_size(options->compressed_size / 4 - 1);
41 if (options->uncompressed_size != LZMA_VLI_VALUE_UNKNOWN) {
42 const size_t add = lzma_vli_size(options->uncompressed_size);
44 return LZMA_PROG_ERROR;
49 // List of Filter Flags
50 if (options->filters == NULL
51 || options->filters[0].id == LZMA_VLI_VALUE_UNKNOWN)
52 return LZMA_PROG_ERROR;
54 for (size_t i = 0; options->filters[i].id != LZMA_VLI_VALUE_UNKNOWN;
56 // Don't allow too many filters.
58 return LZMA_PROG_ERROR;
61 return_if_error(lzma_filter_flags_size(&add,
62 options->filters + i));
67 // Pad to a multiple of four bytes.
68 options->header_size = (size + 3) & ~(size_t)(3);
70 // NOTE: We don't verify that Total Size of the Block stays within
71 // limits. This is because it is possible that we are called with
72 // exaggerated values to reserve space for Block Header, and later
73 // called again with lower, real values.
79 extern LZMA_API lzma_ret
80 lzma_block_header_encode(const lzma_options_block *options, uint8_t *out)
82 if ((options->header_size & 3)
83 || options->header_size < LZMA_BLOCK_HEADER_SIZE_MIN
84 || options->header_size > LZMA_BLOCK_HEADER_SIZE_MAX)
85 return LZMA_PROG_ERROR;
87 // Indicate the size of the buffer _excluding_ the CRC32 field.
88 const size_t out_size = options->header_size - 4;
90 // Store the Block Header Size.
91 out[0] = out_size / 4;
93 // We write Block Flags a little later.
97 if (options->compressed_size != LZMA_VLI_VALUE_UNKNOWN) {
98 // Compressed Size must be non-zero, fit into a 63-bit
99 // integer and be a multiple of four. Also the Total Size
100 // of the Block must fit into 63-bit integer.
101 if (options->compressed_size == 0
102 || (options->compressed_size & 3)
103 || options->compressed_size
105 || lzma_block_total_size_get(options) == 0)
106 return LZMA_PROG_ERROR;
108 return_if_error(lzma_vli_encode(
109 options->compressed_size / 4 - 1, NULL,
110 out, &out_pos, out_size));
114 if (options->uncompressed_size != LZMA_VLI_VALUE_UNKNOWN)
115 return_if_error(lzma_vli_encode(
116 options->uncompressed_size, NULL,
117 out, &out_pos, out_size));
120 if (options->filters == NULL
121 || options->filters[0].id == LZMA_VLI_VALUE_UNKNOWN)
122 return LZMA_PROG_ERROR;
124 size_t filter_count = 0;
126 // There can be at maximum of four filters.
127 if (filter_count == 4)
128 return LZMA_PROG_ERROR;
130 return_if_error(lzma_filter_flags_encode(out, &out_pos,
131 out_size, options->filters + filter_count));
133 } while (options->filters[++filter_count].id
134 != LZMA_VLI_VALUE_UNKNOWN);
137 out[1] = filter_count - 1;
139 if (options->compressed_size != LZMA_VLI_VALUE_UNKNOWN)
142 if (options->uncompressed_size != LZMA_VLI_VALUE_UNKNOWN)
146 memzero(out + out_pos, out_size - out_pos);
149 integer_write_32(out + out_size, lzma_crc32(out, out_size, 0));