James Kuszmaul | 48dd4c8 | 2021-10-27 20:04:08 -0700 | [diff] [blame^] | 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 THIRD_PARTY_SNAPPY_SNAPPY_H__ |
| 40 | #define THIRD_PARTY_SNAPPY_SNAPPY_H__ |
| 41 | |
| 42 | #include <stddef.h> |
| 43 | #include <stdint.h> |
| 44 | |
| 45 | #include <string> |
| 46 | |
| 47 | #include "snappy-stubs-public.h" |
| 48 | |
| 49 | namespace snappy { |
| 50 | class Source; |
| 51 | class Sink; |
| 52 | |
| 53 | // ------------------------------------------------------------------------ |
| 54 | // Generic compression/decompression routines. |
| 55 | // ------------------------------------------------------------------------ |
| 56 | |
| 57 | // Compress the bytes read from "*source" and append to "*sink". Return the |
| 58 | // number of bytes written. |
| 59 | size_t Compress(Source* source, Sink* sink); |
| 60 | |
| 61 | // Find the uncompressed length of the given stream, as given by the header. |
| 62 | // Note that the true length could deviate from this; the stream could e.g. |
| 63 | // be truncated. |
| 64 | // |
| 65 | // Also note that this leaves "*source" in a state that is unsuitable for |
| 66 | // further operations, such as RawUncompress(). You will need to rewind |
| 67 | // or recreate the source yourself before attempting any further calls. |
| 68 | bool GetUncompressedLength(Source* source, uint32_t* result); |
| 69 | |
| 70 | // ------------------------------------------------------------------------ |
| 71 | // Higher-level string based routines (should be sufficient for most users) |
| 72 | // ------------------------------------------------------------------------ |
| 73 | |
| 74 | // Sets "*compressed" to the compressed version of "input[0,input_length-1]". |
| 75 | // Original contents of *compressed are lost. |
| 76 | // |
| 77 | // REQUIRES: "input[]" is not an alias of "*compressed". |
| 78 | size_t Compress(const char* input, size_t input_length, |
| 79 | std::string* compressed); |
| 80 | |
| 81 | // Decompresses "compressed[0,compressed_length-1]" to "*uncompressed". |
| 82 | // Original contents of "*uncompressed" are lost. |
| 83 | // |
| 84 | // REQUIRES: "compressed[]" is not an alias of "*uncompressed". |
| 85 | // |
| 86 | // returns false if the message is corrupted and could not be decompressed |
| 87 | bool Uncompress(const char* compressed, size_t compressed_length, |
| 88 | std::string* uncompressed); |
| 89 | |
| 90 | // Decompresses "compressed" to "*uncompressed". |
| 91 | // |
| 92 | // returns false if the message is corrupted and could not be decompressed |
| 93 | bool Uncompress(Source* compressed, Sink* uncompressed); |
| 94 | |
| 95 | // This routine uncompresses as much of the "compressed" as possible |
| 96 | // into sink. It returns the number of valid bytes added to sink |
| 97 | // (extra invalid bytes may have been added due to errors; the caller |
| 98 | // should ignore those). The emitted data typically has length |
| 99 | // GetUncompressedLength(), but may be shorter if an error is |
| 100 | // encountered. |
| 101 | size_t UncompressAsMuchAsPossible(Source* compressed, Sink* uncompressed); |
| 102 | |
| 103 | // ------------------------------------------------------------------------ |
| 104 | // Lower-level character array based routines. May be useful for |
| 105 | // efficiency reasons in certain circumstances. |
| 106 | // ------------------------------------------------------------------------ |
| 107 | |
| 108 | // REQUIRES: "compressed" must point to an area of memory that is at |
| 109 | // least "MaxCompressedLength(input_length)" bytes in length. |
| 110 | // |
| 111 | // Takes the data stored in "input[0..input_length]" and stores |
| 112 | // it in the array pointed to by "compressed". |
| 113 | // |
| 114 | // "*compressed_length" is set to the length of the compressed output. |
| 115 | // |
| 116 | // Example: |
| 117 | // char* output = new char[snappy::MaxCompressedLength(input_length)]; |
| 118 | // size_t output_length; |
| 119 | // RawCompress(input, input_length, output, &output_length); |
| 120 | // ... Process(output, output_length) ... |
| 121 | // delete [] output; |
| 122 | void RawCompress(const char* input, |
| 123 | size_t input_length, |
| 124 | char* compressed, |
| 125 | size_t* compressed_length); |
| 126 | |
| 127 | // Given data in "compressed[0..compressed_length-1]" generated by |
| 128 | // calling the Snappy::Compress routine, this routine |
| 129 | // stores the uncompressed data to |
| 130 | // uncompressed[0..GetUncompressedLength(compressed)-1] |
| 131 | // returns false if the message is corrupted and could not be decrypted |
| 132 | bool RawUncompress(const char* compressed, size_t compressed_length, |
| 133 | char* uncompressed); |
| 134 | |
| 135 | // Given data from the byte source 'compressed' generated by calling |
| 136 | // the Snappy::Compress routine, this routine stores the uncompressed |
| 137 | // data to |
| 138 | // uncompressed[0..GetUncompressedLength(compressed,compressed_length)-1] |
| 139 | // returns false if the message is corrupted and could not be decrypted |
| 140 | bool RawUncompress(Source* compressed, char* uncompressed); |
| 141 | |
| 142 | // Given data in "compressed[0..compressed_length-1]" generated by |
| 143 | // calling the Snappy::Compress routine, this routine |
| 144 | // stores the uncompressed data to the iovec "iov". The number of physical |
| 145 | // buffers in "iov" is given by iov_cnt and their cumulative size |
| 146 | // must be at least GetUncompressedLength(compressed). The individual buffers |
| 147 | // in "iov" must not overlap with each other. |
| 148 | // |
| 149 | // returns false if the message is corrupted and could not be decrypted |
| 150 | bool RawUncompressToIOVec(const char* compressed, size_t compressed_length, |
| 151 | const struct iovec* iov, size_t iov_cnt); |
| 152 | |
| 153 | // Given data from the byte source 'compressed' generated by calling |
| 154 | // the Snappy::Compress routine, this routine stores the uncompressed |
| 155 | // data to the iovec "iov". The number of physical |
| 156 | // buffers in "iov" is given by iov_cnt and their cumulative size |
| 157 | // must be at least GetUncompressedLength(compressed). The individual buffers |
| 158 | // in "iov" must not overlap with each other. |
| 159 | // |
| 160 | // returns false if the message is corrupted and could not be decrypted |
| 161 | bool RawUncompressToIOVec(Source* compressed, const struct iovec* iov, |
| 162 | size_t iov_cnt); |
| 163 | |
| 164 | // Returns the maximal size of the compressed representation of |
| 165 | // input data that is "source_bytes" bytes in length; |
| 166 | size_t MaxCompressedLength(size_t source_bytes); |
| 167 | |
| 168 | // REQUIRES: "compressed[]" was produced by RawCompress() or Compress() |
| 169 | // Returns true and stores the length of the uncompressed data in |
| 170 | // *result normally. Returns false on parsing error. |
| 171 | // This operation takes O(1) time. |
| 172 | bool GetUncompressedLength(const char* compressed, size_t compressed_length, |
| 173 | size_t* result); |
| 174 | |
| 175 | // Returns true iff the contents of "compressed[]" can be uncompressed |
| 176 | // successfully. Does not return the uncompressed data. Takes |
| 177 | // time proportional to compressed_length, but is usually at least |
| 178 | // a factor of four faster than actual decompression. |
| 179 | bool IsValidCompressedBuffer(const char* compressed, |
| 180 | size_t compressed_length); |
| 181 | |
| 182 | // Returns true iff the contents of "compressed" can be uncompressed |
| 183 | // successfully. Does not return the uncompressed data. Takes |
| 184 | // time proportional to *compressed length, but is usually at least |
| 185 | // a factor of four faster than actual decompression. |
| 186 | // On success, consumes all of *compressed. On failure, consumes an |
| 187 | // unspecified prefix of *compressed. |
| 188 | bool IsValidCompressed(Source* compressed); |
| 189 | |
| 190 | // The size of a compression block. Note that many parts of the compression |
| 191 | // code assumes that kBlockSize <= 65536; in particular, the hash table |
| 192 | // can only store 16-bit offsets, and EmitCopy() also assumes the offset |
| 193 | // is 65535 bytes or less. Note also that if you change this, it will |
| 194 | // affect the framing format (see framing_format.txt). |
| 195 | // |
| 196 | // Note that there might be older data around that is compressed with larger |
| 197 | // block sizes, so the decompression code should not rely on the |
| 198 | // non-existence of long backreferences. |
| 199 | static constexpr int kBlockLog = 16; |
| 200 | static constexpr size_t kBlockSize = 1 << kBlockLog; |
| 201 | |
| 202 | static constexpr int kMinHashTableBits = 8; |
| 203 | static constexpr size_t kMinHashTableSize = 1 << kMinHashTableBits; |
| 204 | |
| 205 | static constexpr int kMaxHashTableBits = 14; |
| 206 | static constexpr size_t kMaxHashTableSize = 1 << kMaxHashTableBits; |
| 207 | } // end namespace snappy |
| 208 | |
| 209 | #endif // THIRD_PARTY_SNAPPY_SNAPPY_H__ |