Skip to content

Commit a8384c6

Browse files
Olaf Webertytso
authored andcommitted
unicode: reduce the size of utf8data[]
Remove the Hangul decompositions from the utf8data trie, and do algorithmic decomposition to calculate them on the fly. To store the decomposition the caller of utf8lookup()/utf8nlookup() must provide a 12-byte buffer, which is used to synthesize a leaf with the decomposition. This significantly reduces the size of the utf8data[] array. Changes made by Gabriel: Rebase to mainline Fix checkpatch errors Extract robustness fixes and merge back to original mkutf8data.c patch Regenerate utf8data.h Signed-off-by: Olaf Weber <olaf@sgi.com> Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.co.uk> Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent 44594c2 commit a8384c6

5 files changed

Lines changed: 3296 additions & 12685 deletions

File tree

‎fs/unicode/README.utf8data‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ cd to this directory (fs/unicode) and run this command:
4646
make C=../.. objdir=../.. utf8data.h.new
4747

4848
After sanity checking the newly generated utf8data.h.new file (the
49-
version generated from the 11.0.0 UCD should be 13,834 lines long, and
50-
have a total size of 1104k) and/or comparing it with the older version
49+
version generated from the 11.0.0 UCD should be 4,061 lines long, and
50+
have a total size of 320k) and/or comparing it with the older version
5151
of utf8data.h, rename it to utf8data.h.
5252

5353
If you are a kernel developer updating to a newer version of the

‎fs/unicode/utf8-norm.c‎

Lines changed: 171 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,38 @@ static inline int utf8clen(const char *s)
9898
return 1 + (c >= 0xC0) + (c >= 0xE0) + (c >= 0xF0);
9999
}
100100

