diff --git a/BMGEditor/FS/Compression.cs b/BMGEditor/FS/Compression.cs index 3380da1..d09dabb 100644 --- a/BMGEditor/FS/Compression.cs +++ b/BMGEditor/FS/Compression.cs @@ -2,54 +2,43 @@ namespace BMGEditor { - public static class Yaz0 // ONLY WORKS WITH BIG ENDIAN - TO REWRITE !! + public static class Yaz0 // TODO: Finish rewriting this { - // inspired from http://www.amnoid.de/gc/yaz0.txt public static void Decompress(ref byte[] data) { if (data[0] != 'Y' || data[1] != 'a' || data[2] != 'z' || data[3] != '0') return; - int fullsize = (data[4] << 24) | (data[5] << 16) | (data[6] << 8) | data[7]; - byte[] output = new byte[fullsize]; - - int inpos = 16, outpos = 0; - while (outpos < fullsize) + Int32 decompSize = (data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7]); + byte[] output = new byte[decompSize]; + int Offs = 16; + int dstoffs = 0; + while (true) { - byte block = data[inpos++]; - + byte header = data[Offs++]; for (int i = 0; i < 8; i++) { - if ((block & 0x80) != 0) - { - // copy one plain byte - output[outpos++] = data[inpos++]; - } + if ((header & 0x80) != 0) output[dstoffs++] = data[Offs++]; else { - // copy N compressed bytes - byte b1 = data[inpos++]; - byte b2 = data[inpos++]; - - int dist = ((b1 & 0xF) << 8) | b2; - int copysrc = outpos - (dist + 1); - - int nbytes = b1 >> 4; - if (nbytes == 0) nbytes = data[inpos++] + 0x12; - else nbytes += 2; - - for (int j = 0; j < nbytes; j++) - output[outpos++] = output[copysrc++]; + byte b = data[Offs++]; + int offs = ((b & 0xF) << 8 | data[Offs++]) + 1; + int length = (b >> 4) + 2; + if (length == 2) length = data[Offs++] + 0x12; + for (int j = 0; j < length; j++) + { + output[dstoffs] = output[dstoffs - offs]; + dstoffs++; + } } - - block <<= 1; - if (outpos >= fullsize || inpos >= data.Length) - break; + if (dstoffs >= decompSize) + { + Array.Resize(ref data, decompSize); + output.CopyTo(data, 0); + } + header <<= 1; } } - - Array.Resize(ref data, fullsize); - output.CopyTo(data, 0); } } }