forked from dhondta/python-codext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaaencode.py
More file actions
79 lines (71 loc) · 4.05 KB
/
Copy pathaaencode.py
File metadata and controls
79 lines (71 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# -*- coding: UTF-8 -*-
"""AAEncode JavaScript emoticon encoding without executing JavaScript."""
from ..__common__ import add, ensure_str
DIGITS = (
"(c^_^o)", "(゚Θ゚)", "((o^_^o) - (゚Θ゚))", "(o^_^o)",
"(゚ー゚)", "((゚ー゚) + (゚Θ゚))", "((o^_^o) +(o^_^o))", "((゚ー゚) + (o^_^o))",
"((゚ー゚) + (゚ー゚))", "((゚ー゚) + (゚ー゚) + (゚Θ゚))", "(゚Д゚) .゚ω゚ノ", "(゚Д゚) .゚Θ゚ノ",
"(゚Д゚) ['c']", "(゚Д゚) .゚ー゚ノ", "(゚Д゚) .゚Д゚ノ", "(゚Д゚) [゚Θ゚]",
)
PREFIX = "゚ω゚ノ= /`m´)ノ ~┻━┻ //*´∇`*/ ['_']; o=(゚ー゚) =_=3; c=(゚Θ゚) =(゚ー゚)-(゚ー゚); "
HEADER = "(゚Д゚)[゚o゚]+ "
BLOCK = "(゚Д゚)[゚ε゚]+"
HEX = "(o゚ー゚o)+ "
SUFFIX = "(゚Д゚)[゚o゚]) (゚Θ゚)) ('_');"
RUNTIME = (
"(゚Д゚) =(゚Θ゚)= (o^_^o)/ (o^_^o);"
"(゚Д゚)={゚Θ゚: '_' ,゚ω゚ノ : ((゚ω゚ノ==3) +'_') [゚Θ゚] "
",゚ー゚ノ :(゚ω゚ノ+ '_')[o^_^o -(゚Θ゚)] "
",゚Д゚ノ:((゚ー゚==3) +'_')[゚ー゚] }; (゚Д゚) [゚Θ゚] =((゚ω゚ノ==3) +'_') [c^_^o];"
"(゚Д゚) ['c'] = ((゚Д゚)+'_') [ (゚ー゚)+(゚ー゚)-(゚Θ゚) ];"
"(゚Д゚) ['o'] = ((゚Д゚)+'_') [゚Θ゚];"
"(゚o゚)=(゚Д゚) ['c']+(゚Д゚) ['o']+(゚ω゚ノ +'_')[゚Θ゚]+ ((゚ω゚ノ==3) +'_') [゚ー゚] + "
"((゚Д゚) +'_') [(゚ー゚)+(゚ー゚)]+ ((゚ー゚==3) +'_') [゚Θ゚]+"
"((゚ー゚==3) +'_') [(゚ー゚) - (゚Θ゚)]+(゚Д゚) ['c']+"
"((゚Д゚)+'_') [(゚ー゚)+(゚ー゚)]+ (゚Д゚) ['o']+"
"((゚ー゚==3) +'_') [゚Θ゚];(゚Д゚) ['_'] =(o^_^o) [゚o゚] [゚o゚];"
"(゚ε゚)=((゚ー゚==3) +'_') [゚Θ゚]+ (゚Д゚) .゚Д゚ノ+"
"((゚Д゚)+'_') [(゚ー゚) + (゚ー゚)]+((゚ー゚==3) +'_') [o^_^o -゚Θ゚]+"
"((゚ー゚==3) +'_') [゚Θ゚]+ (゚ω゚ノ +'_') [゚Θ゚]; "
"(゚ー゚)+=(゚Θ゚); (゚Д゚)[゚ε゚]='\\\\'; "
"(゚Д゚).゚Θ゚ノ=(゚Д゚+ ゚ー゚)[o^_^o -(゚Θ゚)];"
"(o゚ー゚o)=(゚ω゚ノ +'_')[c^_^o];"
"(゚Д゚) [゚o゚]='\"';"
"(゚Д゚) ['_'] ( (゚Д゚) ['_'] (゚ε゚+"
)
def aaencode(text, errors="strict"):
blocks = []
encoded = ensure_str(text).encode("utf-16-be", errors)
for index in range(0, len(encoded), 2):
codepoint = int.from_bytes(encoded[index:index + 2], "big")
radix, marker = (8, "") if codepoint <= 127 else (16, HEX)
blocks.append(BLOCK + marker + "".join(DIGITS[int(digit, radix)] + "+ " for digit in format(codepoint, "o" if radix == 8 else "x")))
result = PREFIX + RUNTIME + HEADER + "".join(blocks) + SUFFIX
return result, len(ensure_str(text))
def aadecode(text, errors="strict"):
value = ensure_str(text).replace("/*´∇`*/", "")
start, end = value.find(HEADER), value.rfind(SUFFIX)
if start < 0 or end < 0 or end < start:
raise ValueError("input is not AAEncoded")
data = value[start + len(HEADER):end]
result = bytearray()
for encoded in data.split(BLOCK)[1:]:
radix = 16 if encoded.startswith(HEX) else 8
encoded = encoded[len(HEX):] if radix == 16 else encoded
number, position = "", 0
ordered = sorted(enumerate(DIGITS), key=lambda item: len(item[1]), reverse=True)
while position < len(encoded):
while position < len(encoded) and encoded[position] in "+ \r\n\t":
position += 1
if position == len(encoded):
break
match = next(((digit, token) for digit, token in ordered if encoded.startswith(token, position)), None)
if match is None:
raise ValueError("invalid AAEncode digit expression")
number += format(match[0], "x")
position += len(match[1])
if not number:
raise ValueError("empty AAEncode character block")
result.extend(int(number, radix).to_bytes(2, "big"))
return result.decode("utf-16-be", errors), len(value)
add("aaencode", aaencode, aadecode, r"^(?:aa[-_]?encode|aa[-_]?codec)$")