087f9955b6489c71204630867d559bb1f66d4829
2 * Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
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.
15 #include <getopt.h> /* for getopt() */
18 #include "buffalo-lib.h"
20 #define ERR(fmt, args...) do { \
22 fprintf(stderr, "[%s] *** error: " fmt "\n", \
23 progname, ## args ); \
26 static char *progname
;
29 static int do_decrypt
;
31 void usage(int status
)
33 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
35 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
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"
48 static const unsigned char *crypt_key1
= (unsigned char *)
49 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
50 static const unsigned char *crypt_key2
= (unsigned char *)
51 "XYZ0123hijklmnopqABCDEFGHrstuvabcdefgwxyzIJKLMSTUVW456789NOPQR";
53 static void crypt_header(unsigned char *buf
, ssize_t len
,
54 const unsigned char *key1
, const unsigned char *key2
)
58 for (i
= 0; i
< len
; i
++) {
61 for (j
= 0; key1
[j
]; j
++)
62 if (buf
[i
] == key1
[j
]) {
69 static int crypt_file(void)
71 unsigned char *buf
= NULL
;
76 src_len
= get_file_size(ifname
);
78 ERR("unable to get size of '%s'", ifname
);
82 buf
= malloc(src_len
);
84 ERR("no memory for the buffer");
88 err
= read_file_to_buf(ifname
, buf
, src_len
);
90 ERR("unable to read from file '%s'", ifname
);
95 crypt_header(buf
, 512, crypt_key2
, crypt_key1
);
97 crypt_header(buf
, 512, crypt_key1
, crypt_key2
);
99 err
= write_buf_to_file(ofname
, buf
, src_len
);
101 ERR("unable to write to file '%s'", ofname
);
112 static int check_params(void)
116 if (ifname
== NULL
) {
117 ERR("no input file specified");
121 if (ofname
== NULL
) {
122 ERR("no output file specified");
132 int main(int argc
, char *argv
[])
134 int res
= EXIT_FAILURE
;
137 progname
= basename(argv
[0]);
142 c
= getopt(argc
, argv
, "di:o:h");
165 err
= check_params();