~drizzle-trunk/drizzle/development

1086.1.1 by devananda
cleaned up formatting in md5.cc
1
/* vim: expandtab:shiftwidth=2:tabstop=2:smarttab: 
2
   Copyright (C) 2006 MySQL AB
139.1.1 by Stewart Smith
Move MD5() into a UDF
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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
17
#include <config.h>
1241.9.67 by Monty Taylor
Fixed Solaris.
18
19
#include <cstdio>
20
#include <cstddef>
21
1377.3.15 by Monty Taylor
Took the last suggestion from Diego and used a library that doesn't have
22
#include <gcrypt.h>
139.1.1 by Stewart Smith
Move MD5() into a UDF
23
2154.2.4 by Brian Aker
This fixes 716459
24
#include <drizzled/charset.h>
25
#include <drizzled/function/str/strfunc.h>
26
#include <drizzled/item/func.h>
1241.9.57 by Monty Taylor
Oy. Bigger change than I normally like - but this stuff is all intertwined.
27
#include <drizzled/plugin/function.h>
520.4.44 by mordred
A whole bunch of solaris/sun studio compile fixes.
28
29
using namespace std;
1093.1.62 by Monty Taylor
Moved UDFs to slot organization.
30
using namespace drizzled;
520.4.44 by mordred
A whole bunch of solaris/sun studio compile fixes.
31
1086.1.2 by devananda
fixed case: md5Function -> Md5Function
32
class Md5Function : public Item_str_func
139.1.8 by Stewart Smith
UDFs are now normal Item_func objects. Simplifies handling them a lot.
33
{
34
public:
1086.1.2 by devananda
fixed case: md5Function -> Md5Function
35
  Md5Function() : Item_str_func() {}
139.1.8 by Stewart Smith
UDFs are now normal Item_func objects. Simplifies handling them a lot.
36
  String *val_str(String*);
1086.1.1 by devananda
cleaned up formatting in md5.cc
37
38
  void fix_length_and_dec() 
39
  {
40
    max_length= 32;
139.1.8 by Stewart Smith
UDFs are now normal Item_func objects. Simplifies handling them a lot.
41
    args[0]->collation.set(
42
      get_charset_by_csname(args[0]->collation.collation->csname,
862 by Brian Aker
Remove charset directory code.
43
                            MY_CS_BINSORT), DERIVATION_COERCIBLE);
139.1.8 by Stewart Smith
UDFs are now normal Item_func objects. Simplifies handling them a lot.
44
  }
1086.1.1 by devananda
cleaned up formatting in md5.cc
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
  }
139.1.8 by Stewart Smith
UDFs are now normal Item_func objects. Simplifies handling them a lot.
55
};
56
57
1086.1.2 by devananda
fixed case: md5Function -> Md5Function
58
String *Md5Function::val_str(String *str)
139.1.8 by Stewart Smith
UDFs are now normal Item_func objects. Simplifies handling them a lot.
59
{
1086.1.3 by devananda
formatting cleanup
60
  assert(fixed == true);
61
62
  String *sptr= args[0]->val_str(str);
2275.2.14 by Olaf van der Spek
Fix
63
  if (sptr == NULL) 
1086.1.3 by devananda
formatting cleanup
64
  {
65
    null_value= true;
66
    return 0;
67
  }
2275.2.14 by Olaf van der Spek
Fix
68
  str->alloc(32);
1086.1.3 by devananda
formatting cleanup
69
70
  null_value= false;
71
139.1.8 by Stewart Smith
UDFs are now normal Item_func objects. Simplifies handling them a lot.
72
  str->set_charset(&my_charset_bin);
1377.3.15 by Monty Taylor
Took the last suggestion from Diego and used a library that doesn't have
73
1518.1.1 by Monty Taylor
Fixed the wrong usage of libgcrypt in md5 module. This fixes the leak that
74
  gcry_md_hd_t md5_context;
75
  gcry_md_open(&md5_context, GCRY_MD_MD5, 0);
76
  gcry_md_write(md5_context, sptr->ptr(), sptr->length());  
77
  unsigned char *digest= gcry_md_read(md5_context, 0);
1086.1.3 by devananda
formatting cleanup
78
79
  snprintf((char *) str->ptr(), 33,
80
    "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
81
    digest[0], digest[1], digest[2], digest[3],
82
    digest[4], digest[5], digest[6], digest[7],
83
    digest[8], digest[9], digest[10], digest[11],
84
    digest[12], digest[13], digest[14], digest[15]);
1086.1.4 by devananda
more tiny formatting cleanup
85
  str->length((uint32_t) 32);
1086.1.3 by devananda
formatting cleanup
86
1518.1.1 by Monty Taylor
Fixed the wrong usage of libgcrypt in md5 module. This fixes the leak that
87
  gcry_md_close(md5_context);
88
1086.1.3 by devananda
formatting cleanup
89
  return str;
139.1.1 by Stewart Smith
Move MD5() into a UDF
90
}
91
139.1.8 by Stewart Smith
UDFs are now normal Item_func objects. Simplifies handling them a lot.
92
1093.1.62 by Monty Taylor
Moved UDFs to slot organization.
93
plugin::Create_function<Md5Function> *md5udf= NULL;
139.1.1 by Stewart Smith
Move MD5() into a UDF
94
1530.2.6 by Monty Taylor
Moved plugin::Context to module::Context.
95
static int initialize(module::Context &context)
139.1.1 by Stewart Smith
Move MD5() into a UDF
96
{
1518.1.1 by Monty Taylor
Fixed the wrong usage of libgcrypt in md5 module. This fixes the leak that
97
  /* Initialize libgcrypt */
