~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_compress.c

  • Committer: Brian Aker
  • Date: 2010-05-27 01:25:56 UTC
  • mfrom: (1567.1.4 new-staging)
  • Revision ID: brian@gaz-20100527012556-5zgkirkl7swbigd6
Merge of Brian, Paul. PBXT compile issue, and test framework cleanup. 

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