michael@0: /* michael@0: * Copyright 2011 Martin Gieseking . michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions are michael@0: * met: michael@0: * michael@0: * * Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * * Redistributions in binary form must reproduce the above michael@0: * copyright notice, this list of conditions and the following disclaimer michael@0: * in the documentation and/or other materials provided with the michael@0: * distribution. michael@0: * * Neither the name of Google Inc. nor the names of its michael@0: * contributors may be used to endorse or promote products derived from michael@0: * this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: * michael@0: * Plain C interface (a wrapper around the C++ implementation). michael@0: */ michael@0: michael@0: #ifndef UTIL_SNAPPY_OPENSOURCE_SNAPPY_C_H_ michael@0: #define UTIL_SNAPPY_OPENSOURCE_SNAPPY_C_H_ michael@0: michael@0: #ifdef __cplusplus michael@0: extern "C" { michael@0: #endif michael@0: michael@0: #include michael@0: michael@0: /* michael@0: * Return values; see the documentation for each function to know michael@0: * what each can return. michael@0: */ michael@0: typedef enum { michael@0: SNAPPY_OK = 0, michael@0: SNAPPY_INVALID_INPUT = 1, michael@0: SNAPPY_BUFFER_TOO_SMALL = 2 michael@0: } snappy_status; michael@0: michael@0: /* michael@0: * Takes the data stored in "input[0..input_length-1]" and stores michael@0: * it in the array pointed to by "compressed". michael@0: * michael@0: * signals the space available in "compressed". michael@0: * If it is not at least equal to "snappy_max_compressed_length(input_length)", michael@0: * SNAPPY_BUFFER_TOO_SMALL is returned. After successful compression, michael@0: * contains the true length of the compressed output, michael@0: * and SNAPPY_OK is returned. michael@0: * michael@0: * Example: michael@0: * size_t output_length = snappy_max_compressed_length(input_length); michael@0: * char* output = (char*)malloc(output_length); michael@0: * if (snappy_compress(input, input_length, output, &output_length) michael@0: * == SNAPPY_OK) { michael@0: * ... Process(output, output_length) ... michael@0: * } michael@0: * free(output); michael@0: */ michael@0: snappy_status snappy_compress(const char* input, michael@0: size_t input_length, michael@0: char* compressed, michael@0: size_t* compressed_length); michael@0: michael@0: /* michael@0: * Given data in "compressed[0..compressed_length-1]" generated by michael@0: * calling the snappy_compress routine, this routine stores michael@0: * the uncompressed data to michael@0: * uncompressed[0..uncompressed_length-1]. michael@0: * Returns failure (a value not equal to SNAPPY_OK) if the message michael@0: * is corrupted and could not be decrypted. michael@0: * michael@0: * signals the space available in "uncompressed". michael@0: * If it is not at least equal to the value returned by michael@0: * snappy_uncompressed_length for this stream, SNAPPY_BUFFER_TOO_SMALL michael@0: * is returned. After successful decompression, michael@0: * contains the true length of the decompressed output. michael@0: * michael@0: * Example: michael@0: * size_t output_length; michael@0: * if (snappy_uncompressed_length(input, input_length, &output_length) michael@0: * != SNAPPY_OK) { michael@0: * ... fail ... michael@0: * } michael@0: * char* output = (char*)malloc(output_length); michael@0: * if (snappy_uncompress(input, input_length, output, &output_length) michael@0: * == SNAPPY_OK) { michael@0: * ... Process(output, output_length) ... michael@0: * } michael@0: * free(output); michael@0: */ michael@0: snappy_status snappy_uncompress(const char* compressed, michael@0: size_t compressed_length, michael@0: char* uncompressed, michael@0: size_t* uncompressed_length); michael@0: michael@0: /* michael@0: * Returns the maximal size of the compressed representation of michael@0: * input data that is "source_length" bytes in length. michael@0: */ michael@0: size_t snappy_max_compressed_length(size_t source_length); michael@0: michael@0: /* michael@0: * REQUIRES: "compressed[]" was produced by snappy_compress() michael@0: * Returns SNAPPY_OK and stores the length of the uncompressed data in michael@0: * *result normally. Returns SNAPPY_INVALID_INPUT on parsing error. michael@0: * This operation takes O(1) time. michael@0: */ michael@0: snappy_status snappy_uncompressed_length(const char* compressed, michael@0: size_t compressed_length, michael@0: size_t* result); michael@0: michael@0: /* michael@0: * Check if the contents of "compressed[]" can be uncompressed successfully. michael@0: * Does not return the uncompressed data; if so, returns SNAPPY_OK, michael@0: * or if not, returns SNAPPY_INVALID_INPUT. michael@0: * Takes time proportional to compressed_length, but is usually at least a michael@0: * factor of four faster than actual decompression. michael@0: */ michael@0: snappy_status snappy_validate_compressed_buffer(const char* compressed, michael@0: size_t compressed_length); michael@0: michael@0: #ifdef __cplusplus michael@0: } // extern "C" michael@0: #endif michael@0: michael@0: #endif /* UTIL_SNAPPY_OPENSOURCE_SNAPPY_C_H_ */