98
  if (not gcry_check_version(GCRYPT_VERSION))
99
  {
2126.3.3 by Brian Aker
Merge in error message rework. Many error messages are fixed in this patch.
100
    errmsg_printf(error::ERROR, _("libgcrypt library version mismatch"));
1518.1.1 by Monty Taylor
Fixed the wrong usage of libgcrypt in md5 module. This fixes the leak that
101
    return 1;
102
  }
103
  /* Disable secure memory.  */
104
  gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
105
106
  /* Tell Libgcrypt that initialization has completed. */
107
  gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
108
1093.1.62 by Monty Taylor
Moved UDFs to slot organization.
109
  md5udf= new plugin::Create_function<Md5Function>("md5");
1324.2.2 by Monty Taylor
Use the plugin::Context everywhere.
110
  context.add(md5udf);
1093.1.62 by Monty Taylor
Moved UDFs to slot organization.
111
  return 0;
1086.1.1 by devananda
cleaned up formatting in md5.cc
112
}
113
1228.1.5 by Monty Taylor
Merged in some naming things.
114
DRIZZLE_DECLARE_PLUGIN
139.1.1 by Stewart Smith
Move MD5() into a UDF
115
{
1241.10.2 by Monty Taylor
Added support for embedding the drizzle version number in the plugin file.
116
  DRIZZLE_VERSION_ID,
139.1.1 by Stewart Smith
Move MD5() into a UDF
117
  "md5",
139.1.4 by Stewart Smith
merge mainline and update md5 and crc32 plugins to new interface
118
  "1.0",
139.1.1 by Stewart Smith
Move MD5() into a UDF
119
  "Stewart Smith",
139.1.4 by Stewart Smith
merge mainline and update md5 and crc32 plugins to new interface
120
  "UDF for computing md5sum",
139.1.1 by Stewart Smith
Move MD5() into a UDF
121
  PLUGIN_LICENSE_GPL,
1086.1.1 by devananda
cleaned up formatting in md5.cc
122
  initialize, /* Plugin Init */
2095.3.1 by Monty Taylor
Re-purpose the old plugin sysvar slot in the struct to be a depends list.
123
  NULL,   /* depends */
139.1.1 by Stewart Smith
Move MD5() into a UDF
124
  NULL    /* config options */
125
}
1228.1.5 by Monty Taylor
Merged in some naming things.
126
DRIZZLE_DECLARE_PLUGIN_END;