stream_buffer_encoder.c revision 213700
1178431Sscf/////////////////////////////////////////////////////////////////////////////// 2178431Sscf// 3178431Sscf/// \file stream_buffer_encoder.c 4178431Sscf/// \brief Single-call .xz Stream encoder 5178431Sscf// 6178431Sscf// Author: Lasse Collin 7178431Sscf// 8178431Sscf// This file has been put into the public domain. 9178431Sscf// You can do whatever you want with this file. 10178431Sscf// 11178431Sscf/////////////////////////////////////////////////////////////////////////////// 12178431Sscf 13178431Sscf#include "index.h" 14178431Sscf 15178431Sscf 16178431Sscf/// Maximum size of Index that has exactly one Record. 17178431Sscf/// Index Indicator + Number of Records + Record + CRC32 rounded up to 18178431Sscf/// the next multiple of four. 19178431Sscf#define INDEX_BOUND ((1 + 1 + 2 * LZMA_VLI_BYTES_MAX + 4 + 3) & ~3) 20178431Sscf 21178431Sscf/// Stream Header, Stream Footer, and Index 22178431Sscf#define HEADERS_BOUND (2 * LZMA_STREAM_HEADER_SIZE + INDEX_BOUND) 23178431Sscf 24178431Sscf 25178431Sscfextern LZMA_API(size_t) 26178431Sscflzma_stream_buffer_bound(size_t uncompressed_size) 27178431Sscf{ 28178431Sscf // Get the maximum possible size of a Block. 29178431Sscf const size_t block_bound = lzma_block_buffer_bound(uncompressed_size); 30178431Sscf if (block_bound == 0) 31178431Sscf return 0; 32178431Sscf 33178431Sscf // Catch the possible integer overflow and also prevent the size of 34178431Sscf // the Stream exceeding LZMA_VLI_MAX (theoretically possible on 35178431Sscf // 64-bit systems). 36178431Sscf if (my_min(SIZE_MAX, LZMA_VLI_MAX) - block_bound < HEADERS_BOUND) 37178431Sscf return 0; 38178431Sscf 39178431Sscf return block_bound + HEADERS_BOUND; 40178431Sscf} 41178431Sscf 42178431Sscf 43178431Sscfextern LZMA_API(lzma_ret) 44178431Sscflzma_stream_buffer_encode(lzma_filter *filters, lzma_check check, 45178431Sscf lzma_allocator *allocator, const uint8_t *in, size_t in_size, 46178431Sscf uint8_t *out, size_t *out_pos_ptr, size_t out_size) 47178431Sscf{ 48178431Sscf // Sanity checks 49178431Sscf if (filters == NULL || (unsigned int)(check) > LZMA_CHECK_ID_MAX 50178431Sscf || (in == NULL && in_size != 0) || out == NULL 51178431Sscf || out_pos_ptr == NULL || *out_pos_ptr > out_size) 52178431Sscf return LZMA_PROG_ERROR; 53178431Sscf 54178431Sscf // Note for the paranoids: Index encoder prevents the Stream from 55178431Sscf // getting too big and still being accepted with LZMA_OK, and Block 56178431Sscf // encoder catches if the input is too big. So we don't need to 57178431Sscf // separately check if the buffers are too big. 58178431Sscf 59178431Sscf // Use a local copy. We update *out_pos_ptr only if everything 60178431Sscf // succeeds. 61178431Sscf size_t out_pos = *out_pos_ptr; 62178431Sscf 63178431Sscf // Check that there's enough space for both Stream Header and 64178431Sscf // Stream Footer. 65178431Sscf if (out_size - out_pos <= 2 * LZMA_STREAM_HEADER_SIZE) 66178431Sscf return LZMA_BUF_ERROR; 67199211Sdes 68178431Sscf // Reserve space for Stream Footer so we don't need to check for 69178431Sscf // available space again before encoding Stream Footer. 70178431Sscf out_size -= LZMA_STREAM_HEADER_SIZE; 71178431Sscf 72178431Sscf // Encode the Stream Header. 73178431Sscf lzma_stream_flags stream_flags = { 74178431Sscf .version = 0, 75178431Sscf .check = check, 76178431Sscf }; 77178431Sscf 78178431Sscf if (lzma_stream_header_encode(&stream_flags, out + out_pos) 79178431Sscf != LZMA_OK) 80178431Sscf return LZMA_PROG_ERROR; 81178431Sscf 82178431Sscf out_pos += LZMA_STREAM_HEADER_SIZE; 83178431Sscf 84178431Sscf // Block 85178431Sscf lzma_block block = { 86178431Sscf .version = 0, 87178431Sscf .check = check, 88178431Sscf .filters = filters, 89178431Sscf }; 90178431Sscf 91178431Sscf return_if_error(lzma_block_buffer_encode(&block, allocator, 92178431Sscf in, in_size, out, &out_pos, out_size)); 93178431Sscf 94178431Sscf // Index 95178431Sscf { 96178431Sscf // Create an Index with one Record. 97178431Sscf lzma_index *i = lzma_index_init(allocator); 98178431Sscf if (i == NULL) 99178431Sscf return LZMA_MEM_ERROR; 100178431Sscf 101178431Sscf lzma_ret ret = lzma_index_append(i, allocator, 102178431Sscf lzma_block_unpadded_size(&block), 103178431Sscf block.uncompressed_size); 104178431Sscf 105178431Sscf // If adding the Record was successful, encode the Index 106178431Sscf // and get its size which will be stored into Stream Footer. 107178431Sscf if (ret == LZMA_OK) { 108178431Sscf ret = lzma_index_buffer_encode( 109178431Sscf i, out, &out_pos, out_size); 110178431Sscf 111178431Sscf stream_flags.backward_size = lzma_index_size(i); 112178431Sscf } 113178431Sscf 114178431Sscf lzma_index_end(i, allocator); 115178431Sscf 116178431Sscf if (ret != LZMA_OK) 117178431Sscf return ret; 118 } 119 120 // Stream Footer. We have already reserved space for this. 121 if (lzma_stream_footer_encode(&stream_flags, out + out_pos) 122 != LZMA_OK) 123 return LZMA_PROG_ERROR; 124 125 out_pos += LZMA_STREAM_HEADER_SIZE; 126 127 // Everything went fine, make the new output position available 128 // to the application. 129 *out_pos_ptr = out_pos; 130 return LZMA_OK; 131} 132