comparison v110/v110-dump16.c @ 40:796cc2d94204

new program v110-dump16
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 12 Sep 2024 21:55:32 +0000
parents v110/v110-dump8.c@957c20852c7c
children
comparison
equal deleted inserted replaced
39:957c20852c7c 40:796cc2d94204
1 /*
2 * This program reads a 64 kbit/s timeslot recording file, examines it
3 * as V.110 with 16 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 < 40) {
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 < 4; n++) {
59 if (cand[n] & 0xC0)
60 return 0;
61 }
62 for (n = 1; n < 10; n++) {
63 if (!(cand[n * 4] & 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 < 4; n++) {
79 accum <<= 1;
80 if (*src & 0x80)
81 accum |= 1;
82 accum <<= 1;
83 if (*src & 0x40)
84 accum |= 1;
85 src++;
86 }
87 return accum;
88 }
89
90 static void
91 process_frame(pos)
92 unsigned pos;
93 {
94 unsigned nb;
95
96 printf("Frame at 0x%x:", pos);
97 pos += 4;
98 for (nb = 0; nb < 9; nb++) {
99 printf(" %02X", extract_data_byte(pos));
100 pos += 4;
101 }
102 putchar('\n');
103 }
104
105 static void
106 process_filebuf()
107 {
108 unsigned p, endp;
109 int sync = 0, match;
110
111 endp = total_size - 40;
112 for (p = 0; p <= endp; ) {
113 match = check_sync(p);
114 if (match != sync) {
115 printf("# %s frame sync at file offset 0x%x\n",
116 match ? "Acquired" : "Lost", p);
117 }
118 if (match) {
119 process_frame(p);
120 p += 40;
121 } else
122 p++;
123 sync = match;
124 }
125 }
126
127 main(argc, argv)
128 char **argv;
129 {
130 if (argc != 2) {
131 fprintf(stderr, "usage: %s binfile\n", argv[0]);
132 exit(1);
133 }
134 read_ts_file(argv[1]);
135 process_filebuf();
136 exit(0);
137 }