Search code examples
pythondecoderpanda3d

Custom Decode Script: EOL while scanning string literal


i made a script to decode a file made a few years ago, and I've run into an issue whilst doing my second decode test.

My code:

#!/usr/bin/python
# -*- coding: utf-8 -*-
from decoder.encodings import *

#Toontown Online Encoded Script Decoder

"""
##########################################
# Decoder was built to decompile         #
# Team Pawz Multihack v2.0               #
##########################################
"""
input = "Text can be located here: http://pastebin.com/rdeAhyar ";
def decode():
    print input.decode('latin_1')
decode()

When i execute the code i get

SyntaxError: EOL while scanning string literal

SyntaxError: EOL while scanning string literal

Press any key to continue . . .

If this helps i'm using the version of Python distributed within Panda3D.


Solution

  • The problem is embedding binary data in source code by simply pasting it. The error appears on Windows because Windows sees a byte value of 26 (hex 1A) as the end of text files and stops reading text files right before this byte value. Linux is not affected by this, that's the reason I was unable to reproduce the problem.

    Observe the difference in file size and the amount of bytes a ”full” read() returns under Windows:

    >>> os.path.getsize('test.py')
    49297L
    >>> len(open('test.py', 'r').read()) # text mode
    1100
    >>> len(open('test.py', 'rb').read()) # binary mode
    49297
    

    The solution is not to embed the binary data in the source code but load it from an extra file. Make sure to open it in binary mode instead of text mode.

    Or you have to encode the binary data so it doesn't contain ”exotic” byte values any more. Base64 encoding is a good candidate for this.