-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathseqbox.py
145 lines (125 loc) · 4.8 KB
/
seqbox.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
#!/usr/bin/env python3
#--------------------------------------------------------------------------
# SeqBox - Sequenced Box container module
#
# Created: 03/03/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 binascii
import random
supported_vers = [1, 2]
class sbxBlock():
"""
Implement a basic SBX block
"""
def __init__(self, ver=1, uid="r"):
self.ver = ver
if ver == 1:
self.blocksize = 512
self.hdrsize = 16
elif ver == 2:
#mostly a test to double check that all tools works correctly
#with different blocks versions/parameters.
#or it could be good for CP/M! :)
self.blocksize = 128
self.hdrsize = 16
else:
raise version_not_supported #put in a proper exception
self.datasize = self.blocksize - self.hdrsize
self.magic = b'SBx' + bytes([ver])
self.blocknum = 0
if uid == "r":
random.seed()
self.uid = random.getrandbits(6*8).to_bytes(6, byteorder='big')
else:
self.uid = (b'\x00'*6 + uid)[-6:]
self.parent_uid = 0
self.metadata = {}
self.data = b""
def __str__(self):
return "SBX Block ver: '%i', size: %i, hdr size: %i, data: %i" % \
(self.ver, self.blocksize, self.hdrsize, self.datasize)
def encode(self):
if self.blocknum == 0:
self.data = b""
if "filename" in self.metadata:
bb = self.metadata["filename"].encode()
self.data += b"FNM" + bytes([len(bb)]) + bb
if "sbxname" in self.metadata:
bb = self.metadata["sbxname"].encode()
self.data += b"SNM" + bytes([len(bb)]) + bb
if "filesize" in self.metadata:
bb = self.metadata["filesize"].to_bytes(8, byteorder='big')
self.data += b"FSZ" + bytes([len(bb)]) + bb
if "hash" in self.metadata:
bb = self.metadata["hash"]
self.data += b"HSH" + bytes([len(bb)]) + bb
data = self.data + b'\x1A' * (self.datasize - len(self.data))
buffer = (self.uid +
self.blocknum.to_bytes(4, byteorder='big') +
data)
crc = binascii.crc_hqx(buffer, self.ver).to_bytes(2,byteorder='big')
return (self.magic + crc + buffer)
def decode(self, buffer):
#start setting an invalid block number
self.blocknum = -1
#check the basics
if len(buffer) != self.blocksize:
return False
if buffer[:3] != self.magic[:3]:
return False
if not buffer[3] in supported_vers:
return False
#check CRC of rest of the block
crc = int.from_bytes(buffer[4:6], byteorder='big')
if crc != binascii.crc_hqx(buffer[6:], self.ver):
return False
self.parent_uid = 0
self.uid = buffer[6:12]
self.blocknum = int.from_bytes(buffer[12:16], byteorder='big')
self.data = buffer[16:]
self.metadata = {}
if self.blocknum == 0:
#decode meta data
p = 0
while p < (len(self.data)-3):
metaid = self.data[p:p+3]
p+=3
if metaid == b"\x1a\x1a\x1a":
break
else:
metalen = self.data[p]
metabb = self.data[p+1:p+1+metalen]
p = p + 1 + metalen
if metaid == b'FNM':
self.metadata["filename"] = metabb.decode('utf-8')
if metaid == b'SNM':
self.metadata["sbxname"] = metabb.decode('utf-8')
if metaid == b'FSZ':
self.metadata["filesize"] = int.from_bytes(metabb, byteorder='big')
if metaid == b'HSH':
self.metadata["hash"] = metabb
return True
def main():
print("SeqBox module!")
sys.exit(0)
if __name__ == '__main__':
main()