~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/item_strfunc.cc

Removed dead variable, sorted authors file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
    (This shouldn't be needed)
26
26
*/
27
27
 
28
 
#include <drizzled/server_includes.h>
29
 
#include <mysys/sha1.h>
 
28
#ifdef USE_PRAGMA_IMPLEMENTATION
 
29
#pragma implementation                          // gcc: Class implementation
 
30
#endif
 
31
 
 
32
 
 
33
#include "mysql_priv.h"
 
34
#include <m_ctype.h>
 
35
#include "my_md5.h"
 
36
#include "sha1.h"
 
37
#include "sha2.h"
30
38
#include <zlib.h>
31
 
 
32
 
#ifdef __cplusplus
33
 
extern "C" {
34
 
#endif
35
 
 
36
 
#include <mysys/my_static.h>                    // For soundex_map
37
 
 
38
 
#ifdef __cplusplus
39
 
}
40
 
#endif
41
 
 
42
 
#include <drizzled/drizzled_error_messages.h>
 
39
C_MODE_START
 
40
#include "../mysys/my_static.h"                 // For soundex_map
 
41
C_MODE_END
43
42
 
44
43
String my_empty_string("",default_charset_info);
45
44
 
53
52
    In Item_str_func::check_well_formed_result() we may set null_value
54
53
    flag on the same condition as in test() below.
55
54
  */
56
 
  maybe_null= (maybe_null || true);
 
55
  maybe_null= (maybe_null ||
 
56
               test(thd->variables.sql_mode &
 
57
                    (MODE_STRICT_TRANS_TABLES | MODE_STRICT_ALL_TABLES)));
57
58
  return res;
58
59
}
59
60
 
97
98
          (int64_t) 0);
98
99
}
99
100
 
 
101
 
 
102
String *Item_func_md5::val_str(String *str)
 
103
{
 
104
  assert(fixed == 1);
 
105
  String * sptr= args[0]->val_str(str);
 
106
  str->set_charset(&my_charset_bin);
 
107
  if (sptr)
 
108
  {
 
109
    my_MD5_CTX context;
 
110
    uchar digest[16];
 
111
 
 
112
    null_value=0;
 
113
    my_MD5Init (&context);
 
114
    my_MD5Update (&context,(uchar *) sptr->ptr(), sptr->length());
 
115
    my_MD5Final (digest, &context);
 
116
    if (str->alloc(32))                         // Ensure that memory is free
 
117
    {
 
118
      null_value=1;
 
119
      return 0;
 
120
    }
 
121
    sprintf((char *) str->ptr(),
 
122
            "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
 
123
            digest[0], digest[1], digest[2], digest[3],
 
124
            digest[4], digest[5], digest[6], digest[7],
 
125
            digest[8], digest[9], digest[10], digest[11],
 
126
            digest[12], digest[13], digest[14], digest[15]);
 
127
    str->length((uint) 32);
 
128
    return str;
 
129
  }
 
130
  null_value=1;
 
131
  return 0;
 
132
}
 
133
 
 
134
 
 
135
void Item_func_md5::fix_length_and_dec()
 
136
{
 
137
  max_length=32;
 
138
  /*
 
139
    The MD5() function treats its parameter as being a case sensitive. Thus
 
140
    we set binary collation on it so different instances of MD5() will be
 
141
    compared properly.
 
142
  */
 
143
  args[0]->collation.set(
 
144
      get_charset_by_csname(args[0]->collation.collation->csname,
 
145
                            MY_CS_BINSORT,MYF(0)), DERIVATION_COERCIBLE);
 
146
}
 
147
 
100
148
/**
101
149
  Concatenate args with the following premises:
102
150
  If only one arg (which is ok), return value of arg;
107
155
{
108
156
  assert(fixed == 1);
109
157
  String *res,*res2,*use_as_buff;
110
 
  uint32_t i;
 
158
  uint i;
111
159
  bool is_const= 0;
112
160
 
113
161
  null_value=0;
132
180
      if (res->length()+res2->length() >
133
181
          current_thd->variables.max_allowed_packet)
134
182
      {
135
 
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
183
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
136
184
                            ER_WARN_ALLOWED_PACKET_OVERFLOWED,
137
185
                            ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
138
186
                            current_thd->variables.max_allowed_packet);
175
223
          now work in place in tmp_value to set it to res | res2
176
224
        */
177
225
        /* Chop the last characters in tmp_value that isn't in res2 */
