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.cpp
Go to the documentation of this file.
1 #include "cmd/unit_generic.h"
3 #include "fileutil.h"
4 #include "vs_globals.h"
5 #include "vsfilesystem.h"
7 
8 //#ifdef _WIN32
9 //#include <winsock.h> // for ntohl
10 //#endif
11 #ifndef PASCAL
12 #define PASCAL
13 #endif
14 #ifndef FAR
15 #define FAR
16 #endif
17 
18 #ifdef CRYPTO
19 #include <crypto++/sha.h>
20 HASHMETHOD FileUtil::Hash;
21 #endif
22 
23 using namespace VSFileSystem;
24 
25 bool FileUtil::use_crypto = false;
26 
27 void FileUtil::WriteSaveFiles( string savestr, string xmlstr, string name )
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 }
51 
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 }
61 
62 int FileUtil::HashStringCompute( std::string buffer, unsigned char *digest )
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 }
95 
96 void FileUtil::displayHash( unsigned char *hash, unsigned int length )
97 {
98  for (unsigned int i = 0; i < length; i++)
99  std::cerr<<hash[i];
100 }
101 
102 int FileUtil::HashCompute( const char *filename, unsigned char *digest, VSFileType type )
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 }
136 
137 //Warning : now takes an absolute path
138 int FileUtil::HashCompare( string filename, unsigned char *hashdigest, VSFileType type )
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 }
177 
178 int FileUtil::HashFileCompute( string filename, unsigned char *hashdigest, VSFileType type )
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 }
201