xorimage.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * xorimage.c - partially based on OpenWrt's addpattern.c
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <stdint.h>
  22. #include <time.h>
  23. #include <unistd.h>
  24. #include <sys/stat.h>
  25. static char default_pattern[] = "12345678";
  26. int xor_data(uint8_t *data, size_t len, const uint8_t *pattern, int p_len, int p_off)
  27. {
  28. int offset = p_off;
  29. while (len--) {
  30. *data ^= pattern[offset];
  31. data++;
  32. offset = (offset + 1) % p_len;
  33. }
  34. return offset;
  35. }
  36. void usage(void) __attribute__ (( __noreturn__ ));
  37. void usage(void)
  38. {
  39. fprintf(stderr, "Usage: xorimage [-i infile] [-o outfile] [-p <pattern>]\n");
  40. exit(EXIT_FAILURE);
  41. }
  42. int main(int argc, char **argv)
  43. {
  44. char buf[1024]; /* keep this at 1k or adjust garbage calc below */
  45. FILE *in = stdin;
  46. FILE *out = stdout;
  47. char *ifn = NULL;
  48. char *ofn = NULL;
  49. const char *pattern = default_pattern;
  50. int c;
  51. int v0, v1, v2;
  52. size_t n;
  53. int p_len, p_off = 0;
  54. while ((c = getopt(argc, argv, "i:o:p:h")) != -1) {
  55. switch (c) {
  56. case 'i':
  57. ifn = optarg;
  58. break;
  59. case 'o':
  60. ofn = optarg;
  61. break;
  62. case 'p':
  63. pattern = optarg;
  64. break;
  65. case 'h':
  66. default:
  67. usage();
  68. }
  69. }
  70. if (optind != argc || optind == 1) {
  71. fprintf(stderr, "illegal arg \"%s\"\n", argv[optind]);
  72. usage();
  73. }
  74. if (ifn && !(in = fopen(ifn, "r"))) {
  75. fprintf(stderr, "can not open \"%s\" for reading\n", ifn);
  76. usage();
  77. }
  78. if (ofn && !(out = fopen(ofn, "w"))) {
  79. fprintf(stderr, "can not open \"%s\" for writing\n", ofn);
  80. usage();
  81. }
  82. p_len = strlen(pattern);
  83. if (p_len == 0) {
  84. fprintf(stderr, "pattern cannot be empty\n");
  85. usage();
  86. }
  87. while ((n = fread(buf, 1, sizeof(buf), in)) > 0) {
  88. if (n < sizeof(buf)) {
  89. if (ferror(in)) {
  90. FREAD_ERROR:
  91. fprintf(stderr, "fread error\n");
  92. return EXIT_FAILURE;
  93. }
  94. }
  95. p_off = xor_data(buf, n, pattern, p_len, p_off);
  96. if (!fwrite(buf, n, 1, out)) {
  97. FWRITE_ERROR:
  98. fprintf(stderr, "fwrite error\n");
  99. return EXIT_FAILURE;
  100. }
  101. }
  102. if (ferror(in)) {
  103. goto FREAD_ERROR;
  104. }
  105. if (fflush(out)) {
  106. goto FWRITE_ERROR;
  107. }
  108. fclose(in);
  109. fclose(out);
  110. return EXIT_SUCCESS;
  111. }