Skip to content

Commit

Permalink
properly get filesize for files > 4GiB
Browse files Browse the repository at this point in the history
  • Loading branch information
qbnu committed Jun 22, 2024
1 parent 69bdf62 commit bd2adc5
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 22 deletions.
6 changes: 6 additions & 0 deletions src/JPEGView/Helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,12 @@ __int64 GetFileSize(LPCTSTR sPath) {
return fileSize;
}

__int64 GetFileSize(HANDLE hFile) {
__int64 fileSize = 0;
::GetFileSizeEx(hFile, (PLARGE_INTEGER)&fileSize);
return fileSize;
}

// Gets the frame index of the next frame, depending on the index of the last image (relevant if the image is a multiframe image)
int GetFrameIndex(CJPEGImage* pImage, bool bNext, bool bPlayAnimation, bool & switchImage) {
bool isMultiFrame = pImage != NULL && pImage->NumberOfFrames() > 1;
Expand Down
3 changes: 3 additions & 0 deletions src/JPEGView/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ namespace Helpers {
// file size for file with path
__int64 GetFileSize(LPCTSTR sPath);

// file size for file with handle
__int64 GetFileSize(HANDLE hFile);

// Gets the frame index of the next frame, depending on the index of the last image (relevant if the image is a multiframe image)
int GetFrameIndex(CJPEGImage* pImage, bool bNext, bool bPlayAnimation, bool & switchImage);

Expand Down
24 changes: 11 additions & 13 deletions src/JPEGView/ImageLoadThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ void CImageLoadThread::ProcessReadJPEGRequest(CRequest * request) {
void* pBuffer = NULL;
try {
// Don't read too huge files
unsigned int nFileSize = ::GetFileSize(hFile, NULL);
long long nFileSize = Helpers::GetFileSize(hFile);
if (nFileSize > MAX_JPEG_FILE_SIZE) {
request->OutOfMemory = true;
::CloseHandle(hFile);
Expand Down Expand Up @@ -590,11 +590,11 @@ void CImageLoadThread::ProcessReadWEBPRequest(CRequest * request) {
}
char* pBuffer = NULL;
try {
unsigned int nFileSize = 0;
long long nFileSize = 0;
unsigned int nNumBytesRead;
if (!bUseCachedDecoder) {
// Don't read too huge files
nFileSize = ::GetFileSize(hFile, NULL);
nFileSize = Helpers::GetFileSize(hFile);
if (nFileSize > MAX_WEBP_FILE_SIZE) {
request->OutOfMemory = true;
::CloseHandle(hFile);
Expand Down Expand Up @@ -665,11 +665,11 @@ void CImageLoadThread::ProcessReadPNGRequest(CRequest* request) {
HGLOBAL hFileBuffer = NULL;
void* pBuffer = NULL;
try {
unsigned int nFileSize;
long long nFileSize;
unsigned int nNumBytesRead;
if (!bUseCachedDecoder) {
// Don't read too huge files
nFileSize = ::GetFileSize(hFile, NULL);
nFileSize = Helpers::GetFileSize(hFile);
if (nFileSize > MAX_PNG_FILE_SIZE) {
request->OutOfMemory = true;
::CloseHandle(hFile);
Expand Down Expand Up @@ -761,11 +761,11 @@ void CImageLoadThread::ProcessReadJXLRequest(CRequest* request) {
char* pBuffer = NULL;
UINT nPrevErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
try {
unsigned int nFileSize = 0;
long long nFileSize = 0;
unsigned int nNumBytesRead;
if (!bUseCachedDecoder) {
// Don't read too huge files
nFileSize = ::GetFileSize(hFile, NULL);
nFileSize = Helpers::GetFileSize(hFile);
if (nFileSize > MAX_JXL_FILE_SIZE) {
request->OutOfMemory = true;
::CloseHandle(hFile);
Expand Down Expand Up @@ -834,11 +834,11 @@ void CImageLoadThread::ProcessReadAVIFRequest(CRequest* request) {
char* pBuffer = NULL;
UINT nPrevErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
try {
unsigned int nFileSize = 0;
long long nFileSize = 0;
unsigned int nNumBytesRead;
if (!bUseCachedDecoder) {
// Don't read too huge files
nFileSize = ::GetFileSize(hFile, NULL);
nFileSize = Helpers::GetFileSize(hFile);
if (nFileSize > MAX_HEIF_FILE_SIZE) {
request->OutOfMemory = true;
::CloseHandle(hFile);
Expand Down Expand Up @@ -899,10 +899,9 @@ void CImageLoadThread::ProcessReadHEIFRequest(CRequest* request) {
char* pBuffer = NULL;
UINT nPrevErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
try {
unsigned int nFileSize = 0;
unsigned int nNumBytesRead;
// Don't read too huge files
nFileSize = ::GetFileSize(hFile, NULL);
long long nFileSize = Helpers::GetFileSize(hFile);
if (nFileSize > MAX_HEIF_FILE_SIZE) {
request->OutOfMemory = true;
::CloseHandle(hFile);
Expand Down Expand Up @@ -962,10 +961,9 @@ void CImageLoadThread::ProcessReadQOIRequest(CRequest* request) {
}
char* pBuffer = NULL;
try {
unsigned int nFileSize = 0;
unsigned int nNumBytesRead;
// Don't read too huge files
nFileSize = ::GetFileSize(hFile, NULL);
long long nFileSize = Helpers::GetFileSize(hFile);
if (nFileSize > MAX_PNG_FILE_SIZE) {
request->OutOfMemory = true;
::CloseHandle(hFile);
Expand Down
3 changes: 2 additions & 1 deletion src/JPEGView/JPEGLosslessTransform.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "StdAfx.h"
#include "JPEGLosslessTransform.h"
#include "Helpers.h"
#include "libjpeg-turbo\include\turbojpeg.h"

CJPEGLosslessTransform::EResult _DoTransformation(LPCTSTR sInputFile, LPCTSTR sOutputFile, tjtransform &transform);
Expand Down Expand Up @@ -73,7 +74,7 @@ static unsigned char* _ReadFile(LPCTSTR sFileName, unsigned int & nLengthBytes)
return NULL;
}

unsigned int nFileSize = ::GetFileSize(hFile, NULL);
long long nFileSize = Helpers::GetFileSize(hFile);
if (nFileSize > MAX_JPEG_FILE_SIZE) {
::CloseHandle(hFile);
return NULL;
Expand Down
3 changes: 1 addition & 2 deletions src/JPEGView/PSDWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ CJPEGImage* PsdReader::ReadImage(LPCTSTR strFileName, bool& bOutOfMemory)
void* transform = NULL;
CJPEGImage* Image = NULL;
try {
unsigned int nFileSize = 0;
nFileSize = ::GetFileSize(hFile, NULL);
long long nFileSize = Helpers::GetFileSize(hFile);
ThrowIf(nFileSize > MAX_PSD_FILE_SIZE);

// Skip file signature
Expand Down
9 changes: 3 additions & 6 deletions src/JPEGView/ParameterDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,8 +575,7 @@ CParameterDB::DBBlock* CParameterDB::LoadFromFile(const CString& sParamDBName, b
}

// Check if file is too large
__int64 nFileSize;
::GetFileSizeEx(hFile, (PLARGE_INTEGER)&nFileSize);
__int64 nFileSize = Helpers::GetFileSize(hFile);
if (nFileSize > MAX_DB_FILE_SIZE) {
HandleErrorAndCloseHandle(errorTooLarge, sParamDBName, hFile);
return NULL;
Expand Down Expand Up @@ -619,8 +618,7 @@ bool CParameterDB::SaveToFile(int nIndex, const CParameterDBEntry & dbEntry) {
}

// Check header, do not overwrite unknown files or versions
__int64 nFileSize;
::GetFileSizeEx(hFile, (PLARGE_INTEGER)&nFileSize);
__int64 nFileSize = Helpers::GetFileSize(hFile);
if (nFileSize >= sizeof(ParameterDBHeader)) {
// check the header
int nVersion = 0;
Expand Down Expand Up @@ -684,8 +682,7 @@ bool CParameterDB::SaveToFile(int nIndex, const CParameterDBEntry & dbEntry) {
}

bool CParameterDB::ConvertVersion1To2(HANDLE hFile, const CString& sFileName) {
__int64 nFileSize;
::GetFileSizeEx(hFile, (PLARGE_INTEGER)&nFileSize);
__int64 nFileSize = Helpers::GetFileSize(hFile);
if (nFileSize > MAX_DB_FILE_SIZE) {
HandleErrorAndCloseHandle(errorTooLarge, sFileName, hFile);
return false;
Expand Down

0 comments on commit bd2adc5

Please sign in to comment.