files_8cpp-source.html

来自「Crypto++是一个非常强大的密码学库,主要是功能全」· HTML 代码 · 共 216 行

HTML
216
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>Crypto++: files.cpp Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.3.2 --><div class="qindex"><a class="qindex" href="index.html">Main&nbsp;Page</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class&nbsp;Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical&nbsp;List</a> | <a class="qindex" href="annotated.html">Compound&nbsp;List</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="namespacemembers.html">Namespace&nbsp;Members</a> | <a class="qindex" href="functions.html">Compound&nbsp;Members</a> | <a class="qindex" href="globals.html">File&nbsp;Members</a></div><h1>files.cpp</h1><div class="fragment"><pre>00001 <span class="comment">// files.cpp - written and placed in the public domain by Wei Dai</span>00002 00003 <span class="preprocessor">#include "pch.h"</span>00004 00005 <span class="preprocessor">#ifndef CRYPTOPP_IMPORTS</span>00006 <span class="preprocessor"></span>00007 <span class="preprocessor">#include "files.h"</span>00008 00009 NAMESPACE_BEGIN(CryptoPP)00010 00011 <span class="keyword">using</span> <span class="keyword">namespace </span>std;00012 00013 <span class="keywordtype">void</span> Files_TestInstantiations()00014 {00015         <a class="code" href="class_file_store.html">FileStore</a> f0;00016         <a class="code" href="class_file_source.html">FileSource</a> f1;00017         <a class="code" href="class_file_sink.html">FileSink</a> f2;00018 }00019 00020 <span class="keywordtype">void</span> FileStore::StoreInitialize(<span class="keyword">const</span> <a class="code" href="class_name_value_pairs.html">NameValuePairs</a> &amp;parameters)00021 {00022         m_file.reset(<span class="keyword">new</span> std::ifstream);00023         <span class="keyword">const</span> <span class="keywordtype">char</span> *fileName;00024         <span class="keywordflow">if</span> (parameters.<a class="code" href="class_name_value_pairs.html#_x_t_r___d_ha39">GetValue</a>(<span class="stringliteral">"InputFileName"</span>, fileName))00025         {00026                 ios::openmode binary = parameters.<a class="code" href="class_name_value_pairs.html#_x_t_r___d_ha40">GetValueWithDefault</a>(<span class="stringliteral">"InputBinaryMode"</span>, <span class="keyword">true</span>) ? ios::binary : ios::openmode(0);00027                 m_file-&gt;open(fileName, ios::in | binary);00028                 <span class="keywordflow">if</span> (!*m_file)00029                         <span class="keywordflow">throw</span> OpenErr(fileName);00030                 m_stream = m_file.get();00031         }00032         <span class="keywordflow">else</span>00033         {00034                 m_stream = NULL;00035                 parameters.<a class="code" href="class_name_value_pairs.html#_x_t_r___d_ha39">GetValue</a>(<span class="stringliteral">"InputStreamPointer"</span>, m_stream);00036         }00037         m_waiting = <span class="keyword">false</span>;00038 }00039 <a name="l00040"></a><a class="code" href="class_file_store.html#_file_storea4">00040</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> <a class="code" href="class_file_store.html#_file_storea4">FileStore::MaxRetrievable</a>()<span class="keyword"> const</span>00041 <span class="keyword"></span>{00042         <span class="keywordflow">if</span> (!m_stream)00043                 <span class="keywordflow">return</span> 0;00044 00045         streampos current = m_stream-&gt;tellg();00046         streampos end = m_stream-&gt;seekg(0, ios::end).tellg();00047         m_stream-&gt;seekg(current);00048         <span class="keywordflow">return</span> end-current;00049 }00050 <a name="l00051"></a><a class="code" href="class_file_store.html#_file_storea5">00051</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="class_file_store.html#_file_storea5">FileStore::Peek</a>(byte &amp;outByte)<span class="keyword"> const</span>00052 <span class="keyword"></span>{00053         <span class="keywordflow">if</span> (!m_stream)00054                 <span class="keywordflow">return</span> 0;00055 00056         <span class="keywordtype">int</span> result = m_stream-&gt;peek();00057         <span class="keywordflow">if</span> (result == EOF)      <span class="comment">// GCC workaround: 2.95.2 doesn't have char_traits&lt;char&gt;::eof()</span>00058                 <span class="keywordflow">return</span> 0;00059         <span class="keywordflow">else</span>00060         {00061                 outByte = byte(result);00062                 <span class="keywordflow">return</span> 1;00063         }00064 }00065 <a name="l00066"></a><a class="code" href="class_file_store.html#_file_storea6">00066</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="class_file_store.html#_file_storea6">FileStore::TransferTo2</a>(<a class="code" href="class_buffered_transformation.html">BufferedTransformation</a> &amp;target, <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> &amp;transferBytes, <span class="keyword">const</span> std::string &amp;channel, <span class="keywordtype">bool</span> blocking)00067 {00068         <span class="keywordflow">if</span> (!m_stream)00069         {00070                 transferBytes = 0;00071                 <span class="keywordflow">return</span> 0;00072         }00073 00074         <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> size=transferBytes;00075         transferBytes = 0;00076 00077         <span class="keywordflow">if</span> (m_waiting)00078                 <span class="keywordflow">goto</span> output;00079 00080         <span class="keywordflow">while</span> (size &amp;&amp; m_stream-&gt;good())00081         {00082                 {00083                 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> spaceSize = 1024;00084                 m_space = HelpCreatePutSpace(target, channel, 1, (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>)STDMIN(size, (<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>)UINT_MAX), spaceSize);00085 00086                 m_stream-&gt;read((<span class="keywordtype">char</span> *)m_space, STDMIN(size, (<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>)spaceSize));00087                 }00088                 m_len = m_stream-&gt;gcount();00089                 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> blockedBytes;00090 output:00091                 blockedBytes = target.<a class="code" href="class_buffered_transformation.html#_zlib_decompressorz13_9">ChannelPutModifiable2</a>(channel, m_space, m_len, 0, blocking);00092                 m_waiting = blockedBytes &gt; 0;00093                 <span class="keywordflow">if</span> (m_waiting)00094                         <span class="keywordflow">return</span> blockedBytes;00095                 size -= m_len;00096                 transferBytes += m_len;00097         }00098 00099         <span class="keywordflow">if</span> (!m_stream-&gt;good() &amp;&amp; !m_stream-&gt;eof())00100                 <span class="keywordflow">throw</span> ReadErr();00101 00102         <span class="keywordflow">return</span> 0;00103 }00104 00105 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> FileStore::CopyRangeTo2(<a class="code" href="class_buffered_transformation.html">BufferedTransformation</a> &amp;target, <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> &amp;begin, <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> end, <span class="keyword">const</span> std::string &amp;channel, <span class="keywordtype">bool</span> blocking)<span class="keyword"> const</span>00106 <span class="keyword"></span>{00107         <span class="keywordflow">if</span> (!m_stream)00108                 <span class="keywordflow">return</span> 0;00109 00110         <span class="comment">// TODO: figure out what happens on cin</span>00111         streampos current = m_stream-&gt;tellg();00112         streampos endPosition = m_stream-&gt;seekg(0, ios::end).tellg();00113         streampos newPosition = current + (streamoff)begin;00114 00115         <span class="keywordflow">if</span> (newPosition &gt;= endPosition)00116         {00117                 m_stream-&gt;seekg(current);00118                 <span class="keywordflow">return</span> 0;       <span class="comment">// don't try to seek beyond the end of file</span>00119         }00120         m_stream-&gt;seekg(newPosition);00121         <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> total = 0;00122         <span class="keywordflow">try</span>00123         {00124                 assert(!m_waiting);00125                 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> copyMax = end-begin;00126                 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> blockedBytes = const_cast&lt;FileStore *&gt;(<span class="keyword">this</span>)-&gt;TransferTo2(target, copyMax, channel, blocking);00127                 begin += copyMax;00128                 <span class="keywordflow">if</span> (blockedBytes)00129                 {00130                         const_cast&lt;FileStore *&gt;(<span class="keyword">this</span>)-&gt;m_waiting = <span class="keyword">false</span>;00131                         <span class="keywordflow">return</span> blockedBytes;00132                 }00133         }00134         <span class="keywordflow">catch</span>(...)00135         {00136                 m_stream-&gt;clear();00137                 m_stream-&gt;seekg(current);00138                 <span class="keywordflow">throw</span>;00139         }00140         m_stream-&gt;clear();00141         m_stream-&gt;seekg(current);00142 00143         <span class="keywordflow">return</span> 0;00144 }00145 <a name="l00146"></a><a class="code" href="class_file_store.html#_file_storea8">00146</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> <a class="code" href="class_file_store.html#_file_storea8">FileStore::Skip</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> skipMax)00147 {00148         <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> oldPos = m_stream-&gt;tellg();00149         m_stream-&gt;seekg(skipMax, ios_base::cur);00150         <span class="keywordflow">return</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>)m_stream-&gt;tellg() - oldPos;00151 }00152 00153 <span class="keywordtype">void</span> FileSink::IsolatedInitialize(<span class="keyword">const</span> <a class="code" href="class_name_value_pairs.html">NameValuePairs</a> &amp;parameters)00154 {00155         m_file.reset(<span class="keyword">new</span> std::ofstream);00156         <span class="keyword">const</span> <span class="keywordtype">char</span> *fileName;00157         <span class="keywordflow">if</span> (parameters.<a class="code" href="class_name_value_pairs.html#_x_t_r___d_ha39">GetValue</a>(<span class="stringliteral">"OutputFileName"</span>, fileName))00158         {00159                 ios::openmode binary = parameters.<a class="code" href="class_name_value_pairs.html#_x_t_r___d_ha40">GetValueWithDefault</a>(<span class="stringliteral">"OutputBinaryMode"</span>, <span class="keyword">true</span>) ? ios::binary : ios::openmode(0);00160                 m_file-&gt;open(fileName, ios::out | ios::trunc | binary);00161                 <span class="keywordflow">if</span> (!*m_file)00162                         <span class="keywordflow">throw</span> OpenErr(fileName);00163                 m_stream = m_file.get();00164         }00165         <span class="keywordflow">else</span>00166         {00167                 m_stream = NULL;00168                 parameters.<a class="code" href="class_name_value_pairs.html#_x_t_r___d_ha39">GetValue</a>(<span class="stringliteral">"OutputStreamPointer"</span>, m_stream);00169         }00170 }00171 00172 <span class="keywordtype">bool</span> FileSink::IsolatedFlush(<span class="keywordtype">bool</span> hardFlush, <span class="keywordtype">bool</span> blocking)00173 {00174         <span class="keywordflow">if</span> (!m_stream)00175                 <span class="keywordflow">throw</span> Err(<span class="stringliteral">"FileSink: output stream not opened"</span>);00176 00177         m_stream-&gt;flush();00178         <span class="keywordflow">if</span> (!m_stream-&gt;good())00179           <span class="keywordflow">throw</span> WriteErr();00180 00181         <span class="keywordflow">return</span> <span class="keyword">false</span>;00182 }00183 <a name="l00184"></a><a class="code" href="class_file_sink.html#_file_sinka5">00184</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="class_file_sink.html#_file_sinka5">FileSink::Put2</a>(<span class="keyword">const</span> byte *inString, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length, <span class="keywordtype">int</span> messageEnd, <span class="keywordtype">bool</span> blocking)00185 {00186         <span class="keywordflow">if</span> (!m_stream)00187                 <span class="keywordflow">throw</span> Err(<span class="stringliteral">"FileSink: output stream not opened"</span>);00188 00189         m_stream-&gt;write((<span class="keyword">const</span> <span class="keywordtype">char</span> *)inString, length);00190 00191         <span class="keywordflow">if</span> (messageEnd)00192                 m_stream-&gt;flush();00193 00194         <span class="keywordflow">if</span> (!m_stream-&gt;good())00195           <span class="keywordflow">throw</span> WriteErr();00196 00197         <span class="keywordflow">return</span> 0;00198 }00199 00200 NAMESPACE_END00201 00202 <span class="preprocessor">#endif</span></pre></div><hr size="1"><address style="align: right;"><small>Generated on Tue Jul 8 23:34:14 2003 for Crypto++ by<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border=0 > </a>1.3.2 </small></address></body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?