1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
|
/* Copyright (c) 2000, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
Without limiting anything contained in the foregoing, this file,
which is part of C Driver for MySQL (Connector/C), is also subject to the
Universal FOSS Exception, version 1.0, a copy of which can be found at
http://oss.oracle.com/licenses/universal-foss-exception.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <algorithm>
#include "m_ctype.h"
#include "m_string.h"
#include "my_byteorder.h"
#include "my_inttypes.h"
#include "my_loglevel.h"
#include "my_macros.h"
#include "my_xml.h"
#include "mysys_err.h"
/*
This files implements routines which parse XML based
character set and collation description files.
Unicode collations are encoded according to
Unicode Technical Standard #35
Locale Data Markup Language (LDML)
http://www.unicode.org/reports/tr35/
and converted into ICU string according to
Collation Customization
http://oss.software.ibm.com/icu/userguide/Collate_Customization.html
*/
int (*my_string_stack_guard)(int) = nullptr;
static char *mstr(char *str, const char *src, size_t l1, size_t l2) {
l1 = l1 < l2 ? l1 : l2;
memcpy(str, src, l1);
str[l1] = '\0';
return str;
}
struct my_cs_file_section_st {
int state;
const char *str;
};
#define _CS_MISC 1
#define _CS_ID 2
#define _CS_CSNAME 3
#define _CS_FAMILY 4
#define _CS_ORDER 5
#define _CS_COLNAME 6
#define _CS_FLAG 7
#define _CS_CHARSET 8
#define _CS_COLLATION 9
#define _CS_UPPERMAP 10
#define _CS_LOWERMAP 11
#define _CS_UNIMAP 12
#define _CS_COLLMAP 13
#define _CS_CTYPEMAP 14
#define _CS_PRIMARY_ID 15
#define _CS_BINARY_ID 16
#define _CS_CSDESCRIPT 17
/* Special purpose commands */
#define _CS_UCA_VERSION 100
#define _CS_CL_SUPPRESS_CONTRACTIONS 101
#define _CS_CL_OPTIMIZE 102
#define _CS_CL_SHIFT_AFTER_METHOD 103
/* Collation Settings */
#define _CS_ST_SETTINGS 200
#define _CS_ST_STRENGTH 201
#define _CS_ST_ALTERNATE 202
#define _CS_ST_BACKWARDS 203
#define _CS_ST_NORMALIZATION 204
#define _CS_ST_CASE_LEVEL 205
#define _CS_ST_CASE_FIRST 206
#define _CS_ST_HIRAGANA_QUATERNARY 207
#define _CS_ST_NUMERIC 208
#define _CS_ST_VARIABLE_TOP 209
#define _CS_ST_MATCH_BOUNDARIES 210
#define _CS_ST_MATCH_STYLE 211
/* Rules */
#define _CS_RULES 300
#define _CS_RESET 301
#define _CS_DIFF1 302
#define _CS_DIFF2 303
#define _CS_DIFF3 304
#define _CS_DIFF4 305
#define _CS_IDENTICAL 306
/* Rules: Expansions */
#define _CS_EXP_X 320
#define _CS_EXP_EXTEND 321
#define _CS_EXP_DIFF1 322
#define _CS_EXP_DIFF2 323
#define _CS_EXP_DIFF3 324
#define _CS_EXP_DIFF4 325
#define _CS_EXP_IDENTICAL 326
/* Rules: Abbreviating Ordering Specifications */
#define _CS_A_DIFF1 351
#define _CS_A_DIFF2 352
#define _CS_A_DIFF3 353
#define _CS_A_DIFF4 354
#define _CS_A_IDENTICAL 355
/* Rules: previous context */
#define _CS_CONTEXT 370
/* Rules: Placing Characters Before Others*/
#define _CS_RESET_BEFORE 380
/* Rules: Logical Reset Positions */
#define _CS_RESET_FIRST_PRIMARY_IGNORABLE 401
#define _CS_RESET_LAST_PRIMARY_IGNORABLE 402
#define _CS_RESET_FIRST_SECONDARY_IGNORABLE 403
#define _CS_RESET_LAST_SECONDARY_IGNORABLE 404
#define _CS_RESET_FIRST_TERTIARY_IGNORABLE 405
#define _CS_RESET_LAST_TERTIARY_IGNORABLE 406
#define _CS_RESET_FIRST_TRAILING 407
#define _CS_RESET_LAST_TRAILING 408
#define _CS_RESET_FIRST_VARIABLE 409
#define _CS_RESET_LAST_VARIABLE 410
#define _CS_RESET_FIRST_NON_IGNORABLE 411
#define _CS_RESET_LAST_NON_IGNORABLE 412
static struct my_cs_file_section_st sec[] = {
{_CS_MISC, "xml"},
{_CS_MISC, "xml/version"},
{_CS_MISC, "xml/encoding"},
{_CS_MISC, "charsets"},
{_CS_MISC, "charsets/max-id"},
{_CS_MISC, "charsets/copyright"},
{_CS_MISC, "charsets/description"},
{_CS_CHARSET, "charsets/charset"},
{_CS_PRIMARY_ID, "charsets/charset/primary-id"},
{_CS_BINARY_ID, "charsets/charset/binary-id"},
{_CS_CSNAME, "charsets/charset/name"},
{_CS_FAMILY, "charsets/charset/family"},
{_CS_CSDESCRIPT, "charsets/charset/description"},
{_CS_MISC, "charsets/charset/alias"},
{_CS_MISC, "charsets/charset/ctype"},
{_CS_CTYPEMAP, "charsets/charset/ctype/map"},
{_CS_MISC, "charsets/charset/upper"},
{_CS_UPPERMAP, "charsets/charset/upper/map"},
{_CS_MISC, "charsets/charset/lower"},
{_CS_LOWERMAP, "charsets/charset/lower/map"},
{_CS_MISC, "charsets/charset/unicode"},
{_CS_UNIMAP, "charsets/charset/unicode/map"},
{_CS_COLLATION, "charsets/charset/collation"},
{_CS_COLNAME, "charsets/charset/collation/name"},
{_CS_ID, "charsets/charset/collation/id"},
{_CS_ORDER, "charsets/charset/collation/order"},
{_CS_FLAG, "charsets/charset/collation/flag"},
{_CS_COLLMAP, "charsets/charset/collation/map"},
/* Special purpose commands */
{_CS_UCA_VERSION, "charsets/charset/collation/version"},
{_CS_CL_SUPPRESS_CONTRACTIONS,
"charsets/charset/collation/suppress_contractions"},
{_CS_CL_OPTIMIZE, "charsets/charset/collation/optimize"},
{_CS_CL_SHIFT_AFTER_METHOD,
"charsets/charset/collation/shift-after-method"},
/* Collation Settings */
{_CS_ST_SETTINGS, "charsets/charset/collation/settings"},
{_CS_ST_STRENGTH, "charsets/charset/collation/settings/strength"},
{_CS_ST_ALTERNATE, "charsets/charset/collation/settings/alternate"},
{_CS_ST_BACKWARDS, "charsets/charset/collation/settings/backwards"},
{_CS_ST_NORMALIZATION, "charsets/charset/collation/settings/normalization"},
{_CS_ST_CASE_LEVEL, "charsets/charset/collation/settings/caseLevel"},
{_CS_ST_CASE_FIRST, "charsets/charset/collation/settings/caseFirst"},
{_CS_ST_HIRAGANA_QUATERNARY,
"charsets/charset/collation/settings/hiraganaQuaternary"},
{_CS_ST_NUMERIC, "charsets/charset/collation/settings/numeric"},
{_CS_ST_VARIABLE_TOP, "charsets/charset/collation/settings/variableTop"},
{_CS_ST_MATCH_BOUNDARIES,
"charsets/charset/collation/settings/match-boundaries"},
{_CS_ST_MATCH_STYLE, "charsets/charset/collation/settings/match-style"},
/* Rules */
{_CS_RULES, "charsets/charset/collation/rules"},
{_CS_RESET, "charsets/charset/collation/rules/reset"},
{_CS_DIFF1, "charsets/charset/collation/rules/p"},
{_CS_DIFF2, "charsets/charset/collation/rules/s"},
{_CS_DIFF3, "charsets/charset/collation/rules/t"},
{_CS_DIFF4, "charsets/charset/collation/rules/q"},
{_CS_IDENTICAL, "charsets/charset/collation/rules/i"},
/* Rules: expansions */
{_CS_EXP_X, "charsets/charset/collation/rules/x"},
{_CS_EXP_EXTEND, "charsets/charset/collation/rules/x/extend"},
{_CS_EXP_DIFF1, "charsets/charset/collation/rules/x/p"},
{_CS_EXP_DIFF2, "charsets/charset/collation/rules/x/s"},
{_CS_EXP_DIFF3, "charsets/charset/collation/rules/x/t"},
{_CS_EXP_DIFF4, "charsets/charset/collation/rules/x/q"},
{_CS_EXP_IDENTICAL, "charsets/charset/collation/rules/x/i"},
/* Rules: previous context */
{_CS_CONTEXT, "charsets/charset/collation/rules/x/context"},
/* Rules: Abbreviating Ordering Specifications */
{_CS_A_DIFF1, "charsets/charset/collation/rules/pc"},
{_CS_A_DIFF2, "charsets/charset/collation/rules/sc"},
{_CS_A_DIFF3, "charsets/charset/collation/rules/tc"},
{_CS_A_DIFF4, "charsets/charset/collation/rules/qc"},
{_CS_A_IDENTICAL, "charsets/charset/collation/rules/ic"},
/* Rules: Placing Characters Before Others*/
{_CS_RESET_BEFORE, "charsets/charset/collation/rules/reset/before"},
/* Rules: Logical Reset Positions */
{_CS_RESET_FIRST_NON_IGNORABLE,
"charsets/charset/collation/rules/reset/first_non_ignorable"},
{_CS_RESET_LAST_NON_IGNORABLE,
"charsets/charset/collation/rules/reset/last_non_ignorable"},
{_CS_RESET_FIRST_PRIMARY_IGNORABLE,
"charsets/charset/collation/rules/reset/first_primary_ignorable"},
{_CS_RESET_LAST_PRIMARY_IGNORABLE,
"charsets/charset/collation/rules/reset/last_primary_ignorable"},
{_CS_RESET_FIRST_SECONDARY_IGNORABLE,
"charsets/charset/collation/rules/reset/first_secondary_ignorable"},
{_CS_RESET_LAST_SECONDARY_IGNORABLE,
"charsets/charset/collation/rules/reset/last_secondary_ignorable"},
{_CS_RESET_FIRST_TERTIARY_IGNORABLE,
"charsets/charset/collation/rules/reset/first_tertiary_ignorable"},
{_CS_RESET_LAST_TERTIARY_IGNORABLE,
"charsets/charset/collation/rules/reset/last_tertiary_ignorable"},
{_CS_RESET_FIRST_TRAILING,
"charsets/charset/collation/rules/reset/first_trailing"},
{_CS_RESET_LAST_TRAILING,
"charsets/charset/collation/rules/reset/last_trailing"},
{_CS_RESET_FIRST_VARIABLE,
"charsets/charset/collation/rules/reset/first_variable"},
{_CS_RESET_LAST_VARIABLE,
"charsets/charset/collation/rules/reset/last_variable"},
{0, nullptr}};
static struct my_cs_file_section_st *cs_file_sec(const char *attr, size_t len) {
struct my_cs_file_section_st *s;
for (s = sec; s->str; s++) {
if (!strncmp(attr, s->str, len) && s->str[len] == 0) return s;
}
return nullptr;
}
#define MY_CS_CSDESCR_SIZE 64
#define MY_CS_CONTEXT_SIZE 64
typedef struct my_cs_file_info {
char csname[MY_CS_NAME_SIZE];
char name[MY_CS_NAME_SIZE];
uchar ctype[MY_CS_CTYPE_TABLE_SIZE];
uchar to_lower[MY_CS_TO_LOWER_TABLE_SIZE];
uchar to_upper[MY_CS_TO_UPPER_TABLE_SIZE];
uchar sort_order[MY_CS_SORT_ORDER_TABLE_SIZE];
uint16 tab_to_uni[MY_CS_TO_UNI_TABLE_SIZE];
char comment[MY_CS_CSDESCR_SIZE];
char *tailoring;
size_t tailoring_length;
size_t tailoring_alloced_length;
char context[MY_CS_CONTEXT_SIZE];
CHARSET_INFO cs;
MY_CHARSET_LOADER *loader;
} MY_CHARSET_FILE;
static void my_charset_file_reset_charset(MY_CHARSET_FILE *i) {
memset(&i->cs, 0, sizeof(i->cs));
}
static void my_charset_file_reset_collation(MY_CHARSET_FILE *i) {
i->tailoring_length = 0;
i->context[0] = '\0';
}
static void my_charset_file_init(MY_CHARSET_FILE *i) {
my_charset_file_reset_charset(i);
my_charset_file_reset_collation(i);
i->tailoring = nullptr;
i->tailoring_alloced_length = 0;
}
static void my_charset_file_free(MY_CHARSET_FILE *i) {
i->loader->mem_free(i->tailoring);
}
static int my_charset_file_tailoring_realloc(MY_CHARSET_FILE *i,
size_t newlen) {
if (i->tailoring_alloced_length > newlen ||
(i->tailoring = static_cast<char *>(i->loader->mem_realloc(
i->tailoring,
(i->tailoring_alloced_length = (newlen + 32 * 1024)))))) {
return MY_XML_OK;
}
return MY_XML_ERROR;
}
static int fill_uchar(uchar *a, uint size, const char *str, size_t len) {
uint i = 0;
const char *s, *b, *e = str + len;
for (s = str; s < e; i++) {
for (; (s < e) && strchr(" \t\r\n", s[0]); s++)
;
b = s;
for (; (s < e) && !strchr(" \t\r\n", s[0]); s++)
;
if (s == b || i > size) break;
a[i] = (uchar)strtoul(b, nullptr, 16);
}
return 0;
}
static int fill_uint16(uint16 *a, uint size, const char *str, size_t len) {
uint i = 0;
const char *s, *b, *e = str + len;
for (s = str; s < e; i++) {
for (; (s < e) && strchr(" \t\r\n", s[0]); s++)
;
b = s;
for (; (s < e) && !strchr(" \t\r\n", s[0]); s++)
;
if (s == b || i > size) break;
a[i] = (uint16)strtol(b, nullptr, 16);
}
return 0;
}
static int tailoring_append(MY_XML_PARSER *st, const char *fmt, size_t len,
const char *attr) {
struct my_cs_file_info *i = (struct my_cs_file_info *)st->user_data;
size_t newlen = i->tailoring_length + len + 64; /* 64 for format */
if (MY_XML_OK == my_charset_file_tailoring_realloc(i, newlen)) {
char *dst = i->tailoring + i->tailoring_length;
sprintf(dst, fmt, (int)len, attr);
i->tailoring_length += strlen(dst);
return MY_XML_OK;
}
return MY_XML_ERROR;
}
static int tailoring_append2(MY_XML_PARSER *st, const char *fmt, size_t len1,
const char *attr1, size_t len2,
const char *attr2) {
struct my_cs_file_info *i = (struct my_cs_file_info *)st->user_data;
size_t newlen = i->tailoring_length + len1 + len2 + 64; /* 64 for format */
if (MY_XML_OK == my_charset_file_tailoring_realloc(i, newlen)) {
char *dst = i->tailoring + i->tailoring_length;
sprintf(dst, fmt, (int)len1, attr1, (int)len2, attr2);
i->tailoring_length += strlen(dst);
return MY_XML_OK;
}
return MY_XML_ERROR;
}
static size_t scan_one_character(const char *s, const char *e, my_wc_t *wc) {
CHARSET_INFO *cs = &my_charset_utf8mb3_general_ci;
if (s >= e) return 0;
/* Escape sequence: \uXXXX */
if (s[0] == '\\' && s + 2 < e && s[1] == 'u' && my_isxdigit(cs, s[2])) {
size_t len = 3; /* We have at least one digit */
for (s += 3; s < e && my_isxdigit(cs, s[0]); s++, len++) {
}
wc[0] = 0;
return len;
} else if ((s[0] & 0x80) == 0) /* 7-bit character */
{
wc[0] = 0;
return 1;
} else /* Non-escaped character */
{
int rc = cs->cset->mb_wc(cs, wc, pointer_cast<const uchar *>(s),
pointer_cast<const uchar *>(e));
if (rc > 0) return (size_t)rc;
}
return 0;
}
static int tailoring_append_abbreviation(MY_XML_PARSER *st, const char *fmt,
size_t len, const char *attr) {
size_t clen;
const char *attrend = attr + len;
my_wc_t wc;
for (; (clen = scan_one_character(attr, attrend, &wc)) > 0; attr += clen) {
assert(attr < attrend);
if (tailoring_append(st, fmt, clen, attr) != MY_XML_OK) return MY_XML_ERROR;
}
return MY_XML_OK;
}
extern "C" {
static int cs_enter(MY_XML_PARSER *st, const char *attr, size_t len) {
struct my_cs_file_info *i = (struct my_cs_file_info *)st->user_data;
struct my_cs_file_section_st *s = cs_file_sec(attr, len);
int state = s ? s->state : 0;
switch (state) {
case 0:
i->loader->reporter(WARNING_LEVEL, EE_UNKNOWN_LDML_TAG, (int)len, attr);
break;
case _CS_CHARSET:
my_charset_file_reset_charset(i);
break;
case _CS_COLLATION:
my_charset_file_reset_collation(i);
break;
case _CS_RESET:
return tailoring_append(st, " &", 0, nullptr);
default:
break;
}
return MY_XML_OK;
}
static int cs_leave(MY_XML_PARSER *st, const char *attr, size_t len) {
struct my_cs_file_info *i = (struct my_cs_file_info *)st->user_data;
struct my_cs_file_section_st *s = cs_file_sec(attr, len);
int state = s ? s->state : 0;
int rc;
switch (state) {
case _CS_COLLATION:
if (i->tailoring_length) i->cs.tailoring = i->tailoring;
rc = i->loader->add_collation(&i->cs);
break;
/* Rules: Logical Reset Positions */
case _CS_RESET_FIRST_NON_IGNORABLE:
rc = tailoring_append(st, "[first non-ignorable]", 0, nullptr);
break;
case _CS_RESET_LAST_NON_IGNORABLE:
rc = tailoring_append(st, "[last non-ignorable]", 0, nullptr);
break;
case _CS_RESET_FIRST_PRIMARY_IGNORABLE:
rc = tailoring_append(st, "[first primary ignorable]", 0, nullptr);
break;
case _CS_RESET_LAST_PRIMARY_IGNORABLE:
rc = tailoring_append(st, "[last primary ignorable]", 0, nullptr);
break;
case _CS_RESET_FIRST_SECONDARY_IGNORABLE:
rc = tailoring_append(st, "[first secondary ignorable]", 0, nullptr);
break;
case _CS_RESET_LAST_SECONDARY_IGNORABLE:
rc = tailoring_append(st, "[last secondary ignorable]", 0, nullptr);
break;
case _CS_RESET_FIRST_TERTIARY_IGNORABLE:
rc = tailoring_append(st, "[first tertiary ignorable]", 0, nullptr);
break;
case _CS_RESET_LAST_TERTIARY_IGNORABLE:
rc = tailoring_append(st, "[last tertiary ignorable]", 0, nullptr);
break;
case _CS_RESET_FIRST_TRAILING:
rc = tailoring_append(st, "[first trailing]", 0, nullptr);
break;
case _CS_RESET_LAST_TRAILING:
rc = tailoring_append(st, "[last trailing]", 0, nullptr);
break;
case _CS_RESET_FIRST_VARIABLE:
rc = tailoring_append(st, "[first variable]", 0, nullptr);
break;
case _CS_RESET_LAST_VARIABLE:
rc = tailoring_append(st, "[last variable]", 0, nullptr);
break;
default:
rc = MY_XML_OK;
}
return rc;
}
} // extern "C"
static const char *diff_fmt[5] = {"<%.*s", "<<%.*s", "<<<%.*s", "<<<<%.*s",
"=%.*s"};
static const char *context_diff_fmt[5] = {
"<%.*s|%.*s", "<<%.*s|%.*s", "<<<%.*s|%.*s", "<<<<%.*s|%.*s", "=%.*s|%.*s"};
extern "C" {
static int cs_value(MY_XML_PARSER *st, const char *attr, size_t len) {
struct my_cs_file_info *i = (struct my_cs_file_info *)st->user_data;
struct my_cs_file_section_st *s;
int state =
(int)((s = cs_file_sec(st->attr.start, st->attr.end - st->attr.start))
? s->state
: 0);
int rc = MY_XML_OK;
switch (state) {
case _CS_MISC:
case _CS_FAMILY:
case _CS_ORDER:
break;
case _CS_ID:
i->cs.number = strtol(attr, (char **)nullptr, 10);
break;
case _CS_BINARY_ID:
i->cs.binary_number = strtol(attr, (char **)nullptr, 10);
break;
case _CS_PRIMARY_ID:
i->cs.primary_number = strtol(attr, (char **)nullptr, 10);
break;
case _CS_COLNAME:
i->cs.m_coll_name = mstr(i->name, attr, len, MY_CS_NAME_SIZE - 1);
break;
case _CS_CSNAME:
// Replace "utf8" with "utf8mb3" for external character sets.
if (0 == strncmp(attr, "utf8", len))
i->cs.csname =
mstr(i->csname, STRING_WITH_LEN("utf8mb3"), MY_CS_NAME_SIZE - 1);
else
i->cs.csname = mstr(i->csname, attr, len, MY_CS_NAME_SIZE - 1);
assert(0 != strcmp(i->cs.csname, "utf8"));
break;
case _CS_CSDESCRIPT:
i->cs.comment = mstr(i->comment, attr, len, MY_CS_CSDESCR_SIZE - 1);
break;
case _CS_FLAG:
if (!strncmp("primary", attr, len))
i->cs.state |= MY_CS_PRIMARY;
else if (!strncmp("binary", attr, len))
i->cs.state |= MY_CS_BINSORT;
else if (!strncmp("compiled", attr, len))
i->cs.state |= MY_CS_COMPILED;
break;
case _CS_UPPERMAP:
fill_uchar(i->to_upper, MY_CS_TO_UPPER_TABLE_SIZE, attr, len);
i->cs.to_upper = i->to_upper;
break;
case _CS_LOWERMAP:
fill_uchar(i->to_lower, MY_CS_TO_LOWER_TABLE_SIZE, attr, len);
i->cs.to_lower = i->to_lower;
break;
case _CS_UNIMAP:
fill_uint16(i->tab_to_uni, MY_CS_TO_UNI_TABLE_SIZE, attr, len);
i->cs.tab_to_uni = i->tab_to_uni;
break;
case _CS_COLLMAP:
fill_uchar(i->sort_order, MY_CS_SORT_ORDER_TABLE_SIZE, attr, len);
i->cs.sort_order = i->sort_order;
break;
case _CS_CTYPEMAP:
fill_uchar(i->ctype, MY_CS_CTYPE_TABLE_SIZE, attr, len);
i->cs.ctype = i->ctype;
break;
/* Special purpose commands */
case _CS_UCA_VERSION:
rc = tailoring_append(st, "[version %.*s]", len, attr);
break;
case _CS_CL_SUPPRESS_CONTRACTIONS:
rc = tailoring_append(st, "[suppress contractions %.*s]", len, attr);
break;
case _CS_CL_OPTIMIZE:
rc = tailoring_append(st, "[optimize %.*s]", len, attr);
break;
case _CS_CL_SHIFT_AFTER_METHOD:
rc = tailoring_append(st, "[shift-after-method %.*s]", len, attr);
break;
/* Collation Settings */
case _CS_ST_STRENGTH:
/* 1, 2, 3, 4, 5, or primary, secondary, tertiary, quaternary, identical
*/
rc = tailoring_append(st, "[strength %.*s]", len, attr);
break;
case _CS_ST_ALTERNATE:
/* non-ignorable, shifted */
rc = tailoring_append(st, "[alternate %.*s]", len, attr);
break;
case _CS_ST_BACKWARDS:
/* on, off, 2 */
rc = tailoring_append(st, "[backwards %.*s]", len, attr);
break;
case _CS_ST_NORMALIZATION:
/*
TODO for WL#896: check collations for normalization: vi.xml
We want precomposed characters work well at this point.
*/
/* on, off */
rc = tailoring_append(st, "[normalization %.*s]", len, attr);
break;
case _CS_ST_CASE_LEVEL:
/* on, off */
rc = tailoring_append(st, "[caseLevel %.*s]", len, attr);
break;
case _CS_ST_CASE_FIRST:
/* upper, lower, off */
rc = tailoring_append(st, "[caseFirst %.*s]", len, attr);
break;
case _CS_ST_HIRAGANA_QUATERNARY:
/* on, off */
rc = tailoring_append(st, "[hiraganaQ %.*s]", len, attr);
break;
case _CS_ST_NUMERIC:
/* on, off */
rc = tailoring_append(st, "[numeric %.*s]", len, attr);
break;
case _CS_ST_VARIABLE_TOP:
/* TODO for WL#896: check value format */
rc = tailoring_append(st, "[variableTop %.*s]", len, attr);
break;
case _CS_ST_MATCH_BOUNDARIES:
/* none, whole-character, whole-word */
rc = tailoring_append(st, "[match-boundaries %.*s]", len, attr);
break;
case _CS_ST_MATCH_STYLE:
/* minimal, medial, maximal */
rc = tailoring_append(st, "[match-style %.*s]", len, attr);
break;
/* Rules */
case _CS_RESET:
rc = tailoring_append(st, "%.*s", len, attr);
break;
case _CS_DIFF1:
case _CS_DIFF2:
case _CS_DIFF3:
case _CS_DIFF4:
case _CS_IDENTICAL:
rc = tailoring_append(st, diff_fmt[state - _CS_DIFF1], len, attr);
break;
/* Rules: Expansion */
case _CS_EXP_EXTEND:
rc = tailoring_append(st, " / %.*s", len, attr);
break;
case _CS_EXP_DIFF1:
case _CS_EXP_DIFF2:
case _CS_EXP_DIFF3:
case _CS_EXP_DIFF4:
case _CS_EXP_IDENTICAL:
if (i->context[0]) {
rc = tailoring_append2(st, context_diff_fmt[state - _CS_EXP_DIFF1],
strlen(i->context), i->context, len, attr);
i->context[0] = 0;
} else
rc = tailoring_append(st, diff_fmt[state - _CS_EXP_DIFF1], len, attr);
break;
/* Rules: Context */
case _CS_CONTEXT:
if (len < sizeof(i->context)) {
memcpy(i->context, attr, len);
i->context[len] = '\0';
}
break;
/* Rules: Abbreviating Ordering Specifications */
case _CS_A_DIFF1:
case _CS_A_DIFF2:
case _CS_A_DIFF3:
case _CS_A_DIFF4:
case _CS_A_IDENTICAL:
rc = tailoring_append_abbreviation(st, diff_fmt[state - _CS_A_DIFF1], len,
attr);
break;
/* Rules: Placing Characters Before Others */
case _CS_RESET_BEFORE:
/*
TODO for WL#896: Add this check into text customization parser:
It is an error if the strength of the before relation is not identical
to the relation after the reset. We'll need this for WL#896.
*/
rc = tailoring_append(st, "[before %.*s]", len, attr);
break;
default:
break;
}
return rc;
}
} // extern "C"
bool my_parse_charset_xml(MY_CHARSET_LOADER *loader, const char *buf,
size_t len) {
MY_XML_PARSER p;
struct my_cs_file_info info;
bool rc;
my_charset_file_init(&info);
my_xml_parser_create(&p);
my_xml_set_enter_handler(&p, cs_enter);
my_xml_set_value_handler(&p, cs_value);
my_xml_set_leave_handler(&p, cs_leave);
info.loader = loader;
my_xml_set_user_data(&p, (void *)&info);
rc = (my_xml_parse(&p, buf, len) == MY_XML_OK) ? false : true;
my_xml_parser_free(&p);
my_charset_file_free(&info);
if (rc != MY_XML_OK) {
const char *errstr = my_xml_error_string(&p);
if (sizeof(loader->errarg) > 32 + strlen(errstr)) {
sprintf(loader->errarg, "at line %d pos %d: %s",
my_xml_error_lineno(&p) + 1, (int)my_xml_error_pos(&p),
my_xml_error_string(&p));
}
}
return rc;
}
/*
Check repertoire: detect pure ascii strings
*/
uint my_string_repertoire(const CHARSET_INFO *cs, const char *str,
size_t length) {
const char *strend = str + length;
if (cs->mbminlen == 1) {
for (; str < strend; str++) {
if (((uchar)*str) > 0x7F) return MY_REPERTOIRE_UNICODE30;
}
} else {
my_wc_t wc;
int chlen;
for (; (chlen = cs->cset->mb_wc(cs, &wc, pointer_cast<const uchar *>(str),
pointer_cast<const uchar *>(strend))) > 0;
str += chlen) {
if (wc > 0x7F) return MY_REPERTOIRE_UNICODE30;
}
}
return MY_REPERTOIRE_ASCII;
}
/*
Returns repertoire for charset
*/
uint my_charset_repertoire(const CHARSET_INFO *cs) {
return cs->state & MY_CS_PUREASCII ? MY_REPERTOIRE_ASCII
: MY_REPERTOIRE_UNICODE30;
}
/*
Detect if a character set is 8bit,
and it is pure ascii, i.e. doesn't have
characters outside U+0000..U+007F
This functions is shared between "conf_to_src"
and dynamic charsets loader in "mysqld".
*/
bool my_charset_is_8bit_pure_ascii(const CHARSET_INFO *cs) {
size_t code;
if (!cs->tab_to_uni) return false;
for (code = 0; code < 256; code++) {
if (cs->tab_to_uni[code] > 0x7F) return false;
}
return true;
}
/*
Shared function between conf_to_src and mysys.
Check if a 8bit character set is compatible with
ascii on the range 0x00..0x7F.
*/
bool my_charset_is_ascii_compatible(const CHARSET_INFO *cs) {
uint i;
if (!cs->tab_to_uni) return true;
for (i = 0; i < 128; i++) {
if (cs->tab_to_uni[i] != i) return false;
}
return true;
}
/**
Convert a string between two character sets.
'to' must be large enough to store (form_length * to_cs->mbmaxlen) bytes.
@param [out] to Store result here
@param to_length Size of "to" buffer
@param to_cs Character set of result string
@param from Copy from here
@param from_length Length of the "from" string
@param from_cs Character set of the "from" string
@param [out] errors Number of conversion errors
@return Number of bytes copied to 'to' string
*/
static size_t my_convert_internal(char *to, size_t to_length,
const CHARSET_INFO *to_cs, const char *from,
size_t from_length,
const CHARSET_INFO *from_cs, uint *errors) {
int cnvres;
my_wc_t wc;
const uchar *from_end = (const uchar *)from + from_length;
char *to_start = to;
uchar *to_end = (uchar *)to + to_length;
my_charset_conv_mb_wc mb_wc = from_cs->cset->mb_wc;
my_charset_conv_wc_mb wc_mb = to_cs->cset->wc_mb;
uint error_count = 0;
while (true) {
if ((cnvres = (*mb_wc)(from_cs, &wc, pointer_cast<const uchar *>(from),
from_end)) > 0)
from += cnvres;
else if (cnvres == MY_CS_ILSEQ) {
error_count++;
from++;
wc = '?';
} else if (cnvres > MY_CS_TOOSMALL) {
/*
A correct multibyte sequence detected
But it doesn't have Unicode mapping.
*/
error_count++;
from += (-cnvres);
wc = '?';
} else
break; // Not enough characters
outp:
if ((cnvres = (*wc_mb)(to_cs, wc, (uchar *)to, to_end)) > 0)
to += cnvres;
else if (cnvres == MY_CS_ILUNI && wc != '?') {
error_count++;
wc = '?';
goto outp;
} else
break;
}
*errors = error_count;
return (uint32)(to - to_start);
}
/**
Convert a string between two character sets.
Optimized for quick copying of ASCII characters in the range 0x00..0x7F.
'to' must be large enough to store (form_length * to_cs->mbmaxlen) bytes.
@param [out] to Store result here
@param to_length Size of "to" buffer
@param to_cs Character set of result string
@param from Copy from here
@param from_length Length of the "from" string
@param from_cs Character set of the "from" string
@param [out] errors Number of conversion errors
@return Number of bytes copied to 'to' string
*/
size_t my_convert(char *to, size_t to_length, const CHARSET_INFO *to_cs,
const char *from, size_t from_length,
const CHARSET_INFO *from_cs, uint *errors) {
size_t length, length2;
/*
If any of the character sets is not ASCII compatible,
immediately switch to slow mb_wc->wc_mb method.
*/
if ((to_cs->state | from_cs->state) & MY_CS_NONASCII)
return my_convert_internal(to, to_length, to_cs, from, from_length, from_cs,
errors);
length = length2 = std::min(to_length, from_length);
#if defined(__i386__) || defined(_WIN32) || defined(__x86_64__)
/*
Special loop for i386, it allows to refer to a
non-aligned memory block as UINT32, which makes
it possible to copy four bytes at once. This
gives about 10% performance improvement comparing
to byte-by-byte loop.
*/
for (; length >= 4; length -= 4, from += 4, to += 4) {
if (uint4korr(from) & 0x80808080) break;
int4store(to, uint4korr(from));
}
#endif /* __i386__ */
for (;; *to++ = *from++, length--) {
if (!length) {
*errors = 0;
return length2;
}
if ((static_cast<uchar>(*from)) > 0x7F) /* A non-ASCII character */
{
size_t copied_length = length2 - length;
to_length -= copied_length;
from_length -= copied_length;
return copied_length + my_convert_internal(to, to_length, to_cs, from,
from_length, from_cs, errors);
}
}
assert(false); // Should never get to here
return 0; // Make compiler happy
}
/**
Get the length of the first code in given sequence of chars.
This func is introduced because we can't determine the length by
checking the first byte only for gb18030, so we first try my_mbcharlen,
and then my_mbcharlen_2 if necessary to get the length
@param[in] cs charset_info
@param[in] s start of the char sequence
@param[in] e end of the char sequence
@return The length of the first code, or 0 for invalid code
*/
uint my_mbcharlen_ptr(const CHARSET_INFO *cs, const char *s, const char *e) {
uint len = my_mbcharlen(cs, (uchar)*s);
if (len == 0 && my_mbmaxlenlen(cs) == 2 && s + 1 < e) {
len = my_mbcharlen_2(cs, (uchar)*s, (uchar) * (s + 1));
/* It could be either a valid multi-byte GB18030 code, or invalid
gb18030 code if return value is 0 */
assert(len == 0 || len == 2 || len == 4);
}
return len;
}
/**
Identify whether given like pattern looks like a prefix pattern, which can
become candidate for index only scan on prefix indexes.
@param cs Character set and collation pointer
@param wildstr Pointer to LIKE pattern.
@param wildend Pointer to end of LIKE pattern.
@param escape Escape character pattern, typically '\'.
@param w_many 'Many characters' pattern, typically '%'.
@param[out] prefix_len Length of LIKE pattern.
@return Optimization status.
@retval TRUE if LIKE pattern can be used for prefix index only scan.
@retval FALSE else.
*/
bool my_is_prefixidx_cand(const CHARSET_INFO *cs, const char *wildstr,
const char *wildend, int escape, int w_many,
size_t *prefix_len) {
my_wc_t wc;
*prefix_len = 0;
/* Find first occurrence of w_many pattern. */
while (wildstr < wildend) {
int res;
if ((res = cs->cset->mb_wc(cs, &wc, pointer_cast<const uchar *>(wildstr),
pointer_cast<const uchar *>(wildend))) <= 0) {
if (res == MY_CS_ILSEQ) /* Bad sequence */
return false;
return true; /* End of the string */
}
wildstr += res;
if (wc == (my_wc_t)w_many) break;
if (wc == (my_wc_t)escape) {
if ((res = cs->cset->mb_wc(cs, &wc, pointer_cast<const uchar *>(wildstr),
pointer_cast<const uchar *>(wildend))) <= 0) {
if (res == MY_CS_ILSEQ) /* Bad sequence */
return false;
(*prefix_len)++;
return true; /* End of the string, last character is escape */
}
wildstr += res;
}
(*prefix_len)++;
}
/* If further char is not w_many then not a candidate prefix sequence. */
while (wildstr < wildend) {
int res;
if ((res = cs->cset->mb_wc(cs, &wc, pointer_cast<const uchar *>(wildstr),
pointer_cast<const uchar *>(wildend))) <= 0) {
if (res == MY_CS_ILSEQ) /* Bad sequence */
return false;
return true; /* End of the string */
}
if (wc != (my_wc_t)w_many) return false;
wildstr += res;
}
return true;
}
|