♻ Extract command line args parsing into separate function

This commit is contained in:
C-3PO 2018-09-14 01:02:12 +02:00
parent c02caaf2ff
commit 65bb5a98e3
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
4 changed files with 95 additions and 78 deletions

View file

@ -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",

View file

@ -1,4 +1,3 @@
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@ -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);
//-------------------------------------------------

72
src/parseArguments.c Normal file
View file

@ -0,0 +1,72 @@
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#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;
}

19
src/parseArguments.h Normal file
View file

@ -0,0 +1,19 @@
#include <stdbool.h>
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[]);