stringfix.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* stringfix.c:
  2. * handles STR("text") for garbling of strings..
  3. */
  4. /* dprintf(idx, STR("A"), STR(""), STR("1" ), STR("")); */
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #ifdef HAVE_CONFIG_H
  9. #include "config.h"
  10. #endif
  11. #define WTF 50720
  12. int help = 0;
  13. #ifdef S_GARBLESTRINGS
  14. void garble(char **inptr, char **outptr)
  15. {
  16. char *in = *inptr, *out = NULL, *p = NULL, obuf[WTF] = "";
  17. int chars = 0;
  18. unsigned char x = 0;
  19. p = in + 5;
  20. if (*p == '"') {
  21. sprintf((*outptr), "\"\"");
  22. *inptr += 7;
  23. *outptr += 2;
  24. return;
  25. }
  26. while ((*p) && !((*p == '"') && (*(p - 1) != '\\')))
  27. p++;
  28. if ((*p == '"') && (*(p - 1) != '\\') && (*(p + 1) == ')')) {
  29. char *c;
  30. c = in + 5;
  31. out = obuf;
  32. x = 0xFF;
  33. while (c < p) {
  34. if (*c == '\\') {
  35. unsigned char e;
  36. c++;
  37. if (*c == 'a')
  38. e = 7;
  39. else if (*c == 'b')
  40. e = 8;
  41. else if (*c == 't')
  42. e = 9;
  43. else if (*c == 'n')
  44. e = 10;
  45. else if (*c == 'v')
  46. e = 11;
  47. else if (*c == 'f')
  48. e = 12;
  49. else if (*c == 'r')
  50. e = 13;
  51. else if ((*c >= '0') && (*c <= '7')) {
  52. int cnt = 0;
  53. e = 0;
  54. while ((*c >= '0') && (*c <= '7') && (cnt < 3)) {
  55. e = (e * 8) + (*c - '0');
  56. cnt++;
  57. c++;
  58. }
  59. c--;
  60. } else
  61. e = *c;
  62. sprintf(out, "\\%03o", e ^ x);
  63. chars++;
  64. x = e;
  65. c++;
  66. } else {
  67. sprintf(out, "\\%03o", ((unsigned char) *c) ^ x);
  68. chars++;
  69. x = *c;
  70. c++;
  71. }
  72. out += 4;
  73. *out = 0;
  74. }
  75. if (help)
  76. sprintf(*outptr, "%d, \"%s\"", chars, obuf);
  77. else
  78. sprintf(*outptr, "degarble(%d, \"%s\")", chars, obuf);
  79. *outptr += strlen(*outptr);
  80. in = p + 2;
  81. } else {
  82. strncpy((*outptr), in, (p - in) + 1);
  83. *outptr += strlen(*outptr);
  84. in = p + 1;
  85. }
  86. *inptr = in;
  87. }
  88. char *outbuf = NULL;
  89. void processline(char *line)
  90. {
  91. char tmpin[WTF] = "", tmpout[WTF] = "", *in = NULL, *out = NULL;
  92. strcpy(tmpin, line);
  93. memset((char *) &tmpin[strlen(tmpin)], 20, 0);
  94. in = tmpin;
  95. out = tmpout;
  96. if (*in) {
  97. while (*in) {
  98. if (!strncmp(in, "STR(\"", 5)) {
  99. *out = 0;
  100. garble(&in, &out);
  101. *out = 0;
  102. } else
  103. *out++ = *in++;
  104. }
  105. *out = 0;
  106. } else
  107. tmpout[0] = 0;
  108. if (outbuf)
  109. outbuf = realloc(outbuf, strlen(outbuf) + strlen(tmpout) + 10);
  110. else {
  111. outbuf = calloc(1, strlen(tmpout) + 10);
  112. }
  113. strcat(outbuf, tmpout);
  114. strcat(outbuf, "\n");
  115. }
  116. #endif /* S_GARBLESTRINGS */
  117. int main(int argc, char *argv[0])
  118. {
  119. #ifdef S_GARBLESTRINGS
  120. FILE *f = NULL;
  121. char *ln = NULL, *nln = NULL, *buf = NULL;
  122. int insize;
  123. if (argc != 3 && argc != 4)
  124. return 1;
  125. if (argc == 4)
  126. help = 1;
  127. if (!(f = fopen(argv[1], "r")))
  128. return 1;
  129. fseek(f, 0, SEEK_END);
  130. insize = ftell(f);
  131. fseek(f, 0, SEEK_SET);
  132. buf = calloc(1, insize + 1);
  133. fread(buf, 1, insize, f);
  134. fclose(f);
  135. buf[insize] = 0;
  136. ln = buf;
  137. while (ln) {
  138. nln = strchr(ln, '\n');
  139. if (nln)
  140. *nln++ = 0;
  141. processline(ln);
  142. ln = nln;
  143. }
  144. if ((f = fopen(argv[2], "w"))) {
  145. fwrite(outbuf, 1, strlen(outbuf), f);
  146. fclose(f);
  147. }
  148. /* printf(outbuf); */
  149. return 0;
  150. #else /* !S_GARBLESTRINGS */
  151. return 1;
  152. #endif /* S_GARBLESTRINGS */
  153. }