This repository has been archived by the owner on Aug 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCompression.cpp
64 lines (53 loc) · 1.92 KB
/
Compression.cpp
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/Compression.h"
#include "mozilla/CheckedInt.h"
using namespace mozilla::Compression;
namespace {
#include "lz4.c"
}/* anonymous namespace */
/* Our wrappers */
size_t
LZ4::compress(const char* source, size_t inputSize, char* dest)
{
CheckedInt<int> inputSizeChecked = inputSize;
MOZ_ASSERT(inputSizeChecked.isValid());
return LZ4_compress(source, dest, inputSizeChecked.value());
}
size_t
LZ4::compressLimitedOutput(const char* source, size_t inputSize, char* dest, size_t maxOutputSize)
{
CheckedInt<int> inputSizeChecked = inputSize;
MOZ_ASSERT(inputSizeChecked.isValid());
CheckedInt<int> maxOutputSizeChecked = maxOutputSize;
MOZ_ASSERT(maxOutputSizeChecked.isValid());
return LZ4_compress_limitedOutput(source, dest, inputSizeChecked.value(),
maxOutputSizeChecked.value());
}
bool
LZ4::decompress(const char* source, char* dest, size_t outputSize)
{
CheckedInt<int> outputSizeChecked = outputSize;
MOZ_ASSERT(outputSizeChecked.isValid());
int ret = LZ4_decompress_fast(source, dest, outputSizeChecked.value());
return ret >= 0;
}
bool
LZ4::decompress(const char* source, size_t inputSize, char* dest, size_t maxOutputSize,
size_t *outputSize)
{
CheckedInt<int> maxOutputSizeChecked = maxOutputSize;
MOZ_ASSERT(maxOutputSizeChecked.isValid());
CheckedInt<int> inputSizeChecked = inputSize;
MOZ_ASSERT(inputSizeChecked.isValid());
int ret = LZ4_decompress_safe(source, dest, inputSizeChecked.value(),
maxOutputSizeChecked.value());
if (ret >= 0) {
*outputSize = ret;
return true;
} else {
*outputSize = 0;
return false;
}
}