Compression.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /* Various simple compression/decompression functions. */
  6. #ifndef mozilla_Compression_h_
  7. #define mozilla_Compression_h_
  8. #include "mozilla/Assertions.h"
  9. #include "mozilla/Types.h"
  10. namespace mozilla {
  11. namespace Compression {
  12. /**
  13. * LZ4 is a very fast byte-wise compression algorithm.
  14. *
  15. * Compared to Google's Snappy it is faster to compress and decompress and
  16. * generally produces output of about the same size.
  17. *
  18. * Compared to zlib it compresses at about 10x the speed, decompresses at about
  19. * 4x the speed and produces output of about 1.5x the size.
  20. */
  21. class LZ4
  22. {
  23. public:
  24. /**
  25. * Compresses |aInputSize| bytes from |aSource| into |aDest|. Destination
  26. * buffer must be already allocated, and must be sized to handle worst cases
  27. * situations (input data not compressible). Worst case size evaluation is
  28. * provided by function maxCompressedSize()
  29. *
  30. * @param aInputSize is the input size. Max supported value is ~1.9GB
  31. * @return the number of bytes written in buffer |aDest|
  32. */
  33. static MFBT_API size_t
  34. compress(const char* aSource, size_t aInputSize, char* aDest);
  35. /**
  36. * Compress |aInputSize| bytes from |aSource| into an output buffer
  37. * |aDest| of maximum size |aMaxOutputSize|. If it cannot achieve it,
  38. * compression will stop, and result of the function will be zero,
  39. * |aDest| will still be written to, but since the number of input
  40. * bytes consumed is not returned the result is not usable.
  41. *
  42. * This function never writes outside of provided output buffer.
  43. *
  44. * @param aInputSize is the input size. Max supported value is ~1.9GB
  45. * @param aMaxOutputSize is the size of the destination buffer (which must
  46. * be already allocated)
  47. * @return the number of bytes written in buffer |aDest| or 0 if the
  48. * compression fails
  49. */
  50. static MFBT_API size_t
  51. compressLimitedOutput(const char* aSource, size_t aInputSize, char* aDest,
  52. size_t aMaxOutputSize);
  53. /**
  54. * If the source stream is malformed, the function will stop decoding
  55. * and return false.
  56. *
  57. * This function never writes outside of provided buffers, and never
  58. * modifies input buffer.
  59. *
  60. * Note: destination buffer must be already allocated, and its size must be a
  61. * minimum of |aOutputSize| bytes.
  62. *
  63. * @param aOutputSize is the output size, therefore the original size
  64. * @return true on success, false on failure
  65. */
  66. static MFBT_API MOZ_MUST_USE bool
  67. decompress(const char* aSource, char* aDest, size_t aOutputSize);
  68. /**
  69. * If the source stream is malformed, the function will stop decoding
  70. * and return false.
  71. *
  72. * This function never writes beyond aDest + aMaxOutputSize, and is
  73. * therefore protected against malicious data packets.
  74. *
  75. * Note: Destination buffer must be already allocated. This version is
  76. * slightly slower than the decompress without the aMaxOutputSize.
  77. *
  78. * @param aInputSize is the length of the input compressed data
  79. * @param aMaxOutputSize is the size of the destination buffer (which must be
  80. * already allocated)
  81. * @param aOutputSize the actual number of bytes decoded in the destination
  82. * buffer (necessarily <= aMaxOutputSize)
  83. * @return true on success, false on failure
  84. */
  85. static MFBT_API MOZ_MUST_USE bool
  86. decompress(const char* aSource, size_t aInputSize, char* aDest,
  87. size_t aMaxOutputSize, size_t* aOutputSize);
  88. /**
  89. * If the source stream is malformed, the function will stop decoding
  90. * and return false.
  91. *
  92. * This function never writes beyond aDest + aMaxOutputSize, and is
  93. * therefore protected against malicious data packets. It also ignores
  94. * unconsumed input upon reaching aMaxOutputSize and can therefore be used
  95. * for partial decompression.
  96. *
  97. * Note: Destination buffer must be already allocated. This version is
  98. * slightly slower than the decompress without the aMaxOutputSize.
  99. *
  100. * @param aInputSize is the length of the input compressed data
  101. * @param aMaxOutputSize is the size of the destination buffer (which must be
  102. * already allocated)
  103. * @param aOutputSize the actual number of bytes decoded in the destination
  104. * buffer (necessarily <= aMaxOutputSize)
  105. * @return true on success, false on failure
  106. */
  107. static MFBT_API MOZ_MUST_USE bool
  108. decompressPartial(const char* aSource, size_t aInputSize, char* aDest,
  109. size_t aMaxOutputSize, size_t* aOutputSize);
  110. /*
  111. * Provides the maximum size that LZ4 may output in a "worst case"
  112. * scenario (input data not compressible) primarily useful for memory
  113. * allocation of output buffer.
  114. * note : this function is limited by "int" range (2^31-1)
  115. *
  116. * @param aInputSize is the input size. Max supported value is ~1.9GB
  117. * @return maximum output size in a "worst case" scenario
  118. */
  119. static inline size_t maxCompressedSize(size_t aInputSize)
  120. {
  121. size_t max = (aInputSize + (aInputSize / 255) + 16);
  122. MOZ_ASSERT(max > aInputSize);
  123. return max;
  124. }
  125. };
  126. } /* namespace Compression */
  127. } /* namespace mozilla */
  128. #endif /* mozilla_Compression_h_ */