1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
5 /// \file stream_buffer_decoder.c
6 /// \brief Single-call .xz Stream decoder
8 // Author: Lasse Collin
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "stream_decoder.h"
15 extern LZMA_API(lzma_ret
)
16 lzma_stream_buffer_decode(uint64_t *memlimit
, uint32_t flags
,
17 const lzma_allocator
*allocator
,
18 const uint8_t *in
, size_t *in_pos
, size_t in_size
,
19 uint8_t *out
, size_t *out_pos
, size_t out_size
)
22 if (in_pos
== NULL
|| (in
== NULL
&& *in_pos
!= in_size
)
23 || *in_pos
> in_size
|| out_pos
== NULL
24 || (out
== NULL
&& *out_pos
!= out_size
)
25 || *out_pos
> out_size
)
26 return LZMA_PROG_ERROR
;
28 // Catch flags that are not allowed in buffer-to-buffer decoding.
29 if (flags
& LZMA_TELL_ANY_CHECK
)
30 return LZMA_PROG_ERROR
;
32 // Initialize the Stream decoder.
33 // TODO: We need something to tell the decoder that it can use the
34 // output buffer as workspace, and thus save significant amount of RAM.
35 lzma_next_coder stream_decoder
= LZMA_NEXT_CODER_INIT
;
36 lzma_ret ret
= lzma_stream_decoder_init(
37 &stream_decoder
, allocator
, *memlimit
, flags
);
40 // Save the positions so that we can restore them in case
42 const size_t in_start
= *in_pos
;
43 const size_t out_start
= *out_pos
;
45 // Do the actual decoding.
46 ret
= stream_decoder
.code(stream_decoder
.coder
, allocator
,
47 in
, in_pos
, in_size
, out
, out_pos
, out_size
,
50 if (ret
== LZMA_STREAM_END
) {
53 // Something went wrong, restore the positions.
58 // Either the input was truncated or the
59 // output buffer was too small.
60 assert(*in_pos
== in_size
61 || *out_pos
== out_size
);
63 // If all the input was consumed, then the
64 // input is truncated, even if the output
65 // buffer is also full. This is because
66 // processing the last byte of the Stream
67 // never produces output.
68 if (*in_pos
== in_size
)
69 ret
= LZMA_DATA_ERROR
;
73 } else if (ret
== LZMA_MEMLIMIT_ERROR
) {
74 // Let the caller know how much memory would
77 (void)stream_decoder
.memconfig(
79 memlimit
, &memusage
, 0);
84 // Free the decoder memory. This needs to be done even if
85 // initialization fails, because the internal API doesn't
86 // require the initialization function to free its memory on error.
87 lzma_next_end(&stream_decoder
, allocator
);