1 /* Copyright (c) 2012, 2021, Oracle and/or its affiliates.
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 "mysql/service_mysql_string.h"
33 /* key_memory_string_iterator */
34 #include "mysqld.h"
35 PSI_memory_key key_memory_string_iterator;
36 
37 /*
38   This service function converts the mysql_string to the character set
39   specified by charset_name parameter.
40 */
41 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)42 int mysql_string_convert_to_char_ptr(mysql_string_handle string_handle,
43                                      const char *charset_name,
44                                      char *buffer,
45                                      unsigned int buffer_size,
46                                      int *error)
47 {
48   String *str= (String *) string_handle;
49   int len= (int)my_convert(buffer, buffer_size - 1, &my_charset_utf8_general_ci,
50                            str->ptr(), str->length(), str->charset(),
51                            (uint*) error);
52   buffer[len]= '\0';
53   return (len);
54 }
55 
56 /*
57   This service function deallocates the mysql_string_handle allocated on
58   server and used in plugins.
59 */
60 extern "C"
mysql_string_free(mysql_string_handle string_handle)61 void mysql_string_free(mysql_string_handle string_handle)
62 {
63   String *str= (String *) string_handle;
64   str->mem_free();
65   delete [] str;
66 }
67 
68 /*
69   This service function deallocates the mysql_string_iterator_handle
70   allocated on server and used in plugins.
71 */
72 extern "C"
mysql_string_iterator_free(mysql_string_iterator_handle iterator_handle)73 void mysql_string_iterator_free(mysql_string_iterator_handle iterator_handle)
74 {
75   my_free((string_iterator *) iterator_handle);
76 }
77 
78 /* This service function allocate mysql_string_iterator_handle and return it */
79 extern "C"
mysql_string_get_iterator(mysql_string_handle string_handle)80 mysql_string_iterator_handle mysql_string_get_iterator(mysql_string_handle
81                                                        string_handle)
82 {
83   String *str= (String *) string_handle;
84   string_iterator *iterator= (string_iterator *) my_malloc(key_memory_string_iterator,
85                                                            sizeof
86                                            (struct st_string_iterator), MYF(0));
87   iterator->iterator_str= str;
88   iterator->iterator_ptr= str->ptr();
89   iterator->ctype= 0;
90   return (iterator);
91 }
92 
93 /* Provide service which returns the next mysql_string_iterator_handle */
94 extern "C"
mysql_string_iterator_next(mysql_string_iterator_handle iterator_handle)95 int mysql_string_iterator_next(mysql_string_iterator_handle iterator_handle)
96 {
97   int char_len, char_type, tmp_len;
98   string_iterator *iterator= (string_iterator *) iterator_handle;
99   String *str= iterator->iterator_str;
100   const CHARSET_INFO *cs= str->charset();
101   char *end= (char*) str->ptr() + str->length();
102   if (iterator->iterator_ptr >= (const char*) end)
103     return (0);
104   char_len= (cs->cset->ctype(cs, &char_type, (uchar*) iterator->iterator_ptr,
105                              (uchar*) end));
106   iterator->ctype= char_type;
107   tmp_len= (char_len > 0 ? char_len : (char_len < 0 ? -char_len : 1));
108   if(iterator->iterator_ptr+tmp_len > end)
109     return (0);
110   else
111     iterator->iterator_ptr+= tmp_len;
112   return (1);
113 }
114 
115 /*
116   Provide service which calculate weather the current iterator_ptr points to
117   upper case character or not
118 */
119 extern "C"
mysql_string_iterator_isupper(mysql_string_iterator_handle iterator_handle)120 int mysql_string_iterator_isupper(mysql_string_iterator_handle iterator_handle)
121 {
122   string_iterator *iterator= (string_iterator *) iterator_handle;
123   return (iterator->ctype & _MY_U);
124 }
125 
126 /*
127   Provide service which calculate weather the current iterator_ptr points to
128   lower case character or not
129 */
130 extern "C"
mysql_string_iterator_islower(mysql_string_iterator_handle iterator_handle)131 int mysql_string_iterator_islower(mysql_string_iterator_handle iterator_handle)
132 {
133   string_iterator *iterator= (string_iterator *) iterator_handle;
134   return (iterator->ctype & _MY_L);
135 }
136 
137 /*
138   Provide service which calculate weather the current iterator_ptr points to
139   digit or not
140 */
141 extern "C"
mysql_string_iterator_isdigit(mysql_string_iterator_handle iterator_handle)142 int mysql_string_iterator_isdigit(mysql_string_iterator_handle iterator_handle)
143 {
144   string_iterator *iterator= (string_iterator *) iterator_handle;
145   return (iterator->ctype & _MY_NMR);
146 }
147 
148 /*
149   This function provide plugin service to convert a String pointed by handle to
150   lower case. Conversion depends on the client character set info
151 */
152 extern "C"
mysql_string_to_lowercase(mysql_string_handle string_handle)153 mysql_string_handle mysql_string_to_lowercase(mysql_string_handle string_handle)
154 {
155   String *str= (String *) string_handle;
156   String *res= new String[1];
157   const CHARSET_INFO *cs= str->charset();
158   if (cs->casedn_multiply == 1)
159   {
160     res->copy(*str);
161     my_casedn_str(cs, res->c_ptr_quick());
162   }
163   else
164   {
165     size_t len= str->length() * cs->casedn_multiply;
166     res->set_charset(cs);
167     res->alloc(len);
168     len= cs->cset->casedn(cs, (char*) str->ptr(), str->length(), (char *) res->ptr(), len);
169     res->length(len);
170   }
171   return (res);
172 }
173