|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #include "mozilla/Compression.h" |
|
6 #include "mozilla/CheckedInt.h" |
|
7 using namespace mozilla::Compression; |
|
8 |
|
9 namespace { |
|
10 |
|
11 #include "lz4.c" |
|
12 |
|
13 }/* anonymous namespace */ |
|
14 |
|
15 /* Our wrappers */ |
|
16 |
|
17 size_t |
|
18 LZ4::compress(const char* source, size_t inputSize, char* dest) |
|
19 { |
|
20 CheckedInt<int> inputSizeChecked = inputSize; |
|
21 MOZ_ASSERT(inputSizeChecked.isValid()); |
|
22 return LZ4_compress(source, dest, inputSizeChecked.value()); |
|
23 } |
|
24 |
|
25 size_t |
|
26 LZ4::compressLimitedOutput(const char* source, size_t inputSize, char* dest, size_t maxOutputSize) |
|
27 { |
|
28 CheckedInt<int> inputSizeChecked = inputSize; |
|
29 MOZ_ASSERT(inputSizeChecked.isValid()); |
|
30 CheckedInt<int> maxOutputSizeChecked = maxOutputSize; |
|
31 MOZ_ASSERT(maxOutputSizeChecked.isValid()); |
|
32 return LZ4_compress_limitedOutput(source, dest, inputSizeChecked.value(), |
|
33 maxOutputSizeChecked.value()); |
|
34 } |
|
35 |
|
36 bool |
|
37 LZ4::decompress(const char* source, char* dest, size_t outputSize) |
|
38 { |
|
39 CheckedInt<int> outputSizeChecked = outputSize; |
|
40 MOZ_ASSERT(outputSizeChecked.isValid()); |
|
41 int ret = LZ4_decompress_fast(source, dest, outputSizeChecked.value()); |
|
42 return ret >= 0; |
|
43 } |
|
44 |
|
45 bool |
|
46 LZ4::decompress(const char* source, size_t inputSize, char* dest, size_t maxOutputSize, |
|
47 size_t *outputSize) |
|
48 { |
|
49 CheckedInt<int> maxOutputSizeChecked = maxOutputSize; |
|
50 MOZ_ASSERT(maxOutputSizeChecked.isValid()); |
|
51 CheckedInt<int> inputSizeChecked = inputSize; |
|
52 MOZ_ASSERT(inputSizeChecked.isValid()); |
|
53 |
|
54 int ret = LZ4_decompress_safe(source, dest, inputSizeChecked.value(), |
|
55 maxOutputSizeChecked.value()); |
|
56 if (ret >= 0) { |
|
57 *outputSize = ret; |
|
58 return true; |
|
59 } else { |
|
60 *outputSize = 0; |
|
61 return false; |
|
62 } |
|
63 } |
|
64 |