snappy-c.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2011 Martin Gieseking <martin.gieseking@uos.de>.
  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. #include "snappy.h"
  29. #include "snappy-c.h"
  30. extern "C" {
  31. snappy_status snappy_compress(const char* input,
  32. size_t input_length,
  33. char* compressed,
  34. size_t *compressed_length) {
  35. if (*compressed_length < snappy_max_compressed_length(input_length)) {
  36. return SNAPPY_BUFFER_TOO_SMALL;
  37. }
  38. snappy::RawCompress(input, input_length, compressed, compressed_length);
  39. return SNAPPY_OK;
  40. }
  41. snappy_status snappy_uncompress(const char* compressed,
  42. size_t compressed_length,
  43. char* uncompressed,
  44. size_t* uncompressed_length) {
  45. size_t real_uncompressed_length;
  46. if (!snappy::GetUncompressedLength(compressed,
  47. compressed_length,
  48. &real_uncompressed_length)) {
  49. return SNAPPY_INVALID_INPUT;
  50. }
  51. if (*uncompressed_length < real_uncompressed_length) {
  52. return SNAPPY_BUFFER_TOO_SMALL;
  53. }
  54. if (!snappy::RawUncompress(compressed, compressed_length, uncompressed)) {
  55. return SNAPPY_INVALID_INPUT;
  56. }
  57. *uncompressed_length = real_uncompressed_length;
  58. return SNAPPY_OK;
  59. }
  60. size_t snappy_max_compressed_length(size_t source_length) {
  61. return snappy::MaxCompressedLength(source_length);
  62. }
  63. snappy_status snappy_uncompressed_length(const char *compressed,
  64. size_t compressed_length,
  65. size_t *result) {
  66. if (snappy::GetUncompressedLength(compressed,
  67. compressed_length,
  68. result)) {
  69. return SNAPPY_OK;
  70. } else {
  71. return SNAPPY_INVALID_INPUT;
  72. }
  73. }
  74. snappy_status snappy_validate_compressed_buffer(const char *compressed,
  75. size_t compressed_length) {
  76. if (snappy::IsValidCompressedBuffer(compressed, compressed_length)) {
  77. return SNAPPY_OK;
  78. } else {
  79. return SNAPPY_INVALID_INPUT;
  80. }
  81. }
  82. } // extern "C"