AudioChunk.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "AudioChunk.h"
  2. std::vector<uint8_t> audioAESIV({0x72, 0xe0, 0x67, 0xfb, 0xdd, 0xcb, 0xcf, 0x77, 0xeb, 0xe8, 0xbc, 0x64, 0x3f, 0x63, 0x0d, 0x93});
  3. AudioChunk::AudioChunk(uint16_t seqId, std::vector<uint8_t> &audioKey, uint32_t startPosition, uint32_t predictedEndPosition)
  4. {
  5. this->crypto = std::make_unique<Crypto>();
  6. this->seqId = seqId;
  7. this->audioKey = audioKey;
  8. this->startPosition = startPosition;
  9. this->endPosition = predictedEndPosition;
  10. this->decryptedData = std::vector<uint8_t>();
  11. this->isHeaderFileSizeLoadedSemaphore = std::make_unique<WrappedSemaphore>(5);
  12. this->isLoadedSemaphore = std::make_unique<WrappedSemaphore>(5);
  13. }
  14. AudioChunk::~AudioChunk()
  15. {
  16. }
  17. void AudioChunk::appendData(const std::vector<uint8_t> &data)
  18. {
  19. //if (this == nullptr) return;
  20. this->decryptedData.insert(this->decryptedData.end(), data.begin(), data.end());
  21. }
  22. void AudioChunk::readData(uint8_t *target, size_t offset, size_t nbytes) {
  23. auto readPos = offset + nbytes;
  24. auto modulo = (readPos % 16);
  25. auto ivReadPos = readPos;
  26. if (modulo != 0) {
  27. ivReadPos += (16 - modulo);
  28. }
  29. if (ivReadPos > decryptedCount) {
  30. // calculate the IV for right position
  31. auto calculatedIV = this->getIVSum((oldStartPos + decryptedCount) / 16);
  32. crypto->aesCTRXcrypt(this->audioKey, calculatedIV, decryptedData.data() + decryptedCount, ivReadPos - decryptedCount);
  33. decryptedCount = ivReadPos;
  34. }
  35. memcpy(target, this->decryptedData.data() + offset, nbytes);
  36. }
  37. void AudioChunk::finalize()
  38. {
  39. this->oldStartPos = this->startPosition;
  40. this->startPosition = this->endPosition - this->decryptedData.size();
  41. this->isLoaded = true;
  42. }
  43. // Basically just big num addition
  44. std::vector<uint8_t> AudioChunk::getIVSum(uint32_t n)
  45. {
  46. return bigNumAdd(audioAESIV, n);
  47. }