16c53f88cf3deb380be299bcfa0ca3f88890c624bJust"""fontTools.misc.eexec.py -- Module implementing the eexec and
26c53f88cf3deb380be299bcfa0ca3f88890c624bJustcharstring encryption algorithm as used by PostScript Type 1 fonts.
36c53f88cf3deb380be299bcfa0ca3f88890c624bJust"""
46c53f88cf3deb380be299bcfa0ca3f88890c624bJust
51ae29591efbb29492ce05378909ccf4028d7c1eeBehdad Esfahbodfrom __future__ import print_function, division, absolute_import
630e691edd056ba22fa8970280e986747817bec3dBehdad Esfahbodfrom fontTools.misc.py23 import *
77ed91eca1eaa96b79eae780778e89bb9ec44c1eeBehdad Esfahbod
86c53f88cf3deb380be299bcfa0ca3f88890c624bJustdef _decryptChar(cipher, R):
9319c5fd10e2ea84304bd299b7483e05b5b0d5480Behdad Esfahbod	cipher = byteord(cipher)
106c53f88cf3deb380be299bcfa0ca3f88890c624bJust	plain = ( (cipher ^ (R>>8)) ) & 0xFF
11ecbe4c87b5b4b57ee965c3f9c0302cb4044a6875Behdad Esfahbod	R = ( (cipher + R) * 52845 + 22719 ) & 0xFFFF
12b7a2d797a40fb658d1e6dca6c08c9d2e1d83e78aBehdad Esfahbod	return bytechr(plain), R
136c53f88cf3deb380be299bcfa0ca3f88890c624bJust
146c53f88cf3deb380be299bcfa0ca3f88890c624bJustdef _encryptChar(plain, R):
15319c5fd10e2ea84304bd299b7483e05b5b0d5480Behdad Esfahbod	plain = byteord(plain)
166c53f88cf3deb380be299bcfa0ca3f88890c624bJust	cipher = ( (plain ^ (R>>8)) ) & 0xFF
17ecbe4c87b5b4b57ee965c3f9c0302cb4044a6875Behdad Esfahbod	R = ( (cipher + R) * 52845 + 22719 ) & 0xFFFF
18b7a2d797a40fb658d1e6dca6c08c9d2e1d83e78aBehdad Esfahbod	return bytechr(cipher), R
196c53f88cf3deb380be299bcfa0ca3f88890c624bJust
206c53f88cf3deb380be299bcfa0ca3f88890c624bJust
216c53f88cf3deb380be299bcfa0ca3f88890c624bJustdef decrypt(cipherstring, R):
226c53f88cf3deb380be299bcfa0ca3f88890c624bJust	plainList = []
236c53f88cf3deb380be299bcfa0ca3f88890c624bJust	for cipher in cipherstring:
246c53f88cf3deb380be299bcfa0ca3f88890c624bJust		plain, R = _decryptChar(cipher, R)
256c53f88cf3deb380be299bcfa0ca3f88890c624bJust		plainList.append(plain)
2618316aa769566eeb6f3f4a6ed2685fa8f8e861c2Behdad Esfahbod	plainstring = strjoin(plainList)
276c53f88cf3deb380be299bcfa0ca3f88890c624bJust	return plainstring, int(R)
286c53f88cf3deb380be299bcfa0ca3f88890c624bJust
296c53f88cf3deb380be299bcfa0ca3f88890c624bJustdef encrypt(plainstring, R):
306c53f88cf3deb380be299bcfa0ca3f88890c624bJust	cipherList = []
316c53f88cf3deb380be299bcfa0ca3f88890c624bJust	for plain in plainstring:
326c53f88cf3deb380be299bcfa0ca3f88890c624bJust		cipher, R = _encryptChar(plain, R)
336c53f88cf3deb380be299bcfa0ca3f88890c624bJust		cipherList.append(cipher)
3418316aa769566eeb6f3f4a6ed2685fa8f8e861c2Behdad Esfahbod	cipherstring = strjoin(cipherList)
356c53f88cf3deb380be299bcfa0ca3f88890c624bJust	return cipherstring, int(R)
366c53f88cf3deb380be299bcfa0ca3f88890c624bJust
376c53f88cf3deb380be299bcfa0ca3f88890c624bJust
386c53f88cf3deb380be299bcfa0ca3f88890c624bJustdef hexString(s):
39d7787131e42826863d468ee6ebf049a87fa44fe6jvr	import binascii
40d7787131e42826863d468ee6ebf049a87fa44fe6jvr	return binascii.hexlify(s)
416c53f88cf3deb380be299bcfa0ca3f88890c624bJust
426c53f88cf3deb380be299bcfa0ca3f88890c624bJustdef deHexString(h):
43d7787131e42826863d468ee6ebf049a87fa44fe6jvr	import binascii
4418316aa769566eeb6f3f4a6ed2685fa8f8e861c2Behdad Esfahbod	h = strjoin(h.split())
45d7787131e42826863d468ee6ebf049a87fa44fe6jvr	return binascii.unhexlify(h)
466c53f88cf3deb380be299bcfa0ca3f88890c624bJust
476c53f88cf3deb380be299bcfa0ca3f88890c624bJust
486c53f88cf3deb380be299bcfa0ca3f88890c624bJustdef _test():
49344757f42d274f70a63d12bb4f45ed2fdb7eb3c6Just	testStr = "\0\0asdadads asds\265"
503ec6a258238b6068e4eef3fe579f1f5c0a06bbbaBehdad Esfahbod	print(decrypt, decrypt(testStr, 12321))
513ec6a258238b6068e4eef3fe579f1f5c0a06bbbaBehdad Esfahbod	print(encrypt, encrypt(testStr, 12321))
526c53f88cf3deb380be299bcfa0ca3f88890c624bJust
536c53f88cf3deb380be299bcfa0ca3f88890c624bJust
546c53f88cf3deb380be299bcfa0ca3f88890c624bJustif __name__ == "__main__":
556c53f88cf3deb380be299bcfa0ca3f88890c624bJust	_test()
56