xmlmime.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. __ __ _
  3. ___\ \/ /_ __ __ _| |_
  4. / _ \\ /| '_ \ / _` | __|
  5. | __// \| |_) | (_| | |_
  6. \___/_/\_\ .__/ \__,_|\__|
  7. |_| XML parser
  8. Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
  9. Copyright (c) 2000-2017 Expat development team
  10. Licensed under the MIT license:
  11. Permission is hereby granted, free of charge, to any person obtaining
  12. a copy of this software and associated documentation files (the
  13. "Software"), to deal in the Software without restriction, including
  14. without limitation the rights to use, copy, modify, merge, publish,
  15. distribute, sublicense, and/or sell copies of the Software, and to permit
  16. persons to whom the Software is furnished to do so, subject to the
  17. following conditions:
  18. The above copyright notice and this permission notice shall be included
  19. in all copies or substantial portions of the Software.
  20. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  23. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  24. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  25. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  26. USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. #include <string.h>
  29. #include "xmlmime.h"
  30. static const char *
  31. getTok(const char **pp)
  32. {
  33. /* inComment means one level of nesting; inComment+1 means two levels etc */
  34. enum { inAtom, inString, init, inComment };
  35. int state = init;
  36. const char *tokStart = 0;
  37. for (;;) {
  38. switch (**pp) {
  39. case '\0':
  40. if (state == inAtom)
  41. return tokStart;
  42. return 0;
  43. case ' ':
  44. case '\r':
  45. case '\t':
  46. case '\n':
  47. if (state == inAtom)
  48. return tokStart;
  49. break;
  50. case '(':
  51. if (state == inAtom)
  52. return tokStart;
  53. if (state != inString)
  54. state++;
  55. break;
  56. case ')':
  57. if (state > init)
  58. --state;
  59. else if (state != inString)
  60. return 0;
  61. break;
  62. case ';':
  63. case '/':
  64. case '=':
  65. if (state == inAtom)
  66. return tokStart;
  67. if (state == init)
  68. return (*pp)++;
  69. break;
  70. case '\\':
  71. ++*pp;
  72. if (**pp == '\0')
  73. return 0;
  74. break;
  75. case '"':
  76. switch (state) {
  77. case inString:
  78. ++*pp;
  79. return tokStart;
  80. case inAtom:
  81. return tokStart;
  82. case init:
  83. tokStart = *pp;
  84. state = inString;
  85. break;
  86. }
  87. break;
  88. default:
  89. if (state == init) {
  90. tokStart = *pp;
  91. state = inAtom;
  92. }
  93. break;
  94. }
  95. ++*pp;
  96. }
  97. /* not reached */
  98. }
  99. /* key must be lowercase ASCII */
  100. static int
  101. matchkey(const char *start, const char *end, const char *key)
  102. {
  103. if (!start)
  104. return 0;
  105. for (; start != end; start++, key++)
  106. if (*start != *key && *start != 'A' + (*key - 'a'))
  107. return 0;
  108. return *key == '\0';
  109. }
  110. void
  111. getXMLCharset(const char *buf, char *charset)
  112. {
  113. const char *next, *p;
  114. charset[0] = '\0';
  115. next = buf;
  116. p = getTok(&next);
  117. if (matchkey(p, next, "text"))
  118. strcpy(charset, "us-ascii");
  119. else if (!matchkey(p, next, "application"))
  120. return;
  121. p = getTok(&next);
  122. if (!p || *p != '/')
  123. return;
  124. p = getTok(&next);
  125. #if 0
  126. if (!matchkey(p, next, "xml") && charset[0] == '\0')
  127. return;
  128. #endif
  129. p = getTok(&next);
  130. while (p) {
  131. if (*p == ';') {
  132. p = getTok(&next);
  133. if (matchkey(p, next, "charset")) {
  134. p = getTok(&next);
  135. if (p && *p == '=') {
  136. p = getTok(&next);
  137. if (p) {
  138. char *s = charset;
  139. if (*p == '"') {
  140. while (++p != next - 1) {
  141. if (*p == '\\')
  142. ++p;
  143. if (s == charset + CHARSET_MAX - 1) {
  144. charset[0] = '\0';
  145. break;
  146. }
  147. *s++ = *p;
  148. }
  149. *s++ = '\0';
  150. }
  151. else {
  152. if (next - p > CHARSET_MAX - 1)
  153. break;
  154. while (p != next)
  155. *s++ = *p++;
  156. *s = 0;
  157. break;
  158. }
  159. }
  160. }
  161. break;
  162. }
  163. }
  164. else
  165. p = getTok(&next);
  166. }
  167. }
  168. #ifdef TEST
  169. #include <stdio.h>
  170. int
  171. main(int argc, char *argv[])
  172. {
  173. char buf[CHARSET_MAX];
  174. if (argc <= 1)
  175. return 1;
  176. printf("%s\n", argv[1]);
  177. getXMLCharset(argv[1], buf);
  178. printf("charset=\"%s\"\n", buf);
  179. return 0;
  180. }
  181. #endif /* TEST */