[8ebc79b] | 1 | // Copyright 2005 and onwards Google Inc. |
---|
| 2 | // |
---|
| 3 | // Redistribution and use in source and binary forms, with or without |
---|
| 4 | // modification, are permitted provided that the following conditions are |
---|
| 5 | // met: |
---|
| 6 | // |
---|
| 7 | // * Redistributions of source code must retain the above copyright |
---|
| 8 | // notice, this list of conditions and the following disclaimer. |
---|
| 9 | // * Redistributions in binary form must reproduce the above |
---|
| 10 | // copyright notice, this list of conditions and the following disclaimer |
---|
| 11 | // in the documentation and/or other materials provided with the |
---|
| 12 | // distribution. |
---|
| 13 | // * Neither the name of Google Inc. nor the names of its |
---|
| 14 | // contributors may be used to endorse or promote products derived from |
---|
| 15 | // this software without specific prior written permission. |
---|
| 16 | // |
---|
| 17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
| 18 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
| 19 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
| 20 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
| 21 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
| 22 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
| 23 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
| 24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
| 25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
| 26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
| 27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 28 | // |
---|
| 29 | // A light-weight compression algorithm. It is designed for speed of |
---|
| 30 | // compression and decompression, rather than for the utmost in space |
---|
| 31 | // savings. |
---|
| 32 | // |
---|
| 33 | // For getting better compression ratios when you are compressing data |
---|
| 34 | // with long repeated sequences or compressing data that is similar to |
---|
| 35 | // other data, while still compressing fast, you might look at first |
---|
| 36 | // using BMDiff and then compressing the output of BMDiff with |
---|
| 37 | // Snappy. |
---|
| 38 | |
---|
| 39 | #ifndef UTIL_SNAPPY_SNAPPY_H__ |
---|
| 40 | #define UTIL_SNAPPY_SNAPPY_H__ |
---|
| 41 | |
---|
| 42 | #include <stddef.h> |
---|
| 43 | #include <string> |
---|
| 44 | |
---|
| 45 | #include "snappy-stubs-public.h" |
---|
| 46 | |
---|
| 47 | // Windows does not define ssize_t by default. This is a workaround. |
---|
| 48 | // Please note that this is only defined in the Blosc sources of Snappy. |
---|
| 49 | #if defined(_WIN32) && !defined(__MINGW32__) |
---|
| 50 | #include <BaseTsd.h> |
---|
| 51 | typedef SSIZE_T ssize_t; |
---|
| 52 | #endif |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | namespace snappy { |
---|
| 56 | class Source; |
---|
| 57 | class Sink; |
---|
| 58 | |
---|
| 59 | // ------------------------------------------------------------------------ |
---|
| 60 | // Generic compression/decompression routines. |
---|
| 61 | // ------------------------------------------------------------------------ |
---|
| 62 | |
---|
| 63 | // Compress the bytes read from "*source" and append to "*sink". Return the |
---|
| 64 | // number of bytes written. |
---|
| 65 | size_t Compress(Source* source, Sink* sink); |
---|
| 66 | |
---|
| 67 | // Find the uncompressed length of the given stream, as given by the header. |
---|
| 68 | // Note that the true length could deviate from this; the stream could e.g. |
---|
| 69 | // be truncated. |
---|
| 70 | // |
---|
| 71 | // Also note that this leaves "*source" in a state that is unsuitable for |
---|
| 72 | // further operations, such as RawUncompress(). You will need to rewind |
---|
| 73 | // or recreate the source yourself before attempting any further calls. |
---|
| 74 | bool GetUncompressedLength(Source* source, uint32* result); |
---|
| 75 | |
---|
| 76 | // ------------------------------------------------------------------------ |
---|
| 77 | // Higher-level string based routines (should be sufficient for most users) |
---|
| 78 | // ------------------------------------------------------------------------ |
---|
| 79 | |
---|
| 80 | // Sets "*output" to the compressed version of "input[0,input_length-1]". |
---|
| 81 | // Original contents of *output are lost. |
---|
| 82 | // |
---|
| 83 | // REQUIRES: "input[]" is not an alias of "*output". |
---|
| 84 | size_t Compress(const char* input, size_t input_length, string* output); |
---|
| 85 | |
---|
| 86 | // Decompresses "compressed[0,compressed_length-1]" to "*uncompressed". |
---|
| 87 | // Original contents of "*uncompressed" are lost. |
---|
| 88 | // |
---|
| 89 | // REQUIRES: "compressed[]" is not an alias of "*uncompressed". |
---|
| 90 | // |
---|
| 91 | // returns false if the message is corrupted and could not be decompressed |
---|
| 92 | bool Uncompress(const char* compressed, size_t compressed_length, |
---|
| 93 | string* uncompressed); |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | // ------------------------------------------------------------------------ |
---|
| 97 | // Lower-level character array based routines. May be useful for |
---|
| 98 | // efficiency reasons in certain circumstances. |
---|
| 99 | // ------------------------------------------------------------------------ |
---|
| 100 | |
---|
| 101 | // REQUIRES: "compressed" must point to an area of memory that is at |
---|
| 102 | // least "MaxCompressedLength(input_length)" bytes in length. |
---|
| 103 | // |
---|
| 104 | // Takes the data stored in "input[0..input_length]" and stores |
---|
| 105 | // it in the array pointed to by "compressed". |
---|
| 106 | // |
---|
| 107 | // "*compressed_length" is set to the length of the compressed output. |
---|
| 108 | // |
---|
| 109 | // Example: |
---|
| 110 | // char* output = new char[snappy::MaxCompressedLength(input_length)]; |
---|
| 111 | // size_t output_length; |
---|
| 112 | // RawCompress(input, input_length, output, &output_length); |
---|
| 113 | // ... Process(output, output_length) ... |
---|
| 114 | // delete [] output; |
---|
| 115 | void RawCompress(const char* input, |
---|
| 116 | size_t input_length, |
---|
| 117 | char* compressed, |
---|
| 118 | size_t* compressed_length); |
---|
| 119 | |
---|
| 120 | // Given data in "compressed[0..compressed_length-1]" generated by |
---|
| 121 | // calling the Snappy::Compress routine, this routine |
---|
| 122 | // stores the uncompressed data to |
---|
| 123 | // uncompressed[0..GetUncompressedLength(compressed)-1] |
---|
| 124 | // returns false if the message is corrupted and could not be decrypted |
---|
| 125 | bool RawUncompress(const char* compressed, size_t compressed_length, |
---|
| 126 | char* uncompressed); |
---|
| 127 | |
---|
| 128 | // Given data from the byte source 'compressed' generated by calling |
---|
| 129 | // the Snappy::Compress routine, this routine stores the uncompressed |
---|
| 130 | // data to |
---|
| 131 | // uncompressed[0..GetUncompressedLength(compressed,compressed_length)-1] |
---|
| 132 | // returns false if the message is corrupted and could not be decrypted |
---|
| 133 | bool RawUncompress(Source* compressed, char* uncompressed); |
---|
| 134 | |
---|
| 135 | // Given data in "compressed[0..compressed_length-1]" generated by |
---|
| 136 | // calling the Snappy::Compress routine, this routine |
---|
| 137 | // stores the uncompressed data to the iovec "iov". The number of physical |
---|
| 138 | // buffers in "iov" is given by iov_cnt and their cumulative size |
---|
| 139 | // must be at least GetUncompressedLength(compressed). The individual buffers |
---|
| 140 | // in "iov" must not overlap with each other. |
---|
| 141 | // |
---|
| 142 | // returns false if the message is corrupted and could not be decrypted |
---|
| 143 | bool RawUncompressToIOVec(const char* compressed, size_t compressed_length, |
---|
| 144 | const struct iovec* iov, size_t iov_cnt); |
---|
| 145 | |
---|
| 146 | // Given data from the byte source 'compressed' generated by calling |
---|
| 147 | // the Snappy::Compress routine, this routine stores the uncompressed |
---|
| 148 | // data to the iovec "iov". The number of physical |
---|
| 149 | // buffers in "iov" is given by iov_cnt and their cumulative size |
---|
| 150 | // must be at least GetUncompressedLength(compressed). The individual buffers |
---|
| 151 | // in "iov" must not overlap with each other. |
---|
| 152 | // |
---|
| 153 | // returns false if the message is corrupted and could not be decrypted |
---|
| 154 | bool RawUncompressToIOVec(Source* compressed, const struct iovec* iov, |
---|
| 155 | size_t iov_cnt); |
---|
| 156 | |
---|
| 157 | // Returns the maximal size of the compressed representation of |
---|
| 158 | // input data that is "source_bytes" bytes in length; |
---|
| 159 | size_t MaxCompressedLength(size_t source_bytes); |
---|
| 160 | |
---|
| 161 | // REQUIRES: "compressed[]" was produced by RawCompress() or Compress() |
---|
| 162 | // Returns true and stores the length of the uncompressed data in |
---|
| 163 | // *result normally. Returns false on parsing error. |
---|
| 164 | // This operation takes O(1) time. |
---|
| 165 | bool GetUncompressedLength(const char* compressed, size_t compressed_length, |
---|
| 166 | size_t* result); |
---|
| 167 | |
---|
| 168 | // Returns true iff the contents of "compressed[]" can be uncompressed |
---|
| 169 | // successfully. Does not return the uncompressed data. Takes |
---|
| 170 | // time proportional to compressed_length, but is usually at least |
---|
| 171 | // a factor of four faster than actual decompression. |
---|
| 172 | bool IsValidCompressedBuffer(const char* compressed, |
---|
| 173 | size_t compressed_length); |
---|
| 174 | |
---|
| 175 | // The size of a compression block. Note that many parts of the compression |
---|
| 176 | // code assumes that kBlockSize <= 65536; in particular, the hash table |
---|
| 177 | // can only store 16-bit offsets, and EmitCopy() also assumes the offset |
---|
| 178 | // is 65535 bytes or less. Note also that if you change this, it will |
---|
| 179 | // affect the framing format (see framing_format.txt). |
---|
| 180 | // |
---|
| 181 | // Note that there might be older data around that is compressed with larger |
---|
| 182 | // block sizes, so the decompression code should not rely on the |
---|
| 183 | // non-existence of long backreferences. |
---|
| 184 | static const int kBlockLog = 16; |
---|
| 185 | static const size_t kBlockSize = 1 << kBlockLog; |
---|
| 186 | |
---|
| 187 | static const int kMaxHashTableBits = 14; |
---|
| 188 | static const size_t kMaxHashTableSize = 1 << kMaxHashTableBits; |
---|
| 189 | } // end namespace snappy |
---|
| 190 | |
---|
| 191 | |
---|
| 192 | #endif // UTIL_SNAPPY_SNAPPY_H__ |
---|