]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/block_encoder.c
8492eb2f2b461d14c021bcea7d4d5933976d50ad
[icculus/xz.git] / src / liblzma / common / block_encoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       block_encoder.c
4 /// \brief      Encodes .xz Blocks
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 "block_encoder.h"
21 #include "filter_encoder.h"
22 #include "check.h"
23
24
25 struct lzma_coder_s {
26         /// The filters in the chain; initialized with lzma_raw_decoder_init().
27         lzma_next_coder next;
28
29         /// Encoding options; we also write Unpadded Size, Compressed Size,
30         /// and Uncompressed Size back to this structure when the encoding
31         /// has been finished.
32         lzma_block *block;
33
34         enum {
35                 SEQ_CODE,
36                 SEQ_PADDING,
37                 SEQ_CHECK,
38         } sequence;
39
40         /// Compressed Size calculated while encoding
41         lzma_vli compressed_size;
42
43         /// Uncompressed Size calculated while encoding
44         lzma_vli uncompressed_size;
45
46         /// Position in the Check field
47         size_t pos;
48
49         /// Check of the uncompressed data
50         lzma_check_state check;
51 };
52
53
54 static lzma_ret
55 block_encode(lzma_coder *coder, lzma_allocator *allocator,
56                 const uint8_t *restrict in, size_t *restrict in_pos,
57                 size_t in_size, uint8_t *restrict out,
58                 size_t *restrict out_pos, size_t out_size, lzma_action action)
59 {
60         // Check that our amount of input stays in proper limits.
61         if (LZMA_VLI_MAX - coder->uncompressed_size < in_size - *in_pos)
62                 return LZMA_DATA_ERROR;
63
64         switch (coder->sequence) {
65         case SEQ_CODE: {
66                 const size_t in_start = *in_pos;
67                 const size_t out_start = *out_pos;
68
69                 const lzma_ret ret = coder->next.code(coder->next.coder,
70                                 allocator, in, in_pos, in_size,
71                                 out, out_pos, out_size, action);
72
73                 const size_t in_used = *in_pos - in_start;
74                 const size_t out_used = *out_pos - out_start;
75
76                 if (COMPRESSED_SIZE_MAX - coder->compressed_size < out_used)
77                         return LZMA_DATA_ERROR;
78
79                 coder->compressed_size += out_used;
80
81                 // No need to check for overflow because we have already
82                 // checked it at the beginning of this function.
83                 coder->uncompressed_size += in_used;
84
85                 lzma_check_update(&coder->check, coder->block->check,
86                                 in + in_start, in_used);
87
88                 if (ret != LZMA_STREAM_END || action == LZMA_SYNC_FLUSH)
89                         return ret;
90
91                 assert(*in_pos == in_size);
92                 assert(action == LZMA_FINISH);
93
94                 // Copy the values into coder->block. The caller
95                 // may use this information to construct Index.
96                 coder->block->compressed_size = coder->compressed_size;
97                 coder->block->uncompressed_size = coder->uncompressed_size;
98
99                 coder->sequence = SEQ_PADDING;
100         }
101
102         // Fall through
103
104         case SEQ_PADDING:
105                 // Pad Compressed Data to a multiple of four bytes. We can
106                 // use coder->compressed_size for this since we don't need
107                 // it for anything else anymore.
108                 while (coder->compressed_size & 3) {
109                         if (*out_pos >= out_size)
110                                 return LZMA_OK;
111
112                         out[*out_pos] = 0x00;
113                         ++*out_pos;
114                         ++coder->compressed_size;
115                 }
116
117                 if (coder->block->check == LZMA_CHECK_NONE)
118                         return LZMA_STREAM_END;
119
120                 lzma_check_finish(&coder->check, coder->block->check);
121
122                 coder->sequence = SEQ_CHECK;
123
124         // Fall through
125
126         case SEQ_CHECK: {
127                 const uint32_t check_size
128                                 = lzma_check_size(coder->block->check);
129
130                 while (*out_pos < out_size) {
131                         out[*out_pos] = coder->check.buffer.u8[coder->pos];
132                         ++*out_pos;
133
134                         if (++coder->pos == check_size)
135                                 return LZMA_STREAM_END;
136                 }
137
138                 return LZMA_OK;
139         }
140         }
141
142         return LZMA_PROG_ERROR;
143 }
144
145
146 static void
147 block_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
148 {
149         lzma_next_end(&coder->next, allocator);
150         lzma_free(coder, allocator);
151         return;
152 }
153
154
155 extern lzma_ret
156 lzma_block_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
157                 lzma_block *block)
158 {
159         lzma_next_coder_init(lzma_block_encoder_init, next, allocator);
160
161         if (block->version != 0)
162                 return LZMA_OPTIONS_ERROR;
163
164         // If the Check ID is not supported, we cannot calculate the check and
165         // thus not create a proper Block.
166         if ((unsigned int)(block->check) > LZMA_CHECK_ID_MAX)
167                 return LZMA_PROG_ERROR;
168
169         if (!lzma_check_is_supported(block->check))
170                 return LZMA_UNSUPPORTED_CHECK;
171
172         // Allocate and initialize *next->coder if needed.
173         if (next->coder == NULL) {
174                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
175                 if (next->coder == NULL)
176                         return LZMA_MEM_ERROR;
177
178                 next->code = &block_encode;
179                 next->end = &block_encoder_end;
180                 next->coder->next = LZMA_NEXT_CODER_INIT;
181         }
182
183         // Basic initializations
184         next->coder->sequence = SEQ_CODE;
185         next->coder->block = block;
186         next->coder->compressed_size = 0;
187         next->coder->uncompressed_size = 0;
188         next->coder->pos = 0;
189
190         // Initialize the check
191         lzma_check_init(&next->coder->check, block->check);
192
193         // Initialize the requested filters.
194         return lzma_raw_encoder_init(&next->coder->next, allocator,
195                         block->filters);
196 }
197
198
199 extern LZMA_API(lzma_ret)
200 lzma_block_encoder(lzma_stream *strm, lzma_block *block)
201 {
202         lzma_next_strm_init(lzma_block_encoder_init, strm, block);
203
204         strm->internal->supported_actions[LZMA_RUN] = true;
205         strm->internal->supported_actions[LZMA_FINISH] = true;
206
207         return LZMA_OK;
208 }