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