Tue, 06 Jan 2015 21:39:09 +0100
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 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* Various simple compression/decompression functions. */ |
michael@0 | 7 | |
michael@0 | 8 | #ifndef mozilla_Compression_h_ |
michael@0 | 9 | #define mozilla_Compression_h_ |
michael@0 | 10 | |
michael@0 | 11 | #include "mozilla/Types.h" |
michael@0 | 12 | #include "mozilla/Assertions.h" |
michael@0 | 13 | |
michael@0 | 14 | namespace mozilla { |
michael@0 | 15 | namespace Compression { |
michael@0 | 16 | |
michael@0 | 17 | /** |
michael@0 | 18 | * LZ4 is a very fast byte-wise compression algorithm. |
michael@0 | 19 | * |
michael@0 | 20 | * Compared to Google's Snappy it is faster to compress and decompress and |
michael@0 | 21 | * generally produces output of about the same size. |
michael@0 | 22 | * |
michael@0 | 23 | * Compared to zlib it compresses at about 10x the speed, decompresses at about |
michael@0 | 24 | * 4x the speed and produces output of about 1.5x the size. |
michael@0 | 25 | * |
michael@0 | 26 | */ |
michael@0 | 27 | |
michael@0 | 28 | class LZ4 |
michael@0 | 29 | { |
michael@0 | 30 | |
michael@0 | 31 | public: |
michael@0 | 32 | |
michael@0 | 33 | /** |
michael@0 | 34 | * Compresses 'inputSize' bytes from 'source' into 'dest'. |
michael@0 | 35 | * Destination buffer must be already allocated, |
michael@0 | 36 | * and must be sized to handle worst cases situations (input data not compressible) |
michael@0 | 37 | * Worst case size evaluation is provided by function maxCompressedSize() |
michael@0 | 38 | * |
michael@0 | 39 | * @param inputSize is the input size. Max supported value is ~1.9GB |
michael@0 | 40 | * @param return the number of bytes written in buffer dest |
michael@0 | 41 | */ |
michael@0 | 42 | static MFBT_API size_t compress(const char* source, size_t inputSize, char* dest); |
michael@0 | 43 | |
michael@0 | 44 | /** |
michael@0 | 45 | * Compress 'inputSize' bytes from 'source' into an output buffer |
michael@0 | 46 | * 'dest' of maximum size 'maxOutputSize'. If it cannot achieve it, |
michael@0 | 47 | * compression will stop, and result of the function will be zero, |
michael@0 | 48 | * 'dest' will still be written to, but since the number of input |
michael@0 | 49 | * bytes consumed is not returned the result is not usable. |
michael@0 | 50 | * |
michael@0 | 51 | * This function never writes outside of provided output buffer. |
michael@0 | 52 | * |
michael@0 | 53 | * @param inputSize is the input size. Max supported value is ~1.9GB |
michael@0 | 54 | * @param maxOutputSize is the size of the destination buffer (which must be already allocated) |
michael@0 | 55 | * @return the number of bytes written in buffer 'dest' |
michael@0 | 56 | or 0 if the compression fails |
michael@0 | 57 | */ |
michael@0 | 58 | static MFBT_API size_t compressLimitedOutput(const char* source, size_t inputSize, char* dest, |
michael@0 | 59 | size_t maxOutputSize); |
michael@0 | 60 | |
michael@0 | 61 | /** |
michael@0 | 62 | * If the source stream is malformed, the function will stop decoding |
michael@0 | 63 | * and return a negative result, indicating the byte position of the |
michael@0 | 64 | * faulty instruction |
michael@0 | 65 | * |
michael@0 | 66 | * This function never writes outside of provided buffers, and never |
michael@0 | 67 | * modifies input buffer. |
michael@0 | 68 | * |
michael@0 | 69 | * note : destination buffer must be already allocated. |
michael@0 | 70 | * its size must be a minimum of 'outputSize' bytes. |
michael@0 | 71 | * @param outputSize is the output size, therefore the original size |
michael@0 | 72 | * @return the number of bytes read in the source buffer |
michael@0 | 73 | */ |
michael@0 | 74 | static MFBT_API bool decompress(const char* source, char* dest, size_t outputSize); |
michael@0 | 75 | |
michael@0 | 76 | /** |
michael@0 | 77 | * If the source stream is malformed, the function will stop decoding |
michael@0 | 78 | * and return false. |
michael@0 | 79 | * |
michael@0 | 80 | * This function never writes beyond dest + maxOutputSize, and is |
michael@0 | 81 | * therefore protected against malicious data packets. |
michael@0 | 82 | * |
michael@0 | 83 | * note : Destination buffer must be already allocated. |
michael@0 | 84 | * This version is slightly slower than the decompress |
michael@0 | 85 | * without the maxOutputSize |
michael@0 | 86 | * |
michael@0 | 87 | * @param inputSize is the length of the input compressed data |
michael@0 | 88 | * @param maxOutputSize is the size of the destination buffer (which must be already allocated) |
michael@0 | 89 | * @param outputSize the actual number of bytes decoded in the destination buffer (necessarily <= maxOutputSize) |
michael@0 | 90 | |
michael@0 | 91 | */ |
michael@0 | 92 | static MFBT_API bool decompress(const char* source, size_t inputSize, char* dest, |
michael@0 | 93 | size_t maxOutputSize, size_t *outputSize); |
michael@0 | 94 | |
michael@0 | 95 | /* |
michael@0 | 96 | Provides the maximum size that LZ4 may output in a "worst case" |
michael@0 | 97 | scenario (input data not compressible) primarily useful for memory |
michael@0 | 98 | allocation of output buffer. |
michael@0 | 99 | note : this function is limited by "int" range (2^31-1) |
michael@0 | 100 | |
michael@0 | 101 | @param inputSize is the input size. Max supported value is ~1.9GB |
michael@0 | 102 | @return maximum output size in a "worst case" scenario |
michael@0 | 103 | */ |
michael@0 | 104 | static inline size_t maxCompressedSize(size_t inputSize) |
michael@0 | 105 | { |
michael@0 | 106 | size_t max = ((inputSize) + ((inputSize)/255) + 16); |
michael@0 | 107 | MOZ_ASSERT(max > inputSize); |
michael@0 | 108 | return max; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | }; |
michael@0 | 112 | |
michael@0 | 113 | } /* namespace Compression */ |
michael@0 | 114 | } /* namespace mozilla */ |
michael@0 | 115 | |
michael@0 | 116 | #endif /* mozilla_Compression_h_ */ |