1 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: expandtab:ts=8:sw=4:softtabstop=4:
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 /// \file stream_buffer_encoder.c
6 /// \brief Single-call .xz Stream encoder
7 //
8 // Author: Lasse Collin
9 //
10 // This file has been put into the public domain.
11 // You can do whatever you want with this file.
12 //
13 ///////////////////////////////////////////////////////////////////////////////
14
15 #include "index.h"
16
17
18 /// Maximum size of Index that has exactly one Record.
19 /// Index Indicator + Number of Records + Record + CRC32 rounded up to
20 /// the next multiple of four.
21 #define INDEX_BOUND ((1 + 1 + 2 * LZMA_VLI_BYTES_MAX + 4 + 3) & ~3)
22
23 /// Stream Header, Stream Footer, and Index
24 #define HEADERS_BOUND (2 * LZMA_STREAM_HEADER_SIZE + INDEX_BOUND)
25
26
27 extern LZMA_API(size_t)
lzma_stream_buffer_bound(size_t uncompressed_size)28 lzma_stream_buffer_bound(size_t uncompressed_size)
29 {
30 // Get the maximum possible size of a Block.
31 const size_t block_bound = lzma_block_buffer_bound(uncompressed_size);
32 if (block_bound == 0)
33 return 0;
34
35 // Catch the possible integer overflow and also prevent the size of
36 // the Stream exceeding LZMA_VLI_MAX (theoretically possible on
37 // 64-bit systems).
38 if (MIN(SIZE_MAX, LZMA_VLI_MAX) - block_bound < HEADERS_BOUND)
39 return 0;
40
41 return block_bound + HEADERS_BOUND;
42 }
43
44
45 extern LZMA_API(lzma_ret)
lzma_stream_buffer_encode(lzma_filter * filters,lzma_check check,lzma_allocator * allocator,const uint8_t * in,size_t in_size,uint8_t * out,size_t * out_pos_ptr,size_t out_size)46 lzma_stream_buffer_encode(lzma_filter *filters, lzma_check check,
47 lzma_allocator *allocator, const uint8_t *in, size_t in_size,
48 uint8_t *out, size_t *out_pos_ptr, size_t out_size)
49 {
50 // Sanity checks
51 if (filters == NULL || (unsigned int)(check) > LZMA_CHECK_ID_MAX
52 || (in == NULL && in_size != 0) || out == NULL
53 || out_pos_ptr == NULL || *out_pos_ptr > out_size)
54 return LZMA_PROG_ERROR;
55
56 // Note for the paranoids: Index encoder prevents the Stream from
57 // getting too big and still being accepted with LZMA_OK, and Block
58 // encoder catches if the input is too big. So we don't need to
59 // separately check if the buffers are too big.
60
61 // Use a local copy. We update *out_pos_ptr only if everything
62 // succeeds.
63 size_t out_pos = *out_pos_ptr;
64
65 // Check that there's enough space for both Stream Header and
66 // Stream Footer.
67 if (out_size - out_pos <= 2 * LZMA_STREAM_HEADER_SIZE)
68 return LZMA_BUF_ERROR;
69
70 // Reserve space for Stream Footer so we don't need to check for
71 // available space again before encoding Stream Footer.
72 out_size -= LZMA_STREAM_HEADER_SIZE;
73
74 // Encode the Stream Header.
75 lzma_stream_flags stream_flags = {
76 .version = 0,
77 .check = check,
78 };
79
80 if (lzma_stream_header_encode(&stream_flags, out + out_pos)
81 != LZMA_OK)
82 return LZMA_PROG_ERROR;
83
84 out_pos += LZMA_STREAM_HEADER_SIZE;
85
86 // Block
87 lzma_block block = {
88 .version = 0,
89 .check = check,
90 .filters = filters,
91 };
92
93 return_if_error(lzma_block_buffer_encode(&block, allocator,
94 in, in_size, out, &out_pos, out_size));
95
96 // Index
97 {
98 // Create an Index with one Record.
99 lzma_index *i = lzma_index_init(NULL, NULL);
100 if (i == NULL)
101 return LZMA_MEM_ERROR;
102
103 lzma_ret ret = lzma_index_append(i, NULL,
104 lzma_block_unpadded_size(&block),
105 block.uncompressed_size);
106
107 // If adding the Record was successful, encode the Index
108 // and get its size which will be stored into Stream Footer.
109 if (ret == LZMA_OK) {
110 ret = lzma_index_buffer_encode(
111 i, out, &out_pos, out_size);
112
113 stream_flags.backward_size = lzma_index_size(i);
114 }
115
116 lzma_index_end(i, NULL);
117
118 if (ret != LZMA_OK)
119 return ret;
120 }
121
122 // Stream Footer. We have already reserved space for this.
123 if (lzma_stream_footer_encode(&stream_flags, out + out_pos)
124 != LZMA_OK)
125 return LZMA_PROG_ERROR;
126
127 out_pos += LZMA_STREAM_HEADER_SIZE;
128
129 // Everything went fine, make the new output position available
130 // to the application.
131 *out_pos_ptr = out_pos;
132 return LZMA_OK;
133 }
134