Skip to content

Commit c67fa0c

Browse files
Include C bindings of Snappy, contributed by Martin Gieseking. I've made a few changes since Martin's version; mostly style nits, but also a semantic change -- most functions that return bool in the C++ version now return an enum, to better match typical C (and zlib) semantics. I've kept the copyright notice, since Martin is obviously the author here; he has signed the contributor license agreement, though, so this should not hinder Google's use in the future. We'll need to update the libtool version number to match the added interface, but as of http://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html I'm going to wait until public release. R=csilvers DELTA=238 (233 added, 0 deleted, 5 changed) Revision created by MOE tool push_codebase. MOE_MIGRATION=1294 git-svn-id: https://snappy.googlecode.com/svn/trunk@27 03e5f5b5-db94-4691-08a0-1a8bf15f6143
1 parent 56be85c commit c67fa0c

File tree

4 files changed

+236
-5
lines changed

4 files changed

+236
-5
lines changed

Makefile.am

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ ACLOCAL_AMFLAGS = -I m4
22

33
# Library.
44
lib_LTLIBRARIES = libsnappy.la
5-
libsnappy_la_SOURCES = snappy.cc snappy-sinksource.cc snappy-stubs-internal.cc
5+
libsnappy_la_SOURCES = snappy.cc snappy-sinksource.cc snappy-stubs-internal.cc snappy-c.cc
66
libsnappy_la_LDFLAGS = -version-info $(SNAPPY_LTVERSION)
77

8-
include_HEADERS = snappy.h snappy-sinksource.h snappy-stubs-public.h
8+
include_HEADERS = snappy.h snappy-sinksource.h snappy-stubs-public.h snappy-c.h
99
noinst_HEADERS = snappy-internal.h snappy-stubs-internal.h snappy-test.h
1010

1111
# Unit tests and benchmarks.

README

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,13 @@ are of course most welcome; see "Contact", below.
6565
Usage
6666
=====
6767

68-
Note that Snappy, both the implementation and the interface,
69-
is written in C++.
68+
Note that Snappy, both the implementation and the main interface,
69+
is written in C++. However, several third-party bindings to other languages
70+
are available; see the Google Code page at http://code.google.com/p/snappy/
71+
for more information. Also, if you want to use Snappy from C code, you can
72+
use the included C bindings in snappy-c.h.
7073

71-
To use Snappy from your own program, include the file "snappy.h" from
74+
To use Snappy from your own C++ program, include the file "snappy.h" from
7275
your calling file, and link against the compiled library.
7376

7477
There are many ways to call Snappy, but the simplest possible is

snappy-c.cc

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright 2011 Martin Gieseking <[email protected]>.
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"

snappy-c.h

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* Copyright 2011 Martin Gieseking <[email protected]>.
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 UTIL_SNAPPY_OPENSOURCE_SNAPPY_C_H_
34+
#define UTIL_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 /* UTIL_SNAPPY_OPENSOURCE_SNAPPY_C_H_ */

0 commit comments

Comments
 (0)