aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/apache/orc/c++/src/LzoDecompressor.cc
blob: f494f4b651d3e3feb709b6c721e6c69199459b07 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#include "Adaptor.hh"
#include "Compression.hh"
#include "orc/Exceptions.hh"

#include <string>

namespace orc {

  static const int32_t DEC_32_TABLE[] = {4, 1, 2, 1, 4, 4, 4, 4};
  static const int32_t DEC_64_TABLE[] = {0, 0, 0, -1, 0, 1, 2, 3};

  static const int32_t SIZE_OF_SHORT = 2;
  static const int32_t SIZE_OF_INT = 4;
  static const int32_t SIZE_OF_LONG = 8;

  static std::string toHex(uint64_t val) {
    std::ostringstream out;
    out << "0x" << std::hex << val;
    return out.str();
  }

  static std::string toString(int64_t val) {
    std::ostringstream out;
    out << val;
    return out.str();
  }

  class MalformedInputException : public ParseError {
   public:
    MalformedInputException(int64_t off)
        : ParseError("MalformedInputException at " + toString(off)) {}

    MalformedInputException(int64_t off, const std::string& msg)
        : ParseError("MalformedInputException " + msg + " at " + toString(off)) {}

    MalformedInputException(const MalformedInputException& other) : ParseError(other.what()) {}

    ~MalformedInputException() noexcept override;
  };

  MalformedInputException::~MalformedInputException() noexcept {
    // PASS
  }

