~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/md5/md5udf.cc

  • Committer: Monty Taylor
  • Date: 2008-12-06 22:41:03 UTC
  • mto: (656.1.7 devel)
  • mto: This revision was merged to the branch mainline in revision 665.
  • Revision ID: monty@inaugust.com-20081206224103-jdouqwt9hb0f01y1
Moved non-working tests into broken suite for easier running of working tests.

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/charset.h>
25
 
#include <drizzled/charset_info.h>
26
 
#include <drizzled/function/str/strfunc.h>
 
16
#include <drizzled/common_includes.h>
 
17
#include <drizzled/sql_udf.h>
27
18
#include <drizzled/item/func.h>
28
 
#include <drizzled/plugin/function.h>
 
19
#include <drizzled/item/strfunc.h>
 
20
 
 
21
#include <openssl/md5.h>
 
22
 
 
23
#include <stdio.h>
29
24
 
30
25
using namespace std;
31
 
using namespace drizzled;
32
26
 
33
 
class Md5Function : public Item_str_func
 
27
class Item_func_md5 : public Item_str_func
34
28
{
35
29
public:
36
 
  Md5Function() : Item_str_func() {}
 
30
  const char *func_name() const { return "md5"; }
37
31
  String *val_str(String*);
38
 
 
39
 
  void fix_length_and_dec() 
40
 
  {
41
 
    max_length= 32;
 
32
  void fix_length_and_dec() {
 
33
    max_length=32;
42
34
    args[0]->collation.set(
43
35
      get_charset_by_csname(args[0]->collation.collation->csname,
44
 
                            MY_CS_BINSORT), DERIVATION_COERCIBLE);
45
 
  }
46
 
 
47
 
  const char *func_name() const 
48
 
  { 
49
 
    return "md5"; 
50
 
  }
51
 
 
52
 
  bool check_argument_count(int n) 
53
 
  { 
54
 
    return (n == 1); 
55
 
  }
 
36
                            MY_CS_BINSORT,MYF(0)), DERIVATION_COERCIBLE);
 
37
  }
 
38
 
56
39
};
57
40
 
58
41
 
59
 
String *Md5Function::val_str(String *str)
 
42
String *Item_func_md5::val_str(String *str)
60
43
{
61
 
  assert(fixed == true);
62
 
 
63
 
  String *sptr= args[0]->val_str(str);
64
 
  if (sptr == NULL || str->alloc(32)) 
65
 
  {
66
 
    null_value= true;
67
 
    return 0;
68
 
  }
69
 
 
70
 
  null_value= false;
71
 
 
 
44
  assert(fixed == 1);
 
45
  String * sptr= args[0]->val_str(str);
72
46
  str->set_charset(&my_charset_bin);
73
 
 
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);
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]);
85
 
  str->length((uint32_t) 32);
86
 
 
87
 
  gcry_md_close(md5_context);
88
 
 
89
 
  return str;
90
 
}
91
 
 
92
 
 
93
 
plugin::Create_function<Md5Function> *md5udf= NULL;
94
 
 
95
 
static int initialize(module::Context &context)
96
 
{
97
 
  /* Initialize libgcrypt */
98
 
  if (not gcry_check_version(GCRYPT_VERSION))
 
47
  if (sptr)
99
48
  {
100
 
    errmsg_printf(error::ERROR, _("libgcrypt library version mismatch"));
101
 
    return 1;
 
49
    MD5_CTX context;
 
50
    unsigned char digest[16];
 
51
 
 
52
    null_value=0;
 
53
    MD5_Init (&context);
 
54
    MD5_Update (&context,(unsigned char *) sptr->ptr(), sptr->length());
 
55
    MD5_Final (digest, &context);
 
56
    if (str->alloc(32))                         // Ensure that memory is free
 
57
    {
 
58
      null_value=1;
 
59
      return 0;
 
60
    }
 
61
    snprintf((char *) str->ptr(), 33,
 
62
            "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
 
63
            digest[0], digest[1], digest[2], digest[3],
 
64
            digest[4], digest[5], digest[6], digest[7],
 
65
            digest[8], digest[9], digest[10], digest[11],
 
66
            digest[12], digest[13], digest[14], digest[15]);
 
67
    str->length((uint) 32);
 
68
    return str;
102
69
  }
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
 
 
109
 
  md5udf= new plugin::Create_function<Md5Function>("md5");
110
 
  context.add(md5udf);
111
 
  return 0;
112
 
}
113
 
 
114
 
DRIZZLE_DECLARE_PLUGIN
115
 
{
116
 
  DRIZZLE_VERSION_ID,
 
70
  null_value=1;
 
71
  return 0;
 
72
}
 
73
 
 
74
 
 
75
Item_func* create_md5udf_item(MEM_ROOT* m)
 
76
{
 
77
  return  new (m) Item_func_md5();
 
78
}
 
79
 
 
80
struct udf_func md5udf = {
 
81
  { C_STRING_WITH_LEN("md5") },
 
82
  create_md5udf_item
 
83
};
 
84
 
 
85
static int md5udf_plugin_init(void *p)
 
86
{
 
87
  udf_func **f = (udf_func**) p;
 
88
 
 
89
  *f= &md5udf;
 
90
 
 
91
  return 0;
 
92
}
 
93
 
 
94
static int md5udf_plugin_deinit(void *p)
 
95
{
 
96
  udf_func *udff = (udf_func *) p;
 
97
  (void)udff;
 
98
  return 0;
 
99
}
 
100
 
 
101
mysql_declare_plugin(md5)
 
102
{
 
103
  DRIZZLE_UDF_PLUGIN,
117
104
  "md5",
118
105
  "1.0",
119
106
  "Stewart Smith",
120
107
  "UDF for computing md5sum",
121
108
  PLUGIN_LICENSE_GPL,
122
 
  initialize, /* Plugin Init */
123
 
  NULL,   /* depends */
 
109
  md5udf_plugin_init, /* Plugin Init */
 
110
  md5udf_plugin_deinit, /* Plugin Deinit */
 
111
  NULL,   /* status variables */
 
112
  NULL,   /* system variables */
124
113
  NULL    /* config options */
125
114
}
126
 
DRIZZLE_DECLARE_PLUGIN_END;
 
115
mysql_declare_plugin_end;