-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
item_strfunc.cc
executable file
·3425 lines (3005 loc) · 87.7 KB
/
item_strfunc.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
@file
@brief
This file defines all string functions
@warning
Some string functions don't always put and end-null on a String.
(This shouldn't be needed)
*/
/* May include caustic 3rd-party defs. Use early, so it can override nothing. */
#include "sha2.h"
#include "my_global.h" // HAVE_*
#include "sql_priv.h"
/*
It is necessary to include set_var.h instead of item.h because there
are dependencies on include order for set_var.h and item.h. This
will be resolved later.
*/
#include "sql_class.h" // set_var.h: THD
#include "set_var.h"
#include "mysqld.h" // LOCK_uuid_generator
#include "sql_acl.h" // SUPER_ACL
#include "password.h" // my_make_scrambled_password,
// my_make_scrambled_password_323
#include "crypt_genhash_impl.h"
#include <m_ctype.h>
#include <base64.h>
#include "my_md5.h"
#include "sha1.h"
#include "my_aes.h"
#include <zlib.h>
#include "my_rnd.h"
C_MODE_START
#include "../mysys/my_static.h" // For soundex_map
C_MODE_END
using std::min;
using std::max;
/*
For the Items which have only val_str_ascii() method
and don't have their own "native" val_str(),
we provide a "wrapper" method to convert from ASCII
to Item character set when it's necessary.
Conversion happens only in case of "tricky" Item character set (e.g. UCS2).
Normally conversion does not happen, and val_str_ascii() is immediately
returned instead.
*/
String *Item_str_func::val_str_from_val_str_ascii(String *str, String *str2)
{
DBUG_ASSERT(fixed == 1);
if (!(collation.collation->state & MY_CS_NONASCII))
{
String *res= val_str_ascii(str);
if (res)
res->set_charset(collation.collation);
return res;
}
DBUG_ASSERT(str != str2);
uint errors;
String *res= val_str_ascii(str);
if (!res)
return 0;
if ((null_value= str2->copy(res->ptr(), res->length(),
&my_charset_latin1, collation.collation,
&errors)))
return 0;
return str2;
}
my_decimal *Item_str_func::val_decimal(my_decimal *decimal_value)
{
DBUG_ASSERT(fixed == 1);
char buff[64];
String *res, tmp(buff,sizeof(buff), &my_charset_bin);
res= val_str(&tmp);
if (!res)
return 0;
(void)str2my_decimal(E_DEC_FATAL_ERROR, (char*) res->ptr(),
res->length(), res->charset(), decimal_value);
return decimal_value;
}
double Item_str_func::val_real()
{
DBUG_ASSERT(fixed == 1);
int err_not_used;
char *end_not_used, buff[64];
String *res, tmp(buff,sizeof(buff), &my_charset_bin);
res= val_str(&tmp);
return res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
&end_not_used, &err_not_used) : 0.0;
}
longlong Item_str_func::val_int()
{
DBUG_ASSERT(fixed == 1);
int err;
char buff[22];
String *res, tmp(buff,sizeof(buff), &my_charset_bin);
res= val_str(&tmp);
return (res ?
my_strntoll(res->charset(), res->ptr(), res->length(), 10, NULL,
&err) :
(longlong) 0);
}
String *Item_func_md5::val_str_ascii(String *str)
{
DBUG_ASSERT(fixed == 1);
String * sptr= args[0]->val_str(str);
str->set_charset(&my_charset_bin);
if (sptr)
{
uchar digest[MD5_HASH_SIZE];
null_value=0;
compute_md5_hash((char *) digest, (const char *) sptr->ptr(), sptr->length());
if (str->alloc(32)) // Ensure that memory is free
{
null_value=1;
return 0;
}
array_to_hex((char *) str->ptr(), digest, MD5_HASH_SIZE);
str->length((uint) 32);
return str;
}
null_value=1;
return 0;
}
String *Item_func_sha::val_str_ascii(String *str)
{
DBUG_ASSERT(fixed == 1);
String * sptr= args[0]->val_str(str);
str->set_charset(&my_charset_bin);
if (sptr) /* If we got value different from NULL */
{
/* Temporary buffer to store 160bit digest */
uint8 digest[SHA1_HASH_SIZE];
compute_sha1_hash(digest, (const char *) sptr->ptr(), sptr->length());
/* Ensure that memory is free */
if (!(str->alloc(SHA1_HASH_SIZE * 2)))
{
array_to_hex((char *) str->ptr(), digest, SHA1_HASH_SIZE);
str->length((uint) SHA1_HASH_SIZE*2);
null_value=0;
return str;
}
}
null_value=1;
return 0;
}
String *Item_func_sha2::val_str_ascii(String *str)
{
DBUG_ASSERT(fixed == 1);
#if defined(HAVE_OPENSSL) && !defined(EMBEDDED_LIBRARY)
unsigned char digest_buf[SHA512_DIGEST_LENGTH];
String *input_string;
unsigned char *input_ptr;
size_t input_len;
uint digest_length= 0;
str->set_charset(&my_charset_bin);
input_string= args[0]->val_str(str);
if (input_string == NULL)
{
null_value= TRUE;
return (String *) NULL;
}
null_value= args[0]->null_value;
if (null_value)
return (String *) NULL;
input_ptr= (unsigned char *) input_string->ptr();
input_len= input_string->length();
switch ((uint) args[1]->val_int()) {
#ifndef OPENSSL_NO_SHA512
case 512:
digest_length= SHA512_DIGEST_LENGTH;
(void) SHA512(input_ptr, input_len, digest_buf);
break;
case 384:
digest_length= SHA384_DIGEST_LENGTH;
(void) SHA384(input_ptr, input_len, digest_buf);
break;
#endif
#ifndef OPENSSL_NO_SHA256
case 224:
digest_length= SHA224_DIGEST_LENGTH;
(void) SHA224(input_ptr, input_len, digest_buf);
break;
case 256:
case 0: // SHA-256 is the default
digest_length= SHA256_DIGEST_LENGTH;
(void) SHA256(input_ptr, input_len, digest_buf);
break;
#endif
default:
if (!args[1]->const_item())
push_warning_printf(current_thd,
Sql_condition::WARN_LEVEL_WARN,
ER_WRONG_PARAMETERS_TO_NATIVE_FCT,
ER(ER_WRONG_PARAMETERS_TO_NATIVE_FCT), "sha2");
null_value= TRUE;
return NULL;
}
/*
Since we're subverting the usual String methods, we must make sure that
the destination has space for the bytes we're about to write.
*/
str->realloc((uint) digest_length*2 + 1); /* Each byte as two nybbles */
/* Convert the large number to a string-hex representation. */
array_to_hex((char *) str->ptr(), digest_buf, digest_length);
/* We poked raw bytes in. We must inform the the String of its length. */
str->length((uint) digest_length*2); /* Each byte as two nybbles */
null_value= FALSE;
return str;
#else
push_warning_printf(current_thd,
Sql_condition::WARN_LEVEL_WARN,
ER_FEATURE_DISABLED,
ER(ER_FEATURE_DISABLED),
"sha2", "--with-ssl");
null_value= TRUE;
return (String *) NULL;
#endif /* defined(HAVE_OPENSSL) && !defined(EMBEDDED_LIBRARY) */
}
/* Implementation of AES encryption routines */
/** helper class to process an IV argument to aes_encrypt/aes_decrypt */
class iv_argument
{
char iv_buff[MY_AES_IV_SIZE + 1]; // +1 to cater for the terminating NULL
String tmp_iv_value;
public:
iv_argument() :
tmp_iv_value(iv_buff, sizeof(iv_buff), system_charset_info)
{}
};
String *Item_func_aes_encrypt::val_str(String *str)
{
null_value=1;
return NULL;
}
String *Item_func_aes_decrypt::val_str(String *str)
{
// Bad parameters. No memory or bad data will all go here
null_value=1;
return NULL;
}
String *Item_func_random_bytes::val_str(String *a)
{
return NULL;
}
String *Item_func_to_base64::val_str_ascii(String *str)
{
String *res= args[0]->val_str(str);
bool too_long= false;
int length;
if (!res ||
res->length() > (uint) base64_encode_max_arg_length() ||
(too_long=
((uint) (length= base64_needed_encoded_length((int) res->length())) >
current_thd->variables.max_allowed_packet)) ||
tmp_value.alloc((uint) length))
{
null_value= 1; // NULL input, too long input, or OOM.
if (too_long)
{
push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
current_thd->variables.max_allowed_packet);
}
return 0;
}
base64_encode(res->ptr(), (int) res->length(), (char*) tmp_value.ptr());
DBUG_ASSERT(length > 0);
tmp_value.length((uint) length - 1); // Without trailing '\0'
null_value= 0;
return &tmp_value;
}
String *Item_func_from_base64::val_str(String *str)
{
String *res= args[0]->val_str_ascii(str);
bool too_long= false;
int length;
const char *end_ptr;
if (!res ||
res->length() > (uint) base64_decode_max_arg_length() ||
(too_long=
((uint) (length= base64_needed_decoded_length((int) res->length())) >
current_thd->variables.max_allowed_packet)) ||
tmp_value.alloc((uint) length) ||
(length= base64_decode(res->ptr(), (int) res->length(),
(char *) tmp_value.ptr(), &end_ptr, 0)) < 0 ||
end_ptr < res->ptr() + res->length())
{
null_value= 1; // NULL input, too long input, OOM, or badly formed input
if (too_long)
{
push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
current_thd->variables.max_allowed_packet);
}
return 0;
}
tmp_value.length((uint) length);
null_value= 0;
return &tmp_value;
}
/**
Concatenate args with the following premises:
If only one arg (which is ok), return value of arg;
Don't reallocate val_str() if not absolute necessary.
*/
String *Item_func_concat::val_str(String *str)
{
DBUG_ASSERT(fixed == 1);
String *res,*res2,*use_as_buff;
uint i;
bool is_const= 0;
null_value=0;
if (!(res=args[0]->val_str(str)))
goto null;
use_as_buff= &tmp_value;
/* Item_subselect in --ps-protocol mode will state it as a non-const */
is_const= args[0]->const_item() || !args[0]->used_tables();
for (i=1 ; i < arg_count ; i++)
{
if (res->length() == 0)
{
if (!(res=args[i]->val_str(str)))
goto null;
/*
CONCAT accumulates its result in the result of its the first
non-empty argument. Because of this we need is_const to be
evaluated only for it.
*/
is_const= args[i]->const_item() || !args[i]->used_tables();
}
else
{
if (!(res2=args[i]->val_str(use_as_buff)))
goto null;
if (res2->length() == 0)
continue;
if (res->length()+res2->length() >
current_thd->variables.max_allowed_packet)
{
push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
current_thd->variables.max_allowed_packet);
goto null;
}
if (!is_const && res->alloced_length() >= res->length()+res2->length())
{ // Use old buffer
res->append(*res2);
}
else if (str->alloced_length() >= res->length()+res2->length())
{
if (str->ptr() == res2->ptr())
str->replace(0,0,*res);
else
{
str->copy(*res);
str->append(*res2);
}
res= str;
use_as_buff= &tmp_value;
}
else if (res == &tmp_value)
{
if (res->append(*res2)) // Must be a blob
goto null;
}
else if (res2 == &tmp_value)
{ // This can happend only 1 time
if (tmp_value.replace(0,0,*res))
goto null;
res= &tmp_value;
use_as_buff=str; // Put next arg here
}
else if (tmp_value.is_alloced() && res2->ptr() >= tmp_value.ptr() &&
res2->ptr() <= tmp_value.ptr() + tmp_value.alloced_length())
{
/*
This happens really seldom:
In this case res2 is sub string of tmp_value. We will
now work in place in tmp_value to set it to res | res2
*/
/* Chop the last characters in tmp_value that isn't in res2 */
tmp_value.length((uint32) (res2->ptr() - tmp_value.ptr()) +
res2->length());
/* Place res2 at start of tmp_value, remove chars before res2 */
if (tmp_value.replace(0,(uint32) (res2->ptr() - tmp_value.ptr()),
*res))
goto null;
res= &tmp_value;
use_as_buff=str; // Put next arg here
}
else
{ // Two big const strings
/*
NOTE: We should be prudent in the initial allocation unit -- the
size of the arguments is a function of data distribution, which
can be any. Instead of overcommitting at the first row, we grow
the allocated amount by the factor of 2. This ensures that no
more than 25% of memory will be overcommitted on average.
*/
uint concat_len= res->length() + res2->length();
if (tmp_value.alloced_length() < concat_len)
{
if (tmp_value.alloced_length() == 0)
{
if (tmp_value.alloc(concat_len))
goto null;
}
else
{
uint new_len = max(tmp_value.alloced_length() * 2, concat_len);
if (tmp_value.realloc(new_len))
goto null;
}
}
if (tmp_value.copy(*res) || tmp_value.append(*res2))
goto null;
res= &tmp_value;
use_as_buff=str;
}
is_const= 0;
}
}
res->set_charset(collation.collation);
return res;
null:
null_value=1;
return 0;
}
/**
@details
Function des_encrypt() by tonu@spam.ee & monty
Works only if compiled with OpenSSL library support.
@return
A binary string where first character is CHAR(128 | key-number).
If one uses a string key key_number is 127.
Encryption result is longer than original by formula:
@code new_length= org_length + (8-(org_length % 8))+1 @endcode
*/
String *Item_func_des_encrypt::val_str(String *str)
{
null_value=1;
return 0;
}
String *Item_func_des_decrypt::val_str(String *str)
{
null_value=1;
return 0;
}
/**
concat with separator. First arg is the separator
concat_ws takes at least two arguments.
*/
String *Item_func_concat_ws::val_str(String *str)
{
DBUG_ASSERT(fixed == 1);
char tmp_str_buff[10];
String tmp_sep_str(tmp_str_buff, sizeof(tmp_str_buff),default_charset_info),
*sep_str, *res, *res2,*use_as_buff;
uint i;
bool is_const= 0;
null_value=0;
if (!(sep_str= args[0]->val_str(&tmp_sep_str)))
goto null;
use_as_buff= &tmp_value;
str->length(0); // QQ; Should be removed
res=str;
// Skip until non-null argument is found.
// If not, return the empty string
for (i=1; i < arg_count; i++)
if ((res= args[i]->val_str(str)))
{
is_const= args[i]->const_item() || !args[i]->used_tables();
break;
}
if (i == arg_count)
return make_empty_result();
for (i++; i < arg_count ; i++)
{
if (!(res2= args[i]->val_str(use_as_buff)))
continue; // Skip NULL
if (res->length() + sep_str->length() + res2->length() >
current_thd->variables.max_allowed_packet)
{
push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
current_thd->variables.max_allowed_packet);
goto null;
}
if (!is_const && res->alloced_length() >=
res->length() + sep_str->length() + res2->length())
{ // Use old buffer
res->append(*sep_str); // res->length() > 0 always
res->append(*res2);
}
else if (str->alloced_length() >=
res->length() + sep_str->length() + res2->length())
{
/* We have room in str; We can't get any errors here */
if (str->ptr() == res2->ptr())
{ // This is quite uncommon!
str->replace(0,0,*sep_str);
str->replace(0,0,*res);
}
else
{
str->copy(*res);
str->append(*sep_str);
str->append(*res2);
}
res=str;
use_as_buff= &tmp_value;
}
else if (res == &tmp_value)
{
if (res->append(*sep_str) || res->append(*res2))
goto null; // Must be a blob
}
else if (res2 == &tmp_value)
{ // This can happend only 1 time
if (tmp_value.replace(0,0,*sep_str) || tmp_value.replace(0,0,*res))
goto null;
res= &tmp_value;
use_as_buff=str; // Put next arg here
}
else if (tmp_value.is_alloced() && res2->ptr() >= tmp_value.ptr() &&
res2->ptr() < tmp_value.ptr() + tmp_value.alloced_length())
{
/*
This happens really seldom:
In this case res2 is sub string of tmp_value. We will
now work in place in tmp_value to set it to res | sep_str | res2
*/
/* Chop the last characters in tmp_value that isn't in res2 */
tmp_value.length((uint32) (res2->ptr() - tmp_value.ptr()) +
res2->length());
/* Place res2 at start of tmp_value, remove chars before res2 */
if (tmp_value.replace(0,(uint32) (res2->ptr() - tmp_value.ptr()),
*res) ||
tmp_value.replace(res->length(),0, *sep_str))
goto null;
res= &tmp_value;
use_as_buff=str; // Put next arg here
}
else
{ // Two big const strings
/*
NOTE: We should be prudent in the initial allocation unit -- the
size of the arguments is a function of data distribution, which can
be any. Instead of overcommitting at the first row, we grow the
allocated amount by the factor of 2. This ensures that no more than
25% of memory will be overcommitted on average.
*/
uint concat_len= res->length() + sep_str->length() + res2->length();
if (tmp_value.alloced_length() < concat_len)
{
if (tmp_value.alloced_length() == 0)
{
if (tmp_value.alloc(concat_len))
goto null;
}
else
{
uint new_len = max(tmp_value.alloced_length() * 2, concat_len);
if (tmp_value.realloc(new_len))
goto null;
}
}
if (tmp_value.copy(*res) ||
tmp_value.append(*sep_str) ||
tmp_value.append(*res2))
goto null;
res= &tmp_value;
use_as_buff=str;
}
}
res->set_charset(collation.collation);
return res;
null:
null_value=1;
return 0;
}
String *Item_func_reverse::val_str(String *str)
{
DBUG_ASSERT(fixed == 1);
String *res = args[0]->val_str(str);
char *ptr, *end, *tmp;
if ((null_value=args[0]->null_value))
return 0;
/* An empty string is a special case as the string pointer may be null */
if (!res->length())
return make_empty_result();
if (tmp_value.alloced_length() < res->length() &&
tmp_value.realloc(res->length()))
{
null_value= 1;
return 0;
}
tmp_value.length(res->length());
tmp_value.set_charset(res->charset());
ptr= (char *) res->ptr();
end= ptr + res->length();
tmp= (char *) tmp_value.ptr() + tmp_value.length();
#ifdef USE_MB
if (use_mb(res->charset()))
{
register uint32 l;
while (ptr < end)
{
if ((l= my_ismbchar(res->charset(),ptr,end)))
{
tmp-= l;
DBUG_ASSERT(tmp >= tmp_value.ptr());
memcpy(tmp,ptr,l);
ptr+= l;
}
else
*--tmp= *ptr++;
}
}
else
#endif /* USE_MB */
{
while (ptr < end)
*--tmp= *ptr++;
}
return &tmp_value;
}
/**
Replace all occurences of string2 in string1 with string3.
Don't reallocate val_str() if not needed.
@todo
Fix that this works with binary strings when using USE_MB
*/
String *Item_func_replace::val_str(String *str)
{
DBUG_ASSERT(fixed == 1);
String *res,*res2,*res3;
int offset;
uint from_length,to_length;
bool alloced=0;
#ifdef USE_MB
const char *ptr,*end,*strend,*search,*search_end;
register uint32 l;
bool binary_cmp;
#endif
null_value=0;
res=args[0]->val_str(str);
if (args[0]->null_value)
goto null;
res2=args[1]->val_str(&tmp_value);
if (args[1]->null_value)
goto null;
res->set_charset(collation.collation);
#ifdef USE_MB
binary_cmp = ((res->charset()->state & MY_CS_BINSORT) || !use_mb(res->charset()));
#endif
if (res2->length() == 0)
return res;
#ifndef USE_MB
if ((offset=res->strstr(*res2)) < 0)
return res;
#else
offset=0;
if (binary_cmp && (offset=res->strstr(*res2)) < 0)
return res;
#endif
if (!(res3=args[2]->val_str(&tmp_value2)))
goto null;
from_length= res2->length();
to_length= res3->length();
#ifdef USE_MB
if (!binary_cmp)
{
search=res2->ptr();
search_end=search+from_length;
redo:
DBUG_ASSERT(res->ptr() || !offset);
ptr=res->ptr()+offset;
strend=res->ptr()+res->length();
/*
In some cases val_str() can return empty string
with ptr() == NULL and length() == 0.
Let's check strend to avoid overflow.
*/
end= strend ? strend - from_length + 1 : NULL;
while (ptr < end)
{
if (*ptr == *search)
{
register char *i,*j;
i=(char*) ptr+1; j=(char*) search+1;
while (j != search_end)
if (*i++ != *j++) goto skip;
offset= (int) (ptr-res->ptr());
if (res->length()-from_length + to_length >
current_thd->variables.max_allowed_packet)
{
push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
func_name(),
current_thd->variables.max_allowed_packet);
goto null;
}
if (!alloced)
{
alloced=1;
res=copy_if_not_alloced(str,res,res->length()+to_length);
}
res->replace((uint) offset,from_length,*res3);
offset+=(int) to_length;
goto redo;
}
skip:
if ((l=my_ismbchar(res->charset(), ptr,strend))) ptr+=l;
else ++ptr;
}
}
else
#endif /* USE_MB */
do
{
if (res->length()-from_length + to_length >
current_thd->variables.max_allowed_packet)
{
push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
current_thd->variables.max_allowed_packet);
goto null;
}
if (!alloced)
{
alloced=1;
res=copy_if_not_alloced(str,res,res->length()+to_length);
}
res->replace((uint) offset,from_length,*res3);
offset+=(int) to_length;
}
while ((offset=res->strstr(*res2,(uint) offset)) >= 0);
return res;
null:
null_value=1;
return 0;
}
String *Item_func_insert::val_str(String *str)
{
DBUG_ASSERT(fixed == 1);
String *res,*res2;
longlong start, length; /* must be longlong to avoid truncation */
null_value=0;
res=args[0]->val_str(str);
res2=args[3]->val_str(&tmp_value);
start= args[1]->val_int() - 1;
length= args[2]->val_int();
if (args[0]->null_value || args[1]->null_value || args[2]->null_value ||
args[3]->null_value)
goto null; /* purecov: inspected */
if ((start < 0) || (start > res->length()))
return res; // Wrong param; skip insert
if ((length < 0) || (length > res->length()))
length= res->length();
/*
There is one exception not handled (intentionaly) by the character set
aggregation code. If one string is strong side and is binary, and
another one is weak side and is a multi-byte character string,
then we need to operate on the second string in terms on bytes when
calling ::numchars() and ::charpos(), rather than in terms of characters.
Lets substitute its character set to binary.
*/
if (collation.collation == &my_charset_bin)
{
res->set_charset(&my_charset_bin);
res2->set_charset(&my_charset_bin);
}
/* start and length are now sufficiently valid to pass to charpos function */
start= res->charpos((int) start);
length= res->charpos((int) length, (uint32) start);
/* Re-testing with corrected params */
if (start > res->length())
return res; /* purecov: inspected */ // Wrong param; skip insert
if (length > res->length() - start)
length= res->length() - start;
if ((ulonglong) (res->length() - length + res2->length()) >
(ulonglong) current_thd->variables.max_allowed_packet)
{
push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
func_name(), current_thd->variables.max_allowed_packet);
goto null;
}
res=copy_if_not_alloced(str,res,res->length());
res->replace((uint32) start,(uint32) length,*res2);
return res;
null:
null_value=1;
return 0;
}
String *Item_str_conv::val_str(String *str)
{
DBUG_ASSERT(fixed == 1);
String *res;
if (!(res=args[0]->val_str(str)))
{
null_value=1; /* purecov: inspected */
return 0; /* purecov: inspected */
}
null_value=0;
if (multiply == 1)
{
uint len;
res= copy_if_not_alloced(str,res,res->length());
len= converter(collation.collation, (char*) res->ptr(), res->length(),
(char*) res->ptr(), res->length());
DBUG_ASSERT(len <= res->length());
res->length(len);
}
else
{
uint len= res->length() * multiply;
tmp_value.alloc(len);
tmp_value.set_charset(collation.collation);
len= converter(collation.collation, (char*) res->ptr(), res->length(),
(char*) tmp_value.ptr(), len);
tmp_value.length(len);
res= &tmp_value;
}
return res;
}
String *Item_func_left::val_str(String *str)
{
DBUG_ASSERT(fixed == 1);
String *res= args[0]->val_str(str);
/* must be longlong to avoid truncation */
longlong length= args[1]->val_int();
uint char_pos;
if ((null_value=(args[0]->null_value || args[1]->null_value)))
return 0;
/* if "unsigned_flag" is set, we have a *huge* positive number. */
if ((length <= 0) && (!args[1]->unsigned_flag))
return make_empty_result();
if ((res->length() <= (ulonglong) length) ||
(res->length() <= (char_pos= res->charpos((int) length))))
return res;
tmp_value.set(*res, 0, char_pos);
return &tmp_value;
}
String *Item_func_right::val_str(String *str)
{
DBUG_ASSERT(fixed == 1);
String *res= args[0]->val_str(str);
/* must be longlong to avoid truncation */
longlong length= args[1]->val_int();
if ((null_value=(args[0]->null_value || args[1]->null_value)))
return 0; /* purecov: inspected */
/* if "unsigned_flag" is set, we have a *huge* positive number. */
if ((length <= 0) && (!args[1]->unsigned_flag))
return make_empty_result(); /* purecov: inspected */
if (res->length() <= (ulonglong) length)
return res; /* purecov: inspected */