firmware-utils: honor env SOURCE_DATE_EPOCH
[oweals/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 <stdint.h>
24 #include <unistd.h>
25 #include <sys/stat.h>
26
27 static char default_pattern[] = "12345678";
28
29
30 int xor_data(uint8_t *data, size_t len, const uint8_t *pattern, int p_len, int p_off)
31 {
32         int offset = p_off;
33         while (len--) {
34                 *data ^= pattern[offset];
35                 data++;
36                 offset = (offset + 1) % p_len;
37         }
38         return offset;
39 }
40
41
42 void usage(void) __attribute__ (( __noreturn__ ));
43
44 void usage(void)
45 {
46         fprintf(stderr, "Usage: xorimage [-i infile] [-o outfile] [-p <pattern>]\n");
47         exit(EXIT_FAILURE);
48 }
49
50
51 int main(int argc, char **argv)
52 {
53         char buf[1024]; /* keep this at 1k or adjust garbage calc below */
54         FILE *in = stdin;
55         FILE *out = stdout;
56         char *ifn = NULL;
57         char *ofn = NULL;
58         const char *pattern = default_pattern;
59         int c;
60         int v0, v1, v2;
61         size_t n;
62         int p_len, p_off = 0;
63
64         while ((c = getopt(argc, argv, "i:o:p:h")) != -1) {
65                 switch (c) {
66                         case 'i':
67                                 ifn = optarg;
68                                 break;
69                         case 'o':
70                                 ofn = optarg;
71                                 break;
72                         case 'p':
73                                 pattern = optarg;
74                                 break;
75                         case 'h':
76                         default:
77                                 usage();
78                 }
79         }
80
81         if (optind != argc || optind == 1) {
82                 fprintf(stderr, "illegal arg \"%s\"\n", argv[optind]);
83                 usage();
84         }
85
86         if (ifn && !(in = fopen(ifn, "r"))) {
87                 fprintf(stderr, "can not open \"%s\" for reading\n", ifn);
88                 usage();
89         }
90
91         if (ofn && !(out = fopen(ofn, "w"))) {
92                 fprintf(stderr, "can not open \"%s\" for writing\n", ofn);
93                 usage();
94         }
95
96         p_len = strlen(pattern);
97
98         if (p_len == 0) {
99                 fprintf(stderr, "pattern cannot be empty\n");
100                 usage();
101         }
102
103
104         while ((n = fread(buf, 1, sizeof(buf), in)) > 0) {
105                 if (n < sizeof(buf)) {
106                         if (ferror(in)) {
107                         FREAD_ERROR:
108                                 fprintf(stderr, "fread error\n");
109                                 return EXIT_FAILURE;
110                         }
111                 }
112
113                 p_off = xor_data(buf, n, pattern, p_len, p_off);
114
115                 if (!fwrite(buf, n, 1, out)) {
116                 FWRITE_ERROR:
117                         fprintf(stderr, "fwrite error\n");
118                         return EXIT_FAILURE;
119                 }
120         }
121
122         if (ferror(in)) {
123                 goto FREAD_ERROR;
124         }
125
126         if (fflush(out)) {
127                 goto FWRITE_ERROR;
128         }
129
130         fclose(in);
131         fclose(out);
132
133         return EXIT_SUCCESS;
134 }