| 4785 | } |
| 4786 | |
| 4787 | static _FORCE_INLINE_ int _xml_unescape(const char32_t *p_src, int p_src_len, char32_t *p_dst) { |
| 4788 | int len = 0; |
| 4789 | while (p_src_len) { |
| 4790 | if (*p_src == '&') { |
| 4791 | int eat = 0; |
| 4792 | |
| 4793 | if (p_src_len >= 4 && p_src[1] == '#') { |
| 4794 | char32_t c = 0; |
| 4795 | bool overflow = false; |
| 4796 | if (p_src[2] == 'x') { |
| 4797 | // Hex entity &#x<num>; |
| 4798 | for (int i = 3; i < p_src_len; i++) { |
| 4799 | eat = i + 1; |
| 4800 | char32_t ct = p_src[i]; |
| 4801 | if (ct == ';') { |
| 4802 | break; |
| 4803 | } else if (is_digit(ct)) { |
| 4804 | ct = ct - '0'; |
| 4805 | } else if (ct >= 'a' && ct <= 'f') { |
| 4806 | ct = (ct - 'a') + 10; |
| 4807 | } else if (ct >= 'A' && ct <= 'F') { |
| 4808 | ct = (ct - 'A') + 10; |
| 4809 | } else { |
| 4810 | break; |
| 4811 | } |
| 4812 | if (c > (UINT32_MAX >> 4)) { |
| 4813 | overflow = true; |
| 4814 | break; |
| 4815 | } |
| 4816 | c <<= 4; |
| 4817 | c |= ct; |
| 4818 | } |
| 4819 | } else { |
| 4820 | // Decimal entity &#<num>; |
| 4821 | for (int i = 2; i < p_src_len; i++) { |
| 4822 | eat = i + 1; |
| 4823 | char32_t ct = p_src[i]; |
| 4824 | if (ct == ';' || !is_digit(ct)) { |
| 4825 | break; |
| 4826 | } |
| 4827 | } |
| 4828 | if (p_src[eat - 1] == ';') { |
| 4829 | int64_t val = String::to_int(p_src + 2, eat - 3); |
| 4830 | if (val > 0 && val <= UINT32_MAX) { |
| 4831 | c = (char32_t)val; |
| 4832 | } else { |
| 4833 | overflow = true; |
| 4834 | } |
| 4835 | } |
| 4836 | } |
| 4837 | |
| 4838 | // Value must be non-zero, in the range of char32_t, |
| 4839 | // actually end with ';'. If invalid, leave the entity as-is |
| 4840 | if (c == '\0' || overflow || p_src[eat - 1] != ';') { |
| 4841 | eat = 1; |
| 4842 | c = *p_src; |
| 4843 | } |
| 4844 | if (p_dst) { |
no test coverage detected