13
14
along with this program; if not, write to the Free Software
14
15
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16
#include <drizzled/common_includes.h>
17
#include <drizzled/sql_udf.h>
19
/* Include these before the openssl headers, because they are BROKEN AS CRAP */
23
#if defined(HAVE_LIBGNUTLS_OPENSSL)
24
# include <gnutls/openssl.h>
26
# include <openssl/md5.h>
27
#endif /* HAVE_GNUTLS_OPENSSL */
29
#include <drizzled/plugin/function.h>
18
30
#include <drizzled/item/func.h>
19
#include <drizzled/item/strfunc.h>
21
#include <openssl/md5.h>
31
#include "drizzled/charset.h"
32
#include <drizzled/function/str/strfunc.h>
25
34
using namespace std;
35
using namespace drizzled;
27
class Item_func_md5 : public Item_str_func
37
class Md5Function : public Item_str_func
30
const char *func_name() const { return "md5"; }
40
Md5Function() : Item_str_func() {}
31
41
String *val_str(String*);
32
void fix_length_and_dec() {
43
void fix_length_and_dec()
34
46
args[0]->collation.set(
35
47
get_charset_by_csname(args[0]->collation.collation->csname,
36
MY_CS_BINSORT,MYF(0)), DERIVATION_COERCIBLE);
48
MY_CS_BINSORT), DERIVATION_COERCIBLE);
51
const char *func_name() const
56
bool check_argument_count(int n)
42
String *Item_func_md5::val_str(String *str)
63
String *Md5Function::val_str(String *str)
45
String * sptr= args[0]->val_str(str);
65
assert(fixed == true);
67
String *sptr= args[0]->val_str(str);
68
if (sptr == NULL || str->alloc(32))
76
unsigned char digest[16];
46
77
str->set_charset(&my_charset_bin);
50
unsigned char digest[16];
54
MD5_Update (&context,(unsigned char *) sptr->ptr(), sptr->length());
55
MD5_Final (digest, &context);
56
if (str->alloc(32)) // Ensure that memory is free
61
snprintf((char *) str->ptr(), 33,
62
"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
63
digest[0], digest[1], digest[2], digest[3],
64
digest[4], digest[5], digest[6], digest[7],
65
digest[8], digest[9], digest[10], digest[11],
66
digest[12], digest[13], digest[14], digest[15]);
67
str->length((uint) 32);
75
Item_func* create_md5udf_item(MEM_ROOT* m)
77
return new (m) Item_func_md5();
80
struct udf_func md5udf = {
81
{ C_STRING_WITH_LEN("md5") },
85
static int md5udf_plugin_init(void *p)
87
udf_func **f = (udf_func**) p;
94
static int md5udf_plugin_deinit(void *p)
96
udf_func *udff = (udf_func *) p;
101
mysql_declare_plugin(md5)
80
MD5_Update(&context, (unsigned char *) sptr->ptr(), sptr->length());
81
MD5_Final(digest, &context);
83
snprintf((char *) str->ptr(), 33,
84
"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
85
digest[0], digest[1], digest[2], digest[3],
86
digest[4], digest[5], digest[6], digest[7],
87
digest[8], digest[9], digest[10], digest[11],
88
digest[12], digest[13], digest[14], digest[15]);
89
str->length((uint32_t) 32);
95
plugin::Create_function<Md5Function> *md5udf= NULL;
97
static int initialize(plugin::Registry ®istry)
99
md5udf= new plugin::Create_function<Md5Function>("md5");
100
registry.add(md5udf);
104
static int finalize(plugin::Registry ®istry)
106
registry.remove(md5udf);
111
DRIZZLE_DECLARE_PLUGIN
107
117
"UDF for computing md5sum",
108
118
PLUGIN_LICENSE_GPL,
109
md5udf_plugin_init, /* Plugin Init */
110
md5udf_plugin_deinit, /* Plugin Deinit */
119
initialize, /* Plugin Init */
120
finalize, /* Plugin Deinit */
111
121
NULL, /* status variables */
112
122
NULL, /* system variables */
113
123
NULL /* config options */
115
mysql_declare_plugin_end;
125
DRIZZLE_DECLARE_PLUGIN_END;