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
;
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()));
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());
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());
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());
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());