~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_compress.c

  • Committer: Patrick Galbraith
  • Date: 2008-07-24 16:57:40 UTC
  • mto: (202.2.4 rename-mysql-to-drizzle)
  • mto: This revision was merged to the branch mainline in revision 212.
  • Revision ID: patg@ishvara-20080724165740-x58yw6zs6d9o17lf
Most everything working with client rename
mysqlslap test still fails... can't connect to the server

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000 MySQL AB
 
2
 
 
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.
 
6
 
 
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.
 
11
 
 
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 */
 
15
 
 
16
/* Written by Sinisa Milivojevic <sinisa@mysql.com> */
 
17
 
 
18
#include <my_global.h>
 
19
#include <my_sys.h>
 
20
#include <m_string.h>
 
21
#include <zlib.h>
 
22
 
 
23
/*
 
24
   This replaces the packet with a compressed packet
 
25
 
 
26
   SYNOPSIS
 
27
     my_compress()
 
28
     packet     Data to compress. This is is replaced with the compressed data.
 
29
     len        Length of data to compress at 'packet'
 
30
     complen    out: 0 if packet was not compressed
 
31
 
 
32
   RETURN
 
33
     1   error. 'len' is not changed'
 
34
     0   ok.  In this case 'len' contains the size of the compressed packet
 
35
*/
 
36
 
 
37
bool my_compress(uchar *packet, size_t *len, size_t *complen)
 
38
{
 
39
  if (*len < MIN_COMPRESS_LENGTH)
 
40
  {
 
41
    *complen=0;
 
42
  }
 
43
  else
 
44
  {
 
45
    uchar *compbuf=my_compress_alloc(packet,len,complen);
 
46
    if (!compbuf)
 
47
      return(*complen ? 0 : 1);
 
48
    memcpy(packet,compbuf,*len);
 
49
    my_free(compbuf,MYF(MY_WME));
 
50
  }
 
51
  return(0);
 
52
}
 
53
 
 
54
 
 
55
uchar *my_compress_alloc(const uchar *packet, size_t *len, size_t *complen)
 
56
{
 
57
  uchar *compbuf;
 
58
  uLongf tmp_complen;
 
59
  int res;
 
60
  *complen=  *len * 120 / 100 + 12;
 
61
 
 
62
  if (!(compbuf= (uchar *) my_malloc(*complen, MYF(MY_WME))))
 
63
    return 0;                                   /* Not enough memory */
 
64
 
 
65
  tmp_complen= *complen;
 
66
  res= compress((Bytef*) compbuf, &tmp_complen, (Bytef*) packet, (uLong) *len);
 
67
  *complen=    tmp_complen;
 
68
 
 
69
  if (res != Z_OK)
 
70
  {
 
71
    my_free(compbuf, MYF(MY_WME));
 
72
    return 0;
 
73
  }
 
74
 
 
75
  if (*complen >= *len)
 
76
  {
 
77
    *complen= 0;
 
78
    my_free(compbuf, MYF(MY_WME));
 
79
    return 0;
 
80
  }
 
81
  /* Store length of compressed packet in *len */
 
82
  swap_variables(size_t, *len, *complen);
 
83
  return compbuf;
 
84
}
 
85
 
 
86
 
 
87
/*
 
88
  Uncompress packet
 
89
 
 
90
   SYNOPSIS
 
91
     my_uncompress()
 
92
     packet     Compressed data. This is is replaced with the orignal data.
 
93
     len        Length of compressed data
 
94
     complen    Length of the packet buffer (must be enough for the original
 
95
                data)
 
96
 
 
97
   RETURN
 
98
     1   error
 
99
     0   ok.  In this case 'complen' contains the updated size of the
 
100
              real data.
 
101
*/
 
102
 
 
103
bool my_uncompress(uchar *packet, size_t len, size_t *complen)
 
104
{
 
105
  uLongf tmp_complen;
 
106
 
 
107
  if (*complen)                                 /* If compressed */
 
108
  {
 
109
    uchar *compbuf= (uchar *) my_malloc(*complen,MYF(MY_WME));
 
110
    int error;
 
111
    if (!compbuf)
 
112
      return(1);                                /* Not enough memory */
 
113
 
 
114
    tmp_complen= *complen;
 
115
    error= uncompress((Bytef*) compbuf, &tmp_complen, (Bytef*) packet,
 
116
                      (uLong) len);
 
117
    *complen= tmp_complen;
 
118
    if (error != Z_OK)
 
119
    {                                           /* Probably wrong packet */
 
120
      my_free(compbuf, MYF(MY_WME));
 
121
      return(1);
 
122
    }
 
123
    memcpy(packet, compbuf, *complen);
 
124
    my_free(compbuf, MYF(MY_WME));
 
125
  }
 
126
  else
 
127
    *complen= len;
 
128
  return(0);
 
129
}