~ubuntu-branches/debian/squeeze/sword/squeeze

« back to all changes in this revision

Viewing changes to src/modules/common/swcipher.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Glassey
  • Date: 2004-01-15 15:50:07 UTC
  • Revision ID: james.westby@ubuntu.com-20040115155007-n9mz4x0zxrs1isd3
Tags: upstream-1.5.7
ImportĀ upstreamĀ versionĀ 1.5.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************************
 
2
 *  swcipher.cpp   - code for class 'SWCipher'- a driver class that provides
 
3
 *                              cipher utilities.
 
4
 */
 
5
 
 
6
#include <stdlib.h>
 
7
#include <string.h>
 
8
#include <swcipher.h>
 
9
 
 
10
SWORD_NAMESPACE_START
 
11
 
 
12
/******************************************************************************
 
13
 * SWCipher Constructor - Initializes data for instance of SWCipher
 
14
 *
 
15
 */
 
16
 
 
17
SWCipher::SWCipher(unsigned char *key) {
 
18
        master.initialize(key, strlen((char *)key));
 
19
        buf = 0;
 
20
}
 
21
 
 
22
 
 
23
/******************************************************************************
 
24
 * SWCipher Destructor - Cleans up instance of SWCipher
 
25
 */
 
26
 
 
27
SWCipher::~SWCipher()
 
28
{
 
29
        if (buf)
 
30
                free(buf);
 
31
}
 
32
 
 
33
 
 
34
char *SWCipher::Buf(const char *ibuf, unsigned long ilen)
 
35
{
 
36
        if (ibuf) {
 
37
        
 
38
                if (buf)
 
39
                        free(buf);
 
40
 
 
41
                if (!ilen) {
 
42
                        len = strlen(buf);
 
43
                        ilen = len + 1;
 
44
                }
 
45
                else len = ilen;
 
46
 
 
47
                buf = (char *) malloc(ilen);
 
48
                memcpy(buf, ibuf, ilen);
 
49
                cipher = false;
 
50
        }
 
51
 
 
52
        Decode();
 
53
 
 
54
        return buf;
 
55
}
 
56
 
 
57
 
 
58
char *SWCipher::cipherBuf(unsigned long *ilen, const char *ibuf)
 
59
{
 
60
        if (ibuf) {
 
61
        
 
62
                if (buf)
 
63
                        free(buf);
 
64
                        
 
65
                buf = (char *) malloc(*ilen+1);
 
66
                memcpy(buf, ibuf, *ilen);
 
67
                len = *ilen;
 
68
                cipher = true;
 
69
        }
 
70
 
 
71
        Encode();
 
72
 
 
73
        *ilen = len;
 
74
        return buf;
 
75
}
 
76
 
 
77
 
 
78
/******************************************************************************
 
79
 * SWCipher::Encode     - This function "encodes" the input stream into the
 
80
 *                                              output stream.
 
81
 *                                              The GetChars() and SendChars() functions are
 
82
 *                                              used to separate this method from the actual
 
83
 *                                              i/o.
 
84
 */
 
85
 
 
86
void SWCipher::Encode(void)
 
87
{
 
88
        if (!cipher) {
 
89
                work = master;
 
90
                for (unsigned long i = 0; i < len; i++)
 
91
                        buf[i] = work.encrypt(buf[i]);
 
92
                cipher = true;
 
93
        }
 
94
}
 
95
 
 
96
 
 
97
/******************************************************************************
 
98
 * SWCipher::Decode     - This function "decodes" the input stream into the
 
99
 *                                              output stream.
 
100
 *                                              The GetChars() and SendChars() functions are
 
101
 *                                              used to separate this method from the actual
 
102
 *                                              i/o.
 
103
 */
 
104
 
 
105
void SWCipher::Decode(void)
 
106
{
 
107
        if (cipher) {
 
108
                work = master;
 
109
                unsigned long i;
 
110
                for (i = 0; i < len; i++)
 
111
                        buf[i] = work.decrypt(buf[i]);
 
112
                buf[i] = 0;
 
113
                cipher = false;
 
114
        }
 
115
}
 
116
 
 
117
 
 
118
/******************************************************************************
 
119
 * SWCipher::setCipherKey       - setter for a new CipherKey
 
120
 *
 
121
 */
 
122
 
 
123
void SWCipher::setCipherKey(const char *ikey) {
 
124
        unsigned char *key = (unsigned char *)ikey;
 
125
        master.initialize(key, strlen((char *)key));
 
126
}
 
127
 
 
128
SWORD_NAMESPACE_END