~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_compress.c

  • Committer: Brian Aker
  • Date: 2008-09-04 19:31:00 UTC
  • Revision ID: brian@tangent.org-20080904193100-l849hgghfy4urj43
Changing default character set from this point on.

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 "mysys_priv.h"
 
19
 
 
20
#include <my_sys.h>
 
21
#include <mystrings/m_string.h>
 
22
#include <zlib.h>
 
23
 
 
24
/**
 
25
  Swap the contents of two variables.
 
26
 */
 
27
#define swap_variables(TYPE, a, b) \
 
28
  do {                             \
 
29
    TYPE dummy;                    \
 
30
    dummy= a;                      \
 
31
    a= b;                          \
 
32
    b= dummy;                      \
 
33
  } while (0)
 
34
 
 
35
/*
 
36
   This replaces the packet with a compressed packet
 
37
 
 
38
   SYNOPSIS
 
39
     my_compress()
 
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
 
43
 
 
44
   RETURN
 
45
     1   error. 'len' is not changed'
 
46
     0   ok.  In this case 'len' contains the size of the compressed packet
 
47
*/
 
48
 
 
49
bool my_compress(uchar *packet, size_t *len, size_t *complen)
 
50
{
 
51
  if (*len < MIN_COMPRESS_LENGTH)
 
52
  {
 
53
    *complen=0;
 
54
  }
 
55
  else
 
56
  {
 
57
    uchar *compbuf=my_compress_alloc(packet,len,complen);
 
58
    if (!compbuf)
 
59
      return(*complen ? 0 : 1);
 
60
    memcpy(packet,compbuf,*len);
 
61
    my_free(compbuf,MYF(MY_WME));
 
62
  }
 
63
  return(0);
 
64
}
 
65
 
 
66
 
 
67
uchar *my_compress_alloc(const uchar *packet, size_t *len, size_t *complen)
 
68
{
 
69
  uchar *compbuf;
 
70
  uLongf tmp_complen;
 
71
  int res;
 
72
  *complen=  *len * 120 / 100 + 12;
 
73
 
 
74
  if (!(compbuf= (uchar *) my_malloc(*complen, MYF(MY_WME))))
 
75
    return 0;                                   /* Not enough memory */
 
76
 
 
77
  tmp_complen= *complen;
 
78
  res= compress((Bytef*) compbuf, &tmp_complen, (Bytef*) packet, (uLong) *len);
 
79
  *complen=    tmp_complen;
 
80
 
 
81
  if (res != Z_OK)
 
82
  {
 
83
    my_free(compbuf, MYF(MY_WME));
 
84
    return 0;
 
85
  }
 
86
 
 
87
  if (*complen >= *len)
 
88
  {
 
89
    *complen= 0;
 
90
    my_free(compbuf, MYF(MY_WME));
 
91
    return 0;
 
92
  }
 
93
  /* Store length of compressed packet in *len */
 
94
  swap_variables(size_t, *len, *complen);
 
95
  return compbuf;
 
96
}
 
97
 
 
98
 
 
99
/*
 
100
  Uncompress packet
 
101
 
 
102
   SYNOPSIS
 
103
     my_uncompress()
 
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
 
107
                data)
 
108
 
 
109
   RETURN
 
110
     1   error
 
111
     0   ok.  In this case 'complen' contains the updated size of the
 
112
              real data.
 
113
*/
 
114
 
 
115
bool my_uncompress(uchar *packet, size_t len, size_t *complen)
 
116
{
 
117
  uLongf tmp_complen;
 
118
 
 
119
  if (*complen)                                 /* If compressed */
 
120
  {
 
121
    uchar *compbuf= (uchar *) my_malloc(*complen,MYF(MY_WME));
 
122
    int error;
 
123
    if (!compbuf)
 
124
      return(1);                                /* Not enough memory */
 
125
 
 
126
    tmp_complen= *complen;
 
127
    error= uncompress((Bytef*) compbuf, &tmp_complen, (Bytef*) packet,
 
128
                      (uLong) len);
 
129
    *complen= tmp_complen;
 
130
    if (error != Z_OK)
 
131
    {                                           /* Probably wrong packet */
 
132
      my_free(compbuf, MYF(MY_WME));
 
133
      return(1);
 
134
    }
 
135
    memcpy(packet, compbuf, *complen);
 
136
    my_free(compbuf, MYF(MY_WME));
 
137
  }
 
138
  else
 
139
    *complen= len;
 
140
  return(0);
 
141
}