mfbt/Compression.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "mozilla/Compression.h"
michael@0 6 #include "mozilla/CheckedInt.h"
michael@0 7 using namespace mozilla::Compression;
michael@0 8
michael@0 9 namespace {
michael@0 10
michael@0 11 #include "lz4.c"
michael@0 12
michael@0 13 }/* anonymous namespace */
michael@0 14
michael@0 15 /* Our wrappers */
michael@0 16
michael@0 17 size_t
michael@0 18 LZ4::compress(const char* source, size_t inputSize, char* dest)
michael@0 19 {
michael@0 20 CheckedInt<int> inputSizeChecked = inputSize;
michael@0 21 MOZ_ASSERT(inputSizeChecked.isValid());
michael@0 22 return LZ4_compress(source, dest, inputSizeChecked.value());
michael@0 23 }
michael@0 24
michael@0 25 size_t
michael@0 26 LZ4::compressLimitedOutput(const char* source, size_t inputSize, char* dest, size_t maxOutputSize)
michael@0 27 {
michael@0 28 CheckedInt<int> inputSizeChecked = inputSize;
michael@0 29 MOZ_ASSERT(inputSizeChecked.isValid());
michael@0 30 CheckedInt<int> maxOutputSizeChecked = maxOutputSize;
michael@0 31 MOZ_ASSERT(maxOutputSizeChecked.isValid());
michael@0 32 return LZ4_compress_limitedOutput(source, dest, inputSizeChecked.value(),
michael@0 33 maxOutputSizeChecked.value());
michael@0 34 }
michael@0 35
michael@0 36 bool
michael@0 37 LZ4::decompress(const char* source, char* dest, size_t outputSize)
michael@0 38 {
michael@0 39 CheckedInt<int> outputSizeChecked = outputSize;
michael@0 40 MOZ_ASSERT(outputSizeChecked.isValid());
michael@0 41 int ret = LZ4_decompress_fast(source, dest, outputSizeChecked.value());
michael@0 42 return ret >= 0;
michael@0 43 }
michael@0 44
michael@0 45 bool
michael@0 46 LZ4::decompress(const char* source, size_t inputSize, char* dest, size_t maxOutputSize,
michael@0 47 size_t *outputSize)
michael@0 48 {
michael@0 49 CheckedInt<int> maxOutputSizeChecked = maxOutputSize;
michael@0 50 MOZ_ASSERT(maxOutputSizeChecked.isValid());
michael@0 51 CheckedInt<int> inputSizeChecked = inputSize;
michael@0 52 MOZ_ASSERT(inputSizeChecked.isValid());
michael@0 53
michael@0 54 int ret = LZ4_decompress_safe(source, dest, inputSizeChecked.value(),
michael@0 55 maxOutputSizeChecked.value());
michael@0 56 if (ret >= 0) {
michael@0 57 *outputSize = ret;
michael@0 58 return true;
michael@0 59 } else {
michael@0 60 *outputSize = 0;
michael@0 61 return false;
michael@0 62 }
michael@0 63 }
michael@0 64

mercurial