| 1 |
/* |
| 2 |
* Copyright © 2024 Pierre Le Marre <[email protected]> |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include "utils.h" |
| 9 |
#include "utf8-decoding.h" |
| 10 |
|
| 11 |
/* Array mapping the leading byte to the length of a UTF-8 sequence. |
| 12 |
* A value of zero indicates that the byte can not begin a UTF-8 sequence. */ |
| 13 |
static const uint8_t utf8_sequence_length_by_leading_byte[256] = { |
| 14 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x00-0x0F */ |
| 15 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x10-0x1F */ |
| 16 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x20-0x2F */ |
| 17 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x30-0x3F */ |
| 18 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40-0x4F */ |
| 19 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x50-0x5F */ |
| 20 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60-0x6F */ |
| 21 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x70-0x7F */ |
| 22 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80-0x8F */ |
| 23 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90-0x9F */ |
| 24 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xA0-0xAF */ |
| 25 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xB0-0xBF */ |
| 26 |
0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xC0-0xCF */ |
| 27 |
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xD0-0xDF */ |
| 28 |
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0xE0-0xEF */ |
| 29 |
4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xF0-0xFF */ |
| 30 |
}; |
| 31 |
|
| 32 |
/* Length of next utf-8 sequence */ |
| 33 |
uint8_t |
| 34 |
utf8_sequence_length(const char *s) |
| 35 |
{ |
| 36 |
return utf8_sequence_length_by_leading_byte[(unsigned char)s[0]]; |
| 37 |
} |
| 38 |
|
| 39 |
/* Reads the next UTF-8 sequence in a string */ |
| 40 |
uint32_t |
| 41 |
utf8_next_code_point(const char *s, size_t max_size, size_t *size_out) |
| 42 |
{ |
| 43 |
uint32_t cp = 0; |
| 44 |
uint8_t len = utf8_sequence_length(s); |
| 45 |
*size_out = 0; |
| 46 |
|
| 47 |
if (!max_size || len > max_size) |
| 48 |
return INVALID_UTF8_CODE_POINT; |
| 49 |
|
| 50 |
/* Handle leading byte */ |
| 51 |
switch (len) { |
| 52 |
case 1: |
| 53 |
*size_out = 1; |
| 54 |
return (uint32_t)s[0]; |
| 55 |
case 2: |
| 56 |
cp = (uint32_t)s[0] & 0x1f; |
| 57 |
break; |
| 58 |
case 3: |
| 59 |
cp = (uint32_t)s[0] & 0x0f; |
| 60 |
break; |
| 61 |
case 4: |
| 62 |
cp = (uint32_t)s[0] & 0x07; |
| 63 |
break; |
| 64 |
default: |
| 65 |
return INVALID_UTF8_CODE_POINT; |
| 66 |
} |
| 67 |
|
| 68 |
/* Process remaining bytes of the UTF-8 sequence */ |
| 69 |
for (size_t k = 1; k < len; k++) { |
| 70 |
if (((uint32_t)s[k] & 0xc0) != 0x80) |
| 71 |
return INVALID_UTF8_CODE_POINT; |
| 72 |
cp <<= 6; |
| 73 |
cp |= (uint32_t)s[k] & 0x3f; |
| 74 |
} |
| 75 |
|
| 76 |
/* Check surrogates */ |
| 77 |
if (is_surrogate(cp)) |
| 78 |
return INVALID_UTF8_CODE_POINT; |
| 79 |
|
| 80 |
*size_out = len; |
| 81 |
return cp; |
| 82 |
} |