  uint64_t lzoDecompress(const char* inputAddress, const char* inputLimit, char* outputAddress,
                         char* outputLimit) {
    // nothing compresses to nothing
    if (inputAddress == inputLimit) {
      return 0;
    }

    // maximum offset in buffers to which it's safe to write long-at-a-time
    char* const fastOutputLimit = outputLimit - SIZE_OF_LONG;

    // LZO can concat two blocks together so, decode until the input data is
    // consumed
    const char* input = inputAddress;
    char* output = outputAddress;
    while (input < inputLimit) {
      //
      // Note: For safety some of the code below may stop decoding early or
      // skip decoding, because input is not available.  This makes the code
      // safe, and since LZO requires an explicit "stop" command, the decoder
      // will still throw a exception.
      //

      bool firstCommand = true;
      uint32_t lastLiteralLength = 0;
      while (true) {
        if (input >= inputLimit) {
          throw MalformedInputException(input - inputAddress);
        }
        uint32_t command = *(input++) & 0xFF;
        if (command == 0x11) {
          break;
        }

        // Commands are described using a bit pattern notation:
        // 0: bit is not set
        // 1: bit is set
        // L: part of literal length
        // P: part of match offset position
        // M: part of match length
        // ?: see documentation in command decoder

        int32_t matchLength;
        int32_t matchOffset;
        uint32_t literalLength;
        if ((command & 0xf0) == 0) {
          if (lastLiteralLength == 0) {
            // 0b0000_LLLL (0bLLLL_LLLL)*

            // copy length :: fixed
            //   0
            matchOffset = 0;

            // copy offset :: fixed
            //   0
            matchLength = 0;

            // literal length - 3 :: variable bits :: valid range [4..]
            //   3 + variableLength(command bits [0..3], 4)
            literalLength = command & 0xf;
            if (literalLength == 0) {
              literalLength = 0xf;

              uint32_t nextByte = 0;
              while (input < inputLimit && (nextByte = *(input++) & 0xFF) == 0) {
                literalLength += 0xff;
              }
              literalLength += nextByte;
            }
            literalLength += 3;
          } else if (lastLiteralLength <= 3) {
            // 0b0000_PPLL 0bPPPP_PPPP

            // copy length: fixed
            //   3
            matchLength = 3;

            // copy offset :: 12 bits :: valid range [2048..3071]
            //   [0..1] from command [2..3]
            //   [2..9] from trailer [0..7]
            //   [10] unset
            //   [11] set
            if (input >= inputLimit) {
              throw MalformedInputException(input - inputAddress);
            }
            matchOffset = (command & 0xc) >> 2;
            matchOffset |= (*(input++) & 0xFF) << 2;
            matchOffset |= 0x800;

            // literal length :: 2 bits :: valid range [0..3]
            //   [0..1] from command [0..1]
            literalLength = (command & 0x3);
          } else {
            // 0b0000_PPLL 0bPPPP_PPPP

            // copy length :: fixed
            //   2
            matchLength = 2;

            // copy offset :: 10 bits :: valid range [0..1023]
            //   [0..1] from command [2..3]
            //   [2..9] from trailer [0..7]
            if (input >= inputLimit) {
              throw MalformedInputException(input - inputAddress);
            }
            matchOffset = (command & 0xc) >> 2;
            matchOffset |= (*(input++) & 0xFF) << 2;

            // literal length :: 2 bits :: valid range [0..3]
            //   [0..1] from command [0..1]
            literalLength = (command & 0x3);
          }
        } else if (firstCommand) {
          // first command has special handling when high nibble is set
          matchLength = 0;
          matchOffset = 0;
          literalLength = command - 17;
        } else if ((command & 0xf0) == 0x10) {
          // 0b0001_?MMM (0bMMMM_MMMM)* 0bPPPP_PPPP_PPPP_PPLL

          // copy length - 2 :: variable bits :: valid range [3..]
          //   2 + variableLength(command bits [0..2], 3)
          matchLength = command & 0x7;
          if (matchLength == 0) {
            matchLength = 0x7;

            int32_t nextByte = 0;
            while (input < inputLimit && (nextByte = *(input++) & 0xFF) == 0) {
              matchLength += 0xff;
            }
            matchLength += nextByte;
          }
          matchLength += 2;

          // read trailer
          if (input + SIZE_OF_SHORT > inputLimit) {
            throw MalformedInputException(input - inputAddress);
          }
          uint32_t trailer = *reinterpret_cast<const uint16_t*>(input) & 0xFFFF;
          input += SIZE_OF_SHORT;

          // copy offset :: 16 bits :: valid range [32767..49151]
          //   [0..13] from trailer [2..15]
          //   [14] if command bit [3] unset
          //   [15] if command bit [3] set
          matchOffset = trailer >> 2;
          if ((command & 0x8) == 0) {
            matchOffset |= 0x4000;
          } else {
            matchOffset |= 0x8000;
          }
          matchOffset--;

          // literal length :: 2 bits :: valid range [0..3]
          //   [0..1] from trailer [0..1]
          literalLength = trailer & 0x3;
        } else if ((command & 0xe0) == 0x20) {
          // 0b001M_MMMM (0bMMMM_MMMM)* 0bPPPP_PPPP_PPPP_PPLL

          // copy length - 2 :: variable bits :: valid range [3..]
          //   2 + variableLength(command bits [0..4], 5)
          matchLength = command & 0x1f;
          if (matchLength == 0) {
            matchLength = 0x1f;

            int nextByte = 0;
            while (input < inputLimit && (nextByte = *(input++) & 0xFF) == 0) {
              matchLength += 0xff;
            }
            matchLength += nextByte;
          }
          matchLength += 2;

          // read trailer
          if (input + SIZE_OF_SHORT > inputLimit) {
            throw MalformedInputException(input - inputAddress);
          }
          int32_t trailer = *reinterpret_cast<const int16_t*>(input) & 0xFFFF;
          input += SIZE_OF_SHORT;

          // copy offset :: 14 bits :: valid range [0..16383]
          //  [0..13] from trailer [2..15]
          matchOffset = trailer >> 2;

          // literal length :: 2 bits :: valid range [0..3]
          //   [0..1] from trailer [0..1]
          literalLength = trailer & 0x3;
        } else if ((command & 0xc0) != 0) {
          // 0bMMMP_PPLL 0bPPPP_PPPP

          // copy length - 1 :: 3 bits :: valid range [1..8]
          //   [0..2] from command [5..7]
          //   add 1
          matchLength = (command & 0xe0) >> 5;
          matchLength += 1;

          // copy offset :: 11 bits :: valid range [0..4095]
          //   [0..2] from command [2..4]
          //   [3..10] from trailer [0..7]
          if (input >= inputLimit) {
            throw MalformedInputException(input - inputAddress);
          }
          matchOffset = (command & 0x1c) >> 2;
          matchOffset |= (*(input++) & 0xFF) << 3;

          // literal length :: 2 bits :: valid range [0..3]
          //   [0..1] from command [0..1]
          literalLength = (command & 0x3);
        } else {
          throw MalformedInputException(input - inputAddress - 1,
                                        "Invalid LZO command " + toHex(command));
        }
        firstCommand = false;

        // copy match
        if (matchLength != 0) {
          // lzo encodes match offset minus one
          matchOffset++;

          char* matchAddress = output - matchOffset;
          if (matchAddress < outputAddress || output + matchLength > outputLimit) {
            throw MalformedInputException(input - inputAddress);
          }
          char* matchOutputLimit = output + matchLength;

          if (output > fastOutputLimit) {
            // slow match copy
            while (output < matchOutputLimit) {
              *(output++) = *(matchAddress++);
            }
          } else {
            // copy repeated sequence
            if (matchOffset < SIZE_OF_LONG) {
              // 8 bytes apart so that we can copy long-at-a-time below
              int32_t increment32 = DEC_32_TABLE[matchOffset];
              int32_t decrement64 = DEC_64_TABLE[matchOffset];

              output[0] = *matchAddress;
              output[1] = *(matchAddress + 1);
              output[2] = *(matchAddress + 2);
              output[3] = *(matchAddress + 3);
              output += SIZE_OF_INT;
              matchAddress += increment32;

              memcpy(output, matchAddress, SIZE_OF_INT);
              output += SIZE_OF_INT;
              matchAddress -= decrement64;
            } else {
              memcpy(output, matchAddress, SIZE_OF_LONG);
              matchAddress += SIZE_OF_LONG;
              output += SIZE_OF_LONG;
            }

            if (matchOutputLimit >= fastOutputLimit) {
              if (matchOutputLimit > outputLimit) {
                throw MalformedInputException(input - inputAddress);
              }

              while (output < fastOutputLimit) {
                memcpy(output, matchAddress, SIZE_OF_LONG);
                matchAddress += SIZE_OF_LONG;
                output += SIZE_OF_LONG;
              }

              while (output < matchOutputLimit) {
                *(output++) = *(matchAddress++);
              }
            } else {
              while (output < matchOutputLimit) {
                memcpy(output, matchAddress, SIZE_OF_LONG);
                matchAddress += SIZE_OF_LONG;
                output += SIZE_OF_LONG;
              }
            }
          }
          output = matchOutputLimit;  // correction in case we over-copied
        }

        // copy literal
        char* literalOutputLimit = output + literalLength;
        if (literalOutputLimit > fastOutputLimit ||
            input + literalLength > inputLimit - SIZE_OF_LONG) {
          if (literalOutputLimit > outputLimit) {
            throw MalformedInputException(input - inputAddress);
          }

          // slow, precise copy
          memcpy(output, input, literalLength);
          input += literalLength;
          output += literalLength;
        } else {
          // fast copy. We may over-copy but there's enough room in input
          // and output to not overrun them
          do {
            memcpy(output, input, SIZE_OF_LONG);
            input += SIZE_OF_LONG;
            output += SIZE_OF_LONG;
          } while (output < literalOutputLimit);
          // adjust index if we over-copied
          input -= (output - literalOutputLimit);
          output = literalOutputLimit;
        }
        lastLiteralLength = literalLength;
      }

      if (input + SIZE_OF_SHORT > inputLimit && *reinterpret_cast<const int16_t*>(input) != 0) {
        throw MalformedInputException(input - inputAddress);
      }
      input += SIZE_OF_SHORT;
    }

    return static_cast<uint64_t>(output - outputAddress);
  }

}  // namespace orc