OpenWrt – Diff between revs 2 and 3

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