The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cipherfil.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * cipherfil.cpp - CipherFilter, a SWFilter descendant to decipher
4  * a module
5  *
6  * $Id: cipherfil.cpp 3754 2020-07-10 17:45:48Z scribe $
7  *
8  * Copyright 1999-2013 CrossWire Bible Society (http://www.crosswire.org)
9  * CrossWire Bible Society
10  * P. O. Box 2528
11  * Tempe, AZ 85280-2528
12  *
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU General Public License as published by the
15  * Free Software Foundation version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * General Public License for more details.
21  *
22  */
23 
24 
25 #include <stdlib.h>
26 #include <cipherfil.h>
27 #include <swcipher.h>
28 #include <swbuf.h>
29 
30 
32 
33 
34 CipherFilter::CipherFilter(const char *key) {
35  cipher = new SWCipher((unsigned char *)key);
36 }
37 
38 
40  delete cipher;
41 }
42 
43 
45  return cipher;
46 }
47 
48 
49 char CipherFilter::processText(SWBuf &text, const SWKey *key, const SWModule *module) {
50  if (text.length() > 2) { //check if it's large enough to substract 2 in the next step.
51  unsigned long len = text.length();
52  if (!key) { // hack, using key to determine encipher, or decipher
53  cipher->setCipheredBuf(&len, text.getRawData()); //set buffer to enciphered text
55  // don't just assign text because we might be compressing binary data
56  text.setSize(len + 5);
57  memcpy(text.getRawData(), cipher->getUncipheredBuf(), len);
58  }
59  else if ((unsigned long)key == 1) {
60  cipher->setUncipheredBuf(text.getRawData(), len);
61  cipher->getCipheredBuf(&len);
62  text.setSize(len + 5);
63  memcpy(text.getRawData(), cipher->getCipheredBuf(&len), len);
64  }
65  }
66  return 0;
67 }
68 
69 
71 
#define SWORD_NAMESPACE_START
Definition: defs.h:39
Definition: swbuf.h:47
SWCipher * cipher
Definition: cipherfil.h:35
unsigned long length() const
Definition: swbuf.h:197
virtual SWCipher * getCipher()
Definition: cipherfil.cpp:44
SWText * module
Definition: osis2mod.cpp:105
virtual void setCipheredBuf(unsigned long *len, const char *buf=0)
Definition: swcipher.cpp:95
virtual ~CipherFilter()
Definition: cipherfil.cpp:39
char * getRawData()
Definition: swbuf.h:379
virtual char * getCipheredBuf(unsigned long *len=0)
Definition: swcipher.cpp:112
CipherFilter(const char *key)
Definition: cipherfil.cpp:34
virtual void setUncipheredBuf(const char *buf=0, unsigned long len=0)
Definition: swcipher.cpp:67
virtual char * getUncipheredBuf()
Definition: swcipher.cpp:87
#define SWORD_NAMESPACE_END
Definition: defs.h:40
virtual char processText(SWBuf &text, const SWKey *key=0, const SWModule *=0)
Definition: cipherfil.cpp:49
Definition: swkey.h:77
void setSize(unsigned long len)
Definition: swbuf.h:255