From 476266863f9f9c5ac0cc2897c4f15d1a98ecfe36 Mon Sep 17 00:00:00 2001 From: C-3PO Date: Tue, 24 Jul 2018 19:56:21 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Parse=20command=20line=20arguments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.c | 84 +++++++++++++++++++++++++++--------- src/utils/decryptUtilities.c | 2 +- src/utils/decryptUtilities.h | 2 - 3 files changed, 64 insertions(+), 24 deletions(-) diff --git a/src/main.c b/src/main.c index 11f883d..6cf4806 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,7 @@ #include +#include #include +#include #include //Import our code @@ -7,49 +9,89 @@ #include "fileReader.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[]) { - printf("Hello World!\n"); + if (argc != 4 && argc != 7) { + fprintf(stderr, "Wrong arguments. Usage: patcher-installer [ ]"); + exit(1); + } //TODO: verify argv and assign it to variables - char* archiveName = "1234"; - const unsigned long archiveOffset = 0UL; - const unsigned long fileLength = 0xffffUL; - uint32_t key0 = 0; - uint32_t key1 = 0; - uint32_t key2 = 0; + char* archiveName = argv[1]; + const unsigned long archiveOffset = atol(argv[2]); + const unsigned long fileLength = atol(argv[3]); + + const bool isEncrypted = argc == 7; + 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 initFileReader(archiveName, archiveOffset); - //Skip header (30 bytes + additional length) + //Skip local file header (30 bytes + additional length) char* fileHeader = getBytes(30UL); - printf(fileHeader); - //TODO: check that header is correct - //TODO: read additional length - const unsigned long additionalLength = 0UL; - getBytes(additionalLength); + //Check that header is correct + const uint32_t magic = getUint32(fileHeader); + if (magic != (uint32_t)0x06054b50) { + fprintf(stderr, "Wrong magic in local file header, expected 0x06054b50 but found %#010x.", magic); + 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) - initDecryptor(key0, key1, key2); + //If file is encrypted, skip 12-byte encryption header + 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 unsigned long remainingBytes = fileLength; while (remainingBytes > 0) { - const unsigned long chunkSize = min(0xFF, remainingBytes); + const unsigned long chunkSize = min(BUFFER_SIZE, remainingBytes); char* chunk = getBytes(chunkSize); remainingBytes -= chunkSize; - decrypt(chunk, 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); + } //Decompress file //TODO //Optionally perform xdelta3 //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; diff --git a/src/utils/decryptUtilities.c b/src/utils/decryptUtilities.c index 19c8117..2c88aca 100644 --- a/src/utils/decryptUtilities.c +++ b/src/utils/decryptUtilities.c @@ -1,6 +1,6 @@ #include "decryptUtilities.h" -static uint32_t crc32_tab[] = { +const uint32_t crc32_tab[] = { 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, diff --git a/src/utils/decryptUtilities.h b/src/utils/decryptUtilities.h index bac8be9..411fe5a 100644 --- a/src/utils/decryptUtilities.h +++ b/src/utils/decryptUtilities.h @@ -2,8 +2,6 @@ #include -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); unsigned char decryptByte(unsigned short keyPart);