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>
16
#include <drizzled/common_includes.h>
25
17
#include <drizzled/item/func.h>
26
#include "drizzled/charset.h"
27
#include <drizzled/function/str/strfunc.h>
18
#include <drizzled/item/strfunc.h>
20
#include <openssl/md5.h>
29
24
using namespace std;
30
using namespace drizzled;
32
class Md5Function : public Item_str_func
26
class Item_func_md5 : public Item_str_func
35
Md5Function() : Item_str_func() {}
29
const char *func_name() const { return "md5"; }
36
30
String *val_str(String*);
38
void fix_length_and_dec()
31
void fix_length_and_dec() {
41
33
args[0]->collation.set(
42
34
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)
35
MY_CS_BINSORT,MYF(0)), DERIVATION_COERCIBLE);
58
String *Md5Function::val_str(String *str)
41
String *Item_func_md5::val_str(String *str)
60
assert(fixed == true);
62
String *sptr= args[0]->val_str(str);
63
if (sptr == NULL || str->alloc(32))
71
unsigned char digest[16];
44
String * sptr= args[0]->val_str(str);
72
45
str->set_charset(&my_charset_bin);
74
gcry_md_hash_buffer(GCRY_MD_MD5, digest, sptr->ptr(), sptr->length());
76
snprintf((char *) str->ptr(), 33,
77
"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
78
digest[0], digest[1], digest[2], digest[3],
79
digest[4], digest[5], digest[6], digest[7],
80
digest[8], digest[9], digest[10], digest[11],
81
digest[12], digest[13], digest[14], digest[15]);
82
str->length((uint32_t) 32);
88
plugin::Create_function<Md5Function> *md5udf= NULL;
90
static int initialize(plugin::Context &context)
92
md5udf= new plugin::Create_function<Md5Function>("md5");
97
DRIZZLE_DECLARE_PLUGIN
49
unsigned char digest[16];
53
MD5_Update (&context,(unsigned char *) sptr->ptr(), sptr->length());
54
MD5_Final (digest, &context);
55
if (str->alloc(32)) // Ensure that memory is free
60
snprintf((char *) str->ptr(), 33,
61
"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
62
digest[0], digest[1], digest[2], digest[3],
63
digest[4], digest[5], digest[6], digest[7],
64
digest[8], digest[9], digest[10], digest[11],
65
digest[12], digest[13], digest[14], digest[15]);
66
str->length((uint) 32);
74
Item_func* create_md5udf_item(MEM_ROOT* m)
76
return new (m) Item_func_md5();
79
struct udf_func md5udf = {
80
{ C_STRING_WITH_LEN("md5") },
84
static int md5udf_plugin_init(void *p)
86
udf_func **f = (udf_func**) p;
93
static int md5udf_plugin_deinit(void *p)
95
udf_func *udff = (udf_func *) p;
100
mysql_declare_plugin(md5)
103
106
"UDF for computing md5sum",
104
107
PLUGIN_LICENSE_GPL,
105
initialize, /* Plugin Init */
108
md5udf_plugin_init, /* Plugin Init */
109
md5udf_plugin_deinit, /* Plugin Deinit */
110
NULL, /* status variables */
106
111
NULL, /* system variables */
107
112
NULL /* config options */
109
DRIZZLE_DECLARE_PLUGIN_END;
114
mysql_declare_plugin_end;