-
Notifications
You must be signed in to change notification settings - Fork 1
/
extract-zfo.sh
executable file
·67 lines (57 loc) · 1.75 KB
/
extract-zfo.sh
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
#!/bin/bash
set -eu
main() {
local infile infile_extension
infile="$1"
infile_extension="${infile##*.}"
OUTDIR="$(realpath "$infile").files"
if [[ "$infile_extension" == 'zfo' ]]; then
extract_zfo "$infile"
else
err 1 "error: unsupported extension (use .zfo)\n" || return $?
fi
}
extract_zfo() {
local xml rc=0 stylesheet files
log "info: verifying the document signature (openssl): "
xml="$(openssl smime -inform DER -verify -noverify -in "$1")" || rc=$?
[[ "$rc" -ne 0 ]] && return "$rc"
stylesheet='
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:for-each select="'"//*[local-name()='dmFile']"'">
<xsl:value-of select="@dmFileDescr"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>'
files="$(xsltproc <(printf '%s' "$stylesheet") - <<<"$xml")"
if [ -z "$files" ]; then
err 1 "error: no files found\n" || return $?
fi
log "info: extracting files to: %s\n" "$(basename "$OUTDIR")"
mkdir -p "$OUTDIR"
cd "$OUTDIR"
while read -r file; do
stylesheet='
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:value-of select="'"//*[local-name()='dmFile' and @dmFileDescr='$file']/*[local-name()='dmEncodedContent']/text()"'"/>
</xsl:template>
</xsl:stylesheet>'
log "info: extracting %s\n" "$file"
xsltproc <(printf '%s' "$stylesheet") - <<<"$xml" | base64 -d > "$file"
done <<<"$files"
}
log() {
printf "$@" >&2
}
err() {
local e="$1"
shift
log "$@"
return "$e"
}
main "$@"