graphitemaster / incbin

Include binary files in C/C++
The Unlicense
966 stars 90 forks source link

Incbin does not supply entirety of file when it contains null bytes #52

Closed ewancg closed 2 years ago

ewancg commented 2 years ago

I am trying to make a resource system where given files are archived into a tarball, which is linked into the executable (where incbin comes into play). On paper this is not a problem, but it seems that with certain filetypes incbin is not including the entirety of the file. Example:

#define INCBIN_PREFIX r_
#include "incbin.h"
#include <fstream>
INCBIN(grc, "./test.tar");

int main(int argc, char *argv[]) {
    std::ofstream out("./test2.tar");
    out << r_grcData;
    out.close();
}

C version, to ensure it's not a C++ thing;


#define INCBIN_PREFIX r_
#include "incbin.h"
#include <stdio.h>
INCBIN(grc, "./test.tar");

int main(int argc, char *argv[]) {
    const char *data = (const char *)r_grcData;
    FILE *fp = fopen("./test2.tar", "wb");
    fputs(data, fp);
    fclose(fp);
}

test.tar is 1536 bytes. test2.tar is 8 bytes.

A hex editor tells me that the first null character in test.tar is on the ninth byte (the cutoff of test2.tar). image image

This happens with a PNG file, where the cutoff happens at its first null byte. image

I'm not entirely sure if this is user error on my part or incbin not knowing when to stop including file. It's pretty rare to find a file with no null bytes in it, maybe it is misinterpreting the null character as the end of the file.

This happens both with gcc/g++ 11.1.0 and clang/clang++ 12.0.1

ewancg commented 2 years ago

To clarify, my implementation(s) will make this cutoff regardless of whether the included file has been shortened because of my char array handling. Either way, the difference is also reflected in filesize of the executable (I've compared the included content in the executable as well, that also gets cut off).

Edit: Nevermind. I'm an idiot. Have a good day