e13531ab4a940b3cd914f4a7e4469b22beccc1a2
[openwrt/openwrt.git] / tools / firmware-utils / src / xorimage.c
1 /*
2 * xorimage.c - partially based on OpenWrt's addpattern.c
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdbool.h>
24 #include <stdint.h>
25 #include <unistd.h>
26 #include <sys/stat.h>
27
28 static char default_pattern[] = "12345678";
29 static int is_hex_pattern;
30
31
32 int xor_data(void *data, size_t len, const void *pattern, int p_len, int p_off)
33 {
34 const uint8_t *key = pattern;
35 uint8_t *d = data;
36
37 while (len--) {
38 *d ^= key[p_off];
39 d++;
40 p_off = (p_off + 1) % p_len;
41 }
42 return p_off;
43 }
44
45
46 void usage(void) __attribute__ (( __noreturn__ ));
47
48 void usage(void)
49 {
50 fprintf(stderr, "Usage: xorimage [-i infile] [-o outfile] [-p <pattern>] [-x]\n");
51 exit(EXIT_FAILURE);
52 }
53
54
55 int main(int argc, char **argv)
56 {
57 char buf[1024]; /* keep this at 1k or adjust garbage calc below */
58 FILE *in = stdin;
59 FILE *out = stdout;
60 char *ifn = NULL;
61 char *ofn = NULL;
62 const char *pattern = default_pattern;
63 char hex_pattern[128];
64 unsigned int hex_buf;
65 int c;
66 size_t n;
67 int p_len, p_off = 0;
68
69 while ((c = getopt(argc, argv, "i:o:p:xh")) != -1) {
70 switch (c) {
71 case 'i':
72 ifn = optarg;
73 break;
74 case 'o':
75 ofn = optarg;
76 break;
77 case 'p':
78 pattern = optarg;
79 break;
80 case 'x':
81 is_hex_pattern = true;
82 break;
83 case 'h':
84 default:
85 usage();
86 }
87 }
88
89 if (optind != argc || optind == 1) {
90 fprintf(stderr, "illegal arg \"%s\"\n", argv[optind]);
91 usage();
92 }
93
94 if (ifn && !(in = fopen(ifn, "r"))) {
95 fprintf(stderr, "can not open \"%s\" for reading\n", ifn);
96 usage();
97 }
98
99 if (ofn && !(out = fopen(ofn, "w"))) {
100 fprintf(stderr, "can not open \"%s\" for writing\n", ofn);
101 usage();
102 }
103
104 p_len = strlen(pattern);
105
106 if (p_len == 0) {
107 fprintf(stderr, "pattern cannot be empty\n");
108 usage();
109 }
110
111 if (is_hex_pattern) {
112 int i;
113
114 if ((p_len / 2) > sizeof(hex_pattern)) {
115 fprintf(stderr, "provided hex pattern is too long\n");
116 usage();
117 }
118
119 if (p_len % 2 != 0) {
120 fprintf(stderr, "the number of characters (hex) is incorrect\n");
121 usage();
122 }
123
124 for (i = 0; i < (p_len / 2); i++) {
125 if (sscanf(pattern + (i * 2), "%2x", &hex_buf) < 0) {
126 fprintf(stderr, "invalid hex digit around %d\n", i * 2);
127 usage();
128 }
129 hex_pattern[i] = (char)hex_buf;
130 }
131 }
132
133 while ((n = fread(buf, 1, sizeof(buf), in)) > 0) {
134 if (n < sizeof(buf)) {
135 if (ferror(in)) {
136 FREAD_ERROR:
137 fprintf(stderr, "fread error\n");
138 return EXIT_FAILURE;
139 }
140 }
141
142 if (is_hex_pattern) {
143 p_off = xor_data(buf, n, hex_pattern, (p_len / 2),
144 p_off);
145 } else {
146 p_off = xor_data(buf, n, pattern, p_len, p_off);
147 }
148
149 if (!fwrite(buf, n, 1, out)) {
150 FWRITE_ERROR:
151 fprintf(stderr, "fwrite error\n");
152 return EXIT_FAILURE;
153 }
154 }
155
156 if (ferror(in)) {
157 goto FREAD_ERROR;
158 }
159
160 if (fflush(out)) {
161 goto FWRITE_ERROR;
162 }
163
164 fclose(in);
165 fclose(out);
166
167 return EXIT_SUCCESS;
168 }