13
13
along with this program; if not, write to the Free Software
14
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16
#include <drizzled/common_includes.h>
16
#include <drizzled/server_includes.h>
17
#include <drizzled/sql_udf.h>
18
#include <drizzled/item/func.h>
19
#include <drizzled/function/str/strfunc.h>
17
21
#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");
27
class Item_func_md5 : public Item_str_func
30
const char *func_name() const { return "md5"; }
31
String *val_str(String*);
32
void fix_length_and_dec() {
34
args[0]->collation.set(
35
get_charset_by_csname(args[0]->collation.collation->csname,
36
MY_CS_BINSORT,MYF(0)), DERIVATION_COERCIBLE);
42
String *Item_func_md5::val_str(String *str)
45
String * sptr= args[0]->val_str(str);
46
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);
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 */
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") },
79
85
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;
87
udf_func **f = (udf_func**) p;