summaryrefslogtreecommitdiffstats
path: root/strings/ctype.c
blob: 46951c3ae1f8f64b5187235075843fadabb9ec5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
/* Copyright (c) 2000, 2013, Oracle and/or its affiliates.
   Copyright (c) 2009, 2020, MariaDB Corporation.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   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 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-1335  USA */

#include "strings_def.h"
#include <m_ctype.h>
#include <my_xml.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
  
*/

const char charset_name_latin2[]= "latin2";
const char charset_name_utf8[]= "utf8";
const char charset_name_utf16[]= "utf16";
const char charset_name_utf32[]= "utf32";
const char charset_name_ucs2[]= "ucs2";
const char charset_name_utf8mb4[]= "utf8mb4";

/*
  Avoid using my_snprintf
  We cannot use my_snprintf() here, because ctype.o is
  used to build conf_to_src, which must require minimum
  dependency.
*/

#undef my_snprinf
#define my_snprintf "We cannot use my_snprintf in this file"


int (*my_string_stack_guard)(int)= NULL;

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
#define _CS_CL_RULES_IMPORT             104
#define _CS_CL_RULES_IMPORT_SOURCE      105


/* 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 const 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"},
  {_CS_CL_RULES_IMPORT,          "charsets/charset/collation/rules/import"},
  {_CS_CL_RULES_IMPORT_SOURCE,   "charsets/charset/collation/rules/import/source"},

  /* 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,	NULL}
};

static const struct my_cs_file_section_st
*cs_file_sec(const char *attr, size_t len)
{
  const 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 NULL;
}

#define MY_CS_CSDESCR_SIZE	64
#define MY_CS_TAILORING_SIZE	(32*1024)
#define MY_CS_UCA_VERSION_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];
  struct charset_info_st 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= NULL;
  i->tailoring_alloced_length= 0;
}


static void
my_charset_file_free(MY_CHARSET_FILE *i)
{
  i->loader->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= i->loader->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,NULL,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,NULL,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 ((int8) s[0] > 0) /* 7-bit character */
  {
    wc[0]= 0;
    return 1;
  }
  else /* Non-escaped character */
  {
    int rc= my_ci_mb_wc(cs, wc, (uchar *) s, (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)
  {
    DBUG_ASSERT(attr < attrend);
    if (tailoring_append(st, fmt, clen, attr) != MY_XML_OK)
      return MY_XML_ERROR;
  }
  return MY_XML_OK;
}


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;
  const 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, "Unknown LDML tag: '%.*s'", 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, NULL);

  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;
  const 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->loader->add_collation(&i->cs) : MY_XML_OK;
    break;

  /* Rules: Logical Reset Positions */
  case _CS_RESET_FIRST_NON_IGNORABLE:
    rc= tailoring_append(st, "[first non-ignorable]", 0, NULL);
    break;

  case _CS_RESET_LAST_NON_IGNORABLE:
    rc= tailoring_append(st, "[last non-ignorable]", 0, NULL);
    break;

  case _CS_RESET_FIRST_PRIMARY_IGNORABLE:
    rc= tailoring_append(st, "[first primary ignorable]", 0, NULL);
    break;

  case _CS_RESET_LAST_PRIMARY_IGNORABLE:
    rc= tailoring_append(st, "[last primary ignorable]", 0, NULL);
    break;

  case _CS_RESET_FIRST_SECONDARY_IGNORABLE:
    rc= tailoring_append(st, "[first secondary ignorable]", 0, NULL);
    break;

  case _CS_RESET_LAST_SECONDARY_IGNORABLE:
    rc= tailoring_append(st, "[last secondary ignorable]", 0, NULL);
    break;

  case _CS_RESET_FIRST_TERTIARY_IGNORABLE:
    rc= tailoring_append(st, "[first tertiary ignorable]", 0, NULL);
    break;

  case _CS_RESET_LAST_TERTIARY_IGNORABLE:
    rc= tailoring_append(st, "[last tertiary ignorable]", 0, NULL);
    break;

  case _CS_RESET_FIRST_TRAILING:
    rc= tailoring_append(st, "[first trailing]", 0, NULL);
    break;

  case _CS_RESET_LAST_TRAILING:
    rc= tailoring_append(st, "[last trailing]", 0, NULL);
    break;

  case _CS_RESET_FIRST_VARIABLE:
    rc= tailoring_append(st, "[first variable]", 0, NULL);
    break;

  case _CS_RESET_LAST_VARIABLE:
    rc= tailoring_append(st, "[last variable]", 0, NULL);
    break;

  default:
    rc=MY_XML_OK;
  }
  return rc;
}


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"
};


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;
  const 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**)NULL,10);
    break;
  case _CS_BINARY_ID:
    i->cs.binary_number= strtol(attr,(char**)NULL,10);
    break;
  case _CS_PRIMARY_ID:
    i->cs.primary_number= strtol(attr,(char**)NULL,10);
    break;
  case _CS_COLNAME:
    i->cs.name=mstr(i->name,attr,len,MY_CS_NAME_SIZE-1);
    break;
  case _CS_CSNAME:
    i->cs.csname=mstr(i->csname,attr,len,MY_CS_NAME_SIZE-1);
    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;
    else if (!strncmp("nopad",attr,len))
      i->cs.state|= MY_CS_NOPAD;
    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.m_ctype=i->ctype;
    break;

  /* Special purpose commands */
  case _CS_UCA_VERSION:
    rc= tailoring_append(st, "[version %.*s]", len, attr);
    break;

  case _CS_CL_RULES_IMPORT_SOURCE:
    rc= tailoring_append(st, "[import %.*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);
    if (len && attr[0] >= '1' && attr[0] <= '9')
      i->cs.levels_for_order= attr[0] - '0';
    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;
}


