~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item_strfunc.cc

  • Committer: Monty Taylor
  • Date: 2008-10-16 06:32:30 UTC
  • mto: (511.1.5 codestyle)
  • mto: This revision was merged to the branch mainline in revision 521.
  • Revision ID: monty@inaugust.com-20081016063230-4brxsra0qsmsg84q
Added -Wunused-macros.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
    (This shouldn't be needed)
26
26
*/
27
27
 
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"
 
28
#include <drizzled/server_includes.h>
 
29
#include <mysys/sha1.h>
38
30
#include <zlib.h>
39
 
C_MODE_START
40
 
#include "../mysys/my_static.h"                 // For soundex_map
41
 
C_MODE_END
 
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>
42
43
 
43
44
String my_empty_string("",default_charset_info);
44
45
 
52
53
    In Item_str_func::check_well_formed_result() we may set null_value
53
54
    flag on the same condition as in test() below.
54
55
  */
55
 
  maybe_null= (maybe_null ||
56
 
               test(thd->variables.sql_mode &
57
 
                    (MODE_STRICT_TRANS_TABLES | MODE_STRICT_ALL_TABLES)));
 
56
  maybe_null= (maybe_null || true);
58
57
  return res;
59
58
}
60
59
 
98
97
          (int64_t) 0);
99
98
}
100
99
 
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
 
 
148
100
/**
149
101
  Concatenate args with the following premises:
150
102
  If only one arg (which is ok), return value of arg;
155
107
{
156
108
  assert(fixed == 1);
157
109
  String *res,*res2,*use_as_buff;
158
 
  uint i;
 
110
  uint32_t i;
159
111
  bool is_const= 0;
160
112
 
161
113
  null_value=0;
180
132
      if (res->length()+res2->length() >
181
133
          current_thd->variables.max_allowed_packet)
182
134
      {
183
 
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
135
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
184
136
                            ER_WARN_ALLOWED_PACKET_OVERFLOWED,
185
137
                            ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
186
138
                            current_thd->variables.max_allowed_packet);
223
175
          now work in place in tmp_value to set it to res | res2
224
176
        */
225
177
        /* Chop the last characters in tmp_value that isn't in res2 */
