charset.c 17.6 KB
Newer Older
unknown's avatar
unknown committed
1 2 3 4 5 6 7 8
/* 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
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
unknown's avatar
unknown committed
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
unknown's avatar
unknown committed
10 11 12 13 14 15
   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 */
unknown's avatar
unknown committed
16 17 18 19 20 21

#include "mysys_priv.h"
#include "mysys_err.h"
#include <m_ctype.h>
#include <m_string.h>
#include <my_dir.h>
unknown's avatar
unknown committed
22
#include <my_xml.h>
unknown's avatar
unknown committed
23

unknown's avatar
unknown committed
24

25
/*
unknown's avatar
unknown committed
26 27 28 29 30
  The code below implements this functionality:
  
    - Initializing charset related structures
    - Loading dynamic charsets
    - Searching for a proper CHARSET_INFO 
31
      using charset name, collation name or collation ID
unknown's avatar
unknown committed
32 33 34
    - Setting server default character set
*/

unknown's avatar
unknown committed
35 36 37 38
my_bool my_charset_same(CHARSET_INFO *cs1, CHARSET_INFO *cs2)
{
  return ((cs1 == cs2) || !strcmp(cs1->csname,cs2->csname));
}
unknown's avatar
unknown committed
39

40

unknown's avatar
unknown committed
41
static my_bool init_state_maps(CHARSET_INFO *cs)
42 43
{
  uint i;
unknown's avatar
unknown committed
44 45
  uchar *state_map;
  uchar *ident_map;
46

unknown's avatar
unknown committed
47 48 49 50 51 52 53 54 55
  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;
  
56 57 58 59 60 61 62 63
  /* 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)
64
    else if (my_mbcharlen(cs, i)>1)
65 66
      state_map[i]=(uchar) MY_LEX_IDENT;
#endif
67
    else if (my_isspace(cs,i))
68
      state_map[i]=(uchar) MY_LEX_SKIP;
69 70 71 72 73 74 75 76 77 78
    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;
79
  state_map[(uchar)';']=(uchar) MY_LEX_SEMICOLON;
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
  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;
  state_map[(uchar)'b']= state_map[(uchar)'b']= (uchar) MY_LEX_IDENT_OR_BIN;
unknown's avatar
unknown committed
101
  state_map[(uchar)'n']= state_map[(uchar)'N']= (uchar) MY_LEX_IDENT_OR_NCHAR;
unknown's avatar
unknown committed
102
  return 0;
103 104
}

105

106
static void simple_cs_init_functions(CHARSET_INFO *cs)
unknown's avatar
unknown committed
107
{
108
  if (cs->state & MY_CS_BINSORT)
109
    cs->coll= &my_collation_8bit_bin_handler;
110
  else
111
    cs->coll= &my_collation_8bit_simple_ci_handler;
112
  
113
  cs->cset= &my_charset_8bit_handler;
unknown's avatar
unknown committed
114 115
}

unknown's avatar
unknown committed
116

unknown's avatar
unknown committed
117

unknown's avatar
unknown committed
118
static int cs_copy_data(CHARSET_INFO *to, CHARSET_INFO *from)
119 120 121 122
{
  to->number= from->number ? from->number : to->number;

  if (from->csname)
unknown's avatar
unknown committed
123 124
    if (!(to->csname= my_once_strdup(from->csname,MYF(MY_WME))))
      goto err;
125 126
  
  if (from->name)
unknown's avatar
unknown committed
127 128
    if (!(to->name= my_once_strdup(from->name,MYF(MY_WME))))
      goto err;
129
  
130
  if (from->comment)
unknown's avatar
unknown committed
131 132
    if (!(to->comment= my_once_strdup(from->comment,MYF(MY_WME))))
      goto err;
133
  
134
  if (from->ctype)
135
  {
unknown's avatar
unknown committed
136 137 138 139
    if (!(to->ctype= (uchar*) my_once_memdup((char*) from->ctype,
					     MY_CS_CTYPE_TABLE_SIZE,
					     MYF(MY_WME))))
      goto err;
unknown's avatar
unknown committed
140 141
    if (init_state_maps(to))
      goto err;
142
  }
143
  if (from->to_lower)
unknown's avatar
unknown committed
144 145 146 147 148
    if (!(to->to_lower= (uchar*) my_once_memdup((char*) from->to_lower,
						MY_CS_TO_LOWER_TABLE_SIZE,
						MYF(MY_WME))))
      goto err;

149
  if (from->to_upper)
unknown's avatar
unknown committed
150 151 152 153
    if (!(to->to_upper= (uchar*) my_once_memdup((char*) from->to_upper,
						MY_CS_TO_UPPER_TABLE_SIZE,
						MYF(MY_WME))))
      goto err;
154 155
  if (from->sort_order)
  {
unknown's avatar
unknown committed
156 157 158 159
    if (!(to->sort_order= (uchar*) my_once_memdup((char*) from->sort_order,
						  MY_CS_SORT_ORDER_TABLE_SIZE,
						  MYF(MY_WME))))
      goto err;
unknown's avatar
unknown committed
160

161 162 163
  }
  if (from->tab_to_uni)
  {
164
    uint sz= MY_CS_TO_UNI_TABLE_SIZE*sizeof(uint16);
unknown's avatar
unknown committed
165 166 167
    if (!(to->tab_to_uni= (uint16*)  my_once_memdup((char*)from->tab_to_uni,
						    sz, MYF(MY_WME))))
      goto err;
168
  }
unknown's avatar
unknown committed
169 170 171
  if (from->tailoring)
    if (!(to->tailoring= my_once_strdup(from->tailoring,MYF(MY_WME))))
      goto err;
unknown's avatar
unknown committed
172 173 174 175 176

  return 0;

err:
  return 1;
177 178 179
}


unknown's avatar
unknown committed
180

181 182 183 184
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) &&
185 186
	  (cs->number && cs->name &&
	  (cs->sort_order || (cs->state & MY_CS_BINSORT) )));
187 188 189 190 191
}


static int add_collation(CHARSET_INFO *cs)
{
192
  if (cs->name && (cs->number || (cs->number=get_collation_number(cs->name))))
193 194 195 196 197 198 199 200
  {
    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));
    }
201 202 203
    
    if (cs->primary_number == cs->number)
      cs->state |= MY_CS_PRIMARY;
204
      
unknown's avatar
unknown committed
205
    if (cs->binary_number == cs->number)
206 207
      cs->state |= MY_CS_BINSORT;
    
unknown's avatar
unknown committed
208 209
    all_charsets[cs->number]->state|= cs->state;
    
210 211
    if (!(all_charsets[cs->number]->state & MY_CS_COMPILED))
    {
unknown's avatar
unknown committed
212 213 214 215
      CHARSET_INFO *new= all_charsets[cs->number];
      if (cs_copy_data(all_charsets[cs->number],cs))
        return MY_XML_ERROR;

unknown's avatar
unknown committed
216 217
      if (!strcmp(cs->csname,"ucs2") )
      {
218
#if defined(HAVE_CHARSET_ucs2) && defined(HAVE_UCA_COLLATIONS)
unknown's avatar
unknown committed
219 220
        new->cset= my_charset_ucs2_general_uca.cset;
        new->coll= my_charset_ucs2_general_uca.coll;
unknown's avatar
unknown committed
221 222 223 224 225
        new->strxfrm_multiply= my_charset_ucs2_general_uca.strxfrm_multiply;
        new->min_sort_char= my_charset_ucs2_general_uca.min_sort_char;
        new->max_sort_char= my_charset_ucs2_general_uca.max_sort_char;
        new->mbminlen= 2;
        new->mbmaxlen= 2;
unknown's avatar
unknown committed
226 227 228 229
        new->state |= MY_CS_AVAILABLE | MY_CS_LOADED;
#endif        
      }
      else
230
      {
231
        uchar *sort_order= all_charsets[cs->number]->sort_order;
unknown's avatar
unknown committed
232
        simple_cs_init_functions(all_charsets[cs->number]);
unknown's avatar
unknown committed
233 234
        new->mbminlen= 1;
        new->mbmaxlen= 1;
unknown's avatar
unknown committed
235 236 237 238 239
        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;
240 241 242 243 244 245 246 247 248 249
        
        /*
          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; 
250 251
      }
    }
252 253
    else
    {
unknown's avatar
unknown committed
254 255 256 257 258 259 260 261 262
      /*
        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().
      */
263
      CHARSET_INFO *dst= all_charsets[cs->number];
unknown's avatar
unknown committed
264
      dst->number= cs->number;
265
      if (cs->comment)
unknown's avatar
unknown committed
266 267
	if (!(dst->comment= my_once_strdup(cs->comment,MYF(MY_WME))))
	  return MY_XML_ERROR;
unknown's avatar
unknown committed
268
      if (cs->csname)
unknown's avatar
unknown committed
269 270
        if (!(dst->csname= my_once_strdup(cs->csname,MYF(MY_WME))))
	  return MY_XML_ERROR;
unknown's avatar
unknown committed
271
      if (cs->name)
unknown's avatar
unknown committed
272 273
	if (!(dst->name= my_once_strdup(cs->name,MYF(MY_WME))))
	  return MY_XML_ERROR;
274
    }
