SDHCDecompressor.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Copyright (C) Teemu Suutari */
  2. #include <cstring>
  3. #include "common/SubBuffer.hpp"
  4. #include "SDHCDecompressor.hpp"
  5. #include "XPKMain.hpp"
  6. #include "DLTADecode.hpp"
  7. #include "common/Common.hpp"
  8. namespace ancient::internal
  9. {
  10. bool SDHCDecompressor::detectHeaderXPK(uint32_t hdr) noexcept
  11. {
  12. return hdr==FourCC("SDHC");
  13. }
  14. std::shared_ptr<XPKDecompressor> SDHCDecompressor::create(uint32_t hdr,uint32_t recursionLevel,const Buffer &packedData,std::shared_ptr<XPKDecompressor::State> &state,bool verify)
  15. {
  16. return std::make_shared<SDHCDecompressor>(hdr,recursionLevel,packedData,state,verify);
  17. }
  18. SDHCDecompressor::SDHCDecompressor(uint32_t hdr,uint32_t recursionLevel,const Buffer &packedData,std::shared_ptr<XPKDecompressor::State> &state,bool verify) :
  19. XPKDecompressor(recursionLevel),
  20. _packedData(packedData)
  21. {
  22. if (!detectHeaderXPK(hdr) || _packedData.size()<2)
  23. throw Decompressor::InvalidFormatError();
  24. _mode=_packedData.readBE16(0);
  25. if (verify && (_mode&0x8000U))
  26. {
  27. ConstSubBuffer src(_packedData,2,_packedData.size()-2);
  28. XPKMain main(src,_recursionLevel+1,true);
  29. }
  30. }
  31. SDHCDecompressor::~SDHCDecompressor()
  32. {
  33. // nothing needed
  34. }
  35. const std::string &SDHCDecompressor::getSubName() const noexcept
  36. {
  37. static std::string name="XPK-SDHC: Sample delta huffman compressor";
  38. return name;
  39. }
  40. void SDHCDecompressor::decompressImpl(Buffer &rawData,const Buffer &previousData,bool verify)
  41. {
  42. ConstSubBuffer src(_packedData,2,_packedData.size()-2);
  43. if (_mode&0x8000U)
  44. {
  45. XPKMain main(src,_recursionLevel+1,false);
  46. main.decompress(rawData,verify);
  47. } else {
  48. if (src.size()!=rawData.size()) throw Decompressor::DecompressionError();
  49. std::memcpy(rawData.data(),src.data(),src.size());
  50. }
  51. size_t length=rawData.size()&~3U;
  52. auto deltaDecodeMono=[&]()
  53. {
  54. uint8_t *buf=rawData.data();
  55. uint16_t ctr=0;
  56. for (size_t i=0;i<length;i+=2)
  57. {
  58. uint16_t tmp;
  59. tmp=(uint16_t(buf[i])<<8)|uint16_t(buf[i+1]);
  60. ctr+=tmp;
  61. buf[i]=ctr>>8;
  62. buf[i+1]=ctr&0xff;
  63. }
  64. };
  65. auto deltaDecodeStereo=[&]()
  66. {
  67. uint8_t *buf=rawData.data();
  68. uint16_t ctr1=0,ctr2=0;
  69. for (size_t i=0;i<length;i+=4)
  70. {
  71. uint16_t tmp;
  72. tmp=(uint16_t(buf[i])<<8)|uint16_t(buf[i+1]);
  73. ctr1+=tmp;
  74. tmp=(uint16_t(buf[i+2])<<8)|uint16_t(buf[i+3]);
  75. ctr2+=tmp;
  76. buf[i]=ctr1>>8;
  77. buf[i+1]=ctr1&0xff;
  78. buf[i+2]=ctr2>>8;
  79. buf[i+3]=ctr2&0xff;
  80. }
  81. };
  82. switch (_mode&15)
  83. {
  84. case 1:
  85. DLTADecode::decode(rawData,rawData,0,length);
  86. // intentional fall through
  87. case 0:
  88. DLTADecode::decode(rawData,rawData,0,length);
  89. break;
  90. case 3:
  91. deltaDecodeMono();
  92. // intentional fall through
  93. case 2:
  94. deltaDecodeMono();
  95. break;
  96. case 11:
  97. deltaDecodeStereo();
  98. // intentional fall through
  99. case 10:
  100. deltaDecodeStereo();
  101. break;
  102. default:
  103. throw Decompressor::DecompressionError();
  104. }
  105. }
  106. }