✨ Parse command line arguments
This commit is contained in:
parent
1638e9047d
commit
476266863f
3 changed files with 64 additions and 24 deletions
82
src/main.c
82
src/main.c
|
@ -1,5 +1,7 @@
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
//Import our code
|
//Import our code
|
||||||
|
@ -7,49 +9,89 @@
|
||||||
#include "fileReader.h"
|
#include "fileReader.h"
|
||||||
#include "utils/min.h"
|
#include "utils/min.h"
|
||||||
|
|
||||||
|
#define BUFFER_SIZE 0xffffUL
|
||||||
|
#define ENCRYPTION_HEADER_LENGTH 12UL
|
||||||
|
|
||||||
|
uint16_t getUint16(char* buffer) {
|
||||||
|
return (uint16_t)buffer[0] | \
|
||||||
|
(uint16_t)buffer[1] << 8;
|
||||||
|
}
|
||||||
|
uint32_t getUint32(char* buffer) {
|
||||||
|
return (uint32_t)buffer[0] | \
|
||||||
|
(uint32_t)buffer[1] << 8 | \
|
||||||
|
(uint32_t)buffer[2] << 16 | \
|
||||||
|
(uint32_t)buffer[3] << 24;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, unsigned char *argv[]) {
|
int main(int argc, unsigned char *argv[]) {
|
||||||
printf("Hello World!\n");
|
if (argc != 4 && argc != 7) {
|
||||||
|
fprintf(stderr, "Wrong arguments. Usage: patcher-installer <disk_name> <disk_offset> <file_size> [<key0> <key1> <key2>]");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
//TODO: verify argv and assign it to variables
|
//TODO: verify argv and assign it to variables
|
||||||
char* archiveName = "1234";
|
char* archiveName = argv[1];
|
||||||
const unsigned long archiveOffset = 0UL;
|
const unsigned long archiveOffset = atol(argv[2]);
|
||||||
const unsigned long fileLength = 0xffffUL;
|
const unsigned long fileLength = atol(argv[3]);
|
||||||
uint32_t key0 = 0;
|
|
||||||
uint32_t key1 = 0;
|
const bool isEncrypted = argc == 7;
|
||||||
uint32_t key2 = 0;
|
if (isEncrypted) {
|
||||||
|
uint32_t key0 = atoi(argv[4]);
|
||||||
|
uint32_t key1 = atoi(argv[5]);
|
||||||
|
uint32_t key2 = atoi(argv[6]);
|
||||||
|
//Initialise decryption (pass decryption keys)
|
||||||
|
initDecryptor(key0, key1, key2);
|
||||||
|
}
|
||||||
|
|
||||||
|
//-------------------------------------------------
|
||||||
|
|
||||||
//Initialise file reader
|
//Initialise file reader
|
||||||
initFileReader(archiveName, archiveOffset);
|
initFileReader(archiveName, archiveOffset);
|
||||||
|
|
||||||
//Skip header (30 bytes + additional length)
|
//Skip local file header (30 bytes + additional length)
|
||||||
char* fileHeader = getBytes(30UL);
|
char* fileHeader = getBytes(30UL);
|
||||||
printf(fileHeader);
|
//Check that header is correct
|
||||||
//TODO: check that header is correct
|
const uint32_t magic = getUint32(fileHeader);
|
||||||
//TODO: read additional length
|
if (magic != (uint32_t)0x06054b50) {
|
||||||
const unsigned long additionalLength = 0UL;
|
fprintf(stderr, "Wrong magic in local file header, expected 0x06054b50 but found %#010x.", magic);
|
||||||
getBytes(additionalLength);
|
exit(1);
|
||||||
|
}
|
||||||
|
free(fileHeader);
|
||||||
|
//Read additional length
|
||||||
|
const unsigned long additionalLength = getUint16(fileHeader + 26) + getUint16(fileHeader + 28);
|
||||||
|
char* additionalBytes = getBytes(additionalLength);
|
||||||
|
free(additionalBytes);
|
||||||
|
|
||||||
//Initialise decryption (pass decryption keys)
|
//If file is encrypted, skip 12-byte encryption header
|
||||||
initDecryptor(key0, key1, key2);
|
if (isEncrypted) {
|
||||||
|
char* encrHeader = getBytes(ENCRYPTION_HEADER_LENGTH);
|
||||||
|
decrypt(encrHeader, ENCRYPTION_HEADER_LENGTH);
|
||||||
|
free(encrHeader);
|
||||||
|
}
|
||||||
|
|
||||||
//Skip 12-byte encryption header
|
//-------------------------------------------------
|
||||||
const unsigned long encrHeaderLength = 12UL;
|
|
||||||
char* encrHeader = getBytes(encrHeaderLength);
|
|
||||||
decrypt(encrHeader, encrHeaderLength);
|
|
||||||
|
|
||||||
//Read actual file
|
//Read actual file
|
||||||
unsigned long remainingBytes = fileLength;
|
unsigned long remainingBytes = fileLength;
|
||||||
while (remainingBytes > 0) {
|
while (remainingBytes > 0) {
|
||||||
const unsigned long chunkSize = min(0xFF, remainingBytes);
|
const unsigned long chunkSize = min(BUFFER_SIZE, remainingBytes);
|
||||||
char* chunk = getBytes(chunkSize);
|
char* chunk = getBytes(chunkSize);
|
||||||
remainingBytes -= chunkSize;
|
remainingBytes -= chunkSize;
|
||||||
|
|
||||||
|
//Decrypt file if it is encrypted
|
||||||
|
if (isEncrypted) {
|
||||||
|
//TODO: For highest performance, we need to move if condition outside of while loop
|
||||||
decrypt(chunk, chunkSize);
|
decrypt(chunk, chunkSize);
|
||||||
|
}
|
||||||
|
|
||||||
//Decompress file
|
//Decompress file
|
||||||
//TODO
|
//TODO
|
||||||
|
|
||||||
//Optionally perform xdelta3
|
//Optionally perform xdelta3
|
||||||
//TODO
|
//TODO
|
||||||
|
|
||||||
|
//release memory
|
||||||
|
//TODO: need to malloc once outside of while loop, and then reuse it instead of a new malloc() each loop
|
||||||
|
free(chunk);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#include "decryptUtilities.h"
|
#include "decryptUtilities.h"
|
||||||
|
|
||||||
static uint32_t crc32_tab[] = {
|
const uint32_t crc32_tab[] = {
|
||||||
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
|
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
|
||||||
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
|
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
|
||||||
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
|
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
|
||||||
|
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
uint32_t getCrc(uint32_t old_crc, unsigned char c);
|
|
||||||
|
|
||||||
void updateKeys(uint32_t *key_0, uint32_t *key_1, uint32_t *key_2, unsigned char c);
|
void updateKeys(uint32_t *key_0, uint32_t *key_1, uint32_t *key_2, unsigned char c);
|
||||||
|
|
||||||
unsigned char decryptByte(unsigned short keyPart);
|
unsigned char decryptByte(unsigned short keyPart);
|
||||||
|
|
Loading…
Reference in a new issue