0
我正在嘗試爲MySQL創建2個UDF,這些UDF將壓縮和解壓縮數據,以便將其存儲在數據庫中。從這個網站,我已經創建了一個成功的壓縮功能,然後在鄉親們一些幫助解壓縮數據:zlib和MySQL UDFs
#include "string.h"
#include "stdio.h"
#include "mysql/mysql.h"
#include "zlib.h"
#include "stdlib.h"
int main()
{
const char *istream = "test sentence";
ulong srcLen = strlen(istream)+1;
ulong destLen = compressBound(srcLen);
char* ostream = malloc(destLen);
int res = compress(ostream, &destLen, istream, srcLen);
if (res == Z_OK) printf("%s\n", ostream);
else printf("%i", res);
const char *data = ostream;
ulong size = strlen(data) + 1;
char *ret = malloc(size);
unsigned long new_size;
int rez;
//int i = 0;
int sz = 8*size;
new_size = sz = sz + 8 - (sz % 8);
for(;;)
{
//fprintf(stderr,"%d[u]: %d\n",++i,(int)sz);
ret = realloc(ret,sz);
rez = uncompress(ret, &new_size, data, new_size);
if(Z_BUF_ERROR == rez){
sz*=2;
new_size = sz;
continue;
}
break;
}
if(Z_OK==rez){
ret = realloc(ret,new_size + 8 - (new_size % 8));
printf("%s\n", ret);
}
else printf("%i", rez);
return 0;
}
它的工作原理就像一個魅力,所以後來我試圖重建它在2個UDF的MySQL的形式。 壓縮功能:
#include "string.h"
#include "stdio.h"
#include "mysql/mysql.h"
#include "zlib.h"
#include "stdlib.h"
my_bool CompFunc_init(UDF_INIT *initid, UDF_ARGS *args, char *msg)
{
if (args->arg_count != 1)
{
memcpy(msg, "Missing message argument.", 26);
return 1;
}
if (args->arg_type[0] != STRING_RESULT)
{
args->arg_type = STRING_RESULT;
}
initid->ptr = malloc(compressBound(strlen(args->args[0])) + 1);
return 0;
}
char *CompFunc(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, my_bool *is_null, my_bool *error)
{
const char *istream = args->args[0];
ulong srcLen = strlen(istream)+1;
ulong destLen = compressBound(srcLen);
int res = compress(initid->ptr, &destLen, istream, srcLen);
if (res == Z_OK) {
*length = destLen;
sprintf(result,"%s", initid->ptr);
return result;
}
else { sprintf(result, "%i", res); return result;}
}
void CompFunc_deinit(UDF_INIT *initid)
{
free(initid->ptr);
}
解壓功能:
#include "string.h"
#include "stdio.h"
#include "mysql/mysql.h"
#include "zlib.h"
#include "stdlib.h"
my_bool UCompFunc_init(UDF_INIT *initid, UDF_ARGS *args, char *msg)
{
if (args->arg_count != 1)
{
memcpy(msg, "Missing message argument.", 26);
return 1;
}
if (args->arg_type[0] != STRING_RESULT)
{
args->arg_type = STRING_RESULT;
}
initid->ptr = malloc(strlen(args->args[0]) + 1);
return 0;
}
char *UCompFunc(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, my_bool *is_null, my_bool *error)
{
const char *data = args->args[0];
ulong size = strlen(data) + 1;
unsigned long new_size;
int rez;
//int i = 0;
int sz = 8*size;
new_size = sz = sz + 8 - (sz % 8);
for(;;)
{
//fprintf(stderr,"%d[u]: %d\n",++i,(int)sz);
initid->ptr = realloc(initid->ptr,sz);
rez = uncompress(initid->ptr, &new_size, data, new_size);
if(Z_BUF_ERROR == rez){
sz*=2;
new_size = sz;
continue;
}
break;
}
if(Z_OK==rez) initid->ptr = realloc(initid->ptr,new_size + 8 - (new_size % 8));
else { sprintf(result, "%i", rez); return result;}
*length = new_size;
sprintf(result,"%s", initid->ptr);
return result;
}
void UCompFunc_deinit(UDF_INIT *initid)
{
free(initid->ptr);
}
壓縮UDF似乎能正常工作,但解壓UDF將不會返回解壓縮的文本,但Z_BUF_ERROR的錯誤,這意味着有輸入緩衝區有問題!有誰能指出我的錯誤嗎?