Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FileUtil Namespace Reference

Functions

vector< string > GetSaveFromBuffer (NetBuffer &buffer)
 
void WriteSaveFiles (string savestr, string xmlstr, string name)
 
void displayHash (unsigned char *hash, unsigned int length)
 
int HashCompare (string filename, unsigned char *hashdigest, VSFileType type)
 
int HashFileCompute (string filename, unsigned char *hashdigest, VSFileType type)
 
int HashCompute (const char *filename, unsigned char *digest, VSFileType type)
 
int HashStringCompute (std::string buffer, unsigned char *digest)
 

Variables

bool use_crypto = false
 

Function Documentation

void FileUtil::displayHash ( unsigned char *  hash,
unsigned int  length 
)

Definition at line 96 of file fileutil.cpp.

References i.

Referenced by HashCompare().

97 {
98  for (unsigned int i = 0; i < length; i++)
99  std::cerr<<hash[i];
100 }
vector< string > FileUtil::GetSaveFromBuffer ( NetBuffer buffer)

Definition at line 52 of file fileutil.cpp.

References NetBuffer::getString().

Referenced by AccountServer::writeSave().

53 {
54  vector< string >saves;
55  //Extract the length of save file
56  saves.push_back( buffer.getString() );
57  saves.push_back( buffer.getString() );
58 
59  return saves;
60 }
int FileUtil::HashCompare ( string  filename,
unsigned char *  hashdigest,
VSFileType  type 
)

Definition at line 138 of file fileutil.cpp.

References displayHash(), HashCompute(), HASHMETHOD, and use_crypto.

Referenced by NetClient::loginAccept().

139 {
140 #ifdef CRYPTO
141  if (use_crypto) {
142  HASHMETHOD Hash;
143  //string full_univ_path = VSFileSystem::datadir+filename;
144  unsigned char *local_digest = new unsigned char[Hash.DigestSize()];
145  int ret;
146  //ret=HashCompute( full_univ_path.c_str(), local_digest);
147  ret = HashCompute( filename.c_str(), local_digest, type );
148  //If the file does not exist or if hashes are !=
149  if (ret) {
150  delete[] local_digest;
151  return 0;
152  }
153  if ( memcmp( hashdigest, local_digest, Hash.DigestSize() ) ) {
154  cerr<<"HashDigest does not match : '";
155  displayHash( hashdigest, Hash.DigestSize() );
156  cerr<<"' != '";
157  displayHash( local_digest, Hash.DigestSize() );
158  cerr<<"' for file "<<filename<<endl;
159  delete[] local_digest;
160  return 0;
161  }
162  cerr<<"HashDigest MATCH : '";
163  displayHash( hashdigest, Hash.DigestSize() );
164  cerr<<"' == '";
165  displayHash( local_digest, Hash.DigestSize() );
166  cerr<<"' for file "<<filename<<endl;
167 
168  delete[] local_digest;
169  return 1;
170  } else {
171  return 1;
172  }
173 #else
174  return 1;
175 #endif
176 }
int FileUtil::HashCompute ( const char *  filename,
unsigned char *  digest,
VSFileType  type 
)

Definition at line 102 of file fileutil.cpp.

References buffer, VSFileSystem::VSFile::Close(), ENOENT, f, HASHMETHOD, VSFileSystem::Ok, VSFileSystem::VSFile::OpenReadOnly(), VSFileSystem::VSFile::Read(), use_crypto, and VSExit().

Referenced by HashCompare(), and HashFileCompute().

