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
17 using namespace mozilla::Compression
;
21 size_t LZ4::compress(const char* aSource
, size_t aInputSize
, char* aDest
) {
22 CheckedInt
<int> inputSizeChecked
= aInputSize
;
23 MOZ_ASSERT(inputSizeChecked
.isValid());
24 return LZ4_compress_default(aSource
, aDest
, inputSizeChecked
.value(),
25 LZ4_compressBound(inputSizeChecked
.value()));
28 size_t LZ4::compressLimitedOutput(const char* aSource
, size_t aInputSize
,
29 char* aDest
, size_t aMaxOutputSize
) {
30 CheckedInt
<int> inputSizeChecked
= aInputSize
;
31 MOZ_ASSERT(inputSizeChecked
.isValid());
32 CheckedInt
<int> maxOutputSizeChecked
= aMaxOutputSize
;
33 MOZ_ASSERT(maxOutputSizeChecked
.isValid());
34 return LZ4_compress_default(aSource
, aDest
, inputSizeChecked
.value(),
35 maxOutputSizeChecked
.value());
38 bool LZ4::decompress(const char* aSource
, char* aDest
, size_t aOutputSize
) {
39 CheckedInt
<int> outputSizeChecked
= aOutputSize
;
40 MOZ_ASSERT(outputSizeChecked
.isValid());
41 int ret
= LZ4_decompress_fast(aSource
, aDest
, outputSizeChecked
.value());
45 bool LZ4::decompress(const char* aSource
, size_t aInputSize
, char* aDest
,
46 size_t aMaxOutputSize
, size_t* aOutputSize
) {
47 CheckedInt
<int> maxOutputSizeChecked
= aMaxOutputSize
;
48 MOZ_ASSERT(maxOutputSizeChecked
.isValid());
49 CheckedInt
<int> inputSizeChecked
= aInputSize
;
50 MOZ_ASSERT(inputSizeChecked
.isValid());
52 int ret
= LZ4_decompress_safe(aSource
, aDest
, inputSizeChecked
.value(),
53 maxOutputSizeChecked
.value());
63 bool LZ4::decompressPartial(const char* aSource
, size_t aInputSize
, char* aDest
,
64 size_t aMaxOutputSize
, size_t* aOutputSize
) {
65 CheckedInt
<int> maxOutputSizeChecked
= aMaxOutputSize
;
66 MOZ_ASSERT(maxOutputSizeChecked
.isValid());
67 CheckedInt
<int> inputSizeChecked
= aInputSize
;
68 MOZ_ASSERT(inputSizeChecked
.isValid());
70 int ret
= LZ4_decompress_safe_partial(
71 aSource
, aDest
, inputSizeChecked
.value(), maxOutputSizeChecked
.value(),
72 maxOutputSizeChecked
.value());