-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstripsgn
More file actions
executable file
·113 lines (78 loc) · 2.88 KB
/
stripsgn
File metadata and controls
executable file
·113 lines (78 loc) · 2.88 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
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
#!/usr/bin/env python3
#
# Copyright (c) 2020 Gareth Palmer <gareth.palmer3@gmail.com>
# This program is free software, distributed under the terms of
# the GNU General Public License Version 2.
import sys
import os
import os.path
import struct
import getopt
import traceback
HEADER_VERSION = 1
HEADER_LENGTH = 2
class ProgramError(Exception):
pass
def strip_sgn_file(sgn_file):
try:
with open(sgn_file, 'rb') as file:
tlv_data = file.read()
except (PermissionError, FileNotFoundError, IsADirectoryError) as error:
raise ProgramError(f'{error.strerror}: {error.filename}')
tlv_index = 0
(tlv_tag, tlv_length) = struct.unpack_from('> B H', tlv_data, tlv_index)
tlv_index += 3
if tlv_tag != HEADER_VERSION:
raise ProgramError(f'Tag is not header version: {tlv_tag}')
# Skip version
tlv_index += tlv_length
(tlv_tag, tlv_length) = struct.unpack_from('> B H', tlv_data, tlv_index)
tlv_index += 3
if tlv_tag != HEADER_LENGTH:
raise ProgramError(f'Tag is not header length: {tlv_tag}')
(header_length,) = struct.unpack_from('> H', tlv_data, tlv_index)
tlv_index += tlv_length
# Remove .sgn or .sha512
output_file = sgn_file[:sgn_file.rindex('.')]
try:
with open(output_file, 'wb') as file:
file.write(tlv_data[header_length:])
except (PermissionError, FileNotFoundError, IsADirectoryError) as error:
raise ProgramError(f'{error.strerror}: {error.filename}')
print(f'Wrote {output_file}')
def main():
try:
short_options = 'H'
long_options = ['help']
sgn_file = None
help = False
try:
options, arguments = getopt.gnu_getopt(sys.argv[1:], short_options, long_options)
except getopt.GetoptError as error:
raise ProgramError(error.msg[0].upper() + error.msg[1:] +
'. Try \'' + os.path.basename(sys.argv[0]) + ' --help\' for more information')
for option, _ in options:
if option in ('-H', '--help'):
help = True
if help:
print('Usage: ' + os.path.basename(sys.argv[0]) + ' [OPTIONS] SGN-FILE\n'
'Remove .sgn header from a file.\n'
'\n'
' -H, --help print this help and exit\n')
return
if len(arguments):
sgn_file = arguments[0]
if sgn_file is None:
raise ProgramError('No .sgn file specified')
if not sgn_file.endswith(('.sgn', '.sha512')):
raise ProgramError(f'File name does not end with .sgn or .sha512: {sgn_file}')
strip_sgn_file(sgn_file)
except ProgramError as error:
print(str(error), file = sys.stderr)
exit(1)
except Exception:
traceback.print_exc(file = sys.stderr)
exit(1)
exit(0)
if __name__ == '__main__':
main()