webdriver: Implement Fullscreen command support (#100)
[gecko.git] / mfbt / Compression.cpp
blob3f5fff53c4255394f3adc305f55e2d06e45d4482
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 // Because we wrap lz4.c in an anonymous namespace, all of its #includes
16 // go in the anonymous namespace too. This would create conflicting
17 // declarations for intrinsic functions that are internally defined
18 // at top-level. Including intrin.h here prevents it from being included
19 // later within the anonymous namespace.
20 #ifdef _MSC_VER
21 #include <intrin.h>
22 #endif
24 using namespace mozilla::Compression;
26 namespace {
28 #include "lz4.c"
30 }/* anonymous namespace */
32 /* Our wrappers */
34 size_t
35 LZ4::compress(const char* aSource, size_t aInputSize, char* aDest)
37 CheckedInt<int> inputSizeChecked = aInputSize;
38 MOZ_ASSERT(inputSizeChecked.isValid());
39 return LZ4_compress(aSource, aDest, inputSizeChecked.value());
42 size_t
43 LZ4::compressLimitedOutput(const char* aSource, size_t aInputSize, char* aDest,
44 size_t aMaxOutputSize)
46 CheckedInt<int> inputSizeChecked = aInputSize;
47 MOZ_ASSERT(inputSizeChecked.isValid());
48 CheckedInt<int> maxOutputSizeChecked = aMaxOutputSize;
49 MOZ_ASSERT(maxOutputSizeChecked.isValid());
50 return LZ4_compress_limitedOutput(aSource, aDest, inputSizeChecked.value(),
51 maxOutputSizeChecked.value());
54 bool
55 LZ4::decompress(const char* aSource, char* aDest, size_t aOutputSize)
57 CheckedInt<int> outputSizeChecked = aOutputSize;
58 MOZ_ASSERT(outputSizeChecked.isValid());
59 int ret = LZ4_decompress_fast(aSource, aDest, outputSizeChecked.value());
60 return ret >= 0;
63 bool
64 LZ4::decompress(const char* aSource, size_t aInputSize, char* aDest,
65 size_t aMaxOutputSize, size_t* aOutputSize)
67 CheckedInt<int> maxOutputSizeChecked = aMaxOutputSize;
68 MOZ_ASSERT(maxOutputSizeChecked.isValid());
69 CheckedInt<int> inputSizeChecked = aInputSize;
70 MOZ_ASSERT(inputSizeChecked.isValid());
72 int ret = LZ4_decompress_safe(aSource, aDest, inputSizeChecked.value(),
73 maxOutputSizeChecked.value());
74 if (ret >= 0) {
75 *aOutputSize = ret;
76 return true;
79 *aOutputSize = 0;
80 return false;