| 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 |
#include <stdlib.h> |
| 26 |
#include <stdio.h> |
| 27 |
#include <stdarg.h> |
| 28 |
#include <inttypes.h> |
| 29 |
#include <string.h> |
| 30 |
#include <assert.h> |
| 31 |
#include <ctype.h> |
| 32 |
#include <time.h> |
| 33 |
|
| 34 |
#include "cutils.h" |
| 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 |
#ifdef USE_TEST |
| 62 |
#include "libunicode.c" |
| 63 |
#endif |
| 64 |
|
| 65 |
#define CHARCODE_MAX 0x10ffff |
| 66 |
#define CC_LEN_MAX 3 |
| 67 |
|
| 68 |
void *mallocz(size_t size) |
| 69 |
{ |
| 70 |
void *ptr; |
| 71 |
ptr = malloc(size); |
| 72 |
memset(ptr, 0, size); |
| 73 |
return ptr; |
| 74 |
} |
| 75 |
|
| 76 |
const char *get_field(const char *p, int n) |
| 77 |
{ |
| 78 |
int i; |
| 79 |
for(i = 0; i < n; i++) { |
| 80 |
while (*p != ';' && *p != '\0') |
| 81 |
p++; |
| 82 |
if (*p == '\0') |
| 83 |
return NULL; |
| 84 |
p++; |
| 85 |
} |
| 86 |
return p; |
| 87 |
} |
| 88 |
|
| 89 |
const char *get_field_buf(char *buf, size_t buf_size, const char *p, int n) |
| 90 |
{ |
| 91 |
char *q; |
| 92 |
p = get_field(p, n); |
| 93 |
q = buf; |
| 94 |
while (*p != ';' && *p != '\0') { |
| 95 |
if ((q - buf) < buf_size - 1) |
| 96 |
*q++ = *p; |
| 97 |
p++; |
| 98 |
} |
| 99 |
*q = '\0'; |
| 100 |
return buf; |
| 101 |
} |
| 102 |
|
| 103 |
void add_char(int **pbuf, int *psize, int *plen, int c) |
| 104 |
{ |
| 105 |
int len, size, *buf; |
| 106 |
buf = *pbuf; |
| 107 |
size = *psize; |
| 108 |
len = *plen; |
| 109 |
if (len >= size) { |
| 110 |
size = *psize; |
| 111 |
size = max_int(len + 1, size * 3 / 2); |
| 112 |
buf = realloc(buf, sizeof(buf[0]) * size); |
| 113 |
*pbuf = buf; |
| 114 |
*psize = size; |
| 115 |
} |
| 116 |
buf[len++] = c; |
| 117 |
*plen = len; |
| 118 |
} |
| 119 |
|
| 120 |
int *get_field_str(int *plen, const char *str, int n) |
| 121 |
{ |
| 122 |
const char *p; |
| 123 |
int *buf, len, size; |
| 124 |
p = get_field(str, n); |
| 125 |
if (!p) { |
| 126 |
*plen = 0; |
| 127 |
return NULL; |
| 128 |
} |
| 129 |
len = 0; |
| 130 |
size = 0; |
| 131 |
buf = NULL; |
| 132 |
for(;;) { |
| 133 |
while (isspace(*p)) |
| 134 |
p++; |
| 135 |
if (!isxdigit(*p)) |
| 136 |
break; |
| 137 |
add_char(&buf, &size, &len, strtoul(p, (char **)&p, 16)); |
| 138 |
} |
| 139 |
*plen = len; |
| 140 |
return buf; |
| 141 |
} |
| 142 |
|
| 143 |
char *get_line(char *buf, int buf_size, FILE *f) |
| 144 |
{ |
| 145 |
int len; |
| 146 |
if (!fgets(buf, buf_size, f)) |
| 147 |
return NULL; |
| 148 |
len = strlen(buf); |
| 149 |
if (len > 0 && buf[len - 1] == '\n') |
| 150 |
buf[len - 1] = '\0'; |
| 151 |
return buf; |
| 152 |
} |
| 153 |
|
| 154 |
#define UNICODE_GENERAL_CATEGORY |
| 155 |
|
| 156 |
typedef enum { |
| 157 |
#define DEF(id, str) GCAT_ ## id, |
| 158 |
#include "unicode_gen_def.h" |
| 159 |
#undef DEF |
| 160 |
GCAT_COUNT, |
| 161 |
} UnicodeGCEnum1; |
| 162 |
|
| 163 |
static const char *unicode_gc_name[] = { |
| 164 |
#define DEF(id, str) #id, |
| 165 |
#include "unicode_gen_def.h" |
| 166 |
#undef DEF |
| 167 |
}; |
| 168 |
|
| 169 |
static const char *unicode_gc_short_name[] = { |
| 170 |
#define DEF(id, str) str, |
| 171 |
#include "unicode_gen_def.h" |
| 172 |
#undef DEF |
| 173 |
}; |
| 174 |
|
| 175 |
#undef UNICODE_GENERAL_CATEGORY |
| 176 |
|
| 177 |
#define UNICODE_SCRIPT |
| 178 |
|
| 179 |
typedef enum { |
| 180 |
#define DEF(id, str) SCRIPT_ ## id, |
| 181 |
#include "unicode_gen_def.h" |
| 182 |
#undef DEF |
| 183 |
SCRIPT_COUNT, |
| 184 |
} UnicodeScriptEnum1; |
| 185 |
|
| 186 |
static const char *unicode_script_name[] = { |
| 187 |
#define DEF(id, str) #id, |
| 188 |
#include "unicode_gen_def.h" |
| 189 |
#undef DEF |
| 190 |
}; |
| 191 |
|
| 192 |
const char *unicode_script_short_name[] = { |
| 193 |
#define DEF(id, str) str, |
| 194 |
#include "unicode_gen_def.h" |
| 195 |
#undef DEF |
| 196 |
}; |
| 197 |
|
| 198 |
#undef UNICODE_SCRIPT |
| 199 |
|
| 200 |
#define UNICODE_PROP_LIST |
| 201 |
|
| 202 |
typedef enum { |
| 203 |
#define DEF(id, str) PROP_ ## id, |
| 204 |
#include "unicode_gen_def.h" |
| 205 |
#undef DEF |
| 206 |
PROP_COUNT, |
| 207 |
} UnicodePropEnum1; |
| 208 |
|
| 209 |
static const char *unicode_prop_name[] = { |
| 210 |
#define DEF(id, str) #id, |
| 211 |
#include "unicode_gen_def.h" |
| 212 |
#undef DEF |
| 213 |
}; |
| 214 |
|
| 215 |
static const char *unicode_prop_short_name[] = { |
| 216 |
#define DEF(id, str) str, |
| 217 |
#include "unicode_gen_def.h" |
| 218 |
#undef DEF |
| 219 |
}; |
| 220 |
|
| 221 |
#undef UNICODE_PROP_LIST |
| 222 |
|
| 223 |
typedef struct { |
| 224 |
|
| 225 |
uint8_t u_len; |
| 226 |
uint8_t l_len; |
| 227 |
uint8_t f_len; |
| 228 |
int u_data[CC_LEN_MAX]; |
| 229 |
int l_data[CC_LEN_MAX]; |
| 230 |
int f_data[CC_LEN_MAX]; |
| 231 |
|
| 232 |
uint8_t combining_class; |
| 233 |
uint8_t is_compat:1; |
| 234 |
uint8_t is_excluded:1; |
| 235 |
uint8_t general_category; |
| 236 |
uint8_t script; |
| 237 |
uint8_t script_ext_len; |
| 238 |
uint8_t *script_ext; |
| 239 |
uint32_t prop_bitmap_tab[3]; |
| 240 |
|
| 241 |
int decomp_len; |
| 242 |
int *decomp_data; |
| 243 |
} CCInfo; |
| 244 |
|
| 245 |
CCInfo *unicode_db; |
| 246 |
|
| 247 |
int find_name(const char **tab, int tab_len, const char *name) |
| 248 |
{ |
| 249 |
int i, len, name_len; |
| 250 |
const char *p, *r; |
| 251 |
|
| 252 |
name_len = strlen(name); |
| 253 |
for(i = 0; i < tab_len; i++) { |
| 254 |
p = tab[i]; |
| 255 |
for(;;) { |
| 256 |
r = strchr(p, ','); |
| 257 |
if (!r) |
| 258 |
len = strlen(p); |
| 259 |
else |
| 260 |
len = r - p; |
| 261 |
if (len == name_len && memcmp(p, name, len) == 0) |
| 262 |
return i; |
| 263 |
if (!r) |
| 264 |
break; |
| 265 |
p = r + 1; |
| 266 |
} |
| 267 |
} |
| 268 |
return -1; |
| 269 |
} |
| 270 |
|
| 271 |
static int get_prop(uint32_t c, int prop_idx) |
| 272 |
{ |
| 273 |
return (unicode_db[c].prop_bitmap_tab[prop_idx >> 5] >> (prop_idx & 0x1f)) & 1; |
| 274 |
} |
| 275 |
|
| 276 |
static void set_prop(uint32_t c, int prop_idx, int val) |
| 277 |
{ |
| 278 |
uint32_t mask; |
| 279 |
mask = 1U << (prop_idx & 0x1f); |
| 280 |
if (val) |
| 281 |
unicode_db[c].prop_bitmap_tab[prop_idx >> 5] |= mask; |
| 282 |
else |
| 283 |
unicode_db[c].prop_bitmap_tab[prop_idx >> 5] &= ~mask; |
| 284 |
} |
| 285 |
|
| 286 |
void parse_unicode_data(const char *filename) |
| 287 |
{ |
| 288 |
FILE *f; |
| 289 |
char line[1024]; |
| 290 |
char buf1[256]; |
| 291 |
const char *p; |
| 292 |
int code, lc, uc, last_code; |
| 293 |
CCInfo *ci, *tab = unicode_db; |
| 294 |
|
| 295 |
f = fopen(filename, "rb"); |
| 296 |
if (!f) { |
| 297 |
perror(filename); |
| 298 |
exit(1); |
| 299 |
} |
| 300 |
|
| 301 |
last_code = 0; |
| 302 |
for(;;) { |
| 303 |
if (!get_line(line, sizeof(line), f)) |
| 304 |
break; |
| 305 |
p = line; |
| 306 |
while (isspace(*p)) |
| 307 |
p++; |
| 308 |
if (*p == '#') |
| 309 |
continue; |
| 310 |
|
| 311 |
p = get_field(line, 0); |
| 312 |
if (!p) |
| 313 |
continue; |
| 314 |
code = strtoul(p, NULL, 16); |
| 315 |
lc = 0; |
| 316 |
uc = 0; |
| 317 |
|
| 318 |
p = get_field(line, 12); |
| 319 |
if (p && *p != ';') { |
| 320 |
uc = strtoul(p, NULL, 16); |
| 321 |
} |
| 322 |
|
| 323 |
p = get_field(line, 13); |
| 324 |
if (p && *p != ';') { |
| 325 |
lc = strtoul(p, NULL, 16); |
| 326 |
} |
| 327 |
ci = &tab[code]; |
| 328 |
if (uc > 0 || lc > 0) { |
| 329 |
assert(code <= CHARCODE_MAX); |
| 330 |
if (uc > 0) { |
| 331 |
assert(ci->u_len == 0); |
| 332 |
ci->u_len = 1; |
| 333 |
ci->u_data[0] = uc; |
| 334 |
} |
| 335 |
if (lc > 0) { |
| 336 |
assert(ci->l_len == 0); |
| 337 |
ci->l_len = 1; |
| 338 |
ci->l_data[0] = lc; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
{ |
| 343 |
int i; |
| 344 |
get_field_buf(buf1, sizeof(buf1), line, 2); |
| 345 |
i = find_name(unicode_gc_name, countof(unicode_gc_name), buf1); |
| 346 |
if (i < 0) { |
| 347 |
fprintf(stderr, "General category '%s' not found\n", |
| 348 |
buf1); |
| 349 |
exit(1); |
| 350 |
} |
| 351 |
ci->general_category = i; |
| 352 |
} |
| 353 |
|
| 354 |
p = get_field(line, 3); |
| 355 |
if (p && *p != ';' && *p != '\0') { |
| 356 |
int cc; |
| 357 |
cc = strtoul(p, NULL, 0); |
| 358 |
if (cc != 0) { |
| 359 |
assert(code <= CHARCODE_MAX); |
| 360 |
ci->combining_class = cc; |
| 361 |
|
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
p = get_field(line, 5); |
| 366 |
if (p && *p != ';' && *p != '\0') { |
| 367 |
int size; |
| 368 |
assert(code <= CHARCODE_MAX); |
| 369 |
ci->is_compat = 0; |
| 370 |
if (*p == '<') { |
| 371 |
while (*p != '\0' && *p != '>') |
| 372 |
p++; |
| 373 |
if (*p == '>') |
| 374 |
p++; |
| 375 |
ci->is_compat = 1; |
| 376 |
} |
| 377 |
size = 0; |
| 378 |
for(;;) { |
| 379 |
while (isspace(*p)) |
| 380 |
p++; |
| 381 |
if (!isxdigit(*p)) |
| 382 |
break; |
| 383 |
add_char(&ci->decomp_data, &size, &ci->decomp_len, strtoul(p, (char **)&p, 16)); |
| 384 |
} |
| 385 |
#if 0 |
| 386 |
{ |
| 387 |
int i; |
| 388 |
static int count, d_count; |
| 389 |
|
| 390 |
printf("%05x: %c", code, ci->is_compat ? 'C': ' '); |
| 391 |
for(i = 0; i < ci->decomp_len; i++) |
| 392 |
printf(" %05x", ci->decomp_data[i]); |
| 393 |
printf("\n"); |
| 394 |
count++; |
| 395 |
d_count += ci->decomp_len; |
| 396 |
|
| 397 |
} |
| 398 |
#endif |
| 399 |
} |
| 400 |
|
| 401 |
p = get_field(line, 9); |
| 402 |
if (p && *p == 'Y') { |
| 403 |
set_prop(code, PROP_Bidi_Mirrored, 1); |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
get_field_buf(buf1, sizeof(buf1), line, 1); |
| 408 |
if (strstr(buf1, " Last>")) { |
| 409 |
int i; |
| 410 |
|
| 411 |
assert(ci->decomp_len == 0); |
| 412 |
assert(ci->script_ext_len == 0); |
| 413 |
for(i = last_code + 1; i < code; i++) { |
| 414 |
unicode_db[i] = *ci; |
| 415 |
} |
| 416 |
} |
| 417 |
last_code = code; |
| 418 |
} |
| 419 |
|
| 420 |
fclose(f); |
| 421 |
} |
| 422 |
|
| 423 |
void parse_special_casing(CCInfo *tab, const char *filename) |
| 424 |
{ |
| 425 |
FILE *f; |
| 426 |
char line[1024]; |
| 427 |
const char *p; |
| 428 |
int code; |
| 429 |
CCInfo *ci; |
| 430 |
|
| 431 |
f = fopen(filename, "rb"); |
| 432 |
if (!f) { |
| 433 |
perror(filename); |
| 434 |
exit(1); |
| 435 |
} |
| 436 |
|
| 437 |
for(;;) { |
| 438 |
if (!get_line(line, sizeof(line), f)) |
| 439 |
break; |
| 440 |
p = line; |
| 441 |
while (isspace(*p)) |
| 442 |
p++; |
| 443 |
if (*p == '#') |
| 444 |
continue; |
| 445 |
|
| 446 |
p = get_field(line, 0); |
| 447 |
if (!p) |
| 448 |
continue; |
| 449 |
code = strtoul(p, NULL, 16); |
| 450 |
assert(code <= CHARCODE_MAX); |
| 451 |
ci = &tab[code]; |
| 452 |
|
| 453 |
p = get_field(line, 4); |
| 454 |
if (p) { |
| 455 |
|
| 456 |
while (isspace(*p)) |
| 457 |
p++; |
| 458 |
if (*p != '#' && *p != '\0') |
| 459 |
continue; |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
p = get_field(line, 1); |
| 464 |
if (p && *p != ';') { |
| 465 |
ci->l_len = 0; |
| 466 |
for(;;) { |
| 467 |
while (isspace(*p)) |
| 468 |
p++; |
| 469 |
if (*p == ';') |
| 470 |
break; |
| 471 |
assert(ci->l_len < CC_LEN_MAX); |
| 472 |
ci->l_data[ci->l_len++] = strtoul(p, (char **)&p, 16); |
| 473 |
} |
| 474 |
|
| 475 |
if (ci->l_len == 1 && ci->l_data[0] == code) |
| 476 |
ci->l_len = 0; |
| 477 |
} |
| 478 |
|
| 479 |
p = get_field(line, 3); |
| 480 |
if (p && *p != ';') { |
| 481 |
ci->u_len = 0; |
| 482 |
for(;;) { |
| 483 |
while (isspace(*p)) |
| 484 |
p++; |
| 485 |
if (*p == ';') |
| 486 |
break; |
| 487 |
assert(ci->u_len < CC_LEN_MAX); |
| 488 |
ci->u_data[ci->u_len++] = strtoul(p, (char **)&p, 16); |
| 489 |
} |
| 490 |
|
| 491 |
if (ci->u_len == 1 && ci->u_data[0] == code) |
| 492 |
ci->u_len = 0; |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
fclose(f); |
| 497 |
} |
| 498 |
|
| 499 |
void parse_case_folding(CCInfo *tab, const char *filename) |
| 500 |
{ |
| 501 |
FILE *f; |
| 502 |
char line[1024]; |
| 503 |
const char *p; |
| 504 |
int code, status; |
| 505 |
CCInfo *ci; |
| 506 |
|
| 507 |
f = fopen(filename, "rb"); |
| 508 |
if (!f) { |
| 509 |
perror(filename); |
| 510 |
exit(1); |
| 511 |
} |
| 512 |
|
| 513 |
for(;;) { |
| 514 |
if (!get_line(line, sizeof(line), f)) |
| 515 |
break; |
| 516 |
p = line; |
| 517 |
while (isspace(*p)) |
| 518 |
p++; |
| 519 |
if (*p == '#') |
| 520 |
continue; |
| 521 |
|
| 522 |
p = get_field(line, 0); |
| 523 |
if (!p) |
| 524 |
continue; |
| 525 |
code = strtoul(p, NULL, 16); |
| 526 |
assert(code <= CHARCODE_MAX); |
| 527 |
ci = &tab[code]; |
| 528 |
|
| 529 |
p = get_field(line, 1); |
| 530 |
if (!p) |
| 531 |
continue; |
| 532 |
|
| 533 |
while (isspace(*p)) |
| 534 |
p++; |
| 535 |
status = *p; |
| 536 |
if (status != 'C' && status != 'S' && status != 'F') |
| 537 |
continue; |
| 538 |
|
| 539 |
p = get_field(line, 2); |
| 540 |
assert(p != NULL); |
| 541 |
if (status == 'S') { |
| 542 |
|
| 543 |
|
| 544 |
assert(ci->f_len >= 2); |
| 545 |
ci->f_len = 0; |
| 546 |
} else { |
| 547 |
assert(ci->f_len == 0); |
| 548 |
} |
| 549 |
for(;;) { |
| 550 |
while (isspace(*p)) |
| 551 |
p++; |
| 552 |
if (*p == ';') |
| 553 |
break; |
| 554 |
assert(ci->l_len < CC_LEN_MAX); |
| 555 |
ci->f_data[ci->f_len++] = strtoul(p, (char **)&p, 16); |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
fclose(f); |
| 560 |
} |
| 561 |
|
| 562 |
void parse_composition_exclusions(const char *filename) |
| 563 |
{ |
| 564 |
FILE *f; |
| 565 |
char line[4096], *p; |
| 566 |
uint32_t c0; |
| 567 |
|
| 568 |
f = fopen(filename, "rb"); |
| 569 |
if (!f) { |
| 570 |
perror(filename); |
| 571 |
exit(1); |
| 572 |
} |
| 573 |
|
| 574 |
for(;;) { |
| 575 |
if (!get_line(line, sizeof(line), f)) |
| 576 |
break; |
| 577 |
p = line; |
| 578 |
while (isspace(*p)) |
| 579 |
p++; |
| 580 |
if (*p == '#' || *p == '@' || *p == '\0') |
| 581 |
continue; |
| 582 |
c0 = strtoul(p, (char **)&p, 16); |
| 583 |
assert(c0 > 0 && c0 <= CHARCODE_MAX); |
| 584 |
unicode_db[c0].is_excluded = TRUE; |
| 585 |
} |
| 586 |
fclose(f); |
| 587 |
} |
| 588 |
|
| 589 |
void parse_derived_core_properties(const char *filename) |
| 590 |
{ |
| 591 |
FILE *f; |
| 592 |
char line[4096], *p, buf[256], *q; |
| 593 |
uint32_t c0, c1, c; |
| 594 |
int i; |
| 595 |
|
| 596 |
f = fopen(filename, "rb"); |
| 597 |
if (!f) { |
| 598 |
perror(filename); |
| 599 |
exit(1); |
| 600 |
} |
| 601 |
|
| 602 |
for(;;) { |
| 603 |
if (!get_line(line, sizeof(line), f)) |
| 604 |
break; |
| 605 |
p = line; |
| 606 |
while (isspace(*p)) |
| 607 |
p++; |
| 608 |
if (*p == '#' || *p == '@' || *p == '\0') |
| 609 |
continue; |
| 610 |
c0 = strtoul(p, (char **)&p, 16); |
| 611 |
if (*p == '.' && p[1] == '.') { |
| 612 |
p += 2; |
| 613 |
c1 = strtoul(p, (char **)&p, 16); |
| 614 |
} else { |
| 615 |
c1 = c0; |
| 616 |
} |
| 617 |
assert(c1 <= CHARCODE_MAX); |
| 618 |
p += strspn(p, " \t"); |
| 619 |
if (*p == ';') { |
| 620 |
p++; |
| 621 |
p += strspn(p, " \t"); |
| 622 |
q = buf; |
| 623 |
while (*p != '\0' && *p != ' ' && *p != '#' && *p != '\t') { |
| 624 |
if ((q - buf) < sizeof(buf) - 1) |
| 625 |
*q++ = *p; |
| 626 |
p++; |
| 627 |
} |
| 628 |
*q = '\0'; |
| 629 |
i = find_name(unicode_prop_name, |
| 630 |
countof(unicode_prop_name), buf); |
| 631 |
if (i < 0) { |
| 632 |
if (!strcmp(buf, "Grapheme_Link")) |
| 633 |
goto next; |
| 634 |
fprintf(stderr, "Property not found: %s\n", buf); |
| 635 |
exit(1); |
| 636 |
} |
| 637 |
for(c = c0; c <= c1; c++) { |
| 638 |
set_prop(c, i, 1); |
| 639 |
} |
| 640 |
next: ; |
| 641 |
} |
| 642 |
} |
| 643 |
fclose(f); |
| 644 |
} |
| 645 |
|
| 646 |
void parse_derived_norm_properties(const char *filename) |
| 647 |
{ |
| 648 |
FILE *f; |
| 649 |
char line[4096], *p, buf[256], *q; |
| 650 |
uint32_t c0, c1, c; |
| 651 |
|
| 652 |
f = fopen(filename, "rb"); |
| 653 |
if (!f) { |
| 654 |
perror(filename); |
| 655 |
exit(1); |
| 656 |
} |
| 657 |
|
| 658 |
for(;;) { |
| 659 |
if (!get_line(line, sizeof(line), f)) |
| 660 |
break; |
| 661 |
p = line; |
| 662 |
while (isspace(*p)) |
| 663 |
p++; |
| 664 |
if (*p == '#' || *p == '@' || *p == '\0') |
| 665 |
continue; |
| 666 |
c0 = strtoul(p, (char **)&p, 16); |
| 667 |
if (*p == '.' && p[1] == '.') { |
| 668 |
p += 2; |
| 669 |
c1 = strtoul(p, (char **)&p, 16); |
| 670 |
} else { |
| 671 |
c1 = c0; |
| 672 |
} |
| 673 |
assert(c1 <= CHARCODE_MAX); |
| 674 |
p += strspn(p, " \t"); |
| 675 |
if (*p == ';') { |
| 676 |
p++; |
| 677 |
p += strspn(p, " \t"); |
| 678 |
q = buf; |
| 679 |
while (*p != '\0' && *p != ' ' && *p != '#' && *p != '\t') { |
| 680 |
if ((q - buf) < sizeof(buf) - 1) |
| 681 |
*q++ = *p; |
| 682 |
p++; |
| 683 |
} |
| 684 |
*q = '\0'; |
| 685 |
if (!strcmp(buf, "Changes_When_NFKC_Casefolded")) { |
| 686 |
for(c = c0; c <= c1; c++) { |
| 687 |
set_prop(c, PROP_Changes_When_NFKC_Casefolded, 1); |
| 688 |
} |
| 689 |
} |
| 690 |
} |
| 691 |
} |
| 692 |
fclose(f); |
| 693 |
} |
| 694 |
|
| 695 |
void parse_prop_list(const char *filename) |
| 696 |
{ |
| 697 |
FILE *f; |
| 698 |
char line[4096], *p, buf[256], *q; |
| 699 |
uint32_t c0, c1, c; |
| 700 |
int i; |
| 701 |
|
| 702 |
f = fopen(filename, "rb"); |
| 703 |
if (!f) { |
| 704 |
perror(filename); |
| 705 |
exit(1); |
| 706 |
} |
| 707 |
|
| 708 |
for(;;) { |
| 709 |
if (!get_line(line, sizeof(line), f)) |
| 710 |
break; |
| 711 |
p = line; |
| 712 |
while (isspace(*p)) |
| 713 |
p++; |
| 714 |
if (*p == '#' || *p == '@' || *p == '\0') |
| 715 |
continue; |
| 716 |
c0 = strtoul(p, (char **)&p, 16); |
| 717 |
if (*p == '.' && p[1] == '.') { |
| 718 |
p += 2; |
| 719 |
c1 = strtoul(p, (char **)&p, 16); |
| 720 |
} else { |
| 721 |
c1 = c0; |
| 722 |
} |
| 723 |
assert(c1 <= CHARCODE_MAX); |
| 724 |
p += strspn(p, " \t"); |
| 725 |
if (*p == ';') { |
| 726 |
p++; |
| 727 |
p += strspn(p, " \t"); |
| 728 |
q = buf; |
| 729 |
while (*p != '\0' && *p != ' ' && *p != '#' && *p != '\t') { |
| 730 |
if ((q - buf) < sizeof(buf) - 1) |
| 731 |
*q++ = *p; |
| 732 |
p++; |
| 733 |
} |
| 734 |
*q = '\0'; |
| 735 |
i = find_name(unicode_prop_name, |
| 736 |
countof(unicode_prop_name), buf); |
| 737 |
if (i < 0) { |
| 738 |
fprintf(stderr, "Property not found: %s\n", buf); |
| 739 |
exit(1); |
| 740 |
} |
| 741 |
for(c = c0; c <= c1; c++) { |
| 742 |
set_prop(c, i, 1); |
| 743 |
} |
| 744 |
} |
| 745 |
} |
| 746 |
fclose(f); |
| 747 |
} |
| 748 |
|
| 749 |
void parse_scripts(const char *filename) |
| 750 |
{ |
| 751 |
FILE *f; |
| 752 |
char line[4096], *p, buf[256], *q; |
| 753 |
uint32_t c0, c1, c; |
| 754 |
int i; |
| 755 |
|
| 756 |
f = fopen(filename, "rb"); |
| 757 |
if (!f) { |
| 758 |
perror(filename); |
| 759 |
exit(1); |
| 760 |
} |
| 761 |
|
| 762 |
for(;;) { |
| 763 |
if (!get_line(line, sizeof(line), f)) |
| 764 |
break; |
| 765 |
p = line; |
| 766 |
while (isspace(*p)) |
| 767 |
p++; |
| 768 |
if (*p == '#' || *p == '@' || *p == '\0') |
| 769 |
continue; |
| 770 |
c0 = strtoul(p, (char **)&p, 16); |
| 771 |
if (*p == '.' && p[1] == '.') { |
| 772 |
p += 2; |
| 773 |
c1 = strtoul(p, (char **)&p, 16); |
| 774 |
} else { |
| 775 |
c1 = c0; |
| 776 |
} |
| 777 |
assert(c1 <= CHARCODE_MAX); |
| 778 |
p += strspn(p, " \t"); |
| 779 |
if (*p == ';') { |
| 780 |
p++; |
| 781 |
p += strspn(p, " \t"); |
| 782 |
q = buf; |
| 783 |
while (*p != '\0' && *p != ' ' && *p != '#' && *p != '\t') { |
| 784 |
if ((q - buf) < sizeof(buf) - 1) |
| 785 |
*q++ = *p; |
| 786 |
p++; |
| 787 |
} |
| 788 |
*q = '\0'; |
| 789 |
i = find_name(unicode_script_name, |
| 790 |
countof(unicode_script_name), buf); |
| 791 |
if (i < 0) { |
| 792 |
fprintf(stderr, "Unknown script: '%s'\n", buf); |
| 793 |
exit(1); |
| 794 |
} |
| 795 |
for(c = c0; c <= c1; c++) |
| 796 |
unicode_db[c].script = i; |
| 797 |
} |
| 798 |
} |
| 799 |
fclose(f); |
| 800 |
} |
| 801 |
|
| 802 |
void parse_script_extensions(const char *filename) |
| 803 |
{ |
| 804 |
FILE *f; |
| 805 |
char line[4096], *p, buf[256], *q; |
| 806 |
uint32_t c0, c1, c; |
| 807 |
int i; |
| 808 |
uint8_t script_ext[255]; |
| 809 |
int script_ext_len; |
| 810 |
|
| 811 |
f = fopen(filename, "rb"); |
| 812 |
if (!f) { |
| 813 |
perror(filename); |
| 814 |
exit(1); |
| 815 |
} |
| 816 |
|
| 817 |
for(;;) { |
| 818 |
if (!get_line(line, sizeof(line), f)) |
| 819 |
break; |
| 820 |
p = line; |
| 821 |
while (isspace(*p)) |
| 822 |
p++; |
| 823 |
if (*p == '#' || *p == '@' || *p == '\0') |
| 824 |
continue; |
| 825 |
c0 = strtoul(p, (char **)&p, 16); |
| 826 |
if (*p == '.' && p[1] == '.') { |
| 827 |
p += 2; |
| 828 |
c1 = strtoul(p, (char **)&p, 16); |
| 829 |
} else { |
| 830 |
c1 = c0; |
| 831 |
} |
| 832 |
assert(c1 <= CHARCODE_MAX); |
| 833 |
p += strspn(p, " \t"); |
| 834 |
script_ext_len = 0; |
| 835 |
if (*p == ';') { |
| 836 |
p++; |
| 837 |
for(;;) { |
| 838 |
p += strspn(p, " \t"); |
| 839 |
q = buf; |
| 840 |
while (*p != '\0' && *p != ' ' && *p != '#' && *p != '\t') { |
| 841 |
if ((q - buf) < sizeof(buf) - 1) |
| 842 |
*q++ = *p; |
| 843 |
p++; |
| 844 |
} |
| 845 |
*q = '\0'; |
| 846 |
if (buf[0] == '\0') |
| 847 |
break; |
| 848 |
i = find_name(unicode_script_short_name, |
| 849 |
countof(unicode_script_short_name), buf); |
| 850 |
if (i < 0) { |
| 851 |
fprintf(stderr, "Script not found: %s\n", buf); |
| 852 |
exit(1); |
| 853 |
} |
| 854 |
assert(script_ext_len < sizeof(script_ext)); |
| 855 |
script_ext[script_ext_len++] = i; |
| 856 |
} |
| 857 |
for(c = c0; c <= c1; c++) { |
| 858 |
CCInfo *ci = &unicode_db[c]; |
| 859 |
ci->script_ext_len = script_ext_len; |
| 860 |
ci->script_ext = malloc(sizeof(ci->script_ext[0]) * script_ext_len); |
| 861 |
for(i = 0; i < script_ext_len; i++) |
| 862 |
ci->script_ext[i] = script_ext[i]; |
| 863 |
} |
| 864 |
} |
| 865 |
} |
| 866 |
fclose(f); |
| 867 |
} |
| 868 |
|
| 869 |
void dump_cc_info(CCInfo *ci, int i) |
| 870 |
{ |
| 871 |
int j; |
| 872 |
printf("%05x:", i); |
| 873 |
if (ci->u_len != 0) { |
| 874 |
printf(" U:"); |
| 875 |
for(j = 0; j < ci->u_len; j++) |
| 876 |
printf(" %05x", ci->u_data[j]); |
| 877 |
} |
| 878 |
if (ci->l_len != 0) { |
| 879 |
printf(" L:"); |
| 880 |
for(j = 0; j < ci->l_len; j++) |
| 881 |
printf(" %05x", ci->l_data[j]); |
| 882 |
} |
| 883 |
if (ci->f_len != 0) { |
| 884 |
printf(" F:"); |
| 885 |
for(j = 0; j < ci->f_len; j++) |
| 886 |
printf(" %05x", ci->f_data[j]); |
| 887 |
} |
| 888 |
printf("\n"); |
| 889 |
} |
| 890 |
|
| 891 |
void dump_unicode_data(CCInfo *tab) |
| 892 |
{ |
| 893 |
int i; |
| 894 |
CCInfo *ci; |
| 895 |
for(i = 0; i <= CHARCODE_MAX; i++) { |
| 896 |
ci = &tab[i]; |
| 897 |
if (ci->u_len != 0 || ci->l_len != 0 || ci->f_len != 0) { |
| 898 |
dump_cc_info(ci, i); |
| 899 |
} |
| 900 |
} |
| 901 |
} |
| 902 |
|
| 903 |
BOOL is_complicated_case(const CCInfo *ci) |
| 904 |
{ |
| 905 |
return (ci->u_len > 1 || ci->l_len > 1 || |
| 906 |
(ci->u_len > 0 && ci->l_len > 0) || |
| 907 |
(ci->f_len != ci->l_len) || |
| 908 |
(memcmp(ci->f_data, ci->l_data, ci->f_len * sizeof(ci->f_data[0])) != 0)); |
| 909 |
} |
| 910 |
|
| 911 |
#ifndef USE_TEST |
| 912 |
enum { |
| 913 |
RUN_TYPE_U, |
| 914 |
RUN_TYPE_L, |
| 915 |
RUN_TYPE_UF, |
| 916 |
RUN_TYPE_LF, |
| 917 |
RUN_TYPE_UL, |
| 918 |
RUN_TYPE_LSU, |
| 919 |
RUN_TYPE_U2L_399_EXT2, |
| 920 |
RUN_TYPE_UF_D20, |
| 921 |
RUN_TYPE_UF_D1_EXT, |
| 922 |
RUN_TYPE_U_EXT, |
| 923 |
RUN_TYPE_LF_EXT, |
| 924 |
RUN_TYPE_UF_EXT2, |
| 925 |
RUN_TYPE_LF_EXT2, |
| 926 |
RUN_TYPE_UF_EXT3, |
| 927 |
}; |
| 928 |
#endif |
| 929 |
|
| 930 |
const char *run_type_str[] = { |
| 931 |
"U", |
| 932 |
"L", |
| 933 |
"UF", |
| 934 |
"LF", |
| 935 |
"UL", |
| 936 |
"LSU", |
| 937 |
"U2L_399_EXT2", |
| 938 |
"UF_D20", |
| 939 |
"UF_D1_EXT", |
| 940 |
"U_EXT", |
| 941 |
"LF_EXT", |
| 942 |
"UF_EXT2", |
| 943 |
"LF_EXT2", |
| 944 |
"UF_EXT3", |
| 945 |
}; |
| 946 |
|
| 947 |
typedef struct { |
| 948 |
int code; |
| 949 |
int len; |
| 950 |
int type; |
| 951 |
int data; |
| 952 |
int ext_len; |
| 953 |
int ext_data[3]; |
| 954 |
int data_index; |
| 955 |
} TableEntry; |
| 956 |
|
| 957 |
static int simple_to_lower(CCInfo *tab, int c) |
| 958 |
{ |
| 959 |
if (tab[c].l_len != 1) |
| 960 |
return c; |
| 961 |
return tab[c].l_data[0]; |
| 962 |
} |
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
void find_run_type(TableEntry *te, CCInfo *tab, int code) |
| 967 |
{ |
| 968 |
int is_lower, len; |
| 969 |
CCInfo *ci, *ci1, *ci2; |
| 970 |
|
| 971 |
ci = &tab[code]; |
| 972 |
ci1 = &tab[code + 1]; |
| 973 |
ci2 = &tab[code + 2]; |
| 974 |
te->code = code; |
| 975 |
|
| 976 |
if (ci->l_len == 1 && ci->l_data[0] == code + 2 && |
| 977 |
ci->f_len == 1 && ci->f_data[0] == ci->l_data[0] && |
| 978 |
ci->u_len == 0 && |
| 979 |
|
| 980 |
ci1->l_len == 1 && ci1->l_data[0] == code + 2 && |
| 981 |
ci1->f_len == 1 && ci1->f_data[0] == ci1->l_data[0] && |
| 982 |
ci1->u_len == 1 && ci1->u_data[0] == code && |
| 983 |
|
| 984 |
ci2->l_len == 0 && |
| 985 |
ci2->f_len == 0 && |
| 986 |
ci2->u_len == 1 && ci2->u_data[0] == code) { |
| 987 |
te->len = 3; |
| 988 |
te->data = 0; |
| 989 |
te->type = RUN_TYPE_LSU; |
| 990 |
return; |
| 991 |
} |
| 992 |
|
| 993 |
if (is_complicated_case(ci)) { |
| 994 |
len = 1; |
| 995 |
while (code + len <= CHARCODE_MAX) { |
| 996 |
ci1 = &tab[code + len]; |
| 997 |
if (ci1->u_len != 1 || |
| 998 |
ci1->u_data[0] != ci->u_data[0] + len || |
| 999 |
ci1->l_len != 0 || |
| 1000 |
ci1->f_len != 1 || ci1->f_data[0] != ci1->u_data[0]) |
| 1001 |
break; |
| 1002 |
len++; |
| 1003 |
} |
| 1004 |
if (len > 1) { |
| 1005 |
te->len = len; |
| 1006 |
te->type = RUN_TYPE_UF; |
| 1007 |
te->data = ci->u_data[0]; |
| 1008 |
return; |
| 1009 |
} |
| 1010 |
|
| 1011 |
if (ci->l_len == 0 && |
| 1012 |
ci->u_len == 2 && ci->u_data[1] == 0x399 && |
| 1013 |
ci->f_len == 2 && ci->f_data[1] == 0x3B9 && |
| 1014 |
ci->f_data[0] == simple_to_lower(tab, ci->u_data[0])) { |
| 1015 |
len = 1; |
| 1016 |
while (code + len <= CHARCODE_MAX) { |
| 1017 |
ci1 = &tab[code + len]; |
| 1018 |
if (!(ci1->u_len == 2 && |
| 1019 |
ci1->u_data[1] == ci->u_data[1] && |
| 1020 |
ci1->u_data[0] == ci->u_data[0] + len && |
| 1021 |
ci1->f_len == 2 && |
| 1022 |
ci1->f_data[1] == ci->f_data[1] && |
| 1023 |
ci1->f_data[0] == ci->f_data[0] + len && |
| 1024 |
ci1->l_len == 0)) |
| 1025 |
break; |
| 1026 |
len++; |
| 1027 |
} |
| 1028 |
te->len = len; |
| 1029 |
te->type = RUN_TYPE_UF_EXT2; |
| 1030 |
te->ext_data[0] = ci->u_data[0]; |
| 1031 |
te->ext_data[1] = ci->u_data[1]; |
| 1032 |
te->ext_len = 2; |
| 1033 |
return; |
| 1034 |
} |
| 1035 |
|
| 1036 |
if (ci->u_len == 2 && ci->u_data[1] == 0x399 && |
| 1037 |
ci->l_len == 1 && |
| 1038 |
ci->f_len == 1 && ci->f_data[0] == ci->l_data[0]) { |
| 1039 |
len = 1; |
| 1040 |
while (code + len <= CHARCODE_MAX) { |
| 1041 |
ci1 = &tab[code + len]; |
| 1042 |
if (!(ci1->u_len == 2 && |
| 1043 |
ci1->u_data[1] == 0x399 && |
| 1044 |
ci1->u_data[0] == ci->u_data[0] + len && |
| 1045 |
ci1->l_len == 1 && |
| 1046 |
ci1->l_data[0] == ci->l_data[0] + len && |
| 1047 |
ci1->f_len == 1 && ci1->f_data[0] == ci1->l_data[0])) |
| 1048 |
break; |
| 1049 |
len++; |
| 1050 |
} |
| 1051 |
te->len = len; |
| 1052 |
te->type = RUN_TYPE_U2L_399_EXT2; |
| 1053 |
te->ext_data[0] = ci->u_data[0]; |
| 1054 |
te->ext_data[1] = ci->l_data[0]; |
| 1055 |
te->ext_len = 2; |
| 1056 |
return; |
| 1057 |
} |
| 1058 |
|
| 1059 |
if (ci->l_len == 1 && ci->u_len == 0 && ci->f_len == 0) { |
| 1060 |
len = 1; |
| 1061 |
while (code + len <= CHARCODE_MAX) { |
| 1062 |
ci1 = &tab[code + len]; |
| 1063 |
if (!(ci1->l_len == 1 && |
| 1064 |
ci1->l_data[0] == ci->l_data[0] + len && |
| 1065 |
ci1->u_len == 0 && ci1->f_len == 0)) |
| 1066 |
break; |
| 1067 |
len++; |
| 1068 |
} |
| 1069 |
te->len = len; |
| 1070 |
te->type = RUN_TYPE_L; |
| 1071 |
te->data = ci->l_data[0]; |
| 1072 |
return; |
| 1073 |
} |
| 1074 |
|
| 1075 |
if (ci->l_len == 0 && |
| 1076 |
ci->u_len == 1 && |
| 1077 |
ci->u_data[0] < 0x1000 && |
| 1078 |
ci->f_len == 1 && ci->f_data[0] == ci->u_data[0] + 0x20) { |
| 1079 |
te->len = 1; |
| 1080 |
te->type = RUN_TYPE_UF_D20; |
| 1081 |
te->data = ci->u_data[0]; |
| 1082 |
} else if (ci->l_len == 0 && |
| 1083 |
ci->u_len == 1 && |
| 1084 |
ci->f_len == 1 && ci->f_data[0] == ci->u_data[0] + 1) { |
| 1085 |
te->len = 1; |
| 1086 |
te->type = RUN_TYPE_UF_D1_EXT; |
| 1087 |
te->ext_data[0] = ci->u_data[0]; |
| 1088 |
te->ext_len = 1; |
| 1089 |
} else if (ci->l_len == 2 && ci->u_len == 0 && ci->f_len == 2 && |
| 1090 |
ci->l_data[0] == ci->f_data[0] && |
| 1091 |
ci->l_data[1] == ci->f_data[1]) { |
| 1092 |
te->len = 1; |
| 1093 |
te->type = RUN_TYPE_LF_EXT2; |
| 1094 |
te->ext_data[0] = ci->l_data[0]; |
| 1095 |
te->ext_data[1] = ci->l_data[1]; |
| 1096 |
te->ext_len = 2; |
| 1097 |
} else if (ci->u_len == 2 && ci->l_len == 0 && ci->f_len == 2 && |
| 1098 |
ci->f_data[0] == simple_to_lower(tab, ci->u_data[0]) && |
| 1099 |
ci->f_data[1] == simple_to_lower(tab, ci->u_data[1])) { |
| 1100 |
te->len = 1; |
| 1101 |
te->type = RUN_TYPE_UF_EXT2; |
| 1102 |
te->ext_data[0] = ci->u_data[0]; |
| 1103 |
te->ext_data[1] = ci->u_data[1]; |
| 1104 |
te->ext_len = 2; |
| 1105 |
} else if (ci->u_len == 3 && ci->l_len == 0 && ci->f_len == 3 && |
| 1106 |
ci->f_data[0] == simple_to_lower(tab, ci->u_data[0]) && |
| 1107 |
ci->f_data[1] == simple_to_lower(tab, ci->u_data[1]) && |
| 1108 |
ci->f_data[2] == simple_to_lower(tab, ci->u_data[2])) { |
| 1109 |
te->len = 1; |
| 1110 |
te->type = RUN_TYPE_UF_EXT3; |
| 1111 |
te->ext_data[0] = ci->u_data[0]; |
| 1112 |
te->ext_data[1] = ci->u_data[1]; |
| 1113 |
te->ext_data[2] = ci->u_data[2]; |
| 1114 |
te->ext_len = 3; |
| 1115 |
} else { |
| 1116 |
printf("unsupported encoding case:\n"); |
| 1117 |
dump_cc_info(ci, code); |
| 1118 |
abort(); |
| 1119 |
} |
| 1120 |
} else { |
| 1121 |
|
| 1122 |
len = 0; |
| 1123 |
for(;;) { |
| 1124 |
if (code >= CHARCODE_MAX || len >= 126) |
| 1125 |
break; |
| 1126 |
ci = &tab[code + len]; |
| 1127 |
ci1 = &tab[code + len + 1]; |
| 1128 |
if (is_complicated_case(ci) || is_complicated_case(ci1)) { |
| 1129 |
break; |
| 1130 |
} |
| 1131 |
if (ci->l_len != 1 || ci->l_data[0] != code + len + 1) |
| 1132 |
break; |
| 1133 |
if (ci1->u_len != 1 || ci1->u_data[0] != code + len) |
| 1134 |
break; |
| 1135 |
len += 2; |
| 1136 |
} |
| 1137 |
if (len > 0) { |
| 1138 |
te->len = len; |
| 1139 |
te->type = RUN_TYPE_UL; |
| 1140 |
te->data = 0; |
| 1141 |
return; |
| 1142 |
} |
| 1143 |
|
| 1144 |
ci = &tab[code]; |
| 1145 |
is_lower = ci->l_len > 0; |
| 1146 |
len = 1; |
| 1147 |
while (code + len <= CHARCODE_MAX) { |
| 1148 |
ci1 = &tab[code + len]; |
| 1149 |
if (is_complicated_case(ci1)) |
| 1150 |
break; |
| 1151 |
if (is_lower) { |
| 1152 |
if (ci1->l_len != 1 || |
| 1153 |
ci1->l_data[0] != ci->l_data[0] + len) |
| 1154 |
break; |
| 1155 |
} else { |
| 1156 |
if (ci1->u_len != 1 || |
| 1157 |
ci1->u_data[0] != ci->u_data[0] + len) |
| 1158 |
break; |
| 1159 |
} |
| 1160 |
len++; |
| 1161 |
} |
| 1162 |
te->len = len; |
| 1163 |
if (is_lower) { |
| 1164 |
te->type = RUN_TYPE_LF; |
| 1165 |
te->data = ci->l_data[0]; |
| 1166 |
} else { |
| 1167 |
te->type = RUN_TYPE_U; |
| 1168 |
te->data = ci->u_data[0]; |
| 1169 |
} |
| 1170 |
} |
| 1171 |
} |
| 1172 |
|
| 1173 |
TableEntry conv_table[1000]; |
| 1174 |
int conv_table_len; |
| 1175 |
int ext_data[1000]; |
| 1176 |
int ext_data_len; |
| 1177 |
|
| 1178 |
void dump_case_conv_table1(void) |
| 1179 |
{ |
| 1180 |
int i, j; |
| 1181 |
const TableEntry *te; |
| 1182 |
|
| 1183 |
for(i = 0; i < conv_table_len; i++) { |
| 1184 |
te = &conv_table[i]; |
| 1185 |
printf("%05x %02x %-10s %05x", |
| 1186 |
te->code, te->len, run_type_str[te->type], te->data); |
| 1187 |
for(j = 0; j < te->ext_len; j++) { |
| 1188 |
printf(" %05x", te->ext_data[j]); |
| 1189 |
} |
| 1190 |
printf("\n"); |
| 1191 |
} |
| 1192 |
printf("table_len=%d ext_len=%d\n", conv_table_len, ext_data_len); |
| 1193 |
} |
| 1194 |
|
| 1195 |
int find_data_index(const TableEntry *conv_table, int len, int data) |
| 1196 |
{ |
| 1197 |
int i; |
| 1198 |
const TableEntry *te; |
| 1199 |
for(i = 0; i < len; i++) { |
| 1200 |
te = &conv_table[i]; |
| 1201 |
if (te->code == data) |
| 1202 |
return i; |
| 1203 |
} |
| 1204 |
return -1; |
| 1205 |
} |
| 1206 |
|
| 1207 |
int find_ext_data_index(int data) |
| 1208 |
{ |
| 1209 |
int i; |
| 1210 |
for(i = 0; i < ext_data_len; i++) { |
| 1211 |
if (ext_data[i] == data) |
| 1212 |
return i; |
| 1213 |
} |
| 1214 |
assert(ext_data_len < countof(ext_data)); |
| 1215 |
ext_data[ext_data_len++] = data; |
| 1216 |
return ext_data_len - 1; |
| 1217 |
} |
| 1218 |
|
| 1219 |
void build_conv_table(CCInfo *tab) |
| 1220 |
{ |
| 1221 |
int code, i, j; |
| 1222 |
CCInfo *ci; |
| 1223 |
TableEntry *te; |
| 1224 |
|
| 1225 |
te = conv_table; |
| 1226 |
for(code = 0; code <= CHARCODE_MAX; code++) { |
| 1227 |
ci = &tab[code]; |
| 1228 |
if (ci->u_len == 0 && ci->l_len == 0 && ci->f_len == 0) |
| 1229 |
continue; |
| 1230 |
assert(te - conv_table < countof(conv_table)); |
| 1231 |
find_run_type(te, tab, code); |
| 1232 |
#if 0 |
| 1233 |
if (te->type == RUN_TYPE_TODO) { |
| 1234 |
printf("TODO: "); |
| 1235 |
dump_cc_info(ci, code); |
| 1236 |
} |
| 1237 |
#endif |
| 1238 |
assert(te->len <= 127); |
| 1239 |
code += te->len - 1; |
| 1240 |
te++; |
| 1241 |
} |
| 1242 |
conv_table_len = te - conv_table; |
| 1243 |
|
| 1244 |
|
| 1245 |
for(i = 0; i < conv_table_len; i++) { |
| 1246 |
int data_index; |
| 1247 |
te = &conv_table[i]; |
| 1248 |
|
| 1249 |
switch(te->type) { |
| 1250 |
case RUN_TYPE_U: |
| 1251 |
case RUN_TYPE_L: |
| 1252 |
case RUN_TYPE_UF: |
| 1253 |
case RUN_TYPE_LF: |
| 1254 |
data_index = find_data_index(conv_table, conv_table_len, te->data); |
| 1255 |
if (data_index < 0) { |
| 1256 |
switch(te->type) { |
| 1257 |
case RUN_TYPE_U: |
| 1258 |
te->type = RUN_TYPE_U_EXT; |
| 1259 |
te->ext_len = 1; |
| 1260 |
te->ext_data[0] = te->data; |
| 1261 |
break; |
| 1262 |
case RUN_TYPE_LF: |
| 1263 |
te->type = RUN_TYPE_LF_EXT; |
| 1264 |
te->ext_len = 1; |
| 1265 |
te->ext_data[0] = te->data; |
| 1266 |
break; |
| 1267 |
default: |
| 1268 |
printf("%05x: index not found\n", te->code); |
| 1269 |
exit(1); |
| 1270 |
} |
| 1271 |
} else { |
| 1272 |
te->data_index = data_index; |
| 1273 |
} |
| 1274 |
break; |
| 1275 |
case RUN_TYPE_UF_D20: |
| 1276 |
te->data_index = te->data; |
| 1277 |
break; |
| 1278 |
} |
| 1279 |
} |
| 1280 |
|
| 1281 |
|
| 1282 |
for(i = 0; i < conv_table_len; i++) { |
| 1283 |
te = &conv_table[i]; |
| 1284 |
if (te->type == RUN_TYPE_UF_EXT3) { |
| 1285 |
int p, v; |
| 1286 |
v = 0; |
| 1287 |
for(j = 0; j < 3; j++) { |
| 1288 |
p = find_ext_data_index(te->ext_data[j]); |
| 1289 |
assert(p < 16); |
| 1290 |
v = (v << 4) | p; |
| 1291 |
} |
| 1292 |
te->data_index = v; |
| 1293 |
} |
| 1294 |
} |
| 1295 |
|
| 1296 |
for(i = 0; i < conv_table_len; i++) { |
| 1297 |
te = &conv_table[i]; |
| 1298 |
if (te->type == RUN_TYPE_LF_EXT2 || |
| 1299 |
te->type == RUN_TYPE_UF_EXT2 || |
| 1300 |
te->type == RUN_TYPE_U2L_399_EXT2) { |
| 1301 |
int p, v; |
| 1302 |
v = 0; |
| 1303 |
for(j = 0; j < 2; j++) { |
| 1304 |
p = find_ext_data_index(te->ext_data[j]); |
| 1305 |
assert(p < 64); |
| 1306 |
v = (v << 6) | p; |
| 1307 |
} |
| 1308 |
te->data_index = v; |
| 1309 |
} |
| 1310 |
} |
| 1311 |
|
| 1312 |
for(i = 0; i < conv_table_len; i++) { |
| 1313 |
te = &conv_table[i]; |
| 1314 |
if (te->type == RUN_TYPE_UF_D1_EXT || |
| 1315 |
te->type == RUN_TYPE_U_EXT || |
| 1316 |
te->type == RUN_TYPE_LF_EXT) { |
| 1317 |
te->data_index = find_ext_data_index(te->ext_data[0]); |
| 1318 |
} |
| 1319 |
} |
| 1320 |
#ifdef DUMP_CASE_CONV_TABLE |
| 1321 |
dump_case_conv_table1(); |
| 1322 |
#endif |
| 1323 |
} |
| 1324 |
|
| 1325 |
void dump_case_conv_table(FILE *f) |
| 1326 |
{ |
| 1327 |
int i; |
| 1328 |
uint32_t v; |
| 1329 |
const TableEntry *te; |
| 1330 |
|
| 1331 |
fprintf(f, "static const uint32_t case_conv_table1[%u] = {", conv_table_len); |
| 1332 |
for(i = 0; i < conv_table_len; i++) { |
| 1333 |
if (i % 4 == 0) |
| 1334 |
fprintf(f, "\n "); |
| 1335 |
te = &conv_table[i]; |
| 1336 |
v = te->code << (32 - 17); |
| 1337 |
v |= te->len << (32 - 17 - 7); |
| 1338 |
v |= te->type << (32 - 17 - 7 - 4); |
| 1339 |
v |= te->data_index >> 8; |
| 1340 |
fprintf(f, " 0x%08x,", v); |
| 1341 |
} |
| 1342 |
fprintf(f, "\n};\n\n"); |
| 1343 |
|
| 1344 |
fprintf(f, "static const uint8_t case_conv_table2[%u] = {", conv_table_len); |
| 1345 |
for(i = 0; i < conv_table_len; i++) { |
| 1346 |
if (i % 8 == 0) |
| 1347 |
fprintf(f, "\n "); |
| 1348 |
te = &conv_table[i]; |
| 1349 |
fprintf(f, " 0x%02x,", te->data_index & 0xff); |
| 1350 |
} |
| 1351 |
fprintf(f, "\n};\n\n"); |
| 1352 |
|
| 1353 |
fprintf(f, "static const uint16_t case_conv_ext[%u] = {", ext_data_len); |
| 1354 |
for(i = 0; i < ext_data_len; i++) { |
| 1355 |
if (i % 8 == 0) |
| 1356 |
fprintf(f, "\n "); |
| 1357 |
fprintf(f, " 0x%04x,", ext_data[i]); |
| 1358 |
} |
| 1359 |
fprintf(f, "\n};\n\n"); |
| 1360 |
} |
| 1361 |
|
| 1362 |
|
| 1363 |
static CCInfo *global_tab; |
| 1364 |
|
| 1365 |
static int sp_cc_cmp(const void *p1, const void *p2) |
| 1366 |
{ |
| 1367 |
CCInfo *c1 = &global_tab[*(const int *)p1]; |
| 1368 |
CCInfo *c2 = &global_tab[*(const int *)p2]; |
| 1369 |
if (c1->f_len < c2->f_len) { |
| 1370 |
return -1; |
| 1371 |
} else if (c2->f_len < c1->f_len) { |
| 1372 |
return 1; |
| 1373 |
} else { |
| 1374 |
return memcmp(c1->f_data, c2->f_data, sizeof(c1->f_data[0]) * c1->f_len); |
| 1375 |
} |
| 1376 |
} |
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
void dump_case_folding_special_cases(CCInfo *tab) |
| 1381 |
{ |
| 1382 |
int i, len, j; |
| 1383 |
int *perm; |
| 1384 |
|
| 1385 |
perm = malloc(sizeof(perm[0]) * (CHARCODE_MAX + 1)); |
| 1386 |
for(i = 0; i <= CHARCODE_MAX; i++) |
| 1387 |
perm[i] = i; |
| 1388 |
global_tab = tab; |
| 1389 |
qsort(perm, CHARCODE_MAX + 1, sizeof(perm[0]), sp_cc_cmp); |
| 1390 |
for(i = 0; i <= CHARCODE_MAX;) { |
| 1391 |
if (tab[perm[i]].f_len <= 1) { |
| 1392 |
i++; |
| 1393 |
} else { |
| 1394 |
len = 1; |
| 1395 |
while ((i + len) <= CHARCODE_MAX && !sp_cc_cmp(&perm[i], &perm[i + len])) |
| 1396 |
len++; |
| 1397 |
|
| 1398 |
if (len > 1) { |
| 1399 |
for(j = i; j < i + len; j++) |
| 1400 |
dump_cc_info(&tab[perm[j]], perm[j]); |
| 1401 |
} |
| 1402 |
i += len; |
| 1403 |
} |
| 1404 |
} |
| 1405 |
free(perm); |
| 1406 |
global_tab = NULL; |
| 1407 |
} |
| 1408 |
|
| 1409 |
|
| 1410 |
int tabcmp(const int *tab1, const int *tab2, int n) |
| 1411 |
{ |
| 1412 |
int i; |
| 1413 |
for(i = 0; i < n; i++) { |
| 1414 |
if (tab1[i] != tab2[i]) |
| 1415 |
return -1; |
| 1416 |
} |
| 1417 |
return 0; |
| 1418 |
} |
| 1419 |
|
| 1420 |
void dump_str(const char *str, const int *buf, int len) |
| 1421 |
{ |
| 1422 |
int i; |
| 1423 |
printf("%s=", str); |
| 1424 |
for(i = 0; i < len; i++) |
| 1425 |
printf(" %05x", buf[i]); |
| 1426 |
printf("\n"); |
| 1427 |
} |
| 1428 |
|
| 1429 |
void compute_internal_props(void) |
| 1430 |
{ |
| 1431 |
int i; |
| 1432 |
BOOL has_ul; |
| 1433 |
|
| 1434 |
for(i = 0; i <= CHARCODE_MAX; i++) { |
| 1435 |
CCInfo *ci = &unicode_db[i]; |
| 1436 |
has_ul = (ci->u_len != 0 || ci->l_len != 0 || ci->f_len != 0); |
| 1437 |
if (has_ul) { |
| 1438 |
assert(get_prop(i, PROP_Cased)); |
| 1439 |
} else { |
| 1440 |
set_prop(i, PROP_Cased1, get_prop(i, PROP_Cased)); |
| 1441 |
} |
| 1442 |
set_prop(i, PROP_ID_Continue1, |
| 1443 |
get_prop(i, PROP_ID_Continue) & (get_prop(i, PROP_ID_Start) ^ 1)); |
| 1444 |
set_prop(i, PROP_XID_Start1, |
| 1445 |
get_prop(i, PROP_ID_Start) ^ get_prop(i, PROP_XID_Start)); |
| 1446 |
set_prop(i, PROP_XID_Continue1, |
| 1447 |
get_prop(i, PROP_ID_Continue) ^ get_prop(i, PROP_XID_Continue)); |
| 1448 |
set_prop(i, PROP_Changes_When_Titlecased1, |
| 1449 |
get_prop(i, PROP_Changes_When_Titlecased) ^ (ci->u_len != 0)); |
| 1450 |
set_prop(i, PROP_Changes_When_Casefolded1, |
| 1451 |
get_prop(i, PROP_Changes_When_Casefolded) ^ (ci->f_len != 0)); |
| 1452 |
|
| 1453 |
set_prop(i, PROP_Changes_When_NFKC_Casefolded1, |
| 1454 |
get_prop(i, PROP_Changes_When_NFKC_Casefolded) ^ (ci->f_len != 0)); |
| 1455 |
#if 0 |
| 1456 |
|
| 1457 |
#define M(x) (1U << GCAT_ ## x) |
| 1458 |
{ |
| 1459 |
int b; |
| 1460 |
b = ((M(Mn) | M(Cf) | M(Lm) | M(Sk)) >> |
| 1461 |
unicode_db[i].general_category) & 1; |
| 1462 |
set_prop(i, PROP_Cased1, |
| 1463 |
get_prop(i, PROP_Case_Ignorable) ^ b); |
| 1464 |
} |
| 1465 |
#undef M |
| 1466 |
#endif |
| 1467 |
} |
| 1468 |
} |
| 1469 |
|
| 1470 |
void dump_byte_table(FILE *f, const char *cname, const uint8_t *tab, int len) |
| 1471 |
{ |
| 1472 |
int i; |
| 1473 |
fprintf(f, "static const uint8_t %s[%d] = {", cname, len); |
| 1474 |
for(i = 0; i < len; i++) { |
| 1475 |
if (i % 8 == 0) |
| 1476 |
fprintf(f, "\n "); |
| 1477 |
fprintf(f, " 0x%02x,", tab[i]); |
| 1478 |
} |
| 1479 |
fprintf(f, "\n};\n\n"); |
| 1480 |
} |
| 1481 |
|
| 1482 |
#define PROP_BLOCK_LEN 32 |
| 1483 |
|
| 1484 |
void build_prop_table(FILE *f, int prop_index, BOOL add_index) |
| 1485 |
{ |
| 1486 |
int i, j, n, v, offset, code; |
| 1487 |
DynBuf dbuf_s, *dbuf = &dbuf_s; |
| 1488 |
DynBuf dbuf1_s, *dbuf1 = &dbuf1_s; |
| 1489 |
DynBuf dbuf2_s, *dbuf2 = &dbuf2_s; |
| 1490 |
const uint32_t *buf; |
| 1491 |
int buf_len, block_end_pos, bit; |
| 1492 |
char cname[128]; |
| 1493 |
|
| 1494 |
dbuf_init(dbuf1); |
| 1495 |
|
| 1496 |
for(i = 0; i <= CHARCODE_MAX;) { |
| 1497 |
v = get_prop(i, prop_index); |
| 1498 |
j = i + 1; |
| 1499 |
while (j <= CHARCODE_MAX && get_prop(j, prop_index) == v) { |
| 1500 |
j++; |
| 1501 |
} |
| 1502 |
n = j - i; |
| 1503 |
if (j == (CHARCODE_MAX + 1) && v == 0) |
| 1504 |
break; |
| 1505 |
|
| 1506 |
dbuf_put_u32(dbuf1, n - 1); |
| 1507 |
i += n; |
| 1508 |
} |
| 1509 |
|
| 1510 |
dbuf_init(dbuf); |
| 1511 |
dbuf_init(dbuf2); |
| 1512 |
buf = (uint32_t *)dbuf1->buf; |
| 1513 |
buf_len = dbuf1->size / sizeof(buf[0]); |
| 1514 |
|
| 1515 |
|
| 1516 |
assert(get_prop(0, prop_index) == 0); |
| 1517 |
|
| 1518 |
block_end_pos = PROP_BLOCK_LEN; |
| 1519 |
i = 0; |
| 1520 |
code = 0; |
| 1521 |
bit = 0; |
| 1522 |
while (i < buf_len) { |
| 1523 |
if (add_index && dbuf->size >= block_end_pos && bit == 0) { |
| 1524 |
offset = (dbuf->size - block_end_pos); |
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
assert(offset <= 7); |
| 1529 |
v = code | (offset << 21); |
| 1530 |
dbuf_putc(dbuf2, v); |
| 1531 |
dbuf_putc(dbuf2, v >> 8); |
| 1532 |
dbuf_putc(dbuf2, v >> 16); |
| 1533 |
block_end_pos += PROP_BLOCK_LEN; |
| 1534 |
} |
| 1535 |
|
| 1536 |
v = buf[i]; |
| 1537 |
code += v + 1; |
| 1538 |
bit ^= 1; |
| 1539 |
if (v < 8 && (i + 1) < buf_len && buf[i + 1] < 8) { |
| 1540 |
code += buf[i + 1] + 1; |
| 1541 |
bit ^= 1; |
| 1542 |
dbuf_putc(dbuf, (v << 3) | buf[i + 1]); |
| 1543 |
i += 2; |
| 1544 |
} else if (v < 128) { |
| 1545 |
dbuf_putc(dbuf, 0x80 + v); |
| 1546 |
i++; |
| 1547 |
} else if (v < (1 << 13)) { |
| 1548 |
dbuf_putc(dbuf, 0x40 + (v >> 8)); |
| 1549 |
dbuf_putc(dbuf, v); |
| 1550 |
i++; |
| 1551 |
} else { |
| 1552 |
assert(v < (1 << 21)); |
| 1553 |
dbuf_putc(dbuf, 0x60 + (v >> 16)); |
| 1554 |
dbuf_putc(dbuf, v >> 8); |
| 1555 |
dbuf_putc(dbuf, v); |
| 1556 |
i++; |
| 1557 |
} |
| 1558 |
} |
| 1559 |
|
| 1560 |
if (add_index) { |
| 1561 |
|
| 1562 |
v = code; |
| 1563 |
dbuf_putc(dbuf2, v); |
| 1564 |
dbuf_putc(dbuf2, v >> 8); |
| 1565 |
dbuf_putc(dbuf2, v >> 16); |
| 1566 |
} |
| 1567 |
|
| 1568 |
#ifdef DUMP_TABLE_SIZE |
| 1569 |
printf("prop %s: length=%d bytes\n", unicode_prop_name[prop_index], |
| 1570 |
(int)(dbuf->size + dbuf2->size)); |
| 1571 |
#endif |
| 1572 |
snprintf(cname, sizeof(cname), "unicode_prop_%s_table", unicode_prop_name[prop_index]); |
| 1573 |
dump_byte_table(f, cname, dbuf->buf, dbuf->size); |
| 1574 |
if (add_index) { |
| 1575 |
snprintf(cname, sizeof(cname), "unicode_prop_%s_index", unicode_prop_name[prop_index]); |
| 1576 |
dump_byte_table(f, cname, dbuf2->buf, dbuf2->size); |
| 1577 |
} |
| 1578 |
|
| 1579 |
dbuf_free(dbuf); |
| 1580 |
dbuf_free(dbuf1); |
| 1581 |
dbuf_free(dbuf2); |
| 1582 |
} |
| 1583 |
|
| 1584 |
void build_flags_tables(FILE *f) |
| 1585 |
{ |
| 1586 |
build_prop_table(f, PROP_Cased1, TRUE); |
| 1587 |
build_prop_table(f, PROP_Case_Ignorable, TRUE); |
| 1588 |
build_prop_table(f, PROP_ID_Start, TRUE); |
| 1589 |
build_prop_table(f, PROP_ID_Continue1, TRUE); |
| 1590 |
} |
| 1591 |
|
| 1592 |
void dump_name_table(FILE *f, const char *cname, const char **tab_name, int len, |
| 1593 |
const char **tab_short_name) |
| 1594 |
{ |
| 1595 |
int i, w, maxw; |
| 1596 |
|
| 1597 |
maxw = 0; |
| 1598 |
for(i = 0; i < len; i++) { |
| 1599 |
w = strlen(tab_name[i]); |
| 1600 |
if (tab_short_name[i][0] != '\0') { |
| 1601 |
w += 1 + strlen(tab_short_name[i]); |
| 1602 |
} |
| 1603 |
if (maxw < w) |
| 1604 |
maxw = w; |
| 1605 |
} |
| 1606 |
|
| 1607 |
|
| 1608 |
fprintf(f, "static const char %s[] =\n", cname); |
| 1609 |
for(i = 0; i < len; i++) { |
| 1610 |
fprintf(f, " \""); |
| 1611 |
w = fprintf(f, "%s", tab_name[i]); |
| 1612 |
if (tab_short_name[i][0] != '\0') { |
| 1613 |
w += fprintf(f, ",%s", tab_short_name[i]); |
| 1614 |
} |
| 1615 |
fprintf(f, "\"%*s\"\\0\"\n", 1 + maxw - w, ""); |
| 1616 |
} |
| 1617 |
fprintf(f, ";\n\n"); |
| 1618 |
} |
| 1619 |
|
| 1620 |
void build_general_category_table(FILE *f) |
| 1621 |
{ |
| 1622 |
int i, v, j, n, n1; |
| 1623 |
DynBuf dbuf_s, *dbuf = &dbuf_s; |
| 1624 |
int cw_count, cw_len_count[4], cw_start; |
| 1625 |
|
| 1626 |
fprintf(f, "typedef enum {\n"); |
| 1627 |
for(i = 0; i < GCAT_COUNT; i++) |
| 1628 |
fprintf(f, " UNICODE_GC_%s,\n", unicode_gc_name[i]); |
| 1629 |
fprintf(f, " UNICODE_GC_COUNT,\n"); |
| 1630 |
fprintf(f, "} UnicodeGCEnum;\n\n"); |
| 1631 |
|
| 1632 |
dump_name_table(f, "unicode_gc_name_table", |
| 1633 |
unicode_gc_name, GCAT_COUNT, |
| 1634 |
unicode_gc_short_name); |
| 1635 |
|
| 1636 |
|
| 1637 |
dbuf_init(dbuf); |
| 1638 |
cw_count = 0; |
| 1639 |
for(i = 0; i < 4; i++) |
| 1640 |
cw_len_count[i] = 0; |
| 1641 |
for(i = 0; i <= CHARCODE_MAX;) { |
| 1642 |
v = unicode_db[i].general_category; |
| 1643 |
j = i + 1; |
| 1644 |
while (j <= CHARCODE_MAX && unicode_db[j].general_category == v) |
| 1645 |
j++; |
| 1646 |
n = j - i; |
| 1647 |
|
| 1648 |
if (v == GCAT_Lu) { |
| 1649 |
n1 = 1; |
| 1650 |
while ((i + n1) <= CHARCODE_MAX && unicode_db[i + n1].general_category == (v + (n1 & 1))) { |
| 1651 |
n1++; |
| 1652 |
} |
| 1653 |
if (n1 > n) { |
| 1654 |
v = 31; |
| 1655 |
n = n1; |
| 1656 |
} |
| 1657 |
} |
| 1658 |
|
| 1659 |
cw_count++; |
| 1660 |
n--; |
| 1661 |
cw_start = dbuf->size; |
| 1662 |
if (n < 7) { |
| 1663 |
dbuf_putc(dbuf, (n << 5) | v); |
| 1664 |
} else if (n < 7 + 128) { |
| 1665 |
n1 = n - 7; |
| 1666 |
assert(n1 < 128); |
| 1667 |
dbuf_putc(dbuf, (0xf << 5) | v); |
| 1668 |
dbuf_putc(dbuf, n1); |
| 1669 |
} else if (n < 7 + 128 + (1 << 14)) { |
| 1670 |
n1 = n - (7 + 128); |
| 1671 |
assert(n1 < (1 << 14)); |
| 1672 |
dbuf_putc(dbuf, (0xf << 5) | v); |
| 1673 |
dbuf_putc(dbuf, (n1 >> 8) + 128); |
| 1674 |
dbuf_putc(dbuf, n1); |
| 1675 |
} else { |
| 1676 |
n1 = n - (7 + 128 + (1 << 14)); |
| 1677 |
assert(n1 < (1 << 22)); |
| 1678 |
dbuf_putc(dbuf, (0xf << 5) | v); |
| 1679 |
dbuf_putc(dbuf, (n1 >> 16) + 128 + 64); |
| 1680 |
dbuf_putc(dbuf, n1 >> 8); |
| 1681 |
dbuf_putc(dbuf, n1); |
| 1682 |
} |
| 1683 |
cw_len_count[dbuf->size - cw_start - 1]++; |
| 1684 |
i += n + 1; |
| 1685 |
} |
| 1686 |
#ifdef DUMP_TABLE_SIZE |
| 1687 |
printf("general category: %d entries [", |
| 1688 |
cw_count); |
| 1689 |
for(i = 0; i < 4; i++) |
| 1690 |
printf(" %d", cw_len_count[i]); |
| 1691 |
printf(" ], length=%d bytes\n", (int)dbuf->size); |
| 1692 |
#endif |
| 1693 |
|
| 1694 |
dump_byte_table(f, "unicode_gc_table", dbuf->buf, dbuf->size); |
| 1695 |
|
| 1696 |
dbuf_free(dbuf); |
| 1697 |
} |
| 1698 |
|
| 1699 |
void build_script_table(FILE *f) |
| 1700 |
{ |
| 1701 |
int i, v, j, n, n1, type; |
| 1702 |
DynBuf dbuf_s, *dbuf = &dbuf_s; |
| 1703 |
int cw_count, cw_len_count[4], cw_start; |
| 1704 |
|
| 1705 |
fprintf(f, "typedef enum {\n"); |
| 1706 |
for(i = 0; i < SCRIPT_COUNT; i++) |
| 1707 |
fprintf(f, " UNICODE_SCRIPT_%s,\n", unicode_script_name[i]); |
| 1708 |
fprintf(f, " UNICODE_SCRIPT_COUNT,\n"); |
| 1709 |
fprintf(f, "} UnicodeScriptEnum;\n\n"); |
| 1710 |
|
| 1711 |
i = 1; |
| 1712 |
dump_name_table(f, "unicode_script_name_table", |
| 1713 |
unicode_script_name + i, SCRIPT_COUNT - i, |
| 1714 |
unicode_script_short_name + i); |
| 1715 |
|
| 1716 |
dbuf_init(dbuf); |
| 1717 |
cw_count = 0; |
| 1718 |
for(i = 0; i < 4; i++) |
| 1719 |
cw_len_count[i] = 0; |
| 1720 |
for(i = 0; i <= CHARCODE_MAX;) { |
| 1721 |
v = unicode_db[i].script; |
| 1722 |
j = i + 1; |
| 1723 |
while (j <= CHARCODE_MAX && unicode_db[j].script == v) |
| 1724 |
j++; |
| 1725 |
n = j - i; |
| 1726 |
if (v == 0 && j == (CHARCODE_MAX + 1)) |
| 1727 |
break; |
| 1728 |
|
| 1729 |
cw_count++; |
| 1730 |
n--; |
| 1731 |
cw_start = dbuf->size; |
| 1732 |
if (v == 0) |
| 1733 |
type = 0; |
| 1734 |
else |
| 1735 |
type = 1; |
| 1736 |
if (n < 96) { |
| 1737 |
dbuf_putc(dbuf, n | (type << 7)); |
| 1738 |
} else if (n < 96 + (1 << 12)) { |
| 1739 |
n1 = n - 96; |
| 1740 |
assert(n1 < (1 << 12)); |
| 1741 |
dbuf_putc(dbuf, ((n1 >> 8) + 96) | (type << 7)); |
| 1742 |
dbuf_putc(dbuf, n1); |
| 1743 |
} else { |
| 1744 |
n1 = n - (96 + (1 << 12)); |
| 1745 |
assert(n1 < (1 << 20)); |
| 1746 |
dbuf_putc(dbuf, ((n1 >> 16) + 112) | (type << 7)); |
| 1747 |
dbuf_putc(dbuf, n1 >> 8); |
| 1748 |
dbuf_putc(dbuf, n1); |
| 1749 |
} |
| 1750 |
if (type != 0) |
| 1751 |
dbuf_putc(dbuf, v); |
| 1752 |
|
| 1753 |
cw_len_count[dbuf->size - cw_start - 1]++; |
| 1754 |
i += n + 1; |
| 1755 |
} |
| 1756 |
#if defined(DUMP_TABLE_SIZE) |
| 1757 |
printf("script: %d entries [", |
| 1758 |
cw_count); |
| 1759 |
for(i = 0; i < 4; i++) |
| 1760 |
printf(" %d", cw_len_count[i]); |
| 1761 |
printf(" ], length=%d bytes\n", (int)dbuf->size); |
| 1762 |
#endif |
| 1763 |
|
| 1764 |
dump_byte_table(f, "unicode_script_table", dbuf->buf, dbuf->size); |
| 1765 |
|
| 1766 |
dbuf_free(dbuf); |
| 1767 |
} |
| 1768 |
|
| 1769 |
void build_script_ext_table(FILE *f) |
| 1770 |
{ |
| 1771 |
int i, j, n, n1, script_ext_len; |
| 1772 |
DynBuf dbuf_s, *dbuf = &dbuf_s; |
| 1773 |
int cw_count; |
| 1774 |
|
| 1775 |
dbuf_init(dbuf); |
| 1776 |
cw_count = 0; |
| 1777 |
for(i = 0; i <= CHARCODE_MAX;) { |
| 1778 |
script_ext_len = unicode_db[i].script_ext_len; |
| 1779 |
j = i + 1; |
| 1780 |
while (j <= CHARCODE_MAX && |
| 1781 |
unicode_db[j].script_ext_len == script_ext_len && |
| 1782 |
!memcmp(unicode_db[j].script_ext, unicode_db[i].script_ext, |
| 1783 |
script_ext_len)) { |
| 1784 |
j++; |
| 1785 |
} |
| 1786 |
n = j - i; |
| 1787 |
cw_count++; |
| 1788 |
n--; |
| 1789 |
if (n < 128) { |
| 1790 |
dbuf_putc(dbuf, n); |
| 1791 |
} else if (n < 128 + (1 << 14)) { |
| 1792 |
n1 = n - 128; |
| 1793 |
assert(n1 < (1 << 14)); |
| 1794 |
dbuf_putc(dbuf, (n1 >> 8) + 128); |
| 1795 |
dbuf_putc(dbuf, n1); |
| 1796 |
} else { |
| 1797 |
n1 = n - (128 + (1 << 14)); |
| 1798 |
assert(n1 < (1 << 22)); |
| 1799 |
dbuf_putc(dbuf, (n1 >> 16) + 128 + 64); |
| 1800 |
dbuf_putc(dbuf, n1 >> 8); |
| 1801 |
dbuf_putc(dbuf, n1); |
| 1802 |
} |
| 1803 |
dbuf_putc(dbuf, script_ext_len); |
| 1804 |
for(j = 0; j < script_ext_len; j++) |
| 1805 |
dbuf_putc(dbuf, unicode_db[i].script_ext[j]); |
| 1806 |
i += n + 1; |
| 1807 |
} |
| 1808 |
#ifdef DUMP_TABLE_SIZE |
| 1809 |
printf("script_ext: %d entries", |
| 1810 |
cw_count); |
| 1811 |
printf(", length=%d bytes\n", (int)dbuf->size); |
| 1812 |
#endif |
| 1813 |
|
| 1814 |
dump_byte_table(f, "unicode_script_ext_table", dbuf->buf, dbuf->size); |
| 1815 |
|
| 1816 |
dbuf_free(dbuf); |
| 1817 |
} |
| 1818 |
|
| 1819 |
|
| 1820 |
#define PROP_TABLE_COUNT PROP_ASCII |
| 1821 |
|
| 1822 |
void build_prop_list_table(FILE *f) |
| 1823 |
{ |
| 1824 |
int i; |
| 1825 |
|
| 1826 |
for(i = 0; i < PROP_TABLE_COUNT; i++) { |
| 1827 |
if (i == PROP_ID_Start || |
| 1828 |
i == PROP_Case_Ignorable || |
| 1829 |
i == PROP_ID_Continue1) { |
| 1830 |
|
| 1831 |
} else { |
| 1832 |
build_prop_table(f, i, FALSE); |
| 1833 |
} |
| 1834 |
} |
| 1835 |
|
| 1836 |
fprintf(f, "typedef enum {\n"); |
| 1837 |
for(i = 0; i < PROP_COUNT; i++) |
| 1838 |
fprintf(f, " UNICODE_PROP_%s,\n", unicode_prop_name[i]); |
| 1839 |
fprintf(f, " UNICODE_PROP_COUNT,\n"); |
| 1840 |
fprintf(f, "} UnicodePropertyEnum;\n\n"); |
| 1841 |
|
| 1842 |
i = PROP_ASCII_Hex_Digit; |
| 1843 |
dump_name_table(f, "unicode_prop_name_table", |
| 1844 |
unicode_prop_name + i, PROP_XID_Start - i + 1, |
| 1845 |
unicode_prop_short_name + i); |
| 1846 |
|
| 1847 |
fprintf(f, "static const uint8_t * const unicode_prop_table[] = {\n"); |
| 1848 |
for(i = 0; i < PROP_TABLE_COUNT; i++) { |
| 1849 |
fprintf(f, " unicode_prop_%s_table,\n", unicode_prop_name[i]); |
| 1850 |
} |
| 1851 |
fprintf(f, "};\n\n"); |
| 1852 |
|
| 1853 |
fprintf(f, "static const uint16_t unicode_prop_len_table[] = {\n"); |
| 1854 |
for(i = 0; i < PROP_TABLE_COUNT; i++) { |
| 1855 |
fprintf(f, " countof(unicode_prop_%s_table),\n", unicode_prop_name[i]); |
| 1856 |
} |
| 1857 |
fprintf(f, "};\n\n"); |
| 1858 |
} |
| 1859 |
|
| 1860 |
#ifdef USE_TEST |
| 1861 |
int check_conv(uint32_t *res, uint32_t c, int conv_type) |
| 1862 |
{ |
| 1863 |
return lre_case_conv(res, c, conv_type); |
| 1864 |
} |
| 1865 |
|
| 1866 |
void check_case_conv(void) |
| 1867 |
{ |
| 1868 |
CCInfo *tab = unicode_db; |
| 1869 |
uint32_t res[3]; |
| 1870 |
int l, error; |
| 1871 |
CCInfo ci_s, *ci1, *ci = &ci_s; |
| 1872 |
int code; |
| 1873 |
|
| 1874 |
for(code = 0; code <= CHARCODE_MAX; code++) { |
| 1875 |
ci1 = &tab[code]; |
| 1876 |
*ci = *ci1; |
| 1877 |
if (ci->l_len == 0) { |
| 1878 |
ci->l_len = 1; |
| 1879 |
ci->l_data[0] = code; |
| 1880 |
} |
| 1881 |
if (ci->u_len == 0) { |
| 1882 |
ci->u_len = 1; |
| 1883 |
ci->u_data[0] = code; |
| 1884 |
} |
| 1885 |
if (ci->f_len == 0) { |
| 1886 |
ci->f_len = 1; |
| 1887 |
ci->f_data[0] = code; |
| 1888 |
} |
| 1889 |
|
| 1890 |
error = 0; |
| 1891 |
l = check_conv(res, code, 0); |
| 1892 |
if (l != ci->u_len || tabcmp((int *)res, ci->u_data, l)) { |
| 1893 |
printf("ERROR: L\n"); |
| 1894 |
error++; |
| 1895 |
} |
| 1896 |
l = check_conv(res, code, 1); |
| 1897 |
if (l != ci->l_len || tabcmp((int *)res, ci->l_data, l)) { |
| 1898 |
printf("ERROR: U\n"); |
| 1899 |
error++; |
| 1900 |
} |
| 1901 |
l = check_conv(res, code, 2); |
| 1902 |
if (l != ci->f_len || tabcmp((int *)res, ci->f_data, l)) { |
| 1903 |
printf("ERROR: F\n"); |
| 1904 |
error++; |
| 1905 |
} |
| 1906 |
if (error) { |
| 1907 |
dump_cc_info(ci, code); |
| 1908 |
exit(1); |
| 1909 |
} |
| 1910 |
} |
| 1911 |
} |
| 1912 |
|
| 1913 |
#ifdef PROFILE |
| 1914 |
static int64_t get_time_ns(void) |
| 1915 |
{ |
| 1916 |
struct timespec ts; |
| 1917 |
clock_gettime(CLOCK_MONOTONIC, &ts); |
| 1918 |
return (int64_t)ts.tv_sec * 1000000000 + ts.tv_nsec; |
| 1919 |
} |
| 1920 |
#endif |
| 1921 |
|
| 1922 |
|
| 1923 |
void check_flags(void) |
| 1924 |
{ |
| 1925 |
int c; |
| 1926 |
BOOL flag_ref, flag; |
| 1927 |
for(c = 0; c <= CHARCODE_MAX; c++) { |
| 1928 |
flag_ref = get_prop(c, PROP_Cased); |
| 1929 |
flag = lre_is_cased(c); |
| 1930 |
if (flag != flag_ref) { |
| 1931 |
printf("ERROR: c=%05x cased=%d ref=%d\n", |
| 1932 |
c, flag, flag_ref); |
| 1933 |
exit(1); |
| 1934 |
} |
| 1935 |
|
| 1936 |
flag_ref = get_prop(c, PROP_Case_Ignorable); |
| 1937 |
flag = lre_is_case_ignorable(c); |
| 1938 |
if (flag != flag_ref) { |
| 1939 |
printf("ERROR: c=%05x case_ignorable=%d ref=%d\n", |
| 1940 |
c, flag, flag_ref); |
| 1941 |
exit(1); |
| 1942 |
} |
| 1943 |
|
| 1944 |
flag_ref = get_prop(c, PROP_ID_Start); |
| 1945 |
flag = lre_is_id_start(c); |
| 1946 |
if (flag != flag_ref) { |
| 1947 |
printf("ERROR: c=%05x id_start=%d ref=%d\n", |
| 1948 |
c, flag, flag_ref); |
| 1949 |
exit(1); |
| 1950 |
} |
| 1951 |
|
| 1952 |
flag_ref = get_prop(c, PROP_ID_Continue); |
| 1953 |
flag = lre_is_id_continue(c); |
| 1954 |
if (flag != flag_ref) { |
| 1955 |
printf("ERROR: c=%05x id_cont=%d ref=%d\n", |
| 1956 |
c, flag, flag_ref); |
| 1957 |
exit(1); |
| 1958 |
} |
| 1959 |
} |
| 1960 |
#ifdef PROFILE |
| 1961 |
{ |
| 1962 |
int64_t ti, count; |
| 1963 |
ti = get_time_ns(); |
| 1964 |
count = 0; |
| 1965 |
for(c = 0x20; c <= 0xffff; c++) { |
| 1966 |
flag_ref = get_prop(c, PROP_ID_Start); |
| 1967 |
flag = lre_is_id_start(c); |
| 1968 |
assert(flag == flag_ref); |
| 1969 |
count++; |
| 1970 |
} |
| 1971 |
ti = get_time_ns() - ti; |
| 1972 |
printf("flags time=%0.1f ns/char\n", |
| 1973 |
(double)ti / count); |
| 1974 |
} |
| 1975 |
#endif |
| 1976 |
} |
| 1977 |
|
| 1978 |
#endif |
| 1979 |
|
| 1980 |
#define CC_BLOCK_LEN 32 |
| 1981 |
|
| 1982 |
void build_cc_table(FILE *f) |
| 1983 |
{ |
| 1984 |
int i, cc, n, cc_table_len, type, n1; |
| 1985 |
DynBuf dbuf_s, *dbuf = &dbuf_s; |
| 1986 |
DynBuf dbuf1_s, *dbuf1 = &dbuf1_s; |
| 1987 |
int cw_len_tab[3], cw_start, block_end_pos; |
| 1988 |
uint32_t v; |
| 1989 |
|
| 1990 |
dbuf_init(dbuf); |
| 1991 |
dbuf_init(dbuf1); |
| 1992 |
cc_table_len = 0; |
| 1993 |
for(i = 0; i < countof(cw_len_tab); i++) |
| 1994 |
cw_len_tab[i] = 0; |
| 1995 |
block_end_pos = CC_BLOCK_LEN; |
| 1996 |
for(i = 0; i <= CHARCODE_MAX;) { |
| 1997 |
cc = unicode_db[i].combining_class; |
| 1998 |
assert(cc <= 255); |
| 1999 |
|
| 2000 |
n = 1; |
| 2001 |
while ((i + n) <= CHARCODE_MAX && |
| 2002 |
unicode_db[i + n].combining_class == (cc + n)) |
| 2003 |
n++; |
| 2004 |
if (n >= 2) { |
| 2005 |
type = 1; |
| 2006 |
} else { |
| 2007 |
type = 0; |
| 2008 |
n = 1; |
| 2009 |
while ((i + n) <= CHARCODE_MAX && |
| 2010 |
unicode_db[i + n].combining_class == cc) |
| 2011 |
n++; |
| 2012 |
} |
| 2013 |
|
| 2014 |
if (cc == 0 && (i + n - 1) == CHARCODE_MAX) |
| 2015 |
break; |
| 2016 |
#ifdef DUMP_CC_TABLE |
| 2017 |
printf("%05x %6d %d %d\n", i, n, type, cc); |
| 2018 |
#endif |
| 2019 |
if (type == 0) { |
| 2020 |
if (cc == 0) |
| 2021 |
type = 2; |
| 2022 |
else if (cc == 230) |
| 2023 |
type = 3; |
| 2024 |
} |
| 2025 |
n1 = n - 1; |
| 2026 |
|
| 2027 |
|
| 2028 |
if (dbuf->size >= block_end_pos) { |
| 2029 |
v = i | ((dbuf->size - block_end_pos) << 21); |
| 2030 |
dbuf_putc(dbuf1, v); |
| 2031 |
dbuf_putc(dbuf1, v >> 8); |
| 2032 |
dbuf_putc(dbuf1, v >> 16); |
| 2033 |
block_end_pos += CC_BLOCK_LEN; |
| 2034 |
} |
| 2035 |
cw_start = dbuf->size; |
| 2036 |
if (n1 < 48) { |
| 2037 |
dbuf_putc(dbuf, n1 | (type << 6)); |
| 2038 |
} else if (n1 < 48 + (1 << 11)) { |
| 2039 |
n1 -= 48; |
| 2040 |
dbuf_putc(dbuf, ((n1 >> 8) + 48) | (type << 6)); |
| 2041 |
dbuf_putc(dbuf, n1); |
| 2042 |
} else { |
| 2043 |
n1 -= 48 + (1 << 11); |
| 2044 |
assert(n1 < (1 << 20)); |
| 2045 |
dbuf_putc(dbuf, ((n1 >> 16) + 56) | (type << 6)); |
| 2046 |
dbuf_putc(dbuf, n1 >> 8); |
| 2047 |
dbuf_putc(dbuf, n1); |
| 2048 |
} |
| 2049 |
cw_len_tab[dbuf->size - cw_start - 1]++; |
| 2050 |
if (type == 0 || type == 1) |
| 2051 |
dbuf_putc(dbuf, cc); |
| 2052 |
cc_table_len++; |
| 2053 |
i += n; |
| 2054 |
} |
| 2055 |
|
| 2056 |
|
| 2057 |
v = i; |
| 2058 |
dbuf_putc(dbuf1, v); |
| 2059 |
dbuf_putc(dbuf1, v >> 8); |
| 2060 |
dbuf_putc(dbuf1, v >> 16); |
| 2061 |
|
| 2062 |
dump_byte_table(f, "unicode_cc_table", dbuf->buf, dbuf->size); |
| 2063 |
dump_byte_table(f, "unicode_cc_index", dbuf1->buf, dbuf1->size); |
| 2064 |
|
| 2065 |
#if defined(DUMP_CC_TABLE) || defined(DUMP_TABLE_SIZE) |
| 2066 |
printf("CC table: size=%d (%d entries) [", |
| 2067 |
(int)(dbuf->size + dbuf1->size), |
| 2068 |
cc_table_len); |
| 2069 |
for(i = 0; i < countof(cw_len_tab); i++) |
| 2070 |
printf(" %d", cw_len_tab[i]); |
| 2071 |
printf(" ]\n"); |
| 2072 |
#endif |
| 2073 |
dbuf_free(dbuf); |
| 2074 |
dbuf_free(dbuf1); |
| 2075 |
} |
| 2076 |
|
| 2077 |
|
| 2078 |
#ifndef USE_TEST |
| 2079 |
typedef enum { |
| 2080 |
DECOMP_TYPE_C1, |
| 2081 |
DECOMP_TYPE_L1, |
| 2082 |
DECOMP_TYPE_L2, |
| 2083 |
DECOMP_TYPE_L3, |
| 2084 |
DECOMP_TYPE_L4, |
| 2085 |
DECOMP_TYPE_L5, |
| 2086 |
DECOMP_TYPE_L6, |
| 2087 |
DECOMP_TYPE_L7, |
| 2088 |
DECOMP_TYPE_LL1, |
| 2089 |
DECOMP_TYPE_LL2, |
| 2090 |
DECOMP_TYPE_S1, |
| 2091 |
DECOMP_TYPE_S2, |
| 2092 |
DECOMP_TYPE_S3, |
| 2093 |
DECOMP_TYPE_S4, |
| 2094 |
DECOMP_TYPE_S5, |
| 2095 |
DECOMP_TYPE_I1, |
| 2096 |
DECOMP_TYPE_I2_0, |
| 2097 |
DECOMP_TYPE_I2_1, |
| 2098 |
DECOMP_TYPE_I3_1, |
| 2099 |
DECOMP_TYPE_I3_2, |
| 2100 |
DECOMP_TYPE_I4_1, |
| 2101 |
DECOMP_TYPE_I4_2, |
| 2102 |
DECOMP_TYPE_B1, |
| 2103 |
DECOMP_TYPE_B2, |
| 2104 |
DECOMP_TYPE_B3, |
| 2105 |
DECOMP_TYPE_B4, |
| 2106 |
DECOMP_TYPE_B5, |
| 2107 |
DECOMP_TYPE_B6, |
| 2108 |
DECOMP_TYPE_B7, |
| 2109 |
DECOMP_TYPE_B8, |
| 2110 |
DECOMP_TYPE_B18, |
| 2111 |
DECOMP_TYPE_LS2, |
| 2112 |
DECOMP_TYPE_PAT3, |
| 2113 |
DECOMP_TYPE_S2_UL, |
| 2114 |
DECOMP_TYPE_LS2_UL, |
| 2115 |
} DecompTypeEnum; |
| 2116 |
#endif |
| 2117 |
|
| 2118 |
const char *decomp_type_str[] = { |
| 2119 |
"C1", |
| 2120 |
"L1", |
| 2121 |
"L2", |
| 2122 |
"L3", |
| 2123 |
"L4", |
| 2124 |
"L5", |
| 2125 |
"L6", |
| 2126 |
"L7", |
| 2127 |
"LL1", |
| 2128 |
"LL2", |
| 2129 |
"S1", |
| 2130 |
"S2", |
| 2131 |
"S3", |
| 2132 |
"S4", |
| 2133 |
"S5", |
| 2134 |
"I1", |
| 2135 |
"I2_0", |
| 2136 |
"I2_1", |
| 2137 |
"I3_1", |
| 2138 |
"I3_2", |
| 2139 |
"I4_1", |
| 2140 |
"I4_2", |
| 2141 |
"B1", |
| 2142 |
"B2", |
| 2143 |
"B3", |
| 2144 |
"B4", |
| 2145 |
"B5", |
| 2146 |
"B6", |
| 2147 |
"B7", |
| 2148 |
"B8", |
| 2149 |
"B18", |
| 2150 |
"LS2", |
| 2151 |
"PAT3", |
| 2152 |
"S2_UL", |
| 2153 |
"LS2_UL", |
| 2154 |
}; |
| 2155 |
|
| 2156 |
const int decomp_incr_tab[4][4] = { |
| 2157 |
{ DECOMP_TYPE_I1, 0, -1 }, |
| 2158 |
{ DECOMP_TYPE_I2_0, 0, 1, -1 }, |
| 2159 |
{ DECOMP_TYPE_I3_1, 1, 2, -1 }, |
| 2160 |
{ DECOMP_TYPE_I4_1, 1, 2, -1 }, |
| 2161 |
}; |
| 2162 |
|
| 2163 |
|
| 2164 |
|
| 2165 |
|
| 2166 |
|
| 2167 |
|
| 2168 |
|
| 2169 |
|
| 2170 |
|
| 2171 |
|
| 2172 |
|
| 2173 |
|
| 2174 |
typedef struct { |
| 2175 |
int code; |
| 2176 |
uint8_t len; |
| 2177 |
uint8_t type; |
| 2178 |
uint8_t c_len; |
| 2179 |
uint16_t c_min; |
| 2180 |
uint16_t data_index; |
| 2181 |
int cost; |
| 2182 |
} DecompEntry; |
| 2183 |
|
| 2184 |
int get_decomp_run_size(const DecompEntry *de) |
| 2185 |
{ |
| 2186 |
int s; |
| 2187 |
s = 6; |
| 2188 |
if (de->type <= DECOMP_TYPE_C1) { |
| 2189 |
|
| 2190 |
} else if (de->type <= DECOMP_TYPE_L7) { |
| 2191 |
s += de->len * de->c_len * 2; |
| 2192 |
} else if (de->type <= DECOMP_TYPE_LL2) { |
| 2193 |
|
| 2194 |
s += (de->len * de->c_len * 18 + 7) / 8; |
| 2195 |
} else if (de->type <= DECOMP_TYPE_S5) { |
| 2196 |
s += de->len * de->c_len; |
| 2197 |
} else if (de->type <= DECOMP_TYPE_I4_2) { |
| 2198 |
s += de->c_len * 2; |
| 2199 |
} else if (de->type <= DECOMP_TYPE_B18) { |
| 2200 |
s += 2 + de->len * de->c_len; |
| 2201 |
} else if (de->type <= DECOMP_TYPE_LS2) { |
| 2202 |
s += de->len * 3; |
| 2203 |
} else if (de->type <= DECOMP_TYPE_PAT3) { |
| 2204 |
s += 4 + de->len * 2; |
| 2205 |
} else if (de->type <= DECOMP_TYPE_S2_UL) { |
| 2206 |
s += de->len; |
| 2207 |
} else if (de->type <= DECOMP_TYPE_LS2_UL) { |
| 2208 |
s += (de->len / 2) * 3; |
| 2209 |
} else { |
| 2210 |
abort(); |
| 2211 |
} |
| 2212 |
return s; |
| 2213 |
} |
| 2214 |
|
| 2215 |
static const uint16_t unicode_short_table[2] = { 0x2044, 0x2215 }; |
| 2216 |
|
| 2217 |
|
| 2218 |
int get_short_code(int c) |
| 2219 |
{ |
| 2220 |
int i; |
| 2221 |
if (c < 0x80) { |
| 2222 |
return c; |
| 2223 |
} else if (c >= 0x300 && c < 0x350) { |
| 2224 |
return c - 0x300 + 0x80; |
| 2225 |
} else { |
| 2226 |
for(i = 0; i < countof(unicode_short_table); i++) { |
| 2227 |
if (c == unicode_short_table[i]) |
| 2228 |
return i + 0x80 + 0x50; |
| 2229 |
} |
| 2230 |
return -1; |
| 2231 |
} |
| 2232 |
} |
| 2233 |
|
| 2234 |
static BOOL is_short(int code) |
| 2235 |
{ |
| 2236 |
return get_short_code(code) >= 0; |
| 2237 |
} |
| 2238 |
|
| 2239 |
static BOOL is_short_tab(const int *tab, int len) |
| 2240 |
{ |
| 2241 |
int i; |
| 2242 |
for(i = 0; i < len; i++) { |
| 2243 |
if (!is_short(tab[i])) |
| 2244 |
return FALSE; |
| 2245 |
} |
| 2246 |
return TRUE; |
| 2247 |
} |
| 2248 |
|
| 2249 |
static BOOL is_16bit(const int *tab, int len) |
| 2250 |
{ |
| 2251 |
int i; |
| 2252 |
for(i = 0; i < len; i++) { |
| 2253 |
if (tab[i] > 0xffff) |
| 2254 |
return FALSE; |
| 2255 |
} |
| 2256 |
return TRUE; |
| 2257 |
} |
| 2258 |
|
| 2259 |
static uint32_t to_lower_simple(uint32_t c) |
| 2260 |
{ |
| 2261 |
|
| 2262 |
if (c < 0x100 || (c >= 0x410 && c <= 0x42f)) |
| 2263 |
c += 0x20; |
| 2264 |
else |
| 2265 |
c++; |
| 2266 |
return c; |
| 2267 |
} |
| 2268 |
|
| 2269 |
|
| 2270 |
void find_decomp_run(DecompEntry *tab_de, int i) |
| 2271 |
{ |
| 2272 |
DecompEntry de_s, *de = &de_s; |
| 2273 |
CCInfo *ci, *ci1, *ci2; |
| 2274 |
int l, j, n, len_max; |
| 2275 |
|
| 2276 |
ci = &unicode_db[i]; |
| 2277 |
l = ci->decomp_len; |
| 2278 |
if (l == 0) { |
| 2279 |
tab_de[i].cost = tab_de[i + 1].cost; |
| 2280 |
return; |
| 2281 |
} |
| 2282 |
|
| 2283 |
|
| 2284 |
|
| 2285 |
if (!ci->is_compat && !ci->is_excluded && l == 2) |
| 2286 |
len_max = 64; |
| 2287 |
else |
| 2288 |
len_max = 127; |
| 2289 |
|
| 2290 |
tab_de[i].cost = 0x7fffffff; |
| 2291 |
|
| 2292 |
if (!is_16bit(ci->decomp_data, l)) { |
| 2293 |
assert(l <= 2); |
| 2294 |
|
| 2295 |
n = 1; |
| 2296 |
for(;;) { |
| 2297 |
de->code = i; |
| 2298 |
de->len = n; |
| 2299 |
de->type = DECOMP_TYPE_LL1 + l - 1; |
| 2300 |
de->c_len = l; |
| 2301 |
de->cost = get_decomp_run_size(de) + tab_de[i + n].cost; |
| 2302 |
if (de->cost < tab_de[i].cost) { |
| 2303 |
tab_de[i] = *de; |
| 2304 |
} |
| 2305 |
if (!((i + n) <= CHARCODE_MAX && n < len_max)) |
| 2306 |
break; |
| 2307 |
ci1 = &unicode_db[i + n]; |
| 2308 |
|
| 2309 |
if (!(ci1->decomp_len == 0 || |
| 2310 |
(ci1->decomp_len == l && |
| 2311 |
ci1->is_compat == ci->is_compat))) |
| 2312 |
break; |
| 2313 |
n++; |
| 2314 |
} |
| 2315 |
return; |
| 2316 |
} |
| 2317 |
|
| 2318 |
if (l <= 7) { |
| 2319 |
n = 1; |
| 2320 |
for(;;) { |
| 2321 |
de->code = i; |
| 2322 |
de->len = n; |
| 2323 |
if (l == 1 && n == 1) { |
| 2324 |
de->type = DECOMP_TYPE_C1; |
| 2325 |
} else { |
| 2326 |
assert(l <= 8); |
| 2327 |
de->type = DECOMP_TYPE_L1 + l - 1; |
| 2328 |
} |
| 2329 |
de->c_len = l; |
| 2330 |
de->cost = get_decomp_run_size(de) + tab_de[i + n].cost; |
| 2331 |
if (de->cost < tab_de[i].cost) { |
| 2332 |
tab_de[i] = *de; |
| 2333 |
} |
| 2334 |
|
| 2335 |
if (!((i + n) <= CHARCODE_MAX && n < len_max)) |
| 2336 |
break; |
| 2337 |
ci1 = &unicode_db[i + n]; |
| 2338 |
|
| 2339 |
if (!(ci1->decomp_len == 0 || |
| 2340 |
(ci1->decomp_len == l && |
| 2341 |
ci1->is_compat == ci->is_compat && |
| 2342 |
is_16bit(ci1->decomp_data, l)))) |
| 2343 |
break; |
| 2344 |
n++; |
| 2345 |
} |
| 2346 |
} |
| 2347 |
|
| 2348 |
if (l <= 8 || l == 18) { |
| 2349 |
int c_min, c_max, c; |
| 2350 |
c_min = c_max = -1; |
| 2351 |
n = 1; |
| 2352 |
for(;;) { |
| 2353 |
ci1 = &unicode_db[i + n - 1]; |
| 2354 |
for(j = 0; j < l; j++) { |
| 2355 |
c = ci1->decomp_data[j]; |
| 2356 |
if (c == 0x20) { |
| 2357 |
|
| 2358 |
} else if (c_min == -1) { |
| 2359 |
c_min = c_max = c; |
| 2360 |
} else { |
| 2361 |
c_min = min_int(c_min, c); |
| 2362 |
c_max = max_int(c_max, c); |
| 2363 |
} |
| 2364 |
} |
| 2365 |
if ((c_max - c_min) > 254) |
| 2366 |
break; |
| 2367 |
de->code = i; |
| 2368 |
de->len = n; |
| 2369 |
if (l == 18) |
| 2370 |
de->type = DECOMP_TYPE_B18; |
| 2371 |
else |
| 2372 |
de->type = DECOMP_TYPE_B1 + l - 1; |
| 2373 |
de->c_len = l; |
| 2374 |
de->c_min = c_min; |
| 2375 |
de->cost = get_decomp_run_size(de) + tab_de[i + n].cost; |
| 2376 |
if (de->cost < tab_de[i].cost) { |
| 2377 |
tab_de[i] = *de; |
| 2378 |
} |
| 2379 |
if (!((i + n) <= CHARCODE_MAX && n < len_max)) |
| 2380 |
break; |
| 2381 |
ci1 = &unicode_db[i + n]; |
| 2382 |
if (!(ci1->decomp_len == l && |
| 2383 |
ci1->is_compat == ci->is_compat)) |
| 2384 |
break; |
| 2385 |
n++; |
| 2386 |
} |
| 2387 |
} |
| 2388 |
|
| 2389 |
|
| 2390 |
if (l <= 5 && is_short_tab(ci->decomp_data, l)) { |
| 2391 |
n = 1; |
| 2392 |
for(;;) { |
| 2393 |
de->code = i; |
| 2394 |
de->len = n; |
| 2395 |
de->type = DECOMP_TYPE_S1 + l - 1; |
| 2396 |
de->c_len = l; |
| 2397 |
de->cost = get_decomp_run_size(de) + tab_de[i + n].cost; |
| 2398 |
if (de->cost < tab_de[i].cost) { |
| 2399 |
tab_de[i] = *de; |
| 2400 |
} |
| 2401 |
|
| 2402 |
if (!((i + n) <= CHARCODE_MAX && n < len_max)) |
| 2403 |
break; |
| 2404 |
ci1 = &unicode_db[i + n]; |
| 2405 |
|
| 2406 |
if (!(ci1->decomp_len == 0 || |
| 2407 |
(ci1->decomp_len == l && |
| 2408 |
ci1->is_compat == ci->is_compat && |
| 2409 |
is_short_tab(ci1->decomp_data, l)))) |
| 2410 |
break; |
| 2411 |
n++; |
| 2412 |
} |
| 2413 |
} |
| 2414 |
|
| 2415 |
|
| 2416 |
if (l <= 4) { |
| 2417 |
int idx1, idx; |
| 2418 |
|
| 2419 |
for(idx1 = 1; (idx = decomp_incr_tab[l - 1][idx1]) >= 0; idx1++) { |
| 2420 |
n = 1; |
| 2421 |
for(;;) { |
| 2422 |
de->code = i; |
| 2423 |
de->len = n; |
| 2424 |
de->type = decomp_incr_tab[l - 1][0] + idx1 - 1; |
| 2425 |
de->c_len = l; |
| 2426 |
de->cost = get_decomp_run_size(de) + tab_de[i + n].cost; |
| 2427 |
if (de->cost < tab_de[i].cost) { |
| 2428 |
tab_de[i] = *de; |
| 2429 |
} |
| 2430 |
|
| 2431 |
if (!((i + n) <= CHARCODE_MAX && n < len_max)) |
| 2432 |
break; |
| 2433 |
ci1 = &unicode_db[i + n]; |
| 2434 |
if (!(ci1->decomp_len == l && |
| 2435 |
ci1->is_compat == ci->is_compat)) |
| 2436 |
goto next1; |
| 2437 |
for(j = 0; j < l; j++) { |
| 2438 |
if (j == idx) { |
| 2439 |
if (ci1->decomp_data[j] != ci->decomp_data[j] + n) |
| 2440 |
goto next1; |
| 2441 |
} else { |
| 2442 |
if (ci1->decomp_data[j] != ci->decomp_data[j]) |
| 2443 |
goto next1; |
| 2444 |
} |
| 2445 |
} |
| 2446 |
n++; |
| 2447 |
} |
| 2448 |
next1: ; |
| 2449 |
} |
| 2450 |
} |
| 2451 |
|
| 2452 |
if (l == 3) { |
| 2453 |
n = 1; |
| 2454 |
for(;;) { |
| 2455 |
de->code = i; |
| 2456 |
de->len = n; |
| 2457 |
de->type = DECOMP_TYPE_PAT3; |
| 2458 |
de->c_len = l; |
| 2459 |
de->cost = get_decomp_run_size(de) + tab_de[i + n].cost; |
| 2460 |
if (de->cost < tab_de[i].cost) { |
| 2461 |
tab_de[i] = *de; |
| 2462 |
} |
| 2463 |
if (!((i + n) <= CHARCODE_MAX && n < len_max)) |
| 2464 |
break; |
| 2465 |
ci1 = &unicode_db[i + n]; |
| 2466 |
if (!(ci1->decomp_len == l && |
| 2467 |
ci1->is_compat == ci->is_compat && |
| 2468 |
ci1->decomp_data[1] <= 0xffff && |
| 2469 |
ci1->decomp_data[0] == ci->decomp_data[0] && |
| 2470 |
ci1->decomp_data[l - 1] == ci->decomp_data[l - 1])) |
| 2471 |
break; |
| 2472 |
n++; |
| 2473 |
} |
| 2474 |
} |
| 2475 |
|
| 2476 |
if (l == 2 && is_short(ci->decomp_data[1])) { |
| 2477 |
n = 1; |
| 2478 |
for(;;) { |
| 2479 |
de->code = i; |
| 2480 |
de->len = n; |
| 2481 |
de->type = DECOMP_TYPE_LS2; |
| 2482 |
de->c_len = l; |
| 2483 |
de->cost = get_decomp_run_size(de) + tab_de[i + n].cost; |
| 2484 |
if (de->cost < tab_de[i].cost) { |
| 2485 |
tab_de[i] = *de; |
| 2486 |
} |
| 2487 |
if (!((i + n) <= CHARCODE_MAX && n < len_max)) |
| 2488 |
break; |
| 2489 |
ci1 = &unicode_db[i + n]; |
| 2490 |
if (!(ci1->decomp_len == 0 || |
| 2491 |
(ci1->decomp_len == l && |
| 2492 |
ci1->is_compat == ci->is_compat && |
| 2493 |
ci1->decomp_data[0] <= 0xffff && |
| 2494 |
is_short(ci1->decomp_data[1])))) |
| 2495 |
break; |
| 2496 |
n++; |
| 2497 |
} |
| 2498 |
} |
| 2499 |
|
| 2500 |
if (l == 2) { |
| 2501 |
BOOL is_16bit; |
| 2502 |
|
| 2503 |
n = 0; |
| 2504 |
is_16bit = FALSE; |
| 2505 |
for(;;) { |
| 2506 |
if (!((i + n + 1) <= CHARCODE_MAX && n + 2 <= len_max)) |
| 2507 |
break; |
| 2508 |
ci1 = &unicode_db[i + n]; |
| 2509 |
if (!(ci1->decomp_len == l && |
| 2510 |
ci1->is_compat == ci->is_compat && |
| 2511 |
is_short(ci1->decomp_data[1]))) |
| 2512 |
break; |
| 2513 |
if (!is_16bit && !is_short(ci1->decomp_data[0])) |
| 2514 |
is_16bit = TRUE; |
| 2515 |
ci2 = &unicode_db[i + n + 1]; |
| 2516 |
if (!(ci2->decomp_len == l && |
| 2517 |
ci2->is_compat == ci->is_compat && |
| 2518 |
ci2->decomp_data[0] == to_lower_simple(ci1->decomp_data[0]) && |
| 2519 |
ci2->decomp_data[1] == ci1->decomp_data[1])) |
| 2520 |
break; |
| 2521 |
n += 2; |
| 2522 |
de->code = i; |
| 2523 |
de->len = n; |
| 2524 |
de->type = DECOMP_TYPE_S2_UL + is_16bit; |
| 2525 |
de->c_len = l; |
| 2526 |
de->cost = get_decomp_run_size(de) + tab_de[i + n].cost; |
| 2527 |
if (de->cost < tab_de[i].cost) { |
| 2528 |
tab_de[i] = *de; |
| 2529 |
} |
| 2530 |
} |
| 2531 |
} |
| 2532 |
} |
| 2533 |
|
| 2534 |
void put16(uint8_t *data_buf, int *pidx, uint16_t c) |
| 2535 |
{ |
| 2536 |
int idx; |
| 2537 |
idx = *pidx; |
| 2538 |
data_buf[idx++] = c; |
| 2539 |
data_buf[idx++] = c >> 8; |
| 2540 |
*pidx = idx; |
| 2541 |
} |
| 2542 |
|
| 2543 |
void add_decomp_data(uint8_t *data_buf, int *pidx, DecompEntry *de) |
| 2544 |
{ |
| 2545 |
int i, j, idx, c; |
| 2546 |
CCInfo *ci; |
| 2547 |
|
| 2548 |
idx = *pidx; |
| 2549 |
de->data_index = idx; |
| 2550 |
if (de->type <= DECOMP_TYPE_C1) { |
| 2551 |
ci = &unicode_db[de->code]; |
| 2552 |
assert(ci->decomp_len == 1); |
| 2553 |
de->data_index = ci->decomp_data[0]; |
| 2554 |
} else if (de->type <= DECOMP_TYPE_L7) { |
| 2555 |
for(i = 0; i < de->len; i++) { |
| 2556 |
ci = &unicode_db[de->code + i]; |
| 2557 |
for(j = 0; j < de->c_len; j++) { |
| 2558 |
if (ci->decomp_len == 0) |
| 2559 |
c = 0; |
| 2560 |
else |
| 2561 |
c = ci->decomp_data[j]; |
| 2562 |
put16(data_buf, &idx, c); |
| 2563 |
} |
| 2564 |
} |
| 2565 |
} else if (de->type <= DECOMP_TYPE_LL2) { |
| 2566 |
int n, p, k; |
| 2567 |
n = (de->len * de->c_len * 18 + 7) / 8; |
| 2568 |
p = de->len * de->c_len * 2; |
| 2569 |
memset(data_buf + idx, 0, n); |
| 2570 |
k = 0; |
| 2571 |
for(i = 0; i < de->len; i++) { |
| 2572 |
ci = &unicode_db[de->code + i]; |
| 2573 |
for(j = 0; j < de->c_len; j++) { |
| 2574 |
if (ci->decomp_len == 0) |
| 2575 |
c = 0; |
| 2576 |
else |
| 2577 |
c = ci->decomp_data[j]; |
| 2578 |
data_buf[idx + k * 2] = c; |
| 2579 |
data_buf[idx + k * 2 + 1] = c >> 8; |
| 2580 |
data_buf[idx + p + (k / 4)] |= (c >> 16) << ((k % 4) * 2); |
| 2581 |
k++; |
| 2582 |
} |
| 2583 |
} |
| 2584 |
idx += n; |
| 2585 |
} else if (de->type <= DECOMP_TYPE_S5) { |
| 2586 |
for(i = 0; i < de->len; i++) { |
| 2587 |
ci = &unicode_db[de->code + i]; |
| 2588 |
for(j = 0; j < de->c_len; j++) { |
| 2589 |
if (ci->decomp_len == 0) |
| 2590 |
c = 0; |
| 2591 |
else |
| 2592 |
c = ci->decomp_data[j]; |
| 2593 |
c = get_short_code(c); |
| 2594 |
assert(c >= 0); |
| 2595 |
data_buf[idx++] = c; |
| 2596 |
} |
| 2597 |
} |
| 2598 |
} else if (de->type <= DECOMP_TYPE_I4_2) { |
| 2599 |
ci = &unicode_db[de->code]; |
| 2600 |
assert(ci->decomp_len == de->c_len); |
| 2601 |
for(j = 0; j < de->c_len; j++) |
| 2602 |
put16(data_buf, &idx, ci->decomp_data[j]); |
| 2603 |
} else if (de->type <= DECOMP_TYPE_B18) { |
| 2604 |
c = de->c_min; |
| 2605 |
data_buf[idx++] = c; |
| 2606 |
data_buf[idx++] = c >> 8; |
| 2607 |
for(i = 0; i < de->len; i++) { |
| 2608 |
ci = &unicode_db[de->code + i]; |
| 2609 |
for(j = 0; j < de->c_len; j++) { |
| 2610 |
assert(ci->decomp_len == de->c_len); |
| 2611 |
c = ci->decomp_data[j]; |
| 2612 |
if (c == 0x20) { |
| 2613 |
c = 0xff; |
| 2614 |
} else { |
| 2615 |
c -= de->c_min; |
| 2616 |
assert((uint32_t)c <= 254); |
| 2617 |
} |
| 2618 |
data_buf[idx++] = c; |
| 2619 |
} |
| 2620 |
} |
| 2621 |
} else if (de->type <= DECOMP_TYPE_LS2) { |
| 2622 |
assert(de->c_len == 2); |
| 2623 |
for(i = 0; i < de->len; i++) { |
| 2624 |
ci = &unicode_db[de->code + i]; |
| 2625 |
if (ci->decomp_len == 0) |
| 2626 |
c = 0; |
| 2627 |
else |
| 2628 |
c = ci->decomp_data[0]; |
| 2629 |
put16(data_buf, &idx, c); |
| 2630 |
|
| 2631 |
if (ci->decomp_len == 0) |
| 2632 |
c = 0; |
| 2633 |
else |
| 2634 |
c = ci->decomp_data[1]; |
| 2635 |
c = get_short_code(c); |
| 2636 |
assert(c >= 0); |
| 2637 |
data_buf[idx++] = c; |
| 2638 |
} |
| 2639 |
} else if (de->type <= DECOMP_TYPE_PAT3) { |
| 2640 |
ci = &unicode_db[de->code]; |
| 2641 |
assert(ci->decomp_len == 3); |
| 2642 |
put16(data_buf, &idx, ci->decomp_data[0]); |
| 2643 |
put16(data_buf, &idx, ci->decomp_data[2]); |
| 2644 |
for(i = 0; i < de->len; i++) { |
| 2645 |
ci = &unicode_db[de->code + i]; |
| 2646 |
assert(ci->decomp_len == 3); |
| 2647 |
put16(data_buf, &idx, ci->decomp_data[1]); |
| 2648 |
} |
| 2649 |
} else if (de->type <= DECOMP_TYPE_S2_UL) { |
| 2650 |
for(i = 0; i < de->len; i += 2) { |
| 2651 |
ci = &unicode_db[de->code + i]; |
| 2652 |
c = ci->decomp_data[0]; |
| 2653 |
c = get_short_code(c); |
| 2654 |
assert(c >= 0); |
| 2655 |
data_buf[idx++] = c; |
| 2656 |
c = ci->decomp_data[1]; |
| 2657 |
c = get_short_code(c); |
| 2658 |
assert(c >= 0); |
| 2659 |
data_buf[idx++] = c; |
| 2660 |
} |
| 2661 |
} else if (de->type <= DECOMP_TYPE_LS2_UL) { |
| 2662 |
for(i = 0; i < de->len; i += 2) { |
| 2663 |
ci = &unicode_db[de->code + i]; |
| 2664 |
c = ci->decomp_data[0]; |
| 2665 |
put16(data_buf, &idx, c); |
| 2666 |
c = ci->decomp_data[1]; |
| 2667 |
c = get_short_code(c); |
| 2668 |
assert(c >= 0); |
| 2669 |
data_buf[idx++] = c; |
| 2670 |
} |
| 2671 |
} else { |
| 2672 |
abort(); |
| 2673 |
} |
| 2674 |
*pidx = idx; |
| 2675 |
} |
| 2676 |
|
| 2677 |
#if 0 |
| 2678 |
void dump_large_char(void) |
| 2679 |
{ |
| 2680 |
int i, j; |
| 2681 |
for(i = 0; i <= CHARCODE_MAX; i++) { |
| 2682 |
CCInfo *ci = &unicode_db[i]; |
| 2683 |
for(j = 0; j < ci->decomp_len; j++) { |
| 2684 |
if (ci->decomp_data[j] > 0xffff) |
| 2685 |
printf("%05x\n", ci->decomp_data[j]); |
| 2686 |
} |
| 2687 |
} |
| 2688 |
} |
| 2689 |
#endif |
| 2690 |
|
| 2691 |
void build_compose_table(FILE *f, const DecompEntry *tab_de); |
| 2692 |
|
| 2693 |
void build_decompose_table(FILE *f) |
| 2694 |
{ |
| 2695 |
int i, array_len, code_max, data_len, count; |
| 2696 |
DecompEntry *tab_de, de_s, *de = &de_s; |
| 2697 |
uint8_t *data_buf; |
| 2698 |
|
| 2699 |
code_max = CHARCODE_MAX; |
| 2700 |
|
| 2701 |
tab_de = mallocz((code_max + 2) * sizeof(*tab_de)); |
| 2702 |
|
| 2703 |
for(i = code_max; i >= 0; i--) { |
| 2704 |
find_decomp_run(tab_de, i); |
| 2705 |
} |
| 2706 |
|
| 2707 |
|
| 2708 |
data_buf = malloc(100000); |
| 2709 |
data_len = 0; |
| 2710 |
array_len = 0; |
| 2711 |
for(i = 0; i <= code_max; i++) { |
| 2712 |
de = &tab_de[i]; |
| 2713 |
if (de->len != 0) { |
| 2714 |
add_decomp_data(data_buf, &data_len, de); |
| 2715 |
i += de->len - 1; |
| 2716 |
array_len++; |
| 2717 |
} |
| 2718 |
} |
| 2719 |
|
| 2720 |
#ifdef DUMP_DECOMP_TABLE |
| 2721 |
|
| 2722 |
{ |
| 2723 |
int size, size1; |
| 2724 |
|
| 2725 |
printf("START LEN TYPE L C SIZE\n"); |
| 2726 |
size = 0; |
| 2727 |
for(i = 0; i <= code_max; i++) { |
| 2728 |
de = &tab_de[i]; |
| 2729 |
if (de->len != 0) { |
| 2730 |
size1 = get_decomp_run_size(de); |
| 2731 |
printf("%05x %3d %6s %2d %1d %4d\n", i, de->len, |
| 2732 |
decomp_type_str[de->type], de->c_len, |
| 2733 |
unicode_db[i].is_compat, size1); |
| 2734 |
i += de->len - 1; |
| 2735 |
size += size1; |
| 2736 |
} |
| 2737 |
} |
| 2738 |
|
| 2739 |
printf("array_len=%d estimated size=%d bytes actual=%d bytes\n", |
| 2740 |
array_len, size, array_len * 6 + data_len); |
| 2741 |
} |
| 2742 |
#endif |
| 2743 |
|
| 2744 |
fprintf(f, "static const uint32_t unicode_decomp_table1[%u] = {", |
| 2745 |
array_len); |
| 2746 |
count = 0; |
| 2747 |
for(i = 0; i <= code_max; i++) { |
| 2748 |
de = &tab_de[i]; |
| 2749 |
if (de->len != 0) { |
| 2750 |
uint32_t v; |
| 2751 |
if (count++ % 4 == 0) |
| 2752 |
fprintf(f, "\n "); |
| 2753 |
v = (de->code << (32 - 18)) | |
| 2754 |
(de->len << (32 - 18 - 7)) | |
| 2755 |
(de->type << (32 - 18 - 7 - 6)) | |
| 2756 |
unicode_db[de->code].is_compat; |
| 2757 |
fprintf(f, " 0x%08x,", v); |
| 2758 |
i += de->len - 1; |
| 2759 |
} |
| 2760 |
} |
| 2761 |
fprintf(f, "\n};\n\n"); |
| 2762 |
|
| 2763 |
fprintf(f, "static const uint16_t unicode_decomp_table2[%u] = {", |
| 2764 |
array_len); |
| 2765 |
count = 0; |
| 2766 |
for(i = 0; i <= code_max; i++) { |
| 2767 |
de = &tab_de[i]; |
| 2768 |
if (de->len != 0) { |
| 2769 |
if (count++ % 8 == 0) |
| 2770 |
fprintf(f, "\n "); |
| 2771 |
fprintf(f, " 0x%04x,", de->data_index); |
| 2772 |
i += de->len - 1; |
| 2773 |
} |
| 2774 |
} |
| 2775 |
fprintf(f, "\n};\n\n"); |
| 2776 |
|
| 2777 |
fprintf(f, "static const uint8_t unicode_decomp_data[%u] = {", |
| 2778 |
data_len); |
| 2779 |
for(i = 0; i < data_len; i++) { |
| 2780 |
if (i % 8 == 0) |
| 2781 |
fprintf(f, "\n "); |
| 2782 |
fprintf(f, " 0x%02x,", data_buf[i]); |
| 2783 |
} |
| 2784 |
fprintf(f, "\n};\n\n"); |
| 2785 |
|
| 2786 |
build_compose_table(f, tab_de); |
| 2787 |
|
| 2788 |
free(data_buf); |
| 2789 |
|
| 2790 |
free(tab_de); |
| 2791 |
} |
| 2792 |
|
| 2793 |
typedef struct { |
| 2794 |
uint32_t c[2]; |
| 2795 |
uint32_t p; |
| 2796 |
} ComposeEntry; |
| 2797 |
|
| 2798 |
#define COMPOSE_LEN_MAX 10000 |
| 2799 |
|
| 2800 |
static int ce_cmp(const void *p1, const void *p2) |
| 2801 |
{ |
| 2802 |
const ComposeEntry *ce1 = p1; |
| 2803 |
const ComposeEntry *ce2 = p2; |
| 2804 |
int i; |
| 2805 |
|
| 2806 |
for(i = 0; i < 2; i++) { |
| 2807 |
if (ce1->c[i] < ce2->c[i]) |
| 2808 |
return -1; |
| 2809 |
else if (ce1->c[i] > ce2->c[i]) |
| 2810 |
return 1; |
| 2811 |
} |
| 2812 |
return 0; |
| 2813 |
} |
| 2814 |
|
| 2815 |
|
| 2816 |
static int get_decomp_pos(const DecompEntry *tab_de, int c) |
| 2817 |
{ |
| 2818 |
int i, v, k; |
| 2819 |
const DecompEntry *de; |
| 2820 |
|
| 2821 |
k = 0; |
| 2822 |
for(i = 0; i <= CHARCODE_MAX; i++) { |
| 2823 |
de = &tab_de[i]; |
| 2824 |
if (de->len != 0) { |
| 2825 |
if (c >= de->code && c < de->code + de->len) { |
| 2826 |
v = c - de->code; |
| 2827 |
assert(v < 64); |
| 2828 |
v |= k << 6; |
| 2829 |
assert(v < 65536); |
| 2830 |
return v; |
| 2831 |
} |
| 2832 |
i += de->len - 1; |
| 2833 |
k++; |
| 2834 |
} |
| 2835 |
} |
| 2836 |
return -1; |
| 2837 |
} |
| 2838 |
|
| 2839 |
void build_compose_table(FILE *f, const DecompEntry *tab_de) |
| 2840 |
{ |
| 2841 |
int i, v, tab_ce_len; |
| 2842 |
ComposeEntry *ce, *tab_ce; |
| 2843 |
|
| 2844 |
tab_ce = malloc(sizeof(*tab_ce) * COMPOSE_LEN_MAX); |
| 2845 |
tab_ce_len = 0; |
| 2846 |
for(i = 0; i <= CHARCODE_MAX; i++) { |
| 2847 |
CCInfo *ci = &unicode_db[i]; |
| 2848 |
if (ci->decomp_len == 2 && !ci->is_compat && |
| 2849 |
!ci->is_excluded) { |
| 2850 |
assert(tab_ce_len < COMPOSE_LEN_MAX); |
| 2851 |
ce = &tab_ce[tab_ce_len++]; |
| 2852 |
ce->c[0] = ci->decomp_data[0]; |
| 2853 |
ce->c[1] = ci->decomp_data[1]; |
| 2854 |
ce->p = i; |
| 2855 |
} |
| 2856 |
} |
| 2857 |
qsort(tab_ce, tab_ce_len, sizeof(*tab_ce), ce_cmp); |
| 2858 |
|
| 2859 |
#if 0 |
| 2860 |
{ |
| 2861 |
printf("tab_ce_len=%d\n", tab_ce_len); |
| 2862 |
for(i = 0; i < tab_ce_len; i++) { |
| 2863 |
ce = &tab_ce[i]; |
| 2864 |
printf("%05x %05x %05x\n", ce->c[0], ce->c[1], ce->p); |
| 2865 |
} |
| 2866 |
} |
| 2867 |
#endif |
| 2868 |
|
| 2869 |
fprintf(f, "static const uint16_t unicode_comp_table[%u] = {", |
| 2870 |
tab_ce_len); |
| 2871 |
for(i = 0; i < tab_ce_len; i++) { |
| 2872 |
if (i % 8 == 0) |
| 2873 |
fprintf(f, "\n "); |
| 2874 |
v = get_decomp_pos(tab_de, tab_ce[i].p); |
| 2875 |
if (v < 0) { |
| 2876 |
printf("ERROR: entry for c=%04x not found\n", |
| 2877 |
tab_ce[i].p); |
| 2878 |
exit(1); |
| 2879 |
} |
| 2880 |
fprintf(f, " 0x%04x,", v); |
| 2881 |
} |
| 2882 |
fprintf(f, "\n};\n\n"); |
| 2883 |
|
| 2884 |
free(tab_ce); |
| 2885 |
} |
| 2886 |
|
| 2887 |
#ifdef USE_TEST |
| 2888 |
void check_decompose_table(void) |
| 2889 |
{ |
| 2890 |
int c; |
| 2891 |
CCInfo *ci; |
| 2892 |
int res[UNICODE_DECOMP_LEN_MAX], *ref; |
| 2893 |
int len, ref_len, is_compat; |
| 2894 |
|
| 2895 |
for(is_compat = 0; is_compat <= 1; is_compat++) { |
| 2896 |
for(c = 0; c < CHARCODE_MAX; c++) { |
| 2897 |
ci = &unicode_db[c]; |
| 2898 |
ref_len = ci->decomp_len; |
| 2899 |
ref = ci->decomp_data; |
| 2900 |
if (!is_compat && ci->is_compat) { |
| 2901 |
ref_len = 0; |
| 2902 |
} |
| 2903 |
len = unicode_decomp_char((uint32_t *)res, c, is_compat); |
| 2904 |
if (len != ref_len || |
| 2905 |
tabcmp(res, ref, ref_len) != 0) { |
| 2906 |
printf("ERROR c=%05x compat=%d\n", c, is_compat); |
| 2907 |
dump_str("res", res, len); |
| 2908 |
dump_str("ref", ref, ref_len); |
| 2909 |
exit(1); |
| 2910 |
} |
| 2911 |
} |
| 2912 |
} |
| 2913 |
} |
| 2914 |
|
| 2915 |
void check_compose_table(void) |
| 2916 |
{ |
| 2917 |
int i, p; |
| 2918 |
|
| 2919 |
|
| 2920 |
for(i = 0; i <= CHARCODE_MAX; i++) { |
| 2921 |
CCInfo *ci = &unicode_db[i]; |
| 2922 |
if (ci->decomp_len == 2 && !ci->is_compat && |
| 2923 |
!ci->is_excluded) { |
| 2924 |
p = unicode_compose_pair(ci->decomp_data[0], ci->decomp_data[1]); |
| 2925 |
if (p != i) { |
| 2926 |
printf("ERROR compose: c=%05x %05x -> %05x ref=%05x\n", |
| 2927 |
ci->decomp_data[0], ci->decomp_data[1], p, i); |
| 2928 |
exit(1); |
| 2929 |
} |
| 2930 |
} |
| 2931 |
} |
| 2932 |
|
| 2933 |
|
| 2934 |
|
| 2935 |
} |
| 2936 |
|
| 2937 |
#endif |
| 2938 |
|
| 2939 |
|
| 2940 |
|
| 2941 |
#ifdef USE_TEST |
| 2942 |
|
| 2943 |
void check_str(const char *msg, int num, const int *in_buf, int in_len, |
| 2944 |
const int *buf1, int len1, |
| 2945 |
const int *buf2, int len2) |
| 2946 |
{ |
| 2947 |
if (len1 != len2 || tabcmp(buf1, buf2, len1) != 0) { |
| 2948 |
printf("%d: ERROR %s:\n", num, msg); |
| 2949 |
dump_str(" in", in_buf, in_len); |
| 2950 |
dump_str("res", buf1, len1); |
| 2951 |
dump_str("ref", buf2, len2); |
| 2952 |
exit(1); |
| 2953 |
} |
| 2954 |
} |
| 2955 |
|
| 2956 |
void check_cc_table(void) |
| 2957 |
{ |
| 2958 |
int cc, cc_ref, c; |
| 2959 |
|
| 2960 |
for(c = 0; c <= CHARCODE_MAX; c++) { |
| 2961 |
cc_ref = unicode_db[c].combining_class; |
| 2962 |
cc = unicode_get_cc(c); |
| 2963 |
if (cc != cc_ref) { |
| 2964 |
printf("ERROR: c=%04x cc=%d cc_ref=%d\n", |
| 2965 |
c, cc, cc_ref); |
| 2966 |
exit(1); |
| 2967 |
} |
| 2968 |
} |
| 2969 |
#ifdef PROFILE |
| 2970 |
{ |
| 2971 |
int64_t ti, count; |
| 2972 |
|
| 2973 |
ti = get_time_ns(); |
| 2974 |
count = 0; |
| 2975 |
|
| 2976 |
for(c = 0x20; c <= 0xffff; c++) { |
| 2977 |
cc_ref = unicode_db[c].combining_class; |
| 2978 |
cc = unicode_get_cc(c); |
| 2979 |
count++; |
| 2980 |
} |
| 2981 |
ti = get_time_ns() - ti; |
| 2982 |
printf("cc time=%0.1f ns/char\n", |
| 2983 |
(double)ti / count); |
| 2984 |
} |
| 2985 |
#endif |
| 2986 |
} |
| 2987 |
|
| 2988 |
void normalization_test(const char *filename) |
| 2989 |
{ |
| 2990 |
FILE *f; |
| 2991 |
char line[4096], *p; |
| 2992 |
int *in_str, *nfc_str, *nfd_str, *nfkc_str, *nfkd_str; |
| 2993 |
int in_len, nfc_len, nfd_len, nfkc_len, nfkd_len; |
| 2994 |
int *buf, buf_len, pos; |
| 2995 |
|
| 2996 |
f = fopen(filename, "rb"); |
| 2997 |
if (!f) { |
| 2998 |
perror(filename); |
| 2999 |
exit(1); |
| 3000 |
} |
| 3001 |
pos = 0; |
| 3002 |
for(;;) { |
| 3003 |
if (!get_line(line, sizeof(line), f)) |
| 3004 |
break; |
| 3005 |
pos++; |
| 3006 |
p = line; |
| 3007 |
while (isspace(*p)) |
| 3008 |
p++; |
| 3009 |
if (*p == '#' || *p == '@') |
| 3010 |
continue; |
| 3011 |
in_str = get_field_str(&in_len, p, 0); |
| 3012 |
nfc_str = get_field_str(&nfc_len, p, 1); |
| 3013 |
nfd_str = get_field_str(&nfd_len, p, 2); |
| 3014 |
nfkc_str = get_field_str(&nfkc_len, p, 3); |
| 3015 |
nfkd_str = get_field_str(&nfkd_len, p, 4); |
| 3016 |
|
| 3017 |
|
| 3018 |
|
| 3019 |
buf_len = unicode_normalize((uint32_t **)&buf, (uint32_t *)in_str, in_len, UNICODE_NFD, NULL, NULL); |
| 3020 |
check_str("nfd", pos, in_str, in_len, buf, buf_len, nfd_str, nfd_len); |
| 3021 |
free(buf); |
| 3022 |
|
| 3023 |
buf_len = unicode_normalize((uint32_t **)&buf, (uint32_t *)in_str, in_len, UNICODE_NFKD, NULL, NULL); |
| 3024 |
check_str("nfkd", pos, in_str, in_len, buf, buf_len, nfkd_str, nfkd_len); |
| 3025 |
free(buf); |
| 3026 |
|
| 3027 |
buf_len = unicode_normalize((uint32_t **)&buf, (uint32_t *)in_str, in_len, UNICODE_NFC, NULL, NULL); |
| 3028 |
check_str("nfc", pos, in_str, in_len, buf, buf_len, nfc_str, nfc_len); |
| 3029 |
free(buf); |
| 3030 |
|
| 3031 |
buf_len = unicode_normalize((uint32_t **)&buf, (uint32_t *)in_str, in_len, UNICODE_NFKC, NULL, NULL); |
| 3032 |
check_str("nfkc", pos, in_str, in_len, buf, buf_len, nfkc_str, nfkc_len); |
| 3033 |
free(buf); |
| 3034 |
|
| 3035 |
free(in_str); |
| 3036 |
free(nfc_str); |
| 3037 |
free(nfd_str); |
| 3038 |
free(nfkc_str); |
| 3039 |
free(nfkd_str); |
| 3040 |
} |
| 3041 |
fclose(f); |
| 3042 |
} |
| 3043 |
#endif |
| 3044 |
|
| 3045 |
int main(int argc, char **argv) |
| 3046 |
{ |
| 3047 |
const char *unicode_db_path, *outfilename; |
| 3048 |
char filename[1024]; |
| 3049 |
|
| 3050 |
if (argc < 2) { |
| 3051 |
printf("usage: %s unicode_db_path [output_file]\n" |
| 3052 |
"\n" |
| 3053 |
"If no output_file is given, a self test is done using the current unicode library\n", |
| 3054 |
argv[0]); |
| 3055 |
exit(1); |
| 3056 |
} |
| 3057 |
unicode_db_path = argv[1]; |
| 3058 |
outfilename = NULL; |
| 3059 |
if (argc >= 3) |
| 3060 |
outfilename = argv[2]; |
| 3061 |
|
| 3062 |
unicode_db = mallocz(sizeof(unicode_db[0]) * (CHARCODE_MAX + 1)); |
| 3063 |
|
| 3064 |
snprintf(filename, sizeof(filename), "%s/UnicodeData.txt", unicode_db_path); |
| 3065 |
|
| 3066 |
parse_unicode_data(filename); |
| 3067 |
|
| 3068 |
snprintf(filename, sizeof(filename), "%s/SpecialCasing.txt", unicode_db_path); |
| 3069 |
parse_special_casing(unicode_db, filename); |
| 3070 |
|
| 3071 |
snprintf(filename, sizeof(filename), "%s/CaseFolding.txt", unicode_db_path); |
| 3072 |
parse_case_folding(unicode_db, filename); |
| 3073 |
|
| 3074 |
snprintf(filename, sizeof(filename), "%s/CompositionExclusions.txt", unicode_db_path); |
| 3075 |
parse_composition_exclusions(filename); |
| 3076 |
|
| 3077 |
snprintf(filename, sizeof(filename), "%s/DerivedCoreProperties.txt", unicode_db_path); |
| 3078 |
parse_derived_core_properties(filename); |
| 3079 |
|
| 3080 |
snprintf(filename, sizeof(filename), "%s/DerivedNormalizationProps.txt", unicode_db_path); |
| 3081 |
parse_derived_norm_properties(filename); |
| 3082 |
|
| 3083 |
snprintf(filename, sizeof(filename), "%s/PropList.txt", unicode_db_path); |
| 3084 |
parse_prop_list(filename); |
| 3085 |
|
| 3086 |
snprintf(filename, sizeof(filename), "%s/Scripts.txt", unicode_db_path); |
| 3087 |
parse_scripts(filename); |
| 3088 |
|
| 3089 |
snprintf(filename, sizeof(filename), "%s/ScriptExtensions.txt", |
| 3090 |
unicode_db_path); |
| 3091 |
parse_script_extensions(filename); |
| 3092 |
|
| 3093 |
snprintf(filename, sizeof(filename), "%s/emoji-data.txt", |
| 3094 |
unicode_db_path); |
| 3095 |
parse_prop_list(filename); |
| 3096 |
|
| 3097 |
|
| 3098 |
build_conv_table(unicode_db); |
| 3099 |
|
| 3100 |
#ifdef DUMP_CASE_FOLDING_SPECIAL_CASES |
| 3101 |
dump_case_folding_special_cases(unicode_db); |
| 3102 |
#endif |
| 3103 |
|
| 3104 |
if (!outfilename) { |
| 3105 |
#ifdef USE_TEST |
| 3106 |
check_case_conv(); |
| 3107 |
check_flags(); |
| 3108 |
check_decompose_table(); |
| 3109 |
check_compose_table(); |
| 3110 |
check_cc_table(); |
| 3111 |
snprintf(filename, sizeof(filename), "%s/NormalizationTest.txt", unicode_db_path); |
| 3112 |
normalization_test(filename); |
| 3113 |
#else |
| 3114 |
fprintf(stderr, "Tests are not compiled\n"); |
| 3115 |
exit(1); |
| 3116 |
#endif |
| 3117 |
} else |
| 3118 |
{ |
| 3119 |
FILE *fo = fopen(outfilename, "wb"); |
| 3120 |
|
| 3121 |
if (!fo) { |
| 3122 |
perror(outfilename); |
| 3123 |
exit(1); |
| 3124 |
} |
| 3125 |
fprintf(fo, |
| 3126 |
"/* Compressed unicode tables */\n" |
| 3127 |
"/* Automatically generated file - do not edit */\n" |
| 3128 |
"\n" |
| 3129 |
"#include <stdint.h>\n" |
| 3130 |
"\n"); |
| 3131 |
dump_case_conv_table(fo); |
| 3132 |
compute_internal_props(); |
| 3133 |
build_flags_tables(fo); |
| 3134 |
fprintf(fo, "#ifdef CONFIG_ALL_UNICODE\n\n"); |
| 3135 |
build_cc_table(fo); |
| 3136 |
build_decompose_table(fo); |
| 3137 |
build_general_category_table(fo); |
| 3138 |
build_script_table(fo); |
| 3139 |
build_script_ext_table(fo); |
| 3140 |
build_prop_list_table(fo); |
| 3141 |
fprintf(fo, "#endif /* CONFIG_ALL_UNICODE */\n"); |
| 3142 |
fclose(fo); |
| 3143 |
} |
| 3144 |
return 0; |
| 3145 |
} |