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