0
데이터를 압축 및 압축 해제하여 데이터베이스에 저장하기 위해 MySQL 용 UDF 2 개를 작성하려고합니다. 내가 MySQL을위한 2 UDF의 형태로 그것을 다시 시도 그럼 그것은 마법처럼 작동zlib 및 MySQL 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;
}
, : 다음 성공적으로 압축하는 기능을 생성 한이 웹 사이트에있는 사람들의 도움으로 데이터 압축 해제 . 압축 기능 :
#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의 오류가 있음을 의미 입력 버퍼에 문제가 있습니다! 누군가 나를 위해 실수를 지적 할 수 있을까?