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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "decrypt.h"
#include "errorAndExit.h"
@ -49,13 +50,26 @@ struct arguments parseArguments(int argc, char *argv[]) {
state.fileSize = atol(optarg);
hasSize = true;
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]);
case 'k': { //decryption keys (12345,12345,12345)
//Split argument by comma into three integers
char* token = strtok(optarg, ",");
uint32_t keys[3] = { 0UL, 0UL, 0UL };
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)
initDecryptor(key0, key1, key2);
initDecryptor(keys[0], keys[1], keys[2]);
state.isEncrypted = true;
break;
}