Created
June 2, 2017 11:49
-
-
Save darfink/71013a083da173a56e30c0e3d2a7400d to your computer and use it in GitHub Desktop.
Decompress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
u32 rle_decompress(u8** out, const u8* src) { | |
// Extract the size of the data | |
u32 originalSize = *((u32*) src) >> 8; | |
u8* originalDst = calloc(originalSize, 1); | |
// Advance past the header | |
src += sizeof(u32); | |
u8* dst = originalDst; | |
for(u32 size = originalSize; size > 0;) { | |
u8 byte = *src++; | |
// Check if it's a compressed block | |
if (byte & 0x80) { | |
// Read the length of the block | |
int len = (byte & 0x7F) + 3; | |
size -= len; | |
// Read in the byte used for the run | |
byte = *src++; | |
memset(dst, byte, len); | |
dst += len; | |
} else /* Uncompressed block */ { | |
// Read the length of uncompressed bytes | |
int len = (byte & 0x7F) + 1; | |
size -= len; | |
memcpy(dst, src, len); | |
dst += len; | |
src += len; | |
} | |
} | |
*out = originalDst; | |
return originalSize; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment