~ubuntu-branches/ubuntu/maverick/mysql-5.1/maverick-proposed

« back to all changes in this revision

Viewing changes to sql/item_strfunc.cc

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2012-02-22 14:16:05 UTC
  • mto: This revision was merged to the branch mainline in revision 20.
  • Revision ID: package-import@ubuntu.com-20120222141605-nxlu9yzc6attylc2
Tags: upstream-5.1.61
ImportĀ upstreamĀ versionĀ 5.1.61

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000-2006 MySQL AB
 
1
/*
 
2
   Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
2
3
 
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
11
12
 
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
 
 
 
15
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
 
16
*/
16
17
 
17
18
/**
18
19
  @file
39
40
#include "../mysys/my_static.h"                 // For soundex_map
40
41
C_MODE_END
41
42
 
 
43
/**
 
44
   @todo Remove this. It is not safe to use a shared String object.
 
45
 */
42
46
String my_empty_string("",default_charset_info);
43
47
 
44
48
 
461
465
  if ((null_value= args[0]->null_value))
462
466
    return 0;                                   // ENCRYPT(NULL) == NULL
463
467
  if ((res_length=res->length()) == 0)
464
 
    return &my_empty_string;
 
468
    return make_empty_result();
465
469
 
466
470
  if (arg_count == 1)
467
471
  {
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));
603
608
  if ((tail=(uint) (uchar) tmp_value[length-2]) > 8)
604
609
    goto wrong_key;                                  // Wrong key
605
610
  tmp_value.length(length-1-tail);
 
611
  tmp_value.set_charset(&my_charset_bin);
606
612
  return &tmp_value;
607
613
 
608
614
error:
652
658
    }
653
659
 
654
660
  if (i ==  arg_count)
655
 
    return &my_empty_string;
 
661
    return make_empty_result();
656
662
 
657
663
  for (i++; i < arg_count ; i++)
658
664
  {
803
809
    return 0;
804
810
  /* An empty string is a special case as the string pointer may be null */
805
811
  if (!res->length())
806
 
    return &my_empty_string;
 
812
    return make_empty_result();
807
813
  if (tmp_value.alloced_length() < res->length() &&
808
814
      tmp_value.realloc(res->length()))
809
815
  {
904
910
    search=res2->ptr();
905
911
    search_end=search+from_length;
906
912
redo:
 
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;
 
916
    /*
 
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.
 
920
    */
 
921
    end= strend ? strend - from_length + 1 : NULL;
910
922
    while (ptr < end)
911
923
    {
912
924
        if (*ptr == *search)
1013
1025
  if ((length < 0) || (length > res->length()))
1014
1026
    length= res->length();
1015
1027
 
 
1028
  /*
 
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.
 
1035
  */
 
1036
  if (collation.collation == &my_charset_bin)
 
1037
  {
 
1038
    res->set_charset(&my_charset_bin);
 
1039
    res2->set_charset(&my_charset_bin);
 
1040
  }
 
1041
 
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);
1123
1149
 
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;
1127
 
 
 
1152
    return make_empty_result();
1128
1153
  if ((res->length() <= (ulonglong) length) ||
1129
1154
      (res->length() <= (char_pos= res->charpos((int) length))))
1130
1155
    return res;
1167
1192
 
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 */
1171
1196
 
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();
1210
1235
 
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();
1221
1246
 
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();
1226
1251
 
1227
1252
  length= res->charpos((int) length, (uint32) start);
1228
1253
  tmp_length= res->length() - start;
1285
1310
  null_value=0;
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
1289
1314
 
1290
1315
  res->set_charset(collation.collation);
1291
1316
 
1634
1659
  if ((null_value=args[0]->null_value))
1635
1660
    return 0;
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());
1640
1665
  return str;
1658
1683
  if ((null_value=args[0]->null_value))
1659
1684
    return 0;
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());
1664
1689
  return str;
1686
1711
  if ((null_value=args[0]->null_value))
1687
1712
    return 0;
1688
1713
  if (res->length() == 0)
1689
 
    return &my_empty_string;
1690
 
 
 
1714
    return make_empty_result();
1691
1715
  if (arg_count == 1)
1692
1716
  {                                     // generate random salt
1693
1717
    time_t timestamp=current_thd->query_start();
1947
1971
  for ( ; ; ) /* Skip pre-space */
1948
1972
  {
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 */
1951
1975
    
1952
1976
    if (rc == 1 && cs->ctype)
1953
1977
    {
1972
1996
        {
1973
1997
          /* Extra safety - should not really happen */
1974
1998
          DBUG_ASSERT(false);
1975
 
          return &my_empty_string;
 
1999
          return make_empty_result();
1976
2000
        }
1977
2001
        to+= rc;
1978
2002
        break;
2269
2293
          else
2270
2294
          {
2271
2295
            if (tmp_str.copy(*res))             // Don't use 'str'
2272
 
              return &my_empty_string;
 
2296
              return make_empty_result();
2273
2297
            result= &tmp_str;
2274
2298
          }
2275
2299
        }
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;
2284
2308
          }
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();
2287
2311
        }
2288
2312
      }
2289
2313
    }
