-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcrpt
executable file
·142 lines (130 loc) · 4.33 KB
/
dcrpt
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
#!/bin/bash
# decrypts all files in the specified readable file 'infile'
# reads keys from keysfile
# writes restored, decompressed and decrypted files into writeable directory 'outdir'
PROGN=${0##*/}
# processfile will decompress and decrypt one file
# it expects three arguments: INDIR KEYDIR OUTDIR
function processfile {
local INFILE="$1"
local KEYFILE="$2"
local OUTFILE="$3"
# the last keyfile extension encodes the compression type in dirs
local CH=${KEYFILE##*.}
# u (uncomressed) can be for a listed extension, or because all compressions have failed
[ "$CH" = 'u' ] && {
symcrypt < "$INFILE" "$KEYFILE" > "$OUTFILE"
return
}
symcrypt < "$INFILE" "$KEYFILE" | case "$CH" in
h ) hexify;;
b ) base64 -w 0;;
i ) lzma -d;;
j ) lzma -d | hexify;;
k ) lzma -d | base64 -w 0;;
l ) zstd -d -c;;
m ) zstd -d -c | hexify;;
n ) zstd -d -c | base64 -w 0;;
* ) printf "$PROGN importfile: unrecognised ch\n"; exit 2;;
esac > "$OUTFILE"
}
# function to process a whole directory
# takes three arguments: INDIR KEYDIR OUTDIR
function processdir {
# create the ouput directory
mkdir "$3" || {
printf "$PROGN processdir: failed to create output directory $3!\n" >&2
exit 2
}
cd "$2" # reading the keydir
# iterate through the listing
for ITEM in *; do
[[ "$ITEM" == '*' ]] && break; # no more
if [ -d "$ITEM" ]; then # directory
[ "$IGNORE" ] && continue;
processdir ../"$1/$ITEM" "$ITEM" ../"$3/$ITEM" &
else # process only genuine file
[ -f "$ITEM" ] && {
local ROOTN="${ITEM%.*}" # remove keyfile's encoding extension
processfile ../"$1/$ROOTN" "$ITEM" ../"$3/$ROOTN" &
}
fi
done
cd ..
wait
} # end of processdir
function usage { printf "Usage: $PROGN -[h][q][r][v] indir keydir outdir\n" >&2; }
function helpmsg {
usage
printf "\t%s\n" \
'-h | --help : print this text' \
'-i | --ignore : ignore subdirectories' \
'-q | --quiet : turn off the final summary' \
'-v | --verbose : detailed reporting' >&2
exit 1
}
##################### Main starts here #######################################
# get options, including long options, extracted as arg to the last option '-'
while getopts hiqv-: OPT; do
if [ "$OPT" = "-" ]; then # long option: reset OPT and OPTARG
OPT="${OPTARG%%=*}" # extract long option name
OPTARG="${OPTARG#$OPT}" # extract long option argument (here empty)
OPTARG="${OPTARG#=}" # remove any assignments `=` from long options
fi
case "$OPT" in
h | help ) helpmsg;;
i | ignore ) IGNORE=0;;
q | quiet ) QUIET=0;;
v | verbose ) VERBOSE=0;;
??* ) printf "$PROGN quitting, invalid long option: $OPT\n" >&2; usage; exit 2;;
*) exit 2;; # invalid short option (error will be reported via getopts)
esac
done
shift $((OPTIND-1)) # remove already parsed options and args from $@ list
# Globally validate the output directory
[ -n "$3" ] || {
printf "$PROGN: outdir arg missing\n" >&2
exit 2
}
[ -e "$3" ] && {
printf "$PROGN: output directory '$3' already exists!\n" >&2
exit 2
}
# Globally validate the input file/dir
[ -e "$1" ] || {
printf "$PROGN: inputdir '$1' not found\n" >&2
exit 2
}
# Globally validate the keyfile/dir
[ -e "$2" ] || {
printf "$PROGN: keysdir '$2' not found\n" >&2
exit 2
}
# Warn about missing decompressors
if [ -z $( which zstd ) ]; then
printf "$PROGN warning, zstd compressor is not installed, will not decompress .zst files\n" >&2
fi
if [ -z $( which lzma ) ]; then
printf "$PROGN warning, lzma compressor is not installed, will not decompress .lz files\n" >&2
fi
if [ -z $( which base64 ) ]; then
printf "$PROGN warning, base64 decoder is not installed, will not unpack .b64 files\n" >&2
fi
if [ -z $( which hexify ) ]; then
printf "$PROGN warning, hexify is not installed, will not unpack .hex files\n" >&2
fi
if [ -d "$1" ] && [ -d "$2" ]; then
processdir "$1" "$2" "$3"
printf "$PROGN: decrypting dirs '$1' & '$2'\n"
printf "$PROGN: into dir '$3', using %d cores\n" $( grep -c ^processor /proc/cpuinfo )
wait
else
printf "$PROGN: inputs '$1' '$2' must be both directories\n" >&2
exit 2
fi
# optional final report
if ! [ $QUIET ]; then
SIZES=( `du -hbs "$3" "$1"` ) # array of size,dir,size,dir
printf "$PROGN: ${SIZES[3]} ${SIZES[2]} (%s%%) => $3 (100%%)\n" \
$(( 100*${SIZES[2]}/${SIZES[0]} )) # calculate percentage
fi