TPWMDecompressor.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* Copyright (C) Teemu Suutari */
  2. #include "TPWMDecompressor.hpp"
  3. #include "InputStream.hpp"
  4. #include "OutputStream.hpp"
  5. #include "common/Common.hpp"
  6. namespace ancient::internal
  7. {
  8. bool TPWMDecompressor::detectHeader(uint32_t hdr) noexcept
  9. {
  10. return hdr==FourCC("TPWM");
  11. }
  12. std::shared_ptr<Decompressor> TPWMDecompressor::create(const Buffer &packedData,bool exactSizeKnown,bool verify)
  13. {
  14. return std::make_shared<TPWMDecompressor>(packedData,verify);
  15. }
  16. TPWMDecompressor::TPWMDecompressor(const Buffer &packedData,bool verify) :
  17. _packedData(packedData)
  18. {
  19. uint32_t hdr=packedData.readBE32(0);
  20. if (!detectHeader(hdr) || packedData.size()<12) throw InvalidFormatError();
  21. _rawSize=packedData.readBE32(4);
  22. if (!_rawSize || _rawSize>getMaxRawSize())
  23. throw InvalidFormatError();
  24. }
  25. TPWMDecompressor::~TPWMDecompressor()
  26. {
  27. // nothing needed
  28. }
  29. const std::string &TPWMDecompressor::getName() const noexcept
  30. {
  31. static std::string name="TPWM: Turbo Packer";
  32. return name;
  33. }
  34. size_t TPWMDecompressor::getPackedSize() const noexcept
  35. {
  36. // No packed size in the stream :(
  37. // After decompression, we can tell how many bytes were actually used
  38. return _decompressedPackedSize;
  39. }
  40. size_t TPWMDecompressor::getRawSize() const noexcept
  41. {
  42. return _rawSize;
  43. }
  44. void TPWMDecompressor::decompressImpl(Buffer &rawData,bool verify)
  45. {
  46. if (rawData.size()<_rawSize) throw DecompressionError();
  47. ForwardInputStream inputStream(_packedData,8,_packedData.size());
  48. MSBBitReader<ForwardInputStream> bitReader(inputStream);
  49. auto readBit=[&]()->uint32_t
  50. {
  51. return bitReader.readBits8(1);
  52. };
  53. auto readByte=[&]()->uint8_t
  54. {
  55. return inputStream.readByte();
  56. };
  57. ForwardOutputStream outputStream(rawData,0,_rawSize);
  58. while (!outputStream.eof())
  59. {
  60. if (readBit())
  61. {
  62. uint8_t byte1=readByte();
  63. uint8_t byte2=readByte();
  64. uint32_t distance=(uint32_t(byte1&0xf0)<<4)|byte2;
  65. uint32_t count=uint32_t(byte1&0xf)+3;
  66. count=std::min(count,uint32_t(_rawSize-outputStream.getOffset()));
  67. outputStream.copy(distance,count);
  68. } else {
  69. outputStream.writeByte(readByte());
  70. }
  71. }
  72. _decompressedPackedSize=inputStream.getOffset();
  73. }
  74. }