<div dir="ltr"><div>Below is a little app that illustrates the issue (Can also be found here: <a href="http://pastebin.com/k8jRN64R">http://pastebin.com/k8jRN64R</a>) I&#39;ve removed most error handling/etc. for illustration purposes. The program expects a &quot;input.txt&quot; to be present in the same directory it&#39;s run from. </div>
<div><div><br></div><div>Note again that this works *most* of the time, but depending on the input file, will crash. Also I forgot to mention before that I&#39;m using Clang on OS X 8.4 and Boost v1.53.</div><div><br></div>
<div>Any help is greatly appreciated!</div><div><br></div><div><div>//<span class="" style="white-space:pre">	</span>Boost.Crypto</div><div>#include &lt;boost/crypto/rc4_cipher.hpp&gt;</div><div><br></div><div>//<span class="" style="white-space:pre">	</span>Boost</div>
<div>#include &lt;boost/filesystem/fstream.hpp&gt;</div><div>#include &lt;boost/iostreams/filter/zlib.hpp&gt;</div><div>#include &lt;boost/iostreams/copy.hpp&gt;</div><div>#include &lt;boost/iostreams/filtering_stream.hpp&gt;</div>
<div><br></div><div><br></div><div>template &lt;typename StreamCipherT&gt;</div><div>class StreamCipherFilterBase</div><div>{</div><div>public:</div><div><span class="" style="white-space:pre">	</span>typedef char<span class="" style="white-space:pre">		</span>char_type;</div>
<div><br></div><div><span class="" style="white-space:pre">	</span>StreamCipherFilterBase(const std::string&amp; key)</div><div><span class="" style="white-space:pre">	</span>{</div><div><span class="" style="white-space:pre">		</span>m_cipher.set_key(key.data(), static_cast&lt;unsigned int&gt;(key.length()));</div>
<div><span class="" style="white-space:pre">	</span>}</div><div>protected:</div><div>    char                m_buffer[1024 * 2]; //  2k internal buffer size</div><div>    StreamCipherT<span class="" style="white-space:pre">		</span>m_cipher;</div>
<div>};</div><div><br></div><div>template &lt;typename StreamCipherT&gt;</div><div>class StreamCipherEncryptFilter</div><div><span class="" style="white-space:pre">	</span>: public StreamCipherFilterBase&lt;StreamCipherT&gt;</div>
<div>{</div><div>public:</div><div><span class="" style="white-space:pre">	</span>typedef boost::iostreams::multichar_output_filter_tag<span class="" style="white-space:pre">	</span>category;</div><div><br></div><div><span class="" style="white-space:pre">	</span>StreamCipherEncryptFilter(const std::string&amp; key)</div>
<div><span class="" style="white-space:pre">		</span>: StreamCipherFilterBase&lt;StreamCipherT&gt;(key)</div><div><span class="" style="white-space:pre">	</span>{</div><div><span class="" style="white-space:pre">	</span>}</div>
<div><br></div><div><span class="" style="white-space:pre">	</span>template&lt;typename Sink&gt;</div><div><span class="" style="white-space:pre">	</span>std::streamsize write(Sink&amp; snk, const char* s, std::streamsize n)</div>
<div><span class="" style="white-space:pre">	</span>{</div><div><span class="" style="white-space:pre">		</span>std::streamsize written<span class="" style="white-space:pre">	</span>= 0;</div><div><span class="" style="white-space:pre">		</span>std::streamsize remain<span class="" style="white-space:pre">	</span>= n;</div>
<div><span class="" style="white-space:pre">		</span>std::streamsize eat;</div><div><span class="" style="white-space:pre">		</span>while(remain) {</div><div><span class="" style="white-space:pre">			</span>eat = remain &lt; sizeof(StreamCipherFilterBase&lt;StreamCipherT&gt;::m_buffer) ?</div>
<div>                remain : sizeof(StreamCipherFilterBase&lt;StreamCipherT&gt;::m_buffer);</div><div><span class="" style="white-space:pre">			</span>StreamCipherFilterBase&lt;StreamCipherT&gt;::m_cipher.encrypt(</div><div>
<span class="" style="white-space:pre">				</span>s,</div><div><span class="" style="white-space:pre">				</span>StreamCipherFilterBase&lt;StreamCipherT&gt;::m_buffer,</div><div><span class="" style="white-space:pre">				</span>eat);</div>
<div><span class="" style="white-space:pre">			</span>written += boost::iostreams::write(snk, StreamCipherFilterBase&lt;StreamCipherT&gt;::m_buffer, eat);</div><div><span class="" style="white-space:pre">			</span>remain -= eat;</div>
<div><span class="" style="white-space:pre">			</span>s += eat;</div><div><span class="" style="white-space:pre">		</span>}</div><div><span class="" style="white-space:pre">		</span>return written;</div><div><span class="" style="white-space:pre">	</span>}</div>
<div>};</div><div><br></div><div>template &lt;typename StreamCipherT&gt;</div><div>class StreamCipherDecryptFilter</div><div><span class="" style="white-space:pre">	</span>: public StreamCipherFilterBase&lt;StreamCipherT&gt;</div>
<div>{</div><div>public:</div><div><span class="" style="white-space:pre">	</span>typedef boost::iostreams::multichar_input_filter_tag<span class="" style="white-space:pre">	</span>category;</div><div><br></div><div><span class="" style="white-space:pre">	</span>StreamCipherDecryptFilter(const std::string&amp; key)</div>
<div><span class="" style="white-space:pre">		</span>: StreamCipherFilterBase&lt;StreamCipherT&gt;(key)</div><div><span class="" style="white-space:pre">	</span>{</div><div><span class="" style="white-space:pre">	</span>}</div>
<div><br></div><div><span class="" style="white-space:pre">	</span>template&lt;typename Source&gt;</div><div><span class="" style="white-space:pre">	</span>std::streamsize read(Source&amp; src, char* s, std::streamsize n)</div>
<div><span class="" style="white-space:pre">	</span>{</div><div><span class="" style="white-space:pre">		</span>const std::streamsize read = boost::iostreams::read(src, s, n);</div><div>        if(EOF == read) {</div><div>
            return EOF;</div><div>        }</div><div><span class="" style="white-space:pre">		</span>StreamCipherFilterBase&lt;StreamCipherT&gt;::m_cipher.decrypt(s, s, read);</div><div><span class="" style="white-space:pre">		</span>return read;</div>
<div><span class="" style="white-space:pre">	</span>}</div><div>};</div><div><br></div><div>const std::string KEY = &quot;Kitteh&quot;;</div><div>typedef boost::crypto::rc4_cipher<span class="" style="white-space:pre">	</span>CipherT;</div>
<div><br></div><div>void Encrypt(</div><div><span class="" style="white-space:pre">	</span>const std::string&amp; in, const std::string&amp; out)</div><div>{</div><div><span class="" style="white-space:pre">	</span>//<span class="" style="white-space:pre">	</span>error handling omitted</div>
<div><span class="" style="white-space:pre">	</span>boost::filesystem::ifstream inf(in, std::ios_base::binary | std::ios_base::in);</div><div><span class="" style="white-space:pre">	</span>boost::filesystem::ofstream outf(out, std::ios_base::binary | std::ios_base::out);</div>
<div><br></div><div><span class="" style="white-space:pre">	</span>StreamCipherEncryptFilter&lt;CipherT&gt; outCipherFilter(KEY);</div><div><span class="" style="white-space:pre">	</span>boost::iostreams::filtering_ostreambuf outs;</div>
<div><span class="" style="white-space:pre">	</span>outs.push(boost::iostreams::zlib_compressor());<span class="" style="white-space:pre">	</span>//<span class="" style="white-space:pre">	</span>compress</div><div>    outs.push(outCipherFilter);<span class="" style="white-space:pre">						</span>//<span class="" style="white-space:pre">	</span>then encrypt</div>
<div><span class="" style="white-space:pre">	</span>outs.push(outf);<span class="" style="white-space:pre">								</span>//<span class="" style="white-space:pre">	</span>write to file</div><div><br></div><div><span class="" style="white-space:pre">	</span>boost::iostreams::copy(inf, outs);</div>
<div>}</div><div><br></div><div>void Decrypt(</div><div><span class="" style="white-space:pre">	</span>const std::string&amp; in, const std::string&amp; out)</div><div>{</div><div><span class="" style="white-space:pre">	</span>//<span class="" style="white-space:pre">	</span>error handling omitted</div>
<div><span class="" style="white-space:pre">	</span>boost::filesystem::ifstream inf(in, std::ios_base::binary | std::ios_base::in);</div><div><span class="" style="white-space:pre">	</span>boost::filesystem::ofstream outf(out, std::ios_base::binary | std::ios_base::out);</div>
<div><br></div><div><span class="" style="white-space:pre">	</span>StreamCipherDecryptFilter&lt;CipherT&gt; inCipherFilter(KEY);</div><div><span class="" style="white-space:pre">	</span>boost::iostreams::filtering_istreambuf ins;</div>
<div><span class="" style="white-space:pre">	</span>ins.push(boost::iostreams::zlib_decompressor());</div><div><span class="" style="white-space:pre">	</span>ins.push(inCipherFilter);</div><div><span class="" style="white-space:pre">	</span>ins.push(inf);</div>
<div><br></div><div><span class="" style="white-space:pre">	</span>boost::iostreams::copy(ins, outf);<span class="" style="white-space:pre">	</span>//<span class="" style="white-space:pre">	</span>&lt;-- exception thrown here with some files</div>
<div>}</div><div><br></div><div>int main()</div><div>{</div><div><span class="" style="white-space:pre">	</span>//<span class="" style="white-space:pre">	</span>compress+encrypt input.txt -&gt; output.encrypt</div><div><span class="" style="white-space:pre">	</span>Encrypt(&quot;input.txt&quot;, &quot;output.encrypt&quot;);</div>
<div><br></div><div><span class="" style="white-space:pre">	</span>//<span class="" style="white-space:pre">	</span>decrypt+decompress output.encrypt -&gt; input.read.txt</div><div><span class="" style="white-space:pre">	</span>Decrypt(&quot;output.encrypt&quot;, &quot;input.read.txt&quot;);</div>
<div>}</div></div><div><div class="gmail_extra"><br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

------------------------------<br>
<br>
Message: 5<br>
Date: Thu, 08 Aug 2013 17:52:50 +0100<br>
From: Ken Appleby &lt;<a href="mailto:ken.appleby@btopenworld.com">ken.appleby@btopenworld.com</a>&gt;<br>
To: <a href="mailto:boost-users@lists.boost.org">boost-users@lists.boost.org</a><br>
Subject: Re: [Boost-users] Iostreams with two filters throws exception<br>
Message-ID: &lt;<a href="mailto:5203CCE2.4060200@btopenworld.com">5203CCE2.4060200@btopenworld.com</a>&gt;<br>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed<br>
<br><br>
You are a lot more likely to get a response if you post a complete<br>
example. Just a single file with a main() would make it a lot easier to<br>
help you. You&#39;re not far from that with the code you posted.<br>
<br></blockquote></div></div></div></div></div>