Python Forum

Full Version: zlib decompress error: invalid code lengths set / invalid block type
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
(this is sort of about python, but it might get slightly off topic. I couldn't find anywhere else it would make sense to post this, and since I'm coding it in python, I though I might as well post it here)

Slight backstory:
For many years, I have had an issue with one of my games where I am not able to save any of my progress. I have done much research but nothing has seemed to have worked, so I though I'd take matters into my own hands and try and fix it myself.

Here's a snippet of what the save file looks like inside:
ÃÂFqÄQ˛˘fl≥-ˆ&N@Öë]ù!+—Ïá$ë 2©MløëΑ*Œ∞áõìÔœÌ Ä∏‡Ì™:y"2≠¥7}˘Ê¥∞Æ;â"ºGªh˛∑¸

Now, according to the hours of research I have done, the way this save file (and apparently all others) is encrypted is by:
gzip encode -> base64 encode -> XOR^11 (I believe)
Meaning to decode the file, you have to work backwards:
XOR^11 -> base64 decode -> gzip decode
Simple right? I wish.

Here's the code I have created to do that:
a = open("/Users/everyone/Desktop/Save.dat", "rb").read()
newchars = []
for i in a:
    newchars.append(i^11)
saveData = bytearray(newchars).decode("utf-8", "ignore")

b64 = base64.b64decode(b"H4sIAAAAAAAAC9y"+str(saveData).replace("-","+").replace("_", "/").encode())

print(b64)

#print( b := bytes(map(lambda x: x ^ 11, b64)))
print(zlib.decompress(b64, -zlib.MAX_WBITS))
The issue is with the gzip decoding:
Error:
Traceback (most recent call last): File "/Users/everyone/Desktop/test.py", line 39, in <module> print(zlib.decompress(b64, -zlib.MAX_WBITS)) zlib.error: Error -3 while decompressing data: invalid block type [Finished in 0.2s with exit code 1]
I'm not the first person to try and decrypt these game saves

The issue I've got is that.. the same error comes up using others' game save decryptors. I also have use .decode("utf-8", "ignore"), which ll the other ones do not, otherwise I get invalid unicode character errors. I also have to append the gzip-ed, base64 encoded header to open the file, not just to save it like the one on GitHub does.

If we take a look a snippet my Windows using friend's game save (I'm using Mac OS X):
?xBJJJJJJJJH&r2X<[:~ANR&Mhh}i=S|}xY@{h8x>}M<LjyzMQS9LSA]do@8_NTm>IJl~ZcZXXZAG&||
Well that's very different - and a lot more correct, since this doesn't error when put into the game save decryptor.
This lead me down the path that my game save was very corrupted, however, if that was the case, then the game shouldn't be able to read it either. I tested that by putting my friends game save into my save folder to see if I would load his progress... and I didn't which clearly means that my save file can't be corrupt, and that the save files differ between platforms.

Remember the encryption method used? Well that's also apparently the built in cocos2d-x game engine encryption method, which is used in the game, meaning the save files should not differ between platforms.

Anyway, away from that bit off-topic information. When I run my python script, before the error, I print the base64 encoded data:
Output:
b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x0b\xdc\x8c\xcd\x9f\x84)_\xfd\x16\x0f\xb5\xaf\xdb\xf4-\xcc\x7f4l\xea\xbf\xe2\xd3\xbbs\xfa/\xe9\x82\x97\x8e\x1b\x14\r\xc2\xd6\xf6\x9d\xe6\xe1\x90U\xc1Jq!)\xd2\xc0\xd4\x1c\xf8\x9d3\x14\xc2ed`kR\xf16n\x83W_KlP7{\xe5\xd9F\x1d\x95\xc0\xfat\xb05\x10\xa81\xc4:\xd5O\xc7`!X\xfc\x8e~\x13\xd2\xa8G\xce\xa3\x8e\xef\xa9\xf8\xcd\x03>'
\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x0b\xdc\x8c
Something along those lines is the header for a gzip file.

According to that website, if the FLG byte is 0 (which it is for me, or at least with the header I appended), the data type is FTEXT which is "probably ASCII text", but the text inside of my file is definitely not ASCII.

So finally, my question is, why can I not unzip this data? (is it even gzip format?)

Feel free to clarify anything, it's a bit confusing.

One last edit: I forgot to mention, when the files is decrypted, the output should be a plain text XML file.