0

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;
}
Uccino _
  • 35
  • 1
  • 6
  • `out = buffer;` Does not do what you think it does, see: https://stackoverflow.com/questions/766893/how-do-i-modify-a-pointer-that-has-been-passed-into-a-function-in-c. Also your `xor_array` would cause `inp` to become a dangling pointer, due to `inp = out; free(out);` except it doesn't actually change the pointer for the same reason – UnholySheep Oct 12 '19 at 22:13
  • As in reddit, you didn't check the return value of malloc, but at least now you free it. You ignore return value of `fread` and don't check `buffer = malloc` for allocation errors. Also `inp = out` modifies a local variables. – KamilCuk Oct 12 '19 at 22:14
  • @UnholySheep the first point is good, but `inp = out` is pointless and harmless. But presumably the full code needs this array. – Weather Vane Oct 12 '19 at 22:15
  • You keep freeing your *out* parameter, even though you've assigned that same space to your *inp* parameter. So the system has no idea what space you've allocated. You should free *inp* before you assign *out* to it. – Hot Licks Oct 12 '19 at 22:20
  • @Hot Licks it's just a function argument, of no effect when the function ends. – Weather Vane Oct 12 '19 at 22:23

0 Answers0