14
13
along with this program; if not, write to the Free Software
15
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
24
#include <drizzled/charset.h>
25
#include <drizzled/charset_info.h>
26
#include <drizzled/function/str/strfunc.h>
27
#include <drizzled/item/func.h>
28
#include <drizzled/plugin/function.h>
31
using namespace drizzled;
33
class Md5Function : public Item_str_func
36
Md5Function() : Item_str_func() {}
37
String *val_str(String*);
39
void fix_length_and_dec()
42
args[0]->collation.set(
43
get_charset_by_csname(args[0]->collation.collation->csname,
44
MY_CS_BINSORT), DERIVATION_COERCIBLE);
47
const char *func_name() const
52
bool check_argument_count(int n)
59
String *Md5Function::val_str(String *str)
61
assert(fixed == true);
63
String *sptr= args[0]->val_str(str);
64
if (sptr == NULL || str->alloc(32))
72
str->set_charset(&my_charset_bin);
74
gcry_md_hd_t md5_context;
75
gcry_md_open(&md5_context, GCRY_MD_MD5, 0);
76
gcry_md_write(md5_context, sptr->ptr(), sptr->length());
77
unsigned char *digest= gcry_md_read(md5_context, 0);
79
snprintf((char *) str->ptr(), 33,
80
"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
81
digest[0], digest[1], digest[2], digest[3],
82
digest[4], digest[5], digest[6], digest[7],
83
digest[8], digest[9], digest[10], digest[11],
84
digest[12], digest[13], digest[14], digest[15]);
85
str->length((uint32_t) 32);
87
gcry_md_close(md5_context);
93
plugin::Create_function<Md5Function> *md5udf= NULL;
95
static int initialize(module::Context &context)
97
/* Initialize libgcrypt */
98
if (not gcry_check_version(GCRYPT_VERSION))
100
errmsg_printf(error::ERROR, _("libgcrypt library version mismatch"));
103
/* Disable secure memory. */
104
gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
106
/* Tell Libgcrypt that initialization has completed. */
107
gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
109
md5udf= new plugin::Create_function<Md5Function>("md5");
114
DRIZZLE_DECLARE_PLUGIN
19
#include <drizzled/mysql_priv.h>
20
#include <drizzled/plugin.h>
21
#include <openssl/md5.h>
23
bool udf_init_md5udf(UDF_INIT *initid, UDF_ARGS *args, char *message)
25
/* initid->ptr keeps state for between udf_init_foo and udf_deinit_foo */
28
if (args->arg_count != 1)
30
strcpy(message,"MD5() requires one arguments");
34
if (args->arg_type[0] != STRING_RESULT)
36
strcpy(message,"MD5() requires a string");
43
char *udf_doit_md5(UDF_INIT *initid, UDF_ARGS *args, char *result,
44
unsigned long *length, char *is_null, char *error)
53
MD5_Update(&context, args->args[0], args->lengths[0]);
55
MD5_Final(digest, &context);
58
"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
59
digest[0], digest[1], digest[2], digest[3],
60
digest[4], digest[5], digest[6], digest[7],
61
digest[8], digest[9], digest[10], digest[11],
62
digest[12], digest[13], digest[14], digest[15]);
66
/* is_null is already zero, this is a demonstration */
69
/* error is already zero, this is a demonstration */
75
void udf_deinit_md5udf(UDF_INIT *initid)
78
/* if we allocated initid->ptr, free it here */
83
static int md5udf_plugin_init(void *p)
85
udf_func *udff= (udf_func *) p;
86
static char md5str[4];
88
strcpy(md5str, "md5");
90
udff->name.str= md5str;
91
udff->name.length= strlen("md5");
92
udff->type= UDFTYPE_FUNCTION;
93
udff->returns= STRING_RESULT;
94
udff->func_init= udf_init_md5udf;
95
udff->func_deinit= udf_deinit_md5udf;
96
udff->func= (Udf_func_any) udf_doit_md5;
101
static int md5udf_plugin_deinit(void *p)
103
udf_func *udff = (udf_func *) p;
108
mysql_declare_plugin(md5)
120
114
"UDF for computing md5sum",
121
115
PLUGIN_LICENSE_GPL,
122
initialize, /* Plugin Init */
116
md5udf_plugin_init, /* Plugin Init */
117
md5udf_plugin_deinit, /* Plugin Deinit */
118
NULL, /* status variables */
119
NULL, /* system variables */
124
120
NULL /* config options */
126
DRIZZLE_DECLARE_PLUGIN_END;
122
mysql_declare_plugin_end;