ssn-installer/src/parseArguments.c

73 lines
1.8 KiB
C
Raw Normal View History

#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;
}