port 2D step 5a: EterPack/EterPackManager + EterBase file units against the real 40250 packs
- Mirror copies: EterPack (EterPack, EterPackManager, EterPackCursor, CSHybridCrypt policy, Inline.h) and EterBase logic units tea/lzo/Timer/Stl/Random, with PORT-tagged width fixes (LONG index fields, static_assert(sizeof(TEterPackIndex) == 192), tea_word = Win32 unsigned long). - TEA pack keys are extracted from the reference EterPack.cpp at configure time into build/.../EterPackKeys.generated.h; nothing key-bearing is tracked. - Platform EterBase: FileBase (stdio, case-insensitive fallback), MappedFile (mmap / MapViewOfFile), CRC32, Debug, Utils StringPath/StringLowers. - Win32Crt: CreateDirectory/DeleteFile/_access/MAKEFOURCC; Win32MinMax.h min/max (force-included on MinGW, whose windows.h omits them in C++). - tests/port_eterpack_test: registers pack/Index like PackInitialize and checks 119 registrations / 103 packs + root / 54891 entries / 52609 paths / 2282 overrides with first-registered-wins, and that exactly the 4 short-layout SECURITY files fail to load. - 2R count corrected (136 -> 119 registrations, 103 distinct packs + root). Not runtime-reachable yet (asset_io pack backend is step 5b), so port-map statuses stay TODO. port_gate: macos/android/ios/windows PASS. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,107 +1,272 @@
|
||||
// Platform skeleton for EterBase/MappedFile.h (40250 EterBase/MappedFile.cpp), generated by platform_stub.py.
|
||||
// Every MT_PLATFORM_STUB() body is unimplemented: replace it with the platform implementation.
|
||||
// Platform implementation of EterBase/MappedFile.h (40250 EterBase/MappedFile.cpp). Everything but Map/Unmap
|
||||
// is the 40250 code; the view is a POSIX mmap (CreateFileMapping/MapViewOfFile on Windows) of the CFileBase
|
||||
// stream, aligned down to the allocation granularity exactly as 40250 does.
|
||||
#include "EterBase/StdAfx.h"
|
||||
#include "EterBase/MappedFile.h"
|
||||
#include "EterBase/Debug.h"
|
||||
|
||||
#include "../PlatformStub.h"
|
||||
#include <cstdio>
|
||||
|
||||
CMappedFile::CMappedFile()
|
||||
#if defined(_WIN32)
|
||||
#include <io.h>
|
||||
#else
|
||||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
static DWORD allocation_granularity()
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
SYSTEM_INFO SysInfo;
|
||||
GetSystemInfo(&SysInfo);
|
||||
return SysInfo.dwAllocationGranularity;
|
||||
#else
|
||||
return static_cast<DWORD>(sysconf(_SC_PAGESIZE));
|
||||
#endif
|
||||
}
|
||||
|
||||
CMappedFile::CMappedFile() :
|
||||
m_hFM(NULL),
|
||||
m_lpMapData(NULL),
|
||||
m_dataOffset(0),
|
||||
m_mapSize(0),
|
||||
m_seekPosition(0),
|
||||
m_pLZObj(NULL),
|
||||
m_pbBufLinkData(NULL),
|
||||
m_dwBufLinkSize(0),
|
||||
m_pbAppendResultDataBlock(NULL),
|
||||
m_dwAppendResultDataSize(0)
|
||||
{
|
||||
MT_PLATFORM_STUB();
|
||||
}
|
||||
|
||||
CMappedFile::~CMappedFile()
|
||||
{
|
||||
MT_PLATFORM_STUB();
|
||||
Destroy();
|
||||
}
|
||||
|
||||
auto CMappedFile::Link(DWORD, const void *) -> void
|
||||
BOOL CMappedFile::Create(const char * filename)
|
||||
{
|
||||
MT_PLATFORM_STUB();
|
||||
Destroy();
|
||||
return CFileBase::Create(filename, FILEMODE_READ);
|
||||
}
|
||||
|
||||
auto CMappedFile::Create(const char *) -> BOOL
|
||||
BOOL CMappedFile::Create(const char * filename, const void** dest, int offset, int size)
|
||||
{
|
||||
MT_PLATFORM_STUB();
|
||||
return mt_platform_stub_return<BOOL>();
|
||||
if (!CMappedFile::Create(filename))
|
||||
return FALSE;
|
||||
|
||||
int ret = Map(dest, offset, size);
|
||||
return (ret) > 0;
|
||||
}
|
||||
|
||||
auto CMappedFile::Create(const char *, const void **, int, int) -> BOOL
|
||||
LPCVOID CMappedFile::Get()
|
||||
{
|
||||
MT_PLATFORM_STUB();
|
||||
return mt_platform_stub_return<BOOL>();
|
||||
return m_lpData;
|
||||
}
|
||||
|
||||
auto CMappedFile::Get() -> LPCVOID
|
||||
void CMappedFile::Link(DWORD dwBufSize, const void* c_pvBufData)
|
||||
{
|
||||
MT_PLATFORM_STUB();
|
||||
return mt_platform_stub_return<LPCVOID>();
|
||||
m_dwBufLinkSize=dwBufSize;
|
||||
m_pbBufLinkData=(BYTE*)c_pvBufData;
|
||||
}
|
||||
|
||||
auto CMappedFile::Destroy() -> void
|
||||
void CMappedFile::BindLZObject(CLZObject * pLZObj)
|
||||
{
|
||||
MT_PLATFORM_STUB();
|
||||
assert(m_pLZObj == NULL);
|
||||
m_pLZObj = pLZObj;
|
||||
|
||||
Link(m_pLZObj->GetSize(), m_pLZObj->GetBuffer());
|
||||
}
|
||||
|
||||
auto CMappedFile::Seek(DWORD, int) -> int
|
||||
void CMappedFile::BindLZObjectWithBufferedSize(CLZObject * pLZObj)
|
||||
{
|
||||
MT_PLATFORM_STUB();
|
||||
return mt_platform_stub_return<int>();
|
||||
assert(m_pLZObj == NULL);
|
||||
m_pLZObj = pLZObj;
|
||||
|
||||
Link(m_pLZObj->GetBufferSize(), m_pLZObj->GetBuffer());
|
||||
}
|
||||
|
||||
auto CMappedFile::Map(const void **, int, int) -> int
|
||||
BYTE* CMappedFile::AppendDataBlock( const void* pBlock, DWORD dwBlockSize )
|
||||
{
|
||||
MT_PLATFORM_STUB();
|
||||
return mt_platform_stub_return<int>();
|
||||
if( m_pbAppendResultDataBlock )
|
||||
{
|
||||
delete []m_pbAppendResultDataBlock;
|
||||
}
|
||||
|
||||
//realloc
|
||||
m_dwAppendResultDataSize = m_dwBufLinkSize+dwBlockSize;
|
||||
m_pbAppendResultDataBlock = new BYTE[m_dwAppendResultDataSize];
|
||||
|
||||
memcpy(m_pbAppendResultDataBlock, m_pbBufLinkData, m_dwBufLinkSize );
|
||||
memcpy(m_pbAppendResultDataBlock + m_dwBufLinkSize, pBlock, dwBlockSize );
|
||||
|
||||
//redirect
|
||||
Link(m_dwAppendResultDataSize, m_pbAppendResultDataBlock);
|
||||
|
||||
return m_pbAppendResultDataBlock;
|
||||
}
|
||||
|
||||
auto CMappedFile::Size() -> DWORD
|
||||
void CMappedFile::Destroy()
|
||||
{
|
||||
MT_PLATFORM_STUB();
|
||||
return mt_platform_stub_return<DWORD>();
|
||||
if (m_pLZObj)
|
||||
{
|
||||
delete m_pLZObj;
|
||||
m_pLZObj = NULL;
|
||||
}
|
||||
|
||||
if (NULL != m_lpMapData)
|
||||
{
|
||||
Unmap(m_lpMapData);
|
||||
m_lpMapData = NULL;
|
||||
}
|
||||
|
||||
#if defined(_WIN32)
|
||||
if (NULL != m_hFM)
|
||||
{
|
||||
CloseHandle(m_hFM);
|
||||
m_hFM = NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
if( m_pbAppendResultDataBlock )
|
||||
{
|
||||
delete []m_pbAppendResultDataBlock;
|
||||
m_pbAppendResultDataBlock = NULL;
|
||||
}
|
||||
|
||||
m_dwAppendResultDataSize = 0;
|
||||
|
||||
m_pbBufLinkData = NULL;
|
||||
m_dwBufLinkSize = 0;
|
||||
|
||||
m_seekPosition = 0;
|
||||
m_dataOffset = 0;
|
||||
m_mapSize = 0;
|
||||
|
||||
CFileBase::Destroy();
|
||||
}
|
||||
|
||||
auto CMappedFile::GetPosition() -> DWORD
|
||||
int CMappedFile::Seek(DWORD offset, int iSeekType)
|
||||
{
|
||||
MT_PLATFORM_STUB();
|
||||
return mt_platform_stub_return<DWORD>();
|
||||
switch (iSeekType)
|
||||
{
|
||||
case SEEK_TYPE_BEGIN:
|
||||
if (offset > m_dwSize)
|
||||
offset = m_dwSize;
|
||||
|
||||
m_seekPosition = offset;
|
||||
break;
|
||||
|
||||
case SEEK_TYPE_CURRENT:
|
||||
m_seekPosition = min(m_seekPosition + offset, Size());
|
||||
break;
|
||||
|
||||
case SEEK_TYPE_END:
|
||||
m_seekPosition = max(0, Size() - offset);
|
||||
break;
|
||||
}
|
||||
|
||||
return m_seekPosition;
|
||||
}
|
||||
|
||||
auto CMappedFile::Read(void *, int) -> BOOL
|
||||
int CMappedFile::Map(const void **dest, int offset, int size)
|
||||
{
|
||||
MT_PLATFORM_STUB();
|
||||
return mt_platform_stub_return<BOOL>();
|
||||
m_dataOffset = offset;
|
||||
|
||||
if (size == 0)
|
||||
m_mapSize = m_dwSize;
|
||||
else
|
||||
m_mapSize = size;
|
||||
|
||||
if (m_dataOffset + m_mapSize > m_dwSize)
|
||||
return 0;
|
||||
|
||||
DWORD dwSysGran = allocation_granularity();
|
||||
DWORD dwFileMapStart = (m_dataOffset / dwSysGran) * dwSysGran;
|
||||
DWORD dwMapViewSize = (m_dataOffset % dwSysGran) + m_mapSize;
|
||||
INT iViewDelta = m_dataOffset - dwFileMapStart;
|
||||
|
||||
FILE* fp = static_cast<FILE*>(m_hFile);
|
||||
#if defined(_WIN32)
|
||||
m_hFM = CreateFileMapping((HANDLE) _get_osfhandle(_fileno(fp)), NULL, PAGE_READONLY, 0,
|
||||
m_dataOffset + m_mapSize, NULL);
|
||||
if (!m_hFM)
|
||||
{
|
||||
OutputDebugString("CMappedFile::Map !m_hFM\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
m_lpMapData = MapViewOfFile(m_hFM, FILE_MAP_READ, 0, dwFileMapStart, dwMapViewSize);
|
||||
if (!m_lpMapData)
|
||||
{
|
||||
TraceError("CMappedFile::Map !m_lpMapData %lu", GetLastError());
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
// A zero-length view (empty file) is a valid 40250 map; mmap rejects length 0.
|
||||
if (dwMapViewSize == 0)
|
||||
dwMapViewSize = 1;
|
||||
void* p = mmap(NULL, dwMapViewSize, PROT_READ, MAP_PRIVATE, fileno(fp), static_cast<off_t>(dwFileMapStart));
|
||||
if (p == MAP_FAILED)
|
||||
{
|
||||
TraceError("CMappedFile::Map !m_lpMapData %s", GetFileName());
|
||||
return 0;
|
||||
}
|
||||
m_lpMapData = p;
|
||||
#endif
|
||||
|
||||
m_lpData = (char*) m_lpMapData + iViewDelta;
|
||||
*dest = (char*) m_lpData;
|
||||
m_seekPosition = 0;
|
||||
|
||||
Link(m_mapSize, m_lpData);
|
||||
|
||||
return (m_mapSize);
|
||||
}
|
||||
|
||||
auto CMappedFile::GetSeekPosition() -> DWORD
|
||||
BYTE * CMappedFile::GetCurrentSeekPoint()
|
||||
{
|
||||
MT_PLATFORM_STUB();
|
||||
return mt_platform_stub_return<DWORD>();
|
||||
return m_pbBufLinkData+m_seekPosition;
|
||||
}
|
||||
|
||||
auto CMappedFile::BindLZObject(CLZObject *) -> void
|
||||
DWORD CMappedFile::Size()
|
||||
{
|
||||
MT_PLATFORM_STUB();
|
||||
return m_dwBufLinkSize;
|
||||
}
|
||||
|
||||
auto CMappedFile::BindLZObjectWithBufferedSize(CLZObject *) -> void
|
||||
DWORD CMappedFile::GetPosition()
|
||||
{
|
||||
MT_PLATFORM_STUB();
|
||||
return m_dataOffset;
|
||||
}
|
||||
|
||||
auto CMappedFile::AppendDataBlock(const void *, DWORD) -> BYTE *
|
||||
BOOL CMappedFile::Read(void * dest, int bytes)
|
||||
{
|
||||
MT_PLATFORM_STUB();
|
||||
return mt_platform_stub_return<BYTE *>();
|
||||
if (m_seekPosition + bytes > Size())
|
||||
return FALSE;
|
||||
|
||||
memcpy(dest, GetCurrentSeekPoint(), bytes);
|
||||
m_seekPosition += bytes;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
auto CMappedFile::GetCurrentSeekPoint() -> BYTE *
|
||||
DWORD CMappedFile::GetSeekPosition(void)
|
||||
{
|
||||
MT_PLATFORM_STUB();
|
||||
return mt_platform_stub_return<BYTE *>();
|
||||
return m_seekPosition;
|
||||
}
|
||||
|
||||
auto CMappedFile::Unmap(LPCVOID) -> void
|
||||
void CMappedFile::Unmap(LPCVOID data)
|
||||
{
|
||||
MT_PLATFORM_STUB();
|
||||
#if defined(_WIN32)
|
||||
if (!UnmapViewOfFile(data))
|
||||
TraceError("CMappedFile::Unmap - Error");
|
||||
#else
|
||||
// Same view length Map computed from the (unchanged) offset and size.
|
||||
const DWORD dwSysGran = allocation_granularity();
|
||||
DWORD dwMapViewSize = (m_dataOffset % dwSysGran) + m_mapSize;
|
||||
if (dwMapViewSize == 0)
|
||||
dwMapViewSize = 1;
|
||||
if (munmap(const_cast<void*>(data), dwMapViewSize) != 0)
|
||||
TraceError("CMappedFile::Unmap - Error");
|
||||
#endif
|
||||
m_lpData = NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user