diff options
author | DiffieHellman <DiffieHellman@endianness.com> | 2023-03-17 22:19:20 +1100 |
---|---|---|
committer | DiffieHellman <DiffieHellman@endianness.com> | 2023-03-17 22:19:20 +1100 |
commit | 199edf7297ac5537739d54dc3ce4736422638fcd (patch) | |
tree | 7702fd02e15d0868394b55ea796d4a096dc93775 | |
parent | 0d6ad24bce587aefde45d08ad13b1b01ccc2baa5 (diff) | |
download | firmware-reaper-master.tar.gz firmware-reaper-master.tar.bz2 firmware-reaper-master.zip |
-rwxr-xr-x | generate_extractor.sh (renamed from firmware_reaper.sh) | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/firmware_reaper.sh b/generate_extractor.sh index 0fce63f..0b9c25a 100755 --- a/firmware_reaper.sh +++ b/generate_extractor.sh @@ -9,12 +9,12 @@ # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License version 3 for more details. - #return invalid usage if there isn't 2 args [ "$#" -eq 1 ] || { echo "Usage: ./generate_extractor.sh <input_file>"; exit 2; } #make the script automatically exit if any subcommand fails -set -euo pipefail +#set -euo pipefail +set -euxo pipefail input="$1" output="output.c" @@ -38,6 +38,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public #include <stdint.h> typedef uint8_t u1Byte; +typedef uint32_t u4Byte; typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; @@ -51,14 +52,16 @@ typedef int32_t s32; tr '\t' ' ' < "$input" | tr '\n' ' ' | tr '\r' ' ' | tr --squeeze-repeats ' ' > "$squeezed_input" #extract C arrays while also preventing arbitrary code execution. grep in -z mode outputs \0 at the end of each match, which is changed to 2 newlines. sadly no UTF-8 support -grep -ozE "((u|s)(8|16|32|64|1Byte)|(|unsigned )((|u)int(|_fast)(|8_t|16_t|32_t)|char|short|long|long long)) "\ -"[[:alnum:]_]+ ?\[ ?]"\ -" ?= ?"\ -"{[[:space:][:digit:]A-F,x.]+} ?;" "$squeezed_input" | sed 's/\x0/\n\n/g' >> "$output" +grep -ozE "((|const )(u|s)(8|16|32|64|1Byte)|(|unsigned )((|u)int(|_fast)(|8_t|16_t|32_t)|char|short|long|long long)) "\ +"[[:alnum:]_]+ ?\[[A-Za-z ]*\]"\ +"[A-Za-z_ ]*= ?"\ +"{[[:space:][:digit:]A-Fa-f,x.]+} ?;" "$squeezed_input" | sed 's/\x0/\n\n/g' >> "$output" #get names of all arrays previously extracted names=$(grep '\[' "$output" | sed 's/ *\[.*//' | sed 's/.* //') +#todo: handle automatically outputting arrays like: u8 long_winded_name[LongWindedName] = { + #array to file function echo 'void openWriteClose(char filename[], char array[], int size) { @@ -87,4 +90,5 @@ echo " rm "$squeezed_input" #compile and run the extractor -gcc "$output" -o extractor && ./extractor && rm extractor +gcc "$output" -o extractor && ./extractor && rm extractor && rm "$output" +grep '\[' "$input" |