1
/* Copyright (C) 2000-2006 MySQL AB
2
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
3
4
This program is free software; you can redistribute it and/or modify
4
5
it under the terms of the GNU General Public License as published by
12
13
You should have received a copy of the GNU General Public License
13
14
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
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
39
40
#include "../mysys/my_static.h" // For soundex_map
44
@todo Remove this. It is not safe to use a shared String object.
42
46
String my_empty_string("",default_charset_info);
50
Convert an array of bytes to a hexadecimal representation.
52
Used to generate a hexadecimal representation of a message digest.
54
static void array_to_hex(char *to, const char *str, uint len)
56
const char *str_end= str + len;
57
for (; str != str_end; ++str)
59
*to++= _dig_vec_lower[((uchar) *str) >> 4];
60
*to++= _dig_vec_lower[((uchar) *str) & 0x0F];
47
65
bool Item_str_func::fix_fields(THD *thd, Item **ref)
117
sprintf((char *) str->ptr(),
118
"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
119
digest[0], digest[1], digest[2], digest[3],
120
digest[4], digest[5], digest[6], digest[7],
121
digest[8], digest[9], digest[10], digest[11],
122
digest[12], digest[13], digest[14], digest[15]);
135
array_to_hex((char *) str->ptr(), (const char*) digest, 16);
123
136
str->length((uint) 32);
160
173
if (!( str->alloc(SHA1_HASH_SIZE*2) ||
161
174
(mysql_sha1_result(&context,digest))))
163
sprintf((char *) str->ptr(),
164
"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\
165
%02x%02x%02x%02x%02x%02x%02x%02x",
166
digest[0], digest[1], digest[2], digest[3],
167
digest[4], digest[5], digest[6], digest[7],
168
digest[8], digest[9], digest[10], digest[11],
169
digest[12], digest[13], digest[14], digest[15],
170
digest[16], digest[17], digest[18], digest[19]);
176
array_to_hex((char *) str->ptr(), (const char*) digest, SHA1_HASH_SIZE);
172
177
str->length((uint) SHA1_HASH_SIZE*2);
515
520
tmp_arg[res_length-1]=tail; // save extra length
516
521
tmp_value.realloc(res_length+1);
517
522
tmp_value.length(res_length+1);
523
tmp_value.set_charset(&my_charset_bin);
518
524
tmp_value[0]=(char) (128 | key_number);
519
525
// Real encryption
520
526
bzero((char*) &ivec,sizeof(ivec));
677
684
res->length() + sep_str->length() + res2->length())
679
686
/* We have room in str; We can't get any errors here */
681
{ // This is quote uncommon!
687
if (str->ptr() == res2->ptr())
688
{ // This is quite uncommon!
682
689
str->replace(0,0,*sep_str);
683
690
str->replace(0,0,*res);
903
910
search=res2->ptr();
904
911
search_end=search+from_length;
913
DBUG_ASSERT(res->ptr() || !offset);
906
914
ptr=res->ptr()+offset;
907
915
strend=res->ptr()+res->length();
908
end=strend-from_length+1;
917
In some cases val_str() can return empty string
918
with ptr() == NULL and length() == 0.
919
Let's check strend to avoid overflow.
921
end= strend ? strend - from_length + 1 : NULL;
909
922
while (ptr < end)
911
924
if (*ptr == *search)
1012
1025
if ((length < 0) || (length > res->length()))
1013
1026
length= res->length();
1029
There is one exception not handled (intentionaly) by the character set
1030
aggregation code. If one string is strong side and is binary, and
1031
another one is weak side and is a multi-byte character string,
1032
then we need to operate on the second string in terms on bytes when
1033
calling ::numchars() and ::charpos(), rather than in terms of characters.
1034
Lets substitute its character set to binary.
1036
if (collation.collation == &my_charset_bin)
1038
res->set_charset(&my_charset_bin);
1039
res2->set_charset(&my_charset_bin);
1015
1042
/* start and length are now sufficiently valid to pass to charpos function */
1016
1043
start= res->charpos((int) start);
1017
1044
length= res->charpos((int) length, (uint32) start);
1123
1150
/* if "unsigned_flag" is set, we have a *huge* positive number. */
1124
1151
if ((length <= 0) && (!args[1]->unsigned_flag))
1125
return &my_empty_string;
1152
return make_empty_result();
1127
1153
if ((res->length() <= (ulonglong) length) ||
1128
1154
(res->length() <= (char_pos= res->charpos((int) length))))
1167
1193
/* if "unsigned_flag" is set, we have a *huge* positive number. */
1168
1194
if ((length <= 0) && (!args[1]->unsigned_flag))
1169
return &my_empty_string; /* purecov: inspected */
1195
return make_empty_result(); /* purecov: inspected */
1171
1197
if (res->length() <= (ulonglong) length)
1172
1198
return res; /* purecov: inspected */
1205
1231
/* Negative or zero length, will return empty string. */
1206
1232
if ((arg_count == 3) && (length <= 0) &&
1207
1233
(length == 0 || !args[2]->unsigned_flag))
1208
return &my_empty_string;
1234
return make_empty_result();
1210
1236
/* Assumes that the maximum length of a String is < INT_MAX32. */
1211
1237
/* Set here so that rest of code sees out-of-bound value as such. */
1216
1242
/* Assumes that the maximum length of a String is < INT_MAX32. */
1217
1243
if ((!args[1]->unsigned_flag && (start < INT_MIN32 || start > INT_MAX32)) ||
1218
1244
(args[1]->unsigned_flag && ((ulonglong) start > INT_MAX32)))
1219
return &my_empty_string;
1245
return make_empty_result();
1221
1247
start= ((start < 0) ? res->numchars() + start : start - 1);
1222
1248
start= res->charpos((int) start);
1223
1249
if ((start < 0) || ((uint) start + 1 > res->length()))
1224
return &my_empty_string;
1250
return make_empty_result();
1226
1252
length= res->charpos((int) length, (uint32) start);
1227
1253
tmp_length= res->length() - start;
1285
1311
uint delimiter_length= delimiter->length();
1286
1312
if (!res->length() || !delimiter_length || !count)
1287
return &my_empty_string; // Wrong parameters
1313
return make_empty_result(); // Wrong parameters
1289
1315
res->set_charset(collation.collation);
1633
1659
if ((null_value=args[0]->null_value))
1635
1661
if (res->length() == 0)
1636
return &my_empty_string;
1662
return make_empty_result();
1637
1663
my_make_scrambled_password(tmp_value, res->ptr(), res->length());
1638
1664
str->set(tmp_value, SCRAMBLED_PASSWORD_CHAR_LENGTH, res->charset());
1657
1683
if ((null_value=args[0]->null_value))
1659
1685
if (res->length() == 0)
1660
return &my_empty_string;
1686
return make_empty_result();
1661
1687
my_make_scrambled_password_323(tmp_value, res->ptr(), res->length());
1662
1688
str->set(tmp_value, SCRAMBLED_PASSWORD_CHAR_LENGTH_323, res->charset());
1720
1745
#endif /* HAVE_CRYPT */
1748
bool Item_func_encode::seed()
1752
String *key, tmp(buf, sizeof(buf), system_charset_info);
1754
if (!(key= args[1]->val_str(&tmp)))
1757
hash_password(rand_nr, key->ptr(), key->length());
1758
sql_crypt.init(rand_nr);
1723
1763
void Item_func_encode::fix_length_and_dec()
1725
1765
max_length=args[0]->max_length;
1726
1766
maybe_null=args[0]->maybe_null || args[1]->maybe_null;
1727
1767
collation.set(&my_charset_bin);
1768
/* Precompute the seed state if the item is constant. */
1769
seeded= args[1]->const_item() &&
1770
(args[1]->result_type() == STRING_RESULT) && !seed();
1730
1773
String *Item_func_encode::val_str(String *str)
1734
String tmp_pw_value(pw_buff, sizeof(pw_buff), system_charset_info);
1736
1776
DBUG_ASSERT(fixed == 1);
1738
1778
if (!(res=args[0]->val_str(str)))
1740
null_value=1; /* purecov: inspected */
1741
return 0; /* purecov: inspected */
1744
if (!(password=args[1]->val_str(& tmp_pw_value)))
1784
if (!seeded && seed())
1751
res=copy_if_not_alloced(str,res,res->length());
1752
SQL_CRYPT sql_crypt(password->ptr(), password->length());
1791
res= copy_if_not_alloced(str, res, res->length());
1792
crypto_transform(res);
1798
void Item_func_encode::crypto_transform(String *res)
1754
1800
sql_crypt.encode((char*) res->ptr(),res->length());
1755
1801
res->set_charset(&my_charset_bin);
1759
String *Item_func_decode::val_str(String *str)
1804
void Item_func_decode::crypto_transform(String *res)
1763
String tmp_pw_value(pw_buff, sizeof(pw_buff), system_charset_info);
1765
DBUG_ASSERT(fixed == 1);
1767
if (!(res=args[0]->val_str(str)))
1769
null_value=1; /* purecov: inspected */
1770
return 0; /* purecov: inspected */
1773
if (!(password=args[1]->val_str(& tmp_pw_value)))
1780
res=copy_if_not_alloced(str,res,res->length());
1781
SQL_CRYPT sql_crypt(password->ptr(), password->length());
1783
1806
sql_crypt.decode((char*) res->ptr(),res->length());
1831
make USER() replicate properly (currently it is replicated to "")
1852
@note USER() is replicated correctly if binlog_format=ROW or (as of
1853
BUG#28086) binlog_format=MIXED, but is incorrectly replicated to ''
1854
if binlog_format=STATEMENT.
1833
1856
bool Item_func_user::init(const char *user, const char *host)
1948
1971
for ( ; ; ) /* Skip pre-space */
1950
1973
if ((rc= cs->cset->mb_wc(cs, &wc, (uchar*) from, (uchar*) end)) <= 0)
1951
return &my_empty_string; /* EOL or invalid byte sequence */
1974
return make_empty_result(); /* EOL or invalid byte sequence */
1953
1976
if (rc == 1 && cs->ctype)
2280
2303
{ // Copy data to tmp_str
2281
2304
if (tmp_str.alloc(result->length()+res->length()+1) ||
2282
2305
tmp_str.copy(*result))
2283
return &my_empty_string;
2306
return make_empty_result();
2284
2307
result= &tmp_str;
2286
2309
if (tmp_str.append(STRING_WITH_LEN(","), &my_charset_bin) || tmp_str.append(*res))
2287
return &my_empty_string;
2310
return make_empty_result();
2515
2538
/* Set here so that rest of code sees out-of-bound value as such. */
2516
2539
if ((ulonglong) count > INT_MAX32)
2517
2540
count= INT_MAX32;
2542
There is one exception not handled (intentionaly) by the character set
2543
aggregation code. If one string is strong side and is binary, and
2544
another one is weak side and is a multi-byte character string,
2545
then we need to operate on the second string in terms on bytes when
2546
calling ::numchars() and ::charpos(), rather than in terms of characters.
2547
Lets substitute its character set to binary.
2549
if (collation.collation == &my_charset_bin)
2551
res->set_charset(&my_charset_bin);
2552
rpad->set_charset(&my_charset_bin);
2518
2555
if (count <= (res_char_length= res->numchars()))
2519
2556
{ // String to pad is big enough
2520
2557
res->length(res->charpos((int) count)); // Shorten result if longer
2617
2654
if ((ulonglong) count > INT_MAX32)
2618
2655
count= INT_MAX32;
2658
There is one exception not handled (intentionaly) by the character set
2659
aggregation code. If one string is strong side and is binary, and
2660
another one is weak side and is a multi-byte character string,
2661
then we need to operate on the second string in terms on bytes when
2662
calling ::numchars() and ::charpos(), rather than in terms of characters.
2663
Lets substitute its character set to binary.
2665
if (collation.collation == &my_charset_bin)
2667
res->set_charset(&my_charset_bin);
2668
pad->set_charset(&my_charset_bin);
2620
2671
res_char_length= res->numchars();
2622
2673
if (count <= res_char_length)
2713
2764
DBUG_ASSERT(fixed == 1);
2714
2765
if (use_cached_value)
2715
2766
return null_value ? 0 : &str_value;
2717
Here we don't pass 'str' as a parameter to args[0]->val_str()
2718
as 'str' may point to 'str_value' (e.g. see Item::save_in_field()),
2719
which we use below to convert string.
2720
Use argument's 'str_value' instead.
2722
String *arg= args[0]->val_str(&args[0]->str_value);
2767
String *arg= args[0]->val_str(str);
2723
2768
uint dummy_errors;
2729
null_value= str_value.copy(arg->ptr(),arg->length(),arg->charset(),
2774
null_value= tmp_value.copy(arg->ptr(), arg->length(), arg->charset(),
2730
2775
conv_charset, &dummy_errors);
2731
return null_value ? 0 : check_well_formed_result(&str_value);
2776
return null_value ? 0 : check_well_formed_result(&tmp_value);
2734
2779
void Item_func_conv_charset::fix_length_and_dec()
2871
2916
ptr= longlong2str(dec,ans,16);
2872
2917
if (str->copy(ans,(uint32) (ptr-ans),default_charset()))
2873
return &my_empty_string; // End of memory
2918
return make_empty_result(); // End of memory
2960
3005
MY_RELATIVE_PATH | MY_UNPACK_FILENAME);
2962
3007
/* Read only allowed from within dir specified by secure_file_priv */
2963
if (opt_secure_file_priv &&
2964
strncmp(opt_secure_file_priv, path, strlen(opt_secure_file_priv)))
3008
if (!is_secure_file_path(path))
2967
3011
if (!my_stat(path, &stat_info, MYF(0)))
3172
3217
arg_length= arg->length();
3173
new_length= arg_length+2; /* for beginning and ending ' signs */
3175
for (from= (char*) arg->ptr(), end= from + arg_length; from < end; from++)
3176
new_length+= get_esc_bit(escmask, (uchar) *from);
3219
if (collation.collation->mbmaxlen == 1)
3221
new_length= arg_length + 2; /* for beginning and ending ' signs */
3222
for (from= (char*) arg->ptr(), end= from + arg_length; from < end; from++)
3223
new_length+= get_esc_bit(escmask, (uchar) *from);
3227
new_length= (arg_length * 2) + /* For string characters */
3228
(2 * collation.collation->mbmaxlen); /* For quotes */
3178
3231
if (tmp_value.alloc(new_length))
3234
if (collation.collation->mbmaxlen > 1)
3236
CHARSET_INFO *cs= collation.collation;
3239
to= (char*) tmp_value.ptr();
3240
to_end= (uchar*) to + new_length;
3242
/* Put leading quote */
3243
if ((mblen= cs->cset->wc_mb(cs, '\'', (uchar *) to, to_end)) <= 0)
3247
for (start= (char*) arg->ptr(), end= start + arg_length; start < end; )
3251
if ((mblen= cs->cset->mb_wc(cs, &wc, (uchar*) start, (uchar*) end)) <= 0)
3255
case 0: escape= 1; wc= '0'; break;
3256
case '\032': escape= 1; wc= 'Z'; break;
3257
case '\'': escape= 1; break;
3258
case '\\': escape= 1; break;
3259
default: escape= 0; break;
3263
if ((mblen= cs->cset->wc_mb(cs, '\\', (uchar*) to, to_end)) <= 0)
3267
if ((mblen= cs->cset->wc_mb(cs, wc, (uchar*) to, to_end)) <= 0)
3272
/* Put trailing quote */
3273
if ((mblen= cs->cset->wc_mb(cs, '\'', (uchar *) to, to_end)) <= 0)
3276
new_length= to - tmp_value.ptr();
3182
3281
We replace characters from the end to the beginning
3365
3466
push_warning_printf(current_thd,MYSQL_ERROR::WARN_LEVEL_ERROR,
3366
3467
ER_TOO_BIG_FOR_UNCOMPRESS,
3367
3468
ER(ER_TOO_BIG_FOR_UNCOMPRESS),
3368
current_thd->variables.max_allowed_packet);
3469
static_cast<int>(current_thd->variables.
3470
max_allowed_packet));
3371
3473
if (buffer.realloc((uint32)new_size))