Overpass API regression suite coverage (Nova Scotia) / template_db/zlib_wrapper.cc

template_db/zlib_wrapper.cc

0 of 55 instrumented lines run by the suite (0.0%) at 8edf11b0419c (this file on GitHub).

runnever runcolumns: line, executions, queries (click for the queries that run the line)

1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al.
2 *
3 * This file is part of Overpass_API.
4 *
5 * Overpass_API is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * Overpass_API is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>.
17 */
19#include "zlib_wrapper.h"
21#include <cstdlib>
22#include <exception>
23#include <iomanip>
24#include <iostream>
25#include <sstream>
26#include <vector>
29namespace
31 template < typename T >
32 std::string to_string(T t)
330 {
340 std::ostringstream out;
350 out<<std::setprecision(14)<<t;
360 return out.str();
370 }
41Zlib_Deflate::Error::Error(int error_code_)
420 : std::runtime_error("Zlib_Deflate: " + to_string(error_code_)), error_code(error_code_)
430{}
46Zlib_Deflate::Zlib_Deflate(int level)
470{
480 strm.zalloc = Z_NULL;
490 strm.zfree = Z_NULL;
500 strm.opaque = Z_NULL;
510 int ret = deflateInit(&strm, level);
520 if (ret != Z_OK)
530 throw Error(ret);
540}
57Zlib_Deflate::~Zlib_Deflate()
580{
590 deflateEnd(&strm);
600}
63int Zlib_Deflate::compress(const void* in, int in_size, void* out, int out_buffer_size)
640{
650 strm.avail_in = in_size;
660 strm.next_in = (unsigned char*) in;
670 strm.avail_out = out_buffer_size;
680 strm.next_out = (unsigned char*) out;
700 int ret = deflate(&strm, Z_FINISH);
720 if (strm.avail_out == 0 || strm.avail_in != 0 || ret != Z_STREAM_END)
730 throw Error(Z_BUF_ERROR); // Too few output space
750 return out_buffer_size - strm.avail_out;
760}
79Zlib_Inflate::Error::Error(int error_code_)
800 : std::runtime_error("Zlib_Inflate: " + to_string(error_code_)), error_code(error_code_)
810{}
84Zlib_Inflate::Zlib_Inflate()
850{
860 strm.zalloc = Z_NULL;
870 strm.zfree = Z_NULL;
880 strm.opaque = Z_NULL;
890 strm.avail_in = 0;
900 strm.next_in = Z_NULL;
910 int ret = inflateInit(&strm);
920 if (ret != Z_OK)
930 throw Error(ret);
940}
97Zlib_Inflate::~Zlib_Inflate()
980{
990 inflateEnd(&strm);
1000}
103int Zlib_Inflate::decompress(const void* in, int in_size, void* out, int out_buffer_size)
1040{
1050 strm.avail_in = in_size;
1060 strm.next_in = (unsigned char*) in;
1070 strm.avail_out = out_buffer_size;
1080 strm.next_out = (unsigned char*) out;
1100 int ret = inflate(&strm, Z_FINISH);
1120 if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR)
1130 throw Error(ret);
1140 if (ret != Z_STREAM_END)
1150 throw Error(Z_BUF_ERROR); // Decompression incomplete
1170 return out_buffer_size - strm.avail_out;
1180}