~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/md5/md5udf.cc

  • Committer: Monty Taylor
  • Date: 2008-08-01 22:33:44 UTC
  • mto: (236.1.42 codestyle)
  • mto: This revision was merged to the branch mainline in revision 261.
  • Revision ID: monty@inaugust.com-20080801223344-vzhlflfmtijp1imv
First pass at gettexizing the error messages.

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