diff options
| author | Louis Burda <quent.burda@gmail.com> | 2023-02-15 19:21:02 +0100 |
|---|---|---|
| committer | Louis Burda <quent.burda@gmail.com> | 2023-02-15 19:21:02 +0100 |
| commit | 03ba9c7da68c3fadb64ca32dbc9258919b7ac19a (patch) | |
| tree | 74f26af5c8b39512c6ff7379e804e72b363062f0 | |
| parent | f48499265ba2c6f39a1c3f35955aaa3b72ecaaa8 (diff) | |
| download | splice-03ba9c7da68c3fadb64ca32dbc9258919b7ac19a.tar.gz splice-03ba9c7da68c3fadb64ca32dbc9258919b7ac19a.zip | |
Rename and clean up
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | Makefile | 9 | ||||
| -rw-r--r-- | binpatch.c | 54 | ||||
| -rw-r--r-- | fwpatch.c | 60 |
4 files changed, 61 insertions, 64 deletions
@@ -1 +1 @@ -fwpatch +binpatch @@ -1,7 +1,10 @@ CFLAGS = -g -.PHONY: all +all: binpatch -all: fwpatch +clean: + rm -f binpatch -fwpatch: fwpatch.c +binpatch: binpatch.c + +.PHONY: all clean diff --git a/binpatch.c b/binpatch.c new file mode 100644 index 0000000..9df9bc2 --- /dev/null +++ b/binpatch.c @@ -0,0 +1,54 @@ +#include <err.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> + +int +main(int argc, const char **argv) +{ + ssize_t addr, size; + char *end, *data; + FILE *file; + int ret; + + if (argc != 4) { + printf("USAGE: fwpatch TARGET OFFSET FILE\n"); + return 0; + } + + addr = strtoull(argv[2], &end, 0); + if (*end) err(1, "strtoull %s", argv[2]); + + file = fopen(argv[3], "rb"); + if (!file) err(1, "fopen %s", argv[3]); + + ret = fseek(file, 0, SEEK_END); + if (ret < 0) err(1, "fseek %s", argv[3]); + + size = ftell(file); + if (size < 0) err(1, "ftell %s", argv[3]); + + data = malloc(size); + if (!data) err(1, "malloc %lu", size); + + ret = fseek(file, 0, SEEK_SET); + if (ret < 0) err(1, "freek %s", argv[3]); + + if (fread(data, 1, size, file) != size) + errx(1, "fread %lu", size); + + fclose(file); + + file = fopen(argv[1], "r+b"); + if (!file) err(1, "fopen %s", argv[1]); + + ret = fseek(file, addr, SEEK_SET); + if (ret < 0) err(1, "fseek %s", argv[1]); + + if (fwrite(data, 1, size, file) != size) + errx(1, "fwrite %lu", size); + + fclose(file); + + free(data); +} diff --git a/fwpatch.c b/fwpatch.c deleted file mode 100644 index c380583..0000000 --- a/fwpatch.c +++ /dev/null @@ -1,60 +0,0 @@ -#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); -} |
