~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/md5/md5udf.cc

  • Committer: Monty Taylor
  • Date: 2008-11-16 20:15:33 UTC
  • mto: (584.1.9 devel)
  • mto: This revision was merged to the branch mainline in revision 589.
  • Revision ID: monty@inaugust.com-20081116201533-d0f19s1bk1h95iyw
Removed a big bank of includes from item.h.

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/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>
 
19
 
 
20
#include <openssl/md5.h>
 
21
 
 
22
#include <stdio.h>
28
23
 
29
24
using namespace std;
30
 
using namespace drizzled;
31
25
 
32
 
class Md5Function : public Item_str_func
 
26
class Item_func_md5 : public Item_str_func
33
27
{
34
28
public:
35
 
  Md5Function() : Item_str_func() {}
 
29
  const char *func_name() const { return "md5"; }
36
30
  String *val_str(String*);
37
 
 
38
 
  void fix_length_and_dec() 
39
 
  {
40
 
    max_length= 32;
 
31
  void fix_length_and_dec() {
 
32
    max_length=32;
41
33
    args[0]->collation.set(
42
34
      get_charset_by_csname(args[0]->collation.collation->csname,
43
 
                            MY_CS_BINSORT), DERIVATION_COERCIBLE);
44
 
  }
45
 
 
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
 
  }
 
35
                            MY_CS_BINSORT,MYF(0)), DERIVATION_COERCIBLE);
 
36
  }
 
37
 
55
38
};
56
39
 
57
40
 
58
 
String *Md5Function::val_str(String *str)
 
41
String *Item_func_md5::val_str(String *str)
59
42
{
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
 
 
71
 
  unsigned char digest[16];
 
43
  assert(fixed == 1);
 
44
  String * sptr= args[0]->val_str(str);
72
45
  str->set_charset(&my_charset_bin);
73
 
 
74
 
  gcry_md_hash_buffer(GCRY_MD_MD5, digest, sptr->ptr(), sptr->length());
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
 
  return str;
85
 
}
86
 
 
87
 
 
88
 
plugin::Create_function<Md5Function> *md5udf= NULL;
89
 
 
90
 
static int initialize(plugin::Context &context)
91
 
{
92
 
  md5udf= new plugin::Create_function<Md5Function>("md5");
93
 
  context.add(md5udf);
94
 
  return 0;
95
 
}
96
 
 
97
 
DRIZZLE_DECLARE_PLUGIN
98
 
{
99
 
  DRIZZLE_VERSION_ID,
 
46
  if (sptr)
 
47
  {
 
48
    MD5_CTX context;
 
49
    unsigned char digest[16];
 
50
 
 
51
    null_value=0;
 
52
    MD5_Init (&context);
 
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
 
56
    {
 
57
      null_value=1;
 
58
      return 0;
 
59
    }
 
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);
 
67
    return str;
 
68
  }
 
69
  null_value=1;
 
70
  return 0;
 
71
}
 
72
 
 
73
 
 
74
Item_func* create_md5udf_item(MEM_ROOT* m)
 
75
{
 
76
  return  new (m) Item_func_md5();
 
77
}
 
78
 
 
79
struct udf_func md5udf = {
 
80
  { C_STRING_WITH_LEN("md5") },
 
81
  create_md5udf_item
 
82
};
 
83
 
 
84
static int md5udf_plugin_init(void *p)
 
85
{
 
86
  udf_func **f = (udf_func**) p;
 
87
 
 
88
  *f= &md5udf;
 
89
 
 
90
  return 0;
 
91
}
 
92
 
 
93
static int md5udf_plugin_deinit(void *p)
 
94
{
 
95
  udf_func *udff = (udf_func *) p;
 
96
  (void)udff;
 
97
  return 0;
 
98
}
 
99
 
 
100
mysql_declare_plugin(md5)
 
101
{
 
102
  DRIZZLE_UDF_PLUGIN,
100
103
  "md5",
101
104
  "1.0",
102
105
  "Stewart Smith",
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 */
108
113
}
109
 
DRIZZLE_DECLARE_PLUGIN_END;
 
114
mysql_declare_plugin_end;