~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/md5/md5udf.cc

  • Committer: Brian Aker
  • Date: 2009-04-27 14:36:40 UTC
  • Revision ID: brian@gaz-20090427143640-f6zjmtt9vm55qgm2
Patch on show processlist from  davi@apache.org

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* vim: expandtab:shiftwidth=2:tabstop=2:smarttab: 
2
 
   Copyright (C) 2006 MySQL AB
 
1
/* Copyright (C) 2006 MySQL AB
3
2
 
4
3
   This program is free software; you can redistribute it and/or modify
5
4
   it under the terms of the GNU General Public License as published by
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 */
16
15
 
17
 
#include "config.h"
18
 
 
19
 
#include <cstdio>
20
 
#include <cstddef>
21
 
 
22
 
#include <gcrypt.h>
23
 
 
24
 
#include <drizzled/plugin/function.h>
 
16
#include <drizzled/server_includes.h>
 
17
#include <drizzled/sql_udf.h>
25
18
#include <drizzled/item/func.h>
26
 
#include "drizzled/charset.h"
27
19
#include <drizzled/function/str/strfunc.h>
28
20
 
 
21
#include <openssl/md5.h>
 
22
 
 
23
#include <stdio.h>
 
24
 
29
25
using namespace std;
30
 
using namespace drizzled;
31
26
 
32
 
class Md5Function : public Item_str_func
 
27
class Item_func_md5 : public Item_str_func
33
28
{
34
29
public:
35
 
  Md5Function() : Item_str_func() {}
 
30
  Item_func_md5() : Item_str_func() {}
 
31
  const char *func_name() const { return "md5"; }
36
32
  String *val_str(String*);
37
 
 
38
 
  void fix_length_and_dec() 
39
 
  {
40
 
    max_length= 32;
 
33
  void fix_length_and_dec() {
 
34
    max_length=32;
41
35
    args[0]->collation.set(
42
36
      get_charset_by_csname(args[0]->collation.collation->csname,
43
37
                            MY_CS_BINSORT), DERIVATION_COERCIBLE);
44
38
  }
45
39
 
46
 
  const char *func_name() const 
47
 
  { 
48
 
    return "md5"; 
49
 
  }
50
 
 
51
 
  bool check_argument_count(int n) 
52
 
  { 
53
 
    return (n == 1); 
54
 
  }
55
40
};
56
41
 
57
42
 
58
 
String *Md5Function::val_str(String *str)
 
43
String *Item_func_md5::val_str(String *str)
59
44
{
60
 
  assert(fixed == true);
61
 
 
62
 
  String *sptr= args[0]->val_str(str);
63
 
  if (sptr == NULL || str->alloc(32)) 
64
 
  {
65
 
    null_value= true;
66
 
    return 0;
67
 
  }
68
 
 
69
 
  null_value= false;
70
 
 
 
45
  assert(fixed == 1);
 
46
  String * sptr= args[0]->val_str(str);
71
47
  str->set_charset(&my_charset_bin);
72
 
 
73
 
  gcry_md_hd_t md5_context;
74
 
  gcry_md_open(&md5_context, GCRY_MD_MD5, 0);
75
 
  gcry_md_write(md5_context, sptr->ptr(), sptr->length());  
76
 
  unsigned char *digest= gcry_md_read(md5_context, 0);
77
 
 
78
 
  snprintf((char *) str->ptr(), 33,
79
 
    "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
80
 
    digest[0], digest[1], digest[2], digest[3],
81
 
    digest[4], digest[5], digest[6], digest[7],
82
 
    digest[8], digest[9], digest[10], digest[11],
83
 
    digest[12], digest[13], digest[14], digest[15]);
84
 
  str->length((uint32_t) 32);
85
 
 
86
 
  gcry_md_close(md5_context);
87
 
 
88
 
  return str;
89
 
}
90
 
 
91
 
 
92
 
plugin::Create_function<Md5Function> *md5udf= NULL;
93
 
 
94
 
static int initialize(module::Context &context)
95
 
{
96
 
  /* Initialize libgcrypt */
97
 
  if (not gcry_check_version(GCRYPT_VERSION))
 
48
  if (sptr)
98
49
  {
99
 
    errmsg_printf(ERRMSG_LVL_ERROR, _("libgcrypt library version mismatch\n"));
100
 
    return 1;
 
50
    MD5_CTX context;
 
51
    unsigned char digest[16];
 
52
 
 
53
    null_value=0;
 
54
    MD5_Init (&context);
 
55
    MD5_Update (&context,(unsigned char *) sptr->ptr(), sptr->length());
 
56
    MD5_Final (digest, &context);
 
57
    if (str->alloc(32))                         // Ensure that memory is free
 
58
    {
 
59
      null_value=1;
 
60
      return 0;
 
61
    }
 
62
    snprintf((char *) str->ptr(), 33,
 
63
            "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
 
64
            digest[0], digest[1], digest[2], digest[3],
 
65
            digest[4], digest[5], digest[6], digest[7],
 
66
            digest[8], digest[9], digest[10], digest[11],
 
67
            digest[12], digest[13], digest[14], digest[15]);
 
68
    str->length((uint) 32);
 
69
    return str;
101
70
  }
102
 
  /* Disable secure memory.  */
103
 
  gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
104
 
 
105
 
  /* Tell Libgcrypt that initialization has completed. */
106
 
  gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
107
 
 
108
 
  md5udf= new plugin::Create_function<Md5Function>("md5");
109
 
  context.add(md5udf);
110
 
  return 0;
111
 
}
112
 
 
113
 
DRIZZLE_DECLARE_PLUGIN
114
 
{
115
 
  DRIZZLE_VERSION_ID,
 
71
  null_value=1;
 
72
  return 0;
 
73
}
 
74
 
 
75
 
 
76
Create_function<Item_func_md5> md5udf(string("md5"));
 
77
 
 
78
static int md5udf_plugin_init(PluginRegistry &registry)
 
79
{
 
80
  registry.add(&md5udf);
 
81
  return 0;
 
82
}
 
83
 
 
84
drizzle_declare_plugin(md5)
 
85
{
116
86
  "md5",
117
87
  "1.0",
118
88
  "Stewart Smith",
119
89
  "UDF for computing md5sum",
120
90
  PLUGIN_LICENSE_GPL,
121
 
  initialize, /* Plugin Init */
 
91
  md5udf_plugin_init, /* Plugin Init */
 
92
  NULL,   /* Plugin Deinit */
 
93
  NULL,   /* status variables */
122
94
  NULL,   /* system variables */
123
95
  NULL    /* config options */
124
96
}
125
 
DRIZZLE_DECLARE_PLUGIN_END;
 
97
drizzle_declare_plugin_end;