Bug 1456906 [wpt PR 10641] - Subtract scrollbar in PerpendicularContainingBlockLogica...
[gecko.git] / mfbt / Compression.cpp
blob830e0b01b43b8c2744ca7db357d04d0648227eb3
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 // Without including <string>, MSVC 2015 complains about e.g. the impossibility
11 // to convert `const void* const` to `void*` when calling memchr from
12 // corecrt_memory.h.
13 #include <string>
15 #include "lz4.h"
17 using namespace mozilla::Compression;
19 /* Our wrappers */
21 size_t
22 LZ4::compress(const char* aSource, size_t aInputSize, char* aDest)
24 CheckedInt<int> inputSizeChecked = aInputSize;
25 MOZ_ASSERT(inputSizeChecked.isValid());
26 return LZ4_compress_default(aSource, aDest, inputSizeChecked.value(),
27 LZ4_compressBound(inputSizeChecked.value()));
30 size_t
31 LZ4::compressLimitedOutput(const char* aSource, size_t aInputSize, char* aDest,
32 size_t aMaxOutputSize)
34 CheckedInt<int> inputSizeChecked = aInputSize;
35 MOZ_ASSERT(inputSizeChecked.isValid());
36 CheckedInt<int> maxOutputSizeChecked = aMaxOutputSize;
37 MOZ_ASSERT(maxOutputSizeChecked.isValid());
38 return LZ4_compress_default(aSource, aDest, inputSizeChecked.value(),
39 maxOutputSizeChecked.value());
42 bool
43 LZ4::decompress(const char* aSource, char* aDest, size_t aOutputSize)
45 CheckedInt<int> outputSizeChecked = aOutputSize;
46 MOZ_ASSERT(outputSizeChecked.isValid());
47 int ret = LZ4_decompress_fast(aSource, aDest, outputSizeChecked.value());
48 return ret >= 0;
51 bool
52 LZ4::decompress(const char* aSource, size_t aInputSize, char* aDest,
53 size_t aMaxOutputSize, size_t* aOutputSize)
55 CheckedInt<int> maxOutputSizeChecked = aMaxOutputSize;
56 MOZ_ASSERT(maxOutputSizeChecked.isValid());
57 CheckedInt<int> inputSizeChecked = aInputSize;
58 MOZ_ASSERT(inputSizeChecked.isValid());
60 int ret = LZ4_decompress_safe(aSource, aDest, inputSizeChecked.value(),
61 maxOutputSizeChecked.value());
62 if (ret >= 0) {
63 *aOutputSize = ret;
64 return true;
67 *aOutputSize = 0;
68 return false;
71 bool
72 LZ4::decompressPartial(const char* aSource, size_t aInputSize, char* aDest,
73 size_t aMaxOutputSize, size_t* aOutputSize)
75 CheckedInt<int> maxOutputSizeChecked = aMaxOutputSize;
76 MOZ_ASSERT(maxOutputSizeChecked.isValid());
77 CheckedInt<int> inputSizeChecked = aInputSize;
78 MOZ_ASSERT(inputSizeChecked.isValid());
80 int ret = LZ4_decompress_safe_partial(aSource, aDest,
81 inputSizeChecked.value(),
82 maxOutputSizeChecked.value(),
83 maxOutputSizeChecked.value());
84 if (ret >= 0) {
85 *aOutputSize = ret;
86 return true;
89 *aOutputSize = 0;
90 return false;