Advanced Strategic Command
password.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  password.cpp - description
3  -------------------
4  begin : Mon Nov 27 2000
5  copyright : (C) 2000 by Martin Bickel
6  email : bickel@asc-hq.org
7  ***************************************************************************/
8 
14 /***************************************************************************
15  * *
16  * This program is free software; you can redistribute it and/or modify *
17  * it under the terms of the GNU General Public License as published by *
18  * the Free Software Foundation; either version 2 of the License, or *
19  * (at your option) any later version. *
20  * *
21  ***************************************************************************/
22 
23 #include "password.h"
24 #include "misc.h"
25 
26 
27 void Password :: setUnencoded ( const string& s )
28 {
29  password = encodedpassword2string ( encodepassword ( s.c_str() ) );
30 }
31 
32 void Password :: setEncoded ( const string& s )
33 {
34  if ( !s.empty () ) {
35  if ( s[0] == 'A' )
36  password = s;
37  else
38  password = encodedpassword2string ( atoi ( s.c_str() ));
39  } else
40  password = s;
41 }
42 
43 
44 void Password :: setInt ( int pwd )
45 {
46  password = encodedpassword2string ( pwd );
47 }
48 
49 bool Password :: operator== ( const Password& p ) const
50 {
51  return p.password == password;
52 }
53 
54 bool Password :: operator!= ( const Password& p ) const
55 {
56  return p.password != password;
57 }
58 
59 
60 int Password :: encodepassword ( const char* pw ) const
61 {
62  if ( !pw )
63  return 0;
64 
65  int len = strlen ( pw );
66 
67  if ( len )
68  return crc32buf( pw, len+1 );
69  else
70  return 0;
71 }
72 
73 string Password :: encodedpassword2string ( int pwd ) const
74 {
75  string s;
76  if ( !pwd )
77  return s;
78 
79  s = "A";
80  if ( pwd > 0 )
81  s+= "A";
82  else
83  s+= "B";
84  s += strrr ( abs (pwd) );
85  return s;
86 }
87 
88 
89 bool Password :: empty () const
90 {
91  return password.empty();
92 }
93 
94 string Password :: toString ( ) const
95 {
96  return password;
97 }
98 
99 
100 void Password::read ( tnstream& stream )
101 {
102  int i = stream.readInt();
103  password = encodedpassword2string ( i );
104 }
105 
106 void Password::write ( tnstream& stream ) const
107 {
108  int i;
109  if ( password[0] == 'A' ) {
110  if ( password[1] == 'A' )
111  i = atoi ( password.substr ( 2 ).c_str() );
112  else
113  i = -atoi ( password.substr ( 2 ).c_str() );
114 
115  stream.writeInt ( i );
116  } else
117  stream.writeInt ( 0 );
118 }
119 
121 {
122  password = "";
123 }
bool operator!=(const Password &p) const
Definition: password.cpp:54
virtual void writeInt(int i)
Writes a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is p...
Definition: basestrm.cpp:363
void write(tnstream &stream) const
Definition: password.cpp:106
int crc32buf(const void *vbuf, int len)
Definition: misc.cpp:135
miscellaneous functions which are used by ASC and all its tools
void read(tnstream &stream)
Definition: password.cpp:100
virtual int readInt(void)
Reads a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is pe...
Definition: basestrm.cpp:284
A class for holding, encoding and comparing passwords.
int encodepassword(const char *pw) const
Definition: password.cpp:60
The interface for all kinds of IO stream.
void setUnencoded(const string &s)
Definition: password.cpp:27
void setEncoded(const string &s)
Definition: password.cpp:32
bool empty() const
Definition: password.cpp:89
char * strrr(int a)
converts a to a string.
Definition: misc.cpp:66
void setInt(int pwd)
Definition: password.cpp:44
int atoi(const std::string &s)
Definition: misc.cpp:152
string encodedpassword2string(int pwd) const
Definition: password.cpp:73
void reset()
Definition: password.cpp:120
string toString() const
Definition: password.cpp:94
bool operator==(const Password &p) const
Definition: password.cpp:49