2016-09-27 19:01:08 +00:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2009-12-08 20:43:55 +00:00
|
|
|
#include "cm_utf8.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
RFC 3629
|
|
|
|
07-bit: 0xxxxxxx
|
|
|
|
11-bit: 110xxxxx 10xxxxxx
|
|
|
|
16-bit: 1110xxxx 10xxxxxx 10xxxxxx
|
|
|
|
21-bit: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
|
|
|
|
|
|
|
|
Pre-RFC Compatibility
|
|
|
|
26-bit: 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
|
|
|
|
31-bit: 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Number of leading ones before a zero in the byte. */
|
|
|
|
static unsigned char const cm_utf8_ones[256] = {
|
2016-05-16 14:34:04 +00:00
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
|
|
|
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
|
|
|
|
3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8
|
2009-12-08 20:43:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Mask away control bits from bytes with n leading ones. */
|
2016-05-16 14:34:04 +00:00
|
|
|
static unsigned char const cm_utf8_mask[7] = { 0xEF, 0x3F, 0x1F, 0x0F,
|
|
|
|
0x07, 0x03, 0x01 };
|
2009-12-08 20:43:55 +00:00
|
|
|
|
|
|
|
/* Minimum allowed value when first byte has n leading ones. */
|
|
|
|
static unsigned int const cm_utf8_min[7] = {
|
2016-05-16 14:34:04 +00:00
|
|
|
0, 0, 1u << 7, 1u << 11, 1u << 16, 1u << 21, 1u << 26 /*, 1u<<31 */
|
2009-12-08 20:43:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const char* cm_utf8_decode_character(const char* first, const char* last,
|
|
|
|
unsigned int* pc)
|
|
|
|
{
|
|
|
|
/* Count leading ones in the first byte. */
|
2010-06-25 12:48:59 +00:00
|
|
|
unsigned char c = (unsigned char)*first++;
|
2009-12-08 20:43:55 +00:00
|
|
|
unsigned char const ones = cm_utf8_ones[c];
|
2016-05-16 14:34:04 +00:00
|
|
|
switch (ones) {
|
|
|
|
case 0:
|
|
|
|
*pc = c;
|
|
|
|
return first; /* One-byte character. */
|
|
|
|
case 1:
|
|
|
|
case 7:
|
|
|
|
case 8:
|
|
|
|
return 0; /* Invalid leading byte. */
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2009-12-08 20:43:55 +00:00
|
|
|
|
|
|
|
/* Extract bits from this multi-byte character. */
|
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
unsigned int uc = c & cm_utf8_mask[ones];
|
|
|
|
int left;
|
|
|
|
for (left = ones - 1; left && first != last; --left) {
|
|
|
|
c = (unsigned char)*first++;
|
|
|
|
if (cm_utf8_ones[c] != 1) {
|
|
|
|
return 0;
|
2009-12-08 20:43:55 +00:00
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
uc = (uc << 6) | (c & cm_utf8_mask[1]);
|
2009-12-08 20:43:55 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
if (left > 0 || uc < cm_utf8_min[ones]) {
|
|
|
|
return 0;
|
2009-12-08 20:43:55 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
*pc = uc;
|
|
|
|
return first;
|
2009-12-08 20:43:55 +00:00
|
|
|
}
|
|
|
|
}
|