Sophie

Sophie

distrib > Mandriva > 9.1 > ppc > by-pkgid > a74ec78bdb789d910d054e3918f3f007 > files > 474

libsword1-devel-1.5.5-2mdk.ppc.rpm

<!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>swcipher.cpp Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.2.15 -->
<center>
<a class="qindex" href="index.html">Main Page</a> &nbsp; <a class="qindex" href="namespaces.html">Namespace List</a> &nbsp; <a class="qindex" href="hierarchy.html">Class Hierarchy</a> &nbsp; <a class="qindex" href="classes.html">Alphabetical List</a> &nbsp; <a class="qindex" href="annotated.html">Compound List</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; <a class="qindex" href="functions.html">Compound Members</a> &nbsp; </center>
<hr><h1>swcipher.cpp</h1><div class="fragment"><pre>00001 <font class="comment">/******************************************************************************</font>
00002 <font class="comment"> *  swcipher.cpp   - code for class 'SWCipher'- a driver class that provides</font>
00003 <font class="comment"> *                              cipher utilities.</font>
00004 <font class="comment"> */</font>
00005 
00006 <font class="preprocessor">#include &lt;string.h&gt;</font>
00007 <font class="preprocessor">#include &lt;stdlib.h&gt;</font>
00008 <font class="preprocessor">#include &lt;swcipher.h&gt;</font>
00009 
00010 
00011 <font class="comment">/******************************************************************************</font>
00012 <font class="comment"> * SWCipher Constructor - Initializes data for instance of SWCipher</font>
00013 <font class="comment"> *</font>
00014 <font class="comment"> */</font>
00015 
00016 SWCipher::SWCipher(<font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> *key) {
00017         master.initialize(key, strlen((<font class="keywordtype">char</font> *)key));
00018         buf = 0;
00019 }
00020 
00021 
00022 <font class="comment">/******************************************************************************</font>
00023 <font class="comment"> * SWCipher Destructor - Cleans up instance of SWCipher</font>
00024 <font class="comment"> */</font>
00025 
00026 SWCipher::~SWCipher()
00027 {
00028         <font class="keywordflow">if</font> (buf)
00029                 free(buf);
00030 }
00031 
00032 
00033 <font class="keywordtype">char</font> *SWCipher::Buf(<font class="keyword">const</font> <font class="keywordtype">char</font> *ibuf, <font class="keywordtype">unsigned</font> <font class="keywordtype">int</font> ilen)
00034 {
00035         <font class="keywordflow">if</font> (ibuf) {
00036         
00037                 <font class="keywordflow">if</font> (buf)
00038                         free(buf);
00039 
00040                 <font class="keywordflow">if</font> (!ilen) {
00041                         len = strlen(buf);
00042                         ilen = len + 1;
00043                 }
00044                 <font class="keywordflow">else</font> len = ilen;
00045 
00046                 buf = (<font class="keywordtype">char</font> *) malloc(ilen);
00047                 memcpy(buf, ibuf, ilen);
00048                 cipher = <font class="keyword">false</font>;
00049         }
00050 
00051         Decode();
00052 
00053         <font class="keywordflow">return</font> buf;
00054 }
00055 
00056 
00057 <font class="keywordtype">char</font> *SWCipher::cipherBuf(<font class="keywordtype">unsigned</font> <font class="keywordtype">int</font> *ilen, <font class="keyword">const</font> <font class="keywordtype">char</font> *ibuf)
00058 {
00059         <font class="keywordflow">if</font> (ibuf) {
00060         
00061                 <font class="keywordflow">if</font> (buf)
00062                         free(buf);
00063                         
00064                 buf = (<font class="keywordtype">char</font> *) malloc(*ilen);
00065                 memcpy(buf, ibuf, *ilen);
00066                 len = *ilen;
00067                 cipher = <font class="keyword">true</font>;
00068         }
00069 
00070         Encode();
00071 
00072         *ilen = (short)len;
00073         <font class="keywordflow">return</font> buf;
00074 }
00075 
00076 
00077 <font class="comment">/******************************************************************************</font>
00078 <font class="comment"> * SWCipher::Encode     - This function "encodes" the input stream into the</font>
00079 <font class="comment"> *                                              output stream.</font>
00080 <font class="comment"> *                                              The GetChars() and SendChars() functions are</font>
00081 <font class="comment"> *                                              used to separate this method from the actual</font>
00082 <font class="comment"> *                                              i/o.</font>
00083 <font class="comment"> */</font>
00084 
00085 <font class="keywordtype">void</font> SWCipher::Encode(<font class="keywordtype">void</font>)
00086 {
00087         <font class="keywordflow">if</font> (!cipher) {
00088                 work = master;
00089                 <font class="keywordflow">for</font> (<font class="keywordtype">int</font> i = 0; i &lt; len; i++)
00090                         buf[i] = work.encrypt(buf[i]);
00091                 cipher = <font class="keyword">true</font>;
00092         }
00093 }
00094 
00095 
00096 <font class="comment">/******************************************************************************</font>
00097 <font class="comment"> * SWCipher::Decode     - This function "decodes" the input stream into the</font>
00098 <font class="comment"> *                                              output stream.</font>
00099 <font class="comment"> *                                              The GetChars() and SendChars() functions are</font>
00100 <font class="comment"> *                                              used to separate this method from the actual</font>
00101 <font class="comment"> *                                              i/o.</font>
00102 <font class="comment"> */</font>
00103 
00104 <font class="keywordtype">void</font> SWCipher::Decode(<font class="keywordtype">void</font>)
00105 {
00106         <font class="keywordflow">if</font> (cipher) {
00107                 work = master;
00108                 <font class="keywordflow">for</font> (<font class="keywordtype">int</font> i = 0; i &lt; len; i++)
00109                         buf[i] = work.decrypt(buf[i]);
00110                 cipher = <font class="keyword">false</font>;
00111         }
00112 }
00113 
00114 
00115 <font class="comment">/******************************************************************************</font>
00116 <font class="comment"> * SWCipher::setCipherKey       - setter for a new CipherKey</font>
00117 <font class="comment"> *</font>
00118 <font class="comment"> */</font>
00119 
00120 <font class="keywordtype">void</font> SWCipher::setCipherKey(<font class="keyword">const</font> <font class="keywordtype">char</font> *ikey) {
00121         <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> *key = (<font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> *)ikey;
00122         master.initialize(key, strlen((<font class="keywordtype">char</font> *)key));
00123 }
</pre></div><hr><address align="right"><small>Generated on Thu Jun 20 22:13:00 2002 for The Sword Project by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0 
width=110 height=53></a>1.2.15 </small></address>
</body>
</html>