1 /*
2  Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights
3  reserved.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License, version 2.0,
7  as published by the Free Software Foundation.
8 
9  This program is also distributed with certain software (including
10  but not limited to OpenSSL) that is licensed under separate terms,
11  as designated in a particular file or component or in included license
12  documentation.  The authors of MySQL hereby grant you an additional
13  permission to link the program and your derivative works with the
14  separately licensed software that they have included with MySQL.
15 
16  This program is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  GNU General Public License, version 2.0, for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24  02110-1301  USA
25  */
26 
27 #include <ctype.h>
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <string.h>
31 
32 #include <my_global.h>
33 #include <mysql.h>
34 #include <my_sys.h>
35 
36 #include <NdbApi.hpp>
37 
38 #include "EncoderCharset.h"
39 
40 /* C++ initializes this to zeros:
41 */
42 EncoderCharset * csinfo_table[MY_CS_CTYPE_TABLE_SIZE];
43 
44 
colIsUtf16le(const NdbDictionary::Column * col)45 inline bool colIsUtf16le(const NdbDictionary::Column *col) {
46   return (strncmp("utf16le", col->getCharset()->csname, 7) == 0);
47 }
48 
colIsUtf8(const NdbDictionary::Column * col)49 inline bool colIsUtf8(const NdbDictionary::Column *col) {
50   return (strncmp("utf8", col->getCharset()->csname, 4) == 0);
51 }
52 
colIsLatin1(const NdbDictionary::Column * col)53 inline bool colIsLatin1(const NdbDictionary::Column *col) {
54   return (strncmp("latin1", col->getCharset()->csname, 6) == 0);
55 }
56 
colIsAscii(const NdbDictionary::Column * col)57 inline bool colIsAscii(const NdbDictionary::Column *col) {
58   return (strncmp("ascii", col->getCharset()->csname, 5) == 0);
59 }
60 
colIsMultibyte(const NdbDictionary::Column * col)61 inline bool colIsMultibyte(const NdbDictionary::Column *col) {
62   return (col->getCharset()->mbminlen > 1);
63 }
64 
65 
createEncoderCharset(const NdbDictionary::Column * col)66 EncoderCharset * createEncoderCharset(const NdbDictionary::Column *col) {
67   EncoderCharset * csinfo = new EncoderCharset;
68 
69   csinfo->name = col->getCharset()->csname;
70   csinfo->minlen = col->getCharset()->mbminlen;
71   csinfo->maxlen = col->getCharset()->mbmaxlen;
72   csinfo->isMultibyte = colIsMultibyte(col);
73   csinfo->isAscii = colIsAscii(col);
74   csinfo->isUtf8 = colIsUtf8(col);
75   csinfo->isUtf16le = colIsUtf16le(col);
76 
77   return csinfo;
78 }
79 
80 
getEncoderCharsetForColumn(const NdbDictionary::Column * col)81 const EncoderCharset * getEncoderCharsetForColumn(const NdbDictionary::Column *col) {
82   int csnum = col->getCharsetNumber();
83   EncoderCharset *csinfo = csinfo_table[csnum];
84   if(csinfo == 0) {
85     csinfo = createEncoderCharset(col);
86     csinfo_table[csnum] = csinfo;
87   }
88   return csinfo;
89 }
90