"Fossies" - the Fresh Open Source Software Archive

Member "mariadb-connector-c-3.0.8-src/libmariadb/ma_compress.c" (18 Dec 2018, 2643 Bytes) of package /linux/misc/mariadb-connector-c-3.0.8-src.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

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