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, Inc.
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
22
#include <drizzled/charset_info.h>
23
#include <drizzled/function/str/strfunc.h>
24
#include <drizzled/internal/m_string.h>
25
#include <drizzled/plugin/function.h>
26
#include <drizzled/util/convert.h>
28
using namespace drizzled;
30
class HexFunction :public Item_str_func
34
HexFunction() :Item_str_func() {}
35
const char *func_name() const { return "hex"; }
36
String *val_str(String *);
37
void fix_length_and_dec()
39
collation.set(default_charset());
41
max_length=args[0]->max_length*2*collation.collation->mbmaxlen;
44
bool check_argument_count(int n) { return n == 1; }
47
class UnHexFunction :public Item_str_func
51
UnHexFunction() :Item_str_func()
53
/* there can be bad hex strings */
56
const char *func_name() const { return "unhex"; }
57
String *val_str(String *);
58
void fix_length_and_dec()
60
collation.set(&my_charset_bin);
62
max_length=(1+args[0]->max_length)/2;
64
bool check_argument_count(int n) { return n == 1; }
68
convert a hex digit into number.
70
static int hexchar_to_int(char c)
72
if (c <= '9' && c >= '0')
75
if (c <= 'f' && c >= 'a')
80
String *HexFunction::val_str(String *str)
84
if (args[0]->result_type() != STRING_RESULT)
88
/* Return hex of unsigned int64_t value */
89
if (args[0]->result_type() == REAL_RESULT ||
90
args[0]->result_type() == DECIMAL_RESULT)
92
double val= args[0]->val_real();
93
if ((val <= (double) INT64_MIN) ||
94
(val >= (double) (uint64_t) UINT64_MAX))
97
dec= (uint64_t) (val + (val > 0 ? 0.5 : -0.5));
100
dec= (uint64_t) args[0]->val_int();
102
if ((null_value= args[0]->null_value))
104
ptr= internal::int64_t2str(dec,ans,16);
105
if (str->copy(ans,(uint32_t) (ptr-ans),default_charset()))
106
return &my_empty_string; // End of memory
110
/* Convert given string to a hex string, character by character */
111
res= args[0]->val_str(str);
112
if (!res || tmp_value.alloc(res->length()*2+1))
118
tmp_value.length(res->length()*2);
120
(void) drizzled_string_to_hex((char*) tmp_value.ptr(), res->ptr(),
125
/** Convert given hex string to a binary string. */
127
String *UnHexFunction::val_str(String *str)
129
const char *from, *end;
135
res= args[0]->val_str(str);
136
if (!res || tmp_value.alloc(length= (1+res->length())/2))
144
tmp_value.length(length);
145
to= (char*) tmp_value.ptr();
146
if (res->length() % 2)
149
*to++= hex_char= hexchar_to_int(*from++);
150
if ((null_value= (hex_char == -1)))
153
for (end=res->ptr()+res->length(); from < end ; from+=2, to++)
156
*to= (hex_char= hexchar_to_int(from[0])) << 4;
157
if ((null_value= (hex_char == -1)))
159
*to|= hex_char= hexchar_to_int(from[1]);
160
if ((null_value= (hex_char == -1)))
166
plugin::Create_function<HexFunction> *hex_function= NULL;
167
plugin::Create_function<UnHexFunction> *unhex_function= NULL;
169
static int initialize(drizzled::module::Context &context)
171
hex_function= new plugin::Create_function<HexFunction>("hex");
172
unhex_function= new plugin::Create_function<UnHexFunction>("unhex");
173
context.add(hex_function);
174
context.add(unhex_function);
178
DRIZZLE_DECLARE_PLUGIN
184
"Convert a string to HEX() or from UNHEX()",
186
initialize, /* Plugin Init */
188
NULL /* config options */
190
DRIZZLE_DECLARE_PLUGIN_END;