Files
int2ssl/Namespace.cpp
T

169 lines
4.2 KiB
C++
Raw 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
// 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
CNamespace::CNamespace()
{
2015-02-14 13:39:47 +03:00
m_Map.InitHashTable(128);
2015-02-13 16:59:11 +03:00
}
CNamespace::~CNamespace()
{
}
void CNamespace::Serialize(CArchive& ar)
{
2015-02-14 14:38:14 +03:00
m_Map.RemoveAll();
2015-02-15 21:30:47 +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-14 14:38:14 +03:00
if (ReadMSBULong(ar, ulLength) != 4)
2015-02-13 16:59:11 +03:00
{
2015-02-14 14:38:14 +03:00
printf("Error: Unable read length of namespace\n");
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
if (ReadMSBWord(ar, wLengthOfString) != 2)
2015-02-13 22:31:52 +03:00
{
2015-02-14 14:38:14 +03:00
printf("Error: Unable read length of string\n");
2015-02-15 19:14:18 +03:00
throw std::exception();
2015-02-14 14:38:14 +03:00
};
if ((wLengthOfString < 2) || (wLengthOfString & 0x0001))
{
printf("Error: Invalid length of string\n");
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
if (ar.Read(lpszNewString, wLengthOfString) != wLengthOfString)
2015-02-13 22:31:52 +03:00
{
2015-02-15 16:16:45 +03:00
strNewString.resize(0);
2015-02-14 14:38:14 +03:00
printf("Error: Unable read string in namespace\n");
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-14 14:38:14 +03:00
printf("Error: Invalid end of string in 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 14:38:14 +03:00
std::string tmpString(lpszNewString);
strNewString = tmpString.c_str();//.ReleaseBuffer();
2015-02-13 22:31:52 +03:00
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
{
2015-02-15 16:16:45 +03:00
strNewString = replace(strNewString, "" + strNonGraph[i], "\\" + 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-14 14:38:14 +03:00
if (ReadMSBULong(ar, ulTerminator) != 4)
{
printf("Error: Unable read 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 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
}
void CNamespace::Dump(CArchive& ar)
{
2015-02-15 16:16:45 +03:00
std::string strOutLine;
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-14 13:39:47 +03:00
ar.WriteString("Empty\n");
}
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-15 16:16:45 +03:00
strOutLine = format("0x%08X: \"%s\"\n", m_Order[i], GetStringByIndex(i).c_str());
2015-02-14 13:39:47 +03:00
ar.WriteString(strOutLine);
}
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
ar.WriteString("==================\n");
2015-02-15 21:30:47 +03:00
strOutLine = format("%d item(s)\n", m_Order.size());
2015-02-14 13:39:47 +03:00
ar.WriteString(strOutLine);
}
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
}