audit: EterBase CRC32/FileBase/MappedFile mapped, with port.utils checks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-09-30 12:46:43 +09:00
co-authored by Claude Opus 5.5
parent 290a331aef
commit f9b1044a4a
5 changed files with 308 additions and 62 deletions
+75
View File
@@ -1,11 +1,14 @@
#include "EterBase/StdAfx.h"
#include "EterBase/Utils.h"
#include "EterBase/FileLoader.h"
#include "EterBase/CRC32.h"
#include "EterBase/MappedFile.h"
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <unistd.h>
int main()
{
@@ -88,5 +91,77 @@ int main()
check(!disk.Open("/nonexistent/mt_fileloader"), "DiskFileLoader missing");
remove(path);
}
// CRC32: the IEEE check value, GetCaseCRC32 folds ASCII case (effect/sound/tree name keys), and the
// file variants (guild symbol CRC + size, GuildMarkDownloader) CRC the whole file.
{
check(GetCRC32("123456789", 9) == 0xCBF43926u, "GetCRC32 check value");
check(GetCaseCRC32("D:/Ymir Work/A.MSE", 18) == GetCaseCRC32("d:/ymir work/a.mse", 18), "GetCaseCRC32 case fold");
check(GetCaseCRC32("abc", 3) == GetCRC32("ABC", 3), "GetCaseCRC32 folds to upper case");
char path[] = "/tmp/mt_crc32_XXXXXX";
int fd = mkstemp(path);
FILE * fp = fdopen(fd, "wb");
fwrite("123456789", 1, 9, fp);
fclose(fp);
check(GetFileCRC32(path) == 0xCBF43926u, "GetFileCRC32");
check(GetFileSize(path) == 9, "GetFileSize");
fp = fopen(path, "rb");
fseek(fp, 4, SEEK_SET);
check(GetHFILECRC32(fp) == 0xCBF43926u && ftell(fp) == 4, "GetHFILECRC32 whole file, position kept");
fclose(fp);
check(GetFileCRC32("/nonexistent/mt_crc32") == 0 && GetFileSize("/nonexistent/mt_crc32") == 0, "CRC32 missing file");
remove(path);
}
// CFileBase (Win32 HANDLE -> FILE*): write mode opens without truncating and tracks the size, Seek clamps
// to the size, SeekCur takes the DWORD as a signed distance, Read succeeds on a short read.
// CMappedFile: the view at a non-aligned offset, Seek clamps, Read refuses past the end,
// AppendDataBlock re-links onto a copy.
{
char path[] = "/tmp/mt_filebase_XXXXXX";
close(mkstemp(path));
{
CFileBase f;
check(f.IsNull(), "CFileBase IsNull before Create");
check(f.Create(path, CFileBase::FILEMODE_WRITE) && !f.IsNull(), "CFileBase Create write");
check(strcmp(f.GetFileName(), path) == 0, "CFileBase GetFileName");
check(f.Write("abcdefgh", 8) && f.Size() == 8 && f.GetPosition() == 8, "CFileBase Write size/position");
}
{
CFileBase f;
check(f.Create(path, CFileBase::FILEMODE_WRITE) && f.Size() == 8, "CFileBase write mode keeps contents");
f.Seek(100);
check(f.GetPosition() == 8, "CFileBase Seek clamps to size");
f.Seek(2);
f.SeekCur(3);
check(f.GetPosition() == 5, "CFileBase SeekCur forward");
f.SeekCur(DWORD(-2));
check(f.GetPosition() == 3, "CFileBase SeekCur negative distance");
char buf[8] = {};
check(f.Read(buf, 2) && buf[0] == 'd' && buf[1] == 'e', "CFileBase Read");
check(f.Read(buf, 8), "CFileBase short Read succeeds");
}
{
CFileBase f;
check(!f.Create("/nonexistent/mt_filebase", CFileBase::FILEMODE_READ) && f.IsNull(), "CFileBase missing file");
}
{
CMappedFile mf;
const void * view = NULL;
check(mf.Create(path, &view, 3, 4) && memcmp(view, "defg", 4) == 0, "CMappedFile view at offset");
check(mf.Get() == view && mf.GetPosition() == 3 && mf.Size() == 4, "CMappedFile Get/GetPosition/Size");
char buf[8] = {};
check(mf.Read(buf, 3) && memcmp(buf, "def", 3) == 0 && mf.GetSeekPosition() == 3, "CMappedFile Read");
check(!mf.Read(buf, 2) && mf.GetSeekPosition() == 3, "CMappedFile Read past end");
check(*mf.GetCurrentSeekPoint() == 'g', "CMappedFile GetCurrentSeekPoint");
check(mf.Seek(1, CMappedFile::SEEK_TYPE_CURRENT) == 4 && mf.Seek(9, CMappedFile::SEEK_TYPE_CURRENT) == 4, "CMappedFile Seek current clamps");
check(mf.Seek(1, CMappedFile::SEEK_TYPE_END) == 3 && mf.Seek(1) == 1, "CMappedFile Seek end/begin");
BYTE * joined = mf.AppendDataBlock("XY", 2);
check(mf.Size() == 6 && memcmp(joined, "defgXY", 6) == 0, "CMappedFile AppendDataBlock");
check(!mf.Create(path, &view, 6, 4), "CMappedFile view past the end");
}
remove(path);
}
return failures ? 1 : 0;
}