2014-01-21 6 views
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의 오류가 있음을 의미 입력 버퍼에 문제가 있습니다! 누군가 나를 위해 실수를 지적 할 수 있을까?

답변

0
  • strlen()을 사용하여 압축 된 데이터의 길이를 가져올 수 없습니다. 0으로 끝나지 않으며 그 안에 0이 포함되어있을 가능성이 큽니다. strlen()의 결과는 압축 된 데이터가 매우 길더라도 매우 작은 숫자 일 수 있습니다. 압축 된 데이터의 길이는 compress()의 두 x 째 인수로 리턴됩니다. 그걸 사용해야합니다.
  • sz = 8*whatever 다음에 sz % 8이 0임을 확신 할 수 있습니다.
  • 매번 출력 버퍼를 재 할당하고 전체 압축 해제를 재 시도하는 것은 엄청난 시간 낭비입니다. 대신 inflateInit(), inflate()inflateEnd() 함수를 사용하여 가능한 버퍼로 압축을 풀고 다시 할당해야합니다.
  • 압축 된 데이터와 함께 압축되지 않은 길이를 보내면 하나의 할당 만하면됩니다.
  • uncompress()의 네 번째 인수는 압축 된 데이터의 길이 여야합니다. 출력 버퍼의 길이가 반복되지 않습니다.
  • uncompress()에 의해 반환되는 경우 무한 루프 상태를 유지한다면 어떻게 Z_BUF_ERROR을 반환 할 수 있습니까?

결론 : 문서 당 uncompress()을 사용하십시오.