1 /* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 
24 /*
25   This file provide mysql_string service to plugins.
26   operations on mysql_string can be performed by plugins via these service
27   functions.
28 */
29 
30 #include <my_sys.h>
31 #include "string_service.h"
32 #include "unireg.h"
33 
34 /*
35   This service function converts the mysql_string to the character set
36   specified by charset_name parameter.
37 */
38 extern "C"
mysql_string_convert_to_char_ptr(mysql_string_handle string_handle,const char * charset_name,char * buffer,unsigned int buffer_size,int * error)39 int mysql_string_convert_to_char_ptr(mysql_string_handle string_handle,
40                                      const char *charset_name,
41                                      char *buffer,
42                                      unsigned int buffer_size,
43                                      int *error)
44 {
45   String *str= (String *) string_handle;
46   int len= (int)my_convert(buffer, buffer_size - 1, &my_charset_utf8_general_ci,
47                            str->ptr(), str->length(), str->charset(),
48                            (uint*) error);
49   buffer[len]= '\0';
50   return (len);
51 }
52 
53 /*
54   This service function deallocates the mysql_string_handle allocated on
55   server and used in plugins.
56 */
57 extern "C"
mysql_string_free(mysql_string_handle string_handle)58 void mysql_string_free(mysql_string_handle string_handle)
59 {
60   String *str= (String *) string_handle;
61   str->free();
62   delete [] str;
63 }
64 
65 /*
66   This service function deallocates the mysql_string_iterator_handle
67   allocated on server and used in plugins.
68 */
69 extern "C"
mysql_string_iterator_free(mysql_string_iterator_handle iterator_handle)70 void mysql_string_iterator_free(mysql_string_iterator_handle iterator_handle)
71 {
72   my_free((string_iterator *) iterator_handle);
73 }
74 
75 /* This service function allocate mysql_string_iterator_handle and return it */
76 extern "C"
mysql_string_get_iterator(mysql_string_handle string_handle)77 mysql_string_iterator_handle mysql_string_get_iterator(mysql_string_handle
78                                                        string_handle)
79 {
80   String *str= (String *) string_handle;
81   string_iterator *iterator= (string_iterator *) my_malloc(sizeof
82                                            (struct st_string_iterator), MYF(0));
83   iterator->iterator_str= str;
84   iterator->iterator_ptr= str->ptr();
85   iterator->ctype= 0;
86   return (iterator);
87 }
88 
89 /* Provide service which returns the next mysql_string_iterator_handle */
90 extern "C"
mysql_string_iterator_next(mysql_string_iterator_handle iterator_handle)91 int mysql_string_iterator_next(mysql_string_iterator_handle iterator_handle)
92 {
93   int char_len, char_type;
94   string_iterator *iterator= (string_iterator *) iterator_handle;
95   String *str= iterator->iterator_str;
96   const CHARSET_INFO *cs= str->charset();
97   char *end= (char*) str->ptr() + str->length();
98   if (iterator->iterator_ptr == (const char*) end)
99     return (0);
100   char_len= (cs->cset->ctype(cs, &char_type, (uchar*) iterator->iterator_ptr,
101                              (uchar*) end));
102   iterator->ctype= char_type;
103   iterator->iterator_ptr+= (char_len > 0 ? char_len : (char_len < 0
104                                                        ? -char_len : 1));
105   return (1);
106 }
107 
108 /*
109   Provide service which calculate weather the current iterator_ptr points to
110   upper case character or not
111 */
112 extern "C"
mysql_string_iterator_isupper(mysql_string_iterator_handle iterator_handle)113 int mysql_string_iterator_isupper(mysql_string_iterator_handle iterator_handle)
114 {
115   string_iterator *iterator= (string_iterator *) iterator_handle;
116   return (iterator->ctype & _MY_U);
117 }
118 
119 /*
120   Provide service which calculate weather the current iterator_ptr points to
121   lower case character or not
122 */
123 extern "C"
mysql_string_iterator_islower(mysql_string_iterator_handle iterator_handle)124 int mysql_string_iterator_islower(mysql_string_iterator_handle iterator_handle)
125 {
126   string_iterator *iterator= (string_iterator *) iterator_handle;
127   return (iterator->ctype & _MY_L);
128 }
129 
130 /*
131   Provide service which calculate weather the current iterator_ptr points to
132   digit or not
133 */
134 extern "C"
mysql_string_iterator_isdigit(mysql_string_iterator_handle iterator_handle)135 int mysql_string_iterator_isdigit(mysql_string_iterator_handle iterator_handle)
136 {
137   string_iterator *iterator= (string_iterator *) iterator_handle;
138   return (iterator->ctype & _MY_NMR);
139 }
140 
141 /*
142   This function provide plugin service to convert a String pointed by handle to
143   lower case. Conversion depends on the client character set info
144 */
145 extern "C"
mysql_string_to_lowercase(mysql_string_handle string_handle)146 mysql_string_handle mysql_string_to_lowercase(mysql_string_handle string_handle)
147 {
148   String *str= (String *) string_handle;
149   String *res= new String[1];
150   const CHARSET_INFO *cs= str->charset();
151   if (cs->casedn_multiply == 1)
152   {
153     res->copy(*str);
154     my_casedn_str(cs, res->c_ptr_quick());
155   }
156   else
157   {
158     uint len= str->length() * cs->casedn_multiply;
159     res->set_charset(cs);
160     res->alloc(len);
161     len= cs->cset->casedn(cs, (char*) str->ptr(), str->length(), (char *) res->ptr(), len);
162     res->length(len);
163   }
164   return (res);
165 }
166