stringfix.c 3.0 KB

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