From d7dcd97c75113b0723cb3c76a018c19374054a19 Mon Sep 17 00:00:00 2001 From: C-3PO Date: Tue, 24 Jul 2018 17:07:47 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20Implement=20file=20reader?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/fileReader.c | 65 +++++++++++++++++++++++++++++++++++++-------- src/fileReader.h | 4 +-- src/fileUtilities.h | 46 ++++++++++++++++++++++++++++++++ src/main.c | 5 ++-- 4 files changed, 105 insertions(+), 15 deletions(-) create mode 100644 src/fileUtilities.h diff --git a/src/fileReader.c b/src/fileReader.c index e6948ce..2cd5ad4 100644 --- a/src/fileReader.c +++ b/src/fileReader.c @@ -4,23 +4,66 @@ #include //for strerror() #include +//for malloc +#include #include "fileReader.h" +#include "fileUtilities.h" -void initFileReader(char path[], unsigned long offset, unsigned long length) { - //Opens the given file at the given address - //Returns a function that will read into memory and return that memory. +//minimum of two integers +#define min(a,b) \ + ({ __typeof__ (a) _a = (a); \ + __typeof__ (b) _b = (b); \ + _a < _b ? _a : _b; }) - FILE *fileFrom = NULL; - char* fileName = path; - fileFrom = fopen(fileName, "r"); - if (fileFrom == NULL) { - fprintf(stderr, "Could not open file %s for reading: %s\n", fileName, strerror(errno)); - //error(); +//Opens the given file at the given offset +void initFileReader(char path[], unsigned long offset) { + file.name = path; + + file.filePointer = fopen(file.name, "r"); + if (file.filePointer == NULL) { + fprintf(stderr, "Could not open file %s for reading: %s\n", file.name, strerror(errno)); + exit(1); } + + //get file size + fseek(file.filePointer, 0L, SEEK_END); + file.size = ftell(file.filePointer); + + //seek to offset + if (offset != 0UL) { + fseek(file.filePointer, offset, SEEK_SET); + } else { + rewind(file.filePointer); + } + file.offset = offset; } -unsigned char* readBytes(unsigned long numBytes) { - //TODO + +//Reads the given amount of bytes from the file and returns them. Automatically opens next file if EOF is reached. +char* getBytes(unsigned long numBytes) { + char* output = malloc(numBytes); + if (output == NULL) { + fprintf(stderr, "Could not allocate %lu bytes.\n", numBytes); + exit(1); + } + char* bufferPosition = output; + unsigned long remainingBytes = numBytes; + + //As long as we still need to read bytes + while (remainingBytes > 0) { + //Read as many bytes as we can from the current file + const unsigned long availableBytes = min(remainingBytes, file.size - file.offset); + readBytesIntoBuffer(bufferPosition, availableBytes); + bufferPosition += availableBytes; + remainingBytes -= availableBytes; + + //If we've reached end of file, close file and open next file + if (file.offset == file.size) { + openNextFile(); + } + } + + return output; } diff --git a/src/fileReader.h b/src/fileReader.h index 687cab1..5db6687 100644 --- a/src/fileReader.h +++ b/src/fileReader.h @@ -1,5 +1,5 @@ #pragma once -void initFileReader(char path[], unsigned long offset, unsigned long length); +void initFileReader(char path[], unsigned long offset); -unsigned char* readBytes(unsigned long numBytes); +char* getBytes(unsigned long numBytes); diff --git a/src/fileUtilities.h b/src/fileUtilities.h new file mode 100644 index 0000000..01f5b6d --- /dev/null +++ b/src/fileUtilities.h @@ -0,0 +1,46 @@ +#pragma once + +#include "fileReader.h" + +//A structure to store various information needed when reading from a file. +struct FILE_INFO { + char* name; + unsigned long size; + FILE* filePointer; + unsigned long offset; +}; + +struct FILE_INFO file; + + +//Reads the given amount of bytes from the currently open file into the given buffer. +void readBytesIntoBuffer(char* buffer, long numBytes) { + const size_t result = fread(buffer, 1, numBytes, file.filePointer); + file.offset += result; + + if (result != numBytes) { + fprintf(stderr, "Could not read %lu bytes from file: %s\n", numBytes, strerror(errno)); + exit(1); + } +} + + +//Closes the currently opened file and opens the next file at its beginning. +void openNextFile() { + const int closeResult = fclose(file.filePointer); + if (closeResult != 0) { + fprintf(stderr, "Could not close file: %s\n", strerror(errno)); + exit(1); + } + + //Open next file + const size_t fileNameLength = strlen(file.name); + //We need to transfer carry if necessary (e.g. .z09 to .z10), otherwise just increase last digit + if (file.name[fileNameLength - 1] == '9') { + file.name[fileNameLength - 2] += 1; + file.name[fileNameLength - 1] = 0; + } else { + file.name[fileNameLength - 1] += 1; + } + initFileReader(file.name, 0UL); +} diff --git a/src/main.c b/src/main.c index edde11a..dab2ccc 100644 --- a/src/main.c +++ b/src/main.c @@ -13,10 +13,11 @@ int main(int argc, unsigned char *argv[]) { printf("Hello World!\n"); //Initialise file reader - initFileReader("1234", 0UL, 10UL); + initFileReader("1234", 0UL); //Skip header (30 bytes + additional length) - readBytes(30UL); + char* fileHeader = getBytes(30UL); + printf(fileHeader); //Initialise decryption (pass decryption keys)