Holger Hans Peter Freyther | 65d67dc | 2009-12-23 12:52:30 +0100 | [diff] [blame] | 1 | /* Routines for parsing an ipacces SDP firmware file */ |
| 2 | |
| 3 | /* (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | * |
| 20 | */ |
| 21 | |
Holger Hans Peter Freyther | bf2bdc6 | 2009-12-23 13:09:27 +0100 | [diff] [blame^] | 22 | #include <arpa/inet.h> |
Holger Hans Peter Freyther | 65d67dc | 2009-12-23 12:52:30 +0100 | [diff] [blame] | 23 | #include <sys/types.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <unistd.h> |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | |
Holger Hans Peter Freyther | e0d2ff4 | 2009-12-23 13:03:12 +0100 | [diff] [blame] | 32 | /* more magic, the second "int" in the header */ |
| 33 | static char more_magic[] = { 0x10, 0x02, 0x00, 0x0 }; |
| 34 | |
Holger Hans Peter Freyther | 65d67dc | 2009-12-23 12:52:30 +0100 | [diff] [blame] | 35 | |
| 36 | static void analyze_file(int fd) |
| 37 | { |
| 38 | char buf[4096]; |
| 39 | int rc; |
Holger Hans Peter Freyther | bf2bdc6 | 2009-12-23 13:09:27 +0100 | [diff] [blame^] | 40 | unsigned int absolute_size; |
Holger Hans Peter Freyther | 65d67dc | 2009-12-23 12:52:30 +0100 | [diff] [blame] | 41 | |
| 42 | rc = read(fd, buf, 4); |
| 43 | if (rc <= 0) { |
| 44 | fprintf(stderr, "Not enough space for the header.\n"); |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | if (strcmp(buf, " SDP") != 0) { |
| 49 | fprintf(stderr, "Wrong magic number at the beginning of the file.\n"); |
| 50 | return; |
| 51 | } |
| 52 | |
Holger Hans Peter Freyther | e0d2ff4 | 2009-12-23 13:03:12 +0100 | [diff] [blame] | 53 | rc = read(fd, buf, 4); |
| 54 | if (rc <= 0) { |
| 55 | fprintf(stderr, "Not enough space for the more_magic.\n"); |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | if (strncmp(buf, more_magic, 4) != 0) { |
| 60 | fprintf(stderr, "The more magic is not right.\n"); |
| 61 | return; |
| 62 | } |
| 63 | |
Holger Hans Peter Freyther | bf2bdc6 | 2009-12-23 13:09:27 +0100 | [diff] [blame^] | 64 | rc = read(fd, buf, 4); |
| 65 | if (rc <= 0) { |
| 66 | fprintf(stderr, "Trying to read the header length failed.\n"); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | memcpy(&absolute_size, &buf[0], 4); |
| 71 | absolute_size = ntohl(absolute_size); |
| 72 | |
Holger Hans Peter Freyther | 65d67dc | 2009-12-23 12:52:30 +0100 | [diff] [blame] | 73 | printf("Printing header information:\n"); |
Holger Hans Peter Freyther | bf2bdc6 | 2009-12-23 13:09:27 +0100 | [diff] [blame^] | 74 | printf("The header is %u bytes long\n", absolute_size); |
Holger Hans Peter Freyther | 65d67dc | 2009-12-23 12:52:30 +0100 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | int main(int argc, char** argv) |
| 78 | { |
| 79 | int i, fd; |
| 80 | |
| 81 | for (i = 1; i < argc; ++i) { |
| 82 | printf("Opening possible firmware '%s'\n", argv[i]); |
| 83 | fd = open(argv[i], O_RDONLY); |
| 84 | if (!fd) { |
| 85 | perror("nada"); |
| 86 | continue; |
| 87 | } |
| 88 | |
| 89 | analyze_file(fd); |
| 90 | } |
| 91 | |
| 92 | return EXIT_SUCCESS; |
| 93 | } |