| 9120e874 Andy McCall |
1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#ifndef UTF8_H |
| 6 |
#define UTF8_H |
| 7 |
|
| 8 |
#include <stdint.h> |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
static void * |
| 26 |
utf8_decode(void *buf, uint32_t *c, int *e) |
| 27 |
{ |
| 28 |
static const char lengths[] = { |
| 29 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 30 |
0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 4, 0 |
| 31 |
}; |
| 32 |
static const int masks[] = {0x00, 0x7f, 0x1f, 0x0f, 0x07}; |
| 33 |
static const uint32_t mins[] = {4194304, 0, 128, 2048, 65536}; |
| 34 |
static const int shiftc[] = {0, 18, 12, 6, 0}; |
| 35 |
static const int shifte[] = {0, 6, 4, 2, 0}; |
| 36 |
|
| 37 |
unsigned char *s = buf; |
| 38 |
int len = lengths[s[0] >> 3]; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
unsigned char *next = s + len + !len; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
*c = (uint32_t)(s[0] & masks[len]) << 18; |
| 50 |
*c |= (uint32_t)(s[1] & 0x3f) << 12; |
| 51 |
*c |= (uint32_t)(s[2] & 0x3f) << 6; |
| 52 |
*c |= (uint32_t)(s[3] & 0x3f) << 0; |
| 53 |
*c >>= shiftc[len]; |
| 54 |
|
| 55 |
|
| 56 |
*e = (*c < mins[len]) << 6; |
| 57 |
*e |= ((*c >> 11) == 0x1b) << 7; |
| 58 |
*e |= (*c > 0x10FFFF) << 8; |
| 59 |
*e |= (s[1] & 0xc0) >> 2; |
| 60 |
*e |= (s[2] & 0xc0) >> 4; |
| 61 |
*e |= (s[3] ) >> 6; |
| 62 |
*e ^= 0x2a; |
| 63 |
*e >>= shifte[len]; |
| 64 |
|
| 65 |
return next; |
| 66 |
} |
| 67 |
|
| 68 |
#endif |