compression.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*************************************************************************
  2. *
  3. * Copyright 2022 Realm Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. **************************************************************************/
  18. #ifndef REALM_UTIL_COMPRESSION_HPP
  19. #define REALM_UTIL_COMPRESSION_HPP
  20. #include <realm/util/buffer.hpp>
  21. #include <realm/util/features.h>
  22. #include <realm/util/input_stream.hpp>
  23. #include <realm/util/span.hpp>
  24. #include <array>
  25. #include <memory>
  26. #include <system_error>
  27. #include <stdint.h>
  28. #include <stddef.h>
  29. #include <string>
  30. #include <vector>
  31. // Use libcompression by default on Apple platforms, but it can be disabled to
  32. // test the zlib codepaths
  33. #ifndef REALM_USE_LIBCOMPRESSION
  34. #define REALM_USE_LIBCOMPRESSION REALM_PLATFORM_APPLE
  35. #endif
  36. namespace realm::util::compression {
  37. enum class error {
  38. out_of_memory = 1,
  39. compress_buffer_too_small = 2,
  40. compress_error = 3,
  41. compress_input_too_long = 4,
  42. corrupt_input = 5,
  43. incorrect_decompressed_size = 6,
  44. decompress_error = 7,
  45. decompress_unsupported = 8,
  46. };
  47. const std::error_category& error_category() noexcept;
  48. std::error_code make_error_code(error) noexcept;
  49. } // namespace realm::util::compression
  50. namespace std {
  51. template <>
  52. struct is_error_code_enum<realm::util::compression::error> {
  53. static const bool value = true;
  54. };
  55. } // namespace std
  56. namespace realm::util::compression {
  57. class Alloc {
  58. public:
  59. // Returns null on "out of memory"
  60. virtual void* alloc(size_t size) noexcept = 0;
  61. virtual void free(void* addr) noexcept = 0;
  62. virtual ~Alloc() {}
  63. };
  64. class CompressMemoryArena : public Alloc {
  65. public:
  66. void* alloc(size_t size) noexcept override final
  67. {
  68. size_t offset = m_offset;
  69. size_t misalignment = offset % alignof(std::max_align_t);
  70. size_t padding = (misalignment == 0) ? 0 : (alignof(std::max_align_t) - misalignment);
  71. if (padding > m_size - offset)
  72. return nullptr;
  73. offset += padding;
  74. REALM_ASSERT(offset % alignof(std::max_align_t) == 0);
  75. void* addr = m_buffer.get() + offset;
  76. if (size > m_size - offset)
  77. return nullptr;
  78. m_offset = offset + size;
  79. return addr;
  80. }
  81. void free(void*) noexcept override final
  82. {
  83. // No-op
  84. }
  85. void reset() noexcept
  86. {
  87. m_offset = 0;
  88. }
  89. size_t size() const noexcept
  90. {
  91. return m_size;
  92. }
  93. void resize(size_t size)
  94. {
  95. m_buffer = std::make_unique<char[]>(size); // Throws
  96. m_size = size;
  97. m_offset = 0;
  98. }
  99. private:
  100. size_t m_size = 0, m_offset = 0;
  101. std::unique_ptr<char[]> m_buffer;
  102. };
  103. /// compress_bound() calculates an upper bound on the size of the compressed
  104. /// data. The caller can use this function to allocate memory buffer calling
  105. /// compress(). Returns 0 if the bound would overflow size_t.
  106. size_t compress_bound(size_t uncompressed_size) noexcept;
  107. /// compress() compresses the data in the \a uncompressed_buf using zlib and
  108. /// stores it in \a compressed_buf. If compression is successful, the
  109. /// compressed size is stored in \a compressed_size. \a compression_level is
  110. /// [1-9] with 1 the fastest for the current zlib implementation. The returned
  111. /// error code is of category compression::error_category. If \a Alloc is
  112. /// non-null, it is used for all memory allocations inside compress() and
  113. /// compress() will not throw any exceptions.
  114. std::error_code compress(Span<const char> uncompressed_buf, Span<char> compressed_buf, size_t& compressed_size,
  115. int compression_level = 1, Alloc* custom_allocator = nullptr);
  116. /// decompress() decompresses zlib-compressed the data in \a compressed_buf into \a decompressed_buf.
  117. /// decompress may throw std::bad_alloc, but all other errors (including the
  118. /// target buffer being too small) are reported by returning an error code of
  119. /// category compression::error_code.
  120. std::error_code decompress(Span<const char> compressed_buf, Span<char> decompressed_buf);
  121. /// decompress() decompresses zlib-compressed data in \a compressed into \a
  122. /// decompressed_buf. decompress may throw std::bad_alloc or any exceptions
  123. /// thrown by \a compressed, but all other errors (including the target buffer
  124. /// being too small) are reported by returning an error code of category
  125. /// compression::error_code.
  126. std::error_code decompress(NoCopyInputStream& compressed, Span<char> decompressed_buf);
  127. /// allocate_and_compress() compresses the data in \a uncompressed_buf using
  128. /// zlib, storing the result in \a compressed_buf. \a compressed_buf is resized
  129. /// to the required size, and on non-error return has size equal to the
  130. /// compressed size. All errors other than std::bad_alloc are returned as an
  131. /// error code of categrory compression::error_code.
  132. std::error_code allocate_and_compress(CompressMemoryArena& compress_memory_arena, Span<const char> uncompressed_buf,
  133. std::vector<char>& compressed_buf);
  134. /// decompress() decompresses data produced by
  135. /// allocate_and_compress_nonportable() in \a compressed into \a decompressed.
  136. /// \a decompressed is resized to the required size, and on non-error return
  137. /// has size equal to the compressed size. All errors other than std::bad_alloc
  138. /// are returned as an error code of categrory compression::error_code.
  139. std::error_code decompress_nonportable(NoCopyInputStream& compressed, AppendBuffer<char>& decompressed);
  140. /// decompress_nonportable_input_stream() returns an input stream which wraps
  141. /// the \a source input stream and decompresses data produced by
  142. /// allocate_and_compress_nonportable(). The returned input stream will be
  143. /// nullptr if the source data is in an unsupported format. Decompression
  144. /// errors will be reported by throwing a std::system_error containing an error
  145. /// code of category compression::error_code. If this returns a non-nullptr
  146. /// input stream, \a total_size is set to the decompressed size of the data
  147. /// which will be produced by fully consuming the returned input stream.
  148. std::unique_ptr<NoCopyInputStream> decompress_nonportable_input_stream(NoCopyInputStream& source, size_t& total_size);
  149. /// allocate_and_compress_nonportable() compresses the data stored in \a
  150. /// uncompressed_buf, writing it to \a compressed_buf.
  151. ///
  152. /// The compressed data may use one of several compression algorithms and
  153. /// contains a nonstandard header, and so it can only be read by
  154. /// decompress_nonportable() or decompress_nonportable_input_stream(). The set
  155. /// of compression algorithms available is platform-specific, so data
  156. /// compressed with this function must only be used locally.
  157. ///
  158. /// This function reports errors by throwing a std::system_error containing an
  159. /// error code of category compression::error_code. It may additionally throw
  160. /// std::bad_alloc.
  161. void allocate_and_compress_nonportable(CompressMemoryArena& compress_memory_arena, Span<const char> uncompressed_buf,
  162. util::AppendBuffer<char>& compressed_buf);
  163. /// allocate_and_compress_nonportable() compresses the data stored in \a
  164. /// uncompressed_buf, returning a buffer of the appropriate size.
  165. ///
  166. /// The compressed data may use one of several compression algorithms and
  167. /// contains a nonstandard header, and so it can only be read by
  168. /// decompress_nonportable() or decompress_nonportable_input_stream(). The set
  169. /// of compression algorithms available is platform-specific, so data
  170. /// compressed with this function must only be used locally.
  171. ///
  172. /// This function reports errors by throwing a std::system_error containing an
  173. /// error code of category compression::error_code. It may additionally throw
  174. /// std::bad_alloc.
  175. util::AppendBuffer<char> allocate_and_compress_nonportable(Span<const char> uncompressed_buf);
  176. /// Get the decompressed size of the data produced by
  177. /// allocate_and_compress_nonportable() which is stored in \a source.
  178. size_t get_uncompressed_size_from_header(NoCopyInputStream& source);
  179. } // namespace realm::util::compression
  180. #endif // REALM_UTIL_COMPRESSION_HPP