Skip to content

Commit

Permalink
OpenJPEG: fix to return the proper number of bytes read when we read …
Browse files Browse the repository at this point in the history
…more than 2 GB at once (fixes uclouvain/openjpeg#1151)
  • Loading branch information
rouault committed Oct 3, 2019
1 parent 44bf617 commit bad858f
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions gdal/frmts/openjpeg/openjpegdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,13 @@ static OPJ_SIZE_T JP2OpenJPEGDataset_Read(void* pBuffer, OPJ_SIZE_T nBytes,
void *pUserData)
{
JP2OpenJPEGFile* psJP2OpenJPEGFile = (JP2OpenJPEGFile* )pUserData;
int nRet = static_cast<int>(VSIFReadL(pBuffer, 1, nBytes, psJP2OpenJPEGFile->fp));
OPJ_SIZE_T nRet = static_cast<OPJ_SIZE_T>(VSIFReadL(pBuffer, 1, nBytes, psJP2OpenJPEGFile->fp));
#ifdef DEBUG_IO
CPLDebug("OPENJPEG", "JP2OpenJPEGDataset_Read(%d) = %d", (int)nBytes, nRet);
CPLDebug("OPENJPEG", "JP2OpenJPEGDataset_Read(" CPL_FRMT_GUIB ") = " CPL_FRMT_GUIB,
static_cast<GUIntBig>(nBytes), static_cast<GUIntBig>(nRet));
#endif
if (nRet == 0)
nRet = -1;
nRet = static_cast<OPJ_SIZE_T>(-1);

return nRet;
}
Expand All @@ -146,11 +147,12 @@ static OPJ_SIZE_T JP2OpenJPEGDataset_Write(void* pBuffer, OPJ_SIZE_T nBytes,
void *pUserData)
{
JP2OpenJPEGFile* psJP2OpenJPEGFile = (JP2OpenJPEGFile* )pUserData;
int nRet = static_cast<int>(VSIFWriteL(pBuffer, 1, nBytes, psJP2OpenJPEGFile->fp));
OPJ_SIZE_T nRet = static_cast<OPJ_SIZE_T>(VSIFWriteL(pBuffer, 1, nBytes, psJP2OpenJPEGFile->fp));
#ifdef DEBUG_IO
CPLDebug("OPENJPEG", "JP2OpenJPEGDataset_Write(%d) = %d", (int)nBytes, nRet);
CPLDebug("OPENJPEG", "JP2OpenJPEGDataset_Write(" CPL_FRMT_GUIB ") = " CPL_FRMT_GUIB,
static_cast<GUIntBig>(nBytes), static_cast<GUIntBig>(nRet));
#endif
if( static_cast<OPJ_SIZE_T>(nRet) != nBytes )
if( nRet != nBytes )
return static_cast<OPJ_SIZE_T>(-1);
return nRet;
}
Expand All @@ -163,7 +165,8 @@ static OPJ_BOOL JP2OpenJPEGDataset_Seek(OPJ_OFF_T nBytes, void * pUserData)
{
JP2OpenJPEGFile* psJP2OpenJPEGFile = (JP2OpenJPEGFile* )pUserData;
#ifdef DEBUG_IO
CPLDebug("OPENJPEG", "JP2OpenJPEGDataset_Seek(%d)", (int)nBytes);
CPLDebug("OPENJPEG", "JP2OpenJPEGDataset_Seek(" CPL_FRMT_GUIB ")",
static_cast<GUIntBig>(nBytes));
#endif
return VSIFSeekL(psJP2OpenJPEGFile->fp, psJP2OpenJPEGFile->nBaseOffset +nBytes,
SEEK_SET) == 0;
Expand All @@ -179,8 +182,8 @@ static OPJ_OFF_T JP2OpenJPEGDataset_Skip(OPJ_OFF_T nBytes, void * pUserData)
vsi_l_offset nOffset = VSIFTellL(psJP2OpenJPEGFile->fp);
nOffset += nBytes;
#ifdef DEBUG_IO
CPLDebug("OPENJPEG", "JP2OpenJPEGDataset_Skip(%d -> " CPL_FRMT_GUIB ")",
(int)nBytes, (GUIntBig)nOffset);
CPLDebug("OPENJPEG", "JP2OpenJPEGDataset_Skip(" CPL_FRMT_GUIB " -> " CPL_FRMT_GUIB ")",
static_cast<GUIntBig>(nBytes), static_cast<GUIntBig>(nOffset));
#endif
VSIFSeekL(psJP2OpenJPEGFile->fp, nOffset, SEEK_SET);
return nBytes;
Expand Down

0 comments on commit bad858f

Please sign in to comment.