我正在尝试为MySQL创建2个UDF,它将压缩和解压缩数据,以便将其进一步存储在数据库中。在本网站上的人们的帮助下,我创建了一个成功压缩然后解压缩数据的函数:
#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错误,这意味着输入缓冲区有问题!谁能指出我的错误?
答案 0 :(得分:0)
strlen()
来获取压缩数据的长度。它不会以零结尾,并且很可能在其中包含零。即使压缩数据很长,strlen()
的结果也可能是一个非常小的数字。压缩数据的长度在compress()
的第二个参数中返回。你应该使用它。sz = 8*whatever
之后,您可以确信sz % 8
为零。inflateInit()
,inflate()
和inflateEnd()
函数,尽可能将其解压缩到可用缓冲区中,然后重新分配。uncompress()
的第四个参数必须是压缩数据的长度。不是重复输出缓冲区的长度。Z_BUF_ERROR
返回,你仍然处于无限循环中,你怎么能返回uncompress()
?底线:根据文档使用uncompress()
。