Parse decryption keys from comma-delimited string

This commit is contained in:
C-3PO 2018-09-14 01:51:48 +02:00
parent 0ce4fcf492
commit 71e4681d5c
Signed by: c3po
GPG key ID: 62993C4BB4D86F24

View file

@ -1,6 +1,7 @@
#include <getopt.h> #include <getopt.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "decrypt.h" #include "decrypt.h"
#include "errorAndExit.h" #include "errorAndExit.h"
@ -49,13 +50,26 @@ struct arguments parseArguments(int argc, char *argv[]) {
state.fileSize = atol(optarg); state.fileSize = atol(optarg);
hasSize = true; hasSize = true;
break; break;
case 'k': { //decryption keys case 'k': { //decryption keys (12345,12345,12345)
//TODO: parse from optarg //Split argument by comma into three integers
uint32_t key0 = atoi(argv[8]); char* token = strtok(optarg, ",");
uint32_t key1 = atoi(argv[9]); uint32_t keys[3] = { 0UL, 0UL, 0UL };
uint32_t key2 = atoi(argv[10]); int index = 0;
while (token != NULL) {
if (index > 2) {
fprintf(stderr, "Too many decryption keys specified with --keys; only 3 are allowed.\n");
errorAndExit();
}
keys[index] = atoi(token);
index += 1;
token = strtok(NULL, ",");
}
if (index < 3) {
fprintf(stderr, "Not enough decryption keys specified with --keys; 3 are required but only found %i.\n", index);
errorAndExit();
}
//Initialize decryption (pass decryption keys) //Initialize decryption (pass decryption keys)
initDecryptor(key0, key1, key2); initDecryptor(keys[0], keys[1], keys[2]);
state.isEncrypted = true; state.isEncrypted = true;
break; break;
} }