1
/* Copyright (C) 2000 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
/* Written by Sinisa Milivojevic <sinisa@mysql.com> */
18
#include "mysys_priv.h"
21
#include <mystrings/m_string.h>
25
Swap the contents of two variables.
27
#define swap_variables(TYPE, a, b) \
36
This replaces the packet with a compressed packet
40
packet Data to compress. This is is replaced with the compressed data.
41
len Length of data to compress at 'packet'
42
complen out: 0 if packet was not compressed
45
1 error. 'len' is not changed'
46
0 ok. In this case 'len' contains the size of the compressed packet
49
bool my_compress(uchar *packet, size_t *len, size_t *complen)
51
if (*len < MIN_COMPRESS_LENGTH)
57
uchar *compbuf=my_compress_alloc(packet,len,complen);
59
return(*complen ? 0 : 1);
60
memcpy(packet,compbuf,*len);
61
my_free(compbuf,MYF(MY_WME));
67
uchar *my_compress_alloc(const uchar *packet, size_t *len, size_t *complen)
72
*complen= *len * 120 / 100 + 12;
74
if (!(compbuf= (uchar *) my_malloc(*complen, MYF(MY_WME))))
75
return 0; /* Not enough memory */
77
tmp_complen= *complen;
78
res= compress((Bytef*) compbuf, &tmp_complen, (Bytef*) packet, (uLong) *len);
79
*complen= tmp_complen;
83
my_free(compbuf, MYF(MY_WME));
90
my_free(compbuf, MYF(MY_WME));
93
/* Store length of compressed packet in *len */
94
swap_variables(size_t, *len, *complen);
104
packet Compressed data. This is is replaced with the orignal data.
105
len Length of compressed data
106
complen Length of the packet buffer (must be enough for the original
111
0 ok. In this case 'complen' contains the updated size of the
115
bool my_uncompress(uchar *packet, size_t len, size_t *complen)
119
if (*complen) /* If compressed */
121
uchar *compbuf= (uchar *) my_malloc(*complen,MYF(MY_WME));
124
return(1); /* Not enough memory */
126
tmp_complen= *complen;
127
error= uncompress((Bytef*) compbuf, &tmp_complen, (Bytef*) packet,
129
*complen= tmp_complen;
131
{ /* Probably wrong packet */
132
my_free(compbuf, MYF(MY_WME));
135
memcpy(packet, compbuf, *complen);
136
my_free(compbuf, MYF(MY_WME));