~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/md5/md5.cc

Style cleanup around TransactionContext::modified_non_trans_table and dead code removal

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2006 MySQL AB
 
1
/* vim: expandtab:shiftwidth=2:tabstop=2:smarttab: 
 
2
   Copyright (C) 2006 MySQL AB
2
3
 
3
4
   This program is free software; you can redistribute it and/or modify
4
5
   it under the terms of the GNU General Public License as published by
13
14
   along with this program; if not, write to the Free Software
14
15
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
16
 
16
 
#include <drizzled/common_includes.h>
17
 
#include <drizzled/sql_udf.h>
 
17
#include "config.h"
 
18
 
 
19
/* Include these before the openssl headers, because they are BROKEN AS CRAP */
 
20
#include <cstdio>
 
21
#include <cstddef>
 
22
 
 
23
#if defined(HAVE_LIBGNUTLS_OPENSSL)
 
24
# include <gnutls/openssl.h>
 
25
#else
 
26
# include <openssl/md5.h>
 
27
#endif /* HAVE_GNUTLS_OPENSSL */
 
28
 
 
29
#include <drizzled/plugin/function.h>
18
30
#include <drizzled/item/func.h>
19
 
#include <drizzled/item/strfunc.h>
20
 
 
21
 
#include <openssl/md5.h>
22
 
 
23
 
#include <stdio.h>
 
31
#include "drizzled/charset.h"
 
32
#include <drizzled/function/str/strfunc.h>
24
33
 
25
34
using namespace std;
 
35
using namespace drizzled;
26
36
 
27
 
class Item_func_md5 : public Item_str_func
 
37
class Md5Function : public Item_str_func
28
38
{
29
39
public:
30
 
  const char *func_name() const { return "md5"; }
 
40
  Md5Function() : Item_str_func() {}
31
41
  String *val_str(String*);
32
 
  void fix_length_and_dec() {
33
 
    max_length=32;
 
42
 
 
43
  void fix_length_and_dec() 
 
44
  {
 
45
    max_length= 32;
34
46
    args[0]->collation.set(
35
47
      get_charset_by_csname(args[0]->collation.collation->csname,
36
 
                            MY_CS_BINSORT,MYF(0)), DERIVATION_COERCIBLE);
37
 
  }
38
 
 
 
48
                            MY_CS_BINSORT), DERIVATION_COERCIBLE);
 
49
  }
 
50
 
 
51
  const char *func_name() const 
 
52
  { 
 
53
    return "md5"; 
 
54
  }
 
55
 
 
56
  bool check_argument_count(int n) 
 
57
  { 
 
58
    return (n == 1); 
 
59
  }
39
60
};
40
61
 
41
62
 
42
 
String *Item_func_md5::val_str(String *str)
 
63
String *Md5Function::val_str(String *str)
43
64
{
44
 
  assert(fixed == 1);
45
 
  String * sptr= args[0]->val_str(str);
 
65
  assert(fixed == true);
 
66
 
 
67
  String *sptr= args[0]->val_str(str);
 
68
  if (sptr == NULL || str->alloc(32)) 
 
69
  {
 
70
    null_value= true;
 
71
    return 0;
 
72
  }
 
73
 
 
74
  null_value= false;
 
75
 
 
76
  unsigned char digest[16];
46
77
  str->set_charset(&my_charset_bin);
47
 
  if (sptr)
48
 
  {
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;
69
 
  }
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,
 
78
  MD5_CTX context;
 
79
  MD5_Init(&context);
 
80
  MD5_Update(&context, (unsigned char *) sptr->ptr(), sptr->length());
 
81
  MD5_Final(digest, &context);
 
82
 
 
83
  snprintf((char *) str->ptr(), 33,
 
84
    "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
 
85
    digest[0], digest[1], digest[2], digest[3],
 
86
    digest[4], digest[5], digest[6], digest[7],
 
87
    digest[8], digest[9], digest[10], digest[11],
 
88
    digest[12], digest[13], digest[14], digest[15]);
 
89
  str->length((uint32_t) 32);
 
90
 
 
91
  return str;
 
92
}
 
93
 
 
94
 
 
95
plugin::Create_function<Md5Function> *md5udf= NULL;
 
96
 
 
97
static int initialize(plugin::Registry &registry)
 
98
{
 
99
  md5udf= new plugin::Create_function<Md5Function>("md5");
 
100
  registry.add(md5udf);
 
101
  return 0;
 
102
}
 
103
 
 
104
static int finalize(plugin::Registry &registry)
 
105
{
 
106
  registry.remove(md5udf);
 
107
  delete md5udf;
 
108
  return 0;
 
109
}
 
110
 
 
111
DRIZZLE_DECLARE_PLUGIN
 
112
{
 
113
  DRIZZLE_VERSION_ID,
104
114
  "md5",
105
115
  "1.0",
106
116
  "Stewart Smith",
107
117
  "UDF for computing md5sum",
108
118
  PLUGIN_LICENSE_GPL,
109
 
  md5udf_plugin_init, /* Plugin Init */
110
 
  md5udf_plugin_deinit, /* Plugin Deinit */
 
119
  initialize, /* Plugin Init */
 
120
  finalize,   /* Plugin Deinit */
111
121
  NULL,   /* status variables */
112
122
  NULL,   /* system variables */
113
123
  NULL    /* config options */
114
124
}
115
 
mysql_declare_plugin_end;
 
125
DRIZZLE_DECLARE_PLUGIN_END;