Coder
Encoding/encryption abstract base class
[Previous] [Main] [Next]


Description
Coder is an abstract base class that defines a common interface for general-purpose encryption and encoding of data. Coder-derived classes are used in conjunction with the htmlCgi class to perform automatic encryption and decryption of name/value pairs.  
 
html++ includes four Coder-derived classes:  
NullCoder  
XorCoder  
ScrambleCoder  
UrlCoder  
 
Declaration
#include <dcmicro/htmlpp/coder.h>  
 
Hierarchy
coder.gif  
 
See Also
htmlServer, NullCoder, XorCoder, ScrambleCoder, UrlCoder  
 
Related Constants/Definitions
None.  
 
Constructors
Coder()  
Constructs an empty object.  
 
Destructors
virtual ~Coder()  
Destroys the object.  
 
Member Methods
ID   virtual int ID() const = 0  
Returns the integer identification of the algorithm. Valid ID values range from 0-99, and the range of 0-30 is reserved by html++. User-defined algorithms may use ID's in the range of 31-99. The htmlCgi class encodes the ID of an algorithm in it's output for later identification during decoding.  
 
Namevirtual String Name() const = 0  
Returns the name of the algorithm as a text string. This is for informational purposes only.  
 
Encodevirtual char * Encode( const char * unencoded, int& length ) = 0  
Encode the unencoded buffer of specified length bytes, allocating storage for the resulting buffer using 'new' and returning a pointer to it. The length reference is updated to reflect the number of bytes returned in the allocated buffer, which could be larger or smaller than the original value depending on the algorithm. It is the responsiblity of the calling routine to release the memory using 'delete'.  
 
NOTE: By convention, when subclassing Coder for user-defined algorithms, the size of the buffer allocated for the encoded result must be (length+1) bytes so that calling routines may optionally append a terminating null character.  
 
Decodevirtual char * Decode( const char * encoded, int& length ) = 0  
Decode the encoded buffer of specified length bytes, allocating storage for the resulting buffer using 'new' and returning a pointer to it. The length reference is updated to reflect the number of bytes returned in the allocated buffer, which could be larger or smaller than the original value depending on the algorithm. It is the responsiblity of the calling routine to release the memory using 'delete'.  
 
NOTE: As with the Encode() method, when subclassing Coder for user-defined algorithms, the size of the buffer allocated for the decoded result must be (length+1) bytes so that calling routines may optionally append a terminating null character.  
 
Example Use
 
The example below illustrates using the ScrambleCoder class to encrypt and then decrypt a buffer of text. In day-to-day use with html++, most applications simply instantiate a global copy of ScrambleCoder, passing it to htmlServer::Crypt() at startup for automatic encryption and decryption of name/value pairs.  
 
#include <stdio.h>  
#include <stdlib.h>  
#include <dcmicro/htmlpp/htmlpp.h>  
 
int main( void )  
{  
   char *  orig = "The quick brown fox" ;  
   char *  enc ;   //  encoded pointer  
   char *  dec ;   //  decoded pointer  
   int     length = strlen(orig) ;  
 
   //  Set up a ScrambleCoder using a password,  
   //  then use it to encrypt the original text.  
   //  Note that the length parameter's value may  
   //  be altered by the Encode() method.  
   ScrambleCoder  s( "password" ) ;  
   enc = s.Encode( orig, length ) ;  
   enc[length] = '\0' ;  //  append term null  
   cout << enc << "\n" ; //  display the encrypted data  
 
   //  Now set up another ScrambleCoder to decrypt  
   //  the data, using the same password.  
   ScrambleCoder  s2( "password" ) ;  
   dec = s2.Decode( enc, length ) ;  
   dec[length] = '\0' ;  //  append term null  
   cout << dec << "\n" ; //  display the result  
 
   //  Release the buffers allocated  
   //  by Encode() and Decode()  
   delete enc ;  
   delete dec ;  
   return 0 ;  
}  
 


©1998 DC Micro Development. All rights reserved.
No portion of this document may be c opied or reproduced without expressed written consent.
html++ is a trademark of DC Micro Development.