diff --git a/.vscode/settings.json b/.vscode/settings.json index d5421a6..505d4d4 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -6,7 +6,8 @@ "decryptutilities.h": "c", "fileutilities.h": "c", "stdio.h": "c", - "stdlib.h": "c" + "stdlib.h": "c", + "stdbool.h": "c" }, "cSpell.words": [ "init", diff --git a/src/main.c b/src/main.c index ea3a849..b2744ff 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,3 @@ -#include #include #include #include @@ -9,6 +8,7 @@ #include "decrypt.h" #include "fileReader.h" #include "inflate.h" +#include "parseArguments.h" #include "utils/min.h" #define BUFFER_SIZE 512UL * 1024UL //512 KiB @@ -26,84 +26,9 @@ uint32_t getUint32(uint8_t* buffer) { (uint32_t)buffer[3] << 24; } -struct arguments { - /** Path to the disk file that contains the start of the file we want to extract. - * The file may stretch across multiple disks though. - */ - char* diskName; - /** Offset into the disk where the file starts. */ - unsigned long diskOffset; - /** Size of the file stored in the disk. */ - unsigned long fileSize; - //Decryption keys - bool isEncrypted; - //For xdelta3, the location of the old file - char* prevFile; -}; - -static struct option long_options[] = { - {"disk", required_argument, 0, 'd'}, - {"offset", required_argument, 0, 'o'}, - {"size", required_argument, 0, 's'}, - {"keys", required_argument, 0, 'k'}, - {"prev", required_argument, 0, 'p'}, -}; - -//Stores current state from command line arguments, initialized to zero -struct arguments state = {}; - int main(int argc, char *argv[]) { //Parse command line arguments - - int requiredOptions = 0; - - while (1) { - //in this variable, getopt_long stores the current position in the command line args array - int option_index = 0; - - int curOption = getopt_long(argc, argv, "d:o:s:k:p:", long_options, &option_index); - - //end of command line arguments reached - if (curOption == -1) { - break; - } - - switch (curOption) { - case 'd': //disk name - state.diskName = optarg; - requiredOptions |= 1; - break; - case 'o': //offset - state.diskOffset = atol(optarg); - requiredOptions |= 2; - break; - case 's': //size - state.fileSize = atol(optarg); - requiredOptions |= 4; - break; - case 'k': { //decryption keys - //TODO: parse from optarg - uint32_t key0 = atoi(argv[8]); - uint32_t key1 = atoi(argv[9]); - uint32_t key2 = atoi(argv[10]); - //Initialize decryption (pass decryption keys) - initDecryptor(key0, key1, key2); - state.isEncrypted = true; - break; - } - case 'p': //prev file for xdelta3 - //TODO - break; - default: - fprintf(stderr, "Unknown option '%c'.", (char)curOption); - exit(1); - } - } - - if (requiredOptions != 7) { - fprintf(stderr, "Missing arguments, received %i.", requiredOptions); - exit(1); - } + struct arguments state = parseArguments(argc, argv); //------------------------------------------------- diff --git a/src/parseArguments.c b/src/parseArguments.c new file mode 100644 index 0000000..04942c1 --- /dev/null +++ b/src/parseArguments.c @@ -0,0 +1,72 @@ +#include +#include +#include + +#include "decrypt.h" +#include "parseArguments.h" + +static struct option long_options[] = { + {"disk", required_argument, 0, 'd'}, + {"offset", required_argument, 0, 'o'}, + {"size", required_argument, 0, 's'}, + {"keys", required_argument, 0, 'k'}, + {"prev", required_argument, 0, 'p'}, +}; + + +struct arguments parseArguments(int argc, char *argv[]) { + //Stores current state from command line arguments, initialized to zero + struct arguments state = {}; + + int requiredOptions = 0; + + while (1) { + //in this variable, getopt_long stores the current position in the command line args array + int option_index = 0; + + int curOption = getopt_long(argc, argv, "d:o:s:k:p:", long_options, &option_index); + + //end of command line arguments reached + if (curOption == -1) { + break; + } + + switch (curOption) { + case 'd': //disk name + state.diskName = optarg; + requiredOptions |= 1; + break; + case 'o': //offset + state.diskOffset = atol(optarg); + requiredOptions |= 2; + break; + case 's': //size + state.fileSize = atol(optarg); + requiredOptions |= 4; + break; + case 'k': { //decryption keys + //TODO: parse from optarg + uint32_t key0 = atoi(argv[8]); + uint32_t key1 = atoi(argv[9]); + uint32_t key2 = atoi(argv[10]); + //Initialize decryption (pass decryption keys) + initDecryptor(key0, key1, key2); + state.isEncrypted = true; + break; + } + case 'p': //prev file for xdelta3 + //TODO + break; + default: + fprintf(stderr, "Unknown option '%c'.", (char)curOption); + exit(1); + } + } + + if (requiredOptions != 7) { + fprintf(stderr, "Missing arguments, received %i.", requiredOptions); + exit(1); + } + + return state; +} diff --git a/src/parseArguments.h b/src/parseArguments.h new file mode 100644 index 0000000..1ba22bb --- /dev/null +++ b/src/parseArguments.h @@ -0,0 +1,19 @@ +#include + +struct arguments { + /** Path to the disk file that contains the start of the file we want to extract. + * The file may stretch across multiple disks though. + */ + char* diskName; + /** Offset into the disk where the file starts. */ + unsigned long diskOffset; + /** Size of the file stored in the disk. */ + unsigned long fileSize; + //Decryption keys + bool isEncrypted; + //For xdelta3, the location of the old file + char* prevFile; +}; + + +struct arguments parseArguments(int argc, char *argv[]);