summaryrefslogtreecommitdiff
path: root/fw/fe310/eos/unicode.c
blob: af41d243efdd55447868b79323d2f977df52c3cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "unicode.h"

int utf8_enc(ucp_t ch, utf8_t *str) {
    if (ch <= 0x7f) {
        str[0] = ch;
        return 1;
    } else if (ch <= 0x7ff) {
        str[0] = 0xc0 | (ch >> 6);
        str[1] = 0x80 | (ch & 0x3f);
        return 2;
    } else if (ch <= 0xffff) {
        if ((ch >= 0xd800) && (ch <= 0xdfff)) return UTF_ERR;
        str[0] = 0xe0 | (ch >> 12);
        str[1] = 0x80 | ((ch >> 6) & 0x3f);
        str[2] = 0x80 | (ch & 0x3f);
        return 3;
    } else if (ch <= 0x10ffff) {
        str[0] = 0xf0 | (ch >> 18);
        str[1] = 0x80 | ((ch >> 12) & 0x3f);
        str[2] = 0x80 | ((ch >> 6) & 0x3f);
        str[3] = 0x80 | (ch & 0x3f);
        return 4;
    } else {
        return UTF_ERR;
    }
}

int utf8_dec(utf8_t *str, ucp_t *ch) {
    if ((str[0] & 0x80) == 0x00) {
        *ch = str[0];
        return 1;
    } else if ((str[0] & 0xe0) == 0xc0) {
        if ((str[1] & 0xc0) != 0x80) return UTF_ERR;
        *ch  = (ucp_t)(str[0] & 0x1f) << 6;
        *ch |= (ucp_t)(str[1] & 0x3f);
        if (*ch < 0x80) return UTF_ERR;
        return 2;
    } else if ((str[0] & 0xf0) == 0xe0) {
        if (((str[1] & 0xc0) != 0x80) || ((str[2] & 0xc0) != 0x80)) return UTF_ERR;
        *ch  = (ucp_t)(str[0] & 0x0f) << 12;
        *ch |= (ucp_t)(str[1] & 0x3f) << 6;
        *ch |= (ucp_t)(str[2] & 0x3f);
        if ((*ch >= 0xd800) && (*ch <= 0xdfff)) return UTF_ERR;
        if (*ch < 0x800) return UTF_ERR;
        return 3;
    } else if ((str[0] & 0xf8) == 0xf0) {
        if (((str[1] & 0xc0) != 0x80) || ((str[2] & 0xc0) != 0x80) || ((str[3] & 0xc0) != 0x80)) return UTF_ERR;
        *ch  = (ucp_t)(str[0] & 0x07) << 18;
        *ch |= (ucp_t)(str[1] & 0x0f) << 12;
        *ch |= (ucp_t)(str[2] & 0x3f) << 6;
        *ch |= (ucp_t)(str[3] & 0x3f);
        if (*ch < 0x010000) return UTF_ERR;
        if (*ch > 0x10ffff) return UTF_ERR;
        return 4;
    } else {
        return UTF_ERR;
    }
}

int utf8_len_ch(ucp_t ch) {
    if (ch <= 0x7f) return 1;
    if (ch <= 0x7ff) return 2;
    if (ch <= 0xffff) return 3;
    if (ch <= 0x10ffff) return 4;

    return UTF_ERR;
}

int utf8_len_str(utf8_t *str) {
    if ((*str & 0x80) == 0x00) return 1;
    if ((*str & 0xe0) == 0xc0) return 2;
    if ((*str & 0xf0) == 0xe0) return 3;
    if ((*str & 0xf8) == 0xf0) return 4;

    return UTF_ERR;
}

int utf8_seek(utf8_t *str, int off, ucp_t *ch) {
    int i;
    int len = 0;

    if (off < 0) {
        off = -off;
        for (i=0; i<off; i++) {
            len--;
            while ((str[len] & 0xc0) == 0x80) len--;
        }
    } else {
        for (i=0; i<off; i++) {
            if ((str[len] & 0x80) == 0x00) {
                len += 1;
            } else if ((str[0] & 0xe0) == 0xc0) {
                len += 2;
            } else if ((str[0] & 0xf0) == 0xe0) {
                len += 3;
            } else if ((str[0] & 0xf8) == 0xf0) {
                len += 4;
            }
        }
    }
    utf8_dec(str + len, ch);
    return len;
}

int utf8_verify(utf8_t *str, size_t str_size, size_t *str_len) {
    ucp_t ch;
    size_t len = 0;
    int ch_l, rv;

    while (len < str_size) {
        if (str_size - len < 4) {
            rv = utf8_len_str(str + len);
            if (rv < 0) break;
            if (str_size - len < rv) break;
        }
        ch_l = utf8_dec(str + len, &ch);
        if (ch_l > 0) {
            if (ch == 0) {
                if (str_len) *str_len = len;
                return UTF_OK;
            }
            len += ch_l;
        } else {
            break;
        }
    }
    if (len == str_size) len -= ch_l;
    if (str_len) *str_len = len;

    return UTF_ERR;
}

int utf16_enc(ucp_t ch, utf16_t *str) {
    if (ch <= 0xffff) {
        if ((ch >= 0xd800) && (ch <= 0xdfff)) return UTF_ERR;
        str[0] = ch >> 8;
        str[1] = ch & 0xff;
        return 2;
    } else if (ch <= 0x10ffff) {
        uint16_t hi;
        uint16_t lo;

        ch -= 0x10000;
        hi = (ch >> 10) + 0xd800;
        lo = (ch & 0x3ff) + 0xdc00;
        str[0] = hi >> 8;
        str[1] = hi & 0xff;
        str[2] = lo >> 8;
        str[3] = lo & 0xff;
        return 4;
    } else {
        return UTF_ERR;
    }
}

int utf16_dec(utf16_t *str, ucp_t *ch) {
    *ch = (str[0] << 8) | str[1];
    if ((*ch >= 0xd800) && (*ch <= 0xdfff)) {
        uint16_t hi = *ch;
        uint16_t lo;

        if (hi > 0xdbff) return UTF_ERR;
        lo = (str[2] << 8) | str[3];
        if ((lo < 0xdc00) || (lo > 0xdfff)) return UTF_ERR;
        *ch = (((hi - 0xd800) << 10) | (lo - 0xdc00)) + 0x10000;
        return 4;
    } else {
        return 2;
    }
}

int utf16_len_ch(ucp_t ch) {
    if (ch <= 0xffff) return 2;
    if (ch <= 0x10ffff) return 4;

    return UTF_ERR;
}

int utf16_len_str(utf16_t *str) {
    uint16_t ch = (str[0] << 8) | str[1];

    if ((ch >= 0xd800) && (ch <= 0xdfff)) return 4;
    return 2;
}

int utf16_seek(utf16_t *str, int off, ucp_t *ch) {
    int i;
    int len = 0;
    uint16_t cu;

    if (off < 0) {
        off = -off;
        for (i=0; i<off; i++) {
            len -= 2;
            cu = (str[len] << 8) | str[len + 1];
            if ((cu >= 0xdc00) && (cu <= 0xdfff)) {
                len -= 2;
            }
        }
    } else {
        for (i=0; i<off; i++) {
            cu = (str[len] << 8) | str[len + 1];
            if ((cu >= 0xd800) && (cu <= 0xdbff)) {
                len += 4;
            } else {
                len += 2;
            }
        }
    }
    utf16_dec(str + len, ch);
    return len;
}