Python Forum
zlib decompress error: invalid code lengths set / invalid block type
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
zlib decompress error: invalid code lengths set / invalid block type
#1
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using OpenCV and image path is invalid AudunNilsen 5 545 Mar-18-2024, 05:28 PM
Last Post: snippsat
  Wrong type error rowan_bradley 6 1,212 Aug-07-2023, 10:44 AM
Last Post: rowan_bradley
  error: invalid command 'egg_info' TimTu 0 949 Jul-27-2023, 07:30 AM
Last Post: TimTu
  Type Error: Unsupported Operand jhancock 2 1,169 Jul-22-2023, 11:33 PM
Last Post: jhancock
  Invalid argument: 'images\x08ackground.jpg' CatBall 4 955 Jun-19-2023, 09:28 AM
Last Post: CatBall
  print(data) is suddenly invalid syntax db042190 6 1,182 Jun-14-2023, 02:55 PM
Last Post: deanhystad
  UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd2 in position 16: invalid cont Melcu54 3 4,915 Mar-26-2023, 12:12 PM
Last Post: Gribouillis
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 10,960 Dec-26-2022, 08:48 AM
Last Post: ibreeden
  python multiple try except block in my code -- can we shorten code mg24 10 6,087 Nov-10-2022, 12:48 PM
Last Post: DeaD_EyE
  WARNING: Ignoring invalid distribution kucingkembar 1 24,490 Sep-02-2022, 06:49 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020