From 335db15909e7f6952c54dcd18fda9532a12938bc Mon Sep 17 00:00:00 2001 From: Vladimir Stackov Date: Fri, 31 Jul 2015 10:52:11 +0300 Subject: [PATCH] Resolved #58 --- bundle.cc | 27 ++++++++++++++++----------- bundle.hh | 4 ++-- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/bundle.cc b/bundle.cc index b944d83..e7e3a51 100644 --- a/bundle.cc +++ b/bundle.cc @@ -54,7 +54,7 @@ void Creator::write( std::string const & fileName, EncryptionKey const & key, for ( ; ; ) { - bool readCurr = reader.is.Next( &bufCurr, &sizeCurr ); + bool readCurr = reader.is->Next( &bufCurr, &sizeCurr ); if ( readCurr ) { @@ -88,6 +88,9 @@ void Creator::write( std::string const & fileName, EncryptionKey const & key, } } } + + if ( reader.is.get() ) + reader.is.reset(); } void Creator::write( Config const & config, std::string const & fileName, @@ -151,18 +154,18 @@ void Creator::write( Config const & config, std::string const & fileName, os.writeAdler32(); } -Reader::Reader( string const & fileName, EncryptionKey const & key, bool prohibitProcessing ): - is( fileName.c_str(), key, Encryption::ZeroIv ) +Reader::Reader( string const & fileName, EncryptionKey const & key, bool keepStream ) { - is.consumeRandomIv(); + is = new EncryptedFile::InputStream( fileName.c_str(), key, Encryption::ZeroIv ); + is->consumeRandomIv(); - Message::parse( header, is ); + Message::parse( header, *is ); if ( header.version() >= FileFormatVersionFirstUnsupported ) throw exUnsupportedVersion(); - Message::parse( info, is ); - is.checkAdler32(); + Message::parse( info, *is ); + is->checkAdler32(); size_t payloadSize = 0; for ( int x = info.chunk_record_size(); x--; ) @@ -170,7 +173,7 @@ Reader::Reader( string const & fileName, EncryptionKey const & key, bool prohibi payload.resize( payloadSize ); - if ( prohibitProcessing ) + if ( keepStream ) return; sptr decoder = Compression::CompressionMethod::findCompression( @@ -183,7 +186,7 @@ Reader::Reader( string const & fileName, EncryptionKey const & key, bool prohibi { void const * data; int size; - if ( !is.Next( &data, &size ) ) + if ( !is->Next( &data, &size ) ) { decoder.reset(); throw exBundleReadFailed(); @@ -196,7 +199,7 @@ Reader::Reader( string const & fileName, EncryptionKey const & key, bool prohibi if ( decoder->process( false ) ) { if ( decoder->getAvailableInput() ) - is.BackUp( decoder->getAvailableInput() ); + is->BackUp( decoder->getAvailableInput() ); break; } @@ -210,7 +213,9 @@ Reader::Reader( string const & fileName, EncryptionKey const & key, bool prohibi decoder.reset(); - is.checkAdler32(); + is->checkAdler32(); + if ( is.get() ) + is.reset(); // Populate the map char const * next = payload.data(); diff --git a/bundle.hh b/bundle.hh index c03b968..709a126 100644 --- a/bundle.hh +++ b/bundle.hh @@ -65,7 +65,7 @@ public: DEF_EX( exDuplicateChunks, "Chunks with the same id found in a bundle", Ex ) Reader( string const & fileName, EncryptionKey const & key, - bool prohibitProcessing = false ); + bool keepStream = false ); /// Reads the chunk into chunkData and returns true, or returns false if there /// was no such chunk in the bundle. chunkData may be enlarged but won't @@ -78,7 +78,7 @@ public: string getPayload() { return payload; } - EncryptedFile::InputStream is; + sptr< EncryptedFile::InputStream > is; }; /// Creates a bundle by adding chunks to it until it's full, then compressing