forked from falltergeist/int2ssl
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Namespace.cpp
177 lines (141 loc) · 4.66 KB
/
Namespace.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
/**
*
* Copyright (c) 2005-2009 Anchorite (TeamX), <anchorite2001@yandex.ru>
* Copyright (c) 2014-2015 Nirran, phobos2077
* Copyright (c) 2015 alexeevdv <mail@alexeevdv.ru>
* Distributed under the GNU GPL v3. For full terms see the file license.txt
*
*/
// C++ standard includes
#include <iostream>
#include <fstream>
#include <algorithm>
// int2ssl includes
#include "Namespace.h"
#include "Utility.h"
// Third party includes
extern std::ifstream g_ifstream;
extern std::ofstream g_ofstream;
CNamespace::CNamespace()
{
}
CNamespace::~CNamespace()
{
}
void CNamespace::Serialize()
{
m_Map.RemoveAll();
m_Order.clear();
uint32_t ulLength;
g_ifstream.read((char*)&ulLength, sizeof(ulLength));
std::reverse((char*)&ulLength, (char*)&ulLength + sizeof(ulLength));
if (!g_ifstream)
{
std::cout << "Error: Unable read length of namespace" << std::endl;
throw std::exception();
}
if (ulLength == 0xFFFFFFFF) return;
uint32_t ulTotalRead = 0;
uint16_t wLengthOfString;
char* lpszNewString;
while(ulTotalRead < ulLength)
{
std::string strNewString;
g_ifstream.read((char*)&wLengthOfString, sizeof(wLengthOfString));
std::reverse((char*)&wLengthOfString, (char*)&wLengthOfString + sizeof(wLengthOfString));
if (!g_ifstream)
{
std::cout << "Error: Unable read length of string" << std::endl;
throw std::exception();
};
if ((wLengthOfString < 2) || (wLengthOfString & 0x0001))
{
std::cout << "Error: Invalid length of string" << std::endl;
throw std::exception();
}
strNewString.resize(wLengthOfString);
lpszNewString = (char*)strNewString.data();
g_ifstream.read(lpszNewString, wLengthOfString);
if (!g_ifstream)
{
strNewString.resize(0);
std::cout << "Error: Unable read string in namespace" << std::endl;
throw std::exception();
}
if ((lpszNewString[wLengthOfString - 1] != '\x00') && (lpszNewString[wLengthOfString - 2] != '\x00'))
{
strNewString.resize(0);
std::cout << "Error: Invalid end of string in namespace" << std::endl;
throw std::exception();
}
std::string tmpString(lpszNewString);
strNewString = tmpString.c_str();
// Convert Nongraphic Characters to Escape sequence
std::string strNonGraph("\\\a\b\f\n\r\t\"");
std::string strEscape("\\abfnrt\"");
for(uint32_t i = 0; i < strNonGraph.length(); i++)
{
strNewString = replace(strNewString, std::string() + strNonGraph[i], std::string("\\") + strEscape[i]);
}
m_Map.SetAt(ulTotalRead + 6, strNewString);
m_Order.push_back(ulTotalRead + 6);
ulTotalRead += (2 + wLengthOfString);
}
uint32_t ulTerminator;
g_ifstream.read((char*)&ulTerminator, sizeof(ulTerminator));
std::reverse((char*)&ulTerminator, (char*)&ulTerminator + sizeof(ulTerminator));
if (!g_ifstream)
{
std::cout << "Error: Unable read terminator of namespace" << std::endl;
throw std::exception();
}
if (ulTerminator != 0xFFFFFFFF)
{
printf("Error: Invalid terminator of namespace\n");
throw std::exception();
}
// //For debugging only
// for(int32_t i = 0; i < GetSize(); i++) {
// printf("Offset: 0x%08X (%9d), string: %s\n", m_Order[i], m_Order[i], GetStringByIndex(i));
// }
//
// printf("\n");
}
int32_t CNamespace::GetSize() const
{
return m_Map.GetSize();
}
std::string CNamespace::GetStringByIndex(int32_t nIndex)
{
return (this->operator [] (m_Order[nIndex]));
}
uint32_t CNamespace::GetOffsetByIndex(int32_t nIndex)
{
return m_Order[nIndex];
}
void CNamespace::Dump()
{
if (m_Order.empty())
{
g_ofstream << "Empty" << std::endl;
}
else
{
for(unsigned int i = 0; i < m_Order.size(); i++)
{
g_ofstream << format("0x%08X: \"%s\"", m_Order[i], GetStringByIndex(i).c_str()) << std::endl;
}
g_ofstream << "==================" << std::endl;
g_ofstream << format("%d item(s)\n", m_Order.size()) << std::endl;
}
}
std::string CNamespace::operator [] (uint32_t ulOffset) const
{
std::string strResult;
if (!m_Map.Lookup(ulOffset, strResult))
{
printf("Error: No string at offset 0x%08x\n", ulOffset);
throw std::exception();
}
return strResult;
}