1 // ==============================================================
2 //	This file is part of Glest Shared Library (www.glest.org)
3 //
4 //	Copyright (C) 2001-2008 Marti�o Figueroa
5 //
6 //	You can redistribute this code and/or modify it under
7 //	the terms of the GNU General Public License as published
8 //	by the Free Software Foundation; either version 2 of the
9 //	License, or (at your option) any later version
10 // ==============================================================
11 
12 #include "checksum.h"
13 
14 #include <cassert>
15 #include <stdexcept>
16 
17 #include "util.h"
18 #include "leak_dumper.h"
19 
20 using namespace std;
21 
22 namespace Shared{ namespace Util{
23 
24 // =====================================================
25 //	class Checksum
26 // =====================================================
27 
Checksum()28 Checksum::Checksum(){
29 	sum= 0;
30 	r= 55665;
31 	c1= 52845;
32 	c2= 22719;
33 }
34 
addByte(int8 value)35 void Checksum::addByte(int8 value){
36 	int32 cipher= (value ^ (r >> 8));
37 
38 	r= (cipher + r) * c1 + c2;
39 	sum+= cipher;
40 }
41 
addString(const string & value)42 void Checksum::addString(const string &value){
43 	for(int i= 0; i<value.size(); ++i){
44 		addByte(value[i]);
45 	}
46 }
47 
addFile(const string & path)48 void Checksum::addFile(const string &path){
49 
50 	FILE* file= fopen(path.c_str(), "rb");
51 
52 	if(file!=NULL){
53 
54 		addString(lastFile(path));
55 
56 		while(!feof(file)){
57 			int8 byte= 0;
58 
59 			fread(&byte, 1, 1, file);
60 			addByte(byte);
61 		}
62 	}
63 	else
64 	{
65 		throw runtime_error("Can not open file: " + path);
66 	}
67 	fclose(file);
68 }
69 
70 }}//end namespace
71