snappy.h 11 KB

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