Hanno Becker | 4a4047c | 2019-04-24 15:27:29 +0100 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | if [ -d include/mbedtls ]; then :; else |
| 4 | echo "$0: must be run from root" >&2 |
| 5 | exit 1 |
| 6 | fi |
| 7 | |
| 8 | CERTS="library/certs.c" |
| 9 | CERTS_TMP="${CERTS}.tmp" |
| 10 | CERTS_NEW="${CERTS}.new" |
| 11 | |
| 12 | # Remove bodies of BEGIN FILE ... END FILE blocks |
| 13 | SED_RM_FILE_BODIES=":o; /BEGIN FILE/!{p;n;bo}; /BEGIN FILE/{p; n; :i; /END FILE/{n; bo}; n; bi}" |
| 14 | sed -n "${SED_RM_FILE_BODIES}" $CERTS > ${CERTS_TMP} |
| 15 | while IFS= read -r line; do |
| 16 | echo "$line" |
| 17 | CMD=`echo "$line" | sed -n 's/^\/\* BEGIN FILE \([^ ]*\) \([^ ]*\) \([^ ]*\) \([^ ]*\)*.*$/\1 \2 \3 \4/p'` |
| 18 | if [ -n "$CMD" ]; then |
| 19 | enc=$(echo "$CMD" | cut -f1 -d' ' ) |
| 20 | type=$(echo "$CMD" | cut -f2 -d' ' ) |
| 21 | name=$(echo "$CMD" | cut -f3 -d' ' ) |
| 22 | file=$(echo "$CMD" | cut -f4 -d' ' ) |
| 23 | |
| 24 | if [ "$type" != "variable" ] && [ "$type" != "macro" ]; then |
| 25 | exit 1 |
| 26 | fi |
| 27 | |
| 28 | if [ "$enc" != "string" ] && [ "$enc" != "binary" ]; then |
| 29 | exit 1 |
| 30 | fi |
| 31 | |
| 32 | # Support 'binary' and 'string' encoding |
| 33 | # Support 'variable' and 'macro' types |
| 34 | |
| 35 | if [ "$enc" = "binary" ]; then |
| 36 | DATA=`xxd -i "$file" | tail -n +2 | head -n -2 | sed 's/^[ ]*/ /'` |
| 37 | elif [ "$enc" = "string" ]; then |
| 38 | DATA=`cat "$file" | sed 's/^/ \"/;s/$/\\r\\n\"/'` |
| 39 | fi |
| 40 | |
| 41 | if [ "$type" = "variable" ]; then |
| 42 | if [ "$enc" = "binary" ]; then |
| 43 | echo "const unsigned char ${name}[] = {" |
| 44 | xxd -i "$file" | sed 's/^[ ]*/ /' | tail -n +2 | head -n -2 |
| 45 | echo "};" |
| 46 | elif [ "$enc" = "string" ]; then |
| 47 | echo "const char ${name}[] =" |
| 48 | cat "$file" | head -n -1 | sed 's/^/ \"/;s/$/\\r\\n\"/' |
| 49 | cat "$file" | tail -n 1 | sed 's/^/ \"/;s/$/\\r\\n\";/' |
| 50 | fi |
| 51 | elif [ "$type" = "macro" ]; then |
| 52 | if [ "$enc" = "binary" ]; then |
| 53 | printf '%-77s\\\n' "#define ${name} {" |
| 54 | xxd -i "$file" | sed 's/^[ ]*/ /' | tail -n +2 | head -n -2 | |
| 55 | xargs -d'\n' printf '%-77s\\\n' |
| 56 | echo "}" |
| 57 | elif [ "$enc" = "string" ]; then |
| 58 | printf '%-75s\\\n' "#define ${name}" |
| 59 | cat "$file" | head -n -1 | sed 's/^/ \"/; s/$/\\r\\n\"/' | xargs -d'\n' printf '%-75s\\\n' |
| 60 | cat "$file" | tail -n 1 | sed 's/^/ \"/; s/$/\\r\\n\"/' |
| 61 | fi |
| 62 | fi |
| 63 | |
| 64 | echo "/* END FILE */" |
| 65 | fi |
| 66 | done < ${CERTS_TMP} > ${CERTS} |