1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
*
* Copyright (C) 2008 Sun Microsystems, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include <drizzled/util/convert.h>
#include "drizzled/internal/m_string.h"
#include <drizzled/plugin/function.h>
#include <drizzled/function/str/strfunc.h>
using namespace drizzled;
class HexFunction :public Item_str_func
{
String tmp_value;
public:
HexFunction() :Item_str_func() {}
const char *func_name() const { return "hex"; }
String *val_str(String *);
void fix_length_and_dec()
{
collation.set(default_charset());
decimals=0;
max_length=args[0]->max_length*2*collation.collation->mbmaxlen;
}
bool check_argument_count(int n) { return n == 1; }
};
class UnHexFunction :public Item_str_func
{
String tmp_value;
public:
UnHexFunction() :Item_str_func()
{
/* there can be bad hex strings */
maybe_null= 1;
}
const char *func_name() const { return "unhex"; }
String *val_str(String *);
void fix_length_and_dec()
{
collation.set(&my_charset_bin);
decimals=0;
max_length=(1+args[0]->max_length)/2;
}
bool check_argument_count(int n) { return n == 1; }
};
/**
convert a hex digit into number.
*/
static int hexchar_to_int(char c)
{
if (c <= '9' && c >= '0')
return c-'0';
c|=32;
if (c <= 'f' && c >= 'a')
return c-'a'+10;
return -1;
}
String *HexFunction::val_str(String *str)
{
String *res;
assert(fixed == 1);
if (args[0]->result_type() != STRING_RESULT)
{
uint64_t dec;
char ans[65],*ptr;
/* Return hex of unsigned int64_t value */
if (args[0]->result_type() == REAL_RESULT ||
args[0]->result_type() == DECIMAL_RESULT)
{
double val= args[0]->val_real();
if ((val <= (double) INT64_MIN) ||
(val >= (double) (uint64_t) UINT64_MAX))
dec= ~(int64_t) 0;
else
dec= (uint64_t) (val + (val > 0 ? 0.5 : -0.5));
}
else
dec= (uint64_t) args[0]->val_int();
if ((null_value= args[0]->null_value))
return 0;
ptr= internal::int64_t2str(dec,ans,16);
if (str->copy(ans,(uint32_t) (ptr-ans),default_charset()))
return &my_empty_string; // End of memory
return str;
}
/* Convert given string to a hex string, character by character */
res= args[0]->val_str(str);
if (!res || tmp_value.alloc(res->length()*2+1))
{
null_value=1;
return 0;
}
null_value=0;
tmp_value.length(res->length()*2);
(void) drizzled_string_to_hex((char*) tmp_value.ptr(), res->ptr(),
res->length());
return &tmp_value;
}
/** Convert given hex string to a binary string. */
String *UnHexFunction::val_str(String *str)
{
const char *from, *end;
char *to;
String *res;
uint32_t length;
assert(fixed == 1);
res= args[0]->val_str(str);
if (!res || tmp_value.alloc(length= (1+res->length())/2))
{
null_value=1;
return 0;
}
from= res->ptr();
null_value= 0;
tmp_value.length(length);
to= (char*) tmp_value.ptr();
if (res->length() % 2)
{
int hex_char;
*to++= hex_char= hexchar_to_int(*from++);
if ((null_value= (hex_char == -1)))
return 0;
}
for (end=res->ptr()+res->length(); from < end ; from+=2, to++)
{
int hex_char;
*to= (hex_char= hexchar_to_int(from[0])) << 4;
if ((null_value= (hex_char == -1)))
return 0;
*to|= hex_char= hexchar_to_int(from[1]);
if ((null_value= (hex_char == -1)))
return 0;
}
return &tmp_value;
}
plugin::Create_function<HexFunction> *hex_function= NULL;
plugin::Create_function<UnHexFunction> *unhex_function= NULL;
static int initialize(drizzled::module::Context &context)
{
hex_function= new plugin::Create_function<HexFunction>("hex");
unhex_function= new plugin::Create_function<UnHexFunction>("unhex");
context.add(hex_function);
context.add(unhex_function);
return 0;
}
DRIZZLE_DECLARE_PLUGIN
{
DRIZZLE_VERSION_ID,
"hex_functions",
"1.0",
"Stewart Smith",
"Convert a string to HEX() or from UNHEX()",
PLUGIN_LICENSE_GPL,
initialize, /* Plugin Init */
NULL, /* system variables */
NULL /* config options */
}
DRIZZLE_DECLARE_PLUGIN_END;
|