| 1 |
// Copyright (c) 2009, Whispersoft s.r.l. |
|---|
| 2 |
// All rights reserved. |
|---|
| 3 |
// |
|---|
| 4 |
// Redistribution and use in source and binary forms, with or without |
|---|
| 5 |
// modification, are permitted provided that the following conditions are |
|---|
| 6 |
// met: |
|---|
| 7 |
// |
|---|
| 8 |
// * Redistributions of source code must retain the above copyright |
|---|
| 9 |
// notice, this list of conditions and the following disclaimer. |
|---|
| 10 |
// * Redistributions in binary form must reproduce the above |
|---|
| 11 |
// copyright notice, this list of conditions and the following disclaimer |
|---|
| 12 |
// in the documentation and/or other materials provided with the |
|---|
| 13 |
// distribution. |
|---|
| 14 |
// * Neither the name of Whispersoft s.r.l. nor the names of its |
|---|
| 15 |
// contributors may be used to endorse or promote products derived from |
|---|
| 16 |
// this software without specific prior written permission. |
|---|
| 17 |
// |
|---|
| 18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 19 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 20 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 21 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 22 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 25 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 26 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 27 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 28 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 29 |
// |
|---|
| 30 |
// Authors: Cosmin Tudorache |
|---|
| 31 |
|
|---|
| 32 |
#include "common/base/log.h" |
|---|
| 33 |
#include "common/base/errno.h" |
|---|
| 34 |
#include "common/io/buffer/memory_stream.h" |
|---|
| 35 |
#include "common/io/buffer/io_memory_stream.h" |
|---|
| 36 |
#include "common/io/zlib/zlibwrapper.h" |
|---|
| 37 |
|
|---|
| 38 |
////////////////////////////////////////////////////////////////////// |
|---|
| 39 |
|
|---|
| 40 |
DEFINE_int32(rand_seed, |
|---|
| 41 |
3274, |
|---|
| 42 |
"Seed the random number generator w/ this"); |
|---|
| 43 |
|
|---|
| 44 |
////////////////////////////////////////////////////////////////////// |
|---|
| 45 |
|
|---|
| 46 |
static unsigned int rand_seed; |
|---|
| 47 |
|
|---|
| 48 |
// original data |
|---|
| 49 |
uint8* g_data = NULL; |
|---|
| 50 |
uint32 g_data_len = 0; |
|---|
| 51 |
|
|---|
| 52 |
// zipped data |
|---|
| 53 |
uint8* g_zdata = NULL; |
|---|
| 54 |
uint32 g_zdata_len = 0; |
|---|
| 55 |
|
|---|
| 56 |
void TestZDecode(uint32 step) { |
|---|
| 57 |
CHECK_GT(step, 0); |
|---|
| 58 |
CHECK_GT(g_zdata_len, 0); |
|---|
| 59 |
CHECK_LE(step, g_zdata_len); |
|---|
| 60 |
|
|---|
| 61 |
io::MemoryStream in; |
|---|
| 62 |
io::MemoryStream out; |
|---|
| 63 |
io::ZlibGzipDecodeWrapper decoder; |
|---|
| 64 |
|
|---|
| 65 |
// decode data step by step |
|---|
| 66 |
// |
|---|
| 67 |
uint32 w = 0; |
|---|
| 68 |
for ( ; w < g_zdata_len - step; w += step ) { |
|---|
| 69 |
in.Write(g_zdata + w, step); |
|---|
| 70 |
const int result = decoder.Decode(&in, &out); |
|---|
| 71 |
CHECK_EQ(result, Z_OK) << " with step: " << step << " at index: " << w; |
|---|
| 72 |
}; |
|---|
| 73 |
in.Write(g_zdata + w, g_zdata_len - w); |
|---|
| 74 |
const int result = decoder.Decode(&in, &out); |
|---|
| 75 |
CHECK_EQ(result, Z_STREAM_END); |
|---|
| 76 |
|
|---|
| 77 |
// check decoded data correctness |
|---|
| 78 |
// |
|---|
| 79 |
CHECK_EQ(out.Size(), g_data_len); |
|---|
| 80 |
for ( uint32 i = 0; i < out.Size(); ) { |
|---|
| 81 |
uint8 tmp[128]; |
|---|
| 82 |
int32 read = out.Read(tmp, sizeof(tmp)); |
|---|
| 83 |
CHECK_EQ(0, ::memcmp(g_data + i, tmp, read)); |
|---|
| 84 |
i += read; |
|---|
| 85 |
} |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
int main(int argc, char** argv) { |
|---|
| 89 |
common::Init(argc, argv); |
|---|
| 90 |
|
|---|
| 91 |
rand_seed = FLAGS_rand_seed; |
|---|
| 92 |
srand(rand_seed); |
|---|
| 93 |
|
|---|
| 94 |
// create original data |
|---|
| 95 |
// |
|---|
| 96 |
g_data_len = 1 << 20; |
|---|
| 97 |
g_data = new uint8[g_data_len]; |
|---|
| 98 |
for ( uint32 i = 1; i < g_data_len; i++ ) { |
|---|
| 99 |
g_data[i] = (uint8)rand_r(&rand_seed); |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
LOG_INFO << "Original data size: " << g_data_len; |
|---|
| 103 |
|
|---|
| 104 |
// encode original data |
|---|
| 105 |
// |
|---|
| 106 |
{ |
|---|
| 107 |
io::MemoryStream in; |
|---|
| 108 |
const int32 write = in.Write(g_data, g_data_len); |
|---|
| 109 |
CHECK_EQ(write, g_data_len); |
|---|
| 110 |
|
|---|
| 111 |
io::MemoryStream out; |
|---|
| 112 |
io::ZlibGzipEncodeWrapper encoder; |
|---|
| 113 |
encoder.Encode(&in, &out); |
|---|
| 114 |
|
|---|
| 115 |
g_zdata_len = out.Size(); |
|---|
| 116 |
g_zdata = new uint8[g_zdata_len]; |
|---|
| 117 |
const int32 read = out.Read(g_zdata, g_zdata_len); |
|---|
| 118 |
CHECK_EQ(read, g_zdata_len); |
|---|
| 119 |
|
|---|
| 120 |
// test encoding was ok |
|---|
| 121 |
// |
|---|
| 122 |
{ |
|---|
| 123 |
io::ZlibGzipDecodeWrapper decoder; |
|---|
| 124 |
io::MemoryStream in; |
|---|
| 125 |
io::MemoryStream out; |
|---|
| 126 |
const int32 write = in.Write(g_zdata, g_zdata_len); |
|---|
| 127 |
CHECK_EQ(write, g_zdata_len); |
|---|
| 128 |
const int result = decoder.Decode(&in, &out); |
|---|
| 129 |
CHECK_EQ(result, Z_STREAM_END); |
|---|
| 130 |
for ( uint32 i = 0; i < out.Size(); ) { |
|---|
| 131 |
uint8 tmp[128]; |
|---|
| 132 |
const int32 read = out.Read(tmp, sizeof(tmp)); |
|---|
| 133 |
CHECK_EQ(0, ::memcmp(g_data + i, tmp, read)); |
|---|
| 134 |
i += read; |
|---|
| 135 |
} |
|---|
| 136 |
} |
|---|
| 137 |
} |
|---|
| 138 |
|
|---|
| 139 |
LOG_INFO << "Encoded data size: " << g_zdata_len; |
|---|
| 140 |
|
|---|
| 141 |
// Decode data in steps of 1, 2, 3 ... |
|---|
| 142 |
// |
|---|
| 143 |
for ( uint32 i = 1; i < 100; i++ ) { |
|---|
| 144 |
TestZDecode(i); |
|---|
| 145 |
cerr << "."; |
|---|
| 146 |
} |
|---|
| 147 |
for ( uint32 i = 100; i < 10000; i += 13 ) { |
|---|
| 148 |
TestZDecode(i); |
|---|
| 149 |
cerr << "."; |
|---|
| 150 |
} |
|---|
| 151 |
for ( uint32 i = 10000; i < g_zdata_len / 3; i += 169 ) { |
|---|
| 152 |
TestZDecode(i); |
|---|
| 153 |
cerr << "."; |
|---|
| 154 |
} |
|---|
| 155 |
cerr << endl; |
|---|
| 156 |
|
|---|
| 157 |
delete [] g_data; |
|---|
| 158 |
g_data = NULL; |
|---|
| 159 |
g_data_len = 0; |
|---|
| 160 |
|
|---|
| 161 |
delete [] g_zdata; |
|---|
| 162 |
g_zdata = NULL; |
|---|
| 163 |
g_zdata_len = 0; |
|---|
| 164 |
|
|---|
| 165 |
LOG_INFO << "Pass"; |
|---|
| 166 |
|
|---|
| 167 |
common::Exit(0); |
|---|
| 168 |
return 0; |
|---|
| 169 |
} |
|---|