added Pcpstream example

This commit is contained in:
TLINDEN
2014-02-20 21:02:52 +01:00
parent 254c4cd39b
commit 30481fed9a
59 changed files with 253 additions and 105 deletions

View File

@@ -100,8 +100,81 @@ Functions</h2></td></tr>
<p>I/O wrapper for files or buffers. </p>
<p>Simple wrapper around FILE streams or Buffers, depending how the user initialized them. The Pcpstream object behaves always the same and it doesn't matter how it's backed.</p>
<p>We use it in the lib, e.g. in the crypto routines. That way we can support blockwise crypto on buffers or files.</p>
<p>Streams are, just like iostreams in c++, either output or input mode. </p>
<h2 class="groupheader">Typedef Documentation</h2>
<p>Streams are, just like iostreams in c++, either output or input mode.</p>
<p>Sample usage:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;stdio.h&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;stdlib.h&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;limits.h&gt;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#include &lt;pcp.h&gt;</span></div>
<div class="line"></div>
<div class="line"><span class="keywordtype">int</span> main() {</div>
<div class="line"> <span class="comment">/* create a file with &quot;encrypted&quot; data */</span></div>
<div class="line"> FILE *out, *in;</div>
<div class="line"> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> clear[8] = <span class="stringliteral">&quot;ABCDEFGH&quot;</span>;</div>
<div class="line"> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> key[8] = <span class="stringliteral">&quot;IxD8Lq1K&quot;</span>;</div>
<div class="line"> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> crypt[8] = {0};</div>
<div class="line"> <span class="keywordtype">int</span> blocks = 8;</div>
<div class="line"> <span class="keywordtype">int</span> i = 0;</div>
<div class="line"></div>
<div class="line"> <span class="keywordflow">if</span>((out = fopen(<span class="stringliteral">&quot;teststream.out&quot;</span>, <span class="stringliteral">&quot;wb+&quot;</span>)) == NULL) {</div>
<div class="line"> fprintf(stderr, <span class="stringliteral">&quot;oops, could not open file!\n&quot;</span>);</div>
<div class="line"> <span class="keywordflow">return</span> 1;</div>
<div class="line"> }</div>
<div class="line"> <a class="code" href="struct__pcp__stream__t.html" title="An I/O wrapper object backed by a file or a buffer.">Pcpstream</a> *pout = <a class="code" href="group__Pcpstream.html#ga6733979d79704b2e3ce914662b0cebc8" title="Create a new stream, backed with an open file.">ps_new_file</a>(out);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* &quot;encrypt&quot; a couple of times into the file */</span></div>
<div class="line"> <span class="keywordflow">for</span>(i=0; i&lt;blocks; i++) {</div>
<div class="line"> memcpy(crypt, clear, 8);</div>
<div class="line"> <a class="code" href="group__UTILs.html#gaad81054336208b62739f1e20091213a6" title="XOR an input buffer with another buffer.">_xorbuf</a>(key, crypt, 8);</div>
<div class="line"> <a class="code" href="group__Pcpstream.html#gab8e98ef81e802a242fbdb788b4387873" title="Write bytes from the given buffer into the current stream.">ps_write</a>(pout, crypt, 8);</div>
<div class="line"> }</div>
<div class="line"> <a class="code" href="group__Pcpstream.html#ga4a68da22eca6e9bd854d54467a071d0a" title="Close the stream and frees allocated memory.">ps_close</a>(pout);</div>
<div class="line"> fclose(out);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* read it in again using an in and an out stream */</span></div>
<div class="line"> <span class="keywordflow">if</span>((in = fopen(<span class="stringliteral">&quot;teststream.out&quot;</span>, <span class="stringliteral">&quot;rb&quot;</span>)) == NULL) {</div>
<div class="line"> fprintf(stderr, <span class="stringliteral">&quot;oops, could not open file!\n&quot;</span>);</div>
<div class="line"> <span class="keywordflow">return</span> 1;</div>
<div class="line"> }</div>
<div class="line"> <a class="code" href="struct__pcp__stream__t.html" title="An I/O wrapper object backed by a file or a buffer.">Pcpstream</a> *pin = <a class="code" href="group__Pcpstream.html#ga6733979d79704b2e3ce914662b0cebc8" title="Create a new stream, backed with an open file.">ps_new_file</a>(in);</div>
<div class="line"> </div>
<div class="line"> <span class="comment">/* we&#39;ll use this stream to put the &quot;decrypted&quot; data in.</span></div>
<div class="line"><span class="comment"> note, that this could be a file as well. */</span></div>
<div class="line"> <a class="code" href="struct__pcp__stream__t.html" title="An I/O wrapper object backed by a file or a buffer.">Pcpstream</a> *pclear = <a class="code" href="group__Pcpstream.html#ga13ec0245b579949e6d586e1817615d2e" title="Create a new output stream, backed with a buffer.">ps_new_outbuffer</a>();</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* read and &quot;decrypt&quot; */</span></div>
<div class="line"> <span class="keywordflow">for</span>(i=0; i&lt;blocks; i++) {</div>
<div class="line"> <a class="code" href="group__Pcpstream.html#gacfede4b1e9fa1ce0ccd7a55379ff6f15" title="Read bytes into the given buffer from the current stream.">ps_read</a>(pin, crypt, 8);</div>
<div class="line"> <a class="code" href="group__UTILs.html#gaad81054336208b62739f1e20091213a6" title="XOR an input buffer with another buffer.">_xorbuf</a>(key, crypt, 8);</div>
<div class="line"> <a class="code" href="group__Pcpstream.html#gab8e98ef81e802a242fbdb788b4387873" title="Write bytes from the given buffer into the current stream.">ps_write</a>(pclear, crypt, 8);</div>
<div class="line"> }</div>
<div class="line"> <a class="code" href="group__Pcpstream.html#ga4a68da22eca6e9bd854d54467a071d0a" title="Close the stream and frees allocated memory.">ps_close</a>(pin);</div>
<div class="line"> fclose(in);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* now extract the buffer from the output stream */</span></div>
<div class="line"> <a class="code" href="struct__pcp__buffer.html" title="A flexible buffer object wich automatically resizes, if neccessary.">Buffer</a> *result = <a class="code" href="group__Pcpstream.html#ga64bc34dfbc1b3951c3d62e82a1ae8c34" title="Access the Buffer backend pointer.">ps_buffer</a>(pclear);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* and verify if it&#39;s &quot;decrypted&quot; (re-use crypt) */</span></div>
<div class="line"> <span class="keywordflow">for</span>(i=0; i&lt;blocks; i++) {</div>
<div class="line"> <a class="code" href="group__Buffer.html#gaddf2e52378c6cd765b940617cdef2bd2" title="Read some chunk of data from the Buffer.">buffer_get_chunk</a>(result, crypt, 8);</div>
<div class="line"> <span class="keywordflow">if</span>(memcmp(crypt, clear, 8) != 0) {</div>
<div class="line"> fprintf(stderr, <span class="stringliteral">&quot;Oops, block %d doesn&#39;t match\n&quot;</span>, i);</div>
<div class="line"> <span class="keywordflow">goto</span> error;</div>
<div class="line"> }</div>
<div class="line"> }</div>
<div class="line"></div>
<div class="line"> <a class="code" href="group__Pcpstream.html#ga4a68da22eca6e9bd854d54467a071d0a" title="Close the stream and frees allocated memory.">ps_close</a>(pclear);</div>
<div class="line"></div>
<div class="line"> fprintf(stderr, <span class="stringliteral">&quot;done\n&quot;</span>);</div>
<div class="line"></div>
<div class="line"> <span class="keywordflow">return</span> 0;</div>
<div class="line"></div>
<div class="line"> error:</div>
<div class="line"> <a class="code" href="group__Pcpstream.html#ga4a68da22eca6e9bd854d54467a071d0a" title="Close the stream and frees allocated memory.">ps_close</a>(pclear);</div>
<div class="line"> <span class="keywordflow">return</span> 1;</div>
<div class="line">}</div>
</div><!-- fragment --> <h2 class="groupheader">Typedef Documentation</h2>
<a class="anchor" id="gaec72241f86d5391d5cae7477c66cdd73"></a>
<div class="memitem">
<div class="memproto">
@@ -114,7 +187,7 @@ Functions</h2></td></tr>
<p>The name used everywhere. </p>
<p>Definition at line <a class="el" href="pcpstream_8h_source.html#l00063">63</a> of file <a class="el" href="pcpstream_8h_source.html">pcpstream.h</a>.</p>
<p>Definition at line <a class="el" href="pcpstream_8h_source.html#l00067">67</a> of file <a class="el" href="pcpstream_8h_source.html">pcpstream.h</a>.</p>
</div>
</div>
@@ -459,7 +532,7 @@ Functions</h2></td></tr>
</div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Thu Feb 20 2014 19:59:14 for libpcp by &#160;<a href="http://www.doxygen.org/index.html">
Generated on Thu Feb 20 2014 20:58:12 for libpcp by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.2
</small></address>