stream_buffer_encoder.c revision 213700
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
54207753Smm	// Note for the paranoids: Index encoder prevents the Stream from
55207753Smm	// getting too big and still being accepted with LZMA_OK, and Block
56207753Smm	// encoder catches if the input is too big. So we don't need to
57207753Smm	// separately check if the buffers are too big.
58207753Smm
59207753Smm	// Use a local copy. We update *out_pos_ptr only if everything
60207753Smm	// succeeds.
61207753Smm	size_t out_pos = *out_pos_ptr;
62207753Smm
63207753Smm	// Check that there's enough space for both Stream Header and
64207753Smm	// Stream Footer.
65207753Smm	if (out_size - out_pos <= 2 * LZMA_STREAM_HEADER_SIZE)
66207753Smm		return LZMA_BUF_ERROR;
67207753Smm
68207753Smm	// Reserve space for Stream Footer so we don't need to check for
69207753Smm	// available space again before encoding Stream Footer.
70207753Smm	out_size -= LZMA_STREAM_HEADER_SIZE;
71207753Smm
72207753Smm	// Encode the Stream Header.
73207753Smm	lzma_stream_flags stream_flags = {
74207753Smm		.version = 0,
75207753Smm		.check = check,
76207753Smm	};
77207753Smm
78207753Smm	if (lzma_stream_header_encode(&stream_flags, out + out_pos)
79207753Smm			!= LZMA_OK)
80207753Smm		return LZMA_PROG_ERROR;
81207753Smm
82207753Smm	out_pos += LZMA_STREAM_HEADER_SIZE;
83207753Smm
84207753Smm	// Block
85207753Smm	lzma_block block = {
86207753Smm		.version = 0,
87207753Smm		.check = check,
88207753Smm		.filters = filters,
89207753Smm	};
90207753Smm
91207753Smm	return_if_error(lzma_block_buffer_encode(&block, allocator,
92207753Smm			in, in_size, out, &out_pos, out_size));
93207753Smm
94207753Smm	// Index
95207753Smm	{
96207753Smm		// Create an Index with one Record.
97207753Smm		lzma_index *i = lzma_index_init(allocator);
98207753Smm		if (i == NULL)
99207753Smm			return LZMA_MEM_ERROR;
100207753Smm
101207753Smm		lzma_ret ret = lzma_index_append(i, allocator,
102207753Smm				lzma_block_unpadded_size(&block),
103207753Smm				block.uncompressed_size);
104207753Smm
105207753Smm		// If adding the Record was successful, encode the Index
106207753Smm		// and get its size which will be stored into Stream Footer.
107207753Smm		if (ret == LZMA_OK) {
108207753Smm			ret = lzma_index_buffer_encode(
109207753Smm					i, out, &out_pos, out_size);
110207753Smm
111207753Smm			stream_flags.backward_size = lzma_index_size(i);
112207753Smm		}
113207753Smm
114207753Smm		lzma_index_end(i, allocator);
115207753Smm
116207753Smm		if (ret != LZMA_OK)
117207753Smm			return ret;
118207753Smm	}
119207753Smm
120207753Smm	// Stream Footer. We have already reserved space for this.
121207753Smm	if (lzma_stream_footer_encode(&stream_flags, out + out_pos)
122207753Smm			!= LZMA_OK)
123207753Smm		return LZMA_PROG_ERROR;
124207753Smm
125207753Smm	out_pos += LZMA_STREAM_HEADER_SIZE;
126207753Smm
127207753Smm	// Everything went fine, make the new output position available
128207753Smm	// to the application.
129207753Smm	*out_pos_ptr = out_pos;
130207753Smm	return LZMA_OK;
131207753Smm}
132