103 {
104 #ifdef CRYPTO
105  if (use_crypto) {
106  HASHMETHOD Hash;
107  VSFile f;
108  VSError err = f.OpenReadOnly( filename, type );
109  if (err > Ok) {
110  if (errno == ENOENT) {
111  //Return 1 if file does not exists
112  return 1;
113  } else {
114  cerr<<"!!! ERROR = couldn't compute hash digest on "<<filename<<" file !!!"<<endl;
115  VSExit( 1 );
116  //return -1;
117  }
118  }
119  const int block_size = Hash.OptimalBlockSize();
120  unsigned char *buffer = new unsigned char[block_size];
121  int nb = 0;
122  while ( ( nb = f.Read( buffer, block_size ) ) > 0 )
123  Hash.Update( buffer, nb );
124  Hash.Final( digest );
125  delete[] buffer;
126  f.Close();
127 
128  return 0;
129  } else {
130  return 0;
131  }
132 #else
133  return 0;
134 #endif
135 }
int FileUtil::HashFileCompute ( string  filename,
unsigned char *  hashdigest,
VSFileType  type 
)

Definition at line 178 of file fileutil.cpp.

References HashCompute(), HASHMETHOD, use_crypto, and VSExit().

179 {
180 #ifdef CRYPTO
181  if (use_crypto) {
182  HASHMETHOD Hash;
183  //string fulluniv = VSFileSystem::datadir+filename;
184  int ret;
185  if ( ( ret = HashCompute( filename.c_str(), hashdigest, type ) ) < 0 || ret ) {
186  cerr<<"!!! ERROR = couldn't get "<<filename<<" HashDigest (not found or error) !!!"<<endl;
187  if (ret)
188  VSExit( 1 );
189  cerr<<"-- FILE HASH : "<<filename<<" = "<<hashdigest<<" --"<<endl;
190  } else {
191  cerr<<"-- FILE HASH : "<<filename<<" = "<<hashdigest<<" --"<<endl;
192  }
193  return ret;
194  } else {
195  return 0;
196  }
197 #else
198  return 0;
199 #endif
200 }
int FileUtil::HashStringCompute ( std::string  buffer,
unsigned char *  digest 
)

Definition at line 62 of file fileutil.cpp.

References HASHMETHOD, VsnetOSS::memcpy(), and use_crypto.

63 {
64 #ifdef CRYPTO
65  if (use_crypto) {
66  HASHMETHOD Hash;
67 
68  const int block_size = Hash.OptimalBlockSize();
69  unsigned char *hashbuf = new unsigned char[block_size];
70  const char *buf = buffer.c_str();
71  int nb = 0;
72  unsigned int offset = 0;
73  while ( offset < buffer.length() ) {
74  memcpy( hashbuf, buf+offset, block_size );
75  offset += block_size;
76  Hash.Update( hashbuf, block_size );
77  }
78  //Here offset is bigger than buffer length so we go back offset times
79  offset -= block_size;
80  if ( ( nb = (buffer.length()-offset) ) ) {
81  memcpy( hashbuf, buf+offset, nb );
82  Hash.Update( hashbuf, nb );
83  }
84  Hash.Final( digest );
85  delete[] hashbuf;
86 
87  return 0;
88  } else {
89  return 0;
90  }
91 #else
92  return 0;
93 #endif
94 }
void FileUtil::WriteSaveFiles ( string  savestr,
string  xmlstr,
string  name 
)

Definition at line 27 of file fileutil.cpp.

References VSFileSystem::AccountFile, VSFileSystem::VSFile::Close(), f, VSFileSystem::Ok, VSFileSystem::VSFile::OpenCreateWrite(), VSExit(), and VSFileSystem::VSFile::Write().

Referenced by AccountServer::writeSave().

28 {
29  string savefile;
30 
31  //Write the save file
32  savefile = name+".save";
33  VSFile f;
34  VSError err = f.OpenCreateWrite( savefile, AccountFile );
35  if (err > Ok) {
36  std::cout<<"Error opening save file "<<savefile<<std::endl;
37  VSExit( 1 );
38  }
39  f.Write( savestr );
40  f.Close();
41  //Write the XML file
42  savefile = name+".xml";
43  err = f.OpenCreateWrite( savefile, AccountFile );
44  if (err > Ok) {
45  std::cout<<"Error opening save file "<<savefile<<std::endl;
46  VSExit( 1 );
47  }
48  f.Write( xmlstr );
49  f.Close();
50 }

Variable Documentation