FreeCalypso > hg > ice1-trau-tester
diff ater/read_file.c @ 51:db39e8855f3d
ater: implement play-d144
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Wed, 25 Sep 2024 05:53:38 +0000 |
parents | 5405c1573027 |
children |
line wrap: on
line diff
--- a/ater/read_file.c Tue Sep 24 06:15:40 2024 +0000 +++ b/ater/read_file.c Wed Sep 25 05:53:38 2024 +0000 @@ -96,3 +96,68 @@ *size_ret = nframes; return 0; } + +static int check_magic_d144(const uint8_t *buf, unsigned nframes) +{ + unsigned n; + + for (n = 0; n < nframes; n++) { + if (*buf != 0xD4) + return -1; + buf += 38; + } + return 0; +} + +int read_binary_file_d144(const char *filename, uint8_t **bufret, + unsigned *size_ret) +{ + int fd, rc; + struct stat st; + uint8_t *buf; + unsigned nframes; + + fd = open(filename, O_RDONLY); + if (fd < 0) { + perror(filename); + return -1; + } + fstat(fd, &st); + if (!S_ISREG(st.st_mode)) { + close(fd); + fprintf(stderr, "error: %s is not a regular file\n", filename); + return -1; + } + if (!st.st_size) { + close(fd); + fprintf(stderr, "error: %s is an empty file\n", filename); + return -1; + } + if (st.st_size % 38) { + close(fd); + fprintf(stderr, + "error: size of %s is not a multiple of 38 bytes\n", + filename); + return -1; + } + buf = malloc(st.st_size); + if (!buf) { + close(fd); + fprintf(stderr, "unable to malloc buffer for %s\n", filename); + return -1; + } + read(fd, buf, st.st_size); + close(fd); + nframes = st.st_size / 38; + + rc = check_magic_d144(buf, nframes); + if (rc < 0) { + free(buf); + fprintf(stderr, "error: %s is not a valid D144 test file\n", + filename); + return -1; + } + *bufret = buf; + *size_ret = nframes; + return 0; +}