123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- #include "Schluessel.h"
- using namespace Framework::Verschlüsselung;
- // Inhalt der Bytes Klasse aus Schlüssel.h
- // Konstruktor
- Bytes::Bytes()
- : bytes( 0 ),
- del( 1 ),
- län( 0 ),
- ref( 1 )
- {}
- Bytes::Bytes( int län )
- : bytes( new char[ län ] ),
- del( 1 ),
- län( län ),
- ref( 1 )
- {}
- Bytes::Bytes( const char *daten, int län )
- : bytes( new char[ län ] ),
- del( 1 ),
- län( län ),
- ref( 1 )
- {
- setBytes( daten );
- }
- // Destruktor
- Bytes::~Bytes()
- {
- if( del )
- delete[] bytes;
- }
- // nicht constant
- void Bytes::setBytes( const char *daten )
- {
- if( !bytes || !daten )
- return;
- char *end = bytes + län;
- for( char *c = bytes; c < end; c++, ++daten )
- *c = *daten;
- }
- void Bytes::setBytes( const char *daten, int län )
- {
- if( !daten || !län )
- return;
- if( del )
- delete[] bytes;
- del = 1;
- bytes = new char[ län ];
- this->län = län;
- setBytes( daten );
- }
- void Bytes::setBytesZ( char *daten, int län )
- {
- if( del )
- delete[] bytes;
- del = 0;
- bytes = daten;
- this->län = län;
- }
- void Bytes::füll( const char c )
- {
- if( !bytes )
- return;
- char *end = bytes + län;
- for( char *b = bytes; b < end; ++b )
- *b = c;
- }
- void Bytes::füll( const char c, int län )
- {
- if( !bytes )
- bytes = new char[ län ];
- län = län > this->län ? this->län : län;
- char *end = bytes + län;
- for( char *b = bytes; b < end; ++b )
- *b = c;
- }
- void Bytes::füll( const char c, int beg, int end )
- {
- if( beg >= län )
- return;
- if( !bytes )
- bytes = new char[ end - beg ];
- end = end > this->län ? this->län : end;
- char *endB = bytes + end;
- for( char *b = bytes + beg; b < endB; ++b )
- *b = c;
- }
- void Bytes::füll( const char *c, int cLän )
- {
- if( !c || !cLän || !bytes )
- return;
- char *endB = bytes + län;
- const char *endC = c + cLän;
- const char *d = c;
- for( char *b = bytes; b < endB; b++, d = d < endC - 1 ? d + 1 : c )
- *b = *d;
- }
- void Bytes::set( const char c, int pos )
- {
- if( !bytes || pos >= län )
- return;
- bytes[ pos ] = c;
- }
- // constant
- int Bytes::getLänge() const
- {
- return län;
- }
- char *Bytes::getBytes() const
- {
- return bytes;
- }
- // Reference Counting
- Bytes *Bytes::getThis()
- {
- ++ref;
- return this;
- }
- Bytes *Bytes::release()
- {
- --ref;
- if( !ref )
- delete this;
- return 0;
- }
- // Inhalt der Schlüssel Klasse aus Schlüssel.h
- // Konstruktor
- Schlüssel::Schlüssel()
- : schlüssel( 0 ),
- län( 0 ),
- pos( 0 ),
- ref( 1 )
- {}
- Schlüssel::Schlüssel( const char *s, int län )
- : schlüssel( new unsigned char[ län ] ),
- län( län ),
- pos( 0 ),
- ref( 1 )
- {
- for( int i = 0; i < län; ++i )
- schlüssel[ i ] = s[ i ];
- }
- // Destruktor
- Schlüssel::~Schlüssel()
- {
- delete[] schlüssel;
- }
- // nicht constant
- void Schlüssel::setPos( int p )
- {
- if( p < 0 )
- p = 0;
- pos = p > län ? 0 : p;
- }
- void Schlüssel::setSchlüssel( const char *s, int län )
- {
- delete[] schlüssel;
- schlüssel = new unsigned char[ län ];
- for( int i = 0; i < län; ++i )
- schlüssel[ i ] = s[ i ];
- pos = 0;
- this->län = län;
- }
- void Schlüssel::codieren( Bytes *daten )
- {
- if( !schlüssel || !län )
- {
- daten->release();
- return;
- }
- int dLän = daten->getLänge();
- char *bytes = daten->getBytes();
- char *bEnd = bytes + dLän;
- for( char *c = bytes; c < bEnd; ++c )
- {
- *c = *c + schlüssel[ pos ];
- ++pos;
- if( pos >= län )
- pos = 0;
- }
- daten->release();
- }
- void Schlüssel::decodieren( Bytes *daten )
- {
- if( !schlüssel || !län )
- {
- daten->release();
- return;
- }
- int dLän = daten->getLänge();
- char *bytes = daten->getBytes();
- char *bEnd = bytes + dLän;
- for( char *c = bytes; c < bEnd; ++c )
- {
- *c = *c - schlüssel[ pos ];
- ++pos;
- if( pos >= län )
- pos = 0;
- }
- daten->release();
- }
- // Reference Counting
- Schlüssel *Schlüssel::getThis()
- {
- ++ref;
- return this;
- }
- Schlüssel *Schlüssel::release()
- {
- --ref;
- if( !ref )
- delete this;
- return 0;
- }
|