1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
11 #include <getopt.h> /* for getopt() */
14 #include "buffalo-lib.h"
16 #define ERR(fmt, args...) do { \
18 fprintf(stderr, "[%s] *** error: " fmt "\n", \
19 progname, ## args ); \
22 static char *progname
;
25 static int do_decrypt
;
27 void usage(int status
)
29 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
31 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
35 " -d decrypt instead of encrypt\n"
36 " -i <file> read input from the file <file>\n"
37 " -o <file> write output to the file <file>\n"
38 " -h show this screen\n"
44 static const unsigned char *crypt_key1
= (unsigned char *)
45 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
46 static const unsigned char *crypt_key2
= (unsigned char *)
47 "XYZ0123hijklmnopqABCDEFGHrstuvabcdefgwxyzIJKLMSTUVW456789NOPQR";
49 static void crypt_header(unsigned char *buf
, ssize_t len
,
50 const unsigned char *key1
, const unsigned char *key2
)
54 for (i
= 0; i
< len
; i
++) {
57 for (j
= 0; key1
[j
]; j
++)
58 if (buf
[i
] == key1
[j
]) {
65 static int crypt_file(void)
67 unsigned char *buf
= NULL
;
72 src_len
= get_file_size(ifname
);
74 ERR("unable to get size of '%s'", ifname
);
78 buf
= malloc(src_len
);
80 ERR("no memory for the buffer");
84 err
= read_file_to_buf(ifname
, buf
, src_len
);
86 ERR("unable to read from file '%s'", ifname
);
91 crypt_header(buf
, 512, crypt_key2
, crypt_key1
);
93 crypt_header(buf
, 512, crypt_key1
, crypt_key2
);
95 err
= write_buf_to_file(ofname
, buf
, src_len
);
97 ERR("unable to write to file '%s'", ofname
);
108 static int check_params(void)
112 if (ifname
== NULL
) {
113 ERR("no input file specified");
117 if (ofname
== NULL
) {
118 ERR("no output file specified");
128 int main(int argc
, char *argv
[])
130 int res
= EXIT_FAILURE
;
133 progname
= basename(argv
[0]);
138 c
= getopt(argc
, argv
, "di:o:h");
161 err
= check_params();