buffalo-tftp.c 3.0 KB

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