MCPcopy Create free account
hub / github.com/OpenDUNE/OpenDUNE / Format80_Decode

Function Format80_Decode

src/codec/format80.c:16–85  ·  view source on GitHub ↗

* Decode a memory fragment which is encoded with 'format80'. * @param dest The place the decoded fragment will be loaded. * @param source The encoded fragment. * @param destLength The length of the destination buffer. * @return The length of decoded data. */

Source from the content-addressed store, hash-verified

14 * @return The length of decoded data.
15 */
16uint16 Format80_Decode(uint8 *dest, const uint8 *source, uint16 destLength)
17{
18 uint8 *start = dest;
19 uint8 *end = dest + destLength;
20
21 while (dest != end) {
22 uint8 cmd;
23 uint16 size;
24 uint16 offset;
25
26 cmd = *source++;
27
28 if (cmd == 0x80) {
29 /* Exit */
30 break;
31
32 } else if ((cmd & 0x80) == 0) {
33 /* Short move, relative */
34 size = (cmd >> 4) + 3;
35 if (size > end - dest) size = (uint16)(end - dest);
36
37 offset = ((cmd & 0xF) << 8) + (*source++);
38
39 /* This decoder assumes memcpy. As some platforms implement memcpy as memmove, this is much safer */
40 for (; size > 0; size--) { *dest = *(dest - offset); dest++; }
41
42 } else if (cmd == 0xFE) {
43 /* Long set */
44 size = *source++;
45 size += (*source++) << 8;
46 if (size > end - dest) size = (uint16)(end - dest);
47
48 memset(dest, (*source++), size);
49 dest += size;
50
51 } else if (cmd == 0xFF) {
52 /* Long move, absolute */
53 size = *source++;
54 size += (*source++) << 8;
55 if (size > end - dest) size = (uint16)(end - dest);
56
57 offset = *source++;
58 offset += (*source++) << 8;
59
60 /* This decoder assumes memcpy. As some platforms implement memcpy as memmove, this is much safer */
61 for (; size > 0; size--) *dest++ = start[offset++];
62
63 } else if ((cmd & 0x40) != 0) {
64 /* Short move, absolute */
65 size = (cmd & 0x3F) + 3;
66 if (size > end - dest) size = (uint16)(end - dest);
67
68 offset = *source++;
69 offset += (*source++) << 8;
70
71 /* This decoder assumes memcpy. As some platforms implement memcpy as memmove, this is much safer */
72 for (; size > 0; size--) *dest++ = start[offset++];
73

Callers 6

WSA_GotoNextFrameFunction · 0.85
WSA_LoadFileFunction · 0.85
Sprites_LoadFunction · 0.85
Sprites_DecodeFunction · 0.85
Sprites_SetMouseSpriteFunction · 0.85
GUI_DrawSpriteFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected