zlibstream.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/streams/zlibstream.cpp
  3. // Purpose: Test wxZlibInputStream/wxZlibOutputStream
  4. // Author: Hans Van Leemputten
  5. // Copyright: (c) 2004 Hans Van Leemputten
  6. // Licence: wxWindows licence
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // For compilers that support precompilation, includes "wx/wx.h".
  9. // and "wx/cppunit.h"
  10. #include "testprec.h"
  11. #ifdef __BORLANDC__
  12. #pragma hdrstop
  13. #endif
  14. // for all others, include the necessary headers
  15. #ifndef WX_PRECOMP
  16. #include "wx/wx.h"
  17. #endif
  18. #include "wx/zstream.h"
  19. #include "wx/wfstream.h"
  20. #include "wx/mstream.h"
  21. #include "wx/txtstrm.h"
  22. #include "wx/buffer.h"
  23. #include "bstream.h"
  24. using std::string;
  25. #define WXTEST_WITH_GZIP_CONDITION(testMethod) \
  26. WXTEST_WITH_CONDITION( COMPOSE_TEST_NAME(zlibStream), wxZlibInputStream::CanHandleGZip() && wxZlibOutputStream::CanHandleGZip(), testMethod )
  27. #define DATABUFFER_SIZE 1024
  28. static const wxString FILENAME_GZ = wxT("zlibtest.gz");
  29. ///////////////////////////////////////////////////////////////////////////////
  30. // The test case
  31. //
  32. // Try to fully test wxZlibInputStream and wxZlibOutputStream
  33. class zlibStream : public BaseStreamTestCase<wxZlibInputStream, wxZlibOutputStream>
  34. {
  35. public:
  36. zlibStream();
  37. virtual ~zlibStream();
  38. CPPUNIT_TEST_SUITE(zlibStream);
  39. // Base class stream tests the zlibstream supports.
  40. CPPUNIT_TEST_FAIL(Input_GetSize);
  41. CPPUNIT_TEST(Input_GetC);
  42. CPPUNIT_TEST(Input_Read);
  43. CPPUNIT_TEST(Input_Eof);
  44. CPPUNIT_TEST(Input_LastRead);
  45. CPPUNIT_TEST(Input_CanRead);
  46. CPPUNIT_TEST_FAIL(Input_SeekI);
  47. CPPUNIT_TEST(Input_TellI);
  48. CPPUNIT_TEST(Input_Peek);
  49. CPPUNIT_TEST(Input_Ungetch);
  50. CPPUNIT_TEST(Output_PutC);
  51. CPPUNIT_TEST(Output_Write);
  52. CPPUNIT_TEST(Output_LastWrite);
  53. CPPUNIT_TEST_FAIL(Output_SeekO);
  54. CPPUNIT_TEST(Output_TellO);
  55. // Other test specific for zlib stream test case.
  56. CPPUNIT_TEST(TestStream_NoHeader_Default);
  57. CPPUNIT_TEST(TestStream_NoHeader_NoComp);
  58. CPPUNIT_TEST(TestStream_NoHeader_SpeedComp);
  59. CPPUNIT_TEST(TestStream_NoHeader_BestComp);
  60. CPPUNIT_TEST(TestStream_NoHeader_Dictionary);
  61. CPPUNIT_TEST(TestStream_ZLib_Default);
  62. CPPUNIT_TEST(TestStream_ZLib_NoComp);
  63. CPPUNIT_TEST(TestStream_ZLib_SpeedComp);
  64. CPPUNIT_TEST(TestStream_ZLib_BestComp);
  65. WXTEST_WITH_GZIP_CONDITION(TestStream_GZip_Default);
  66. WXTEST_WITH_GZIP_CONDITION(TestStream_GZip_NoComp);
  67. WXTEST_WITH_GZIP_CONDITION(TestStream_GZip_SpeedComp);
  68. WXTEST_WITH_GZIP_CONDITION(TestStream_GZip_BestComp);
  69. WXTEST_WITH_GZIP_CONDITION(TestStream_GZip_Dictionary);
  70. WXTEST_WITH_GZIP_CONDITION(TestStream_ZLibGZip);
  71. CPPUNIT_TEST(Decompress_BadData);
  72. CPPUNIT_TEST(Decompress_wx251_zlib114_Data_NoHeader);
  73. CPPUNIT_TEST(Decompress_wx251_zlib114_Data_ZLib);
  74. WXTEST_WITH_GZIP_CONDITION(Decompress_gzip135Data);
  75. CPPUNIT_TEST_SUITE_END();
  76. protected:
  77. // Test different stream construct settings.
  78. void TestStream_NoHeader_Default();
  79. void TestStream_NoHeader_NoComp();
  80. void TestStream_NoHeader_SpeedComp();
  81. void TestStream_NoHeader_BestComp();
  82. void TestStream_NoHeader_Dictionary();
  83. void TestStream_ZLib_Default();
  84. void TestStream_ZLib_NoComp();
  85. void TestStream_ZLib_SpeedComp();
  86. void TestStream_ZLib_BestComp();
  87. void TestStream_GZip_Default();
  88. void TestStream_GZip_NoComp();
  89. void TestStream_GZip_SpeedComp();
  90. void TestStream_GZip_BestComp();
  91. void TestStream_GZip_Dictionary();
  92. void TestStream_ZLibGZip();
  93. // Try to decompress bad data.
  94. void Decompress_BadData();
  95. // Decompress data that was compress by an external app.
  96. // (like test wx 2.4.2, 2.5.1 and gzip data)
  97. // Note: This test is limited in testing range!
  98. void Decompress_wx251_zlib114_Data_NoHeader();
  99. void Decompress_wx251_zlib114_Data_ZLib();
  100. void Decompress_gzip135Data();
  101. private:
  102. const char *GetDataBuffer();
  103. const unsigned char *GetCompressedData();
  104. void doTestStreamData(int input_flag, int output_flag, int compress_level, const wxMemoryBuffer *buf = NULL);
  105. void doDecompress_ExternalData(const unsigned char *data, const char *value, size_t data_size, size_t value_size, int flag = wxZLIB_AUTO);
  106. private:
  107. // Implement base class functions.
  108. virtual wxZlibInputStream *DoCreateInStream();
  109. virtual wxZlibOutputStream *DoCreateOutStream();
  110. virtual void DoDeleteInStream();
  111. virtual void DoDeleteOutStream();
  112. // Helper that can be used to create new wx compatibility tests...
  113. // Otherwise not used by the tests.
  114. void genExtTestData(wxTextOutputStream &out, const char *buf, int flag);
  115. private:
  116. char m_DataBuffer[DATABUFFER_SIZE];
  117. size_t m_SizeCompressedData;
  118. unsigned char *m_pCompressedData;
  119. wxMemoryBuffer m_Dictionary;
  120. // Used by the base Creat[In|Out]Stream and Delete[In|Out]Stream.
  121. wxMemoryInputStream *m_pTmpMemInStream;
  122. wxMemoryOutputStream *m_pTmpMemOutStream;
  123. };
  124. zlibStream::zlibStream()
  125. :m_SizeCompressedData(0),
  126. m_pCompressedData(NULL),
  127. m_pTmpMemInStream(NULL),
  128. m_pTmpMemOutStream(NULL)
  129. {
  130. // Init the data buffer.
  131. for (size_t i = 0; i < DATABUFFER_SIZE; i++)
  132. m_DataBuffer[i] = (i % 0xFF);
  133. m_Dictionary.AppendData(m_DataBuffer, sizeof(m_DataBuffer) / 2);
  134. // Set extra base config settings.
  135. m_bSimpleTellITest = true;
  136. m_bSimpleTellOTest = true;
  137. /* Example code on how to produce test data...
  138. {
  139. wxFFileOutputStream fstream_out(wxT("gentest.cpp"));
  140. wxTextOutputStream out( fstream_out );
  141. genExtTestData(out, "zlib data created with wxWidgets 2.5.x [March 27], wxZLIB_NO_HEADER, zlib 1.1.4", wxZLIB_NO_HEADER);
  142. genExtTestData(out, "zlib data created with wxWidgets 2.5.x [March 27], wxZLIB_ZLIB, zlib 1.1.4", wxZLIB_ZLIB);
  143. }
  144. */
  145. }
  146. zlibStream::~zlibStream()
  147. {
  148. delete[] m_pCompressedData;
  149. delete m_pTmpMemInStream;
  150. delete m_pTmpMemOutStream;
  151. }
  152. void zlibStream::TestStream_NoHeader_Default()
  153. {
  154. doTestStreamData(wxZLIB_NO_HEADER, wxZLIB_NO_HEADER, wxZ_DEFAULT_COMPRESSION);
  155. }
  156. void zlibStream::TestStream_NoHeader_NoComp()
  157. {
  158. doTestStreamData(wxZLIB_NO_HEADER, wxZLIB_NO_HEADER, wxZ_NO_COMPRESSION);
  159. }
  160. void zlibStream::TestStream_NoHeader_SpeedComp()
  161. {
  162. doTestStreamData(wxZLIB_NO_HEADER, wxZLIB_NO_HEADER, wxZ_BEST_SPEED);
  163. }
  164. void zlibStream::TestStream_NoHeader_BestComp()
  165. {
  166. doTestStreamData(wxZLIB_NO_HEADER, wxZLIB_NO_HEADER, wxZ_BEST_COMPRESSION);
  167. }
  168. void zlibStream::TestStream_NoHeader_Dictionary()
  169. {
  170. doTestStreamData(wxZLIB_NO_HEADER, wxZLIB_NO_HEADER, wxZ_DEFAULT_COMPRESSION, &m_Dictionary);
  171. }
  172. void zlibStream::TestStream_ZLib_Default()
  173. {
  174. doTestStreamData(wxZLIB_ZLIB, wxZLIB_ZLIB, wxZ_DEFAULT_COMPRESSION);
  175. }
  176. void zlibStream::TestStream_ZLib_NoComp()
  177. {
  178. doTestStreamData(wxZLIB_ZLIB, wxZLIB_ZLIB, wxZ_NO_COMPRESSION);
  179. }
  180. void zlibStream::TestStream_ZLib_SpeedComp()
  181. {
  182. doTestStreamData(wxZLIB_ZLIB, wxZLIB_ZLIB, wxZ_BEST_SPEED);
  183. }
  184. void zlibStream::TestStream_ZLib_BestComp()
  185. {
  186. doTestStreamData(wxZLIB_ZLIB, wxZLIB_ZLIB, wxZ_BEST_COMPRESSION);
  187. }
  188. void zlibStream::TestStream_GZip_Default()
  189. {
  190. doTestStreamData(wxZLIB_GZIP, wxZLIB_GZIP, wxZ_DEFAULT_COMPRESSION);
  191. }
  192. void zlibStream::TestStream_GZip_NoComp()
  193. {
  194. doTestStreamData(wxZLIB_GZIP, wxZLIB_GZIP, wxZ_NO_COMPRESSION);
  195. }
  196. void zlibStream::TestStream_GZip_SpeedComp()
  197. {
  198. doTestStreamData(wxZLIB_GZIP, wxZLIB_GZIP, wxZ_BEST_SPEED);
  199. }
  200. void zlibStream::TestStream_GZip_BestComp()
  201. {
  202. doTestStreamData(wxZLIB_GZIP, wxZLIB_GZIP, wxZ_BEST_COMPRESSION);
  203. }
  204. void zlibStream::TestStream_GZip_Dictionary()
  205. {
  206. doTestStreamData(wxZLIB_GZIP, wxZLIB_GZIP, wxZ_DEFAULT_COMPRESSION, &m_Dictionary);
  207. }
  208. void zlibStream::TestStream_ZLibGZip()
  209. {
  210. // Only use default compression level, as this test is
  211. // for testing if the streams can determine the stream type info them self...
  212. doTestStreamData(wxZLIB_AUTO, wxZLIB_ZLIB, wxZ_DEFAULT_COMPRESSION);
  213. doTestStreamData(wxZLIB_AUTO, wxZLIB_GZIP, wxZ_DEFAULT_COMPRESSION);
  214. }
  215. void zlibStream::Decompress_BadData()
  216. {
  217. // Setup the bad data stream and the zlib stream.
  218. wxMemoryInputStream memstream_in(GetDataBuffer(), DATABUFFER_SIZE);
  219. CPPUNIT_ASSERT(memstream_in.IsOk());
  220. wxZlibInputStream zstream_in(memstream_in);
  221. CPPUNIT_ASSERT(zstream_in.IsOk()); // We did not yet read from the stream
  222. // so it should still be OK.
  223. // Try to force the stream to go to bad status.
  224. CPPUNIT_ASSERT(!zstream_in.Eof());
  225. if (zstream_in.IsOk())
  226. zstream_in.GetC();
  227. // Because of the bad data in the input stream the zlib
  228. // stream should be marked as NOT OK.
  229. CPPUNIT_ASSERT(!zstream_in.IsOk());
  230. }
  231. void zlibStream::Decompress_wx251_zlib114_Data_NoHeader()
  232. {
  233. const unsigned char data[] = {171,202,201,76,82,72,73,44,73,84,72,46,74,77,44,73,77,81,40,207,44,201,80,40,175,8,207,76,73,79,45,41,86,48,210,51,213,171,80,136,246,77,44,74,206,80,48,50,143,213,1,202,69,249,120,58,197,251,249,199,123,184,58,186,184,6,233,40,84,129,12,49,212,51,212,51,1,0,32};
  234. const char *value = "zlib data created with wxWidgets 2.5.x [March 27], wxZLIB_NO_HEADER, zlib 1.1.4";
  235. const size_t data_size = sizeof(data);
  236. const size_t value_size = strlen(value);
  237. // We need to specify wxZLIB_NO_HEADER because wxZLIB_AUTO can't find it his self.
  238. doDecompress_ExternalData(data, value, data_size, value_size, wxZLIB_NO_HEADER);
  239. }
  240. void zlibStream::Decompress_wx251_zlib114_Data_ZLib()
  241. {
  242. const unsigned char data[] = {120,156,171,202,201,76,82,72,73,44,73,84,72,46,74,77,44,73,77,81,40,207,44,201,80,40,175,8,207,76,73,79,45,41,86,48,210,51,213,171,80,136,246,77,44,74,206,80,48,50,143,213,1,202,69,249,120,58,197,131,8,29,133,42,144,126,67,61,67,61,19,0,191,86,23,216};
  243. const char *value = "zlib data created with wxWidgets 2.5.x [March 27], wxZLIB_ZLIB, zlib 1.1.4";
  244. const size_t data_size = sizeof(data);
  245. const size_t value_size = strlen(value);
  246. doDecompress_ExternalData(data, value, data_size, value_size);
  247. }
  248. void zlibStream::Decompress_gzip135Data()
  249. {
  250. // Compressed data was on the command line with gzip 1.3.5.
  251. const unsigned char gzip135_data[] = {31,139,8,0,177,248,112,64,4,3,115,206,207,45,40,74,45,46,78,77,81,72,73,44,73,84,72,46,74,77,44,1,114,202,51,75,50,20,220,253,66,21,210,171,50,11,20,12,245,140,245,76,185,0,1,107,16,80,44,0,0,0,0};
  252. const char *gzip135_value = "Compressed data created with GNU gzip 1.3.5\n";
  253. // Size of the value and date items.
  254. const size_t data_size = sizeof(gzip135_data);
  255. const size_t value_size = strlen(gzip135_value);
  256. // Perform a generic data test on the data.
  257. doDecompress_ExternalData(gzip135_data, gzip135_value, data_size, value_size);
  258. }
  259. const char *zlibStream::GetDataBuffer()
  260. {
  261. return m_DataBuffer;
  262. }
  263. const unsigned char *zlibStream::GetCompressedData()
  264. {
  265. if (!m_pCompressedData)
  266. {
  267. // Construct the compressed data live.
  268. wxMemoryOutputStream memstream_out;
  269. {
  270. const char *buf = "01234567890123456789012345678901234567890123456789"; /* = 50 */
  271. wxZlibOutputStream zstream_out(memstream_out);
  272. zstream_out.Write(buf, strlen(buf));
  273. }
  274. // Copy the to the
  275. m_SizeCompressedData = memstream_out.GetSize();
  276. m_pCompressedData = new unsigned char[m_SizeCompressedData];
  277. memstream_out.CopyTo(m_pCompressedData, m_SizeCompressedData);
  278. }
  279. CPPUNIT_ASSERT(m_pCompressedData != NULL);
  280. return m_pCompressedData;
  281. }
  282. void zlibStream::doTestStreamData(int input_flag, int output_flag, int compress_level, const wxMemoryBuffer *buf)
  283. {
  284. size_t fail_pos;
  285. char last_value = 0;
  286. bool bWasEOF;
  287. { // Part one: Create a compressed file.
  288. wxFileOutputStream fstream_out(FILENAME_GZ);
  289. CPPUNIT_ASSERT(fstream_out.IsOk());
  290. {
  291. wxZlibOutputStream zstream_out(fstream_out, compress_level, output_flag);
  292. CPPUNIT_ASSERT_MESSAGE("Could not create the output stream", zstream_out.IsOk());
  293. if (buf)
  294. zstream_out.SetDictionary(*buf);
  295. // Next: Compress some data so the file is containing something.
  296. zstream_out.Write(GetDataBuffer(), DATABUFFER_SIZE);
  297. }
  298. // Next thing is required by zlib versions pre 1.2.0.
  299. if (input_flag == wxZLIB_NO_HEADER)
  300. fstream_out.PutC(' ');
  301. }
  302. { // Part two: Verify that the compressed data when uncompressed
  303. // matches the original data.
  304. wxFileInputStream fstream_in(FILENAME_GZ);
  305. CPPUNIT_ASSERT(fstream_in.IsOk());
  306. wxZlibInputStream zstream_in(fstream_in, input_flag);
  307. CPPUNIT_ASSERT_MESSAGE("Could not create the input stream", zstream_in.IsOk());
  308. if (buf)
  309. zstream_in.SetDictionary(*buf);
  310. // Next: Check char per char if the returned data is valid.
  311. const char *pbuf = GetDataBuffer();
  312. for (fail_pos = 0; !zstream_in.Eof(); fail_pos++)
  313. {
  314. last_value = zstream_in.GetC();
  315. if (zstream_in.LastRead() != 1 ||
  316. last_value != pbuf[fail_pos])
  317. break;
  318. }
  319. bWasEOF = zstream_in.Eof();
  320. }
  321. // Remove the temp file...
  322. ::wxRemoveFile(FILENAME_GZ);
  323. // Check state of the verify action.
  324. if (fail_pos != DATABUFFER_SIZE || !bWasEOF)
  325. {
  326. wxString msg;
  327. msg << wxT("Wrong data item at pos ") << fail_pos
  328. << wxT(" (Org_val ") << GetDataBuffer()[fail_pos]
  329. << wxT(" != Zlib_val ") << last_value
  330. << wxT("), with compression level ") << compress_level;
  331. CPPUNIT_FAIL(string(msg.mb_str()));
  332. }
  333. }
  334. void zlibStream::doDecompress_ExternalData(const unsigned char *data, const char *value, size_t data_size, size_t value_size, int flag)
  335. {
  336. // See that the input is ok.
  337. wxASSERT(data != NULL);
  338. wxASSERT(value != NULL);
  339. wxASSERT(data_size > 0);
  340. wxASSERT(value_size > 0);
  341. // Quickly try to see if the data is valid.
  342. switch (flag)
  343. {
  344. case wxZLIB_NO_HEADER:
  345. break;
  346. case wxZLIB_ZLIB:
  347. if (!(data_size >= 1 && data[0] == 0x78))
  348. {
  349. wxLogError(wxT("zlib data seems to not be zlib data!"));
  350. }
  351. break;
  352. case wxZLIB_GZIP:
  353. if (!(data_size >= 2 && data[0] == 0x1F && data[1] == 0x8B))
  354. {
  355. wxLogError(wxT("gzip data seems to not be gzip data!"));
  356. }
  357. break;
  358. case wxZLIB_AUTO:
  359. if (!(data_size >= 1 && data[0] == 0x78) ||
  360. !(data_size >= 2 && data[0] == 0x1F && data[1] == 0x8B))
  361. {
  362. wxLogError(wxT("Data seems to not be zlib or gzip data!"));
  363. }
  364. default:
  365. wxLogError(wxT("Unknown flag, skipping quick test."));
  366. };
  367. // Creat the needed streams.
  368. wxMemoryInputStream memstream_in(data, data_size);
  369. CPPUNIT_ASSERT(memstream_in.IsOk());
  370. wxZlibInputStream zstream_in(memstream_in, flag);
  371. CPPUNIT_ASSERT(zstream_in.IsOk());
  372. bool bValueEq = true;
  373. size_t i;
  374. for (i = 0; !zstream_in.Eof(); i++)
  375. {
  376. char last_value = zstream_in.GetC();
  377. // First check if it is a valid read.
  378. if (zstream_in.LastRead() == 1)
  379. {
  380. // Check the values
  381. if (last_value != value[i])
  382. {
  383. bValueEq = false;
  384. break;
  385. }
  386. }
  387. else
  388. {
  389. // If the read failed and turned the stream to Eof we stop reading.
  390. if (zstream_in.Eof())
  391. break;
  392. CPPUNIT_ASSERT_MESSAGE("Stream is no longer ok!", zstream_in.IsOk());
  393. }
  394. // Don't go over the end of the value buffer...
  395. if (i == value_size)
  396. {
  397. // And if we do then try to see how long the stream actually is.
  398. while (!zstream_in.Eof())
  399. {
  400. // Move one item along in the stream.
  401. (void)zstream_in.GetC();
  402. i++;
  403. // Check if we are in an infinite loop by multiplying value_size
  404. // by 5 to have a *much* bigger range then the real range.
  405. // Note: In case you ask yourself, why 5, the answer is no reason...
  406. // it is not too big and not to small a size, nothing more
  407. // nothing less to it.
  408. if (i > (value_size*5))
  409. {
  410. // Note: Please make sure Input_Eof test passed.
  411. CPPUNIT_FAIL("Infinite stream detected, breaking the infinite loop");
  412. return;
  413. }
  414. }
  415. }
  416. }
  417. CPPUNIT_ASSERT_MESSAGE("Could not decompress the compressed data, original and restored value did not match.",
  418. i == value_size && bValueEq);
  419. }
  420. wxZlibInputStream *zlibStream::DoCreateInStream()
  421. {
  422. const unsigned char *buf = GetCompressedData();
  423. m_pTmpMemInStream = new wxMemoryInputStream(buf, m_SizeCompressedData);
  424. CPPUNIT_ASSERT(m_pTmpMemInStream->IsOk());
  425. wxZlibInputStream *pzstream_in = new wxZlibInputStream(*m_pTmpMemInStream);
  426. CPPUNIT_ASSERT(pzstream_in->IsOk());
  427. return pzstream_in;
  428. }
  429. wxZlibOutputStream *zlibStream::DoCreateOutStream()
  430. {
  431. m_pTmpMemOutStream = new wxMemoryOutputStream();
  432. CPPUNIT_ASSERT(m_pTmpMemOutStream->IsOk());
  433. wxZlibOutputStream *pzstream_out = new wxZlibOutputStream(*m_pTmpMemOutStream);
  434. CPPUNIT_ASSERT(pzstream_out->IsOk());
  435. return pzstream_out;
  436. }
  437. void zlibStream::DoDeleteInStream()
  438. {
  439. delete m_pTmpMemInStream;
  440. m_pTmpMemInStream = NULL;
  441. }
  442. void zlibStream::DoDeleteOutStream()
  443. {
  444. delete m_pTmpMemOutStream;
  445. m_pTmpMemOutStream = NULL;
  446. }
  447. void zlibStream::genExtTestData(wxTextOutputStream &out, const char *buf, int flag)
  448. {
  449. unsigned char *data;
  450. size_t size;
  451. { // Gen data
  452. wxMemoryOutputStream memstream_out;
  453. {
  454. wxZlibOutputStream zstream_out(memstream_out, wxZ_DEFAULT_COMPRESSION, flag);
  455. zstream_out.Write(buf, strlen(buf));
  456. }
  457. if (flag == wxZLIB_NO_HEADER)
  458. memstream_out.PutC(' ');
  459. size = memstream_out.GetSize();
  460. data = new unsigned char[size];
  461. memstream_out.CopyTo(data, size);
  462. }
  463. out << wxT("void zlibStream::Decompress_wxXXXData()") << wxT("\n");
  464. out << wxT("{") << wxT("\n") << wxT(" const unsigned char data[] = {");
  465. size_t i;
  466. for (i = 0; i < size; i++)
  467. {
  468. if (i+1 != size)
  469. out << wxString::Format(wxT("%d,"), data[i]);
  470. else
  471. out << wxString::Format(wxT("%d"), data[i]);
  472. }
  473. delete [] data;
  474. out << wxT("};") << wxT("\n");
  475. out << wxT(" const char *value = \"") << wxString(buf, wxConvUTF8) << wxT("\";") << wxT("\n");
  476. out << wxT(" const size_t data_size = sizeof(data);") << wxT("\n");
  477. out << wxT(" const size_t value_size = strlen(value);") << wxT("\n");
  478. out << wxT(" doDecompress_ExternalData(data, value, data_size, value_size);") << wxT("\n");
  479. out << wxT("}") << wxT("\n");
  480. }
  481. // Register the stream sub suite, by using some stream helper macro.
  482. // Note: Don't forget to connect it to the base suite (See: bstream.cpp => StreamCase::suite())
  483. STREAM_TEST_SUBSUITE_NAMED_REGISTRATION(zlibStream)