This repository has been archived by the owner on Mar 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdecoder.cpp
62 lines (59 loc) · 1.57 KB
/
decoder.cpp
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
/*
* Fade To Black engine rewrite
* Copyright (C) 2006-2012 Gregory Montoir ([email protected])
*/
#include "decoder.h"
void decodeLZSS(const uint8_t *src, uint8_t *dst, int decodedSize) {
while (decodedSize > 0) {
const int code = *src++;
for (int bit = 0; bit < 8 && decodedSize > 0; ++bit) {
if (code & (1 << bit)) {
*dst++ = *src++;
--decodedSize;
} else {
// LE16 - offset,count - bits == 4
const int offset = (src[1] << 4) | (src[0] >> 4);
int count = (src[0] & 15) + 2;
src += 2;
if (count > decodedSize) {
warning("Invalid end of stream for compressed LZSS data, size %d count %d", decodedSize, count);
count = decodedSize;
}
decodedSize -= count;
while (count-- != 0) {
*dst = *(dst - offset - 1);
++dst;
}
}
}
}
}
void decodeRAC(const uint8_t *src, uint8_t *dst, int decodedSize) {
static const int bits = 10;
while (decodedSize > 0) {
const uint8_t code = *src++;
for (int bit = 0; bit < 8 && decodedSize > 0; ++bit) {
if (code & (1 << bit)) {
*dst++ = *src++;
--decodedSize;
} else {
// LE16 - count,offset - bits == 10
int offset = READ_LE_UINT16(src); src += 2;
int count = (offset >> bits) + 2;
offset &= (1 << bits) - 1;
if (offset == 0) { // end of data marker
return;
}
if (count > decodedSize) {
warning("Invalid end of stream for compressed RAC data, size %d count %d", decodedSize, count);
count = decodedSize;
}
decodedSize -= count;
while (count-- != 0) {
*dst = *(dst - offset);
++dst;
}
}
}
}
}