24
zipstream Library License:
25
--------------------------
27
The zlib/libpng License Copyright (c) 2003 Jonathan de Halleux.
29
This software is provided 'as-is', without any express or implied warranty.
30
In no event will the authors be held liable for any damages arising from the use of this software.
32
Permission is granted to anyone to use this software for any purpose, including commercial applications,
33
and to alter it and redistribute it freely, subject to the following restrictions:
35
1. The origin of this software must not be misrepresented; you must not claim that you wrote the
36
original software. If you use this software in a product, an acknowledgment in the product
37
documentation would be appreciated but is not required.
39
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
41
3. This notice may not be removed or altered from any source distribution
43
Author: Jonathan de Halleux, dehalleux@pelikhan.com, 2003
46
#include "zipstream.h"
54
#include "zip_stream_test.h"
57
namespace zlib_stream{
63
dummy(float f_ = 3.14f, int i_= 1) : f(f_), i(i_)
65
void reset(){f=0.0;i=0;};
67
friend ostream& operator << ( ostream& out, dummy const& d)
69
out<<" "<<d.f<<" "<<d.i;
72
friend istream& operator >> ( istream& in, dummy& d)
78
friend wostream& operator << ( wostream& out, dummy const& d)
80
out<<L" "<<d.f<<L" "<<d.i;
83
friend wistream& operator >> ( wistream& in, dummy& d)
93
void test_buffer_to_buffer()
96
char in_buffer[n]={'\0'};
97
char zip_buffer[n]={'\0'};
99
for (size_t i=0;i<n-1;++i)
100
in_buffer[i]=static_cast<char>(48+i%48);
104
zip_ostream zipper(out);
106
zipper.write(in_buffer, n);
108
cerr<<"failed to write stream"<<endl;
113
istringstream in( out.str() );
114
zip_istream unzipper(in);
115
unzipper.read(zip_buffer, n);
117
cerr<<"buffers equals: "<<endl
118
<<"-----------------"<<endl
119
<<"\t crc source:"<<crc32(0L,(unsigned char*)in_buffer,n)<<endl
120
<<"\t crc result:"<<crc32(0L,(unsigned char*)zip_buffer,n)<<endl;
124
void test_wbuffer_to_wbuffer()
126
const size_t n= 1024;
127
wchar_t in_buffer[n]={'\0'};
128
wchar_t zip_buffer[n]={'\0'};
130
for (size_t i=0;i<n-1;++i)
131
in_buffer[i]=static_cast<wchar_t>(48+i%48);
135
zip_wostream zipper(out);
137
zipper.write(in_buffer, n);
139
cerr<<"failed to write stream"<<endl;
144
wistringstream in( out.str() );
145
zip_wistream unzipper(in);
146
unzipper.read(zip_buffer, n);
148
wcerr<<L"buffers equals: "<<endl
149
<<L"-----------------"<<endl
150
<<L"\t crc source:"<<crc32(0L,(unsigned char*)in_buffer,n*sizeof(wchar_t))<<endl
151
<<L"\t crc result:"<<crc32(0L,(unsigned char*)zip_buffer,n*sizeof(wchar_t))<<endl;
154
void test_string_string()
156
// create some test values
158
string s_r="",s = "std::string";
159
double d_r=0,d = asin( static_cast<double>(1.0) ) *2.0;
160
float f_r=0, f = static_cast<float>(asin( 1.0 ) *2.0);
161
unsigned int ui_r=0,ui = rand();
162
unsigned long ul_r=0,ul = rand();
163
unsigned short us_r=0, us = rand();
164
dummy dum,dum_r(0,0);
166
/*----------------------------------------------------------------------
168
Testing string to string zipping/unzipping
170
-------------------------------------------------------------------------*/
171
// creating the target zip string, could be a fstream
172
ostringstream ostringstream_(ios::out );
173
// creating the zip layer
174
zip_ostream zipper(ostringstream_);
177
zipper<<f<<" "<<d<<" "<<ui<<" "<<ul<<" "<<us<<" "<<c<<" "<<dum;
178
// zip ostream needs special flushing...
181
// create a stream on zip string
182
istringstream istringstream_( ostringstream_.str(), ios::in);
183
// create unzipper istream
184
zip_istream unzipper( istringstream_);
187
unzipper>>f_r>>d_r>>ui_r>>ul_r>>us_r>>c_r>>dum_r;
190
cerr<<"tests string-string, char:"<<endl
191
<<"----------------------------"<<endl
192
<<"double : "<<d<<" "<<d_r<<endl
193
<<"char : "<<c<<" "<<c_r<<endl
194
<<"float : "<<f<<" "<<f_r<<endl
195
<<"unsigned int : "<<ui<<" "<<ui_r<<endl
196
<<"unsigned long : "<<ul<<" "<<ul_r<<endl
197
<<"unsigned short : "<<us<<" "<<us_r<<endl
198
<<"dummy class: "<<dum<<" "<<dum_r<<endl
203
void test_wstring_wstring()
205
// create some test values
207
double d_r=0,d = asin( 1.0 ) *2.0;
208
float f_r=0, f = static_cast<float>(asin( 1.0 ) *2.0);
209
unsigned int ui_r=0,ui = rand();
210
unsigned long ul_r=0,ul = rand();
211
unsigned short us_r=0, us = rand();
212
dummy dum,dum_r(0,0);
216
Testing wide characters
223
// creating the target zip string, could be a fstream
224
wostringstream wostringstream_;
225
// creating the zip layer
226
zip_wostream wzipper(wostringstream_);
229
wzipper<<f<<L" "<<d<<L" "<<ui<<L" "<<ul<<L" "<<us<<L" "<<dum;
230
// zip ostream needs special flushing...
234
// create a stream on zip string
235
wistringstream wistringstream_( wostringstream_.str());
236
// create unzipper istream
237
zip_wistream wunzipper( wistringstream_ );
240
wunzipper>>f_r>>d_r>>ui_r>>ul_r>>us_r>>dum_r;
243
cerr<<"tests string-string (wchar_t):"<<endl
244
<<"------------------------------"<<endl
245
<<"double : "<<d<<" "<<d_r<<endl
246
<<"float : "<<f<<" "<<f_r<<endl
247
<<"unsigned int : "<<ui<<" "<<ui_r<<endl
248
<<"unsigned long : "<<ul<<" "<<ul_r<<endl
249
<<"unsigned short : "<<us<<" "<<us_r<<endl
250
<<"dummy class: "<<dum<<" "<<dum_r<<endl
255
void test_file_file(bool add_gzip_header)
257
// create some test values
259
const char* sz = "const char*";
260
string s_r="",s = "std::string";
261
double d_r=0,d = asin( 1.0 ) *2.0;
262
float f_r=0, f = static_cast<float>(asin( 1.0 ) *2.0);
263
unsigned int ui_r=0,ui = rand();
264
unsigned long ul_r=0,ul = rand();
265
unsigned short us_r=0, us = rand();
266
dummy dum,dum_r(0,0);
267
char sbuf[256]={'-'};
268
long crc_z(0), crc_uz(0);
269
long in_size_z(0), in_size_uz(0), out_size_z(0), out_size_uz(0);
271
/*----------------------------------------------------------------------------
273
Testing file to file unzipping
275
------------------------------------------------------------------------------*/
278
ui_r=ul_r=us_r=0; dum_r.reset();
281
// creating the target zip string, could be a fstream
282
ofstream ofstream_("test.zip",ios::out | ios::binary );
283
// creating the zip layer
284
zip_ostream fzipper(ofstream_, add_gzip_header);
286
fzipper<<f<<" "<<d<<" "<<ui<<" "<<ul<<" "<<us<<" "<<c<<" "<<dum;
287
// zip ostream needs special flushing...
289
crc_z = fzipper.rdbuf()->get_crc();
290
in_size_z = fzipper.rdbuf()->get_in_size();
291
out_size_z = fzipper.rdbuf()->get_out_size();
294
// create a stream on zip string
296
ifstream_.open("test.zip", ios::in | ios::binary);
297
if (!ifstream_.is_open())
299
cerr<<"Could not open file test.zip"<<endl;
301
// create unzipper istream
302
zip_istream funzipper( ifstream_);
305
funzipper>>f_r>>d_r>>ui_r>>ul_r>>us_r>>c_r>>dum_r;
306
funzipper.read_footer();
308
crc_uz = funzipper.rdbuf()->get_crc();
309
in_size_uz = funzipper.rdbuf()->get_in_size();
310
out_size_uz = funzipper.rdbuf()->get_out_size();
314
cerr<<"tests file-to-file (char, "<<(funzipper.is_gzip() ? "gzip" : "no gzip")<<"):"<<endl
315
<<"------------------------------"<<endl
316
<<"double : "<<d<<" "<<d_r<<endl
317
<<"char : "<<c<<" "<<c_r<<endl
318
<<"float : "<<f<<" "<<f_r<<endl
319
<<"unsigned int : "<<ui<<" "<<ui_r<<endl
320
<<"unsigned long : "<<ul<<" "<<ul_r<<endl
321
<<"unsigned short : "<<us<<" "<<us_r<<endl
322
<<"dummy class: "<<dum<<" "<<dum_r<<endl
323
<<"crc (zipped, unzipped, from file): "<<crc_z<<" "<<crc_uz<<" "<<funzipper.get_gzip_crc()<<endl
324
<<"uncompressed data size: "<<in_size_z<<" "<<out_size_uz<<" "<<funzipper.get_gzip_data_size()<<endl
325
<<"compressed data size: "<<out_size_z<<" "<<in_size_uz<<endl
326
<<"check_crc: "<<( funzipper.check_crc() ? "ok" : "failed")<<endl
327
<<"check_data_size: "<<( funzipper.check_data_size() ? "ok" : "failed")<<endl
336
ofstream outFile("test.gz", ios::out | ios::binary);
337
zip_ostream zipOut(outFile,true);
339
for (i = 0; i < 102400; i++)
341
buff[i] = (char)48+i%48;
343
zipOut.write(buff, sizeof(buff));
346
ifstream inFile("test.gz", ios::in | ios::binary);
348
zip_istream unzipper(inFile);
352
for (i = 0; i < 102400; i++)
357
unzipper.read_footer();
359
std::cout << "DoMyTest() "
360
<< ", outsize " << unzipper.get_out_size()
361
<< ", insize " << unzipper.get_in_size()
362
<< ", z_err " << unzipper.get_zerr()
363
<< ", crc " << unzipper.get_crc()
364
<< ", gzip data size " << unzipper.get_gzip_data_size() << std::endl;
24
zipstream Library License:
25
--------------------------
27
The zlib/libpng License Copyright (c) 2003 Jonathan de Halleux.
29
This software is provided 'as-is', without any express or implied warranty.
30
In no event will the authors be held liable for any damages arising from the use of this software.
32
Permission is granted to anyone to use this software for any purpose, including commercial applications,
33
and to alter it and redistribute it freely, subject to the following restrictions:
35
1. The origin of this software must not be misrepresented; you must not claim that you wrote the
36
original software. If you use this software in a product, an acknowledgment in the product
37
documentation would be appreciated but is not required.
39
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
41
3. This notice may not be removed or altered from any source distribution
43
Author: Jonathan de Halleux, dehalleux@pelikhan.com, 2003
46
#include "zipstream.h"
54
#include "zip_stream_test.h"
57
namespace zlib_stream{
63
dummy(float f_ = 3.14f, int i_= 1) : f(f_), i(i_)
65
void reset(){f=0.0;i=0;};
67
friend ostream& operator << ( ostream& out, dummy const& d)
69
out<<" "<<d.f<<" "<<d.i;
72
friend istream& operator >> ( istream& in, dummy& d)
78
friend wostream& operator << ( wostream& out, dummy const& d)
80
out<<L" "<<d.f<<L" "<<d.i;
83
friend wistream& operator >> ( wistream& in, dummy& d)
93
void test_buffer_to_buffer()
96
char in_buffer[n]={'\0'};
97
char zip_buffer[n]={'\0'};
99
for (size_t i=0;i<n-1;++i)
100
in_buffer[i]=static_cast<char>(48+i%48);
104
zip_ostream zipper(out);
106
zipper.write(in_buffer, n);
108
cerr<<"failed to write stream"<<endl;
113
istringstream in( out.str() );
114
zip_istream unzipper(in);
115
unzipper.read(zip_buffer, n);
117
cerr<<"buffers equals: "<<endl
118
<<"-----------------"<<endl
119
<<"\t crc source:"<<crc32(0L,(unsigned char*)in_buffer,n)<<endl
120
<<"\t crc result:"<<crc32(0L,(unsigned char*)zip_buffer,n)<<endl;
124
void test_wbuffer_to_wbuffer()
126
const size_t n= 1024;
127
wchar_t in_buffer[n]={'\0'};
128
wchar_t zip_buffer[n]={'\0'};
130
for (size_t i=0;i<n-1;++i)
131
in_buffer[i]=static_cast<wchar_t>(48+i%48);
135
zip_wostream zipper(out);
137
zipper.write(in_buffer, n);
139
cerr<<"failed to write stream"<<endl;
144
wistringstream in( out.str() );
145
zip_wistream unzipper(in);
146
unzipper.read(zip_buffer, n);
148
wcerr<<L"buffers equals: "<<endl
149
<<L"-----------------"<<endl
150
<<L"\t crc source:"<<crc32(0L,(unsigned char*)in_buffer,n*sizeof(wchar_t))<<endl
151
<<L"\t crc result:"<<crc32(0L,(unsigned char*)zip_buffer,n*sizeof(wchar_t))<<endl;
154
void test_string_string()
156
// create some test values
158
string s_r="",s = "std::string";
159
double d_r=0,d = asin( static_cast<double>(1.0) ) *2.0;
160
float f_r=0, f = static_cast<float>(asin( 1.0 ) *2.0);
161
unsigned int ui_r=0,ui = rand();
162
unsigned long ul_r=0,ul = rand();
163
unsigned short us_r=0, us = rand();
164
dummy dum,dum_r(0,0);
166
/*----------------------------------------------------------------------
168
Testing string to string zipping/unzipping
170
-------------------------------------------------------------------------*/
171
// creating the target zip string, could be a fstream
172
ostringstream ostringstream_(ios::out );
173
// creating the zip layer
174
zip_ostream zipper(ostringstream_);
177
zipper<<f<<" "<<d<<" "<<ui<<" "<<ul<<" "<<us<<" "<<c<<" "<<dum;
178
// zip ostream needs special flushing...
181
// create a stream on zip string
182
istringstream istringstream_( ostringstream_.str(), ios::in);
183
// create unzipper istream
184
zip_istream unzipper( istringstream_);
187
unzipper>>f_r>>d_r>>ui_r>>ul_r>>us_r>>c_r>>dum_r;
190
cerr<<"tests string-string, char:"<<endl
191
<<"----------------------------"<<endl
192
<<"double : "<<d<<" "<<d_r<<endl
193
<<"char : "<<c<<" "<<c_r<<endl
194
<<"float : "<<f<<" "<<f_r<<endl
195
<<"unsigned int : "<<ui<<" "<<ui_r<<endl
196
<<"unsigned long : "<<ul<<" "<<ul_r<<endl
197
<<"unsigned short : "<<us<<" "<<us_r<<endl
198
<<"dummy class: "<<dum<<" "<<dum_r<<endl
203
void test_wstring_wstring()
205
// create some test values
207
double d_r=0,d = asin( 1.0 ) *2.0;
208
float f_r=0, f = static_cast<float>(asin( 1.0 ) *2.0);
209
unsigned int ui_r=0,ui = rand();
210
unsigned long ul_r=0,ul = rand();
211
unsigned short us_r=0, us = rand();
212
dummy dum,dum_r(0,0);
216
Testing wide characters
223
// creating the target zip string, could be a fstream
224
wostringstream wostringstream_;
225
// creating the zip layer
226
zip_wostream wzipper(wostringstream_);
229
wzipper<<f<<L" "<<d<<L" "<<ui<<L" "<<ul<<L" "<<us<<L" "<<dum;
230
// zip ostream needs special flushing...
234
// create a stream on zip string
235
wistringstream wistringstream_( wostringstream_.str());
236
// create unzipper istream
237
zip_wistream wunzipper( wistringstream_ );
240
wunzipper>>f_r>>d_r>>ui_r>>ul_r>>us_r>>dum_r;
243
cerr<<"tests string-string (wchar_t):"<<endl
244
<<"------------------------------"<<endl
245
<<"double : "<<d<<" "<<d_r<<endl
246
<<"float : "<<f<<" "<<f_r<<endl
247
<<"unsigned int : "<<ui<<" "<<ui_r<<endl
248
<<"unsigned long : "<<ul<<" "<<ul_r<<endl
249
<<"unsigned short : "<<us<<" "<<us_r<<endl
250
<<"dummy class: "<<dum<<" "<<dum_r<<endl
255
void test_file_file(bool add_gzip_header)
257
// create some test values
259
const char* sz = "const char*";
260
string s_r="",s = "std::string";
261
double d_r=0,d = asin( 1.0 ) *2.0;
262
float f_r=0, f = static_cast<float>(asin( 1.0 ) *2.0);
263
unsigned int ui_r=0,ui = rand();
264
unsigned long ul_r=0,ul = rand();
265
unsigned short us_r=0, us = rand();
266
dummy dum,dum_r(0,0);
267
char sbuf[256]={'-'};
268
long crc_z(0), crc_uz(0);
269
long in_size_z(0), in_size_uz(0), out_size_z(0), out_size_uz(0);
271
/*----------------------------------------------------------------------------
273
Testing file to file unzipping
275
------------------------------------------------------------------------------*/
278
ui_r=ul_r=us_r=0; dum_r.reset();
281
// creating the target zip string, could be a fstream
282
ofstream ofstream_("test.zip",ios::out | ios::binary );
283
// creating the zip layer
284
zip_ostream fzipper(ofstream_, add_gzip_header);
286
fzipper<<f<<" "<<d<<" "<<ui<<" "<<ul<<" "<<us<<" "<<c<<" "<<dum;
287
// zip ostream needs special flushing...
289
crc_z = fzipper.rdbuf()->get_crc();
290
in_size_z = fzipper.rdbuf()->get_in_size();
291
out_size_z = fzipper.rdbuf()->get_out_size();
294
// create a stream on zip string
296
ifstream_.open("test.zip", ios::in | ios::binary);
297
if (!ifstream_.is_open())
299
cerr<<"Could not open file test.zip"<<endl;
301
// create unzipper istream
302
zip_istream funzipper( ifstream_);
305
funzipper>>f_r>>d_r>>ui_r>>ul_r>>us_r>>c_r>>dum_r;
306
funzipper.read_footer();
308
crc_uz = funzipper.rdbuf()->get_crc();
309
in_size_uz = funzipper.rdbuf()->get_in_size();
310
out_size_uz = funzipper.rdbuf()->get_out_size();
314
cerr<<"tests file-to-file (char, "<<(funzipper.is_gzip() ? "gzip" : "no gzip")<<"):"<<endl
315
<<"------------------------------"<<endl
316
<<"double : "<<d<<" "<<d_r<<endl
317
<<"char : "<<c<<" "<<c_r<<endl
318
<<"float : "<<f<<" "<<f_r<<endl
319
<<"unsigned int : "<<ui<<" "<<ui_r<<endl
320
<<"unsigned long : "<<ul<<" "<<ul_r<<endl
321
<<"unsigned short : "<<us<<" "<<us_r<<endl
322
<<"dummy class: "<<dum<<" "<<dum_r<<endl
323
<<"crc (zipped, unzipped, from file): "<<crc_z<<" "<<crc_uz<<" "<<funzipper.get_gzip_crc()<<endl
324
<<"uncompressed data size: "<<in_size_z<<" "<<out_size_uz<<" "<<funzipper.get_gzip_data_size()<<endl
325
<<"compressed data size: "<<out_size_z<<" "<<in_size_uz<<endl
326
<<"check_crc: "<<( funzipper.check_crc() ? "ok" : "failed")<<endl
327
<<"check_data_size: "<<( funzipper.check_data_size() ? "ok" : "failed")<<endl
336
ofstream outFile("test.gz", ios::out | ios::binary);
337
zip_ostream zipOut(outFile,true);
339
for (i = 0; i < 102400; i++)
341
buff[i] = (char)48+i%48;
343
zipOut.write(buff, sizeof(buff));
346
ifstream inFile("test.gz", ios::in | ios::binary);
348
zip_istream unzipper(inFile);
352
for (i = 0; i < 102400; i++)
357
unzipper.read_footer();
359
std::cout << "DoMyTest() "
360
<< ", outsize " << unzipper.get_out_size()
361
<< ", insize " << unzipper.get_in_size()
362
<< ", z_err " << unzipper.get_zerr()
363
<< ", crc " << unzipper.get_crc()
364
<< ", gzip data size " << unzipper.get_gzip_data_size() << std::endl;