226
 
        tmp_value.length((uint32) (res2->ptr() - tmp_value.ptr()) +
 
178
        tmp_value.length((uint32_t) (res2->ptr() - tmp_value.ptr()) +
227
179
                         res2->length());
228
180
        /* Place res2 at start of tmp_value, remove chars before res2 */
229
 
        if (tmp_value.replace(0,(uint32) (res2->ptr() - tmp_value.ptr()),
 
181
        if (tmp_value.replace(0,(uint32_t) (res2->ptr() - tmp_value.ptr()),
230
182
                              *res))
231
183
          goto null;
232
184
        res= &tmp_value;
242
194
          more than 25% of memory will be overcommitted on average.
243
195
        */
244
196
 
245
 
        uint concat_len= res->length() + res2->length();
 
197
        uint32_t concat_len= res->length() + res2->length();
246
198
 
247
199
        if (tmp_value.alloced_length() < concat_len)
248
200
        {
253
205
          }
254
206
          else
255
207
          {
256
 
            uint new_len = max(tmp_value.alloced_length() * 2, concat_len);
 
208
            uint32_t new_len = cmax(tmp_value.alloced_length() * 2, concat_len);
257
209
 
258
210
            if (tmp_value.realloc(new_len))
259
211
              goto null;
285
237
  if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1))
286
238
    return;
287
239
 
288
 
  for (uint i=0 ; i < arg_count ; i++)
 
240
  for (uint32_t i=0 ; i < arg_count ; i++)
289
241
  {
290
242
    if (args[i]->collation.collation->mbmaxlen != collation.collation->mbmaxlen)
291
243
      max_result_length+= (args[i]->max_length /
315
267
  char tmp_str_buff[10];
316
268
  String tmp_sep_str(tmp_str_buff, sizeof(tmp_str_buff),default_charset_info),
317
269
         *sep_str, *res, *res2,*use_as_buff;
318
 
  uint i;
 
270
  uint32_t i;
319
271
 
320
272
  null_value=0;
321
273
  if (!(sep_str= args[0]->val_str(&tmp_sep_str)))
341
293
    if (res->length() + sep_str->length() + res2->length() >
342
294
        current_thd->variables.max_allowed_packet)
343
295
    {
344
 
      push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
296
      push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
345
297
                          ER_WARN_ALLOWED_PACKET_OVERFLOWED,
346
298
                          ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
347
299
                          current_thd->variables.max_allowed_packet);
392
344
        now work in place in tmp_value to set it to res | sep_str | res2
393
345
      */
394
346
      /* Chop the last characters in tmp_value that isn't in res2 */
395
 
      tmp_value.length((uint32) (res2->ptr() - tmp_value.ptr()) +
 
347
      tmp_value.length((uint32_t) (res2->ptr() - tmp_value.ptr()) +
396
348
                       res2->length());
397
349
      /* Place res2 at start of tmp_value, remove chars before res2 */
398
 
      if (tmp_value.replace(0,(uint32) (res2->ptr() - tmp_value.ptr()),
 
350
      if (tmp_value.replace(0,(uint32_t) (res2->ptr() - tmp_value.ptr()),
399
351
                            *res) ||
400
352
          tmp_value.replace(res->length(),0, *sep_str))
401
353
        goto null;
412
364
        25% of memory will be overcommitted on average.
413
365
      */
414
366
 
415
 
      uint concat_len= res->length() + sep_str->length() + res2->length();
 
367
      uint32_t concat_len= res->length() + sep_str->length() + res2->length();
416
368
 
417
369
      if (tmp_value.alloced_length() < concat_len)
418
370
      {
423
375
        }
424
376
        else
425
377
        {
426
 
          uint new_len = max(tmp_value.alloced_length() * 2, concat_len);
 
378
          uint32_t new_len = cmax(tmp_value.alloced_length() * 2, concat_len);
427
379
 
428
380
          if (tmp_value.realloc(new_len))
429
381
            goto null;
460
412
     so, (arg_count - 2) is safe here.
461
413
  */
462
414
  max_result_length= (uint64_t) args[0]->max_length * (arg_count - 2);
463
 
  for (uint i=1 ; i < arg_count ; i++)
 
415
  for (uint32_t i=1 ; i < arg_count ; i++)
464
416
    max_result_length+=args[i]->max_length;
465
417
 
466
418
  if (max_result_length >= MAX_BLOB_WIDTH)
497
449
#ifdef USE_MB
498
450
  if (use_mb(res->charset()))
499
451
  {
500
 
    register uint32 l;
 
452
    register uint32_t l;
501
453
    while (ptr < end)
502
454
    {
503
455
      if ((l= my_ismbchar(res->charset(),ptr,end)))
540
492
  assert(fixed == 1);
541
493
  String *res,*res2,*res3;
542
494
  int offset;
543
 
  uint from_length,to_length;
 
495
  uint32_t from_length,to_length;
544
496
  bool alloced=0;
545
497
#ifdef USE_MB
546
498
  const char *ptr,*end,*strend,*search,*search_end;
547
 
  register uint32 l;
 
499
  register uint32_t l;
548
500
  bool binary_cmp;
549
501
#endif
550
502
 
598
550
          if (res->length()-from_length + to_length >
599
551
              current_thd->variables.max_allowed_packet)
600
552
          {
601
 
            push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
553
            push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
602
554
                                ER_WARN_ALLOWED_PACKET_OVERFLOWED,
603
555
                                ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
604
556
                                func_name(),
627
579
      if (res->length()-from_length + to_length >
628
580
          current_thd->variables.max_allowed_packet)
629
581
      {
630
 
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
582
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
631
583
                            ER_WARN_ALLOWED_PACKET_OVERFLOWED,
632
584
                            ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
633
585
                            current_thd->variables.max_allowed_packet);
694
646
 
695
647
  /* start and length are now sufficiently valid to pass to charpos function */
696
648
   start= res->charpos((int) start);
697
 
   length= res->charpos((int) length, (uint32) start);
 
649
   length= res->charpos((int) length, (uint32_t) start);
698
650
 
699
651
  /* Re-testing with corrected params */
700
652
  if (start > res->length())
705
657
  if ((uint64_t) (res->length() - length + res2->length()) >
706
658
      (uint64_t) current_thd->variables.max_allowed_packet)
707
659
  {
708
 
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
660
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
709
661
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
710
662
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
711
663
                        func_name(), current_thd->variables.max_allowed_packet);
712
664
    goto null;
713
665
  }
714
666
  res=copy_if_not_alloced(str,res,res->length());
715
 
  res->replace((uint32) start,(uint32) length,*res2);
 
667
  res->replace((uint32_t) start,(uint32_t) length,*res2);
716
668
  return res;
717
669
null:
718
670
  null_value=1;
750
702
  null_value=0;
751
703
  if (multiply == 1)
752
704
  {
753
 
    uint len;
 
705
    uint32_t len;
754
706
    res= copy_if_not_alloced(str,res,res->length());
755
707
    len= converter(collation.collation, (char*) res->ptr(), res->length(),
756
708
                                        (char*) res->ptr(), res->length());
759
711
  }
760
712
  else
761
713
  {
762
 
    uint len= res->length() * multiply;
 
714
    uint32_t len= res->length() * multiply;
763
715
    tmp_value.alloc(len);
764
716
    tmp_value.set_charset(collation.collation);
765
717
    len= converter(collation.collation, (char*) res->ptr(), res->length(),
795
747
 
796
748
  /* must be int64_t to avoid truncation */
797
749
  int64_t length= args[1]->val_int();
798
 
  uint char_pos;
 
750
  uint32_t char_pos;
799
751
 
800
752
  if ((null_value=(args[0]->null_value || args[1]->null_value)))
801
753
    return 0;
851
803
  if (res->length() <= (uint64_t) length)
852
804
    return res; /* purecov: inspected */
853
805
 
854
 
  uint start=res->numchars();
 
806
  uint32_t start=res->numchars();
855
807
  if (start <= (uint) length)
856
808
    return res;
857
809
  start=res->charpos(start - (uint) length);
903
855
  if ((start < 0) || ((uint) start + 1 > res->length()))
904
856
    return &my_empty_string;
905
857
 
906
 
  length= res->charpos((int) length, (uint32) start);
 
858
  length= res->charpos((int) length, (uint32_t) start);
907
859
  tmp_length= res->length() - start;
908
 
  length= min(length, tmp_length);
 
860
  length= cmin(length, tmp_length);
909
861
 
910
862
  if (!start && (int64_t) res->length() == length)
911
863
    return res;
912
 
  tmp_value.set(*res, (uint32) start, (uint32) length);
 
864
  tmp_value.set(*res, (uint32_t) start, (uint32_t) length);
913
865
  return &tmp_value;
914
866
}
915
867
 
921
873
  collation.set(args[0]->collation);
922
874
  if (args[1]->const_item())
923
875
  {
924
 
    int32 start= (int32) args[1]->val_int();
 
876
    int32_t start= (int32_t) args[1]->val_int();
925
877
    if (start < 0)
926
878
      max_length= ((uint)(-start) > max_length) ? 0 : (uint)(-start);
927
879
    else
928
 
      max_length-= min((uint)(start - 1), max_length);
 
880
      max_length-= cmin((uint)(start - 1), max_length);
929
881
  }
930
882
  if (arg_count == 3 && args[2]->const_item())
931
883
  {
932
 
    int32 length= (int32) args[2]->val_int();
 
884
    int32_t length= (int32_t) args[2]->val_int();
933
885
    if (length <= 0)
934
886
      max_length=0; /* purecov: inspected */
935
887
    else
953
905
  assert(fixed == 1);
954
906
  String *res= args[0]->val_str(str);
955
907
  String *delimiter= args[1]->val_str(&tmp_value);
956
 
  int32 count= (int32) args[2]->val_int();
957
 
  uint offset;
 
908
  int32_t count= (int32_t) args[2]->val_int();
 
909
  uint32_t offset;
958
910
 
959
911
  if (args[0]->null_value || args[1]->null_value || args[2]->null_value)
960
912
  {                                     // string and/or delim are null
962
914
    return 0;
963
915
  }
964
916
  null_value=0;
965
 
  uint delimiter_length= delimiter->length();
 
917
  uint32_t delimiter_length= delimiter->length();
966
918
  if (!res->length() || !delimiter_length || !count)
967
919
    return &my_empty_string;            // Wrong parameters
968
920
 
976
928
    const char *end= strend-delimiter_length+1;
977
929
    const char *search= delimiter->ptr();
978
930
    const char *search_end= search+delimiter_length;
979
 
    int32 n=0,c=count,pass;
980
 
    register uint32 l;
 
931
    int32_t n=0,c=count,pass;
 
932
    register uint32_t l;
981
933
    for (pass=(count>0);pass<2;++pass)
982
934
    {
983
935
      while (ptr < end)
1084
1036
  char buff[MAX_FIELD_WIDTH], *ptr, *end;
1085
1037
  String tmp(buff,sizeof(buff),system_charset_info);
1086
1038
  String *res, *remove_str;
1087
 
  uint remove_length;
 
1039
  uint32_t remove_length;
1088
1040
 
1089
1041
  res= args[0]->val_str(str);
1090
1042
  if ((null_value=args[0]->null_value))
1130
1082
  char buff[MAX_FIELD_WIDTH], *ptr, *end;
1131
1083
  String tmp(buff, sizeof(buff), system_charset_info);
1132
1084
  String *res, *remove_str;
1133
 
  uint remove_length;
 
1085
  uint32_t remove_length;
1134
1086
 
1135
1087
  res= args[0]->val_str(str);
1136
1088
  if ((null_value=args[0]->null_value))
1151
1103
  end= ptr+res->length();
1152
1104
#ifdef USE_MB
1153
1105
  char *p=ptr;
1154
 
  register uint32 l;
 
1106
  register uint32_t l;
1155
1107
#endif
1156
1108
  if (remove_length == 1)
1157
1109
  {
1211
1163
  const char *r_ptr;
1212
1164
  String tmp(buff, sizeof(buff), system_charset_info);
1213
1165
  String *res, *remove_str;
1214
 
  uint remove_length;
 
1166
  uint32_t remove_length;
1215
1167
 
1216
1168
  res= args[0]->val_str(str);
1217
1169
  if ((null_value=args[0]->null_value))
1237
1189
  if (use_mb(res->charset()))
1238
1190
  {
1239
1191
    char *p=ptr;
1240
 
    register uint32 l;
 
1192
    register uint32_t l;
1241
1193
 loop:
1242
1194
    while (ptr + remove_length < end)
1243
1195
    {
1301
1253
}
1302
1254
 
1303
1255
 
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)
 
1256
Item *Item_func_sysconst::safe_charset_converter(const CHARSET_INFO * const tocs)
1440
1257
{
1441
1258
  Item_string *conv;
1442
 
  uint conv_errors;
 
1259
  uint32_t conv_errors;
1443
1260
  String tmp, cstr, *ostr= val_str(&tmp);
1444
1261
  cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
1445
1262
  if (conv_errors ||
1482
1299
  // For system threads (e.g. replication SQL thread) user may be empty
1483
1300
  if (user)
1484
1301
  {
1485
 
    CHARSET_INFO *cs= str_value.charset();
1486
 
    uint res_length= (strlen(user)+strlen(host)+2) * cs->mbmaxlen;
 
1302
    const CHARSET_INFO * const cs= str_value.charset();
 
1303
    uint32_t res_length= (strlen(user)+strlen(host)+2) * cs->mbmaxlen;
1487
1304
 
1488
1305
    if (str_value.alloc(res_length))
1489
1306
    {
1504
1321
{
1505
1322
  return (Item_func_sysconst::fix_fields(thd, ref) ||
1506
1323
          init(thd->main_security_ctx.user,
1507
 
               thd->main_security_ctx.host_or_ip));
 
1324
               thd->main_security_ctx.ip));
1508
1325
}
1509
1326
 
1510
1327
 
1515
1332
 
1516
1333
  Security_context *ctx=
1517
1334
                         thd->security_ctx;
1518
 
  return init(ctx->priv_user, ctx->priv_host);
 
1335
  return init(ctx->user, ctx->ip);
1519
1336
}
1520
1337
 
1521
1338
 
1535
1352
void Item_func_format::fix_length_and_dec()
1536
1353
{
1537
1354
  collation.set(default_charset());
1538
 
  uint char_length= args[0]->max_length/args[0]->collation.collation->mbmaxlen;
 
1355
  uint32_t char_length= args[0]->max_length/args[0]->collation.collation->mbmaxlen;
1539
1356
  max_length= ((char_length + (char_length-args[0]->decimals)/3) *
1540
1357
               collation.collation->mbmaxlen);
1541
1358
}
1549
1366
 
1550
1367
String *Item_func_format::val_str(String *str)
1551
1368
{
1552
 
  uint32 length;
1553
 
  uint32 str_length;
 
1369
  uint32_t length;
 
1370
  uint32_t str_length;
1554
1371
  /* Number of decimal digits */
1555
1372
  int dec;
1556
1373
  /* Number of characters used to represent the decimals, including '.' */
1557
 
  uint32 dec_length;
 
1374
  uint32_t dec_length;
1558
1375
  int diff;
1559
1376
  assert(fixed == 1);
1560
1377
 
1590
1407
    nr= my_double_round(nr, (int64_t) dec, false, false);
1591
1408
    /* Here default_charset() is right as this is not an automatic conversion */
1592
1409
    str->set_real(nr, dec, default_charset());
1593
 
    if (isnan(nr))
 
1410
    if (std::isnan(nr))
1594
1411
      return str;
1595
1412
    str_length=str->length();
1596
1413
    if (nr < 0)
1640
1457
  if (agg_arg_charsets(collation, args+1, arg_count-1, MY_COLL_ALLOW_CONV, 1))
1641
1458
    return;
1642
1459
 
1643
 
  for (uint i= 1 ; i < arg_count ; i++)
 
1460
  for (uint32_t i= 1 ; i < arg_count ; i++)
1644
1461
  {
1645
1462
    set_if_bigger(max_length,args[i]->max_length);
1646
1463
    set_if_bigger(decimals,args[i]->decimals);
1652
1469
double Item_func_elt::val_real()
1653
1470
{
1654
1471
  assert(fixed == 1);
1655
 
  uint tmp;
 
1472
  uint32_t tmp;
1656
1473
  null_value=1;
1657
1474
  if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
1658
1475
    return 0.0;
1665
1482
int64_t Item_func_elt::val_int()
1666
1483
{
1667
1484
  assert(fixed == 1);
1668
 
  uint tmp;
 
1485
  uint32_t tmp;
1669
1486
  null_value=1;
1670
1487
  if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
1671
1488
    return 0;
1679
1496
String *Item_func_elt::val_str(String *str)
1680
1497
{
1681
1498
  assert(fixed == 1);
1682
 
  uint tmp;
 
1499
  uint32_t tmp;
1683
1500
  null_value=1;
1684
1501
  if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
1685
1502
    return NULL;
1707
1524
  if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1))
1708
1525
    return;
1709
1526
  
1710
 
  for (uint i=0 ; i < arg_count ; i++)
 
1527
  for (uint32_t i=0 ; i < arg_count ; i++)
1711
1528
    max_length+=args[i]->max_length;
1712
1529
 
1713
1530
  used_tables_cache|=     item->used_tables();
1779
1596
}
1780
1597
 
1781
1598
 
1782
 
Item *Item_func_make_set::transform(Item_transformer transformer, uchar *arg)
 
1599
Item *Item_func_make_set::transform(Item_transformer transformer, unsigned char *arg)
1783
1600
{
1784
1601
  Item *new_item= item->transform(transformer, arg);
1785
1602
  if (!new_item)
1815
1632
  assert(fixed == 1);
1816
1633
  str->length(0);
1817
1634
  str->set_charset(collation.collation);
1818
 
  for (uint i=0 ; i < arg_count ; i++)
 
1635
  for (uint32_t i=0 ; i < arg_count ; i++)
1819
1636
  {
1820
 
    int32 num=(int32) args[i]->val_int();
 
1637
    int32_t num=(int32_t) args[i]->val_int();
1821
1638
    if (!args[i]->null_value)
1822
1639
    {
1823
1640
      char char_num= (char) num;
1896
1713
String *Item_func_repeat::val_str(String *str)
1897
1714
{
1898
1715
  assert(fixed == 1);
1899
 
  uint length,tot_length;
 
1716
  uint32_t length,tot_length;
1900
1717
  char *to;
1901
1718
  /* must be int64_t to avoid truncation */
1902
1719
  int64_t count= args[1]->val_int();
1919
1736
  // Safe length check
1920
1737
  if (length > current_thd->variables.max_allowed_packet / (uint) count)
1921
1738
  {
1922
 
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
1739
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1923
1740
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
1924
1741
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
1925
1742
                        func_name(), current_thd->variables.max_allowed_packet);
1982
1799
String *Item_func_rpad::val_str(String *str)
1983
1800
{
1984
1801
  assert(fixed == 1);
1985
 
  uint32 res_byte_length,res_char_length,pad_char_length,pad_byte_length;
 
1802
  uint32_t res_byte_length,res_char_length,pad_char_length,pad_byte_length;
1986
1803
  char *to;
1987
1804
  const char *ptr_pad;
1988
1805
  /* must be int64_t to avoid truncation */
2009
1826
  byte_count= count * collation.collation->mbmaxlen;
2010
1827
  if ((uint64_t) byte_count > current_thd->variables.max_allowed_packet)
2011
1828
  {
2012
 
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
1829
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2013
1830
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
2014
1831
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
2015
1832
                        func_name(), current_thd->variables.max_allowed_packet);
2025
1842
  ptr_pad=rpad->ptr();
2026
1843
  pad_byte_length= rpad->length();
2027
1844
  count-= res_char_length;
2028
 
  for ( ; (uint32) count > pad_char_length; count-= pad_char_length)
 
1845
  for ( ; (uint32_t) count > pad_char_length; count-= pad_char_length)
2029
1846
  {
2030
1847
    memcpy(to,ptr_pad,pad_byte_length);
2031
1848
    to+= pad_byte_length;
2085
1902
String *Item_func_lpad::val_str(String *str)
2086
1903
{
2087
1904
  assert(fixed == 1);
2088
 
  uint32 res_char_length,pad_char_length;
 
1905
  uint32_t res_char_length,pad_char_length;
2089
1906
  /* must be int64_t to avoid truncation */
2090
1907
  int64_t count= args[1]->val_int();
2091
1908
  int64_t byte_count;
2114
1931
  
2115
1932
  if ((uint64_t) byte_count > current_thd->variables.max_allowed_packet)
2116
1933
  {
2117
 
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
1934
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2118
1935
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
2119
1936
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
2120
1937
                        func_name(), current_thd->variables.max_allowed_packet);
2122
1939
  }
2123
1940
 
2124
1941
  if (args[2]->null_value || !pad_char_length ||
2125
 
      str->alloc((uint32) byte_count))
 
1942
      str->alloc((uint32_t) byte_count))
2126
1943
    goto err;
2127
1944
  
2128
1945
  str->length(0);
2174
1991
                                 from_base, &endptr, &err);
2175
1992
 
2176
1993
  ptr= int64_t2str(dec, ans, to_base);
2177
 
  if (str->copy(ans, (uint32) (ptr-ans), default_charset()))
 
1994
  if (str->copy(ans, (uint32_t) (ptr-ans), default_charset()))
2178
1995
    return &my_empty_string;
2179
1996
  return str;
2180
1997
}
2186
2003
  if (use_cached_value)
2187
2004
    return null_value ? 0 : &str_value;
2188
2005
  String *arg= args[0]->val_str(str);
2189
 
  uint dummy_errors;
 
2006
  uint32_t dummy_errors;
2190
2007
  if (!arg)
2191
2008
  {
2192
2009
    null_value=1;
2224
2041
 
2225
2042
void Item_func_set_collation::fix_length_and_dec()
2226
2043
{
2227
 
  CHARSET_INFO *set_collation;
 
2044
  const CHARSET_INFO *set_collation;
2228
2045
  const char *colname;
2229
2046
  String tmp, *str= args[1]->val_str(&tmp);
2230
2047
  colname= str->c_ptr();
2267
2084
  Item_func_set_collation *item_func_sc=(Item_func_set_collation*) item;
2268
2085
  if (collation.collation != item_func_sc->collation.collation)
2269
2086
    return 0;
2270
 
  for (uint i=0; i < arg_count ; i++)
 
2087
  for (uint32_t i=0; i < arg_count ; i++)
2271
2088
    if (!args[i]->eq(item_func_sc->args[i], binary_cmp))
2272
2089
      return 0;
2273
2090
  return 1;
2288
2105
String *Item_func_charset::val_str(String *str)
2289
2106
{
2290
2107
  assert(fixed == 1);
2291
 
  uint dummy_errors;
 
2108
  uint32_t dummy_errors;
2292
2109
 
2293
 
  CHARSET_INFO *cs= args[0]->collation.collation; 
 
2110
  const CHARSET_INFO * const cs= args[0]->collation.collation; 
2294
2111
  null_value= 0;
2295
2112
  str->copy(cs->csname, strlen(cs->csname),
2296
 
            &my_charset_latin1, collation.collation, &dummy_errors);
 
2113
            &my_charset_utf8_general_ci, collation.collation, &dummy_errors);
2297
2114
  return str;
2298
2115
}
2299
2116
 
2300
2117
String *Item_func_collation::val_str(String *str)
2301
2118
{
2302
2119
  assert(fixed == 1);
2303
 
  uint dummy_errors;
2304
 
  CHARSET_INFO *cs= args[0]->collation.collation; 
 
2120
  uint32_t dummy_errors;
 
2121
  const CHARSET_INFO * const cs= args[0]->collation.collation; 
2305
2122
 
2306
2123
  null_value= 0;
2307
2124
  str->copy(cs->name, strlen(cs->name),
2308
 
            &my_charset_latin1, collation.collation, &dummy_errors);
 
2125
            &my_charset_utf8_general_ci, collation.collation, &dummy_errors);
2309
2126
  return str;
2310
2127
}
2311
2128
 
2312
2129
 
2313
2130
void Item_func_weight_string::fix_length_and_dec()
2314
2131
{
2315
 
  CHARSET_INFO *cs= args[0]->collation.collation;
 
2132
  const CHARSET_INFO * const cs= args[0]->collation.collation;
2316
2133
  collation.set(&my_charset_bin, args[0]->collation.derivation);
2317
2134
  flags= my_strxfrm_flag_normalize(flags, cs->levels_for_order);
2318
 
  max_length= cs->mbmaxlen * max(args[0]->max_length, nweights);
 
2135
  max_length= cs->mbmaxlen * cmax(args[0]->max_length, nweights);
2319
2136
  maybe_null= 1;
2320
2137
}
2321
2138
 
2324
2141
String *Item_func_weight_string::val_str(String *str)
2325
2142
{
2326
2143
  String *res;
2327
 
  CHARSET_INFO *cs= args[0]->collation.collation;
2328
 
  uint tmp_length, frm_length;
 
2144
  const CHARSET_INFO * const cs= args[0]->collation.collation;
 
2145
  uint32_t tmp_length, frm_length;
2329
2146
  assert(fixed == 1);
2330
2147
 
2331
2148
  if (args[0]->result_type() != STRING_RESULT ||
2333
2150
    goto nl;
2334
2151
  
2335
2152
  tmp_length= cs->coll->strnxfrmlen(cs, cs->mbmaxlen *
2336
 
                                        max(res->length(), nweights));
 
2153
                                        cmax(res->length(), nweights));
2337
2154
 
2338
2155
  if (tmp_value.alloc(tmp_length))
2339
2156
    goto nl;
2340
2157
 
2341
2158
  frm_length= cs->coll->strnxfrm(cs,
2342
 
                                 (uchar*) tmp_value.ptr(), tmp_length,
 
2159
                                 (unsigned char*) tmp_value.ptr(), tmp_length,
2343
2160
                                 nweights ? nweights : tmp_length,
2344
 
                                 (const uchar*) res->ptr(), res->length(),
 
2161
                                 (const unsigned char*) res->ptr(), res->length(),
2345
2162
                                 flags);
2346
2163
  tmp_value.length(frm_length);
2347
2164
  null_value= 0;
2378
2195
    if ((null_value= args[0]->null_value))
2379
2196
      return 0;
2380
2197
    ptr= int64_t2str(dec,ans,16);
2381
 
    if (str->copy(ans,(uint32) (ptr-ans),default_charset()))
 
2198
    if (str->copy(ans,(uint32_t) (ptr-ans),default_charset()))
2382
2199
      return &my_empty_string;                  // End of memory
2383
2200
    return str;
2384
2201
  }
2404
2221
  const char *from, *end;
2405
2222
  char *to;
2406
2223
  String *res;
2407
 
  uint length;
 
2224
  uint32_t length;
2408
2225
  assert(fixed == 1);
2409
2226
 
2410
2227
  res= args[0]->val_str(str);
2447
2264
}
2448
2265
 
2449
2266
 
2450
 
#include <my_dir.h>
2451
 
 
2452
2267
String *Item_load_file::val_str(String *str)
2453
2268
{
2454
2269
  assert(fixed == 1);
2478
2293
  }
2479
2294
  if (stat_info.st_size > (long) current_thd->variables.max_allowed_packet)
2480
2295
  {
2481
 
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
2296
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2482
2297
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
2483
2298
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
2484
2299
                        func_name(), current_thd->variables.max_allowed_packet);
2488
2303
    goto err;
2489
2304
  if ((file = my_open(file_name->c_ptr(), O_RDONLY, MYF(0))) < 0)
2490
2305
    goto err;
2491
 
  if (my_read(file, (uchar*) tmp_value.ptr(), stat_info.st_size, MYF(MY_NABP)))
 
2306
  if (my_read(file, (unsigned char*) tmp_value.ptr(), stat_info.st_size, MYF(MY_NABP)))
2492
2307
  {
2493
2308
    my_close(file, MYF(0));
2494
2309
    goto err;
2514
2329
  no = args[2]->val_str(&no_buf);
2515
2330
  String *sep = NULL, sep_buf ;
2516
2331
 
2517
 
  uint num_set_values = 64;
 
2332
  uint32_t num_set_values = 64;
2518
2333
  uint64_t mask = 0x1;
2519
2334
  str->length(0);
2520
2335
  str->set_charset(collation.collation);
2550
2365
  case 3:
2551
2366
    {
2552
2367
      /* errors is not checked - assume "," can always be converted */
2553
 
      uint errors;
 
2368
      uint32_t errors;
2554
2369
      sep_buf.copy(STRING_WITH_LEN(","), &my_charset_bin, collation.collation, &errors);
2555
2370
      sep = &sep_buf;
2556
2371
    }
2560
2375
  }
2561
2376
  null_value=0;
2562
2377
 
2563
 
  for (uint i = 0; i < num_set_values; i++, mask = (mask << 1))
 
2378
  for (uint32_t i = 0; i < num_set_values; i++, mask = (mask << 1))
2564
2379
  {
2565
2380
    if (the_set & mask)
2566
2381
      str->append(*yes);
2574
2389
 
2575
2390
void Item_func_export_set::fix_length_and_dec()
2576
2391
{
2577
 
  uint length=max(args[1]->max_length,args[2]->max_length);
2578
 
  uint sep_length=(arg_count > 3 ? args[3]->max_length : 1);
 
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);
2579
2394
  max_length=length*64+sep_length*63;
2580
2395
 
2581
 
  if (agg_arg_charsets(collation, args+1, min(4,arg_count)-1,
 
2396
  if (agg_arg_charsets(collation, args+1, cmin((uint)4,arg_count)-1,
2582
2397
                       MY_COLL_ALLOW_CONV, 1))
2583
2398
    return;
2584
2399
}
2613
2428
    0, \, ' and ^Z
2614
2429
  */
2615
2430
 
2616
 
  static uchar escmask[32]=
 
2431
  static unsigned char escmask[32]=
2617
2432
  {
2618
2433
    0x01, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00, 0x00,
2619
2434
    0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,
2623
2438
 
2624
2439
  char *from, *to, *end, *start;
2625
2440
  String *arg= args[0]->val_str(str);
2626
 
  uint arg_length, new_length;
 
2441
  uint32_t arg_length, new_length;
2627
2442
  if (!arg)                                     // Null argument
2628
2443
  {
2629
2444
    /* Return the string 'NULL' */
2636
2451
  new_length= arg_length+2; /* for beginning and ending ' signs */
2637
2452
 
2638
2453
  for (from= (char*) arg->ptr(), end= from + arg_length; from < end; from++)
2639
 
    new_length+= get_esc_bit(escmask, (uchar) *from);
 
2454
    new_length+= get_esc_bit(escmask, (unsigned char) *from);
2640
2455
 
2641
2456
  if (tmp_value.alloc(new_length))
2642
2457
    goto null;
2682
2497
  return 0;
2683
2498
}
2684
2499
 
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
 
 
2840
2500
/*
2841
2501
  UUID, as in
2842
2502
    DCE 1.1: Remote Procedure Call,
2846
2506
*/
2847
2507
 
2848
2508
static struct rand_struct uuid_rand;
2849
 
static uint nanoseq;
 
2509
static uint32_t nanoseq;
2850
2510
static uint64_t uuid_time=0;
2851
2511
static char clock_seq_and_node_str[]="-0000-000000000000";
2852
2512
 
2859
2519
#define UUID_VERSION      0x1000
2860
2520
#define UUID_VARIANT      0x8000
2861
2521
 
2862
 
static void tohex(char *to, uint from, uint len)
 
2522
static void tohex(char *to, uint32_t from, uint32_t len)
2863
2523
{
2864
2524
  to+= len;
2865
2525
  while (len--)
2871
2531
 
2872
2532
static void set_clock_seq_str()
2873
2533
{
2874
 
  uint16 clock_seq= ((uint)(my_rnd(&uuid_rand)*16383)) | UUID_VARIANT;
 
2534
  uint16_t clock_seq= ((uint)(my_rnd(&uuid_rand)*16383)) | UUID_VARIANT;
2875
2535
  tohex(clock_seq_and_node_str+1, clock_seq, 4);
2876
2536
  nanoseq= 0;
2877
2537
}
2885
2545
  pthread_mutex_lock(&LOCK_uuid_generator);
2886
2546
  if (! uuid_time) /* first UUID() call. initializing data */
2887
2547
  {
2888
 
    ulong tmp=sql_rnd_with_mutex();
2889
 
    uchar mac[6];
 
2548
    ulong tmp= sql_rnd();
 
2549
    unsigned char mac[6];
2890
2550
    int i;
2891
2551
    if (my_gethwaddr(mac))
2892
2552
    {
2899
2559
      */
2900
2560
      randominit(&uuid_rand, tmp + (ulong) thd, tmp + (ulong)global_query_id);
2901
2561
      for (i=0; i < (int)sizeof(mac); i++)
2902
 
        mac[i]=(uchar)(my_rnd(&uuid_rand)*255);
 
2562
        mac[i]=(unsigned char)(my_rnd(&uuid_rand)*255);
2903
2563
      /* purecov: end */    
2904
2564
    }
2905
2565
    s=clock_seq_and_node_str+sizeof(clock_seq_and_node_str)-1;
2934
2594
  uuid_time=tv;
2935
2595
  pthread_mutex_unlock(&LOCK_uuid_generator);
2936
2596
 
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);
 
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);
2940
2600
 
2941
2601
  str->realloc(UUID_LENGTH+1);
2942
2602
  str->length(UUID_LENGTH);
2946
2606
  tohex(s, time_low, 8);
2947
2607
  tohex(s+9, time_mid, 4);
2948
2608
  tohex(s+14, time_hi_and_version, 4);
2949
 
  strmov(s+18, clock_seq_and_node_str);
 
2609
  my_stpcpy(s+18, clock_seq_and_node_str);
2950
2610
  return str;
2951
2611
}