snappy.h 13 KB

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