Compression.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Container/ArrayPtr.h"
  24. #include "../IO/Compression.h"
  25. #include "../IO/Deserializer.h"
  26. #include "../IO/Serializer.h"
  27. #include "../IO/VectorBuffer.h"
  28. #include <LZ4/lz4.h>
  29. #include <LZ4/lz4hc.h>
  30. namespace Atomic
  31. {
  32. unsigned EstimateCompressBound(unsigned srcSize)
  33. {
  34. return (unsigned)LZ4_compressBound(srcSize);
  35. }
  36. unsigned CompressData(void* dest, const void* src, unsigned srcSize)
  37. {
  38. if (!dest || !src || !srcSize)
  39. return 0;
  40. else
  41. return (unsigned)LZ4_compress_HC((const char*)src, (char*)dest, srcSize, LZ4_compressBound(srcSize), 0);
  42. }
  43. unsigned DecompressData(void* dest, const void* src, unsigned destSize)
  44. {
  45. if (!dest || !src || !destSize)
  46. return 0;
  47. else
  48. return (unsigned)LZ4_decompress_fast((const char*)src, (char*)dest, destSize);
  49. }
  50. bool CompressStream(Serializer& dest, Deserializer& src)
  51. {
  52. unsigned srcSize = src.GetSize() - src.GetPosition();
  53. // Prepend the source and dest. data size in the stream so that we know to buffer & uncompress the right amount
  54. if (!srcSize)
  55. {
  56. dest.WriteUInt(0);
  57. dest.WriteUInt(0);
  58. return true;
  59. }
  60. unsigned maxDestSize = (unsigned)LZ4_compressBound(srcSize);
  61. SharedArrayPtr<unsigned char> srcBuffer(new unsigned char[srcSize]);
  62. SharedArrayPtr<unsigned char> destBuffer(new unsigned char[maxDestSize]);
  63. if (src.Read(srcBuffer, srcSize) != srcSize)
  64. return false;
  65. unsigned destSize = (unsigned)LZ4_compress_HC((const char*)srcBuffer.Get(), (char*)destBuffer.Get(), srcSize, LZ4_compressBound(srcSize), 0);
  66. bool success = true;
  67. success &= dest.WriteUInt(srcSize);
  68. success &= dest.WriteUInt(destSize);
  69. success &= dest.Write(destBuffer, destSize) == destSize;
  70. return success;
  71. }
  72. bool DecompressStream(Serializer& dest, Deserializer& src)
  73. {
  74. if (src.IsEof())
  75. return false;
  76. unsigned destSize = src.ReadUInt();
  77. unsigned srcSize = src.ReadUInt();
  78. if (!srcSize || !destSize)
  79. return true; // No data
  80. if (srcSize > src.GetSize())
  81. return false; // Illegal source (packed data) size reported, possibly not valid data
  82. SharedArrayPtr<unsigned char> srcBuffer(new unsigned char[srcSize]);
  83. SharedArrayPtr<unsigned char> destBuffer(new unsigned char[destSize]);
  84. if (src.Read(srcBuffer, srcSize) != srcSize)
  85. return false;
  86. LZ4_decompress_fast((const char*)srcBuffer.Get(), (char*)destBuffer.Get(), destSize);
  87. return dest.Write(destBuffer, destSize) == destSize;
  88. }
  89. VectorBuffer CompressVectorBuffer(VectorBuffer& src)
  90. {
  91. VectorBuffer ret;
  92. src.Seek(0);
  93. CompressStream(ret, src);
  94. ret.Seek(0);
  95. return ret;
  96. }
  97. VectorBuffer DecompressVectorBuffer(VectorBuffer& src)
  98. {
  99. VectorBuffer ret;
  100. src.Seek(0);
  101. DecompressStream(ret, src);
  102. ret.Seek(0);
  103. return ret;
  104. }
  105. }