275
    cs->number= 0;
276 277
    cs->primary_number= 0;
    cs->binary_number= 0;
278 279 280 281 282 283 284 285 286
    cs->name= NULL;
    cs->state= 0;
    cs->sort_order= NULL;
    cs->state= 0;
  }
  return MY_XML_OK;
}


unknown's avatar
unknown committed
287
#define MY_MAX_ALLOWED_BUF 1024*1024
288 289 290 291 292 293 294 295 296 297 298
#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)
{
  char *buf;
  int  fd;
  uint len;
unknown's avatar
unknown committed
299
  MY_STAT stat_info;
300
  
unknown's avatar
unknown committed
301
  if (!my_stat(filename, &stat_info, MYF(myflags)) ||
unknown's avatar
unknown committed
302 303 304
       ((len= (uint)stat_info.st_size) > MY_MAX_ALLOWED_BUF) ||
       !(buf= (char *)my_malloc(len,myflags)))
    return TRUE;
305 306 307 308 309 310
  
  if ((fd=my_open(filename,O_RDONLY,myflags)) < 0)
  {
    my_free(buf,myflags);
    return TRUE;
  }
unknown's avatar
unknown committed
311
  len=read(fd,buf,len);
312 313
  my_close(fd,myflags);
  
314
  if (my_parse_charset_xml(buf,len,add_collation))
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
  {
#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
  }
  
  my_free(buf, myflags);  
  return FALSE;
}