my_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;
  my_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->error) > 32 + strlen(errstr))
    {
      /* We cannot use my_snprintf() here. See previous comment. */
      sprintf(loader->error, "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;
}


uint
my_string_repertoire_8bit(CHARSET_INFO *cs, const char *str, size_t length)
{
  const char *strend;
  if ((cs->state & MY_CS_NONASCII) && length > 0)
    return MY_REPERTOIRE_UNICODE30;
  for (strend= str + length; str < strend; str++)
  {
    if (((uchar) *str) > 0x7F)
      return MY_REPERTOIRE_UNICODE30;
  }
  return MY_REPERTOIRE_ASCII;
}


static void
my_string_metadata_init(MY_STRING_METADATA *metadata)
{
  metadata->repertoire= MY_REPERTOIRE_ASCII;
  metadata->char_length= 0;
}


/**
  This should probably eventually go as a virtual function into
  MY_CHARSET_HANDLER or MY_COLLATION_HANDLER.
*/
static void
my_string_metadata_get_mb(MY_STRING_METADATA *metadata,
                          CHARSET_INFO *cs, const char *str, ulong length)
{
  const char *strend= str + length;
  for (my_string_metadata_init(metadata) ;
       str < strend;
       metadata->char_length++)
  {
    my_wc_t wc;
    int mblen= my_ci_mb_wc(cs, &wc, (const uchar *) str, (const uchar *) strend);
    if (mblen > 0) /* Assigned character */
    {
      if (wc > 0x7F)
        metadata->repertoire|= MY_REPERTOIRE_EXTENDED;
      str+= mblen;
    }
    else if (mblen == MY_CS_ILSEQ) /* Bad byte sequence */
    {
      metadata->repertoire|= MY_REPERTOIRE_EXTENDED;
      str++;
    }
    else if (mblen > MY_CS_TOOSMALL) /* Unassigned character */ 
    {
      metadata->repertoire|= MY_REPERTOIRE_EXTENDED;
      str+= (-mblen);
    }
    else /* Incomplete character, premature end-of-line */
    {
      metadata->repertoire|= MY_REPERTOIRE_EXTENDED; /* Just in case */
      break;
    }
  }
}


/**
  Collect string metadata: length in characters and repertoire.
*/
void
my_string_metadata_get(MY_STRING_METADATA *metadata,
                       CHARSET_INFO *cs, const char *str, size_t length)
{
  if (cs->mbmaxlen == 1 && !(cs->state & MY_CS_NONASCII))
  {
    metadata->char_length= length;
    metadata->repertoire= my_string_repertoire_8bit(cs, str, (ulong)length);
  }
  else
  {
    my_string_metadata_get_mb(metadata, cs, str, (ulong)length);
  }
}


/*
  Check repertoire: detect pure ascii strings
*/
my_repertoire_t
my_string_repertoire(CHARSET_INFO *cs, const char *str, size_t length)
{
  if (cs->mbminlen == 1 && !(cs->state & MY_CS_NONASCII))
  {
    return my_string_repertoire_8bit(cs, str, length);
  }
  else
  {
    const char *strend= str + length;
    my_wc_t wc;
    int chlen;
    for (;
         (chlen= my_ci_mb_wc(cs, &wc, (uchar*) str, (uchar*) strend)) > 0;
         str+= chlen)
    {
      if (wc > 0x7F)
        return MY_REPERTOIRE_UNICODE30;
    }
  }
  return MY_REPERTOIRE_ASCII;
}


/*
  Returns repertoire for charset
*/
my_repertoire_t my_charset_repertoire(CHARSET_INFO *cs)
{
  return cs->state & MY_CS_PUREASCII ?
    MY_REPERTOIRE_ASCII : MY_REPERTOIRE_UNICODE30;
}


/*
  Detect whether a character set is ASCII compatible.

  Returns TRUE for:
  
  - all 8bit character sets whose Unicode mapping of 0x7B is '{'
    (ignores swe7 which maps 0x7B to "LATIN LETTER A WITH DIAERESIS")
  
  - all multi-byte character sets having mbminlen == 1
    (ignores ucs2 whose mbminlen is 2)
  
  TODO:
  
  When merging to 5.2, this function should be changed
  to check a new flag MY_CS_NONASCII, 
  
     return (cs->flag & MY_CS_NONASCII) ? 0 : 1;
  
  This flag was previously added into 5.2 under terms
  of WL#3759 "Optimize identifier conversion in client-server protocol"
  especially to mark character sets not compatible with ASCII.
  
  We won't backport this flag to 5.0 or 5.1.
  This function is Ok for 5.0 and 5.1, because we're not going
  to introduce new tricky character sets between 5.0 and 5.2.
*/
my_bool
my_charset_is_ascii_based(CHARSET_INFO *cs)
{
  return 
    (cs->mbmaxlen == 1 && cs->tab_to_uni && cs->tab_to_uni['{'] == '{') ||
    (cs->mbminlen == 1 && cs->mbmaxlen > 1);
}


/**
  Detect if a Unicode code point is printable.
*/
static inline my_bool
my_is_printable(my_wc_t wc)
{
  /*
    Blocks:
      U+0000 .. U+001F     control
      U+0020 .. U+007E     printable
      U+007F .. U+009F     control
      U+00A0 .. U+00FF     printable
      U+0100 .. U+10FFFF   As of Unicode-6.1.0, this range does not have any
                           characters of the "Cc" (Other, control) category.
                           Should be mostly safe to print.
                           Except for the surrogate halfs,
                           which are encoding components, not real characters.
  */
  if (wc >= 0x20 && wc <= 0x7E) /* Quickly detect ASCII printable */
    return TRUE;
  if (wc <= 0x9F)    /* The rest of U+0000..U+009F are control characters */
  {
    /* NL, CR, TAB are Ok */
    return (wc == '\r' || wc == '\n' || wc == '\t');
  }
  /*
    Surrogate halfs (when alone) print badly in terminals:
      SELECT _ucs2 0xD800;
    Let's escape them as well.
  */
  if (wc >= 0xD800 && wc <= 0xDFFF)
    return FALSE;
  return TRUE;
}


static uint to_printable_8bit(uchar *dst, my_wc_t wc, uint bs)
{
  /*
    This function is used only in context of error messages for now.
    All non-BMP characters are currently replaced to question marks
    when a message is put into diagnostics area.
  */
  DBUG_ASSERT(wc < 0x10000);
  *dst++= (char) bs;
  *dst++= _dig_vec_upper[(wc >> 12) & 0x0F];
  *dst++= _dig_vec_upper[(wc >> 8) & 0x0F];
  *dst++= _dig_vec_upper[(wc >> 4) & 0x0F];
  *dst++= _dig_vec_upper[wc & 0x0F];
  return MY_CS_PRINTABLE_CHAR_LENGTH;
}


static uint my_printable_length(uint bslen, uint diglen)
{
  return bslen + (MY_CS_PRINTABLE_CHAR_LENGTH - 1) * diglen;
}


/**
  Encode an Unicode character "wc" into a printable string.
  This function is suitable for any character set, including
  ASCII-incompatible multi-byte character sets, e.g. ucs2, utf16, utf32.
*/
int
my_wc_to_printable_ex(CHARSET_INFO *cs, my_wc_t wc,
                      uchar *str, uchar *end,
                      uint bs, uint bslen, uint diglen)
{
  uchar *str0;
  uint i, length;
  uchar tmp[MY_CS_PRINTABLE_CHAR_LENGTH * MY_CS_MBMAXLEN];

  if (my_is_printable(wc))
  {
    int mblen= my_ci_wc_mb(cs, wc, str, end);
    if (mblen > 0)
      return mblen;
  }

  if (str + my_printable_length(bslen, diglen) > end)
    return MY_CS_TOOSMALLN(my_printable_length(bslen, diglen));

  if ((cs->state & MY_CS_NONASCII) == 0)
    return to_printable_8bit(str, wc, bs);

  length= to_printable_8bit(tmp, wc, bs);
  str0= str;
  for (i= 0; i < length; i++)
  {
    uint expected_length= i == 0 ? bslen : diglen;
    if (my_ci_wc_mb(cs, tmp[i], str, end) != (int) expected_length)
    {
      DBUG_ASSERT(0);
      return MY_CS_ILSEQ;
    }
    str+= expected_length;
  }
  return (int) (str - str0);
}


int
my_wc_to_printable_8bit(CHARSET_INFO *cs, my_wc_t wc,
                        uchar *str, uchar *end)
{
  /*
    Special case: swe7 does not have the backslash character.
    Use dot instead of backslash for escaping.
  */
  uint bs= cs->tab_to_uni && cs->tab_to_uni['\\'] != '\\' ? '.' : '\\';
  DBUG_ASSERT(cs->mbminlen == 1);
  /*
    Additionally, if the original swe7 string contains backslashes,
    replace them to dots, so this error message:
      Invalid swe7 character string: '\xEF\xBC\xB4'
    is displayed as:
      Invalid swe7 character string: '.xEF.xBC.xB4'
    which is more readable than what would happen without '\'-to-dot mapping:
      Invalid swe7 character string: '.005CxEF.005CxBC.005CxB4'
  */
  if (bs == '.' && wc == '\\')
    wc= '.';
  return my_wc_to_printable_ex(cs, wc, str, end, bs, 1, 1);
}


int
my_wc_to_printable_generic(CHARSET_INFO *cs, my_wc_t wc,
                           uchar *str, uchar *end)
{
  return my_wc_to_printable_ex(cs, wc, str, end, '\\',
                               cs->mbminlen, cs->mbminlen);
}


/*
  Convert a string between two character sets.
  'to' must be large enough to store (form_length * to_cs->mbmaxlen) bytes.

  @param  to[OUT]       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  errors[OUT]   Number of conversion errors

  @return Number of bytes copied to 'to' string
*/

uint32
my_convert_using_func(char *to, size_t to_length,
                      CHARSET_INFO *to_cs, my_charset_conv_wc_mb wc_mb,
                      const char *from, size_t from_length,
                      CHARSET_INFO *from_cs, my_charset_conv_mb_wc mb_wc,
                      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;
  uint error_count= 0;

  while (1)
  {
    if ((cnvres= (*mb_wc)(from_cs, &wc, (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
    {
      if ((uchar *) from >= from_end)
        break;  /* End of line */
      /* Incomplete byte sequence */
      error_count++;
      from++;
      wc= '?';
    }

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  to[OUT]       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  errors[OUT]   Number of conversion errors

  @return Number of bytes copied to 'to' string
*/

uint32
my_convert(char *to, uint32 to_length, CHARSET_INFO *to_cs,
           const char *from, uint32 from_length,
           CHARSET_INFO *from_cs, uint *errors)
{
  uint32 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_using_func(to, to_length,
                                 to_cs, to_cs->cset->wc_mb,
                                 from, from_length,
                                 from_cs, from_cs->cset->mb_wc,
                                 errors);

  length= length2= MY_MIN(to_length, from_length);

#if defined(__i386__) || 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 ((*(uint32*)from) & 0x80808080)
      break;
    *((uint32*) to)= *((const uint32*) from);
  }
#endif /* __i386__ */

  for (; ; *to++= *from++, length--)
  {
    if (!length)
    {
      *errors= 0;
      return length2;
    }
    if (*((unsigned char*) from) > 0x7F) /* A non-ASCII character */
    {
      uint32 copied_length= length2 - length;
      to_length-= copied_length;
      from_length-= copied_length;
      return copied_length + my_convert_using_func(to, to_length, to_cs,
                                                   to_cs->cset->wc_mb,
                                                   from, from_length, from_cs,
                                                   from_cs->cset->mb_wc,
                                                   errors);
    }
  }

  DBUG_ASSERT(FALSE); // Should never get to here
  return 0;           // Make compiler happy
}


size_t
my_convert_fix(CHARSET_INFO *to_cs, char *to, size_t to_length,
               CHARSET_INFO *from_cs, const char *from, size_t from_length,
               size_t nchars,
               MY_STRCOPY_STATUS *copy_status,
               MY_STRCONV_STATUS *conv_status)
{
  int cnvres;
  my_wc_t wc;
  my_charset_conv_mb_wc mb_wc= from_cs->cset->mb_wc;
  my_charset_conv_wc_mb wc_mb= to_cs->cset->wc_mb;
  const uchar *from_end= (const uchar*) from + from_length;
  uchar *to_end= (uchar*) to + to_length;
  char *to_start= to;

  DBUG_ASSERT(to_cs != &my_charset_bin);
  DBUG_ASSERT(from_cs != &my_charset_bin);

  copy_status->m_well_formed_error_pos= NULL;
  conv_status->m_cannot_convert_error_pos= NULL;

  for ( ; nchars; nchars--)
  {
    const char *from_prev= from;
    if ((cnvres= (*mb_wc)(from_cs, &wc, (uchar*) from, from_end)) > 0)
      from+= cnvres;
    else if (cnvres == MY_CS_ILSEQ)
    {
      if (!copy_status->m_well_formed_error_pos)
        copy_status->m_well_formed_error_pos= from;
      from++;
      wc= '?';
    }
    else if (cnvres > MY_CS_TOOSMALL)
    {
      /*
        A correct multibyte sequence detected
        But it doesn't have Unicode mapping.
      */
      if (!conv_status->m_cannot_convert_error_pos)
        conv_status->m_cannot_convert_error_pos= from;
      from+= (-cnvres);
      wc= '?';
    }
    else
    {
      if ((uchar *) from >= from_end)
        break; // End of line
      // Incomplete byte sequence
      if (!copy_status->m_well_formed_error_pos)
        copy_status->m_well_formed_error_pos= from;
      from++;
      wc= '?';
    }
outp:
    if ((cnvres= (*wc_mb)(to_cs, wc, (uchar*) to, to_end)) > 0)
      to+= cnvres;
    else if (cnvres == MY_CS_ILUNI && wc != '?')
    {
      if (!conv_status->m_cannot_convert_error_pos)
        conv_status->m_cannot_convert_error_pos= from_prev;
      wc= '?';
      goto outp;
    }
    else
    {
      from= from_prev;
      break;
    }
  }
  copy_status->m_source_end_pos= from;
  return to - to_start;
}