Skip to content

Commit d8a7c40

Browse files
committed
add image_crc32 function
1 parent 4547223 commit d8a7c40

File tree

3 files changed

+129
-4
lines changed

3 files changed

+129
-4
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mysql-image-plugin
33

44
Build
55
-----
6-
gcc -I/usr/include/mysql -I./ -fPIC -shared -o image.so image.c
6+
gcc -O3 -g -I/usr/include/mysql -I/usr/include/sys -fPIC -shared -o image.so image.c
77
sudo mv image.so /usr/lib/mysql/plugin/
88

99
Setup
@@ -20,11 +20,17 @@ Plugin
2020
drop function image_check;
2121
drop function image_remove;
2222
drop function image_rename;
23+
drop function image_crc32;
24+
drop function image_md5sum;
2325

2426
create function image_check returns string soname 'image.so';
2527
create function image_remove returns string soname 'image.so';
2628
create function image_rename returns string soname 'image.so';
29+
create function image_crc32 returns string soname 'image.so';
30+
create function image_md5sum returns string soname 'image.so';
2731

2832
select image_check('/tmp/filename');
2933
select image_remove('/tmp/filename');
30-
select image_rename('/tmp/aa','/tmp/bb');
34+
select image_rename('/tmp/aa','/tmp/bb');
35+
select image_crc32('/tmp/test.jpg');
36+
select image_md5sum('/tmp/test');

src/image.c

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Author: netkiller<netkiller@msn.com>
77
#include <mysql.h>
88
#include <string.h>
99
#include <io.h>
10-
10+
#include <openssl/md5.h>
1111
#include "image.h"
1212

1313
/* ------------------------ image_check ----------------------------- */
@@ -18,7 +18,7 @@ my_bool image_check_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
1818
if (args->arg_count != 1)
1919
{
2020
strncpy(message,
21-
"two arguments must be supplied: image_check('<filename>').",
21+
"one arguments must be supplied: image_check('<filename>').",
2222
MYSQL_ERRMSG_SIZE);
2323
return 1;
2424
}
@@ -168,3 +168,93 @@ void image_remove_deinit(UDF_INIT *initid)
168168
{
169169
return;
170170
}
171+
172+
/* ------------------------ image_crc32 ----------------------------- */
173+
174+
my_bool image_crc32_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
175+
{
176+
if (args->arg_count != 1)
177+
{
178+
strncpy(message,
179+
"one arguments must be supplied: image_crc32('<filename>').",
180+
MYSQL_ERRMSG_SIZE);
181+
return 1;
182+
}
183+
184+
args->arg_type[0]= STRING_RESULT;
185+
return 0;
186+
}
187+
188+
char *image_crc32(UDF_INIT *initid, UDF_ARGS *args,
189+
__attribute__ ((unused)) char *result,
190+
unsigned long *length,
191+
__attribute__ ((unused)) char *is_null,
192+
__attribute__ ((unused)) char *error)
193+
{
194+
195+
char *status;
196+
char *data;
197+
198+
data = readfile( args->args[0]);
199+
if( data != NULL ){
200+
asprintf(&status, "%lu", crc32(0, (const void*)data, strlen(data)));
201+
}else{
202+
status = "false";
203+
}
204+
*length = strlen(status);
205+
return ((char *)status);
206+
}
207+
208+
void image_crc32_deinit(UDF_INIT *initid)
209+
{
210+
return;
211+
}
212+
213+
/* ------------------------ image_md5sum ----------------------------- */
214+
/*
215+
my_bool image_md5sum_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
216+
{
217+
if (args->arg_count != 1)
218+
{
219+
strncpy(message,
220+
"one arguments must be supplied: image_md5sum('<filename>').",
221+
MYSQL_ERRMSG_SIZE);
222+
return 1;
223+
}
224+
225+
args->arg_type[0]= STRING_RESULT;
226+
return 0;
227+
}
228+
229+
char *image_md5sum(UDF_INIT *initid, UDF_ARGS *args,
230+
__attribute__ ((unused)) char *result,
231+
unsigned long *length,
232+
__attribute__ ((unused)) char *is_null,
233+
__attribute__ ((unused)) char *error)
234+
{
235+
236+
char *status;
237+
char *data;
238+
unsigned char digest[MD5_DIGEST_LENGTH];
239+
char string[33];
240+
int i;
241+
data = readfile( args->args[0] );
242+
if( data != NULL ){
243+
MD5((unsigned char*)&data, strlen(data), (unsigned char*)&digest);
244+
for(i = 0; i < 16; i++)
245+
sprintf(&string[i*2], "%02x", (unsigned int)digest[i]);
246+
247+
asprintf(&status, "md5 digest: %s", string);
248+
//status = string;
249+
}else{
250+
status = "false";
251+
}
252+
*length = strlen(status);
253+
return ((char *)status);
254+
}
255+
256+
void image_md5sum_deinit(UDF_INIT *initid)
257+
{
258+
return;
259+
}
260+
*/

src/image.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,32 @@ void image_remove_deinit(UDF_INIT *initid);
1313
my_bool image_rename_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
1414
char *image_rename(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error);
1515
void image_rename_deinit(UDF_INIT *initid);
16+
17+
my_bool image_crc32_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
18+
char *image_crc32(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error);
19+
void image_crc32_deinit(UDF_INIT *initid);
20+
/*
21+
my_bool image_md5sum_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
22+
char *image_md5sum(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error);
23+
void image_md5sum_deinit(UDF_INIT *initid);
24+
*/
25+
char * readfile(char * path)
26+
{
27+
FILE * file;
28+
char * data;
29+
int *length;
30+
31+
file = fopen(path, "rb");
32+
if (file == NULL)
33+
{
34+
return NULL;
35+
}
36+
fseek(file, 0, SEEK_END);
37+
*length = ftell(file);
38+
data = (char *)malloc((*length + 1) * sizeof(char));
39+
rewind(file);
40+
*length = fread(data, 1, *length, file);
41+
//data[*length] = '\0';
42+
fclose(file);
43+
return data;
44+
}

0 commit comments

Comments
 (0)