Line data Source code
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/. */
6 :
7 : #include "mozilla/Compression.h"
8 : #include "mozilla/CheckedInt.h"
9 :
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>
14 :
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
23 :
24 : using namespace mozilla::Compression;
25 :
26 : namespace {
27 :
28 : #include "lz4.c"
29 :
30 : }/* anonymous namespace */
31 :
32 : /* Our wrappers */
33 :
34 : size_t
35 0 : LZ4::compress(const char* aSource, size_t aInputSize, char* aDest)
36 : {
37 0 : CheckedInt<int> inputSizeChecked = aInputSize;
38 0 : MOZ_ASSERT(inputSizeChecked.isValid());
39 0 : return LZ4_compress(aSource, aDest, inputSizeChecked.value());
40 : }
41 :
42 : size_t
43 0 : LZ4::compressLimitedOutput(const char* aSource, size_t aInputSize, char* aDest,
44 : size_t aMaxOutputSize)
45 : {
46 0 : CheckedInt<int> inputSizeChecked = aInputSize;
47 0 : MOZ_ASSERT(inputSizeChecked.isValid());
48 0 : CheckedInt<int> maxOutputSizeChecked = aMaxOutputSize;
49 0 : MOZ_ASSERT(maxOutputSizeChecked.isValid());
50 0 : return LZ4_compress_limitedOutput(aSource, aDest, inputSizeChecked.value(),
51 0 : maxOutputSizeChecked.value());
52 : }
53 :
54 : bool
55 1 : LZ4::decompress(const char* aSource, char* aDest, size_t aOutputSize)
56 : {
57 1 : CheckedInt<int> outputSizeChecked = aOutputSize;
58 1 : MOZ_ASSERT(outputSizeChecked.isValid());
59 1 : int ret = LZ4_decompress_fast(aSource, aDest, outputSizeChecked.value());
60 1 : return ret >= 0;
61 : }
62 :
63 : bool
64 0 : LZ4::decompress(const char* aSource, size_t aInputSize, char* aDest,
65 : size_t aMaxOutputSize, size_t* aOutputSize)
66 : {
67 0 : CheckedInt<int> maxOutputSizeChecked = aMaxOutputSize;
68 0 : MOZ_ASSERT(maxOutputSizeChecked.isValid());
69 0 : CheckedInt<int> inputSizeChecked = aInputSize;
70 0 : MOZ_ASSERT(inputSizeChecked.isValid());
71 :
72 0 : int ret = LZ4_decompress_safe(aSource, aDest, inputSizeChecked.value(),
73 0 : maxOutputSizeChecked.value());
74 0 : if (ret >= 0) {
75 0 : *aOutputSize = ret;
76 0 : return true;
77 : }
78 :
79 0 : *aOutputSize = 0;
80 0 : return false;
81 : }
82 :
|