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