1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
5 /// \file stream_buffer_encoder.c
6 /// \brief Single-call .xz Stream encoder
8 // Author: Lasse Collin
10 ///////////////////////////////////////////////////////////////////////////////
16 /// Maximum size of Index that has exactly one Record.
17 /// Index Indicator + Number of Records + Record + CRC32 rounded up to
18 /// the next multiple of four.
19 #define INDEX_BOUND ((1 + 1 + 2 * LZMA_VLI_BYTES_MAX + 4 + 3) & ~3)
21 /// Stream Header, Stream Footer, and Index
22 #define HEADERS_BOUND (2 * LZMA_STREAM_HEADER_SIZE + INDEX_BOUND)
25 extern LZMA_API(size_t)
26 lzma_stream_buffer_bound(size_t uncompressed_size
)
28 // Get the maximum possible size of a Block.
29 const size_t block_bound
= lzma_block_buffer_bound(uncompressed_size
);
33 // Catch the possible integer overflow and also prevent the size of
34 // the Stream exceeding LZMA_VLI_MAX (theoretically possible on
36 if (my_min(SIZE_MAX
, LZMA_VLI_MAX
) - block_bound
< HEADERS_BOUND
)
39 return block_bound
+ HEADERS_BOUND
;
43 extern LZMA_API(lzma_ret
)
44 lzma_stream_buffer_encode(lzma_filter
*filters
, lzma_check check
,
45 const lzma_allocator
*allocator
,
46 const uint8_t *in
, size_t in_size
,
47 uint8_t *out
, size_t *out_pos_ptr
, size_t out_size
)
50 if (filters
== NULL
|| (unsigned int)(check
) > LZMA_CHECK_ID_MAX
51 || (in
== NULL
&& in_size
!= 0) || out
== NULL
52 || out_pos_ptr
== NULL
|| *out_pos_ptr
> out_size
)
53 return LZMA_PROG_ERROR
;
55 if (!lzma_check_is_supported(check
))
56 return LZMA_UNSUPPORTED_CHECK
;
58 // Note for the paranoids: Index encoder prevents the Stream from
59 // getting too big and still being accepted with LZMA_OK, and Block
60 // encoder catches if the input is too big. So we don't need to
61 // separately check if the buffers are too big.
63 // Use a local copy. We update *out_pos_ptr only if everything
65 size_t out_pos
= *out_pos_ptr
;
67 // Check that there's enough space for both Stream Header and
69 if (out_size
- out_pos
<= 2 * LZMA_STREAM_HEADER_SIZE
)
70 return LZMA_BUF_ERROR
;
72 // Reserve space for Stream Footer so we don't need to check for
73 // available space again before encoding Stream Footer.
74 out_size
-= LZMA_STREAM_HEADER_SIZE
;
76 // Encode the Stream Header.
77 lzma_stream_flags stream_flags
= {
82 if (lzma_stream_header_encode(&stream_flags
, out
+ out_pos
)
84 return LZMA_PROG_ERROR
;
86 out_pos
+= LZMA_STREAM_HEADER_SIZE
;
88 // Encode a Block but only if there is at least one byte of input.
96 return_if_error(lzma_block_buffer_encode(&block
, allocator
,
97 in
, in_size
, out
, &out_pos
, out_size
));
101 // Create an Index. It will have one Record if there was
102 // at least one byte of input to encode. Otherwise the
103 // Index will be empty.
104 lzma_index
*i
= lzma_index_init(allocator
);
106 return LZMA_MEM_ERROR
;
108 lzma_ret ret
= LZMA_OK
;
111 ret
= lzma_index_append(i
, allocator
,
112 lzma_block_unpadded_size(&block
),
113 block
.uncompressed_size
);
115 // If adding the Record was successful, encode the Index
116 // and get its size which will be stored into Stream Footer.
117 if (ret
== LZMA_OK
) {
118 ret
= lzma_index_buffer_encode(
119 i
, out
, &out_pos
, out_size
);
121 stream_flags
.backward_size
= lzma_index_size(i
);
124 lzma_index_end(i
, allocator
);
130 // Stream Footer. We have already reserved space for this.
131 if (lzma_stream_footer_encode(&stream_flags
, out
+ out_pos
)
133 return LZMA_PROG_ERROR
;
135 out_pos
+= LZMA_STREAM_HEADER_SIZE
;
137 // Everything went fine, make the new output position available
138 // to the application.
139 *out_pos_ptr
= out_pos
;