I'm learning C comming from C# so I'm sorry if there are any stupid mistakes I am making here.
I'm trying to read a file, XOR it and then do some other operations on it. However in the xor_array function the loop stops not even halfway through.
I'm inputting a file which is 1343488 bytes big. However when I try to run the program using GCC main.c -o main.exe && main.exe vscom.exe
The printf statement stops at roughly Iteration: 44927 of 1340408 which varies on how I place the code in the file.
I'm not sure what I did wrong here and how I'm supposed to fix it, could anyone give me a direction to look for?
Thank you for your time.
I've got the following code:
#include <stdio.h>
#include <stdlib.h>
int read_file_bytes(char file_path[256], char *out)
{
FILE *fileptr;
char *buffer;
long filelen;
fileptr = fopen(file_path, "rb");
if (fileptr)
{
fseek(fileptr, 0, SEEK_END);
filelen = ftell(fileptr);
rewind(fileptr);
buffer = (char *)malloc((filelen) * sizeof(char));
fread(buffer, filelen, 1, fileptr);
fclose(fileptr);
out = buffer;
return filelen;
}
return 0;
}
void xor_array(char *inp, int inplen)
{
char* out = (char*)malloc(inplen * sizeof(char));
for (int i = 0; i < inplen; i++)
{
// Originally used a key but that had the same output so dumbing it down till I can fix the problem
out[i] = inp[i] ^ 1;
printf("Iteration: %d of %d \n", i, inplen);
}
inp = out;
free(out);
}
// Argument you give is the path to another file
int main(int argc, char* argv[])
{
char* file_bytes; // Buffer to hold our file's bytes
int bytes_read; // Amount of bytes read
if(argc == 1)
{
return 1;
}
bytes_read = read_file_bytes(argv[1], file_bytes);
if(bytes_read == 0)
{
return 1;
}
xor_array(file_bytes, bytes_read);
return 0;
}