1 /*
2  Copyright (c) 2010, 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  * CharsetMap.hpp
26  */
27 
28 #ifndef CharsetMap_hpp
29 #define CharsetMap_hpp
30 
31 #include "ndb_types.h"
32 
33 /**
34  * Handles encoding issues for character data
35  * while keeping MySQL's CHARSET_INFO structure hidden.
36  */
37 class CharsetMap {
38 public:
39 
40     CharsetMap();
41     /* The compiler-generated destructor is OK. */
42     /* The compiler-generated copy constructor is OK. */
43     /* The compiler-generated assignment operator is OK. */
44 
45     /**
46      * Initializes any global CharsetMap resources.
47      * Should be called exactly once before any other CharsetMap function
48      * from a single thread.
49      */
50     static void init();
51 
52     /**
53      * Releases all global CharsetMap resources.
54      * Also not thread-safe
55      */
56     static void unload();
57 
58     /**
59      * Returns a standard character set name.
60      *
61      * The cs_number argument in getName(), getMysqlName(), and recode()
62      * can be obtained from NdbDictionary::Column::getCharsetNumber().
63      *
64      * getName() returns a name that in most cases will be a preferred name
65      * from http://www.iana.org/assignments/character-sets and will
66      * be recognized and usable by Java (e.g. java.nio, java.io, and java.lang).
67      * However it may return "binary" if a column is BLOB / BINARY / VARBINARY,
68      * or it may return the name of an obscure MySQL character set such as
69      * "keybcs2" or "dec8".
70      */
71     const char * getName(int cs_number) const;
72 
73     /**
74      * Returns just the internal mysql name of the charset.
75      */
76     const char * getMysqlName(int cs_number) const;
77 
78     /**
79      * Takes the mysql name (not the standardized name) and returns a
80      * character set number.
81      */
82     int getCharsetNumber(const char *mysql_name) const;
83 
84     /**
85      * Convenience function for UTF-8.
86      */
87     int getUTF8CharsetNumber() const;
88 
89     /**
90      * Convenience function for UTF-16.
91      */
92     int getUTF16CharsetNumber() const;
93 
94     /**
95      * The return status of a buffer recode operation.
96      */
97     enum RecodeStatus {
98         RECODE_OK ,
99         RECODE_BAD_CHARSET ,
100         RECODE_BAD_SRC ,
101         RECODE_BUFF_TOO_SMALL
102     };
103 
104     /**
105      * Returns true if this charset number refers to a multibyte charset;
106      * otherwise false.
107      */
108   const bool * isMultibyte(int cs_number) const;
109 
110     /**
111      * Recodes the content of a source buffer into destination buffer.
112      *
113      * Takes five arguments:
114      * lengths is an array of two ints: first the source
115      *  buffer length, then the destination buffer length.
116      * From and To are character set numbers.
117      *  src and dest are buffers.
118      *
119      * On return, lengths[0] is set to the number of bytes consumed from src
120      * and lengths[1] to the number of bytes written to dest.
121      *
122      * The string in src will be recoded from charset cs_from to charset cs_to.
123      * If the conversion is successful we return RECODE_OK.
124      * Other return values are noted above.
125      */
126     RecodeStatus recode(Int32 *lengths /* IN/OUT */,
127                         int cs_from, int cs_to, const void *src,
128                         void *dest) const;
129 
130 };
131 
132 #endif // CharsetMap_hpp
133