xmlmime.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include <string.h>
  2. #include "xmlmime.h"
  3. static const char *
  4. getTok(const char **pp)
  5. {
  6. /* inComment means one level of nesting; inComment+1 means two levels etc */
  7. enum { inAtom, inString, init, inComment };
  8. int state = init;
  9. const char *tokStart = 0;
  10. for (;;) {
  11. switch (**pp) {
  12. case '\0':
  13. if (state == inAtom)
  14. return tokStart;
  15. return 0;
  16. case ' ':
  17. case '\r':
  18. case '\t':
  19. case '\n':
  20. if (state == inAtom)
  21. return tokStart;
  22. break;
  23. case '(':
  24. if (state == inAtom)
  25. return tokStart;
  26. if (state != inString)
  27. state++;
  28. break;
  29. case ')':
  30. if (state > init)
  31. --state;
  32. else if (state != inString)
  33. return 0;
  34. break;
  35. case ';':
  36. case '/':
  37. case '=':
  38. if (state == inAtom)
  39. return tokStart;
  40. if (state == init)
  41. return (*pp)++;
  42. break;
  43. case '\\':
  44. ++*pp;
  45. if (**pp == '\0')
  46. return 0;
  47. break;
  48. case '"':
  49. switch (state) {
  50. case inString:
  51. ++*pp;
  52. return tokStart;
  53. case inAtom:
  54. return tokStart;
  55. case init:
  56. tokStart = *pp;
  57. state = inString;
  58. break;
  59. }
  60. break;
  61. default:
  62. if (state == init) {
  63. tokStart = *pp;
  64. state = inAtom;
  65. }
  66. break;
  67. }
  68. ++*pp;
  69. }
  70. /* not reached */
  71. }
  72. /* key must be lowercase ASCII */
  73. static int
  74. matchkey(const char *start, const char *end, const char *key)
  75. {
  76. if (!start)
  77. return 0;
  78. for (; start != end; start++, key++)
  79. if (*start != *key && *start != 'A' + (*key - 'a'))
  80. return 0;
  81. return *key == '\0';
  82. }
  83. void
  84. getXMLCharset(const char *buf, char *charset)
  85. {
  86. const char *next, *p;
  87. charset[0] = '\0';
  88. next = buf;
  89. p = getTok(&next);
  90. if (matchkey(p, next, "text"))
  91. strcpy(charset, "us-ascii");
  92. else if (!matchkey(p, next, "application"))
  93. return;
  94. p = getTok(&next);
  95. if (!p || *p != '/')
  96. return;
  97. p = getTok(&next);
  98. #if 0
  99. if (!matchkey(p, next, "xml") && charset[0] == '\0')
  100. return;
  101. #endif
  102. p = getTok(&next);
  103. while (p) {
  104. if (*p == ';') {
  105. p = getTok(&next);
  106. if (matchkey(p, next, "charset")) {
  107. p = getTok(&next);
  108. if (p && *p == '=') {
  109. p = getTok(&next);
  110. if (p) {
  111. char *s = charset;
  112. if (*p == '"') {
  113. while (++p != next - 1) {
  114. if (*p == '\\')
  115. ++p;
  116. if (s == charset + CHARSET_MAX - 1) {
  117. charset[0] = '\0';
  118. break;
  119. }
  120. *s++ = *p;
  121. }
  122. *s++ = '\0';
  123. }
  124. else {
  125. if (next - p > CHARSET_MAX - 1)
  126. break;
  127. while (p != next)
  128. *s++ = *p++;
  129. *s = 0;
  130. break;
  131. }
  132. }
  133. }
  134. break;
  135. }
  136. }
  137. else
  138. p = getTok(&next);
  139. }
  140. }
  141. #ifdef TEST
  142. #include <stdio.h>
  143. int
  144. main(int argc, char *argv[])
  145. {
  146. char buf[CHARSET_MAX];
  147. if (argc <= 1)
  148. return 1;
  149. printf("%s\n", argv[1]);
  150. getXMLCharset(argv[1], buf);
  151. printf("charset=\"%s\"\n", buf);
  152. return 0;
  153. }
  154. #endif /* TEST */