~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/item_strfunc.cc

  • Committer: Monty Taylor
  • Date: 2008-07-01 14:33:36 UTC
  • mto: (28.1.12 backport_patch)
  • mto: This revision was merged to the branch mainline in revision 34.
  • Revision ID: monty@inaugust.com-20080701143336-8uihm7dhpu92rt0q
Somehow missed moving password.c. Duh.

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
 
60
61
 
61
62
my_decimal *Item_str_func::val_decimal(my_decimal *decimal_value)
62
63
{
63
 
  assert(fixed == 1);
 
64
  DBUG_ASSERT(fixed == 1);
64
65
  char buff[64];
65
66
  String *res, tmp(buff,sizeof(buff), &my_charset_bin);
66
67
  res= val_str(&tmp);
74
75
 
75
76
double Item_str_func::val_real()
76
77
{
77
 
  assert(fixed == 1);
 
78
  DBUG_ASSERT(fixed == 1);
78
79
  int err_not_used;
79
80
  char *end_not_used, buff[64];
80
81
  String *res, tmp(buff,sizeof(buff), &my_charset_bin);
84
85
}
85
86
 
86
87
 
87
 
int64_t Item_str_func::val_int()
 
88
longlong Item_str_func::val_int()
88
89
{
89
 
  assert(fixed == 1);
 
90
  DBUG_ASSERT(fixed == 1);
90
91
  int err;
91
92
  char buff[22];
92
93
  String *res, tmp(buff,sizeof(buff), &my_charset_bin);
94
95
  return (res ?
95
96
          my_strntoll(res->charset(), res->ptr(), res->length(), 10, NULL,
96
97
                      &err) :
97
 
          (int64_t) 0);
 
98
          (longlong) 0);
 
99
}
 
100
 
 
101
 
 
102
String *Item_func_md5::val_str(String *str)
 
103
{
 
104
  DBUG_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);
98
146
}
99
147
 
100
148
/**
105
153
 
106
154
String *Item_func_concat::val_str(String *str)
107
155
{
108
 
  assert(fixed == 1);
 
156
  DBUG_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;
232
280
 
233
281
void Item_func_concat::fix_length_and_dec()
234
282
{
235
 
  uint64_t max_result_length= 0;
 
283
  ulonglong max_result_length= 0;
236
284
 
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 /
263
311
 
264
312
String *Item_func_concat_ws::val_str(String *str)
265
313
{
266
 
  assert(fixed == 1);
 
314
  DBUG_ASSERT(fixed == 1);
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;
401
449
 
402
450
void Item_func_concat_ws::fix_length_and_dec()
403
451
{
404
 
  uint64_t max_result_length;
 
452
  ulonglong max_result_length;
405
453
 
406
454
  if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1))
407
455
    return;
411
459
     it is done on parser level in sql_yacc.yy
412
460
     so, (arg_count - 2) is safe here.
413
461
  */
414
 
  max_result_length= (uint64_t) args[0]->max_length * (arg_count - 2);
415
 
  for (uint32_t i=1 ; i < arg_count ; i++)
 
462
  max_result_length= (ulonglong) args[0]->max_length * (arg_count - 2);
 
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)
426
474
 
427
475
String *Item_func_reverse::val_str(String *str)
428
476
{
429
 
  assert(fixed == 1);
 
477
  DBUG_ASSERT(fixed == 1);
430
478
  String *res = args[0]->val_str(str);
431
479
  char *ptr, *end, *tmp;
432
480
 
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)))
489
537
 
490
538
String *Item_func_replace::val_str(String *str)
491
539
{
492
 
  assert(fixed == 1);
 
540
  DBUG_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);
604
652
 
