~ubuntu-branches/ubuntu/trusty/mariadb-5.5/trusty-proposed

« back to all changes in this revision

Viewing changes to storage/tokudb/ft-index/third_party/xz-4.999.9beta/src/liblzma/common/block_decoder.c

  • Committer: Package Import Robot
  • Author(s): James Page, Otto Kekäläinen
  • Date: 2014-02-17 16:51:52 UTC
  • mfrom: (2.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20140217165152-k315d3175g865kkx
Tags: 5.5.35-1
[ Otto Kekäläinen ]
* New upstream release, fixing the following security issues:
  - Buffer overflow in client/mysql.cc (Closes: #737597).
    - CVE-2014-0001
  - http://www.oracle.com/technetwork/topics/security/cpujan2014-1972949.html
    - CVE-2013-5891
    - CVE-2013-5908
    - CVE-2014-0386
    - CVE-2014-0393
    - CVE-2014-0401
    - CVE-2014-0402
    - CVE-2014-0412
    - CVE-2014-0420
    - CVE-2014-0437
* Upstream https://mariadb.atlassian.net/browse/MDEV-4902
  fixes compatibility with Bison 3.0 (Closes: #733002)
* Updated Russian debconf translation (Closes: #734426)
* Updated Japanese debconf translation (Closes: #735284)
* Updated French debconf translation (Closes: #736480)
* Renamed SONAME properly (Closes: #732967)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
 
2
// vim: expandtab:ts=8:sw=4:softtabstop=4:
 
3
///////////////////////////////////////////////////////////////////////////////
 
4
//
 
5
/// \file       block_decoder.c
 
6
/// \brief      Decodes .xz Blocks
 
7
//
 
8
//  Author:     Lasse Collin
 
9
//
 
10
//  This file has been put into the public domain.
 
11
//  You can do whatever you want with this file.
 
12
//
 
13
///////////////////////////////////////////////////////////////////////////////
 
14
 
 
15
#include "block_decoder.h"
 
16
#include "filter_decoder.h"
 
17
#include "check.h"
 
18
 
 
19
 
 
20
struct lzma_coder_s {
 
21
        enum {
 
22
                SEQ_CODE,
 
23
                SEQ_PADDING,
 
24
                SEQ_CHECK,
 
25
        } sequence;
 
26
 
 
27
        /// The filters in the chain; initialized with lzma_raw_decoder_init().
 
28
        lzma_next_coder next;
 
29
 
 
30
        /// Decoding options; we also write Compressed Size and Uncompressed
 
31
        /// Size back to this structure when the decoding has been finished.
 
32
        lzma_block *block;
 
33
 
 
34
        /// Compressed Size calculated while decoding
 
35
        lzma_vli compressed_size;
 
36
 
 
37
        /// Uncompressed Size calculated while decoding
 
38
        lzma_vli uncompressed_size;
 
39
 
 
40
        /// Maximum allowed Compressed Size; this takes into account the
 
41
        /// size of the Block Header and Check fields when Compressed Size
 
42
        /// is unknown.
 
43
        lzma_vli compressed_limit;
 
44
 
 
45
        /// Position when reading the Check field
 
46
        size_t check_pos;
 
47
 
 
48
        /// Check of the uncompressed data
 
49
        lzma_check_state check;
 
50
};
 
51
 
 
52
 
 
53
static inline bool
 
54
update_size(lzma_vli *size, lzma_vli add, lzma_vli limit)
 
55
{
 
56
        if (limit > LZMA_VLI_MAX)
 
57
                limit = LZMA_VLI_MAX;
 
58
 
 
59
        if (limit < *size || limit - *size < add)
 
60
                return true;
 
61
 
 
62
        *size += add;
 
63
 
 
64
        return false;
 
65
}
 
66
 
 
67
 
 
68
static inline bool
 
69
is_size_valid(lzma_vli size, lzma_vli reference)
 
70
{
 
71
        return reference == LZMA_VLI_UNKNOWN || reference == size;
 
72
}
 
73
 
 
74
 
 
75
static lzma_ret
 
76
block_decode(lzma_coder *coder, lzma_allocator *allocator,
 
77
                const uint8_t *restrict in, size_t *restrict in_pos,
 
78
                size_t in_size, uint8_t *restrict out,
 
79
                size_t *restrict out_pos, size_t out_size, lzma_action action)
 
80
{
 
81
        switch (coder->sequence) {
 
82
        case SEQ_CODE: {
 
83
                const size_t in_start = *in_pos;
 
84
                const size_t out_start = *out_pos;
 
85
 
 
86
                const lzma_ret ret = coder->next.code(coder->next.coder,
 
87
                                allocator, in, in_pos, in_size,
 
88
                                out, out_pos, out_size, action);
 
89
 
 
90
                const size_t in_used = *in_pos - in_start;
 
91
                const size_t out_used = *out_pos - out_start;
 
92
 
 
93
                // NOTE: We compare to compressed_limit here, which prevents
 
94
                // the total size of the Block growing past LZMA_VLI_MAX.
 
95
                if (update_size(&coder->compressed_size, in_used,
 
96
                                        coder->compressed_limit)
 
97
                                || update_size(&coder->uncompressed_size,
 
98
                                        out_used,
 
99
                                        coder->block->uncompressed_size))
 
100
                        return LZMA_DATA_ERROR;
 
101
 
 
102
                lzma_check_update(&coder->check, coder->block->check,
 
103
                                out + out_start, out_used);
 
104
 
 
105
                if (ret != LZMA_STREAM_END)
 
106
                        return ret;
 
107
 
 
108
                // Compressed and Uncompressed Sizes are now at their final
 
109
                // values. Verify that they match the values given to us.
 
110
                if (!is_size_valid(coder->compressed_size,
 
111
                                        coder->block->compressed_size)
 
112
                                || !is_size_valid(coder->uncompressed_size,
 
113
                                        coder->block->uncompressed_size))
 
114
                        return LZMA_DATA_ERROR;
 
115
 
 
116
                // Copy the values into coder->block. The caller
 
117
                // may use this information to construct Index.
 
118
                coder->block->compressed_size = coder->compressed_size;
 
119
                coder->block->uncompressed_size = coder->uncompressed_size;
 
120
 
 
121
                coder->sequence = SEQ_PADDING;
 
122
        }
 
123
 
 
124
        // Fall through
 
125
 
 
126
        case SEQ_PADDING:
 
127
                // Compressed Data is padded to a multiple of four bytes.
 
128
                while (coder->compressed_size & 3) {
 
129
                        if (*in_pos >= in_size)
 
130
                                return LZMA_OK;
 
131
 
 
132
                        // We use compressed_size here just get the Padding
 
133
                        // right. The actual Compressed Size was stored to
 
134
                        // coder->block already, and won't be modified by
 
135
                        // us anymore.
 
136
                        ++coder->compressed_size;
 
137
 
 
138
                        if (in[(*in_pos)++] != 0x00)
 
139
                                return LZMA_DATA_ERROR;
 
140
                }
 
141
 
 
142
                if (coder->block->check == LZMA_CHECK_NONE)
 
143
                        return LZMA_STREAM_END;
 
144
 
 
145
                lzma_check_finish(&coder->check, coder->block->check);
 
146
                coder->sequence = SEQ_CHECK;
 
147
 
 
148
        // Fall through
 
149
 
 
150
        case SEQ_CHECK: {
 
151
                const size_t check_size = lzma_check_size(coder->block->check);
 
152
                lzma_bufcpy(in, in_pos, in_size, coder->block->raw_check,
 
153
                                &coder->check_pos, check_size);
 
154
                if (coder->check_pos < check_size)
 
155
                        return LZMA_OK;
 
156
 
 
157
                // Validate the Check only if we support it.
 
158
                // coder->check.buffer may be uninitialized
 
159
                // when the Check ID is not supported.
 
160
                if (lzma_check_is_supported(coder->block->check)
 
161
                                && memcmp(coder->block->raw_check,
 
162
                                        coder->check.buffer.u8,
 
163
                                        check_size) != 0)
 
164
                        return LZMA_DATA_ERROR;
 
165
 
 
166
                return LZMA_STREAM_END;
 
167
        }
 
168
        }
 
169
 
 
170
        return LZMA_PROG_ERROR;
 
171
}
 
172
 
 
173
 
 
174
static void
 
175
block_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
 
176
{
 
177
        lzma_next_end(&coder->next, allocator);
 
178
        lzma_free(coder, allocator);
 
179
        return;
 
180
}
 
181
 
 
182
 
 
183
extern lzma_ret
 
184
lzma_block_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
 
185
                lzma_block *block)
 
186
{
 
187
        lzma_next_coder_init(&lzma_block_decoder_init, next, allocator);
 
188
 
 
189
        // Validate the options. lzma_block_unpadded_size() does that for us
 
190
        // except for Uncompressed Size and filters. Filters are validated
 
191
        // by the raw decoder.
 
192
        if (lzma_block_unpadded_size(block) == 0
 
193
                        || !lzma_vli_is_valid(block->uncompressed_size))
 
194
                return LZMA_PROG_ERROR;
 
195
 
 
196
        // Allocate and initialize *next->coder if needed.
 
197
        if (next->coder == NULL) {
 
198
                next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
 
199
                if (next->coder == NULL)
 
200
                        return LZMA_MEM_ERROR;
 
201
 
 
202
                next->code = &block_decode;
 
203
                next->end = &block_decoder_end;
 
204
                next->coder->next = LZMA_NEXT_CODER_INIT;
 
205
        }
 
206
 
 
207
        // Basic initializations
 
208
        next->coder->sequence = SEQ_CODE;
 
209
        next->coder->block = block;
 
210
        next->coder->compressed_size = 0;
 
211
        next->coder->uncompressed_size = 0;
 
212
 
 
213
        // If Compressed Size is not known, we calculate the maximum allowed
 
214
        // value so that encoded size of the Block (including Block Padding)
 
215
        // is still a valid VLI and a multiple of four.
 
216
        next->coder->compressed_limit
 
217
                        = block->compressed_size == LZMA_VLI_UNKNOWN
 
218
                                ? (LZMA_VLI_MAX & ~LZMA_VLI_C(3))
 
219
                                        - block->header_size
 
220
                                        - lzma_check_size(block->check)
 
221
                                : block->compressed_size;
 
222
 
 
223
        // Initialize the check. It's caller's problem if the Check ID is not
 
224
        // supported, and the Block decoder cannot verify the Check field.
 
225
        // Caller can test lzma_check_is_supported(block->check).
 
226
        next->coder->check_pos = 0;
 
227
        lzma_check_init(&next->coder->check, block->check);
 
228
 
 
229
        // Initialize the filter chain.
 
230
        return lzma_raw_decoder_init(&next->coder->next, allocator,
 
231
                        block->filters);
 
232
}
 
233
 
 
234
 
 
235
extern LZMA_API(lzma_ret)
 
236
lzma_block_decoder(lzma_stream *strm, lzma_block *block)
 
237
{
 
238
        lzma_next_strm_init(lzma_block_decoder_init, strm, block);
 
239
 
 
240
        strm->internal->supported_actions[LZMA_RUN] = true;
 
241
        strm->internal->supported_actions[LZMA_FINISH] = true;
 
242
 
 
243
        return LZMA_OK;
 
244
}