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.

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

mercurial