You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3052 lines
76 KiB

26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
23 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
23 years ago
26 years ago
23 years ago
23 years ago
23 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
22 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
23 years ago
26 years ago
26 years ago
26 years ago
23 years ago
26 years ago
26 years ago
22 years ago
22 years ago
26 years ago
26 years ago
26 years ago
26 years ago
23 years ago
26 years ago
26 years ago
26 years ago
26 years ago
23 years ago
26 years ago
26 years ago
26 years ago
26 years ago
23 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
22 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
26 years ago
26 years ago
26 years ago
26 years ago
22 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
22 years ago
22 years ago
22 years ago
22 years ago
  1. /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  13. /* This file defines all string functions
  14. ** Warning: Some string functions doesn't always put and end-null on a String
  15. ** (This shouldn't be needed)
  16. */
  17. #ifdef USE_PRAGMA_IMPLEMENTATION
  18. #pragma implementation // gcc: Class implementation
  19. #endif
  20. #include "mysql_priv.h"
  21. #include <m_ctype.h>
  22. #ifdef HAVE_OPENSSL
  23. #include <openssl/des.h>
  24. #endif /* HAVE_OPENSSL */
  25. #include "md5.h"
  26. #include "sha1.h"
  27. #include "my_aes.h"
  28. C_MODE_START
  29. #include "../mysys/my_static.h" // For soundex_map
  30. C_MODE_END
  31. String my_empty_string("",default_charset_info);
  32. static void my_coll_agg_error(DTCollation &c1, DTCollation &c2,
  33. const char *fname)
  34. {
  35. my_error(ER_CANT_AGGREGATE_2COLLATIONS,MYF(0),
  36. c1.collation->name,c1.derivation_name(),
  37. c2.collation->name,c2.derivation_name(),
  38. fname);
  39. }
  40. uint nr_of_decimals(const char *str)
  41. {
  42. if ((str=strchr(str,'.')))
  43. {
  44. const char *start= ++str;
  45. for (; my_isdigit(system_charset_info,*str) ; str++) ;
  46. return (uint) (str-start);
  47. }
  48. return 0;
  49. }
  50. double Item_str_func::val()
  51. {
  52. DBUG_ASSERT(fixed == 1);
  53. int err;
  54. char buff[64];
  55. char *end_not_used;
  56. String *res, tmp(buff,sizeof(buff), &my_charset_bin);
  57. res= val_str(&tmp);
  58. return res ? my_strntod(res->charset(), (char*) res->ptr(),res->length(),
  59. &end_not_used, &err) : 0.0;
  60. }
  61. longlong Item_str_func::val_int()
  62. {
  63. DBUG_ASSERT(fixed == 1);
  64. int err;
  65. char buff[22];
  66. String *res, tmp(buff,sizeof(buff), &my_charset_bin);
  67. res= val_str(&tmp);
  68. return (res ?
  69. my_strntoll(res->charset(), res->ptr(), res->length(), 10, NULL,
  70. &err) :
  71. (longlong) 0);
  72. }
  73. String *Item_func_md5::val_str(String *str)
  74. {
  75. DBUG_ASSERT(fixed == 1);
  76. String * sptr= args[0]->val_str(str);
  77. str->set_charset(&my_charset_bin);
  78. if (sptr)
  79. {
  80. my_MD5_CTX context;
  81. unsigned char digest[16];
  82. null_value=0;
  83. my_MD5Init (&context);
  84. my_MD5Update (&context,(unsigned char *) sptr->ptr(), sptr->length());
  85. my_MD5Final (digest, &context);
  86. if (str->alloc(32)) // Ensure that memory is free
  87. {
  88. null_value=1;
  89. return 0;
  90. }
  91. sprintf((char *) str->ptr(),
  92. "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
  93. digest[0], digest[1], digest[2], digest[3],
  94. digest[4], digest[5], digest[6], digest[7],
  95. digest[8], digest[9], digest[10], digest[11],
  96. digest[12], digest[13], digest[14], digest[15]);
  97. str->length((uint) 32);
  98. return str;
  99. }
  100. null_value=1;
  101. return 0;
  102. }
  103. void Item_func_md5::fix_length_and_dec()
  104. {
  105. max_length=32;
  106. /*
  107. The MD5() function treats its parameter as being a case sensitive. Thus
  108. we set binary collation on it so different instances of MD5() will be
  109. compared properly.
  110. */
  111. args[0]->collation.set(
  112. get_charset_by_csname(args[0]->collation.collation->csname,
  113. MY_CS_BINSORT,MYF(0)), DERIVATION_COERCIBLE);
  114. }
  115. String *Item_func_sha::val_str(String *str)
  116. {
  117. DBUG_ASSERT(fixed == 1);
  118. String * sptr= args[0]->val_str(str);
  119. str->set_charset(&my_charset_bin);
  120. if (sptr) /* If we got value different from NULL */
  121. {
  122. SHA1_CONTEXT context; /* Context used to generate SHA1 hash */
  123. /* Temporary buffer to store 160bit digest */
  124. uint8 digest[SHA1_HASH_SIZE];
  125. mysql_sha1_reset(&context); /* We do not have to check for error here */
  126. /* No need to check error as the only case would be too long message */
  127. mysql_sha1_input(&context,
  128. (const unsigned char *) sptr->ptr(), sptr->length());
  129. /* Ensure that memory is free and we got result */
  130. if (!( str->alloc(SHA1_HASH_SIZE*2) ||
  131. (mysql_sha1_result(&context,digest))))
  132. {
  133. sprintf((char *) str->ptr(),
  134. "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\
  135. %02x%02x%02x%02x%02x%02x%02x%02x",
  136. digest[0], digest[1], digest[2], digest[3],
  137. digest[4], digest[5], digest[6], digest[7],
  138. digest[8], digest[9], digest[10], digest[11],
  139. digest[12], digest[13], digest[14], digest[15],
  140. digest[16], digest[17], digest[18], digest[19]);
  141. str->length((uint) SHA1_HASH_SIZE*2);
  142. null_value=0;
  143. return str;
  144. }
  145. }
  146. null_value=1;
  147. return 0;
  148. }
  149. void Item_func_sha::fix_length_and_dec()
  150. {
  151. max_length=SHA1_HASH_SIZE*2; // size of hex representation of hash
  152. /*
  153. The SHA() function treats its parameter as being a case sensitive. Thus
  154. we set binary collation on it so different instances of MD5() will be
  155. compared properly.
  156. */
  157. args[0]->collation.set(
  158. get_charset_by_csname(args[0]->collation.collation->csname,
  159. MY_CS_BINSORT,MYF(0)), DERIVATION_COERCIBLE);
  160. }
  161. /* Implementation of AES encryption routines */
  162. String *Item_func_aes_encrypt::val_str(String *str)
  163. {
  164. DBUG_ASSERT(fixed == 1);
  165. char key_buff[80];
  166. String tmp_key_value(key_buff, sizeof(key_buff), system_charset_info);
  167. String *sptr= args[0]->val_str(str); // String to encrypt
  168. String *key= args[1]->val_str(&tmp_key_value); // key
  169. int aes_length;
  170. if (sptr && key) // we need both arguments to be not NULL
  171. {
  172. null_value=0;
  173. aes_length=my_aes_get_size(sptr->length()); // Calculate result length
  174. if (!str_value.alloc(aes_length)) // Ensure that memory is free
  175. {
  176. // finally encrypt directly to allocated buffer.
  177. if (my_aes_encrypt(sptr->ptr(),sptr->length(), (char*) str_value.ptr(),
  178. key->ptr(), key->length()) == aes_length)
  179. {
  180. // We got the expected result length
  181. str_value.length((uint) aes_length);
  182. return &str_value;
  183. }
  184. }
  185. }
  186. null_value=1;
  187. return 0;
  188. }
  189. void Item_func_aes_encrypt::fix_length_and_dec()
  190. {
  191. max_length=my_aes_get_size(args[0]->max_length);
  192. }
  193. String *Item_func_aes_decrypt::val_str(String *str)
  194. {
  195. DBUG_ASSERT(fixed == 1);
  196. char key_buff[80];
  197. String tmp_key_value(key_buff, sizeof(key_buff), system_charset_info);
  198. String *sptr, *key;
  199. DBUG_ENTER("Item_func_aes_decrypt::val_str");
  200. sptr= args[0]->val_str(str); // String to decrypt
  201. key= args[1]->val_str(&tmp_key_value); // Key
  202. if (sptr && key) // Need to have both arguments not NULL
  203. {
  204. null_value=0;
  205. if (!str_value.alloc(sptr->length())) // Ensure that memory is free
  206. {
  207. // finally decrypt directly to allocated buffer.
  208. int length;
  209. length=my_aes_decrypt(sptr->ptr(), sptr->length(),
  210. (char*) str_value.ptr(),
  211. key->ptr(), key->length());
  212. if (length >= 0) // if we got correct data data
  213. {
  214. str_value.length((uint) length);
  215. DBUG_RETURN(&str_value);
  216. }
  217. }
  218. }
  219. // Bad parameters. No memory or bad data will all go here
  220. null_value=1;
  221. DBUG_RETURN(0);
  222. }
  223. void Item_func_aes_decrypt::fix_length_and_dec()
  224. {
  225. max_length=args[0]->max_length;
  226. maybe_null= 1;
  227. }
  228. /*
  229. Concatenate args with the following premises:
  230. If only one arg (which is ok), return value of arg
  231. Don't reallocate val_str() if not absolute necessary.
  232. */
  233. String *Item_func_concat::val_str(String *str)
  234. {
  235. DBUG_ASSERT(fixed == 1);
  236. String *res,*res2,*use_as_buff;
  237. uint i;
  238. bool is_const= 0;
  239. null_value=0;
  240. if (!(res=args[0]->val_str(str)))
  241. goto null;
  242. use_as_buff= &tmp_value;
  243. /* Item_subselect in --ps-protocol mode will state it as a non-const */
  244. is_const= args[0]->const_item() || !args[0]->used_tables();
  245. for (i=1 ; i < arg_count ; i++)
  246. {
  247. if (res->length() == 0)
  248. {
  249. if (!(res=args[i]->val_str(str)))
  250. goto null;
  251. }
  252. else
  253. {
  254. if (!(res2=args[i]->val_str(use_as_buff)))
  255. goto null;
  256. if (res2->length() == 0)
  257. continue;
  258. if (res->length()+res2->length() >
  259. current_thd->variables.max_allowed_packet)
  260. {
  261. push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
  262. ER_WARN_ALLOWED_PACKET_OVERFLOWED,
  263. ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
  264. current_thd->variables.max_allowed_packet);
  265. goto null;
  266. }
  267. if (!is_const && res->alloced_length() >= res->length()+res2->length())
  268. { // Use old buffer
  269. res->append(*res2);
  270. }
  271. else if (str->alloced_length() >= res->length()+res2->length())
  272. {
  273. if (str == res2)
  274. str->replace(0,0,*res);
  275. else
  276. {
  277. str->copy(*res);
  278. str->append(*res2);
  279. }
  280. res= str;
  281. use_as_buff= &tmp_value;
  282. }
  283. else if (res == &tmp_value)
  284. {
  285. if (res->append(*res2)) // Must be a blob
  286. goto null;
  287. }
  288. else if (res2 == &tmp_value)
  289. { // This can happend only 1 time
  290. if (tmp_value.replace(0,0,*res))
  291. goto null;
  292. res= &tmp_value;
  293. use_as_buff=str; // Put next arg here
  294. }
  295. else if (tmp_value.is_alloced() && res2->ptr() >= tmp_value.ptr() &&
  296. res2->ptr() <= tmp_value.ptr() + tmp_value.alloced_length())
  297. {
  298. /*
  299. This happens really seldom:
  300. In this case res2 is sub string of tmp_value. We will
  301. now work in place in tmp_value to set it to res | res2
  302. */
  303. /* Chop the last characters in tmp_value that isn't in res2 */
  304. tmp_value.length((uint32) (res2->ptr() - tmp_value.ptr()) +
  305. res2->length());
  306. /* Place res2 at start of tmp_value, remove chars before res2 */
  307. if (tmp_value.replace(0,(uint32) (res2->ptr() - tmp_value.ptr()),
  308. *res))
  309. goto null;
  310. res= &tmp_value;
  311. use_as_buff=str; // Put next arg here
  312. }
  313. else
  314. { // Two big const strings
  315. if (tmp_value.alloc(max_length) ||
  316. tmp_value.copy(*res) ||
  317. tmp_value.append(*res2))
  318. goto null;
  319. res= &tmp_value;
  320. use_as_buff=str;
  321. }
  322. is_const= 0;
  323. }
  324. }
  325. res->set_charset(collation.collation);
  326. return res;
  327. null:
  328. null_value=1;
  329. return 0;
  330. }
  331. void Item_func_concat::fix_length_and_dec()
  332. {
  333. ulonglong max_result_length= 0;
  334. if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV))
  335. return;
  336. for (uint i=0 ; i < arg_count ; i++)
  337. max_result_length+= args[i]->max_length;
  338. if (max_result_length >= MAX_BLOB_WIDTH)
  339. {
  340. max_result_length= MAX_BLOB_WIDTH;
  341. maybe_null= 1;
  342. }
  343. max_length= (ulong) max_result_length;
  344. }
  345. /*
  346. Function des_encrypt() by tonu@spam.ee & monty
  347. Works only if compiled with OpenSSL library support.
  348. This returns a binary string where first character is CHAR(128 | key-number).
  349. If one uses a string key key_number is 127.
  350. Encryption result is longer than original by formula:
  351. new_length= org_length + (8-(org_length % 8))+1
  352. */
  353. String *Item_func_des_encrypt::val_str(String *str)
  354. {
  355. DBUG_ASSERT(fixed == 1);
  356. #ifdef HAVE_OPENSSL
  357. uint code= ER_WRONG_PARAMETERS_TO_PROCEDURE;
  358. DES_cblock ivec;
  359. struct st_des_keyblock keyblock;
  360. struct st_des_keyschedule keyschedule;
  361. const char *append_str="********";
  362. uint key_number, res_length, tail;
  363. String *res= args[0]->val_str(str);
  364. if ((null_value= args[0]->null_value))
  365. return 0; // ENCRYPT(NULL) == NULL
  366. if ((res_length=res->length()) == 0)
  367. return &my_empty_string;
  368. if (arg_count == 1)
  369. {
  370. /* Protect against someone doing FLUSH DES_KEY_FILE */
  371. VOID(pthread_mutex_lock(&LOCK_des_key_file));
  372. keyschedule= des_keyschedule[key_number=des_default_key];
  373. VOID(pthread_mutex_unlock(&LOCK_des_key_file));
  374. }
  375. else if (args[1]->result_type() == INT_RESULT)
  376. {
  377. key_number= (uint) args[1]->val_int();
  378. if (key_number > 9)
  379. goto error;
  380. VOID(pthread_mutex_lock(&LOCK_des_key_file));
  381. keyschedule= des_keyschedule[key_number];
  382. VOID(pthread_mutex_unlock(&LOCK_des_key_file));
  383. }
  384. else
  385. {
  386. String *keystr=args[1]->val_str(&tmp_value);
  387. if (!keystr)
  388. goto error;
  389. key_number=127; // User key string
  390. /* We make good 24-byte (168 bit) key from given plaintext key with MD5 */
  391. bzero((char*) &ivec,sizeof(ivec));
  392. EVP_BytesToKey(EVP_des_ede3_cbc(),EVP_md5(),NULL,
  393. (uchar*) keystr->ptr(), (int) keystr->length(),
  394. 1, (uchar*) &keyblock,ivec);
  395. DES_set_key_unchecked(&keyblock.key1,&keyschedule.ks1);
  396. DES_set_key_unchecked(&keyblock.key2,&keyschedule.ks2);
  397. DES_set_key_unchecked(&keyblock.key3,&keyschedule.ks3);
  398. }
  399. /*
  400. The problem: DES algorithm requires original data to be in 8-bytes
  401. chunks. Missing bytes get filled with '*'s and result of encryption
  402. can be up to 8 bytes longer than original string. When decrypted,
  403. we do not know the size of original string :(
  404. We add one byte with value 0x1..0x8 as the last byte of the padded
  405. string marking change of string length.
  406. */
  407. tail= (8-(res_length) % 8); // 1..8 marking extra length
  408. res_length+=tail;
  409. code= ER_OUT_OF_RESOURCES;
  410. if (tail && res->append(append_str, tail) || tmp_value.alloc(res_length+1))
  411. goto error;
  412. (*res)[res_length-1]=tail; // save extra length
  413. tmp_value.length(res_length+1);
  414. tmp_value[0]=(char) (128 | key_number);
  415. // Real encryption
  416. bzero((char*) &ivec,sizeof(ivec));
  417. DES_ede3_cbc_encrypt((const uchar*) (res->ptr()),
  418. (uchar*) (tmp_value.ptr()+1),
  419. res_length,
  420. &keyschedule.ks1,
  421. &keyschedule.ks2,
  422. &keyschedule.ks3,
  423. &ivec, TRUE);
  424. return &tmp_value;
  425. error:
  426. push_warning_printf(current_thd,MYSQL_ERROR::WARN_LEVEL_ERROR,
  427. code, ER(code),
  428. "des_encrypt");
  429. #else
  430. push_warning_printf(current_thd,MYSQL_ERROR::WARN_LEVEL_ERROR,
  431. ER_FEATURE_DISABLED, ER(ER_FEATURE_DISABLED),
  432. "des_encrypt","--with-openssl");
  433. #endif /* HAVE_OPENSSL */
  434. null_value=1;
  435. return 0;
  436. }
  437. String *Item_func_des_decrypt::val_str(String *str)
  438. {
  439. DBUG_ASSERT(fixed == 1);
  440. #ifdef HAVE_OPENSSL
  441. uint code= ER_WRONG_PARAMETERS_TO_PROCEDURE;
  442. DES_key_schedule ks1, ks2, ks3;
  443. DES_cblock ivec;
  444. struct st_des_keyblock keyblock;
  445. struct st_des_keyschedule keyschedule;
  446. String *res= args[0]->val_str(str);
  447. uint length,tail;
  448. if ((null_value= args[0]->null_value))
  449. return 0;
  450. length= res->length();
  451. if (length < 9 || (length % 8) != 1 || !((*res)[0] & 128))
  452. return res; // Skip decryption if not encrypted
  453. if (arg_count == 1) // If automatic uncompression
  454. {
  455. uint key_number=(uint) (*res)[0] & 127;
  456. // Check if automatic key and that we have privilege to uncompress using it
  457. if (!(current_thd->master_access & SUPER_ACL) || key_number > 9)
  458. goto error;
  459. VOID(pthread_mutex_lock(&LOCK_des_key_file));
  460. keyschedule= des_keyschedule[key_number];
  461. VOID(pthread_mutex_unlock(&LOCK_des_key_file));
  462. }
  463. else
  464. {
  465. // We make good 24-byte (168 bit) key from given plaintext key with MD5
  466. String *keystr=args[1]->val_str(&tmp_value);
  467. if (!keystr)
  468. goto error;
  469. bzero((char*) &ivec,sizeof(ivec));
  470. EVP_BytesToKey(EVP_des_ede3_cbc(),EVP_md5(),NULL,
  471. (uchar*) keystr->ptr(),(int) keystr->length(),
  472. 1,(uchar*) &keyblock,ivec);
  473. // Here we set all 64-bit keys (56 effective) one by one
  474. DES_set_key_unchecked(&keyblock.key1,&keyschedule.ks1);
  475. DES_set_key_unchecked(&keyblock.key2,&keyschedule.ks2);
  476. DES_set_key_unchecked(&keyblock.key3,&keyschedule.ks3);
  477. }
  478. code= ER_OUT_OF_RESOURCES;
  479. if (tmp_value.alloc(length-1))
  480. goto error;
  481. bzero((char*) &ivec,sizeof(ivec));
  482. DES_ede3_cbc_encrypt((const uchar*) res->ptr()+1,
  483. (uchar*) (tmp_value.ptr()),
  484. length-1,
  485. &keyschedule.ks1,
  486. &keyschedule.ks2,
  487. &keyschedule.ks3,
  488. &ivec, FALSE);
  489. /* Restore old length of key */
  490. if ((tail=(uint) (uchar) tmp_value[length-2]) > 8)
  491. goto wrong_key; // Wrong key
  492. tmp_value.length(length-1-tail);
  493. return &tmp_value;
  494. error:
  495. push_warning_printf(current_thd,MYSQL_ERROR::WARN_LEVEL_ERROR,
  496. code, ER(code),
  497. "des_decrypt");
  498. wrong_key:
  499. #else
  500. push_warning_printf(current_thd,MYSQL_ERROR::WARN_LEVEL_ERROR,
  501. ER_FEATURE_DISABLED, ER(ER_FEATURE_DISABLED),
  502. "des_decrypt","--with-openssl");
  503. #endif /* HAVE_OPENSSL */
  504. null_value=1;
  505. return 0;
  506. }
  507. /*
  508. concat with separator. First arg is the separator
  509. concat_ws takes at least two arguments.
  510. */
  511. String *Item_func_concat_ws::val_str(String *str)
  512. {
  513. DBUG_ASSERT(fixed == 1);
  514. char tmp_str_buff[10];
  515. String tmp_sep_str(tmp_str_buff, sizeof(tmp_str_buff),default_charset_info),
  516. *sep_str, *res, *res2,*use_as_buff;
  517. uint i;
  518. null_value=0;
  519. if (!(sep_str= args[0]->val_str(&tmp_sep_str)))
  520. goto null;
  521. use_as_buff= &tmp_value;
  522. str->length(0); // QQ; Should be removed
  523. res=str;
  524. // Skip until non-null argument is found.
  525. // If not, return the empty string
  526. for (i=1; i < arg_count; i++)
  527. if ((res= args[i]->val_str(str)))
  528. break;
  529. if (i == arg_count)
  530. return &my_empty_string;
  531. for (i++; i < arg_count ; i++)
  532. {
  533. if (!(res2= args[i]->val_str(use_as_buff)))
  534. continue; // Skip NULL
  535. if (res->length() + sep_str->length() + res2->length() >
  536. current_thd->variables.max_allowed_packet)
  537. {
  538. push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
  539. ER_WARN_ALLOWED_PACKET_OVERFLOWED,
  540. ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
  541. current_thd->variables.max_allowed_packet);
  542. goto null;
  543. }
  544. if (res->alloced_length() >=
  545. res->length() + sep_str->length() + res2->length())
  546. { // Use old buffer
  547. res->append(*sep_str); // res->length() > 0 always
  548. res->append(*res2);
  549. }
  550. else if (str->alloced_length() >=
  551. res->length() + sep_str->length() + res2->length())
  552. {
  553. /* We have room in str; We can't get any errors here */
  554. if (str == res2)
  555. { // This is quote uncommon!
  556. str->replace(0,0,*sep_str);
  557. str->replace(0,0,*res);
  558. }
  559. else
  560. {
  561. str->copy(*res);
  562. str->append(*sep_str);
  563. str->append(*res2);
  564. }
  565. res=str;
  566. use_as_buff= &tmp_value;
  567. }
  568. else if (res == &tmp_value)
  569. {
  570. if (res->append(*sep_str) || res->append(*res2))
  571. goto null; // Must be a blob
  572. }
  573. else if (res2 == &tmp_value)
  574. { // This can happend only 1 time
  575. if (tmp_value.replace(0,0,*sep_str) || tmp_value.replace(0,0,*res))
  576. goto null;
  577. res= &tmp_value;
  578. use_as_buff=str; // Put next arg here
  579. }
  580. else if (tmp_value.is_alloced() && res2->ptr() >= tmp_value.ptr() &&
  581. res2->ptr() < tmp_value.ptr() + tmp_value.alloced_length())
  582. {
  583. /*
  584. This happens really seldom:
  585. In this case res2 is sub string of tmp_value. We will
  586. now work in place in tmp_value to set it to res | sep_str | res2
  587. */
  588. /* Chop the last characters in tmp_value that isn't in res2 */
  589. tmp_value.length((uint32) (res2->ptr() - tmp_value.ptr()) +
  590. res2->length());
  591. /* Place res2 at start of tmp_value, remove chars before res2 */
  592. if (tmp_value.replace(0,(uint32) (res2->ptr() - tmp_value.ptr()),
  593. *res) ||
  594. tmp_value.replace(res->length(),0, *sep_str))
  595. goto null;
  596. res= &tmp_value;
  597. use_as_buff=str; // Put next arg here
  598. }
  599. else
  600. { // Two big const strings
  601. if (tmp_value.alloc(max_length) ||
  602. tmp_value.copy(*res) ||
  603. tmp_value.append(*sep_str) ||
  604. tmp_value.append(*res2))
  605. goto null;
  606. res= &tmp_value;
  607. use_as_buff=str;
  608. }
  609. }
  610. res->set_charset(collation.collation);
  611. return res;
  612. null:
  613. null_value=1;
  614. return 0;
  615. }
  616. void Item_func_concat_ws::fix_length_and_dec()
  617. {
  618. ulonglong max_result_length;
  619. if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV))
  620. return;
  621. /*
  622. arg_count cannot be less than 2,
  623. it is done on parser level in sql_yacc.yy
  624. so, (arg_count - 2) is safe here.
  625. */
  626. max_result_length= (ulonglong) args[0]->max_length * (arg_count - 2);
  627. for (uint i=1 ; i < arg_count ; i++)
  628. max_result_length+=args[i]->max_length;
  629. if (max_result_length >= MAX_BLOB_WIDTH)
  630. {
  631. max_result_length= MAX_BLOB_WIDTH;
  632. maybe_null= 1;
  633. }
  634. max_length= (ulong) max_result_length;
  635. }
  636. String *Item_func_reverse::val_str(String *str)
  637. {
  638. DBUG_ASSERT(fixed == 1);
  639. String *res = args[0]->val_str(str);
  640. char *ptr, *end, *tmp;
  641. if ((null_value=args[0]->null_value))
  642. return 0;
  643. /* An empty string is a special case as the string pointer may be null */
  644. if (!res->length())
  645. return &my_empty_string;
  646. if (tmp_value.alloced_length() < res->length() &&
  647. tmp_value.realloc(res->length()))
  648. {
  649. null_value= 1;
  650. return 0;
  651. }
  652. tmp_value.length(res->length());
  653. tmp_value.set_charset(res->charset());
  654. ptr= (char *) res->ptr();
  655. end= ptr + res->length();
  656. tmp= (char *) tmp_value.ptr() + tmp_value.length();
  657. #ifdef USE_MB
  658. if (use_mb(res->charset()))
  659. {
  660. register uint32 l;
  661. while (ptr < end)
  662. {
  663. if ((l= my_ismbchar(res->charset(),ptr,end)))
  664. {
  665. tmp-= l;
  666. memcpy(tmp,ptr,l);
  667. ptr+= l;
  668. }
  669. else
  670. *--tmp= *ptr++;
  671. }
  672. }
  673. else
  674. #endif /* USE_MB */
  675. {
  676. while (ptr < end)
  677. *--tmp= *ptr++;
  678. }
  679. return &tmp_value;
  680. }
  681. void Item_func_reverse::fix_length_and_dec()
  682. {
  683. collation.set(args[0]->collation);
  684. max_length = args[0]->max_length;
  685. }
  686. /*
  687. ** Replace all occurences of string2 in string1 with string3.
  688. ** Don't reallocate val_str() if not needed
  689. */
  690. /* TODO: Fix that this works with binary strings when using USE_MB */
  691. String *Item_func_replace::val_str(String *str)
  692. {
  693. DBUG_ASSERT(fixed == 1);
  694. String *res,*res2,*res3;
  695. int offset;
  696. uint from_length,to_length;
  697. bool alloced=0;
  698. #ifdef USE_MB
  699. const char *ptr,*end,*strend,*search,*search_end;
  700. register uint32 l;
  701. bool binary_cmp;
  702. #endif
  703. null_value=0;
  704. res=args[0]->val_str(str);
  705. if (args[0]->null_value)
  706. goto null;
  707. res2=args[1]->val_str(&tmp_value);
  708. if (args[1]->null_value)
  709. goto null;
  710. res->set_charset(collation.collation);
  711. #ifdef USE_MB
  712. binary_cmp = ((res->charset()->state & MY_CS_BINSORT) || !use_mb(res->charset()));
  713. #endif
  714. if (res2->length() == 0)
  715. return res;
  716. #ifndef USE_MB
  717. if ((offset=res->strstr(*res2)) < 0)
  718. return res;
  719. #else
  720. offset=0;
  721. if (binary_cmp && (offset=res->strstr(*res2)) < 0)
  722. return res;
  723. #endif
  724. if (!(res3=args[2]->val_str(&tmp_value2)))
  725. goto null;
  726. from_length= res2->length();
  727. to_length= res3->length();
  728. #ifdef USE_MB
  729. if (!binary_cmp)
  730. {
  731. search=res2->ptr();
  732. search_end=search+from_length;
  733. redo:
  734. ptr=res->ptr()+offset;
  735. strend=res->ptr()+res->length();
  736. end=strend-from_length+1;
  737. while (ptr < end)
  738. {
  739. if (*ptr == *search)
  740. {
  741. register char *i,*j;
  742. i=(char*) ptr+1; j=(char*) search+1;
  743. while (j != search_end)
  744. if (*i++ != *j++) goto skip;
  745. offset= (int) (ptr-res->ptr());
  746. if (res->length()-from_length + to_length >
  747. current_thd->variables.max_allowed_packet)
  748. {
  749. push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
  750. ER_WARN_ALLOWED_PACKET_OVERFLOWED,
  751. ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
  752. func_name(),
  753. current_thd->variables.max_allowed_packet);
  754. goto null;
  755. }
  756. if (!alloced)
  757. {
  758. alloced=1;
  759. res=copy_if_not_alloced(str,res,res->length()+to_length);
  760. }
  761. res->replace((uint) offset,from_length,*res3);
  762. offset+=(int) to_length;
  763. goto redo;
  764. }
  765. skip:
  766. if ((l=my_ismbchar(res->charset(), ptr,strend))) ptr+=l;
  767. else ++ptr;
  768. }
  769. }
  770. else
  771. #endif /* USE_MB */
  772. do
  773. {
  774. if (res->length()-from_length + to_length >
  775. current_thd->variables.max_allowed_packet)
  776. {
  777. push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
  778. ER_WARN_ALLOWED_PACKET_OVERFLOWED,
  779. ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
  780. current_thd->variables.max_allowed_packet);
  781. goto null;
  782. }
  783. if (!alloced)
  784. {
  785. alloced=1;
  786. res=copy_if_not_alloced(str,res,res->length()+to_length);
  787. }
  788. res->replace((uint) offset,from_length,*res3);
  789. offset+=(int) to_length;
  790. }
  791. while ((offset=res->strstr(*res2,(uint) offset)) >= 0);
  792. return res;
  793. null:
  794. null_value=1;
  795. return 0;
  796. }
  797. void Item_func_replace::fix_length_and_dec()
  798. {
  799. ulonglong max_result_length= args[0]->max_length;
  800. int diff=(int) (args[2]->max_length - args[1]->max_length);
  801. if (diff > 0 && args[1]->max_length)
  802. { // Calculate of maxreplaces
  803. ulonglong max_substrs= max_result_length/args[1]->max_length;
  804. max_result_length+= max_substrs * (uint) diff;
  805. }
  806. if (max_result_length >= MAX_BLOB_WIDTH)
  807. {
  808. max_result_length= MAX_BLOB_WIDTH;
  809. maybe_null= 1;
  810. }
  811. max_length= (ulong) max_result_length;
  812. if (agg_arg_charsets(collation, args, 3, MY_COLL_CMP_CONV))
  813. return;
  814. }
  815. String *Item_func_insert::val_str(String *str)
  816. {
  817. DBUG_ASSERT(fixed == 1);
  818. String *res,*res2;
  819. uint start,length;
  820. null_value=0;
  821. res=args[0]->val_str(str);
  822. res2=args[3]->val_str(&tmp_value);
  823. start=(uint) args[1]->val_int()-1;
  824. length=(uint) args[2]->val_int();
  825. if (args[0]->null_value || args[1]->null_value || args[2]->null_value ||
  826. args[3]->null_value)
  827. goto null; /* purecov: inspected */
  828. start=res->charpos(start);
  829. length=res->charpos(length,start);
  830. if (start > res->length()+1)
  831. return res; // Wrong param; skip insert
  832. if (length > res->length()-start)
  833. length=res->length()-start;
  834. if (res->length() - length + res2->length() >
  835. current_thd->variables.max_allowed_packet)
  836. {
  837. push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
  838. ER_WARN_ALLOWED_PACKET_OVERFLOWED,
  839. ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
  840. func_name(), current_thd->variables.max_allowed_packet);
  841. goto null;
  842. }
  843. res=copy_if_not_alloced(str,res,res->length());
  844. res->replace(start,length,*res2);
  845. return res;
  846. null:
  847. null_value=1;
  848. return 0;
  849. }
  850. void Item_func_insert::fix_length_and_dec()
  851. {
  852. Item *cargs[2];
  853. ulonglong max_result_length;
  854. cargs[0]= args[0];
  855. cargs[1]= args[3];
  856. if (agg_arg_charsets(collation, cargs, 2, MY_COLL_ALLOW_CONV))
  857. return;
  858. args[0]= cargs[0];
  859. args[3]= cargs[1];
  860. max_result_length= ((ulonglong) args[0]->max_length+
  861. (ulonglong) args[3]->max_length);
  862. if (max_result_length >= MAX_BLOB_WIDTH)
  863. {
  864. max_result_length= MAX_BLOB_WIDTH;
  865. maybe_null= 1;
  866. }
  867. max_length= (ulong) max_result_length;
  868. }
  869. String *Item_func_lcase::val_str(String *str)
  870. {
  871. DBUG_ASSERT(fixed == 1);
  872. String *res;
  873. if (!(res=args[0]->val_str(str)))
  874. {
  875. null_value=1; /* purecov: inspected */
  876. return 0; /* purecov: inspected */
  877. }
  878. null_value=0;
  879. res=copy_if_not_alloced(str,res,res->length());
  880. res->casedn();
  881. return res;
  882. }
  883. String *Item_func_ucase::val_str(String *str)
  884. {
  885. DBUG_ASSERT(fixed == 1);
  886. String *res;
  887. if (!(res=args[0]->val_str(str)))
  888. {
  889. null_value=1; /* purecov: inspected */
  890. return 0; /* purecov: inspected */
  891. }
  892. null_value=0;
  893. res=copy_if_not_alloced(str,res,res->length());
  894. res->caseup();
  895. return res;
  896. }
  897. String *Item_func_left::val_str(String *str)
  898. {
  899. DBUG_ASSERT(fixed == 1);
  900. String *res =args[0]->val_str(str);
  901. long length =(long) args[1]->val_int();
  902. uint char_pos;
  903. if ((null_value=args[0]->null_value))
  904. return 0;
  905. if (length <= 0)
  906. return &my_empty_string;
  907. if (res->length() <= (uint) length ||
  908. res->length() <= (char_pos= res->charpos(length)))
  909. return res;
  910. tmp_value.set(*res, 0, char_pos);
  911. return &tmp_value;
  912. }
  913. void Item_str_func::left_right_max_length()
  914. {
  915. max_length=args[0]->max_length;
  916. if (args[1]->const_item())
  917. {
  918. int length=(int) args[1]->val_int()*collation.collation->mbmaxlen;
  919. if (length <= 0)
  920. max_length=0;
  921. else
  922. set_if_smaller(max_length,(uint) length);
  923. }
  924. }
  925. void Item_func_left::fix_length_and_dec()
  926. {
  927. collation.set(args[0]->collation);
  928. left_right_max_length();
  929. }
  930. String *Item_func_right::val_str(String *str)
  931. {
  932. DBUG_ASSERT(fixed == 1);
  933. String *res =args[0]->val_str(str);
  934. long length =(long) args[1]->val_int();
  935. if ((null_value=args[0]->null_value))
  936. return 0; /* purecov: inspected */
  937. if (length <= 0)
  938. return &my_empty_string; /* purecov: inspected */
  939. if (res->length() <= (uint) length)
  940. return res; /* purecov: inspected */
  941. uint start=res->numchars();
  942. if (start <= (uint) length)
  943. return res;
  944. start=res->charpos(start - (uint) length);
  945. tmp_value.set(*res,start,res->length()-start);
  946. return &tmp_value;
  947. }
  948. void Item_func_right::fix_length_and_dec()
  949. {
  950. collation.set(args[0]->collation);
  951. left_right_max_length();
  952. }
  953. String *Item_func_substr::val_str(String *str)
  954. {
  955. DBUG_ASSERT(fixed == 1);
  956. String *res = args[0]->val_str(str);
  957. int32 start = (int32) args[1]->val_int();
  958. int32 length = arg_count == 3 ? (int32) args[2]->val_int() : INT_MAX32;
  959. int32 tmp_length;
  960. if ((null_value=(args[0]->null_value || args[1]->null_value ||
  961. (arg_count == 3 && args[2]->null_value))))
  962. return 0; /* purecov: inspected */
  963. start= (int32)((start < 0) ? res->numchars() + start : start -1);
  964. start=res->charpos(start);
  965. length=res->charpos(length,start);
  966. if (start < 0 || (uint) start+1 > res->length() || length <= 0)
  967. return &my_empty_string;
  968. tmp_length=(int32) res->length()-start;
  969. length=min(length,tmp_length);
  970. if (!start && res->length() == (uint) length)
  971. return res;
  972. tmp_value.set(*res,(uint) start,(uint) length);
  973. return &tmp_value;
  974. }
  975. void Item_func_substr::fix_length_and_dec()
  976. {
  977. max_length=args[0]->max_length;
  978. collation.set(args[0]->collation);
  979. if (args[1]->const_item())
  980. {
  981. int32 start= (int32) args[1]->val_int();
  982. start= (int32)((start < 0) ? max_length + start : start - 1);
  983. if (start < 0 || start >= (int32) max_length)
  984. max_length=0; /* purecov: inspected */
  985. else
  986. max_length-= (uint) start;
  987. }
  988. if (arg_count == 3 && args[2]->const_item())
  989. {
  990. int32 length= (int32) args[2]->val_int();
  991. if (length <= 0)
  992. max_length=0; /* purecov: inspected */
  993. else
  994. set_if_smaller(max_length,(uint) length);
  995. }
  996. max_length*= collation.collation->mbmaxlen;
  997. }
  998. void Item_func_substr_index::fix_length_and_dec()
  999. {
  1000. max_length= args[0]->max_length;
  1001. if (agg_arg_charsets(collation, args, 2, MY_COLL_CMP_CONV))
  1002. return;
  1003. }
  1004. String *Item_func_substr_index::val_str(String *str)
  1005. {
  1006. DBUG_ASSERT(fixed == 1);
  1007. String *res =args[0]->val_str(str);
  1008. String *delimeter =args[1]->val_str(&tmp_value);
  1009. int32 count = (int32) args[2]->val_int();
  1010. uint offset;
  1011. if (args[0]->null_value || args[1]->null_value || args[2]->null_value)
  1012. { // string and/or delim are null
  1013. null_value=1;
  1014. return 0;
  1015. }
  1016. null_value=0;
  1017. uint delimeter_length=delimeter->length();
  1018. if (!res->length() || !delimeter_length || !count)
  1019. return &my_empty_string; // Wrong parameters
  1020. res->set_charset(collation.collation);
  1021. #ifdef USE_MB
  1022. if (use_mb(res->charset()))
  1023. {
  1024. const char *ptr=res->ptr();
  1025. const char *strend = ptr+res->length();
  1026. const char *end=strend-delimeter_length+1;
  1027. const char *search=delimeter->ptr();
  1028. const char *search_end=search+delimeter_length;
  1029. int32 n=0,c=count,pass;
  1030. register uint32 l;
  1031. for (pass=(count>0);pass<2;++pass)
  1032. {
  1033. while (ptr < end)
  1034. {
  1035. if (*ptr == *search)
  1036. {
  1037. register char *i,*j;
  1038. i=(char*) ptr+1; j=(char*) search+1;
  1039. while (j != search_end)
  1040. if (*i++ != *j++) goto skip;
  1041. if (pass==0) ++n;
  1042. else if (!--c) break;
  1043. ptr+=delimeter_length;
  1044. continue;
  1045. }
  1046. skip:
  1047. if ((l=my_ismbchar(res->charset(), ptr,strend))) ptr+=l;
  1048. else ++ptr;
  1049. } /* either not found or got total number when count<0 */
  1050. if (pass == 0) /* count<0 */
  1051. {
  1052. c+=n+1;
  1053. if (c<=0) return res; /* not found, return original string */
  1054. ptr=res->ptr();
  1055. }
  1056. else
  1057. {
  1058. if (c) return res; /* Not found, return original string */
  1059. if (count>0) /* return left part */
  1060. {
  1061. tmp_value.set(*res,0,(ulong) (ptr-res->ptr()));
  1062. }
  1063. else /* return right part */
  1064. {
  1065. ptr+=delimeter_length;
  1066. tmp_value.set(*res,(ulong) (ptr-res->ptr()), (ulong) (strend-ptr));
  1067. }
  1068. }
  1069. }
  1070. }
  1071. else
  1072. #endif /* USE_MB */
  1073. {
  1074. if (count > 0)
  1075. { // start counting from the beginning
  1076. for (offset=0 ;; offset+=delimeter_length)
  1077. {
  1078. if ((int) (offset=res->strstr(*delimeter,offset)) < 0)
  1079. return res; // Didn't find, return org string
  1080. if (!--count)
  1081. {
  1082. tmp_value.set(*res,0,offset);
  1083. break;
  1084. }
  1085. }
  1086. }
  1087. else
  1088. {
  1089. /*
  1090. Negative index, start counting at the end
  1091. */
  1092. for (offset=res->length(); offset ;)
  1093. {
  1094. /*
  1095. this call will result in finding the position pointing to one
  1096. address space less than where the found substring is located
  1097. in res
  1098. */
  1099. if ((int) (offset=res->strrstr(*delimeter,offset)) < 0)
  1100. return res; // Didn't find, return org string
  1101. /*
  1102. At this point, we've searched for the substring
  1103. the number of times as supplied by the index value
  1104. */
  1105. if (!++count)
  1106. {
  1107. offset+=delimeter_length;
  1108. tmp_value.set(*res,offset,res->length()- offset);
  1109. break;
  1110. }
  1111. }
  1112. }
  1113. }
  1114. return (&tmp_value);
  1115. }
  1116. /*
  1117. ** The trim functions are extension to ANSI SQL because they trim substrings
  1118. ** They ltrim() and rtrim() functions are optimized for 1 byte strings
  1119. ** They also return the original string if possible, else they return
  1120. ** a substring that points at the original string.
  1121. */
  1122. String *Item_func_ltrim::val_str(String *str)
  1123. {
  1124. DBUG_ASSERT(fixed == 1);
  1125. String *res =args[0]->val_str(str);
  1126. if ((null_value=args[0]->null_value))
  1127. return 0; /* purecov: inspected */
  1128. char buff[MAX_FIELD_WIDTH];
  1129. String tmp(buff,sizeof(buff),res->charset());
  1130. String *remove_str= (arg_count==2) ? args[1]->val_str(&tmp) : &remove;
  1131. uint remove_length;
  1132. LINT_INIT(remove_length);
  1133. if (!remove_str || (remove_length=remove_str->length()) == 0 ||
  1134. remove_length > res->length())
  1135. return res;
  1136. char *ptr=(char*) res->ptr();
  1137. char *end=ptr+res->length();
  1138. if (remove_length == 1)
  1139. {
  1140. char chr=(*remove_str)[0];
  1141. while (ptr != end && *ptr == chr)
  1142. ptr++;
  1143. }
  1144. else
  1145. {
  1146. const char *r_ptr=remove_str->ptr();
  1147. end-=remove_length;
  1148. while (ptr <= end && !memcmp(ptr, r_ptr, remove_length))
  1149. ptr+=remove_length;
  1150. end+=remove_length;
  1151. }
  1152. if (ptr == res->ptr())
  1153. return res;
  1154. tmp_value.set(*res,(uint) (ptr - res->ptr()),(uint) (end-ptr));
  1155. return &tmp_value;
  1156. }
  1157. String *Item_func_rtrim::val_str(String *str)
  1158. {
  1159. DBUG_ASSERT(fixed == 1);
  1160. String *res =args[0]->val_str(str);
  1161. if ((null_value=args[0]->null_value))
  1162. return 0; /* purecov: inspected */
  1163. char buff[MAX_FIELD_WIDTH];
  1164. String tmp(buff,sizeof(buff),res->charset());
  1165. String *remove_str= (arg_count==2) ? args[1]->val_str(&tmp) : &remove;
  1166. uint remove_length;
  1167. LINT_INIT(remove_length);
  1168. if (!remove_str || (remove_length=remove_str->length()) == 0 ||
  1169. remove_length > res->length())
  1170. return res;
  1171. char *ptr=(char*) res->ptr();
  1172. char *end=ptr+res->length();
  1173. #ifdef USE_MB
  1174. char *p=ptr;
  1175. register uint32 l;
  1176. #endif
  1177. if (remove_length == 1)
  1178. {
  1179. char chr=(*remove_str)[0];
  1180. #ifdef USE_MB
  1181. if (use_mb(res->charset()))
  1182. {
  1183. while (ptr < end)
  1184. {
  1185. if ((l=my_ismbchar(res->charset(), ptr,end))) ptr+=l,p=ptr;
  1186. else ++ptr;
  1187. }
  1188. ptr=p;
  1189. }
  1190. #endif
  1191. while (ptr != end && end[-1] == chr)
  1192. end--;
  1193. }
  1194. else
  1195. {
  1196. const char *r_ptr=remove_str->ptr();
  1197. #ifdef USE_MB
  1198. if (use_mb(res->charset()))
  1199. {
  1200. loop:
  1201. while (ptr + remove_length < end)
  1202. {
  1203. if ((l=my_ismbchar(res->charset(), ptr,end))) ptr+=l;
  1204. else ++ptr;
  1205. }
  1206. if (ptr + remove_length == end && !memcmp(ptr,r_ptr,remove_length))
  1207. {
  1208. end-=remove_length;
  1209. ptr=p;
  1210. goto loop;
  1211. }
  1212. }
  1213. else
  1214. #endif /* USE_MB */
  1215. {
  1216. while (ptr + remove_length <= end &&
  1217. !memcmp(end-remove_length, r_ptr, remove_length))
  1218. end-=remove_length;
  1219. }
  1220. }
  1221. if (end == res->ptr()+res->length())
  1222. return res;
  1223. tmp_value.set(*res,0,(uint) (end-res->ptr()));
  1224. return &tmp_value;
  1225. }
  1226. String *Item_func_trim::val_str(String *str)
  1227. {
  1228. DBUG_ASSERT(fixed == 1);
  1229. String *res =args[0]->val_str(str);
  1230. if ((null_value=args[0]->null_value))
  1231. return 0; /* purecov: inspected */
  1232. char buff[MAX_FIELD_WIDTH];
  1233. String tmp(buff,sizeof(buff),res->charset());
  1234. uint remove_length;
  1235. LINT_INIT(remove_length);
  1236. String *remove_str; /* The string to remove from res. */
  1237. if (arg_count == 2)
  1238. {
  1239. remove_str= args[1]->val_str(&tmp);
  1240. if ((null_value= args[1]->null_value))
  1241. return 0;
  1242. }
  1243. else
  1244. remove_str= &remove; /* Default value. */
  1245. if (!remove_str || (remove_length=remove_str->length()) == 0 ||
  1246. remove_length > res->length())
  1247. return res;
  1248. char *ptr=(char*) res->ptr();
  1249. char *end=ptr+res->length();
  1250. const char *r_ptr=remove_str->ptr();
  1251. while (ptr+remove_length <= end && !memcmp(ptr,r_ptr,remove_length))
  1252. ptr+=remove_length;
  1253. #ifdef USE_MB
  1254. if (use_mb(res->charset()))
  1255. {
  1256. char *p=ptr;
  1257. register uint32 l;
  1258. loop:
  1259. while (ptr + remove_length < end)
  1260. {
  1261. if ((l=my_ismbchar(res->charset(), ptr,end))) ptr+=l;
  1262. else ++ptr;
  1263. }
  1264. if (ptr + remove_length == end && !memcmp(ptr,r_ptr,remove_length))
  1265. {
  1266. end-=remove_length;
  1267. ptr=p;
  1268. goto loop;
  1269. }
  1270. ptr=p;
  1271. }
  1272. else
  1273. #endif /* USE_MB */
  1274. {
  1275. while (ptr + remove_length <= end &&
  1276. !memcmp(end-remove_length,r_ptr,remove_length))
  1277. end-=remove_length;
  1278. }
  1279. if (ptr == res->ptr() && end == ptr+res->length())
  1280. return res;
  1281. tmp_value.set(*res,(uint) (ptr - res->ptr()),(uint) (end-ptr));
  1282. return &tmp_value;
  1283. }
  1284. void Item_func_trim::fix_length_and_dec()
  1285. {
  1286. max_length= args[0]->max_length;
  1287. if (arg_count == 1)
  1288. {
  1289. collation.set(args[0]->collation);
  1290. remove.set_charset(collation.collation);
  1291. remove.set_ascii(" ",1);
  1292. }
  1293. else
  1294. {
  1295. Item *cargs[2];
  1296. cargs[0]= args[1];
  1297. cargs[1]= args[0];
  1298. if (agg_arg_charsets(collation, cargs, 2, MY_COLL_CMP_CONV))
  1299. return;
  1300. args[0]= cargs[1];
  1301. args[1]= cargs[0];
  1302. }
  1303. }
  1304. void Item_func_trim::print(String *str)
  1305. {
  1306. if (arg_count == 1)
  1307. {
  1308. Item_func::print(str);
  1309. return;
  1310. }
  1311. str->append(Item_func_trim::func_name());
  1312. str->append('(');
  1313. str->append(mode_name());
  1314. str->append(' ');
  1315. args[1]->print(str);
  1316. str->append(" from ",6);
  1317. args[0]->print(str);
  1318. str->append(')');
  1319. }
  1320. /* Item_func_password */
  1321. String *Item_func_password::val_str(String *str)
  1322. {
  1323. DBUG_ASSERT(fixed == 1);
  1324. String *res= args[0]->val_str(str);
  1325. if ((null_value=args[0]->null_value))
  1326. return 0;
  1327. if (res->length() == 0)
  1328. return &my_empty_string;
  1329. make_scrambled_password(tmp_value, res->c_ptr());
  1330. str->set(tmp_value, SCRAMBLED_PASSWORD_CHAR_LENGTH, res->charset());
  1331. return str;
  1332. }
  1333. char *Item_func_password::alloc(THD *thd, const char *password)
  1334. {
  1335. char *buff= (char *) thd->alloc(SCRAMBLED_PASSWORD_CHAR_LENGTH+1);
  1336. if (buff)
  1337. make_scrambled_password(buff, password);
  1338. return buff;
  1339. }
  1340. /* Item_func_old_password */
  1341. String *Item_func_old_password::val_str(String *str)
  1342. {
  1343. DBUG_ASSERT(fixed == 1);
  1344. String *res= args[0]->val_str(str);
  1345. if ((null_value=args[0]->null_value))
  1346. return 0;
  1347. if (res->length() == 0)
  1348. return &my_empty_string;
  1349. make_scrambled_password_323(tmp_value, res->c_ptr());
  1350. str->set(tmp_value, SCRAMBLED_PASSWORD_CHAR_LENGTH_323, res->charset());
  1351. return str;
  1352. }
  1353. char *Item_func_old_password::alloc(THD *thd, const char *password)
  1354. {
  1355. char *buff= (char *) thd->alloc(SCRAMBLED_PASSWORD_CHAR_LENGTH_323+1);
  1356. if (buff)
  1357. make_scrambled_password_323(buff, password);
  1358. return buff;
  1359. }
  1360. #define bin_to_ascii(c) ((c)>=38?((c)-38+'a'):(c)>=12?((c)-12+'A'):(c)+'.')
  1361. String *Item_func_encrypt::val_str(String *str)
  1362. {
  1363. DBUG_ASSERT(fixed == 1);
  1364. String *res =args[0]->val_str(str);
  1365. #ifdef HAVE_CRYPT
  1366. char salt[3],*salt_ptr;
  1367. if ((null_value=args[0]->null_value))
  1368. return 0;
  1369. if (res->length() == 0)
  1370. return &my_empty_string;
  1371. if (arg_count == 1)
  1372. { // generate random salt
  1373. time_t timestamp=current_thd->query_start();
  1374. salt[0] = bin_to_ascii( (ulong) timestamp & 0x3f);
  1375. salt[1] = bin_to_ascii(( (ulong) timestamp >> 5) & 0x3f);
  1376. salt[2] = 0;
  1377. salt_ptr=salt;
  1378. }
  1379. else
  1380. { // obtain salt from the first two bytes
  1381. String *salt_str=args[1]->val_str(&tmp_value);
  1382. if ((null_value= (args[1]->null_value || salt_str->length() < 2)))
  1383. return 0;
  1384. salt_ptr= salt_str->c_ptr();
  1385. }
  1386. pthread_mutex_lock(&LOCK_crypt);
  1387. char *tmp= crypt(res->c_ptr(),salt_ptr);
  1388. if (!tmp)
  1389. {
  1390. pthread_mutex_unlock(&LOCK_crypt);
  1391. null_value= 1;
  1392. return 0;
  1393. }
  1394. str->set(tmp, (uint) strlen(tmp), &my_charset_bin);
  1395. str->copy();
  1396. pthread_mutex_unlock(&LOCK_crypt);
  1397. return str;
  1398. #else
  1399. null_value=1;
  1400. return 0;
  1401. #endif /* HAVE_CRYPT */
  1402. }
  1403. void Item_func_encode::fix_length_and_dec()
  1404. {
  1405. max_length=args[0]->max_length;
  1406. maybe_null=args[0]->maybe_null;
  1407. collation.set(&my_charset_bin);
  1408. }
  1409. String *Item_func_encode::val_str(String *str)
  1410. {
  1411. DBUG_ASSERT(fixed == 1);
  1412. String *res;
  1413. if (!(res=args[0]->val_str(str)))
  1414. {
  1415. null_value=1; /* purecov: inspected */
  1416. return 0; /* purecov: inspected */
  1417. }
  1418. null_value=0;
  1419. res=copy_if_not_alloced(str,res,res->length());
  1420. sql_crypt.init();
  1421. sql_crypt.encode((char*) res->ptr(),res->length());
  1422. res->set_charset(&my_charset_bin);
  1423. return res;
  1424. }
  1425. void Item_func_encode::print(String *str)
  1426. {
  1427. str->append(func_name());
  1428. str->append('(');
  1429. args[0]->print(str);
  1430. str->append(',');
  1431. str->append('\'');
  1432. str->append(seed);
  1433. str->append('\'');
  1434. str->append(')');
  1435. }
  1436. String *Item_func_decode::val_str(String *str)
  1437. {
  1438. DBUG_ASSERT(fixed == 1);
  1439. String *res;
  1440. if (!(res=args[0]->val_str(str)))
  1441. {
  1442. null_value=1; /* purecov: inspected */
  1443. return 0; /* purecov: inspected */
  1444. }
  1445. null_value=0;
  1446. res=copy_if_not_alloced(str,res,res->length());
  1447. sql_crypt.init();
  1448. sql_crypt.decode((char*) res->ptr(),res->length());
  1449. return res;
  1450. }
  1451. Item *Item_func_sysconst::safe_charset_converter(CHARSET_INFO *tocs)
  1452. {
  1453. Item_string *conv;
  1454. uint conv_errors;
  1455. String tmp, cstr, *ostr= val_str(&tmp);
  1456. cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
  1457. if (conv_errors || !(conv= new Item_string(cstr.ptr(), cstr.length(),
  1458. cstr.charset(),
  1459. collation.derivation)))
  1460. {
  1461. return NULL;
  1462. }
  1463. conv->str_value.copy();
  1464. conv->str_value.shrink_to_length();
  1465. return conv;
  1466. }
  1467. String *Item_func_database::val_str(String *str)
  1468. {
  1469. DBUG_ASSERT(fixed == 1);
  1470. THD *thd= current_thd;
  1471. if (!thd->db)
  1472. {
  1473. null_value= 1;
  1474. return 0;
  1475. }
  1476. else
  1477. str->copy((const char*) thd->db,(uint) strlen(thd->db),system_charset_info);
  1478. return str;
  1479. }
  1480. // TODO: make USER() replicate properly (currently it is replicated to "")
  1481. String *Item_func_user::val_str(String *str)
  1482. {
  1483. DBUG_ASSERT(fixed == 1);
  1484. THD *thd=current_thd;
  1485. CHARSET_INFO *cs= system_charset_info;
  1486. const char *host= thd->host_or_ip;
  1487. uint res_length;
  1488. // For system threads (e.g. replication SQL thread) user may be empty
  1489. if (!thd->user)
  1490. return &my_empty_string;
  1491. res_length= (strlen(thd->user)+strlen(host)+2) * cs->mbmaxlen;
  1492. if (str->alloc(res_length))
  1493. {
  1494. null_value=1;
  1495. return 0;
  1496. }
  1497. res_length=cs->cset->snprintf(cs, (char*)str->ptr(), res_length, "%s@%s",
  1498. thd->user, host);
  1499. str->length(res_length);
  1500. str->set_charset(cs);
  1501. return str;
  1502. }
  1503. void Item_func_soundex::fix_length_and_dec()
  1504. {
  1505. collation.set(args[0]->collation);
  1506. max_length=args[0]->max_length;
  1507. set_if_bigger(max_length,4);
  1508. }
  1509. /*
  1510. If alpha, map input letter to soundex code.
  1511. If not alpha and remove_garbage is set then skip to next char
  1512. else return 0
  1513. */
  1514. static char soundex_toupper(char ch)
  1515. {
  1516. return (ch >= 'a' && ch <= 'z') ? ch - 'a' + 'A' : ch;
  1517. }
  1518. static char get_scode(char *ptr)
  1519. {
  1520. uchar ch= soundex_toupper(*ptr);
  1521. if (ch < 'A' || ch > 'Z')
  1522. {
  1523. // Thread extended alfa (country spec)
  1524. return '0'; // as vokal
  1525. }
  1526. return(soundex_map[ch-'A']);
  1527. }
  1528. String *Item_func_soundex::val_str(String *str)
  1529. {
  1530. DBUG_ASSERT(fixed == 1);
  1531. String *res =args[0]->val_str(str);
  1532. char last_ch,ch;
  1533. CHARSET_INFO *cs= collation.collation;
  1534. if ((null_value=args[0]->null_value))
  1535. return 0; /* purecov: inspected */
  1536. if (tmp_value.alloc(max(res->length(),4)))
  1537. return str; /* purecov: inspected */
  1538. char *to= (char *) tmp_value.ptr();
  1539. char *from= (char *) res->ptr(), *end=from+res->length();
  1540. tmp_value.set_charset(cs);
  1541. while (from != end && !my_isalpha(cs,*from)) // Skip pre-space
  1542. from++; /* purecov: inspected */
  1543. if (from == end)
  1544. return &my_empty_string; // No alpha characters.
  1545. *to++ = soundex_toupper(*from); // Copy first letter
  1546. last_ch = get_scode(from); // code of the first letter
  1547. // for the first 'double-letter check.
  1548. // Loop on input letters until
  1549. // end of input (null) or output
  1550. // letter code count = 3
  1551. for (from++ ; from < end ; from++)
  1552. {
  1553. if (!my_isalpha(cs,*from))
  1554. continue;
  1555. ch=get_scode(from);
  1556. if ((ch != '0') && (ch != last_ch)) // if not skipped or double
  1557. {
  1558. *to++ = ch; // letter, copy to output
  1559. last_ch = ch; // save code of last input letter
  1560. } // for next double-letter check
  1561. }
  1562. for (end=(char*) tmp_value.ptr()+4 ; to < end ; to++)
  1563. *to = '0';
  1564. *to=0; // end string
  1565. tmp_value.length((uint) (to-tmp_value.ptr()));
  1566. return &tmp_value;
  1567. }
  1568. /*
  1569. ** Change a number to format '3,333,333,333.000'
  1570. ** This should be 'internationalized' sometimes.
  1571. */
  1572. Item_func_format::Item_func_format(Item *org,int dec) :Item_str_func(org)
  1573. {
  1574. decimals=(uint) set_zone(dec,0,30);
  1575. }
  1576. /*
  1577. TODO: This needs to be fixed for multi-byte character set where numbers
  1578. are stored in more than one byte
  1579. */
  1580. String *Item_func_format::val_str(String *str)
  1581. {
  1582. DBUG_ASSERT(fixed == 1);
  1583. double nr =args[0]->val();
  1584. int diff;
  1585. uint32 length, str_length;
  1586. uint dec;
  1587. if ((null_value=args[0]->null_value))
  1588. return 0; /* purecov: inspected */
  1589. dec= decimals ? decimals+1 : 0;
  1590. /* Here default_charset() is right as this is not an automatic conversion */
  1591. str->set(nr,decimals, default_charset());
  1592. if (isnan(nr))
  1593. return str;
  1594. str_length=str->length();
  1595. if (nr < 0)
  1596. str_length--; // Don't count sign
  1597. /* We need this test to handle 'nan' values */
  1598. if (str_length >= dec+4)
  1599. {
  1600. char *tmp,*pos;
  1601. length= str->length()+(diff= (int)(str_length- dec-1)/3);
  1602. str= copy_if_not_alloced(&tmp_str,str,length);
  1603. str->length(length);
  1604. tmp= (char*) str->ptr()+length - dec-1;
  1605. for (pos= (char*) str->ptr()+length-1; pos != tmp; pos--)
  1606. pos[0]= pos[-diff];
  1607. while (diff)
  1608. {
  1609. *pos= *(pos - diff);
  1610. pos--;
  1611. *pos= *(pos - diff);
  1612. pos--;
  1613. *pos= *(pos - diff);
  1614. pos--;
  1615. pos[0]=',';
  1616. pos--;
  1617. diff--;
  1618. }
  1619. }
  1620. return str;
  1621. }
  1622. void Item_func_format::print(String *str)
  1623. {
  1624. str->append("format(", 7);
  1625. args[0]->print(str);
  1626. str->append(',');
  1627. // my_charset_bin is good enough for numbers
  1628. char buffer[20];
  1629. String st(buffer, sizeof(buffer), &my_charset_bin);
  1630. st.set((ulonglong)decimals, &my_charset_bin);
  1631. str->append(st);
  1632. str->append(')');
  1633. }
  1634. void Item_func_elt::fix_length_and_dec()
  1635. {
  1636. max_length=0;
  1637. decimals=0;
  1638. if (agg_arg_charsets(collation, args+1, arg_count-1, MY_COLL_ALLOW_CONV))
  1639. return;
  1640. for (uint i= 1 ; i < arg_count ; i++)
  1641. {
  1642. set_if_bigger(max_length,args[i]->max_length);
  1643. set_if_bigger(decimals,args[i]->decimals);
  1644. }
  1645. maybe_null=1; // NULL if wrong first arg
  1646. }
  1647. double Item_func_elt::val()
  1648. {
  1649. DBUG_ASSERT(fixed == 1);
  1650. uint tmp;
  1651. null_value=1;
  1652. if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
  1653. return 0.0;
  1654. double result= args[tmp]->val();
  1655. null_value= args[tmp]->null_value;
  1656. return result;
  1657. }
  1658. longlong Item_func_elt::val_int()
  1659. {
  1660. DBUG_ASSERT(fixed == 1);
  1661. uint tmp;
  1662. null_value=1;
  1663. if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
  1664. return 0;
  1665. longlong result= args[tmp]->val_int();
  1666. null_value= args[tmp]->null_value;
  1667. return result;
  1668. }
  1669. String *Item_func_elt::val_str(String *str)
  1670. {
  1671. DBUG_ASSERT(fixed == 1);
  1672. uint tmp;
  1673. null_value=1;
  1674. if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
  1675. return NULL;
  1676. String *result= args[tmp]->val_str(str);
  1677. if (result)
  1678. result->set_charset(collation.collation);
  1679. null_value= args[tmp]->null_value;
  1680. return result;
  1681. }
  1682. void Item_func_make_set::split_sum_func(THD *thd, Item **ref_pointer_array,
  1683. List<Item> &fields)
  1684. {
  1685. item->split_sum_func2(thd, ref_pointer_array, fields, &item);
  1686. Item_str_func::split_sum_func(thd, ref_pointer_array, fields);
  1687. }
  1688. void Item_func_make_set::fix_length_and_dec()
  1689. {
  1690. max_length=arg_count-1;
  1691. if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV))
  1692. return;
  1693. for (uint i=0 ; i < arg_count ; i++)
  1694. max_length+=args[i]->max_length;
  1695. used_tables_cache|= item->used_tables();
  1696. not_null_tables_cache&= item->not_null_tables();
  1697. const_item_cache&= item->const_item();
  1698. with_sum_func= with_sum_func || item->with_sum_func;
  1699. }
  1700. void Item_func_make_set::update_used_tables()
  1701. {
  1702. Item_func::update_used_tables();
  1703. item->update_used_tables();
  1704. used_tables_cache|=item->used_tables();
  1705. const_item_cache&=item->const_item();
  1706. }
  1707. String *Item_func_make_set::val_str(String *str)
  1708. {
  1709. DBUG_ASSERT(fixed == 1);
  1710. ulonglong bits;
  1711. bool first_found=0;
  1712. Item **ptr=args;
  1713. String *result=&my_empty_string;
  1714. bits=item->val_int();
  1715. if ((null_value=item->null_value))
  1716. return NULL;
  1717. if (arg_count < 64)
  1718. bits &= ((ulonglong) 1 << arg_count)-1;
  1719. for (; bits; bits >>= 1, ptr++)
  1720. {
  1721. if (bits & 1)
  1722. {
  1723. String *res= (*ptr)->val_str(str);
  1724. if (res) // Skip nulls
  1725. {
  1726. if (!first_found)
  1727. { // First argument
  1728. first_found=1;
  1729. if (res != str)
  1730. result=res; // Use original string
  1731. else
  1732. {
  1733. if (tmp_str.copy(*res)) // Don't use 'str'
  1734. return &my_empty_string;
  1735. result= &tmp_str;
  1736. }
  1737. }
  1738. else
  1739. {
  1740. if (result != &tmp_str)
  1741. { // Copy data to tmp_str
  1742. if (tmp_str.alloc(result->length()+res->length()+1) ||
  1743. tmp_str.copy(*result))
  1744. return &my_empty_string;
  1745. result= &tmp_str;
  1746. }
  1747. if (tmp_str.append(",", 1, &my_charset_bin) || tmp_str.append(*res))
  1748. return &my_empty_string;
  1749. }
  1750. }
  1751. }
  1752. }
  1753. return result;
  1754. }
  1755. void Item_func_make_set::print(String *str)
  1756. {
  1757. str->append("make_set(", 9);
  1758. item->print(str);
  1759. if (arg_count)
  1760. {
  1761. str->append(',');
  1762. print_args(str, 0);
  1763. }
  1764. str->append(')');
  1765. }
  1766. String *Item_func_char::val_str(String *str)
  1767. {
  1768. DBUG_ASSERT(fixed == 1);
  1769. str->length(0);
  1770. for (uint i=0 ; i < arg_count ; i++)
  1771. {
  1772. int32 num=(int32) args[i]->val_int();
  1773. if (!args[i]->null_value)
  1774. {
  1775. #ifdef USE_MB
  1776. if (use_mb(collation.collation))
  1777. {
  1778. if (num&0xFF000000L) {
  1779. str->append((char)(num>>24));
  1780. goto b2;
  1781. } else if (num&0xFF0000L) {
  1782. b2: str->append((char)(num>>16));
  1783. goto b1;
  1784. } else if (num&0xFF00L) {
  1785. b1: str->append((char)(num>>8));
  1786. }
  1787. }
  1788. #endif
  1789. str->append((char)num);
  1790. }
  1791. }
  1792. str->set_charset(collation.collation);
  1793. str->realloc(str->length()); // Add end 0 (for Purify)
  1794. return str;
  1795. }
  1796. inline String* alloc_buffer(String *res,String *str,String *tmp_value,
  1797. ulong length)
  1798. {
  1799. if (res->alloced_length() < length)
  1800. {
  1801. if (str->alloced_length() >= length)
  1802. {
  1803. (void) str->copy(*res);
  1804. str->length(length);
  1805. return str;
  1806. }
  1807. if (tmp_value->alloc(length))
  1808. return 0;
  1809. (void) tmp_value->copy(*res);
  1810. tmp_value->length(length);
  1811. return tmp_value;
  1812. }
  1813. res->length(length);
  1814. return res;
  1815. }
  1816. void Item_func_repeat::fix_length_and_dec()
  1817. {
  1818. collation.set(args[0]->collation);
  1819. if (args[1]->const_item())
  1820. {
  1821. ulonglong max_result_length= ((ulonglong) args[0]->max_length *
  1822. args[1]->val_int());
  1823. if (max_result_length >= MAX_BLOB_WIDTH)
  1824. {
  1825. max_result_length= MAX_BLOB_WIDTH;
  1826. maybe_null= 1;
  1827. }
  1828. max_length= (ulong) max_result_length;
  1829. }
  1830. else
  1831. {
  1832. max_length= MAX_BLOB_WIDTH;
  1833. maybe_null= 1;
  1834. }
  1835. }
  1836. /*
  1837. ** Item_func_repeat::str is carefully written to avoid reallocs
  1838. ** as much as possible at the cost of a local buffer
  1839. */
  1840. String *Item_func_repeat::val_str(String *str)
  1841. {
  1842. DBUG_ASSERT(fixed == 1);
  1843. uint length,tot_length;
  1844. char *to;
  1845. long count= (long) args[1]->val_int();
  1846. String *res =args[0]->val_str(str);
  1847. if (args[0]->null_value || args[1]->null_value)
  1848. goto err; // string and/or delim are null
  1849. null_value=0;
  1850. if (count <= 0) // For nicer SQL code
  1851. return &my_empty_string;
  1852. if (count == 1) // To avoid reallocs
  1853. return res;
  1854. length=res->length();
  1855. // Safe length check
  1856. if (length > current_thd->variables.max_allowed_packet/count)
  1857. {
  1858. push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
  1859. ER_WARN_ALLOWED_PACKET_OVERFLOWED,
  1860. ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
  1861. func_name(), current_thd->variables.max_allowed_packet);
  1862. goto err;
  1863. }
  1864. tot_length= length*(uint) count;
  1865. if (!(res= alloc_buffer(res,str,&tmp_value,tot_length)))
  1866. goto err;
  1867. to=(char*) res->ptr()+length;
  1868. while (--count)
  1869. {
  1870. memcpy(to,res->ptr(),length);
  1871. to+=length;
  1872. }
  1873. return (res);
  1874. err:
  1875. null_value=1;
  1876. return 0;
  1877. }
  1878. void Item_func_rpad::fix_length_and_dec()
  1879. {
  1880. Item *cargs[2];
  1881. cargs[0]= args[0];
  1882. cargs[1]= args[2];
  1883. if (agg_arg_charsets(collation, cargs, 2, MY_COLL_ALLOW_CONV))
  1884. return;
  1885. args[0]= cargs[0];
  1886. args[2]= cargs[1];
  1887. if (args[1]->const_item())
  1888. {
  1889. ulonglong length= ((ulonglong) args[1]->val_int() *
  1890. collation.collation->mbmaxlen);
  1891. if (length >= MAX_BLOB_WIDTH)
  1892. {
  1893. length= MAX_BLOB_WIDTH;
  1894. maybe_null= 1;
  1895. }
  1896. max_length= (ulong) length;
  1897. }
  1898. else
  1899. {
  1900. max_length= MAX_BLOB_WIDTH;
  1901. maybe_null= 1;
  1902. }
  1903. }
  1904. String *Item_func_rpad::val_str(String *str)
  1905. {
  1906. DBUG_ASSERT(fixed == 1);
  1907. uint32 res_byte_length,res_char_length,pad_char_length,pad_byte_length;
  1908. char *to;
  1909. const char *ptr_pad;
  1910. int32 count= (int32) args[1]->val_int();
  1911. int32 byte_count= count * collation.collation->mbmaxlen;
  1912. String *res =args[0]->val_str(str);
  1913. String *rpad = args[2]->val_str(&rpad_str);
  1914. if (!res || args[1]->null_value || !rpad || count < 0)
  1915. goto err;
  1916. null_value=0;
  1917. if (count <= (int32) (res_char_length=res->numchars()))
  1918. { // String to pad is big enough
  1919. res->length(res->charpos(count)); // Shorten result if longer
  1920. return (res);
  1921. }
  1922. pad_char_length= rpad->numchars();
  1923. if ((ulong) byte_count > current_thd->variables.max_allowed_packet)
  1924. {
  1925. push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
  1926. ER_WARN_ALLOWED_PACKET_OVERFLOWED,
  1927. ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
  1928. func_name(), current_thd->variables.max_allowed_packet);
  1929. goto err;
  1930. }
  1931. if(args[2]->null_value || !pad_char_length)
  1932. goto err;
  1933. res_byte_length= res->length(); /* Must be done before alloc_buffer */
  1934. if (!(res= alloc_buffer(res,str,&tmp_value,byte_count)))
  1935. goto err;
  1936. to= (char*) res->ptr()+res_byte_length;
  1937. ptr_pad=rpad->ptr();
  1938. pad_byte_length= rpad->length();
  1939. count-= res_char_length;
  1940. for ( ; (uint32) count > pad_char_length; count-= pad_char_length)
  1941. {
  1942. memcpy(to,ptr_pad,pad_byte_length);
  1943. to+= pad_byte_length;
  1944. }
  1945. if (count)
  1946. {
  1947. pad_byte_length= rpad->charpos(count);
  1948. memcpy(to,ptr_pad,(size_t) pad_byte_length);
  1949. to+= pad_byte_length;
  1950. }
  1951. res->length(to- (char*) res->ptr());
  1952. return (res);
  1953. err:
  1954. null_value=1;
  1955. return 0;
  1956. }
  1957. void Item_func_lpad::fix_length_and_dec()
  1958. {
  1959. Item *cargs[2];
  1960. cargs[0]= args[0];
  1961. cargs[1]= args[2];
  1962. if (agg_arg_charsets(collation, cargs, 2, MY_COLL_ALLOW_CONV))
  1963. return;
  1964. args[0]= cargs[0];
  1965. args[2]= cargs[1];
  1966. if (args[1]->const_item())
  1967. {
  1968. ulonglong length= ((ulonglong) args[1]->val_int() *
  1969. collation.collation->mbmaxlen);
  1970. if (length >= MAX_BLOB_WIDTH)
  1971. {
  1972. length= MAX_BLOB_WIDTH;
  1973. maybe_null= 1;
  1974. }
  1975. max_length= (ulong) length;
  1976. }
  1977. else
  1978. {
  1979. max_length= MAX_BLOB_WIDTH;
  1980. maybe_null= 1;
  1981. }
  1982. }
  1983. String *Item_func_lpad::val_str(String *str)
  1984. {
  1985. DBUG_ASSERT(fixed == 1);
  1986. uint32 res_char_length,pad_char_length;
  1987. ulong count= (long) args[1]->val_int(), byte_count;
  1988. String *res= args[0]->val_str(&tmp_value);
  1989. String *pad= args[2]->val_str(&lpad_str);
  1990. if (!res || args[1]->null_value || !pad)
  1991. goto err;
  1992. null_value=0;
  1993. res_char_length= res->numchars();
  1994. if (count <= res_char_length)
  1995. {
  1996. res->length(res->charpos(count));
  1997. return res;
  1998. }
  1999. pad_char_length= pad->numchars();
  2000. byte_count= count * collation.collation->mbmaxlen;
  2001. if (byte_count > current_thd->variables.max_allowed_packet)
  2002. {
  2003. push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
  2004. ER_WARN_ALLOWED_PACKET_OVERFLOWED,
  2005. ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
  2006. func_name(), current_thd->variables.max_allowed_packet);
  2007. goto err;
  2008. }
  2009. if (args[2]->null_value || !pad_char_length || str->alloc(byte_count))
  2010. goto err;
  2011. str->length(0);
  2012. str->set_charset(collation.collation);
  2013. count-= res_char_length;
  2014. while (count >= pad_char_length)
  2015. {
  2016. str->append(*pad);
  2017. count-= pad_char_length;
  2018. }
  2019. if (count > 0)
  2020. str->append(pad->ptr(), pad->charpos(count), collation.collation);
  2021. str->append(*res);
  2022. null_value= 0;
  2023. return str;
  2024. err:
  2025. null_value= 1;
  2026. return 0;
  2027. }
  2028. String *Item_func_conv::val_str(String *str)
  2029. {
  2030. DBUG_ASSERT(fixed == 1);
  2031. String *res= args[0]->val_str(str);
  2032. char *endptr,ans[65],*ptr;
  2033. longlong dec;
  2034. int from_base= (int) args[1]->val_int();
  2035. int to_base= (int) args[2]->val_int();
  2036. int err;
  2037. if (args[0]->null_value || args[1]->null_value || args[2]->null_value ||
  2038. abs(to_base) > 36 || abs(to_base) < 2 ||
  2039. abs(from_base) > 36 || abs(from_base) < 2 || !(res->length()))
  2040. {
  2041. null_value=1;
  2042. return 0;
  2043. }
  2044. null_value=0;
  2045. unsigned_flag= !(from_base < 0);
  2046. if (from_base < 0)
  2047. dec= my_strntoll(res->charset(),res->ptr(),res->length(),-from_base,&endptr,&err);
  2048. else
  2049. dec= (longlong) my_strntoull(res->charset(),res->ptr(),res->length(),from_base,&endptr,&err);
  2050. ptr= longlong2str(dec,ans,to_base);
  2051. if (str->copy(ans,(uint32) (ptr-ans), default_charset()))
  2052. return &my_empty_string;
  2053. return str;
  2054. }
  2055. String *Item_func_conv_charset::val_str(String *str)
  2056. {
  2057. DBUG_ASSERT(fixed == 1);
  2058. if (use_cached_value)
  2059. return null_value ? 0 : &str_value;
  2060. String *arg= args[0]->val_str(str);
  2061. uint dummy_errors;
  2062. if (!arg)
  2063. {
  2064. null_value=1;
  2065. return 0;
  2066. }
  2067. null_value= str_value.copy(arg->ptr(),arg->length(),arg->charset(),
  2068. conv_charset, &dummy_errors);
  2069. return null_value ? 0 : &str_value;
  2070. }
  2071. void Item_func_conv_charset::fix_length_and_dec()
  2072. {
  2073. collation.set(conv_charset, DERIVATION_IMPLICIT);
  2074. max_length = args[0]->max_length*conv_charset->mbmaxlen;
  2075. }
  2076. void Item_func_conv_charset::print(String *str)
  2077. {
  2078. str->append("convert(", 8);
  2079. args[0]->print(str);
  2080. str->append(" using ", 7);
  2081. str->append(conv_charset->csname);
  2082. str->append(')');
  2083. }
  2084. String *Item_func_set_collation::val_str(String *str)
  2085. {
  2086. DBUG_ASSERT(fixed == 1);
  2087. str=args[0]->val_str(str);
  2088. if ((null_value=args[0]->null_value))
  2089. return 0;
  2090. str->set_charset(collation.collation);
  2091. return str;
  2092. }
  2093. void Item_func_set_collation::fix_length_and_dec()
  2094. {
  2095. CHARSET_INFO *set_collation;
  2096. const char *colname;
  2097. String tmp, *str= args[1]->val_str(&tmp);
  2098. colname= str->c_ptr();
  2099. if (colname == binary_keyword)
  2100. set_collation= get_charset_by_csname(args[0]->collation.collation->csname,
  2101. MY_CS_BINSORT,MYF(0));
  2102. else
  2103. {
  2104. if (!(set_collation= get_charset_by_name(colname,MYF(0))))
  2105. {
  2106. my_error(ER_UNKNOWN_COLLATION, MYF(0), colname);
  2107. return;
  2108. }
  2109. }
  2110. if (!set_collation ||
  2111. !my_charset_same(args[0]->collation.collation,set_collation))
  2112. {
  2113. my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
  2114. colname,args[0]->collation.collation->csname);
  2115. return;
  2116. }
  2117. collation.set(set_collation, DERIVATION_EXPLICIT);
  2118. max_length= args[0]->max_length;
  2119. }
  2120. bool Item_func_set_collation::eq(const Item *item, bool binary_cmp) const
  2121. {
  2122. /* Assume we don't have rtti */
  2123. if (this == item)
  2124. return 1;
  2125. if (item->type() != FUNC_ITEM)
  2126. return 0;
  2127. Item_func *item_func=(Item_func*) item;
  2128. if (arg_count != item_func->arg_count ||
  2129. func_name() != item_func->func_name())
  2130. return 0;
  2131. Item_func_set_collation *item_func_sc=(Item_func_set_collation*) item;
  2132. if (collation.collation != item_func_sc->collation.collation)
  2133. return 0;
  2134. for (uint i=0; i < arg_count ; i++)
  2135. if (!args[i]->eq(item_func_sc->args[i], binary_cmp))
  2136. return 0;
  2137. return 1;
  2138. }
  2139. String *Item_func_charset::val_str(String *str)
  2140. {
  2141. DBUG_ASSERT(fixed == 1);
  2142. uint dummy_errors;
  2143. CHARSET_INFO *cs= args[0]->collation.collation;
  2144. null_value= 0;
  2145. str->copy(cs->csname, strlen(cs->csname),
  2146. &my_charset_latin1, collation.collation, &dummy_errors);
  2147. return str;
  2148. }
  2149. String *Item_func_collation::val_str(String *str)
  2150. {
  2151. DBUG_ASSERT(fixed == 1);
  2152. uint dummy_errors;
  2153. CHARSET_INFO *cs= args[0]->collation.collation;
  2154. null_value= 0;
  2155. str->copy(cs->name, strlen(cs->name),
  2156. &my_charset_latin1, collation.collation, &dummy_errors);
  2157. return str;
  2158. }
  2159. String *Item_func_hex::val_str(String *str)
  2160. {
  2161. DBUG_ASSERT(fixed == 1);
  2162. if (args[0]->result_type() != STRING_RESULT)
  2163. {
  2164. ulonglong dec;
  2165. char ans[65],*ptr;
  2166. /* Return hex of unsigned longlong value */
  2167. if (args[0]->result_type() == REAL_RESULT)
  2168. {
  2169. double val= args[0]->val();
  2170. if ((val <= (double) LONGLONG_MIN) ||
  2171. (val >= (double) (ulonglong) ULONGLONG_MAX))
  2172. dec= ~(longlong) 0;
  2173. else
  2174. dec= (ulonglong) (val + (val > 0 ? 0.5 : -0.5));
  2175. }
  2176. else
  2177. dec= (ulonglong) args[0]->val_int();
  2178. if ((null_value= args[0]->null_value))
  2179. return 0;
  2180. ptr= longlong2str(dec,ans,16);
  2181. if (str->copy(ans,(uint32) (ptr-ans),default_charset()))
  2182. return &my_empty_string; // End of memory
  2183. return str;
  2184. }
  2185. /* Convert given string to a hex string, character by character */
  2186. String *res= args[0]->val_str(str);
  2187. const char *from, *end;
  2188. char *to;
  2189. if (!res || tmp_value.alloc(res->length()*2))
  2190. {
  2191. null_value=1;
  2192. return 0;
  2193. }
  2194. null_value=0;
  2195. tmp_value.length(res->length()*2);
  2196. for (from=res->ptr(), end=from+res->length(), to= (char*) tmp_value.ptr();
  2197. from < end ;
  2198. from++, to+=2)
  2199. {
  2200. uint tmp=(uint) (uchar) *from;
  2201. to[0]=_dig_vec_upper[tmp >> 4];
  2202. to[1]=_dig_vec_upper[tmp & 15];
  2203. }
  2204. return &tmp_value;
  2205. }
  2206. /* Convert given hex string to a binary string */
  2207. String *Item_func_unhex::val_str(String *str)
  2208. {
  2209. const char *from, *end;
  2210. char *to;
  2211. String *res;
  2212. uint length;
  2213. DBUG_ASSERT(fixed == 1);
  2214. res= args[0]->val_str(str);
  2215. if (!res || tmp_value.alloc(length= (1+res->length())/2))
  2216. {
  2217. null_value=1;
  2218. return 0;
  2219. }
  2220. from= res->ptr();
  2221. null_value= 0;
  2222. tmp_value.length(length);
  2223. to= (char*) tmp_value.ptr();
  2224. if (res->length() % 2)
  2225. {
  2226. int hex_char;
  2227. *to++= hex_char= hexchar_to_int(*from++);
  2228. if ((null_value= (hex_char == -1)))
  2229. return 0;
  2230. }
  2231. for (end=res->ptr()+res->length(); from < end ; from+=2, to++)
  2232. {
  2233. int hex_char;
  2234. *to= (hex_char= hexchar_to_int(from[0])) << 4;
  2235. if ((null_value= (hex_char == -1)))
  2236. return 0;
  2237. *to|= hex_char= hexchar_to_int(from[1]);
  2238. if ((null_value= (hex_char == -1)))
  2239. return 0;
  2240. }
  2241. return &tmp_value;
  2242. }
  2243. void Item_func_binary::print(String *str)
  2244. {
  2245. str->append("cast(", 5);
  2246. args[0]->print(str);
  2247. str->append(" as binary)", 11);
  2248. }
  2249. #include <my_dir.h> // For my_stat
  2250. String *Item_load_file::val_str(String *str)
  2251. {
  2252. DBUG_ASSERT(fixed == 1);
  2253. String *file_name;
  2254. File file;
  2255. MY_STAT stat_info;
  2256. char path[FN_REFLEN];
  2257. DBUG_ENTER("load_file");
  2258. if (!(file_name= args[0]->val_str(str))
  2259. #ifndef NO_EMBEDDED_ACCESS_CHECKS
  2260. || !(current_thd->master_access & FILE_ACL)
  2261. #endif
  2262. )
  2263. goto err;
  2264. (void) fn_format(path, file_name->c_ptr(), mysql_real_data_home, "",
  2265. MY_RELATIVE_PATH | MY_UNPACK_FILENAME);
  2266. if (!my_stat(path, &stat_info, MYF(0)))
  2267. goto err;
  2268. if (!(stat_info.st_mode & S_IROTH))
  2269. {
  2270. /* my_error(ER_TEXTFILE_NOT_READABLE, MYF(0), file_name->c_ptr()); */
  2271. goto err;
  2272. }
  2273. if (stat_info.st_size > (long) current_thd->variables.max_allowed_packet)
  2274. {
  2275. push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
  2276. ER_WARN_ALLOWED_PACKET_OVERFLOWED,
  2277. ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
  2278. func_name(), current_thd->variables.max_allowed_packet);
  2279. goto err;
  2280. }
  2281. if (tmp_value.alloc(stat_info.st_size))
  2282. goto err;
  2283. if ((file = my_open(file_name->c_ptr(), O_RDONLY, MYF(0))) < 0)
  2284. goto err;
  2285. if (my_read(file, (byte*) tmp_value.ptr(), stat_info.st_size, MYF(MY_NABP)))
  2286. {
  2287. my_close(file, MYF(0));
  2288. goto err;
  2289. }
  2290. tmp_value.length(stat_info.st_size);
  2291. my_close(file, MYF(0));
  2292. null_value = 0;
  2293. return &tmp_value;
  2294. err:
  2295. null_value = 1;
  2296. DBUG_RETURN(0);
  2297. }
  2298. String* Item_func_export_set::val_str(String* str)
  2299. {
  2300. DBUG_ASSERT(fixed == 1);
  2301. ulonglong the_set = (ulonglong) args[0]->val_int();
  2302. String yes_buf, *yes;
  2303. yes = args[1]->val_str(&yes_buf);
  2304. String no_buf, *no;
  2305. no = args[2]->val_str(&no_buf);
  2306. String *sep = NULL, sep_buf ;
  2307. uint num_set_values = 64;
  2308. ulonglong mask = 0x1;
  2309. str->length(0);
  2310. str->set_charset(collation.collation);
  2311. /* Check if some argument is a NULL value */
  2312. if (args[0]->null_value || args[1]->null_value || args[2]->null_value)
  2313. {
  2314. null_value=1;
  2315. return 0;
  2316. }
  2317. /*
  2318. Arg count can only be 3, 4 or 5 here. This is guaranteed from the
  2319. grammar for EXPORT_SET()
  2320. */
  2321. switch(arg_count) {
  2322. case 5:
  2323. num_set_values = (uint) args[4]->val_int();
  2324. if (num_set_values > 64)
  2325. num_set_values=64;
  2326. if (args[4]->null_value)
  2327. {
  2328. null_value=1;
  2329. return 0;
  2330. }
  2331. /* Fall through */
  2332. case 4:
  2333. if (!(sep = args[3]->val_str(&sep_buf))) // Only true if NULL
  2334. {
  2335. null_value=1;
  2336. return 0;
  2337. }
  2338. break;
  2339. case 3:
  2340. {
  2341. /* errors is not checked - assume "," can always be converted */
  2342. uint errors;
  2343. sep_buf.copy(",", 1, &my_charset_bin, collation.collation, &errors);
  2344. sep = &sep_buf;
  2345. }
  2346. break;
  2347. default:
  2348. DBUG_ASSERT(0); // cannot happen
  2349. }
  2350. null_value=0;
  2351. for (uint i = 0; i < num_set_values; i++, mask = (mask << 1))
  2352. {
  2353. if (the_set & mask)
  2354. str->append(*yes);
  2355. else
  2356. str->append(*no);
  2357. if (i != num_set_values - 1)
  2358. str->append(*sep);
  2359. }
  2360. return str;
  2361. }
  2362. void Item_func_export_set::fix_length_and_dec()
  2363. {
  2364. uint length=max(args[1]->max_length,args[2]->max_length);
  2365. uint sep_length=(arg_count > 3 ? args[3]->max_length : 1);
  2366. max_length=length*64+sep_length*63;
  2367. if (agg_arg_charsets(collation, args+1, min(4,arg_count)-1,
  2368. MY_COLL_ALLOW_CONV))
  2369. return;
  2370. }
  2371. String* Item_func_inet_ntoa::val_str(String* str)
  2372. {
  2373. DBUG_ASSERT(fixed == 1);
  2374. uchar buf[8], *p;
  2375. ulonglong n = (ulonglong) args[0]->val_int();
  2376. char num[4];
  2377. /*
  2378. We do not know if args[0] is NULL until we have called
  2379. some val function on it if args[0] is not a constant!
  2380. Also return null if n > 255.255.255.255
  2381. */
  2382. if ((null_value= (args[0]->null_value || n > (ulonglong) LL(4294967295))))
  2383. return 0; // Null value
  2384. str->length(0);
  2385. int4store(buf,n);
  2386. /* Now we can assume little endian. */
  2387. num[3]='.';
  2388. for (p=buf+4 ; p-- > buf ; )
  2389. {
  2390. uint c = *p;
  2391. uint n1,n2; // Try to avoid divisions
  2392. n1= c / 100; // 100 digits
  2393. c-= n1*100;
  2394. n2= c / 10; // 10 digits
  2395. c-=n2*10; // last digit
  2396. num[0]=(char) n1+'0';
  2397. num[1]=(char) n2+'0';
  2398. num[2]=(char) c+'0';
  2399. uint length=(n1 ? 4 : n2 ? 3 : 2); // Remove pre-zero
  2400. (void) str->append(num+4-length,length);
  2401. }
  2402. str->length(str->length()-1); // Remove last '.';
  2403. return str;
  2404. }
  2405. /*
  2406. QUOTE() function returns argument string in single quotes suitable for
  2407. using in a SQL statement.
  2408. DESCRIPTION
  2409. Adds a \ before all characters that needs to be escaped in a SQL string.
  2410. We also escape '^Z' (END-OF-FILE in windows) to avoid probelms when
  2411. running commands from a file in windows.
  2412. This function is very useful when you want to generate SQL statements
  2413. NOTE
  2414. QUOTE(NULL) returns the string 'NULL' (4 letters, without quotes).
  2415. RETURN VALUES
  2416. str Quoted string
  2417. NULL Out of memory.
  2418. */
  2419. #define get_esc_bit(mask, num) (1 & (*((mask) + ((num) >> 3))) >> ((num) & 7))
  2420. String *Item_func_quote::val_str(String *str)
  2421. {
  2422. DBUG_ASSERT(fixed == 1);
  2423. /*
  2424. Bit mask that has 1 for set for the position of the following characters:
  2425. 0, \, ' and ^Z
  2426. */
  2427. static uchar escmask[32]=
  2428. {
  2429. 0x01, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00, 0x00,
  2430. 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,
  2431. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2432. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  2433. };
  2434. char *from, *to, *end, *start;
  2435. String *arg= args[0]->val_str(str);
  2436. uint arg_length, new_length;
  2437. if (!arg) // Null argument
  2438. {
  2439. str->copy("NULL", 4, collation.collation); // Return the string 'NULL'
  2440. null_value= 0;
  2441. return str;
  2442. }
  2443. arg_length= arg->length();
  2444. new_length= arg_length+2; /* for beginning and ending ' signs */
  2445. for (from= (char*) arg->ptr(), end= from + arg_length; from < end; from++)
  2446. new_length+= get_esc_bit(escmask, (uchar) *from);
  2447. if (tmp_value.alloc(new_length))
  2448. goto null;
  2449. /*
  2450. We replace characters from the end to the beginning
  2451. */
  2452. to= (char*) tmp_value.ptr() + new_length - 1;
  2453. *to--= '\'';
  2454. for (start= (char*) arg->ptr(),end= start + arg_length; end-- != start; to--)
  2455. {
  2456. /*
  2457. We can't use the bitmask here as we want to replace \O and ^Z with 0
  2458. and Z
  2459. */
  2460. switch (*end) {
  2461. case 0:
  2462. *to--= '0';
  2463. *to= '\\';
  2464. break;
  2465. case '\032':
  2466. *to--= 'Z';
  2467. *to= '\\';
  2468. break;
  2469. case '\'':
  2470. case '\\':
  2471. *to--= *end;
  2472. *to= '\\';
  2473. break;
  2474. default:
  2475. *to= *end;
  2476. break;
  2477. }
  2478. }
  2479. *to= '\'';
  2480. tmp_value.length(new_length);
  2481. tmp_value.set_charset(collation.collation);
  2482. null_value= 0;
  2483. return &tmp_value;
  2484. null:
  2485. null_value= 1;
  2486. return 0;
  2487. }
  2488. longlong Item_func_uncompressed_length::val_int()
  2489. {
  2490. DBUG_ASSERT(fixed == 1);
  2491. String *res= args[0]->val_str(&value);
  2492. if (!res)
  2493. {
  2494. null_value=1;
  2495. return 0; /* purecov: inspected */
  2496. }
  2497. null_value=0;
  2498. if (res->is_empty()) return 0;
  2499. /*
  2500. res->ptr() using is safe because we have tested that string is not empty,
  2501. res->c_ptr() is not used because:
  2502. - we do not need \0 terminated string to get first 4 bytes
  2503. - c_ptr() tests simbol after string end (uninitialiozed memory) which
  2504. confuse valgrind
  2505. */
  2506. return uint4korr(res->ptr()) & 0x3FFFFFFF;
  2507. }
  2508. longlong Item_func_crc32::val_int()
  2509. {
  2510. DBUG_ASSERT(fixed == 1);
  2511. String *res=args[0]->val_str(&value);
  2512. if (!res)
  2513. {
  2514. null_value=1;
  2515. return 0; /* purecov: inspected */
  2516. }
  2517. null_value=0;
  2518. return (longlong) crc32(0L, (uchar*)res->ptr(), res->length());
  2519. }
  2520. #ifdef HAVE_COMPRESS
  2521. #include "zlib.h"
  2522. String *Item_func_compress::val_str(String *str)
  2523. {
  2524. int err= Z_OK, code;
  2525. ulong new_size;
  2526. String *res;
  2527. Byte *body;
  2528. char *tmp, *last_char;
  2529. DBUG_ASSERT(fixed == 1);
  2530. if (!(res= args[0]->val_str(str)))
  2531. {
  2532. null_value= 1;
  2533. return 0;
  2534. }
  2535. null_value= 0;
  2536. if (res->is_empty()) return res;
  2537. /*
  2538. Citation from zlib.h (comment for compress function):
  2539. Compresses the source buffer into the destination buffer. sourceLen is
  2540. the byte length of the source buffer. Upon entry, destLen is the total
  2541. size of the destination buffer, which must be at least 0.1% larger than
  2542. sourceLen plus 12 bytes.
  2543. We assume here that the buffer can't grow more than .25 %.
  2544. */
  2545. new_size= res->length() + res->length() / 5 + 12;
  2546. // Check new_size overflow: new_size <= res->length()
  2547. if (((uint32) (new_size+5) <= res->length()) ||
  2548. buffer.realloc((uint32) new_size + 4 + 1))
  2549. {
  2550. null_value= 1;
  2551. return 0;
  2552. }
  2553. body= ((Byte*)buffer.ptr()) + 4;
  2554. // As far as we have checked res->is_empty() we can use ptr()
  2555. if ((err= compress(body, &new_size,
  2556. (const Bytef*)res->ptr(), res->length())) != Z_OK)
  2557. {
  2558. code= err==Z_MEM_ERROR ? ER_ZLIB_Z_MEM_ERROR : ER_ZLIB_Z_BUF_ERROR;
  2559. push_warning(current_thd,MYSQL_ERROR::WARN_LEVEL_ERROR,code,ER(code));
  2560. null_value= 1;
  2561. return 0;
  2562. }
  2563. tmp= (char*)buffer.ptr(); // int4store is a macro; avoid side effects
  2564. int4store(tmp, res->length() & 0x3FFFFFFF);
  2565. /* This is to ensure that things works for CHAR fields, which trim ' ': */
  2566. last_char= ((char*)body)+new_size-1;
  2567. if (*last_char == ' ')
  2568. {
  2569. *++last_char= '.';
  2570. new_size++;
  2571. }
  2572. buffer.length((uint32)new_size + 4);
  2573. return &buffer;
  2574. }
  2575. String *Item_func_uncompress::val_str(String *str)
  2576. {
  2577. DBUG_ASSERT(fixed == 1);
  2578. String *res= args[0]->val_str(str);
  2579. ulong new_size;
  2580. int err;
  2581. uint code;
  2582. if (!res)
  2583. goto err;
  2584. null_value= 0;
  2585. if (res->is_empty())
  2586. return res;
  2587. new_size= uint4korr(res->ptr()) & 0x3FFFFFFF;
  2588. if (new_size > current_thd->variables.max_allowed_packet)
  2589. {
  2590. push_warning_printf(current_thd,MYSQL_ERROR::WARN_LEVEL_ERROR,
  2591. ER_TOO_BIG_FOR_UNCOMPRESS,
  2592. ER(ER_TOO_BIG_FOR_UNCOMPRESS),
  2593. current_thd->variables.max_allowed_packet);
  2594. goto err;
  2595. }
  2596. if (buffer.realloc((uint32)new_size))
  2597. goto err;
  2598. if ((err= uncompress((Byte*)buffer.ptr(), &new_size,
  2599. ((const Bytef*)res->ptr())+4,res->length())) == Z_OK)
  2600. {
  2601. buffer.length((uint32) new_size);
  2602. return &buffer;
  2603. }
  2604. code= ((err == Z_BUF_ERROR) ? ER_ZLIB_Z_BUF_ERROR :
  2605. ((err == Z_MEM_ERROR) ? ER_ZLIB_Z_MEM_ERROR : ER_ZLIB_Z_DATA_ERROR));
  2606. push_warning(current_thd,MYSQL_ERROR::WARN_LEVEL_ERROR,code,ER(code));
  2607. err:
  2608. null_value= 1;
  2609. return 0;
  2610. }
  2611. #endif
  2612. /*
  2613. UUID, as in
  2614. DCE 1.1: Remote Procedure Call,
  2615. Open Group Technical Standard Document Number C706, October 1997,
  2616. (supersedes C309 DCE: Remote Procedure Call 8/1994,
  2617. which was basis for ISO/IEC 11578:1996 specification)
  2618. */
  2619. static struct rand_struct uuid_rand;
  2620. static uint nanoseq;
  2621. static ulonglong uuid_time=0;
  2622. static char clock_seq_and_node_str[]="-0000-000000000000";
  2623. /* number of 100-nanosecond intervals between
  2624. 1582-10-15 00:00:00.00 and 1970-01-01 00:00:00.00 */
  2625. #define UUID_TIME_OFFSET ((ulonglong) 141427 * 24 * 60 * 60 * 1000 * 10 )
  2626. #define UUID_VERSION 0x1000
  2627. #define UUID_VARIANT 0x8000
  2628. static void tohex(char *to, uint from, uint len)
  2629. {
  2630. to+= len;
  2631. while (len--)
  2632. {
  2633. *--to= _dig_vec_lower[from & 15];
  2634. from >>= 4;
  2635. }
  2636. }
  2637. static void set_clock_seq_str()
  2638. {
  2639. uint16 clock_seq= ((uint)(my_rnd(&uuid_rand)*16383)) | UUID_VARIANT;
  2640. tohex(clock_seq_and_node_str+1, clock_seq, 4);
  2641. nanoseq= 0;
  2642. }
  2643. String *Item_func_uuid::val_str(String *str)
  2644. {
  2645. DBUG_ASSERT(fixed == 1);
  2646. char *s;
  2647. pthread_mutex_lock(&LOCK_uuid_generator);
  2648. if (! uuid_time) /* first UUID() call. initializing data */
  2649. {
  2650. ulong tmp=sql_rnd_with_mutex();
  2651. uchar mac[6];
  2652. int i;
  2653. if (my_gethwaddr(mac))
  2654. {
  2655. /*
  2656. generating random "hardware addr"
  2657. and because specs explicitly specify that it should NOT correlate
  2658. with a clock_seq value (initialized random below), we use a separate
  2659. randominit() here
  2660. */
  2661. randominit(&uuid_rand, tmp + (ulong)current_thd, tmp + query_id);
  2662. for (i=0; i < (int)sizeof(mac); i++)
  2663. mac[i]=(uchar)(my_rnd(&uuid_rand)*255);
  2664. }
  2665. s=clock_seq_and_node_str+sizeof(clock_seq_and_node_str)-1;
  2666. for (i=sizeof(mac)-1 ; i>=0 ; i--)
  2667. {
  2668. *--s=_dig_vec_lower[mac[i] & 15];
  2669. *--s=_dig_vec_lower[mac[i] >> 4];
  2670. }
  2671. randominit(&uuid_rand, tmp + (ulong)start_time, tmp + bytes_sent);
  2672. set_clock_seq_str();
  2673. }
  2674. ulonglong tv=my_getsystime() + UUID_TIME_OFFSET + nanoseq;
  2675. if (unlikely(tv < uuid_time))
  2676. set_clock_seq_str();
  2677. else
  2678. if (unlikely(tv == uuid_time))
  2679. { /* special protection from low-res system clocks */
  2680. nanoseq++;
  2681. tv++;
  2682. }
  2683. else
  2684. {
  2685. if (nanoseq)
  2686. {
  2687. tv-=nanoseq;
  2688. nanoseq=0;
  2689. }
  2690. DBUG_ASSERT(tv > uuid_time);
  2691. }
  2692. uuid_time=tv;
  2693. pthread_mutex_unlock(&LOCK_uuid_generator);
  2694. uint32 time_low= (uint32) (tv & 0xFFFFFFFF);
  2695. uint16 time_mid= (uint16) ((tv >> 32) & 0xFFFF);
  2696. uint16 time_hi_and_version= (uint16) ((tv >> 48) | UUID_VERSION);
  2697. str->realloc(UUID_LENGTH+1);
  2698. str->length(UUID_LENGTH);
  2699. str->set_charset(system_charset_info);
  2700. s=(char *) str->ptr();
  2701. s[8]=s[13]='-';
  2702. tohex(s, time_low, 8);
  2703. tohex(s+9, time_mid, 4);
  2704. tohex(s+14, time_hi_and_version, 4);
  2705. strmov(s+18, clock_seq_and_node_str);
  2706. return str;
  2707. }