~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to drizzled/charset.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-03-18 12:12:31 UTC
  • Revision ID: james.westby@ubuntu.com-20100318121231-k6g1xe6cshbwa0f8
Tags: upstream-2010.03.1347
ImportĀ upstreamĀ versionĀ 2010.03.1347

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#include "config.h"
 
17
 
 
18
#include "drizzled/charset.h"
 
19
#include "drizzled/error.h"
 
20
#include "drizzled/charset_info.h"
 
21
#include "drizzled/internal/m_string.h"
 
22
#include <drizzled/configmake.h>
 
23
#include <vector>
 
24
 
 
25
using namespace std;
 
26
 
 
27
namespace drizzled
 
28
{
 
29
 
 
30
/*
 
31
  We collect memory in this vector that we free on delete.
 
32
*/
 
33
static vector<void *>memory_vector;
 
34
 
 
35
/*
 
36
  The code below implements this functionality:
 
37
 
 
38
    - Initializing charset related structures
 
39
    - Loading dynamic charsets
 
40
    - Searching for a proper CHARSET_INFO
 
41
      using charset name, collation name or collation ID
 
42
    - Setting server default character set
 
43
*/
 
44
 
 
45
bool my_charset_same(const CHARSET_INFO *cs1, const CHARSET_INFO *cs2)
 
46
{
 
47
  return ((cs1 == cs2) || !strcmp(cs1->csname,cs2->csname));
 
48
}
 
49
 
 
50
 
 
51
static uint
 
52
get_collation_number_internal(const char *name)
 
53
{
 
54
  CHARSET_INFO **cs;
 
55
  for (cs= all_charsets;
 
56
       cs < all_charsets+array_elements(all_charsets)-1 ;
 
57
       cs++)
 
58
  {
 
59
    if ( cs[0] && cs[0]->name &&
 
60
         !my_strcasecmp(&my_charset_utf8_general_ci, cs[0]->name, name))
 
61
      return cs[0]->number;
 
62
  }
 
63
  return 0;
 
64
}
 
65
 
 
66
 
 
67
static bool init_state_maps(CHARSET_INFO *cs)
 
68
{
 
69
  uint32_t i;
 
70
  unsigned char *state_map;
 
71
  unsigned char *ident_map;
 
72
 
 
73
  if (!(cs->state_map= (unsigned char*) cs_alloc(256)))
 
74
    return 1;
 
75
    
 
76
  if (!(cs->ident_map= (unsigned char*) cs_alloc(256)))
 
77
    return 1;
 
78
 
 
79
  state_map= cs->state_map;
 
80
  ident_map= cs->ident_map;
 
81
 
 
82
  /* Fill state_map with states to get a faster parser */
 
83
  for (i=0; i < 256 ; i++)
 
84
  {
 
85
    if (my_isalpha(cs,i))
 
86
      state_map[i]=(unsigned char) MY_LEX_IDENT;
 
87
    else if (my_isdigit(cs,i))
 
88
      state_map[i]=(unsigned char) MY_LEX_NUMBER_IDENT;
 
89
    else if (my_mbcharlen(cs, i)>1)
 
90
      state_map[i]=(unsigned char) MY_LEX_IDENT;
 
91
    else if (my_isspace(cs,i))
 
92
      state_map[i]=(unsigned char) MY_LEX_SKIP;
 
93
    else
 
94
      state_map[i]=(unsigned char) MY_LEX_CHAR;
 
95
  }
 
96
  state_map[(unsigned char)'_']=state_map[(unsigned char)'$']=(unsigned char) MY_LEX_IDENT;
 
97
  state_map[(unsigned char)'\'']=(unsigned char) MY_LEX_STRING;
 
98
  state_map[(unsigned char)'.']=(unsigned char) MY_LEX_REAL_OR_POINT;
 
99
  state_map[(unsigned char)'>']=state_map[(unsigned char)'=']=state_map[(unsigned char)'!']= (unsigned char) MY_LEX_CMP_OP;
 
100
  state_map[(unsigned char)'<']= (unsigned char) MY_LEX_LONG_CMP_OP;
 
101
  state_map[(unsigned char)'&']=state_map[(unsigned char)'|']=(unsigned char) MY_LEX_BOOL;
 
102
  state_map[(unsigned char)'#']=(unsigned char) MY_LEX_COMMENT;
 
103
  state_map[(unsigned char)';']=(unsigned char) MY_LEX_SEMICOLON;
 
104
  state_map[(unsigned char)':']=(unsigned char) MY_LEX_SET_VAR;
 
105
  state_map[0]=(unsigned char) MY_LEX_EOL;
 
106
  state_map[(unsigned char)'\\']= (unsigned char) MY_LEX_ESCAPE;
 
107
  state_map[(unsigned char)'/']= (unsigned char) MY_LEX_LONG_COMMENT;
 
108
  state_map[(unsigned char)'*']= (unsigned char) MY_LEX_END_LONG_COMMENT;
 
109
  state_map[(unsigned char)'@']= (unsigned char) MY_LEX_USER_END;
 
110
  state_map[(unsigned char) '`']= (unsigned char) MY_LEX_USER_VARIABLE_DELIMITER;
 
111
  state_map[(unsigned char)'"']= (unsigned char) MY_LEX_STRING_OR_DELIMITER;
 
112
 
 
113
  /*
 
114
    Create a second map to make it faster to find identifiers
 
115
  */
 
116
  for (i=0; i < 256 ; i++)
 
117
  {
 
118
    ident_map[i]= (unsigned char) (state_map[i] == MY_LEX_IDENT ||
 
119
                           state_map[i] == MY_LEX_NUMBER_IDENT);
 
120
  }
 
121
 
 
122
  /* Special handling of hex and binary strings */
 
123
  state_map[(unsigned char)'x']= state_map[(unsigned char)'X']= (unsigned char) MY_LEX_IDENT_OR_HEX;
 
124
  state_map[(unsigned char)'b']= state_map[(unsigned char)'B']= (unsigned char) MY_LEX_IDENT_OR_BIN;
 
125
  return 0;
 
126
}
 
127
 
 
128
 
 
129
static bool charset_initialized= false;
 
130
 
 
131
CHARSET_INFO *all_charsets[256];
 
132
const CHARSET_INFO *default_charset_info = &my_charset_utf8_general_ci;
 
133
 
 
134
void add_compiled_collation(CHARSET_INFO * cs)
 
135
{
 
136
  all_charsets[cs->number]= cs;
 
137
  cs->state|= MY_CS_AVAILABLE;
 
138
}
 
139
 
 
140
void *cs_alloc(size_t size)
 
141
{
 
142
  void *ptr= malloc(size);
 
143
 
 
144
  memory_vector.push_back(ptr);
 
145
 
 
146
  return ptr;
 
147
}
 
148
 
 
149
 
 
150
 
 
151
static bool init_available_charsets(myf myflags)
 
152
{
 
153
  bool error= false;
 
154
  /*
 
155
    We have to use charset_initialized to not lock on THR_LOCK_charset
 
156
    inside get_internal_charset...
 
157
  */
 
158
  if (charset_initialized == false)
 
159
  {
 
160
    CHARSET_INFO **cs;
 
161
    memset(&all_charsets, 0, sizeof(all_charsets));
 
162
    init_compiled_charsets(myflags);
 
163
 
 
164
    /* Copy compiled charsets */
 
165
    for (cs=all_charsets;
 
166
         cs < all_charsets+array_elements(all_charsets)-1 ;
 
167
         cs++)
 
168
    {
 
169
      if (*cs)
 
170
      {
 
171
        if (cs[0]->ctype)
 
172
          if (init_state_maps(*cs))
 
173
            *cs= NULL;
 
174
      }
 
175
    }
 
176
 
 
177
    charset_initialized= true;
 
178
  }
 
179
  assert(charset_initialized);
 
180
 
 
181
  return error;
 
182
}
 
183
 
 
184
 
 
185
void free_charsets(void)
 
186
{
 
187
  charset_initialized= true;
 
188
 
 
189
  while (memory_vector.empty() == false)
 
190
  {
 
191
    void *ptr= memory_vector.back();
 
192
    memory_vector.pop_back();
 
193
    free(ptr);
 
194
  }
 
195
  memory_vector.clear();
 
196
 
 
197
}
 
198
 
 
199
 
 
200
uint32_t get_collation_number(const char *name)
 
201
{
 
202
  init_available_charsets(MYF(0));
 
203
  return get_collation_number_internal(name);
 
204
}
 
205
 
 
206
 
 
207
uint32_t get_charset_number(const char *charset_name, uint32_t cs_flags)
 
208
{
 
209
  CHARSET_INFO **cs;
 
210
  init_available_charsets(MYF(0));
 
211
 
 
212
  for (cs= all_charsets;
 
213
       cs < all_charsets+array_elements(all_charsets)-1 ;
 
214
       cs++)
 
215
  {
 
216
    if ( cs[0] && cs[0]->csname && (cs[0]->state & cs_flags) &&
 
217
         !my_strcasecmp(&my_charset_utf8_general_ci, cs[0]->csname, charset_name))
 
218
      return cs[0]->number;
 
219
  }
 
220
  return 0;
 
221
}
 
222
 
 
223
 
 
224
const char *get_charset_name(uint32_t charset_number)
 
225
{
 
226
  const CHARSET_INFO *cs;
 
227
  init_available_charsets(MYF(0));
 
228
 
 
229
  cs=all_charsets[charset_number];
 
230
  if (cs && (cs->number == charset_number) && cs->name )
 
231
    return (char*) cs->name;
 
232
 
 
233
  return (char*) "?";   /* this mimics find_type() */
 
234
}
 
235
 
 
236
 
 
237
static const CHARSET_INFO *get_internal_charset(uint32_t cs_number)
 
238
{
 
239
  CHARSET_INFO *cs;
 
240
  /*
 
241
    To make things thread safe we are not allowing other threads to interfere
 
242
    while we may changing the cs_info_table
 
243
  */
 
244
  if ((cs= all_charsets[cs_number]))
 
245
  {
 
246
    if (!(cs->state & MY_CS_COMPILED) && !(cs->state & MY_CS_LOADED))
 
247
    {
 
248
      assert(0);
 
249
    }
 
250
    cs= (cs->state & MY_CS_AVAILABLE) ? cs : NULL;
 
251
  }
 
252
  if (cs && !(cs->state & MY_CS_READY))
 
253
  {
 
254
    if ((cs->cset->init && cs->cset->init(cs, cs_alloc)) ||
 
255
        (cs->coll->init && cs->coll->init(cs, cs_alloc)))
 
256
      cs= NULL;
 
257
    else
 
258
      cs->state|= MY_CS_READY;
 
259
  }
 
260
 
 
261
  return cs;
 
262
}
 
263
 
 
264
 
 
265
const CHARSET_INFO *get_charset(uint32_t cs_number)
 
266
{
 
267
  const CHARSET_INFO *cs;
 
268
  if (cs_number == default_charset_info->number)
 
269
    return default_charset_info;
 
270
 
 
271
  (void) init_available_charsets(MYF(0));       /* If it isn't initialized */
 
272
 
 
273
  if (!cs_number || cs_number >= array_elements(all_charsets)-1)
 
274
    return NULL;
 
275
 
 
276
  cs= get_internal_charset(cs_number);
 
277
 
 
278
  return cs;
 
279
}
 
280
 
 
281
const CHARSET_INFO *get_charset_by_name(const char *cs_name)
 
282
{
 
283
  uint32_t cs_number;
 
284
  const CHARSET_INFO *cs;
 
285
  (void) init_available_charsets(MYF(0));       /* If it isn't initialized */
 
286
 
 
287
  cs_number= get_collation_number(cs_name);
 
288
  cs= cs_number ? get_internal_charset(cs_number) : NULL;
 
289
 
 
290
  return cs;
 
291
}
 
292
 
 
293
 
 
294
const CHARSET_INFO *get_charset_by_csname(const char *cs_name, uint32_t cs_flags)
 
295
{
 
296
  uint32_t cs_number;
 
297
  const CHARSET_INFO *cs;
 
298
 
 
299
  (void) init_available_charsets(MYF(0));       /* If it isn't initialized */
 
300
 
 
301
  cs_number= get_charset_number(cs_name, cs_flags);
 
302
  cs= cs_number ? get_internal_charset(cs_number) : NULL;
 
303
 
 
304
  return(cs);
 
305
}
 
306
 
 
307
 
 
308
/*
 
309
  Escape apostrophes by doubling them up
 
310
 
 
311
  SYNOPSIS
 
312
    escape_quotes_for_drizzle()
 
313
    charset_info        Charset of the strings
 
314
    to                  Buffer for escaped string
 
315
    to_length           Length of destination buffer, or 0
 
316
    from                The string to escape
 
317
    length              The length of the string to escape
 
318
 
 
319
  DESCRIPTION
 
320
    This escapes the contents of a string by doubling up any apostrophes that
 
321
    it contains. This is used when the NO_BACKSLASH_ESCAPES SQL_MODE is in
 
322
    effect on the server.
 
323
 
 
324
  NOTE
 
325
    To be consistent with escape_string_for_mysql(), to_length may be 0 to
 
326
    mean "big enough"
 
327
 
 
328
  RETURN VALUES
 
329
    UINT32_MAX  The escaped string did not fit in the to buffer
 
330
    >=0         The length of the escaped string
 
331
*/
 
332
 
 
333
size_t escape_quotes_for_drizzle(const CHARSET_INFO *charset_info,
 
334
                                 char *to, size_t to_length,
 
335
                                 const char *from, size_t length)
 
336
{
 
337
  const char *to_start= to;
 
338
  const char *end, *to_end=to_start + (to_length ? to_length-1 : 2*length);
 
339
  bool overflow= false;
 
340
  bool use_mb_flag= use_mb(charset_info);
 
341
  for (end= from + length; from < end; from++)
 
342
  {
 
343
    int tmp_length;
 
344
    if (use_mb_flag && (tmp_length= my_ismbchar(charset_info, from, end)))
 
345
    {
 
346
      if (to + tmp_length > to_end)
 
347
      {
 
348
        overflow= true;
 
349
        break;
 
350
      }
 
351
      while (tmp_length--)
 
352
        *to++= *from++;
 
353
      from--;
 
354
      continue;
 
355
    }
 
356
    /*
 
357
      We don't have the same issue here with a non-multi-byte character being
 
358
      turned into a multi-byte character by the addition of an escaping
 
359
      character, because we are only escaping the ' character with itself.
 
360
     */
 
361
    if (*from == '\'')
 
362
    {
 
363
      if (to + 2 > to_end)
 
364
      {
 
365
        overflow= true;
 
366
        break;
 
367
      }
 
368
      *to++= '\'';
 
369
      *to++= '\'';
 
370
    }
 
371
    else
 
372
    {
 
373
      if (to + 1 > to_end)
 
374
      {
 
375
        overflow= true;
 
376
        break;
 
377
      }
 
378
      *to++= *from;
 
379
    }
 
380
  }
 
381
  *to= 0;
 
382
  return overflow ? UINT32_MAX : (uint32_t) (to - to_start);
 
383
}
 
384
 
 
385
} /* namespace drizzled */