Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
52867 views
1
#!/bin/sh
2
3
# Convert standard input to a C char array, write to a file, then create an
4
# MD5 sum of that file and append said MD5 sum as char array to the file.
5
6
[ -n "$1" ] || exit 1
7
8
# Filter out whitespace, empty lines, and comments.
9
sanitize() {
10
sed 's/^[[:space:]]*//; /^$/d; /^\/\//d'
11
}
12
13
# Convert stdin to a \0-terminated char array.
14
dump() {
15
echo "static const char $1[] = {"
16
od -v -A n -t x1 | sed 's/[[:space:]]*\([[:alnum:]]\{2\}\)/0x\1, /g'
17
echo '0x00 };'
18
}
19
20
# Print MD5 hash w/o newline character to not embed the character in the array.
21
hash() {
22
# md5sum is not standard, so try different platform-specific alternatives.
23
{ md5sum "$1" || md5 -q "$1" || digest -a md5 "$1"; } 2>/dev/null |
24
cut -b -32 | tr -d '\n\r'
25
}
26
27
trap 'rm -f "$1.temp"' EXIT
28
29
sanitize | tee "$1.temp" |
30
dump 'x264_opencl_source' > "$1"
31
32
hash "$1.temp" |
33
dump 'x264_opencl_source_hash' >> "$1"
34
35