Download | Plain Text | No Line Numbers


  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <errno.h>
  6. #include <assert.h>
  7. #include "cipher.h"
  8.  
  9. #ifdef DEBUG
  10. # include "hexdump.h"
  11. #endif
  12.  
  13. #define BLOCKSIZE 8192
  14. #define PERL_SHEBANG "#!/usr/bin/perl\n"
  15. #define PERL_MODULE "use Confixx::Filter;\n"
  16.  
  17. static const char *me;
  18.  
  19. void usage()
  20. {
  21. fprintf(stderr, "Usage: %s [options] <file>\n", me);
  22. fprintf(stderr, "\nOptions:\n");
  23. fprintf(stderr, " -d\tdecode mode\n");
  24. fprintf(stderr, " -e\tencode mode\n");
  25. fprintf(stderr, " -k\tdon't prepend hash key in decode mode\n");
  26. fprintf(stderr, " -h\tthis help\n");
  27. exit(EXIT_FAILURE);
  28. }
  29.  
  30. int main(int argc, char *argv[])
  31. {
  32. short encode = 0, stripped = 0, hashkey = 1;
  33. char *filename = NULL;
  34. int opt;
  35.  
  36. me = argv[0];
  37.  
  38. while ((opt = getopt(argc, argv, "hked")) != -1)
  39. {
  40. switch(opt)
  41. {
  42. case 'e':
  43. encode = 1;
  44. hashkey = 0;
  45. break;
  46. case 'd':
  47. break;
  48. case 'k':
  49. hashkey = 0;
  50. break;
  51. case 'h':
  52. default:
  53. usage();
  54. break;
  55. }
  56. }
  57. argv += optind;
  58.  
  59. if (argv)
  60. filename = *argv;
  61.  
  62. if (!filename)
  63. usage();
  64.  
  65. #ifdef DEBUG
  66. fprintf(stderr, "[DEBUG] Mode: %s\n", (encode) ? "encode" : "decode");
  67. fprintf(stderr, "[DEBUG] filename: %s\n", filename);
  68. #endif
  69.  
  70. FILE *fp = stdin;
  71. if (strcmp(filename, "-") == 0)
  72. filename = NULL;
  73. if (filename != NULL && (fp = fopen(filename, "r")) == NULL)
  74. {
  75. fprintf(stderr, "Unable to open input file '%s'", filename);
  76. if (errno)
  77. fprintf(stderr, ": %s", strerror(errno));
  78. fprintf(stderr, "\n");
  79. exit(EXIT_FAILURE);
  80. }
  81.  
  82. /* allocate buffer for decrypted stuff */
  83. char out[BLOCKSIZE];
  84.  
  85. /* cipher */
  86. ctx_t cipher;
  87. memset(&cipher, 0, sizeof(ctx_t)); //TODO
  88. int ret = CipherInit(&cipher);
  89. if (ret != 256)
  90. {
  91. fprintf(stderr, "Unexpected return value of CipherInit()\n");
  92. exit(EXIT_FAILURE);
  93. }
  94. #ifdef DEBUG
  95. fprintf(stderr, "[DEBUG] CipherInit()=%d\n", ret);
  96. #endif
  97.  
  98. /* set pointer to output buffer */
  99. cipher.out = out;
  100.  
  101. /* some sanity checks */
  102. assert(cipher.out == out);
  103. assert(cipher.ret == 0);
  104. assert(cipher.off1 == 0);
  105. assert(cipher.off2 == 0);
  106. assert(cipher.prepend_hashkey == 1);
  107.  
  108. /* let CipherUpdate don't prepend a hashkey */
  109. cipher.prepend_hashkey = hashkey;
  110.  
  111. /* allocate input buffer */
  112. char in[BLOCKSIZE];
  113.  
  114. if (encode)
  115. {
  116. printf(PERL_SHEBANG);
  117. printf(PERL_MODULE);
  118. }
  119.  
  120. while(!feof(fp))
  121. {
  122. /* read at most BLOCKSIZE - 1 bytes:
  123.   * - 1 char ("#"-char) will be prepended in decode mode during
  124.   * first call of CipherUpdate()
  125.   */
  126. size_t in_len = fread(in, sizeof(char), BLOCKSIZE - 1, fp);
  127. assert(in_len == (uint32_t)in_len);
  128. if (ferror(fp))
  129. {
  130. fprintf(stderr, "Error while reading input file");
  131. if (errno)
  132. fprintf(stderr, ": %s", strerror(errno));
  133. fprintf(stderr, "\n");
  134. if (filename != NULL)
  135. fclose(fp);
  136. exit(EXIT_FAILURE);
  137. }
  138. in[in_len] = '\0';
  139.  
  140. #ifdef DEBUG
  141. fprintf(stderr, "[DEBUG] read %zu bytes from input file\n", in_len);
  142. #endif
  143.  
  144. char *inptr = in;
  145. if (!encode && !stripped)
  146. {
  147. if (strncmp(inptr, PERL_SHEBANG, sizeof(PERL_SHEBANG) - 1) == 0)
  148. inptr += sizeof(PERL_SHEBANG) - 1;
  149. if (strncmp(inptr, PERL_MODULE, sizeof(PERL_MODULE) - 1) == 0)
  150. inptr += sizeof(PERL_MODULE) - 1;
  151. in_len -= inptr - in;
  152. }
  153. stripped = 1;
  154.  
  155. uint32_t out_len = CipherUpdate(&cipher, inptr, in_len);
  156. #ifdef DEBUG
  157. fprintf(stderr, "[DEBUG] CipherUpdate()=%u\n", out_len);
  158. #endif
  159. /* we don't need one extra byte to terminate the buffer
  160.   * as fwrite has a size parameter
  161.   */
  162. assert(out_len <= BLOCKSIZE);
  163.  
  164. #ifdef DEBUG
  165. fprintf(stderr, "[DEBUG] Output:\n");
  166. #endif
  167. fwrite(out, out_len, sizeof(char), stdout);
  168. }
  169.  
  170. if (filename != NULL)
  171. fclose(fp);
  172. return 0;
  173. }
  174.