comparison miscprog/rfcap-grep.c @ 129:597143ba1c37

miscellaneous C programs moved out of the top level directory
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sun, 06 Apr 2014 20:20:39 +0000
parents rfcap-grep.c@b8753e705e1a
children
comparison
equal deleted inserted replaced
128:03f8a618689e 129:597143ba1c37
1 /*
2 * This utility performs a memmem() binary "grep", checking to see if a given
3 * binary file (mokoN firmware image) contains a particular binary "string"
4 * of 16 bytes: namely, the 16 bytes found in the "standard" /gsm/com/rfcap
5 * file on GTA0x modems.
6 */
7
8 #define _GNU_SOURCE
9 #include <sys/types.h>
10 #include <sys/file.h>
11 #include <sys/stat.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16
17 u_char needle[16] = {0x00, 0x1F, 0x41, 0x14, 0x00, 0x00, 0x00, 0x00,
18 0x50, 0x00, 0x00, 0xA5, 0x05, 0x00, 0xC0, 0x00};
19 u_char *haystack;
20 size_t haystack_size;
21
22 read_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 haystack_size = st.st_size;
39 haystack = malloc(haystack_size);
40 if (!haystack) {
41 fprintf(stderr, "unable to malloc buffer for %s\n", filename);
42 exit(1);
43 }
44 read(fd, haystack, haystack_size);
45 close(fd);
46 }
47
48 main(argc, argv)
49 char **argv;
50 {
51 u_char *result;
52
53 if (argc != 2) {
54 fprintf(stderr, "usage: %s mokoN.bin\n", argv[0]);
55 exit(1);
56 }
57 read_file(argv[1]);
58 result = memmem(haystack, haystack_size, needle, sizeof needle);
59 if (result)
60 printf("Found the rfcap bytes at offset 0x%x\n",
61 result - haystack);
62 else
63 printf("rfcap bytes not found in this image\n");
64 exit(0);
65 }