Drizzled Public API Documentation

md5.cc
00001 /* vim: expandtab:shiftwidth=2:tabstop=2:smarttab: 
00002    Copyright (C) 2006 MySQL AB
00003 
00004    This program is free software; you can redistribute it and/or modify
00005    it under the terms of the GNU General Public License as published by
00006    the Free Software Foundation; version 2 of the License.
00007 
00008    This program is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011    GNU General Public License for more details.
00012 
00013    You should have received a copy of the GNU General Public License
00014    along with this program; if not, write to the Free Software
00015    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
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/function/str/strfunc.h>
00026 #include <drizzled/item/func.h>
00027 #include <drizzled/plugin/function.h>
00028 
00029 using namespace std;
00030 using namespace drizzled;
00031 
00032 class Md5Function : public Item_str_func
00033 {
00034 public:
00035   String *val_str(String*);
00036 
00037   void fix_length_and_dec() 
00038   {
00039     max_length= 32;
00040     args[0]->collation.set(get_charset_by_csname(args[0]->collation.collation->csname, MY_CS_BINSORT), DERIVATION_COERCIBLE);
00041   }
00042 
00043   const char *func_name() const 
00044   { 
00045     return "md5"; 
00046   }
00047 
00048   bool check_argument_count(int n) 
00049   { 
00050     return (n == 1); 
00051   }
00052 };
00053 
00054 
00055 String *Md5Function::val_str(String *str)
00056 {
00057   assert(fixed == true);
00058 
00059   String *sptr= args[0]->val_str(str);
00060   if (sptr == NULL) 
00061   {
00062     null_value= true;
00063     return 0;
00064   }
00065   str->alloc(32);
00066 
00067   null_value= false;
00068 
00069   str->set_charset(&my_charset_bin);
00070 
00071   gcry_md_hd_t md5_context;
00072   gcry_md_open(&md5_context, GCRY_MD_MD5, 0);
00073   gcry_md_write(md5_context, sptr->ptr(), sptr->length());  
00074   unsigned char *digest= gcry_md_read(md5_context, 0);
00075 
00076   snprintf((char *) str->ptr(), 33,
00077     "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
00078     digest[0], digest[1], digest[2], digest[3],
00079     digest[4], digest[5], digest[6], digest[7],
00080     digest[8], digest[9], digest[10], digest[11],
00081     digest[12], digest[13], digest[14], digest[15]);
00082   str->length((uint32_t) 32);
00083 
00084   gcry_md_close(md5_context);
00085 
00086   return str;
00087 }
00088 
00089 static int initialize(module::Context &context)
00090 {
00091   /* Initialize libgcrypt */
00092   if (not gcry_check_version(GCRYPT_VERSION))
00093   {
00094     errmsg_printf(error::ERROR, _("libgcrypt library version mismatch"));
00095     return 1;
00096   }
00097   /* Disable secure memory.  */
00098   gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
00099 
00100   /* Tell Libgcrypt that initialization has completed. */
00101   gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
00102 
00103   context.add(new plugin::Create_function<Md5Function>("md5"));
00104   return 0;
00105 }
00106 
00107 DRIZZLE_DECLARE_PLUGIN
00108 {
00109   DRIZZLE_VERSION_ID,
00110   "md5",
00111   "1.0",
00112   "Stewart Smith",
00113   "UDF for computing md5sum",
00114   PLUGIN_LICENSE_GPL,
00115   initialize, /* Plugin Init */
00116   NULL,   /* depends */
00117   NULL    /* config options */
00118 }
00119 DRIZZLE_DECLARE_PLUGIN_END;