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
21
#include <drizzled/util/convert.h>
22
#include <drizzled/function/str/hex.h>
23
#include "drizzled/internal/m_string.h"
26
convert a hex digit into number.
28
static int hexchar_to_int(char c)
30
if (c <= '9' && c >= '0')
33
if (c <= 'f' && c >= 'a')
38
String *Item_func_hex::val_str(String *str)
42
if (args[0]->result_type() != STRING_RESULT)
46
/* Return hex of unsigned int64_t value */
47
if (args[0]->result_type() == REAL_RESULT ||
48
args[0]->result_type() == DECIMAL_RESULT)
50
double val= args[0]->val_real();
51
if ((val <= (double) INT64_MIN) ||
52
(val >= (double) (uint64_t) UINT64_MAX))
55
dec= (uint64_t) (val + (val > 0 ? 0.5 : -0.5));
58
dec= (uint64_t) args[0]->val_int();
60
if ((null_value= args[0]->null_value))
62
ptr= int64_t2str(dec,ans,16);
63
if (str->copy(ans,(uint32_t) (ptr-ans),default_charset()))
64
return &my_empty_string; // End of memory
68
/* Convert given string to a hex string, character by character */
69
res= args[0]->val_str(str);
70
if (!res || tmp_value.alloc(res->length()*2+1))
76
tmp_value.length(res->length()*2);
78
(void) drizzled_string_to_hex((char*) tmp_value.ptr(), res->ptr(),
83
/** Convert given hex string to a binary string. */
85
String *Item_func_unhex::val_str(String *str)
87
const char *from, *end;
93
res= args[0]->val_str(str);
94
if (!res || tmp_value.alloc(length= (1+res->length())/2))
102
tmp_value.length(length);
103
to= (char*) tmp_value.ptr();
104
if (res->length() % 2)
107
*to++= hex_char= hexchar_to_int(*from++);
108
if ((null_value= (hex_char == -1)))
111
for (end=res->ptr()+res->length(); from < end ; from+=2, to++)
114
*to= (hex_char= hexchar_to_int(from[0])) << 4;
115
if ((null_value= (hex_char == -1)))
117
*to|= hex_char= hexchar_to_int(from[1]);
118
if ((null_value= (hex_char == -1)))