| 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 |
#include "expand.h" |
| 36 |
|
| 37 |
#define CARMACK_NEARTAG 0xA7 |
| 38 |
#define CARMACK_FARTAG 0xA8 |
| 39 |
|
| 40 |
static WORD ReadLittleShort(const BYTE *const ptr) |
| 41 |
{ |
| 42 |
return (WORD)((BYTE)*ptr) | ((WORD)(BYTE)(*(ptr + 1)) << 8); |
| 43 |
} |
| 44 |
static inline void WriteLittleShort(BYTE *const ptr, WORD value) |
| 45 |
{ |
| 46 |
ptr[0] = value & 0xFF; |
| 47 |
ptr[1] = (value >> 8) & 0xFF; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
void ExpandCarmack(const unsigned char *in, unsigned char *out) |
| 52 |
{ |
| 53 |
const unsigned char *const end = out + ReadLittleShort((const BYTE *)in); |
| 54 |
const unsigned char *const start = out; |
| 55 |
in += 2; |
| 56 |
|
| 57 |
const unsigned char *copy; |
| 58 |
BYTE length; |
| 59 |
while (out < end) |
| 60 |
{ |
| 61 |
length = *in++; |
| 62 |
if (length == 0 && (*in == CARMACK_NEARTAG || *in == CARMACK_FARTAG)) |
| 63 |
{ |
| 64 |
*out++ = in[1]; |
| 65 |
*out++ = in[0]; |
| 66 |
in += 2; |
| 67 |
continue; |
| 68 |
} |
| 69 |
else if (*in == CARMACK_NEARTAG) |
| 70 |
{ |
| 71 |
copy = out - (in[1] * 2); |
| 72 |
in += 2; |
| 73 |
} |
| 74 |
else if (*in == CARMACK_FARTAG) |
| 75 |
{ |
| 76 |
copy = start + (ReadLittleShort((const BYTE *)(in + 1)) * 2); |
| 77 |
in += 3; |
| 78 |
} |
| 79 |
else |
| 80 |
{ |
| 81 |
*out++ = length; |
| 82 |
*out++ = *in++; |
| 83 |
continue; |
| 84 |
} |
| 85 |
if (out + (length * 2) > end) |
| 86 |
break; |
| 87 |
while (length-- > 0) |
| 88 |
{ |
| 89 |
*out++ = *copy++; |
| 90 |
*out++ = *copy++; |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
void ExpandRLEW( |
| 97 |
const unsigned char *in, unsigned char *out, const WORD rlewTag, |
| 98 |
const bool hasFinalLength) |
| 99 |
{ |
| 100 |
DWORD length = 64 * 64 * 2; |
| 101 |
if (hasFinalLength) |
| 102 |
{ |
| 103 |
length = ReadLittleShort((const BYTE *)in); |
| 104 |
in += 2; |
| 105 |
} |
| 106 |
const unsigned char *const end = out + length; |
| 107 |
|
| 108 |
while (out < end) |
| 109 |
{ |
| 110 |
if (ReadLittleShort((const BYTE *)in) != rlewTag) |
| 111 |
{ |
| 112 |
*out++ = *in++; |
| 113 |
*out++ = *in++; |
| 114 |
} |
| 115 |
else |
| 116 |
{ |
| 117 |
WORD count = ReadLittleShort((const BYTE *)(in + 2)); |
| 118 |
WORD input = ReadLittleShort((const BYTE *)(in + 4)); |
| 119 |
in += 6; |
| 120 |
for (int i = 0; i < count; i++) |
| 121 |
{ |
| 122 |
WriteLittleShort((BYTE *)out, input); |
| 123 |
out += 2; |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
} |