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
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.
24 using namespace mozilla::Compression
;
30 }/* anonymous namespace */
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());
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());
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());
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());