-
Notifications
You must be signed in to change notification settings - Fork 7
/
pad
executable file
·71 lines (52 loc) · 2.29 KB
/
pad
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
#!/usr/bin/env python3
"""
Tool to add and remove simple padding to files up to 4GB.
Copyright 2020 Brave Software Inc.
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this file,
You can obtain one at https://mozilla.org/MPL/2.0/.
"""
import argparse
import sys
from simplepadding import decode, encode
VERSION = "1.0"
def open_file(filename, is_input):
"""Open the given filename or return stdin/sdtout."""
if is_input:
if filename == '-':
return sys.stdin.buffer
return open(filename, "rb")
if filename == '':
return sys.stdout.buffer
return open(filename, "wb")
def decode_file(input_file, output_file):
"""Unpad the input file and output it."""
output_file.write(decode(input_file.read()))
return 0
def encode_file(input_file, output_file, target_length):
"""Pad the input file and output it."""
output_file.write(encode(input_file.read(), target_length))
return 0
def main(): # pylint: disable=missing-docstring
parser = argparse.ArgumentParser(
description="Add and remove simple padding from files.")
parser.add_argument('input_file', type=str, nargs='?',
default='-', help="input file (default: STDIN)")
parser.add_argument('-o', '--output', dest='output_file', type=str, nargs='?',
default='', help="output file (default: STDOUT)")
parser.add_argument('-l', '--length', dest='length', type=int,
default=0, help="final length of file after padding")
parser.add_argument('-d', '--decode', const='decode', action='store_const',
help="remove padding instead of adding it.")
parser.add_argument('-V', '--version', action='version',
version=f'pad {VERSION}')
args = parser.parse_args()
if not args.decode and args.length <= 0:
print('Length is required for padding', file=sys.stderr)
return 1
with open_file(args.input_file, True) as input_file:
with open_file(args.output_file, False) as output_file:
if args.decode:
return decode_file(input_file, output_file)
return encode_file(input_file, output_file, args.length)
sys.exit(main())