Files

178 lines
4.7 KiB
C++
Raw Permalink Normal View History

2015-02-15 18:53:04 +03:00
/**
*
* Copyright (c) 2005-2009 Anchorite (TeamX), <anchorite2001@yandex.ru>
2015-02-15 21:33:27 +03:00
* Copyright (c) 2014-2015 Nirran, phobos2077
* Copyright (c) 2015 alexeevdv <mail@alexeevdv.ru>
2015-02-15 18:53:04 +03:00
* Distributed under the GNU GPL v3. For full terms see the file license.txt
*
*/
2015-02-13 16:59:11 +03:00
2015-02-15 18:53:04 +03:00
// C++ standard includes
2015-02-16 00:30:55 +03:00
#include <iostream>
#include <fstream>
#include <algorithm>
2015-02-15 18:53:04 +03:00
// int2ssl includes
2015-02-13 16:59:11 +03:00
#include "Namespace.h"
#include "Utility.h"
2015-02-15 18:53:04 +03:00
// Third party includes
2015-02-13 16:59:11 +03:00
2015-02-16 00:30:55 +03:00
extern std::ifstream g_ifstream;
extern std::ofstream g_ofstream;
2015-02-13 16:59:11 +03:00
CNamespace::CNamespace()
{
}
CNamespace::~CNamespace()
{
}
2015-02-16 00:30:55 +03:00
void CNamespace::Serialize()
2015-02-13 16:59:11 +03:00
{
2015-02-14 14:38:14 +03:00
m_Map.RemoveAll();
2015-02-16 00:30:55 +03:00
m_Order.clear();
2015-02-14 14:38:14 +03:00
2015-02-15 18:28:27 +03:00
uint32_t ulLength;
2015-02-16 00:30:55 +03:00
g_ifstream.read((char*)&ulLength, sizeof(ulLength));
std::reverse((char*)&ulLength, (char*)&ulLength + sizeof(ulLength));
2015-02-14 14:38:14 +03:00
2015-02-16 00:30:55 +03:00
if (!g_ifstream)
2015-02-13 16:59:11 +03:00
{
2015-02-16 00:30:55 +03:00
std::cout << "Error: Unable read length of namespace" << std::endl;
2015-02-15 19:14:18 +03:00
throw std::exception();
2015-02-14 13:39:47 +03:00
}
2015-02-13 16:59:11 +03:00
2015-02-14 14:38:14 +03:00
if (ulLength == 0xFFFFFFFF) return;
2015-02-13 16:59:11 +03:00
2015-02-15 18:28:27 +03:00
uint32_t ulTotalRead = 0;
2015-02-15 18:10:01 +03:00
uint16_t wLengthOfString;
2015-02-15 16:16:45 +03:00
char* lpszNewString;
2015-02-14 14:38:14 +03:00
while(ulTotalRead < ulLength)
{
2015-02-15 16:16:45 +03:00
std::string strNewString;
2015-02-14 14:38:14 +03:00
2015-02-16 00:30:55 +03:00
g_ifstream.read((char*)&wLengthOfString, sizeof(wLengthOfString));
std::reverse((char*)&wLengthOfString, (char*)&wLengthOfString + sizeof(wLengthOfString));
if (!g_ifstream)
2015-02-13 22:31:52 +03:00
{
2015-02-16 00:30:55 +03:00
std::cout << "Error: Unable read length of string" << std::endl;
2015-02-15 19:14:18 +03:00
throw std::exception();
2015-02-14 14:38:14 +03:00
};
if ((wLengthOfString < 2) || (wLengthOfString & 0x0001))
{
2015-02-16 00:30:55 +03:00
std::cout << "Error: Invalid length of string" << std::endl;
2015-02-15 19:14:18 +03:00
throw std::exception();
2015-02-14 13:39:47 +03:00
}
2015-02-13 16:59:11 +03:00
2015-02-15 16:16:45 +03:00
strNewString.resize(wLengthOfString);
lpszNewString = (char*)strNewString.data();
2015-02-14 14:38:14 +03:00
2015-02-16 00:30:55 +03:00
g_ifstream.read(lpszNewString, wLengthOfString);
if (!g_ifstream)
2015-02-13 22:31:52 +03:00
{
2015-02-15 16:16:45 +03:00
strNewString.resize(0);
2015-02-16 00:30:55 +03:00
std::cout << "Error: Unable read string in namespace" << std::endl;
2015-02-15 19:14:18 +03:00
throw std::exception();
2015-02-14 14:38:14 +03:00
}
2015-02-13 22:31:52 +03:00
2015-02-14 14:38:14 +03:00
if ((lpszNewString[wLengthOfString - 1] != '\x00') && (lpszNewString[wLengthOfString - 2] != '\x00'))
{
2015-02-15 16:16:45 +03:00
strNewString.resize(0);
2015-02-16 00:30:55 +03:00
std::cout << "Error: Invalid end of string in namespace" << std::endl;
2015-02-15 19:14:18 +03:00
throw std::exception();
2015-02-14 14:38:14 +03:00
}
2015-02-13 16:59:11 +03:00
2015-02-14 14:38:14 +03:00
std::string tmpString(lpszNewString);
2015-02-16 00:30:55 +03:00
strNewString = tmpString.c_str();
2015-02-13 16:59:11 +03:00
2015-02-14 14:38:14 +03:00
// Convert Nongraphic Characters to Escape sequence
2015-02-15 16:16:45 +03:00
std::string strNonGraph("\\\a\b\f\n\r\t\"");
std::string strEscape("\\abfnrt\"");
2015-02-13 16:59:11 +03:00
2015-02-15 22:55:52 +03:00
for(uint32_t i = 0; i < strNonGraph.length(); i++)
2015-02-14 14:38:14 +03:00
{
2017-10-12 00:29:39 +07:00
strNewString = replace(strNewString, std::string() + strNonGraph[i], std::string("\\") + strEscape[i]);
2015-02-14 14:38:14 +03:00
}
2015-02-13 22:31:52 +03:00
2015-02-14 14:38:14 +03:00
m_Map.SetAt(ulTotalRead + 6, strNewString);
2015-02-15 21:30:47 +03:00
m_Order.push_back(ulTotalRead + 6);
2015-02-13 16:59:11 +03:00
2015-02-14 14:38:14 +03:00
ulTotalRead += (2 + wLengthOfString);
}
2015-02-13 16:59:11 +03:00
2015-02-15 18:28:27 +03:00
uint32_t ulTerminator;
2015-02-13 16:59:11 +03:00
2015-02-16 00:30:55 +03:00
g_ifstream.read((char*)&ulTerminator, sizeof(ulTerminator));
std::reverse((char*)&ulTerminator, (char*)&ulTerminator + sizeof(ulTerminator));
if (!g_ifstream)
2015-02-14 14:38:14 +03:00
{
2015-02-16 00:30:55 +03:00
std::cout << "Error: Unable read terminator of namespace" << std::endl;
2015-02-15 19:14:18 +03:00
throw std::exception();
2015-02-14 14:38:14 +03:00
}
2015-02-13 16:59:11 +03:00
2015-02-14 14:38:14 +03:00
if (ulTerminator != 0xFFFFFFFF)
{
printf("Error: Invalid terminator of namespace\n");
2015-02-15 19:14:18 +03:00
throw std::exception();
2015-02-14 14:38:14 +03:00
}
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
// //For debugging only
2015-02-15 19:05:45 +03:00
// for(int32_t i = 0; i < GetSize(); i++) {
2015-02-14 13:39:47 +03:00
// printf("Offset: 0x%08X (%9d), string: %s\n", m_Order[i], m_Order[i], GetStringByIndex(i));
// }
2015-02-13 16:59:11 +03:00
//
2015-02-14 13:39:47 +03:00
// printf("\n");
2015-02-13 16:59:11 +03:00
}
2015-02-15 19:05:45 +03:00
int32_t CNamespace::GetSize() const
2015-02-13 16:59:11 +03:00
{
2015-02-14 13:39:47 +03:00
return m_Map.GetSize();
2015-02-13 16:59:11 +03:00
}
2015-02-15 19:05:45 +03:00
std::string CNamespace::GetStringByIndex(int32_t nIndex)
2015-02-13 16:59:11 +03:00
{
2015-02-14 23:18:15 +03:00
return (this->operator [] (m_Order[nIndex]));
2015-02-13 16:59:11 +03:00
}
2015-02-15 19:05:45 +03:00
uint32_t CNamespace::GetOffsetByIndex(int32_t nIndex)
2015-02-13 16:59:11 +03:00
{
2015-02-14 23:18:15 +03:00
return m_Order[nIndex];
2015-02-13 16:59:11 +03:00
}
2015-02-16 00:30:55 +03:00
void CNamespace::Dump()
2015-02-13 16:59:11 +03:00
{
2015-02-15 21:30:47 +03:00
if (m_Order.empty())
2015-02-13 22:31:52 +03:00
{
2015-02-16 00:30:55 +03:00
g_ofstream << "Empty" << std::endl;
2015-02-14 13:39:47 +03:00
}
2015-02-13 22:31:52 +03:00
else
{
2015-02-15 21:30:47 +03:00
for(unsigned int i = 0; i < m_Order.size(); i++)
2015-02-13 22:31:52 +03:00
{
2015-02-16 00:30:55 +03:00
g_ofstream << format("0x%08X: \"%s\"", m_Order[i], GetStringByIndex(i).c_str()) << std::endl;
2015-02-14 13:39:47 +03:00
}
2015-02-13 16:59:11 +03:00
2015-02-16 00:30:55 +03:00
g_ofstream << "==================" << std::endl;
g_ofstream << format("%d item(s)\n", m_Order.size()) << std::endl;
2015-02-14 13:39:47 +03:00
}
2015-02-13 16:59:11 +03:00
}
2015-02-15 18:28:27 +03:00
std::string CNamespace::operator [] (uint32_t ulOffset) const
2015-02-13 16:59:11 +03:00
{
2015-02-15 16:16:45 +03:00
std::string strResult;
2015-02-13 16:59:11 +03:00
2015-02-13 22:31:52 +03:00
if (!m_Map.Lookup(ulOffset, strResult))
{
2015-02-14 13:39:47 +03:00
printf("Error: No string at offset 0x%08x\n", ulOffset);
2015-02-15 19:14:18 +03:00
throw std::exception();
2015-02-14 13:39:47 +03:00
}
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
return strResult;
2015-02-13 16:59:11 +03:00
}