Compression.h 4.3 KB

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