]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/xz/src/liblzma/common/stream_buffer_encoder.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / xz / src / liblzma / common / stream_buffer_encoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       stream_buffer_encoder.c
4 /// \brief      Single-call .xz Stream encoder
5 //
6 //  Author:     Lasse Collin
7 //
8 //  This file has been put into the public domain.
9 //  You can do whatever you want with this file.
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12
13 #include "index.h"
14
15
16 /// Maximum size of Index that has exactly one Record.
17 /// Index Indicator + Number of Records + Record + CRC32 rounded up to
18 /// the next multiple of four.
19 #define INDEX_BOUND ((1 + 1 + 2 * LZMA_VLI_BYTES_MAX + 4 + 3) & ~3)
20
21 /// Stream Header, Stream Footer, and Index
22 #define HEADERS_BOUND (2 * LZMA_STREAM_HEADER_SIZE + INDEX_BOUND)
23
24
25 extern LZMA_API(size_t)
26 lzma_stream_buffer_bound(size_t uncompressed_size)
27 {
28         // Get the maximum possible size of a Block.
29         const size_t block_bound = lzma_block_buffer_bound(uncompressed_size);
30         if (block_bound == 0)
31                 return 0;
32
33         // Catch the possible integer overflow and also prevent the size of
34         // the Stream exceeding LZMA_VLI_MAX (theoretically possible on
35         // 64-bit systems).
36         if (my_min(SIZE_MAX, LZMA_VLI_MAX) - block_bound < HEADERS_BOUND)
37                 return 0;
38
39         return block_bound + HEADERS_BOUND;
40 }
41
42
43 extern LZMA_API(lzma_ret)
44 lzma_stream_buffer_encode(lzma_filter *filters, lzma_check check,
45                 lzma_allocator *allocator, const uint8_t *in, size_t in_size,
46                 uint8_t *out, size_t *out_pos_ptr, size_t out_size)
47 {
48         // Sanity checks
49         if (filters == NULL || (unsigned int)(check) > LZMA_CHECK_ID_MAX
50                         || (in == NULL && in_size != 0) || out == NULL
51                         || out_pos_ptr == NULL || *out_pos_ptr > out_size)
52                 return LZMA_PROG_ERROR;
53
54         if (!lzma_check_is_supported(check))
55                 return LZMA_UNSUPPORTED_CHECK;
56
57         // Note for the paranoids: Index encoder prevents the Stream from
58         // getting too big and still being accepted with LZMA_OK, and Block
59         // encoder catches if the input is too big. So we don't need to
60         // separately check if the buffers are too big.
61
62         // Use a local copy. We update *out_pos_ptr only if everything
63         // succeeds.
64         size_t out_pos = *out_pos_ptr;
65
66         // Check that there's enough space for both Stream Header and
67         // Stream Footer.
68         if (out_size - out_pos <= 2 * LZMA_STREAM_HEADER_SIZE)
69                 return LZMA_BUF_ERROR;
70
71         // Reserve space for Stream Footer so we don't need to check for
72         // available space again before encoding Stream Footer.
73         out_size -= LZMA_STREAM_HEADER_SIZE;
74
75         // Encode the Stream Header.
76         lzma_stream_flags stream_flags = {
77                 .version = 0,
78                 .check = check,
79         };
80
81         if (lzma_stream_header_encode(&stream_flags, out + out_pos)
82                         != LZMA_OK)
83                 return LZMA_PROG_ERROR;
84
85         out_pos += LZMA_STREAM_HEADER_SIZE;
86
87         // Encode a Block but only if there is at least one byte of input.
88         lzma_block block = {
89                 .version = 0,
90                 .check = check,
91                 .filters = filters,
92         };
93
94         if (in_size > 0)
95                 return_if_error(lzma_block_buffer_encode(&block, allocator,
96                                 in, in_size, out, &out_pos, out_size));
97
98         // Index
99         {
100                 // Create an Index. It will have one Record if there was
101                 // at least one byte of input to encode. Otherwise the
102                 // Index will be empty.
103                 lzma_index *i = lzma_index_init(allocator);
104                 if (i == NULL)
105                         return LZMA_MEM_ERROR;
106
107                 lzma_ret ret = LZMA_OK;
108
109                 if (in_size > 0)
110                         ret = lzma_index_append(i, allocator,
111                                         lzma_block_unpadded_size(&block),
112                                         block.uncompressed_size);
113
114                 // If adding the Record was successful, encode the Index
115                 // and get its size which will be stored into Stream Footer.
116                 if (ret == LZMA_OK) {
117                         ret = lzma_index_buffer_encode(
118                                         i, out, &out_pos, out_size);
119
120                         stream_flags.backward_size = lzma_index_size(i);
121                 }
122
123                 lzma_index_end(i, allocator);
124
125                 if (ret != LZMA_OK)
126                         return ret;
127         }
128
129         // Stream Footer. We have already reserved space for this.
130         if (lzma_stream_footer_encode(&stream_flags, out + out_pos)
131                         != LZMA_OK)
132                 return LZMA_PROG_ERROR;
133
134         out_pos += LZMA_STREAM_HEADER_SIZE;
135
136         // Everything went fine, make the new output position available
137         // to the application.
138         *out_pos_ptr = out_pos;
139         return LZMA_OK;
140 }