• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/firmware-utils/src/buffalo-tftp.c

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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt