tools/mkfwimage2: remove 256 length limit for partition images
[openwrt/openwrt.git] / tools / firmware-utils / src / buffalo-tftp.c
1 /*
2 * Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
7 *
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <stdint.h>
13 #include <string.h>
14 #include <libgen.h>
15 #include <getopt.h> /* for getopt() */
16 #include <stdarg.h>
17
18 #include "buffalo-lib.h"
19
20 #define ERR(fmt, args...) do { \
21 fflush(0); \
22 fprintf(stderr, "[%s] *** error: " fmt "\n", \
23 progname, ## args ); \
24 } while (0)
25
26 static char *progname;
27 static char *ifname;
28 static char *ofname;
29 static int do_decrypt;
30
31 void usage(int status)
32 {
33 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
34
35 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
36 fprintf(stream,
37 "\n"
38 "Options:\n"
39 " -d decrypt instead of encrypt\n"
40 " -i <file> read input from the file <file>\n"
41 " -o <file> write output to the file <file>\n"
42 " -h show this screen\n"
43 );
44
45 exit(status);
46 }
47
48 static const unsigned char *crypt_key1 = (unsigned char *)
49 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
50 static const unsigned char *crypt_key2 = (unsigned char *)
51 "XYZ0123hijklmnopqABCDEFGHrstuvabcdefgwxyzIJKLMSTUVW456789NOPQR";
52
53 static void crypt_header(unsigned char *buf, ssize_t len,
54 const unsigned char *key1, const unsigned char *key2)
55 {
56 ssize_t i;
57
58 for (i = 0; i < len; i++) {
59 unsigned int j;
60
61 for (j = 0; key1[j]; j++)
62 if (buf[i] == key1[j]) {
63 buf[i] = key2[j];
64 break;
65 }
66 }
67 }
68
69 static int crypt_file(void)
70 {
71 unsigned char *buf = NULL;
72 ssize_t src_len;
73 int err;
74 int ret = -1;
75
76 src_len = get_file_size(ifname);
77 if (src_len < 0) {
78 ERR("unable to get size of '%s'", ifname);
79 goto out;
80 }
81
82 buf = malloc(src_len);
83 if (buf == NULL) {
84 ERR("no memory for the buffer");
85 goto out;
86 }
87
88 err = read_file_to_buf(ifname, buf, src_len);
89 if (err) {
90 ERR("unable to read from file '%s'", ifname);
91 goto out;
92 }
93
94 if (do_decrypt)
95 crypt_header(buf, 512, crypt_key2, crypt_key1);
96 else
97 crypt_header(buf, 512, crypt_key1, crypt_key2);
98
99 err = write_buf_to_file(ofname, buf, src_len);
100 if (err) {
101 ERR("unable to write to file '%s'", ofname);
102 goto out;
103 }
104
105 ret = 0;
106
107 out:
108 free(buf);
109 return ret;
110 }
111
112 static int check_params(void)
113 {
114 int ret = -1;
115
116 if (ifname == NULL) {
117 ERR("no input file specified");
118 goto out;
119 }
120
121 if (ofname == NULL) {
122 ERR("no output file specified");
123 goto out;
124 }
125
126 ret = 0;
127
128 out:
129 return ret;
130 }
131
132 int main(int argc, char *argv[])
133 {
134 int res = EXIT_FAILURE;
135 int err;
136
137 progname = basename(argv[0]);
138
139 while ( 1 ) {
140 int c;
141
142 c = getopt(argc, argv, "di:o:h");
143 if (c == -1)
144 break;
145
146 switch (c) {
147 case 'd':
148 do_decrypt = 1;
149 break;
150 case 'i':
151 ifname = optarg;
152 break;
153 case 'o':
154 ofname = optarg;
155 break;
156 case 'h':
157 usage(EXIT_SUCCESS);
158 break;
159 default:
160 usage(EXIT_FAILURE);
161 break;
162 }
163 }
164
165 err = check_params();
166 if (err)
167 goto out;
168
169 err = crypt_file();
170 if (err)
171 goto out;
172
173 res = EXIT_SUCCESS;
174
175 out:
176 return res;
177 }