xorimage.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * xorimage.c - partially based on libreCMC'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 <stdbool.h>
  22. #include <stdint.h>
  23. #include <unistd.h>
  24. #include <sys/stat.h>
  25. static char default_pattern[] = "12345678";
  26. static int is_hex_pattern;
  27. int xor_data(uint8_t *data, size_t len, const uint8_t *pattern, int p_len, int p_off)
  28. {
  29. int offset = p_off;
  30. while (len--) {
  31. *data ^= pattern[offset];
  32. data++;
  33. offset = (offset + 1) % p_len;
  34. }
  35. return offset;
  36. }
  37. void usage(void) __attribute__ (( __noreturn__ ));
  38. void usage(void)
  39. {
  40. fprintf(stderr, "Usage: xorimage [-i infile] [-o outfile] [-p <pattern>] [-x]\n");
  41. exit(EXIT_FAILURE);
  42. }
  43. int main(int argc, char **argv)
  44. {
  45. char buf[1024]; /* keep this at 1k or adjust garbage calc below */
  46. FILE *in = stdin;
  47. FILE *out = stdout;
  48. char *ifn = NULL;
  49. char *ofn = NULL;
  50. const char *pattern = default_pattern;
  51. char hex_pattern[128];
  52. unsigned int hex_buf;
  53. int c;
  54. int v0, v1, v2;
  55. size_t n;
  56. int p_len, p_off = 0;
  57. while ((c = getopt(argc, argv, "i:o:p:xh")) != -1) {
  58. switch (c) {
  59. case 'i':
  60. ifn = optarg;
  61. break;
  62. case 'o':
  63. ofn = optarg;
  64. break;
  65. case 'p':
  66. pattern = optarg;
  67. break;
  68. case 'x':
  69. is_hex_pattern = true;
  70. break;
  71. case 'h':
  72. default:
  73. usage();
  74. }
  75. }
  76. if (optind != argc || optind == 1) {
  77. fprintf(stderr, "illegal arg \"%s\"\n", argv[optind]);
  78. usage();
  79. }
  80. if (ifn && !(in = fopen(ifn, "r"))) {
  81. fprintf(stderr, "can not open \"%s\" for reading\n", ifn);
  82. usage();
  83. }
  84. if (ofn && !(out = fopen(ofn, "w"))) {
  85. fprintf(stderr, "can not open \"%s\" for writing\n", ofn);
  86. usage();
  87. }
  88. p_len = strlen(pattern);
  89. if (p_len == 0) {
  90. fprintf(stderr, "pattern cannot be empty\n");
  91. usage();
  92. }
  93. if (is_hex_pattern) {
  94. int i;
  95. if ((p_len / 2) > sizeof(hex_pattern)) {
  96. fprintf(stderr, "provided hex pattern is too long\n");
  97. usage();
  98. }
  99. if (p_len % 2 != 0) {
  100. fprintf(stderr, "the number of characters (hex) is incorrect\n");
  101. usage();
  102. }
  103. for (i = 0; i < (p_len / 2); i++) {
  104. if (sscanf(pattern + (i * 2), "%2x", &hex_buf) < 0) {
  105. fprintf(stderr, "invalid hex digit around %d\n", i * 2);
  106. usage();
  107. }
  108. hex_pattern[i] = (char)hex_buf;
  109. }
  110. }
  111. while ((n = fread(buf, 1, sizeof(buf), in)) > 0) {
  112. if (n < sizeof(buf)) {
  113. if (ferror(in)) {
  114. FREAD_ERROR:
  115. fprintf(stderr, "fread error\n");
  116. return EXIT_FAILURE;
  117. }
  118. }
  119. if (is_hex_pattern) {
  120. p_off = xor_data(buf, n, hex_pattern, (p_len / 2),
  121. p_off);
  122. } else {
  123. p_off = xor_data(buf, n, pattern, p_len, p_off);
  124. }
  125. if (!fwrite(buf, n, 1, out)) {
  126. FWRITE_ERROR:
  127. fprintf(stderr, "fwrite error\n");
  128. return EXIT_FAILURE;
  129. }
  130. }
  131. if (ferror(in)) {
  132. goto FREAD_ERROR;
  133. }
  134. if (fflush(out)) {
  135. goto FWRITE_ERROR;
  136. }
  137. fclose(in);
  138. fclose(out);
  139. return EXIT_SUCCESS;
  140. }