![]() |
|
|||
File indexing completed on 2025-08-27 09:55:47
0001 // Copyright 2005 and onwards Google Inc. 0002 // 0003 // Redistribution and use in source and binary forms, with or without 0004 // modification, are permitted provided that the following conditions are 0005 // met: 0006 // 0007 // * Redistributions of source code must retain the above copyright 0008 // notice, this list of conditions and the following disclaimer. 0009 // * Redistributions in binary form must reproduce the above 0010 // copyright notice, this list of conditions and the following disclaimer 0011 // in the documentation and/or other materials provided with the 0012 // distribution. 0013 // * Neither the name of Google Inc. nor the names of its 0014 // contributors may be used to endorse or promote products derived from 0015 // this software without specific prior written permission. 0016 // 0017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 0018 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 0019 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 0020 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 0021 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 0022 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 0023 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 0024 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 0025 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 0026 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 0027 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 0028 // 0029 // A light-weight compression algorithm. It is designed for speed of 0030 // compression and decompression, rather than for the utmost in space 0031 // savings. 0032 // 0033 // For getting better compression ratios when you are compressing data 0034 // with long repeated sequences or compressing data that is similar to 0035 // other data, while still compressing fast, you might look at first 0036 // using BMDiff and then compressing the output of BMDiff with 0037 // Snappy. 0038 0039 #ifndef THIRD_PARTY_SNAPPY_SNAPPY_H__ 0040 #define THIRD_PARTY_SNAPPY_SNAPPY_H__ 0041 0042 #include <stddef.h> 0043 #include <stdint.h> 0044 0045 #include <string> 0046 0047 #include "snappy-stubs-public.h" 0048 0049 namespace snappy { 0050 class Source; 0051 class Sink; 0052 0053 struct CompressionOptions { 0054 // Compression level. 0055 // Level 1 is the fastest 0056 // Level 2 is a little slower but provides better compression. Level 2 is 0057 // **EXPERIMENTAL** for the time being. It might happen that we decide to 0058 // fall back to level 1 in the future. 0059 // Levels 3+ are currently not supported. We plan to support levels up to 0060 // 9 in the future. 0061 // If you played with other compression algorithms, level 1 is equivalent to 0062 // fast mode (level 1) of LZ4, level 2 is equivalent to LZ4's level 2 mode 0063 // and compresses somewhere around zstd:-3 and zstd:-2 but generally with 0064 // faster decompression speeds than snappy:1 and zstd:-3. 0065 int level = DefaultCompressionLevel(); 0066 0067 constexpr CompressionOptions() = default; 0068 constexpr CompressionOptions(int compression_level) 0069 : level(compression_level) {} 0070 static constexpr int MinCompressionLevel() { return 1; } 0071 static constexpr int MaxCompressionLevel() { return 2; } 0072 static constexpr int DefaultCompressionLevel() { return 1; } 0073 }; 0074 0075 // ------------------------------------------------------------------------ 0076 // Generic compression/decompression routines. 0077 // ------------------------------------------------------------------------ 0078 0079 // Compress the bytes read from "*reader" and append to "*writer". Return the 0080 // number of bytes written. 0081 // First version is to preserve ABI. 0082 size_t Compress(Source* reader, Sink* writer); 0083 size_t Compress(Source* reader, Sink* writer, 0084 CompressionOptions options); 0085 0086 // Find the uncompressed length of the given stream, as given by the header. 0087 // Note that the true length could deviate from this; the stream could e.g. 0088 // be truncated. 0089 // 0090 // Also note that this leaves "*source" in a state that is unsuitable for 0091 // further operations, such as RawUncompress(). You will need to rewind 0092 // or recreate the source yourself before attempting any further calls. 0093 bool GetUncompressedLength(Source* source, uint32_t* result); 0094 0095 // ------------------------------------------------------------------------ 0096 // Higher-level string based routines (should be sufficient for most users) 0097 // ------------------------------------------------------------------------ 0098 0099 // Sets "*compressed" to the compressed version of "input[0..input_length-1]". 0100 // Original contents of *compressed are lost. 0101 // 0102 // REQUIRES: "input[]" is not an alias of "*compressed". 0103 // First version is to preserve ABI. 0104 size_t Compress(const char* input, size_t input_length, 0105 std::string* compressed); 0106 size_t Compress(const char* input, size_t input_length, 0107 std::string* compressed, CompressionOptions options); 0108 0109 // Same as `Compress` above but taking an `iovec` array as input. Note that 0110 // this function preprocesses the inputs to compute the sum of 0111 // `iov[0..iov_cnt-1].iov_len` before reading. To avoid this, use 0112 // `RawCompressFromIOVec` below. 0113 // First version is to preserve ABI. 0114 size_t CompressFromIOVec(const struct iovec* iov, size_t iov_cnt, 0115 std::string* compressed); 0116 size_t CompressFromIOVec(const struct iovec* iov, size_t iov_cnt, 0117 std::string* compressed, 0118 CompressionOptions options); 0119 0120 // Decompresses "compressed[0..compressed_length-1]" to "*uncompressed". 0121 // Original contents of "*uncompressed" are lost. 0122 // 0123 // REQUIRES: "compressed[]" is not an alias of "*uncompressed". 0124 // 0125 // returns false if the message is corrupted and could not be decompressed 0126 bool Uncompress(const char* compressed, size_t compressed_length, 0127 std::string* uncompressed); 0128 0129 // Decompresses "compressed" to "*uncompressed". 0130 // 0131 // returns false if the message is corrupted and could not be decompressed 0132 bool Uncompress(Source* compressed, Sink* uncompressed); 0133 0134 // This routine uncompresses as much of the "compressed" as possible 0135 // into sink. It returns the number of valid bytes added to sink 0136 // (extra invalid bytes may have been added due to errors; the caller 0137 // should ignore those). The emitted data typically has length 0138 // GetUncompressedLength(), but may be shorter if an error is 0139 // encountered. 0140 size_t UncompressAsMuchAsPossible(Source* compressed, Sink* uncompressed); 0141 0142 // ------------------------------------------------------------------------ 0143 // Lower-level character array based routines. May be useful for 0144 // efficiency reasons in certain circumstances. 0145 // ------------------------------------------------------------------------ 0146 0147 // REQUIRES: "compressed" must point to an area of memory that is at 0148 // least "MaxCompressedLength(input_length)" bytes in length. 0149 // 0150 // Takes the data stored in "input[0..input_length]" and stores 0151 // it in the array pointed to by "compressed". 0152 // 0153 // "*compressed_length" is set to the length of the compressed output. 0154 // 0155 // Example: 0156 // char* output = new char[snappy::MaxCompressedLength(input_length)]; 0157 // size_t output_length; 0158 // RawCompress(input, input_length, output, &output_length); 0159 // ... Process(output, output_length) ... 0160 // delete [] output; 0161 void RawCompress(const char* input, size_t input_length, char* compressed, 0162 size_t* compressed_length); 0163 void RawCompress(const char* input, size_t input_length, char* compressed, 0164 size_t* compressed_length, CompressionOptions options); 0165 0166 // Same as `RawCompress` above but taking an `iovec` array as input. Note that 0167 // `uncompressed_length` is the total number of bytes to be read from the 0168 // elements of `iov` (_not_ the number of elements in `iov`). 0169 void RawCompressFromIOVec(const struct iovec* iov, size_t uncompressed_length, 0170 char* compressed, size_t* compressed_length); 0171 void RawCompressFromIOVec(const struct iovec* iov, size_t uncompressed_length, 0172 char* compressed, size_t* compressed_length, 0173 CompressionOptions options); 0174 0175 // Given data in "compressed[0..compressed_length-1]" generated by 0176 // calling the Snappy::Compress routine, this routine 0177 // stores the uncompressed data to 0178 // uncompressed[0..GetUncompressedLength(compressed)-1] 0179 // returns false if the message is corrupted and could not be decrypted 0180 bool RawUncompress(const char* compressed, size_t compressed_length, 0181 char* uncompressed); 0182 0183 // Given data from the byte source 'compressed' generated by calling 0184 // the Snappy::Compress routine, this routine stores the uncompressed 0185 // data to 0186 // uncompressed[0..GetUncompressedLength(compressed,compressed_length)-1] 0187 // returns false if the message is corrupted and could not be decrypted 0188 bool RawUncompress(Source* compressed, char* uncompressed); 0189 0190 // Given data in "compressed[0..compressed_length-1]" generated by 0191 // calling the Snappy::Compress routine, this routine 0192 // stores the uncompressed data to the iovec "iov". The number of physical 0193 // buffers in "iov" is given by iov_cnt and their cumulative size 0194 // must be at least GetUncompressedLength(compressed). The individual buffers 0195 // in "iov" must not overlap with each other. 0196 // 0197 // returns false if the message is corrupted and could not be decrypted 0198 bool RawUncompressToIOVec(const char* compressed, size_t compressed_length, 0199 const struct iovec* iov, size_t iov_cnt); 0200 0201 // Given data from the byte source 'compressed' generated by calling 0202 // the Snappy::Compress routine, this routine stores the uncompressed 0203 // data to the iovec "iov". The number of physical 0204 // buffers in "iov" is given by iov_cnt and their cumulative size 0205 // must be at least GetUncompressedLength(compressed). The individual buffers 0206 // in "iov" must not overlap with each other. 0207 // 0208 // returns false if the message is corrupted and could not be decrypted 0209 bool RawUncompressToIOVec(Source* compressed, const struct iovec* iov, 0210 size_t iov_cnt); 0211 0212 // Returns the maximal size of the compressed representation of 0213 // input data that is "source_bytes" bytes in length; 0214 size_t MaxCompressedLength(size_t source_bytes); 0215 0216 // REQUIRES: "compressed[]" was produced by RawCompress() or Compress() 0217 // Returns true and stores the length of the uncompressed data in 0218 // *result normally. Returns false on parsing error. 0219 // This operation takes O(1) time. 0220 bool GetUncompressedLength(const char* compressed, size_t compressed_length, 0221 size_t* result); 0222 0223 // Returns true iff the contents of "compressed[]" can be uncompressed 0224 // successfully. Does not return the uncompressed data. Takes 0225 // time proportional to compressed_length, but is usually at least 0226 // a factor of four faster than actual decompression. 0227 bool IsValidCompressedBuffer(const char* compressed, 0228 size_t compressed_length); 0229 0230 // Returns true iff the contents of "compressed" can be uncompressed 0231 // successfully. Does not return the uncompressed data. Takes 0232 // time proportional to *compressed length, but is usually at least 0233 // a factor of four faster than actual decompression. 0234 // On success, consumes all of *compressed. On failure, consumes an 0235 // unspecified prefix of *compressed. 0236 bool IsValidCompressed(Source* compressed); 0237 0238 // The size of a compression block. Note that many parts of the compression 0239 // code assumes that kBlockSize <= 65536; in particular, the hash table 0240 // can only store 16-bit offsets, and EmitCopy() also assumes the offset 0241 // is 65535 bytes or less. Note also that if you change this, it will 0242 // affect the framing format (see framing_format.txt). 0243 // 0244 // Note that there might be older data around that is compressed with larger 0245 // block sizes, so the decompression code should not rely on the 0246 // non-existence of long backreferences. 0247 static constexpr int kBlockLog = 16; 0248 static constexpr size_t kBlockSize = 1 << kBlockLog; 0249 0250 static constexpr int kMinHashTableBits = 8; 0251 static constexpr size_t kMinHashTableSize = 1 << kMinHashTableBits; 0252 0253 static constexpr int kMaxHashTableBits = 15; 0254 static constexpr size_t kMaxHashTableSize = 1 << kMaxHashTableBits; 0255 } // end namespace snappy 0256 0257 #endif // THIRD_PARTY_SNAPPY_SNAPPY_H__
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |