]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/block_header_encoder.c
Update the code to mostly match the new simpler file format
[icculus/xz.git] / src / liblzma / common / block_header_encoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       block_header_encoder.c
4 /// \brief      Encodes Block Header for .lzma files
5 //
6 //  Copyright (C) 2007 Lasse Collin
7 //
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.
12 //
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.
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 #include "common.h"
21 #include "check.h"
22
23
24 extern LZMA_API lzma_ret
25 lzma_block_header_size(lzma_options_block *options)
26 {
27         // Block Header Size + Block Flags + CRC32.
28         size_t size = 1 + 1 + 4;
29
30         // Compressed Size
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;
36
37                 size += lzma_vli_size(options->compressed_size / 4 - 1);
38         }
39
40         // Uncompressed Size
41         if (options->uncompressed_size != LZMA_VLI_VALUE_UNKNOWN) {
42                 const size_t add = lzma_vli_size(options->uncompressed_size);
43                 if (add == 0)
44                         return LZMA_PROG_ERROR;
45
46                 size += add;
47         }
48
49         // List of Filter Flags
50         if (options->filters == NULL
51                         || options->filters[0].id == LZMA_VLI_VALUE_UNKNOWN)
52                 return LZMA_PROG_ERROR;
53
54         for (size_t i = 0; options->filters[i].id != LZMA_VLI_VALUE_UNKNOWN;
55                         ++i) {
56                 // Don't allow too many filters.
57                 if (i == 4)
58                         return LZMA_PROG_ERROR;
59
60                 uint32_t add;
61                 return_if_error(lzma_filter_flags_size(&add,
62                                 options->filters + i));
63
64                 size += add;
65         }
66
67         // Pad to a multiple of four bytes.
68         options->header_size = (size + 3) & ~(size_t)(3);
69
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.
74
75         return LZMA_OK;
76 }
77
78
79 extern LZMA_API lzma_ret
80 lzma_block_header_encode(const lzma_options_block *options, uint8_t *out)
81 {
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;
86
87         // Indicate the size of the buffer _excluding_ the CRC32 field.
88         const size_t out_size = options->header_size - 4;
89
90         // Store the Block Header Size.
91         out[0] = out_size / 4;
92
93         // We write Block Flags a little later.
94         size_t out_pos = 2;
95
96         // Compressed Size
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
104                                         > LZMA_VLI_VALUE_MAX
105                                 || lzma_block_total_size_get(options) == 0)
106                         return LZMA_PROG_ERROR;
107
108                 return_if_error(lzma_vli_encode(
109                                 options->compressed_size / 4 - 1, NULL,
110                                 out, &out_pos, out_size));
111         }
112
113         // Uncompressed 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));
118
119         // Filter Flags
120         if (options->filters == NULL
121                         || options->filters[0].id == LZMA_VLI_VALUE_UNKNOWN)
122                 return LZMA_PROG_ERROR;
123
124         size_t filter_count = 0;
125         do {
126                 // There can be at maximum of four filters.
127                 if (filter_count == 4)
128                         return LZMA_PROG_ERROR;
129
130                 return_if_error(lzma_filter_flags_encode(out, &out_pos,
131                                 out_size, options->filters + filter_count));
132
133         } while (options->filters[++filter_count].id
134                         != LZMA_VLI_VALUE_UNKNOWN);
135
136         // Block Flags
137         out[1] = filter_count - 1;
138
139         if (options->compressed_size != LZMA_VLI_VALUE_UNKNOWN)
140                 out[1] |= 0x40;
141
142         if (options->uncompressed_size != LZMA_VLI_VALUE_UNKNOWN)
143                 out[1] |= 0x80;
144
145         // Padding
146         memzero(out + out_pos, out_size - out_pos);
147
148         // CRC32
149         integer_write_32(out + out_size, lzma_crc32(out, out_size, 0));
150
151         return LZMA_OK;
152 }