Overpass API regression suite coverage (Saxony) / template_db/lz4_wrapper.cc

template_db/lz4_wrapper.cc

13 of 48 instrumented lines run by the suite (27.1%) 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 "lz4_wrapper.h"
21#include <cstring>
22#include <iomanip>
23#include <iostream>
24#include <sstream>
25#include <stdexcept>
28namespace
30 template < typename T >
31 std::string to_string(T t)
320 {
330 std::ostringstream out;
340 out<<std::setprecision(14)<<t;
350 return out.str();
360 }
40LZ4_Deflate::Error::Error(int error_code_)
410 : std::runtime_error("LZ4_Deflate: " + to_string(error_code_)), error_code(error_code_)
420{}
450LZ4_Deflate::LZ4_Deflate() { }
470LZ4_Deflate::~LZ4_Deflate() { }
49int LZ4_Deflate::compress(const void* in, int in_size, void* out, int out_buffer_size)
500{
510#ifdef HAVE_LZ4
530 int ret = LZ4_compress_limitedOutput((const char*) in, (char *) out + 4, in_size, out_buffer_size - 4);
550 if (ret == 0)
560 { // compression failed
570 if (in_size > out_buffer_size - 4)
580 throw std::runtime_error("LZ4: output buffer too small during compression");
600 *(int*)out = in_size * -1;
610 std::memcpy ((char *) out + 4, (const char*)in, in_size);
620 ret = in_size;
630 }
640 else
650 *(int*)out = ret;
670 return ret + 4;
69#else
71 throw std::runtime_error("Overpass API was compiled without lz4 compression library support");
73#endif
740}
77LZ4_Inflate::Error::Error(int error_code_)
780 : std::runtime_error("LZ4_Inflate: " + to_string(error_code_)), error_code(error_code_)
790{}
82937967668 qLZ4_Inflate::LZ4_Inflate() { }
84937967668 qLZ4_Inflate::~LZ4_Inflate() { }
87int LZ4_Inflate::decompress(const void* in, int in_size, void* out, int out_buffer_size)
88937967668 q{
89937967668 q#ifdef HAVE_LZ4
91937967668 q int ret;
92937967668 q int in_buffer_size = *(int*)in;
94937967668 q if (in_buffer_size > 0)
95937967668 q {
96937967668 q ret = LZ4_decompress_safe((const char*) in + 4, (char*) out, in_buffer_size, out_buffer_size);
97937967668 q if (ret < 0)
980 throw std::runtime_error("LZ4_decompress_safe failed");
99937967668 q }
1000 else
1010 {
1020 in_buffer_size *= -1;
1030 if (in_buffer_size > out_buffer_size)
1040 throw std::runtime_error ("LZ4: output buffer too small during decompression");
1060 std::memcpy ((char*) out, (const char*) in + 4, in_buffer_size);
1070 ret = in_buffer_size;
1080 }
109937967668 q return ret;
111#else
113 throw std::runtime_error("Overpass API was compiled without lz4 compression library support");
115#endif
116937967668 q}