comparison v110/v110-dump8.c @ 39:957c20852c7c

new program v110-dump8
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 12 Sep 2024 21:38:22 +0000
parents trau-decode/trau-hr-dump.c@9bcdb091c24d
children 796cc2d94204
comparison
equal deleted inserted replaced
38:d7674c80426c 39:957c20852c7c
1 /*
2 * This program reads a 64 kbit/s timeslot recording file, examines it
3 * as V.110 with 8 kbit/s intermediate rate, locates 80-bit V.110 frames
4 * and dumps them in a mostly raw form.
5 */
6
7 #include <sys/types.h>
8 #include <sys/file.h>
9 #include <sys/stat.h>
10 #include <sys/mman.h>
11 #include <stdio.h>
12 #include <stdint.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <strings.h>
16 #include <unistd.h>
17
18 static const uint8_t *filebuf;
19 static unsigned total_size;
20
21 static void
22 read_ts_file(filename)
23 char *filename;
24 {
25 int fd;
26 struct stat st;
27
28 fd = open(filename, O_RDONLY);
29 if (fd < 0) {
30 perror(filename);
31 exit(1);
32 }
33 fstat(fd, &st);
34 if (!S_ISREG(st.st_mode)) {
35 fprintf(stderr, "error: %s is not a regular file\n", filename);
36 exit(1);
37 }
38 total_size = st.st_size;
39 if (total_size < 80) {
40 fprintf(stderr, "error: %s is too short\n", filename);
41 exit(1);
42 }
43 filebuf = mmap(NULL, total_size, PROT_READ, MAP_PRIVATE, fd, 0);
44 if (filebuf == MAP_FAILED) {
45 perror("mmap");
46 exit(1);
47 }
48 close(fd);
49 }
50
51 static int
52 check_sync(pos)
53 unsigned pos;
54 {
55 const uint8_t *cand = filebuf + pos;
56 unsigned n;
57
58 for (n = 0; n < 8; n++) {
59 if (cand[n] & 0x80)
60 return 0;
61 }
62 for (n = 1; n < 10; n++) {
63 if (!(cand[n * 8] & 0x80))
64 return 0;
65 }
66 return 1;
67 }
68
69 static unsigned
70 extract_data_byte(pos)
71 unsigned pos;
72 {
73 const uint8_t *src = filebuf + pos;
74 unsigned accum;
75 unsigned n;
76
77 accum = 0;
78 for (n = 0; n < 8; n++) {
79 accum <<= 1;
80 if (*src & 0x80)
81 accum |= 1;
82 src++;
83 }
84 return accum;
85 }
86
87 static void
88 process_frame(pos)
89 unsigned pos;
90 {
91 unsigned nb;
92
93 printf("Frame at 0x%x:", pos);
94 pos += 8;
95 for (nb = 0; nb < 9; nb++) {
96 printf(" %02X", extract_data_byte(pos));
97 pos += 8;
98 }
99 putchar('\n');
100 }
101
102 static void
103 process_filebuf()
104 {
105 unsigned p, endp;
106 int sync = 0, match;
107
108 endp = total_size - 80;
109 for (p = 0; p < endp; ) {
110 match = check_sync(p);
111 if (match != sync) {
112 printf("# %s frame sync at file offset 0x%x\n",
113 match ? "Acquired" : "Lost", p);
114 }
115 if (match) {
116 process_frame(p);
117 p += 80;
118 } else
119 p++;
120 sync = match;
121 }
122 }
123
124 main(argc, argv)
125 char **argv;
126 {
127 if (argc != 2) {
128 fprintf(stderr, "usage: %s binfile\n", argv[0]);
129 exit(1);
130 }
131 read_ts_file(argv[1]);
132 process_filebuf();
133 exit(0);
134 }