-
Notifications
You must be signed in to change notification settings - Fork 2
/
RuneHMMInfo.cpp
178 lines (151 loc) · 3.55 KB
/
RuneHMMInfo.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "RuneHMMInfo.h"
CRuneHMMPool::CRuneHMMPool()
{
m_pCryptTable = NULL;
m_pBase = NULL;
m_RuneHMMPoolList = NULL;
}
CRuneHMMPool::~CRuneHMMPool()
{
Close();
}
//hash算法对应的加密字符串词单
void CRuneHMMPool::prepareCryptTable()
{
unsigned long seed = 0x00100001, index1 = 0, index2 = 0, i;
for(index1 = 0; index1 < 0x100; index1++)
{
for(index2 = index1, i = 0; i < 5; i++, index2 += 0x100)
{
unsigned long temp1, temp2;
seed = (seed * 125 + 3) % 0x2AAAAB;
temp1 = (seed & 0xFFFF) << 0x10;
seed = (seed * 125 + 3) % 0x2AAAAB;
temp2 = (seed & 0xFFFF);
if(index2 > 1280)
{
printf("[prepareCryptTable]index2=%u.\n", (unsigned int)index2);
}
m_pCryptTable[index2] = (char)(temp1 | temp2);
}
}
}
void CRuneHMMPool::Close()
{
m_pCryptTable = NULL;
m_pBase = NULL;
}
char* CRuneHMMPool::GetCryptTable()
{
return m_pCryptTable;
}
size_t CRuneHMMPool::Init(int nPoolCount, char* pData)
{
Close();
m_pBase = pData;
size_t nPos = 0;
m_pCryptTable = pData;
memset(m_pCryptTable, 0, 1280);
prepareCryptTable();
nPos += 1280;
//printf("[CRuneHMMPool::Init]nPos=%d.\n", nPos);
m_RuneHMMPoolList = (_RuneHMMInfo* )&pData[nPos];
nPos += sizeof(_RuneHMMInfo) * nPoolCount;
for(int i = 0; i < nPoolCount; i++)
{
m_RuneHMMPoolList[i].Init();
m_RuneHMMPoolList[i].Set_Index(i);
//printf("[CRuneHMMPool::Init](0)nPos=%d.\n", nPos);
}
m_nPoolCount = nPoolCount;
m_nCurrIndex = 0;
return nPos;
}
size_t CRuneHMMPool::Load(int nPoolCount, char* pData)
{
Close();
m_pBase = pData;
size_t nPos = 0;
m_pCryptTable = pData;
nPos += 1280;
printf("[CNodePool::Load]nPos=%d.\n", nPos);
m_RuneHMMPoolList = (_RuneHMMInfo* )&pData[nPos];
nPos += sizeof(_RuneHMMInfo) * nPoolCount;
m_nPoolCount = nPoolCount;
m_nCurrIndex = 0;
return nPos;
}
_RuneHMMInfo* CRuneHMMPool::Create()
{
if(NULL == m_RuneHMMPoolList)
{
return NULL;
}
if(m_nCurrIndex >= m_nPoolCount - 1)
{
m_nCurrIndex = 0;
}
if(m_RuneHMMPoolList[m_nCurrIndex].m_cUsed == 0)
{
//printf("[CRuneHMMPool::Create]m_nCurrIndex=%d, nIndex=%d.\n", m_nCurrIndex, m_RuneHMMPoolList[m_nCurrIndex].Get_Index());
m_RuneHMMPoolList[m_nCurrIndex].m_cUsed = 1;
return &m_RuneHMMPoolList[m_nCurrIndex++];
}
else
{
//循环寻找空位
for(int i = m_nCurrIndex + 1; i < m_nPoolCount; i++)
{
if(m_RuneHMMPoolList[i].m_cUsed == 0)
{
m_nCurrIndex = i + 1;
if(m_nCurrIndex == m_nPoolCount)
{
m_nCurrIndex = 0;
}
m_RuneHMMPoolList[i].m_cUsed = 1;
return &m_RuneHMMPoolList[i];
}
}
printf("[CRuneHMMPool::Create]m_nCurrIndex=%d,m_nPoolCount=%d.\n", m_nCurrIndex, m_nPoolCount);
int nStart = 0;
//没找到,再重头开始找
for(int i = nStart; i < m_nCurrIndex - 1; i++)
{
if(m_RuneHMMPoolList[i].m_cUsed == 0)
{
m_nCurrIndex = i + 1;
m_RuneHMMPoolList[i].m_cUsed = 1;
return &m_RuneHMMPoolList[i];
}
}
//已经没有空位
return NULL;
}
}
int CRuneHMMPool::Get_Node_Offset(_RuneHMMInfo* pRuneHMMInfo)
{
int nOffset = 0;
if(NULL != pRuneHMMInfo)
{
nOffset = (int)((char* )pRuneHMMInfo - m_pBase);
}
return nOffset;
}
_RuneHMMInfo* CRuneHMMPool::Get_NodeOffset_Ptr(int nOffset)
{
return (_RuneHMMInfo* )(m_pBase + nOffset);
}
bool CRuneHMMPool::Delete(_RuneHMMInfo* pRuneHMMInfo)
{
if(NULL == m_RuneHMMPoolList || NULL == pRuneHMMInfo)
{
return false;
}
if(-1 == pRuneHMMInfo->Get_Index())
{
return false;
}
m_RuneHMMPoolList[pRuneHMMInfo->Get_Index()].m_cUsed = 0;
return true;
}