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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
/*
* kmscon - Unicode Handling
*
* Copyright (c) 2011 David Herrmann <dh.herrmann@googlemail.com>
* Copyright (c) 2011 University of Tuebingen
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* This kmscon-utf8-state-machine is based on the wayland-compositor demos:
*
* Copyright © 2008 Kristian Høgsberg
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of the copyright holders not be used in
* advertising or publicity pertaining to distribution of the software
* without specific, written prior permission. The copyright holders make
* no representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
* SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
* SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
* CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Unicode Handling
* Main implementation of the symbol datatype. The symbol table contains two-way
* references. The Hash Table contains all the symbols with the symbol ucs4
* string as key and the symbol ID as value.
* The index array contains the symbol ID as key and a pointer to the ucs4
* string as value. But the hash table owns the ucs4 string.
* This allows fast implementations of *_get() and *_append() without long
* search intervals.
*
* When creating a new symbol, we simply return the UCS4 value as new symbol. We
* do not add it to our symbol table as it is only one character. However, if a
* character is appended to an existing symbol, we create a new ucs4 string and
* push the new symbol into the symbol table.
*/
#include <errno.h>
#include <glib.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include "log.h"
#include "unicode.h"
#define KMSCON_UCS4_MAXLEN 10
#define KMSCON_UCS4_MAX 0x7fffffffUL
#define KMSCON_UCS4_INVALID 0xfffd
const kmscon_symbol_t kmscon_symbol_default = 0;
static const char default_u8[] = { 0 };
struct kmscon_symbol_table {
unsigned long ref;
GArray *index;
GHashTable *symbols;
uint32_t next_id;
};
static guint hash_ucs4(gconstpointer key)
{
guint val = 5381;
size_t i;
const uint32_t *ucs4 = key;
i = 0;
while (ucs4[i] <= KMSCON_UCS4_MAX) {
val = val * 33 + ucs4[i];
++i;
}
return val;
}
static gboolean cmp_ucs4(gconstpointer a, gconstpointer b)
{
size_t i;
const uint32_t *v1, *v2;
v1 = a;
v2 = b;
i = 0;
while (1) {
if (v1[i] > KMSCON_UCS4_MAX && v2[i] > KMSCON_UCS4_MAX)
return TRUE;
if (v1[i] > KMSCON_UCS4_MAX && v2[i] <= KMSCON_UCS4_MAX)
return FALSE;
if (v1[i] <= KMSCON_UCS4_MAX && v2[i] > KMSCON_UCS4_MAX)
return FALSE;
if (v1[i] != v2[i])
return FALSE;
++i;
}
}
int kmscon_symbol_table_new(struct kmscon_symbol_table **out)
{
struct kmscon_symbol_table *st;
int ret;
static const uint32_t *val = NULL; /* we need an lvalue for glib */
if (!out)
return -EINVAL;
st = malloc(sizeof(*st));
if (!st)
return -ENOMEM;
memset(st, 0, sizeof(*st));
st->ref = 1;
st->next_id = KMSCON_UCS4_MAX + 2;
st->index = g_array_new(FALSE, TRUE, sizeof(uint32_t*));
if (!st->index) {
ret = -ENOMEM;
goto err_free;
}
g_array_append_val(st->index, val);
st->symbols = g_hash_table_new_full(hash_ucs4, cmp_ucs4,
(GDestroyNotify) free, NULL);
if (!st->symbols) {
ret = -ENOMEM;
goto err_arr;
}
*out = st;
return 0;
err_arr:
g_array_unref(st->index);
err_free:
free(st);
return ret;
}
void kmscon_symbol_table_ref(struct kmscon_symbol_table *st)
{
if (!st)
return;
++st->ref;
}
void kmscon_symbol_table_unref(struct kmscon_symbol_table *st)
{
if (!st || !st->ref)
return;
if (--st->ref)
return;
g_hash_table_unref(st->symbols);
g_array_unref(st->index);
free(st);
}
kmscon_symbol_t kmscon_symbol_make(uint32_t ucs4)
{
if (ucs4 > KMSCON_UCS4_MAX) {
log_warn("unicode: invalid ucs4 character\n");
return 0;
} else {
return ucs4;
}
}
kmscon_symbol_t kmscon_symbol_append(struct kmscon_symbol_table *st,
kmscon_symbol_t sym, uint32_t ucs4)
{
uint32_t buf[KMSCON_UCS4_MAXLEN + 1], nsym, *nval;
const uint32_t *ptr;
size_t s;
if (!st)
return sym;
if (ucs4 > KMSCON_UCS4_MAX) {
log_warn("unicode: invalid ucs4 character\n");
return sym;
}
ptr = kmscon_symbol_get(st, &sym, &s);
if (s >= KMSCON_UCS4_MAXLEN)
return sym;
memcpy(buf, ptr, s * sizeof(uint32_t));
buf[s++] = ucs4;
buf[s++] = KMSCON_UCS4_MAX + 1;
nsym = GPOINTER_TO_UINT(g_hash_table_lookup(st->symbols, buf));
if (nsym)
return nsym;
log_debug("unicode: adding new composed symbol\n");
nval = malloc(sizeof(uint32_t) * s);
if (!nval)
return sym;
memcpy(nval, buf, s * sizeof(uint32_t));
nsym = st->next_id++;
g_hash_table_insert(st->symbols, nval, GUINT_TO_POINTER(nsym));
g_array_append_val(st->index, nval);
return nsym;
}
/*
* This decomposes a symbol into a ucs4 string and a size value. If \sym is a
* valid UCS4 character, this returns a pointer to \sym and writes 1 into \size.
* Therefore, the returned value may get destroyed if your \sym argument gets
* destroyed.
* If \sym is a composed ucs4 string, then the returned value points into the
* hash table of the symbol table and lives as long as the symbol table does.
*
* This always returns a valid value. If an error happens, the default character
* is returned. If \size is NULL, then the size value is omitted.
*/
const uint32_t *kmscon_symbol_get(const struct kmscon_symbol_table *st,
kmscon_symbol_t *sym, size_t *size)
{
uint32_t *ucs4;
if (*sym <= KMSCON_UCS4_MAX) {
if (size)
*size = 1;
return sym;
}
if (!st)
goto def_value;
ucs4 = g_array_index(st->index, uint32_t*,
*sym - (KMSCON_UCS4_MAX + 1));
if (!ucs4)
goto def_value;
if (size) {
*size = 0;
while (ucs4[*size] <= KMSCON_UCS4_MAX)
++*size;
}
return ucs4;
def_value:
if (size)
*size = 1;
return &kmscon_symbol_default;
}
const char *kmscon_symbol_get_u8(const struct kmscon_symbol_table *st,
kmscon_symbol_t sym, size_t *size)
{
const uint32_t *ucs4;
gchar *val;
glong len;
if (!st)
goto def_value;
ucs4 = kmscon_symbol_get(st, &sym, size);
val = g_ucs4_to_utf8(ucs4, *size, NULL, &len, NULL);
if (!val || len < 0)
goto def_value;
*size = len;
return val;
def_value:
if (size)
*size = 1;
return default_u8;
}
void kmscon_symbol_free_u8(const char *s)
{
if (s != default_u8)
g_free((char*)s);
}
struct kmscon_utf8_mach {
int state;
uint32_t ch;
};
int kmscon_utf8_mach_new(struct kmscon_utf8_mach **out)
{
struct kmscon_utf8_mach *mach;
if (!out)
return -EINVAL;
mach = malloc(sizeof(*mach));
if (!mach)
return -ENOMEM;
memset(mach, 0, sizeof(*mach));
mach->state = KMSCON_UTF8_START;
*out = mach;
return 0;
}
void kmscon_utf8_mach_free(struct kmscon_utf8_mach *mach)
{
if (!mach)
return;
free(mach);
}
int kmscon_utf8_mach_feed(struct kmscon_utf8_mach *mach, char ci)
{
uint32_t c;
if (!mach)
return KMSCON_UTF8_START;
c = ci;
switch (mach->state) {
case KMSCON_UTF8_START:
case KMSCON_UTF8_ACCEPT:
case KMSCON_UTF8_REJECT:
if (c == 0xC0 || c == 0xC1) {
/* overlong encoding for ASCII, reject */
mach->state = KMSCON_UTF8_REJECT;
} else if ((c & 0x80) == 0) {
/* single byte, accept */
mach->ch = c;
mach->state = KMSCON_UTF8_ACCEPT;
} else if ((c & 0xC0) == 0x80) {
/* parser out of sync, ignore byte */
mach->state = KMSCON_UTF8_START;
} else if ((c & 0xE0) == 0xC0) {
/* start of two byte sequence */
mach->ch = (c & 0x1F) << 6;
mach->state = KMSCON_UTF8_EXPECT1;
} else if ((c & 0xF0) == 0xE0) {
/* start of three byte sequence */
mach->ch = (c & 0x0F) << 12;
mach->state = KMSCON_UTF8_EXPECT2;
} else if ((c & 0xF8) == 0xF0) {
/* start of four byte sequence */
mach->ch = (c & 0x07) << 18;
mach->state = KMSCON_UTF8_EXPECT3;
} else {
/* overlong encoding, reject */
mach->state = KMSCON_UTF8_REJECT;
}
break;
case KMSCON_UTF8_EXPECT3:
mach->ch |= (c & 0x3F) << 12;
if ((c & 0xC0) == 0x80)
mach->state = KMSCON_UTF8_EXPECT2;
else
mach->state = KMSCON_UTF8_REJECT;
break;
case KMSCON_UTF8_EXPECT2:
mach->ch |= (c & 0x3F) << 6;
if ((c & 0xC0) == 0x80)
mach->state = KMSCON_UTF8_EXPECT1;
else
mach->state = KMSCON_UTF8_REJECT;
break;
case KMSCON_UTF8_EXPECT1:
mach->ch |= c & 0x3F;
if ((c & 0xC0) == 0x80)
mach->state = KMSCON_UTF8_ACCEPT;
else
mach->state = KMSCON_UTF8_REJECT;
break;
default:
mach->state = KMSCON_UTF8_REJECT;
break;
}
return mach->state;
}
uint32_t kmscon_utf8_mach_get(struct kmscon_utf8_mach *mach)
{
if (!mach || mach->state != KMSCON_UTF8_ACCEPT)
return KMSCON_UCS4_INVALID;
return mach->ch;
}
|