toolkit/components/workerlz4/lz4.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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
michael@0 7 /**
michael@0 8 * LZ4 is a very fast byte-wise compression algorithm.
michael@0 9 *
michael@0 10 * Compared to Google's Snappy it is faster to compress and decompress and
michael@0 11 * generally produces output of about the same size.
michael@0 12 *
michael@0 13 * Compared to zlib it compresses at about 10x the speed, decompresses at about
michael@0 14 * 4x the speed and produces output of about 1.5x the size.
michael@0 15 *
michael@0 16 */
michael@0 17
michael@0 18 using namespace mozilla::Compression;
michael@0 19
michael@0 20 /**
michael@0 21 * Compresses 'inputSize' bytes from 'source' into 'dest'.
michael@0 22 * Destination buffer must be already allocated,
michael@0 23 * and must be sized to handle worst cases situations (input data not compressible)
michael@0 24 * Worst case size evaluation is provided by function LZ4_compressBound()
michael@0 25 *
michael@0 26 * @param inputSize is the input size. Max supported value is ~1.9GB
michael@0 27 * @param return the number of bytes written in buffer dest
michael@0 28 */
michael@0 29 extern "C" MOZ_EXPORT size_t
michael@0 30 workerlz4_compress(const char* source, size_t inputSize, char* dest) {
michael@0 31 return LZ4::compress(source, inputSize, dest);
michael@0 32 }
michael@0 33
michael@0 34 /**
michael@0 35 * If the source stream is malformed, the function will stop decoding
michael@0 36 * and return a negative result, indicating the byte position of the
michael@0 37 * faulty instruction
michael@0 38 *
michael@0 39 * This function never writes outside of provided buffers, and never
michael@0 40 * modifies input buffer.
michael@0 41 *
michael@0 42 * note : destination buffer must be already allocated.
michael@0 43 * its size must be a minimum of 'outputSize' bytes.
michael@0 44 * @param outputSize is the output size, therefore the original size
michael@0 45 * @return true/false
michael@0 46 */
michael@0 47 extern "C" MOZ_EXPORT int
michael@0 48 workerlz4_decompress(const char* source, size_t inputSize,
michael@0 49 char* dest, size_t maxOutputSize,
michael@0 50 size_t *bytesOutput) {
michael@0 51 return LZ4::decompress(source, inputSize,
michael@0 52 dest, maxOutputSize,
michael@0 53 bytesOutput);
michael@0 54 }
michael@0 55
michael@0 56
michael@0 57 /*
michael@0 58 Provides the maximum size that LZ4 may output in a "worst case"
michael@0 59 scenario (input data not compressible) primarily useful for memory
michael@0 60 allocation of output buffer.
michael@0 61 note : this function is limited by "int" range (2^31-1)
michael@0 62
michael@0 63 @param inputSize is the input size. Max supported value is ~1.9GB
michael@0 64 @return maximum output size in a "worst case" scenario
michael@0 65 */
michael@0 66 extern "C" MOZ_EXPORT size_t
michael@0 67 workerlz4_maxCompressedSize(size_t inputSize)
michael@0 68 {
michael@0 69 return LZ4::maxCompressedSize(inputSize);
michael@0 70 }
michael@0 71
michael@0 72
michael@0 73

mercurial