2 * Copyright (c) 2020-2021 Samsung Electronics Co., Ltd. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 #include "tvgXmlParser.h"
34 /************************************************************************/
35 /* Internal Class Implementation */
36 /************************************************************************/
38 #ifdef THORVG_LOG_ENABLED
42 string simpleXmlNodeTypeToString(SvgNodeType type)
45 case SvgNodeType::Doc: return "Doc";
46 case SvgNodeType::G: return "G";
47 case SvgNodeType::Defs: return "Defs";
48 case SvgNodeType::Animation: return "Animation";
49 case SvgNodeType::Arc: return "Arc";
50 case SvgNodeType::Circle: return "Circle";
51 case SvgNodeType::Ellipse: return "Ellipse";
52 case SvgNodeType::Image: return "Image";
53 case SvgNodeType::Line: return "Line";
54 case SvgNodeType::Path: return "Path";
55 case SvgNodeType::Polygon: return "Polygon";
56 case SvgNodeType::Polyline: return "Polyline";
57 case SvgNodeType::Rect: return "Rect";
58 case SvgNodeType::Text: return "Text";
59 case SvgNodeType::TextArea: return "TextArea";
60 case SvgNodeType::Tspan: return "Tspan";
61 case SvgNodeType::Use: return "Use";
62 case SvgNodeType::Video: return "Video";
63 case SvgNodeType::ClipPath: return "ClipPath";
64 default: return "Unknown";
70 static const char* _simpleXmlFindWhiteSpace(const char* itr, const char* itrEnd)
72 for (; itr < itrEnd; itr++) {
73 if (isspace((unsigned char)*itr)) break;
79 static const char* _simpleXmlSkipWhiteSpace(const char* itr, const char* itrEnd)
81 for (; itr < itrEnd; itr++) {
82 if (!isspace((unsigned char)*itr)) break;
88 static const char* _simpleXmlUnskipWhiteSpace(const char* itr, const char* itrStart)
90 for (itr--; itr > itrStart; itr--) {
91 if (!isspace((unsigned char)*itr)) break;
97 static const char* _simpleXmlSkipXmlEntities(const char* itr, const char* itrEnd)
100 while (*itr == '&' && itr < itrEnd) {
101 for (int i = 0; i < NUMBER_OF_XML_ENTITIES; ++i) {
102 if (strncmp(itr, xmlEntity[i], xmlEntityLength[i]) == 0) {
103 itr += xmlEntityLength[i];
114 static const char* _simpleXmlUnskipXmlEntities(const char* itr, const char* itrStart)
117 while (*(itr - 1) == ';' && itr > itrStart) {
118 for (int i = 0; i < NUMBER_OF_XML_ENTITIES; ++i) {
119 if (itr - xmlEntityLength[i] > itrStart &&
120 strncmp(itr - xmlEntityLength[i], xmlEntity[i], xmlEntityLength[i]) == 0) {
121 itr -= xmlEntityLength[i];
132 static const char* _skipWhiteSpacesAndXmlEntities(const char* itr, const char* itrEnd)
134 itr = _simpleXmlSkipWhiteSpace(itr, itrEnd);
137 if (p != (itr = _simpleXmlSkipXmlEntities(itr, itrEnd))) p = itr;
139 if (p != (itr = _simpleXmlSkipWhiteSpace(itr, itrEnd))) p = itr;
146 static const char* _unskipWhiteSpacesAndXmlEntities(const char* itr, const char* itrStart)
148 itr = _simpleXmlUnskipWhiteSpace(itr, itrStart);
151 if (p != (itr = _simpleXmlUnskipXmlEntities(itr, itrStart))) p = itr;
153 if (p != (itr = _simpleXmlUnskipWhiteSpace(itr, itrStart))) p = itr;
160 static const char* _simpleXmlFindStartTag(const char* itr, const char* itrEnd)
162 return (const char*)memchr(itr, '<', itrEnd - itr);
166 static const char* _simpleXmlFindEndTag(const char* itr, const char* itrEnd)
168 bool insideQuote = false;
169 for (; itr < itrEnd; itr++) {
170 if (*itr == '"') insideQuote = !insideQuote;
172 if ((*itr == '>') || (*itr == '<'))
180 static const char* _simpleXmlFindEndCommentTag(const char* itr, const char* itrEnd)
182 for (; itr < itrEnd; itr++) {
183 if ((*itr == '-') && ((itr + 1 < itrEnd) && (*(itr + 1) == '-')) && ((itr + 2 < itrEnd) && (*(itr + 2) == '>'))) return itr + 2;
189 static const char* _simpleXmlFindEndCdataTag(const char* itr, const char* itrEnd)
191 for (; itr < itrEnd; itr++) {
192 if ((*itr == ']') && ((itr + 1 < itrEnd) && (*(itr + 1) == ']')) && ((itr + 2 < itrEnd) && (*(itr + 2) == '>'))) return itr + 2;
198 static const char* _simpleXmlFindDoctypeChildEndTag(const char* itr, const char* itrEnd)
200 for (; itr < itrEnd; itr++) {
201 if (*itr == '>') return itr;
207 /************************************************************************/
208 /* External Class Implementation */
209 /************************************************************************/
212 bool simpleXmlParseAttributes(const char* buf, unsigned bufLength, simpleXMLAttributeCb func, const void* data)
214 const char *itr = buf, *itrEnd = buf + bufLength;
215 char* tmpBuf = (char*)alloca(bufLength + 1);
217 if (!buf || !func) return false;
219 while (itr < itrEnd) {
220 const char* p = _skipWhiteSpacesAndXmlEntities(itr, itrEnd);
221 const char *key, *keyEnd, *value, *valueEnd;
224 if (p == itrEnd) return true;
227 for (keyEnd = key; keyEnd < itrEnd; keyEnd++) {
228 if ((*keyEnd == '=') || (isspace((unsigned char)*keyEnd))) break;
230 if (keyEnd == itrEnd) return false;
231 if (keyEnd == key) continue;
233 if (*keyEnd == '=') value = keyEnd + 1;
235 value = (const char*)memchr(keyEnd, '=', itrEnd - keyEnd);
236 if (!value) return false;
239 keyEnd = _simpleXmlUnskipXmlEntities(keyEnd, key);
241 value = _skipWhiteSpacesAndXmlEntities(value, itrEnd);
242 if (value == itrEnd) return false;
244 if ((*value == '"') || (*value == '\'')) {
245 valueEnd = (const char*)memchr(value + 1, *value, itrEnd - value);
246 if (!valueEnd) return false;
249 valueEnd = _simpleXmlFindWhiteSpace(value, itrEnd);
254 value = _skipWhiteSpacesAndXmlEntities(value, itrEnd);
255 valueEnd = _unskipWhiteSpacesAndXmlEntities(valueEnd, value);
257 memcpy(tmpBuf, key, keyEnd - key);
258 tmpBuf[keyEnd - key] = '\0';
260 tval = tmpBuf + (keyEnd - key) + 1;
262 while (value < valueEnd) {
263 value = _simpleXmlSkipXmlEntities(value, valueEnd);
269 #ifdef THORVG_LOG_ENABLED
270 if (!func((void*)data, tmpBuf, tval)) printf("SVG: Unsupported attributes used [Elements type: %s][Attribute: %s]\n", simpleXmlNodeTypeToString(((SvgLoaderData*)data)->svgParse->node->type).c_str(), tmpBuf);
272 func((void*)data, tmpBuf, tval);
279 bool simpleXmlParse(const char* buf, unsigned bufLength, bool strip, simpleXMLCb func, const void* data)
281 const char *itr = buf, *itrEnd = buf + bufLength;
283 if (!buf || !func) return false;
285 #define CB(type, start, end) \
287 size_t _sz = end - start; \
289 _ret = func((void*)data, type, start, _sz); \
294 while (itr < itrEnd) {
296 if (itr + 1 >= itrEnd) {
297 CB(SimpleXMLType::Error, itr, itrEnd);
305 type = SimpleXMLType::Close;
307 } else if (itr[1] == '?') {
308 type = SimpleXMLType::Processing;
310 } else if (itr[1] == '!') {
311 if ((itr + sizeof("<!DOCTYPE>") - 1 < itrEnd) && (!memcmp(itr + 2, "DOCTYPE", sizeof("DOCTYPE") - 1)) && ((itr[2 + sizeof("DOCTYPE") - 1] == '>') || (isspace((unsigned char)itr[2 + sizeof("DOCTYPE") - 1])))) {
312 type = SimpleXMLType::Doctype;
313 toff = sizeof("!DOCTYPE") - 1;
314 } else if ((itr + sizeof("<!---->") - 1 < itrEnd) && (!memcmp(itr + 2, "--", sizeof("--") - 1))) {
315 type = SimpleXMLType::Comment;
316 toff = sizeof("!--") - 1;
317 } else if ((itr + sizeof("<![CDATA[]]>") - 1 < itrEnd) && (!memcmp(itr + 2, "[CDATA[", sizeof("[CDATA[") - 1))) {
318 type = SimpleXMLType::CData;
319 toff = sizeof("![CDATA[") - 1;
320 } else if (itr + sizeof("<!>") - 1 < itrEnd) {
321 type = SimpleXMLType::DoctypeChild;
322 toff = sizeof("!") - 1;
324 type = SimpleXMLType::Open;
328 type = SimpleXMLType::Open;
332 if (type == SimpleXMLType::CData) p = _simpleXmlFindEndCdataTag(itr + 1 + toff, itrEnd);
333 else if (type == SimpleXMLType::DoctypeChild) p = _simpleXmlFindDoctypeChildEndTag(itr + 1 + toff, itrEnd);
334 else if (type == SimpleXMLType::Comment) p = _simpleXmlFindEndCommentTag(itr + 1 + toff, itrEnd);
335 else p = _simpleXmlFindEndTag(itr + 1 + toff, itrEnd);
337 if ((p) && (*p == '<')) {
338 type = SimpleXMLType::Error;
343 const char *start, *end;
345 start = itr + 1 + toff;
349 case SimpleXMLType::Open: {
351 type = SimpleXMLType::OpenEmpty;
356 case SimpleXMLType::CData: {
357 if (!memcmp(p - 2, "]]", 2)) end -= 2;
360 case SimpleXMLType::Processing: {
361 if (p[-1] == '?') end--;
364 case SimpleXMLType::Comment: {
365 if (!memcmp(p - 2, "--", 2)) end -= 2;
373 if ((strip) && (type != SimpleXMLType::Error) && (type != SimpleXMLType::CData)) {
374 start = _skipWhiteSpacesAndXmlEntities(start, end);
375 end = _unskipWhiteSpacesAndXmlEntities(end, start);
378 CB(type, start, end);
380 if (type != SimpleXMLType::Error) itr = p + 1;
383 CB(SimpleXMLType::Error, itr, itrEnd);
392 p = _skipWhiteSpacesAndXmlEntities(p, itrEnd);
394 CB(SimpleXMLType::Ignored, itr, p);
399 p = _simpleXmlFindStartTag(itr, itrEnd);
403 if (strip) end = _unskipWhiteSpacesAndXmlEntities(end, itr);
405 if (itr != end) CB(SimpleXMLType::Data, itr, end);
407 if ((strip) && (end < p)) CB(SimpleXMLType::Ignored, end, p);
419 bool simpleXmlParseW3CAttribute(const char* buf, simpleXMLAttributeCb func, const void* data)
426 if (!buf) return false;
428 end = buf + strlen(buf);
429 key = (char*)alloca(end - buf + 1);
430 val = (char*)alloca(end - buf + 1);
432 if (buf == end) return true;
435 char* sep = (char*)strchr(buf, ':');
436 next = (char*)strchr(buf, ';');
441 if (next == nullptr && sep != nullptr) {
442 memcpy(key, buf, sep - buf);
443 key[sep - buf] = '\0';
445 memcpy(val, sep + 1, end - sep - 1);
446 val[end - sep - 1] = '\0';
447 } else if (sep < next && sep != nullptr) {
448 memcpy(key, buf, sep - buf);
449 key[sep - buf] = '\0';
451 memcpy(val, sep + 1, next - sep - 1);
452 val[next - sep - 1] = '\0';
454 memcpy(key, buf, next - buf);
455 key[next - buf] = '\0';
459 if (!func((void*)data, key, val)) return false;
463 } while (next != nullptr);
469 const char* simpleXmlFindAttributesTag(const char* buf, unsigned bufLength)
471 const char *itr = buf, *itrEnd = buf + bufLength;
473 for (; itr < itrEnd; itr++) {
474 if (!isspace((unsigned char)*itr)) {
475 //User skip tagname and already gave it the attributes.
476 if (*itr == '=') return buf;
478 itr = _simpleXmlUnskipXmlEntities(itr, buf);
479 if (itr == itrEnd) return nullptr;