Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

swcipher.cpp

00001 /******************************************************************************
00002  *  swcipher.cpp   - code for class 'SWCipher'- a driver class that provides
00003  *                              cipher utilities.
00004  */
00005 
00006 #include <string.h>
00007 #include <stdlib.h>
00008 #include <swcipher.h>
00009 
00010 
00011 /******************************************************************************
00012  * SWCipher Constructor - Initializes data for instance of SWCipher
00013  *
00014  */
00015 
00016 SWCipher::SWCipher(unsigned char *key) {
00017         master.initialize(key, strlen((char *)key));
00018         buf = 0;
00019 }
00020 
00021 
00022 /******************************************************************************
00023  * SWCipher Destructor - Cleans up instance of SWCipher
00024  */
00025 
00026 SWCipher::~SWCipher()
00027 {
00028         if (buf)
00029                 free(buf);
00030 }
00031 
00032 
00033 char *SWCipher::Buf(const char *ibuf, unsigned int ilen)
00034 {
00035         if (ibuf) {
00036         
00037                 if (buf)
00038                         free(buf);
00039 
00040                 if (!ilen) {
00041                         len = strlen(buf);
00042                         ilen = len + 1;
00043                 }
00044                 else len = ilen;
00045 
00046                 buf = (char *) malloc(ilen);
00047                 memcpy(buf, ibuf, ilen);
00048                 cipher = false;
00049         }
00050 
00051         Decode();
00052 
00053         return buf;
00054 }
00055 
00056 
00057 char *SWCipher::cipherBuf(unsigned int *ilen, const char *ibuf)
00058 {
00059         if (ibuf) {
00060         
00061                 if (buf)
00062                         free(buf);
00063                         
00064                 buf = (char *) malloc(*ilen);
00065                 memcpy(buf, ibuf, *ilen);
00066                 len = *ilen;
00067                 cipher = true;
00068         }
00069 
00070         Encode();
00071 
00072         *ilen = (short)len;
00073         return buf;
00074 }
00075 
00076 
00077 /******************************************************************************
00078  * SWCipher::Encode     - This function "encodes" the input stream into the
00079  *                                              output stream.
00080  *                                              The GetChars() and SendChars() functions are
00081  *                                              used to separate this method from the actual
00082  *                                              i/o.
00083  */
00084 
00085 void SWCipher::Encode(void)
00086 {
00087         if (!cipher) {
00088                 work = master;
00089                 for (int i = 0; i < len; i++)
00090                         buf[i] = work.encrypt(buf[i]);
00091                 cipher = true;
00092         }
00093 }
00094 
00095 
00096 /******************************************************************************
00097  * SWCipher::Decode     - This function "decodes" the input stream into the
00098  *                                              output stream.
00099  *                                              The GetChars() and SendChars() functions are
00100  *                                              used to separate this method from the actual
00101  *                                              i/o.
00102  */
00103 
00104 void SWCipher::Decode(void)
00105 {
00106         if (cipher) {
00107                 work = master;
00108                 for (int i = 0; i < len; i++)
00109                         buf[i] = work.decrypt(buf[i]);
00110                 cipher = false;
00111         }
00112 }
00113 
00114 
00115 /******************************************************************************
00116  * SWCipher::setCipherKey       - setter for a new CipherKey
00117  *
00118  */
00119 
00120 void SWCipher::setCipherKey(const char *ikey) {
00121         unsigned char *key = (unsigned char *)ikey;
00122         master.initialize(key, strlen((char *)key));
00123 }

Generated on Thu Jun 20 22:13:00 2002 for The Sword Project by doxygen1.2.15