michael@0: // Copyright 2005 and onwards Google Inc. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: // michael@0: // A light-weight compression algorithm. It is designed for speed of michael@0: // compression and decompression, rather than for the utmost in space michael@0: // savings. michael@0: // michael@0: // For getting better compression ratios when you are compressing data michael@0: // with long repeated sequences or compressing data that is similar to michael@0: // other data, while still compressing fast, you might look at first michael@0: // using BMDiff and then compressing the output of BMDiff with michael@0: // Snappy. michael@0: michael@0: #ifndef UTIL_SNAPPY_SNAPPY_H__ michael@0: #define UTIL_SNAPPY_SNAPPY_H__ michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "snappy-stubs-public.h" michael@0: michael@0: namespace snappy { michael@0: class Source; michael@0: class Sink; michael@0: michael@0: // ------------------------------------------------------------------------ michael@0: // Generic compression/decompression routines. michael@0: // ------------------------------------------------------------------------ michael@0: michael@0: // Compress the bytes read from "*source" and append to "*sink". Return the michael@0: // number of bytes written. michael@0: size_t Compress(Source* source, Sink* sink); michael@0: michael@0: bool GetUncompressedLength(Source* source, uint32* result); michael@0: michael@0: // ------------------------------------------------------------------------ michael@0: // Higher-level string based routines (should be sufficient for most users) michael@0: // ------------------------------------------------------------------------ michael@0: michael@0: // Sets "*output" to the compressed version of "input[0,input_length-1]". michael@0: // Original contents of *output are lost. michael@0: // michael@0: // REQUIRES: "input[]" is not an alias of "*output". michael@0: size_t Compress(const char* input, size_t input_length, string* output); michael@0: michael@0: // Decompresses "compressed[0,compressed_length-1]" to "*uncompressed". michael@0: // Original contents of "*uncompressed" are lost. michael@0: // michael@0: // REQUIRES: "compressed[]" is not an alias of "*uncompressed". michael@0: // michael@0: // returns false if the message is corrupted and could not be decompressed michael@0: bool Uncompress(const char* compressed, size_t compressed_length, michael@0: string* uncompressed); michael@0: michael@0: michael@0: // ------------------------------------------------------------------------ michael@0: // Lower-level character array based routines. May be useful for michael@0: // efficiency reasons in certain circumstances. michael@0: // ------------------------------------------------------------------------ michael@0: michael@0: // REQUIRES: "compressed" must point to an area of memory that is at michael@0: // least "MaxCompressedLength(input_length)" bytes in length. michael@0: // michael@0: // Takes the data stored in "input[0..input_length]" and stores michael@0: // it in the array pointed to by "compressed". michael@0: // michael@0: // "*compressed_length" is set to the length of the compressed output. michael@0: // michael@0: // Example: michael@0: // char* output = new char[snappy::MaxCompressedLength(input_length)]; michael@0: // size_t output_length; michael@0: // RawCompress(input, input_length, output, &output_length); michael@0: // ... Process(output, output_length) ... michael@0: // delete [] output; michael@0: void RawCompress(const char* input, michael@0: size_t input_length, michael@0: char* compressed, michael@0: size_t* compressed_length); michael@0: michael@0: // Given data in "compressed[0..compressed_length-1]" generated by michael@0: // calling the Snappy::Compress routine, this routine michael@0: // stores the uncompressed data to michael@0: // uncompressed[0..GetUncompressedLength(compressed)-1] michael@0: // returns false if the message is corrupted and could not be decrypted michael@0: bool RawUncompress(const char* compressed, size_t compressed_length, michael@0: char* uncompressed); michael@0: michael@0: // Given data from the byte source 'compressed' generated by calling michael@0: // the Snappy::Compress routine, this routine stores the uncompressed michael@0: // data to michael@0: // uncompressed[0..GetUncompressedLength(compressed,compressed_length)-1] michael@0: // returns false if the message is corrupted and could not be decrypted michael@0: bool RawUncompress(Source* compressed, char* uncompressed); michael@0: michael@0: // Returns the maximal size of the compressed representation of michael@0: // input data that is "source_bytes" bytes in length; michael@0: size_t MaxCompressedLength(size_t source_bytes); michael@0: michael@0: // REQUIRES: "compressed[]" was produced by RawCompress() or Compress() michael@0: // Returns true and stores the length of the uncompressed data in michael@0: // *result normally. Returns false on parsing error. michael@0: // This operation takes O(1) time. michael@0: bool GetUncompressedLength(const char* compressed, size_t compressed_length, michael@0: size_t* result); michael@0: michael@0: // Returns true iff the contents of "compressed[]" can be uncompressed michael@0: // successfully. Does not return the uncompressed data. Takes michael@0: // time proportional to compressed_length, but is usually at least michael@0: // a factor of four faster than actual decompression. michael@0: bool IsValidCompressedBuffer(const char* compressed, michael@0: size_t compressed_length); michael@0: michael@0: // *** DO NOT CHANGE THE VALUE OF kBlockSize *** michael@0: // michael@0: // New Compression code chops up the input into blocks of at most michael@0: // the following size. This ensures that back-references in the michael@0: // output never cross kBlockSize block boundaries. This can be michael@0: // helpful in implementing blocked decompression. However the michael@0: // decompression code should not rely on this guarantee since older michael@0: // compression code may not obey it. michael@0: static const int kBlockLog = 15; michael@0: static const size_t kBlockSize = 1 << kBlockLog; michael@0: michael@0: static const int kMaxHashTableBits = 14; michael@0: static const size_t kMaxHashTableSize = 1 << kMaxHashTableBits; michael@0: michael@0: } // end namespace snappy michael@0: michael@0: michael@0: #endif // UTIL_SNAPPY_SNAPPY_H__