Compression.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // Copyright (c) 2008-2015 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 "../Container/ArrayPtr.h"
  23. #include "../IO/Compression.h"
  24. #include "../IO/Deserializer.h"
  25. #include "../IO/Serializer.h"
  26. #include "../IO/VectorBuffer.h"
  27. #include <LZ4/lz4.h>
  28. #include <LZ4/lz4hc.h>
  29. namespace Urho3D
  30. {
  31. unsigned EstimateCompressBound(unsigned srcSize)
  32. {
  33. return LZ4_compressBound(srcSize);
  34. }
  35. unsigned CompressData(void* dest, const void* src, unsigned srcSize)
  36. {
  37. if (!dest || !src || !srcSize)
  38. return 0;
  39. else
  40. return LZ4_compressHC((const char*)src, (char*)dest, srcSize);
  41. }
  42. unsigned DecompressData(void* dest, const void* src, unsigned destSize)
  43. {
  44. if (!dest || !src || !destSize)
  45. return 0;
  46. else
  47. return LZ4_decompress_fast((const char*)src, (char*)dest, destSize);
  48. }
  49. bool CompressStream(Serializer& dest, Deserializer& src)
  50. {
  51. unsigned srcSize = src.GetSize() - src.GetPosition();
  52. // Prepend the source and dest. data size in the stream so that we know to buffer & uncompress the right amount
  53. if (!srcSize)
  54. {
  55. dest.WriteUInt(0);
  56. dest.WriteUInt(0);
  57. return true;
  58. }
  59. unsigned maxDestSize = LZ4_compressBound(srcSize);
  60. SharedArrayPtr<unsigned char> srcBuffer(new unsigned char[srcSize]);
  61. SharedArrayPtr<unsigned char> destBuffer(new unsigned char[maxDestSize]);
  62. if (src.Read(srcBuffer, srcSize) != srcSize)
  63. return false;
  64. unsigned destSize = LZ4_compressHC((const char*)srcBuffer.Get(), (char*)destBuffer.Get(), srcSize);
  65. bool success = true;
  66. success &= dest.WriteUInt(srcSize);
  67. success &= dest.WriteUInt(destSize);
  68. success &= dest.Write(destBuffer, destSize) == destSize;
  69. return success;
  70. }
  71. bool DecompressStream(Serializer& dest, Deserializer& src)
  72. {
  73. if (src.IsEof())
  74. return false;
  75. unsigned destSize = src.ReadUInt();
  76. unsigned srcSize = src.ReadUInt();
  77. if (!srcSize || !destSize)
  78. return true; // No data
  79. if (srcSize > src.GetSize())
  80. return false; // Illegal source (packed data) size reported, possibly not valid data
  81. SharedArrayPtr<unsigned char> srcBuffer(new unsigned char[srcSize]);
  82. SharedArrayPtr<unsigned char> destBuffer(new unsigned char[destSize]);
  83. if (src.Read(srcBuffer, srcSize) != srcSize)
  84. return false;
  85. LZ4_decompress_fast((const char*)srcBuffer.Get(), (char*)destBuffer.Get(), destSize);
  86. return dest.Write(destBuffer, destSize) == destSize;
  87. }
  88. VectorBuffer CompressVectorBuffer(VectorBuffer& src)
  89. {
  90. VectorBuffer ret;
  91. src.Seek(0);
  92. CompressStream(ret, src);
  93. ret.Seek(0);
  94. return ret;
  95. }
  96. VectorBuffer DecompressVectorBuffer(VectorBuffer& src)
  97. {
  98. VectorBuffer ret;
  99. src.Seek(0);
  100. DecompressStream(ret, src);
  101. ret.Seek(0);
  102. return ret;
  103. }
  104. }