605
653
void Item_func_replace::fix_length_and_dec()
606
654
{
607
 
  uint64_t max_result_length= args[0]->max_length;
 
655
  ulonglong max_result_length= args[0]->max_length;
608
656
  int diff=(int) (args[2]->max_length - args[1]->max_length);
609
657
  if (diff > 0 && args[1]->max_length)
610
658
  {                                             // Calculate of maxreplaces
611
 
    uint64_t max_substrs= max_result_length/args[1]->max_length;
 
659
    ulonglong max_substrs= max_result_length/args[1]->max_length;
612
660
    max_result_length+= max_substrs * (uint) diff;
613
661
  }
614
662
  if (max_result_length >= MAX_BLOB_WIDTH)
625
673
 
626
674
String *Item_func_insert::val_str(String *str)
627
675
{
628
 
  assert(fixed == 1);
 
676
  DBUG_ASSERT(fixed == 1);
629
677
  String *res,*res2;
630
 
  int64_t start, length;  /* must be int64_t to avoid truncation */
 
678
  longlong start, length;  /* must be longlong to avoid truncation */
631
679
 
632
680
  null_value=0;
633
681
  res=args[0]->val_str(str);
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())
654
702
  if (length > res->length() - start)
655
703
    length= res->length() - start;
656
704
 
657
 
  if ((uint64_t) (res->length() - length + res2->length()) >
658
 
      (uint64_t) current_thd->variables.max_allowed_packet)
 
705
  if ((ulonglong) (res->length() - length + res2->length()) >
 
706
      (ulonglong) 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;
674
722
 
675
723
void Item_func_insert::fix_length_and_dec()
676
724
{
677
 
  uint64_t max_result_length;
 
725
  ulonglong max_result_length;
678
726
 
679
727
  // Handle character set for args[0] and args[3].
680
728
  if (agg_arg_charsets(collation, &args[0], 2, MY_COLL_ALLOW_CONV, 3))
681
729
    return;
682
 
  max_result_length= ((uint64_t) args[0]->max_length+
683
 
                      (uint64_t) args[3]->max_length);
 
730
  max_result_length= ((ulonglong) args[0]->max_length+
 
731
                      (ulonglong) args[3]->max_length);
684
732
  if (max_result_length >= MAX_BLOB_WIDTH)
685
733
  {
686
734
    max_result_length= MAX_BLOB_WIDTH;
692
740
 
693
741
String *Item_str_conv::val_str(String *str)
694
742
{
695
 
  assert(fixed == 1);
 
743
  DBUG_ASSERT(fixed == 1);
696
744
  String *res;
697
745
  if (!(res=args[0]->val_str(str)))
698
746
  {
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());
709
 
    assert(len <= res->length());
 
757
    DBUG_ASSERT(len <= res->length());
710
758
    res->length(len);
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(),
742
790
 
743
791
String *Item_func_left::val_str(String *str)
744
792
{
745
 
  assert(fixed == 1);
 
793
  DBUG_ASSERT(fixed == 1);
746
794
  String *res= args[0]->val_str(str);
747
795
 
748
 
  /* must be int64_t to avoid truncation */
749
 
  int64_t length= args[1]->val_int();
750
 
  uint32_t char_pos;
 
796
  /* must be longlong to avoid truncation */
 
797
  longlong length= args[1]->val_int();
 
798
  uint char_pos;
751
799
 
752
800
  if ((null_value=(args[0]->null_value || args[1]->null_value)))
753
801
    return 0;
756
804
  if ((length <= 0) && (!args[1]->unsigned_flag))
757
805
    return &my_empty_string;
758
806
 
759
 
  if ((res->length() <= (uint64_t) length) ||
 
807
  if ((res->length() <= (ulonglong) length) ||
760
808
      (res->length() <= (char_pos= res->charpos((int) length))))
761
809
    return res;
762
810
 
788
836
 
789
837
String *Item_func_right::val_str(String *str)
790
838
{
791
 
  assert(fixed == 1);
 
839
  DBUG_ASSERT(fixed == 1);
792
840
  String *res= args[0]->val_str(str);
793
 
  /* must be int64_t to avoid truncation */
794
 
  int64_t length= args[1]->val_int();
 
841
  /* must be longlong to avoid truncation */
 
842
  longlong length= args[1]->val_int();
795
843
 
796
844
  if ((null_value=(args[0]->null_value || args[1]->null_value)))
797
845
    return 0; /* purecov: inspected */
800
848
  if ((length <= 0) && (!args[1]->unsigned_flag))
801
849
    return &my_empty_string; /* purecov: inspected */
802
850
 
803
 
  if (res->length() <= (uint64_t) length)
 
851
  if (res->length() <= (ulonglong) 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);
821
869
 
822
870
String *Item_func_substr::val_str(String *str)
823
871
{
824
 
  assert(fixed == 1);
 
872
  DBUG_ASSERT(fixed == 1);
825
873
  String *res  = args[0]->val_str(str);
826
 
  /* must be int64_t to avoid truncation */
827
 
  int64_t start= args[1]->val_int();
828
 
  /* Assumes that the maximum length of a String is < INT32_MAX. */
 
874
  /* must be longlong to avoid truncation */
 
875
  longlong start= args[1]->val_int();
 
876
  /* Assumes that the maximum length of a String is < INT_MAX32. */
829
877
  /* Limit so that code sees out-of-bound value properly. */
830
 
  int64_t length= arg_count == 3 ? args[2]->val_int() : INT32_MAX;
831
 
  int64_t tmp_length;
 
878
  longlong length= arg_count == 3 ? args[2]->val_int() : INT_MAX32;
 
879
  longlong tmp_length;
832
880
 
833
881
  if ((null_value=(args[0]->null_value || args[1]->null_value ||
834
882
                   (arg_count == 3 && args[2]->null_value))))
839
887
      (length == 0 || !args[2]->unsigned_flag))
840
888
    return &my_empty_string;
841
889
 
842
 
  /* Assumes that the maximum length of a String is < INT32_MAX. */
 
890
  /* Assumes that the maximum length of a String is < INT_MAX32. */
843
891
  /* Set here so that rest of code sees out-of-bound value as such. */
844
 
  if ((length <= 0) || (length > INT32_MAX))
845
 
    length= INT32_MAX;
 
892
  if ((length <= 0) || (length > INT_MAX32))
 
893
    length= INT_MAX32;
846
894
 
847
895
  /* if "unsigned_flag" is set, we have a *huge* positive number. */
848
 
  /* Assumes that the maximum length of a String is < INT32_MAX. */
849
 
  if ((!args[1]->unsigned_flag && (start < INT32_MIN || start > INT32_MAX)) ||
850
 
      (args[1]->unsigned_flag && ((uint64_t) start > INT32_MAX)))
 
896
  /* Assumes that the maximum length of a String is < INT_MAX32. */
 
897
  if ((!args[1]->unsigned_flag && (start < INT_MIN32 || start > INT_MAX32)) ||
 
898
      (args[1]->unsigned_flag && ((ulonglong) start > INT_MAX32)))
851
899
    return &my_empty_string;
852
900
 
853
901
  start= ((start < 0) ? res->numchars() + start : start - 1);
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
 
  if (!start && (int64_t) res->length() == length)
 
910
  if (!start && (longlong) 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
902
950
 
903
951
String *Item_func_substr_index::val_str(String *str)
904
952
{
905
 
  assert(fixed == 1);
 
953
  DBUG_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)
1032
1080
 
1033
1081
String *Item_func_ltrim::val_str(String *str)
1034
1082
{
1035
 
  assert(fixed == 1);
 
1083
  DBUG_ASSERT(fixed == 1);
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))
1078
1126
 
1079
1127
String *Item_func_rtrim::val_str(String *str)
1080
1128
{
1081
 
  assert(fixed == 1);
 
1129
  DBUG_ASSERT(fixed == 1);
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
  {
1158
1206
 
1159
1207
String *Item_func_trim::val_str(String *str)
1160
1208
{
1161
 
  assert(fixed == 1);
 
1209
  DBUG_ASSERT(fixed == 1);
1162
1210
  char buff[MAX_FIELD_WIDTH], *ptr, *end;
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
  DBUG_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
/* Item_func_old_password */
 
1328
 
 
1329
String *Item_func_old_password::val_str(String *str)
 
1330
{
 
1331
  DBUG_ASSERT(fixed == 1);
 
1332
  String *res= args[0]->val_str(str);
 
1333
  if ((null_value=args[0]->null_value))
 
1334
    return 0;
 
1335
  if (res->length() == 0)
 
1336
    return &my_empty_string;
 
1337
  make_scrambled_password_323(tmp_value, res->c_ptr());
 
1338
  str->set(tmp_value, SCRAMBLED_PASSWORD_CHAR_LENGTH_323, res->charset());
 
1339
  return str;
 
1340
}
 
1341
 
 
1342
char *Item_func_old_password::alloc(THD *thd, const char *password)
 
1343
{
 
1344
  char *buff= (char *) thd->alloc(SCRAMBLED_PASSWORD_CHAR_LENGTH_323+1);
 
1345
  if (buff)
 
1346
    make_scrambled_password_323(buff, password);
 
1347
  return buff;
 
1348
}
 
1349
 
 
1350
 
 
1351
#define bin_to_ascii(c) ((c)>=38?((c)-38+'a'):(c)>=12?((c)-12+'A'):(c)+'.')
 
1352
 
 
1353
String *Item_func_encrypt::val_str(String *str)
 
1354
{
 
1355
  DBUG_ASSERT(fixed == 1);
 
1356
  String *res  =args[0]->val_str(str);
 
1357
 
 
1358
#ifdef HAVE_CRYPT
 
1359
  char salt[3],*salt_ptr;
 
1360
  if ((null_value=args[0]->null_value))
 
1361
    return 0;
 
1362
  if (res->length() == 0)
 
1363
    return &my_empty_string;
 
1364
 
 
1365
  if (arg_count == 1)
 
1366
  {                                     // generate random salt
 
1367
    time_t timestamp=current_thd->query_start();
 
1368
    salt[0] = bin_to_ascii( (ulong) timestamp & 0x3f);
 
1369
    salt[1] = bin_to_ascii(( (ulong) timestamp >> 5) & 0x3f);
 
1370
    salt[2] = 0;
 
1371
    salt_ptr=salt;
 
1372
  }
 
1373
  else
 
1374
  {                                     // obtain salt from the first two bytes
 
1375
    String *salt_str=args[1]->val_str(&tmp_value);
 
1376
    if ((null_value= (args[1]->null_value || salt_str->length() < 2)))
 
1377
      return 0;
 
1378
    salt_ptr= salt_str->c_ptr();
 
1379
  }
 
1380
  pthread_mutex_lock(&LOCK_crypt);
 
1381
  char *tmp= crypt(res->c_ptr(),salt_ptr);
 
1382
  if (!tmp)
 
1383
  {
 
1384
    pthread_mutex_unlock(&LOCK_crypt);
 
1385
    null_value= 1;
 
1386
    return 0;
 
1387
  }
 
1388
  str->set(tmp, (uint) strlen(tmp), &my_charset_bin);
 
1389
  str->copy();
 
1390
  pthread_mutex_unlock(&LOCK_crypt);
 
1391
  return str;
 
1392
#else
 
1393
  null_value=1;
 
1394
  return 0;
 
1395
#endif  /* HAVE_CRYPT */
 
1396
}
 
1397
 
 
1398
void Item_func_encode::fix_length_and_dec()
 
1399
{
 
1400
  max_length=args[0]->max_length;
 
1401
  maybe_null=args[0]->maybe_null || args[1]->maybe_null;
 
1402
  collation.set(&my_charset_bin);
 
1403
}
 
1404
 
 
1405
String *Item_func_encode::val_str(String *str)
 
1406
{
 
1407
  String *res;
 
1408
  char pw_buff[80];
 
1409
  String tmp_pw_value(pw_buff, sizeof(pw_buff), system_charset_info);
 
1410
  String *password;
 
1411
  DBUG_ASSERT(fixed == 1);
 
1412
 
 
1413
  if (!(res=args[0]->val_str(str)))
 
1414
  {
 
1415
    null_value=1; /* purecov: inspected */
 
1416
    return 0; /* purecov: inspected */
 
1417
  }
 
1418
 
 
1419
  if (!(password=args[1]->val_str(& tmp_pw_value)))
 
1420
  {
 
1421
    null_value=1;
 
1422
    return 0;
 
1423
  }
 
1424
 
 
1425
  null_value=0;
 
1426
  res=copy_if_not_alloced(str,res,res->length());
 
1427
  SQL_CRYPT sql_crypt(password->ptr());
 
1428
  sql_crypt.init();
 
1429
  sql_crypt.encode((char*) res->ptr(),res->length());
 
1430
  res->set_charset(&my_charset_bin);
 
1431
  return res;
 
1432
}
 
1433
 
 
1434
String *Item_func_decode::val_str(String *str)
 
1435
{
 
1436
  String *res;
 
1437
  char pw_buff[80];
 
1438
  String tmp_pw_value(pw_buff, sizeof(pw_buff), system_charset_info);
 
1439
  String *password;
 
1440
  DBUG_ASSERT(fixed == 1);
 
1441
 
 
1442
  if (!(res=args[0]->val_str(str)))
 
1443
  {
 
1444
    null_value=1; /* purecov: inspected */
 
1445
    return 0; /* purecov: inspected */
 
1446
  }
 
1447
 
 
1448
  if (!(password=args[1]->val_str(& tmp_pw_value)))
 
1449
  {
 
1450
    null_value=1;
 
1451
    return 0;
 
1452
  }
 
1453
 
 
1454
  null_value=0;
 
1455
  res=copy_if_not_alloced(str,res,res->length());
 
1456
  SQL_CRYPT sql_crypt(password->ptr());
 
1457
  sql_crypt.init();
 
1458
  sql_crypt.decode((char*) res->ptr(),res->length());
 
1459
  return res;
 
1460
}
 
1461
 
 
1462
 
 
1463
Item *Item_func_sysconst::safe_charset_converter(CHARSET_INFO *tocs)
1257
1464
{
1258
1465
  Item_string *conv;
1259
 
  uint32_t conv_errors;
 
1466
  uint conv_errors;
1260
1467
  String tmp, cstr, *ostr= val_str(&tmp);
1261
1468
  cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
1262
1469
  if (conv_errors ||
1275
1482
 
1276
1483
String *Item_func_database::val_str(String *str)
1277
1484
{
1278
 
  assert(fixed == 1);
 
1485
  DBUG_ASSERT(fixed == 1);
1279
1486
  THD *thd= current_thd;
1280
1487
  if (thd->db == NULL)
1281
1488
  {
1294
1501
*/
1295
1502
bool Item_func_user::init(const char *user, const char *host)
1296
1503
{
1297
 
  assert(fixed == 1);
 
1504
  DBUG_ASSERT(fixed == 1);
1298
1505
 
1299
1506
  // For system threads (e.g. replication SQL thread) user may be empty
1300
1507
  if (user)
1301
1508
  {
1302
 
    const CHARSET_INFO * const cs= str_value.charset();
1303
 
    uint32_t res_length= (strlen(user)+strlen(host)+2) * cs->mbmaxlen;
 
1509
    CHARSET_INFO *cs= str_value.charset();
 
1510
    uint res_length= (strlen(user)+strlen(host)+2) * cs->mbmaxlen;
1304
1511
 
1305
1512
    if (str_value.alloc(res_length))
1306
1513
    {
1307
1514
      null_value=1;
1308
 
      return true;
 
1515
      return TRUE;
1309
1516
    }
1310
1517
 
1311
1518
    res_length=cs->cset->snprintf(cs, (char*)str_value.ptr(), res_length,
1313
1520
    str_value.length(res_length);
1314
1521
    str_value.mark_as_const();
1315
1522
  }
1316
 
  return false;
 
1523
  return FALSE;
1317
1524
}
1318
1525
 
1319
1526
 
1321
1528
{
1322
1529
  return (Item_func_sysconst::fix_fields(thd, ref) ||
1323
1530
          init(thd->main_security_ctx.user,
1324
 
               thd->main_security_ctx.ip));
 
1531
               thd->main_security_ctx.host_or_ip));
1325
1532
}
1326
1533
 
1327
1534
 
1328
1535
bool Item_func_current_user::fix_fields(THD *thd, Item **ref)
1329
1536
{
1330
1537
  if (Item_func_sysconst::fix_fields(thd, ref))
1331
 
    return true;
 
1538
    return TRUE;
1332
1539
 
1333
1540
  Security_context *ctx=
1334
1541
                         thd->security_ctx;
1335
 
  return init(ctx->user, ctx->ip);
 
1542
  return init(ctx->priv_user, ctx->priv_host);
1336
1543
}
1337
1544
 
1338
1545
 
1352
1559
void Item_func_format::fix_length_and_dec()
1353
1560
{
1354
1561
  collation.set(default_charset());
1355
 
  uint32_t char_length= args[0]->max_length/args[0]->collation.collation->mbmaxlen;
 
1562
  uint char_length= args[0]->max_length/args[0]->collation.collation->mbmaxlen;
1356
1563
  max_length= ((char_length + (char_length-args[0]->decimals)/3) *
1357
1564
               collation.collation->mbmaxlen);
1358
1565
}
1366
1573
 
1367
1574
String *Item_func_format::val_str(String *str)
1368
1575
{
1369
 
  uint32_t length;
1370
 
  uint32_t str_length;
 
1576
  uint32 length;
 
1577
  uint32 str_length;
1371
1578
  /* Number of decimal digits */
1372
1579
  int dec;
1373
1580
  /* Number of characters used to represent the decimals, including '.' */
1374
 
  uint32_t dec_length;
 
1581
  uint32 dec_length;
1375
1582
  int diff;
1376
 
  assert(fixed == 1);
 
1583
  DBUG_ASSERT(fixed == 1);
1377
1584
 
1378
1585
  dec= (int) args[1]->val_int();
1379
1586
  if (args[1]->null_value)
1404
1611
    double nr= args[0]->val_real();
1405
1612
    if ((null_value=args[0]->null_value))
1406
1613
      return 0; /* purecov: inspected */
1407
 
    nr= my_double_round(nr, (int64_t) dec, false, false);
 
1614
    nr= my_double_round(nr, (longlong) dec, FALSE, FALSE);
1408
1615
    /* Here default_charset() is right as this is not an automatic conversion */
1409
1616
    str->set_real(nr, dec, default_charset());
1410
 
    if (std::isnan(nr))
 
1617
    if (isnan(nr))
1411
1618
      return str;
1412
1619
    str_length=str->length();
1413
1620
    if (nr < 0)
1457
1664
  if (agg_arg_charsets(collation, args+1, arg_count-1, MY_COLL_ALLOW_CONV, 1))
1458
1665
    return;
1459
1666
 
1460
 
  for (uint32_t i= 1 ; i < arg_count ; i++)
 
1667
  for (uint i= 1 ; i < arg_count ; i++)
1461
1668
  {
1462
1669
    set_if_bigger(max_length,args[i]->max_length);
1463
1670
    set_if_bigger(decimals,args[i]->decimals);
1468
1675
 
1469
1676
double Item_func_elt::val_real()
1470
1677
{
1471
 
  assert(fixed == 1);
1472
 
  uint32_t tmp;
 
1678
  DBUG_ASSERT(fixed == 1);
 
1679
  uint tmp;
1473
1680
  null_value=1;
1474
1681
  if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
1475
1682
    return 0.0;
1479
1686
}
1480
1687
 
1481
1688
 
1482
 
int64_t Item_func_elt::val_int()
 
1689
longlong Item_func_elt::val_int()
1483
1690
{
1484
 
  assert(fixed == 1);
1485
 
  uint32_t tmp;
 
1691
  DBUG_ASSERT(fixed == 1);
 
1692
  uint tmp;
1486
1693
  null_value=1;
1487
1694
  if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
1488
1695
    return 0;
1489
1696
 
1490
 
  int64_t result= args[tmp]->val_int();
 
1697
  longlong result= args[tmp]->val_int();
1491
1698
  null_value= args[tmp]->null_value;
1492
1699
  return result;
1493
1700
}
1495
1702
 
1496
1703
String *Item_func_elt::val_str(String *str)
1497
1704
{
1498
 
  assert(fixed == 1);
1499
 
  uint32_t tmp;
 
1705
  DBUG_ASSERT(fixed == 1);
 
1706
  uint tmp;
1500
1707
  null_value=1;
1501
1708
  if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
1502
1709
    return NULL;
1512
1719
void Item_func_make_set::split_sum_func(THD *thd, Item **ref_pointer_array,
1513
1720
                                        List<Item> &fields)
1514
1721
{
1515
 
  item->split_sum_func2(thd, ref_pointer_array, fields, &item, true);
 
1722
  item->split_sum_func2(thd, ref_pointer_array, fields, &item, TRUE);
1516
1723
  Item_str_func::split_sum_func(thd, ref_pointer_array, fields);
1517
1724
}
1518
1725
 
1524
1731
  if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1))
1525
1732
    return;
1526
1733
  
1527
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
1734
  for (uint i=0 ; i < arg_count ; i++)
1528
1735
    max_length+=args[i]->max_length;
1529
1736
 
1530
1737
  used_tables_cache|=     item->used_tables();
1545
1752
 
1546
1753
String *Item_func_make_set::val_str(String *str)
1547
1754
{
1548
 
  assert(fixed == 1);
1549
 
  uint64_t bits;
 
1755
  DBUG_ASSERT(fixed == 1);
 
1756
  ulonglong bits;
1550
1757
  bool first_found=0;
1551
1758
  Item **ptr=args;
1552
1759
  String *result=&my_empty_string;
1556
1763
    return NULL;
1557
1764
 
1558
1765
  if (arg_count < 64)
1559
 
    bits &= ((uint64_t) 1 << arg_count)-1;
 
1766
    bits &= ((ulonglong) 1 << arg_count)-1;
1560
1767
 
1561
1768
  for (; bits; bits >>= 1, ptr++)
1562
1769
  {
1596
1803
}
1597
1804
 
1598
1805
 
1599
 
Item *Item_func_make_set::transform(Item_transformer transformer, unsigned char *arg)
 
1806
Item *Item_func_make_set::transform(Item_transformer transformer, uchar *arg)
1600
1807
{
1601
1808
  Item *new_item= item->transform(transformer, arg);
1602
1809
  if (!new_item)
1629
1836
 
1630
1837
String *Item_func_char::val_str(String *str)
1631
1838
{
1632
 
  assert(fixed == 1);
 
1839
  DBUG_ASSERT(fixed == 1);
1633
1840
  str->length(0);
1634
1841
  str->set_charset(collation.collation);
1635
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
1842
  for (uint i=0 ; i < arg_count ; i++)
1636
1843
  {
1637
 
    int32_t num=(int32_t) args[i]->val_int();
 
1844
    int32 num=(int32) args[i]->val_int();
1638
1845
    if (!args[i]->null_value)
1639
1846
    {
1640
1847
      char char_num= (char) num;
1682
1889
  collation.set(args[0]->collation);
1683
1890
  if (args[1]->const_item())
1684
1891
  {
1685
 
    /* must be int64_t to avoid truncation */
1686
 
    int64_t count= args[1]->val_int();
 
1892
    /* must be longlong to avoid truncation */
 
1893
    longlong count= args[1]->val_int();
1687
1894
 
1688
 
    /* Assumes that the maximum length of a String is < INT32_MAX. */
 
1895
    /* Assumes that the maximum length of a String is < INT_MAX32. */
1689
1896
    /* Set here so that rest of code sees out-of-bound value as such. */
1690
 
    if (count > INT32_MAX)
1691
 
      count= INT32_MAX;
 
1897
    if (count > INT_MAX32)
 
1898
      count= INT_MAX32;
1692
1899
 
1693
 
    uint64_t max_result_length= (uint64_t) args[0]->max_length * count;
 
1900
    ulonglong max_result_length= (ulonglong) args[0]->max_length * count;
1694
1901
    if (max_result_length >= MAX_BLOB_WIDTH)
1695
1902
    {
1696
1903
      max_result_length= MAX_BLOB_WIDTH;
1712
1919
 
1713
1920
String *Item_func_repeat::val_str(String *str)
1714
1921
{
1715
 
  assert(fixed == 1);
1716
 
  uint32_t length,tot_length;
 
1922
  DBUG_ASSERT(fixed == 1);
 
1923
  uint length,tot_length;
1717
1924
  char *to;
1718
 
  /* must be int64_t to avoid truncation */
1719
 
  int64_t count= args[1]->val_int();
 
1925
  /* must be longlong to avoid truncation */
 
1926
  longlong count= args[1]->val_int();
1720
1927
  String *res= args[0]->val_str(str);
1721
1928
 
1722
1929
  if (args[0]->null_value || args[1]->null_value)
1726
1933
  if (count <= 0 && (count == 0 || !args[1]->unsigned_flag))
1727
1934
    return &my_empty_string;
1728
1935
 
1729
 
  /* Assumes that the maximum length of a String is < INT32_MAX. */
 
1936
  /* Assumes that the maximum length of a String is < INT_MAX32. */
1730
1937
  /* Bounds check on count:  If this is triggered, we will error. */
1731
 
  if ((uint64_t) count > INT32_MAX)
1732
 
    count= INT32_MAX;
 
1938
  if ((ulonglong) count > INT_MAX32)
 
1939
    count= INT_MAX32;
1733
1940
  if (count == 1)                       // To avoid reallocs
1734
1941
    return res;
1735
1942
  length=res->length();
1736
1943
  // Safe length check
1737
1944
  if (length > current_thd->variables.max_allowed_packet / (uint) count)
1738
1945
  {
1739
 
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
1946
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1740
1947
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
1741
1948
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
1742
1949
                        func_name(), current_thd->variables.max_allowed_packet);
1767
1974
    return;
1768
1975
  if (args[1]->const_item())
1769
1976
  {
1770
 
    uint64_t length= 0;
 
1977
    ulonglong length= 0;
1771
1978
 
1772
1979
    if (collation.collation->mbmaxlen > 0)
1773
1980
    {
1774
 
      uint64_t temp= (uint64_t) args[1]->val_int();
 
1981
      ulonglong temp= (ulonglong) args[1]->val_int();
1775
1982
 
1776
 
      /* Assumes that the maximum length of a String is < INT32_MAX. */
 
1983
      /* Assumes that the maximum length of a String is < INT_MAX32. */
1777
1984
      /* Set here so that rest of code sees out-of-bound value as such. */
1778
 
      if (temp > INT32_MAX)
1779
 
        temp = INT32_MAX;
 
1985
      if (temp > INT_MAX32)
 
1986
        temp = INT_MAX32;
1780
1987
 
1781
1988
      length= temp * collation.collation->mbmaxlen;
1782
1989
    }
1798
2005
 
1799
2006
String *Item_func_rpad::val_str(String *str)
1800
2007
{
1801
 
  assert(fixed == 1);
1802
 
  uint32_t res_byte_length,res_char_length,pad_char_length,pad_byte_length;
 
2008
  DBUG_ASSERT(fixed == 1);
 
2009
  uint32 res_byte_length,res_char_length,pad_char_length,pad_byte_length;
1803
2010
  char *to;
1804
2011
  const char *ptr_pad;
1805
 
  /* must be int64_t to avoid truncation */
1806
 
  int64_t count= args[1]->val_int();
1807
 
  int64_t byte_count;
 
2012
  /* must be longlong to avoid truncation */
 
2013
  longlong count= args[1]->val_int();
 
2014
  longlong byte_count;
1808
2015
  String *res= args[0]->val_str(str);
1809
2016
  String *rpad= args[2]->val_str(&rpad_str);
1810
2017
 
1812
2019
      ((count < 0) && !args[1]->unsigned_flag))
1813
2020
    goto err;
1814
2021
  null_value=0;
1815
 
  /* Assumes that the maximum length of a String is < INT32_MAX. */
 
2022
  /* Assumes that the maximum length of a String is < INT_MAX32. */
1816
2023
  /* Set here so that rest of code sees out-of-bound value as such. */
1817
 
  if ((uint64_t) count > INT32_MAX)
1818
 
    count= INT32_MAX;
 
2024
  if ((ulonglong) count > INT_MAX32)
 
2025
    count= INT_MAX32;
1819
2026
  if (count <= (res_char_length= res->numchars()))
1820
2027
  {                                             // String to pad is big enough
1821
2028
    res->length(res->charpos((int) count));     // Shorten result if longer
1824
2031
  pad_char_length= rpad->numchars();
1825
2032
 
1826
2033
  byte_count= count * collation.collation->mbmaxlen;
1827
 
  if ((uint64_t) byte_count > current_thd->variables.max_allowed_packet)
 
2034
  if ((ulonglong) byte_count > current_thd->variables.max_allowed_packet)
1828
2035
  {
1829
 
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2036
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1830
2037
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
1831
2038
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
1832
2039
                        func_name(), current_thd->variables.max_allowed_packet);
1842
2049
  ptr_pad=rpad->ptr();
1843
2050
  pad_byte_length= rpad->length();
1844
2051
  count-= res_char_length;
1845
 
  for ( ; (uint32_t) count > pad_char_length; count-= pad_char_length)
 
2052
  for ( ; (uint32) count > pad_char_length; count-= pad_char_length)
1846
2053
  {
1847
2054
    memcpy(to,ptr_pad,pad_byte_length);
1848
2055
    to+= pad_byte_length;
1870
2077
  
1871
2078
  if (args[1]->const_item())
1872
2079
  {
1873
 
    uint64_t length= 0;
 
2080
    ulonglong length= 0;
1874
2081
 
1875
2082
    if (collation.collation->mbmaxlen > 0)
1876
2083
    {
1877
 
      uint64_t temp= (uint64_t) args[1]->val_int();
 
2084
      ulonglong temp= (ulonglong) args[1]->val_int();
1878
2085
 
1879
 
      /* Assumes that the maximum length of a String is < INT32_MAX. */
 
2086
      /* Assumes that the maximum length of a String is < INT_MAX32. */
1880
2087
      /* Set here so that rest of code sees out-of-bound value as such. */
1881
 
      if (temp > INT32_MAX)
1882
 
        temp= INT32_MAX;
 
2088
      if (temp > INT_MAX32)
 
2089
        temp= INT_MAX32;
1883
2090
 
1884
2091
      length= temp * collation.collation->mbmaxlen;
1885
2092
    }
1901
2108
 
1902
2109
String *Item_func_lpad::val_str(String *str)
1903
2110
{
1904
 
  assert(fixed == 1);
1905
 
  uint32_t res_char_length,pad_char_length;
1906
 
  /* must be int64_t to avoid truncation */
1907
 
  int64_t count= args[1]->val_int();
1908
 
  int64_t byte_count;
 
2111
  DBUG_ASSERT(fixed == 1);
 
2112
  uint32 res_char_length,pad_char_length;
 
2113
  /* must be longlong to avoid truncation */
 
2114
  longlong count= args[1]->val_int();
 
2115
  longlong byte_count;
1909
2116
  String *res= args[0]->val_str(&tmp_value);
1910
2117
  String *pad= args[2]->val_str(&lpad_str);
1911
2118
 
1913
2120
      ((count < 0) && !args[1]->unsigned_flag))
1914
2121
    goto err;  
1915
2122
  null_value=0;
1916
 
  /* Assumes that the maximum length of a String is < INT32_MAX. */
 
2123
  /* Assumes that the maximum length of a String is < INT_MAX32. */
1917
2124
  /* Set here so that rest of code sees out-of-bound value as such. */
1918
 
  if ((uint64_t) count > INT32_MAX)
1919
 
    count= INT32_MAX;
 
2125
  if ((ulonglong) count > INT_MAX32)
 
2126
    count= INT_MAX32;
1920
2127
 
1921
2128
  res_char_length= res->numchars();
1922
2129
 
1929
2136
  pad_char_length= pad->numchars();
1930
2137
  byte_count= count * collation.collation->mbmaxlen;
1931
2138
  
1932
 
  if ((uint64_t) byte_count > current_thd->variables.max_allowed_packet)
 
2139
  if ((ulonglong) byte_count > current_thd->variables.max_allowed_packet)
1933
2140
  {
1934
 
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2141
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1935
2142
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
1936
2143
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
1937
2144
                        func_name(), current_thd->variables.max_allowed_packet);
1939
2146
  }
1940
2147
 
1941
2148
  if (args[2]->null_value || !pad_char_length ||
1942
 
      str->alloc((uint32_t) byte_count))
 
2149
      str->alloc((uint32) byte_count))
1943
2150
    goto err;
1944
2151
  
1945
2152
  str->length(0);
1965
2172
 
1966
2173
String *Item_func_conv::val_str(String *str)
1967
2174
{
1968
 
  assert(fixed == 1);
 
2175
  DBUG_ASSERT(fixed == 1);
1969
2176
  String *res= args[0]->val_str(str);
1970
2177
  char *endptr,ans[65],*ptr;
1971
 
  int64_t dec;
 
2178
  longlong dec;
1972
2179
  int from_base= (int) args[1]->val_int();
1973
2180
  int to_base= (int) args[2]->val_int();
1974
2181
  int err;
1983
2190
  null_value= 0;
1984
2191
  unsigned_flag= !(from_base < 0);
1985
2192
 
1986
 
  if (from_base < 0)
1987
 
    dec= my_strntoll(res->charset(), res->ptr(), res->length(),
1988
 
                     -from_base, &endptr, &err);
 
2193
  if (args[0]->field_type() == MYSQL_TYPE_BIT) 
 
2194
  {
 
2195
    /* 
 
2196
     Special case: The string representation of BIT doesn't resemble the
 
2197
     decimal representation, so we shouldn't change it to string and then to
 
2198
     decimal. 
 
2199
    */
 
2200
    dec= args[0]->val_int();
 
2201
  }
1989
2202
  else
1990
 
    dec= (int64_t) my_strntoull(res->charset(), res->ptr(), res->length(),
1991
 
                                 from_base, &endptr, &err);
 
2203
  {
 
2204
    if (from_base < 0)
 
2205
      dec= my_strntoll(res->charset(), res->ptr(), res->length(),
 
2206
                       -from_base, &endptr, &err);
 
2207
    else
 
2208
      dec= (longlong) my_strntoull(res->charset(), res->ptr(), res->length(),
 
2209
                                   from_base, &endptr, &err);
 
2210
  }
1992
2211
 
1993
 
  ptr= int64_t2str(dec, ans, to_base);
1994
 
  if (str->copy(ans, (uint32_t) (ptr-ans), default_charset()))
 
2212
  ptr= longlong2str(dec, ans, to_base);
 
2213
  if (str->copy(ans, (uint32) (ptr-ans), default_charset()))
1995
2214
    return &my_empty_string;
1996
2215
  return str;
1997
2216
}
1999
2218
 
2000
2219
String *Item_func_conv_charset::val_str(String *str)
2001
2220
{
2002
 
  assert(fixed == 1);
 
2221
  DBUG_ASSERT(fixed == 1);
2003
2222
  if (use_cached_value)
2004
2223
    return null_value ? 0 : &str_value;
2005
2224
  String *arg= args[0]->val_str(str);
2006
 
  uint32_t dummy_errors;
 
2225
  uint dummy_errors;
2007
2226
  if (!arg)
2008
2227
  {
2009
2228
    null_value=1;
2031
2250
 
2032
2251
String *Item_func_set_collation::val_str(String *str)
2033
2252
{
2034
 
  assert(fixed == 1);
 
2253
  DBUG_ASSERT(fixed == 1);
2035
2254
  str=args[0]->val_str(str);
2036
2255
  if ((null_value=args[0]->null_value))
2037
2256
    return 0;
2041
2260
 
2042
2261
void Item_func_set_collation::fix_length_and_dec()
2043
2262
{
2044
 
  const CHARSET_INFO *set_collation;
 
2263
  CHARSET_INFO *set_collation;
2045
2264
  const char *colname;
2046
2265
  String tmp, *str= args[1]->val_str(&tmp);
2047
2266
  colname= str->c_ptr();
2084
2303
  Item_func_set_collation *item_func_sc=(Item_func_set_collation*) item;
2085
2304
  if (collation.collation != item_func_sc->collation.collation)
2086
2305
    return 0;
2087
 
  for (uint32_t i=0; i < arg_count ; i++)
 
2306
  for (uint i=0; i < arg_count ; i++)
2088
2307
    if (!args[i]->eq(item_func_sc->args[i], binary_cmp))
2089
2308
      return 0;
2090
2309
  return 1;
2096
2315
  str->append('(');
2097
2316
  args[0]->print(str, query_type);
2098
2317
  str->append(STRING_WITH_LEN(" collate "));
2099
 
  assert(args[1]->basic_const_item() &&
 
2318
  DBUG_ASSERT(args[1]->basic_const_item() &&
2100
2319
              args[1]->type() == Item::STRING_ITEM);
2101
2320
  args[1]->str_value.print(str);
2102
2321
  str->append(')');
2104
2323
 
2105
2324
String *Item_func_charset::val_str(String *str)
2106
2325
{
2107
 
  assert(fixed == 1);
2108
 
  uint32_t dummy_errors;
 
2326
  DBUG_ASSERT(fixed == 1);
 
2327
  uint dummy_errors;
2109
2328
 
2110
 
  const CHARSET_INFO * const cs= args[0]->collation.collation; 
 
2329
  CHARSET_INFO *cs= args[0]->collation.collation; 
2111
2330
  null_value= 0;
2112
2331
  str->copy(cs->csname, strlen(cs->csname),
2113
 
            &my_charset_utf8_general_ci, collation.collation, &dummy_errors);
 
2332
            &my_charset_latin1, collation.collation, &dummy_errors);
2114
2333
  return str;
2115
2334
}
2116
2335
 
2117
2336
String *Item_func_collation::val_str(String *str)
2118
2337
{
2119
 
  assert(fixed == 1);
2120
 
  uint32_t dummy_errors;
2121
 
  const CHARSET_INFO * const cs= args[0]->collation.collation; 
 
2338
  DBUG_ASSERT(fixed == 1);
 
2339
  uint dummy_errors;
 
2340
  CHARSET_INFO *cs= args[0]->collation.collation; 
2122
2341
 
2123
2342
  null_value= 0;
2124
2343
  str->copy(cs->name, strlen(cs->name),
2125
 
            &my_charset_utf8_general_ci, collation.collation, &dummy_errors);
 
2344
            &my_charset_latin1, collation.collation, &dummy_errors);
2126
2345
  return str;
2127
2346
}
2128
2347
 
2129
2348
 
2130
2349
void Item_func_weight_string::fix_length_and_dec()
2131
2350
{
2132
 
  const CHARSET_INFO * const cs= args[0]->collation.collation;
 
2351
  CHARSET_INFO *cs= args[0]->collation.collation;
2133
2352
  collation.set(&my_charset_bin, args[0]->collation.derivation);
2134
2353
  flags= my_strxfrm_flag_normalize(flags, cs->levels_for_order);
2135
 
  max_length= cs->mbmaxlen * cmax(args[0]->max_length, nweights);
 
2354
  max_length= cs->mbmaxlen * max(args[0]->max_length, nweights);
2136
2355
  maybe_null= 1;
2137
2356
}
2138
2357
 
2141
2360
String *Item_func_weight_string::val_str(String *str)
2142
2361
{
2143
2362
  String *res;
2144
 
  const CHARSET_INFO * const cs= args[0]->collation.collation;
2145
 
  uint32_t tmp_length, frm_length;
2146
 
  assert(fixed == 1);
 
2363
  CHARSET_INFO *cs= args[0]->collation.collation;
 
2364
  uint tmp_length, frm_length;
 
2365
  DBUG_ASSERT(fixed == 1);
2147
2366
 
2148
2367
  if (args[0]->result_type() != STRING_RESULT ||
2149
2368
      !(res= args[0]->val_str(str)))
2150
2369
    goto nl;
2151
2370
  
2152
2371
  tmp_length= cs->coll->strnxfrmlen(cs, cs->mbmaxlen *
2153
 
                                        cmax(res->length(), nweights));
 
2372
                                        max(res->length(), nweights));
2154
2373
 
2155
2374
  if (tmp_value.alloc(tmp_length))
2156
2375
    goto nl;
2157
2376
 
2158
2377
  frm_length= cs->coll->strnxfrm(cs,
2159
 
                                 (unsigned char*) tmp_value.ptr(), tmp_length,
 
2378
                                 (uchar*) tmp_value.ptr(), tmp_length,
2160
2379
                                 nweights ? nweights : tmp_length,
2161
 
                                 (const unsigned char*) res->ptr(), res->length(),
 
2380
                                 (const uchar*) res->ptr(), res->length(),
2162
2381
                                 flags);
2163
2382
  tmp_value.length(frm_length);
2164
2383
  null_value= 0;
2173
2392
String *Item_func_hex::val_str(String *str)
2174
2393
{
2175
2394
  String *res;
2176
 
  assert(fixed == 1);
 
2395
  DBUG_ASSERT(fixed == 1);
2177
2396
  if (args[0]->result_type() != STRING_RESULT)
2178
2397
  {
2179
 
    uint64_t dec;
 
2398
    ulonglong dec;
2180
2399
    char ans[65],*ptr;
2181
 
    /* Return hex of unsigned int64_t value */
 
2400
    /* Return hex of unsigned longlong value */
2182
2401
    if (args[0]->result_type() == REAL_RESULT ||
2183
2402
        args[0]->result_type() == DECIMAL_RESULT)
2184
2403
    {
2185
2404
      double val= args[0]->val_real();
2186
 
      if ((val <= (double) INT64_MIN) || 
2187
 
          (val >= (double) (uint64_t) UINT64_MAX))
2188
 
        dec=  ~(int64_t) 0;
 
2405
      if ((val <= (double) LONGLONG_MIN) || 
 
2406
          (val >= (double) (ulonglong) ULONGLONG_MAX))
 
2407
        dec=  ~(longlong) 0;
2189
2408
      else
2190
 
        dec= (uint64_t) (val + (val > 0 ? 0.5 : -0.5));
 
2409
        dec= (ulonglong) (val + (val > 0 ? 0.5 : -0.5));
2191
2410
    }
2192
2411
    else
2193
 
      dec= (uint64_t) args[0]->val_int();
 
2412
      dec= (ulonglong) args[0]->val_int();
2194
2413
 
2195
2414
    if ((null_value= args[0]->null_value))
2196
2415
      return 0;
2197
 
    ptr= int64_t2str(dec,ans,16);
2198
 
    if (str->copy(ans,(uint32_t) (ptr-ans),default_charset()))
 
2416
    ptr= longlong2str(dec,ans,16);
 
2417
    if (str->copy(ans,(uint32) (ptr-ans),default_charset()))
2199
2418
      return &my_empty_string;                  // End of memory
2200
2419
    return str;
2201
2420
  }
2221
2440
  const char *from, *end;
2222
2441
  char *to;
2223
2442
  String *res;
2224
 
  uint32_t length;
2225
 
  assert(fixed == 1);
 
2443
  uint length;
 
2444
  DBUG_ASSERT(fixed == 1);
2226
2445
 
2227
2446
  res= args[0]->val_str(str);
2228
2447
  if (!res || tmp_value.alloc(length= (1+res->length())/2))
2264
2483
}
2265
2484
 
2266
2485
 
 
2486
#include <my_dir.h>
 
2487
 
2267
2488
String *Item_load_file::val_str(String *str)
2268
2489
{
2269
 
  assert(fixed == 1);
 
2490
  DBUG_ASSERT(fixed == 1);
2270
2491
  String *file_name;
2271
2492
  File file;
2272
2493
  struct stat stat_info;
2273
2494
  char path[FN_REFLEN];
 
2495
  DBUG_ENTER("load_file");
2274
2496
 
2275
2497
  if (!(file_name= args[0]->val_str(str)))
2276
2498
    goto err;
2293
2515
  }
2294
2516
  if (stat_info.st_size > (long) current_thd->variables.max_allowed_packet)
2295
2517
  {
2296
 
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2518
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
2297
2519
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
2298
2520
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
2299
2521
                        func_name(), current_thd->variables.max_allowed_packet);
2303
2525
    goto err;
2304
2526
  if ((file = my_open(file_name->c_ptr(), O_RDONLY, MYF(0))) < 0)
2305
2527
    goto err;
2306
 
  if (my_read(file, (unsigned char*) tmp_value.ptr(), stat_info.st_size, MYF(MY_NABP)))
 
2528
  if (my_read(file, (uchar*) tmp_value.ptr(), stat_info.st_size, MYF(MY_NABP)))
2307
2529
  {
2308
2530
    my_close(file, MYF(0));
2309
2531
    goto err;
2311
2533
  tmp_value.length(stat_info.st_size);
2312
2534
  my_close(file, MYF(0));
2313
2535
  null_value = 0;
2314
 
  return(&tmp_value);
 
2536
  DBUG_RETURN(&tmp_value);
2315
2537
 
2316
2538
err:
2317
2539
  null_value = 1;
2318
 
  return(0);
 
2540
  DBUG_RETURN(0);
2319
2541
}
2320
2542
 
2321
2543
 
2322
2544
String* Item_func_export_set::val_str(String* str)
2323
2545
{
2324
 
  assert(fixed == 1);
2325
 
  uint64_t the_set = (uint64_t) args[0]->val_int();
 
2546
  DBUG_ASSERT(fixed == 1);
 
2547
  ulonglong the_set = (ulonglong) args[0]->val_int();
2326
2548
  String yes_buf, *yes;
2327
2549
  yes = args[1]->val_str(&yes_buf);
2328
2550
  String no_buf, *no;
2329
2551
  no = args[2]->val_str(&no_buf);
2330
2552
  String *sep = NULL, sep_buf ;
2331
2553
 
2332
 
  uint32_t num_set_values = 64;
2333
 
  uint64_t mask = 0x1;
 
2554
  uint num_set_values = 64;
 
2555
  ulonglong mask = 0x1;
2334
2556
  str->length(0);
2335
2557
  str->set_charset(collation.collation);
2336
2558
 
2365
2587
  case 3:
2366
2588
    {
2367
2589
      /* errors is not checked - assume "," can always be converted */
2368
 
      uint32_t errors;
 
2590
      uint errors;
2369
2591
      sep_buf.copy(STRING_WITH_LEN(","), &my_charset_bin, collation.collation, &errors);
2370
2592
      sep = &sep_buf;
2371
2593
    }
2372
2594
    break;
2373
2595
  default:
2374
 
    assert(0); // cannot happen
 
2596
    DBUG_ASSERT(0); // cannot happen
2375
2597
  }
2376
2598
  null_value=0;
2377
2599
 
2378
 
  for (uint32_t i = 0; i < num_set_values; i++, mask = (mask << 1))
 
2600
  for (uint i = 0; i < num_set_values; i++, mask = (mask << 1))
2379
2601
  {
2380
2602
    if (the_set & mask)
2381
2603
      str->append(*yes);
2389
2611
 
2390
2612
void Item_func_export_set::fix_length_and_dec()
2391
2613
{
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);
 
2614
  uint length=max(args[1]->max_length,args[2]->max_length);
 
2615
  uint sep_length=(arg_count > 3 ? args[3]->max_length : 1);
2394
2616
  max_length=length*64+sep_length*63;
2395
2617
 
2396
 
  if (agg_arg_charsets(collation, args+1, cmin((uint)4,arg_count)-1,
 
2618
  if (agg_arg_charsets(collation, args+1, min(4,arg_count)-1,
2397
2619
                       MY_COLL_ALLOW_CONV, 1))
2398
2620
    return;
2399
2621
}
2422
2644
 
2423
2645
String *Item_func_quote::val_str(String *str)
2424
2646
{
2425
 
  assert(fixed == 1);
 
2647
  DBUG_ASSERT(fixed == 1);
2426
2648
  /*
2427
2649
    Bit mask that has 1 for set for the position of the following characters:
2428
2650
    0, \, ' and ^Z
2429
2651
  */
2430
2652
 
2431
 
  static unsigned char escmask[32]=
 
2653
  static uchar escmask[32]=
2432
2654
  {
2433
2655
    0x01, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00, 0x00,
2434
2656
    0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,
2438
2660
 
2439
2661
  char *from, *to, *end, *start;
2440
2662
  String *arg= args[0]->val_str(str);
2441
 
  uint32_t arg_length, new_length;
 
2663
  uint arg_length, new_length;
2442
2664
  if (!arg)                                     // Null argument
2443
2665
  {
2444
2666
    /* Return the string 'NULL' */
2451
2673
  new_length= arg_length+2; /* for beginning and ending ' signs */
2452
2674
 
2453
2675
  for (from= (char*) arg->ptr(), end= from + arg_length; from < end; from++)
2454
 
    new_length+= get_esc_bit(escmask, (unsigned char) *from);
 
2676
    new_length+= get_esc_bit(escmask, (uchar) *from);
2455
2677
 
2456
2678
  if (tmp_value.alloc(new_length))
2457
2679
    goto null;
2497
2719
  return 0;
2498
2720
}
2499
2721
 
 
2722
longlong Item_func_uncompressed_length::val_int()
 
2723
{
 
2724
  DBUG_ASSERT(fixed == 1);
 
2725
  String *res= args[0]->val_str(&value);
 
2726
  if (!res)
 
2727
  {
 
2728
    null_value=1;
 
2729
    return 0; /* purecov: inspected */
 
2730
  }
 
2731
  null_value=0;
 
2732
  if (res->is_empty()) return 0;
 
2733
 
 
2734
  /*
 
2735
    res->ptr() using is safe because we have tested that string is not empty,
 
2736
    res->c_ptr() is not used because:
 
2737
      - we do not need \0 terminated string to get first 4 bytes
 
2738
      - c_ptr() tests simbol after string end (uninitialiozed memory) which
 
2739
        confuse valgrind
 
2740
  */
 
2741
  return uint4korr(res->ptr()) & 0x3FFFFFFF;
 
2742
}
 
2743
 
 
2744
longlong Item_func_crc32::val_int()
 
2745
{
 
2746
  DBUG_ASSERT(fixed == 1);
 
2747
  String *res=args[0]->val_str(&value);
 
2748
  if (!res)
 
2749
  {
 
2750
    null_value=1;
 
2751
    return 0; /* purecov: inspected */
 
2752
  }
 
2753
  null_value=0;
 
2754
  return (longlong) crc32(0L, (uchar*)res->ptr(), res->length());
 
2755
}
 
2756
 
 
2757
#ifdef HAVE_COMPRESS
 
2758
#include "zlib.h"
 
2759
 
 
2760
String *Item_func_compress::val_str(String *str)
 
2761
{
 
2762
  int err= Z_OK, code;
 
2763
  ulong new_size;
 
2764
  String *res;
 
2765
  Byte *body;
 
2766
  char *tmp, *last_char;
 
2767
  DBUG_ASSERT(fixed == 1);
 
2768
 
 
2769
  if (!(res= args[0]->val_str(str)))
 
2770
  {
 
2771
    null_value= 1;
 
2772
    return 0;
 
2773
  }
 
2774
  null_value= 0;
 
2775
  if (res->is_empty()) return res;
 
2776
 
 
2777
  /*
 
2778
    Citation from zlib.h (comment for compress function):
 
2779
 
 
2780
    Compresses the source buffer into the destination buffer.  sourceLen is
 
2781
    the byte length of the source buffer. Upon entry, destLen is the total
 
2782
    size of the destination buffer, which must be at least 0.1% larger than
 
2783
    sourceLen plus 12 bytes.
 
2784
    We assume here that the buffer can't grow more than .25 %.
 
2785
  */
 
2786
  new_size= res->length() + res->length() / 5 + 12;
 
2787
 
 
2788
  // Check new_size overflow: new_size <= res->length()
 
2789
  if (((uint32) (new_size+5) <= res->length()) || 
 
2790
      buffer.realloc((uint32) new_size + 4 + 1))
 
2791
  {
 
2792
    null_value= 1;
 
2793
    return 0;
 
2794
  }
 
2795
 
 
2796
  body= ((Byte*)buffer.ptr()) + 4;
 
2797
 
 
2798
  // As far as we have checked res->is_empty() we can use ptr()
 
2799
  if ((err= compress(body, &new_size,
 
2800
                     (const Bytef*)res->ptr(), res->length())) != Z_OK)
 
2801
  {
 
2802
    code= err==Z_MEM_ERROR ? ER_ZLIB_Z_MEM_ERROR : ER_ZLIB_Z_BUF_ERROR;
 
2803
    push_warning(current_thd,MYSQL_ERROR::WARN_LEVEL_ERROR,code,ER(code));
 
2804
    null_value= 1;
 
2805
    return 0;
 
2806
  }
 
2807
 
 
2808
  tmp= (char*)buffer.ptr(); // int4store is a macro; avoid side effects
 
2809
  int4store(tmp, res->length() & 0x3FFFFFFF);
 
2810
 
 
2811
  /* This is to ensure that things works for CHAR fields, which trim ' ': */
 
2812
  last_char= ((char*)body)+new_size-1;
 
2813
  if (*last_char == ' ')
 
2814
  {
 
2815
    *++last_char= '.';
 
2816
    new_size++;
 
2817
  }
 
2818
 
 
2819
  buffer.length((uint32)new_size + 4);
 
2820
  return &buffer;
 
2821
}
 
2822
 
 
2823
 
 
2824
String *Item_func_uncompress::val_str(String *str)
 
2825
{
 
2826
  DBUG_ASSERT(fixed == 1);
 
2827
  String *res= args[0]->val_str(str);
 
2828
  ulong new_size;
 
2829
  int err;
 
2830
  uint code;
 
2831
 
 
2832
  if (!res)
 
2833
    goto err;
 
2834
  null_value= 0;
 
2835
  if (res->is_empty())
 
2836
    return res;
 
2837
 
 
2838
  /* If length is less than 4 bytes, data is corrupt */
 
2839
  if (res->length() <= 4)
 
2840
  {
 
2841
    push_warning_printf(current_thd,MYSQL_ERROR::WARN_LEVEL_ERROR,
 
2842
                        ER_ZLIB_Z_DATA_ERROR,
 
2843
                        ER(ER_ZLIB_Z_DATA_ERROR));
 
2844
    goto err;
 
2845
  }
 
2846
 
 
2847
  /* Size of uncompressed data is stored as first 4 bytes of field */
 
2848
  new_size= uint4korr(res->ptr()) & 0x3FFFFFFF;
 
2849
  if (new_size > current_thd->variables.max_allowed_packet)
 
2850
  {
 
2851
    push_warning_printf(current_thd,MYSQL_ERROR::WARN_LEVEL_ERROR,
 
2852
                        ER_TOO_BIG_FOR_UNCOMPRESS,
 
2853
                        ER(ER_TOO_BIG_FOR_UNCOMPRESS),
 
2854
                        current_thd->variables.max_allowed_packet);
 
2855
    goto err;
 
2856
  }
 
2857
  if (buffer.realloc((uint32)new_size))
 
2858
    goto err;
 
2859
 
 
2860
  if ((err= uncompress((Byte*)buffer.ptr(), &new_size,
 
2861
                       ((const Bytef*)res->ptr())+4,res->length())) == Z_OK)
 
2862
  {
 
2863
    buffer.length((uint32) new_size);
 
2864
    return &buffer;
 
2865
  }
 
2866
 
 
2867
  code= ((err == Z_BUF_ERROR) ? ER_ZLIB_Z_BUF_ERROR :
 
2868
         ((err == Z_MEM_ERROR) ? ER_ZLIB_Z_MEM_ERROR : ER_ZLIB_Z_DATA_ERROR));
 
2869
  push_warning(current_thd,MYSQL_ERROR::WARN_LEVEL_ERROR,code,ER(code));
 
2870
 
 
2871
err:
 
2872
  null_value= 1;
 
2873
  return 0;
 
2874
}
 
2875
#endif
 
2876
 
2500
2877
/*
2501
2878
  UUID, as in
2502
2879
    DCE 1.1: Remote Procedure Call,
2506
2883
*/
2507
2884
 
2508
2885
static struct rand_struct uuid_rand;
2509
 
static uint32_t nanoseq;
2510
 
static uint64_t uuid_time=0;
 
2886
static uint nanoseq;
 
2887
static ulonglong uuid_time=0;
2511
2888
static char clock_seq_and_node_str[]="-0000-000000000000";
2512
2889
 
2513
2890
/**
2514
2891
  number of 100-nanosecond intervals between
2515
2892
  1582-10-15 00:00:00.00 and 1970-01-01 00:00:00.00.
2516
2893
*/
2517
 
#define UUID_TIME_OFFSET ((uint64_t) 141427 * 24 * 60 * 60 * 1000 * 10 )
 
2894
#define UUID_TIME_OFFSET ((ulonglong) 141427 * 24 * 60 * 60 * 1000 * 10 )
2518
2895
 
2519
2896
#define UUID_VERSION      0x1000
2520
2897
#define UUID_VARIANT      0x8000
2521
2898
 
2522
 
static void tohex(char *to, uint32_t from, uint32_t len)
 
2899
static void tohex(char *to, uint from, uint len)
2523
2900
{
2524
2901
  to+= len;
2525
2902
  while (len--)
2531
2908
 
2532
2909
static void set_clock_seq_str()
2533
2910
{
2534
 
  uint16_t clock_seq= ((uint)(my_rnd(&uuid_rand)*16383)) | UUID_VARIANT;
 
2911
  uint16 clock_seq= ((uint)(my_rnd(&uuid_rand)*16383)) | UUID_VARIANT;
2535
2912
  tohex(clock_seq_and_node_str+1, clock_seq, 4);
2536
2913
  nanoseq= 0;
2537
2914
}
2538
2915
 
2539
2916
String *Item_func_uuid::val_str(String *str)
2540
2917
{
2541
 
  assert(fixed == 1);
 
2918
  DBUG_ASSERT(fixed == 1);
2542
2919
  char *s;
2543
2920
  THD *thd= current_thd;
2544
2921
 
2545
2922
  pthread_mutex_lock(&LOCK_uuid_generator);
2546
2923
  if (! uuid_time) /* first UUID() call. initializing data */
2547
2924
  {
2548
 
    ulong tmp= sql_rnd();
2549
 
    unsigned char mac[6];
 
2925
    ulong tmp=sql_rnd_with_mutex();
 
2926
    uchar mac[6];
2550
2927
    int i;
2551
2928
    if (my_gethwaddr(mac))
2552
2929
    {
2559
2936
      */
2560
2937
      randominit(&uuid_rand, tmp + (ulong) thd, tmp + (ulong)global_query_id);
2561
2938
      for (i=0; i < (int)sizeof(mac); i++)
2562
 
        mac[i]=(unsigned char)(my_rnd(&uuid_rand)*255);
 
2939
        mac[i]=(uchar)(my_rnd(&uuid_rand)*255);
2563
2940
      /* purecov: end */    
2564
2941
    }
2565
2942
    s=clock_seq_and_node_str+sizeof(clock_seq_and_node_str)-1;
2573
2950
    set_clock_seq_str();
2574
2951
  }
2575
2952
 
2576
 
  uint64_t tv=my_getsystime() + UUID_TIME_OFFSET + nanoseq;
 
2953
  ulonglong tv=my_getsystime() + UUID_TIME_OFFSET + nanoseq;
2577
2954
  if (unlikely(tv < uuid_time))
2578
2955
    set_clock_seq_str();
2579
2956
  else if (unlikely(tv == uuid_time))
2589
2966
      tv-=nanoseq;
2590
2967
      nanoseq=0;
2591
2968
    }
2592
 
    assert(tv > uuid_time);
 
2969
    DBUG_ASSERT(tv > uuid_time);
2593
2970
  }
2594
2971
  uuid_time=tv;
2595
2972
  pthread_mutex_unlock(&LOCK_uuid_generator);
2596
2973
 
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);
 
2974
  uint32 time_low=            (uint32) (tv & 0xFFFFFFFF);
 
2975
  uint16 time_mid=            (uint16) ((tv >> 32) & 0xFFFF);
 
2976
  uint16 time_hi_and_version= (uint16) ((tv >> 48) | UUID_VERSION);
2600
2977
 
2601
2978
  str->realloc(UUID_LENGTH+1);
2602
2979
  str->length(UUID_LENGTH);
2606
2983
  tohex(s, time_low, 8);
2607
2984
  tohex(s+9, time_mid, 4);
2608
2985
  tohex(s+14, time_hi_and_version, 4);
2609
 
  my_stpcpy(s+18, clock_seq_and_node_str);
 
2986
  strmov(s+18, clock_seq_and_node_str);
2610
2987
  return str;
2611
2988
}