LCOV - code coverage report
Current view: top level - common - compression_stream.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 7028d852e609 Lines: 55 88 62.5 %
Date: 2019-02-17 14:59:59 Functions: 5 5 100.0 %
Branches: 30 90 33.3 %

           Branch data     Line data    Source code
       1                 :            : /** @file compression_stream.cc
       2                 :            :  * @brief class wrapper around zlib
       3                 :            :  */
       4                 :            : /* Copyright (C) 2007,2009,2012,2013,2014,2016 Olly Betts
       5                 :            :  * Copyright (C) 2009 Richard Boulton
       6                 :            :  * Copyright (C) 2012 Dan Colish
       7                 :            :  *
       8                 :            :  * This program is free software; you can redistribute it and/or modify
       9                 :            :  * it under the terms of the GNU General Public License as published by
      10                 :            :  * the Free Software Foundation; either version 2 of the License, or
      11                 :            :  * (at your option) any later version.
      12                 :            :  *
      13                 :            :  * This program is distributed in the hope that it will be useful,
      14                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      15                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16                 :            :  * GNU General Public License for more details.
      17                 :            :  *
      18                 :            :  * You should have received a copy of the GNU General Public License
      19                 :            :  * along with this program; if not, write to the Free Software
      20                 :            :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
      21                 :            :  */
      22                 :            : 
      23                 :            : #include <config.h>
      24                 :            : #include "compression_stream.h"
      25                 :            : 
      26                 :            : #include "omassert.h"
      27                 :            : #include "str.h"
      28                 :            : #include "stringutils.h"
      29                 :            : 
      30                 :            : #include "xapian/error.h"
      31                 :            : 
      32                 :            : using namespace std;
      33                 :            : 
      34                 :      95916 : CompressionStream::~CompressionStream() {
      35         [ +  + ]:      95916 :     if (deflate_zstream) {
      36                 :            :         // Errors which we care about have already been handled, so just ignore
      37                 :            :         // any which get returned here.
      38                 :      16908 :         (void) deflateEnd(deflate_zstream);
      39                 :      16908 :         delete deflate_zstream;
      40                 :            :     }
      41                 :            : 
      42         [ +  + ]:      95916 :     if (inflate_zstream) {
      43                 :            :         // Errors which we care about have already been handled, so just ignore
      44                 :            :         // any which get returned here.
      45                 :       2435 :         (void) inflateEnd(inflate_zstream);
      46                 :       2435 :         delete inflate_zstream;
      47                 :            :     }
      48                 :            : 
      49         [ +  + ]:      95916 :     delete [] out;
      50                 :      95916 : }
      51                 :            : 
      52                 :            : const char*
      53                 :     151882 : CompressionStream::compress(const char* buf, size_t* p_size) {
      54                 :     151882 :     lazy_alloc_deflate_zstream();
      55                 :     151882 :     size_t size = *p_size;
      56 [ +  + ][ +  + ]:     151882 :     if (!out || out_len < size - 1) {
      57                 :      18959 :         out_len = size - 1;
      58         [ +  + ]:      18959 :         delete [] out;
      59                 :      18959 :         out = new char[out_len];
      60                 :            :     }
      61                 :     151882 :     deflate_zstream->avail_in = static_cast<uInt>(size);
      62                 :            :     deflate_zstream->next_in =
      63                 :     151882 :         reinterpret_cast<Bytef*>(const_cast<char*>(buf));
      64                 :     151882 :     deflate_zstream->next_out = reinterpret_cast<Bytef*>(out);
      65                 :     151882 :     deflate_zstream->avail_out = static_cast<uInt>((size - 1));
      66                 :     151882 :     int zerr = deflate(deflate_zstream, Z_FINISH);
      67         [ +  + ]:     151882 :     if (zerr != Z_STREAM_END) {
      68                 :            :         // Deflate failed - presumably the data wasn't compressible.
      69                 :      86336 :         return NULL;
      70                 :            :     }
      71                 :            : 
      72                 :            :     // If deflate succeeded, then the output was at least one byte smaller than
      73                 :            :     // the input.
      74                 :            : 
      75                 :      65546 :     *p_size = deflate_zstream->total_out;
      76                 :      65546 :     return out;
      77                 :            : }
      78                 :            : 
      79                 :            : bool
      80                 :     285703 : CompressionStream::decompress_chunk(const char* p, int len, string & buf)
      81                 :            : {
      82                 :            :     Bytef blk[8192];
      83                 :            : 
      84                 :            :     inflate_zstream->next_in =
      85                 :     285703 :         reinterpret_cast<Bytef*>(const_cast<char*>(p));
      86                 :     285703 :     inflate_zstream->avail_in = static_cast<uInt>(len);
      87                 :            : 
      88                 :            :     while (true) {
      89                 :     285703 :         inflate_zstream->next_out = blk;
      90                 :     285703 :         inflate_zstream->avail_out = static_cast<uInt>(sizeof(blk));
      91         [ +  - ]:     285703 :         int err = inflate(inflate_zstream, Z_SYNC_FLUSH);
      92 [ +  + ][ -  + ]:     285703 :         if (err != Z_OK && err != Z_STREAM_END) {
      93         [ #  # ]:          0 :             if (err == Z_MEM_ERROR) throw std::bad_alloc();
      94         [ #  # ]:          0 :             string msg = "inflate failed";
      95         [ #  # ]:          0 :             if (inflate_zstream->msg) {
      96         [ #  # ]:          0 :                 msg += " (";
      97         [ #  # ]:          0 :                 msg += inflate_zstream->msg;
      98         [ #  # ]:          0 :                 msg += ')';
      99                 :            :             }
     100 [ #  # ][ #  # ]:          0 :             throw Xapian::DatabaseError(msg);
     101                 :            :         }
     102                 :            : 
     103                 :            :         buf.append(reinterpret_cast<const char *>(blk),
     104         [ +  - ]:     285703 :                    inflate_zstream->next_out - blk);
     105         [ +  + ]:     285703 :         if (err == Z_STREAM_END) return true;
     106         [ +  - ]:      50732 :         if (inflate_zstream->avail_in == 0) return false;
     107                 :     285703 :     }
     108                 :            : }
     109                 :            : 
     110                 :            : void
     111                 :     151882 : CompressionStream::lazy_alloc_deflate_zstream() {
     112         [ +  + ]:     151882 :     if (usual(deflate_zstream)) {
     113         [ +  - ]:     151882 :         if (usual(deflateReset(deflate_zstream) == Z_OK)) return;
     114                 :            :         // Try to recover by deleting the stream and starting from scratch.
     115                 :          0 :         delete deflate_zstream;
     116                 :            :     }
     117                 :            : 
     118                 :      16908 :     deflate_zstream = new z_stream;
     119                 :            : 
     120                 :      16908 :     deflate_zstream->zalloc = reinterpret_cast<alloc_func>(0);
     121                 :      16908 :     deflate_zstream->zfree = reinterpret_cast<free_func>(0);
     122                 :      16908 :     deflate_zstream->opaque = static_cast<voidpf>(0);
     123                 :            : 
     124                 :            :     // -15 means raw deflate with 32K LZ77 window (largest)
     125                 :            :     // memLevel 9 is the highest (8 is default)
     126                 :      16908 :     int err = deflateInit2(deflate_zstream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
     127                 :            :                            -15, 9, compress_strategy);
     128         [ -  + ]:      16908 :     if (rare(err != Z_OK)) {
     129         [ #  # ]:          0 :         if (err == Z_MEM_ERROR) {
     130                 :          0 :             delete deflate_zstream;
     131                 :          0 :             deflate_zstream = 0;
     132                 :          0 :             throw std::bad_alloc();
     133                 :            :         }
     134         [ #  # ]:          0 :         string msg = "deflateInit2 failed (";
     135         [ #  # ]:          0 :         if (deflate_zstream->msg) {
     136         [ #  # ]:          0 :             msg += deflate_zstream->msg;
     137                 :            :         } else {
     138 [ #  # ][ #  # ]:          0 :             msg += str(err);
     139                 :            :         }
     140         [ #  # ]:          0 :         msg += ')';
     141                 :          0 :         delete deflate_zstream;
     142                 :          0 :         deflate_zstream = 0;
     143 [ #  # ][ #  # ]:          0 :         throw Xapian::DatabaseError(msg);
     144                 :            :     }
     145                 :            : }
     146                 :            : 
     147                 :            : void
     148                 :     234971 : CompressionStream::lazy_alloc_inflate_zstream() {
     149         [ +  + ]:     234971 :     if (usual(inflate_zstream)) {
     150         [ +  - ]:     234971 :         if (usual(inflateReset(inflate_zstream) == Z_OK)) return;
     151                 :            :         // Try to recover by deleting the stream and starting from scratch.
     152                 :          0 :         delete inflate_zstream;
     153                 :            :     }
     154                 :            : 
     155                 :       2435 :     inflate_zstream = new z_stream;
     156                 :            : 
     157                 :       2435 :     inflate_zstream->zalloc = reinterpret_cast<alloc_func>(0);
     158                 :       2435 :     inflate_zstream->zfree = reinterpret_cast<free_func>(0);
     159                 :            : 
     160                 :       2435 :     inflate_zstream->next_in = Z_NULL;
     161                 :       2435 :     inflate_zstream->avail_in = 0;
     162                 :            : 
     163                 :       2435 :     int err = inflateInit2(inflate_zstream, -15);
     164         [ -  + ]:       2435 :     if (rare(err != Z_OK)) {
     165         [ #  # ]:          0 :         if (err == Z_MEM_ERROR) {
     166                 :          0 :             delete inflate_zstream;
     167                 :          0 :             inflate_zstream = 0;
     168                 :          0 :             throw std::bad_alloc();
     169                 :            :         }
     170         [ #  # ]:          0 :         string msg = "inflateInit2 failed (";
     171         [ #  # ]:          0 :         if (inflate_zstream->msg) {
     172         [ #  # ]:          0 :             msg += inflate_zstream->msg;
     173                 :            :         } else {
     174 [ #  # ][ #  # ]:          0 :             msg += str(err);
     175                 :            :         }
     176         [ #  # ]:          0 :         msg += ')';
     177                 :          0 :         delete inflate_zstream;
     178                 :          0 :         inflate_zstream = 0;
     179 [ #  # ][ #  # ]:          0 :         throw Xapian::DatabaseError(msg);
     180                 :            :     }
     181                 :            : }

Generated by: LCOV version 1.11