stringfix.cc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #define MAX_LINE_LEN 35000
  9. int help = 0;
  10. void garble(char **inptr, char **outptr)
  11. {
  12. char *in = *inptr, *out = NULL, *p = NULL, obuf[MAX_LINE_LEN] = "";
  13. size_t chars = 0;
  14. unsigned char x = 0;
  15. p = in + 5;
  16. if (*p == '"') {
  17. sprintf((*outptr), "\"\"");
  18. *inptr += 7;
  19. *outptr += 2;
  20. return;
  21. }
  22. while ((*p) && !((*p == '"') && (*(p - 1) != '\\')))
  23. p++;
  24. if ((*p == '"') && (*(p - 1) != '\\') && (*(p + 1) == ')')) {
  25. char *c;
  26. c = in + 5;
  27. out = obuf;
  28. x = 0xFF;
  29. while (c < p) {
  30. if (*c == '\\') {
  31. unsigned char e;
  32. c++;
  33. if (*c == 'a')
  34. e = 7;
  35. else if (*c == 'b')
  36. e = 8;
  37. else if (*c == 't')
  38. e = 9;
  39. else if (*c == 'n')
  40. e = 10;
  41. else if (*c == 'v')
  42. e = 11;
  43. else if (*c == 'f')
  44. e = 12;
  45. else if (*c == 'r')
  46. e = 13;
  47. else if ((*c >= '0') && (*c <= '7')) {
  48. int cnt = 0;
  49. e = 0;
  50. while ((*c >= '0') && (*c <= '7') && (cnt < 3)) {
  51. e = (e * 8) + (*c - '0');
  52. cnt++;
  53. c++;
  54. }
  55. c--;
  56. } else
  57. e = *c;
  58. sprintf(out, "\\%03o", e ^ x);
  59. chars++;
  60. x = e;
  61. c++;
  62. } else {
  63. sprintf(out, "\\%03o", ((unsigned char) *c) ^ x);
  64. chars++;
  65. x = *c;
  66. c++;
  67. }
  68. out += 4;
  69. *out = 0;
  70. }
  71. if (help)
  72. sprintf(*outptr, "%zu, \"%s\"", chars, obuf);
  73. else
  74. sprintf(*outptr, "degarble(%zu, \"%s\")", chars, obuf);
  75. *outptr += strlen(*outptr);
  76. in = p + 2;
  77. } else {
  78. strncpy((*outptr), in, (p - in) + 1);
  79. *outptr += strlen(*outptr);
  80. in = p + 1;
  81. }
  82. *inptr = in;
  83. }
  84. void processline(char *line)
  85. {
  86. char tmpout[MAX_LINE_LEN] = "", *in = NULL, *out = NULL;
  87. size_t outlen = 0;
  88. in = line;
  89. out = tmpout;
  90. if (*in) {
  91. while (*in) {
  92. if (!strncmp(in, "STR(\"", 5)) {
  93. *out = 0;
  94. garble(&in, &out);
  95. *out = 0;
  96. } else
  97. *out++ = *in++;
  98. }
  99. *out = 0;
  100. } else
  101. tmpout[0] = 0;
  102. outlen = strlen(tmpout);
  103. fwrite(tmpout, outlen, 1, stdout);
  104. }
  105. int main(int argc, char *argv[])
  106. {
  107. int readData = 0;
  108. if (argc == 2)
  109. help = 1;
  110. char tempBuf[MAX_LINE_LEN] = "";
  111. while (!feof(stdin)) {
  112. if (fgets(tempBuf, sizeof(tempBuf), stdin) && !feof(stdin)) {
  113. processline(tempBuf);
  114. readData = 1;
  115. }
  116. }
  117. return !(readData);
  118. }
  119. /* vim: set sts=2 sw=2 ts=8 et: */