1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file stream_buffer_encoder.c
4 /// \brief Single-call .xz Stream encoder
6 // Copyright (C) 2009 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 ///////////////////////////////////////////////////////////////////////////////
23 /// Maximum size of Index that has exactly one Record.
24 /// Index Indicator + Number of Records + Record + CRC32 rounded up to
25 /// the next multiple of four.
26 #define INDEX_BOUND ((1 + 1 + 2 * LZMA_VLI_BYTES_MAX + 4 + 3) & ~3)
28 /// Stream Header, Stream Footer, and Index
29 #define HEADERS_BOUND (2 * LZMA_STREAM_HEADER_SIZE + INDEX_BOUND)
32 extern LZMA_API(size_t)
33 lzma_stream_buffer_bound(size_t uncompressed_size)
35 // Get the maximum possible size of a Block.
36 const size_t block_bound = lzma_block_buffer_bound(uncompressed_size);
40 // Catch the possible integer overflow and also prevent the size of
41 // the Stream exceeding LZMA_VLI_MAX (theoretically possible on
43 if (MIN(SIZE_MAX, LZMA_VLI_MAX) - block_bound < HEADERS_BOUND)
46 return block_bound + HEADERS_BOUND;
50 extern LZMA_API(lzma_ret)
51 lzma_stream_buffer_encode(lzma_filter *filters, lzma_check check,
52 lzma_allocator *allocator, const uint8_t *in, size_t in_size,
53 uint8_t *out, size_t *out_pos_ptr, size_t out_size)
56 if (filters == NULL || (unsigned int)(check) > LZMA_CHECK_ID_MAX
57 || (in == NULL && in_size != 0) || out == NULL
58 || out_pos_ptr == NULL || *out_pos_ptr > out_size)
59 return LZMA_PROG_ERROR;
61 // Note for the paranoids: Index encoder prevents the Stream from
62 // getting too big and still being accepted with LZMA_OK, and Block
63 // encoder catches if the input is too big. So we don't need to
64 // separately check if the buffers are too big.
66 // Use a local copy. We update *out_pos_ptr only if everything
68 size_t out_pos = *out_pos_ptr;
70 // Check that there's enough space for both Stream Header and
72 if (out_size - out_pos <= 2 * LZMA_STREAM_HEADER_SIZE)
73 return LZMA_BUF_ERROR;
75 // Reserve space for Stream Footer so we don't need to check for
76 // available space again before encoding Stream Footer.
77 out_size -= LZMA_STREAM_HEADER_SIZE;
79 // Encode the Stream Header.
80 lzma_stream_flags stream_flags = {
85 if (lzma_stream_header_encode(&stream_flags, out + out_pos)
87 return LZMA_PROG_ERROR;
89 out_pos += LZMA_STREAM_HEADER_SIZE;
98 return_if_error(lzma_block_buffer_encode(&block, allocator,
99 in, in_size, out, &out_pos, out_size));
103 // Create an Index with one Record.
104 lzma_index *i = lzma_index_init(NULL, NULL);
106 return LZMA_MEM_ERROR;
108 lzma_ret ret = lzma_index_append(i, NULL,
109 lzma_block_unpadded_size(&block),
110 block.uncompressed_size);
112 // If adding the Record was successful, encode the Index
113 // and get its size which will be stored into Stream Footer.
114 if (ret == LZMA_OK) {
115 ret = lzma_index_buffer_encode(
116 i, out, &out_pos, out_size);
118 stream_flags.backward_size = lzma_index_size(i);
121 lzma_index_end(i, NULL);
127 // Stream Footer. We have already reserved space for this.
128 if (lzma_stream_footer_encode(&stream_flags, out + out_pos)
130 return LZMA_PROG_ERROR;
132 out_pos += LZMA_STREAM_HEADER_SIZE;
134 // Everything went fine, make the new output position available
135 // to the application.
136 *out_pos_ptr = out_pos;