|
| 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