/
base64uue
executable file
·171 lines (163 loc) · 4.55 KB
/
base64uue
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Base64 & UUEncode: encode/decode
# 2022.12.31
#
# For Base64 you can use base64.wcx.
#
# If you want to use Python 2:
# just replace shebang on "#!/usr/bin/python2" or "#!/usr/bin/python"
#
# File extension: b64 (Base64) or uue (UUEncode)
#
# Using:
#
# base64uue -l <encoded-file>
# Get the name of encoded file and date/time (for MultiArc).
# Output format: yyyy tt dd hh mm ss aaaaa zzzzzzzzzzzz n+
# For example: 2012-11-23 01:54:17 ....A 664 filename.ext
# base64uue -eb <encoded-file> <file-for-encoding>
# Encode file <file-for-encoding> to Base64 format (like in Total Commander).
# base64uue -eu <encoded-file> <file-for-encoding>
# Encode file <file-for-encoding> to UUEncode format.
# base64uue -d <encoded-file> <filename-for-'extraction'>
# Decode file <encoded-file> (detect by extension).
#
# MultiArc config:
# Base64 https://github.com/j2969719/doublecmd-plugins/blob/master/multiarc.ini.md#b64
# UUEncode https://github.com/j2969719/doublecmd-plugins/blob/master/multiarc.ini.md#uue
import base64
import binascii
import os
import sys
import time
import uu
def get_name_and_size_b64(fl):
j = 1
f = open(fl, 'r')
for line in f:
line = line.strip()
if j > 4:
break
if line[0:13] == 'Content-Type:':
i = line.find(' name="')
n1 = line[i + 7:-1]
elif line[0:20] == 'Content-Disposition:':
i = line.find(' filename="')
n2 = line[i + 11:-1]
j = j + 1
f.close()
if len(n1) >= 1:
n = n1
elif len(n2) >= 1:
n = n2
else:
n = os.path.basename(fl)
n = n[0:-4]
l = []
f = open(fl, 'r')
for line in f:
if line.find('MIME-') > -1 or line.find('Content-') > -1:
continue
else:
l.append(line.strip())
f.close()
s = ''.join(l)
d = base64.b64decode(s.strip())
return n, len(d)
def pack_b64(fl, n):
h = 'MIME-Version: 1.0\nContent-Type: application/octet-stream; name="' + n + '"\nContent-Transfer-Encoding: base64\nContent-Disposition: attachment; filename="' + n + '"'
f = open(n, 'rb')
d = f.read()
f.close()
e = base64.b64encode(d)
l = []
for i in range(0, len(e), 76):
l.append(e[i:i + 76])
o = '\n'.join(l)
f = open(fl, 'w')
f.write(h + '\n\n' + o + '\n')
f.close()
def unpack_b64(fl, n):
l = []
f = open(fl, 'r')
for line in f:
if line.find('MIME-') > -1 or line.find('Content-') > -1:
continue
else:
l.append(line.strip())
f.close()
s = ''.join(l)
d = base64.b64decode(s.strip())
f = open(n, 'wb')
f.write(d)
f.close()
def get_name_and_size_uue(fl):
j = 0
s = 0
l = []
f = open(fl, 'r')
for line in f:
if j == 0:
n1 = line[10:].strip()
j = 1
continue
if line[0:3] == 'end':
j = 2
continue
if j != 2:
l.append(binascii.a2b_uu(line))
continue
i = line.find('size')
if i > 0:
s = line[7:].strip()
i = s.find('/')
s = s[i + 1:]
f.close()
if len(n1) >= 1:
n = n1
else:
n = os.path.basename(fl)
n = n[0:-4]
if s > 0:
return n, s
else:
s = ''.join(l)
return n, len(s)
def pack_uue(fl, n):
uu.encode(n, fl)
def unpack_uue(fl, n):
uu.decode(fl, n)
if len(sys.argv) == 3 or len(sys.argv) == 4:
if len(sys.argv) == 4:
inm = sys.argv[3]
else:
inm = ''
if sys.argv[1] == '-l':
# Получаем имя и размер закодированного файла
if sys.argv[2][-4:] == '.b64':
nnm, sz = get_name_and_size_b64(sys.argv[2])
elif sys.argv[2][-4:] == '.uue':
nnm, sz = get_name_and_size_uue(sys.argv[2])
else:
sys.exit(1)
# Получаем дату
tm = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(os.path.getmtime(sys.argv[2])))
sz = str(sz)
print(tm + ' ....A ' + sz.rjust(12) + ' ' + nnm)
elif sys.argv[1] == '-eb':
pack_b64(sys.argv[2], inm)
elif sys.argv[1] == '-eu':
pack_uue(sys.argv[2], inm)
elif sys.argv[1] == '-d':
if sys.argv[2][-4:] == '.b64':
unpack_b64(sys.argv[2], inm)
elif sys.argv[2][-4:] == '.uue':
unpack_uue(sys.argv[2], inm)
else:
sys.exit(1)
else:
sys.exit(1)
else:
sys.exit(1)