#include "stdafx.h"
#include "DakosmuBytes.h"
#include "../../Util/libjpeg/jpeglib.h"
#include "Util.h"
#include <Shlwapi.h>
#pragma comment(lib, "Shlwapi.lib")
#pragma comment(lib, "jpeg.lib")
#define pOpen ((FILE*(*)(const char *,const char*))0x009D0040)
#define pSeek ((int*(*)(FILE*,long,int))0x009D6A53)
#define pRead ((DWORD*(*)(void*,DWORD,DWORD,FILE*))0x009CFF5F)
void* DakosmuJpegBuffer;
DWORD DakosmuJpegBufferSize;
void DakosmuDec()
{
SetCompleteHook(0xE9, 0x004FC7AC, &DakosmuOpenJpegBuffer1);
SetCompleteHook(0xE9, 0x004FCB85, &DakosmuOpenJpegBuffer2);
SetCompleteHook(0xE9, 0x004FCCBA, &DakosmuOpenTga);
SetCompleteHook(0xE9, 0x0054C28C, &DakosmuOpenBmd);
SetCompleteHook(0xE9, 0x005D68FF, &DakosmuOpenAtt);
SetCompleteHook(0xE9, 0x005D6ECF, &DakosmuOpenMap);
SetCompleteHook(0xE9, 0x005F34A8, &DakosmuOpenObj);
SetCompleteHook(0xFF, 0x004FC6C7, &DakosmuOpenFile);
SetCompleteHook(0xFF, 0x004FCC3E, &DakosmuOpenFile);
SetCompleteHook(0xFF, 0x0054C21D, &DakosmuOpenFile);
SetCompleteHook(0xFF, 0x005D6826, &DakosmuOpenFile);
SetCompleteHook(0xFF, 0x005D6E64, &DakosmuOpenFile);
SetCompleteHook(0xFF, 0x005F3402, &DakosmuOpenFile);
}
FILE* DakosmuOpenFile(const char* path, const char* f)
{
char encpath[MAX_PATH] = { 0 };
wsprintf(encpath, "%s.enc", path);
if (PathFileExists(encpath) != 0)
{
return pOpen(encpath, f);
}
return pOpen(path, f);
}
int DakosmuCheckFile(FILE* hFile)
{
BYTE Buffer[0x18] = { 0 };
pSeek(hFile, 0, SEEK_SET);
pRead(Buffer, 1, sizeof(Buffer), hFile);
if (Buffer[0] != 'E' || Buffer[1] != 'N' || Buffer[2] != 'C')
{
return 0;
}
if (Buffer[3] == 1)
{
return 2;
}
return 1;
}
BOOL DakosmuDecryptFile(FILE* hFile, BYTE* Buffer, int Size, int Origin, int Offset)
{
if (DakosmuCheckFile(hFile) != 0)
{
pSeek(hFile, 0x18, SEEK_SET);
pRead(Buffer, 1, Size, hFile);
for (int n = 0; n < (Size - 0x18); n++)
{
Buffer[n] += (BYTE)(0xA0 ^ HIBYTE(n));
Buffer[n] ^= (BYTE)(0xAB ^ LOBYTE(n));
}
return 1;
}
pSeek(hFile, Offset, SEEK_SET);
pRead(Buffer, 1, Size, hFile);
return 0;
}
__declspec(naked) void DakosmuOpenJpegBuffer1()
{
static DWORD Address1 = 0x004FC7C2;
static DWORD Address2 = 0x004FC7C2;
static DWORD Address3 = 0x009CFF5F;
static DWORD Address4 = 0x009D6A53;
static DWORD Address5 = 0x009D6CF0;
static DWORD Address6 = 0x009D00C5;
_asm
{
Push 2
Push 0x18
Mov Ecx, Dword Ptr Ss : [Ebp - 0x2C4]
Push Ecx
Call[Address4]
Add Esp, 0x0C
Mov Ecx, Dword Ptr Ss : [Ebp - 0x2C4]
Push Ecx
Call[Address5]
Add Esp, 0x04
Mov DakosmuJpegBufferSize, Eax
Push 0
Push 0x18
Mov Ecx, Dword Ptr Ss : [Ebp - 0x2C4]
Push Ecx
Call[Address4]
Add Esp, 0x0C
Push DakosmuJpegBufferSize
Call[Address6]
Add Esp, 0x04
Push 0x18
Push 0
Mov DakosmuJpegBuffer, Eax
Push DakosmuJpegBufferSize
Push DakosmuJpegBuffer
Mov Ecx, Dword Ptr Ss : [Ebp - 0x2C4]
Push Ecx
Call[DakosmuDecryptFile]
Add Esp, 0x14
Test Eax, Eax
Je EXIT
Mov Edx, DakosmuJpegBufferSize
Sub Edx, 0x18
Push Edx
Mov Eax, DakosmuJpegBuffer
Add Eax, 0x18
Push Eax
Lea Ecx, Dword Ptr Ss : [Ebp - 0x2A4]
Push Ecx
Call[jpeg_mem_src]
Add Esp, 0x0C
Jmp Address1
EXIT :
Push DakosmuJpegBufferSize
Push DakosmuJpegBuffer
Lea Ecx, Dword Ptr Ss : [Ebp - 0x2A4]
Push Ecx
Call[jpeg_mem_src]
Add Esp, 0x0C
Jmp Address2
}
}
__declspec(naked) void DakosmuOpenJpegBuffer2()
{
static DWORD Address1 = 0x004FCB8C;
static DWORD Address2 = 0x009CF11A;
_asm
{
Push DakosmuJpegBuffer
Call[Address2]
Add Esp, 0x04
Mov DakosmuJpegBuffer, 0
Mov Byte Ptr Ss : [Ebp - 0x325] , 1
Jmp Address1
}
}
__declspec(naked) void DakosmuOpenTga()
{
static DWORD Address1 = 0x004FCCD0;
static DWORD Address2 = 0x004FCCD0;
_asm
{
Push 0
Push 0x18
Mov Edx, Dword Ptr Ss : [Ebp - 0x34]
Push Edx
Mov Eax, Dword Ptr Ss : [Ebp - 0x44]
Push Eax
Mov Ecx, Dword Ptr Ss : [Ebp - 0x3C]
Push Ecx
Call[DakosmuDecryptFile]
Add Esp, 0x14
Jmp Address1
}
}
__declspec(naked) void DakosmuOpenBmd()
{
static DWORD Address1 = 0x0054C2A2;
static DWORD Address2 = 0x0054C2A2;
_asm
{
Push 0
Push 0x18
Mov Edx, Dword Ptr Ss : [Ebp - 0x64]
Push Edx
Mov Eax, Dword Ptr Ss : [Ebp - 0x68]
Push Eax
Mov Ecx, Dword Ptr Ss : [Ebp - 0x18]
Push Ecx
Call[DakosmuDecryptFile]
Add Esp, 0x14
Jmp Address1
}
}
__declspec(naked) void DakosmuOpenAtt()
{
static DWORD Address1 = 0x005D6915;
static DWORD Address2 = 0x005D6915;
_asm
{
Push 0
Push 0x18
Mov Edx, Dword Ptr Ss : [Ebp - 0x14]
Push Edx
Mov Eax, Dword Ptr Ss : [Ebp - 0x0C]
Push Eax
Mov Ecx, Dword Ptr Ss : [Ebp - 0x8]
Push Ecx
Call[DakosmuDecryptFile]
Add Esp, 0x14
Test Eax, Eax
Je EXIT
Mov Edx, Dword Ptr Ss : [Ebp - 0x14]
Sub Edx, 0x18
Mov Dword Ptr Ss : [Ebp - 0x14] , Edx
Jmp Address1
EXIT :
Jmp Address2
}
}
__declspec(naked) void DakosmuOpenMap()
{
static DWORD Address1 = 0x005D6EE5;
static DWORD Address2 = 0x005D6EE5;
_asm
{
Push 0
Push 0x18
Mov Edx, Dword Ptr Ss : [Ebp - 0x14]
Push Edx
Mov Eax, Dword Ptr Ss : [Ebp - 0x0C]
Push Eax
Mov Ecx, Dword Ptr Ss : [Ebp - 0x8]
Push Ecx
Call[DakosmuDecryptFile]
Add Esp, 0x14
Test Eax, Eax
Je EXIT
Mov Edx, Dword Ptr Ss : [Ebp - 0x14]
Sub Edx, 0x18
Mov Dword Ptr Ss : [Ebp - 0x14] , Edx
Jmp Address1
EXIT :
Jmp Address2
}
}
__declspec(naked) void DakosmuOpenObj()
{
static DWORD Address1 = 0x005F34BE;
static DWORD Address2 = 0x005F34BE;
_asm
{
Push 0
Push 0x18
Mov Edx, Dword Ptr Ss : [Ebp - 0x18]
Push Edx
Mov Eax, Dword Ptr Ss : [Ebp - 0x10]
Push Eax
Mov Ecx, Dword Ptr Ss : [Ebp - 0x8]
Push Ecx
Call[DakosmuDecryptFile]
Add Esp, 0x14
Test Eax, Eax
Je EXIT
Mov Edx, Dword Ptr Ss : [Ebp - 0x18]
Sub Edx, 0x18
Mov Dword Ptr Ss : [Ebp - 0x18] , Edx
Jmp Address1
EXIT :
Jmp Address2
}
}