Browse code

add MD5 hash generation of file to configreader

Stefan Sayer authored on 27/11/2010 19:07:33
Showing 7 changed files
... ...
@@ -28,8 +28,10 @@
28 28
 #include "AmConfig.h"
29 29
 #include "log.h"
30 30
 #include "AmUtils.h"
31
+#include "md5.h"
31 32
 
32 33
 #include <errno.h>
34
+#include <fstream>
33 35
 
34 36
 #define IS_SPACE(c) ((c == ' ') || (c == '\t'))
35 37
 
... ...
@@ -147,6 +149,33 @@ int  AmConfigReader::loadFile(const string& path)
147 149
   return -1;
148 150
 }
149 151
 
152
+bool AmConfigReader::getMD5(const string& path, string& md5hash, bool lowercase) {
153
+    std::ifstream data_file(path.c_str(), std::ios::in | std::ios::binary);
154
+    if (!data_file) {
155
+      DBG("could not read file '%s'\n", path.c_str());
156
+      return false;
157
+    }
158
+    // that one is clever...
159
+    // (see http://www.gamedev.net/community/forums/topic.asp?topic_id=353162 )
160
+    string file_data((std::istreambuf_iterator<char>(data_file)),
161
+		     std::istreambuf_iterator<char>());
162
+
163
+    if (file_data.empty()) {
164
+      return false;
165
+    }
166
+
167
+    MD5_CTX md5ctx;
168
+    MD5Init(&md5ctx);
169
+    MD5Update(&md5ctx, (unsigned char*)file_data.c_str(), file_data.length());
170
+    unsigned char _md5hash[16];
171
+    MD5Final(_md5hash, &md5ctx);
172
+    md5hash = "";
173
+    for (size_t i=0;i<16;i++) {
174
+      md5hash+=char2hex(_md5hash[i], lowercase);
175
+    }
176
+    return true;
177
+}
178
+
150 179
 bool AmConfigReader::hasParameter(const string& param)
151 180
 {
152 181
   return (keys.find(param) != keys.end());
... ...
@@ -48,6 +48,8 @@ class AmConfigReader
48 48
 
49 49
  public:
50 50
   int  loadFile(const string& path);
51
+  /** get md5 hash of file contents */
52
+  bool getMD5(const string& path, string& md5hash, bool lowercase = true);
51 53
   bool hasParameter(const string& param);
52 54
   const string& getParameter(const string& param, const string& defval = "");
53 55
   unsigned int getParameterInt(const string& param, unsigned int defval = 0);
... ...
@@ -109,8 +109,22 @@ string long2str(long int val)
109 109
 }
110 110
 
111 111
 static char _int2hex_lookup[] = { '0', '1', '2', '3', '4', '5', '6' , '7', '8', '9','A','B','C','D','E','F' };
112
+static char _int2hex_lookup_l[] = { '0', '1', '2', '3', '4', '5', '6' , '7', '8', '9','a','b','c','d','e','f' };
112 113
 
113
-string int2hex(unsigned int val)
114
+string char2hex(unsigned char val, bool lowercase)
115
+{
116
+  string res;
117
+  if (lowercase) {
118
+    res += _int2hex_lookup_l[val >> 4];
119
+    res += _int2hex_lookup_l[val & 0x0f];
120
+  } else {
121
+    res += _int2hex_lookup[val >> 4];
122
+    res += _int2hex_lookup[val & 0x0f];
123
+  }
124
+  return res;
125
+}
126
+
127
+string int2hex(unsigned int val, bool lowercase)
114 128
 {
115 129
   unsigned int digit=0;
116 130
 
... ...
@@ -120,7 +134,8 @@ string int2hex(unsigned int val)
120 134
   for(i=0; i<int(2*sizeof(int)); i++){
121 135
     digit = val >> 4*(2*sizeof(int)-1);
122 136
     val = val << 4;
123
-    buffer[j++] = _int2hex_lookup[(unsigned char)digit];
137
+    buffer[j++] = lowercase ?
138
+      _int2hex_lookup_l[(unsigned char)digit] : _int2hex_lookup[(unsigned char)digit];
124 139
   }
125 140
 
126 141
   return string((char*)buffer);
... ...
@@ -56,9 +56,14 @@ string int2str(int val);
56 56
 string long2str(long int val);
57 57
 
58 58
 /** 
59
+ * Convert a a byte to a string using hexdecimal representation.
60
+ */
61
+string char2hex(unsigned char val, bool lowercase = false);
62
+
63
+/**
59 64
  * Convert an unsigned int to a string using hexdecimal representation. 
60 65
  */
61
-string int2hex(unsigned int val);
66
+string int2hex(unsigned int val, bool lowercase = false);
62 67
 
63 68
 /** 
64 69
  * Convert an unsigned long to a string using hexdecimal representation. 
65 70
similarity index 100%
66 71
rename from core/plug-in/uac_auth/md5.cpp
67 72
rename to core/md5.cpp
68 73
similarity index 100%
69 74
rename from core/plug-in/uac_auth/md5.h
70 75
rename to core/md5.h
71 76
similarity index 100%
72 77
rename from core/plug-in/uac_auth/md5global.h
73 78
rename to core/md5global.h