101+
/*
102+
* Decode a 3-byte UTF-8 sequence.
103+
*/
104+
static unsigned int
105+
utf8decode3(const char *str)
106+
{
107+
unsigned int uc;
108+
109+
uc = *str++ & 0x0F;
110+
uc <<= 6;
111+
uc |= *str++ & 0x3F;
112+
uc <<= 6;
113+
uc |= *str++ & 0x3F;
114+
115+
return uc;
116+
}
117+
118+
/*
119+
* Encode a 3-byte UTF-8 sequence.
120+
*/
121+
static int
122+
utf8encode3(char *str, unsigned int val)
123+
{
124+
str[2] = (val & 0x3F) | 0x80;
125+
val >>= 6;
126+
str[1] = (val & 0x3F) | 0x80;
127+
val >>= 6;
128+
str[0] = val | 0xE0;
129+
130+
return 3;
131+
}
132+
101133
/*
102134
* utf8trie_t
103135
*
@@ -159,7 +191,8 @@ typedef const unsigned char utf8trie_t;
159191
* characters with the Default_Ignorable_Code_Point property.
160192
* These do affect normalization, as they all have CCC 0.
161193
*
162-
* The decompositions in the trie have been fully expanded.
194+
* The decompositions in the trie have been fully expanded, with the
195+
* exception of Hangul syllables, which are decomposed algorithmically.
163196
*
164197
* Casefolding, if applicable, is also done using decompositions.
165198
*
@@ -179,6 +212,105 @@ typedef const unsigned char utf8leaf_t;
179212
#define STOPPER (0)
180213
#define DECOMPOSE (255)
181214

215+
/* Marker for hangul syllable decomposition. */
216+
#define HANGUL ((char)(255))
217+
/* Size of the synthesized leaf used for Hangul syllable decomposition. */
218+
#define UTF8HANGULLEAF (12)
219+
220+
/*
221+
* Hangul decomposition (algorithm from Section 3.12 of Unicode 6.3.0)
222+
*
223+
* AC00;<Hangul Syllable, First>;Lo;0;L;;;;;N;;;;;
224+
* D7A3;<Hangul Syllable, Last>;Lo;0;L;;;;;N;;;;;
225+
*
226+
* SBase = 0xAC00
227+
* LBase = 0x1100
228+
* VBase = 0x1161
229+
* TBase = 0x11A7
230+
* LCount = 19
231+
* VCount = 21
232+
* TCount = 28
233+
* NCount = 588 (VCount * TCount)
234+
* SCount = 11172 (LCount * NCount)
235+
*
236+
* Decomposition:
237+
* SIndex = s - SBase
238+
*
239+
* LV (Canonical/Full)
240+
* LIndex = SIndex / NCount
241+
* VIndex = (Sindex % NCount) / TCount
242+
* LPart = LBase + LIndex
243+
* VPart = VBase + VIndex
244+
*
245+
* LVT (Canonical)
246+
* LVIndex = (SIndex / TCount) * TCount
247+
* TIndex = (Sindex % TCount)
248+
* LVPart = SBase + LVIndex
249+
* TPart = TBase + TIndex
250+
*
251+
* LVT (Full)
252+
* LIndex = SIndex / NCount
253+
* VIndex = (Sindex % NCount) / TCount
254+
* TIndex = (Sindex % TCount)
255+
* LPart = LBase + LIndex
256+
* VPart = VBase + VIndex
257+
* if (TIndex == 0) {
258+
* d = <LPart, VPart>
259+
* } else {
260+
* TPart = TBase + TIndex
261+
* d = <LPart, TPart, VPart>
262+
* }
263+
*/
264+
265+
/* Constants */
266+
#define SB (0xAC00)
267+
#define LB (0x1100)
268+
#define VB (0x1161)
269+
#define TB (0x11A7)
270+
#define LC (19)
271+
#define VC (21)
272+
#define TC (28)
273+
#define NC (VC * TC)
274+
#define SC (LC * NC)
275+
276+
/* Algorithmic decomposition of hangul syllable. */
277+
static utf8leaf_t *
278+
utf8hangul(const char *str, unsigned char *hangul)
279+
{
280+
unsigned int si;
281+
unsigned int li;
282+
unsigned int vi;
283+
unsigned int ti;
284+
unsigned char *h;
285+
286+
/* Calculate the SI, LI, VI, and TI values. */
287+
si = utf8decode3(str) - SB;
288+
li = si / NC;
289+
vi = (si % NC) / TC;
290+
ti = si % TC;
291+
292+
/* Fill in base of leaf. */
293+
h = hangul;
294+
LEAF_GEN(h) = 2;
295+
LEAF_CCC(h) = DECOMPOSE;
296+
h += 2;
297+
298+
/* Add LPart, a 3-byte UTF-8 sequence. */
299+
h += utf8encode3((char *)h, li + LB);
300+
301+
/* Add VPart, a 3-byte UTF-8 sequence. */
302+
h += utf8encode3((char *)h, vi + VB);
303+
304+
/* Add TPart if required, also a 3-byte UTF-8 sequence. */
305+
if (ti)
306+
h += utf8encode3((char *)h, ti + TB);
307+
308+
/* Terminate string. */
309+
h[0] = '\0';
310+
311+
return hangul;
312+
}
313+
182314
/*
183315
* Use trie to scan s, touching at most len bytes.
184316
* Returns the leaf if one exists, NULL otherwise.
@@ -187,8 +319,8 @@ typedef const unsigned char utf8leaf_t;
187319
* is well-formed and corresponds to a known unicode code point. The
188320
* shorthand for this will be "is valid UTF-8 unicode".
189321
*/
190-
static utf8leaf_t *utf8nlookup(const struct utf8data *data, const char *s,
191-
size_t len)
322+
static utf8leaf_t *utf8nlookup(const struct utf8data *data,
323+
unsigned char *hangul, const char *s, size_t len)
192324
{
193325
utf8trie_t *trie = NULL;
194326
int offlen;
@@ -228,8 +360,7 @@ static utf8leaf_t *utf8nlookup(const struct utf8data *data, const char *s,
228360
trie++;
229361
} else {
230362
/* No right node. */
231-
node = 0;
232-
trie = NULL;
363+
return NULL;
233364
}
234365
} else {
235366
/* Left leg */
@@ -239,15 +370,22 @@ static utf8leaf_t *utf8nlookup(const struct utf8data *data, const char *s,
239370
trie += offlen + 1;
240371
} else if (*trie & RIGHTPATH) {
241372
/* No left node. */
242-
node = 0;
243-
trie = NULL;
373+
return NULL;
244374
} else {
245375
/* Left node after this node */
246376
node = (*trie & TRIENODE);
247377
trie++;
248378
}
249379
}
250380
}
381+
/*
382+
* Hangul decomposition is done algorithmically. These are the
383+
* codepoints >= 0xAC00 and <= 0xD7A3. Their UTF-8 encoding is
384+
* always 3 bytes long, so s has been advanced twice, and the
385+
* start of the sequence is at s-2.
386+
*/
387+
if (LEAF_CCC(trie) == DECOMPOSE && LEAF_STR(trie)[0] == HANGUL)
388+
trie = utf8hangul(s - 2, hangul);
251389
return trie;
252390
}
253391

