/usr/include/mysql/server/private
NameSizeModeActions
atomic/-0755rm
data/-0755rm
providers/-0755rm
aligned.h11360644editdlrm
aria_backup.h17870644editdlrm
assume_aligned.h23500644editdlrm
authors.h101410644editdlrm
backup.h17030644editdlrm
bounded_queue.h60930644editdlrm
client_settings.h19350644editdlrm
compat56.h22800644editdlrm
config.h145430644editdlrm
contributors.h48780644editdlrm
create_options.h41930644editdlrm
create_tmp_table.h28080644editdlrm
cset_narrowing.h39680644editdlrm
custom_conf.h10820644editdlrm
datadict.h17000644editdlrm
ddl_log.h128070644editdlrm
debug.h12340644editdlrm
debug_sync.h20460644editdlrm
derived_handler.h23790644editdlrm
derror.h9800644editdlrm
des_key_file.h12360644editdlrm
discover.h15700644editdlrm
dur_prop.h10820644editdlrm
embedded_priv.h17330644editdlrm
events.h47040644editdlrm
event_data_objects.h41870644editdlrm
event_db_repository.h36480644editdlrm
event_parse_data.h28990644editdlrm
event_queue.h34380644editdlrm
event_scheduler.h32900644editdlrm
field.h2223360644editdlrm
field_comp.h11740644editdlrm
filesort.h72830644editdlrm
filesort_utils.h81950644editdlrm
ft_global.h31130644editdlrm
gcalc_slicescan.h172720644editdlrm
gcalc_tools.h119000644editdlrm
grant.h27580644editdlrm
group_by_handler.h35340644editdlrm
gstream.h24370644editdlrm
handler.h1997300644editdlrm
handle_connections_win.h8840644editdlrm
hash.h44520644editdlrm
hash_filo.h56880644editdlrm
ha_handler_stats.h23350644editdlrm
ha_partition.h646960644editdlrm
ha_sequence.h62450644editdlrm
heap.h94800644editdlrm
hostname.h54190644editdlrm
ilist.h72370644editdlrm
init.h8520644editdlrm
innodb_priv.h13190644editdlrm
item.h2831880644editdlrm
item_cmpfunc.h1350710644editdlrm
item_create.h115080644editdlrm
item_func.h1386380644editdlrm
item_geofunc.h396120644editdlrm
item_jsonfunc.h252640644editdlrm
item_row.h52290644editdlrm
item_strfunc.h749950644editdlrm
item_subselect.h590280644editdlrm
item_sum.h726950644editdlrm
item_timefunc.h658960644editdlrm
item_vers.h44130644editdlrm
item_windowfunc.h345560644editdlrm
item_xmlfunc.h46500644editdlrm
json_table.h96670644editdlrm
key.h21320644editdlrm
keycaches.h19950644editdlrm
lex.h298400644editdlrm
lex_charset.h243130644editdlrm
lex_hash.h1431080644editdlrm
lex_ident.h21220644editdlrm
lex_string.h40770644editdlrm
lex_symbol.h13230644editdlrm
lex_token.h426410644editdlrm
lf.h64620644editdlrm
lock.h22200644editdlrm
log.h463700644editdlrm
log_event.h1891060644editdlrm
log_event_data_type.h18900644editdlrm
log_event_old.h198300644editdlrm
log_slow.h24420644editdlrm
maria.h58720644editdlrm
mariadb.h12770644editdlrm
mdl.h385030644editdlrm
mem_root_array.h71060644editdlrm
message.h11950644editdlrm
multi_range_read.h231790644editdlrm
myisam.h175060644editdlrm
myisamchk.h47340644editdlrm
myisammrg.h48970644editdlrm
myisampack.h149290644editdlrm
mysqld.h410630644editdlrm
mysqld_default_groups.h2040644editdlrm
mysqld_suffix.h12010644editdlrm
mysys_err.h30220644editdlrm
my_alarm.h24290644editdlrm
my_apc.h47470644editdlrm
my_atomic.h72810644editdlrm
my_atomic_wrapper.h30510644editdlrm
my_base.h276560644editdlrm
my_bit.h61960644editdlrm
my_bitmap.h55020644editdlrm
my_check_opt.h26180644editdlrm
my_compare.h111940644editdlrm
my_counter.h17210644editdlrm
my_cpu.h48550644editdlrm
my_crypt.h9040644editdlrm
my_decimal.h144890644editdlrm
my_default.h18800644editdlrm
my_handler_errors.h48820644editdlrm
my_json_writer.h185730644editdlrm
my_libwrap.h11830644editdlrm
my_md5.h14860644editdlrm
my_minidump.h8480644editdlrm
my_nosys.h14380644editdlrm
my_rdtsc.h101190644editdlrm
my_rnd.h10140644editdlrm
my_service_manager.h20870644editdlrm
my_stacktrace.h32150644editdlrm
my_stack_alloc.h64930644editdlrm
my_time.h104140644editdlrm
my_tree.h39910644editdlrm
my_uctype.h695280644editdlrm
my_user.h11260644editdlrm
my_virtual_mem.h11270644editdlrm
opt_histogram_json.h46380644editdlrm
opt_range.h659030644editdlrm
opt_subselect.h145450644editdlrm
opt_trace.h84940644editdlrm
opt_trace_context.h32910644editdlrm
parse_file.h43870644editdlrm
partition_element.h54280644editdlrm
partition_info.h198640644editdlrm
password.h11700644editdlrm
pfs_file_provider.h31530644editdlrm
pfs_idle_provider.h13850644editdlrm
pfs_memory_provider.h16260644editdlrm
pfs_metadata_provider.h18980644editdlrm
pfs_socket_provider.h22580644editdlrm
pfs_stage_provider.h15560644editdlrm
pfs_statement_provider.h43470644editdlrm
pfs_table_provider.h26250644editdlrm
pfs_thread_provider.h55600644editdlrm
pfs_transaction_provider.h28460644editdlrm
privilege.h288890644editdlrm
probes_mysql.h9730644editdlrm
probes_mysql_nodtrace.h60870644editdlrm
procedure.h68190644editdlrm
protocol.h125370644editdlrm
proxy_protocol.h5480644editdlrm
queues.h34770644editdlrm
records.h31470644editdlrm
replication.h161300644editdlrm
repl_failsafe.h15850644editdlrm
rijndael.h17110644editdlrm
rowid_filter.h154740644editdlrm
rpl_constants.h33570644editdlrm
rpl_filter.h47740644editdlrm
rpl_gtid.h299390644editdlrm
rpl_injector.h96210644editdlrm
rpl_mi.h166380644editdlrm
rpl_parallel.h182280644editdlrm
rpl_record.h15850644editdlrm
rpl_record_old.h14070644editdlrm
rpl_reporting.h37130644editdlrm
rpl_rli.h360700644editdlrm
rpl_tblmap.h31770644editdlrm
rpl_utility.h96300644editdlrm
scheduler.h31990644editdlrm
scope.h43930644editdlrm
select_handler.h22280644editdlrm
semisync.h22870644editdlrm
semisync_master.h257660644editdlrm
semisync_master_ack_receiver.h87090644editdlrm
semisync_slave.h37360644editdlrm
service_versions.h22850644editdlrm
session_tracker.h142750644editdlrm
set_var.h165510644editdlrm
slave.h122750644editdlrm
socketpair.h8420644editdlrm
source_revision.h670644editdlrm
sp.h225880644editdlrm
span.h39310644editdlrm
spatial.h226980644editdlrm
sp_cache.h20370644editdlrm
sp_head.h649170644editdlrm
sp_pcontext.h248930644editdlrm
sp_rcontext.h144410644editdlrm
sql_acl.h141670644editdlrm
sql_admin.h29150644editdlrm
sql_alloc.h17320644editdlrm
sql_alter.h153950644editdlrm
sql_analyse.h111250644editdlrm
sql_analyze_stmt.h126830644editdlrm
sql_array.h68580644editdlrm
sql_audit.h141620644editdlrm
sql_base.h260410644editdlrm
sql_basic_types.h95230644editdlrm
sql_binlog.h8950644editdlrm
sql_bitmap.h78420644editdlrm
sql_bootstrap.h18120644editdlrm
sql_cache.h216760644editdlrm
sql_callback.h15420644editdlrm
sql_class.h2711200644editdlrm
sql_cmd.h59620644editdlrm
sql_command.h48000644editdlrm
sql_connect.h40870644editdlrm
sql_const.h114150644editdlrm
sql_crypt.h14370644editdlrm
sql_cte.h164940644editdlrm
sql_cursor.h42390644editdlrm
sql_db.h24380644editdlrm
sql_debug.h56460644editdlrm
sql_delete.h13430644editdlrm
sql_derived.h12890644editdlrm
sql_digest.h38190644editdlrm
sql_digest_stream.h15670644editdlrm
sql_do.h9540644editdlrm
sql_error.h398430644editdlrm
sql_explain.h303190644editdlrm
sql_expression_cache.h43590644editdlrm
sql_get_diagnostics.h78830644editdlrm
sql_handler.h29100644editdlrm
sql_help.h9950644editdlrm
sql_hset.h34010644editdlrm
sql_insert.h51730644editdlrm
sql_i_s.h84870644editdlrm
sql_join_cache.h486590644editdlrm
sql_lex.h1746930644editdlrm
sql_lifo_buffer.h96760644editdlrm
sql_limit.h31870644editdlrm
sql_list.h223910644editdlrm
sql_load.h12760644editdlrm
sql_locale.h27010644editdlrm
sql_manager.h9600644editdlrm
sql_mode.h67350644editdlrm
sql_parse.h89700644editdlrm
sql_partition.h126740644editdlrm
sql_partition_admin.h59400644editdlrm
sql_plist.h77110644editdlrm
sql_plugin.h75770644editdlrm
sql_plugin_compat.h22370644editdlrm
sql_prepare.h114090644editdlrm
sql_priv.h184740644editdlrm
sql_profile.h78160644editdlrm
sql_reload.h10360644editdlrm
sql_rename.h9820644editdlrm
sql_repl.h30450644editdlrm
sql_schema.h33030644editdlrm
sql_select.h897430644editdlrm
sql_sequence.h51770644editdlrm
sql_servers.h17770644editdlrm
sql_show.h98300644editdlrm
sql_signal.h33620644editdlrm
sql_sort.h219640644editdlrm
sql_statistics.h164940644editdlrm
sql_string.h390090644editdlrm
sql_table.h97520644editdlrm
sql_test.h15890644editdlrm
sql_time.h83740644editdlrm
sql_trigger.h123320644editdlrm
sql_truncate.h20790644editdlrm
sql_tvc.h24180644editdlrm
sql_type.h2960720644editdlrm
sql_type_fixedbin.h655780644editdlrm
sql_type_fixedbin_storage.h54670644editdlrm
sql_type_geom.h190390644editdlrm
sql_type_int.h100010644editdlrm
sql_type_json.h61550644editdlrm
sql_type_real.h12570644editdlrm
sql_type_string.h16290644editdlrm
sql_udf.h48500644editdlrm
sql_union.h10680644editdlrm
sql_update.h19230644editdlrm
sql_view.h24700644editdlrm
sql_window.h68140644editdlrm
ssl_compat.h33800644editdlrm
strfunc.h22750644editdlrm
structs.h295300644editdlrm
sys_vars_shared.h27290644editdlrm
table.h1184140644editdlrm
table_cache.h42320644editdlrm
threadpool.h48100644editdlrm
threadpool_generic.h39690644editdlrm
threadpool_winsockets.h22900644editdlrm
thread_cache.h59050644editdlrm
thr_alarm.h29320644editdlrm
thr_lock.h73500644editdlrm
thr_malloc.h12020644editdlrm
thr_timer.h15630644editdlrm
transaction.h14660644editdlrm
tzfile.h50140644editdlrm
tztime.h33970644editdlrm
t_ctype.h56390644editdlrm
uniques.h42170644editdlrm
unireg.h77220644editdlrm
vers_string.h25430644editdlrm
violite.h100860644editdlrm
waiting_threads.h45320644editdlrm
welcome_copyright_notice.h12180644editdlrm
winservice.h60190644editdlrm
win_tzname_data.h65060644editdlrm
wqueue.h15650644editdlrm
wsrep.h33080644editdlrm
wsrep_allowlist_service.h10350644editdlrm
wsrep_applier.h27030644editdlrm
wsrep_binlog.h35510644editdlrm
wsrep_client_service.h25600644editdlrm
wsrep_client_state.h15660644editdlrm
wsrep_condition_variable.h14840644editdlrm
wsrep_high_priority_service.h49120644editdlrm
wsrep_mutex.h12390644editdlrm
wsrep_mysqld.h215240644editdlrm
wsrep_mysqld_c.h12270644editdlrm
wsrep_on.h17180644editdlrm
wsrep_priv.h16340644editdlrm
wsrep_schema.h56080644editdlrm
wsrep_server_service.h36310644editdlrm
wsrep_server_state.h25290644editdlrm
wsrep_sst.h39510644editdlrm
wsrep_status.h18160644editdlrm
wsrep_storage_service.h18090644editdlrm
wsrep_thd.h114870644editdlrm
wsrep_trans_observer.h181740644editdlrm
wsrep_types.h11100644editdlrm
wsrep_utils.h98070644editdlrm
wsrep_var.h44910644editdlrm
wsrep_xid.h15490644editdlrm
xa.h18450644editdlrm
Edit: /usr/include/mysql/server/private/sql_string.h (39009B)
#ifndef SQL_STRING_INCLUDED #define SQL_STRING_INCLUDED /* Copyright (c) 2000, 2013, Oracle and/or its affiliates. Copyright (c) 2008, 2020, MariaDB Corporation. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. 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 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-1335 USA */ /* This file is originally from the mysql distribution. Coded by monty */ #ifdef USE_PRAGMA_INTERFACE #pragma interface /* gcc class implementation */ #endif #include "m_ctype.h" /* my_charset_bin */ #include /* alloc_root, my_free, my_realloc */ #include "m_string.h" /* TRASH */ #include "sql_list.h" class String; #ifdef MYSQL_SERVER extern PSI_memory_key key_memory_String_value; #define STRING_PSI_MEMORY_KEY key_memory_String_value #else #define STRING_PSI_MEMORY_KEY PSI_NOT_INSTRUMENTED #endif typedef struct st_io_cache IO_CACHE; typedef struct st_mem_root MEM_ROOT; #define ASSERT_LENGTH(A) DBUG_ASSERT(str_length + (uint32) (A) <= Alloced_length) #include "pack.h" class Binary_string; int sortcmp(const Binary_string *s, const Binary_string *t, CHARSET_INFO *cs); int stringcmp(const Binary_string *s, const Binary_string *t); String *copy_if_not_alloced(String *a,String *b,uint32 arg_length); inline uint32 copy_and_convert(char *to, size_t to_length, CHARSET_INFO *to_cs, const char *from, size_t from_length, CHARSET_INFO *from_cs, uint *errors) { return my_convert(to, (uint)to_length, to_cs, from, (uint)from_length, from_cs, errors); } class String_copy_status: protected MY_STRCOPY_STATUS { public: const char *source_end_pos() const { return m_source_end_pos; } const char *well_formed_error_pos() const { return m_well_formed_error_pos; } }; class Well_formed_prefix_status: public String_copy_status { public: Well_formed_prefix_status(CHARSET_INFO *cs, const char *str, const char *end, size_t nchars) { cs->well_formed_char_length(str, end, nchars, this); } }; class Well_formed_prefix: public Well_formed_prefix_status { const char *m_str; // The beginning of the string public: Well_formed_prefix(CHARSET_INFO *cs, const char *str, const char *end, size_t nchars) :Well_formed_prefix_status(cs, str, end, nchars), m_str(str) { } Well_formed_prefix(CHARSET_INFO *cs, const char *str, size_t length, size_t nchars) :Well_formed_prefix_status(cs, str, str + length, nchars), m_str(str) { } Well_formed_prefix(CHARSET_INFO *cs, const char *str, size_t length) :Well_formed_prefix_status(cs, str, str + length, length), m_str(str) { } Well_formed_prefix(CHARSET_INFO *cs, LEX_CSTRING str, size_t nchars) :Well_formed_prefix_status(cs, str.str, str.str + str.length, nchars), m_str(str.str) { } size_t length() const { return m_source_end_pos - m_str; } }; class String_copier: public String_copy_status, protected MY_STRCONV_STATUS { public: const char *cannot_convert_error_pos() const { return m_cannot_convert_error_pos; } const char *most_important_error_pos() const { return well_formed_error_pos() ? well_formed_error_pos() : cannot_convert_error_pos(); } /* Convert a string between character sets. "dstcs" and "srccs" cannot be &my_charset_bin. */ size_t convert_fix(CHARSET_INFO *dstcs, char *dst, size_t dst_length, CHARSET_INFO *srccs, const char *src, size_t src_length, size_t nchars) { return my_convert_fix(dstcs, dst, dst_length, srccs, src, src_length, nchars, this, this); } /* Copy a string. Fix bad bytes/characters to '?'. */ uint well_formed_copy(CHARSET_INFO *to_cs, char *to, size_t to_length, CHARSET_INFO *from_cs, const char *from, size_t from_length, size_t nchars); // Same as above, but without the "nchars" limit. uint well_formed_copy(CHARSET_INFO *to_cs, char *to, size_t to_length, CHARSET_INFO *from_cs, const char *from, size_t from_length) { return well_formed_copy(to_cs, to, to_length, from_cs, from, from_length, from_length /* No limit on "nchars"*/); } }; size_t my_copy_with_hex_escaping(CHARSET_INFO *cs, char *dst, size_t dstlen, const char *src, size_t srclen); uint convert_to_printable(char *to, size_t to_len, const char *from, size_t from_len, CHARSET_INFO *from_cs, size_t nbytes= 0); size_t convert_to_printable_required_length(uint len); class Charset { CHARSET_INFO *m_charset; public: Charset() :m_charset(&my_charset_bin) { } Charset(CHARSET_INFO *cs) :m_charset(cs) { } CHARSET_INFO *charset() const { return m_charset; } bool use_mb() const { return m_charset->use_mb(); } uint mbminlen() const { return m_charset->mbminlen; } uint mbmaxlen() const { return m_charset->mbmaxlen; } bool is_good_for_ft() const { // Binary and UCS2/UTF16/UTF32 are not supported return m_charset != &my_charset_bin && m_charset->mbminlen == 1; } size_t numchars(const char *str, const char *end) const { return m_charset->numchars(str, end); } size_t lengthsp(const char *str, size_t length) const { return m_charset->lengthsp(str, length); } size_t charpos(const char *str, const char *end, size_t pos) const { return m_charset->charpos(str, end, pos); } void set_charset(CHARSET_INFO *charset_arg) { m_charset= charset_arg; } void set_charset(const Charset &other) { m_charset= other.m_charset; } void swap(Charset &other) { swap_variables(CHARSET_INFO*, m_charset, other.m_charset); } bool same_encoding(const Charset &other) const { return my_charset_same(m_charset, other.m_charset); } /* Collation name without the character set name. For example, in case of "latin1_swedish_ci", this method returns "_swedish_ci". */ LEX_CSTRING collation_specific_name() const; bool encoding_allows_reinterpret_as(CHARSET_INFO *cs) const; bool eq_collation_specific_names(CHARSET_INFO *cs) const; bool can_have_collate_clause() const { return m_charset != &my_charset_bin; } /* The MariaDB version when the last collation change happened, e.g. due to a bug fix. See functions below. */ static ulong latest_mariadb_version_with_collation_change() { return 110002; } /* Check if the collation with the given ID changed its order since the given MariaDB version. */ static bool collation_changed_order(ulong mysql_version, uint cs_number) { if ((mysql_version < 50048 && (cs_number == 11 || /* ascii_general_ci - bug #29499, bug #27562 */ cs_number == 41 || /* latin7_general_ci - bug #29461 */ cs_number == 42 || /* latin7_general_cs - bug #29461 */ cs_number == 20 || /* latin7_estonian_cs - bug #29461 */ cs_number == 21 || /* latin2_hungarian_ci - bug #29461 */ cs_number == 22 || /* koi8u_general_ci - bug #29461 */ cs_number == 23 || /* cp1251_ukrainian_ci - bug #29461 */ cs_number == 26)) || /* cp1250_general_ci - bug #29461 */ (mysql_version < 50124 && (cs_number == 33 || /* utf8mb3_general_ci - bug #27877 */ cs_number == 35))) /* ucs2_general_ci - bug #27877 */ return true; if (cs_number == 159 && /* ucs2_general_mysql500_ci - MDEV-30746 */ ((mysql_version >= 100400 && mysql_version < 100429) || (mysql_version >= 100500 && mysql_version < 100520) || (mysql_version >= 100600 && mysql_version < 100613) || (mysql_version >= 100700 && mysql_version < 100708) || (mysql_version >= 100800 && mysql_version < 100808) || (mysql_version >= 100900 && mysql_version < 100906) || (mysql_version >= 101000 && mysql_version < 101004) || (mysql_version >= 101100 && mysql_version < 101103) || (mysql_version >= 110000 && mysql_version < 110002))) return true; return false; } /** Check if a collation has changed ID since the given version. Return the new ID. @param mysql_version @param cs_number - collation ID @retval the new collation ID (or cs_number, if no change) */ static uint upgrade_collation_id(ulong mysql_version, uint cs_number) { if (mysql_version >= 50300 && mysql_version <= 50399) { switch (cs_number) { case 149: return MY_PAGE2_COLLATION_ID_UCS2; // ucs2_crotian_ci case 213: return MY_PAGE2_COLLATION_ID_UTF8; // utf8_crotian_ci } } if ((mysql_version >= 50500 && mysql_version <= 50599) || (mysql_version >= 100000 && mysql_version <= 100005)) { switch (cs_number) { case 149: return MY_PAGE2_COLLATION_ID_UCS2; // ucs2_crotian_ci case 213: return MY_PAGE2_COLLATION_ID_UTF8; // utf8_crotian_ci case 214: return MY_PAGE2_COLLATION_ID_UTF32; // utf32_croatian_ci case 215: return MY_PAGE2_COLLATION_ID_UTF16; // utf16_croatian_ci case 245: return MY_PAGE2_COLLATION_ID_UTF8MB4;// utf8mb4_croatian_ci } } return cs_number; } }; /** Storage for strings with both length and allocated length. Automatically grows on demand. */ class Binary_string: public Sql_alloc { protected: char *Ptr; uint32 str_length, Alloced_length, extra_alloc; bool alloced, thread_specific; void init_private_data() { Ptr= 0; Alloced_length= extra_alloc= str_length= 0; alloced= thread_specific= false; } inline void free_buffer() { if (alloced) { alloced=0; my_free(Ptr); } } public: Binary_string() { init_private_data(); } explicit Binary_string(size_t length_arg) { init_private_data(); (void) real_alloc(length_arg); } /* NOTE: If one intend to use the c_ptr() method, the following two contructors need the size of memory for STR to be at least LEN+1 (to make room for zero termination). */ Binary_string(const char *str, size_t len) { Ptr= (char*) str; str_length= (uint32) len; Alloced_length= 0; /* Memory cannot be written to */ extra_alloc= 0; alloced= thread_specific= 0; } Binary_string(char *str, size_t len) { Ptr= str; str_length= Alloced_length= (uint32) len; extra_alloc= 0; alloced= thread_specific= 0; } explicit Binary_string(const Binary_string &str) { Ptr= str.Ptr; str_length= str.str_length; Alloced_length= str.Alloced_length; extra_alloc= 0; alloced= thread_specific= 0; } ~Binary_string() { free(); } inline uint32 length() const { return str_length;} inline char& operator [] (size_t i) const { return Ptr[i]; } inline void length(size_t len) { str_length=(uint32)len ; } inline bool is_empty() const { return (str_length == 0); } inline const char *ptr() const { return Ptr; } inline const char *end() const { return Ptr + str_length; } bool has_8bit_bytes() const { for (const char *c= ptr(), *c_end= end(); c < c_end; c++) { if (!my_isascii(*c)) return true; } return false; } bool bin_eq(const Binary_string *other) const { return length() == other->length() && !memcmp(ptr(), other->ptr(), length()); } // Returns offset to substring or -1 int strstr(const Binary_string &search, uint32 offset=0) const; int strstr(const char *search, uint32 search_length, uint32 offset=0) const; // Returns offset to substring or -1 int strrstr(const Binary_string &search, uint32 offset=0) const; /* The following append operations do not extend the strings and in production mode do NOT check that alloced memory! q_*** methods writes values of parameters itself qs_*** methods writes string representation of value */ void q_append(const char c) { ASSERT_LENGTH(1); Ptr[str_length++] = c; } void q_append2b(const uint32 n) { ASSERT_LENGTH(2); int2store(Ptr + str_length, n); str_length += 2; } void q_append(const uint32 n) { ASSERT_LENGTH(4); int4store(Ptr + str_length, n); str_length += 4; } void q_append(double d) { ASSERT_LENGTH(8); float8store(Ptr + str_length, d); str_length += 8; } void q_append(double *d) { ASSERT_LENGTH(8); float8store(Ptr + str_length, *d); str_length += 8; } /* Append a wide character. The caller must have allocated at least cs->mbmaxlen bytes. */ int q_append_wc(my_wc_t wc, CHARSET_INFO *cs) { int mblen; if ((mblen= cs->cset->wc_mb(cs, wc, (uchar *) end(), (uchar *) end() + cs->mbmaxlen)) > 0) str_length+= (uint32) mblen; return mblen; } void q_append(const char *data, size_t data_len) { ASSERT_LENGTH(data_len); if (data_len) memcpy(Ptr + str_length, data, data_len); DBUG_ASSERT(str_length <= UINT_MAX32 - data_len); str_length += (uint)data_len; } void q_append(const LEX_CSTRING *ls) { DBUG_ASSERT(ls->length < UINT_MAX32 && ((ls->length == 0 && !ls->str) || ls->length == strlen(ls->str))); q_append(ls->str, (uint32) ls->length); } void write_at_position(uint32 position, uint32 value) { DBUG_ASSERT(str_length >= position + 4); int4store(Ptr + position,value); } void qs_append(const LEX_CSTRING *ls) { DBUG_ASSERT(ls->length < UINT_MAX32 && ((ls->length == 0 && !ls->str) || ls->length == strlen(ls->str))); qs_append(ls->str, (uint32)ls->length); } void qs_append(const char *str, size_t len); void qs_append_hex(const char *str, uint32 len); void qs_append_hex_uint32(uint32 num); void qs_append(double d); void qs_append(const double *d); inline void qs_append(const char c) { ASSERT_LENGTH(1); Ptr[str_length]= c; str_length++; } void qs_append(int i); void qs_append(uint i) { qs_append((ulonglong)i); } void qs_append(ulong i) { qs_append((ulonglong)i); } void qs_append(ulonglong i); void qs_append(longlong i, int radix) { ASSERT_LENGTH(22); char *buff= Ptr + str_length; char *end= ll2str(i, buff, radix, 0); str_length+= (uint32) (end-buff); } /* Mark variable thread specific it it's not allocated already */ inline void set_thread_specific() { if (!alloced) thread_specific= 1; } bool is_alloced() const { return alloced; } inline uint32 alloced_length() const { return Alloced_length;} inline uint32 extra_allocation() const { return extra_alloc;} inline void extra_allocation(size_t len) { extra_alloc= (uint32)len; } inline void mark_as_const() { Alloced_length= 0;} inline bool uses_buffer_owned_by(const Binary_string *s) const { return (s->alloced && Ptr >= s->Ptr && Ptr < s->Ptr + s->Alloced_length); } /* Swap two string objects. Efficient way to exchange data without memcpy. */ void swap(Binary_string &s) { swap_variables(char *, Ptr, s.Ptr); swap_variables(uint32, str_length, s.str_length); swap_variables(uint32, Alloced_length, s.Alloced_length); swap_variables(bool, alloced, s.alloced); } /** Points the internal buffer to the supplied one. The old buffer is freed. @param str Pointer to the new buffer. @param arg_length Length of the new buffer in characters, excluding any null character. @note The new buffer will not be null terminated. */ void set_alloced(char *str, size_t length, size_t alloced_length) { free_buffer(); Ptr= str; str_length= (uint32) length; DBUG_ASSERT(alloced_length < UINT_MAX32); Alloced_length= (uint32) alloced_length; } inline void set(char *str, size_t arg_length) { set_alloced(str, arg_length, arg_length); } inline void set(const char *str, size_t length) { free_buffer(); Ptr= (char*) str; str_length= (uint32) length; Alloced_length= 0; } void set(Binary_string &str, size_t offset, size_t length) { DBUG_ASSERT(&str != this); free_buffer(); Ptr= str.Ptr + offset; str_length= (uint32) length; Alloced_length= 0; if (str.Alloced_length) Alloced_length= (uint32) (str.Alloced_length - offset); } LEX_CSTRING to_lex_cstring() const { LEX_CSTRING tmp= {Ptr, str_length}; return tmp; } inline LEX_CSTRING *get_value(LEX_CSTRING *res) const { res->str= Ptr; res->length= str_length; return res; } /* Take over handling of buffer from some other object */ void reset(char *ptr_arg, size_t length_arg, size_t alloced_length_arg) { set_alloced(ptr_arg, length_arg, alloced_length_arg); alloced= ptr_arg != 0; } /* Forget about the buffer, let some other object handle it */ char *release() { char *old= Ptr; init_private_data(); return old; } /* This is used to set a new buffer for String. However if the String already has an allocated buffer, it will keep that one. It's not to be used to set the value or length of the string. */ inline void set_buffer_if_not_allocated(char *str, size_t arg_length) { if (!alloced) { /* Following should really set str_length= 0, but some code may depend on that the String length is same as buffer length. */ Ptr= str; str_length= Alloced_length= (uint32) arg_length; } /* One should set str_length before using it */ MEM_UNDEFINED(&str_length, sizeof(str_length)); } inline Binary_string& operator=(const Binary_string &s) { if (&s != this) { /* It is forbidden to do assignments like some_string = substring_of_that_string */ DBUG_ASSERT(!s.uses_buffer_owned_by(this)); set_alloced((char *) s.Ptr, s.str_length, s.Alloced_length); } return *this; } bool set_hex(ulonglong num); bool set_hex(const char *str, uint32 len); bool set_fcvt(double num, uint decimals); bool copy(); // Alloc string if not alloced bool copy(const Binary_string &s); // Allocate new string bool copy(const char *s, size_t arg_length); // Allocate new string bool copy_or_move(const char *s,size_t arg_length); /** Convert a string to a printable format. All non-convertable and control characters are replaced to 5-character sequences '\hhhh'. */ bool copy_printable_hhhh(CHARSET_INFO *to_cs, CHARSET_INFO *from_cs, const char *from, size_t from_length); bool append_ulonglong(ulonglong val); bool append_longlong(longlong val); bool append(const char *s, size_t size) { if (!size) return false; if (realloc_with_extra_if_needed(str_length + size)) return true; q_append(s, size); return false; } bool append(const LEX_CSTRING &s) { return append(s.str, s.length); } bool append(const Binary_string &s) { return append(s.ptr(), s.length()); } bool append(IO_CACHE* file, uint32 arg_length); inline bool append_char(char chr) { if (str_length < Alloced_length) { Ptr[str_length++]= chr; } else { if (unlikely(realloc_with_extra(str_length + 1))) return true; Ptr[str_length++]= chr; } return false; } bool append_hex(const char *src, uint32 srclen) { for (const char *src_end= src + srclen ; src != src_end ; src++) { if (unlikely(append_char(_dig_vec_lower[((uchar) *src) >> 4])) || unlikely(append_char(_dig_vec_lower[((uchar) *src) & 0x0F]))) return true; } return false; } bool append_hex_uint32(uint32 num) { if (reserve(8)) return true; qs_append_hex_uint32(num); return false; } bool append_with_step(const char *s, uint32 arg_length, uint32 step_alloc) { uint32 new_length= arg_length + str_length; if (new_length > Alloced_length && unlikely(realloc(new_length + step_alloc))) return true; q_append(s, arg_length); return false; } inline char *c_ptr() { if (unlikely(!Ptr)) return (char*) ""; /* Here we assume that any buffer used to initalize String has an end \0 or have at least an accessable character at end. This is to handle the case of String("Hello",5) and String("hello",5) efficiently. We have two options here. To test for !Alloced_length or !alloced. Using "Alloced_length" is slightly safer so that we do not read from potentially unintialized memory (normally not dangerous but may give warnings in valgrind), but "alloced" is safer as there are less change to get memory loss from code that is using String((char*), length) or String.set((char*), length) and does not free things properly (and there is several places in the code where this happens and it is hard to find out if any of these will call c_ptr(). */ if (unlikely(!alloced && !Ptr[str_length])) return Ptr; if (str_length < Alloced_length) { Ptr[str_length]=0; return Ptr; } (void) realloc(str_length); /* This will add end \0 */ return Ptr; } /* One should use c_ptr() instead for most cases. This will be deleted soon, kept for compatiblity. */ inline char *c_ptr_quick() { return c_ptr_safe(); } /* This is to be used only in the case when one cannot use c_ptr(). The cases are: - When one initializes String with an external buffer and length and buffer[length] could be uninitalized when c_ptr() is called. - When valgrind gives warnings about uninitialized memory with c_ptr(). */ inline char *c_ptr_safe() { if (Ptr && str_length < Alloced_length) Ptr[str_length]=0; else (void) realloc(str_length); return Ptr; } inline void free() { free_buffer(); /* We have to clear the values as some Strings, like in Field, are reused after free(). Because of this we cannot use MEM_UNDEFINED() here. */ Ptr= 0; str_length= 0; Alloced_length= extra_alloc= 0; } inline bool alloc(size_t arg_length) { /* Allocate if we need more space or if we don't have done any allocation yet (we don't want to have Ptr to be NULL for empty strings). Note that if arg_length == Alloced_length then we don't allocate. This ensures we don't do any extra allocations in protocol and String:int, but the string will not be automatically null terminated if c_ptr() is not called. */ if (arg_length <= Alloced_length && Alloced_length) return 0; return real_alloc(arg_length); } bool real_alloc(size_t arg_length); // Empties old string bool realloc_raw(size_t arg_length); bool realloc(size_t arg_length) { if (realloc_raw(arg_length+1)) return TRUE; Ptr[arg_length]= 0; // This make other funcs shorter return FALSE; } bool realloc_with_extra(size_t arg_length) { if (extra_alloc < 4096) extra_alloc= extra_alloc*2+128; if (realloc_raw(arg_length + extra_alloc)) return TRUE; Ptr[arg_length]=0; // This make other funcs shorter return FALSE; } bool realloc_with_extra_if_needed(size_t arg_length) { if (arg_length < Alloced_length) { Ptr[arg_length]=0; // behave as if realloc was called. return 0; } return realloc_with_extra(arg_length); } // Shrink the buffer, but only if it is allocated on the heap. void shrink(size_t arg_length); void move(Binary_string &s) { set_alloced(s.Ptr, s.str_length, s.Alloced_length); extra_alloc= s.extra_alloc; alloced= s.alloced; thread_specific= s.thread_specific; s.alloced= 0; } bool fill(size_t max_length,char fill); /* Replace substring with string If wrong parameter or not enough memory, do nothing */ bool replace(uint32 offset,uint32 arg_length, const char *to, uint32 length); bool replace(uint32 offset,uint32 arg_length, const Binary_string &to) { return replace(offset,arg_length,to.ptr(),to.length()); } int reserve(size_t space_needed) { DBUG_ASSERT((ulonglong) str_length + space_needed < UINT_MAX32); return realloc(str_length + space_needed); } int reserve(size_t space_needed, size_t grow_by); inline char *prep_append(uint32 arg_length, uint32 step_alloc) { uint32 new_length= arg_length + str_length; if (new_length > Alloced_length) { if (unlikely(realloc(new_length + step_alloc))) return 0; } uint32 old_length= str_length; str_length+= arg_length; return Ptr + old_length; // Area to use } void q_net_store_length(ulonglong length) { DBUG_ASSERT(Alloced_length >= (str_length + net_length_size(length))); char *pos= (char *) net_store_length((uchar *)(Ptr + str_length), length); str_length= uint32(pos - Ptr); } void q_net_store_data(const uchar *from, size_t length) { DBUG_ASSERT(length < UINT_MAX32); DBUG_ASSERT(Alloced_length >= (str_length + length + net_length_size(length))); q_net_store_length(length); q_append((const char *)from, (uint32) length); } }; class String: public Charset, public Binary_string { public: String() = default; String(size_t length_arg) :Binary_string(length_arg) { } /* NOTE: If one intend to use the c_ptr() method, the following two contructors need the size of memory for STR to be at least LEN+1 (to make room for zero termination). */ String(const char *str, size_t len, CHARSET_INFO *cs) :Charset(cs), Binary_string(str, len) { } String(char *str, size_t len, CHARSET_INFO *cs) :Charset(cs), Binary_string(str, len) { } String(const String &str) = default; String(String &&str) noexcept :Charset(std::move(str)), Binary_string(std::move(str)){} void set(String &str,size_t offset,size_t arg_length) { Binary_string::set(str, offset, arg_length); set_charset(str); } inline void set(char *str,size_t arg_length, CHARSET_INFO *cs) { Binary_string::set(str, arg_length); set_charset(cs); } inline void set(const char *str,size_t arg_length, CHARSET_INFO *cs) { Binary_string::set(str, arg_length); set_charset(cs); } bool set_ascii(const char *str, size_t arg_length); inline void set_buffer_if_not_allocated(char *str,size_t arg_length, CHARSET_INFO *cs) { Binary_string::set_buffer_if_not_allocated(str, arg_length); set_charset(cs); } bool set_int(longlong num, bool unsigned_flag, CHARSET_INFO *cs); bool set(int num, CHARSET_INFO *cs) { return set_int(num, false, cs); } bool set(uint num, CHARSET_INFO *cs) { return set_int(num, true, cs); } bool set(long num, CHARSET_INFO *cs) { return set_int(num, false, cs); } bool set(ulong num, CHARSET_INFO *cs) { return set_int(num, true, cs); } bool set(longlong num, CHARSET_INFO *cs) { return set_int(num, false, cs); } bool set(ulonglong num, CHARSET_INFO *cs) { return set_int((longlong)num, true, cs); } bool set_real(double num,uint decimals, CHARSET_INFO *cs); bool set_fcvt(double num, uint decimals) { set_charset(&my_charset_latin1); return Binary_string::set_fcvt(num, decimals); } bool set_hex(ulonglong num) { set_charset(&my_charset_latin1); return Binary_string::set_hex(num); } bool set_hex(const char *str, uint32 len) { set_charset(&my_charset_latin1); return Binary_string::set_hex(str, len); } /* Take over handling of buffer from some other object */ void reset(char *ptr_arg, size_t length_arg, size_t alloced_length_arg, CHARSET_INFO *cs) { Binary_string::reset(ptr_arg, length_arg, alloced_length_arg); set_charset(cs); } inline String& operator = (const String &s) { if (&s != this) { set_charset(s); Binary_string::operator=(s); } return *this; } bool copy() { return Binary_string::copy(); } bool copy(const String &s) { set_charset(s); return Binary_string::copy(s); } bool copy(const char *s, size_t arg_length, CHARSET_INFO *cs) { set_charset(cs); return Binary_string::copy(s, arg_length); } bool copy_or_move(const char *s, size_t arg_length, CHARSET_INFO *cs) { set_charset(cs); return Binary_string::copy_or_move(s, arg_length); } static bool needs_conversion(size_t arg_length, CHARSET_INFO *cs_from, CHARSET_INFO *cs_to, uint32 *offset); static bool needs_conversion_on_storage(size_t arg_length, CHARSET_INFO *cs_from, CHARSET_INFO *cs_to); bool copy_aligned(const char *s, size_t arg_length, size_t offset, CHARSET_INFO *cs); bool set_or_copy_aligned(const char *s, size_t arg_length, CHARSET_INFO *cs); bool can_be_safely_converted_to(CHARSET_INFO *tocs) const { if (charset() == &my_charset_bin) return Well_formed_prefix(tocs, ptr(), length()).length() == length(); String try_val; uint try_conv_error= 0; try_val.copy(ptr(), length(), charset(), tocs, &try_conv_error); return try_conv_error == 0; } bool copy(const char*s, size_t arg_length, CHARSET_INFO *csfrom, CHARSET_INFO *csto, uint *errors); bool copy(const String *str, CHARSET_INFO *tocs, uint *errors) { return copy(str->ptr(), str->length(), str->charset(), tocs, errors); } bool copy(CHARSET_INFO *tocs, CHARSET_INFO *fromcs, const char *src, size_t src_length, size_t nchars, String_copier *copier) { if (unlikely(alloc(tocs->mbmaxlen * src_length))) return true; str_length= copier->well_formed_copy(tocs, Ptr, alloced_length(), fromcs, src, (uint) src_length, (uint) nchars); set_charset(tocs); return false; } // Append without character set conversion bool append(const String &s) { return Binary_string::append(s); } inline bool append(char chr) { return append(&chr, 1); } bool append_hex(const char *src, uint32 srclen) { return Binary_string::append_hex(src, srclen); } bool append_hex(const uchar *src, uint32 srclen) { return Binary_string::append_hex((const char*)src, srclen); } bool append_introducer_and_hex(const String *str) { return append('_') || append(str->charset()->cs_name) || append(STRING_WITH_LEN(" 0x")) || append_hex(str->ptr(), (uint32) str->length()); } bool append(IO_CACHE* file, uint32 arg_length) { return Binary_string::append(file, arg_length); } inline bool append(const char *s, uint32 arg_length, uint32 step_alloc) { return append_with_step(s, arg_length, step_alloc); } // Append with optional character set conversion from ASCII (e.g. to UCS2) bool append(const LEX_STRING *ls) { DBUG_ASSERT(ls->length < UINT_MAX32 && ((ls->length == 0 && !ls->str) || ls->length == strlen(ls->str))); return append(ls->str, (uint32) ls->length); } bool append(const LEX_CSTRING *ls) { DBUG_ASSERT(ls->length < UINT_MAX32 && ((ls->length == 0 && !ls->str) || ls->length == strlen(ls->str))); return append(ls->str, (uint32) ls->length); } bool append(const LEX_CSTRING &ls) { return append(&ls); } bool append_name_value(const LEX_CSTRING &name, const LEX_CSTRING &value, uchar quot= '\0') { return append(name) || append('=') || (quot && append(quot)) || append(value) || (quot && append(quot)); } bool append(const char *s, size_t size); bool append_parenthesized(long nr, int radix= 10); // Append with optional character set conversion from cs to charset() bool append(const char *s, size_t arg_length, CHARSET_INFO *cs); bool append(const LEX_CSTRING &s, CHARSET_INFO *cs) { return append(s.str, s.length, cs); } // Append a wide character bool append_wc(my_wc_t wc) { if (reserve(mbmaxlen())) return true; int mblen= q_append_wc(wc, charset()); if (mblen > 0) return false; else if (mblen == MY_CS_ILUNI && wc != '?') return q_append_wc('?', charset()) <= 0; return true; } // Append a number with zero prefilling bool append_zerofill(uint num, uint width) { static const char zeros[15]= "00000000000000"; char intbuff[15]; uint length= (uint) (int10_to_str(num, intbuff, 10) - intbuff); if (length < width && append(zeros, width - length, &my_charset_latin1)) return true; return append(intbuff, length, &my_charset_latin1); } /* Append a bitmask in an uint32 with a translation into a C-style human readable representation, e.g.: 0x05 -> "(flag04|flag01)" @param flags - the flags to translate @param names - an array of flag names @param count - the number of available elements in "names" */ bool append_flag32_names(uint32 flags, LEX_CSTRING names[], size_t count) { bool added= false; if (flags && append('(')) return true; for (ulong i= 0; i <= 31; i++) { ulong bit= 31 - i; if (flags & (1 << bit)) { if (added && append('|')) return true; if (bit < count ? append(names[bit]) : append('?')) return true; added= true; } } if (flags && append(')')) return true; return false; } inline void chop() { if (str_length) { str_length--; str_length= well_formed_length(); } } void strip_sp(); friend String *copy_if_not_alloced(String *a,String *b,uint32 arg_length); friend class Field; uint32 numchars() const { return (uint32) Charset::numchars(ptr(), end()); } int charpos(longlong i, uint32 offset=0) { if (i <= 0) return (int) i; return (int) Charset::charpos(ptr() + offset, end(), (size_t) i); } size_t lengthsp() const { return Charset::lengthsp(Ptr, str_length); } void print(String *to) const; void print_with_conversion(String *to, CHARSET_INFO *cs) const; void print(String *to, CHARSET_INFO *cs) const { if (my_charset_same(charset(), cs)) print(to); else print_with_conversion(to, cs); } static my_wc_t escaped_wc_for_single_quote(my_wc_t ch) { switch (ch) { case '\\': return '\\'; case '\0': return '0'; case '\'': return '\''; case '\n': return 'n'; case '\r': return 'r'; case '\032': return 'Z'; } return 0; } // Append for single quote using mb_wc/wc_mb Unicode conversion bool append_for_single_quote_using_mb_wc(const char *str, size_t length, CHARSET_INFO *cs); // Append for single quote with optional mb_wc/wc_mb conversion bool append_for_single_quote_opt_convert(const char *str, size_t length, CHARSET_INFO *cs) { return charset() == &my_charset_bin || cs == &my_charset_bin || my_charset_same(charset(), cs) ? append_for_single_quote(str, length) : append_for_single_quote_using_mb_wc(str, length, cs); } bool append_for_single_quote_opt_convert(const String &str) { return append_for_single_quote_opt_convert(str.ptr(), str.length(), str.charset()); } bool append_for_single_quote(const char *st, size_t len); bool append_for_single_quote(const String *s) { return append_for_single_quote(s->ptr(), s->length()); } void swap(String &s) { Charset::swap(s); Binary_string::swap(s); } uint well_formed_length() const { return (uint) Well_formed_prefix(charset(), ptr(), length()).length(); } bool is_ascii() const { if (length() == 0) return TRUE; if (charset()->mbminlen > 1) return FALSE; return !has_8bit_bytes(); } bool eq(const String *other, CHARSET_INFO *cs) const { return !sortcmp(this, other, cs); } private: bool append_semi_hex(const char *s, uint len, CHARSET_INFO *cs); }; // The following class is a backport from MySQL 5.6: /** String class wrapper with a preallocated buffer of size buff_sz This class allows to replace sequences of: char buff[12345]; String str(buff, sizeof(buff)); str.length(0); with a simple equivalent declaration: StringBuffer<12345> str; */ template class StringBuffer : public String { char buff[buff_sz]; public: StringBuffer() : String(buff, buff_sz, &my_charset_bin) { length(0); } explicit StringBuffer(CHARSET_INFO *cs) : String(buff, buff_sz, cs) { length(0); } void set_buffer_if_not_allocated(CHARSET_INFO *cs) { if (!is_alloced()) { Ptr= buff; Alloced_length= (uint32) buff_sz; } str_length= 0; /* Safety, not required */ /* One should set str_length before using it */ MEM_UNDEFINED(&str_length, sizeof(str_length)); set_charset(cs); } }; template class BinaryStringBuffer : public Binary_string { char buff[buff_sz]; public: BinaryStringBuffer() : Binary_string(buff, buff_sz) { length(0); } }; static inline bool check_if_only_end_space(CHARSET_INFO *cs, const char *str, const char *end) { return str + cs->scan(str, end, MY_SEQ_SPACES) == end; } int append_query_string(CHARSET_INFO *csinfo, String *to, const char *str, size_t len, bool no_backslash); #endif /* SQL_STRING_INCLUDED */