Files
int2ssl/ProcTable.cpp
T

315 lines
8.0 KiB
C++
Raw Normal View History

2015-02-13 16:59:11 +03:00
#include "stdafx.h"
#include "ProcTable.h"
#include "Utility.h"
#include "ObjectAttributes.h"
#include <iostream>
CProcDescriptor::CProcDescriptor() :
2015-02-14 13:39:47 +03:00
m_ulNameOffset(0),
m_ulType(0),
m_ulTime(0),
m_ulExpressionOffset(0),
m_ulBodyOffset(0),
m_ulNumArgs(0)
2015-02-13 16:59:11 +03:00
{
}
CProcDescriptor::CProcDescriptor(const CProcDescriptor& Item) :
2015-02-14 13:39:47 +03:00
m_ulNameOffset(Item.m_ulNameOffset),
m_ulType(Item.m_ulType),
m_ulTime(Item.m_ulTime),
m_ulExpressionOffset(Item.m_ulExpressionOffset),
m_ulBodyOffset(Item.m_ulBodyOffset),
m_ulNumArgs(Item.m_ulNumArgs)
2015-02-13 16:59:11 +03:00
{
}
CProcDescriptor::~CProcDescriptor()
{
}
CProcDescriptor& CProcDescriptor::operator = (const CProcDescriptor& Item)
{
2015-02-14 14:38:14 +03:00
if (&Item != this)
{
2015-02-14 13:39:47 +03:00
m_ulNameOffset = Item.m_ulNameOffset;
m_ulType = Item.m_ulType;
m_ulTime = Item.m_ulTime;
m_ulExpressionOffset = Item.m_ulExpressionOffset;
m_ulBodyOffset = Item.m_ulBodyOffset;
m_ulNumArgs = Item.m_ulNumArgs;
}
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
return (*this);
2015-02-13 16:59:11 +03:00
}
void CProcDescriptor::Serialize(CArchive& ar)
{
2015-02-15 18:20:01 +03:00
uint32_t uiTotalRead;
2015-02-14 14:38:14 +03:00
uiTotalRead = ReadMSBULong(ar, m_ulNameOffset);
uiTotalRead += ReadMSBULong(ar, m_ulType);
uiTotalRead += ReadMSBULong(ar, m_ulTime);
uiTotalRead += ReadMSBULong(ar, m_ulExpressionOffset);
uiTotalRead += ReadMSBULong(ar, m_ulBodyOffset);
uiTotalRead += ReadMSBULong(ar, m_ulNumArgs);
// printf("=================\n");
// printf("uiTotalRead = %u\n", uiTotalRead);
// printf("m_ulNameOffset = 0x%08X\n", m_ulNameOffset);
// printf("m_ulType = 0x%08X\n", m_ulType);
// printf("m_ulTime = 0x%08X\n", m_ulTime);
// printf("m_ulExpressionOffset = 0x%08X\n", m_ulExpressionOffset);
// printf("m_ulBodyOffset = 0x%08X\n", m_ulBodyOffset);
// printf("m_ulNumArgs = 0x%08X\n", m_ulNumArgs);
if (uiTotalRead != (sizeof(ULONG) * 6))
2015-02-13 16:59:11 +03:00
{
2015-02-14 14:38:14 +03:00
printf("Error: Unable read procedure descriptor\n");
AfxThrowUserException();
2015-02-14 13:39:47 +03:00
}
2015-02-13 16:59:11 +03:00
}
void CProcDescriptor::Dump(CArchive& ar)
{
2015-02-15 16:16:45 +03:00
std::string strOutLine;
std::string strType;
2015-02-13 16:59:11 +03:00
2015-02-13 22:31:52 +03:00
if (m_ulType != 0x00000000)
{
2015-02-14 13:39:47 +03:00
strType = "( ";
2015-02-13 16:59:11 +03:00
2015-02-13 22:31:52 +03:00
if (m_ulType & P_TIMED)
{
2015-02-14 13:39:47 +03:00
strType += "timed ";
}
2015-02-13 16:59:11 +03:00
2015-02-13 22:31:52 +03:00
if (m_ulType & P_CONDITIONAL)
{
2015-02-14 13:39:47 +03:00
strType += "conditional ";
}
2015-02-13 16:59:11 +03:00
2015-02-13 22:31:52 +03:00
if (m_ulType & P_IMPORT)
{
2015-02-14 13:39:47 +03:00
strType += "import ";
}
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
if (m_ulType & P_EXPORT) {
strType += "export ";
}
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
if (m_ulType & P_CRITICAL) {
strType += "critical ";
}
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
strType += ")";
}
2015-02-14 14:38:14 +03:00
else
{
2015-02-14 13:39:47 +03:00
strType = "No special types";
}
2015-02-13 16:59:11 +03:00
2015-02-15 16:16:45 +03:00
strOutLine = format("Name offset: 0x%08X\n", m_ulNameOffset);
2015-02-14 13:39:47 +03:00
ar.WriteString(strOutLine);
2015-02-15 16:16:45 +03:00
strOutLine = format("Type: 0x%08X // %s\n", m_ulType, strType.c_str());
2015-02-14 13:39:47 +03:00
ar.WriteString(strOutLine);
2015-02-15 16:16:45 +03:00
strOutLine = format("Time: 0x%08X // %d\n", m_ulTime, m_ulTime);
2015-02-14 13:39:47 +03:00
ar.WriteString(strOutLine);
2015-02-15 16:16:45 +03:00
strOutLine = format("Expression offset: 0x%08X\n", m_ulExpressionOffset);
2015-02-14 13:39:47 +03:00
ar.WriteString(strOutLine);
2015-02-15 16:16:45 +03:00
strOutLine = format("Body offset: 0x%08X\n", m_ulBodyOffset);
2015-02-14 13:39:47 +03:00
ar.WriteString(strOutLine);
2015-02-15 16:16:45 +03:00
strOutLine = format("Number of args: 0x%08X // %d\n", m_ulNumArgs, m_ulNumArgs);
2015-02-14 13:39:47 +03:00
ar.WriteString(strOutLine);
2015-02-13 16:59:11 +03:00
}
// CProcTable
CProcTable::CProcTable()
{
}
CProcTable::~CProcTable()
{
}
struct ProcBodyOffset {
2015-02-14 13:39:47 +03:00
ULONG m_ulProcIndex;
ULONG m_ulBodyOffset;
2015-02-13 16:59:11 +03:00
};
int compareProcBodyOffsets(const void* elem0, const void* elem1)
{
2015-02-14 13:39:47 +03:00
ProcBodyOffset offset0 = *((ProcBodyOffset*)elem0);
ProcBodyOffset offset1 = *((ProcBodyOffset*)elem1);
2015-02-13 16:59:11 +03:00
2015-02-14 14:38:14 +03:00
if (offset0.m_ulBodyOffset < offset1.m_ulBodyOffset)
{
2015-02-14 13:39:47 +03:00
return -1;
}
2015-02-14 14:38:14 +03:00
else if (offset0.m_ulBodyOffset > offset1.m_ulBodyOffset)
{
2015-02-14 13:39:47 +03:00
return 1;
}
2015-02-14 14:38:14 +03:00
else
{
if (offset0.m_ulProcIndex < offset1.m_ulProcIndex)
{
2015-02-14 13:39:47 +03:00
return -1;
}
2015-02-14 14:38:14 +03:00
else if (offset0.m_ulProcIndex > offset1.m_ulProcIndex)
{
2015-02-14 13:39:47 +03:00
return 1;
}
2015-02-14 14:38:14 +03:00
else
{
2015-02-14 13:39:47 +03:00
return 0;
}
}
2015-02-13 16:59:11 +03:00
}
void CProcTable::Serialize(CArchive& ar)
{
2015-02-14 14:38:14 +03:00
m_Table.RemoveAll();
m_ProcSize.RemoveAll();
ULONG ulSizeOfTable;
ULONG ulRead;
ar.Flush();
ULONG ulSizeOfFile = ULONG(ar.GetFile()->GetLength());
ulRead = ReadMSBULong(ar, ulSizeOfTable);
if (ulRead != sizeof(ulSizeOfTable))
2015-02-13 16:59:11 +03:00
{
2015-02-14 14:38:14 +03:00
printf("Error: Unable read size of procedures table\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
m_Table.SetSize(ulSizeOfTable);
m_ProcSize.SetSize(ulSizeOfTable);
2015-02-13 16:59:11 +03:00
2015-02-14 14:38:14 +03:00
ULONG ulIndexOfProcOffset = 0;
ProcBodyOffset* pOffsets = new ProcBodyOffset[ulSizeOfTable + 1];
2015-02-13 16:59:11 +03:00
2015-02-14 14:38:14 +03:00
if (pOffsets == NULL)
{
// AfxThrowMemoryException();
throw 1;
}
2015-02-13 16:59:11 +03:00
2015-02-14 14:38:14 +03:00
for(ULONG i = 0; i < ulSizeOfTable; i++)
{
// printf("======== %u =========\n", i);
2015-02-14 23:18:15 +03:00
m_Table[i].Serialize(ar);
m_ProcSize[i] = 0; // Initialize size of procedure
2015-02-14 14:38:14 +03:00
2015-02-14 23:18:15 +03:00
if (!(m_Table[i].m_ulType & P_IMPORT))
2015-02-13 22:31:52 +03:00
{
2015-02-14 23:18:15 +03:00
if ((m_Table[i].m_ulBodyOffset != 0) && (m_Table[i].m_ulBodyOffset != ulSizeOfFile))
2015-02-14 14:38:14 +03:00
{
pOffsets[ulIndexOfProcOffset].m_ulProcIndex = i;
2015-02-14 23:18:15 +03:00
pOffsets[ulIndexOfProcOffset].m_ulBodyOffset = m_Table[i].m_ulBodyOffset;
2015-02-14 14:38:14 +03:00
ulIndexOfProcOffset++;
}
else
{
2015-02-14 23:18:15 +03:00
m_Table[i].m_ulType |= P_NOTIMPLEMENTED;
2015-02-14 14:38:14 +03:00
}
}
}
pOffsets[ulIndexOfProcOffset].m_ulProcIndex = ulIndexOfProcOffset + 1;
pOffsets[ulIndexOfProcOffset].m_ulBodyOffset = ulSizeOfFile;
qsort(pOffsets, ulIndexOfProcOffset, sizeof(ProcBodyOffset), compareProcBodyOffsets);
// Sizes of procedures
for(ULONG i = 0; i < ulIndexOfProcOffset; i++)
{
if (pOffsets[i + 1].m_ulBodyOffset >= pOffsets[i].m_ulBodyOffset)
{
2015-02-14 23:18:15 +03:00
m_ProcSize[pOffsets[i].m_ulProcIndex] = pOffsets[i + 1].m_ulBodyOffset - pOffsets[i].m_ulBodyOffset;
2015-02-14 14:38:14 +03:00
}
else
{
printf("Error: Procedures are not sorted in ascending address order\n");
2015-02-14 13:39:47 +03:00
AfxThrowUserException();
}
}
2015-02-14 14:38:14 +03:00
delete [] pOffsets;
for(ULONG i = 0; i < ulSizeOfTable; i++)
{
2015-02-14 23:18:15 +03:00
if (m_Table[i].m_ulBodyOffset != 0)
2015-02-14 14:38:14 +03:00
{
2015-02-14 23:18:15 +03:00
m_ulOffsetOfProcSection = m_Table[i].m_ulBodyOffset;
2015-02-14 14:38:14 +03:00
break;
}
}
// // For debugging only
// printf("Total: %d\n", m_Table.GetSize());
// for(i = 0; i < ulSizeOfTable; i++)
// {
2015-02-14 23:18:15 +03:00
// printf("Offset: 0x%08X, size: 0x%08X\n", m_Table[i].m_ulBodyOffset, m_ProcSize[i]);
2015-02-14 14:38:14 +03:00
// }
// printf("m_ulOffsetOfProcSection: 0x%08X\n", m_ulOffsetOfProcSection);
2015-02-13 16:59:11 +03:00
}
INT_PTR CProcTable::GetSize()
{
2015-02-14 13:39:47 +03:00
return m_Table.GetSize();
2015-02-13 16:59:11 +03:00
}
ULONG CProcTable::GetSizeOfProc(INT_PTR nIndex)
{
2015-02-14 14:38:14 +03:00
if ((nIndex < 0) || (nIndex > m_ProcSize.GetUpperBound()))
{
2015-02-14 13:39:47 +03:00
printf("Warning: Invalid index of procedure (%d). Exception will be thrown\n", nIndex);
AfxThrowUserException();
}
2015-02-13 16:59:11 +03:00
2015-02-14 23:18:15 +03:00
return m_ProcSize[nIndex];
2015-02-13 16:59:11 +03:00
}
ULONG CProcTable::GetOffsetOfProcSection()
{
2015-02-14 13:39:47 +03:00
return m_ulOffsetOfProcSection;
2015-02-13 16:59:11 +03:00
}
void CProcTable::Dump(CArchive& ar)
{
2015-02-15 16:16:45 +03:00
std::string strOutLine;
2015-02-13 16:59:11 +03:00
2015-02-13 22:31:52 +03:00
for(unsigned int i = 0; i < m_Table.GetSize(); i++)
{
2015-02-15 16:16:45 +03:00
strOutLine = format("======== Procedure %d ========\n", i);
2015-02-13 22:31:52 +03:00
2015-02-14 13:39:47 +03:00
ar.WriteString(strOutLine);
2015-02-14 23:18:15 +03:00
m_Table[i].Dump(ar);
2015-02-14 13:39:47 +03:00
ar.WriteString("\n");
}
2015-02-13 16:59:11 +03:00
}
CProcDescriptor& CProcTable::operator [] (INT_PTR nIndex)
{
2015-02-14 14:38:14 +03:00
if ((nIndex < 0) || (nIndex > m_ProcSize.GetUpperBound()))
{
2015-02-14 13:39:47 +03:00
printf("Warning: Invalid index of procedure (%d). Exception will be thrown\n", nIndex);
AfxThrowUserException();
}
2015-02-13 16:59:11 +03:00
2015-02-14 23:18:15 +03:00
return m_Table[nIndex];
2015-02-13 16:59:11 +03:00
}