Files
int2ssl/ProcTable.cpp
T

313 lines
8.4 KiB
C++
Raw Normal View History

2015-02-13 16:59:11 +03:00
// ProcTable.cpp : implementation file
//
#include "stdafx.h"
#include "ProcTable.h"
#include "Utility.h"
#include "ObjectAttributes.h"
#include "Hacks/CString.h"
#include <iostream>
// CProcDescriptor
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 13:39:47 +03:00
if (&Item != this) {
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)
{
//if (ar.IsStoring()) {
if (false)
{
2015-02-14 13:39:47 +03:00
// ASSERT(FALSE);
}
else {
UINT uiTotalRead;
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +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);
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
// 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);
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
if (uiTotalRead != (sizeof(ULONG) * 6)) {
printf("Error: Unable read procedure descriptor\n");
AfxThrowUserException();
}
}
2015-02-13 16:59:11 +03:00
}
void CProcDescriptor::Dump(CArchive& ar)
{
2015-02-14 13:39:47 +03:00
CString strOutLine;
CString 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 += ")";
}
else {
strType = "No special types";
}
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
strOutLine.Format("Name offset: 0x%08X\n", m_ulNameOffset);
ar.WriteString(strOutLine);
2015-02-13 16:59:11 +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);
strOutLine.Format("Time: 0x%08X // %d\n", m_ulTime, m_ulTime);
ar.WriteString(strOutLine);
strOutLine.Format("Expression offset: 0x%08X\n", m_ulExpressionOffset);
ar.WriteString(strOutLine);
strOutLine.Format("Body offset: 0x%08X\n", m_ulBodyOffset);
ar.WriteString(strOutLine);
strOutLine.Format("Number of args: 0x%08X // %d\n", m_ulNumArgs, m_ulNumArgs);
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 13:39:47 +03:00
if (offset0.m_ulBodyOffset < offset1.m_ulBodyOffset) {
return -1;
}
else if (offset0.m_ulBodyOffset > offset1.m_ulBodyOffset) {
return 1;
}
else {
if (offset0.m_ulProcIndex < offset1.m_ulProcIndex) {
return -1;
}
else if (offset0.m_ulProcIndex > offset1.m_ulProcIndex) {
return 1;
}
else {
return 0;
}
}
2015-02-13 16:59:11 +03:00
}
void CProcTable::Serialize(CArchive& ar)
{
//if (ar.IsStoring()) {
if(false)
{
2015-02-14 13:39:47 +03:00
// ASSERT(FALSE);
}
else {
m_Table.RemoveAll();
m_ProcSize.RemoveAll();
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
ULONG ulSizeOfTable;
ULONG ulRead;
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
ar.Flush();
ULONG ulSizeOfFile = ULONG(ar.GetFile()->GetLength());
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
ulRead = ReadMSBULong(ar, ulSizeOfTable);
2015-02-13 16:59:11 +03:00
2015-02-13 22:31:52 +03:00
if (ulRead != sizeof(ulSizeOfTable))
{
2015-02-14 13:39:47 +03:00
printf("Error: Unable read size of procedures table\n");
AfxThrowUserException();
}
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
m_Table.SetSize(ulSizeOfTable);
m_ProcSize.SetSize(ulSizeOfTable);
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
ULONG ulIndexOfProcOffset = 0;
ProcBodyOffset* pOffsets = new ProcBodyOffset[ulSizeOfTable + 1];
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
if (pOffsets == NULL) {
// AfxThrowMemoryException();
2015-02-13 16:59:11 +03:00
throw 1;
2015-02-14 13:39:47 +03:00
}
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
for(ULONG i = 0; i < ulSizeOfTable; i++) {
// printf("======== %u =========\n", i);
2015-02-13 16:59:11 +03:00
m_Table.at(i).Serialize(ar);
2015-02-14 13:39:47 +03:00
m_ProcSize.at(i) = 0; // Initialize size of procedure
2015-02-13 16:59:11 +03:00
if (!(m_Table.at(i).m_ulType & P_IMPORT)) {
if ((m_Table.at(i).m_ulBodyOffset != 0) && (m_Table.at(i).m_ulBodyOffset != ulSizeOfFile)) {
2015-02-14 13:39:47 +03:00
pOffsets[ulIndexOfProcOffset].m_ulProcIndex = i;
2015-02-13 16:59:11 +03:00
pOffsets[ulIndexOfProcOffset].m_ulBodyOffset = m_Table.at(i).m_ulBodyOffset;
2015-02-14 13:39:47 +03:00
ulIndexOfProcOffset++;
}
else {
2015-02-13 16:59:11 +03:00
m_Table.at(i).m_ulType |= P_NOTIMPLEMENTED;
2015-02-14 13:39:47 +03:00
}
}
}
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
pOffsets[ulIndexOfProcOffset].m_ulProcIndex = ulIndexOfProcOffset + 1;
pOffsets[ulIndexOfProcOffset].m_ulBodyOffset = ulSizeOfFile;
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
qsort(pOffsets, ulIndexOfProcOffset, sizeof(ProcBodyOffset), compareProcBodyOffsets);
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
// Sizes of procedures
for(ULONG i = 0; i < ulIndexOfProcOffset; i++) {
if (pOffsets[i + 1].m_ulBodyOffset >= pOffsets[i].m_ulBodyOffset) {
2015-02-13 16:59:11 +03:00
m_ProcSize.at(pOffsets[i].m_ulProcIndex) = pOffsets[i + 1].m_ulBodyOffset - pOffsets[i].m_ulBodyOffset;
2015-02-14 13:39:47 +03:00
}
else {
printf("Error: Procedures are not sorted in ascending address order\n");
AfxThrowUserException();
}
}
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
delete [] pOffsets;
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
for(ULONG i = 0; i < ulSizeOfTable; i++) {
2015-02-13 16:59:11 +03:00
if (m_Table.at(i).m_ulBodyOffset != 0) {
m_ulOffsetOfProcSection = m_Table.at(i).m_ulBodyOffset;
2015-02-14 13:39:47 +03:00
break;
}
}
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
// // For debugging only
// printf("Total: %d\n", m_Table.GetSize());
2015-02-13 16:59:11 +03:00
//
2015-02-14 13:39:47 +03:00
// for(i = 0; i < ulSizeOfTable; i++) {
// printf("Offset: 0x%08X, size: 0x%08X\n", m_Table.at(i).m_ulBodyOffset, m_ProcSize.at(i));
// }
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +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 13:39:47 +03:00
if ((nIndex < 0) || (nIndex > m_ProcSize.GetUpperBound())) {
printf("Warning: Invalid index of procedure (%d). Exception will be thrown\n", nIndex);
AfxThrowUserException();
}
2015-02-13 16:59:11 +03:00
return m_ProcSize.at(nIndex);
}
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-14 13:39:47 +03:00
CString 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-14 13:39:47 +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-13 16:59:11 +03:00
m_Table.at(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 13:39:47 +03:00
if ((nIndex < 0) || (nIndex > m_ProcSize.GetUpperBound())) {
printf("Warning: Invalid index of procedure (%d). Exception will be thrown\n", nIndex);
AfxThrowUserException();
}
2015-02-13 16:59:11 +03:00
return m_Table.at(nIndex);
}