~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/md5/md5udf.cc

  • Committer: Monty Taylor
  • Date: 2008-08-16 21:06:22 UTC
  • Revision ID: monty@inaugust.com-20080816210622-zpnn13unyinqzn72
Updated po files.

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 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>
30
 
#include <drizzled/item/func.h>
31
 
#include "drizzled/charset.h"
32
 
#include <drizzled/function/str/strfunc.h>
33
 
 
34
 
using namespace std;
35
 
using namespace drizzled;
36
 
 
37
 
class Md5Function : public Item_str_func
38
 
{
39
 
public:
40
 
  Md5Function() : Item_str_func() {}
41
 
  String *val_str(String*);
42
 
 
43
 
  void fix_length_and_dec() 
44
 
  {
45
 
    max_length= 32;
46
 
    args[0]->collation.set(
47
 
      get_charset_by_csname(args[0]->collation.collation->csname,
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
 
  }
60
 
};
61
 
 
62
 
 
63
 
String *Md5Function::val_str(String *str)
64
 
{
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];
77
 
  str->set_charset(&my_charset_bin);
 
16
#include <drizzled/common_includes.h>
 
17
#include <openssl/md5.h>
 
18
 
 
19
bool udf_init_md5udf(UDF_INIT *initid, UDF_ARGS *args, char *message)
 
20
{
 
21
  /* initid->ptr keeps state for between udf_init_foo and udf_deinit_foo */
 
22
  initid->ptr= NULL;
 
23
 
 
24
  if (args->arg_count != 1)
 
25
   {
 
26
      strcpy(message,"MD5() requires one arguments");
 
27
      return 1;
 
28
   }
 
29
 
 
30
   if (args->arg_type[0] != STRING_RESULT)
 
31
   {
 
32
      strcpy(message,"MD5() requires a string");
 
33
      return 1;
 
34
   }
 
35
 
 
36
  return 0;
 
37
}
 
38
 
 
39
char *udf_doit_md5(UDF_INIT *initid, UDF_ARGS *args, char *result,
 
40
                           unsigned long *length, char *is_null, char *error)
 
41
{
78
42
  MD5_CTX context;
 
43
  uchar digest[16];
 
44
 
 
45
  (void)initid;
 
46
 
79
47
  MD5_Init(&context);
80
 
  MD5_Update(&context, (unsigned char *) sptr->ptr(), sptr->length());
 
48
 
 
49
  MD5_Update(&context, args->args[0], args->lengths[0]);
 
50
 
81
51
  MD5_Final(digest, &context);
82
52
 
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,
 
53
  sprintf(result,
 
54
          "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
 
55
          digest[0], digest[1], digest[2], digest[3],
 
56
          digest[4], digest[5], digest[6], digest[7],
 
57
          digest[8], digest[9], digest[10], digest[11],
 
58
          digest[12], digest[13], digest[14], digest[15]);
 
59
 
 
60
  *length= 32;
 
61
 
 
62
  /* is_null is already zero, this is a demonstration */
 
63
  *is_null= 0;
 
64
 
 
65
  /* error is already zero, this is a demonstration */
 
66
  *error= 0;
 
67
 
 
68
  return result;
 
69
}
 
70
 
 
71
void udf_deinit_md5udf(UDF_INIT *initid)
 
72
{
 
73
  (void)initid;
 
74
  /* if we allocated initid->ptr, free it here */
 
75
  return;
 
76
}
 
77
 
 
78
 
 
79
static int md5udf_plugin_init(void *p)
 
80
{
 
81
  udf_func *udff= (udf_func *) p;
 
82
  static char md5str[4];
 
83
 
 
84
  strcpy(md5str, "md5");
 
85
 
 
86
  udff->name.str= md5str;
 
87
  udff->name.length= strlen("md5");
 
88
  udff->type= UDFTYPE_FUNCTION;
 
89
  udff->returns= STRING_RESULT;
 
90
  udff->func_init= udf_init_md5udf;
 
91
  udff->func_deinit= udf_deinit_md5udf;
 
92
  udff->func= (Udf_func_any) udf_doit_md5;
 
93
 
 
94
  return 0;
 
95
}
 
96
 
 
97
static int md5udf_plugin_deinit(void *p)
 
98
{
 
99
  udf_func *udff = (udf_func *) p;
 
100
  (void)udff;
 
101
  return 0;
 
102
}
 
103
 
 
104
mysql_declare_plugin(md5)
 
105
{
 
106
  DRIZZLE_UDF_PLUGIN,
114
107
  "md5",
115
108
  "1.0",
116
109
  "Stewart Smith",
117
110
  "UDF for computing md5sum",
118
111
  PLUGIN_LICENSE_GPL,
119
 
  initialize, /* Plugin Init */
120
 
  finalize,   /* Plugin Deinit */
 
112
  md5udf_plugin_init, /* Plugin Init */
 
113
  md5udf_plugin_deinit, /* Plugin Deinit */
121
114
  NULL,   /* status variables */
122
115
  NULL,   /* system variables */
123
116
  NULL    /* config options */
124
117
}
125
 
DRIZZLE_DECLARE_PLUGIN_END;
 
118
mysql_declare_plugin_end;