1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
#include <drizzled/server_includes.h>
22
#include <drizzled/functions/str/hex.h>
24
String *Item_func_hex::val_str(String *str)
28
if (args[0]->result_type() != STRING_RESULT)
32
/* Return hex of unsigned int64_t value */
33
if (args[0]->result_type() == REAL_RESULT ||
34
args[0]->result_type() == DECIMAL_RESULT)
36
double val= args[0]->val_real();
37
if ((val <= (double) INT64_MIN) ||
38
(val >= (double) (uint64_t) UINT64_MAX))
41
dec= (uint64_t) (val + (val > 0 ? 0.5 : -0.5));
44
dec= (uint64_t) args[0]->val_int();
46
if ((null_value= args[0]->null_value))
48
ptr= int64_t2str(dec,ans,16);
49
if (str->copy(ans,(uint32_t) (ptr-ans),default_charset()))
50
return &my_empty_string; // End of memory
54
/* Convert given string to a hex string, character by character */
55
res= args[0]->val_str(str);
56
if (!res || tmp_value.alloc(res->length()*2+1))
62
tmp_value.length(res->length()*2);
64
octet2hex((char*) tmp_value.ptr(), res->ptr(), res->length());
68
/** Convert given hex string to a binary string. */
70
String *Item_func_unhex::val_str(String *str)
72
const char *from, *end;
78
res= args[0]->val_str(str);
79
if (!res || tmp_value.alloc(length= (1+res->length())/2))
87
tmp_value.length(length);
88
to= (char*) tmp_value.ptr();
89
if (res->length() % 2)
92
*to++= hex_char= hexchar_to_int(*from++);
93
if ((null_value= (hex_char == -1)))
96
for (end=res->ptr()+res->length(); from < end ; from+=2, to++)
99
*to= (hex_char= hexchar_to_int(from[0])) << 4;
100
if ((null_value= (hex_char == -1)))
102
*to|= hex_char= hexchar_to_int(from[1]);
103
if ((null_value= (hex_char == -1)))