comparison ater8/read_file.c @ 42:ff94d7fc5891

new program itt-ater-8
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 30 Aug 2024 19:02:42 +0000
parents ater/read_file.c@5405c1573027
children
comparison
equal deleted inserted replaced
41:50a72d4ff498 42:ff94d7fc5891
1 /*
2 * Here we implement the function that reads ETSI *.dec binary files
3 * which we've adopted as our TRAU-UL test input format for GSM-HR.
4 */
5
6 #include <sys/types.h>
7 #include <sys/file.h>
8 #include <sys/stat.h>
9 #include <stdint.h>
10 #include <stdbool.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include "../libhr/tw_gsmhr.h"
16
17 #include "read_file.h"
18
19 static int validate_frames(const int16_t *buf, unsigned nframes)
20 {
21 unsigned n;
22 int rc;
23 int16_t expect_taf;
24
25 for (n = 0; n < nframes; n++) {
26 rc = gsmhr_check_decoder_params(buf);
27 if (rc < 0)
28 return rc;
29 /* disallow BFI=2 non-ETSI extension */
30 if (buf[18] > 1)
31 return -1;
32 /* enforce TAF matching position */
33 if (n % 12 == 11)
34 expect_taf = 1;
35 else
36 expect_taf = 0;
37 if (buf[21] != expect_taf)
38 return -1;
39 buf += GSMHR_NUM_PARAMS_DEC;
40 }
41 return 0;
42 }
43
44 int read_binary_file(const char *filename, int16_t **bufret, unsigned *size_ret)
45 {
46 int fd, rc;
47 struct stat st;
48 int16_t *buf;
49 unsigned nframes;
50
51 fd = open(filename, O_RDONLY);
52 if (fd < 0) {
53 perror(filename);
54 return -1;
55 }
56 fstat(fd, &st);
57 if (!S_ISREG(st.st_mode)) {
58 close(fd);
59 fprintf(stderr, "error: %s is not a regular file\n", filename);
60 return -1;
61 }
62 if (!st.st_size) {
63 close(fd);
64 fprintf(stderr, "error: %s is an empty file\n", filename);
65 return -1;
66 }
67 if (st.st_size % 44) {
68 close(fd);
69 fprintf(stderr,
70 "error: size of %s is not a multiple of 44 bytes\n",
71 filename);
72 return -1;
73 }
74 buf = malloc(st.st_size);
75 if (!buf) {
76 close(fd);
77 fprintf(stderr, "unable to malloc buffer for %s\n", filename);
78 return -1;
79 }
80 read(fd, buf, st.st_size);
81 close(fd);
82 nframes = st.st_size / 44;
83
84 rc = validate_frames(buf, nframes);
85 if (rc < 0) {
86 free(buf);
87 fprintf(stderr,
88 "error: %s is not a valid GSM-HR dec file, or has wrong endian\n",
89 filename);
90 return -1;
91 }
92 *bufret = buf;
93 *size_ret = nframes;
94 return 0;
95 }