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 bool _isIgnoreUnsupportedLogAttributes(TVG_UNUSED const char* tagAttribute, TVG_UNUSED const char* tagValue)
40 #ifdef THORVG_LOG_ENABLED
41 const auto attributesNum = 6;
45 bool tagWildcard; //If true, it is assumed that a wildcard is used after the tag. (ex: tagName*)
48 {"id", false, nullptr},
49 {"data-name", false, nullptr},
50 {"overflow", false, "visible"},
51 {"version", false, nullptr},
52 {"xmlns", true, nullptr},
53 {"xml:space", false, nullptr},
56 for (unsigned int i = 0; i < attributesNum; ++i) {
57 if (!strncmp(tagAttribute, attributes[i].tag, attributes[i].tagWildcard ? strlen(attributes[i].tag) : strlen(tagAttribute))) {
58 if (attributes[i].value && tagValue) {
59 if (!strncmp(tagValue, attributes[i].value, strlen(tagValue))) {
72 static const char* _simpleXmlFindWhiteSpace(const char* itr, const char* itrEnd)
74 for (; itr < itrEnd; itr++) {
75 if (isspace((unsigned char)*itr)) break;
81 static const char* _simpleXmlSkipWhiteSpace(const char* itr, const char* itrEnd)
83 for (; itr < itrEnd; itr++) {
84 if (!isspace((unsigned char)*itr)) break;
90 static const char* _simpleXmlUnskipWhiteSpace(const char* itr, const char* itrStart)
92 for (itr--; itr > itrStart; itr--) {
93 if (!isspace((unsigned char)*itr)) break;
99 static const char* _simpleXmlSkipXmlEntities(const char* itr, const char* itrEnd)
102 while (itr < itrEnd && *itr == '&') {
103 for (int i = 0; i < NUMBER_OF_XML_ENTITIES; ++i) {
104 if (strncmp(itr, xmlEntity[i], xmlEntityLength[i]) == 0) {
105 itr += xmlEntityLength[i];
116 static const char* _simpleXmlUnskipXmlEntities(const char* itr, const char* itrStart)
119 while (itr > itrStart && *(itr - 1) == ';') {
120 for (int i = 0; i < NUMBER_OF_XML_ENTITIES; ++i) {
121 if (itr - xmlEntityLength[i] > itrStart &&
122 strncmp(itr - xmlEntityLength[i], xmlEntity[i], xmlEntityLength[i]) == 0) {
123 itr -= xmlEntityLength[i];
134 static const char* _skipWhiteSpacesAndXmlEntities(const char* itr, const char* itrEnd)
136 itr = _simpleXmlSkipWhiteSpace(itr, itrEnd);
139 if (p != (itr = _simpleXmlSkipXmlEntities(itr, itrEnd))) p = itr;
141 if (p != (itr = _simpleXmlSkipWhiteSpace(itr, itrEnd))) p = itr;
148 static const char* _unskipWhiteSpacesAndXmlEntities(const char* itr, const char* itrStart)
150 itr = _simpleXmlUnskipWhiteSpace(itr, itrStart);
153 if (p != (itr = _simpleXmlUnskipXmlEntities(itr, itrStart))) p = itr;
155 if (p != (itr = _simpleXmlUnskipWhiteSpace(itr, itrStart))) p = itr;
162 static const char* _simpleXmlFindStartTag(const char* itr, const char* itrEnd)
164 return (const char*)memchr(itr, '<', itrEnd - itr);
168 static const char* _simpleXmlFindEndTag(const char* itr, const char* itrEnd)
170 bool insideQuote = false;
171 for (; itr < itrEnd; itr++) {
172 if (*itr == '"') insideQuote = !insideQuote;
174 if ((*itr == '>') || (*itr == '<'))
182 static const char* _simpleXmlFindEndCommentTag(const char* itr, const char* itrEnd)
184 for (; itr < itrEnd; itr++) {
185 if ((*itr == '-') && ((itr + 1 < itrEnd) && (*(itr + 1) == '-')) && ((itr + 2 < itrEnd) && (*(itr + 2) == '>'))) return itr + 2;
191 static const char* _simpleXmlFindEndCdataTag(const char* itr, const char* itrEnd)
193 for (; itr < itrEnd; itr++) {
194 if ((*itr == ']') && ((itr + 1 < itrEnd) && (*(itr + 1) == ']')) && ((itr + 2 < itrEnd) && (*(itr + 2) == '>'))) return itr + 2;
200 static const char* _simpleXmlFindDoctypeChildEndTag(const char* itr, const char* itrEnd)
202 for (; itr < itrEnd; itr++) {
203 if (*itr == '>') return itr;
209 static SimpleXMLType _getXMLType(const char* itr, const char* itrEnd, size_t &toff)
214 return SimpleXMLType::Close;
215 } else if (itr[1] == '?') {
217 return SimpleXMLType::Processing;
218 } else if (itr[1] == '!') {
219 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])))) {
220 toff = sizeof("!DOCTYPE") - 1;
221 return SimpleXMLType::Doctype;
222 } else if (itr + sizeof("<!>") - 1 < itrEnd) {
223 toff = sizeof("!") - 1;
224 return SimpleXMLType::DoctypeChild;
225 } else if ((itr + sizeof("<![CDATA[]]>") - 1 < itrEnd) && (!memcmp(itr + 2, "[CDATA[", sizeof("[CDATA[") - 1))) {
226 toff = sizeof("![CDATA[") - 1;
227 return SimpleXMLType::CData;
228 } else if ((itr + sizeof("<!---->") - 1 < itrEnd) && (!memcmp(itr + 2, "--", sizeof("--") - 1))) {
229 toff = sizeof("!--") - 1;
230 return SimpleXMLType::Comment;
232 return SimpleXMLType::Open;
234 return SimpleXMLType::Open;
238 /************************************************************************/
239 /* External Class Implementation */
240 /************************************************************************/
242 const char* simpleXmlNodeTypeToString(TVG_UNUSED SvgNodeType type)
244 #ifdef THORVG_LOG_ENABLED
245 static const char* TYPE_NAMES[] = {
268 return TYPE_NAMES[(int) type];
274 bool isIgnoreUnsupportedLogElements(TVG_UNUSED const char* tagName)
276 #ifdef THORVG_LOG_ENABLED
277 const auto elementsNum = 1;
278 const char* const elements[] = { "title" };
280 for (unsigned int i = 0; i < elementsNum; ++i) {
281 if (!strncmp(tagName, elements[i], strlen(tagName))) {
292 bool simpleXmlParseAttributes(const char* buf, unsigned bufLength, simpleXMLAttributeCb func, const void* data)
294 const char *itr = buf, *itrEnd = buf + bufLength;
295 char* tmpBuf = (char*)alloca(bufLength + 1);
297 if (!buf || !func) return false;
299 while (itr < itrEnd) {
300 const char* p = _skipWhiteSpacesAndXmlEntities(itr, itrEnd);
301 const char *key, *keyEnd, *value, *valueEnd;
304 if (p == itrEnd) return true;
307 for (keyEnd = key; keyEnd < itrEnd; keyEnd++) {
308 if ((*keyEnd == '=') || (isspace((unsigned char)*keyEnd))) break;
310 if (keyEnd == itrEnd) return false;
311 if (keyEnd == key) continue;
313 if (*keyEnd == '=') value = keyEnd + 1;
315 value = (const char*)memchr(keyEnd, '=', itrEnd - keyEnd);
316 if (!value) return false;
319 keyEnd = _simpleXmlUnskipXmlEntities(keyEnd, key);
321 value = _skipWhiteSpacesAndXmlEntities(value, itrEnd);
322 if (value == itrEnd) return false;
324 if ((*value == '"') || (*value == '\'')) {
325 valueEnd = (const char*)memchr(value + 1, *value, itrEnd - value);
326 if (!valueEnd) return false;
329 valueEnd = _simpleXmlFindWhiteSpace(value, itrEnd);
334 value = _skipWhiteSpacesAndXmlEntities(value, itrEnd);
335 valueEnd = _unskipWhiteSpacesAndXmlEntities(valueEnd, value);
337 memcpy(tmpBuf, key, keyEnd - key);
338 tmpBuf[keyEnd - key] = '\0';
340 tval = tmpBuf + (keyEnd - key) + 1;
342 while (value < valueEnd) {
343 value = _simpleXmlSkipXmlEntities(value, valueEnd);
349 if (!func((void*)data, tmpBuf, tval)) {
350 if (!_isIgnoreUnsupportedLogAttributes(tmpBuf, tval)) {
351 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->c_str() : "NO_ID", tmpBuf, tval ? tval : "NONE");
359 bool simpleXmlParse(const char* buf, unsigned bufLength, bool strip, simpleXMLCb func, const void* data)
361 const char *itr = buf, *itrEnd = buf + bufLength;
363 if (!buf || !func) return false;
365 while (itr < itrEnd) {
368 if (itr + 1 >= itrEnd) return false;
371 SimpleXMLType type = _getXMLType(itr, itrEnd, toff);
374 if (type == SimpleXMLType::CData) p = _simpleXmlFindEndCdataTag(itr + 1 + toff, itrEnd);
375 else if (type == SimpleXMLType::DoctypeChild) p = _simpleXmlFindDoctypeChildEndTag(itr + 1 + toff, itrEnd);
376 else if (type == SimpleXMLType::Comment) p = _simpleXmlFindEndCommentTag(itr + 1 + toff, itrEnd);
377 else p = _simpleXmlFindEndTag(itr + 1 + toff, itrEnd);
380 //Invalid case: '<' nested
381 if (*p == '<') return false;
382 const char *start, *end;
384 start = itr + 1 + toff;
388 case SimpleXMLType::Open: {
390 type = SimpleXMLType::OpenEmpty;
395 case SimpleXMLType::CData: {
396 if (!memcmp(p - 2, "]]", 2)) end -= 2;
399 case SimpleXMLType::Processing: {
400 if (p[-1] == '?') end--;
403 case SimpleXMLType::Comment: {
404 if (!memcmp(p - 2, "--", 2)) end -= 2;
412 if (strip && (type != SimpleXMLType::CData)) {
413 start = _skipWhiteSpacesAndXmlEntities(start, end);
414 end = _unskipWhiteSpacesAndXmlEntities(end, start);
417 if (!func((void*)data, type, start, (unsigned int)(end - start))) return false;
428 p = _skipWhiteSpacesAndXmlEntities(p, itrEnd);
430 if (!func((void*)data, SimpleXMLType::Ignored, itr, (unsigned int)(p - itr))) return false;
435 p = _simpleXmlFindStartTag(itr, itrEnd);
439 if (strip) end = _unskipWhiteSpacesAndXmlEntities(end, itr);
441 if (itr != end && !func((void*)data, SimpleXMLType::Data, itr, (size_t)(end - itr))) return false;
443 if (strip && (end < p) && !func((void*)data, SimpleXMLType::Ignored, end, (size_t)(p - end))) return false;
452 bool simpleXmlParseW3CAttribute(const char* buf, simpleXMLAttributeCb func, const void* data)
459 if (!buf) return false;
461 end = buf + strlen(buf);
462 key = (char*)alloca(end - buf + 1);
463 val = (char*)alloca(end - buf + 1);
465 if (buf == end) return true;
468 char* sep = (char*)strchr(buf, ':');
469 next = (char*)strchr(buf, ';');
474 if (next == nullptr && sep != nullptr) {
475 memcpy(key, buf, sep - buf);
476 key[sep - buf] = '\0';
478 memcpy(val, sep + 1, end - sep - 1);
479 val[end - sep - 1] = '\0';
480 } else if (sep < next && sep != nullptr) {
481 memcpy(key, buf, sep - buf);
482 key[sep - buf] = '\0';
484 memcpy(val, sep + 1, next - sep - 1);
485 val[next - sep - 1] = '\0';
487 memcpy(key, buf, next - buf);
488 key[next - buf] = '\0';
492 key = const_cast<char*>(_simpleXmlSkipWhiteSpace(key, key + strlen(key)));
493 key[_simpleXmlUnskipWhiteSpace(key + strlen(key) , key) - key] = '\0';
494 val = const_cast<char*>(_simpleXmlSkipWhiteSpace(val, val + strlen(val)));
495 val[_simpleXmlUnskipWhiteSpace(val + strlen(val) , val) - val] = '\0';
497 if (!func((void*)data, key, val)) {
498 if (!_isIgnoreUnsupportedLogAttributes(key, val)) {
499 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->c_str() : "NO_ID", key, val ? val : "NONE");
505 } while (next != nullptr);
511 const char* simpleXmlFindAttributesTag(const char* buf, unsigned bufLength)
513 const char *itr = buf, *itrEnd = buf + bufLength;
515 for (; itr < itrEnd; itr++) {
516 if (!isspace((unsigned char)*itr)) {
517 //User skip tagname and already gave it the attributes.
518 if (*itr == '=') return buf;
520 itr = _simpleXmlUnskipXmlEntities(itr, buf);
521 if (itr == itrEnd) return nullptr;