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/plugin/function.h>
25
#include <drizzled/item/func.h>
26
#include "drizzled/charset.h"
27
#include <drizzled/function/str/strfunc.h>
30
using namespace drizzled;
32
class Md5Function : public Item_str_func
35
Md5Function() : Item_str_func() {}
36
String *val_str(String*);
38
void fix_length_and_dec()
41
args[0]->collation.set(
42
get_charset_by_csname(args[0]->collation.collation->csname,
43
MY_CS_BINSORT), DERIVATION_COERCIBLE);
46
const char *func_name() const
51
bool check_argument_count(int n)
58
String *Md5Function::val_str(String *str)
60
assert(fixed == true);
62
String *sptr= args[0]->val_str(str);
63
if (sptr == NULL || str->alloc(32))
71
str->set_charset(&my_charset_bin);
73
gcry_md_hd_t md5_context;
74
gcry_md_open(&md5_context, GCRY_MD_MD5, 0);
75
gcry_md_write(md5_context, sptr->ptr(), sptr->length());
76
unsigned char *digest= gcry_md_read(md5_context, 0);
78
snprintf((char *) str->ptr(), 33,
79
"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
80
digest[0], digest[1], digest[2], digest[3],
81
digest[4], digest[5], digest[6], digest[7],
82
digest[8], digest[9], digest[10], digest[11],
83
digest[12], digest[13], digest[14], digest[15]);
84
str->length((uint32_t) 32);
86
gcry_md_close(md5_context);
92
plugin::Create_function<Md5Function> *md5udf= NULL;
94
static int initialize(module::Context &context)
96
/* Initialize libgcrypt */
97
if (not gcry_check_version(GCRYPT_VERSION))
99
errmsg_printf(ERRMSG_LVL_ERROR, _("libgcrypt library version mismatch\n"));
102
/* Disable secure memory. */
103
gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
105
/* Tell Libgcrypt that initialization has completed. */
106
gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
108
md5udf= new plugin::Create_function<Md5Function>("md5");
113
DRIZZLE_DECLARE_PLUGIN
16
#include <drizzled/common_includes.h>
17
#include <openssl/md5.h>
19
bool udf_init_md5udf(UDF_INIT *initid, UDF_ARGS *args, char *message)
21
/* initid->ptr keeps state for between udf_init_foo and udf_deinit_foo */
24
if (args->arg_count != 1)
26
strcpy(message,"MD5() requires one arguments");
30
if (args->arg_type[0] != STRING_RESULT)
32
strcpy(message,"MD5() requires a string");
39
char *udf_doit_md5(UDF_INIT *initid, UDF_ARGS *args, char *result,
40
unsigned long *length, char *is_null, char *error)
49
MD5_Update(&context, args->args[0], args->lengths[0]);
51
MD5_Final(digest, &context);
54
"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
55
digest[0], digest[1], digest[2], digest[3],
56
digest[4], digest[5], digest[6], digest[7],
57
digest[8], digest[9], digest[10], digest[11],
58
digest[12], digest[13], digest[14], digest[15]);
62
/* is_null is already zero, this is a demonstration */
65
/* error is already zero, this is a demonstration */
71
void udf_deinit_md5udf(UDF_INIT *initid)
74
/* if we allocated initid->ptr, free it here */
79
static int md5udf_plugin_init(void *p)
81
udf_func *udff= (udf_func *) p;
82
static char md5str[4];
84
strcpy(md5str, "md5");
86
udff->name.str= md5str;
87
udff->name.length= strlen("md5");
88
udff->type= UDFTYPE_FUNCTION;
89
udff->returns= STRING_RESULT;
90
udff->func_init= udf_init_md5udf;
91
udff->func_deinit= udf_deinit_md5udf;
92
udff->func= (Udf_func_any) udf_doit_md5;
97
static int md5udf_plugin_deinit(void *p)
99
udf_func *udff = (udf_func *) p;
104
mysql_declare_plugin(md5)
119
110
"UDF for computing md5sum",
120
111
PLUGIN_LICENSE_GPL,
121
initialize, /* Plugin Init */
112
md5udf_plugin_init, /* Plugin Init */
113
md5udf_plugin_deinit, /* Plugin Deinit */
114
NULL, /* status variables */
122
115
NULL, /* system variables */
123
116
NULL /* config options */
125
DRIZZLE_DECLARE_PLUGIN_END;
118
mysql_declare_plugin_end;