diff options
Diffstat (limited to 'misc')
| -rw-r--r-- | misc/kassemble.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/misc/kassemble.c b/misc/kassemble.c new file mode 100644 index 0000000..a818cc5 --- /dev/null +++ b/misc/kassemble.c @@ -0,0 +1,47 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <errno.h> + +int main(int argc, char **argv) { + if (argc != 4) { + fprintf(stderr, "usage: %s <in> <out> <image_size>\n", argv[0]); + return 1; + } + + const char *in_path = argv[1]; + const char *out_path = argv[2]; + char *end; + errno = 0; + unsigned long long size = strtoull(argv[3], &end, 16); + if (errno || *end != '\0') { + fprintf(stderr, "bad size: %s, invalid chars: %s\n", argv[3], end); + return 1; + } + + FILE *in = fopen(in_path, "rb"); + if (!in) { perror("fopen in"); return 1; } + + FILE *out = fopen(out_path, "wb"); + if (!out) { perror("fopen out"); fclose(in); return 1; } + + /* Copy in -> out */ + unsigned char buf[65536]; + size_t n; + while ((n = fread(buf, 1, sizeof buf, in)) > 0) { + if (fwrite(buf, 1, n, out) != n) { perror("fwrite"); return 1; } + } + if (ferror(in)) { perror("fread"); return 1; } + + /* Write size as u64 little-endian at offset 16 */ + unsigned char le[8]; + for (int i = 0; i < 8; i++) + le[i] = (unsigned char)(size >> (8 * i)); + + if (fseek(out, 16, SEEK_SET) != 0) { perror("fseek"); return 1; } + if (fwrite(le, 1, 8, out) != 8) { perror("fwrite size"); return 1; } + + fclose(in); + if (fclose(out) != 0) { perror("fclose out"); return 1; } + return 0; +} |
