my_compress.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:2k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  2.    
  3.    This library is free software; you can redistribute it and/or
  4.    modify it under the terms of the GNU Library General Public
  5.    License as published by the Free Software Foundation; either
  6.    version 2 of the License, or (at your option) any later version.
  7.    
  8.    This library is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11.    Library General Public License for more details.
  12.    
  13.    You should have received a copy of the GNU Library General Public
  14.    License along with this library; if not, write to the Free
  15.    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  16.    MA 02111-1307, USA */
  17. /* Written by Sinisa Milivojevic <sinisa@coresinc.com> */
  18. #include <global.h>
  19. #ifdef HAVE_COMPRESS
  20. #include <my_sys.h>
  21. #include <zlib.h>
  22. /*
  23. ** This replaces the packet with a compressed packet
  24. ** Returns 1 on error
  25. ** *complen is 0 if the packet wasn't compressed
  26. */
  27. my_bool my_compress(byte *packet, ulong *len, ulong *complen)
  28. {
  29.   if (*len < MIN_COMPRESS_LENGTH)
  30.     *complen=0;
  31.   else
  32.   {
  33.     byte *compbuf=my_compress_alloc(packet,len,complen);
  34.     if (!compbuf)
  35.       return *complen ? 0 : 1;
  36.     memcpy(packet,compbuf,*len);
  37.     my_free(compbuf,MYF(MY_WME));   }
  38.   return 0;
  39. }
  40. byte *my_compress_alloc(const byte *packet, ulong *len, ulong *complen)
  41. {
  42.   byte *compbuf;
  43.   *complen =  *len * 120 / 100 + 12;
  44.   if (!(compbuf = (byte *) my_malloc(*complen,MYF(MY_WME))))
  45.     return 0; /* Not enough memory */
  46.   if (compress((Bytef*) compbuf,(ulong *) complen, (Bytef*) packet,
  47.        (uLong) *len ) != Z_OK)
  48.   {
  49.     my_free(compbuf,MYF(MY_WME));
  50.     return 0;
  51.   }
  52.   if (*complen >= *len)
  53.   {
  54.     *complen=0;
  55.     my_free(compbuf,MYF(MY_WME));
  56.     return 0;
  57.   }
  58.   swap(ulong,*len,*complen); /* *len is now packet length */
  59.   return compbuf;
  60. }
  61. my_bool my_uncompress (byte *packet, ulong *len, ulong *complen)
  62. {
  63.   if (*complen) /* If compressed */
  64.   {
  65.     byte *compbuf = (byte *) my_malloc (*complen,MYF(MY_WME));
  66.     if (!compbuf)
  67.       return 1; /* Not enough memory */
  68.     if (uncompress((Bytef*) compbuf, complen, (Bytef*) packet, *len) != Z_OK)
  69.     { /* Probably wrong packet */
  70.       my_free (compbuf,MYF(MY_WME));
  71.       return 1;
  72.     }
  73.     *len = *complen;
  74.     memcpy(packet,compbuf,*len);
  75.     my_free(compbuf,MYF(MY_WME));
  76.   }
  77.   return 0;
  78. }
  79. #endif /* HAVE_COMPRESS */