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, 45207753Smm lzma_allocator *allocator, const uint8_t *in, size_t in_size, 46207753Smm uint8_t *out, size_t *out_pos_ptr, size_t out_size) 47207753Smm{ 48207753Smm // Sanity checks 49207753Smm if (filters == NULL || (unsigned int)(check) > LZMA_CHECK_ID_MAX 50207753Smm || (in == NULL && in_size != 0) || out == NULL 51207753Smm || out_pos_ptr == NULL || *out_pos_ptr > out_size) 52207753Smm return LZMA_PROG_ERROR; 53207753Smm 54223935Smm if (!lzma_check_is_supported(check)) 55223935Smm return LZMA_UNSUPPORTED_CHECK; 56223935Smm 57207753Smm // Note for the paranoids: Index encoder prevents the Stream from 58207753Smm // getting too big and still being accepted with LZMA_OK, and Block 59207753Smm // encoder catches if the input is too big. So we don't need to 60207753Smm // separately check if the buffers are too big. 61207753Smm 62207753Smm // Use a local copy. We update *out_pos_ptr only if everything 63207753Smm // succeeds. 64207753Smm size_t out_pos = *out_pos_ptr; 65207753Smm 66207753Smm // Check that there's enough space for both Stream Header and 67207753Smm // Stream Footer. 68207753Smm if (out_size - out_pos <= 2 * LZMA_STREAM_HEADER_SIZE) 69207753Smm return LZMA_BUF_ERROR; 70207753Smm 71207753Smm // Reserve space for Stream Footer so we don't need to check for 72207753Smm // available space again before encoding Stream Footer. 73207753Smm out_size -= LZMA_STREAM_HEADER_SIZE; 74207753Smm 75207753Smm // Encode the Stream Header. 76207753Smm lzma_stream_flags stream_flags = { 77207753Smm .version = 0, 78207753Smm .check = check, 79207753Smm }; 80207753Smm 81207753Smm if (lzma_stream_header_encode(&stream_flags, out + out_pos) 82207753Smm != LZMA_OK) 83207753Smm return LZMA_PROG_ERROR; 84207753Smm 85207753Smm out_pos += LZMA_STREAM_HEADER_SIZE; 86207753Smm 87223935Smm // Encode a Block but only if there is at least one byte of input. 88207753Smm lzma_block block = { 89207753Smm .version = 0, 90207753Smm .check = check, 91207753Smm .filters = filters, 92207753Smm }; 93207753Smm 94223935Smm if (in_size > 0) 95223935Smm return_if_error(lzma_block_buffer_encode(&block, allocator, 96223935Smm in, in_size, out, &out_pos, out_size)); 97207753Smm 98207753Smm // Index 99207753Smm { 100223935Smm // Create an Index. It will have one Record if there was 101223935Smm // at least one byte of input to encode. Otherwise the 102223935Smm // Index will be empty. 103207753Smm lzma_index *i = lzma_index_init(allocator); 104207753Smm if (i == NULL) 105207753Smm return LZMA_MEM_ERROR; 106207753Smm 107223935Smm lzma_ret ret = LZMA_OK; 108207753Smm 109223935Smm if (in_size > 0) 110223935Smm ret = lzma_index_append(i, allocator, 111223935Smm lzma_block_unpadded_size(&block), 112223935Smm block.uncompressed_size); 113223935Smm 114207753Smm // If adding the Record was successful, encode the Index 115207753Smm // and get its size which will be stored into Stream Footer. 116207753Smm if (ret == LZMA_OK) { 117207753Smm ret = lzma_index_buffer_encode( 118207753Smm i, out, &out_pos, out_size); 119207753Smm 120207753Smm stream_flags.backward_size = lzma_index_size(i); 121207753Smm } 122207753Smm 123207753Smm lzma_index_end(i, allocator); 124207753Smm 125207753Smm if (ret != LZMA_OK) 126207753Smm return ret; 127207753Smm } 128207753Smm 129207753Smm // Stream Footer. We have already reserved space for this. 130207753Smm if (lzma_stream_footer_encode(&stream_flags, out + out_pos) 131207753Smm != LZMA_OK) 132207753Smm return LZMA_PROG_ERROR; 133207753Smm 134207753Smm out_pos += LZMA_STREAM_HEADER_SIZE; 135207753Smm 136207753Smm // Everything went fine, make the new output position available 137207753Smm // to the application. 138207753Smm *out_pos_ptr = out_pos; 139207753Smm return LZMA_OK; 140207753Smm} 141