Drizzled Public API Documentation

md5.cc
1 /* vim: expandtab:shiftwidth=2:tabstop=2:smarttab:
2  Copyright (C) 2006 MySQL AB
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16 
17 #include <config.h>
18 
19 #include <cstdio>
20 #include <cstddef>
21 
22 #include <gcrypt.h>
23 
24 #include <drizzled/charset.h>
25 #include <drizzled/function/str/strfunc.h>
26 #include <drizzled/item/func.h>
27 #include <drizzled/plugin/function.h>
28 
29 using namespace std;
30 using namespace drizzled;
31 
32 class Md5Function : public Item_str_func
33 {
34 public:
35  String *val_str(String*);
36 
37  void fix_length_and_dec()
38  {
39  max_length= 32;
40  args[0]->collation.set(get_charset_by_csname(args[0]->collation.collation->csname, MY_CS_BINSORT), DERIVATION_COERCIBLE);
41  }
42 
43  const char *func_name() const
44  {
45  return "md5";
46  }
47 
48  bool check_argument_count(int n)
49  {
50  return (n == 1);
51  }
52 };
53 
54 
56 {
57  assert(fixed == true);
58 
59  String *sptr= args[0]->val_str(str);
60  if (sptr == NULL)
61  {
62  null_value= true;
63  return 0;
64  }
65  str->alloc(32);
66 
67  null_value= false;
68 
69  str->set_charset(&my_charset_bin);
70 
71  gcry_md_hd_t md5_context;
72  gcry_md_open(&md5_context, GCRY_MD_MD5, 0);
73  gcry_md_write(md5_context, sptr->ptr(), sptr->length());
74  unsigned char *digest= gcry_md_read(md5_context, 0);
75 
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);
83 
84  gcry_md_close(md5_context);
85 
86  return str;
87 }
88 
89 static int initialize(module::Context &context)
90 {
91  /* Initialize libgcrypt */
92  if (not gcry_check_version(GCRYPT_VERSION))
93  {
94  errmsg_printf(error::ERROR, _("libgcrypt library version mismatch"));
95  return 1;
96  }
97  /* Disable secure memory. */
98  gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
99 
100  /* Tell Libgcrypt that initialization has completed. */
101  gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
102 
103  context.add(new plugin::Create_function<Md5Function>("md5"));
104  return 0;
105 }
106 
107 DRIZZLE_DECLARE_PLUGIN
108 {
109  DRIZZLE_VERSION_ID,
110  "md5",
111  "1.0",
112  "Stewart Smith",
113  N_("MD5 function"),
114  PLUGIN_LICENSE_GPL,
115  initialize,
116  NULL,
117  NULL
118 }
119 DRIZZLE_DECLARE_PLUGIN_END;