]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/block_header_encoder.c
Moved var declarations out of for-loops. Makes pre-C99 compilers happier.
[icculus/xz.git] / src / liblzma / common / block_header_encoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       block_header_encoder.c
4 /// \brief      Encodes Block Header for .xz files
5 //
6 //  Author:     Lasse Collin
7 //
8 //  This file has been put into the public domain.
9 //  You can do whatever you want with this file.
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12
13 #include "common.h"
14 #include "check.h"
15
16
17 extern LZMA_API(lzma_ret)
18 lzma_block_header_size(lzma_block *block)
19 {
20         if (block->version != 0)
21                 return LZMA_OPTIONS_ERROR;
22
23         // Block Header Size + Block Flags + CRC32.
24         uint32_t size = 1 + 1 + 4;
25
26         // Compressed Size
27         if (block->compressed_size != LZMA_VLI_UNKNOWN) {
28                 const uint32_t add = lzma_vli_size(block->compressed_size);
29                 if (add == 0 || block->compressed_size == 0)
30                         return LZMA_PROG_ERROR;
31
32                 size += add;
33         }
34
35         // Uncompressed Size
36         if (block->uncompressed_size != LZMA_VLI_UNKNOWN) {
37                 const uint32_t add = lzma_vli_size(block->uncompressed_size);
38                 if (add == 0)
39                         return LZMA_PROG_ERROR;
40
41                 size += add;
42         }
43
44         // List of Filter Flags
45         if (block->filters == NULL || block->filters[0].id == LZMA_VLI_UNKNOWN)
46                 return LZMA_PROG_ERROR;
47
48         size_t i;
49         for (i = 0; block->filters[i].id != LZMA_VLI_UNKNOWN; ++i) {
50                 // Don't allow too many filters.
51                 if (i == LZMA_FILTERS_MAX)
52                         return LZMA_PROG_ERROR;
53
54                 uint32_t add;
55                 return_if_error(lzma_filter_flags_size(&add,
56                                 block->filters + i));
57
58                 size += add;
59         }
60
61         // Pad to a multiple of four bytes.
62         block->header_size = (size + 3) & ~UINT32_C(3);
63
64         // NOTE: We don't verify that the encoded size of the Block stays
65         // within limits. This is because it is possible that we are called
66         // with exaggerated Compressed Size (e.g. LZMA_VLI_MAX) to reserve
67         // space for Block Header, and later called again with lower,
68         // real values.
69
70         return LZMA_OK;
71 }
72
73
74 extern LZMA_API(lzma_ret)
75 lzma_block_header_encode(const lzma_block *block, uint8_t *out)
76 {
77         // Validate everything but filters.
78         if (lzma_block_unpadded_size(block) == 0
79                         || !lzma_vli_is_valid(block->uncompressed_size))
80                 return LZMA_PROG_ERROR;
81
82         // Indicate the size of the buffer _excluding_ the CRC32 field.
83         const size_t out_size = block->header_size - 4;
84
85         // Store the Block Header Size.
86         out[0] = out_size / 4;
87
88         // We write Block Flags in pieces.
89         out[1] = 0x00;
90         size_t out_pos = 2;
91
92         // Compressed Size
93         if (block->compressed_size != LZMA_VLI_UNKNOWN) {
94                 return_if_error(lzma_vli_encode(block->compressed_size, NULL,
95                                 out, &out_pos, out_size));
96
97                 out[1] |= 0x40;
98         }
99
100         // Uncompressed Size
101         if (block->uncompressed_size != LZMA_VLI_UNKNOWN) {
102                 return_if_error(lzma_vli_encode(block->uncompressed_size, NULL,
103                                 out, &out_pos, out_size));
104
105                 out[1] |= 0x80;
106         }
107
108         // Filter Flags
109         if (block->filters == NULL || block->filters[0].id == LZMA_VLI_UNKNOWN)
110                 return LZMA_PROG_ERROR;
111
112         size_t filter_count = 0;
113         do {
114                 // There can be a maximum of four filters.
115                 if (filter_count == LZMA_FILTERS_MAX)
116                         return LZMA_PROG_ERROR;
117
118                 return_if_error(lzma_filter_flags_encode(
119                                 block->filters + filter_count,
120                                 out, &out_pos, out_size));
121
122         } while (block->filters[++filter_count].id != LZMA_VLI_UNKNOWN);
123
124         out[1] |= filter_count - 1;
125
126         // Padding
127         memzero(out + out_pos, out_size - out_pos);
128
129         // CRC32
130         unaligned_write32le(out + out_size, lzma_crc32(out, out_size, 0));
131
132         return LZMA_OK;
133 }