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/internal/m_string.h"
24
#include <drizzled/plugin/function.h>
25
#include <drizzled/function/str/strfunc.h>
27
using namespace drizzled;
29
class HexFunction :public Item_str_func
33
HexFunction() :Item_str_func() {}
34
const char *func_name() const { return "hex"; }
35
String *val_str(String *);
36
void fix_length_and_dec()
38
collation.set(default_charset());
40
max_length=args[0]->max_length*2*collation.collation->mbmaxlen;
43
bool check_argument_count(int n) { return n == 1; }
46
class UnHexFunction :public Item_str_func
50
UnHexFunction() :Item_str_func()
52
/* there can be bad hex strings */
55
const char *func_name() const { return "unhex"; }
56
String *val_str(String *);
57
void fix_length_and_dec()
59
collation.set(&my_charset_bin);
61
max_length=(1+args[0]->max_length)/2;
63
bool check_argument_count(int n) { return n == 1; }
67
convert a hex digit into number.
69
static int hexchar_to_int(char c)
71
if (c <= '9' && c >= '0')
74
if (c <= 'f' && c >= 'a')
79
String *HexFunction::val_str(String *str)
83
if (args[0]->result_type() != STRING_RESULT)
87
/* Return hex of unsigned int64_t value */
88
if (args[0]->result_type() == REAL_RESULT ||
89
args[0]->result_type() == DECIMAL_RESULT)
91
double val= args[0]->val_real();
92
if ((val <= (double) INT64_MIN) ||
93
(val >= (double) (uint64_t) UINT64_MAX))
96
dec= (uint64_t) (val + (val > 0 ? 0.5 : -0.5));
99
dec= (uint64_t) args[0]->val_int();
101
if ((null_value= args[0]->null_value))
103
ptr= internal::int64_t2str(dec,ans,16);
104
if (str->copy(ans,(uint32_t) (ptr-ans),default_charset()))
105
return &my_empty_string; // End of memory
109
/* Convert given string to a hex string, character by character */
110
res= args[0]->val_str(str);
111
if (!res || tmp_value.alloc(res->length()*2+1))
117
tmp_value.length(res->length()*2);
119
(void) drizzled_string_to_hex((char*) tmp_value.ptr(), res->ptr(),
124
/** Convert given hex string to a binary string. */
126
String *UnHexFunction::val_str(String *str)
128
const char *from, *end;
134
res= args[0]->val_str(str);
135
if (!res || tmp_value.alloc(length= (1+res->length())/2))
143
tmp_value.length(length);
144
to= (char*) tmp_value.ptr();
145
if (res->length() % 2)
148
*to++= hex_char= hexchar_to_int(*from++);
149
if ((null_value= (hex_char == -1)))
152
for (end=res->ptr()+res->length(); from < end ; from+=2, to++)
155
*to= (hex_char= hexchar_to_int(from[0])) << 4;
156
if ((null_value= (hex_char == -1)))
158
*to|= hex_char= hexchar_to_int(from[1]);
159
if ((null_value= (hex_char == -1)))
165
plugin::Create_function<HexFunction> *hex_function= NULL;
166
plugin::Create_function<UnHexFunction> *unhex_function= NULL;
168
static int initialize(drizzled::plugin::Registry ®istry)
170
hex_function= new plugin::Create_function<HexFunction>("hex");
171
unhex_function= new plugin::Create_function<UnHexFunction>("unhex");
172
registry.add(hex_function);
173
registry.add(unhex_function);
177
static int finalize(drizzled::plugin::Registry ®istry)
179
registry.remove(hex_function);
180
registry.remove(unhex_function);
182
delete unhex_function;
186
DRIZZLE_DECLARE_PLUGIN
192
"Convert a string to HEX() or from UNHEX()",
194
initialize, /* Plugin Init */
195
finalize, /* Plugin Deinit */
196
NULL, /* status variables */
197
NULL, /* system variables */
198
NULL /* config options */
200
DRIZZLE_DECLARE_PLUGIN_END;