178
 
        tmp_value.length((uint32_t) (res2->ptr() - tmp_value.ptr()) +
 
226
        tmp_value.length((uint32) (res2->ptr() - tmp_value.ptr()) +
179
227
                         res2->length());
180
228
        /* Place res2 at start of tmp_value, remove chars before res2 */
181
 
        if (tmp_value.replace(0,(uint32_t) (res2->ptr() - tmp_value.ptr()),
 
229
        if (tmp_value.replace(0,(uint32) (res2->ptr() - tmp_value.ptr()),
182
230
                              *res))
183
231
          goto null;
184
232
        res= &tmp_value;
194
242
          more than 25% of memory will be overcommitted on average.
195
243
        */
196
244
 
197
 
        uint32_t concat_len= res->length() + res2->length();
 
245
        uint concat_len= res->length() + res2->length();
198
246
 
199
247
        if (tmp_value.alloced_length() < concat_len)
200
248
        {
205
253
          }
206
254
          else
207
255
          {
208
 
            uint32_t new_len = cmax(tmp_value.alloced_length() * 2, concat_len);
 
256
            uint new_len = max(tmp_value.alloced_length() * 2, concat_len);
209
257
 
210
258
            if (tmp_value.realloc(new_len))
211
259
              goto null;
237
285
  if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1))
238
286
    return;
239
287
 
240
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
288
  for (uint i=0 ; i < arg_count ; i++)
241
289
  {
242
290
    if (args[i]->collation.collation->mbmaxlen != collation.collation->mbmaxlen)
243
291
      max_result_length+= (args[i]->max_length /
267
315
  char tmp_str_buff[10];
268
316
  String tmp_sep_str(tmp_str_buff, sizeof(tmp_str_buff),default_charset_info),
269
317
         *sep_str, *res, *res2,*use_as_buff;
270
 
  uint32_t i;
 
318
  uint i;
271
319
 
272
320
  null_value=0;
273
321
  if (!(sep_str= args[0]->val_str(&tmp_sep_str)))
293
341
    if (res->length() + sep_str->length() + res2->length() >
294
342
        current_thd->variables.max_allowed_packet)
295
343
    {
296
 
      push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
344
      push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
297
345
                          ER_WARN_ALLOWED_PACKET_OVERFLOWED,
298
346
                          ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
299
347
                          current_thd->variables.max_allowed_packet);
344
392
        now work in place in tmp_value to set it to res | sep_str | res2
345
393
      */
346
394
      /* Chop the last characters in tmp_value that isn't in res2 */
347
 
      tmp_value.length((uint32_t) (res2->ptr() - tmp_value.ptr()) +
 
395
      tmp_value.length((uint32) (res2->ptr() - tmp_value.ptr()) +
348
396
                       res2->length());
349
397
      /* Place res2 at start of tmp_value, remove chars before res2 */
350
 
      if (tmp_value.replace(0,(uint32_t) (res2->ptr() - tmp_value.ptr()),
 
398
      if (tmp_value.replace(0,(uint32) (res2->ptr() - tmp_value.ptr()),
351
399
                            *res) ||
352
400
          tmp_value.replace(res->length(),0, *sep_str))
353
401
        goto null;
364
412
        25% of memory will be overcommitted on average.
365
413
      */
366
414
 
367
 
      uint32_t concat_len= res->length() + sep_str->length() + res2->length();
 
415
      uint concat_len= res->length() + sep_str->length() + res2->length();
368
416
 
369
417
      if (tmp_value.alloced_length() < concat_len)
370
418
      {
375
423
        }
376
424
        else
377
425
        {
378
 
          uint32_t new_len = cmax(tmp_value.alloced_length() * 2, concat_len);
 
426
          uint new_len = max(tmp_value.alloced_length() * 2, concat_len);
379
427
 
380
428
          if (tmp_value.realloc(new_len))
381
429
            goto null;
412
460
     so, (arg_count - 2) is safe here.
413
461
  */
414
462
  max_result_length= (uint64_t) args[0]->max_length * (arg_count - 2);
415
 
  for (uint32_t i=1 ; i < arg_count ; i++)
 
463
  for (uint i=1 ; i < arg_count ; i++)
416
464
    max_result_length+=args[i]->max_length;
417
465
 
418
466
  if (max_result_length >= MAX_BLOB_WIDTH)
449
497
#ifdef USE_MB
450
498
  if (use_mb(res->charset()))
451
499
  {
452
 
    register uint32_t l;
 
500
    register uint32 l;
453
501
    while (ptr < end)
454
502
    {
455
503
      if ((l= my_ismbchar(res->charset(),ptr,end)))
492
540
  assert(fixed == 1);
493
541
  String *res,*res2,*res3;
494
542
  int offset;
495
 
  uint32_t from_length,to_length;
 
543
  uint from_length,to_length;
496
544
  bool alloced=0;
497
545
#ifdef USE_MB
498
546
  const char *ptr,*end,*strend,*search,*search_end;
499
 
  register uint32_t l;
 
547
  register uint32 l;
500
548
  bool binary_cmp;
501
549
#endif
502
550
 
550
598
          if (res->length()-from_length + to_length >
551
599
              current_thd->variables.max_allowed_packet)
552
600
          {
553
 
            push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
601
            push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
554
602
                                ER_WARN_ALLOWED_PACKET_OVERFLOWED,
555
603
                                ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
556
604
                                func_name(),
579
627
      if (res->length()-from_length + to_length >
580
628
          current_thd->variables.max_allowed_packet)
581
629
      {
582
 
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
630
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
583
631
                            ER_WARN_ALLOWED_PACKET_OVERFLOWED,
584
632
                            ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
585
633
                            current_thd->variables.max_allowed_packet);
646
694
 
647
695
  /* start and length are now sufficiently valid to pass to charpos function */
648
696
   start= res->charpos((int) start);
649
 
   length= res->charpos((int) length, (uint32_t) start);
 
697
   length= res->charpos((int) length, (uint32) start);
650
698
 
651
699
  /* Re-testing with corrected params */
652
700
  if (start > res->length())
657
705
  if ((uint64_t) (res->length() - length + res2->length()) >
658
706
      (uint64_t) current_thd->variables.max_allowed_packet)
659
707
  {
660
 
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
708
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
661
709
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
662
710
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
663
711
                        func_name(), current_thd->variables.max_allowed_packet);
664
712
    goto null;
665
713
  }
666
714
  res=copy_if_not_alloced(str,res,res->length());
667
 
  res->replace((uint32_t) start,(uint32_t) length,*res2);
 
715
  res->replace((uint32) start,(uint32) length,*res2);
668
716
  return res;
669
717
null:
670
718
  null_value=1;
702
750
  null_value=0;
703
751
  if (multiply == 1)
704
752
  {
705
 
    uint32_t len;
 
753
    uint len;
706
754
    res= copy_if_not_alloced(str,res,res->length());
707
755
    len= converter(collation.collation, (char*) res->ptr(), res->length(),
708
756
                                        (char*) res->ptr(), res->length());
711
759
  }
712
760
  else
713
761
  {
714
 
    uint32_t len= res->length() * multiply;
 
762
    uint len= res->length() * multiply;
715
763
    tmp_value.alloc(len);
716
764
    tmp_value.set_charset(collation.collation);
717
765
    len= converter(collation.collation, (char*) res->ptr(), res->length(),
747
795
 
748
796
  /* must be int64_t to avoid truncation */
749
797
  int64_t length= args[1]->val_int();
750
 
  uint32_t char_pos;
 
798
  uint char_pos;
751
799
 
752
800
  if ((null_value=(args[0]->null_value || args[1]->null_value)))
753
801
    return 0;
803
851
  if (res->length() <= (uint64_t) length)
804
852
    return res; /* purecov: inspected */
805
853
 
806
 
  uint32_t start=res->numchars();
 
854
  uint start=res->numchars();
807
855
  if (start <= (uint) length)
808
856
    return res;
809
857
  start=res->charpos(start - (uint) length);
855
903
  if ((start < 0) || ((uint) start + 1 > res->length()))
856
904
    return &my_empty_string;
857
905
 
858
 
  length= res->charpos((int) length, (uint32_t) start);
 
906
  length= res->charpos((int) length, (uint32) start);
859
907
  tmp_length= res->length() - start;
860
 
  length= cmin(length, tmp_length);
 
908
  length= min(length, tmp_length);
861
909
 
862
910
  if (!start && (int64_t) res->length() == length)
863
911
    return res;
864
 
  tmp_value.set(*res, (uint32_t) start, (uint32_t) length);
 
912
  tmp_value.set(*res, (uint32) start, (uint32) length);
865
913
  return &tmp_value;
866
914
}
867
915
 
873
921
  collation.set(args[0]->collation);
874
922
  if (args[1]->const_item())
875
923
  {
876
 
    int32_t start= (int32_t) args[1]->val_int();
 
924
    int32 start= (int32) args[1]->val_int();
877
925
    if (start < 0)
878
926
      max_length= ((uint)(-start) > max_length) ? 0 : (uint)(-start);
879
927
    else
880
 
      max_length-= cmin((uint)(start - 1), max_length);
 
928
      max_length-= min((uint)(start - 1), max_length);
881
929
  }
882
930
  if (arg_count == 3 && args[2]->const_item())
883
931
  {
884
 
    int32_t length= (int32_t) args[2]->val_int();
 
932
    int32 length= (int32) args[2]->val_int();
885
933
    if (length <= 0)
886
934
      max_length=0; /* purecov: inspected */
887
935
    else
905
953
  assert(fixed == 1);
906
954
  String *res= args[0]->val_str(str);
907
955
  String *delimiter= args[1]->val_str(&tmp_value);
908
 
  int32_t count= (int32_t) args[2]->val_int();
909
 
  uint32_t offset;
 
956
  int32 count= (int32) args[2]->val_int();
 
957
  uint offset;
910
958
 
911
959
  if (args[0]->null_value || args[1]->null_value || args[2]->null_value)
912
960
  {                                     // string and/or delim are null
914
962
    return 0;
915
963
  }
916
964
  null_value=0;
917
 
  uint32_t delimiter_length= delimiter->length();
 
965
  uint delimiter_length= delimiter->length();
918
966
  if (!res->length() || !delimiter_length || !count)
919
967
    return &my_empty_string;            // Wrong parameters
920
968
 
928
976
    const char *end= strend-delimiter_length+1;
929
977
    const char *search= delimiter->ptr();
930
978
    const char *search_end= search+delimiter_length;
931
 
    int32_t n=0,c=count,pass;
932
 
    register uint32_t l;
 
979
    int32 n=0,c=count,pass;
 
980
    register uint32 l;
933
981
    for (pass=(count>0);pass<2;++pass)
934
982
    {
935
983
      while (ptr < end)
1036
1084
  char buff[MAX_FIELD_WIDTH], *ptr, *end;
1037
1085
  String tmp(buff,sizeof(buff),system_charset_info);
1038
1086
  String *res, *remove_str;
1039
 
  uint32_t remove_length;
 
1087
  uint remove_length;
1040
1088
 
1041
1089
  res= args[0]->val_str(str);
1042
1090
  if ((null_value=args[0]->null_value))
1082
1130
  char buff[MAX_FIELD_WIDTH], *ptr, *end;
1083
1131
  String tmp(buff, sizeof(buff), system_charset_info);
1084
1132
  String *res, *remove_str;
1085
 
  uint32_t remove_length;
 
1133
  uint remove_length;
1086
1134
 
1087
1135
  res= args[0]->val_str(str);
1088
1136
  if ((null_value=args[0]->null_value))
1103
1151
  end= ptr+res->length();
1104
1152
#ifdef USE_MB
1105
1153
  char *p=ptr;
1106
 
  register uint32_t l;
 
1154
  register uint32 l;
1107
1155
#endif
1108
1156
  if (remove_length == 1)
1109
1157
  {
1163
1211
  const char *r_ptr;
1164
1212
  String tmp(buff, sizeof(buff), system_charset_info);
1165
1213
  String *res, *remove_str;
1166
 
  uint32_t remove_length;
 
1214
  uint remove_length;
1167
1215
 
1168
1216
  res= args[0]->val_str(str);
1169
1217
  if ((null_value=args[0]->null_value))
1189
1237
  if (use_mb(res->charset()))
1190
1238
  {
1191
1239
    char *p=ptr;
1192
 
    register uint32_t l;
 
1240
    register uint32 l;
1193
1241
 loop:
1194
1242
    while (ptr + remove_length < end)
1195
1243
    {
1253
1301
}
1254
1302
 
1255
1303
 
1256
 
Item *Item_func_sysconst::safe_charset_converter(const CHARSET_INFO * const tocs)
 
1304
/* Item_func_password */
 
1305
 
 
1306
String *Item_func_password::val_str(String *str)
 
1307
{
 
1308
  assert(fixed == 1);
 
1309
  String *res= args[0]->val_str(str); 
 
1310
  if ((null_value=args[0]->null_value))
 
1311
    return 0;
 
1312
  if (res->length() == 0)
 
1313
    return &my_empty_string;
 
1314
  make_scrambled_password(tmp_value, res->c_ptr());
 
1315
  str->set(tmp_value, SCRAMBLED_PASSWORD_CHAR_LENGTH, res->charset());
 
1316
  return str;
 
1317
}
 
1318
 
 
1319
char *Item_func_password::alloc(THD *thd, const char *password)
 
1320
{
 
1321
  char *buff= (char *) thd->alloc(SCRAMBLED_PASSWORD_CHAR_LENGTH+1);
 
1322
  if (buff)
 
1323
    make_scrambled_password(buff, password);
 
1324
  return buff;
 
1325
}
 
1326
 
 
1327
#define bin_to_ascii(c) ((c)>=38?((c)-38+'a'):(c)>=12?((c)-12+'A'):(c)+'.')
 
1328
 
 
1329
String *Item_func_encrypt::val_str(String *str)
 
1330
{
 
1331
  assert(fixed == 1);
 
1332
  String *res  =args[0]->val_str(str);
 
1333
 
 
1334
#ifdef HAVE_CRYPT
 
1335
  char salt[3],*salt_ptr;
 
1336
  if ((null_value=args[0]->null_value))
 
1337
    return 0;
 
1338
  if (res->length() == 0)
 
1339
    return &my_empty_string;
 
1340
 
 
1341
  if (arg_count == 1)
 
1342
  {                                     // generate random salt
 
1343
    time_t timestamp=current_thd->query_start();
 
1344
    salt[0] = bin_to_ascii( (ulong) timestamp & 0x3f);
 
1345
    salt[1] = bin_to_ascii(( (ulong) timestamp >> 5) & 0x3f);
 
1346
    salt[2] = 0;
 
1347
    salt_ptr=salt;
 
1348
  }
 
1349
  else
 
1350
  {                                     // obtain salt from the first two bytes
 
1351
    String *salt_str=args[1]->val_str(&tmp_value);
 
1352
    if ((null_value= (args[1]->null_value || salt_str->length() < 2)))
 
1353
      return 0;
 
1354
    salt_ptr= salt_str->c_ptr();
 
1355
  }
 
1356
  pthread_mutex_lock(&LOCK_crypt);
 
1357
  char *tmp= crypt(res->c_ptr(),salt_ptr);
 
1358
  if (!tmp)
 
1359
  {
 
1360
    pthread_mutex_unlock(&LOCK_crypt);
 
1361
    null_value= 1;
 
1362
    return 0;
 
1363
  }
 
1364
  str->set(tmp, (uint) strlen(tmp), &my_charset_bin);
 
1365
  str->copy();
 
1366
  pthread_mutex_unlock(&LOCK_crypt);
 
1367
  return str;
 
1368
#else
 
1369
  null_value=1;
 
1370
  return 0;
 
1371
#endif  /* HAVE_CRYPT */
 
1372
}
 
1373
 
 
1374
void Item_func_encode::fix_length_and_dec()
 
1375
{
 
1376
  max_length=args[0]->max_length;
 
1377
  maybe_null=args[0]->maybe_null || args[1]->maybe_null;
 
1378
  collation.set(&my_charset_bin);
 
1379
}
 
1380
 
 
1381
String *Item_func_encode::val_str(String *str)
 
1382
{
 
1383
  String *res;
 
1384
  char pw_buff[80];
 
1385
  String tmp_pw_value(pw_buff, sizeof(pw_buff), system_charset_info);
 
1386
  String *password;
 
1387
  assert(fixed == 1);
 
1388
 
 
1389
  if (!(res=args[0]->val_str(str)))
 
1390
  {
 
1391
    null_value=1; /* purecov: inspected */
 
1392
    return 0; /* purecov: inspected */
 
1393
  }
 
1394
 
 
1395
  if (!(password=args[1]->val_str(& tmp_pw_value)))
 
1396
  {
 
1397
    null_value=1;
 
1398
    return 0;
 
1399
  }
 
1400
 
 
1401
  null_value=0;
 
1402
  res=copy_if_not_alloced(str,res,res->length());
 
1403
  SQL_CRYPT sql_crypt(password->ptr());
 
1404
  sql_crypt.init();
 
1405
  sql_crypt.encode((char*) res->ptr(),res->length());
 
1406
  res->set_charset(&my_charset_bin);
 
1407
  return res;
 
1408
}
 
1409
 
 
1410
String *Item_func_decode::val_str(String *str)
 
1411
{
 
1412
  String *res;
 
1413
  char pw_buff[80];
 
1414
  String tmp_pw_value(pw_buff, sizeof(pw_buff), system_charset_info);
 
1415
  String *password;
 
1416
  assert(fixed == 1);
 
1417
 
 
1418
  if (!(res=args[0]->val_str(str)))
 
1419
  {
 
1420
    null_value=1; /* purecov: inspected */
 
1421
    return 0; /* purecov: inspected */
 
1422
  }
 
1423
 
 
1424
  if (!(password=args[1]->val_str(& tmp_pw_value)))
 
1425
  {
 
1426
    null_value=1;
 
1427
    return 0;
 
1428
  }
 
1429
 
 
1430
  null_value=0;
 
1431
  res=copy_if_not_alloced(str,res,res->length());
 
1432
  SQL_CRYPT sql_crypt(password->ptr());
 
1433
  sql_crypt.init();
 
1434
  sql_crypt.decode((char*) res->ptr(),res->length());
 
1435
  return res;
 
1436
}
 
1437
 
 
1438
 
 
1439
Item *Item_func_sysconst::safe_charset_converter(CHARSET_INFO *tocs)
1257
1440
{
1258
1441
  Item_string *conv;
1259
 
  uint32_t conv_errors;
 
1442
  uint conv_errors;
1260
1443
  String tmp, cstr, *ostr= val_str(&tmp);
1261
1444
  cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
1262
1445
  if (conv_errors ||
1299
1482
  // For system threads (e.g. replication SQL thread) user may be empty
1300
1483
  if (user)
1301
1484
  {
1302
 
    const CHARSET_INFO * const cs= str_value.charset();
1303
 
    uint32_t res_length= (strlen(user)+strlen(host)+2) * cs->mbmaxlen;
 
1485
    CHARSET_INFO *cs= str_value.charset();
 
1486
    uint res_length= (strlen(user)+strlen(host)+2) * cs->mbmaxlen;
1304
1487
 
1305
1488
    if (str_value.alloc(res_length))
1306
1489
    {
1321
1504
{
1322
1505
  return (Item_func_sysconst::fix_fields(thd, ref) ||
1323
1506
          init(thd->main_security_ctx.user,
1324
 
               thd->main_security_ctx.ip));
 
1507
               thd->main_security_ctx.host_or_ip));
1325
1508
}
1326
1509
 
1327
1510
 
1332
1515
 
1333
1516
  Security_context *ctx=
1334
1517
                         thd->security_ctx;
1335
 
  return init(ctx->user, ctx->ip);
 
1518
  return init(ctx->priv_user, ctx->priv_host);
1336
1519
}
1337
1520
 
1338
1521
 
1352
1535
void Item_func_format::fix_length_and_dec()
1353
1536
{
1354
1537
  collation.set(default_charset());
1355
 
  uint32_t char_length= args[0]->max_length/args[0]->collation.collation->mbmaxlen;
 
1538
  uint char_length= args[0]->max_length/args[0]->collation.collation->mbmaxlen;
1356
1539
  max_length= ((char_length + (char_length-args[0]->decimals)/3) *
1357
1540
               collation.collation->mbmaxlen);
1358
1541
}
1366
1549
 
1367
1550
String *Item_func_format::val_str(String *str)
1368
1551
{
1369
 
  uint32_t length;
1370
 
  uint32_t str_length;
 
1552
  uint32 length;
 
1553
  uint32 str_length;
1371
1554
  /* Number of decimal digits */
1372
1555
  int dec;
1373
1556
  /* Number of characters used to represent the decimals, including '.' */
1374
 
  uint32_t dec_length;
 
1557
  uint32 dec_length;
1375
1558
  int diff;
1376
1559
  assert(fixed == 1);
1377
1560
 
1407
1590
    nr= my_double_round(nr, (int64_t) dec, false, false);
1408
1591
    /* Here default_charset() is right as this is not an automatic conversion */
1409
1592
    str->set_real(nr, dec, default_charset());
1410
 
    if (std::isnan(nr))
 
1593
    if (isnan(nr))
1411
1594
      return str;
1412
1595
    str_length=str->length();
1413
1596
    if (nr < 0)
1457
1640
  if (agg_arg_charsets(collation, args+1, arg_count-1, MY_COLL_ALLOW_CONV, 1))
1458
1641
    return;
1459
1642
 
1460
 
  for (uint32_t i= 1 ; i < arg_count ; i++)
 
1643
  for (uint i= 1 ; i < arg_count ; i++)
1461
1644
  {
1462
1645
    set_if_bigger(max_length,args[i]->max_length);
1463
1646
    set_if_bigger(decimals,args[i]->decimals);
1469
1652
double Item_func_elt::val_real()
1470
1653
{
1471
1654
  assert(fixed == 1);
1472
 
  uint32_t tmp;
 
1655
  uint tmp;
1473
1656
  null_value=1;
1474
1657
  if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
1475
1658
    return 0.0;
1482
1665
int64_t Item_func_elt::val_int()
1483
1666
{
1484
1667
  assert(fixed == 1);
1485
 
  uint32_t tmp;
 
1668
  uint tmp;
1486
1669
  null_value=1;
1487
1670
  if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
1488
1671
    return 0;
1496
1679
String *Item_func_elt::val_str(String *str)
1497
1680
{
1498
1681
  assert(fixed == 1);
1499
 
  uint32_t tmp;
 
1682
  uint tmp;
1500
1683
  null_value=1;
1501
1684
  if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
1502
1685
    return NULL;
1524
1707
  if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1))
1525
1708
    return;
1526
1709
  
1527
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
1710
  for (uint i=0 ; i < arg_count ; i++)
1528
1711
    max_length+=args[i]->max_length;
1529
1712
 
1530
1713
  used_tables_cache|=     item->used_tables();
1596
1779
}
1597
1780
 
1598
1781
 
1599
 
Item *Item_func_make_set::transform(Item_transformer transformer, unsigned char *arg)
 
1782
Item *Item_func_make_set::transform(Item_transformer transformer, uchar *arg)
1600
1783
{
1601
1784
  Item *new_item= item->transform(transformer, arg);
1602
1785
  if (!new_item)
1632
1815
  assert(fixed == 1);
1633
1816
  str->length(0);
1634
1817
  str->set_charset(collation.collation);
1635
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
1818
  for (uint i=0 ; i < arg_count ; i++)
1636
1819
  {
1637
 
    int32_t num=(int32_t) args[i]->val_int();
 
1820
    int32 num=(int32) args[i]->val_int();
1638
1821
    if (!args[i]->null_value)
1639
1822
    {
1640
1823
      char char_num= (char) num;
1713
1896
String *Item_func_repeat::val_str(String *str)
1714
1897
{
1715
1898
  assert(fixed == 1);
1716
 
  uint32_t length,tot_length;
 
1899
  uint length,tot_length;
1717
1900
  char *to;
1718
1901
  /* must be int64_t to avoid truncation */
1719
1902
  int64_t count= args[1]->val_int();
1736
1919
  // Safe length check
1737
1920
  if (length > current_thd->variables.max_allowed_packet / (uint) count)
1738
1921
  {
1739
 
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
1922
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1740
1923
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
1741
1924
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
1742
1925
                        func_name(), current_thd->variables.max_allowed_packet);
1799
1982
String *Item_func_rpad::val_str(String *str)
1800
1983
{
1801
1984
  assert(fixed == 1);
1802
 
  uint32_t res_byte_length,res_char_length,pad_char_length,pad_byte_length;
 
1985
  uint32 res_byte_length,res_char_length,pad_char_length,pad_byte_length;
1803
1986
  char *to;
1804
1987
  const char *ptr_pad;
1805
1988
  /* must be int64_t to avoid truncation */
1826
2009
  byte_count= count * collation.collation->mbmaxlen;
1827
2010
  if ((uint64_t) byte_count > current_thd->variables.max_allowed_packet)
1828
2011
  {
1829
 
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2012
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1830
2013
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
1831
2014
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
1832
2015
                        func_name(), current_thd->variables.max_allowed_packet);
1842
2025
  ptr_pad=rpad->ptr();
1843
2026
  pad_byte_length= rpad->length();
1844
2027
  count-= res_char_length;
1845
 
  for ( ; (uint32_t) count > pad_char_length; count-= pad_char_length)
 
2028
  for ( ; (uint32) count > pad_char_length; count-= pad_char_length)
1846
2029
  {
1847
2030
    memcpy(to,ptr_pad,pad_byte_length);
1848
2031
    to+= pad_byte_length;
1902
2085
String *Item_func_lpad::val_str(String *str)
1903
2086
{
1904
2087
  assert(fixed == 1);
1905
 
  uint32_t res_char_length,pad_char_length;
 
2088
  uint32 res_char_length,pad_char_length;
1906
2089
  /* must be int64_t to avoid truncation */
1907
2090
  int64_t count= args[1]->val_int();
1908
2091
  int64_t byte_count;
1931
2114
  
1932
2115
  if ((uint64_t) byte_count > current_thd->variables.max_allowed_packet)
1933
2116
  {
1934
 
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2117
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1935
2118
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
1936
2119
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
1937
2120
                        func_name(), current_thd->variables.max_allowed_packet);
1939
2122
  }
1940
2123
 
1941
2124
  if (args[2]->null_value || !pad_char_length ||
1942
 
      str->alloc((uint32_t) byte_count))
 
2125
      str->alloc((uint32) byte_count))
1943
2126
    goto err;
1944
2127
  
1945
2128
  str->length(0);
1991
2174
                                 from_base, &endptr, &err);
1992
2175
 
1993
2176
  ptr= int64_t2str(dec, ans, to_base);
1994
 
  if (str->copy(ans, (uint32_t) (ptr-ans), default_charset()))
 
2177
  if (str->copy(ans, (uint32) (ptr-ans), default_charset()))
1995
2178
    return &my_empty_string;
1996
2179
  return str;
1997
2180
}
2003
2186
  if (use_cached_value)
2004
2187
    return null_value ? 0 : &str_value;
2005
2188
  String *arg= args[0]->val_str(str);
2006
 
  uint32_t dummy_errors;
 
2189
  uint dummy_errors;
2007
2190
  if (!arg)
2008
2191
  {
2009
2192
    null_value=1;
2041
2224
 
2042
2225
void Item_func_set_collation::fix_length_and_dec()
2043
2226
{
2044
 
  const CHARSET_INFO *set_collation;
 
2227
  CHARSET_INFO *set_collation;
2045
2228
  const char *colname;
2046
2229
  String tmp, *str= args[1]->val_str(&tmp);
2047
2230
  colname= str->c_ptr();
2084
2267
  Item_func_set_collation *item_func_sc=(Item_func_set_collation*) item;
2085
2268
  if (collation.collation != item_func_sc->collation.collation)
2086
2269
    return 0;
2087
 
  for (uint32_t i=0; i < arg_count ; i++)
 
2270
  for (uint i=0; i < arg_count ; i++)
2088
2271
    if (!args[i]->eq(item_func_sc->args[i], binary_cmp))
2089
2272
      return 0;
2090
2273
  return 1;
2105
2288
String *Item_func_charset::val_str(String *str)
2106
2289
{
2107
2290
  assert(fixed == 1);
2108
 
  uint32_t dummy_errors;
 
2291
  uint dummy_errors;
2109
2292
 
2110
 
  const CHARSET_INFO * const cs= args[0]->collation.collation; 
 
2293
  CHARSET_INFO *cs= args[0]->collation.collation; 
2111
2294
  null_value= 0;
2112
2295
  str->copy(cs->csname, strlen(cs->csname),
2113
 
            &my_charset_utf8_general_ci, collation.collation, &dummy_errors);
 
2296
            &my_charset_latin1, collation.collation, &dummy_errors);
2114
2297
  return str;
2115
2298
}
2116
2299
 
2117
2300
String *Item_func_collation::val_str(String *str)
2118
2301
{
2119
2302
  assert(fixed == 1);
2120
 
  uint32_t dummy_errors;
2121
 
  const CHARSET_INFO * const cs= args[0]->collation.collation; 
 
2303
  uint dummy_errors;
 
2304
  CHARSET_INFO *cs= args[0]->collation.collation; 
2122
2305
 
2123
2306
  null_value= 0;
2124
2307
  str->copy(cs->name, strlen(cs->name),
2125
 
            &my_charset_utf8_general_ci, collation.collation, &dummy_errors);
 
2308
            &my_charset_latin1, collation.collation, &dummy_errors);
2126
2309
  return str;
2127
2310
}
2128
2311
 
2129
2312
 
2130
2313
void Item_func_weight_string::fix_length_and_dec()
2131
2314
{
2132
 
  const CHARSET_INFO * const cs= args[0]->collation.collation;
 
2315
  CHARSET_INFO *cs= args[0]->collation.collation;
2133
2316
  collation.set(&my_charset_bin, args[0]->collation.derivation);
2134
2317
  flags= my_strxfrm_flag_normalize(flags, cs->levels_for_order);
2135
 
  max_length= cs->mbmaxlen * cmax(args[0]->max_length, nweights);
 
2318
  max_length= cs->mbmaxlen * max(args[0]->max_length, nweights);
2136
2319
  maybe_null= 1;
2137
2320
}
2138
2321
 
2141
2324
String *Item_func_weight_string::val_str(String *str)
2142
2325
{
2143
2326
  String *res;
2144
 
  const CHARSET_INFO * const cs= args[0]->collation.collation;
2145
 
  uint32_t tmp_length, frm_length;
 
2327
  CHARSET_INFO *cs= args[0]->collation.collation;
 
2328
  uint tmp_length, frm_length;
2146
2329
  assert(fixed == 1);
2147
2330
 
2148
2331
  if (args[0]->result_type() != STRING_RESULT ||
2150
2333
    goto nl;
2151
2334
  
2152
2335
  tmp_length= cs->coll->strnxfrmlen(cs, cs->mbmaxlen *
2153
 
                                        cmax(res->length(), nweights));
 
2336
                                        max(res->length(), nweights));
2154
2337
 
2155
2338
  if (tmp_value.alloc(tmp_length))
2156
2339
    goto nl;
2157
2340
 
2158
2341
  frm_length= cs->coll->strnxfrm(cs,
2159
 
                                 (unsigned char*) tmp_value.ptr(), tmp_length,
 
2342
                                 (uchar*) tmp_value.ptr(), tmp_length,
2160
2343
                                 nweights ? nweights : tmp_length,
2161
 
                                 (const unsigned char*) res->ptr(), res->length(),
 
2344
                                 (const uchar*) res->ptr(), res->length(),
2162
2345
                                 flags);
2163
2346
  tmp_value.length(frm_length);
2164
2347
  null_value= 0;
2195
2378
    if ((null_value= args[0]->null_value))
2196
2379
      return 0;
2197
2380
    ptr= int64_t2str(dec,ans,16);
2198
 
    if (str->copy(ans,(uint32_t) (ptr-ans),default_charset()))
 
2381
    if (str->copy(ans,(uint32) (ptr-ans),default_charset()))
2199
2382
      return &my_empty_string;                  // End of memory
2200
2383
    return str;
2201
2384
  }
2221
2404
  const char *from, *end;
2222
2405
  char *to;
2223
2406
  String *res;
2224
 
  uint32_t length;
 
2407
  uint length;
2225
2408
  assert(fixed == 1);
2226
2409
 
2227
2410
  res= args[0]->val_str(str);
2264
2447
}
2265
2448
 
2266
2449
 
 
2450
#include <my_dir.h>
 
2451
 
2267
2452
String *Item_load_file::val_str(String *str)
2268
2453
{
2269
2454
  assert(fixed == 1);
2293
2478
  }
2294
2479
  if (stat_info.st_size > (long) current_thd->variables.max_allowed_packet)
2295
2480
  {
2296
 
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2481
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
2297
2482
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
2298
2483
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
2299
2484
                        func_name(), current_thd->variables.max_allowed_packet);
2303
2488
    goto err;
2304
2489
  if ((file = my_open(file_name->c_ptr(), O_RDONLY, MYF(0))) < 0)
2305
2490
    goto err;
2306
 
  if (my_read(file, (unsigned char*) tmp_value.ptr(), stat_info.st_size, MYF(MY_NABP)))
 
2491
  if (my_read(file, (uchar*) tmp_value.ptr(), stat_info.st_size, MYF(MY_NABP)))
2307
2492
  {
2308
2493
    my_close(file, MYF(0));
2309
2494
    goto err;
2329
2514
  no = args[2]->val_str(&no_buf);
2330
2515
  String *sep = NULL, sep_buf ;
2331
2516
 
2332
 
  uint32_t num_set_values = 64;
 
2517
  uint num_set_values = 64;
2333
2518
  uint64_t mask = 0x1;
2334
2519
  str->length(0);
2335
2520
  str->set_charset(collation.collation);
2365
2550
  case 3:
2366
2551
    {
2367
2552
      /* errors is not checked - assume "," can always be converted */
2368
 
      uint32_t errors;
 
2553
      uint errors;
2369
2554
      sep_buf.copy(STRING_WITH_LEN(","), &my_charset_bin, collation.collation, &errors);
2370
2555
      sep = &sep_buf;
2371
2556
    }
2375
2560
  }
2376
2561
  null_value=0;
2377
2562
 
2378
 
  for (uint32_t i = 0; i < num_set_values; i++, mask = (mask << 1))
 
2563
  for (uint i = 0; i < num_set_values; i++, mask = (mask << 1))
2379
2564
  {
2380
2565
    if (the_set & mask)
2381
2566
      str->append(*yes);
2389
2574
 
2390
2575
void Item_func_export_set::fix_length_and_dec()
2391
2576
{
2392
 
  uint32_t length=cmax(args[1]->max_length,args[2]->max_length);
2393
 
  uint32_t sep_length=(arg_count > 3 ? args[3]->max_length : 1);
 
2577
  uint length=max(args[1]->max_length,args[2]->max_length);
 
2578
  uint sep_length=(arg_count > 3 ? args[3]->max_length : 1);
2394
2579
  max_length=length*64+sep_length*63;
2395
2580
 
2396
 
  if (agg_arg_charsets(collation, args+1, cmin((uint)4,arg_count)-1,
 
2581
  if (agg_arg_charsets(collation, args+1, min(4,arg_count)-1,
2397
2582
                       MY_COLL_ALLOW_CONV, 1))
2398
2583
    return;
2399
2584
}
2428
2613
    0, \, ' and ^Z
2429
2614
  */
2430
2615
 
2431
 
  static unsigned char escmask[32]=
 
2616
  static uchar escmask[32]=
2432
2617
  {
2433
2618
    0x01, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00, 0x00,
2434
2619
    0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,
2438
2623
 
2439
2624
  char *from, *to, *end, *start;
2440
2625
  String *arg= args[0]->val_str(str);
2441
 
  uint32_t arg_length, new_length;
 
2626
  uint arg_length, new_length;
2442
2627
  if (!arg)                                     // Null argument
2443
2628
  {
2444
2629
    /* Return the string 'NULL' */
2451
2636
  new_length= arg_length+2; /* for beginning and ending ' signs */
2452
2637
 
2453
2638
  for (from= (char*) arg->ptr(), end= from + arg_length; from < end; from++)
2454
 
    new_length+= get_esc_bit(escmask, (unsigned char) *from);
 
2639
    new_length+= get_esc_bit(escmask, (uchar) *from);
2455
2640
 
2456
2641
  if (tmp_value.alloc(new_length))
2457
2642
    goto null;
2497
2682
  return 0;
2498
2683
}
2499
2684
 
 
2685
int64_t Item_func_uncompressed_length::val_int()
 
2686
{
 
2687
  assert(fixed == 1);
 
2688
  String *res= args[0]->val_str(&value);
 
2689
  if (!res)
 
2690
  {
 
2691
    null_value=1;
 
2692
    return 0; /* purecov: inspected */
 
2693
  }
 
2694
  null_value=0;
 
2695
  if (res->is_empty()) return 0;
 
2696
 
 
2697
  /*
 
2698
    res->ptr() using is safe because we have tested that string is not empty,
 
2699
    res->c_ptr() is not used because:
 
2700
      - we do not need \0 terminated string to get first 4 bytes
 
2701
      - c_ptr() tests simbol after string end (uninitialiozed memory) which
 
2702
        confuse valgrind
 
2703
  */
 
2704
  return uint4korr(res->ptr()) & 0x3FFFFFFF;
 
2705
}
 
2706
 
 
2707
int64_t Item_func_crc32::val_int()
 
2708
{
 
2709
  assert(fixed == 1);
 
2710
  String *res=args[0]->val_str(&value);
 
2711
  if (!res)
 
2712
  {
 
2713
    null_value=1;
 
2714
    return 0; /* purecov: inspected */
 
2715
  }
 
2716
  null_value=0;
 
2717
  return (int64_t) crc32(0L, (uchar*)res->ptr(), res->length());
 
2718
}
 
2719
 
 
2720
#ifdef HAVE_COMPRESS
 
2721
#include "zlib.h"
 
2722
 
 
2723
String *Item_func_compress::val_str(String *str)
 
2724
{
 
2725
  int err= Z_OK, code;
 
2726
  ulong new_size;
 
2727
  String *res;
 
2728
  Byte *body;
 
2729
  char *tmp, *last_char;
 
2730
  assert(fixed == 1);
 
2731
 
 
2732
  if (!(res= args[0]->val_str(str)))
 
2733
  {
 
2734
    null_value= 1;
 
2735
    return 0;
 
2736
  }
 
2737
  null_value= 0;
 
2738
  if (res->is_empty()) return res;
 
2739
 
 
2740
  /*
 
2741
    Citation from zlib.h (comment for compress function):
 
2742
 
 
2743
    Compresses the source buffer into the destination buffer.  sourceLen is
 
2744
    the byte length of the source buffer. Upon entry, destLen is the total
 
2745
    size of the destination buffer, which must be at least 0.1% larger than
 
2746
    sourceLen plus 12 bytes.
 
2747
    We assume here that the buffer can't grow more than .25 %.
 
2748
  */
 
2749
  new_size= res->length() + res->length() / 5 + 12;
 
2750
 
 
2751
  // Check new_size overflow: new_size <= res->length()
 
2752
  if (((uint32) (new_size+5) <= res->length()) || 
 
2753
      buffer.realloc((uint32) new_size + 4 + 1))
 
2754
  {
 
2755
    null_value= 1;
 
2756
    return 0;
 
2757
  }
 
2758
 
 
2759
  body= ((Byte*)buffer.ptr()) + 4;
 
2760
 
 
2761
  // As far as we have checked res->is_empty() we can use ptr()
 
2762
  if ((err= compress(body, &new_size,
 
2763
                     (const Bytef*)res->ptr(), res->length())) != Z_OK)
 
2764
  {
 
2765
    code= err==Z_MEM_ERROR ? ER_ZLIB_Z_MEM_ERROR : ER_ZLIB_Z_BUF_ERROR;
 
2766
    push_warning(current_thd,MYSQL_ERROR::WARN_LEVEL_ERROR,code,ER(code));
 
2767
    null_value= 1;
 
2768
    return 0;
 
2769
  }
 
2770
 
 
2771
  tmp= (char*)buffer.ptr(); // int4store is a macro; avoid side effects
 
2772
  int4store(tmp, res->length() & 0x3FFFFFFF);
 
2773
 
 
2774
  /* This is to ensure that things works for CHAR fields, which trim ' ': */
 
2775
  last_char= ((char*)body)+new_size-1;
 
2776
  if (*last_char == ' ')
 
2777
  {
 
2778
    *++last_char= '.';
 
2779
    new_size++;
 
2780
  }
 
2781
 
 
2782
  buffer.length((uint32)new_size + 4);
 
2783
  return &buffer;
 
2784
}
 
2785
 
 
2786
 
 
2787
String *Item_func_uncompress::val_str(String *str)
 
2788
{
 
2789
  assert(fixed == 1);
 
2790
  String *res= args[0]->val_str(str);
 
2791
  ulong new_size;
 
2792
  int err;
 
2793
  uint code;
 
2794
 
 
2795
  if (!res)
 
2796
    goto err;
 
2797
  null_value= 0;
 
2798
  if (res->is_empty())
 
2799
    return res;
 
2800
 
 
2801
  /* If length is less than 4 bytes, data is corrupt */
 
2802
  if (res->length() <= 4)
 
2803
  {
 
2804
    push_warning_printf(current_thd,MYSQL_ERROR::WARN_LEVEL_ERROR,
 
2805
                        ER_ZLIB_Z_DATA_ERROR,
 
2806
                        ER(ER_ZLIB_Z_DATA_ERROR));
 
2807
    goto err;
 
2808
  }
 
2809
 
 
2810
  /* Size of uncompressed data is stored as first 4 bytes of field */
 
2811
  new_size= uint4korr(res->ptr()) & 0x3FFFFFFF;
 
2812
  if (new_size > current_thd->variables.max_allowed_packet)
 
2813
  {
 
2814
    push_warning_printf(current_thd,MYSQL_ERROR::WARN_LEVEL_ERROR,
 
2815
                        ER_TOO_BIG_FOR_UNCOMPRESS,
 
2816
                        ER(ER_TOO_BIG_FOR_UNCOMPRESS),
 
2817
                        current_thd->variables.max_allowed_packet);
 
2818
    goto err;
 
2819
  }
 
2820
  if (buffer.realloc((uint32)new_size))
 
2821
    goto err;
 
2822
 
 
2823
  if ((err= uncompress((Byte*)buffer.ptr(), &new_size,
 
2824
                       ((const Bytef*)res->ptr())+4,res->length())) == Z_OK)
 
2825
  {
 
2826
    buffer.length((uint32) new_size);
 
2827
    return &buffer;
 
2828
  }
 
2829
 
 
2830
  code= ((err == Z_BUF_ERROR) ? ER_ZLIB_Z_BUF_ERROR :
 
2831
         ((err == Z_MEM_ERROR) ? ER_ZLIB_Z_MEM_ERROR : ER_ZLIB_Z_DATA_ERROR));
 
2832
  push_warning(current_thd,MYSQL_ERROR::WARN_LEVEL_ERROR,code,ER(code));
 
2833
 
 
2834
err:
 
2835
  null_value= 1;
 
2836
  return 0;
 
2837
}
 
2838
#endif
 
2839
 
2500
2840
/*
2501
2841
  UUID, as in
2502
2842
    DCE 1.1: Remote Procedure Call,
2506
2846
*/
2507
2847
 
2508
2848
static struct rand_struct uuid_rand;
2509
 
static uint32_t nanoseq;
 
2849
static uint nanoseq;
2510
2850
static uint64_t uuid_time=0;
2511
2851
static char clock_seq_and_node_str[]="-0000-000000000000";
2512
2852
 
2519
2859
#define UUID_VERSION      0x1000
2520
2860
#define UUID_VARIANT      0x8000
2521
2861
 
2522
 
static void tohex(char *to, uint32_t from, uint32_t len)
 
2862
static void tohex(char *to, uint from, uint len)
2523
2863
{
2524
2864
  to+= len;
2525
2865
  while (len--)
2531
2871
 
2532
2872
static void set_clock_seq_str()
2533
2873
{
2534
 
  uint16_t clock_seq= ((uint)(my_rnd(&uuid_rand)*16383)) | UUID_VARIANT;
 
2874
  uint16 clock_seq= ((uint)(my_rnd(&uuid_rand)*16383)) | UUID_VARIANT;
2535
2875
  tohex(clock_seq_and_node_str+1, clock_seq, 4);
2536
2876
  nanoseq= 0;
2537
2877
}
2545
2885
  pthread_mutex_lock(&LOCK_uuid_generator);
2546
2886
  if (! uuid_time) /* first UUID() call. initializing data */
2547
2887
  {
2548
 
    ulong tmp= sql_rnd();
2549
 
    unsigned char mac[6];
 
2888
    ulong tmp=sql_rnd_with_mutex();
 
2889
    uchar mac[6];
2550
2890
    int i;
2551
2891
    if (my_gethwaddr(mac))
2552
2892
    {
2559
2899
      */
2560
2900
      randominit(&uuid_rand, tmp + (ulong) thd, tmp + (ulong)global_query_id);
2561
2901
      for (i=0; i < (int)sizeof(mac); i++)
2562
 
        mac[i]=(unsigned char)(my_rnd(&uuid_rand)*255);
 
2902
        mac[i]=(uchar)(my_rnd(&uuid_rand)*255);
2563
2903
      /* purecov: end */    
2564
2904
    }
2565
2905
    s=clock_seq_and_node_str+sizeof(clock_seq_and_node_str)-1;
2594
2934
  uuid_time=tv;
2595
2935
  pthread_mutex_unlock(&LOCK_uuid_generator);
2596
2936
 
2597
 
  uint32_t time_low=            (uint32_t) (tv & 0xFFFFFFFF);
2598
 
  uint16_t time_mid=            (uint16_t) ((tv >> 32) & 0xFFFF);
2599
 
  uint16_t time_hi_and_version= (uint16_t) ((tv >> 48) | UUID_VERSION);
 
2937
  uint32 time_low=            (uint32) (tv & 0xFFFFFFFF);
 
2938
  uint16 time_mid=            (uint16) ((tv >> 32) & 0xFFFF);
 
2939
  uint16 time_hi_and_version= (uint16) ((tv >> 48) | UUID_VERSION);
2600
2940
 
2601
2941
  str->realloc(UUID_LENGTH+1);
2602
2942
  str->length(UUID_LENGTH);
2606
2946
  tohex(s, time_low, 8);
2607
2947
  tohex(s+9, time_mid, 4);
2608
2948
  tohex(s+14, time_hi_and_version, 4);
2609
 
  my_stpcpy(s+18, clock_seq_and_node_str);
 
2949
  strmov(s+18, clock_seq_and_node_str);
2610
2950
  return str;
2611
2951
}