Debian Codegraph @

thirdparty/thorvg/src/loaders/svg/tvgXmlParser.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved.
3
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:
10
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13
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
20 * SOFTWARE.
21 */
22
23 #include <cstring>
24 #include <ctype.h>
25 #include <string>
26
27 #ifdef _WIN32
28 #include <malloc.h>
29 #elif defined(__linux__) || defined(__ZEPHYR__)
30 #include <alloca.h>
31 #else
32 #include <stdlib.h>
33 #endif
34
35 #include "tvgXmlParser.h"
36 #include "tvgStr.h"
37
38 /************************************************************************/
39 /* Internal Class Implementation */
40 /************************************************************************/
41
42 bool _isIgnoreUnsupportedLogAttributes(TVG_UNUSED const char* tagAttribute, TVG_UNUSED const char* tagValue)
43 {
44 #ifdef THORVG_LOG_ENABLED
45 const auto attributesNum = 6;
46 const struct
47 {
48 const char* tag;
49 bool tagWildcard; //If true, it is assumed that a wildcard is used after the tag. (ex: tagName*)
50 const char* value;
51 } attributes[] = {
52 {"id", false, nullptr},
53 {"data-name", false, nullptr},
54 {"overflow", false, "visible"},
55 {"version", false, nullptr},
56 {"xmlns", true, nullptr},
57 {"xml:space", false, nullptr},
58 };
59
60 for (unsigned int i = 0; i < attributesNum; ++i) {
61 if (!strncmp(tagAttribute, attributes[i].tag, attributes[i].tagWildcard ? strlen(attributes[i].tag) : strlen(tagAttribute))) {
62 if (attributes[i].value && tagValue) {
63 if (!strncmp(tagValue, attributes[i].value, strlen(tagValue))) {
64 return true;
65 } else continue;
66 }
67 return true;
68 }
69 }
70 return false;
71 #endif
72 return true;
73 }
74
75
76 static const char* _simpleXmlFindWhiteSpace(const char* itr, const char* itrEnd)
77 {
78 for (; itr < itrEnd; itr++) {
79 if (isspace((unsigned char)*itr)) break;
80 }
81 return itr;
82 }
83
84
85 static const char* _simpleXmlSkipWhiteSpace(const char* itr, const char* itrEnd)
86 {
87 for (; itr < itrEnd; itr++) {
88 if (!isspace((unsigned char)*itr)) break;
89 }
90 return itr;
91 }
92
93
94 static const char* _simpleXmlUnskipWhiteSpace(const char* itr, const char* itrStart)
95 {
96 for (itr--; itr > itrStart; itr--) {
97 if (!isspace((unsigned char)*itr)) break;
98 }
99 return itr + 1;
100 }
101
102
103 static const char* _simpleXmlSkipXmlEntities(const char* itr, const char* itrEnd)
104 {
105 auto p = itr;
106 while (itr < itrEnd && *itr == '&') {
107 for (int i = 0; i < NUMBER_OF_XML_ENTITIES; ++i) {
108 if (strncmp(itr, xmlEntity[i], xmlEntityLength[i]) == 0) {
109 itr += xmlEntityLength[i];
110 break;
111 }
112 }
113 if (itr == p) break;
114 p = itr;
115 }
116 return itr;
117 }
118
119
120 static const char* _simpleXmlUnskipXmlEntities(const char* itr, const char* itrStart)
121 {
122 auto p = itr;
123 while (itr > itrStart && *(itr - 1) == ';') {
124 for (int i = 0; i < NUMBER_OF_XML_ENTITIES; ++i) {
125 if (itr - xmlEntityLength[i] > itrStart &&
126 strncmp(itr - xmlEntityLength[i], xmlEntity[i], xmlEntityLength[i]) == 0) {
127 itr -= xmlEntityLength[i];
128 break;
129 }
130 }
131 if (itr == p) break;
132 p = itr;
133 }
134 return itr;
135 }
136
137
138 static const char* _skipWhiteSpacesAndXmlEntities(const char* itr, const char* itrEnd)
139 {
140 itr = _simpleXmlSkipWhiteSpace(itr, itrEnd);
141 auto p = itr;
142 while (true) {
143 if (p != (itr = _simpleXmlSkipXmlEntities(itr, itrEnd))) p = itr;
144 else break;
145 if (p != (itr = _simpleXmlSkipWhiteSpace(itr, itrEnd))) p = itr;
146 else break;
147 }
148 return itr;
149 }
150
151
152 static const char* _unskipWhiteSpacesAndXmlEntities(const char* itr, const char* itrStart)
153 {
154 itr = _simpleXmlUnskipWhiteSpace(itr, itrStart);
155 auto p = itr;
156 while (true) {
157 if (p != (itr = _simpleXmlUnskipXmlEntities(itr, itrStart))) p = itr;
158 else break;
159 if (p != (itr = _simpleXmlUnskipWhiteSpace(itr, itrStart))) p = itr;
160 else break;
161 }
162 return itr;
163 }
164
165
166 static const char* _simpleXmlFindStartTag(const char* itr, const char* itrEnd)
167 {
168 return (const char*)memchr(itr, '<', itrEnd - itr);
169 }
170
171
172 static const char* _simpleXmlFindEndTag(const char* itr, const char* itrEnd)
173 {
174 bool insideQuote[2] = {false, false}; // 0: ", 1: '
175 for (; itr < itrEnd; itr++) {
176 if (*itr == '"' && !insideQuote[1]) insideQuote[0] = !insideQuote[0];
177 if (*itr == '\'' && !insideQuote[0]) insideQuote[1] = !insideQuote[1];
178 if (!insideQuote[0] && !insideQuote[1]) {
179 if ((*itr == '>') || (*itr == '<'))
180 return itr;
181 }
182 }
183 return nullptr;
184 }
185
186
187 static const char* _simpleXmlFindEndCommentTag(const char* itr, const char* itrEnd)
188 {
189 for (; itr < itrEnd; itr++) {
190 if ((*itr == '-') && ((itr + 1 < itrEnd) && (*(itr + 1) == '-')) && ((itr + 2 < itrEnd) && (*(itr + 2) == '>'))) return itr + 2;
191 }
192 return nullptr;
193 }
194
195
196 static const char* _simpleXmlFindEndCdataTag(const char* itr, const char* itrEnd)
197 {
198 for (; itr < itrEnd; itr++) {
199 if ((*itr == ']') && ((itr + 1 < itrEnd) && (*(itr + 1) == ']')) && ((itr + 2 < itrEnd) && (*(itr + 2) == '>'))) return itr + 2;
200 }
201 return nullptr;
202 }
203
204
205 static const char* _simpleXmlFindDoctypeChildEndTag(const char* itr, const char* itrEnd)
206 {
207 for (; itr < itrEnd; itr++) {
208 if (*itr == '>') return itr;
209 }
210 return nullptr;
211 }
212
213
214 static SimpleXMLType _getXMLType(const char* itr, const char* itrEnd, size_t &toff)
215 {
216 toff = 0;
217 if (itr[1] == '/') {
218 toff = 1;
219 return SimpleXMLType::Close;
220 } else if (itr[1] == '?') {
221 toff = 1;
222 return SimpleXMLType::Processing;
223 } else if (itr[1] == '!') {
224 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])))) {
225 toff = sizeof("!DOCTYPE") - 1;
226 return SimpleXMLType::Doctype;
227 } else if ((itr + sizeof("<![CDATA[]]>") - 1 < itrEnd) && (!memcmp(itr + 2, "[CDATA[", sizeof("[CDATA[") - 1))) {
228 toff = sizeof("![CDATA[") - 1;
229 return SimpleXMLType::CData;
230 } else if ((itr + sizeof("<!---->") - 1 < itrEnd) && (!memcmp(itr + 2, "--", sizeof("--") - 1))) {
231 toff = sizeof("!--") - 1;
232 return SimpleXMLType::Comment;
233 } else if (itr + sizeof("<!>") - 1 < itrEnd) {
234 toff = sizeof("!") - 1;
235 return SimpleXMLType::DoctypeChild;
236 }
237 return SimpleXMLType::Open;
238 }
239 return SimpleXMLType::Open;
240 }
241
242
243 /************************************************************************/
244 /* External Class Implementation */
245 /************************************************************************/
246
247 const char* simpleXmlNodeTypeToString(TVG_UNUSED SvgNodeType type)
248 {
249 #ifdef THORVG_LOG_ENABLED
250 static const char* TYPE_NAMES[] = {
251 "Svg",
252 "G",
253 "Defs",
254 "Animation",
255 "Arc",
256 "Circle",
257 "Ellipse",
258 "Image",
259 "Line",
260 "Path",
261 "Polygon",
262 "Polyline",
263 "Rect",
264 "Text",
265 "TextArea",
266 "Tspan",
267 "Use",
268 "Video",
269 "ClipPath",
270 "Mask",
271 "Symbol",
272 "Unknown",
273 };
274 return TYPE_NAMES[(int) type];
275 #endif
276 return nullptr;
277 }
278
279
280 bool isIgnoreUnsupportedLogElements(TVG_UNUSED const char* tagName)
281 {
282 #ifdef THORVG_LOG_ENABLED
283 const auto elementsNum = 1;
284 const char* const elements[] = { "title" };
285
286 for (unsigned int i = 0; i < elementsNum; ++i) {
287 if (!strncmp(tagName, elements[i], strlen(tagName))) {
288 return true;
289 }
290 }
291 return false;
292 #else
293 return true;
294 #endif
295 }
296
297
298 bool simpleXmlParseAttributes(const char* buf, unsigned bufLength, simpleXMLAttributeCb func, const void* data)
299 {
300 const char *itr = buf, *itrEnd = buf + bufLength;
301 char* tmpBuf = (char*)malloc(bufLength + 1);
302
303 if (!buf || !func || !tmpBuf) goto error;
304
305 while (itr < itrEnd) {
306 const char* p = _skipWhiteSpacesAndXmlEntities(itr, itrEnd);
307 const char *key, *keyEnd, *value, *valueEnd;
308 char* tval;
309
310 if (p == itrEnd) goto success;
311
312 key = p;
313 for (keyEnd = key; keyEnd < itrEnd; keyEnd++) {
314 if ((*keyEnd == '=') || (isspace((unsigned char)*keyEnd))) break;
315 }
316 if (keyEnd == itrEnd) goto error;
317 if (keyEnd == key) { // There is no key. This case is invalid, but explores the following syntax.
318 itr = keyEnd + 1;
319 continue;
320 }
321
322 if (*keyEnd == '=') value = keyEnd + 1;
323 else {
324 value = (const char*)memchr(keyEnd, '=', itrEnd - keyEnd);
325 if (!value) goto error;
326 value++;
327 }
328 keyEnd = _simpleXmlUnskipXmlEntities(keyEnd, key);
329
330 value = _skipWhiteSpacesAndXmlEntities(value, itrEnd);
331 if (value == itrEnd) goto error;
332
333 if ((*value == '"') || (*value == '\'')) {
334 valueEnd = (const char*)memchr(value + 1, *value, itrEnd - value);
335 if (!valueEnd) goto error;
336 value++;
337 } else {
338 valueEnd = _simpleXmlFindWhiteSpace(value, itrEnd);
339 }
340
341 itr = valueEnd + 1;
342
343 value = _skipWhiteSpacesAndXmlEntities(value, itrEnd);
344 valueEnd = _unskipWhiteSpacesAndXmlEntities(valueEnd, value);
345
346 memcpy(tmpBuf, key, keyEnd - key);
347 tmpBuf[keyEnd - key] = '\0';
348
349 tval = tmpBuf + (keyEnd - key) + 1;
350 int i = 0;
351 while (value < valueEnd) {
352 value = _simpleXmlSkipXmlEntities(value, valueEnd);
353 tval[i++] = *value;
354 value++;
355 }
356 tval[i] = '\0';
357
358 if (!func((void*)data, tmpBuf, tval)) {
359 if (!_isIgnoreUnsupportedLogAttributes(tmpBuf, tval)) {
360 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");
361 }
362 }
363 }
364
365 success:
366 free(tmpBuf);
367 return true;
368
369 error:
370 free(tmpBuf);
371 return false;
372 }
373
374
375 bool simpleXmlParse(const char* buf, unsigned bufLength, bool strip, simpleXMLCb func, const void* data)
376 {
377 const char *itr = buf, *itrEnd = buf + bufLength;
378
379 if (!buf || !func) return false;
380
381 while (itr < itrEnd) {
382 if (itr[0] == '<') {
383 //Invalid case
384 if (itr + 1 >= itrEnd) return false;
385
386 size_t toff = 0;
387 SimpleXMLType type = _getXMLType(itr, itrEnd, toff);
388
389 const char* p;
390 if (type == SimpleXMLType::CData) p = _simpleXmlFindEndCdataTag(itr + 1 + toff, itrEnd);
391 else if (type == SimpleXMLType::DoctypeChild) p = _simpleXmlFindDoctypeChildEndTag(itr + 1 + toff, itrEnd);
392 else if (type == SimpleXMLType::Comment) p = _simpleXmlFindEndCommentTag(itr + 1 + toff, itrEnd);
393 else p = _simpleXmlFindEndTag(itr + 1 + toff, itrEnd);
394
395 if (p) {
396 //Invalid case: '<' nested
397 if (*p == '<' && type != SimpleXMLType::Doctype) return false;
398 const char *start, *end;
399
400 start = itr + 1 + toff;
401 end = p;
402
403 switch (type) {
404 case SimpleXMLType::Open: {
405 if (p[-1] == '/') {
406 type = SimpleXMLType::OpenEmpty;
407 end--;
408 }
409 break;
410 }
411 case SimpleXMLType::CData: {
412 if (!memcmp(p - 2, "]]", 2)) end -= 2;
413 break;
414 }
415 case SimpleXMLType::Processing: {
416 if (p[-1] == '?') end--;
417 break;
418 }
419 case SimpleXMLType::Comment: {
420 if (!memcmp(p - 2, "--", 2)) end -= 2;
421 break;
422 }
423 default: {
424 break;
425 }
426 }
427
428 if (strip && (type != SimpleXMLType::CData)) {
429 start = _skipWhiteSpacesAndXmlEntities(start, end);
430 end = _unskipWhiteSpacesAndXmlEntities(end, start);
431 }
432
433 if (!func((void*)data, type, start, (unsigned int)(end - start))) return false;
434
435 itr = p + 1;
436 } else {
437 return false;
438 }
439 } else {
440 const char *p, *end;
441
442 if (strip) {
443 p = itr;
444 p = _skipWhiteSpacesAndXmlEntities(p, itrEnd);
445 if (p) {
446 if (!func((void*)data, SimpleXMLType::Ignored, itr, (unsigned int)(p - itr))) return false;
447 itr = p;
448 }
449 }
450
451 p = _simpleXmlFindStartTag(itr, itrEnd);
452 if (!p) p = itrEnd;
453
454 end = p;
455 if (strip) end = _unskipWhiteSpacesAndXmlEntities(end, itr);
456
457 if (itr != end && !func((void*)data, SimpleXMLType::Data, itr, (unsigned int)(end - itr))) return false;
458
459 if (strip && (end < p) && !func((void*)data, SimpleXMLType::Ignored, end, (unsigned int)(p - end))) return false;
460
461 itr = p;
462 }
463 }
464 return true;
465 }
466
467
468 bool simpleXmlParseW3CAttribute(const char* buf, unsigned bufLength, simpleXMLAttributeCb func, const void* data)
469 {
470 const char* end;
471 char* key;
472 char* val;
473 char* next;
474
475 if (!buf) return false;
476
477 end = buf + bufLength;
478
479 if (buf == end) return true;
480
481 char* key_buf = (char*)malloc(end - buf + 1);
482 char* val_buf = (char*)malloc(end - buf + 1);
483
484 key = key_buf;
485 val = val_buf;
486 do {
487 char* sep = (char*)strchr(buf, ':');
488 next = (char*)strchr(buf, ';');
489
490 if (auto src = strstr(buf, "src")) {//src tag from css font-face contains extra semicolon
491 if (src < sep) {
492 if (next + 1 < end) next = (char*)strchr(next + 1, ';');
493 else {
494 free(key_buf);
495 free(val_buf);
496 return true;
497 }
498 }
499 }
500
501 if (sep >= end) {
502 next = nullptr;
503 sep = nullptr;
504 }
505 if (next >= end) next = nullptr;
506
507 key[0] = '\0';
508 val[0] = '\0';
509
510 if (sep != nullptr && next == nullptr) {
511 memcpy(key, buf, sep - buf);
512 key[sep - buf] = '\0';
513
514 memcpy(val, sep + 1, end - sep - 1);
515 val[end - sep - 1] = '\0';
516 } else if (sep != nullptr && sep < next) {
517 memcpy(key, buf, sep - buf);
518 key[sep - buf] = '\0';
519
520 memcpy(val, sep + 1, next - sep - 1);
521 val[next - sep - 1] = '\0';
522 } else if (next) {
523 memcpy(key, buf, next - buf);
524 key[next - buf] = '\0';
525 }
526
527 if (key[0]) {
528 key = const_cast<char*>(_simpleXmlSkipWhiteSpace(key, key + strlen(key)));
529 key[_simpleXmlUnskipWhiteSpace(key + strlen(key) , key) - key] = '\0';
530 val = const_cast<char*>(_simpleXmlSkipWhiteSpace(val, val + strlen(val)));
531 val[_simpleXmlUnskipWhiteSpace(val + strlen(val) , val) - val] = '\0';
532
533 if (!func((void*)data, key, val)) {
534 if (!_isIgnoreUnsupportedLogAttributes(key, val)) {
535 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");
536 }
537 }
538 }
539
540 if (!next) break;
541 buf = next + 1;
542 } while (true);
543
544 free(key_buf);
545 free(val_buf);
546
547 return true;
548 }
549
550
551 /*
552 * Supported formats:
553 * tag {}, .name {}, tag.name{}
554 */
555 const char* simpleXmlParseCSSAttribute(const char* buf, unsigned bufLength, char** tag, char** name, const char** attrs, unsigned* attrsLength)
556 {
557 if (!buf) return nullptr;
558
559 *tag = *name = nullptr;
560 *attrsLength = 0;
561
562 auto itr = _simpleXmlSkipWhiteSpace(buf, buf + bufLength);
563 auto itrEnd = (const char*)memchr(buf, '{', bufLength);
564
565 if (!itrEnd || itr == itrEnd) return nullptr;
566
567 auto nextElement = (const char*)memchr(itrEnd, '}', bufLength - (itrEnd - buf));
568 if (!nextElement) return nullptr;
569
570 *attrs = itrEnd + 1;
571 *attrsLength = nextElement - *attrs;
572
573 const char *p;
574
575 itrEnd = _simpleXmlUnskipWhiteSpace(itrEnd, itr);
576 if (*(itrEnd - 1) == '.') return nullptr;
577
578 for (p = itr; p < itrEnd; p++) {
579 if (*p == '.') break;
580 }
581
582 if (p == itr) *tag = strdup("all");
583 else *tag = strDuplicate(itr, p - itr);
584
585 if (p == itrEnd) *name = nullptr;
586 else *name = strDuplicate(p + 1, itrEnd - p - 1);
587
588 return (nextElement ? nextElement + 1 : nullptr);
589 }
590
591
592 const char* simpleXmlFindAttributesTag(const char* buf, unsigned bufLength)
593 {
594 const char *itr = buf, *itrEnd = buf + bufLength;
595
596 for (; itr < itrEnd; itr++) {
597 if (!isspace((unsigned char)*itr)) {
598 //User skip tagname and already gave it the attributes.
599 if (*itr == '=') return buf;
600 } else {
601 itr = _simpleXmlUnskipXmlEntities(itr, buf);
602 if (itr == itrEnd) return nullptr;
603 return itr;
604 }
605 }
606
607 return nullptr;
608 }