18 #include <my_global.h>
40 my_bool my_compress(uchar *packet,
size_t *len,
size_t *complen)
42 DBUG_ENTER(
"my_compress");
43 if (*len < MIN_COMPRESS_LENGTH)
46 DBUG_PRINT(
"note",(
"Packet too short: Not compressed"));
50 uchar *compbuf=my_compress_alloc(packet,len,complen);
52 DBUG_RETURN(*complen ? 0 : 1);
53 memcpy(packet,compbuf,*len);
60 uchar *my_compress_alloc(
const uchar *packet,
size_t *len,
size_t *complen)
65 *complen= *len * 120 / 100 + 12;
67 if (!(compbuf= (uchar *) my_malloc(*complen, MYF(MY_WME))))
70 tmp_complen= (uint) *complen;
71 res= compress((Bytef*) compbuf, &tmp_complen, (Bytef*) packet, (uLong) *len);
72 *complen= tmp_complen;
84 DBUG_PRINT(
"note",(
"Packet got longer on compression; Not compressed"));
88 swap_variables(
size_t, *len, *complen);
109 my_bool my_uncompress(uchar *packet,
size_t len,
size_t *complen)
112 DBUG_ENTER(
"my_uncompress");
116 uchar *compbuf= (uchar *) my_malloc(*complen,MYF(MY_WME));
121 tmp_complen= (uint) *complen;
122 error= uncompress((Bytef*) compbuf, &tmp_complen, (Bytef*) packet,
124 *complen= tmp_complen;
127 DBUG_PRINT(
"error",(
"Can't uncompress packet, error: %d",error));
131 memcpy(packet, compbuf, *complen);
147 #define BLOB_HEADER 12
170 int packfrm(uchar *data,
size_t len,
171 uchar **pack_data,
size_t *pack_len)
174 size_t org_len, comp_len, blob_len;
176 DBUG_ENTER(
"packfrm");
177 DBUG_PRINT(
"enter", (
"data: 0x%lx len: %lu", (
long) data, (ulong) len));
181 if (my_compress((uchar*)data, &org_len, &comp_len))
184 DBUG_PRINT(
"info", (
"org_len: %lu comp_len: %lu", (ulong) org_len,
186 DBUG_DUMP(
"compressed", data, org_len);
189 blob_len= BLOB_HEADER + org_len;
190 if (!(blob= (uchar*) my_malloc(blob_len,MYF(MY_WME))))
195 int4store(blob+4, (uint32) len);
196 int4store(blob+8, (uint32) org_len);
199 memcpy(blob+BLOB_HEADER, data, org_len);
205 DBUG_PRINT(
"exit", (
"pack_data: 0x%lx pack_len: %lu",
206 (
long) *pack_data, (ulong) *pack_len));
228 int unpackfrm(uchar **unpack_data,
size_t *unpack_len,
229 const uchar *pack_data)
232 size_t complen, orglen;
234 DBUG_ENTER(
"unpackfrm");
235 DBUG_PRINT(
"enter", (
"pack_data: 0x%lx", (
long) pack_data));
237 ver= uint4korr(pack_data);
238 orglen= uint4korr(pack_data+4);
239 complen= uint4korr(pack_data+8);
241 DBUG_PRINT(
"blob",(
"ver: %lu complen: %lu orglen: %lu",
242 ver, (ulong) complen, (ulong) orglen));
243 DBUG_DUMP(
"blob->data", pack_data + BLOB_HEADER, complen);
247 if (!(data= my_malloc(MY_MAX(orglen, complen), MYF(MY_WME))))
249 memcpy(data, pack_data + BLOB_HEADER, complen);
251 if (my_uncompress(data, complen, &orglen))
260 DBUG_PRINT(
"exit", (
"frmdata: 0x%lx len: %lu", (
long) *unpack_data,
261 (ulong) *unpack_len));