-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathb64d
More file actions
executable file
·54 lines (40 loc) · 1.04 KB
/
b64d
File metadata and controls
executable file
·54 lines (40 loc) · 1.04 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
#!/usr/bin/python3
import sys
import re
from base64 import b64decode
from base64 import urlsafe_b64decode
USAGE = """
USAGE
b64 < text.b64
b64 text.b64
DESCRIPTION
Decode base64 encoded UTF-8 text with optional padding that's either url or
normal base64. Also continue decoding recursively for things that are
encoded multiple times.
"""
b64u_chars = re.compile("[-_]")
def decode(data):
_decode = b64decode
if b64u_chars.search(data) is not None:
_decode = urlsafe_b64decode
while True:
padding = "=" * (4 - (len(data) % 4))
try:
data = _decode(data + padding).decode(encoding="utf-8")
except:
return data
def main():
match len(sys.argv):
case 1:
infile = sys.stdin
case 2:
infile = open(sys.argv[1], encoding="UTF-8")
case _:
print(usage, file=sys.stderr)
sys.exit(1)
try:
print(decode(infile.read()))
finally:
infile.close()
if __name__ == "__main__":
main()