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
516
520
tmp_arg[res_length-1]=tail; // save extra length
517
521
tmp_value.realloc(res_length+1);
518
522
tmp_value.length(res_length+1);
523
tmp_value.set_charset(&my_charset_bin);
519
524
tmp_value[0]=(char) (128 | key_number);
520
525
// Real encryption
521
526
bzero((char*) &ivec,sizeof(ivec));
904
910
search=res2->ptr();
905
911
search_end=search+from_length;
913
DBUG_ASSERT(res->ptr() || !offset);
907
914
ptr=res->ptr()+offset;
908
915
strend=res->ptr()+res->length();
909
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;
910
922
while (ptr < end)
912
924
if (*ptr == *search)
1013
1025
if ((length < 0) || (length > res->length()))
1014
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);
1016
1042
/* start and length are now sufficiently valid to pass to charpos function */
1017
1043
start= res->charpos((int) start);
1018
1044
length= res->charpos((int) length, (uint32) start);
1124
1150
/* if "unsigned_flag" is set, we have a *huge* positive number. */
1125
1151
if ((length <= 0) && (!args[1]->unsigned_flag))
1126
return &my_empty_string;
1152
return make_empty_result();
1128
1153
if ((res->length() <= (ulonglong) length) ||
1129
1154
(res->length() <= (char_pos= res->charpos((int) length))))
1168
1193
/* if "unsigned_flag" is set, we have a *huge* positive number. */
1169
1194
if ((length <= 0) && (!args[1]->unsigned_flag))
1170
return &my_empty_string; /* purecov: inspected */
1195
return make_empty_result(); /* purecov: inspected */
1172
1197
if (res->length() <= (ulonglong) length)
1173
1198
return res; /* purecov: inspected */
1206
1231
/* Negative or zero length, will return empty string. */
1207
1232
if ((arg_count == 3) && (length <= 0) &&
1208
1233
(length == 0 || !args[2]->unsigned_flag))
1209
return &my_empty_string;
1234
return make_empty_result();
1211
1236
/* Assumes that the maximum length of a String is < INT_MAX32. */
1212
1237
/* Set here so that rest of code sees out-of-bound value as such. */
1217
1242
/* Assumes that the maximum length of a String is < INT_MAX32. */
1218
1243
if ((!args[1]->unsigned_flag && (start < INT_MIN32 || start > INT_MAX32)) ||
1219
1244
(args[1]->unsigned_flag && ((ulonglong) start > INT_MAX32)))
1220
return &my_empty_string;
1245
return make_empty_result();
1222
1247
start= ((start < 0) ? res->numchars() + start : start - 1);
1223
1248
start= res->charpos((int) start);
1224
1249
if ((start < 0) || ((uint) start + 1 > res->length()))
1225
return &my_empty_string;
1250
return make_empty_result();
1227
1252
length= res->charpos((int) length, (uint32) start);
1228
1253
tmp_length= res->length() - start;
1286
1311
uint delimiter_length= delimiter->length();
1287
1312
if (!res->length() || !delimiter_length || !count)
1288
return &my_empty_string; // Wrong parameters
1313
return make_empty_result(); // Wrong parameters
1290
1315
res->set_charset(collation.collation);
1634
1659
if ((null_value=args[0]->null_value))
1636
1661
if (res->length() == 0)
1637
return &my_empty_string;
1662
return make_empty_result();
1638
1663
my_make_scrambled_password(tmp_value, res->ptr(), res->length());
1639
1664
str->set(tmp_value, SCRAMBLED_PASSWORD_CHAR_LENGTH, res->charset());
1658
1683
if ((null_value=args[0]->null_value))
1660
1685
if (res->length() == 0)
1661
return &my_empty_string;
1686
return make_empty_result();
1662
1687
my_make_scrambled_password_323(tmp_value, res->ptr(), res->length());
1663
1688
str->set(tmp_value, SCRAMBLED_PASSWORD_CHAR_LENGTH_323, res->charset());
1947
1971
for ( ; ; ) /* Skip pre-space */
1949
1973
if ((rc= cs->cset->mb_wc(cs, &wc, (uchar*) from, (uchar*) end)) <= 0)
1950
return &my_empty_string; /* EOL or invalid byte sequence */
1974
return make_empty_result(); /* EOL or invalid byte sequence */
1952
1976
if (rc == 1 && cs->ctype)
2279
2303
{ // Copy data to tmp_str
2280
2304
if (tmp_str.alloc(result->length()+res->length()+1) ||
2281
2305
tmp_str.copy(*result))
2282
return &my_empty_string;
2306
return make_empty_result();
2283
2307
result= &tmp_str;
2285
2309
if (tmp_str.append(STRING_WITH_LEN(","), &my_charset_bin) || tmp_str.append(*res))
2286
return &my_empty_string;
2310
return make_empty_result();
2424
2448
if (count <= 0 && (count == 0 || !args[1]->unsigned_flag))
2425
return &my_empty_string;
2449
return make_empty_result();
2427
2451
/* Assumes that the maximum length of a String is < INT_MAX32. */
2428
2452
/* Bounds check on count: If this is triggered, we will error. */
2514
2538
/* Set here so that rest of code sees out-of-bound value as such. */
2515
2539
if ((ulonglong) count > INT_MAX32)
2516
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);
2517
2555
if (count <= (res_char_length= res->numchars()))
2518
2556
{ // String to pad is big enough
2519
2557
res->length(res->charpos((int) count)); // Shorten result if longer
2616
2654
if ((ulonglong) count > INT_MAX32)
2617
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);
2619
2671
res_char_length= res->numchars();
2621
2673
if (count <= res_char_length)
2703
2755
ptr= longlong2str(dec, ans, to_base);
2704
2756
if (str->copy(ans, (uint32) (ptr-ans), default_charset()))
2705
return &my_empty_string;
2757
return make_empty_result();
2712
2764
DBUG_ASSERT(fixed == 1);
2713
2765
if (use_cached_value)
2714
2766
return null_value ? 0 : &str_value;
2716
Here we don't pass 'str' as a parameter to args[0]->val_str()
2717
as 'str' may point to 'str_value' (e.g. see Item::save_in_field()),
2718
which we use below to convert string.
2719
Use argument's 'str_value' instead.
2721
String *arg= args[0]->val_str(&args[0]->str_value);
2767
String *arg= args[0]->val_str(str);
2722
2768
uint dummy_errors;
2728
null_value= str_value.copy(arg->ptr(),arg->length(),arg->charset(),
2774
null_value= tmp_value.copy(arg->ptr(), arg->length(), arg->charset(),
2729
2775
conv_charset, &dummy_errors);
2730
return null_value ? 0 : check_well_formed_result(&str_value);
2776
return null_value ? 0 : check_well_formed_result(&tmp_value);
2733
2779
void Item_func_conv_charset::fix_length_and_dec()
2870
2916
ptr= longlong2str(dec,ans,16);
2871
2917
if (str->copy(ans,(uint32) (ptr-ans),default_charset()))
2872
return &my_empty_string; // End of memory
2918
return make_empty_result(); // End of memory
3170
3217
arg_length= arg->length();
3171
new_length= arg_length+2; /* for beginning and ending ' signs */
3173
for (from= (char*) arg->ptr(), end= from + arg_length; from < end; from++)
3174
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 */
3176
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();
3180
3281
We replace characters from the end to the beginning
3363
3466
push_warning_printf(current_thd,MYSQL_ERROR::WARN_LEVEL_ERROR,
3364
3467
ER_TOO_BIG_FOR_UNCOMPRESS,
3365
3468
ER(ER_TOO_BIG_FOR_UNCOMPRESS),
3366
current_thd->variables.max_allowed_packet);
3469
static_cast<int>(current_thd->variables.
3470
max_allowed_packet));
3369
3473
if (buffer.realloc((uint32)new_size))