snappy-c.h 5.3 KB

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