forked from czajowaty/ad_resources_dumper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdResourceUnpacker.cpp
More file actions
110 lines (98 loc) · 3.06 KB
/
Copy pathAdResourceUnpacker.cpp
File metadata and controls
110 lines (98 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "AdResourceUnpacker.hpp"
AdResourceUnpacker::AdResourceUnpacker(uint8_t const* buffer, uint32_t size)
: inBuffer_{buffer},
inBufferSize_{size},
inBufferEnd_{inBuffer_ + inBufferSize_}
{}
void AdResourceUnpacker::setOutBufferSize(uint32_t outBufferSize)
{ outBufferSize_ = outBufferSize; }
QByteArray AdResourceUnpacker::unpack()
{
resetInternalVariables();
while (true)
{
while (!readControlBoolFlag())
{ writeByte(readByte()); }
uint16_t bytesToDuplicateNumber;
uint16_t startDuplicationFromOffset;
if (readControlBoolFlag())
{
bytesToDuplicateNumber = readTwoControlBits() + 2;
startDuplicationFromOffset = readByte();
if (startDuplicationFromOffset == 0)
{ startDuplicationFromOffset = 0x100; }
}
else
{
uint16_t nextTwoBytes = readTwoBytes();
if (nextTwoBytes == 0)
{ break; }
bytesToDuplicateNumber = (nextTwoBytes & 0xf);
if (bytesToDuplicateNumber == 0)
{ bytesToDuplicateNumber = readByte() + 1; }
else
{ bytesToDuplicateNumber += 2; }
startDuplicationFromOffset = nextTwoBytes >> 4;
}
duplicateWrittenBytes(
startDuplicationFromOffset,
bytesToDuplicateNumber);
}
resizeBufferToWrittenData();
return outBuffer_;
}
void AdResourceUnpacker::resetInternalVariables()
{
controlByteBitIndex_ = BITS_IN_BYTE;
inPtr_ = inBuffer_;
outBuffer_.resize(outBufferSize_);
outPtr_ = reinterpret_cast<uint8_t*>(outBuffer_.data());
outBufferEnd_ = outPtr_ + outBuffer_.size();
}
void AdResourceUnpacker::duplicateWrittenBytes(
uint16_t offset,
uint16_t bytesNumber)
{
auto const* src = outPtr_ - offset;
if (src < reinterpret_cast<decltype(src)>(outBuffer_.data()))
{ throw QString("Trying to read beyond output buffer."); }
for (uint16_t i = 0; i < bytesNumber; ++i, ++src)
{ writeByte(*src); }
}
void AdResourceUnpacker::resizeBufferToWrittenData()
{
auto outBufferSize =
outPtr_ - reinterpret_cast<uint8_t const*>(outBuffer_.data());
outBuffer_.resize(outBufferSize);
}
uint8_t AdResourceUnpacker::readByte()
{
if (inPtr_ >= inBufferEnd_)
{ throw QString("Trying to read beyond input buffer."); }
uint8_t nextByte = *inPtr_;
++inPtr_;
return nextByte;
}
uint16_t AdResourceUnpacker::readTwoBytes()
{
uint16_t nextTwoBytes = readByte();
nextTwoBytes <<= 8;
nextTwoBytes |= readByte();
return nextTwoBytes;
}
uint8_t AdResourceUnpacker::readControlBit()
{
if (controlByteBitIndex_ >= BITS_IN_BYTE)
{
controlByte_ = readByte();
controlByteBitIndex_ = 0;
}
uint8_t controlBit = controlByte_ & 1;
controlByte_ >>= 1;
++controlByteBitIndex_;
return controlBit;
}
uint8_t AdResourceUnpacker::readTwoControlBits()
{ return (readControlBit() << 1) | readControlBit(); }
bool AdResourceUnpacker::readControlBoolFlag()
{ return readControlBit() != 0; }