ACCADecompressor.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Copyright (C) Teemu Suutari */
  2. #include "ACCADecompressor.hpp"
  3. #include "InputStream.hpp"
  4. #include "OutputStream.hpp"
  5. #include "common/Common.hpp"
  6. namespace ancient::internal
  7. {
  8. bool ACCADecompressor::detectHeaderXPK(uint32_t hdr) noexcept
  9. {
  10. return hdr==FourCC("ACCA");
  11. }
  12. std::shared_ptr<XPKDecompressor> ACCADecompressor::create(uint32_t hdr,uint32_t recursionLevel,const Buffer &packedData,std::shared_ptr<XPKDecompressor::State> &state,bool verify)
  13. {
  14. return std::make_shared<ACCADecompressor>(hdr,recursionLevel,packedData,state,verify);
  15. }
  16. ACCADecompressor::ACCADecompressor(uint32_t hdr,uint32_t recursionLevel,const Buffer &packedData,std::shared_ptr<XPKDecompressor::State> &state,bool verify) :
  17. XPKDecompressor(recursionLevel),
  18. _packedData(packedData)
  19. {
  20. if (!detectHeaderXPK(hdr)) throw Decompressor::InvalidFormatError();
  21. }
  22. ACCADecompressor::~ACCADecompressor()
  23. {
  24. // nothing needed
  25. }
  26. const std::string &ACCADecompressor::getSubName() const noexcept
  27. {
  28. static std::string name="XPK-ACCA: Andre's code compression algorithm";
  29. return name;
  30. }
  31. void ACCADecompressor::decompressImpl(Buffer &rawData,const Buffer &previousData,bool verify)
  32. {
  33. ForwardInputStream inputStream(_packedData,0,_packedData.size());
  34. MSBBitReader<ForwardInputStream> bitReader(inputStream);
  35. auto readBit=[&]()->uint32_t
  36. {
  37. return bitReader.readBitsBE16(1);
  38. };
  39. auto readByte=[&]()->uint8_t
  40. {
  41. return inputStream.readByte();
  42. };
  43. ForwardOutputStream outputStream(rawData,0,rawData.size());
  44. while (!outputStream.eof())
  45. {
  46. if (!readBit())
  47. {
  48. outputStream.writeByte(readByte());
  49. } else {
  50. static const uint8_t staticBytes[16]={
  51. 0x00,0x01,0x02,0x03,0x04,0x08,0x10,0x20,
  52. 0x40,0x55,0x60,0x80,0xaa,0xc0,0xe0,0xff};
  53. uint8_t tmp=readByte();
  54. uint32_t count=tmp&0xf;
  55. uint32_t code=tmp>>4;
  56. uint32_t distance=0;
  57. uint8_t repeatChar=0;
  58. bool doRLE=false;
  59. switch (code)
  60. {
  61. case 0:
  62. repeatChar=readByte();
  63. case 14:
  64. count+=3;
  65. doRLE=true;
  66. break;
  67. case 1:
  68. count=(count|(uint32_t(readByte())<<4))+19;
  69. repeatChar=readByte();
  70. doRLE=true;
  71. break;
  72. case 2:
  73. repeatChar=staticBytes[count];
  74. count=2;
  75. doRLE=true;
  76. break;
  77. case 15:
  78. distance=(count|(uint32_t(readByte())<<4))+3;
  79. count=uint32_t(readByte())+14;
  80. break;
  81. default: /* 3 to 13 */
  82. distance=(count|(uint32_t(readByte())<<4))+3;
  83. count=code;
  84. break;
  85. }
  86. if (doRLE)
  87. {
  88. for (uint32_t i=0;i<count;i++) outputStream.writeByte(repeatChar);
  89. } else {
  90. outputStream.copy(distance,count);
  91. }
  92. }
  93. }
  94. }
  95. }