char *get_charsets_dir(char *buf)
{
  const char *sharedir= SHAREDIR;
332
  char *res;
333 334 335 336 337 338 339 340 341 342 343 344 345
  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);
  }
346
  res= convert_dirname(buf,buf,NullS);
347
  DBUG_PRINT("info",("charsets dir: '%s'", buf));
348
  DBUG_RETURN(res);
349 350
}

351
CHARSET_INFO *all_charsets[256];
unknown's avatar
unknown committed
352
CHARSET_INFO *default_charset_info = &my_charset_latin1;
353

354
void add_compiled_collation(CHARSET_INFO *cs)
unknown's avatar
unknown committed
355 356 357 358
{
  all_charsets[cs->number]= cs;
  cs->state|= MY_CS_AVAILABLE;
}
359

360 361 362 363
static void *cs_alloc(uint size)
{
  return my_once_alloc(size, MYF(MY_WME));
}
364 365


unknown's avatar
unknown committed
366 367 368
#ifdef __NETWARE__
my_bool STDCALL init_available_charsets(myf myflags)
#else
369
static my_bool init_available_charsets(myf myflags)
unknown's avatar
unknown committed
370
#endif
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
{
  char fname[FN_REFLEN];
  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);
unknown's avatar
unknown committed
386
    if (!charset_initialized)
