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