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"
29
convert a hex digit into number.
31
static int hexchar_to_int(char c)
33
if (c <= '9' && c >= '0')
36
if (c <= 'f' && c >= 'a')
41
String *Item_func_hex::val_str(String *str)
45
if (args[0]->result_type() != STRING_RESULT)
49
/* Return hex of unsigned int64_t value */
50
if (args[0]->result_type() == REAL_RESULT ||
51
args[0]->result_type() == DECIMAL_RESULT)
53
double val= args[0]->val_real();
54
if ((val <= (double) INT64_MIN) ||
55
(val >= (double) (uint64_t) UINT64_MAX))
58
dec= (uint64_t) (val + (val > 0 ? 0.5 : -0.5));
61
dec= (uint64_t) args[0]->val_int();
63
if ((null_value= args[0]->null_value))
65
ptr= internal::int64_t2str(dec,ans,16);
66
if (str->copy(ans,(uint32_t) (ptr-ans),default_charset()))
67
return &my_empty_string; // End of memory
71
/* Convert given string to a hex string, character by character */
72
res= args[0]->val_str(str);
73
if (!res || tmp_value.alloc(res->length()*2+1))
79
tmp_value.length(res->length()*2);
81
(void) drizzled_string_to_hex((char*) tmp_value.ptr(), res->ptr(),
86
/** Convert given hex string to a binary string. */
88
String *Item_func_unhex::val_str(String *str)
90
const char *from, *end;
96
res= args[0]->val_str(str);
97
if (!res || tmp_value.alloc(length= (1+res->length())/2))
105
tmp_value.length(length);
106
to= (char*) tmp_value.ptr();
107
if (res->length() % 2)
110
*to++= hex_char= hexchar_to_int(*from++);
111
if ((null_value= (hex_char == -1)))
114
for (end=res->ptr()+res->length(); from < end ; from+=2, to++)
117
*to= (hex_char= hexchar_to_int(from[0])) << 4;
118
if ((null_value= (hex_char == -1)))
120
*to|= hex_char= hexchar_to_int(from[1]);
121
if ((null_value= (hex_char == -1)))
127
} /* namespace drizzled */