diff options
author | unknown <serg@serg.mylan> | 2004-03-04 23:18:54 +0100 |
---|---|---|
committer | unknown <serg@serg.mylan> | 2004-03-04 23:18:54 +0100 |
commit | 1530dd73a036d39358bc1057a471f0162894db2a (patch) | |
tree | 9b95ffe2c52174cda86a1c3cbb8ad987be1fcea2 /sql/item_strfunc.cc | |
parent | ceacb0346d8df3f22a27ff1210411be663c3ea89 (diff) | |
download | mariadb-git-1530dd73a036d39358bc1057a471f0162894db2a.tar.gz |
UNHEX() function
Diffstat (limited to 'sql/item_strfunc.cc')
-rw-r--r-- | sql/item_strfunc.cc | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/sql/item_strfunc.cc b/sql/item_strfunc.cc index fdf65a1f834..b9604cf900b 100644 --- a/sql/item_strfunc.cc +++ b/sql/item_strfunc.cc @@ -2231,7 +2231,7 @@ String *Item_func_hex::val_str(String *str) null_value=0; tmp_value.length(res->length()*2); for (from=res->ptr(), end=from+res->length(), to= (char*) tmp_value.ptr(); - from != end ; + from < end ; from++, to+=2) { uint tmp=(uint) (uchar) *from; @@ -2241,6 +2241,48 @@ String *Item_func_hex::val_str(String *str) return &tmp_value; } +int inline hexchar_to_int(char c) +{ + if (c <= '9' && c >= '0') + return c-'0'; + c|=32; + if (c <= 'f' && c >= 'a') + return c-'a'+10; + return -1; +} + +String *Item_func_unhex::val_str(String *str) +{ + /* Convert given hex string to a binary string */ + String *res= args[0]->val_str(str); + const char *from=res->ptr(), *end; + char *to; + int r; + if (!res || tmp_value.alloc((1+res->length())/2)) + { + null_value=1; + return 0; + } + null_value=0; + tmp_value.length((1+res->length())/2); + to= (char*) tmp_value.ptr(); + if (res->length() % 2) + { + *to++= r= hexchar_to_int(*from++); + if ((null_value= (r == -1))) + return 0; + } + for (end=res->ptr()+res->length(); from < end ; from+=2, to++) + { + *to= (r= hexchar_to_int(from[0])) << 4; + if ((null_value= (r == -1))) + return 0; + *to|= r= hexchar_to_int(from[1]); + if ((null_value= (r == -1))) + return 0; + } + return &tmp_value; +} void Item_func_binary::print(String *str) { |