2001-12-06 13:10:51 +01:00
|
|
|
/* Copyright (C) 2000 MySQL AB
|
|
|
|
|
|
|
|
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
|
2006-12-23 20:17:15 +01:00
|
|
|
the Free Software Foundation; version 2 of the License.
|
2001-12-06 13:10:51 +01:00
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
2000-07-31 21:29:14 +02:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2001-12-06 13:10:51 +01:00
|
|
|
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
2000-07-31 21:29:14 +02:00
|
|
|
|
|
|
|
#include "mysys_priv.h"
|
|
|
|
#include "mysys_err.h"
|
|
|
|
#include <m_ctype.h>
|
|
|
|
#include <m_string.h>
|
|
|
|
#include <my_dir.h>
|
2003-01-03 11:35:32 +01:00
|
|
|
#include <my_xml.h>
|
2000-07-31 21:29:14 +02:00
|
|
|
|
2004-06-03 14:45:53 +02:00
|
|
|
|
2003-10-06 21:56:34 +02:00
|
|
|
/*
|
2003-01-17 13:25:10 +01:00
|
|
|
The code below implements this functionality:
|
|
|
|
|
|
|
|
- Initializing charset related structures
|
|
|
|
- Loading dynamic charsets
|
|
|
|
- Searching for a proper CHARSET_INFO
|
2003-03-14 15:08:12 +01:00
|
|
|
using charset name, collation name or collation ID
|
2003-01-17 13:25:10 +01:00
|
|
|
- Setting server default character set
|
|
|
|
*/
|
|
|
|
|
2003-03-16 14:19:24 +01:00
|
|
|
my_bool my_charset_same(CHARSET_INFO *cs1, CHARSET_INFO *cs2)
|
|
|
|
{
|
|
|
|
return ((cs1 == cs2) || !strcmp(cs1->csname,cs2->csname));
|
|
|
|
}
|
2003-01-07 22:32:25 +01:00
|
|
|
|
2003-10-06 21:56:34 +02:00
|
|
|
|
2005-10-20 07:30:51 +02:00
|
|
|
static uint
|
|
|
|
get_collation_number_internal(const char *name)
|
|
|
|
{
|
|
|
|
CHARSET_INFO **cs;
|
|
|
|
for (cs= all_charsets;
|
|
|
|
cs < all_charsets+array_elements(all_charsets)-1 ;
|
|
|
|
cs++)
|
|
|
|
{
|
|
|
|
if ( cs[0] && cs[0]->name &&
|
|
|
|
!my_strcasecmp(&my_charset_latin1, cs[0]->name, name))
|
|
|
|
return cs[0]->number;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-10 16:10:21 +02:00
|
|
|
static my_bool init_state_maps(CHARSET_INFO *cs)
|
2003-03-14 15:08:12 +01:00
|
|
|
{
|
|
|
|
uint i;
|
2004-06-10 16:10:21 +02:00
|
|
|
uchar *state_map;
|
|
|
|
uchar *ident_map;
|
2003-07-20 12:26:18 +02:00
|
|
|
|
2004-06-10 16:10:21 +02:00
|
|
|
if (!(cs->state_map= (uchar*) my_once_alloc(256, MYF(MY_WME))))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (!(cs->ident_map= (uchar*) my_once_alloc(256, MYF(MY_WME))))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
state_map= cs->state_map;
|
|
|
|
ident_map= cs->ident_map;
|
|
|
|
|
2003-03-14 15:08:12 +01:00
|
|
|
/* Fill state_map with states to get a faster parser */
|
|
|
|
for (i=0; i < 256 ; i++)
|
|
|
|
{
|
|
|
|
if (my_isalpha(cs,i))
|
|
|
|
state_map[i]=(uchar) MY_LEX_IDENT;
|
|
|
|
else if (my_isdigit(cs,i))
|
|
|
|
state_map[i]=(uchar) MY_LEX_NUMBER_IDENT;
|
|
|
|
#if defined(USE_MB) && defined(USE_MB_IDENT)
|
2003-12-06 19:05:26 +01:00
|
|
|
else if (my_mbcharlen(cs, i)>1)
|
2003-03-14 15:08:12 +01:00
|
|
|
state_map[i]=(uchar) MY_LEX_IDENT;
|
|
|
|
#endif
|
2005-02-22 06:56:07 +01:00
|
|
|
else if (my_isspace(cs,i))
|
2003-07-20 12:26:18 +02:00
|
|
|
state_map[i]=(uchar) MY_LEX_SKIP;
|
2003-03-14 15:08:12 +01:00
|
|
|
else
|
|
|
|
state_map[i]=(uchar) MY_LEX_CHAR;
|
|
|
|
}
|
|
|
|
state_map[(uchar)'_']=state_map[(uchar)'$']=(uchar) MY_LEX_IDENT;
|
|
|
|
state_map[(uchar)'\'']=(uchar) MY_LEX_STRING;
|
|
|
|
state_map[(uchar)'.']=(uchar) MY_LEX_REAL_OR_POINT;
|
|
|
|
state_map[(uchar)'>']=state_map[(uchar)'=']=state_map[(uchar)'!']= (uchar) MY_LEX_CMP_OP;
|
|
|
|
state_map[(uchar)'<']= (uchar) MY_LEX_LONG_CMP_OP;
|
|
|
|
state_map[(uchar)'&']=state_map[(uchar)'|']=(uchar) MY_LEX_BOOL;
|
|
|
|
state_map[(uchar)'#']=(uchar) MY_LEX_COMMENT;
|
2004-04-27 23:49:05 +02:00
|
|
|
state_map[(uchar)';']=(uchar) MY_LEX_SEMICOLON;
|
2003-03-14 15:08:12 +01:00
|
|
|
state_map[(uchar)':']=(uchar) MY_LEX_SET_VAR;
|
|
|
|
state_map[0]=(uchar) MY_LEX_EOL;
|
|
|
|
state_map[(uchar)'\\']= (uchar) MY_LEX_ESCAPE;
|
|
|
|
state_map[(uchar)'/']= (uchar) MY_LEX_LONG_COMMENT;
|
|
|
|
state_map[(uchar)'*']= (uchar) MY_LEX_END_LONG_COMMENT;
|
|
|
|
state_map[(uchar)'@']= (uchar) MY_LEX_USER_END;
|
|
|
|
state_map[(uchar) '`']= (uchar) MY_LEX_USER_VARIABLE_DELIMITER;
|
|
|
|
state_map[(uchar)'"']= (uchar) MY_LEX_STRING_OR_DELIMITER;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Create a second map to make it faster to find identifiers
|
|
|
|
*/
|
|
|
|
for (i=0; i < 256 ; i++)
|
|
|
|
{
|
|
|
|
ident_map[i]= (uchar) (state_map[i] == MY_LEX_IDENT ||
|
|
|
|
state_map[i] == MY_LEX_NUMBER_IDENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Special handling of hex and binary strings */
|
|
|
|
state_map[(uchar)'x']= state_map[(uchar)'X']= (uchar) MY_LEX_IDENT_OR_HEX;
|
2005-06-16 09:17:15 +02:00
|
|
|
state_map[(uchar)'b']= state_map[(uchar)'B']= (uchar) MY_LEX_IDENT_OR_BIN;
|
2003-03-20 19:01:03 +01:00
|
|
|
state_map[(uchar)'n']= state_map[(uchar)'N']= (uchar) MY_LEX_IDENT_OR_NCHAR;
|
2004-06-10 16:10:21 +02:00
|
|
|
return 0;
|
2003-03-14 15:08:12 +01:00
|
|
|
}
|
|
|
|
|
2003-10-06 21:56:34 +02:00
|
|
|
|
2003-01-14 08:32:40 +01:00
|
|
|
static void simple_cs_init_functions(CHARSET_INFO *cs)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2003-03-04 08:04:19 +01:00
|
|
|
if (cs->state & MY_CS_BINSORT)
|
2003-09-19 12:18:19 +02:00
|
|
|
cs->coll= &my_collation_8bit_bin_handler;
|
2003-03-04 08:04:19 +01:00
|
|
|
else
|
2003-05-23 14:45:52 +02:00
|
|
|
cs->coll= &my_collation_8bit_simple_ci_handler;
|
2003-03-04 08:04:19 +01:00
|
|
|
|
2003-05-23 14:45:52 +02:00
|
|
|
cs->cset= &my_charset_8bit_handler;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
2003-01-07 22:32:25 +01:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
|
2004-06-11 15:25:50 +02:00
|
|
|
static int cs_copy_data(CHARSET_INFO *to, CHARSET_INFO *from)
|
2003-01-14 08:32:40 +01:00
|
|
|
{
|
|
|
|
to->number= from->number ? from->number : to->number;
|
|
|
|
|
|
|
|
if (from->csname)
|
2003-12-19 15:25:50 +01:00
|
|
|
if (!(to->csname= my_once_strdup(from->csname,MYF(MY_WME))))
|
|
|
|
goto err;
|
2003-01-14 08:32:40 +01:00
|
|
|
|
|
|
|
if (from->name)
|
2003-12-19 15:25:50 +01:00
|
|
|
if (!(to->name= my_once_strdup(from->name,MYF(MY_WME))))
|
|
|
|
goto err;
|
2003-01-14 08:32:40 +01:00
|
|
|
|
2003-03-25 13:12:49 +01:00
|
|
|
if (from->comment)
|
2003-12-19 15:25:50 +01:00
|
|
|
if (!(to->comment= my_once_strdup(from->comment,MYF(MY_WME))))
|
|
|
|
goto err;
|
2003-03-25 13:12:49 +01:00
|
|
|
|
2003-01-14 08:32:40 +01:00
|
|
|
if (from->ctype)
|
2003-03-14 15:08:12 +01:00
|
|
|
{
|
2003-12-19 15:25:50 +01:00
|
|
|
if (!(to->ctype= (uchar*) my_once_memdup((char*) from->ctype,
|
|
|
|
MY_CS_CTYPE_TABLE_SIZE,
|
|
|
|
MYF(MY_WME))))
|
|
|
|
goto err;
|
2004-06-10 16:10:21 +02:00
|
|
|
if (init_state_maps(to))
|
|
|
|
goto err;
|
2003-03-14 15:08:12 +01:00
|
|
|
}
|
2003-01-14 08:32:40 +01:00
|
|
|
if (from->to_lower)
|
2003-12-19 15:25:50 +01:00
|
|
|
if (!(to->to_lower= (uchar*) my_once_memdup((char*) from->to_lower,
|
|
|
|
MY_CS_TO_LOWER_TABLE_SIZE,
|
|
|
|
MYF(MY_WME))))
|
|
|
|
goto err;
|
|
|
|
|
2003-01-14 08:32:40 +01:00
|
|
|
if (from->to_upper)
|
2003-12-19 15:25:50 +01:00
|
|
|
if (!(to->to_upper= (uchar*) my_once_memdup((char*) from->to_upper,
|
|
|
|
MY_CS_TO_UPPER_TABLE_SIZE,
|
|
|
|
MYF(MY_WME))))
|
|
|
|
goto err;
|
2003-01-14 08:32:40 +01:00
|
|
|
if (from->sort_order)
|
|
|
|
{
|
2003-12-19 15:25:50 +01:00
|
|
|
if (!(to->sort_order= (uchar*) my_once_memdup((char*) from->sort_order,
|
|
|
|
MY_CS_SORT_ORDER_TABLE_SIZE,
|
|
|
|
MYF(MY_WME))))
|
|
|
|
goto err;
|
2004-06-10 16:10:21 +02:00
|
|
|
|
2003-01-14 08:32:40 +01:00
|
|
|
}
|
|
|
|
if (from->tab_to_uni)
|
|
|
|
{
|
2003-01-14 10:36:22 +01:00
|
|
|
uint sz= MY_CS_TO_UNI_TABLE_SIZE*sizeof(uint16);
|
2003-12-19 15:25:50 +01:00
|
|
|
if (!(to->tab_to_uni= (uint16*) my_once_memdup((char*)from->tab_to_uni,
|
|
|
|
sz, MYF(MY_WME))))
|
|
|
|
goto err;
|
2003-01-14 08:32:40 +01:00
|
|
|
}
|
2004-06-11 15:25:50 +02:00
|
|
|
if (from->tailoring)
|
|
|
|
if (!(to->tailoring= my_once_strdup(from->tailoring,MYF(MY_WME))))
|
|
|
|
goto err;
|
2003-12-19 15:25:50 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
return 1;
|
2003-01-14 08:32:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-03 14:45:53 +02:00
|
|
|
|
2003-01-14 08:32:40 +01:00
|
|
|
static my_bool simple_cs_is_full(CHARSET_INFO *cs)
|
|
|
|
{
|
|
|
|
return ((cs->csname && cs->tab_to_uni && cs->ctype && cs->to_upper &&
|
|
|
|
cs->to_lower) &&
|
2003-03-04 08:04:19 +01:00
|
|
|
(cs->number && cs->name &&
|
|
|
|
(cs->sort_order || (cs->state & MY_CS_BINSORT) )));
|
2003-01-14 08:32:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-07 14:55:55 +02:00
|
|
|
static void
|
|
|
|
copy_uca_collation(CHARSET_INFO *to, CHARSET_INFO *from)
|
|
|
|
{
|
|
|
|
to->cset= from->cset;
|
|
|
|
to->coll= from->coll;
|
|
|
|
to->strxfrm_multiply= from->strxfrm_multiply;
|
|
|
|
to->min_sort_char= from->min_sort_char;
|
|
|
|
to->max_sort_char= from->max_sort_char;
|
|
|
|
to->mbminlen= from->mbminlen;
|
|
|
|
to->mbmaxlen= from->mbmaxlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-14 08:32:40 +01:00
|
|
|
static int add_collation(CHARSET_INFO *cs)
|
|
|
|
{
|
2005-10-20 07:30:51 +02:00
|
|
|
if (cs->name && (cs->number ||
|
|
|
|
(cs->number=get_collation_number_internal(cs->name))))
|
2003-01-14 08:32:40 +01:00
|
|
|
{
|
|
|
|
if (!all_charsets[cs->number])
|
|
|
|
{
|
|
|
|
if (!(all_charsets[cs->number]=
|
|
|
|
(CHARSET_INFO*) my_once_alloc(sizeof(CHARSET_INFO),MYF(0))))
|
|
|
|
return MY_XML_ERROR;
|
|
|
|
bzero((void*)all_charsets[cs->number],sizeof(CHARSET_INFO));
|
|
|
|
}
|
2003-03-05 13:43:10 +01:00
|
|
|
|
|
|
|
if (cs->primary_number == cs->number)
|
|
|
|
cs->state |= MY_CS_PRIMARY;
|
2003-01-14 08:32:40 +01:00
|
|
|
|
2003-03-17 18:56:34 +01:00
|
|
|
if (cs->binary_number == cs->number)
|
2003-03-05 13:43:10 +01:00
|
|
|
cs->state |= MY_CS_BINSORT;
|
|
|
|
|
2003-09-22 08:11:36 +02:00
|
|
|
all_charsets[cs->number]->state|= cs->state;
|
|
|
|
|
2003-01-14 08:32:40 +01:00
|
|
|
if (!(all_charsets[cs->number]->state & MY_CS_COMPILED))
|
|
|
|
{
|
2007-06-07 14:55:55 +02:00
|
|
|
CHARSET_INFO *newcs= all_charsets[cs->number];
|
2004-06-11 15:25:50 +02:00
|
|
|
if (cs_copy_data(all_charsets[cs->number],cs))
|
|
|
|
return MY_XML_ERROR;
|
|
|
|
|
2004-06-03 14:45:53 +02:00
|
|
|
if (!strcmp(cs->csname,"ucs2") )
|
|
|
|
{
|
2005-02-18 09:58:16 +01:00
|
|
|
#if defined(HAVE_CHARSET_ucs2) && defined(HAVE_UCA_COLLATIONS)
|
2007-06-07 14:55:55 +02:00
|
|
|
copy_uca_collation(newcs, &my_charset_ucs2_unicode_ci);
|
|
|
|
newcs->state|= MY_CS_AVAILABLE | MY_CS_LOADED;
|
2004-06-03 14:45:53 +02:00
|
|
|
#endif
|
2007-06-07 14:55:55 +02:00
|
|
|
}
|
|
|
|
else if (!strcmp(cs->csname, "utf8"))
|
|
|
|
{
|
|
|
|
#if defined (HAVE_CHARSET_utf8) && defined(HAVE_UCA_COLLATIONS)
|
|
|
|
copy_uca_collation(newcs, &my_charset_utf8_unicode_ci);
|
|
|
|
newcs->state|= MY_CS_AVAILABLE | MY_CS_LOADED;
|
|
|
|
#endif
|
2004-06-03 14:45:53 +02:00
|
|
|
}
|
|
|
|
else
|
2003-01-14 08:32:40 +01:00
|
|
|
{
|
2004-11-22 08:58:40 +01:00
|
|
|
uchar *sort_order= all_charsets[cs->number]->sort_order;
|
2004-06-03 14:45:53 +02:00
|
|
|
simple_cs_init_functions(all_charsets[cs->number]);
|
2007-06-07 14:55:55 +02:00
|
|
|
newcs->mbminlen= 1;
|
|
|
|
newcs->mbmaxlen= 1;
|
2004-06-03 14:45:53 +02:00
|
|
|
if (simple_cs_is_full(all_charsets[cs->number]))
|
|
|
|
{
|
|
|
|
all_charsets[cs->number]->state |= MY_CS_LOADED;
|
|
|
|
}
|
|
|
|
all_charsets[cs->number]->state|= MY_CS_AVAILABLE;
|
2004-11-22 08:58:40 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
Check if case sensitive sort order: A < a < B.
|
|
|
|
We need MY_CS_FLAG for regex library, and for
|
|
|
|
case sensitivity flag for 5.0 client protocol,
|
|
|
|
to support isCaseSensitive() method in JDBC driver
|
|
|
|
*/
|
|
|
|
if (sort_order && sort_order['A'] < sort_order['a'] &&
|
|
|
|
sort_order['a'] < sort_order['B'])
|
|
|
|
all_charsets[cs->number]->state|= MY_CS_CSSORT;
|
2007-08-03 12:25:23 +02:00
|
|
|
|
|
|
|
if (my_charset_is_8bit_pure_ascii(all_charsets[cs->number]))
|
|
|
|
all_charsets[cs->number]->state|= MY_CS_PUREASCII;
|
2003-01-14 08:32:40 +01:00
|
|
|
}
|
|
|
|
}
|
2003-03-05 13:43:10 +01:00
|
|
|
else
|
|
|
|
{
|
2003-09-23 07:41:58 +02:00
|
|
|
/*
|
|
|
|
We need the below to make get_charset_name()
|
|
|
|
and get_charset_number() working even if a
|
|
|
|
character set has not been really incompiled.
|
|
|
|
The above functions are used for example
|
|
|
|
in error message compiler extra/comp_err.c.
|
|
|
|
If a character set was compiled, this information
|
|
|
|
will get lost and overwritten in add_compiled_collation().
|
|
|
|
*/
|
2003-03-25 13:12:49 +01:00
|
|
|
CHARSET_INFO *dst= all_charsets[cs->number];
|
2003-09-23 07:41:58 +02:00
|
|
|
dst->number= cs->number;
|
2003-03-25 13:12:49 +01:00
|
|
|
if (cs->comment)
|
2003-12-19 15:25:50 +01:00
|
|
|
if (!(dst->comment= my_once_strdup(cs->comment,MYF(MY_WME))))
|
|
|
|
return MY_XML_ERROR;
|
2003-09-23 07:41:58 +02:00
|
|
|
if (cs->csname)
|
2003-12-19 15:25:50 +01:00
|
|
|
if (!(dst->csname= my_once_strdup(cs->csname,MYF(MY_WME))))
|
|
|
|
return MY_XML_ERROR;
|
2003-09-23 07:41:58 +02:00
|
|
|
if (cs->name)
|
2003-12-19 15:25:50 +01:00
|
|
|
if (!(dst->name= my_once_strdup(cs->name,MYF(MY_WME))))
|
|
|
|
return MY_XML_ERROR;
|
2003-03-05 13:43:10 +01:00
|
|
|
}
|
2003-01-14 08:32:40 +01:00
|
|
|
cs->number= 0;
|
2003-03-18 10:42:45 +01:00
|
|
|
cs->primary_number= 0;
|
|
|
|
cs->binary_number= 0;
|
2003-01-14 08:32:40 +01:00
|
|
|
cs->name= NULL;
|
|
|
|
cs->state= 0;
|
|
|
|
cs->sort_order= NULL;
|
|
|
|
cs->state= 0;
|
|
|
|
}
|
|
|
|
return MY_XML_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-15 14:07:29 +01:00
|
|
|
#define MY_MAX_ALLOWED_BUF 1024*1024
|
2003-01-14 08:32:40 +01:00
|
|
|
#define MY_CHARSET_INDEX "Index.xml"
|
|
|
|
|
|
|
|
const char *charsets_dir= NULL;
|
|
|
|
static int charset_initialized=0;
|
|
|
|
|
|
|
|
|
|
|
|
static my_bool my_read_charset_file(const char *filename, myf myflags)
|
|
|
|
{
|
2007-07-30 10:33:50 +02:00
|
|
|
uchar *buf;
|
2003-01-14 08:32:40 +01:00
|
|
|
int fd;
|
2006-09-15 05:06:14 +02:00
|
|
|
uint len, tmp_len;
|
2004-01-15 14:07:29 +01:00
|
|
|
MY_STAT stat_info;
|
2003-01-14 08:32:40 +01:00
|
|
|
|
2004-04-27 16:32:40 +02:00
|
|
|
if (!my_stat(filename, &stat_info, MYF(myflags)) ||
|
2004-01-15 14:07:29 +01:00
|
|
|
((len= (uint)stat_info.st_size) > MY_MAX_ALLOWED_BUF) ||
|
2007-07-30 10:33:50 +02:00
|
|
|
!(buf= (uchar*) my_malloc(len,myflags)))
|
2004-01-15 14:07:29 +01:00
|
|
|
return TRUE;
|
2003-01-14 08:32:40 +01:00
|
|
|
|
|
|
|
if ((fd=my_open(filename,O_RDONLY,myflags)) < 0)
|
2006-09-15 05:06:14 +02:00
|
|
|
goto error;
|
|
|
|
tmp_len=my_read(fd, buf, len, myflags);
|
2003-01-14 08:32:40 +01:00
|
|
|
my_close(fd,myflags);
|
2006-09-15 05:06:14 +02:00
|
|
|
if (tmp_len != len)
|
|
|
|
goto error;
|
2003-01-14 08:32:40 +01:00
|
|
|
|
2007-07-30 10:33:50 +02:00
|
|
|
if (my_parse_charset_xml((char*) buf,len,add_collation))
|
2003-01-14 08:32:40 +01:00
|
|
|
{
|
|
|
|
#ifdef NOT_YET
|
|
|
|
printf("ERROR at line %d pos %d '%s'\n",
|
|
|
|
my_xml_error_lineno(&p)+1,
|
|
|
|
my_xml_error_pos(&p),
|
|
|
|
my_xml_error_string(&p));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-07-30 10:33:50 +02:00
|
|
|
my_free(buf, myflags);
|
2003-01-14 08:32:40 +01:00
|
|
|
return FALSE;
|
2006-09-15 05:06:14 +02:00
|
|
|
|
|
|
|
error:
|
|
|
|
my_free(buf, myflags);
|
|
|
|
return TRUE;
|
2003-01-14 08:32:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *get_charsets_dir(char *buf)
|
|
|
|
{
|
|
|
|
const char *sharedir= SHAREDIR;
|
2003-06-14 10:37:42 +02:00
|
|
|
char *res;
|
2003-01-14 08:32:40 +01:00
|
|
|
DBUG_ENTER("get_charsets_dir");
|
|
|
|
|
|
|
|
if (charsets_dir != NULL)
|
|
|
|
strmake(buf, charsets_dir, FN_REFLEN-1);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (test_if_hard_path(sharedir) ||
|
|
|
|
is_prefix(sharedir, DEFAULT_CHARSET_HOME))
|
|
|
|
strxmov(buf, sharedir, "/", CHARSET_DIR, NullS);
|
|
|
|
else
|
|
|
|
strxmov(buf, DEFAULT_CHARSET_HOME, "/", sharedir, "/", CHARSET_DIR,
|
|
|
|
NullS);
|
|
|
|
}
|
2003-06-14 10:37:42 +02:00
|
|
|
res= convert_dirname(buf,buf,NullS);
|
2003-01-14 08:32:40 +01:00
|
|
|
DBUG_PRINT("info",("charsets dir: '%s'", buf));
|
2003-06-14 10:37:42 +02:00
|
|
|
DBUG_RETURN(res);
|
2003-01-14 08:32:40 +01:00
|
|
|
}
|
|
|
|
|
2003-01-29 12:08:09 +01:00
|
|
|
CHARSET_INFO *all_charsets[256];
|
2003-01-29 14:31:20 +01:00
|
|
|
CHARSET_INFO *default_charset_info = &my_charset_latin1;
|
2003-01-29 12:08:09 +01:00
|
|
|
|
2003-10-06 21:56:34 +02:00
|
|
|
void add_compiled_collation(CHARSET_INFO *cs)
|
2003-09-22 08:11:36 +02:00
|
|
|
{
|
|
|
|
all_charsets[cs->number]= cs;
|
|
|
|
cs->state|= MY_CS_AVAILABLE;
|
|
|
|
}
|
2003-01-29 12:08:09 +01:00
|
|
|
|
WL#3817: Simplify string / memory area types and make things more consistent (first part)
The following type conversions was done:
- Changed byte to uchar
- Changed gptr to uchar*
- Change my_string to char *
- Change my_size_t to size_t
- Change size_s to size_t
Removed declaration of byte, gptr, my_string, my_size_t and size_s.
Following function parameter changes was done:
- All string functions in mysys/strings was changed to use size_t
instead of uint for string lengths.
- All read()/write() functions changed to use size_t (including vio).
- All protocoll functions changed to use size_t instead of uint
- Functions that used a pointer to a string length was changed to use size_t*
- Changed malloc(), free() and related functions from using gptr to use void *
as this requires fewer casts in the code and is more in line with how the
standard functions work.
- Added extra length argument to dirname_part() to return the length of the
created string.
- Changed (at least) following functions to take uchar* as argument:
- db_dump()
- my_net_write()
- net_write_command()
- net_store_data()
- DBUG_DUMP()
- decimal2bin() & bin2decimal()
- Changed my_compress() and my_uncompress() to use size_t. Changed one
argument to my_uncompress() from a pointer to a value as we only return
one value (makes function easier to use).
- Changed type of 'pack_data' argument to packfrm() to avoid casts.
- Changed in readfrm() and writefrom(), ha_discover and handler::discover()
the type for argument 'frmdata' to uchar** to avoid casts.
- Changed most Field functions to use uchar* instead of char* (reduced a lot of
casts).
- Changed field->val_xxx(xxx, new_ptr) to take const pointers.
Other changes:
- Removed a lot of not needed casts
- Added a few new cast required by other changes
- Added some cast to my_multi_malloc() arguments for safety (as string lengths
needs to be uint, not size_t).
- Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done
explicitely as this conflict was often hided by casting the function to
hash_get_key).
- Changed some buffers to memory regions to uchar* to avoid casts.
- Changed some string lengths from uint to size_t.
- Changed field->ptr to be uchar* instead of char*. This allowed us to
get rid of a lot of casts.
- Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar
- Include zlib.h in some files as we needed declaration of crc32()
- Changed MY_FILE_ERROR to be (size_t) -1.
- Changed many variables to hold the result of my_read() / my_write() to be
size_t. This was needed to properly detect errors (which are
returned as (size_t) -1).
- Removed some very old VMS code
- Changed packfrm()/unpackfrm() to not be depending on uint size
(portability fix)
- Removed windows specific code to restore cursor position as this
causes slowdown on windows and we should not mix read() and pread()
calls anyway as this is not thread safe. Updated function comment to
reflect this. Changed function that depended on original behavior of
my_pwrite() to itself restore the cursor position (one such case).
- Added some missing checking of return value of malloc().
- Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow.
- Changed type of table_def::m_size from my_size_t to ulong to reflect that
m_size is the number of elements in the array, not a string/memory
length.
- Moved THD::max_row_length() to table.cc (as it's not depending on THD).
Inlined max_row_length_blob() into this function.
- More function comments
- Fixed some compiler warnings when compiled without partitions.
- Removed setting of LEX_STRING() arguments in declaration (portability fix).
- Some trivial indentation/variable name changes.
- Some trivial code simplifications:
- Replaced some calls to alloc_root + memcpy to use
strmake_root()/strdup_root().
- Changed some calls from memdup() to strmake() (Safety fix)
- Simpler loops in client-simple.c
2007-05-10 11:59:39 +02:00
|
|
|
static void *cs_alloc(size_t size)
|
2004-06-11 13:29:16 +02:00
|
|
|
{
|
|
|
|
return my_once_alloc(size, MYF(MY_WME));
|
|
|
|
}
|
2003-01-29 12:08:09 +01:00
|
|
|
|
|
|
|
|
2003-01-28 07:38:28 +01:00
|
|
|
#ifdef __NETWARE__
|
|
|
|
my_bool STDCALL init_available_charsets(myf myflags)
|
|
|
|
#else
|
2003-01-14 08:32:40 +01:00
|
|
|
static my_bool init_available_charsets(myf myflags)
|
2003-01-28 07:38:28 +01:00
|
|
|
#endif
|
2003-01-14 08:32:40 +01:00
|
|
|
{
|
2007-04-16 09:28:02 +02:00
|
|
|
char fname[FN_REFLEN + sizeof(MY_CHARSET_INDEX)];
|
2003-01-14 08:32:40 +01:00
|
|
|
my_bool error=FALSE;
|
|
|
|
/*
|
|
|
|
We have to use charset_initialized to not lock on THR_LOCK_charset
|
|
|
|
inside get_internal_charset...
|
|
|
|
*/
|
|
|
|
if (!charset_initialized)
|
|
|
|
{
|
|
|
|
CHARSET_INFO **cs;
|
|
|
|
/*
|
|
|
|
To make things thread safe we are not allowing other threads to interfere
|
|
|
|
while we may changing the cs_info_table
|
|
|
|
*/
|
|
|
|
pthread_mutex_lock(&THR_LOCK_charset);
|
2005-07-25 18:34:20 +02:00
|
|
|
if (!charset_initialized)
|
2003-01-14 08:32:40 +01:00
|
|
|
{
|
2005-07-25 18:34:20 +02:00
|
|
|
bzero(&all_charsets,sizeof(all_charsets));
|
|
|
|
init_compiled_charsets(myflags);
|
|
|
|
|
|
|
|
/* Copy compiled charsets */
|
|
|
|
for (cs=all_charsets;
|
|
|
|
cs < all_charsets+array_elements(all_charsets)-1 ;
|
|
|
|
cs++)
|
2003-03-14 15:08:12 +01:00
|
|
|
{
|
2005-07-25 18:34:20 +02:00
|
|
|
if (*cs)
|
|
|
|
{
|
|
|
|
if (cs[0]->ctype)
|
|
|
|
if (init_state_maps(*cs))
|
|
|
|
*cs= NULL;
|
|
|
|
}
|
2003-03-14 15:08:12 +01:00
|
|
|
}
|
2005-07-25 18:34:20 +02:00
|
|
|
|
|
|
|
strmov(get_charsets_dir(fname), MY_CHARSET_INDEX);
|
|
|
|
error= my_read_charset_file(fname,myflags);
|
|
|
|
charset_initialized=1;
|
2003-01-14 08:32:40 +01:00
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&THR_LOCK_charset);
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void free_charsets(void)
|
|
|
|
{
|
|
|
|
charset_initialized=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-17 13:22:58 +02:00
|
|
|
uint get_collation_number(const char *name)
|
2002-07-30 11:02:29 +02:00
|
|
|
{
|
2004-03-25 14:05:01 +01:00
|
|
|
init_available_charsets(MYF(0));
|
2005-10-20 07:30:51 +02:00
|
|
|
return get_collation_number_internal(name);
|
2002-07-30 11:02:29 +02:00
|
|
|
}
|
|
|
|
|
2004-03-25 14:05:01 +01:00
|
|
|
|
2003-09-17 13:22:58 +02:00
|
|
|
uint get_charset_number(const char *charset_name, uint cs_flags)
|
|
|
|
{
|
|
|
|
CHARSET_INFO **cs;
|
2004-03-25 14:05:01 +01:00
|
|
|
init_available_charsets(MYF(0));
|
2003-09-17 13:22:58 +02:00
|
|
|
|
2004-03-25 14:05:01 +01:00
|
|
|
for (cs= all_charsets;
|
|
|
|
cs < all_charsets+array_elements(all_charsets)-1 ;
|
|
|
|
cs++)
|
2003-09-17 13:22:58 +02:00
|
|
|
{
|
|
|
|
if ( cs[0] && cs[0]->csname && (cs[0]->state & cs_flags) &&
|
|
|
|
!my_strcasecmp(&my_charset_latin1, cs[0]->csname, charset_name))
|
|
|
|
return cs[0]->number;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-30 11:02:29 +02:00
|
|
|
|
|
|
|
const char *get_charset_name(uint charset_number)
|
|
|
|
{
|
|
|
|
CHARSET_INFO *cs;
|
2004-03-25 14:05:01 +01:00
|
|
|
init_available_charsets(MYF(0));
|
2002-07-30 11:02:29 +02:00
|
|
|
|
2002-10-10 12:52:32 +02:00
|
|
|
cs=all_charsets[charset_number];
|
2003-01-07 22:32:25 +01:00
|
|
|
if (cs && (cs->number == charset_number) && cs->name )
|
2002-07-30 14:12:51 +02:00
|
|
|
return (char*) cs->name;
|
|
|
|
|
2002-07-30 11:02:29 +02:00
|
|
|
return (char*) "?"; /* this mimics find_type() */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-06 01:15:45 +01:00
|
|
|
static CHARSET_INFO *get_internal_charset(uint cs_number, myf flags)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2003-01-07 22:32:25 +01:00
|
|
|
char buf[FN_REFLEN];
|
2000-07-31 21:29:14 +02:00
|
|
|
CHARSET_INFO *cs;
|
|
|
|
/*
|
|
|
|
To make things thread safe we are not allowing other threads to interfere
|
|
|
|
while we may changing the cs_info_table
|
|
|
|
*/
|
|
|
|
pthread_mutex_lock(&THR_LOCK_charset);
|
2004-01-19 23:51:17 +01:00
|
|
|
if ((cs= all_charsets[cs_number]))
|
2003-01-05 14:34:24 +01:00
|
|
|
{
|
2004-01-19 23:51:17 +01:00
|
|
|
if (!(cs->state & MY_CS_COMPILED) && !(cs->state & MY_CS_LOADED))
|
|
|
|
{
|
|
|
|
strxmov(get_charsets_dir(buf), cs->csname, ".xml", NullS);
|
|
|
|
my_read_charset_file(buf,flags);
|
|
|
|
}
|
|
|
|
cs= (cs->state & MY_CS_AVAILABLE) ? cs : NULL;
|
2003-01-05 14:34:24 +01:00
|
|
|
}
|
2004-06-11 13:29:16 +02:00
|
|
|
if (cs && !(cs->state & MY_CS_READY))
|
|
|
|
{
|
|
|
|
if ((cs->cset->init && cs->cset->init(cs, cs_alloc)) ||
|
|
|
|
(cs->coll->init && cs->coll->init(cs, cs_alloc)))
|
|
|
|
cs= NULL;
|
|
|
|
else
|
|
|
|
cs->state|= MY_CS_READY;
|
|
|
|
}
|
2004-06-16 16:06:45 +02:00
|
|
|
pthread_mutex_unlock(&THR_LOCK_charset);
|
2000-07-31 21:29:14 +02:00
|
|
|
return cs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CHARSET_INFO *get_charset(uint cs_number, myf flags)
|
|
|
|
{
|
|
|
|
CHARSET_INFO *cs;
|
2004-03-25 14:05:01 +01:00
|
|
|
if (cs_number == default_charset_info->number)
|
|
|
|
return default_charset_info;
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
(void) init_available_charsets(MYF(0)); /* If it isn't initialized */
|
2002-07-30 14:12:51 +02:00
|
|
|
|
2004-03-25 14:05:01 +01:00
|
|
|
if (!cs_number || cs_number >= array_elements(all_charsets)-1)
|
2002-07-30 14:12:51 +02:00
|
|
|
return NULL;
|
|
|
|
|
2001-11-06 01:15:45 +01:00
|
|
|
cs=get_internal_charset(cs_number, flags);
|
2000-07-31 21:29:14 +02:00
|
|
|
|
2000-09-07 03:55:17 +02:00
|
|
|
if (!cs && (flags & MY_WME))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2007-04-16 09:28:02 +02:00
|
|
|
char index_file[FN_REFLEN + sizeof(MY_CHARSET_INDEX)], cs_string[23];
|
2003-01-03 11:35:32 +01:00
|
|
|
strmov(get_charsets_dir(index_file),MY_CHARSET_INDEX);
|
2000-07-31 21:29:14 +02:00
|
|
|
cs_string[0]='#';
|
|
|
|
int10_to_str(cs_number, cs_string+1, 10);
|
|
|
|
my_error(EE_UNKNOWN_CHARSET, MYF(ME_BELL), cs_string, index_file);
|
|
|
|
}
|
|
|
|
return cs;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHARSET_INFO *get_charset_by_name(const char *cs_name, myf flags)
|
|
|
|
{
|
2003-01-08 12:39:15 +01:00
|
|
|
uint cs_number;
|
2000-07-31 21:29:14 +02:00
|
|
|
CHARSET_INFO *cs;
|
|
|
|
(void) init_available_charsets(MYF(0)); /* If it isn't initialized */
|
|
|
|
|
2003-09-17 13:22:58 +02:00
|
|
|
cs_number=get_collation_number(cs_name);
|
2003-01-08 12:39:15 +01:00
|
|
|
cs= cs_number ? get_internal_charset(cs_number,flags) : NULL;
|
|
|
|
|
|
|
|
if (!cs && (flags & MY_WME))
|
|
|
|
{
|
2007-04-16 09:28:02 +02:00
|
|
|
char index_file[FN_REFLEN + sizeof(MY_CHARSET_INDEX)];
|
2003-01-08 12:39:15 +01:00
|
|
|
strmov(get_charsets_dir(index_file),MY_CHARSET_INDEX);
|
2005-02-22 13:37:25 +01:00
|
|
|
my_error(EE_UNKNOWN_COLLATION, MYF(ME_BELL), cs_name, index_file);
|
2003-01-08 12:39:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return cs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-04 16:53:53 +01:00
|
|
|
CHARSET_INFO *get_charset_by_csname(const char *cs_name,
|
|
|
|
uint cs_flags,
|
|
|
|
myf flags)
|
2003-01-08 12:39:15 +01:00
|
|
|
{
|
2003-09-17 13:22:58 +02:00
|
|
|
uint cs_number;
|
|
|
|
CHARSET_INFO *cs;
|
2003-06-14 10:37:42 +02:00
|
|
|
DBUG_ENTER("get_charset_by_csname");
|
|
|
|
DBUG_PRINT("enter",("name: '%s'", cs_name));
|
|
|
|
|
2003-01-08 12:39:15 +01:00
|
|
|
(void) init_available_charsets(MYF(0)); /* If it isn't initialized */
|
2005-03-17 12:27:45 +01:00
|
|
|
|
2003-09-17 13:22:58 +02:00
|
|
|
cs_number= get_charset_number(cs_name, cs_flags);
|
|
|
|
cs= cs_number ? get_internal_charset(cs_number, flags) : NULL;
|
2005-03-17 12:27:45 +01:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
if (!cs && (flags & MY_WME))
|
|
|
|
{
|
2007-04-16 09:28:02 +02:00
|
|
|
char index_file[FN_REFLEN + sizeof(MY_CHARSET_INDEX)];
|
2003-01-03 11:35:32 +01:00
|
|
|
strmov(get_charsets_dir(index_file),MY_CHARSET_INDEX);
|
2000-07-31 21:29:14 +02:00
|
|
|
my_error(EE_UNKNOWN_CHARSET, MYF(ME_BELL), cs_name, index_file);
|
|
|
|
}
|
|
|
|
|
2003-06-14 10:37:42 +02:00
|
|
|
DBUG_RETURN(cs);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2004-05-25 00:03:49 +02:00
|
|
|
|
2005-07-05 18:45:32 +02:00
|
|
|
|
2007-06-14 17:23:55 +02:00
|
|
|
/**
|
|
|
|
Resolve character set by the character set name (utf8, latin1, ...).
|
|
|
|
|
|
|
|
The function tries to resolve character set by the specified name. If
|
|
|
|
there is character set with the given name, it is assigned to the "cs"
|
|
|
|
parameter and FALSE is returned. If there is no such character set,
|
|
|
|
"default_cs" is assigned to the "cs" and TRUE is returned.
|
|
|
|
|
|
|
|
@param[in] cs_name Character set name.
|
|
|
|
@param[in] default_cs Default character set.
|
Patch for the following bugs:
- BUG#11986: Stored routines and triggers can fail if the code
has a non-ascii symbol
- BUG#16291: mysqldump corrupts string-constants with non-ascii-chars
- BUG#19443: INFORMATION_SCHEMA does not support charsets properly
- BUG#21249: Character set of SP-var can be ignored
- BUG#25212: Character set of string constant is ignored (stored routines)
- BUG#25221: Character set of string constant is ignored (triggers)
There were a few general problems that caused these bugs:
1. Character set information of the original (definition) query for views,
triggers, stored routines and events was lost.
2. mysqldump output query in client character set, which can be
inappropriate to encode definition-query.
3. INFORMATION_SCHEMA used strings with mixed encodings to display object
definition;
1. No query-definition-character set.
In order to compile query into execution code, some extra data (such as
environment variables or the database character set) is used. The problem
here was that this context was not preserved. So, on the next load it can
differ from the original one, thus the result will be different.
The context contains the following data:
- client character set;
- connection collation (character set and collation);
- collation of the owner database;
The fix is to store this context and use it each time we parse (compile)
and execute the object (stored routine, trigger, ...).
2. Wrong mysqldump-output.
The original query can contain several encodings (by means of character set
introducers). The problem here was that we tried to convert original query
to the mysqldump-client character set.
Moreover, we stored queries in different character sets for different
objects (views, for one, used UTF8, triggers used original character set).
The solution is
- to store definition queries in the original character set;
- to change SHOW CREATE statement to output definition query in the
binary character set (i.e. without any conversion);
- introduce SHOW CREATE TRIGGER statement;
- to dump special statements to switch the context to the original one
before dumping and restore it afterwards.
Note, in order to preserve the database collation at the creation time,
additional ALTER DATABASE might be used (to temporary switch the database
collation back to the original value). In this case, ALTER DATABASE
privilege will be required. This is a backward-incompatible change.
3. INFORMATION_SCHEMA showed non-UTF8 strings
The fix is to generate UTF8-query during the parsing, store it in the object
and show it in the INFORMATION_SCHEMA.
Basically, the idea is to create a copy of the original query convert it to
UTF8. Character set introducers are removed and all text literals are
converted to UTF8.
This UTF8 query is intended to provide user-readable output. It must not be
used to recreate the object. Specialized SHOW CREATE statements should be
used for this.
The reason for this limitation is the following: the original query can
contain symbols from several character sets (by means of character set
introducers).
Example:
- original query:
CREATE VIEW v1 AS SELECT _cp1251 'Hello' AS c1;
- UTF8 query (for INFORMATION_SCHEMA):
CREATE VIEW v1 AS SELECT 'Hello' AS c1;
2007-06-28 19:34:54 +02:00
|
|
|
@param[out] cs Variable to store character set.
|
2007-06-14 17:23:55 +02:00
|
|
|
|
|
|
|
@return FALSE if character set was resolved successfully; TRUE if there
|
|
|
|
is no character set with given name.
|
|
|
|
*/
|
|
|
|
|
Patch for the following bugs:
- BUG#11986: Stored routines and triggers can fail if the code
has a non-ascii symbol
- BUG#16291: mysqldump corrupts string-constants with non-ascii-chars
- BUG#19443: INFORMATION_SCHEMA does not support charsets properly
- BUG#21249: Character set of SP-var can be ignored
- BUG#25212: Character set of string constant is ignored (stored routines)
- BUG#25221: Character set of string constant is ignored (triggers)
There were a few general problems that caused these bugs:
1. Character set information of the original (definition) query for views,
triggers, stored routines and events was lost.
2. mysqldump output query in client character set, which can be
inappropriate to encode definition-query.
3. INFORMATION_SCHEMA used strings with mixed encodings to display object
definition;
1. No query-definition-character set.
In order to compile query into execution code, some extra data (such as
environment variables or the database character set) is used. The problem
here was that this context was not preserved. So, on the next load it can
differ from the original one, thus the result will be different.
The context contains the following data:
- client character set;
- connection collation (character set and collation);
- collation of the owner database;
The fix is to store this context and use it each time we parse (compile)
and execute the object (stored routine, trigger, ...).
2. Wrong mysqldump-output.
The original query can contain several encodings (by means of character set
introducers). The problem here was that we tried to convert original query
to the mysqldump-client character set.
Moreover, we stored queries in different character sets for different
objects (views, for one, used UTF8, triggers used original character set).
The solution is
- to store definition queries in the original character set;
- to change SHOW CREATE statement to output definition query in the
binary character set (i.e. without any conversion);
- introduce SHOW CREATE TRIGGER statement;
- to dump special statements to switch the context to the original one
before dumping and restore it afterwards.
Note, in order to preserve the database collation at the creation time,
additional ALTER DATABASE might be used (to temporary switch the database
collation back to the original value). In this case, ALTER DATABASE
privilege will be required. This is a backward-incompatible change.
3. INFORMATION_SCHEMA showed non-UTF8 strings
The fix is to generate UTF8-query during the parsing, store it in the object
and show it in the INFORMATION_SCHEMA.
Basically, the idea is to create a copy of the original query convert it to
UTF8. Character set introducers are removed and all text literals are
converted to UTF8.
This UTF8 query is intended to provide user-readable output. It must not be
used to recreate the object. Specialized SHOW CREATE statements should be
used for this.
The reason for this limitation is the following: the original query can
contain symbols from several character sets (by means of character set
introducers).
Example:
- original query:
CREATE VIEW v1 AS SELECT _cp1251 'Hello' AS c1;
- UTF8 query (for INFORMATION_SCHEMA):
CREATE VIEW v1 AS SELECT 'Hello' AS c1;
2007-06-28 19:34:54 +02:00
|
|
|
bool resolve_charset(const char *cs_name,
|
|
|
|
CHARSET_INFO *default_cs,
|
|
|
|
CHARSET_INFO **cs)
|
2007-06-14 17:23:55 +02:00
|
|
|
{
|
|
|
|
*cs= get_charset_by_csname(cs_name, MY_CS_PRIMARY, MYF(0));
|
|
|
|
|
|
|
|
if (*cs == NULL)
|
|
|
|
{
|
|
|
|
*cs= default_cs;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Resolve collation by the collation name (utf8_general_ci, ...).
|
|
|
|
|
|
|
|
The function tries to resolve collation by the specified name. If there
|
|
|
|
is collation with the given name, it is assigned to the "cl" parameter
|
|
|
|
and FALSE is returned. If there is no such collation, "default_cl" is
|
|
|
|
assigned to the "cl" and TRUE is returned.
|
|
|
|
|
|
|
|
@param[out] cl Variable to store collation.
|
|
|
|
@param[in] cl_name Collation name.
|
|
|
|
@param[in] default_cl Default collation.
|
|
|
|
|
|
|
|
@return FALSE if collation was resolved successfully; TRUE if there is no
|
|
|
|
collation with given name.
|
|
|
|
*/
|
|
|
|
|
Patch for the following bugs:
- BUG#11986: Stored routines and triggers can fail if the code
has a non-ascii symbol
- BUG#16291: mysqldump corrupts string-constants with non-ascii-chars
- BUG#19443: INFORMATION_SCHEMA does not support charsets properly
- BUG#21249: Character set of SP-var can be ignored
- BUG#25212: Character set of string constant is ignored (stored routines)
- BUG#25221: Character set of string constant is ignored (triggers)
There were a few general problems that caused these bugs:
1. Character set information of the original (definition) query for views,
triggers, stored routines and events was lost.
2. mysqldump output query in client character set, which can be
inappropriate to encode definition-query.
3. INFORMATION_SCHEMA used strings with mixed encodings to display object
definition;
1. No query-definition-character set.
In order to compile query into execution code, some extra data (such as
environment variables or the database character set) is used. The problem
here was that this context was not preserved. So, on the next load it can
differ from the original one, thus the result will be different.
The context contains the following data:
- client character set;
- connection collation (character set and collation);
- collation of the owner database;
The fix is to store this context and use it each time we parse (compile)
and execute the object (stored routine, trigger, ...).
2. Wrong mysqldump-output.
The original query can contain several encodings (by means of character set
introducers). The problem here was that we tried to convert original query
to the mysqldump-client character set.
Moreover, we stored queries in different character sets for different
objects (views, for one, used UTF8, triggers used original character set).
The solution is
- to store definition queries in the original character set;
- to change SHOW CREATE statement to output definition query in the
binary character set (i.e. without any conversion);
- introduce SHOW CREATE TRIGGER statement;
- to dump special statements to switch the context to the original one
before dumping and restore it afterwards.
Note, in order to preserve the database collation at the creation time,
additional ALTER DATABASE might be used (to temporary switch the database
collation back to the original value). In this case, ALTER DATABASE
privilege will be required. This is a backward-incompatible change.
3. INFORMATION_SCHEMA showed non-UTF8 strings
The fix is to generate UTF8-query during the parsing, store it in the object
and show it in the INFORMATION_SCHEMA.
Basically, the idea is to create a copy of the original query convert it to
UTF8. Character set introducers are removed and all text literals are
converted to UTF8.
This UTF8 query is intended to provide user-readable output. It must not be
used to recreate the object. Specialized SHOW CREATE statements should be
used for this.
The reason for this limitation is the following: the original query can
contain symbols from several character sets (by means of character set
introducers).
Example:
- original query:
CREATE VIEW v1 AS SELECT _cp1251 'Hello' AS c1;
- UTF8 query (for INFORMATION_SCHEMA):
CREATE VIEW v1 AS SELECT 'Hello' AS c1;
2007-06-28 19:34:54 +02:00
|
|
|
bool resolve_collation(const char *cl_name,
|
|
|
|
CHARSET_INFO *default_cl,
|
|
|
|
CHARSET_INFO **cl)
|
2007-06-14 17:23:55 +02:00
|
|
|
{
|
|
|
|
*cl= get_charset_by_name(cl_name, MYF(0));
|
|
|
|
|
|
|
|
if (*cl == NULL)
|
|
|
|
{
|
|
|
|
*cl= default_cl;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-17 12:27:45 +01:00
|
|
|
/*
|
2005-07-05 18:45:32 +02:00
|
|
|
Escape string with backslashes (\)
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
escape_string_for_mysql()
|
|
|
|
charset_info Charset of the strings
|
|
|
|
to Buffer for escaped string
|
|
|
|
to_length Length of destination buffer, or 0
|
|
|
|
from The string to escape
|
|
|
|
length The length of the string to escape
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
This escapes the contents of a string by adding backslashes before special
|
|
|
|
characters, and turning others into specific escape sequences, such as
|
|
|
|
turning newlines into \n and null bytes into \0.
|
|
|
|
|
2005-03-17 12:27:45 +01:00
|
|
|
NOTE
|
2005-07-05 18:45:32 +02:00
|
|
|
To maintain compatibility with the old C API, to_length may be 0 to mean
|
|
|
|
"big enough"
|
|
|
|
|
|
|
|
RETURN VALUES
|
WL#3817: Simplify string / memory area types and make things more consistent (first part)
The following type conversions was done:
- Changed byte to uchar
- Changed gptr to uchar*
- Change my_string to char *
- Change my_size_t to size_t
- Change size_s to size_t
Removed declaration of byte, gptr, my_string, my_size_t and size_s.
Following function parameter changes was done:
- All string functions in mysys/strings was changed to use size_t
instead of uint for string lengths.
- All read()/write() functions changed to use size_t (including vio).
- All protocoll functions changed to use size_t instead of uint
- Functions that used a pointer to a string length was changed to use size_t*
- Changed malloc(), free() and related functions from using gptr to use void *
as this requires fewer casts in the code and is more in line with how the
standard functions work.
- Added extra length argument to dirname_part() to return the length of the
created string.
- Changed (at least) following functions to take uchar* as argument:
- db_dump()
- my_net_write()
- net_write_command()
- net_store_data()
- DBUG_DUMP()
- decimal2bin() & bin2decimal()
- Changed my_compress() and my_uncompress() to use size_t. Changed one
argument to my_uncompress() from a pointer to a value as we only return
one value (makes function easier to use).
- Changed type of 'pack_data' argument to packfrm() to avoid casts.
- Changed in readfrm() and writefrom(), ha_discover and handler::discover()
the type for argument 'frmdata' to uchar** to avoid casts.
- Changed most Field functions to use uchar* instead of char* (reduced a lot of
casts).
- Changed field->val_xxx(xxx, new_ptr) to take const pointers.
Other changes:
- Removed a lot of not needed casts
- Added a few new cast required by other changes
- Added some cast to my_multi_malloc() arguments for safety (as string lengths
needs to be uint, not size_t).
- Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done
explicitely as this conflict was often hided by casting the function to
hash_get_key).
- Changed some buffers to memory regions to uchar* to avoid casts.
- Changed some string lengths from uint to size_t.
- Changed field->ptr to be uchar* instead of char*. This allowed us to
get rid of a lot of casts.
- Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar
- Include zlib.h in some files as we needed declaration of crc32()
- Changed MY_FILE_ERROR to be (size_t) -1.
- Changed many variables to hold the result of my_read() / my_write() to be
size_t. This was needed to properly detect errors (which are
returned as (size_t) -1).
- Removed some very old VMS code
- Changed packfrm()/unpackfrm() to not be depending on uint size
(portability fix)
- Removed windows specific code to restore cursor position as this
causes slowdown on windows and we should not mix read() and pread()
calls anyway as this is not thread safe. Updated function comment to
reflect this. Changed function that depended on original behavior of
my_pwrite() to itself restore the cursor position (one such case).
- Added some missing checking of return value of malloc().
- Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow.
- Changed type of table_def::m_size from my_size_t to ulong to reflect that
m_size is the number of elements in the array, not a string/memory
length.
- Moved THD::max_row_length() to table.cc (as it's not depending on THD).
Inlined max_row_length_blob() into this function.
- More function comments
- Fixed some compiler warnings when compiled without partitions.
- Removed setting of LEX_STRING() arguments in declaration (portability fix).
- Some trivial indentation/variable name changes.
- Some trivial code simplifications:
- Replaced some calls to alloc_root + memcpy to use
strmake_root()/strdup_root().
- Changed some calls from memdup() to strmake() (Safety fix)
- Simpler loops in client-simple.c
2007-05-10 11:59:39 +02:00
|
|
|
(size_t) -1 The escaped string did not fit in the to buffer
|
|
|
|
# The length of the escaped string
|
2005-03-17 12:27:45 +01:00
|
|
|
*/
|
2005-07-28 15:10:14 +02:00
|
|
|
|
WL#3817: Simplify string / memory area types and make things more consistent (first part)
The following type conversions was done:
- Changed byte to uchar
- Changed gptr to uchar*
- Change my_string to char *
- Change my_size_t to size_t
- Change size_s to size_t
Removed declaration of byte, gptr, my_string, my_size_t and size_s.
Following function parameter changes was done:
- All string functions in mysys/strings was changed to use size_t
instead of uint for string lengths.
- All read()/write() functions changed to use size_t (including vio).
- All protocoll functions changed to use size_t instead of uint
- Functions that used a pointer to a string length was changed to use size_t*
- Changed malloc(), free() and related functions from using gptr to use void *
as this requires fewer casts in the code and is more in line with how the
standard functions work.
- Added extra length argument to dirname_part() to return the length of the
created string.
- Changed (at least) following functions to take uchar* as argument:
- db_dump()
- my_net_write()
- net_write_command()
- net_store_data()
- DBUG_DUMP()
- decimal2bin() & bin2decimal()
- Changed my_compress() and my_uncompress() to use size_t. Changed one
argument to my_uncompress() from a pointer to a value as we only return
one value (makes function easier to use).
- Changed type of 'pack_data' argument to packfrm() to avoid casts.
- Changed in readfrm() and writefrom(), ha_discover and handler::discover()
the type for argument 'frmdata' to uchar** to avoid casts.
- Changed most Field functions to use uchar* instead of char* (reduced a lot of
casts).
- Changed field->val_xxx(xxx, new_ptr) to take const pointers.
Other changes:
- Removed a lot of not needed casts
- Added a few new cast required by other changes
- Added some cast to my_multi_malloc() arguments for safety (as string lengths
needs to be uint, not size_t).
- Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done
explicitely as this conflict was often hided by casting the function to
hash_get_key).
- Changed some buffers to memory regions to uchar* to avoid casts.
- Changed some string lengths from uint to size_t.
- Changed field->ptr to be uchar* instead of char*. This allowed us to
get rid of a lot of casts.
- Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar
- Include zlib.h in some files as we needed declaration of crc32()
- Changed MY_FILE_ERROR to be (size_t) -1.
- Changed many variables to hold the result of my_read() / my_write() to be
size_t. This was needed to properly detect errors (which are
returned as (size_t) -1).
- Removed some very old VMS code
- Changed packfrm()/unpackfrm() to not be depending on uint size
(portability fix)
- Removed windows specific code to restore cursor position as this
causes slowdown on windows and we should not mix read() and pread()
calls anyway as this is not thread safe. Updated function comment to
reflect this. Changed function that depended on original behavior of
my_pwrite() to itself restore the cursor position (one such case).
- Added some missing checking of return value of malloc().
- Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow.
- Changed type of table_def::m_size from my_size_t to ulong to reflect that
m_size is the number of elements in the array, not a string/memory
length.
- Moved THD::max_row_length() to table.cc (as it's not depending on THD).
Inlined max_row_length_blob() into this function.
- More function comments
- Fixed some compiler warnings when compiled without partitions.
- Removed setting of LEX_STRING() arguments in declaration (portability fix).
- Some trivial indentation/variable name changes.
- Some trivial code simplifications:
- Replaced some calls to alloc_root + memcpy to use
strmake_root()/strdup_root().
- Changed some calls from memdup() to strmake() (Safety fix)
- Simpler loops in client-simple.c
2007-05-10 11:59:39 +02:00
|
|
|
size_t escape_string_for_mysql(CHARSET_INFO *charset_info,
|
|
|
|
char *to, size_t to_length,
|
|
|
|
const char *from, size_t length)
|
2004-05-25 00:03:49 +02:00
|
|
|
{
|
|
|
|
const char *to_start= to;
|
2005-03-17 12:27:45 +01:00
|
|
|
const char *end, *to_end=to_start + (to_length ? to_length-1 : 2*length);
|
2005-07-05 18:45:32 +02:00
|
|
|
my_bool overflow= FALSE;
|
2004-05-25 00:03:49 +02:00
|
|
|
#ifdef USE_MB
|
|
|
|
my_bool use_mb_flag= use_mb(charset_info);
|
|
|
|
#endif
|
2005-03-17 12:27:45 +01:00
|
|
|
for (end= from + length; from < end; from++)
|
2004-05-25 00:03:49 +02:00
|
|
|
{
|
2005-07-05 18:45:32 +02:00
|
|
|
char escape= 0;
|
2004-05-25 00:03:49 +02:00
|
|
|
#ifdef USE_MB
|
2005-04-26 12:43:20 +02:00
|
|
|
int tmp_length;
|
|
|
|
if (use_mb_flag && (tmp_length= my_ismbchar(charset_info, from, end)))
|
2004-05-25 00:03:49 +02:00
|
|
|
{
|
2005-04-26 12:43:20 +02:00
|
|
|
if (to + tmp_length > to_end)
|
2005-03-17 12:27:45 +01:00
|
|
|
{
|
2005-07-05 18:45:32 +02:00
|
|
|
overflow= TRUE;
|
2005-03-17 12:27:45 +01:00
|
|
|
break;
|
|
|
|
}
|
2005-04-26 12:43:20 +02:00
|
|
|
while (tmp_length--)
|
2004-05-25 00:03:49 +02:00
|
|
|
*to++= *from++;
|
|
|
|
from--;
|
|
|
|
continue;
|
|
|
|
}
|
2005-02-10 01:14:13 +01:00
|
|
|
/*
|
|
|
|
If the next character appears to begin a multi-byte character, we
|
2005-02-15 20:31:01 +01:00
|
|
|
escape that first byte of that apparent multi-byte character. (The
|
|
|
|
character just looks like a multi-byte character -- if it were actually
|
|
|
|
a multi-byte character, it would have been passed through in the test
|
|
|
|
above.)
|
2005-02-10 01:14:13 +01:00
|
|
|
|
|
|
|
Without this check, we can create a problem by converting an invalid
|
|
|
|
multi-byte character into a valid one. For example, 0xbf27 is not
|
|
|
|
a valid GBK character, but 0xbf5c is. (0x27 = ', 0x5c = \)
|
|
|
|
*/
|
2005-04-26 12:43:20 +02:00
|
|
|
if (use_mb_flag && (tmp_length= my_mbcharlen(charset_info, *from)) > 1)
|
2005-03-17 12:27:45 +01:00
|
|
|
escape= *from;
|
|
|
|
else
|
2004-05-25 00:03:49 +02:00
|
|
|
#endif
|
|
|
|
switch (*from) {
|
|
|
|
case 0: /* Must be escaped for 'mysql' */
|
2005-03-17 12:27:45 +01:00
|
|
|
escape= '0';
|
2004-05-25 00:03:49 +02:00
|
|
|
break;
|
|
|
|
case '\n': /* Must be escaped for logs */
|
2005-03-17 12:27:45 +01:00
|
|
|
escape= 'n';
|
2004-05-25 00:03:49 +02:00
|
|
|
break;
|
|
|
|
case '\r':
|
2005-03-17 12:27:45 +01:00
|
|
|
escape= 'r';
|
2004-05-25 00:03:49 +02:00
|
|
|
break;
|
|
|
|
case '\\':
|
2005-03-17 12:27:45 +01:00
|
|
|
escape= '\\';
|
2004-05-25 00:03:49 +02:00
|
|
|
break;
|
|
|
|
case '\'':
|
2005-03-17 12:27:45 +01:00
|
|
|
escape= '\'';
|
2004-05-25 00:03:49 +02:00
|
|
|
break;
|
|
|
|
case '"': /* Better safe than sorry */
|
2005-03-17 12:27:45 +01:00
|
|
|
escape= '"';
|
2004-05-25 00:03:49 +02:00
|
|
|
break;
|
|
|
|
case '\032': /* This gives problems on Win32 */
|
2005-03-17 12:27:45 +01:00
|
|
|
escape= 'Z';
|
2004-05-25 00:03:49 +02:00
|
|
|
break;
|
2005-03-17 12:27:45 +01:00
|
|
|
}
|
|
|
|
if (escape)
|
|
|
|
{
|
2005-04-26 12:43:20 +02:00
|
|
|
if (to + 2 > to_end)
|
2005-03-17 12:27:45 +01:00
|
|
|
{
|
2005-07-05 18:45:32 +02:00
|
|
|
overflow= TRUE;
|
2005-03-17 12:27:45 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
*to++= '\\';
|
|
|
|
*to++= escape;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-04-26 12:43:20 +02:00
|
|
|
if (to + 1 > to_end)
|
2005-03-17 12:27:45 +01:00
|
|
|
{
|
2005-07-05 18:45:32 +02:00
|
|
|
overflow= TRUE;
|
2005-03-17 12:27:45 +01:00
|
|
|
break;
|
|
|
|
}
|
2004-05-25 00:03:49 +02:00
|
|
|
*to++= *from;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*to= 0;
|
WL#3817: Simplify string / memory area types and make things more consistent (first part)
The following type conversions was done:
- Changed byte to uchar
- Changed gptr to uchar*
- Change my_string to char *
- Change my_size_t to size_t
- Change size_s to size_t
Removed declaration of byte, gptr, my_string, my_size_t and size_s.
Following function parameter changes was done:
- All string functions in mysys/strings was changed to use size_t
instead of uint for string lengths.
- All read()/write() functions changed to use size_t (including vio).
- All protocoll functions changed to use size_t instead of uint
- Functions that used a pointer to a string length was changed to use size_t*
- Changed malloc(), free() and related functions from using gptr to use void *
as this requires fewer casts in the code and is more in line with how the
standard functions work.
- Added extra length argument to dirname_part() to return the length of the
created string.
- Changed (at least) following functions to take uchar* as argument:
- db_dump()
- my_net_write()
- net_write_command()
- net_store_data()
- DBUG_DUMP()
- decimal2bin() & bin2decimal()
- Changed my_compress() and my_uncompress() to use size_t. Changed one
argument to my_uncompress() from a pointer to a value as we only return
one value (makes function easier to use).
- Changed type of 'pack_data' argument to packfrm() to avoid casts.
- Changed in readfrm() and writefrom(), ha_discover and handler::discover()
the type for argument 'frmdata' to uchar** to avoid casts.
- Changed most Field functions to use uchar* instead of char* (reduced a lot of
casts).
- Changed field->val_xxx(xxx, new_ptr) to take const pointers.
Other changes:
- Removed a lot of not needed casts
- Added a few new cast required by other changes
- Added some cast to my_multi_malloc() arguments for safety (as string lengths
needs to be uint, not size_t).
- Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done
explicitely as this conflict was often hided by casting the function to
hash_get_key).
- Changed some buffers to memory regions to uchar* to avoid casts.
- Changed some string lengths from uint to size_t.
- Changed field->ptr to be uchar* instead of char*. This allowed us to
get rid of a lot of casts.
- Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar
- Include zlib.h in some files as we needed declaration of crc32()
- Changed MY_FILE_ERROR to be (size_t) -1.
- Changed many variables to hold the result of my_read() / my_write() to be
size_t. This was needed to properly detect errors (which are
returned as (size_t) -1).
- Removed some very old VMS code
- Changed packfrm()/unpackfrm() to not be depending on uint size
(portability fix)
- Removed windows specific code to restore cursor position as this
causes slowdown on windows and we should not mix read() and pread()
calls anyway as this is not thread safe. Updated function comment to
reflect this. Changed function that depended on original behavior of
my_pwrite() to itself restore the cursor position (one such case).
- Added some missing checking of return value of malloc().
- Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow.
- Changed type of table_def::m_size from my_size_t to ulong to reflect that
m_size is the number of elements in the array, not a string/memory
length.
- Moved THD::max_row_length() to table.cc (as it's not depending on THD).
Inlined max_row_length_blob() into this function.
- More function comments
- Fixed some compiler warnings when compiled without partitions.
- Removed setting of LEX_STRING() arguments in declaration (portability fix).
- Some trivial indentation/variable name changes.
- Some trivial code simplifications:
- Replaced some calls to alloc_root + memcpy to use
strmake_root()/strdup_root().
- Changed some calls from memdup() to strmake() (Safety fix)
- Simpler loops in client-simple.c
2007-05-10 11:59:39 +02:00
|
|
|
return overflow ? (size_t) -1 : (size_t) (to - to_start);
|
2004-05-25 00:03:49 +02:00
|
|
|
}
|
2005-03-17 12:27:45 +01:00
|
|
|
|
2005-06-24 03:29:56 +02:00
|
|
|
|
2005-08-08 16:52:30 +02:00
|
|
|
#ifdef BACKSLASH_MBTAIL
|
|
|
|
static CHARSET_INFO *fs_cset_cache= NULL;
|
|
|
|
|
|
|
|
CHARSET_INFO *fs_character_set()
|
|
|
|
{
|
|
|
|
if (!fs_cset_cache)
|
|
|
|
{
|
|
|
|
char buf[10]= "cp";
|
|
|
|
GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_IDEFAULTANSICODEPAGE,
|
|
|
|
buf+2, sizeof(buf)-3);
|
|
|
|
/*
|
|
|
|
We cannot call get_charset_by_name here
|
|
|
|
because fs_character_set() is executed before
|
|
|
|
LOCK_THD_charset mutex initialization, which
|
|
|
|
is used inside get_charset_by_name.
|
|
|
|
As we're now interested in cp932 only,
|
|
|
|
let's just detect it using strcmp().
|
|
|
|
*/
|
|
|
|
fs_cset_cache= !strcmp(buf, "cp932") ?
|
|
|
|
&my_charset_cp932_japanese_ci : &my_charset_bin;
|
|
|
|
}
|
|
|
|
return fs_cset_cache;
|
|
|
|
}
|
|
|
|
#endif
|
2005-08-11 13:18:53 +02:00
|
|
|
|
2005-06-24 03:29:56 +02:00
|
|
|
/*
|
2005-07-05 18:45:32 +02:00
|
|
|
Escape apostrophes by doubling them up
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
escape_quotes_for_mysql()
|
|
|
|
charset_info Charset of the strings
|
|
|
|
to Buffer for escaped string
|
|
|
|
to_length Length of destination buffer, or 0
|
|
|
|
from The string to escape
|
|
|
|
length The length of the string to escape
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
This escapes the contents of a string by doubling up any apostrophes that
|
|
|
|
it contains. This is used when the NO_BACKSLASH_ESCAPES SQL_MODE is in
|
|
|
|
effect on the server.
|
|
|
|
|
2005-06-24 03:29:56 +02:00
|
|
|
NOTE
|
2005-07-05 18:45:32 +02:00
|
|
|
To be consistent with escape_string_for_mysql(), to_length may be 0 to
|
2005-06-24 03:29:56 +02:00
|
|
|
mean "big enough"
|
2005-07-05 18:45:32 +02:00
|
|
|
|
|
|
|
RETURN VALUES
|
|
|
|
~0 The escaped string did not fit in the to buffer
|
|
|
|
>=0 The length of the escaped string
|
2005-06-24 03:29:56 +02:00
|
|
|
*/
|
2005-07-28 15:10:14 +02:00
|
|
|
|
WL#3817: Simplify string / memory area types and make things more consistent (first part)
The following type conversions was done:
- Changed byte to uchar
- Changed gptr to uchar*
- Change my_string to char *
- Change my_size_t to size_t
- Change size_s to size_t
Removed declaration of byte, gptr, my_string, my_size_t and size_s.
Following function parameter changes was done:
- All string functions in mysys/strings was changed to use size_t
instead of uint for string lengths.
- All read()/write() functions changed to use size_t (including vio).
- All protocoll functions changed to use size_t instead of uint
- Functions that used a pointer to a string length was changed to use size_t*
- Changed malloc(), free() and related functions from using gptr to use void *
as this requires fewer casts in the code and is more in line with how the
standard functions work.
- Added extra length argument to dirname_part() to return the length of the
created string.
- Changed (at least) following functions to take uchar* as argument:
- db_dump()
- my_net_write()
- net_write_command()
- net_store_data()
- DBUG_DUMP()
- decimal2bin() & bin2decimal()
- Changed my_compress() and my_uncompress() to use size_t. Changed one
argument to my_uncompress() from a pointer to a value as we only return
one value (makes function easier to use).
- Changed type of 'pack_data' argument to packfrm() to avoid casts.
- Changed in readfrm() and writefrom(), ha_discover and handler::discover()
the type for argument 'frmdata' to uchar** to avoid casts.
- Changed most Field functions to use uchar* instead of char* (reduced a lot of
casts).
- Changed field->val_xxx(xxx, new_ptr) to take const pointers.
Other changes:
- Removed a lot of not needed casts
- Added a few new cast required by other changes
- Added some cast to my_multi_malloc() arguments for safety (as string lengths
needs to be uint, not size_t).
- Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done
explicitely as this conflict was often hided by casting the function to
hash_get_key).
- Changed some buffers to memory regions to uchar* to avoid casts.
- Changed some string lengths from uint to size_t.
- Changed field->ptr to be uchar* instead of char*. This allowed us to
get rid of a lot of casts.
- Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar
- Include zlib.h in some files as we needed declaration of crc32()
- Changed MY_FILE_ERROR to be (size_t) -1.
- Changed many variables to hold the result of my_read() / my_write() to be
size_t. This was needed to properly detect errors (which are
returned as (size_t) -1).
- Removed some very old VMS code
- Changed packfrm()/unpackfrm() to not be depending on uint size
(portability fix)
- Removed windows specific code to restore cursor position as this
causes slowdown on windows and we should not mix read() and pread()
calls anyway as this is not thread safe. Updated function comment to
reflect this. Changed function that depended on original behavior of
my_pwrite() to itself restore the cursor position (one such case).
- Added some missing checking of return value of malloc().
- Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow.
- Changed type of table_def::m_size from my_size_t to ulong to reflect that
m_size is the number of elements in the array, not a string/memory
length.
- Moved THD::max_row_length() to table.cc (as it's not depending on THD).
Inlined max_row_length_blob() into this function.
- More function comments
- Fixed some compiler warnings when compiled without partitions.
- Removed setting of LEX_STRING() arguments in declaration (portability fix).
- Some trivial indentation/variable name changes.
- Some trivial code simplifications:
- Replaced some calls to alloc_root + memcpy to use
strmake_root()/strdup_root().
- Changed some calls from memdup() to strmake() (Safety fix)
- Simpler loops in client-simple.c
2007-05-10 11:59:39 +02:00
|
|
|
size_t escape_quotes_for_mysql(CHARSET_INFO *charset_info,
|
|
|
|
char *to, size_t to_length,
|
|
|
|
const char *from, size_t length)
|
2005-06-24 03:29:56 +02:00
|
|
|
{
|
|
|
|
const char *to_start= to;
|
|
|
|
const char *end, *to_end=to_start + (to_length ? to_length-1 : 2*length);
|
2005-07-05 18:45:32 +02:00
|
|
|
my_bool overflow= FALSE;
|
2005-06-24 03:29:56 +02:00
|
|
|
#ifdef USE_MB
|
|
|
|
my_bool use_mb_flag= use_mb(charset_info);
|
|
|
|
#endif
|
|
|
|
for (end= from + length; from < end; from++)
|
|
|
|
{
|
|
|
|
#ifdef USE_MB
|
|
|
|
int tmp_length;
|
|
|
|
if (use_mb_flag && (tmp_length= my_ismbchar(charset_info, from, end)))
|
|
|
|
{
|
|
|
|
if (to + tmp_length > to_end)
|
|
|
|
{
|
2005-07-05 18:45:32 +02:00
|
|
|
overflow= TRUE;
|
2005-06-24 03:29:56 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
while (tmp_length--)
|
|
|
|
*to++= *from++;
|
|
|
|
from--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
We don't have the same issue here with a non-multi-byte character being
|
|
|
|
turned into a multi-byte character by the addition of an escaping
|
|
|
|
character, because we are only escaping the ' character with itself.
|
|
|
|
*/
|
|
|
|
#endif
|
|
|
|
if (*from == '\'')
|
|
|
|
{
|
|
|
|
if (to + 2 > to_end)
|
|
|
|
{
|
2005-07-05 18:45:32 +02:00
|
|
|
overflow= TRUE;
|
2005-06-24 03:29:56 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
*to++= '\'';
|
|
|
|
*to++= '\'';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (to + 1 > to_end)
|
|
|
|
{
|
2005-07-05 18:45:32 +02:00
|
|
|
overflow= TRUE;
|
2005-06-24 03:29:56 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
*to++= *from;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*to= 0;
|
|
|
|
return overflow ? (ulong)~0 : (ulong) (to - to_start);
|
|
|
|
}
|