| 1791 | } |
| 1792 | |
| 1793 | static int jl_decode_utf8(const unsigned char *text, size_t remaining, uint32_t *codepoint, |
| 1794 | size_t *byte_count) { |
| 1795 | if (remaining == 0U) { |
| 1796 | return -1; |
| 1797 | } |
| 1798 | unsigned char first = text[0]; |
| 1799 | if (first < 0x80U) { |
| 1800 | *codepoint = first; |
| 1801 | *byte_count = 1U; |
| 1802 | return 0; |
| 1803 | } |
| 1804 | size_t count = 0; |
| 1805 | uint32_t value = 0; |
| 1806 | uint32_t minimum = 0; |
| 1807 | if (first >= 0xC2U && first <= 0xDFU) { |
| 1808 | count = 2U; |
| 1809 | value = first & 0x1FU; |
| 1810 | minimum = 0x80U; |
| 1811 | } else if (first >= 0xE0U && first <= 0xEFU) { |
| 1812 | count = 3U; |
| 1813 | value = first & 0x0FU; |
| 1814 | minimum = 0x800U; |
| 1815 | } else if (first >= 0xF0U && first <= 0xF4U) { |
| 1816 | count = 4U; |
| 1817 | value = first & 0x07U; |
| 1818 | minimum = 0x10000U; |
| 1819 | } else { |
| 1820 | return -1; |
| 1821 | } |
| 1822 | if (remaining < count) { |
| 1823 | return -1; |
| 1824 | } |
| 1825 | for (size_t i = 1U; i < count; i++) { |
| 1826 | if ((text[i] & 0xC0U) != 0x80U) { |
| 1827 | return -1; |
| 1828 | } |
| 1829 | value = (value << 6U) | (text[i] & 0x3FU); |
| 1830 | } |
| 1831 | if (value < minimum || value > 0x10FFFFU || (value >= 0xD800U && value <= 0xDFFFU)) { |
| 1832 | return -1; |
| 1833 | } |
| 1834 | *codepoint = value; |
| 1835 | *byte_count = count; |
| 1836 | return 0; |
| 1837 | } |
| 1838 | |
| 1839 | static int jl_validate_utf8(const char *text, size_t length) { |
| 1840 | size_t pos = 0; |
no outgoing calls
no test coverage detected