MCPcopy Create free account
hub / github.com/GJDuck/e9patch / emitBinary

Function emitBinary

src/e9patch/e9emit.cpp:39–59  ·  view source on GitHub ↗

* Emit the complete patched executable binary file. */

Source from the content-addressed store, hash-verified

37 * Emit the complete patched executable binary file.
38 */
39void emitBinary(const char *filename, const uint8_t *bin, size_t len)
40{
41 FILE *out = fopen(filename, "w");
42 if (out == nullptr)
43 error("failed to open output file \"%s\" for writing: %s", filename,
44 strerror(errno));
45 if (fwrite(bin, sizeof(uint8_t), len, out) != len)
46 error("failed to write output to file \"%s\": %s", filename,
47 strerror(errno));
48 if (fchmod(fileno(out), S_IRUSR | S_IWUSR | S_IXUSR |
49 S_IRGRP | S_IWGRP | S_IXGRP |
50 S_IROTH | S_IWOTH | S_IXOTH))
51 {
52 // This is a warning since the output might be /dev/null
53 warning("failed to set execute permission for output file \"%s\": %s",
54 filename, strerror(errno));
55 }
56 if (fclose(out) < 0)
57 error("failed to close output file \"%s\": %s", filename,
58 strerror(errno));
59}
60
61/*
62 * Emit a binary patch. Implements (an approximation of) the pipeline:

Callers 1

parseEmitFunction · 0.85

Calls 7

fopenFunction · 0.85
errorFunction · 0.85
strerrorFunction · 0.85
fwriteFunction · 0.85
filenoFunction · 0.85
warningFunction · 0.85
fcloseFunction · 0.85

Tested by

no test coverage detected