2 * Copyright (c) 2020 - 2022 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
33 #include "tvgXmlParser.h"
35 /************************************************************************/
36 /* Internal Class Implementation */
37 /************************************************************************/
39 bool _isIgnoreUnsupportedLogAttributes(TVG_UNUSED const char* tagAttribute, TVG_UNUSED const char* tagValue)
41 #ifdef THORVG_LOG_ENABLED
42 const auto attributesNum = 6;
46 bool tagWildcard; //If true, it is assumed that a wildcard is used after the tag. (ex: tagName*)
49 {"id", false, nullptr},
50 {"data-name", false, nullptr},
51 {"overflow", false, "visible"},
52 {"version", false, nullptr},
53 {"xmlns", true, nullptr},
54 {"xml:space", false, nullptr},
57 for (unsigned int i = 0; i < attributesNum; ++i) {
58 if (!strncmp(tagAttribute, attributes[i].tag, attributes[i].tagWildcard ? strlen(attributes[i].tag) : strlen(tagAttribute))) {
59 if (attributes[i].value && tagValue) {
60 if (!strncmp(tagValue, attributes[i].value, strlen(tagValue))) {
73 static const char* _simpleXmlFindWhiteSpace(const char* itr, const char* itrEnd)
75 for (; itr < itrEnd; itr++) {
76 if (isspace((unsigned char)*itr)) break;
82 static const char* _simpleXmlSkipWhiteSpace(const char* itr, const char* itrEnd)
84 for (; itr < itrEnd; itr++) {
85 if (!isspace((unsigned char)*itr)) break;
91 static const char* _simpleXmlUnskipWhiteSpace(const char* itr, const char* itrStart)
93 for (itr--; itr > itrStart; itr--) {
94 if (!isspace((unsigned char)*itr)) break;
100 static const char* _simpleXmlSkipXmlEntities(const char* itr, const char* itrEnd)
103 while (itr < itrEnd && *itr == '&') {
104 for (int i = 0; i < NUMBER_OF_XML_ENTITIES; ++i) {
105 if (strncmp(itr, xmlEntity[i], xmlEntityLength[i]) == 0) {
106 itr += xmlEntityLength[i];
117 static const char* _simpleXmlUnskipXmlEntities(const char* itr, const char* itrStart)
120 while (itr > itrStart && *(itr - 1) == ';') {
121 for (int i = 0; i < NUMBER_OF_XML_ENTITIES; ++i) {
122 if (itr - xmlEntityLength[i] > itrStart &&
123 strncmp(itr - xmlEntityLength[i], xmlEntity[i], xmlEntityLength[i]) == 0) {
124 itr -= xmlEntityLength[i];
135 static const char* _skipWhiteSpacesAndXmlEntities(const char* itr, const char* itrEnd)
137 itr = _simpleXmlSkipWhiteSpace(itr, itrEnd);
140 if (p != (itr = _simpleXmlSkipXmlEntities(itr, itrEnd))) p = itr;
142 if (p != (itr = _simpleXmlSkipWhiteSpace(itr, itrEnd))) p = itr;
149 static const char* _unskipWhiteSpacesAndXmlEntities(const char* itr, const char* itrStart)
151 itr = _simpleXmlUnskipWhiteSpace(itr, itrStart);
154 if (p != (itr = _simpleXmlUnskipXmlEntities(itr, itrStart))) p = itr;
156 if (p != (itr = _simpleXmlUnskipWhiteSpace(itr, itrStart))) p = itr;
163 static const char* _simpleXmlFindStartTag(const char* itr, const char* itrEnd)
165 return (const char*)memchr(itr, '<', itrEnd - itr);
169 static const char* _simpleXmlFindEndTag(const char* itr, const char* itrEnd)
171 bool insideQuote = false;
172 for (; itr < itrEnd; itr++) {
173 if (*itr == '"') insideQuote = !insideQuote;
175 if ((*itr == '>') || (*itr == '<'))
183 static const char* _simpleXmlFindEndCommentTag(const char* itr, const char* itrEnd)
185 for (; itr < itrEnd; itr++) {
186 if ((*itr == '-') && ((itr + 1 < itrEnd) && (*(itr + 1) == '-')) && ((itr + 2 < itrEnd) && (*(itr + 2) == '>'))) return itr + 2;
192 static const char* _simpleXmlFindEndCdataTag(const char* itr, const char* itrEnd)
194 for (; itr < itrEnd; itr++) {
195 if ((*itr == ']') && ((itr + 1 < itrEnd) && (*(itr + 1) == ']')) && ((itr + 2 < itrEnd) && (*(itr + 2) == '>'))) return itr + 2;
201 static const char* _simpleXmlFindDoctypeChildEndTag(const char* itr, const char* itrEnd)
203 for (; itr < itrEnd; itr++) {
204 if (*itr == '>') return itr;
210 static SimpleXMLType _getXMLType(const char* itr, const char* itrEnd, size_t &toff)
215 return SimpleXMLType::Close;
216 } else if (itr[1] == '?') {
218 return SimpleXMLType::Processing;
219 } else if (itr[1] == '!') {
220 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])))) {
221 toff = sizeof("!DOCTYPE") - 1;
222 return SimpleXMLType::Doctype;
223 } else if ((itr + sizeof("<![CDATA[]]>") - 1 < itrEnd) && (!memcmp(itr + 2, "[CDATA[", sizeof("[CDATA[") - 1))) {
224 toff = sizeof("![CDATA[") - 1;
225 return SimpleXMLType::CData;
226 } else if ((itr + sizeof("<!---->") - 1 < itrEnd) && (!memcmp(itr + 2, "--", sizeof("--") - 1))) {
227 toff = sizeof("!--") - 1;
228 return SimpleXMLType::Comment;
229 } else if (itr + sizeof("<!>") - 1 < itrEnd) {
230 toff = sizeof("!") - 1;
231 return SimpleXMLType::DoctypeChild;
233 return SimpleXMLType::Open;
235 return SimpleXMLType::Open;
239 /************************************************************************/
240 /* External Class Implementation */
241 /************************************************************************/
243 const char* simpleXmlNodeTypeToString(TVG_UNUSED SvgNodeType type)
245 #ifdef THORVG_LOG_ENABLED
246 static const char* TYPE_NAMES[] = {
269 return TYPE_NAMES[(int) type];
275 bool isIgnoreUnsupportedLogElements(TVG_UNUSED const char* tagName)
277 #ifdef THORVG_LOG_ENABLED
278 const auto elementsNum = 1;
279 const char* const elements[] = { "title" };
281 for (unsigned int i = 0; i < elementsNum; ++i) {
282 if (!strncmp(tagName, elements[i], strlen(tagName))) {
293 bool simpleXmlParseAttributes(const char* buf, unsigned bufLength, simpleXMLAttributeCb func, const void* data)
295 const char *itr = buf, *itrEnd = buf + bufLength;
296 char* tmpBuf = (char*)alloca(bufLength + 1);
298 if (!buf || !func) return false;
300 while (itr < itrEnd) {
301 const char* p = _skipWhiteSpacesAndXmlEntities(itr, itrEnd);
302 const char *key, *keyEnd, *value, *valueEnd;
305 if (p == itrEnd) return true;
308 for (keyEnd = key; keyEnd < itrEnd; keyEnd++) {
309 if ((*keyEnd == '=') || (isspace((unsigned char)*keyEnd))) break;
311 if (keyEnd == itrEnd) return false;
312 if (keyEnd == key) continue;
314 if (*keyEnd == '=') value = keyEnd + 1;
316 value = (const char*)memchr(keyEnd, '=', itrEnd - keyEnd);
317 if (!value) return false;
320 keyEnd = _simpleXmlUnskipXmlEntities(keyEnd, key);
322 value = _skipWhiteSpacesAndXmlEntities(value, itrEnd);
323 if (value == itrEnd) return false;
325 if ((*value == '"') || (*value == '\'')) {
326 valueEnd = (const char*)memchr(value + 1, *value, itrEnd - value);
327 if (!valueEnd) return false;
330 valueEnd = _simpleXmlFindWhiteSpace(value, itrEnd);
335 value = _skipWhiteSpacesAndXmlEntities(value, itrEnd);
336 valueEnd = _unskipWhiteSpacesAndXmlEntities(valueEnd, value);
338 memcpy(tmpBuf, key, keyEnd - key);
339 tmpBuf[keyEnd - key] = '\0';
341 tval = tmpBuf + (keyEnd - key) + 1;
343 while (value < valueEnd) {
344 value = _simpleXmlSkipXmlEntities(value, valueEnd);
350 if (!func((void*)data, tmpBuf, tval)) {
351 if (!_isIgnoreUnsupportedLogAttributes(tmpBuf, tval)) {
352 TVGLOG("SVG", "Unsupported attributes used [Elements type: %s][Id : %s][Attribute: %s][Value: %s]", simpleXmlNodeTypeToString(((SvgLoaderData*)data)->svgParse->node->type), ((SvgLoaderData*)data)->svgParse->node->id ? ((SvgLoaderData*)data)->svgParse->node->id : "NO_ID", tmpBuf, tval ? tval : "NONE");
360 bool simpleXmlParse(const char* buf, unsigned bufLength, bool strip, simpleXMLCb func, const void* data)
362 const char *itr = buf, *itrEnd = buf + bufLength;
364 if (!buf || !func) return false;
366 while (itr < itrEnd) {
369 if (itr + 1 >= itrEnd) return false;
372 SimpleXMLType type = _getXMLType(itr, itrEnd, toff);
375 if (type == SimpleXMLType::CData) p = _simpleXmlFindEndCdataTag(itr + 1 + toff, itrEnd);
376 else if (type == SimpleXMLType::DoctypeChild) p = _simpleXmlFindDoctypeChildEndTag(itr + 1 + toff, itrEnd);
377 else if (type == SimpleXMLType::Comment) p = _simpleXmlFindEndCommentTag(itr + 1 + toff, itrEnd);
378 else p = _simpleXmlFindEndTag(itr + 1 + toff, itrEnd);
381 //Invalid case: '<' nested
382 if (*p == '<') return false;
383 const char *start, *end;
385 start = itr + 1 + toff;
389 case SimpleXMLType::Open: {
391 type = SimpleXMLType::OpenEmpty;
396 case SimpleXMLType::CData: {
397 if (!memcmp(p - 2, "]]", 2)) end -= 2;
400 case SimpleXMLType::Processing: {
401 if (p[-1] == '?') end--;
404 case SimpleXMLType::Comment: {
405 if (!memcmp(p - 2, "--", 2)) end -= 2;
413 if (strip && (type != SimpleXMLType::CData)) {
414 start = _skipWhiteSpacesAndXmlEntities(start, end);
415 end = _unskipWhiteSpacesAndXmlEntities(end, start);
418 if (!func((void*)data, type, start, (unsigned int)(end - start))) return false;
429 p = _skipWhiteSpacesAndXmlEntities(p, itrEnd);
431 if (!func((void*)data, SimpleXMLType::Ignored, itr, (unsigned int)(p - itr))) return false;
436 p = _simpleXmlFindStartTag(itr, itrEnd);
440 if (strip) end = _unskipWhiteSpacesAndXmlEntities(end, itr);
442 if (itr != end && !func((void*)data, SimpleXMLType::Data, itr, (unsigned int)(end - itr))) return false;
444 if (strip && (end < p) && !func((void*)data, SimpleXMLType::Ignored, end, (unsigned int)(p - end))) return false;
453 bool simpleXmlParseW3CAttribute(const char* buf, unsigned buflen, simpleXMLAttributeCb func, const void* data)
460 if (!buf) return false;
463 key = (char*)alloca(end - buf + 1);
464 val = (char*)alloca(end - buf + 1);
466 if (buf == end) return true;
469 char* sep = (char*)strchr(buf, ':');
470 next = (char*)strchr(buf, ';');
476 if (next >= end) next = nullptr;
481 if (next == nullptr && sep != nullptr) {
482 memcpy(key, buf, sep - buf);
483 key[sep - buf] = '\0';
485 memcpy(val, sep + 1, end - sep - 1);
486 val[end - sep - 1] = '\0';
487 } else if (sep < next && sep != nullptr) {
488 memcpy(key, buf, sep - buf);
489 key[sep - buf] = '\0';
491 memcpy(val, sep + 1, next - sep - 1);
492 val[next - sep - 1] = '\0';
494 memcpy(key, buf, next - buf);
495 key[next - buf] = '\0';
499 key = const_cast<char*>(_simpleXmlSkipWhiteSpace(key, key + strlen(key)));
500 key[_simpleXmlUnskipWhiteSpace(key + strlen(key) , key) - key] = '\0';
501 val = const_cast<char*>(_simpleXmlSkipWhiteSpace(val, val + strlen(val)));
502 val[_simpleXmlUnskipWhiteSpace(val + strlen(val) , val) - val] = '\0';
504 if (!func((void*)data, key, val)) {
505 if (!_isIgnoreUnsupportedLogAttributes(key, val)) {
506 TVGLOG("SVG", "Unsupported attributes used [Elements type: %s][Id : %s][Attribute: %s][Value: %s]", simpleXmlNodeTypeToString(((SvgLoaderData*)data)->svgParse->node->type), ((SvgLoaderData*)data)->svgParse->node->id ? ((SvgLoaderData*)data)->svgParse->node->id : "NO_ID", key, val ? val : "NONE");
512 } while (next != nullptr);
520 * tag {}, .name {}, tag.name{}
522 const char* simpleXmlParseCSSAttribute(const char* buf, unsigned bufLength, char** tag, char** name, const char** attrs, unsigned* attrsLength)
524 if (!buf) return nullptr;
526 *tag = *name = nullptr;
529 auto itr = _simpleXmlSkipWhiteSpace(buf, buf + bufLength);
530 auto itrEnd = (const char*)memchr(buf, '{', bufLength);
532 if (!itrEnd || itr == itrEnd) return nullptr;
534 auto nextElement = (const char*)memchr(itrEnd, '}', bufLength - (itrEnd - buf));
535 if (!nextElement) return nullptr;
538 *attrsLength = nextElement - *attrs;
542 itrEnd = _simpleXmlUnskipWhiteSpace(itrEnd, itr);
543 if (*(itrEnd - 1) == '.') return nullptr;
545 for (p = itr; p < itrEnd; p++) {
546 if (*p == '.') break;
549 if (p == itr) *tag = strdup("all");
550 else *tag = strndup(itr, p - itr);
552 if (p == itrEnd) *name = nullptr;
553 else *name = strndup(p + 1, itrEnd - p - 1);
555 return (nextElement ? nextElement + 1 : nullptr);
559 const char* simpleXmlFindAttributesTag(const char* buf, unsigned bufLength)
561 const char *itr = buf, *itrEnd = buf + bufLength;
563 for (; itr < itrEnd; itr++) {
564 if (!isspace((unsigned char)*itr)) {
565 //User skip tagname and already gave it the attributes.
566 if (*itr == '=') return buf;
568 itr = _simpleXmlUnskipXmlEntities(itr, buf);
569 if (itr == itrEnd) return nullptr;