soundsmith/extract/trimwb.c
2017-08-21 11:12:33 -07:00

48 lines
1.0 KiB
C

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <memory.h>
static inline uint16_t r16(uint8_t *p) {
uint16_t r = *p++;
r |= *p++ << 8;
return r;
}
int main(int argc, char **argv) {
if (argc != 4) {
fprintf(stderr, "Usage: %s <file> <start> <outfile>\n", argv[0]);
fprintf(stderr, " start is in hex\n");
return -1;
}
FILE *f = fopen(argv[1], "rb");
if (!f) {
fprintf(stderr, "Couldn't open %s\n", argv[1]);
return -1;
}
fseek(f, 0, SEEK_END);
int len = ftell(f);
fseek(f, 0, SEEK_SET);
uint8_t *data = malloc(len);
fread(data, 1, len, f);
fclose(f);
uint32_t start = strtol(argv[2], NULL, 16);
if (start > len - 4) {
fprintf(stderr, "Start address is too large\n");
return -1;
}
uint16_t numInst = r16(data + start) & 0xff;
uint32_t fileLen = numInst * 0x5c + 0x1005e + 32;
f = fopen(argv[3], "wb");
if (!f) {
fprintf(stderr, "Couldn't create %s\n", argv[3]);
return -1;
}
fwrite(data + start, 1, fileLen, f);
fclose(f);
}