Files
int2ssl/FalloutScript.cpp
T

282 lines
8.8 KiB
C++
Raw Normal View History

2015-02-13 16:59:11 +03:00
// FalloutScript.cpp : implementation file
//
#include "stdafx.h"
#include "FalloutScript.h"
#include <iostream>
// Constants
//c_strBogusProcedureName = "..............";
//c_strArgumentTemplate = "arg%u";
//c_strGlobalVarTemplate = "GVar%u";
//c_strLocalVarTemplate = "LVar%u";
// CFalloutScript
CFalloutScript::CFalloutScript()
{
}
CFalloutScript::~CFalloutScript()
{
}
void CFalloutScript::Serialize(CArchive& ar)
{
2015-02-13 23:09:21 +03:00
printf(" Read strtup code\n");
m_StartupCode.Serialize(ar);
printf(" Read procedures table\n");
m_ProcTable.Serialize(ar);
printf(" Read namespace\n");
m_Namespace.Serialize(ar);
printf(" Read stringspace\n");
m_Stringspace.Serialize(ar);
COpcode opcode;
printf(" Read tail of startup code\n");
opcode.Expect(ar, COpcode::O_SET_GLOBAL);
// Sections with variable sizes
ar.Flush();
ULONG ullCurrentOffset = ar.GetFile()->GetPosition();
// Load globals
m_GlobalVar.RemoveAll();
m_ExportedVar.RemoveAll();
m_ExportedVarValue.RemoveAll();
m_ExportedProc.RemoveAll();
COpcodeArray HeaderTail;
while(ullCurrentOffset < m_ProcTable.GetOffsetOfProcSection())
2015-02-13 16:59:11 +03:00
{
2015-02-13 23:09:21 +03:00
opcode.Serialize(ar);
ullCurrentOffset += opcode.GetSize();
HeaderTail.Add(opcode);
}
2015-02-13 16:59:11 +03:00
2015-02-13 23:09:21 +03:00
// Jump to 'start' procedure
printf(" Check \"Jump to \'start\' procedure\" / \"Jump to end of statup code\"\n");
2015-02-13 16:59:11 +03:00
2015-02-13 23:09:21 +03:00
if (!HeaderTail.IsEmpty())
{
INT_PTR nIndexOfStart = GetIndexOfProc("start");
ULONG ulStartProcAddress = (nIndexOfStart != -1) ? m_ProcTable[nIndexOfStart].m_ulBodyOffset : 18;
2015-02-13 16:59:11 +03:00
2015-02-13 23:09:21 +03:00
opcode = HeaderTail.at(HeaderTail.GetUpperBound());
2015-02-13 16:59:11 +03:00
2015-02-13 23:09:21 +03:00
if (opcode.GetOperator() == COpcode::O_JMP)
2015-02-13 22:31:52 +03:00
{
2015-02-13 23:09:21 +03:00
HeaderTail.RemoveAt(HeaderTail.GetUpperBound());
2015-02-13 16:59:11 +03:00
2015-02-13 23:09:21 +03:00
if (HeaderTail.IsEmpty())
2015-02-13 22:31:52 +03:00
{
2015-02-13 23:09:21 +03:00
printf("\n");
printf("Warning: Omitted address of jump\n");
printf("\n");
}
2015-02-13 22:31:52 +03:00
else
{
2015-02-13 23:09:21 +03:00
opcode = HeaderTail.at(HeaderTail.GetUpperBound());
2015-02-13 16:59:11 +03:00
2015-02-13 23:09:21 +03:00
if (opcode.GetOperator() == COpcode::O_INTOP)
{
HeaderTail.RemoveAt(HeaderTail.GetUpperBound());
2015-02-13 16:59:11 +03:00
2015-02-13 23:09:21 +03:00
if (opcode.GetArgument() != ulStartProcAddress)
{
printf("\n");
printf("Warning: Invalid jump address\n");
printf("\n");
}
}
else
{
printf("\n");
printf("Warning: Invalid opcode for jump addres\n");
printf("\n");
}
}
}
else
2015-02-13 22:31:52 +03:00
{
2015-02-13 23:09:21 +03:00
printf("\n");
printf("Warning: Omitted \"Jump to \'start\' procedure\" / \"Jump to end of statup code\"\n");
printf("\n");
}
}
2015-02-13 16:59:11 +03:00
2015-02-13 23:09:21 +03:00
// # of argument to 'start' procedure
printf(" Check \"# of argument to \'start\' procedure\"\n");
CString strNumOfArgsWarning("Warning: Omitted \"# of argument to \'start\' procedure\"\n");
if (!HeaderTail.IsEmpty()) {
opcode = HeaderTail.at(HeaderTail.GetUpperBound());
if (opcode.GetOperator() == COpcode::O_CRITICAL_DONE) {
HeaderTail.RemoveAt(HeaderTail.GetUpperBound());
if (!HeaderTail.IsEmpty()) {
opcode = HeaderTail.at(HeaderTail.GetUpperBound());
if (opcode.HasArgument()) {
HeaderTail.RemoveAt(HeaderTail.GetUpperBound());
}
else {
printf("\n");
printf("%s", strNumOfArgsWarning.c_str());
printf("\n");
}
}
else {
printf("\n");
printf("%s", strNumOfArgsWarning.c_str());
printf("\n");
}
}
else {
printf("\n");
printf("Warning: Omitted expected opcode 0x8003. Opcode with # of arguments will be not analysed\n");
printf("\n");
}
}
else {
printf("\n");
printf("%s", strNumOfArgsWarning.c_str());
printf("\n");
}
// Extract 'Export var' section
printf(" Extract \"Export var\" section\n");
ExtractCodeElements(HeaderTail, m_ExportedVar, COpcode::O_EXPORT_VAR, 2, "Error: Malformed \"Export var\" section\n", &CFalloutScript::CheckExportVarCode);
// Extract 'Set exported var values' section
printf(" Extract \"Set exported var values\" section\n");
ExtractCodeElements(HeaderTail, m_ExportedVarValue, COpcode::O_STORE_EXTERNAL, 3, "Error: Malformed \"Set exported var values\" section\n", &CFalloutScript::CheckSetExportedVarValueCode);
// Extract 'Export procedures' section
printf(" Extract \"Export procedures\" section\n");
ExtractCodeElements(HeaderTail, m_ExportedProc, COpcode::O_EXPORT_PROC, 3, "Error: Malformed \"Export procedures\" section\n", &CFalloutScript::CheckExportProcCode);
// Global variables
printf(" Extract \"Global variables\" section\n");
for(INT_PTR i = 0; i < HeaderTail.GetSize(); i++) {
WORD wGlobalVarOperator = HeaderTail.at(i).GetOperator();
if ((wGlobalVarOperator != COpcode::O_STRINGOP) &&
(wGlobalVarOperator != COpcode::O_FLOATOP) &&
(wGlobalVarOperator != COpcode::O_INTOP)) {
printf("Error: Malformed \"Global variables\" section\n");
AfxThrowUserException();
}
m_GlobalVar.Add(HeaderTail.at(i));
}
// Procedures bodyes
printf(" Read procedure\'s bodies\n");
2015-02-14 13:39:47 +03:00
m_ProcBodies.RemoveAll(); // Destroy old data
2015-02-13 23:09:21 +03:00
m_Conditions.RemoveAll();
m_ProcBodies.SetSize(m_ProcTable.GetSize());
m_Conditions.SetSize(m_ProcTable.GetSize());
CNode node;
for(INT_PTR i = 0; i < m_ProcTable.GetSize(); i++)
{
printf(" Procedure: %d\r", i);
ULONG ulOffset = m_ProcTable[i].m_ulBodyOffset;
ULONG ulSize = m_ProcTable.GetSizeOfProc(i);
ar.Flush();
ar.GetFile()->Seek(ulOffset, CFile::begin);
while(ulOffset < m_ProcTable[i].m_ulBodyOffset + ulSize)
{
node.m_Opcode.Serialize(ar);
node.m_ulOffset = ulOffset;
m_ProcBodies.at(i).Add(node);
ulOffset += node.m_Opcode.GetSize();
}
}
2015-02-13 16:59:11 +03:00
}
void CFalloutScript::ExtractCodeElements(COpcodeArray& Source, COpcodeArray& Destination, WORD wDelimeter, int nSizeOfCodeItem, LPCTSTR lpszErrorMessage, BOOL (CFalloutScript::*pCheckFunc)(WORD, INT_PTR))
{
2015-02-14 13:39:47 +03:00
INT_PTR i = 0;
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
for(; i < Source.GetSize(); i++) {
2015-02-13 16:59:11 +03:00
if (Source.at(i).GetOperator() == wDelimeter) {
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
if (i < Source.GetSize()) {
if (i < nSizeOfCodeItem - 1) {
printf(lpszErrorMessage);
AfxThrowUserException();
}
2015-02-13 16:59:11 +03:00
while(Source.at(i).GetOperator() == wDelimeter) {
2015-02-14 13:39:47 +03:00
for(INT_PTR j = 0; j < nSizeOfCodeItem - 1; j++) {
2015-02-13 16:59:11 +03:00
if (!((this->*pCheckFunc)(Source.at(i - nSizeOfCodeItem + 1 + j).GetOperator(), j))) {
2015-02-14 13:39:47 +03:00
printf(lpszErrorMessage);
AfxThrowUserException();
}
}
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
for(INT_PTR j = 0; j < nSizeOfCodeItem - 1; j++) {
2015-02-13 16:59:11 +03:00
Destination.Add(Source.at(i - nSizeOfCodeItem + 1));
2015-02-14 13:39:47 +03:00
Source.RemoveAt(i - nSizeOfCodeItem + 1);
}
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
Source.RemoveAt(i - nSizeOfCodeItem + 1); // Delimeter
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
if (i > Source.GetUpperBound()) {
break;
}
}
}
2015-02-13 16:59:11 +03:00
}
BOOL CFalloutScript::CheckExportVarCode(WORD wOperator, INT_PTR nIndex)
{
2015-02-14 13:39:47 +03:00
return (nIndex == 0) && (wOperator == COpcode::O_STRINGOP);
2015-02-13 16:59:11 +03:00
}
BOOL CFalloutScript::CheckSetExportedVarValueCode(WORD wOperator, INT_PTR nIndex)
{
2015-02-14 13:39:47 +03:00
switch(nIndex) {
case 0:
return (wOperator == COpcode::O_STRINGOP) ||
(wOperator == COpcode::O_FLOATOP) ||
(wOperator == COpcode::O_INTOP);
case 1:
return (wOperator == COpcode::O_STRINGOP);
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
default:
return FALSE;
}
2015-02-13 16:59:11 +03:00
}
BOOL CFalloutScript::CheckExportProcCode(WORD wOperator, INT_PTR nIndex)
{
2015-02-14 13:39:47 +03:00
return ((nIndex == 0) || (nIndex == 1)) && (wOperator == COpcode::O_INTOP);
2015-02-13 16:59:11 +03:00
}
bool CFalloutScript::ArgNeedParens( const CNode& node, const CNode& argument, CFalloutScript::Assoc assoc)
{
2015-02-14 13:39:47 +03:00
return (argument.IsInfix()
&& ((GetPriority(argument.m_Opcode.GetOperator()) != GetPriority(node.m_Opcode.GetOperator()))
|| (GetAssociation(node.m_Opcode.GetOperator()) != assoc)));
2015-02-13 16:59:11 +03:00
}