annotate tfo/find-is-hdr.c @ 72:afebef67e8d4 default tip

tmo/CSD-tests: document Experiment 7
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 28 Nov 2024 02:53:59 +0000
parents 98c0881c2af0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This program reads a binary file containing a G.711 PCM stream capture
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * and looks for an IS_Header pattern as defined in ETSI TS 101 504
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * (GSM 08.62) section A.1.2. The objective is to analyze PCM streams
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 * originating from extant commercial GSM network operators and see if
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 * they implement in-band TFO.
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 */
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <sys/types.h>
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <sys/file.h>
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <sys/stat.h>
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <sys/mman.h>
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <stdio.h>
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include <stdlib.h>
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 #include <unistd.h>
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 static char *pcmfile;
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 static size_t pcm_file_size;
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 static u_char *filemap;
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 static const u_char hdr_pattern[20] = {0, 1, 0, 1, 0, 1, 1, 0, 1, 0,
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 0, 1, 1, 0, 1, 0, 1, 0, 0, 1};
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 static void
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 mmap_pcm_file()
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 {
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 int fd;
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 struct stat st;
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 fd = open(pcmfile, O_RDONLY);
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 if (fd < 0) {
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 perror(pcmfile);
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 exit(1);
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 }
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 fstat(fd, &st);
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 if (!S_ISREG(st.st_mode)) {
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 fprintf(stderr, "error: %s is not a regular file\n", pcmfile);
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 exit(1);
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 }
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 pcm_file_size = st.st_size;
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 if (pcm_file_size < 320) {
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 fprintf(stderr, "error: %s is too short\n", pcmfile);
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 exit(1);
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 }
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 filemap = mmap(NULL, pcm_file_size, PROT_READ, MAP_PRIVATE, fd, 0L);
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 if (filemap == MAP_FAILED) {
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 perror("mmap");
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 exit(1);
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 }
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 close(fd);
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 }
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 static void
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 try_offset(offset)
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 size_t offset;
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 {
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 unsigned n;
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 for (n = 0; n < 20; n++) {
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 if ((filemap[offset + n * 16] & 1) != hdr_pattern[n])
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 return;
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 }
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 printf("Found IS_Header at offset %lu (0x%lx)\n", (u_long) offset,
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 (u_long) offset);
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 }
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 main(argc, argv)
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 char **argv;
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 {
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 size_t offset, endoff;
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 if (argc != 2) {
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 fprintf(stderr, "usage: %s pcm-capture-file\n", argv[0]);
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 exit(1);
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 }
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 pcmfile = argv[1];
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 mmap_pcm_file();
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 endoff = pcm_file_size - 320;
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 for (offset = 0; offset <= endoff; offset++)
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 try_offset(offset);
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 exit(0);
98c0881c2af0 tfo/find-is-hdr: move here from freecalypso-reveng
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 }