forked from kenygia/MooseEdit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SanityHash.cpp
112 lines (96 loc) · 3.2 KB
/
SanityHash.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "SanityHash.h"
#include <cmath>
void SanityHash::addFile(const unsigned char *file, unsigned long fileSize) {
const unsigned long BLOCK_SIZE = 64;
unsigned char block[64] = {0};
unsigned long bytesLeft = fileSize;
unsigned long bytesToRead;
const unsigned char *current = file;
while (bytesLeft > 0) {
bytesToRead = std::min(BLOCK_SIZE, bytesLeft);
memcpy(block, current, bytesToRead);
current += bytesToRead;
bytesLeft -= bytesToRead;
md5_update(&ctx, block, BLOCK_SIZE);
}
}
unsigned char *SanityHash::compute() {
if (!freed) {
freed = true;
md5_finish(&ctx, output);
md5_free( &ctx );
}
return output;
}
template <typename T>
void SanityHash::copyVec(std::vector<T>& vec, std::vector<T *>& ptrVec) {
for (int i=0; i<vec.size(); ++i) {
ptrVec.push_back(&vec[i]);
}
}
const static std::string hexmap = "0123456789abcdef";
std::string SanityHash::generateHexString() {
std::string result = "";
unsigned char *computed = this->compute();
for (int i=0; i<COMPUTE_SIZE; ++i) {
unsigned char byte = computed[i];
result += hexmap[byte/0x10];
result += hexmap[byte%0x10];
}
return result;
}
void SanityHash::generateLsb(std::vector<TAG_LSB *>& sanityTags, std::vector<LsbObject *>& sanityObjects) {
this->sanityTags.resize(3);
TAG_LSB& sanityTag = this->sanityTags[0];
sanityTag.index = 0;
std::string sanityTagText = "Sanity";
sanityTag.tag = new char[sanityTagText.length() + 1];
strcpy(sanityTag.tag, sanityTagText.c_str());
sanityTag.tagLength = sanityTagText.length();
TAG_LSB& rootTag = this->sanityTags[1];
rootTag.index = 1;
std::string rootTagText = "root";
rootTag.tag = new char[rootTagText.length() + 1];
strcpy(rootTag.tag, rootTagText.c_str());
rootTag.tagLength = rootTagText.length();
TAG_LSB& hashTag = this->sanityTags[2];
hashTag.index = 2;
std::string hashTagText = "Hash";
hashTag.tag = new char[hashTagText.length() + 1];
strcpy(hashTag.tag, hashTagText.c_str());
hashTag.tagLength = hashTagText.length();
this->sanityObjects.resize(4);
this->sanityObjects[0] = LsbObject(true, sanityTag.index, sanityTag.tag, 0, 0);
LsbObject& sanityEntity = this->sanityObjects[0];
this->sanityObjects[1] = LsbObject(true, rootTag.index, rootTag.tag, 0, &sanityEntity);
LsbObject& rootNode = this->sanityObjects[1];
this->sanityObjects[2] = LsbObject(true, sanityTag.index, sanityTag.tag, 0, &rootNode);
LsbObject& sanityNode = this->sanityObjects[2];
this->sanityObjects[3] = LsbObject(false, hashTag.index, hashTag.tag, 0x17, &sanityNode);
LsbObject& hashNode = this->sanityObjects[3];
sanityEntity.setEntityId(1);
std::string md5Text = this->generateHexString();
hashNode.setData(md5Text.c_str(), md5Text.length() + 1);
sanityEntity.addChild(&rootNode);
rootNode.addChild(&sanityNode);
sanityNode.addChild(&hashNode);
copyVec(this->sanityTags, sanityTags);
sanityObjects.push_back(&sanityEntity);
}
SanityHash::~SanityHash() {
if (!freed) {
md5_finish( &ctx, output );
md5_free( &ctx );
}
for (int i=0; i<sanityTags.size(); ++i) {
delete[] sanityTags[i].tag;
}
for (int i=0; i<sanityObjects.size(); ++i) {
LsbObject &object = sanityObjects[i];
object.getChildren().clear();
}
}
SanityHash::SanityHash() {
md5_init( &ctx );
md5_starts( &ctx );
}