~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/md5/md5udf.cc

  • Committer: Monty Taylor
  • Date: 2008-12-24 01:49:53 UTC
  • mto: This revision was merged to the branch mainline in revision 751.
  • Revision ID: mordred@inaugust.com-20081224014953-rc9p7a162p74y889
Fixed connect.test.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
   along with this program; if not, write to the Free Software
14
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
15
 
16
 
#include <drizzled/common_includes.h>
 
16
#include <drizzled/server_includes.h>
 
17
#include <drizzled/sql_udf.h>
 
18
#include <drizzled/item/func.h>
 
19
#include <drizzled/function/str/strfunc.h>
 
20
 
17
21
#include <openssl/md5.h>
18
22
 
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
 
 
 
23
#include <stdio.h>
 
24
 
 
25
using namespace std;
 
26
 
 
27
class Item_func_md5 : public Item_str_func
 
28
{
 
29
public:
 
30
  const char *func_name() const { return "md5"; }
 
31
  String *val_str(String*);
 
32
  void fix_length_and_dec() {
 
33
    max_length=32;
 
34
    args[0]->collation.set(
 
35
      get_charset_by_csname(args[0]->collation.collation->csname,
 
36
                            MY_CS_BINSORT,MYF(0)), DERIVATION_COERCIBLE);
 
37
  }
 
38
 
 
39
};
 
40
 
 
41
 
 
42
String *Item_func_md5::val_str(String *str)
 
43
{
 
44
  assert(fixed == 1);
 
45
  String * sptr= args[0]->val_str(str);
 
46
  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;
36
71
  return 0;
37
72
}
38
73
 
39
 
char *udf_doit_md5(UDF_INIT *initid, UDF_ARGS *args, char *result,
40
 
                           unsigned long *length, char *is_null, char *error)
41
 
{
42
 
  MD5_CTX context;
43
 
  uchar digest[16];
44
 
 
45
 
  (void)initid;
46
 
 
47
 
  MD5_Init(&context);
48
 
 
49
 
  MD5_Update(&context, args->args[0], args->lengths[0]);
50
 
 
51
 
  MD5_Final(digest, &context);
52
 
 
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
 
 
 
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
};
78
84
 
79
85
static int md5udf_plugin_init(void *p)
80
86
{
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;
 
87
  udf_func **f = (udf_func**) p;
 
88
 
 
89
  *f= &md5udf;
93
90
 
94
91
  return 0;
95
92
}