mfbt/Compression.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mfbt/Compression.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,64 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "mozilla/Compression.h"
     1.9 +#include "mozilla/CheckedInt.h"
    1.10 +using namespace mozilla::Compression;
    1.11 +
    1.12 +namespace {
    1.13 +
    1.14 +#include "lz4.c"
    1.15 +
    1.16 +}/* anonymous namespace */
    1.17 +
    1.18 +/* Our wrappers */
    1.19 +
    1.20 +size_t
    1.21 +LZ4::compress(const char* source, size_t inputSize, char* dest)
    1.22 +{
    1.23 +    CheckedInt<int> inputSizeChecked = inputSize;
    1.24 +    MOZ_ASSERT(inputSizeChecked.isValid());
    1.25 +    return LZ4_compress(source, dest, inputSizeChecked.value());
    1.26 +}
    1.27 +
    1.28 +size_t
    1.29 +LZ4::compressLimitedOutput(const char* source, size_t inputSize, char* dest, size_t maxOutputSize)
    1.30 +{
    1.31 +    CheckedInt<int> inputSizeChecked = inputSize;
    1.32 +    MOZ_ASSERT(inputSizeChecked.isValid());
    1.33 +    CheckedInt<int> maxOutputSizeChecked = maxOutputSize;
    1.34 +    MOZ_ASSERT(maxOutputSizeChecked.isValid());
    1.35 +    return LZ4_compress_limitedOutput(source, dest, inputSizeChecked.value(),
    1.36 +                                      maxOutputSizeChecked.value());
    1.37 +}
    1.38 +
    1.39 +bool
    1.40 +LZ4::decompress(const char* source, char* dest, size_t outputSize)
    1.41 +{
    1.42 +    CheckedInt<int> outputSizeChecked = outputSize;
    1.43 +    MOZ_ASSERT(outputSizeChecked.isValid());
    1.44 +    int ret = LZ4_decompress_fast(source, dest, outputSizeChecked.value());
    1.45 +    return ret >= 0;
    1.46 +}
    1.47 +
    1.48 +bool
    1.49 +LZ4::decompress(const char* source, size_t inputSize, char* dest, size_t maxOutputSize,
    1.50 +                size_t *outputSize)
    1.51 +{
    1.52 +    CheckedInt<int> maxOutputSizeChecked = maxOutputSize;
    1.53 +    MOZ_ASSERT(maxOutputSizeChecked.isValid());
    1.54 +    CheckedInt<int> inputSizeChecked = inputSize;
    1.55 +    MOZ_ASSERT(inputSizeChecked.isValid());
    1.56 +
    1.57 +    int ret = LZ4_decompress_safe(source, dest, inputSizeChecked.value(),
    1.58 +                                  maxOutputSizeChecked.value());
    1.59 +    if (ret >= 0) {
    1.60 +        *outputSize = ret;
    1.61 +        return true;
    1.62 +    } else {
    1.63 +        *outputSize = 0;
    1.64 +        return false;
    1.65 +    }
    1.66 +}
    1.67 +

mercurial