Skip to content

Commit ef453fa

Browse files
committed
Refine protection code
1 parent 999214a commit ef453fa

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

src/protect_code2.pt

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,24 @@ def protect_pytransform():
1212
raise RuntimeError('%s() is not a builtin' % func.__name__)
1313

1414
def check_obfuscated_script():
15+
from sys import _getframe
1516
CO_SIZES = 30, 39
1617
CO_NAMES = set(['pytransform{suffix}', 'pyarmor',
1718
'__name__', '__file__'])
18-
co = pytransform.sys._getframe(1).f_code
19+
co = _getframe(3).f_code
1920
if not ((set(co.co_names) <= CO_NAMES)
2021
and (len(co.co_code) in CO_SIZES)):
2122
raise RuntimeError('unexpected obfuscated script')
2223

2324
def check_lib_pytransform():
24-
platname = pytransform.sys.platform
25-
libname = 'pytransform{suffix}.dylib' if platname.startswith('darwin') else \
26-
'pytransform{suffix}.dll' if platname.startswith('win') else \
27-
'pytransform{suffix}.so'
28-
if getattr(pytransform.sys, 'frozen', False):
29-
filename = pytransform.os.path.join(pytransform.sys._MEIPASS, libname)
30-
else:
31-
filename = pytransform.os.path.join({rpath}, {spath}, libname)
32-
33-
size = pytransform.os.path.getsize(filename) & 0xFFFFFFF0
34-
n = size >> 2
25+
from struct import unpack
26+
filename = pytransform.__file__
3527
with open(filename, 'rb') as f:
36-
buf = f.read(size)
28+
buf = f.read()
29+
size = len(buf) & 0xFFFFFFF0
30+
n = size >> 2
3731
fmt = 'I' * n
38-
checksum = sum(pytransform.struct.unpack(fmt, buf)) & 0xFFFFFFFF
32+
checksum = sum(unpack(fmt, buf[:size])) & 0xFFFFFFFF
3933
if checksum not in {checksum}:
4034
raise RuntimeError('unexpected %s' % filename)
4135

0 commit comments

Comments
 (0)