blob: 736a676c3486a82274a8fabb4da1d6ca7e221b56 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
<!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> <a class="qindex" href="namespaces.html">Namespace List</a> <a class="qindex" href="hierarchy.html">Class Hierarchy</a> <a class="qindex" href="classes.html">Alphabetical List</a> <a class="qindex" href="annotated.html">Compound List</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="functions.html">Compound Members</a> </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 <string.h></font>
00007 <font class="preprocessor">#include <stdlib.h></font>
00008 <font class="preprocessor">#include <swcipher.h></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 < 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 < 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>
|