387
    {
unknown's avatar
unknown committed
388 389 390 391 392 393 394
      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++)
395
      {
unknown's avatar
unknown committed
396 397 398 399 400 401
        if (*cs)
        {
          if (cs[0]->ctype)
            if (init_state_maps(*cs))
              *cs= NULL;
        }
402
      }
unknown's avatar
unknown committed
403 404 405 406
      
      strmov(get_charsets_dir(fname), MY_CHARSET_INDEX);
      error= my_read_charset_file(fname,myflags);
      charset_initialized=1;
407 408 409 410 411 412 413 414 415 416 417 418 419
    }
    pthread_mutex_unlock(&THR_LOCK_charset);
  }
  return error;
}


void free_charsets(void)
{
  charset_initialized=0;
}


420
uint get_collation_number(const char *name)
421
{
422
  CHARSET_INFO **cs;
423
  init_available_charsets(MYF(0));
424
  
425 426 427
  for (cs= all_charsets;
       cs < all_charsets+array_elements(all_charsets)-1 ;
       cs++)
unknown's avatar
unknown committed
428
  {
429
    if ( cs[0] && cs[0]->name && 
430
         !my_strcasecmp(&my_charset_latin1, cs[0]->name, name))
431
      return cs[0]->number;
unknown's avatar
unknown committed
432
  }  
433 434 435
  return 0;   /* this mimics find_type() */
}

436

437 438 439
uint get_charset_number(const char *charset_name, uint cs_flags)
{
  CHARSET_INFO **cs;
440
  init_available_charsets(MYF(0));
441
  
442 443 444
  for (cs= all_charsets;
       cs < all_charsets+array_elements(all_charsets)-1 ;
       cs++)
445 446 447 448 449 450 451 452
  {
    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;
}

453 454 455 456

const char *get_charset_name(uint charset_number)
{
  CHARSET_INFO *cs;
457
  init_available_charsets(MYF(0));
458

459
  cs=all_charsets[charset_number];
unknown's avatar
unknown committed
460
  if (cs && (cs->number == charset_number) && cs->name )
unknown's avatar
unknown committed
461 462
    return (char*) cs->name;
  
463 464 465 466
  return (char*) "?";   /* this mimics find_type() */
}


467
static CHARSET_INFO *get_internal_charset(uint cs_number, myf flags)
unknown's avatar
unknown committed
468
{
unknown's avatar
unknown committed
469
  char  buf[FN_REFLEN];
unknown's avatar
unknown committed
470 471 472 473 474 475
  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);
unknown's avatar
unknown committed
476
  if ((cs= all_charsets[cs_number]))
unknown's avatar
unknown committed
477
  {
unknown's avatar
unknown committed
478 479 480 481 482 483
    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;
unknown's avatar
unknown committed
484
  }
485 486 487 488 489 490 491 492
  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;
  }
unknown's avatar
unknown committed
493
  pthread_mutex_unlock(&THR_LOCK_charset);
unknown's avatar
unknown committed
494 495 496 497 498 499 500
  return cs;
}


