Score:0

Reversing a basic encoder (code in Python)

sx flag

Moderator note: this question has moved there at reverseengineering-SE.


I'm trying to reverse the save game of an old game. It use text codes to save the games (anyone remember this?). When decoded the data contains game info like level, checkpoint, objects in inventory, etc.

I ported the code of the decoding function from assembler to Python. I now would like to produce the inverse function in order to have an encoder and to be able to forge saved games.

The problem is I don't understand the math behind the code and I don't know how to create the inverse function. I've read a lot about encoding and biwise operators but I still don't have the skill to do this.

I also would like to learn how this type of encoding is called and where to read about it. Any help would be great. :)

Here is the code:

def load_and_decode_value(text, index, table):
  current_char = ord(text[index])
  return table[current_char]

def decode(text):
  with open("DECODE_TABLE.bin", mode='rb') as file:
    DECODE_TABLE = file.read()

  decoded = []
  char_index = 0
  text_len = len(text)

  val_0 = load_and_decode_value(text, char_index, DECODE_TABLE)
  char_index += 1
  val_1 = load_and_decode_value(text, char_index, DECODE_TABLE)
  char_index += 1
  val_next_0 = (val_0 * 2048) | (val_1 * 64)
  val_1 = (val_next_0 // 256) & 0xFF
  decoded.append(val_1)

  val_2 = load_and_decode_value(text, char_index, DECODE_TABLE)
  char_index += 1
  val_3 = load_and_decode_value(text, char_index, DECODE_TABLE)
  char_index += 1
  val_next_1 = (((val_2 * 2) | val_next_0) * 16777216) | (val_3 * 1048576)
  val_3 = (val_next_1 // 16777216) & 0xFF
  decoded.append(val_3)

  val_4 = load_and_decode_value(text, char_index, DECODE_TABLE)
  char_index += 1
  val_next_2 = val_next_1 | (val_4 * 32768)
  val_4 = (val_next_2 // 65536) & 0xFF
  decoded.append(val_4)

  val_5 = load_and_decode_value(text, char_index, DECODE_TABLE)
  char_index += 1
  val_6 = load_and_decode_value(text, char_index, DECODE_TABLE)
  char_index += 1
  val_next_2 = val_next_2 | (val_5 * 1024)
  val_next_3 = val_next_2 | (val_6 * 32)
  val_6 = (val_next_3 // 256) & 0xFF
  decoded.append(val_6)

  val_7 = load_and_decode_value(text, char_index, DECODE_TABLE)
  char_index += 1
  val_8 = (val_next_3 | val_7) & 0xFF 
  decoded.append(val_8)

  return decoded

def encoded(binary):
  #todo...
  return

The content of decode table:

FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 
FF FF 00 01 02 03 04 05 06 07 08 09 FF FF FF FF FF FF FF 0A 0B 0C 0D 
0E 0F 10 11 01 12 13 01 14 15 00 16 17 18 19 1A FF 1B 1C 1D 1E 1F FF 
FF FF FF FF FF 0A 0B 0C 0D 0E 0F 10 11 01 12 13 01 14 15 00 16 17 18 
19 1A 1B FF 1C 1D 1E 1F FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 
FF FF FF                                                             
r3mainer avatar
us flag
https://reverseengineering.stackexchange.com might be more suitable for this sort of question
Maarten Bodewes avatar
in flag
@r3mainer Asked their mods on their chat channel, but the latest seen mod was 4 days ago, after which it gets worse.
fgrieu avatar
ng flag
The question is closed as off-topic since it is about encoding binary to text with 5 bits per character, with no cryptography. The table suggests [this variant of Base32](https://en.wikipedia.org/wiki/Base32#Crockford's_Base32).
0xC0000022L avatar
cn flag
@MaartenBodewes thanks for pinging me. It's odd I only saw your message now, since I was last online on SE yesterday. But whatever. Yes, I think this could be considered on-topic over at RE.SE, although I'd assume one of our folks would ask to have a look at the assembly, too.
Maarten Bodewes avatar
in flag
@0xC0000022L I migrated the question but it was then deleted leading to a 404. What happened?
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.