00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <config.h>
00018
00019 #include <cstdio>
00020 #include <cstddef>
00021
00022 #include <gcrypt.h>
00023
00024 #include <drizzled/charset.h>
00025 #include <drizzled/charset_info.h>
00026 #include <drizzled/function/str/strfunc.h>
00027 #include <drizzled/item/func.h>
00028 #include <drizzled/plugin/function.h>
00029
00030 using namespace std;
00031 using namespace drizzled;
00032
00033 class Md5Function : public Item_str_func
00034 {
00035 public:
00036 Md5Function() : Item_str_func() {}
00037 String *val_str(String*);
00038
00039 void fix_length_and_dec()
00040 {
00041 max_length= 32;
00042 args[0]->collation.set(
00043 get_charset_by_csname(args[0]->collation.collation->csname,
00044 MY_CS_BINSORT), DERIVATION_COERCIBLE);
00045 }
00046
00047 const char *func_name() const
00048 {
00049 return "md5";
00050 }
00051
00052 bool check_argument_count(int n)
00053 {
00054 return (n == 1);
00055 }
00056 };
00057
00058
00059 String *Md5Function::val_str(String *str)
00060 {
00061 assert(fixed == true);
00062
00063 String *sptr= args[0]->val_str(str);
00064 if (sptr == NULL || str->alloc(32))
00065 {
00066 null_value= true;
00067 return 0;
00068 }
00069
00070 null_value= false;
00071
00072 str->set_charset(&my_charset_bin);
00073
00074 gcry_md_hd_t md5_context;
00075 gcry_md_open(&md5_context, GCRY_MD_MD5, 0);
00076 gcry_md_write(md5_context, sptr->ptr(), sptr->length());
00077 unsigned char *digest= gcry_md_read(md5_context, 0);
00078
00079 snprintf((char *) str->ptr(), 33,
00080 "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
00081 digest[0], digest[1], digest[2], digest[3],
00082 digest[4], digest[5], digest[6], digest[7],
00083 digest[8], digest[9], digest[10], digest[11],
00084 digest[12], digest[13], digest[14], digest[15]);
00085 str->length((uint32_t) 32);
00086
00087 gcry_md_close(md5_context);
00088
00089 return str;
00090 }
00091
00092
00093 plugin::Create_function<Md5Function> *md5udf= NULL;
00094
00095 static int initialize(module::Context &context)
00096 {
00097
00098 if (not gcry_check_version(GCRYPT_VERSION))
00099 {
00100 errmsg_printf(error::ERROR, _("libgcrypt library version mismatch"));
00101 return 1;
00102 }
00103
00104 gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
00105
00106
00107 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
00108
00109 md5udf= new plugin::Create_function<Md5Function>("md5");
00110 context.add(md5udf);
00111 return 0;
00112 }
00113
00114 DRIZZLE_DECLARE_PLUGIN
00115 {
00116 DRIZZLE_VERSION_ID,
00117 "md5",
00118 "1.0",
00119 "Stewart Smith",
00120 "UDF for computing md5sum",
00121 PLUGIN_LICENSE_GPL,
00122 initialize,
00123 NULL,
00124 NULL
00125 }
00126 DRIZZLE_DECLARE_PLUGIN_END;