1 /* Copyright (c) 2000, 2013, Oracle and/or its affiliates.
2    Copyright (c) 2009, 2020, MariaDB Corporation.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; version 2 of the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335  USA */
16 
17 /*
18   A better inplementation of the UNIX ctype(3) library.
19 */
20 
21 #ifndef _m_ctype_h
22 #define _m_ctype_h
23 
24 #include <my_attribute.h>
25 
26 enum loglevel {
27    ERROR_LEVEL=       0,
28    WARNING_LEVEL=     1,
29    INFORMATION_LEVEL= 2
30 };
31 
32 #ifdef	__cplusplus
33 extern "C" {
34 #endif
35 
36 #define MY_CS_NAME_SIZE			32
37 #define MY_CS_CTYPE_TABLE_SIZE		257
38 #define MY_CS_TO_LOWER_TABLE_SIZE	256
39 #define MY_CS_TO_UPPER_TABLE_SIZE	256
40 #define MY_CS_SORT_ORDER_TABLE_SIZE	256
41 #define MY_CS_TO_UNI_TABLE_SIZE		256
42 
43 #define CHARSET_DIR	"charsets/"
44 
45 #define my_wc_t ulong
46 
47 #define MY_CS_REPLACEMENT_CHARACTER 0xFFFD
48 
49 /**
50   Maximum character length of a string produced by wc_to_printable().
51   Note, wc_to_printable() is currently limited to BMP.
52   One non-printable or non-convertable character can produce a string
53   with at most 5 characters: \hhhh.
54   If we ever modify wc_to_printable() to support supplementary characters,
55   e.g. \+hhhhhh, this constant should be changed to 8.
56   Note, maximum octet length of a wc_to_printable() result can be calculated
57   as: (MY_CS_PRINTABLE_CHAR_LENGTH*cs->mbminlen).
58 */
59 #define MY_CS_PRINTABLE_CHAR_LENGTH  5
60 
61 
62 /*
63   On i386 we store Unicode->CS conversion tables for
64   some character sets using Big-endian order,
65   to copy two bytes at once.
66   This gives some performance improvement.
67 */
68 #ifdef __i386__
69 #define MB2(x)                (((x) >> 8) + (((x) & 0xFF) << 8))
70 #define MY_PUT_MB2(s, code)   { *((uint16*)(s))= (code); }
71 #else
72 #define MB2(x)                (x)
73 #define MY_PUT_MB2(s, code)   { (s)[0]= code >> 8; (s)[1]= code & 0xFF; }
74 #endif
75 
76 typedef const struct my_charset_handler_st MY_CHARSET_HANDLER;
77 typedef const struct my_collation_handler_st MY_COLLATION_HANDLER;
78 
79 typedef const struct unicase_info_st MY_UNICASE_INFO;
80 typedef const struct uni_ctype_st MY_UNI_CTYPE;
81 typedef const struct my_uni_idx_st MY_UNI_IDX;
82 
83 typedef struct unicase_info_char_st
84 {
85   uint32 toupper;
86   uint32 tolower;
87   uint32 sort;
88 } MY_UNICASE_CHARACTER;
89 
90 
91 struct unicase_info_st
92 {
93   my_wc_t maxchar;
94   MY_UNICASE_CHARACTER **page;
95 };
96 
97 
98 extern MY_UNICASE_INFO my_unicase_default;
99 extern MY_UNICASE_INFO my_unicase_turkish;
100 extern MY_UNICASE_INFO my_unicase_mysql500;
101 extern MY_UNICASE_INFO my_unicase_unicode520;
102 
103 #define MY_UCA_MAX_CONTRACTION 6
104 /*
105   The DUCET tables in ctype-uca.c are dumped with a limit of 8 weights
106   per character. cs->strxfrm_multiply is set to 8 for all UCA based collations.
107 
108   In language-specific UCA collations (with tailorings) we also do not allow
109   a single character to have more than 8 weights to stay with the same
110   strxfrm_multiply limit. Note, contractions are allowed to have twice longer
111   weight strings (up to 16 weights). As a contraction consists of at
112   least 2 characters, this makes sure that strxfrm_multiply ratio of 8
113   is respected.
114 */
115 #define MY_UCA_MAX_WEIGHT_SIZE (8+1)               /* Including 0 terminator */
116 #define MY_UCA_CONTRACTION_MAX_WEIGHT_SIZE (2*8+1) /* Including 0 terminator */
117 #define MY_UCA_WEIGHT_LEVELS   2
118 
119 typedef struct my_contraction_t
120 {
121   my_wc_t ch[MY_UCA_MAX_CONTRACTION];   /* Character sequence              */
122   uint16 weight[MY_UCA_CONTRACTION_MAX_WEIGHT_SIZE];/* Its weight string, 0-terminated */
123   my_bool with_context;
124 } MY_CONTRACTION;
125 
126 
127 typedef struct my_contraction_list_t
128 {
129   size_t nitems;         /* Number of items in the list                  */
130   MY_CONTRACTION *item;  /* List of contractions                         */
131   char *flags;           /* Character flags, e.g. "is contraction head") */
132 } MY_CONTRACTIONS;
133 
134 my_bool my_uca_can_be_contraction_head(const MY_CONTRACTIONS *c, my_wc_t wc);
135 my_bool my_uca_can_be_contraction_tail(const MY_CONTRACTIONS *c, my_wc_t wc);
136 uint16 *my_uca_contraction2_weight(const MY_CONTRACTIONS *c,
137                                    my_wc_t wc1, my_wc_t wc2);
138 
139 
140 /* Collation weights on a single level (e.g. primary, secondary, tertiarty) */
141 typedef struct my_uca_level_info_st
142 {
143   my_wc_t maxchar;
144   uchar   *lengths;
145   uint16  **weights;
146   MY_CONTRACTIONS contractions;
147   uint    levelno;
148 } MY_UCA_WEIGHT_LEVEL;
149 
150 
151 typedef struct uca_info_st
152 {
153   MY_UCA_WEIGHT_LEVEL level[MY_UCA_WEIGHT_LEVELS];
154 
155   /* Logical positions */
156   my_wc_t first_non_ignorable;
157   my_wc_t last_non_ignorable;
158   my_wc_t first_primary_ignorable;
159   my_wc_t last_primary_ignorable;
160   my_wc_t first_secondary_ignorable;
161   my_wc_t last_secondary_ignorable;
162   my_wc_t first_tertiary_ignorable;
163   my_wc_t last_tertiary_ignorable;
164   my_wc_t first_trailing;
165   my_wc_t last_trailing;
166   my_wc_t first_variable;
167   my_wc_t last_variable;
168 
169 } MY_UCA_INFO;
170 
171 
172 
173 extern MY_UCA_INFO my_uca_v400;
174 
175 
176 struct uni_ctype_st
177 {
178   uchar  pctype;
179   const uchar  *ctype;
180 };
181 
182 extern MY_UNI_CTYPE my_uni_ctype[256];
183 
184 /* wm_wc and wc_mb return codes */
185 #define MY_CS_ILSEQ	0     /* Wrong by sequence: wb_wc                   */
186 #define MY_CS_ILUNI	0     /* Cannot encode Unicode to charset: wc_mb    */
187 #define MY_CS_TOOSMALL  -101  /* Need at least one byte:    wc_mb and mb_wc */
188 #define MY_CS_TOOSMALL2 -102  /* Need at least two bytes:   wc_mb and mb_wc */
189 #define MY_CS_TOOSMALL3 -103  /* Need at least three bytes: wc_mb and mb_wc */
190 /* These following three are currently not really used */
191 #define MY_CS_TOOSMALL4 -104  /* Need at least 4 bytes: wc_mb and mb_wc */
192 #define MY_CS_TOOSMALL5 -105  /* Need at least 5 bytes: wc_mb and mb_wc */
193 #define MY_CS_TOOSMALL6 -106  /* Need at least 6 bytes: wc_mb and mb_wc */
194 /* A helper macros for "need at least n bytes" */
195 #define MY_CS_TOOSMALLN(n)    (-100-(n))
196 
197 #define MY_CS_MBMAXLEN  6     /* Maximum supported mbmaxlen */
198 #define MY_CS_IS_TOOSMALL(rc) ((rc) >= MY_CS_TOOSMALL6 && (rc) <= MY_CS_TOOSMALL)
199 
200 #define MY_SEQ_INTTAIL	1
201 #define MY_SEQ_SPACES	2
202 #define MY_SEQ_NONSPACES 3 /* Skip non-space characters, including bad bytes */
203 
204         /* My charsets_list flags */
205 #define MY_CS_COMPILED  1      /* compiled-in sets               */
206 #define MY_CS_CONFIG    2      /* sets that have a *.conf file   */
207 #define MY_CS_INDEX     4      /* sets listed in the Index file  */
208 #define MY_CS_LOADED    8      /* sets that are currently loaded */
209 #define MY_CS_BINSORT	16     /* if binary sort order           */
210 #define MY_CS_PRIMARY	32     /* if primary collation           */
211 #define MY_CS_STRNXFRM	64     /* if strnxfrm is used for sort   */
212 #define MY_CS_UNICODE	128    /* is a charset is BMP Unicode    */
213 #define MY_CS_READY	256    /* if a charset is initialized    */
214 #define MY_CS_AVAILABLE	512    /* If either compiled-in or loaded*/
215 #define MY_CS_CSSORT	1024   /* if case sensitive sort order   */
216 #define MY_CS_HIDDEN	2048   /* don't display in SHOW          */
217 #define MY_CS_PUREASCII 4096   /* if a charset is pure ascii     */
218 #define MY_CS_NONASCII  8192   /* if not ASCII-compatible        */
219 #define MY_CS_UNICODE_SUPPLEMENT 16384 /* Non-BMP Unicode characters */
220 #define MY_CS_LOWER_SORT 32768 /* If use lower case as weight   */
221 #define MY_CS_STRNXFRM_BAD_NWEIGHTS 0x10000 /* strnxfrm ignores "nweights" */
222 #define MY_CS_NOPAD   0x20000  /* if does not ignore trailing spaces */
223 #define MY_CS_NON1TO1 0x40000  /* Has a complex mapping from characters
224                                   to weights, e.g. contractions, expansions,
225                                   ignorable characters */
226 #define MY_CHARSET_UNDEFINED 0
227 
228 /* Character repertoire flags */
229 typedef enum enum_repertoire_t
230 {
231   MY_REPERTOIRE_NONE=        0,
232   MY_REPERTOIRE_ASCII=       1, /* Pure ASCII            U+0000..U+007F */
233   MY_REPERTOIRE_EXTENDED=    2, /* Extended characters:  U+0080..U+FFFF */
234   MY_REPERTOIRE_UNICODE30=   3  /* ASCII | EXTENDED:     U+0000..U+FFFF */
235 } my_repertoire_t;
236 
237 
238 /* Flags for strxfrm */
239 #define MY_STRXFRM_LEVEL1          0x00000001 /* for primary weights   */
240 #define MY_STRXFRM_LEVEL2          0x00000002 /* for secondary weights */
241 #define MY_STRXFRM_LEVEL3          0x00000004 /* for tertiary weights  */
242 #define MY_STRXFRM_LEVEL4          0x00000008 /* fourth level weights  */
243 #define MY_STRXFRM_LEVEL5          0x00000010 /* fifth level weights   */
244 #define MY_STRXFRM_LEVEL6          0x00000020 /* sixth level weights   */
245 #define MY_STRXFRM_LEVEL_ALL       0x0000003F /* Bit OR for the above six */
246 #define MY_STRXFRM_NLEVELS         6          /* Number of possible levels*/
247 
248 #define MY_STRXFRM_PAD_WITH_SPACE  0x00000040 /* if pad result with spaces */
249 #define MY_STRXFRM_PAD_TO_MAXLEN   0x00000080 /* if pad tail(for filesort) */
250 
251 #define MY_STRXFRM_DESC_LEVEL1     0x00000100 /* if desc order for level1 */
252 #define MY_STRXFRM_DESC_LEVEL2     0x00000200 /* if desc order for level2 */
253 #define MY_STRXFRM_DESC_LEVEL3     0x00000300 /* if desc order for level3 */
254 #define MY_STRXFRM_DESC_LEVEL4     0x00000800 /* if desc order for level4 */
255 #define MY_STRXFRM_DESC_LEVEL5     0x00001000 /* if desc order for level5 */
256 #define MY_STRXFRM_DESC_LEVEL6     0x00002000 /* if desc order for level6 */
257 #define MY_STRXFRM_DESC_SHIFT      8
258 
259 #define MY_STRXFRM_UNUSED_00004000 0x00004000 /* for future extensions     */
260 #define MY_STRXFRM_UNUSED_00008000 0x00008000 /* for future extensions     */
261 
262 #define MY_STRXFRM_REVERSE_LEVEL1  0x00010000 /* if reverse order for level1 */
263 #define MY_STRXFRM_REVERSE_LEVEL2  0x00020000 /* if reverse order for level2 */
264 #define MY_STRXFRM_REVERSE_LEVEL3  0x00040000 /* if reverse order for level3 */
265 #define MY_STRXFRM_REVERSE_LEVEL4  0x00080000 /* if reverse order for level4 */
266 #define MY_STRXFRM_REVERSE_LEVEL5  0x00100000 /* if reverse order for level5 */
267 #define MY_STRXFRM_REVERSE_LEVEL6  0x00200000 /* if reverse order for level6 */
268 #define MY_STRXFRM_REVERSE_SHIFT   16
269 
270 /*
271    Collation IDs for MariaDB that should not conflict with MySQL.
272    We reserve 256..511, because MySQL will most likely use this range
273    when the range 0..255 is full.
274 
275    We use the next 256 IDs starting from 512 and divide
276    them into 8 chunks, 32 collations each, as follows:
277 
278    512 + (0..31)    for single byte collations (e.g. latin9)
279    512 + (32..63)   reserved (e.g. for utf32le, or more single byte collations)
280    512 + (64..95)   for utf8
281    512 + (96..127)  for utf8mb4
282    512 + (128..159) for ucs2
283    512 + (160..192) for utf16
284    512 + (192..223) for utf16le
285    512 + (224..255) for utf32
286 */
287 #define MY_PAGE2_COLLATION_ID_8BIT     0x200
288 #define MY_PAGE2_COLLATION_ID_RESERVED 0x220
289 #define MY_PAGE2_COLLATION_ID_UTF8     0x240
290 #define MY_PAGE2_COLLATION_ID_UTF8MB4  0x260
291 #define MY_PAGE2_COLLATION_ID_UCS2     0x280
292 #define MY_PAGE2_COLLATION_ID_UTF16    0x2A0
293 #define MY_PAGE2_COLLATION_ID_UTF16LE  0x2C0
294 #define MY_PAGE2_COLLATION_ID_UTF32    0x2E0
295 
296 struct my_uni_idx_st
297 {
298   uint16 from;
299   uint16 to;
300   const uchar *tab;
301 };
302 
303 typedef struct
304 {
305   uint beg;
306   uint end;
307   uint mb_len;
308 } my_match_t;
309 
310 enum my_lex_states
311 {
312   MY_LEX_START, MY_LEX_CHAR, MY_LEX_IDENT,
313   MY_LEX_IDENT_SEP, MY_LEX_IDENT_START,
314   MY_LEX_REAL, MY_LEX_HEX_NUMBER, MY_LEX_BIN_NUMBER,
315   MY_LEX_CMP_OP, MY_LEX_LONG_CMP_OP, MY_LEX_STRING, MY_LEX_COMMENT, MY_LEX_END,
316   MY_LEX_OPERATOR_OR_IDENT, MY_LEX_NUMBER_IDENT, MY_LEX_INT_OR_REAL,
317   MY_LEX_REAL_OR_POINT, MY_LEX_BOOL, MY_LEX_EOL, MY_LEX_ESCAPE,
318   MY_LEX_LONG_COMMENT, MY_LEX_END_LONG_COMMENT, MY_LEX_SEMICOLON,
319   MY_LEX_SET_VAR, MY_LEX_USER_END, MY_LEX_HOSTNAME, MY_LEX_SKIP,
320   MY_LEX_USER_VARIABLE_DELIMITER, MY_LEX_SYSTEM_VAR,
321   MY_LEX_IDENT_OR_KEYWORD,
322   MY_LEX_IDENT_OR_HEX, MY_LEX_IDENT_OR_BIN, MY_LEX_IDENT_OR_NCHAR,
323   MY_LEX_STRING_OR_DELIMITER, MY_LEX_MINUS_OR_COMMENT, MY_LEX_PLACEHOLDER,
324   MY_LEX_COMMA
325 };
326 
327 struct charset_info_st;
328 
329 typedef struct my_charset_loader_st
330 {
331   char error[128];
332   void *(*once_alloc)(size_t);
333   void *(*malloc)(size_t);
334   void *(*realloc)(void *, size_t);
335   void (*free)(void *);
336   void (*reporter)(enum loglevel, const char *format, ...);
337   int  (*add_collation)(struct charset_info_st *cs);
338 } MY_CHARSET_LOADER;
339 
340 
341 extern int (*my_string_stack_guard)(int);
342 
343 /* See strings/CHARSET_INFO.txt for information about this structure  */
344 struct my_collation_handler_st
345 {
346   my_bool (*init)(struct charset_info_st *, MY_CHARSET_LOADER *);
347   /* Collation routines */
348   int     (*strnncoll)(CHARSET_INFO *,
349 		       const uchar *, size_t, const uchar *, size_t, my_bool);
350   int     (*strnncollsp)(CHARSET_INFO *,
351                          const uchar *, size_t, const uchar *, size_t);
352   /*
353     strnncollsp_nchars() - similar to strnncollsp() but assumes that both
354                            strings were originally CHAR(N) values with the
355                            same N, then were optionally space-padded,
356                            or optionally space-trimmed.
357 
358                            In other words, this function compares in the way
359                            if we insert both values into a CHAR(N) column
360                            and then compare the two column values.
361 
362     It compares the same amount of characters from the two strings.
363     This is especially important for NOPAD collations.
364 
365     If CHAR_LENGTH of the two strings are different,
366     the shorter string is virtually padded with trailing spaces
367     up to CHAR_LENGTH of the longer string, to guarantee that the
368     same amount of characters are compared.
369     This is important if the two CHAR(N) strings are space-trimmed
370     (e.g. like in InnoDB compact format for CHAR).
371 
372     The function compares not more than "nchars" characters only.
373     This can be useful to compare CHAR(N) space-padded strings
374     (when the exact N is known) without having to truncate them before
375     the comparison.
376 
377     For example, Field_string stores a "CHAR(3) CHARACTER SET utf8mb4" value
378     of "aaa" as 12 bytes in a record buffer:
379     - 3 bytes of the actual data, followed by
380     - 9 bytes of spaces (just fillers, not real data)
381     The caller can pass nchars=3 to compare CHAR(3) record values.
382     In such case, the comparator won't go inside the 9 bytes of the fillers.
383 
384     If N is not known, the caller can pass max(len1,len2) as the "nchars" value
385     (i.e. the maximum of the OCTET_LENGTH of the two strings).
386 
387     Notes on complex collations.
388 
389     This function counts contraction parts as individual characters.
390     For example, the Czech letter 'ch' (in Czech collations)
391     is ordinarily counted by the "nchars" limit as TWO characters
392     (although it is only one letter).
393     This corresponds to what CHAR(N) does in INSERT.
394 
395     If the "nchars" limit tears apart a contraction, only the part fitting
396     into "nchars" characters is used. For example, in case of a Czech collation,
397     the string "ach" with nchars=2 is compared as 'ac': the contraction
398     'ch' is torn apart and the letter 'c' acts as an individual character.
399     This emulates the same comparison result with the scenario when we insert
400     'ach' into a CHAR(2) column and then compare it.
401   */
402   int     (*strnncollsp_nchars)(CHARSET_INFO *,
403                                 const uchar *str1, size_t len1,
404                                 const uchar *str2, size_t len2,
405                                 size_t nchars);
406   size_t     (*strnxfrm)(CHARSET_INFO *,
407                          uchar *dst, size_t dstlen, uint nweights,
408                          const uchar *src, size_t srclen, uint flags);
409   size_t    (*strnxfrmlen)(CHARSET_INFO *, size_t);
410   my_bool (*like_range)(CHARSET_INFO *,
411 			const char *s, size_t s_length,
412 			pchar w_prefix, pchar w_one, pchar w_many,
413 			size_t res_length,
414 			char *min_str, char *max_str,
415 			size_t *min_len, size_t *max_len);
416   int     (*wildcmp)(CHARSET_INFO *,
417   		     const char *str,const char *str_end,
418                      const char *wildstr,const char *wildend,
419                      int escape,int w_one, int w_many);
420 
421   int  (*strcasecmp)(CHARSET_INFO *, const char *, const char *);
422 
423   uint (*instr)(CHARSET_INFO *,
424                 const char *b, size_t b_length,
425                 const char *s, size_t s_length,
426                 my_match_t *match, uint nmatch);
427 
428   /* Hash calculation */
429   void (*hash_sort)(CHARSET_INFO *cs, const uchar *key, size_t len,
430 		    ulong *nr1, ulong *nr2);
431   my_bool (*propagate)(CHARSET_INFO *cs, const uchar *str, size_t len);
432 };
433 
434 extern MY_COLLATION_HANDLER my_collation_8bit_bin_handler;
435 extern MY_COLLATION_HANDLER my_collation_8bit_simple_ci_handler;
436 extern MY_COLLATION_HANDLER my_collation_8bit_nopad_bin_handler;
437 extern MY_COLLATION_HANDLER my_collation_8bit_simple_nopad_ci_handler;
438 
439 /* Some typedef to make it easy for C++ to make function pointers */
440 typedef int (*my_charset_conv_mb_wc)(CHARSET_INFO *, my_wc_t *,
441                                      const uchar *, const uchar *);
442 typedef int (*my_charset_conv_wc_mb)(CHARSET_INFO *, my_wc_t,
443                                      uchar *, uchar *);
444 typedef size_t (*my_charset_conv_case)(CHARSET_INFO *,
445                                        const char *, size_t, char *, size_t);
446 
447 /*
448   A structure to return the statistics of a native string copying,
449   when no Unicode conversion is involved.
450 
451   The structure is OK to be uninitialized before calling a copying routine.
452   A copying routine must populate the structure as follows:
453     - m_source_end_pos must be set by to a non-NULL value
454       in the range of the input string.
455     - m_well_formed_error_pos must be set to NULL if the string was
456       well formed, or to the position of the leftmost bad byte sequence.
457 */
458 typedef struct
459 {
460   const char *m_source_end_pos;        /* Position where reading stopped */
461   const char *m_well_formed_error_pos; /* Position where a bad byte was found*/
462 } MY_STRCOPY_STATUS;
463 
464 
465 /*
466   A structure to return the statistics of a Unicode string conversion.
467 */
468 typedef struct
469 {
470   const char *m_cannot_convert_error_pos;
471 } MY_STRCONV_STATUS;
472 
473 
474 /* See strings/CHARSET_INFO.txt about information on this structure  */
475 struct my_charset_handler_st
476 {
477   my_bool (*init)(struct charset_info_st *, MY_CHARSET_LOADER *loader);
478   /* Multibyte routines */
479   size_t  (*numchars)(CHARSET_INFO *, const char *b, const char *e);
480   size_t  (*charpos)(CHARSET_INFO *, const char *b, const char *e,
481                      size_t pos);
482   size_t  (*lengthsp)(CHARSET_INFO *, const char *ptr, size_t length);
483   size_t  (*numcells)(CHARSET_INFO *, const char *b, const char *e);
484 
485   /* Unicode conversion */
486   my_charset_conv_mb_wc mb_wc;
487   my_charset_conv_wc_mb wc_mb;
488 
489   /* CTYPE scanner */
490   int (*ctype)(CHARSET_INFO *cs, int *ctype,
491                const uchar *s, const uchar *e);
492 
493   /* Functions for case and sort conversion */
494   size_t  (*caseup_str)(CHARSET_INFO *, char *);
495   size_t  (*casedn_str)(CHARSET_INFO *, char *);
496 
497   my_charset_conv_case caseup;
498   my_charset_conv_case casedn;
499 
500   /* Charset dependent snprintf() */
501   size_t (*snprintf)(CHARSET_INFO *, char *to, size_t n,
502                      const char *fmt,
503                      ...) ATTRIBUTE_FORMAT_FPTR(printf, 4, 5);
504   size_t (*long10_to_str)(CHARSET_INFO *, char *to, size_t n,
505                           int radix, long int val);
506   size_t (*longlong10_to_str)(CHARSET_INFO *, char *to, size_t n,
507                               int radix, longlong val);
508 
509   void (*fill)(CHARSET_INFO *, char *to, size_t len, int fill);
510 
511   /* String-to-number conversion routines */
512   long        (*strntol)(CHARSET_INFO *, const char *s, size_t l,
513 			 int base, char **e, int *err);
514   ulong      (*strntoul)(CHARSET_INFO *, const char *s, size_t l,
515 			 int base, char **e, int *err);
516   longlong   (*strntoll)(CHARSET_INFO *, const char *s, size_t l,
517 			 int base, char **e, int *err);
518   ulonglong (*strntoull)(CHARSET_INFO *, const char *s, size_t l,
519 			 int base, char **e, int *err);
520   double      (*strntod)(CHARSET_INFO *, char *s, size_t l, char **e,
521 			 int *err);
522   longlong    (*strtoll10)(CHARSET_INFO *cs,
523                            const char *nptr, char **endptr, int *error);
524   ulonglong   (*strntoull10rnd)(CHARSET_INFO *cs,
525                                 const char *str, size_t length,
526                                 int unsigned_fl,
527                                 char **endptr, int *error);
528   size_t        (*scan)(CHARSET_INFO *, const char *b, const char *e,
529                         int sq);
530 
531   /* String copying routines and helpers for them */
532   /*
533     charlen() - calculate length of the left-most character in bytes.
534     @param  cs    Character set
535     @param  str   The beginning of the string
536     @param  end   The end of the string
537 
538     @return       MY_CS_ILSEQ if a bad byte sequence was found.
539     @return       MY_CS_TOOSMALLN(x) if the string ended unexpectedly.
540     @return       a positive number in the range 1..mbmaxlen,
541                   if a valid character was found.
542   */
543   int (*charlen)(CHARSET_INFO *cs, const uchar *str, const uchar *end);
544   /*
545     well_formed_char_length() - returns character length of a string.
546 
547     @param cs          Character set
548     @param str         The beginning of the string
549     @param end         The end of the string
550     @param nchars      Not more than "nchars" left-most characters are checked.
551     @param status[OUT] Additional statistics is returned here.
552                        "status" can be uninitialized before the call,
553                        and it is fully initialized after the call.
554 
555     status->m_source_end_pos is set to the position where reading stopped.
556 
557     If a bad byte sequence is found, the function returns immediately and
558     status->m_well_formed_error_pos is set to the position where a bad byte
559     sequence was found.
560 
561     status->m_well_formed_error_pos is set to NULL if no bad bytes were found.
562     If status->m_well_formed_error_pos is NULL after the call, that means:
563     - either the function reached the end of the string,
564     - or all "nchars" characters were read.
565     The caller can check status->m_source_end_pos to detect which of these two
566     happened.
567   */
568   size_t (*well_formed_char_length)(CHARSET_INFO *cs,
569                                     const char *str, const char *end,
570                                     size_t nchars,
571                                     MY_STRCOPY_STATUS *status);
572 
573   /*
574     copy_fix() - copy a string, replace bad bytes to '?'.
575     Not more than "nchars" characters are copied.
576 
577     status->m_source_end_pos is set to a position in the range
578     between "src" and "src + src_length", where reading stopped.
579 
580     status->m_well_formed_error_pos is set to NULL if the string
581     in the range "src" and "status->m_source_end_pos" was well formed,
582     or is set to a position between "src" and "src + src_length" where
583     the leftmost bad byte sequence was found.
584   */
585   size_t  (*copy_fix)(CHARSET_INFO *,
586                       char *dst, size_t dst_length,
587                       const char *src, size_t src_length,
588                       size_t nchars, MY_STRCOPY_STATUS *status);
589   /**
590     Write a character to the target string, using its native code.
591     For Unicode character sets (utf8, ucs2, utf16, utf16le, utf32, filename)
592     native codes are equivalent to Unicode code points.
593     For 8bit character sets the native code is just the byte value.
594     For Asian characters sets:
595     - MB1 native code is just the byte value (e.g. on the ASCII range)
596     - MB2 native code is ((b0 << 8) + b1).
597     - MB3 native code is ((b0 <<16) + (b1 << 8) + b2)
598     Note, CHARSET_INFO::min_sort_char and CHARSET_INFO::max_sort_char
599     are defined in native notation and should be written using
600     my_ci_native_to_mb() rather than my_ci_wc_mb().
601   */
602   my_charset_conv_wc_mb native_to_mb;
603   my_charset_conv_wc_mb wc_to_printable;
604 };
605 
606 extern MY_CHARSET_HANDLER my_charset_8bit_handler;
607 extern MY_CHARSET_HANDLER my_charset_ucs2_handler;
608 extern MY_CHARSET_HANDLER my_charset_utf8mb3_handler;
609 
610 
611 /*
612   We define this CHARSET_INFO_DEFINED here to prevent a repeat of the
613   typedef in hash.c, which will cause a compiler error.
614 */
615 #define CHARSET_INFO_DEFINED
616 
617 
618 /* See strings/CHARSET_INFO.txt about information on this structure  */
619 struct charset_info_st
620 {
621   uint      number;
622   uint      primary_number;
623   uint      binary_number;
624   uint      state;
625   const char *csname;
626   const char *name;
627   const char *comment;
628   const char *tailoring;
629   const uchar *m_ctype;
630   const uchar *to_lower;
631   const uchar *to_upper;
632   const uchar *sort_order;
633   MY_UCA_INFO *uca;
634   const uint16 *tab_to_uni;
635   MY_UNI_IDX  *tab_from_uni;
636   MY_UNICASE_INFO *caseinfo;
637   const uchar  *state_map;
638   const uchar  *ident_map;
639   uint      strxfrm_multiply;
640   uchar     caseup_multiply;
641   uchar     casedn_multiply;
642   uint      mbminlen;
643   uint      mbmaxlen;
644   my_wc_t   min_sort_char;
645   my_wc_t   max_sort_char; /* For LIKE optimization */
646   uchar     pad_char;
647   my_bool   escape_with_backslash_is_dangerous;
648   uchar     levels_for_order;
649 
650   MY_CHARSET_HANDLER *cset;
651   MY_COLLATION_HANDLER *coll;
652 
653 #ifdef __cplusplus
654   /* Character set routines */
use_mbcharset_info_st655   bool use_mb() const
656   {
657     return mbmaxlen > 1;
658   }
659 
numcharscharset_info_st660   size_t numchars(const char *b, const char *e) const
661   {
662     return (cset->numchars)(this, b, e);
663   }
664 
charposcharset_info_st665   size_t charpos(const char *b, const char *e, size_t pos) const
666   {
667     return (cset->charpos)(this, b, e, pos);
668   }
charposcharset_info_st669   size_t charpos(const uchar *b, const uchar *e, size_t pos) const
670   {
671     return (cset->charpos)(this, (const char *) b, (const char*) e, pos);
672   }
673 
lengthspcharset_info_st674   size_t lengthsp(const char *str, size_t length) const
675   {
676     return (cset->lengthsp)(this, str, length);
677   }
678 
numcellscharset_info_st679   size_t numcells(const char *b, const char *e) const
680   {
681     return (cset->numcells)(this, b, e);
682   }
683 
caseupcharset_info_st684   size_t caseup(const char *src, size_t srclen,
685                 char *dst, size_t dstlen) const
686   {
687     return (cset->caseup)(this, src, srclen, dst, dstlen);
688   }
689 
casedncharset_info_st690   size_t casedn(const char *src, size_t srclen,
691                 char *dst, size_t dstlen) const
692   {
693     return (cset->casedn)(this, src, srclen, dst, dstlen);
694   }
695 
long10_to_strcharset_info_st696   size_t long10_to_str(char *dst, size_t dstlen,
697                        int radix, long int val) const
698   {
699     return (cset->long10_to_str)(this, dst, dstlen, radix, val);
700   }
701 
size_tcharset_info_st702   size_t (longlong10_to_str)(char *dst, size_t dstlen,
703                              int radix, longlong val) const
704   {
705     return (cset->longlong10_to_str)(this, dst, dstlen, radix, val);
706   }
707 
mb_wccharset_info_st708   int mb_wc(my_wc_t *wc, const uchar *b, const uchar *e) const
709   {
710     return (cset->mb_wc)(this, wc, b, e);
711   }
712 
wc_mbcharset_info_st713   int wc_mb(my_wc_t wc, uchar *s, uchar *e) const
714   {
715     return (cset->wc_mb)(this, wc, s, e);
716   }
717 
native_to_mbcharset_info_st718   int native_to_mb(my_wc_t wc, uchar *s, uchar *e) const
719   {
720     return (cset->native_to_mb)(this, wc, s, e);
721   }
722 
wc_to_printablecharset_info_st723   int wc_to_printable(my_wc_t wc, uchar *s, uchar *e) const
724   {
725     return (cset->wc_to_printable)(this, wc, s, e);
726   }
727 
ctypecharset_info_st728   int ctype(int *to, const uchar *s, const uchar *e) const
729   {
730     return (cset->ctype)(this, to, s, e);
731   }
732 
fillcharset_info_st733   void fill(char *to, size_t len, int ch) const
734   {
735     (cset->fill)(this, to, len, ch);
736   }
737 
strntolcharset_info_st738   long strntol(const char *str, size_t length,
739                int base, char **endptr, int *error) const
740   {
741     return (cset->strntol)(this, str, length, base, endptr, error);
742   }
743 
strntoulcharset_info_st744   ulong strntoul(const char *str, size_t length,
745                  int base, char **endptr, int *error) const
746   {
747     return (cset->strntoul)(this, str, length, base, endptr, error);
748   }
749 
strntollcharset_info_st750   longlong strntoll(const char *str, size_t length,
751                     int base, char **endptr, int *error) const
752   {
753     return (cset->strntoll)(this, str, length, base, endptr, error);
754   }
755 
strntoullcharset_info_st756   ulonglong strntoull(const char *str, size_t length,
757                       int base, char **endptr, int *error) const
758   {
759     return (cset->strntoull)(this, str, length, base, endptr, error);
760   }
761 
strntodcharset_info_st762   double strntod(char *str, size_t length,
763                  char **endptr, int *error) const
764   {
765     return (cset->strntod)(this, str, length, endptr, error);
766   }
767 
strtoll10charset_info_st768   longlong strtoll10(const char *str, char **endptr, int *error) const
769   {
770     return (cset->strtoll10)(this, str, endptr, error);
771   }
772 
strntoull10rndcharset_info_st773   ulonglong strntoull10rnd(const char *str, size_t length, int unsigned_fl,
774                            char **endptr, int *error) const
775   {
776     return (cset->strntoull10rnd)(this, str, length, unsigned_fl, endptr, error);
777   }
778 
scancharset_info_st779   size_t scan(const char *b, const char *e, int seq) const
780   {
781     return (cset->scan)(this, b, e, seq);
782   }
783 
charlencharset_info_st784   int charlen(const uchar *str, const uchar *end) const
785   {
786     return (cset->charlen)(this, str, end);
787   }
charlencharset_info_st788   int charlen(const char *str, const char *end) const
789   {
790     return (cset->charlen)(this, (const uchar *) str, (const uchar *) end);
791   }
792 
charlen_fixcharset_info_st793   uint charlen_fix(const uchar *str, const uchar *end) const
794   {
795     int char_length= (cset->charlen)(this, str, end);
796     DBUG_ASSERT(str < end);
797     return char_length > 0 ? (uint) char_length : (uint) 1U;
798   }
charlen_fixcharset_info_st799   uint charlen_fix(const char *str, const char *end) const
800   {
801     return charlen_fix((const uchar *) str, (const uchar *) end);
802   }
803 
well_formed_char_lengthcharset_info_st804   size_t well_formed_char_length(const char *str, const char *end,
805                                  size_t nchars,
806                                  MY_STRCOPY_STATUS *status) const
807   {
808     return (cset->well_formed_char_length)(this, str, end, nchars, status);
809   }
810 
copy_fixcharset_info_st811   size_t copy_fix(char *dst, size_t dst_length,
812                   const char *src, size_t src_length,
813                   size_t nchars, MY_STRCOPY_STATUS *status) const
814   {
815     return (cset->copy_fix)(this, dst, dst_length, src, src_length, nchars,
816                           status);
817   }
818 
819   /* Collation routines */
820   int strnncoll(const uchar *a, size_t alen,
821                 const uchar *b, size_t blen, my_bool b_is_prefix= FALSE) const
822   {
823     return (coll->strnncoll)(this, a, alen, b, blen, b_is_prefix);
824   }
825   int strnncoll(const char *a, size_t alen,
826                 const char *b, size_t blen, my_bool b_is_prefix= FALSE) const
827   {
828     return (coll->strnncoll)(this,
829                              (const uchar *) a, alen,
830                              (const uchar *) b, blen, b_is_prefix);
831   }
832 
strnncollspcharset_info_st833   int strnncollsp(const uchar *a, size_t alen,
834                   const uchar *b, size_t blen) const
835   {
836     return (coll->strnncollsp)(this, a, alen, b, blen);
837   }
strnncollspcharset_info_st838   int strnncollsp(const char *a, size_t alen,
839                   const char *b, size_t blen) const
840   {
841     return (coll->strnncollsp)(this, (uchar *) a, alen, (uchar *) b, blen);
842   }
843 
strnxfrmcharset_info_st844   size_t strnxfrm(char *dst, size_t dstlen, uint nweights,
845                   const char *src, size_t srclen, uint flags) const
846   {
847     return (coll->strnxfrm)(this,
848                             (uchar *) dst, dstlen, nweights,
849                             (const uchar *) src, srclen, flags);
850   }
strnxfrmcharset_info_st851   size_t strnxfrm(uchar *dst, size_t dstlen, uint nweights,
852                   const uchar *src, size_t srclen, uint flags) const
853   {
854     return (coll->strnxfrm)(this,
855                             dst, dstlen, nweights,
856                             src, srclen, flags);
857   }
strnxfrmcharset_info_st858   size_t strnxfrm(uchar *dst, size_t dstlen,
859                   const uchar *src, size_t srclen) const
860   {
861     return (coll->strnxfrm)(this,
862                             dst, dstlen, (uint) dstlen,
863                             src, srclen, MY_STRXFRM_PAD_WITH_SPACE);
864   }
865 
strnxfrmlencharset_info_st866   size_t strnxfrmlen(size_t length) const
867   {
868     return (coll->strnxfrmlen)(this, length);
869   }
870 
like_rangecharset_info_st871   my_bool like_range(const char *s, size_t s_length,
872                      pchar w_prefix, pchar w_one, pchar w_many,
873                      size_t res_length,
874                      char *min_str, char *max_str,
875                      size_t *min_len, size_t *max_len) const
876   {
877     return (coll->like_range)(this, s, s_length,
878                               w_prefix, w_one, w_many,
879                               res_length, min_str, max_str,
880                               min_len, max_len);
881   }
882 
wildcmpcharset_info_st883   int wildcmp(const char *str,const char *str_end,
884               const char *wildstr,const char *wildend,
885               int escape,int w_one, int w_many) const
886   {
887     return (coll->wildcmp)(this, str, str_end, wildstr, wildend, escape, w_one, w_many);
888   }
889 
instrcharset_info_st890   uint instr(const char *b, size_t b_length,
891              const char *s, size_t s_length,
892              my_match_t *match, uint nmatch) const
893   {
894     return (coll->instr)(this, b, b_length, s, s_length, match, nmatch);
895   }
896 
hash_sortcharset_info_st897   void hash_sort(const uchar *key, size_t len, ulong *nr1, ulong *nr2) const
898   {
899     (coll->hash_sort)(this, key, len, nr1, nr2);
900   }
901 
propagatecharset_info_st902   my_bool propagate(const uchar *str, size_t len) const
903   {
904     return (coll->propagate)(this, str, len);
905   }
906 
907 #endif /* __cplusplus */
908 };
909 
910 
911 /* Character set routines */
912 
913 static inline my_bool
my_ci_init_charset(struct charset_info_st * ci,MY_CHARSET_LOADER * loader)914 my_ci_init_charset(struct charset_info_st *ci, MY_CHARSET_LOADER *loader)
915 {
916   if (!ci->cset->init)
917     return FALSE;
918   return (ci->cset->init)(ci, loader);
919 }
920 
921 
922 static inline my_bool
my_ci_use_mb(CHARSET_INFO * ci)923 my_ci_use_mb(CHARSET_INFO *ci)
924 {
925   return ci->mbmaxlen > 1 ? TRUE : FALSE;
926 }
927 
928 static inline size_t
my_ci_numchars(CHARSET_INFO * cs,const char * b,const char * e)929 my_ci_numchars(CHARSET_INFO *cs, const char *b, const char *e)
930 {
931   return (cs->cset->numchars)(cs, b, e);
932 }
933 
934 static inline size_t
my_ci_charpos(CHARSET_INFO * cs,const char * b,const char * e,size_t pos)935 my_ci_charpos(CHARSET_INFO *cs, const char *b, const char *e, size_t pos)
936 {
937   return (cs->cset->charpos)(cs, b, e, pos);
938 }
939 
940 static inline size_t
my_ci_lengthsp(CHARSET_INFO * cs,const char * str,size_t length)941 my_ci_lengthsp(CHARSET_INFO *cs, const char *str, size_t length)
942 {
943   return (cs->cset->lengthsp)(cs, str, length);
944 }
945 
946 static inline size_t
my_ci_numcells(CHARSET_INFO * cs,const char * b,const char * e)947 my_ci_numcells(CHARSET_INFO *cs, const char *b, const char *e)
948 {
949   return (cs->cset->numcells)(cs, b, e);
950 }
951 
952 static inline size_t
my_ci_caseup(CHARSET_INFO * ci,const char * src,size_t srclen,char * dst,size_t dstlen)953 my_ci_caseup(CHARSET_INFO *ci,
954              const char *src, size_t srclen,
955              char *dst, size_t dstlen)
956 {
957   return (ci->cset->caseup)(ci, src, srclen, dst, dstlen);
958 }
959 
960 static inline size_t
my_ci_casedn(CHARSET_INFO * ci,const char * src,size_t srclen,char * dst,size_t dstlen)961 my_ci_casedn(CHARSET_INFO *ci,
962              const char *src, size_t srclen,
963              char *dst, size_t dstlen)
964 {
965   return (ci->cset->casedn)(ci, src, srclen, dst, dstlen);
966 }
967 
968 static inline size_t
my_ci_long10_to_str(CHARSET_INFO * cs,char * dst,size_t dstlen,int radix,long int val)969 my_ci_long10_to_str(CHARSET_INFO *cs, char *dst, size_t dstlen,
970                     int radix, long int val)
971 {
972   return (cs->cset->long10_to_str)(cs, dst, dstlen, radix, val);
973 }
974 
975 static inline size_t
my_ci_longlong10_to_str(CHARSET_INFO * cs,char * dst,size_t dstlen,int radix,longlong val)976 my_ci_longlong10_to_str(CHARSET_INFO *cs, char *dst, size_t dstlen,
977                         int radix, longlong val)
978 {
979   return (cs->cset->longlong10_to_str)(cs, dst, dstlen, radix, val);
980 }
981 
982 #define my_ci_mb_wc(s, pwc, b, e)        ((s)->cset->mb_wc)(s, pwc, b, e)
983 #define my_ci_wc_mb(s, wc, b, e)         ((s)->cset->wc_mb)(s, wc, b, e)
984 #define my_ci_native_to_mb(s, wc, b, e)  ((s)->cset->native_to_mb)(s, wc, b, e)
985 #define my_ci_ctype(s, pctype, b, e)     ((s)->cset->ctype)(s, pctype, b, e)
986 
987 static inline void
my_ci_fill(CHARSET_INFO * cs,char * to,size_t len,int ch)988 my_ci_fill(CHARSET_INFO *cs, char *to, size_t len, int ch)
989 {
990   (cs->cset->fill)(cs, to, len, ch);
991 }
992 
993 static inline long
my_ci_strntol(CHARSET_INFO * cs,const char * str,size_t length,int base,char ** endptr,int * error)994 my_ci_strntol(CHARSET_INFO *cs, const char *str, size_t length,
995               int base, char **endptr, int *error)
996 {
997   return (cs->cset->strntol)(cs, str, length, base, endptr, error);
998 }
999 
1000 static inline ulong
my_ci_strntoul(CHARSET_INFO * cs,const char * str,size_t length,int base,char ** endptr,int * error)1001 my_ci_strntoul(CHARSET_INFO *cs, const char *str, size_t length,
1002                int base, char **endptr, int *error)
1003 {
1004   return (cs->cset->strntoul)(cs, str, length, base, endptr, error);
1005 }
1006 
1007 static inline longlong
my_ci_strntoll(CHARSET_INFO * cs,const char * str,size_t length,int base,char ** endptr,int * error)1008 my_ci_strntoll(CHARSET_INFO *cs, const char *str, size_t length,
1009                int base, char **endptr, int *error)
1010 {
1011   return (cs->cset->strntoll)(cs, str, length, base, endptr, error);
1012 }
1013 
1014 static inline ulonglong
my_ci_strntoull(CHARSET_INFO * cs,const char * str,size_t length,int base,char ** endptr,int * error)1015 my_ci_strntoull(CHARSET_INFO *cs, const char *str, size_t length,
1016                 int base, char **endptr, int *error)
1017 {
1018   return (cs->cset->strntoull)(cs, str, length, base, endptr, error);
1019 }
1020 
1021 static inline double
my_ci_strntod(CHARSET_INFO * cs,char * str,size_t length,char ** endptr,int * error)1022 my_ci_strntod(CHARSET_INFO *cs, char *str, size_t length,
1023               char **endptr, int *error)
1024 {
1025   return (cs->cset->strntod)(cs, str, length, endptr, error);
1026 }
1027 
1028 static inline longlong
my_ci_strtoll10(CHARSET_INFO * cs,const char * str,char ** endptr,int * error)1029 my_ci_strtoll10(CHARSET_INFO *cs, const char *str, char **endptr, int *error)
1030 {
1031   return (cs->cset->strtoll10)(cs, str, endptr, error);
1032 }
1033 
1034 static inline ulonglong
my_ci_strntoull10rnd(CHARSET_INFO * cs,const char * str,size_t length,int unsigned_fl,char ** endptr,int * error)1035 my_ci_strntoull10rnd(CHARSET_INFO *cs,
1036                      const char *str, size_t length, int unsigned_fl,
1037                      char **endptr, int *error)
1038 {
1039   return (cs->cset->strntoull10rnd)(cs, str, length, unsigned_fl, endptr, error);
1040 }
1041 
1042 
1043 static inline size_t
my_ci_scan(CHARSET_INFO * cs,const char * b,const char * e,int seq)1044 my_ci_scan(CHARSET_INFO *cs, const char *b, const char *e, int seq)
1045 {
1046   return (cs->cset->scan)(cs, b, e, seq);
1047 }
1048 
1049 
1050 /**
1051   Return length of the leftmost character in a string.
1052   @param cs  - character set
1053   @param str - the beginning of the string
1054   @param end - the string end (the next byte after the string)
1055   @return  <=0 on errors (EOL, wrong byte sequence)
1056   @return    1 on a single byte character
1057   @return   >1 on a multi-byte character
1058 
1059   Note, inlike my_ismbchar(), 1 is returned for a single byte character.
1060 */
1061 
1062 static inline int
my_ci_charlen(CHARSET_INFO * cs,const uchar * str,const uchar * end)1063 my_ci_charlen(CHARSET_INFO *cs, const uchar *str, const uchar *end)
1064 {
1065   return (cs->cset->charlen)(cs, str, end);
1066 }
1067 
1068 
1069 static inline size_t
my_ci_well_formed_char_length(CHARSET_INFO * cs,const char * str,const char * end,size_t nchars,MY_STRCOPY_STATUS * status)1070 my_ci_well_formed_char_length(CHARSET_INFO *cs,
1071                               const char *str, const char *end,
1072                               size_t nchars,
1073                               MY_STRCOPY_STATUS *status)
1074 {
1075   return (cs->cset->well_formed_char_length)(cs, str, end, nchars, status);
1076 }
1077 
1078 
1079 static inline size_t
my_ci_copy_fix(CHARSET_INFO * cs,char * dst,size_t dst_length,const char * src,size_t src_length,size_t nchars,MY_STRCOPY_STATUS * status)1080 my_ci_copy_fix(CHARSET_INFO *cs,
1081                char *dst, size_t dst_length,
1082                const char *src, size_t src_length,
1083                size_t nchars, MY_STRCOPY_STATUS *status)
1084 {
1085   return (cs->cset->copy_fix)(cs, dst, dst_length, src, src_length, nchars,
1086                               status);
1087 }
1088 
1089 
1090 /* Collation routines */
1091 
1092 static inline my_bool
my_ci_init_collation(struct charset_info_st * ci,MY_CHARSET_LOADER * loader)1093 my_ci_init_collation(struct charset_info_st *ci, MY_CHARSET_LOADER *loader)
1094 {
1095   if (!ci->coll->init)
1096     return FALSE;
1097   return (ci->coll->init)(ci, loader);
1098 }
1099 
1100 
1101 static inline int
my_ci_strnncoll(CHARSET_INFO * ci,const uchar * a,size_t alen,const uchar * b,size_t blen,my_bool b_is_prefix)1102 my_ci_strnncoll(CHARSET_INFO *ci,
1103                 const uchar *a, size_t alen,
1104                 const uchar *b, size_t blen,
1105                 my_bool b_is_prefix)
1106 {
1107   return (ci->coll->strnncoll)(ci, a, alen, b, blen, b_is_prefix);
1108 }
1109 
1110 static inline int
my_ci_strnncollsp(CHARSET_INFO * ci,const uchar * a,size_t alen,const uchar * b,size_t blen)1111 my_ci_strnncollsp(CHARSET_INFO *ci,
1112                   const uchar *a, size_t alen,
1113                   const uchar *b, size_t blen)
1114 {
1115   return (ci->coll->strnncollsp)(ci, a, alen, b, blen);
1116 }
1117 
1118 
1119 static inline my_bool
my_ci_like_range(CHARSET_INFO * ci,const char * s,size_t s_length,pchar w_prefix,pchar w_one,pchar w_many,size_t res_length,char * min_str,char * max_str,size_t * min_len,size_t * max_len)1120 my_ci_like_range(CHARSET_INFO *ci,
1121                  const char *s, size_t s_length,
1122                  pchar w_prefix, pchar w_one, pchar w_many,
1123                  size_t res_length,
1124                  char *min_str, char *max_str,
1125                  size_t *min_len, size_t *max_len)
1126 {
1127   return (ci->coll->like_range)(ci, s, s_length,
1128                                 w_prefix, w_one, w_many,
1129                                 res_length, min_str, max_str,
1130                                 min_len, max_len);
1131 }
1132 
1133 
1134 static inline uint
my_ci_instr(CHARSET_INFO * ci,const char * b,size_t b_length,const char * s,size_t s_length,my_match_t * match,uint nmatch)1135 my_ci_instr(CHARSET_INFO *ci,
1136             const char *b, size_t b_length,
1137             const char *s, size_t s_length,
1138             my_match_t *match, uint nmatch)
1139 {
1140   return (ci->coll->instr)(ci, b, b_length, s, s_length, match, nmatch);
1141 }
1142 
1143 
1144 static inline void
my_ci_hash_sort(CHARSET_INFO * ci,const uchar * key,size_t len,ulong * nr1,ulong * nr2)1145 my_ci_hash_sort(CHARSET_INFO *ci,
1146                 const uchar *key, size_t len,
1147                 ulong *nr1, ulong *nr2)
1148 {
1149   (ci->coll->hash_sort)(ci, key, len, nr1, nr2);
1150 }
1151 
1152 
1153 #define ILLEGAL_CHARSET_INFO_NUMBER (~0U)
1154 
1155 extern MYSQL_PLUGIN_IMPORT struct charset_info_st my_charset_bin;
1156 extern MYSQL_PLUGIN_IMPORT struct charset_info_st my_charset_latin1;
1157 extern MYSQL_PLUGIN_IMPORT struct charset_info_st my_charset_latin1_nopad;
1158 extern MYSQL_PLUGIN_IMPORT struct charset_info_st my_charset_filename;
1159 extern MYSQL_PLUGIN_IMPORT struct charset_info_st my_charset_utf8mb3_general_ci;
1160 
1161 extern struct charset_info_st my_charset_big5_bin;
1162 extern struct charset_info_st my_charset_big5_chinese_ci;
1163 extern struct charset_info_st my_charset_big5_nopad_bin;
1164 extern struct charset_info_st my_charset_big5_chinese_nopad_ci;
1165 extern struct charset_info_st my_charset_cp1250_czech_ci;
1166 extern struct charset_info_st my_charset_cp932_bin;
1167 extern struct charset_info_st my_charset_cp932_japanese_ci;
1168 extern struct charset_info_st my_charset_cp932_nopad_bin;
1169 extern struct charset_info_st my_charset_cp932_japanese_nopad_ci;
1170 extern struct charset_info_st my_charset_eucjpms_bin;
1171 extern struct charset_info_st my_charset_eucjpms_japanese_ci;
1172 extern struct charset_info_st my_charset_eucjpms_nopad_bin;
1173 extern struct charset_info_st my_charset_eucjpms_japanese_nopad_ci;
1174 extern struct charset_info_st my_charset_euckr_bin;
1175 extern struct charset_info_st my_charset_euckr_korean_ci;
1176 extern struct charset_info_st my_charset_euckr_nopad_bin;
1177 extern struct charset_info_st my_charset_euckr_korean_nopad_ci;
1178 extern struct charset_info_st my_charset_gb2312_bin;
1179 extern struct charset_info_st my_charset_gb2312_chinese_ci;
1180 extern struct charset_info_st my_charset_gb2312_nopad_bin;
1181 extern struct charset_info_st my_charset_gb2312_chinese_nopad_ci;
1182 extern struct charset_info_st my_charset_gbk_bin;
1183 extern struct charset_info_st my_charset_gbk_chinese_ci;
1184 extern struct charset_info_st my_charset_gbk_nopad_bin;
1185 extern struct charset_info_st my_charset_gbk_chinese_nopad_ci;
1186 extern struct charset_info_st my_charset_latin1_bin;
1187 extern struct charset_info_st my_charset_latin1_nopad_bin;
1188 extern struct charset_info_st my_charset_latin1_german2_ci;
1189 extern struct charset_info_st my_charset_latin2_czech_ci;
1190 extern struct charset_info_st my_charset_sjis_bin;
1191 extern struct charset_info_st my_charset_sjis_japanese_ci;
1192 extern struct charset_info_st my_charset_sjis_nopad_bin;
1193 extern struct charset_info_st my_charset_sjis_japanese_nopad_ci;
1194 extern struct charset_info_st my_charset_tis620_bin;
1195 extern struct charset_info_st my_charset_tis620_thai_ci;
1196 extern struct charset_info_st my_charset_tis620_nopad_bin;
1197 extern struct charset_info_st my_charset_tis620_thai_nopad_ci;
1198 extern struct charset_info_st my_charset_ucs2_bin;
1199 extern struct charset_info_st my_charset_ucs2_general_ci;
1200 extern struct charset_info_st my_charset_ucs2_nopad_bin;
1201 extern struct charset_info_st my_charset_ucs2_general_nopad_ci;
1202 extern struct charset_info_st my_charset_ucs2_general_mysql500_ci;
1203 extern struct charset_info_st my_charset_ucs2_unicode_ci;
1204 extern struct charset_info_st my_charset_ucs2_unicode_nopad_ci;
1205 extern struct charset_info_st my_charset_ucs2_general_mysql500_ci;
1206 extern struct charset_info_st my_charset_ujis_bin;
1207 extern struct charset_info_st my_charset_ujis_japanese_ci;
1208 extern struct charset_info_st my_charset_ujis_nopad_bin;
1209 extern struct charset_info_st my_charset_ujis_japanese_nopad_ci;
1210 extern struct charset_info_st my_charset_utf16_bin;
1211 extern struct charset_info_st my_charset_utf16_general_ci;
1212 extern struct charset_info_st my_charset_utf16_unicode_ci;
1213 extern struct charset_info_st my_charset_utf16_unicode_nopad_ci;
1214 extern struct charset_info_st my_charset_utf16le_bin;
1215 extern struct charset_info_st my_charset_utf16le_general_ci;
1216 extern struct charset_info_st my_charset_utf16_general_nopad_ci;
1217 extern struct charset_info_st my_charset_utf16_nopad_bin;
1218 extern struct charset_info_st my_charset_utf16le_nopad_bin;
1219 extern struct charset_info_st my_charset_utf16le_general_nopad_ci;
1220 extern struct charset_info_st my_charset_utf32_bin;
1221 extern struct charset_info_st my_charset_utf32_general_ci;
1222 extern struct charset_info_st my_charset_utf32_unicode_ci;
1223 extern struct charset_info_st my_charset_utf32_unicode_nopad_ci;
1224 extern struct charset_info_st my_charset_utf32_nopad_bin;
1225 extern struct charset_info_st my_charset_utf32_general_nopad_ci;
1226 extern MYSQL_PLUGIN_IMPORT struct charset_info_st my_charset_utf8mb3_bin;
1227 extern struct charset_info_st my_charset_utf8mb3_nopad_bin;
1228 extern struct charset_info_st my_charset_utf8mb3_general_nopad_ci;
1229 extern struct charset_info_st my_charset_utf8mb3_general_mysql500_ci;
1230 extern struct charset_info_st my_charset_utf8mb3_unicode_ci;
1231 extern struct charset_info_st my_charset_utf8mb3_unicode_nopad_ci;
1232 extern MYSQL_PLUGIN_IMPORT struct charset_info_st my_charset_utf8mb4_bin;
1233 extern struct charset_info_st my_charset_utf8mb4_general_ci;
1234 extern struct charset_info_st my_charset_utf8mb4_nopad_bin;
1235 extern struct charset_info_st my_charset_utf8mb4_general_nopad_ci;
1236 extern struct charset_info_st my_charset_utf8mb4_unicode_ci;
1237 extern struct charset_info_st my_charset_utf8mb4_unicode_nopad_ci;
1238 
1239 #define MY_UTF8MB3                 "utf8"
1240 #define MY_UTF8MB4                 "utf8mb4"
1241 
1242 my_bool my_cs_have_contractions(CHARSET_INFO *cs);
1243 my_bool my_cs_can_be_contraction_head(CHARSET_INFO *cs, my_wc_t wc);
1244 my_bool my_cs_can_be_contraction_tail(CHARSET_INFO *cs, my_wc_t wc);
1245 const uint16 *my_cs_contraction2_weight(CHARSET_INFO *cs, my_wc_t wc1,
1246                                          my_wc_t wc2);
1247 
1248 /* declarations for simple charsets */
1249 extern size_t my_strnxfrm_simple(CHARSET_INFO *,
1250                                  uchar *dst, size_t dstlen, uint nweights,
1251                                  const uchar *src, size_t srclen, uint flags);
1252 size_t  my_strnxfrmlen_simple(CHARSET_INFO *, size_t);
1253 extern int  my_strnncoll_simple(CHARSET_INFO *, const uchar *, size_t,
1254 				const uchar *, size_t, my_bool);
1255 
1256 extern int  my_strnncollsp_simple(CHARSET_INFO *, const uchar *, size_t,
1257                                   const uchar *, size_t);
1258 
1259 extern void my_hash_sort_simple(CHARSET_INFO *cs,
1260 				const uchar *key, size_t len,
1261 				ulong *nr1, ulong *nr2);
1262 
1263 extern void my_hash_sort_simple_nopad(CHARSET_INFO *cs,
1264 				      const uchar *key, size_t len,
1265 				      ulong *nr1, ulong *nr2);
1266 
1267 extern void my_hash_sort_bin(CHARSET_INFO *cs,
1268                              const uchar *key, size_t len, ulong *nr1,
1269                              ulong *nr2);
1270 
1271 /**
1272   Compare a string to an array of spaces, for PAD SPACE comparison.
1273   The function iterates through the string and compares every byte to 0x20.
1274   @param       - the string
1275   @param       - its length
1276   @return <0   - if a byte less than 0x20 was found in the string.
1277   @return  0   - if all bytes in the string were 0x20, or if length was 0.
1278   @return >0   - if a byte greater than 0x20 was found in the string.
1279 */
1280 extern int my_strnncollsp_padspace_bin(const uchar *str, size_t length);
1281 
1282 extern size_t my_lengthsp_8bit(CHARSET_INFO *cs, const char *ptr, size_t length);
1283 
1284 extern uint my_instr_simple(CHARSET_INFO *,
1285                             const char *b, size_t b_length,
1286                             const char *s, size_t s_length,
1287                             my_match_t *match, uint nmatch);
1288 
1289 size_t my_copy_8bit(CHARSET_INFO *,
1290                     char *dst, size_t dst_length,
1291                     const char *src, size_t src_length,
1292                     size_t nchars, MY_STRCOPY_STATUS *);
1293 size_t my_copy_fix_mb(CHARSET_INFO *cs,
1294                       char *dst, size_t dst_length,
1295                       const char *src, size_t src_length,
1296                       size_t nchars, MY_STRCOPY_STATUS *);
1297 
1298 /* Functions for 8bit */
1299 extern size_t my_caseup_str_8bit(CHARSET_INFO *, char *);
1300 extern size_t my_casedn_str_8bit(CHARSET_INFO *, char *);
1301 extern size_t my_caseup_8bit(CHARSET_INFO *,
1302                              const char *src, size_t srclen,
1303                              char *dst, size_t dstlen);
1304 extern size_t my_casedn_8bit(CHARSET_INFO *,
1305                              const char *src, size_t srclen,
1306                              char *dst, size_t dstlen);
1307 
1308 extern int my_strcasecmp_8bit(CHARSET_INFO * cs, const char *, const char *);
1309 
1310 int my_mb_wc_8bit(CHARSET_INFO *cs,my_wc_t *wc, const uchar *s,const uchar *e);
1311 int my_wc_mb_8bit(CHARSET_INFO *cs,my_wc_t wc, uchar *s, uchar *e);
1312 int my_wc_mb_bin(CHARSET_INFO *cs,my_wc_t wc, uchar *s, uchar *e);
1313 
1314 int my_mb_ctype_8bit(CHARSET_INFO *,int *, const uchar *,const uchar *);
1315 int my_mb_ctype_mb(CHARSET_INFO *,int *, const uchar *,const uchar *);
1316 
1317 size_t my_scan_8bit(CHARSET_INFO *cs, const char *b, const char *e, int sq);
1318 
1319 size_t my_snprintf_8bit(CHARSET_INFO *, char *to, size_t n,
1320                         const char *fmt, ...)
1321   ATTRIBUTE_FORMAT(printf, 4, 5);
1322 
1323 long       my_strntol_8bit(CHARSET_INFO *, const char *s, size_t l, int base,
1324                            char **e, int *err);
1325 ulong      my_strntoul_8bit(CHARSET_INFO *, const char *s, size_t l, int base,
1326 			    char **e, int *err);
1327 longlong   my_strntoll_8bit(CHARSET_INFO *, const char *s, size_t l, int base,
1328 			    char **e, int *err);
1329 ulonglong my_strntoull_8bit(CHARSET_INFO *, const char *s, size_t l, int base,
1330 			    char **e, int *err);
1331 double      my_strntod_8bit(CHARSET_INFO *, char *s, size_t l,char **e,
1332 			    int *err);
1333 size_t my_long10_to_str_8bit(CHARSET_INFO *, char *to, size_t l, int radix,
1334                              long int val);
1335 size_t my_longlong10_to_str_8bit(CHARSET_INFO *, char *to, size_t l, int radix,
1336                                  longlong val);
1337 
1338 longlong my_strtoll10_8bit(CHARSET_INFO *cs,
1339                            const char *nptr, char **endptr, int *error);
1340 longlong my_strtoll10_ucs2(CHARSET_INFO *cs,
1341                            const char *nptr, char **endptr, int *error);
1342 
1343 ulonglong my_strntoull10rnd_8bit(CHARSET_INFO *cs,
1344                                  const char *str, size_t length, int
1345                                  unsigned_fl, char **endptr, int *error);
1346 ulonglong my_strntoull10rnd_ucs2(CHARSET_INFO *cs,
1347                                  const char *str, size_t length,
1348                                  int unsigned_fl, char **endptr, int *error);
1349 
1350 void my_fill_8bit(CHARSET_INFO *cs, char* to, size_t l, int fill);
1351 
1352 /* For 8-bit character set */
1353 my_bool  my_like_range_simple(CHARSET_INFO *cs,
1354 			      const char *ptr, size_t ptr_length,
1355 			      pbool escape, pbool w_one, pbool w_many,
1356 			      size_t res_length,
1357 			      char *min_str, char *max_str,
1358 			      size_t *min_length, size_t *max_length);
1359 
1360 /* For ASCII-based multi-byte character sets with mbminlen=1 */
1361 my_bool  my_like_range_mb(CHARSET_INFO *cs,
1362 			  const char *ptr, size_t ptr_length,
1363 			  pbool escape, pbool w_one, pbool w_many,
1364 			  size_t res_length,
1365 			  char *min_str, char *max_str,
1366 			  size_t *min_length, size_t *max_length);
1367 
1368 /* For other character sets, with arbitrary mbminlen and mbmaxlen numbers */
1369 my_bool  my_like_range_generic(CHARSET_INFO *cs,
1370                                const char *ptr, size_t ptr_length,
1371                                pbool escape, pbool w_one, pbool w_many,
1372                                size_t res_length,
1373                                char *min_str, char *max_str,
1374                                size_t *min_length, size_t *max_length);
1375 
1376 int my_wildcmp_8bit(CHARSET_INFO *,
1377 		    const char *str,const char *str_end,
1378 		    const char *wildstr,const char *wildend,
1379 		    int escape, int w_one, int w_many);
1380 
1381 int my_wildcmp_bin(CHARSET_INFO *,
1382 		   const char *str,const char *str_end,
1383 		   const char *wildstr,const char *wildend,
1384 		   int escape, int w_one, int w_many);
1385 
1386 size_t my_numchars_8bit(CHARSET_INFO *, const char *b, const char *e);
1387 size_t my_numcells_8bit(CHARSET_INFO *, const char *b, const char *e);
1388 size_t my_charpos_8bit(CHARSET_INFO *, const char *b, const char *e, size_t pos);
1389 size_t my_well_formed_char_length_8bit(CHARSET_INFO *cs,
1390                                        const char *b, const char *e,
1391                                        size_t nchars,
1392                                        MY_STRCOPY_STATUS *status);
1393 int my_charlen_8bit(CHARSET_INFO *, const uchar *str, const uchar *end);
1394 
1395 
1396 /* Functions for multibyte charsets */
1397 extern size_t my_caseup_str_mb(CHARSET_INFO *, char *);
1398 extern size_t my_casedn_str_mb(CHARSET_INFO *, char *);
1399 extern size_t my_caseup_mb(CHARSET_INFO *,
1400                            const char *src, size_t srclen,
1401                            char *dst, size_t dstlen);
1402 extern size_t my_casedn_mb(CHARSET_INFO *,
1403                            const char *src, size_t srclen,
1404                            char *dst, size_t dstlen);
1405 extern size_t my_caseup_ujis(CHARSET_INFO *,
1406                              const char *src, size_t srclen,
1407                              char *dst, size_t dstlen);
1408 extern size_t my_casedn_ujis(CHARSET_INFO *,
1409                              const char *src, size_t srclen,
1410                              char *dst, size_t dstlen);
1411 extern int my_strcasecmp_mb(CHARSET_INFO * cs,const char *, const char *);
1412 
1413 int my_wildcmp_mb(CHARSET_INFO *,
1414 		  const char *str,const char *str_end,
1415 		  const char *wildstr,const char *wildend,
1416 		  int escape, int w_one, int w_many);
1417 size_t my_numchars_mb(CHARSET_INFO *, const char *b, const char *e);
1418 size_t my_numcells_mb(CHARSET_INFO *, const char *b, const char *e);
1419 size_t my_charpos_mb(CHARSET_INFO *, const char *b, const char *e, size_t pos);
1420 uint my_instr_mb(CHARSET_INFO *,
1421                  const char *b, size_t b_length,
1422                  const char *s, size_t s_length,
1423                  my_match_t *match, uint nmatch);
1424 
1425 int my_wildcmp_mb_bin(CHARSET_INFO *cs,
1426                       const char *str,const char *str_end,
1427                       const char *wildstr,const char *wildend,
1428                       int escape, int w_one, int w_many);
1429 
1430 int my_strcasecmp_mb_bin(CHARSET_INFO * cs __attribute__((unused)),
1431                          const char *s, const char *t);
1432 
1433 void my_hash_sort_mb_bin(CHARSET_INFO *cs __attribute__((unused)),
1434                          const uchar *key, size_t len,ulong *nr1, ulong *nr2);
1435 
1436 void my_hash_sort_mb_nopad_bin(CHARSET_INFO *cs __attribute__((unused)),
1437                                const uchar *key, size_t len,
1438                                ulong *nr1, ulong *nr2);
1439 
1440 size_t my_strnxfrm_mb(CHARSET_INFO *,
1441                       uchar *dst, size_t dstlen, uint nweights,
1442                       const uchar *src, size_t srclen, uint flags);
1443 
1444 size_t my_strnxfrm_mb_nopad(CHARSET_INFO *,
1445 			    uchar *dst, size_t dstlen, uint nweights,
1446 			    const uchar *src, size_t srclen, uint flags);
1447 
1448 size_t  my_strnxfrmlen_unicode(CHARSET_INFO *, size_t);
1449 
1450 size_t my_strnxfrm_unicode_full_bin(CHARSET_INFO *,
1451                                     uchar *dst, size_t dstlen,
1452                                     uint nweights, const uchar *src,
1453                                     size_t srclen, uint flags);
1454 
1455 size_t my_strnxfrm_unicode_full_nopad_bin(CHARSET_INFO *,
1456 					  uchar *dst, size_t dstlen,
1457 					  uint nweights, const uchar *src,
1458 					  size_t srclen, uint flags);
1459 
1460 size_t  my_strnxfrmlen_unicode_full_bin(CHARSET_INFO *, size_t);
1461 
1462 int my_wildcmp_unicode(CHARSET_INFO *cs,
1463                        const char *str, const char *str_end,
1464                        const char *wildstr, const char *wildend,
1465                        int escape, int w_one, int w_many,
1466                        MY_UNICASE_INFO *weights);
1467 
1468 extern my_bool my_parse_charset_xml(MY_CHARSET_LOADER *loader,
1469                                     const char *buf, size_t buflen);
1470 extern char *my_strchr(CHARSET_INFO *cs, const char *str, const char *end,
1471                        pchar c);
1472 extern size_t my_strcspn(CHARSET_INFO *cs, const char *str, const char *end,
1473                          const char *accept);
1474 
1475 my_bool my_propagate_simple(CHARSET_INFO *cs, const uchar *str, size_t len);
1476 my_bool my_propagate_complex(CHARSET_INFO *cs, const uchar *str, size_t len);
1477 
1478 
1479 typedef struct
1480 {
1481   size_t char_length;
1482   my_repertoire_t repertoire;
1483 } MY_STRING_METADATA;
1484 
1485 void my_string_metadata_get(MY_STRING_METADATA *metadata,
1486                             CHARSET_INFO *cs, const char *str, size_t len);
1487 my_repertoire_t my_string_repertoire(CHARSET_INFO *cs,
1488                                      const char *str, size_t len);
1489 my_bool my_charset_is_ascii_based(CHARSET_INFO *cs);
1490 my_repertoire_t my_charset_repertoire(CHARSET_INFO *cs);
1491 
1492 uint my_strxfrm_flag_normalize(uint flags, uint nlevels);
1493 void my_strxfrm_desc_and_reverse(uchar *str, uchar *strend,
1494                                  uint flags, uint level);
1495 size_t my_strxfrm_pad_desc_and_reverse(CHARSET_INFO *cs,
1496                                        uchar *str, uchar *frmend, uchar *strend,
1497                                        uint nweights, uint flags, uint level);
1498 size_t my_strxfrm_pad_desc_and_reverse_nopad(CHARSET_INFO *cs,
1499 					     uchar *str, uchar *frmend,
1500 					     uchar *strend, uint nweights,
1501 					     uint flags, uint level);
1502 
1503 const MY_CONTRACTIONS *my_charset_get_contractions(CHARSET_INFO *cs,
1504                                                    int level);
1505 
1506 extern size_t my_vsnprintf_ex(CHARSET_INFO *cs, char *to, size_t n,
1507                               const char* fmt, va_list ap);
1508 
1509 /*
1510   Convert a string between two character sets.
1511   Bad byte sequences as well as characters that cannot be
1512   encoded in the destination character set are replaced to '?'.
1513 */
1514 uint32 my_convert(char *to, uint32 to_length, CHARSET_INFO *to_cs,
1515                   const char *from, uint32 from_length,
1516                   CHARSET_INFO *from_cs, uint *errors);
1517 
1518 /**
1519   An extended version of my_convert(), to pass non-default mb_wc() and wc_mb().
1520   For example, String::copy_printable() which is used in
1521   Protocol::store_warning() uses this to escape control
1522   and non-convertable characters.
1523 */
1524 uint32 my_convert_using_func(char *to, size_t to_length, CHARSET_INFO *to_cs,
1525                              my_charset_conv_wc_mb mb_wc,
1526                              const char *from, size_t from_length,
1527                              CHARSET_INFO *from_cs,
1528                              my_charset_conv_mb_wc wc_mb,
1529                              uint *errors);
1530 /*
1531   Convert a string between two character sets.
1532   Bad byte sequences as well as characters that cannot be
1533   encoded in the destination character set are replaced to '?'.
1534   Not more than "nchars" characters are copied.
1535   Conversion statistics is returned in "status" and is set as follows:
1536   - status->m_native_copy_status.m_source_end_pos - to the position
1537     between (src) and (src+src_length), where the function stopped reading
1538     the source string.
1539   - status->m_native_copy_status.m_well_formed_error_pos - to the position
1540     between (src) and (src+src_length), where the first badly formed byte
1541     sequence was found, or to NULL if the string was well formed in the
1542     given range.
1543   - status->m_cannot_convert_error_pos - to the position
1544     between (src) and (src+src_length), where the first character that
1545     cannot be represented in the destination character set was found,
1546     or to NULL if all characters in the given range were successfully
1547     converted.
1548 
1549   "src" is allowed to be a NULL pointer. In this case "src_length" must
1550   be equal to 0. All "status" members are initialized to NULL, and 0 is
1551   returned.
1552 */
1553 size_t my_convert_fix(CHARSET_INFO *dstcs, char *dst, size_t dst_length,
1554                       CHARSET_INFO *srccs, const char *src, size_t src_length,
1555                       size_t nchars,
1556                       MY_STRCOPY_STATUS *copy_status,
1557                       MY_STRCONV_STATUS *conv_status);
1558 
1559 #define	_MY_U	01	/* Upper case */
1560 #define	_MY_L	02	/* Lower case */
1561 #define	_MY_NMR	04	/* Numeral (digit) */
1562 #define	_MY_SPC	010	/* Spacing character */
1563 #define	_MY_PNT	020	/* Punctuation */
1564 #define	_MY_CTR	040	/* Control character */
1565 #define	_MY_B	0100	/* Blank */
1566 #define	_MY_X	0200	/* heXadecimal digit */
1567 
1568 
1569 #define	my_isascii(c)	(!((c) & ~0177))
1570 #define	my_toascii(c)	((c) & 0177)
1571 #define my_tocntrl(c)	((c) & 31)
1572 #define my_toprint(c)	((c) | 64)
1573 #define my_toupper(s,c)	(char) ((s)->to_upper[(uchar) (c)])
1574 #define my_tolower(s,c)	(char) ((s)->to_lower[(uchar) (c)])
1575 #define	my_isalpha(s, c)  (((s)->m_ctype+1)[(uchar) (c)] & (_MY_U | _MY_L))
1576 #define	my_isupper(s, c)  (((s)->m_ctype+1)[(uchar) (c)] & _MY_U)
1577 #define	my_islower(s, c)  (((s)->m_ctype+1)[(uchar) (c)] & _MY_L)
1578 #define	my_isdigit(s, c)  (((s)->m_ctype+1)[(uchar) (c)] & _MY_NMR)
1579 #define	my_isxdigit(s, c) (((s)->m_ctype+1)[(uchar) (c)] & _MY_X)
1580 #define	my_isalnum(s, c)  (((s)->m_ctype+1)[(uchar) (c)] & (_MY_U | _MY_L | _MY_NMR))
1581 #define	my_isspace(s, c)  (((s)->m_ctype+1)[(uchar) (c)] & _MY_SPC)
1582 #define	my_ispunct(s, c)  (((s)->m_ctype+1)[(uchar) (c)] & _MY_PNT)
1583 #define	my_isprint(s, c)  (((s)->m_ctype+1)[(uchar) (c)] & (_MY_PNT | _MY_U | _MY_L | _MY_NMR | _MY_B))
1584 #define	my_isgraph(s, c)  (((s)->m_ctype+1)[(uchar) (c)] & (_MY_PNT | _MY_U | _MY_L | _MY_NMR))
1585 #define	my_iscntrl(s, c)  (((s)->m_ctype+1)[(uchar) (c)] & _MY_CTR)
1586 
1587 /* Some macros that should be cleaned up a little */
1588 #define my_isvar(s,c)                 (my_isalnum(s,c) || (c) == '_')
1589 #define my_isvar_start(s,c)           (my_isalpha(s,c) || (c) == '_')
1590 
1591 #define my_binary_compare(s)	      ((s)->state  & MY_CS_BINSORT)
1592 #define use_strnxfrm(s)               ((s)->state  & MY_CS_STRNXFRM)
1593 #define my_strnncoll(s, a, b, c, d) ((s)->coll->strnncoll((s), (a), (b), (c), (d), 0))
1594 #define my_strcasecmp(s, a, b)        ((s)->coll->strcasecmp((s), (a), (b)))
1595 
1596 /**
1597   Detect if the leftmost character in a string is a valid multi-byte character
1598   and return its length, or return 0 otherwise.
1599   @param cs  - character set
1600   @param str - the beginning of the string
1601   @param end - the string end (the next byte after the string)
1602   @return    >0, for a multi-byte character
1603   @rerurn    0,  for a single byte character, broken sequence, empty string.
1604 */
1605 static inline
my_ismbchar(CHARSET_INFO * cs,const char * str,const char * end)1606 uint my_ismbchar(CHARSET_INFO *cs, const char *str, const char *end)
1607 {
1608   int char_length= (cs->cset->charlen)(cs, (const uchar *) str,
1609                                            (const uchar *) end);
1610   return char_length > 1 ? (uint) char_length : 0U;
1611 }
1612 
1613 
1614 /**
1615   Convert broken and incomplete byte sequences to 1 byte.
1616 */
1617 static inline
my_ci_charlen_fix(CHARSET_INFO * cs,const uchar * str,const uchar * end)1618 uint my_ci_charlen_fix(CHARSET_INFO *cs, const uchar *str, const uchar *end)
1619 {
1620   int char_length= my_ci_charlen(cs, str, end);
1621   DBUG_ASSERT(str < end);
1622   return char_length > 0 ? (uint) char_length : (uint) 1U;
1623 }
1624 
1625 
1626 /*
1627   A compatibility replacement pure C function for the former
1628     cs->cset->well_formed_len().
1629   In C++ code please use Well_formed_prefix::length() instead.
1630 */
1631 static inline size_t
my_well_formed_length(CHARSET_INFO * cs,const char * b,const char * e,size_t nchars,int * error)1632 my_well_formed_length(CHARSET_INFO *cs, const char *b, const char *e,
1633                       size_t nchars, int *error)
1634 {
1635   MY_STRCOPY_STATUS status;
1636   (void) my_ci_well_formed_char_length(cs, b, e, nchars, &status);
1637   *error= status.m_well_formed_error_pos == NULL ? 0 : 1;
1638   return (size_t) (status.m_source_end_pos - b);
1639 }
1640 
1641 
1642 #define my_caseup_str(s, a)           ((s)->cset->caseup_str((s), (a)))
1643 #define my_casedn_str(s, a)           ((s)->cset->casedn_str((s), (a)))
1644 
1645 /* XXX: still need to take care of this one */
1646 #ifdef MY_CHARSET_TIS620
1647 #error The TIS620 charset is broken at the moment.  Tell tim to fix it.
1648 #define USE_TIS620
1649 #include "t_ctype.h"
1650 #endif
1651 
1652 #ifdef	__cplusplus
1653 }
1654 #endif
1655 
1656 #endif /* _m_ctype_h */
1657