Files
int2ssl/Namespace.cpp
T

167 lines
3.9 KiB
C++
Raw Normal View History

2015-02-13 16:59:11 +03:00
// Namespace.cpp : implementation file
//
#include "stdafx.h"
#include "Namespace.h"
#include "Utility.h"
#include "Hacks/CString.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CNamespace
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();
m_Order.RemoveAll();
ULONG ulLength;
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");
AfxThrowUserException();
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-14 14:38:14 +03:00
ULONG ulTotalRead = 0;
WORD wLengthOfString;
LPTSTR lpszNewString;
while(ulTotalRead < ulLength)
{
CString strNewString;
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");
AfxThrowUserException();
};
if ((wLengthOfString < 2) || (wLengthOfString & 0x0001))
{
printf("Error: Invalid length of string\n");
2015-02-14 13:39:47 +03:00
AfxThrowUserException();
}
2015-02-13 16:59:11 +03:00
2015-02-14 14:38:14 +03:00
lpszNewString = strNewString.GetBuffer(wLengthOfString);
if (ar.Read(lpszNewString, wLengthOfString) != wLengthOfString)
2015-02-13 22:31:52 +03:00
{
2015-02-14 14:38:14 +03:00
strNewString.ReleaseBufferSetLength(0);
printf("Error: Unable read string in namespace\n");
AfxThrowUserException();
}
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'))
{
strNewString.ReleaseBufferSetLength(0);
printf("Error: Invalid end of string in namespace\n");
AfxThrowUserException();
}
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
CString strNonGraph("\\\a\b\f\n\r\t\"");
CString strEscape("\\abfnrt\"");
2015-02-13 16:59:11 +03:00
2015-02-14 14:38:14 +03:00
for(int i = 0; i < strNonGraph.GetLength(); i++)
{
strNewString.Replace(CString(strNonGraph[i]), CString("\\" + strEscape[i]));
}
2015-02-13 22:31:52 +03:00
2015-02-14 14:38:14 +03:00
m_Map.SetAt(ulTotalRead + 6, strNewString);
m_Order.Add(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-14 14:38:14 +03:00
ULONG 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");
AfxThrowUserException();
}
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");
AfxThrowUserException();
}
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
// //For debugging only
// for(INT_PTR i = 0; i < GetSize(); i++) {
// 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
}
INT_PTR CNamespace::GetSize() const
{
2015-02-14 13:39:47 +03:00
return m_Map.GetSize();
2015-02-13 16:59:11 +03:00
}
CString CNamespace::GetStringByIndex(INT_PTR nIndex)
{
2015-02-14 23:18:15 +03:00
return (this->operator [] (m_Order[nIndex]));
2015-02-13 16:59:11 +03:00
}
ULONG CNamespace::GetOffsetByIndex(INT_PTR nIndex)
{
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-14 13:39:47 +03:00
CString strOutLine;
2015-02-13 16:59:11 +03:00
2015-02-13 22:31:52 +03:00
if (m_Order.IsEmpty())
{
2015-02-14 13:39:47 +03:00
ar.WriteString("Empty\n");
}
2015-02-13 22:31:52 +03:00
else
{
for(unsigned int i = 0; i < m_Order.GetSize(); i++)
{
2015-02-14 23:18:15 +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");
strOutLine.Format("%d item(s)\n", m_Order.GetSize());
ar.WriteString(strOutLine);
}
2015-02-13 16:59:11 +03:00
}
CString CNamespace::operator [] (ULONG ulOffset) const
{
2015-02-14 13:39:47 +03:00
CString 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);
AfxThrowUserException();
}
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
}