comparison trau-decode/trau-hr-dump-hex.c @ 75:b518ab15b518

trau-decode: new program trau-hr-dump-hex
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 08 Feb 2025 03:38:54 +0000
parents
children
comparison
equal deleted inserted replaced
74:e78c6b1ecb91 75:b518ab15b518
1 /*
2 * This program reads a line-based text file in an ad hoc hex format
3 * where each line represents a TRAU-8k frame. These TRAU-8k frames
4 * are then decoded.
5 */
6
7 #include <ctype.h>
8 #include <stdio.h>
9 #include <stdint.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <strings.h>
13
14 static int
15 decode_hex_digit(c)
16 {
17 if (isdigit(c))
18 return c - '0';
19 else if (isupper(c))
20 return c - 'A' + 10;
21 else
22 return c - 'a' + 10;
23 }
24
25 static void
26 hex2bits(hex, bits)
27 char *hex;
28 uint8_t *bits;
29 {
30 unsigned n, m, x;
31
32 for (n = 0; n < 40; n++) {
33 x = decode_hex_digit(hex[n]);
34 for (m = 8; m; m >>= 1) {
35 if (x & m)
36 *bits++ = 1;
37 else
38 *bits++ = 0;
39 }
40 }
41 }
42
43 static void
44 process_record(hex_frame, lineno)
45 char *hex_frame;
46 {
47 uint8_t frame_bits[160];
48
49 printf("line %d: %s\n", lineno, hex_frame);
50 hex2bits(hex_frame, frame_bits);
51 print_gsmhr_frame(frame_bits);
52 }
53
54 static void
55 process_line(linebuf, filename, lineno)
56 char *linebuf, *filename;
57 {
58 char *cp, *np;
59 unsigned n;
60
61 for (cp = linebuf; isspace(*cp); cp++)
62 ;
63 if (*cp == '\0' || *cp == '#')
64 return;
65 np = cp;
66 for (n = 0; n < 40; n++) {
67 if (!isxdigit(*cp))
68 goto inv;
69 cp++;
70 }
71 if (*cp) {
72 if (!isspace(*cp))
73 goto inv;
74 *cp++ = '\0';
75 }
76 while (isspace(*cp))
77 cp++;
78 if (*cp != '\0' && *cp != '#')
79 goto inv;
80 process_record(np, lineno);
81 return;
82
83 inv: fprintf(stderr, "%s line %d: invalid syntax\n", filename, lineno);
84 exit(1);
85 }
86
87 main(argc, argv)
88 char **argv;
89 {
90 FILE *inf;
91 char linebuf[128];
92 int lineno;
93
94 if (argc != 2) {
95 fprintf(stderr, "usage: %s hex-file\n", argv[0]);
96 exit(1);
97 }
98 inf = fopen(argv[1], "r");
99 if (!inf) {
100 perror(argv[1]);
101 exit(1);
102 }
103 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++)
104 process_line(linebuf, argv[1], lineno);
105 exit(0);
106 }