Compression.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #include "mozilla/Compression.h"
  6. #include "mozilla/CheckedInt.h"
  7. // Without including <string>, MSVC 2015 complains about e.g. the impossibility
  8. // to convert `const void* const` to `void*` when calling memchr from
  9. // corecrt_memory.h.
  10. #include <string>
  11. using namespace mozilla::Compression;
  12. namespace {
  13. extern "C" {
  14. #include "lz4.c"
  15. }
  16. }/* anonymous namespace */
  17. /* Our wrappers */
  18. size_t
  19. LZ4::compress(const char* aSource, size_t aInputSize, char* aDest)
  20. {
  21. CheckedInt<int> inputSizeChecked = aInputSize;
  22. MOZ_ASSERT(inputSizeChecked.isValid());
  23. return LZ4_compress_default(aSource, aDest, inputSizeChecked.value(),
  24. LZ4_compressBound(inputSizeChecked.value()));
  25. }
  26. size_t
  27. LZ4::compressLimitedOutput(const char* aSource, size_t aInputSize, char* aDest,
  28. size_t aMaxOutputSize)
  29. {
  30. CheckedInt<int> inputSizeChecked = aInputSize;
  31. MOZ_ASSERT(inputSizeChecked.isValid());
  32. CheckedInt<int> maxOutputSizeChecked = aMaxOutputSize;
  33. MOZ_ASSERT(maxOutputSizeChecked.isValid());
  34. return LZ4_compress_default(aSource, aDest, inputSizeChecked.value(),
  35. maxOutputSizeChecked.value());
  36. }
  37. bool
  38. LZ4::decompress(const char* aSource, char* aDest, size_t aOutputSize)
  39. {
  40. CheckedInt<int> outputSizeChecked = aOutputSize;
  41. MOZ_ASSERT(outputSizeChecked.isValid());
  42. int ret = LZ4_decompress_fast(aSource, aDest, outputSizeChecked.value());
  43. return ret >= 0;
  44. }
  45. bool
  46. LZ4::decompress(const char* aSource, size_t aInputSize, char* aDest,
  47. size_t aMaxOutputSize, size_t* aOutputSize)
  48. {
  49. CheckedInt<int> maxOutputSizeChecked = aMaxOutputSize;
  50. MOZ_ASSERT(maxOutputSizeChecked.isValid());
  51. CheckedInt<int> inputSizeChecked = aInputSize;
  52. MOZ_ASSERT(inputSizeChecked.isValid());
  53. int ret = LZ4_decompress_safe(aSource, aDest, inputSizeChecked.value(),
  54. maxOutputSizeChecked.value());
  55. if (ret >= 0) {
  56. *aOutputSize = ret;
  57. return true;
  58. }
  59. *aOutputSize = 0;
  60. return false;
  61. }
  62. bool
  63. LZ4::decompressPartial(const char* aSource, size_t aInputSize, char* aDest,
  64. size_t aMaxOutputSize, size_t* aOutputSize)
  65. {
  66. CheckedInt<int> maxOutputSizeChecked = aMaxOutputSize;
  67. MOZ_ASSERT(maxOutputSizeChecked.isValid());
  68. CheckedInt<int> inputSizeChecked = aInputSize;
  69. MOZ_ASSERT(inputSizeChecked.isValid());
  70. int ret = LZ4_decompress_safe_partial(aSource, aDest,
  71. inputSizeChecked.value(),
  72. maxOutputSizeChecked.value(),
  73. maxOutputSizeChecked.value());
  74. if (ret >= 0) {
  75. *aOutputSize = ret;
  76. return true;
  77. }
  78. *aOutputSize = 0;
  79. return false;
  80. }