Bumping manifests a=b2g-bump
[gecko.git] / mfbt / Compression.cpp
blob154cef1031a62e247ade9327b9a2a2b935d94489
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/Compression.h"
8 #include "mozilla/CheckedInt.h"
10 using namespace mozilla::Compression;
12 namespace {
14 #include "lz4.c"
16 }/* anonymous namespace */
18 /* Our wrappers */
20 size_t
21 LZ4::compress(const char* aSource, size_t aInputSize, char* aDest)
23 CheckedInt<int> inputSizeChecked = aInputSize;
24 MOZ_ASSERT(inputSizeChecked.isValid());
25 return LZ4_compress(aSource, aDest, inputSizeChecked.value());
28 size_t
29 LZ4::compressLimitedOutput(const char* aSource, size_t aInputSize, char* aDest,
30 size_t aMaxOutputSize)
32 CheckedInt<int> inputSizeChecked = aInputSize;
33 MOZ_ASSERT(inputSizeChecked.isValid());
34 CheckedInt<int> maxOutputSizeChecked = aMaxOutputSize;
35 MOZ_ASSERT(maxOutputSizeChecked.isValid());
36 return LZ4_compress_limitedOutput(aSource, aDest, inputSizeChecked.value(),
37 maxOutputSizeChecked.value());
40 bool
41 LZ4::decompress(const char* aSource, char* aDest, size_t aOutputSize)
43 CheckedInt<int> outputSizeChecked = aOutputSize;
44 MOZ_ASSERT(outputSizeChecked.isValid());
45 int ret = LZ4_decompress_fast(aSource, aDest, outputSizeChecked.value());
46 return ret >= 0;
49 bool
50 LZ4::decompress(const char* aSource, size_t aInputSize, char* aDest,
51 size_t aMaxOutputSize, size_t* aOutputSize)
53 CheckedInt<int> maxOutputSizeChecked = aMaxOutputSize;
54 MOZ_ASSERT(maxOutputSizeChecked.isValid());
55 CheckedInt<int> inputSizeChecked = aInputSize;
56 MOZ_ASSERT(inputSizeChecked.isValid());
58 int ret = LZ4_decompress_safe(aSource, aDest, inputSizeChecked.value(),
59 maxOutputSizeChecked.value());
60 if (ret >= 0) {
61 *aOutputSize = ret;
62 return true;
65 *aOutputSize = 0;
66 return false;