00001 /*************************************************************************** 00002 password.cpp - description 00003 ------------------- 00004 begin : Mon Nov 27 2000 00005 copyright : (C) 2000 by Martin Bickel 00006 email : bickel@asc-hq.org 00007 ***************************************************************************/ 00008 00014 /*************************************************************************** 00015 * * 00016 * This program is free software; you can redistribute it and/or modify * 00017 * it under the terms of the GNU General Public License as published by * 00018 * the Free Software Foundation; either version 2 of the License, or * 00019 * (at your option) any later version. * 00020 * * 00021 ***************************************************************************/ 00022 00023 #include "password.h" 00024 #include "misc.h" 00025 00026 00027 void Password :: setUnencoded ( const string& s ) 00028 { 00029 password = encodedpassword2string ( encodepassword ( s.c_str() ) ); 00030 } 00031 00032 void Password :: setEncoded ( const string& s ) 00033 { 00034 if ( !s.empty () ) { 00035 if ( s[0] == 'A' ) 00036 password = s; 00037 else 00038 password = encodedpassword2string ( atoi ( s.c_str() )); 00039 } else 00040 password = s; 00041 } 00042 00043 00044 void Password :: setInt ( int pwd ) 00045 { 00046 password = encodedpassword2string ( pwd ); 00047 } 00048 00049 bool Password :: operator== ( const Password& p ) const 00050 { 00051 return p.password == password; 00052 } 00053 00054 bool Password :: operator!= ( const Password& p ) const 00055 { 00056 return p.password != password; 00057 } 00058 00059 00060 int Password :: encodepassword ( const char* pw ) const 00061 { 00062 if ( !pw ) 00063 return 0; 00064 00065 int len = strlen ( pw ); 00066 00067 if ( len ) 00068 return crc32buf( pw, len+1 ); 00069 else 00070 return 0; 00071 } 00072 00073 string Password :: encodedpassword2string ( int pwd ) const 00074 { 00075 string s; 00076 if ( !pwd ) 00077 return s; 00078 00079 s = "A"; 00080 if ( pwd > 0 ) 00081 s+= "A"; 00082 else 00083 s+= "B"; 00084 s += strrr ( abs (pwd) ); 00085 return s; 00086 } 00087 00088 00089 bool Password :: empty () const 00090 { 00091 return password.empty(); 00092 } 00093 00094 string Password :: toString ( ) const 00095 { 00096 return password; 00097 } 00098 00099 00100 void Password::read ( tnstream& stream ) 00101 { 00102 int i = stream.readInt(); 00103 password = encodedpassword2string ( i ); 00104 } 00105 00106 void Password::write ( tnstream& stream ) const 00107 { 00108 int i; 00109 if ( password[0] == 'A' ) { 00110 if ( password[1] == 'A' ) 00111 i = atoi ( password.substr ( 2 ).c_str() ); 00112 else 00113 i = -atoi ( password.substr ( 2 ).c_str() ); 00114 00115 stream.writeInt ( i ); 00116 } else 00117 stream.writeInt ( 0 ); 00118 } 00119 00120 void Password::reset() 00121 { 00122 password = ""; 00123 }
1.4.2