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>
21
#include <drizzled/util/convert.h>
23
#include <drizzled/function/str/hex.h>
25
String *Item_func_hex::val_str(String *str)
29
if (args[0]->result_type() != STRING_RESULT)
33
/* Return hex of unsigned int64_t value */
34
if (args[0]->result_type() == REAL_RESULT ||
35
args[0]->result_type() == DECIMAL_RESULT)
37
double val= args[0]->val_real();
38
if ((val <= (double) INT64_MIN) ||
39
(val >= (double) (uint64_t) UINT64_MAX))
42
dec= (uint64_t) (val + (val > 0 ? 0.5 : -0.5));
45
dec= (uint64_t) args[0]->val_int();
47
if ((null_value= args[0]->null_value))
49
ptr= int64_t2str(dec,ans,16);
50
if (str->copy(ans,(uint32_t) (ptr-ans),default_charset()))
51
return &my_empty_string; // End of memory
55
/* Convert given string to a hex string, character by character */
56
res= args[0]->val_str(str);
57
if (!res || tmp_value.alloc(res->length()*2+1))
63
tmp_value.length(res->length()*2);
65
(void) drizzled_string_to_hex((char*) tmp_value.ptr(), res->ptr(),
70
/** Convert given hex string to a binary string. */
72
String *Item_func_unhex::val_str(String *str)
74
const char *from, *end;
80
res= args[0]->val_str(str);
81
if (!res || tmp_value.alloc(length= (1+res->length())/2))
89
tmp_value.length(length);
90
to= (char*) tmp_value.ptr();
91
if (res->length() % 2)
94
*to++= hex_char= hexchar_to_int(*from++);
95
if ((null_value= (hex_char == -1)))
98
for (end=res->ptr()+res->length(); from < end ; from+=2, to++)
101
*to= (hex_char= hexchar_to_int(from[0])) << 4;
102
if ((null_value= (hex_char == -1)))
104
*to|= hex_char= hexchar_to_int(from[1]);
105
if ((null_value= (hex_char == -1)))