annotate tchtools/fc-vm2gsmx.c @ 921:74d284add54d

fc-fsio: guard against bogus readdir results from the target If the FFS being operated on contains SE K2x0 extended filenames, readdir will return strings that are bad for printing. We need to guard against this possibility, and also against possible other bogosity that could be sent by other alien firmwares.
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 31 Dec 2022 22:55:23 +0000
parents 546bf873ccc8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
170
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
904
5041bcb8140f tchtools/fc-vm2hex.c: update header comment for new situation
Mychaela Falconia <falcon@freecalypso.org>
parents: 902
diff changeset
2 * This utility converts old-fashioned (non-AMR) TCS211 voice memo files
905
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
3 * read out of FFS into Themyscira Wireless gsmx (extended-libgsm) format,
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
4 * allowing further decoding into playable speech with gsmfr-decode.
170
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 */
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <sys/types.h>
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdio.h>
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdlib.h>
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10
905
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
11 static char *infname;
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
12 static FILE *inf, *outf;
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
13 static u_long file_offset;
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
14
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
15 static const u_char bfi_marker[2] = {0xBF, 0x00};
170
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 static unsigned
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 get_word()
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 {
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 u_char b[2];
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 int i, c;
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 for (i = 0; i < 2; i++) {
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 c = getc(inf);
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 if (c < 0) {
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 fprintf(stderr, "error: premature EOF in %s\n",
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 infname);
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 exit(1);
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 }
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 b[i] = c;
905
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
31 file_offset++;
170
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 }
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 return((b[1] << 8) | b[0]);
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 }
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35
905
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
36 static void
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
37 read_dsp_bytes(bytes)
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
38 u_char *bytes;
170
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 {
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 int i, dp;
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 unsigned word;
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 dp = 0;
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 for (i = 0; i < 17; i++) {
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 word = get_word();
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 bytes[dp++] = word >> 8;
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 bytes[dp++] = word;
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 }
905
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
49 }
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
50
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
51 static void
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
52 convert_speech_sample()
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
53 {
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
54 u_char tidsp_bytes[34], libgsm_bytes[33];
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
55
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
56 read_dsp_bytes(tidsp_bytes);
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
57 gsm0610_tidsp_to_libgsm(tidsp_bytes, libgsm_bytes);
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
58 fwrite(libgsm_bytes, 1, 33, outf);
170
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 }
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 main(argc, argv)
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 char **argv;
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 {
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 unsigned first_word;
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65
905
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
66 if (argc != 3) {
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
67 fprintf(stderr, "usage: %s infile outfile\n", argv[0]);
170
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 exit(1);
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 }
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 infname = argv[1];
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 inf = fopen(infname, "r");
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 if (!inf) {
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 perror(infname);
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 exit(1);
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 }
905
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
76 outf = fopen(argv[2], "w");
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
77 if (!outf) {
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
78 perror(argv[2]);
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
79 exit(1);
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
80 }
170
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 for (;;) {
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 first_word = get_word();
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 if (first_word == 0xFBFF) /* SC_VM_END_MASK */
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 break;
905
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
85 if ((first_word & 0xB7FF) == 0x8400) {
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
86 /* skip dummy header words 1 and 2 */
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
87 get_word();
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
88 get_word();
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
89 /* process the actual speech or SID frame */
170
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 convert_speech_sample();
905
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
91 } else if (first_word == 0x0400) {
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
92 /* it's a skipped frame, aka BFI */
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
93 fwrite(bfi_marker, 1, 2, outf);
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
94 } else {
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
95 fprintf(stderr,
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
96 "error in %s at offset 0x%lx: invalid frame header word 0x%04X\n",
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
97 infname, file_offset, first_word);
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
98 exit(1);
170
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 }
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 }
905
546bf873ccc8 tchtools: new program fc-vm2gsmx
Mychaela Falconia <falcon@freecalypso.org>
parents: 904
diff changeset
101 fclose(outf);
170
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 exit(0);
a72bbc3ace09 fc-vm2hex utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 }