papermario/tools/splat/util/n64/Yay0decompress.c
pixel-stuck e61e67ea9b
[WIP] Work on npc.c (#295)
* formatting

* format + misc work

* lots of work on npc.c

* PR comments, asm delet + other misc fixes

* missed one

* temporarily remove cleanup step for testing

* test fix for splat

* test

* o2

* o0

* fix Yay0decompress.c bug

* debug logging

* stuff

* stuff2

* restore stuff

* more PR comments and some general cleanup

* NPC flags + improve match + misc

Co-authored-by: Ethan Roseman <ethteck@gmail.com>
2021-06-15 17:05:16 +09:00

45 lines
1.1 KiB
C

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
typedef struct {
uint32_t magic;
uint32_t uncompressedLength;
uint32_t opPtr;
uint32_t dataPtr;
} Yay0Header;
void decompress(Yay0Header* hdr, uint8_t* srcPtr, uint8_t* dstPtr, bool isBigEndian) {
uint8_t byte = 0, mask = 0;
uint8_t* ctrl, * ops, * data;
uint16_t copy, op;
uint32_t written = 0;
ctrl = srcPtr + sizeof(Yay0Header);
ops = srcPtr + hdr->opPtr;
data = srcPtr + hdr->dataPtr;
while (written < hdr->uncompressedLength) {
if ((mask >>= 1) == 0) {
byte = *ctrl++;
mask = 0x80;
}
if (byte & mask) {
*dstPtr++ = *data++;
written++;
} else {
op = isBigEndian ? (ops[0] << 8) | ops[1] : (ops[1] << 8) | ops[0];
ops += 2;
written += copy = (op >> 12) ? (2 + (op >> 12)) : (18 + *data++);
while (copy--) {
*dstPtr = dstPtr[-(op & 0xfff) - 1];
dstPtr++;
}
}
}
}