~drizzle-trunk/drizzle/development

139.1.1 by Stewart Smith
Move MD5() into a UDF
1
/* Copyright (C) 2006 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
243.1.14 by Jay Pipes
* Ensured all drizzled/field/x.cc files to include mysql_priv.h
16
#include <drizzled/common_includes.h>
139.1.8 by Stewart Smith
UDFs are now normal Item_func objects. Simplifies handling them a lot.
17
#include <drizzled/item_func.h>
18
#include <drizzled/item_strfunc.h>
19
139.1.1 by Stewart Smith
Move MD5() into a UDF
20
#include <openssl/md5.h>
21
139.1.8 by Stewart Smith
UDFs are now normal Item_func objects. Simplifies handling them a lot.
22
class Item_func_md5 : public Item_str_func
23
{
24
public:
25
  const char *func_name() const { return "md5"; }
26
  String *val_str(String*);
27
  void fix_length_and_dec() {
28
    max_length=32;
29
    args[0]->collation.set(
30
      get_charset_by_csname(args[0]->collation.collation->csname,
31
                            MY_CS_BINSORT,MYF(0)), DERIVATION_COERCIBLE);
32
  }
33
34
};
35
36
37
String *Item_func_md5::val_str(String *str)
38
{
39
  assert(fixed == 1);
40
  String * sptr= args[0]->val_str(str);
41
  str->set_charset(&my_charset_bin);
42
  if (sptr)
43
  {
44
    MD5_CTX context;
481 by Brian Aker
Remove all of uchar.
45
    unsigned char digest[16];
139.1.8 by Stewart Smith
UDFs are now normal Item_func objects. Simplifies handling them a lot.
46
47
    null_value=0;
48
    MD5_Init (&context);
481 by Brian Aker
Remove all of uchar.
49
    MD5_Update (&context,(unsigned char *) sptr->ptr(), sptr->length());
139.1.8 by Stewart Smith
UDFs are now normal Item_func objects. Simplifies handling them a lot.
50
    MD5_Final (digest, &context);
51
    if (str->alloc(32))				// Ensure that memory is free
52
    {
53
      null_value=1;
54
      return 0;
55
    }
56
    snprintf((char *) str->ptr(), 33,
57
	    "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
58
	    digest[0], digest[1], digest[2], digest[3],
59
	    digest[4], digest[5], digest[6], digest[7],
60
	    digest[8], digest[9], digest[10], digest[11],
61
	    digest[12], digest[13], digest[14], digest[15]);
62
    str->length((uint) 32);
63
    return str;
64
  }
65
  null_value=1;
139.1.1 by Stewart Smith
Move MD5() into a UDF
66
  return 0;
67
}
68
139.1.8 by Stewart Smith
UDFs are now normal Item_func objects. Simplifies handling them a lot.
69
70
Item_func* create_md5udf_item(MEM_ROOT* m)
71
{
72
  return  new (m) Item_func_md5();
73
}
74
75
struct udf_func md5udf = {
76
  { C_STRING_WITH_LEN("md5") },
77
  create_md5udf_item
78
};
139.1.1 by Stewart Smith
Move MD5() into a UDF
79
80
static int md5udf_plugin_init(void *p)
81
{
139.1.8 by Stewart Smith
UDFs are now normal Item_func objects. Simplifies handling them a lot.
82
  udf_func **f = (udf_func**) p;
83
84
  *f= &md5udf;
139.1.1 by Stewart Smith
Move MD5() into a UDF
85
86
  return 0;
87
}
88
89
static int md5udf_plugin_deinit(void *p)
90
{
91
  udf_func *udff = (udf_func *) p;
139.1.4 by Stewart Smith
merge mainline and update md5 and crc32 plugins to new interface
92
  (void)udff;
139.1.1 by Stewart Smith
Move MD5() into a UDF
93
  return 0;
94
}
95
139.1.4 by Stewart Smith
merge mainline and update md5 and crc32 plugins to new interface
96
mysql_declare_plugin(md5)
139.1.1 by Stewart Smith
Move MD5() into a UDF
97
{
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
98
  DRIZZLE_UDF_PLUGIN,
139.1.1 by Stewart Smith
Move MD5() into a UDF
99
  "md5",
139.1.4 by Stewart Smith
merge mainline and update md5 and crc32 plugins to new interface
100
  "1.0",
139.1.1 by Stewart Smith
Move MD5() into a UDF
101
  "Stewart Smith",
139.1.4 by Stewart Smith
merge mainline and update md5 and crc32 plugins to new interface
102
  "UDF for computing md5sum",
139.1.1 by Stewart Smith
Move MD5() into a UDF
103
  PLUGIN_LICENSE_GPL,
104
  md5udf_plugin_init, /* Plugin Init */
105
  md5udf_plugin_deinit, /* Plugin Deinit */
106
  NULL,   /* status variables */
107
  NULL,   /* system variables */
108
  NULL    /* config options */
109
}
110
mysql_declare_plugin_end;