summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLouis Burda <quent.burda@gmail.com>2022-04-29 17:28:25 +0200
committerLouis Burda <quent.burda@gmail.com>2022-04-29 17:28:25 +0200
commitf48499265ba2c6f39a1c3f35955aaa3b72ecaaa8 (patch)
treeddf58bfb0164c07f036ac275869f900836f0f1fa
downloadsplice-f48499265ba2c6f39a1c3f35955aaa3b72ecaaa8.tar.gz
splice-f48499265ba2c6f39a1c3f35955aaa3b72ecaaa8.zip
Simple binary patch
-rw-r--r--.gitignore1
-rw-r--r--Makefile7
-rw-r--r--fwpatch.c60
3 files changed, 68 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..dcc5283
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+fwpatch
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..12e0558
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,7 @@
+CFLAGS = -g
+
+.PHONY: all
+
+all: fwpatch
+
+fwpatch: fwpatch.c
diff --git a/fwpatch.c b/fwpatch.c
new file mode 100644
index 0000000..c380583
--- /dev/null
+++ b/fwpatch.c
@@ -0,0 +1,60 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <stdint.h>
+
+const char usage[] = "USAGE: fwpatch TARGET OFFSET FILE";
+
+void
+die(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+
+ exit(1);
+}
+
+int
+main(int argc, const char **argv)
+{
+ size_t addr, size;
+ FILE *file;
+ char *end, *data;
+
+ if (argc != 4)
+ die(usage);
+
+ addr = strtoull(argv[2], &end, 0);
+ if (*end) die("Invalid offset: %s", argv[2]);
+
+ file = fopen(argv[3], "rb");
+ if (!file) die("Failed to open: %s", argv[3]);
+
+ fseek(file, 0, SEEK_END);
+ size = ftell(file);
+
+ data = malloc(size);
+ if (!data) die("Out of memory\n");
+
+ fseek(file, 0, SEEK_SET);
+ if (fread(data, 1, size, file) != size)
+ die("Read failed\n");
+
+ fclose(file);
+
+ file = fopen(argv[1], "r+b");
+ if (!file) die("Failed to open: %s", argv[1]);
+
+ fseek(file, addr, SEEK_SET);
+ if (ftell(file) != addr) die("Invalid offset\n");
+
+ if (fwrite(data, 1, size, file) != size)
+ die("Write failed\n");
+
+ fclose(file);
+
+ free(data);
+}