snappy-ruby 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +77 -0
- data/Rakefile +12 -0
- data/ext/snappy/extconf.rb +83 -0
- data/ext/snappy/snappy-src/AUTHORS +1 -0
- data/ext/snappy/snappy-src/BUILD.bazel +211 -0
- data/ext/snappy/snappy-src/CMakeLists.txt +467 -0
- data/ext/snappy/snappy-src/CONTRIBUTING.md +31 -0
- data/ext/snappy/snappy-src/COPYING +54 -0
- data/ext/snappy/snappy-src/MODULE.bazel +23 -0
- data/ext/snappy/snappy-src/NEWS +215 -0
- data/ext/snappy/snappy-src/README.md +165 -0
- data/ext/snappy/snappy-src/WORKSPACE +27 -0
- data/ext/snappy/snappy-src/WORKSPACE.bzlmod +0 -0
- data/ext/snappy/snappy-src/cmake/SnappyConfig.cmake.in +33 -0
- data/ext/snappy/snappy-src/cmake/config.h.in +75 -0
- data/ext/snappy/snappy-src/config.h +78 -0
- data/ext/snappy/snappy-src/docs/README.md +72 -0
- data/ext/snappy/snappy-src/format_description.txt +110 -0
- data/ext/snappy/snappy-src/framing_format.txt +135 -0
- data/ext/snappy/snappy-src/snappy-c.cc +90 -0
- data/ext/snappy/snappy-src/snappy-c.h +138 -0
- data/ext/snappy/snappy-src/snappy-internal.h +444 -0
- data/ext/snappy/snappy-src/snappy-sinksource.cc +121 -0
- data/ext/snappy/snappy-src/snappy-sinksource.h +182 -0
- data/ext/snappy/snappy-src/snappy-stubs-internal.cc +42 -0
- data/ext/snappy/snappy-src/snappy-stubs-internal.h +531 -0
- data/ext/snappy/snappy-src/snappy-stubs-public.h +60 -0
- data/ext/snappy/snappy-src/snappy-stubs-public.h.in +63 -0
- data/ext/snappy/snappy-src/snappy-test.cc +503 -0
- data/ext/snappy/snappy-src/snappy-test.h +342 -0
- data/ext/snappy/snappy-src/snappy.cc +2666 -0
- data/ext/snappy/snappy-src/snappy.h +257 -0
- data/ext/snappy/snappy-src/snappy_test_data.cc +57 -0
- data/ext/snappy/snappy-src/snappy_test_data.h +68 -0
- data/ext/snappy/snappy-src/snappy_test_tool.cc +471 -0
- data/ext/snappy/snappy-src/snappy_unittest.cc +1023 -0
- data/ext/snappy/snappy.c +282 -0
- data/lib/snappy/snappy.so +0 -0
- data/lib/snappy.rb +5 -0
- metadata +142 -0
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
Snappy compressed format description
|
|
2
|
+
Last revised: 2011-10-05
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
This is not a formal specification, but should suffice to explain most
|
|
6
|
+
relevant parts of how the Snappy format works. It is originally based on
|
|
7
|
+
text by Zeev Tarantov.
|
|
8
|
+
|
|
9
|
+
Snappy is a LZ77-type compressor with a fixed, byte-oriented encoding.
|
|
10
|
+
There is no entropy encoder backend nor framing layer -- the latter is
|
|
11
|
+
assumed to be handled by other parts of the system.
|
|
12
|
+
|
|
13
|
+
This document only describes the format, not how the Snappy compressor nor
|
|
14
|
+
decompressor actually works. The correctness of the decompressor should not
|
|
15
|
+
depend on implementation details of the compressor, and vice versa.
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
1. Preamble
|
|
19
|
+
|
|
20
|
+
The stream starts with the uncompressed length (up to a maximum of 2^32 - 1),
|
|
21
|
+
stored as a little-endian varint. Varints consist of a series of bytes,
|
|
22
|
+
where the lower 7 bits are data and the upper bit is set iff there are
|
|
23
|
+
more bytes to be read. In other words, an uncompressed length of 64 would
|
|
24
|
+
be stored as 0x40, and an uncompressed length of 2097150 (0x1FFFFE)
|
|
25
|
+
would be stored as 0xFE 0xFF 0x7F.
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
2. The compressed stream itself
|
|
29
|
+
|
|
30
|
+
There are two types of elements in a Snappy stream: Literals and
|
|
31
|
+
copies (backreferences). There is no restriction on the order of elements,
|
|
32
|
+
except that the stream naturally cannot start with a copy. (Having
|
|
33
|
+
two literals in a row is never optimal from a compression point of
|
|
34
|
+
view, but nevertheless fully permitted.) Each element starts with a tag byte,
|
|
35
|
+
and the lower two bits of this tag byte signal what type of element will
|
|
36
|
+
follow:
|
|
37
|
+
|
|
38
|
+
00: Literal
|
|
39
|
+
01: Copy with 1-byte offset
|
|
40
|
+
10: Copy with 2-byte offset
|
|
41
|
+
11: Copy with 4-byte offset
|
|
42
|
+
|
|
43
|
+
The interpretation of the upper six bits are element-dependent.
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
2.1. Literals (00)
|
|
47
|
+
|
|
48
|
+
Literals are uncompressed data stored directly in the byte stream.
|
|
49
|
+
The literal length is stored differently depending on the length
|
|
50
|
+
of the literal:
|
|
51
|
+
|
|
52
|
+
- For literals up to and including 60 bytes in length, the upper
|
|
53
|
+
six bits of the tag byte contain (len-1). The literal follows
|
|
54
|
+
immediately thereafter in the bytestream.
|
|
55
|
+
- For longer literals, the (len-1) value is stored after the tag byte,
|
|
56
|
+
little-endian. The upper six bits of the tag byte describe how
|
|
57
|
+
many bytes are used for the length; 60, 61, 62 or 63 for
|
|
58
|
+
1-4 bytes, respectively. The literal itself follows after the
|
|
59
|
+
length.
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
2.2. Copies
|
|
63
|
+
|
|
64
|
+
Copies are references back into previous decompressed data, telling
|
|
65
|
+
the decompressor to reuse data it has previously decoded.
|
|
66
|
+
They encode two values: The _offset_, saying how many bytes back
|
|
67
|
+
from the current position to read, and the _length_, how many bytes
|
|
68
|
+
to copy. Offsets of zero can be encoded, but are not legal;
|
|
69
|
+
similarly, it is possible to encode backreferences that would
|
|
70
|
+
go past the end of the block (offset > current decompressed position),
|
|
71
|
+
which is also nonsensical and thus not allowed.
|
|
72
|
+
|
|
73
|
+
As in most LZ77-based compressors, the length can be larger than the offset,
|
|
74
|
+
yielding a form of run-length encoding (RLE). For instance,
|
|
75
|
+
"xababab" could be encoded as
|
|
76
|
+
|
|
77
|
+
<literal: "xab"> <copy: offset=2 length=4>
|
|
78
|
+
|
|
79
|
+
Note that since the current Snappy compressor works in 32 kB
|
|
80
|
+
blocks and does not do matching across blocks, it will never produce
|
|
81
|
+
a bitstream with offsets larger than about 32768. However, the
|
|
82
|
+
decompressor should not rely on this, as it may change in the future.
|
|
83
|
+
|
|
84
|
+
There are several different kinds of copy elements, depending on
|
|
85
|
+
the amount of bytes to be copied (length), and how far back the
|
|
86
|
+
data to be copied is (offset).
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
2.2.1. Copy with 1-byte offset (01)
|
|
90
|
+
|
|
91
|
+
These elements can encode lengths between [4..11] bytes and offsets
|
|
92
|
+
between [0..2047] bytes. (len-4) occupies three bits and is stored
|
|
93
|
+
in bits [2..4] of the tag byte. The offset occupies 11 bits, of which the
|
|
94
|
+
upper three are stored in the upper three bits ([5..7]) of the tag byte,
|
|
95
|
+
and the lower eight are stored in a byte following the tag byte.
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
2.2.2. Copy with 2-byte offset (10)
|
|
99
|
+
|
|
100
|
+
These elements can encode lengths between [1..64] and offsets from
|
|
101
|
+
[0..65535]. (len-1) occupies six bits and is stored in the upper
|
|
102
|
+
six bits ([2..7]) of the tag byte. The offset is stored as a
|
|
103
|
+
little-endian 16-bit integer in the two bytes following the tag byte.
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
2.2.3. Copy with 4-byte offset (11)
|
|
107
|
+
|
|
108
|
+
These are like the copies with 2-byte offsets (see previous subsection),
|
|
109
|
+
except that the offset is stored as a 32-bit integer instead of a
|
|
110
|
+
16-bit integer (and thus will occupy four bytes).
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
Snappy framing format description
|
|
2
|
+
Last revised: 2013-10-25
|
|
3
|
+
|
|
4
|
+
This format decribes a framing format for Snappy, allowing compressing to
|
|
5
|
+
files or streams that can then more easily be decompressed without having
|
|
6
|
+
to hold the entire stream in memory. It also provides data checksums to
|
|
7
|
+
help verify integrity. It does not provide metadata checksums, so it does
|
|
8
|
+
not protect against e.g. all forms of truncations.
|
|
9
|
+
|
|
10
|
+
Implementation of the framing format is optional for Snappy compressors and
|
|
11
|
+
decompressor; it is not part of the Snappy core specification.
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
1. General structure
|
|
15
|
+
|
|
16
|
+
The file consists solely of chunks, lying back-to-back with no padding
|
|
17
|
+
in between. Each chunk consists first a single byte of chunk identifier,
|
|
18
|
+
then a three-byte little-endian length of the chunk in bytes (from 0 to
|
|
19
|
+
16777215, inclusive), and then the data if any. The four bytes of chunk
|
|
20
|
+
header is not counted in the data length.
|
|
21
|
+
|
|
22
|
+
The different chunk types are listed below. The first chunk must always
|
|
23
|
+
be the stream identifier chunk (see section 4.1, below). The stream
|
|
24
|
+
ends when the file ends -- there is no explicit end-of-file marker.
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
2. File type identification
|
|
28
|
+
|
|
29
|
+
The following identifiers for this format are recommended where appropriate.
|
|
30
|
+
However, note that none have been registered officially, so this is only to
|
|
31
|
+
be taken as a guideline. We use "Snappy framed" to distinguish between this
|
|
32
|
+
format and raw Snappy data.
|
|
33
|
+
|
|
34
|
+
File extension: .sz
|
|
35
|
+
MIME type: application/x-snappy-framed
|
|
36
|
+
HTTP Content-Encoding: x-snappy-framed
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
3. Checksum format
|
|
40
|
+
|
|
41
|
+
Some chunks have data protected by a checksum (the ones that do will say so
|
|
42
|
+
explicitly). The checksums are always masked CRC-32Cs.
|
|
43
|
+
|
|
44
|
+
A description of CRC-32C can be found in RFC 3720, section 12.1, with
|
|
45
|
+
examples in section B.4.
|
|
46
|
+
|
|
47
|
+
Checksums are not stored directly, but masked, as checksumming data and
|
|
48
|
+
then its own checksum can be problematic. The masking is the same as used
|
|
49
|
+
in Apache Hadoop: Rotate the checksum by 15 bits, then add the constant
|
|
50
|
+
0xa282ead8 (using wraparound as normal for unsigned integers). This is
|
|
51
|
+
equivalent to the following C code:
|
|
52
|
+
|
|
53
|
+
uint32_t mask_checksum(uint32_t x) {
|
|
54
|
+
return ((x >> 15) | (x << 17)) + 0xa282ead8;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
Note that the masking is reversible.
|
|
58
|
+
|
|
59
|
+
The checksum is always stored as a four bytes long integer, in little-endian.
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
4. Chunk types
|
|
63
|
+
|
|
64
|
+
The currently supported chunk types are described below. The list may
|
|
65
|
+
be extended in the future.
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
4.1. Stream identifier (chunk type 0xff)
|
|
69
|
+
|
|
70
|
+
The stream identifier is always the first element in the stream.
|
|
71
|
+
It is exactly six bytes long and contains "sNaPpY" in ASCII. This means that
|
|
72
|
+
a valid Snappy framed stream always starts with the bytes
|
|
73
|
+
|
|
74
|
+
0xff 0x06 0x00 0x00 0x73 0x4e 0x61 0x50 0x70 0x59
|
|
75
|
+
|
|
76
|
+
The stream identifier chunk can come multiple times in the stream besides
|
|
77
|
+
the first; if such a chunk shows up, it should simply be ignored, assuming
|
|
78
|
+
it has the right length and contents. This allows for easy concatenation of
|
|
79
|
+
compressed files without the need for re-framing.
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
4.2. Compressed data (chunk type 0x00)
|
|
83
|
+
|
|
84
|
+
Compressed data chunks contain a normal Snappy compressed bitstream;
|
|
85
|
+
see the compressed format specification. The compressed data is preceded by
|
|
86
|
+
the CRC-32C (see section 3) of the _uncompressed_ data.
|
|
87
|
+
|
|
88
|
+
Note that the data portion of the chunk, i.e., the compressed contents,
|
|
89
|
+
can be at most 16777211 bytes (2^24 - 1, minus the checksum).
|
|
90
|
+
However, we place an additional restriction that the uncompressed data
|
|
91
|
+
in a chunk must be no longer than 65536 bytes. This allows consumers to
|
|
92
|
+
easily use small fixed-size buffers.
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
4.3. Uncompressed data (chunk type 0x01)
|
|
96
|
+
|
|
97
|
+
Uncompressed data chunks allow a compressor to send uncompressed,
|
|
98
|
+
raw data; this is useful if, for instance, uncompressible or
|
|
99
|
+
near-incompressible data is detected, and faster decompression is desired.
|
|
100
|
+
|
|
101
|
+
As in the compressed chunks, the data is preceded by its own masked
|
|
102
|
+
CRC-32C (see section 3).
|
|
103
|
+
|
|
104
|
+
An uncompressed data chunk, like compressed data chunks, should contain
|
|
105
|
+
no more than 65536 data bytes, so the maximum legal chunk length with the
|
|
106
|
+
checksum is 65540.
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
4.4. Padding (chunk type 0xfe)
|
|
110
|
+
|
|
111
|
+
Padding chunks allow a compressor to increase the size of the data stream
|
|
112
|
+
so that it complies with external demands, e.g. that the total number of
|
|
113
|
+
bytes is a multiple of some value.
|
|
114
|
+
|
|
115
|
+
All bytes of the padding chunk, except the chunk byte itself and the length,
|
|
116
|
+
should be zero, but decompressors must not try to interpret or verify the
|
|
117
|
+
padding data in any way.
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
4.5. Reserved unskippable chunks (chunk types 0x02-0x7f)
|
|
121
|
+
|
|
122
|
+
These are reserved for future expansion. A decoder that sees such a chunk
|
|
123
|
+
should immediately return an error, as it must assume it cannot decode the
|
|
124
|
+
stream correctly.
|
|
125
|
+
|
|
126
|
+
Future versions of this specification may define meanings for these chunks.
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
4.6. Reserved skippable chunks (chunk types 0x80-0xfd)
|
|
130
|
+
|
|
131
|
+
These are also reserved for future expansion, but unlike the chunks
|
|
132
|
+
described in 4.5, a decoder seeing these must skip them and continue
|
|
133
|
+
decoding.
|
|
134
|
+
|
|
135
|
+
Future versions of this specification may define meanings for these chunks.
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// Copyright 2011 Martin Gieseking <martin.gieseking@uos.de>.
|
|
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
|
+
#include "snappy.h"
|
|
30
|
+
#include "snappy-c.h"
|
|
31
|
+
|
|
32
|
+
extern "C" {
|
|
33
|
+
|
|
34
|
+
snappy_status snappy_compress(const char* input,
|
|
35
|
+
size_t input_length,
|
|
36
|
+
char* compressed,
|
|
37
|
+
size_t *compressed_length) {
|
|
38
|
+
if (*compressed_length < snappy_max_compressed_length(input_length)) {
|
|
39
|
+
return SNAPPY_BUFFER_TOO_SMALL;
|
|
40
|
+
}
|
|
41
|
+
snappy::RawCompress(input, input_length, compressed, compressed_length);
|
|
42
|
+
return SNAPPY_OK;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
snappy_status snappy_uncompress(const char* compressed,
|
|
46
|
+
size_t compressed_length,
|
|
47
|
+
char* uncompressed,
|
|
48
|
+
size_t* uncompressed_length) {
|
|
49
|
+
size_t real_uncompressed_length;
|
|
50
|
+
if (!snappy::GetUncompressedLength(compressed,
|
|
51
|
+
compressed_length,
|
|
52
|
+
&real_uncompressed_length)) {
|
|
53
|
+
return SNAPPY_INVALID_INPUT;
|
|
54
|
+
}
|
|
55
|
+
if (*uncompressed_length < real_uncompressed_length) {
|
|
56
|
+
return SNAPPY_BUFFER_TOO_SMALL;
|
|
57
|
+
}
|
|
58
|
+
if (!snappy::RawUncompress(compressed, compressed_length, uncompressed)) {
|
|
59
|
+
return SNAPPY_INVALID_INPUT;
|
|
60
|
+
}
|
|
61
|
+
*uncompressed_length = real_uncompressed_length;
|
|
62
|
+
return SNAPPY_OK;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
size_t snappy_max_compressed_length(size_t source_length) {
|
|
66
|
+
return snappy::MaxCompressedLength(source_length);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
snappy_status snappy_uncompressed_length(const char *compressed,
|
|
70
|
+
size_t compressed_length,
|
|
71
|
+
size_t *result) {
|
|
72
|
+
if (snappy::GetUncompressedLength(compressed,
|
|
73
|
+
compressed_length,
|
|
74
|
+
result)) {
|
|
75
|
+
return SNAPPY_OK;
|
|
76
|
+
} else {
|
|
77
|
+
return SNAPPY_INVALID_INPUT;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
snappy_status snappy_validate_compressed_buffer(const char *compressed,
|
|
82
|
+
size_t compressed_length) {
|
|
83
|
+
if (snappy::IsValidCompressedBuffer(compressed, compressed_length)) {
|
|
84
|
+
return SNAPPY_OK;
|
|
85
|
+
} else {
|
|
86
|
+
return SNAPPY_INVALID_INPUT;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
} // extern "C"
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2011 Martin Gieseking <martin.gieseking@uos.de>.
|
|
3
|
+
*
|
|
4
|
+
* Redistribution and use in source and binary forms, with or without
|
|
5
|
+
* modification, are permitted provided that the following conditions are
|
|
6
|
+
* met:
|
|
7
|
+
*
|
|
8
|
+
* * Redistributions of source code must retain the above copyright
|
|
9
|
+
* notice, this list of conditions and the following disclaimer.
|
|
10
|
+
* * Redistributions in binary form must reproduce the above
|
|
11
|
+
* copyright notice, this list of conditions and the following disclaimer
|
|
12
|
+
* in the documentation and/or other materials provided with the
|
|
13
|
+
* distribution.
|
|
14
|
+
* * Neither the name of Google Inc. nor the names of its
|
|
15
|
+
* contributors may be used to endorse or promote products derived from
|
|
16
|
+
* this software without specific prior written permission.
|
|
17
|
+
*
|
|
18
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
19
|
+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
20
|
+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
21
|
+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
22
|
+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
23
|
+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
24
|
+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
25
|
+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
26
|
+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
27
|
+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
29
|
+
*
|
|
30
|
+
* Plain C interface (a wrapper around the C++ implementation).
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
#ifndef THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_C_H_
|
|
34
|
+
#define THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_C_H_
|
|
35
|
+
|
|
36
|
+
#ifdef __cplusplus
|
|
37
|
+
extern "C" {
|
|
38
|
+
#endif
|
|
39
|
+
|
|
40
|
+
#include <stddef.h>
|
|
41
|
+
|
|
42
|
+
/*
|
|
43
|
+
* Return values; see the documentation for each function to know
|
|
44
|
+
* what each can return.
|
|
45
|
+
*/
|
|
46
|
+
typedef enum {
|
|
47
|
+
SNAPPY_OK = 0,
|
|
48
|
+
SNAPPY_INVALID_INPUT = 1,
|
|
49
|
+
SNAPPY_BUFFER_TOO_SMALL = 2
|
|
50
|
+
} snappy_status;
|
|
51
|
+
|
|
52
|
+
/*
|
|
53
|
+
* Takes the data stored in "input[0..input_length-1]" and stores
|
|
54
|
+
* it in the array pointed to by "compressed".
|
|
55
|
+
*
|
|
56
|
+
* <compressed_length> signals the space available in "compressed".
|
|
57
|
+
* If it is not at least equal to "snappy_max_compressed_length(input_length)",
|
|
58
|
+
* SNAPPY_BUFFER_TOO_SMALL is returned. After successful compression,
|
|
59
|
+
* <compressed_length> contains the true length of the compressed output,
|
|
60
|
+
* and SNAPPY_OK is returned.
|
|
61
|
+
*
|
|
62
|
+
* Example:
|
|
63
|
+
* size_t output_length = snappy_max_compressed_length(input_length);
|
|
64
|
+
* char* output = (char*)malloc(output_length);
|
|
65
|
+
* if (snappy_compress(input, input_length, output, &output_length)
|
|
66
|
+
* == SNAPPY_OK) {
|
|
67
|
+
* ... Process(output, output_length) ...
|
|
68
|
+
* }
|
|
69
|
+
* free(output);
|
|
70
|
+
*/
|
|
71
|
+
snappy_status snappy_compress(const char* input,
|
|
72
|
+
size_t input_length,
|
|
73
|
+
char* compressed,
|
|
74
|
+
size_t* compressed_length);
|
|
75
|
+
|
|
76
|
+
/*
|
|
77
|
+
* Given data in "compressed[0..compressed_length-1]" generated by
|
|
78
|
+
* calling the snappy_compress routine, this routine stores
|
|
79
|
+
* the uncompressed data to
|
|
80
|
+
* uncompressed[0..uncompressed_length-1].
|
|
81
|
+
* Returns failure (a value not equal to SNAPPY_OK) if the message
|
|
82
|
+
* is corrupted and could not be decrypted.
|
|
83
|
+
*
|
|
84
|
+
* <uncompressed_length> signals the space available in "uncompressed".
|
|
85
|
+
* If it is not at least equal to the value returned by
|
|
86
|
+
* snappy_uncompressed_length for this stream, SNAPPY_BUFFER_TOO_SMALL
|
|
87
|
+
* is returned. After successful decompression, <uncompressed_length>
|
|
88
|
+
* contains the true length of the decompressed output.
|
|
89
|
+
*
|
|
90
|
+
* Example:
|
|
91
|
+
* size_t output_length;
|
|
92
|
+
* if (snappy_uncompressed_length(input, input_length, &output_length)
|
|
93
|
+
* != SNAPPY_OK) {
|
|
94
|
+
* ... fail ...
|
|
95
|
+
* }
|
|
96
|
+
* char* output = (char*)malloc(output_length);
|
|
97
|
+
* if (snappy_uncompress(input, input_length, output, &output_length)
|
|
98
|
+
* == SNAPPY_OK) {
|
|
99
|
+
* ... Process(output, output_length) ...
|
|
100
|
+
* }
|
|
101
|
+
* free(output);
|
|
102
|
+
*/
|
|
103
|
+
snappy_status snappy_uncompress(const char* compressed,
|
|
104
|
+
size_t compressed_length,
|
|
105
|
+
char* uncompressed,
|
|
106
|
+
size_t* uncompressed_length);
|
|
107
|
+
|
|
108
|
+
/*
|
|
109
|
+
* Returns the maximal size of the compressed representation of
|
|
110
|
+
* input data that is "source_length" bytes in length.
|
|
111
|
+
*/
|
|
112
|
+
size_t snappy_max_compressed_length(size_t source_length);
|
|
113
|
+
|
|
114
|
+
/*
|
|
115
|
+
* REQUIRES: "compressed[]" was produced by snappy_compress()
|
|
116
|
+
* Returns SNAPPY_OK and stores the length of the uncompressed data in
|
|
117
|
+
* *result normally. Returns SNAPPY_INVALID_INPUT on parsing error.
|
|
118
|
+
* This operation takes O(1) time.
|
|
119
|
+
*/
|
|
120
|
+
snappy_status snappy_uncompressed_length(const char* compressed,
|
|
121
|
+
size_t compressed_length,
|
|
122
|
+
size_t* result);
|
|
123
|
+
|
|
124
|
+
/*
|
|
125
|
+
* Check if the contents of "compressed[]" can be uncompressed successfully.
|
|
126
|
+
* Does not return the uncompressed data; if so, returns SNAPPY_OK,
|
|
127
|
+
* or if not, returns SNAPPY_INVALID_INPUT.
|
|
128
|
+
* Takes time proportional to compressed_length, but is usually at least a
|
|
129
|
+
* factor of four faster than actual decompression.
|
|
130
|
+
*/
|
|
131
|
+
snappy_status snappy_validate_compressed_buffer(const char* compressed,
|
|
132
|
+
size_t compressed_length);
|
|
133
|
+
|
|
134
|
+
#ifdef __cplusplus
|
|
135
|
+
} // extern "C"
|
|
136
|
+
#endif
|
|
137
|
+
|
|
138
|
+
#endif /* THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_C_H_ */
|