-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathsbxenc.py
163 lines (137 loc) · 5.63 KB
/
sbxenc.py
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
#!/usr/bin/env python3
#--------------------------------------------------------------------------
# SBXEnc - Sequenced Box container Encoder
#
# Created: 10/02/2017
#
# Copyright (C) 2017 Marco Pontello - http://mark0.net/
#
# Licence:
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#--------------------------------------------------------------------------
import os
import sys
import hashlib
import argparse
import binascii
from functools import partial
from time import time
import seqbox
PROGRAM_VER = "1.0.2"
def get_cmdline():
"""Evaluate command line parameters, usage & help."""
parser = argparse.ArgumentParser(
description="create a SeqBox container",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
prefix_chars='-+')
parser.add_argument("-v", "--version", action='version',
version='SeqBox - Sequenced Box container - ' +
'Encoder v%s - (C) 2017 by M.Pontello' % PROGRAM_VER)
parser.add_argument("filename", action="store",
help="file to encode")
parser.add_argument("sbxfilename", action="store", nargs='?',
help="SBX container")
parser.add_argument("-o", "--overwrite", action="store_true", default=False,
help="overwrite existing file")
parser.add_argument("-nm","--nometa", action="store_true", default=False,
help="exclude matadata block")
parser.add_argument("-uid", action="store", default="r", type=str,
help="use random or custom UID (up to 12 hexdigits)")
parser.add_argument("-sv", "--sbxver", type=int, default=1,
help="SBX blocks version", metavar="n")
parser.add_argument("-p", "--password", type=str, default="",
help="encrypt with password", metavar="pass")
res = parser.parse_args()
return res
def errexit(errlev=1, mess=""):
"""Display an error and exit."""
if mess != "":
sys.stderr.write("%s: error: %s\n" %
(os.path.split(sys.argv[0])[1], mess))
sys.exit(errlev)
def getsha256(filename):
"""SHA256 used to verify the integrity of the encoded file"""
with open(filename, mode='rb') as fin:
d = hashlib.sha256()
for buf in iter(partial(fin.read, 1024*1024), b''):
d.update(buf)
return d.digest()
def main():
cmdline = get_cmdline()
filename = cmdline.filename
sbxfilename = cmdline.sbxfilename
if not sbxfilename:
sbxfilename = os.path.split(filename)[1] + ".sbx"
elif os.path.isdir(sbxfilename):
sbxfilename = os.path.join(sbxfilename,
os.path.split(filename)[1] + ".sbx")
if os.path.exists(sbxfilename) and not cmdline.overwrite:
errexit(1, "SBX file '%s' already exists!" % (sbxfilename))
#parse eventual custom uid
uid = cmdline.uid
if uid !="r":
uid = uid[-12:]
try:
uid = int(uid, 16).to_bytes(6, byteorder='big')
except:
errexit(1, "invalid UID")
if not os.path.exists(filename):
errexit(1, "file '%s' not found" % (filename))
filesize = os.path.getsize(filename)
fout = open(sbxfilename, "wb", buffering=1024*1024)
#calc hash - before all processing, and not while reading the file,
#just to be cautious
if not cmdline.nometa:
print("hashing file '%s'..." % (filename))
sha256 = getsha256(filename)
print("SHA256",binascii.hexlify(sha256).decode())
fin = open(filename, "rb", buffering=1024*1024)
print("creating file '%s'..." % sbxfilename)
sbx = seqbox.SbxBlock(uid=uid, ver=cmdline.sbxver, pswd=cmdline.password)
#write metadata block 0
if not cmdline.nometa:
sbx.metadata = {"filesize":filesize,
"filename":os.path.split(filename)[1],
"sbxname":os.path.split(sbxfilename)[1],
"filedatetime":int(os.path.getmtime(filename)),
"sbxdatetime":int(time()),
"hash":b'\x12\x20'+sha256} #multihash
fout.write(sbx.encode())
#write all other blocks
ticks = 0
updatetime = time()
while True:
buffer = fin.read(sbx.datasize)
if len(buffer) < sbx.datasize:
if len(buffer) == 0:
break
sbx.blocknum += 1
sbx.data = buffer
fout.write(sbx.encode())
#some progress update
if time() > updatetime:
print("%.1f%%" % (fin.tell()*100.0/filesize), " ",
end="\r", flush=True)
updatetime = time() + .1
print("100% ")
fin.close()
fout.close()
totblocks = sbx.blocknum if cmdline.nometa else sbx.blocknum + 1
sbxfilesize = totblocks * sbx.blocksize
overhead = 100.0 * sbxfilesize / filesize - 100 if filesize > 0 else 0
print("SBX file size: %i - blocks: %i - overhead: %.1f%%" %
(sbxfilesize, totblocks, overhead))
if __name__ == '__main__':
main()