More checks added to MD5.

develop
Petr Mrázek 2011-11-03 02:40:49 +01:00
parent dc629bc251
commit a6eea1adfd
3 changed files with 38 additions and 4 deletions

@ -40,6 +40,7 @@ using namespace std;
#include "dfhack/VersionInfoFactory.h"
#include "dfhack/VersionInfo.h"
#include "dfhack/Error.h"
#include <string.h>
using namespace DFHack;
Process::Process(VersionInfoFactory * known_versions)
@ -54,8 +55,11 @@ Process::Process(VersionInfoFactory * known_versions)
my_descriptor = 0;
md5wrapper md5;
uint32_t length;
uint8_t first_kb [1024];
memset(first_kb, 0, sizeof(first_kb));
// get hash of the running DF process
string hash = md5.getHashFromFile(exe_link_name);
string hash = md5.getHashFromFile(exe_link_name, length, (char *) first_kb);
// create linux process, add it to the vector
VersionInfo * vinfo = known_versions->getVersionInfoByMD5(hash);
if(vinfo)
@ -71,6 +75,29 @@ Process::Process(VersionInfoFactory * known_versions)
cerr << "File: " << exe_link_name << endl;
cerr << "MD5: " << hash << endl;
cerr << "working dir: " << wd << endl;
cerr << "length:" << length << endl;
cerr << "1KB hexdump follows:" << endl;
for(int i = 0; i < 64; i++)
{
fprintf(stderr, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
first_kb[i*16],
first_kb[i*16+1],
first_kb[i*16+2],
first_kb[i*16+3],
first_kb[i*16+4],
first_kb[i*16+5],
first_kb[i*16+6],
first_kb[i*16+7],
first_kb[i*16+8],
first_kb[i*16+9],
first_kb[i*16+10],
first_kb[i*16+11],
first_kb[i*16+12],
first_kb[i*16+13],
first_kb[i*16+14],
first_kb[i*16+15]
);
}
free(wd);
}
}

@ -108,12 +108,13 @@ std::string md5wrapper::getHashFromString(std::string text)
* (based on Ronald L. Rivest's code
* from RFC1321 "The MD5 Message-Digest Algorithm")
*/
std::string md5wrapper::getHashFromFile(std::string filename)
std::string md5wrapper::getHashFromFile(std::string filename, uint32_t & length, char * first_kb)
{
FILE *file;
MD5_CTX context;
int len;
int saved = 0;
unsigned char buffer[1024], digest[16];
//open file
@ -121,7 +122,7 @@ std::string md5wrapper::getHashFromFile(std::string filename)
{
return "file unreadable.";
}
length = 0;
//init md5
md5->MD5Init (&context);
@ -130,6 +131,12 @@ std::string md5wrapper::getHashFromFile(std::string filename)
{
errno = 0;
len = fread (buffer, 1, 1024, file);
if(saved < 1024 && first_kb)
{
memcpy(first_kb + saved, buffer, std::min (len, 1024 - saved));
saved += len;
}
length += len;
if(len != 1024)
{
int err = ferror(file);

@ -61,7 +61,7 @@ class md5wrapper
* a file specified in "filename" and
* returns it as string
*/
std::string getHashFromFile(std::string filename);
std::string getHashFromFile(const std::string filename, uint32_t & length, char * first_kb = NULL);
};