annotate miscprog/rfcap-grep.c @ 167:c25367bb7656

objgrep: written, compiles
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Fri, 04 Jul 2014 00:54:33 +0000
parents 597143ba1c37
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
64
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
1 /*
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
2 * This utility performs a memmem() binary "grep", checking to see if a given
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
3 * binary file (mokoN firmware image) contains a particular binary "string"
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
4 * of 16 bytes: namely, the 16 bytes found in the "standard" /gsm/com/rfcap
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
5 * file on GTA0x modems.
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
6 */
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
7
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
8 #define _GNU_SOURCE
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
9 #include <sys/types.h>
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
10 #include <sys/file.h>
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
11 #include <sys/stat.h>
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
12 #include <stdio.h>
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
13 #include <string.h>
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
14 #include <stdlib.h>
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
15 #include <unistd.h>
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
16
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
17 u_char needle[16] = {0x00, 0x1F, 0x41, 0x14, 0x00, 0x00, 0x00, 0x00,
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
18 0x50, 0x00, 0x00, 0xA5, 0x05, 0x00, 0xC0, 0x00};
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
19 u_char *haystack;
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
20 size_t haystack_size;
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
21
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
22 read_file(filename)
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
23 char *filename;
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
24 {
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
25 int fd;
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
26 struct stat st;
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
27
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
28 fd = open(filename, O_RDONLY);
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
29 if (fd < 0) {
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
30 perror(filename);
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
31 exit(1);
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
32 }
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
33 fstat(fd, &st);
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
34 if (!S_ISREG(st.st_mode)) {
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
35 fprintf(stderr, "error: %s is not a regular file\n", filename);
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
36 exit(1);
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
37 }
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
38 haystack_size = st.st_size;
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
39 haystack = malloc(haystack_size);
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
40 if (!haystack) {
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
41 fprintf(stderr, "unable to malloc buffer for %s\n", filename);
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
42 exit(1);
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
43 }
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
44 read(fd, haystack, haystack_size);
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
45 close(fd);
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
46 }
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
47
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
48 main(argc, argv)
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
49 char **argv;
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
50 {
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
51 u_char *result;
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
52
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
53 if (argc != 2) {
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
54 fprintf(stderr, "usage: %s mokoN.bin\n", argv[0]);
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
55 exit(1);
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
56 }
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
57 read_file(argv[1]);
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
58 result = memmem(haystack, haystack_size, needle, sizeof needle);
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
59 if (result)
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
60 printf("Found the rfcap bytes at offset 0x%x\n",
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
61 result - haystack);
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
62 else
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
63 printf("rfcap bytes not found in this image\n");
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
64 exit(0);
b8753e705e1a rfcap-grep.c hack-utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
65 }