blob: ade0789df5656459f5d2491d80b535dab20823c1 [file] [log] [blame]
Just6c53f882000-01-12 19:15:12 +00001"""fontTools.misc.eexec.py -- Module implementing the eexec and
2charstring encryption algorithm as used by PostScript Type 1 fonts.
3"""
4
5# Warning: Although a Python implementation is provided here,
6# all four public functions get overridden by the *much* faster
7# C extension module eexecOp, if available.
8
9import string
10
11error = "eexec.error"
12
13
14def _decryptChar(cipher, R):
15 cipher = ord(cipher)
16 plain = ( (cipher ^ (R>>8)) ) & 0xFF
17 R = ( (cipher + R) * 52845L + 22719L ) & 0xFFFF
18 return chr(plain), R
19
20def _encryptChar(plain, R):
21 plain = ord(plain)
22 cipher = ( (plain ^ (R>>8)) ) & 0xFF
23 R = ( (cipher + R) * 52845L + 22719L ) & 0xFFFF
24 return chr(cipher), R
25
26
27def decrypt(cipherstring, R):
28 # I could probably speed this up by inlining _decryptChar,
29 # but... we've got eexecOp, so who cares ;-)
30 plainList = []
31 for cipher in cipherstring:
32 plain, R = _decryptChar(cipher, R)
33 plainList.append(plain)
34 plainstring = string.join(plainList, '')
35 return plainstring, int(R)
36
37def encrypt(plainstring, R):
38 cipherList = []
39 for plain in plainstring:
40 cipher, R = _encryptChar(plain, R)
41 cipherList.append(cipher)
42 cipherstring = string.join(cipherList, '')
43 return cipherstring, int(R)
44
45
46def hexString(s):
jvrd7787132002-07-23 09:26:19 +000047 import binascii
48 return binascii.hexlify(s)
Just6c53f882000-01-12 19:15:12 +000049
50def deHexString(h):
jvrd7787132002-07-23 09:26:19 +000051 import binascii
52 h = "".join(h.split())
53 return binascii.unhexlify(h)
Just6c53f882000-01-12 19:15:12 +000054
55
56def _test():
pabs306823162009-11-08 15:57:07 +000057 import fontTools.misc.eexecOp as eexecOp
Just344757f2001-04-20 18:39:21 +000058 testStr = "\0\0asdadads asds\265"
Just6c53f882000-01-12 19:15:12 +000059 print decrypt, decrypt(testStr, 12321)
60 print eexecOp.decrypt, eexecOp.decrypt(testStr, 12321)
61 print encrypt, encrypt(testStr, 12321)
62 print eexecOp.encrypt, eexecOp.encrypt(testStr, 12321)
63
64
65if __name__ == "__main__":
66 _test()
67
68
69try:
pabs306823162009-11-08 15:57:07 +000070 from fontTools.misc.eexecOp import *
Just6c53f882000-01-12 19:15:12 +000071except ImportError:
72 pass # Use the slow Python versions
73