Skip to content

Commit

Permalink
Merge pull request #485 from VincentBlondeau/fixMoreThanDWORDSizeImage
Browse files Browse the repository at this point in the history
Fix #484 [Win64]Cannot save and load image files with a heap whose size is more than 0xff ff ff ff (~4.1GB)
  • Loading branch information
eliotmiranda committed Apr 11, 2020
2 parents ab241d0 + 561bea7 commit 2a79468
Showing 1 changed file with 37 additions and 11 deletions.
48 changes: 37 additions & 11 deletions platforms/win32/plugins/FilePlugin/sqWin32FilePrims.c
Expand Up @@ -37,6 +37,7 @@ extern struct VirtualMachine *interpreterProxy;

#define true 1
#define false 0
static const DWORD MAX_DWORD = 4294967295;

#define FILE_HANDLE(f) ((HANDLE) (f)->file)
#define FAIL() { return interpreterProxy->primitiveFail(); }
Expand Down Expand Up @@ -511,18 +512,26 @@ squeakFileOffsetType sqImageFilePosition(sqImageFile h)
size_t sqImageFileRead(void *ptr, size_t sz, size_t count, sqImageFile h)
{
DWORD dwReallyRead;
size_t reallyRead = 0;
size_t totalToRead = count * sz;
squeakFileOffsetType position;

position = sqImageFilePosition(h);
ReadFile((HANDLE)(h-1), (LPVOID) ptr, count*sz, &dwReallyRead, NULL);
while(dwReallyRead != (DWORD)(count*sz)) {
DWORD err = GetLastError();
if(sqMessageBox(MB_ABORTRETRYIGNORE, TEXT("Squeak Warning"),TEXT("Image file read problem (%d out of %d bytes read)"), dwReallyRead, count*sz)
== IDABORT) return (dwReallyRead / sz);
sqImageFileSeek(h, position);
ReadFile((HANDLE)(h-1), (LPVOID) ptr, count*sz, &dwReallyRead, NULL);
while (reallyRead != totalToRead) {
DWORD toRead = (totalToRead - reallyRead) > (size_t)MAX_DWORD ? MAX_DWORD : totalToRead - reallyRead;
BOOL ret = ReadFile((HANDLE)(h - 1), (LPVOID)((sqInt)ptr + (sqInt)reallyRead), toRead, &dwReallyRead, NULL);
reallyRead += dwReallyRead;

if (!ret | dwReallyRead != toRead) {
DWORD err = GetLastError();
if (sqMessageBox(MB_ABORTRETRYIGNORE, TEXT("VM Warning"), TEXT("Image file read problem (%d out of %d bytes read)"), dwReallyRead, toRead)
== IDABORT) return (size_t)(reallyRead / sz);
sqImageFileSeek(h, position);
reallyRead = 0;
}

}
return (dwReallyRead / sz);
return (reallyRead / sz);
}

squeakFileOffsetType sqImageFileSeek(sqImageFile h, squeakFileOffsetType pos)
Expand All @@ -543,9 +552,26 @@ squeakFileOffsetType sqImageFileSeekEnd(sqImageFile h, squeakFileOffsetType pos)

size_t sqImageFileWrite(const void *ptr, size_t sz, size_t count, sqImageFile h)
{
size_t reallyWritten =0;
DWORD dwReallyWritten;
WriteFile((HANDLE)(h-1), (LPVOID) ptr, count*sz, &dwReallyWritten, NULL);
return (size_t) (dwReallyWritten / sz);
size_t totalToWrite = count * sz;
squeakFileOffsetType position;

position = sqImageFilePosition(h);
while (reallyWritten != totalToWrite) {
DWORD toWrite = (totalToWrite - reallyWritten) > (size_t) MAX_DWORD ? MAX_DWORD : totalToWrite - reallyWritten;
BOOL ret = WriteFile((HANDLE)(h - 1), (LPVOID)((sqInt)ptr + (sqInt) reallyWritten), toWrite, &dwReallyWritten, NULL);
reallyWritten += dwReallyWritten;

if (!ret | dwReallyWritten != toWrite) {
DWORD err = GetLastError();
if (sqMessageBox(MB_ABORTRETRYIGNORE, TEXT("VM Warning"), TEXT("Image file read problem (%d out of %d bytes read)"), dwReallyWritten, toWrite)
== IDABORT) return (size_t)(reallyWritten / sz);
sqImageFileSeek(h, position);
reallyWritten = 0;
}
}
return (size_t) (reallyWritten / sz);
}

squeakFileOffsetType sqImageFileSize(sqImageFile h)
Expand Down

0 comments on commit 2a79468

Please sign in to comment.