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 #include "lz4.h"
16 
17 using namespace mozilla::Compression;
18 
19 /* Our wrappers */
20 
compress(const char * aSource,size_t aInputSize,char * aDest)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()));
26 }
27 
compressLimitedOutput(const char * aSource,size_t aInputSize,char * aDest,size_t aMaxOutputSize)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());
36 }
37 
decompress(const char * aSource,char * aDest,size_t aOutputSize)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());
42   return ret >= 0;
43 }
44 
decompress(const char * aSource,size_t aInputSize,char * aDest,size_t aMaxOutputSize,size_t * aOutputSize)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());
51 
52   int ret = LZ4_decompress_safe(aSource, aDest, inputSizeChecked.value(),
53                                 maxOutputSizeChecked.value());
54   if (ret >= 0) {
55     *aOutputSize = ret;
56     return true;
57   }
58 
59   *aOutputSize = 0;
60   return false;
61 }
62 
decompressPartial(const char * aSource,size_t aInputSize,char * aDest,size_t aMaxOutputSize,size_t * aOutputSize)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());
69 
70   int ret = LZ4_decompress_safe_partial(
71       aSource, aDest, inputSizeChecked.value(), maxOutputSizeChecked.value(),
72       maxOutputSizeChecked.value());
73   if (ret >= 0) {
74     *aOutputSize = ret;
75     return true;
76   }
77 
78   *aOutputSize = 0;
79   return false;
80 }
81