-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathmysql_string_service.cc
491 lines (442 loc) · 15.4 KB
/
mysql_string_service.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include <string.h>
#include <sys/types.h>
#include "../../components/mysql_server/mysql_string_service.h"
#include "../../components/mysql_server/server_component.h"
#include "m_ctype.h"
#include "my_inttypes.h"
#include "my_sys.h"
#include "mysql/components/service_implementation.h"
#include "mysql/components/services/mysql_string.h"
#include "mysql/psi/psi_memory.h"
#include "mysql/service_mysql_alloc.h"
#include "sql_string.h"
PSI_memory_key key_memory_string_service_iterator;
/**
The string functions as a service to the mysql_server component.
So, that by default this service is available to all the components
register to the server.
*/
/**
Its a dummy initialization function. And it will be called from
mysql_component_infrastructure_init(). Else linker, is cutting out (as
library optimization) the string service code because libsql code is not
calling any functions of it.
*/
void mysql_string_services_init() { return; }
struct my_h_string_imp {};
struct my_h_string_iterator_imp {};
/**
Creates a new instance of string object
@param out_string holds pointer to newly created string object.
@return Status of performed operation
@retval false success
@retval true failure
*/
DEFINE_BOOL_METHOD(mysql_string_imp::create, (my_h_string * out_string)) {
try {
String *res = new String[1];
*out_string = (my_h_string)res;
return false;
} catch (...) {
mysql_components_handle_std_exception(__func__);
}
return true;
}
/**
Convert a String pointed by handle to lower case. Conversion depends on the
client character set info
@param out_string Holds the converted lower case string object.
@param in_string Pointer to string object to be converted.
@return Status of performed operation
@retval false success
@retval true failure
*/
DEFINE_BOOL_METHOD(mysql_string_imp::tolower,
(my_h_string * out_string, my_h_string in_string)) {
try {
String *str = reinterpret_cast<String *>(in_string);
if (str == NULL) return true;
String *res = reinterpret_cast<String *>(*out_string);
const CHARSET_INFO *cs = str->charset();
if (cs->casedn_multiply == 1) {
res->copy(*str);
my_casedn_str(cs, res->c_ptr_quick());
} else {
size_t len = str->length() * cs->casedn_multiply;
res->set_charset(cs);
res->alloc(len);
len = cs->cset->casedn(cs, (char *)str->ptr(), str->length(),
(char *)res->ptr(), len);
res->length(len);
}
*out_string = (my_h_string)res;
return false;
} catch (...) {
mysql_components_handle_std_exception(__func__);
}
return true;
}
/**
Convert a String pointed by handle to upper case. Conversion depends on the
client character set info
@param out_string Holds the converted lower case string object.
@param in_string Pointer to string object to be converted.
@return Status of performed operation
@retval false success
@retval true failure
*/
DEFINE_BOOL_METHOD(mysql_string_imp::toupper,
(my_h_string * out_string, my_h_string in_string)) {
try {
String *str = reinterpret_cast<String *>(in_string);
if (str == NULL) return true;
String *res = reinterpret_cast<String *>(*out_string);
const CHARSET_INFO *cs = str->charset();
if (cs->caseup_multiply == 1) {
res->copy(*str);
my_caseup_str(cs, res->c_ptr_quick());
} else {
size_t len = str->length() * cs->caseup_multiply;
res->set_charset(cs);
res->alloc(len);
len = cs->cset->caseup(cs, (char *)str->ptr(), str->length(),
(char *)res->ptr(), len);
res->length(len);
}
*out_string = (my_h_string)res;
return false;
} catch (...) {
mysql_components_handle_std_exception(__func__);
}
return true;
}
/**
alocates a string object and converts the character buffer to string
of specified charset_name.
call destroy() api to free the allocated string.
@param [out] out_string Pointer to string object handle to set new string
to.
@param in_buffer Pointer to the buffer with data to be interpreted as
string.
@param length Length of the buffer to copy in bytes, not in character count.
@param charset_name Handle to charset that is used for convertion.
@return Status of performed operation
@retval false success
@retval true failure
*/
DEFINE_BOOL_METHOD(mysql_string_imp::convert_from_buffer,
(my_h_string * out_string, const char *in_buffer,
uint64 length, const char *charset_name)) {
try {
if (in_buffer == NULL || length == 0 || length > strlen(in_buffer))
return true;
String *res = new String[1];
CHARSET_INFO *cs =
get_charset_by_csname(charset_name, MY_CS_PRIMARY, MYF(0));
if (!cs || res->copy(in_buffer, length, cs)) return true;
*out_string = (my_h_string)res;
return false;
} catch (...) {
mysql_components_handle_std_exception(__func__);
}
return true;
}
/**
converts the mysql_string to the character buffer specified by
charset_name parameter.
@param [out] out_buffer Pointer to char buffer used to hold the converted
string.
@param in_string pointer to string handle which will be converted to char
data.
@param length Length of the buffer to copy in bytes, not in character count.
@param charset_name Handle to charset that is used for convertion.
@return Status of performed operation
@retval false success
@retval true failure
*/
DEFINE_BOOL_METHOD(mysql_string_imp::convert_to_buffer,
(my_h_string in_string, char *out_buffer, uint64 length,
const char *charset_name)) {
try {
String *str = reinterpret_cast<String *>(in_string);
uint error;
CHARSET_INFO *cs =
get_charset_by_csname(charset_name, MY_CS_PRIMARY, MYF(0));
if (str == NULL || cs == NULL || length == 0) return true;
size_t len = my_convert(out_buffer, length - 1, cs, str->ptr(),
str->length(), str->charset(), &error);
out_buffer[len] = '\0';
return false;
} catch (...) {
mysql_components_handle_std_exception(__func__);
}
return true;
}
/**
Destroys specified string object and data contained by it.
@param string String object handle to release.
@return Status of performed operation
@retval false success
@retval true failure
*/
DEFINE_METHOD(void, mysql_string_imp::destroy, (my_h_string string)) {
try {
String *str = reinterpret_cast<String *>(string);
if (str == NULL) return;
str->mem_free();
delete[] str;
} catch (...) {
mysql_components_handle_std_exception(__func__);
}
}
/**
Gets character code of character on specified index position in
string to a specified buffer.
@param string String object handle to get character from.
@param index Index, position of character to query.
@param [out] out_char Pointer to long value to store character to.
@return Status of performed operation
@retval false success
@retval true failure
*/
DEFINE_BOOL_METHOD(mysql_string_imp::get_char,
(my_h_string string, uint index, ulong *out_char)) {
try {
String *str = reinterpret_cast<String *>(string);
if (str == NULL || index >= str->length()) return true;
my_charset_conv_mb_wc mb_wc = (str->charset())->cset->mb_wc;
int ret = str->charpos(index);
if (ret < 0) return true;
const char *ptr = (str->ptr() + ret);
if ((*mb_wc)(str->charset(), out_char, (uchar *)ptr,
(const uchar *)(str->ptr() + str->length())) <= 0)
return true;
return false;
} catch (...) {
mysql_components_handle_std_exception(__func__);
}
return true;
}
/**
Gets length of specified string expressed as number of characters.
@param string String object handle to get length of.
@param [out] out_length Pointer to 32bit value to store length of string to.
@return Status of performed operation
@retval false success
@retval true failure
*/
DEFINE_BOOL_METHOD(mysql_string_imp::get_char_length,
(my_h_string string, uint *out_length)) {
try {
String *str = reinterpret_cast<String *>(string);
if (str == NULL) return true;
*out_length = str->numchars();
return false;
} catch (...) {
mysql_components_handle_std_exception(__func__);
}
return true;
}
/**
Gets byte code of string on specified index position in
string to a specified 32-bit buffer.
@param string String object handle to get character from.
@param index Index, position of character to query.
@param [out] out_char Pointer to 32bit value to store byte to.
@return Status of performed operation
@retval false success
@retval true failure
*/
DEFINE_BOOL_METHOD(mysql_string_imp::get_byte,
(my_h_string string, uint index, uint *out_char)) {
try {
String *str = reinterpret_cast<String *>(string);
if (str == NULL || index >= str->length()) return true;
const char *ptr = str->ptr();
if (ptr == NULL) return true;
*out_char = ptr[index];
return false;
} catch (...) {
mysql_components_handle_std_exception(__func__);
}
return true;
}
/**
Gets length of specified string expressed as number of bytes.
@param string String object handle to get length of.
@param [out] out_length Pointer to 32bit value to store length of string to.
@return Status of performed operation
@retval false success
@retval true failure
*/
DEFINE_BOOL_METHOD(mysql_string_imp::get_byte_length,
(my_h_string string, uint *out_length)) {
try {
String *str = reinterpret_cast<String *>(string);
if (str == NULL) return true;
*out_length = str->length();
return false;
} catch (...) {
mysql_components_handle_std_exception(__func__);
}
return true;
}
/**
Creates an iterator for a specified string to allow iteration through all
characters in the string.
@param string String object handle to get iterator to.
@param [out] out_iterator Pointer to string iterator handle to store result
object to.
@return Status of performed operation
@retval false success
@retval true failure
*/
DEFINE_BOOL_METHOD(mysql_string_imp::iterator_create,
(my_h_string string, my_h_string_iterator *out_iterator)) {
try {
String *str = reinterpret_cast<String *>(string);
if (str == NULL) return true;
st_string_iterator *iterator = (st_string_iterator *)my_malloc(
key_memory_string_service_iterator, sizeof(st_string_iterator), MYF(0));
iterator->iterator_str = str;
iterator->iterator_ptr = str->ptr();
iterator->ctype = 0;
*out_iterator = (my_h_string_iterator)iterator;
return false;
} catch (...) {
mysql_components_handle_std_exception(__func__);
}
return true;
}
/**
Retrieves character code at current iterator position and advances the
iterator.
@param iter String iterator object handle to advance.
@param [out] out_char Pointer to 32bit value to store character to. May be
NULL to omit retrieval of character and just advance the iterator.
@return Status of performed operation
@retval false success
@retval true failure
*/
DEFINE_BOOL_METHOD(mysql_string_imp::iterator_get_next,
(my_h_string_iterator iter, int *out_char)) {
try {
int char_len, tmp_len;
st_string_iterator *iterator = (st_string_iterator *)iter;
if (iterator == NULL) return true;
String *str = iterator->iterator_str;
const CHARSET_INFO *cs = str->charset();
char *end = (char *)str->ptr() + str->length();
*out_char = 0;
if (iterator->iterator_ptr >= (const char *)end) return true;
char_len = (cs->cset->ctype(cs, out_char, (uchar *)iterator->iterator_ptr,
(uchar *)end));
iterator->ctype = *out_char;
tmp_len = (char_len > 0 ? char_len : (char_len < 0 ? -char_len : 1));
if (iterator->iterator_ptr + tmp_len > end)
return true;
else
iterator->iterator_ptr += tmp_len;
return false;
} catch (...) {
mysql_components_handle_std_exception(__func__);
}
return true;
}
/**
Releases the string iterator object specified.
@param iter String iterator object handle te release.
@return Status of performed operation
@retval false success
@retval true failure
*/
DEFINE_METHOD(void, mysql_string_imp::iterator_destroy,
(my_h_string_iterator iter)) {
try {
if (iter == NULL) return;
my_free((st_string_iterator *)iter);
} catch (...) {
mysql_components_handle_std_exception(__func__);
}
}
/**
Checks if character on current position the iterator points to is an upper
case.
@param iter String iterator object handle to advance.
@param [out] out Pointer to bool value to store if character is an upper
case.
@return Status of performed operation
@retval false success
@retval true failure
*/
DEFINE_BOOL_METHOD(mysql_string_imp::is_upper,
(my_h_string_iterator iter, bool *out)) {
try {
st_string_iterator *iterator = (st_string_iterator *)iter;
if (iterator == NULL) return true;
*out = (iterator->ctype & _MY_U);
return false;
} catch (...) {
mysql_components_handle_std_exception(__func__);
}
return true;
}
/**
Checks if character on current position the iterator points to is a lower
case.
@param iter String iterator object handle to advance.
@param [out] out Pointer to bool value to store if character is a lower
case.
@return Status of performed operation
@retval false success
@retval true failure
*/
DEFINE_BOOL_METHOD(mysql_string_imp::is_lower,
(my_h_string_iterator iter, bool *out)) {
try {
st_string_iterator *iterator = (st_string_iterator *)iter;
if (iterator == NULL) return true;
*out = (iterator->ctype & _MY_L);
return false;
} catch (...) {
mysql_components_handle_std_exception(__func__);
}
return true;
}
/**
Checks if character on current position the iterator points to is a digit.
@param iter String iterator object handle to advance.
@param [out] out Pointer to bool value to store if character is a digit.
@return Status of performed operation
@retval false success
@retval true failure
*/
DEFINE_BOOL_METHOD(mysql_string_imp::is_digit,
(my_h_string_iterator iter, bool *out)) {
try {
st_string_iterator *iterator = (st_string_iterator *)iter;
if (iterator == NULL) return true;
*out = (iterator->ctype & _MY_NMR);
return false;
} catch (...) {
mysql_components_handle_std_exception(__func__);
}
return true;
}