CHARSET_INFO *get_charset(uint cs_number, myf flags)
{
  CHARSET_INFO *cs;
501 502 503
  if (cs_number == default_charset_info->number)
    return default_charset_info;

unknown's avatar
unknown committed
504
  (void) init_available_charsets(MYF(0));	/* If it isn't initialized */
unknown's avatar
unknown committed
505
  
506
  if (!cs_number || cs_number >= array_elements(all_charsets)-1)
unknown's avatar
unknown committed
507 508
    return NULL;
  
509
  cs=get_internal_charset(cs_number, flags);
unknown's avatar
unknown committed
510

unknown's avatar
unknown committed
511
  if (!cs && (flags & MY_WME))
unknown's avatar
unknown committed
512 513
  {
    char index_file[FN_REFLEN], cs_string[23];
unknown's avatar
unknown committed
514
    strmov(get_charsets_dir(index_file),MY_CHARSET_INDEX);
unknown's avatar
unknown committed
515 516 517 518 519 520 521 522 523
    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)
{
524
  uint cs_number;
unknown's avatar
unknown committed
525 526 527
  CHARSET_INFO *cs;
  (void) init_available_charsets(MYF(0));	/* If it isn't initialized */

528
  cs_number=get_collation_number(cs_name);
529 530 531 532 533 534
  cs= cs_number ? get_internal_charset(cs_number,flags) : NULL;

  if (!cs && (flags & MY_WME))
  {
    char index_file[FN_REFLEN];
    strmov(get_charsets_dir(index_file),MY_CHARSET_INDEX);
535
    my_error(EE_UNKNOWN_COLLATION, MYF(ME_BELL), cs_name, index_file);
536 537 538 539 540 541
  }

  return cs;
}


unknown's avatar
unknown committed
542 543 544
CHARSET_INFO *get_charset_by_csname(const char *cs_name,
				    uint cs_flags,
				    myf flags)
545
{
546 547
  uint cs_number;
  CHARSET_INFO *cs;
548 549 550
  DBUG_ENTER("get_charset_by_csname");
  DBUG_PRINT("enter",("name: '%s'", cs_name));

551 552
  (void) init_available_charsets(MYF(0));	/* If it isn't initialized */
  
553 554
  cs_number= get_charset_number(cs_name, cs_flags);
  cs= cs_number ? get_internal_charset(cs_number, flags) : NULL;
555
  
unknown's avatar
unknown committed
556 557 558
  if (!cs && (flags & MY_WME))
  {
    char index_file[FN_REFLEN];
unknown's avatar
unknown committed
559
    strmov(get_charsets_dir(index_file),MY_CHARSET_INDEX);
unknown's avatar
unknown committed
560 561 562
    my_error(EE_UNKNOWN_CHARSET, MYF(ME_BELL), cs_name, index_file);
  }

563
  DBUG_RETURN(cs);
unknown's avatar
unknown committed
564
}
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585


ulong escape_string_for_mysql(CHARSET_INFO *charset_info, char *to,
                              const char *from, ulong length)
{
  const char *to_start= to;
  const char *end;
#ifdef USE_MB
  my_bool use_mb_flag= use_mb(charset_info);
#endif
  for (end= from + length; from != end; from++)
  {
#ifdef USE_MB
    int l;
    if (use_mb_flag && (l= my_ismbchar(charset_info, from, end)))
    {
      while (l--)
	*to++= *from++;
      from--;
      continue;
    }
586 587
    /*
     If the next character appears to begin a multi-byte character, we
588 589 590 591
     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.)
592 593 594 595 596 597 598

     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 = \)
    */
    if (use_mb_flag && (l= my_mbcharlen(charset_info, *from)) > 1)
    {
599 600
      *to++= '\\';
      *to++= *from;
601 602
      continue;
    }
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
#endif
    switch (*from) {
    case 0:				/* Must be escaped for 'mysql' */
      *to++= '\\';
      *to++= '0';
      break;
    case '\n':				/* Must be escaped for logs */
      *to++= '\\';
      *to++= 'n';
      break;
    case '\r':
      *to++= '\\';
      *to++= 'r';
      break;
    case '\\':
      *to++= '\\';
      *to++= '\\';
      break;
    case '\'':
      *to++= '\\';
      *to++= '\'';
      break;
    case '"':				/* Better safe than sorry */
      *to++= '\\';
      *to++= '"';
      break;
    case '\032':			/* This gives problems on Win32 */
      *to++= '\\';
      *to++= 'Z';
      break;
    default:
      *to++= *from;
    }
  }
  *to= 0;
  return (ulong) (to - to_start);
}
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665


#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