From 9ec80355a7212a0a2f8c89d98e51b1d8b4e34eec Mon Sep 17 00:00:00 2001 From: Lasse Collin Date: Tue, 20 Jan 2009 16:37:27 +0200 Subject: Add some single-call buffer-to-buffer coding functions. --- src/liblzma/api/lzma/block.h | 57 ++++++++++++++++++++++++++++++++ src/liblzma/api/lzma/container.h | 56 ++++++++++++++++++++++++++++++++ src/liblzma/api/lzma/index.h | 70 ++++++++++++++++++++++++++++++++++++---- 3 files changed, 177 insertions(+), 6 deletions(-) (limited to 'src/liblzma/api/lzma') diff --git a/src/liblzma/api/lzma/block.h b/src/liblzma/api/lzma/block.h index a747b145..3a49be3a 100644 --- a/src/liblzma/api/lzma/block.h +++ b/src/liblzma/api/lzma/block.h @@ -55,6 +55,7 @@ typedef struct { * - lzma_block_total_size() * - lzma_block_encoder() * - lzma_block_decoder() + * - lzma_block_buffer_encode() * * Written by: * - lzma_block_header_decode() @@ -76,6 +77,7 @@ typedef struct { * * Written by: * - lzma_block_header_size() + * - lzma_block_buffer_encode() */ uint32_t header_size; # define LZMA_BLOCK_HEADER_SIZE_MIN 8 @@ -95,6 +97,7 @@ typedef struct { * - lzma_block_total_size() * - lzma_block_encoder() * - lzma_block_decoder() + * - lzma_block_buffer_encode() */ lzma_check check; @@ -147,6 +150,7 @@ typedef struct { * - lzma_block_compressed_size() * - lzma_block_encoder() * - lzma_block_decoder() + * - lzma_block_buffer_encode() */ lzma_vli compressed_size; @@ -168,6 +172,7 @@ typedef struct { * - lzma_block_header_decode() * - lzma_block_encoder() * - lzma_block_decoder() + * - lzma_block_buffer_encode() */ lzma_vli uncompressed_size; @@ -182,6 +187,7 @@ typedef struct { * - lzma_block_header_encode() * - lzma_block_encoder() * - lzma_block_decoder() + * - lzma_block_buffer_encode() * * Written by: * - lzma_block_header_decode(): Note that this does NOT free() @@ -415,3 +421,54 @@ extern lzma_ret lzma_block_encoder(lzma_stream *strm, lzma_block *block) */ extern lzma_ret lzma_block_decoder(lzma_stream *strm, lzma_block *block) lzma_attr_warn_unused_result; + + +/** + * \brief Calculate maximum output buffer size for single-call encoding + * + * This is equivalent to lzma_stream_buffer_bound() but for .xz Blocks. + * See the documentation of lzma_stream_buffer_bound(). + */ +extern size_t lzma_block_buffer_bound(size_t uncompressed_size); + + +/** + * \brief Single-call .xz Block encoder + * + * In contrast to the multi-call encoder initialized with + * lzma_block_encoder(), this function encodes also the Block Header. This + * is required to make it possible to write appropriate Block Header also + * in case the data isn't compressible, and different filter chain has to be + * used to encode the data in uncompressed form using uncompressed chunks + * of the LZMA2 filter. + * + * When the data isn't compressible, header_size, compressed_size, and + * uncompressed_size are set just like when the data was compressible, but + * it is possible that header_size is too small to hold the filter chain + * specified in block->filters, because that isn't necessarily the filter + * chain that was actually used to encode the data. lzma_block_unpadded_size() + * still works normally, because it doesn't read the filters array. + * + * \param block Block options: block->version, block->check, + * and block->filters must be initialized. + * \param allocator lzma_allocator for custom allocator functions. + * Set to NULL to use malloc() and free(). + * \param in Beginning of the input buffer + * \param in_size Size of the input buffer + * \param out Beginning of the output buffer + * \param out_pos The next byte will be written to out[*out_pos]. + * *out_pos is updated only if encoding succeeds. + * \param out_size Size of the out buffer; the first byte into + * which no data is written to is out[out_size]. + * + * \return - LZMA_OK: Encoding was successful. + * - LZMA_BUF_ERROR: Not enough output buffer space. + * - LZMA_OPTIONS_ERROR + * - LZMA_MEM_ERROR + * - LZMA_DATA_ERROR + * - LZMA_PROG_ERROR + */ +extern lzma_ret lzma_block_buffer_encode( + lzma_block *block, lzma_allocator *allocator, + const uint8_t *in, size_t in_size, + uint8_t *out, size_t *out_pos, size_t out_size); diff --git a/src/liblzma/api/lzma/container.h b/src/liblzma/api/lzma/container.h index f5c0e7ab..240d5dfb 100644 --- a/src/liblzma/api/lzma/container.h +++ b/src/liblzma/api/lzma/container.h @@ -169,6 +169,62 @@ extern lzma_ret lzma_alone_encoder( lzma_attr_warn_unused_result; +/** + * \brief Calculate output buffer size for single-call Stream encoder + * + * When trying to compress uncompressible data, the encoded size will be + * slightly bigger than the input data. This function calculates how much + * output buffer space is required to be sure that lzma_stream_buffer_encode() + * doesn't return LZMA_BUF_ERROR. + * + * The calculated value is not exact, but it is guaranteed to be big enough. + * The actual maximum output space required may be slightly smaller (up to + * about 100 bytes). This should not be a problem in practice. + * + * If the calculated maximum size doesn't fit into size_t or would make the + * Stream grow past LZMA_VLI_MAX (which should never happen in practice), + * zero is returned to indicate the error. + * + * \note The limit calculated by this function applies only to + * single-call encoding. Multi-call encoding may (and probably + * will) have larger maximum expansion when encoding + * uncompressible data. Currently there is no function to + * calculate the maximum expansion of multi-call encoding. + */ +extern size_t lzma_stream_buffer_bound(size_t uncompressed_size); + + +/** + * \brief Single-call Stream encoder + * + * \param filters Array of filters. This must be terminated with + * filters[n].id = LZMA_VLI_UNKNOWN. See filter.h + * for more information. + * \param check Type of the integrity check to calculate from + * uncompressed data. + * \param allocator lzma_allocator for custom allocator functions. + * Set to NULL to use malloc() and free(). + * \param in Beginning of the input buffer + * \param in_size Size of the input buffer + * \param out Beginning of the output buffer + * \param out_pos The next byte will be written to out[*out_pos]. + * *out_pos is updated only if encoding succeeds. + * \param out_size Size of the out buffer; the first byte into + * which no data is written to is out[out_size]. + * + * \return - LZMA_OK: Encoding was successful. + * - LZMA_BUF_ERROR: Not enough output buffer space. + * - LZMA_OPTIONS_ERROR + * - LZMA_MEM_ERROR + * - LZMA_DATA_ERROR + * - LZMA_PROG_ERROR + */ +extern lzma_ret lzma_stream_buffer_encode( + lzma_filter *filters, lzma_check check, + lzma_allocator *allocator, const uint8_t *in, size_t in_size, + uint8_t *out, size_t *out_pos, size_t out_size); + + /************ * Decoding * ************/ diff --git a/src/liblzma/api/lzma/index.h b/src/liblzma/api/lzma/index.h index 9d6b7550..9af296dd 100644 --- a/src/liblzma/api/lzma/index.h +++ b/src/liblzma/api/lzma/index.h @@ -255,7 +255,7 @@ extern lzma_ret lzma_index_cat(lzma_index *lzma_restrict dest, /** - * \brief Duplicates an Index list + * \brief Duplicate an Index list * * Makes an identical copy of the Index. Also the read position is copied. * @@ -267,7 +267,7 @@ extern lzma_index *lzma_index_dup( /** - * \brief Compares if two Index lists are identical + * \brief Compare if two Index lists are identical * * \return True if *a and *b are equal, false otherwise. */ @@ -276,7 +276,7 @@ extern lzma_bool lzma_index_equal(const lzma_index *a, const lzma_index *b) /** - * \brief Initializes Index encoder + * \brief Initialize Index encoder * * \param strm Pointer to properly prepared lzma_stream * \param i Pointer to lzma_index which should be encoded. @@ -294,14 +294,15 @@ extern lzma_ret lzma_index_encoder(lzma_stream *strm, lzma_index *i) /** - * \brief Initializes Index decoder + * \brief Initialize Index decoder * * \param strm Pointer to properly prepared lzma_stream * \param i Pointer to a pointer that will be made to point * to the final decoded Index once lzma_code() has * returned LZMA_STREAM_END. That is, - * lzma_index_decoder() takes care of allocating - * a new lzma_index structure. + * lzma_index_decoder() always takes care of + * allocating a new lzma_index structure, and *i + * doesn't need to be initialized by the caller. * \param memlimit How much memory the resulting Index is allowed * to require. * @@ -321,3 +322,60 @@ extern lzma_ret lzma_index_encoder(lzma_stream *strm, lzma_index *i) extern lzma_ret lzma_index_decoder( lzma_stream *strm, lzma_index **i, uint64_t memlimit) lzma_attr_warn_unused_result; + + +/** + * \brief Single-call Index encoder + * + * \param i Index to be encoded. The read position will be at + * the end of the Index if encoding succeeds, or at + * unspecified position in case an error occurs. + * \param out Beginning of the output buffer + * \param out_pos The next byte will be written to out[*out_pos]. + * *out_pos is updated only if encoding succeeds. + * \param out_size Size of the out buffer; the first byte into + * which no data is written to is out[out_size]. + * + * \return - LZMA_OK: Encoding was successful. + * - LZMA_BUF_ERROR: Output buffer is too small. Use + * lzma_index_size() to find out how much output + * space is needed. + * - LZMA_PROG_ERROR + * + * \note This function doesn't take allocator argument since all + * the internal data is allocated on stack. + */ +extern lzma_ret lzma_index_buffer_encode(lzma_index *i, + uint8_t *out, size_t *out_pos, size_t out_size); + + +/** + * \brief Single-call Index decoder + * + * \param i Pointer to a pointer that will be made to point + * to the final decoded Index if decoding is + * successful. That is, lzma_index_buffer_decode() + * always takes care of allocating a new + * lzma_index structure, and *i doesn't need to be + * initialized by the caller. + * \param memlimit Pointer to how much memory the resulting Index + * is allowed to require. The value pointed by + * this pointer is modified if and only if + * LZMA_MEMLIMIT_ERROR is returned. + * \param allocator Pointer to lzma_allocator, or NULL to use malloc() + * \param in Beginning of the input buffer + * \param in_pos The next byte will be read from in[*in_pos]. + * *in_pos is updated only if decoding succeeds. + * \param in_size Size of the input buffer; the first byte that + * won't be read is in[in_size]. + * + * \return - LZMA_OK: Decoding was successful. + * - LZMA_MEM_ERROR + * - LZMA_MEMLIMIT_ERROR: Memory usage limit was reached. + * The minimum required memlimit value was stored to *memlimit. + * - LZMA_DATA_ERROR + * - LZMA_PROG_ERROR + */ +extern lzma_ret lzma_index_buffer_decode( + lzma_index **i, uint64_t *memlimit, lzma_allocator *allocator, + const uint8_t *in, size_t *in_pos, size_t in_size); -- cgit v1.2.3