@@ -257,9 +395,10 @@ static utf8leaf_t *utf8nlookup(const struct utf8data *data, const char *s,
257395
*
258396
* Forwards to utf8nlookup().
259397
*/
260-
static utf8leaf_t *utf8lookup(const struct utf8data *data, const char *s)
398+
static utf8leaf_t *utf8lookup(const struct utf8data *data,
399+
unsigned char *hangul, const char *s)
261400
{
262-
return utf8nlookup(data, s, (size_t)-1);
401+
return utf8nlookup(data, hangul, s, (size_t)-1);
263402
}
264403

265404
/*
@@ -272,11 +411,13 @@ int utf8agemax(const struct utf8data *data, const char *s)
272411
utf8leaf_t *leaf;
273412
int age = 0;
274413
int leaf_age;
414+
unsigned char hangul[UTF8HANGULLEAF];
275415

276416
if (!data)
277417
return -1;
418+
278419
while (*s) {
279-
leaf = utf8lookup(data, s);
420+
leaf = utf8lookup(data, hangul, s);
280421
if (!leaf)
281422
return -1;
282423

@@ -299,12 +440,13 @@ int utf8agemin(const struct utf8data *data, const char *s)
299440
utf8leaf_t *leaf;
300441
int age;
301442
int leaf_age;
443+
unsigned char hangul[UTF8HANGULLEAF];
302444

303445
if (!data)
304446
return -1;
305447
age = data->maxage;
306448
while (*s) {
307-
leaf = utf8lookup(data, s);
449+
leaf = utf8lookup(data, hangul, s);
308450
if (!leaf)
309451
return -1;
310452
leaf_age = utf8agetab[LEAF_GEN(leaf)];
@@ -325,11 +467,13 @@ int utf8nagemax(const struct utf8data *data, const char *s, size_t len)
325467
utf8leaf_t *leaf;
326468
int age = 0;
327469
int leaf_age;
470+
unsigned char hangul[UTF8HANGULLEAF];
328471

329472
if (!data)
330473
return -1;
474+
331475
while (len && *s) {
332-
leaf = utf8nlookup(data, s, len);
476+
leaf = utf8nlookup(data, hangul, s, len);
333477
if (!leaf)
334478
return -1;
335479
leaf_age = utf8agetab[LEAF_GEN(leaf)];
@@ -351,12 +495,13 @@ int utf8nagemin(const struct utf8data *data, const char *s, size_t len)
351495
utf8leaf_t *leaf;
352496
int leaf_age;
353497
int age;
498+
unsigned char hangul[UTF8HANGULLEAF];
354499

355500
if (!data)
356501
return -1;
357502
age = data->maxage;
358503
while (len && *s) {
359-
leaf = utf8nlookup(data, s, len);
504+
leaf = utf8nlookup(data, hangul, s, len);
360505
if (!leaf)
361506
return -1;
362507
leaf_age = utf8agetab[LEAF_GEN(leaf)];
@@ -379,11 +524,12 @@ ssize_t utf8len(const struct utf8data *data, const char *s)
379524
{
380525
utf8leaf_t *leaf;
381526
size_t ret = 0;
527+
unsigned char hangul[UTF8HANGULLEAF];
382528

383529
if (!data)
384530
return -1;
385531
while (*s) {
386-
leaf = utf8lookup(data, s);
532+
leaf = utf8lookup(data, hangul, s);
387533
if (!leaf)
388534
return -1;
389535
if (utf8agetab[LEAF_GEN(leaf)] > data->maxage)
@@ -406,11 +552,12 @@ ssize_t utf8nlen(const struct utf8data *data, const char *s, size_t len)
406552
{
407553
utf8leaf_t *leaf;
408554
size_t ret = 0;
555+
unsigned char hangul[UTF8HANGULLEAF];
409556

410557
if (!data)
411558
return -1;
412559
while (len && *s) {
413-
leaf = utf8nlookup(data, s, len);
560+
leaf = utf8nlookup(data, hangul, s, len);
414561
if (!leaf)
415562
return -1;
416563
if (utf8agetab[LEAF_GEN(leaf)] > data->maxage)
@@ -533,10 +680,12 @@ int utf8byte(struct utf8cursor *u8c)
533680
}
534681

535682
/* Look up the data for the current character. */
536-
if (u8c->p)
537-
leaf = utf8lookup(u8c->data, u8c->s);
538-
else
539-
leaf = utf8nlookup(u8c->data, u8c->s, u8c->len);
683+
if (u8c->p) {
684+
leaf = utf8lookup(u8c->data, u8c->hangul, u8c->s);
685+
} else {
686+
leaf = utf8nlookup(u8c->data, u8c->hangul,
687+
u8c->s, u8c->len);
688+
}
540689

541690
/* No leaf found implies that the input is a binary blob. */
542691
if (!leaf)
@@ -557,7 +706,9 @@ int utf8byte(struct utf8cursor *u8c)
557706
ccc = STOPPER;
558707
goto ccc_mismatch;
559708
}
560-
leaf = utf8lookup(u8c->data, u8c->s);
709+
710+
leaf = utf8lookup(u8c->data, u8c->hangul, u8c->s);
711+
ccc = LEAF_CCC(leaf);
561712
}
562713

563714
/*

0 commit comments

Comments
 (0)