2422
2446
  null_value= 0;
2423
2447
 
2424
2448
  if (count <= 0 && (count == 0 || !args[1]->unsigned_flag))
2425
 
    return &my_empty_string;
 
2449
    return make_empty_result();
2426
2450
 
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;
 
2541
  /*
 
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.
 
2548
  */
 
2549
  if (collation.collation == &my_charset_bin)
 
2550
  {
 
2551
    res->set_charset(&my_charset_bin);
 
2552
    rpad->set_charset(&my_charset_bin);
 
2553
  }
 
2554
 
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;
2618
2656
 
 
2657
  /*
 
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.
 
2664
  */
 
2665
  if (collation.collation == &my_charset_bin)
 
2666
  {
 
2667
    res->set_charset(&my_charset_bin);
 
2668
    pad->set_charset(&my_charset_bin);
 
2669
  }
 
2670
 
2619
2671
  res_char_length= res->numchars();
2620
2672
 
2621
2673
  if (count <= res_char_length)
2702
2754
 
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();
2706
2758
  return str;
2707
2759
}
2708
2760
 
2712
2764
  DBUG_ASSERT(fixed == 1);
2713
2765
  if (use_cached_value)
2714
2766
    return null_value ? 0 : &str_value;
2715
 
  /* 
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.
2720
 
  */
2721
 
  String *arg= args[0]->val_str(&args[0]->str_value);
 
2767
  String *arg= args[0]->val_str(str);
2722
2768
  uint dummy_errors;
2723
2769
  if (!arg)
2724
2770
  {
2725
2771
    null_value=1;
2726
2772
    return 0;
2727
2773
  }
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);
2731
2777
}
2732
2778
 
2733
2779
void Item_func_conv_charset::fix_length_and_dec()
2869
2915
      return 0;
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
2873
2919
    return str;
2874
2920
  }
2875
2921
 
3093
3139
  if ((null_value= (args[0]->null_value || n > (ulonglong) LL(4294967295))))
3094
3140
    return 0;                                   // Null value
3095
3141
 
 
3142
  str->set_charset(collation.collation);
3096
3143
  str->length(0);
3097
3144
  int4store(buf,n);
3098
3145
 
3168
3215
  }
3169
3216
 
3170
3217
  arg_length= arg->length();
3171
 
  new_length= arg_length+2; /* for beginning and ending ' signs */
3172
3218
 
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)
 
3220
  {
 
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);
 
3224
  }
 
3225
  else
 
3226
  {
 
3227
    new_length= (arg_length * 2) +  /* For string characters */
 
3228
                (2 * collation.collation->mbmaxlen); /* For quotes */
 
3229
  }
3175
3230
 
3176
3231
  if (tmp_value.alloc(new_length))
3177
3232
    goto null;
3178
3233
 
 
3234
  if (collation.collation->mbmaxlen > 1)
 
3235
  {
 
3236
    CHARSET_INFO *cs= collation.collation;
 
3237
    int mblen;
 
3238
    uchar *to_end;
 
3239
    to= (char*) tmp_value.ptr();
 
3240
    to_end= (uchar*) to + new_length;
 
3241
 
 
3242
    /* Put leading quote */
 
3243
    if ((mblen= cs->cset->wc_mb(cs, '\'', (uchar *) to, to_end)) <= 0)
 
3244
      goto null;
 
3245
    to+= mblen;
 
3246
 
 
3247
    for (start= (char*) arg->ptr(), end= start + arg_length; start < end; )
 
3248
    {
 
3249
      my_wc_t wc;
 
3250
      bool escape;
 
3251
      if ((mblen= cs->cset->mb_wc(cs, &wc, (uchar*) start, (uchar*) end)) <= 0)
 
3252
        goto null;
 
3253
      start+= mblen;
 
3254
      switch (wc) {
 
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;
 
3260
      }
 
3261
      if (escape)
 
3262
      {
 
3263
        if ((mblen= cs->cset->wc_mb(cs, '\\', (uchar*) to, to_end)) <= 0)
 
3264
          goto null;
 
3265
        to+= mblen;
 
3266
      }
 
3267
      if ((mblen= cs->cset->wc_mb(cs, wc, (uchar*) to, to_end)) <= 0)
 
3268
        goto null;
 
3269
      to+= mblen;
 
3270
    }
 
3271
 
 
3272
    /* Put trailing quote */
 
3273
    if ((mblen= cs->cset->wc_mb(cs, '\'', (uchar *) to, to_end)) <= 0)
 
3274
      goto null;
 
3275
    to+= mblen;
 
3276
    new_length= to - tmp_value.ptr();
 
3277
    goto ret;
 
3278
  }
 
3279
 
3179
3280
  /*
3180
3281
    We replace characters from the end to the beginning
3181
3282
  */
3207
3308
    }
3208
3309
  }
3209
3310
  *to= '\'';
 
3311
 
 
3312
ret:
3210
3313
  tmp_value.length(new_length);
3211
3314
  tmp_value.set_charset(collation.collation);
3212
3315
  null_value= 0;
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));
3367
3471
    goto err;
3368
3472
  }
3369
3473
  if (buffer.realloc((uint32)new_size))