15a645f22SBen Gras ///////////////////////////////////////////////////////////////////////////////
25a645f22SBen Gras //
35a645f22SBen Gras /// \file stream_buffer_encoder.c
45a645f22SBen Gras /// \brief Single-call .xz Stream encoder
55a645f22SBen Gras //
65a645f22SBen Gras // Author: Lasse Collin
75a645f22SBen Gras //
85a645f22SBen Gras // This file has been put into the public domain.
95a645f22SBen Gras // You can do whatever you want with this file.
105a645f22SBen Gras //
115a645f22SBen Gras ///////////////////////////////////////////////////////////////////////////////
125a645f22SBen Gras
135a645f22SBen Gras #include "index.h"
145a645f22SBen Gras
155a645f22SBen Gras
165a645f22SBen Gras /// Maximum size of Index that has exactly one Record.
175a645f22SBen Gras /// Index Indicator + Number of Records + Record + CRC32 rounded up to
185a645f22SBen Gras /// the next multiple of four.
195a645f22SBen Gras #define INDEX_BOUND ((1 + 1 + 2 * LZMA_VLI_BYTES_MAX + 4 + 3) & ~3)
205a645f22SBen Gras
215a645f22SBen Gras /// Stream Header, Stream Footer, and Index
225a645f22SBen Gras #define HEADERS_BOUND (2 * LZMA_STREAM_HEADER_SIZE + INDEX_BOUND)
235a645f22SBen Gras
245a645f22SBen Gras
255a645f22SBen Gras extern LZMA_API(size_t)
lzma_stream_buffer_bound(size_t uncompressed_size)265a645f22SBen Gras lzma_stream_buffer_bound(size_t uncompressed_size)
275a645f22SBen Gras {
285a645f22SBen Gras // Get the maximum possible size of a Block.
295a645f22SBen Gras const size_t block_bound = lzma_block_buffer_bound(uncompressed_size);
305a645f22SBen Gras if (block_bound == 0)
315a645f22SBen Gras return 0;
325a645f22SBen Gras
335a645f22SBen Gras // Catch the possible integer overflow and also prevent the size of
345a645f22SBen Gras // the Stream exceeding LZMA_VLI_MAX (theoretically possible on
355a645f22SBen Gras // 64-bit systems).
365a645f22SBen Gras if (my_min(SIZE_MAX, LZMA_VLI_MAX) - block_bound < HEADERS_BOUND)
375a645f22SBen Gras return 0;
385a645f22SBen Gras
395a645f22SBen Gras return block_bound + HEADERS_BOUND;
405a645f22SBen Gras }
415a645f22SBen Gras
425a645f22SBen Gras
435a645f22SBen Gras extern LZMA_API(lzma_ret)
lzma_stream_buffer_encode(lzma_filter * filters,lzma_check check,const lzma_allocator * allocator,const uint8_t * in,size_t in_size,uint8_t * out,size_t * out_pos_ptr,size_t out_size)445a645f22SBen Gras lzma_stream_buffer_encode(lzma_filter *filters, lzma_check check,
45*0a6a1f1dSLionel Sambuc const lzma_allocator *allocator,
46*0a6a1f1dSLionel Sambuc const uint8_t *in, size_t in_size,
475a645f22SBen Gras uint8_t *out, size_t *out_pos_ptr, size_t out_size)
485a645f22SBen Gras {
495a645f22SBen Gras // Sanity checks
505a645f22SBen Gras if (filters == NULL || (unsigned int)(check) > LZMA_CHECK_ID_MAX
515a645f22SBen Gras || (in == NULL && in_size != 0) || out == NULL
525a645f22SBen Gras || out_pos_ptr == NULL || *out_pos_ptr > out_size)
535a645f22SBen Gras return LZMA_PROG_ERROR;
545a645f22SBen Gras
5511be35a1SLionel Sambuc if (!lzma_check_is_supported(check))
5611be35a1SLionel Sambuc return LZMA_UNSUPPORTED_CHECK;
5711be35a1SLionel Sambuc
585a645f22SBen Gras // Note for the paranoids: Index encoder prevents the Stream from
595a645f22SBen Gras // getting too big and still being accepted with LZMA_OK, and Block
605a645f22SBen Gras // encoder catches if the input is too big. So we don't need to
615a645f22SBen Gras // separately check if the buffers are too big.
625a645f22SBen Gras
635a645f22SBen Gras // Use a local copy. We update *out_pos_ptr only if everything
645a645f22SBen Gras // succeeds.
655a645f22SBen Gras size_t out_pos = *out_pos_ptr;
665a645f22SBen Gras
675a645f22SBen Gras // Check that there's enough space for both Stream Header and
685a645f22SBen Gras // Stream Footer.
695a645f22SBen Gras if (out_size - out_pos <= 2 * LZMA_STREAM_HEADER_SIZE)
705a645f22SBen Gras return LZMA_BUF_ERROR;
715a645f22SBen Gras
725a645f22SBen Gras // Reserve space for Stream Footer so we don't need to check for
735a645f22SBen Gras // available space again before encoding Stream Footer.
745a645f22SBen Gras out_size -= LZMA_STREAM_HEADER_SIZE;
755a645f22SBen Gras
765a645f22SBen Gras // Encode the Stream Header.
775a645f22SBen Gras lzma_stream_flags stream_flags = {
785a645f22SBen Gras .version = 0,
795a645f22SBen Gras .check = check,
805a645f22SBen Gras };
815a645f22SBen Gras
825a645f22SBen Gras if (lzma_stream_header_encode(&stream_flags, out + out_pos)
835a645f22SBen Gras != LZMA_OK)
845a645f22SBen Gras return LZMA_PROG_ERROR;
855a645f22SBen Gras
865a645f22SBen Gras out_pos += LZMA_STREAM_HEADER_SIZE;
875a645f22SBen Gras
8811be35a1SLionel Sambuc // Encode a Block but only if there is at least one byte of input.
895a645f22SBen Gras lzma_block block = {
905a645f22SBen Gras .version = 0,
915a645f22SBen Gras .check = check,
925a645f22SBen Gras .filters = filters,
935a645f22SBen Gras };
945a645f22SBen Gras
9511be35a1SLionel Sambuc if (in_size > 0)
965a645f22SBen Gras return_if_error(lzma_block_buffer_encode(&block, allocator,
975a645f22SBen Gras in, in_size, out, &out_pos, out_size));
985a645f22SBen Gras
995a645f22SBen Gras // Index
1005a645f22SBen Gras {
10111be35a1SLionel Sambuc // Create an Index. It will have one Record if there was
10211be35a1SLionel Sambuc // at least one byte of input to encode. Otherwise the
10311be35a1SLionel Sambuc // Index will be empty.
1045a645f22SBen Gras lzma_index *i = lzma_index_init(allocator);
1055a645f22SBen Gras if (i == NULL)
1065a645f22SBen Gras return LZMA_MEM_ERROR;
1075a645f22SBen Gras
10811be35a1SLionel Sambuc lzma_ret ret = LZMA_OK;
10911be35a1SLionel Sambuc
11011be35a1SLionel Sambuc if (in_size > 0)
11111be35a1SLionel Sambuc ret = lzma_index_append(i, allocator,
1125a645f22SBen Gras lzma_block_unpadded_size(&block),
1135a645f22SBen Gras block.uncompressed_size);
1145a645f22SBen Gras
1155a645f22SBen Gras // If adding the Record was successful, encode the Index
1165a645f22SBen Gras // and get its size which will be stored into Stream Footer.
1175a645f22SBen Gras if (ret == LZMA_OK) {
1185a645f22SBen Gras ret = lzma_index_buffer_encode(
1195a645f22SBen Gras i, out, &out_pos, out_size);
1205a645f22SBen Gras
1215a645f22SBen Gras stream_flags.backward_size = lzma_index_size(i);
1225a645f22SBen Gras }
1235a645f22SBen Gras
1245a645f22SBen Gras lzma_index_end(i, allocator);
1255a645f22SBen Gras
1265a645f22SBen Gras if (ret != LZMA_OK)
1275a645f22SBen Gras return ret;
1285a645f22SBen Gras }
1295a645f22SBen Gras
1305a645f22SBen Gras // Stream Footer. We have already reserved space for this.
1315a645f22SBen Gras if (lzma_stream_footer_encode(&stream_flags, out + out_pos)
1325a645f22SBen Gras != LZMA_OK)
1335a645f22SBen Gras return LZMA_PROG_ERROR;
1345a645f22SBen Gras
1355a645f22SBen Gras out_pos += LZMA_STREAM_HEADER_SIZE;
1365a645f22SBen Gras
1375a645f22SBen Gras // Everything went fine, make the new output position available
1385a645f22SBen Gras // to the application.
1395a645f22SBen Gras *out_pos_ptr = out_pos;
1405a645f22SBen Gras return LZMA_OK;
1415a645f22SBen Gras }
142