snappy-c.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #ifndef UTIL_SNAPPY_OPENSOURCE_SNAPPY_C_H_
  33. #define UTIL_SNAPPY_OPENSOURCE_SNAPPY_C_H_
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. // The next is for getting the Snappy version even if used the C API.
  38. // Please note that this is only defined in the Blosc sources of Snappy.
  39. #define SNAPPY_MAJOR 1
  40. #define SNAPPY_MINOR 1
  41. #define SNAPPY_PATCHLEVEL 1
  42. #define SNAPPY_VERSION \
  43. ((SNAPPY_MAJOR << 16) | (SNAPPY_MINOR << 8) | SNAPPY_PATCHLEVEL)
  44. #include <stddef.h>
  45. /*
  46. * Return values; see the documentation for each function to know
  47. * what each can return.
  48. */
  49. typedef enum {
  50. SNAPPY_OK = 0,
  51. SNAPPY_INVALID_INPUT = 1,
  52. SNAPPY_BUFFER_TOO_SMALL = 2
  53. } snappy_status;
  54. /*
  55. * Takes the data stored in "input[0..input_length-1]" and stores
  56. * it in the array pointed to by "compressed".
  57. *
  58. * <compressed_length> signals the space available in "compressed".
  59. * If it is not at least equal to "snappy_max_compressed_length(input_length)",
  60. * SNAPPY_BUFFER_TOO_SMALL is returned. After successful compression,
  61. * <compressed_length> contains the true length of the compressed output,
  62. * and SNAPPY_OK is returned.
  63. *
  64. * Example:
  65. * size_t output_length = snappy_max_compressed_length(input_length);
  66. * char* output = (char*)malloc(output_length);
  67. * if (snappy_compress(input, input_length, output, &output_length)
  68. * == SNAPPY_OK) {
  69. * ... Process(output, output_length) ...
  70. * }
  71. * free(output);
  72. */
  73. snappy_status snappy_compress(const char* input,
  74. size_t input_length,
  75. char* compressed,
  76. size_t* compressed_length);
  77. /*
  78. * Given data in "compressed[0..compressed_length-1]" generated by
  79. * calling the snappy_compress routine, this routine stores
  80. * the uncompressed data to
  81. * uncompressed[0..uncompressed_length-1].
  82. * Returns failure (a value not equal to SNAPPY_OK) if the message
  83. * is corrupted and could not be decrypted.
  84. *
  85. * <uncompressed_length> signals the space available in "uncompressed".
  86. * If it is not at least equal to the value returned by
  87. * snappy_uncompressed_length for this stream, SNAPPY_BUFFER_TOO_SMALL
  88. * is returned. After successful decompression, <uncompressed_length>
  89. * contains the true length of the decompressed output.
  90. *
  91. * Example:
  92. * size_t output_length;
  93. * if (snappy_uncompressed_length(input, input_length, &output_length)
  94. * != SNAPPY_OK) {
  95. * ... fail ...
  96. * }
  97. * char* output = (char*)malloc(output_length);
  98. * if (snappy_uncompress(input, input_length, output, &output_length)
  99. * == SNAPPY_OK) {
  100. * ... Process(output, output_length) ...
  101. * }
  102. * free(output);
  103. */
  104. snappy_status snappy_uncompress(const char* compressed,
  105. size_t compressed_length,
  106. char* uncompressed,
  107. size_t* uncompressed_length);
  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. * REQUIRES: "compressed[]" was produced by snappy_compress()
  115. * Returns SNAPPY_OK and stores the length of the uncompressed data in
  116. * *result normally. Returns SNAPPY_INVALID_INPUT on parsing error.
  117. * This operation takes O(1) time.
  118. */
  119. snappy_status snappy_uncompressed_length(const char* compressed,
  120. size_t compressed_length,
  121. size_t* result);
  122. /*
  123. * Check if the contents of "compressed[]" can be uncompressed successfully.
  124. * Does not return the uncompressed data; if so, returns SNAPPY_OK,
  125. * or if not, returns SNAPPY_INVALID_INPUT.
  126. * Takes time proportional to compressed_length, but is usually at least a
  127. * factor of four faster than actual decompression.
  128. */
  129. snappy_status snappy_validate_compressed_buffer(const char* compressed,
  130. size_t compressed_length);
  131. #ifdef __cplusplus
  132. } // extern "C"
  133. #endif
  134. #endif /* UTIL_SNAPPY_OPENSOURCE_SNAPPY_C_H_ */