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