1207753Smm/////////////////////////////////////////////////////////////////////////////// 2207753Smm// 3207753Smm/// \file stream_buffer_encoder.c 4207753Smm/// \brief Single-call .xz Stream encoder 5207753Smm// 6207753Smm// Author: Lasse Collin 7207753Smm// 8207753Smm// This file has been put into the public domain. 9207753Smm// You can do whatever you want with this file. 10207753Smm// 11207753Smm/////////////////////////////////////////////////////////////////////////////// 12207753Smm 13207753Smm#include "index.h" 14207753Smm 15207753Smm 16207753Smm/// Maximum size of Index that has exactly one Record. 17207753Smm/// Index Indicator + Number of Records + Record + CRC32 rounded up to 18207753Smm/// the next multiple of four. 19207753Smm#define INDEX_BOUND ((1 + 1 + 2 * LZMA_VLI_BYTES_MAX + 4 + 3) & ~3) 20207753Smm 21207753Smm/// Stream Header, Stream Footer, and Index 22207753Smm#define HEADERS_BOUND (2 * LZMA_STREAM_HEADER_SIZE + INDEX_BOUND) 23207753Smm 24207753Smm 25207753Smmextern LZMA_API(size_t) 26207753Smmlzma_stream_buffer_bound(size_t uncompressed_size) 27207753Smm{ 28207753Smm // Get the maximum possible size of a Block. 29207753Smm const size_t block_bound = lzma_block_buffer_bound(uncompressed_size); 30207753Smm if (block_bound == 0) 31207753Smm return 0; 32207753Smm 33207753Smm // Catch the possible integer overflow and also prevent the size of 34207753Smm // the Stream exceeding LZMA_VLI_MAX (theoretically possible on 35207753Smm // 64-bit systems). 36213700Smm if (my_min(SIZE_MAX, LZMA_VLI_MAX) - block_bound < HEADERS_BOUND) 37207753Smm return 0; 38207753Smm 39207753Smm return block_bound + HEADERS_BOUND; 40207753Smm} 41207753Smm 42207753Smm 43207753Smmextern LZMA_API(lzma_ret) 44207753Smmlzma_stream_buffer_encode(lzma_filter *filters, lzma_check check, 45292588Sdelphij const lzma_allocator *allocator, 46292588Sdelphij const uint8_t *in, size_t in_size, 47207753Smm uint8_t *out, size_t *out_pos_ptr, size_t out_size) 48207753Smm{ 49207753Smm // Sanity checks 50207753Smm if (filters == NULL || (unsigned int)(check) > LZMA_CHECK_ID_MAX 51207753Smm || (in == NULL && in_size != 0) || out == NULL 52207753Smm || out_pos_ptr == NULL || *out_pos_ptr > out_size) 53207753Smm return LZMA_PROG_ERROR; 54207753Smm 55223935Smm if (!lzma_check_is_supported(check)) 56223935Smm return LZMA_UNSUPPORTED_CHECK; 57223935Smm 58207753Smm // Note for the paranoids: Index encoder prevents the Stream from 59207753Smm // getting too big and still being accepted with LZMA_OK, and Block 60207753Smm // encoder catches if the input is too big. So we don't need to 61207753Smm // separately check if the buffers are too big. 62207753Smm 63207753Smm // Use a local copy. We update *out_pos_ptr only if everything 64207753Smm // succeeds. 65207753Smm size_t out_pos = *out_pos_ptr; 66207753Smm 67207753Smm // Check that there's enough space for both Stream Header and 68207753Smm // Stream Footer. 69207753Smm if (out_size - out_pos <= 2 * LZMA_STREAM_HEADER_SIZE) 70207753Smm return LZMA_BUF_ERROR; 71207753Smm 72207753Smm // Reserve space for Stream Footer so we don't need to check for 73207753Smm // available space again before encoding Stream Footer. 74207753Smm out_size -= LZMA_STREAM_HEADER_SIZE; 75207753Smm 76207753Smm // Encode the Stream Header. 77207753Smm lzma_stream_flags stream_flags = { 78207753Smm .version = 0, 79207753Smm .check = check, 80207753Smm }; 81207753Smm 82207753Smm if (lzma_stream_header_encode(&stream_flags, out + out_pos) 83207753Smm != LZMA_OK) 84207753Smm return LZMA_PROG_ERROR; 85207753Smm 86207753Smm out_pos += LZMA_STREAM_HEADER_SIZE; 87207753Smm 88223935Smm // Encode a Block but only if there is at least one byte of input. 89207753Smm lzma_block block = { 90207753Smm .version = 0, 91207753Smm .check = check, 92207753Smm .filters = filters, 93207753Smm }; 94207753Smm 95223935Smm if (in_size > 0) 96223935Smm return_if_error(lzma_block_buffer_encode(&block, allocator, 97223935Smm in, in_size, out, &out_pos, out_size)); 98207753Smm 99207753Smm // Index 100207753Smm { 101223935Smm // Create an Index. It will have one Record if there was 102223935Smm // at least one byte of input to encode. Otherwise the 103223935Smm // Index will be empty. 104207753Smm lzma_index *i = lzma_index_init(allocator); 105207753Smm if (i == NULL) 106207753Smm return LZMA_MEM_ERROR; 107207753Smm 108223935Smm lzma_ret ret = LZMA_OK; 109207753Smm 110223935Smm if (in_size > 0) 111223935Smm ret = lzma_index_append(i, allocator, 112223935Smm lzma_block_unpadded_size(&block), 113223935Smm block.uncompressed_size); 114223935Smm 115207753Smm // If adding the Record was successful, encode the Index 116207753Smm // and get its size which will be stored into Stream Footer. 117207753Smm if (ret == LZMA_OK) { 118207753Smm ret = lzma_index_buffer_encode( 119207753Smm i, out, &out_pos, out_size); 120207753Smm 121207753Smm stream_flags.backward_size = lzma_index_size(i); 122207753Smm } 123207753Smm 124207753Smm lzma_index_end(i, allocator); 125207753Smm 126207753Smm if (ret != LZMA_OK) 127207753Smm return ret; 128207753Smm } 129207753Smm 130207753Smm // Stream Footer. We have already reserved space for this. 131207753Smm if (lzma_stream_footer_encode(&stream_flags, out + out_pos) 132207753Smm != LZMA_OK) 133207753Smm return LZMA_PROG_ERROR; 134207753Smm 135207753Smm out_pos += LZMA_STREAM_HEADER_SIZE; 136207753Smm 137207753Smm // Everything went fine, make the new output position available 138207753Smm // to the application. 139207753Smm *out_pos_ptr = out_pos; 140207753Smm return LZMA_OK; 141207753Smm} 142