1 // Copyright 2005 and onwards Google Inc.
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
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
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.
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.
29 // A light-weight compression algorithm. It is designed for speed of
30 // compression and decompression, rather than for the utmost in space
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
39 #ifndef UTIL_SNAPPY_SNAPPY_H__
40 #define UTIL_SNAPPY_SNAPPY_H__
45 #include "snappy-stubs-public.h"
51 // ------------------------------------------------------------------------
52 // Generic compression/decompression routines.
53 // ------------------------------------------------------------------------
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);
59 bool GetUncompressedLength(Source* source, uint32* result);
61 // ------------------------------------------------------------------------
62 // Higher-level string based routines (should be sufficient for most users)
63 // ------------------------------------------------------------------------
65 // Sets "*output" to the compressed version of "input[0,input_length-1]".
66 // Original contents of *output are lost.
68 // REQUIRES: "input[]" is not an alias of "*output".
69 size_t Compress(const char* input, size_t input_length, string* output);
71 // Decompresses "compressed[0,compressed_length-1]" to "*uncompressed".
72 // Original contents of "*uncompressed" are lost.
74 // REQUIRES: "compressed[]" is not an alias of "*uncompressed".
76 // returns false if the message is corrupted and could not be decompressed
77 bool Uncompress(const char* compressed, size_t compressed_length,
78 string* uncompressed);
81 // ------------------------------------------------------------------------
82 // Lower-level character array based routines. May be useful for
83 // efficiency reasons in certain circumstances.
84 // ------------------------------------------------------------------------
86 // REQUIRES: "compressed" must point to an area of memory that is at
87 // least "MaxCompressedLength(input_length)" bytes in length.
89 // Takes the data stored in "input[0..input_length]" and stores
90 // it in the array pointed to by "compressed".
92 // "*compressed_length" is set to the length of the compressed output.
95 // char* output = new char[snappy::MaxCompressedLength(input_length)];
96 // size_t output_length;
97 // RawCompress(input, input_length, output, &output_length);
98 // ... Process(output, output_length) ...
100 void RawCompress(const char* input,
103 size_t* compressed_length);
105 // Given data in "compressed[0..compressed_length-1]" generated by
106 // calling the Snappy::Compress routine, this routine
107 // stores the uncompressed data to
108 // uncompressed[0..GetUncompressedLength(compressed)-1]
109 // returns false if the message is corrupted and could not be decrypted
110 bool RawUncompress(const char* compressed, size_t compressed_length,
113 // Given data from the byte source 'compressed' generated by calling
114 // the Snappy::Compress routine, this routine stores the uncompressed
116 // uncompressed[0..GetUncompressedLength(compressed,compressed_length)-1]
117 // returns false if the message is corrupted and could not be decrypted
118 bool RawUncompress(Source* compressed, char* uncompressed);
120 // Returns the maximal size of the compressed representation of
121 // input data that is "source_bytes" bytes in length;
122 size_t MaxCompressedLength(size_t source_bytes);
124 // REQUIRES: "compressed[]" was produced by RawCompress() or Compress()
125 // Returns true and stores the length of the uncompressed data in
126 // *result normally. Returns false on parsing error.
127 // This operation takes O(1) time.
128 bool GetUncompressedLength(const char* compressed, size_t compressed_length,
131 // Returns true iff the contents of "compressed[]" can be uncompressed
132 // successfully. Does not return the uncompressed data. Takes
133 // time proportional to compressed_length, but is usually at least
134 // a factor of four faster than actual decompression.
135 bool IsValidCompressedBuffer(const char* compressed,
136 size_t compressed_length);
138 // *** DO NOT CHANGE THE VALUE OF kBlockSize ***
140 // New Compression code chops up the input into blocks of at most
141 // the following size. This ensures that back-references in the
142 // output never cross kBlockSize block boundaries. This can be
143 // helpful in implementing blocked decompression. However the
144 // decompression code should not rely on this guarantee since older
145 // compression code may not obey it.
146 static const int kBlockLog = 15;
147 static const int kBlockSize = 1 << kBlockLog;
149 static const int kMaxHashTableBits = 14;
150 static const int kMaxHashTableSize = 1 << kMaxHashTableBits;
152 } // end namespace snappy
155 #endif // UTIL_SNAPPY_SNAPPY_H__