Line data Source code
1 : /* 2 : Mosh: the mobile shell 3 : Copyright 2012 Keith Winstein 4 : 5 : This program is free software: you can redistribute it and/or modify 6 : it under the terms of the GNU General Public License as published by 7 : the Free Software Foundation, either version 3 of the License, or 8 : (at your option) any later version. 9 : 10 : This program 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 General Public License 16 : along with this program. If not, see <http://www.gnu.org/licenses/>. 17 : 18 : In addition, as a special exception, the copyright holders give 19 : permission to link the code of portions of this program with the 20 : OpenSSL library under certain conditions as described in each 21 : individual source file, and distribute linked combinations including 22 : the two. 23 : 24 : You must obey the GNU General Public License in all respects for all 25 : of the code used other than OpenSSL. If you modify file(s) with this 26 : exception, you may extend this exception to your version of the 27 : file(s), but you are not obligated to do so. If you do not wish to do 28 : so, delete this exception statement from your version. If you delete 29 : this exception statement from all source files in the program, then 30 : also delete it here. 31 : */ 32 : 33 : #include <zlib.h> 34 : 35 : #include "compressor.h" 36 : #include "dos_assert.h" 37 : 38 : using namespace Network; 39 : using std::string; 40 : 41 5329 : string Compressor::compress_str( const string &input ) 42 : { 43 5329 : long unsigned int len = BUFFER_SIZE; 44 5329 : dos_assert( Z_OK == compress( buffer, &len, 45 : reinterpret_cast<const unsigned char *>( input.data() ), 46 : input.size() ) ); 47 5329 : return string( reinterpret_cast<char *>( buffer ), len ); 48 : } 49 : 50 5281 : string Compressor::uncompress_str( const string &input ) 51 : { 52 5281 : long unsigned int len = BUFFER_SIZE; 53 5281 : dos_assert( Z_OK == uncompress( buffer, &len, 54 : reinterpret_cast<const unsigned char *>( input.data() ), 55 : input.size() ) ); 56 5281 : return string( reinterpret_cast<char *>( buffer ), len ); 57 : } 58 : 59 : /* construct on first use */ 60 10610 : Compressor & Network::get_compressor( void ) 61 : { 62 11510 : static Compressor the_compressor; 63 10610 : return the_compressor; 64 : }