1818#include " jpgdataset.h"
1919
2020#include < cerrno>
21+ #include < cinttypes>
2122#include < climits>
2223#include < cstddef>
2324#include < cstdio>
@@ -149,7 +150,7 @@ void JPGDatasetCommon::ReadImageStructureMetadata()
149150 const vsi_l_offset nCurOffset = VSIFTellL (m_fpImage);
150151
151152 GByte abyChunkHeader[4 ];
152- int nChunkLoc = 2 ;
153+ vsi_l_offset nChunkLoc = 2 ;
153154 constexpr GByte MARKER_QUANT_TABLE = 0xDB ;
154155 struct CPLMD5Context context;
155156 CPLMD5Init (&context);
@@ -331,7 +332,7 @@ void JPGDatasetCommon::ReadXMPMetadata()
331332 constexpr int APP1_XMP_SIGNATURE_LEN =
332333 static_cast <int >(sizeof (APP1_XMP_SIGNATURE));
333334 GByte abyChunkHeader[JFIF_MARKER_SIZE + APP1_XMP_SIGNATURE_LEN] = {};
334- int nChunkLoc = 2 ;
335+ vsi_l_offset nChunkLoc = 2 ;
335336 bool bFoundXMP = false ;
336337
337338 while (true )
@@ -433,7 +434,7 @@ void JPGDatasetCommon::ReadDJIMetadata()
433434
434435 const vsi_l_offset nCurOffset = VSIFTellL (m_fpImage);
435436
436- int nChunkLoc = 2 ;
437+ vsi_l_offset nChunkLoc = 2 ;
437438 // size of APP1 segment marker"
438439 GByte abyChunkHeader[4 ];
439440
@@ -552,7 +553,7 @@ void JPGDatasetCommon::ReadFLIRMetadata()
552553 // Save current position to avoid disturbing JPEG stream decoding.
553554 const vsi_l_offset nCurOffset = VSIFTellL (m_fpImage);
554555
555- int nChunkLoc = 2 ;
556+ vsi_l_offset nChunkLoc = 2 ;
556557 // size of APP1 segment marker + size of "FLIR\0"
557558 GByte abyChunkHeader[4 + 5 ];
558559 std::vector<GByte> abyFLIR;
@@ -1198,7 +1199,7 @@ void JPGDatasetCommon::ReadICCProfile()
11981199
11991200 // Search for APP2 chunk.
12001201 GByte abyChunkHeader[18 ] = {};
1201- int nChunkLoc = 2 ;
1202+ vsi_l_offset nChunkLoc = 2 ;
12021203 bool bOk = true ;
12031204
12041205 while (true )
@@ -1378,10 +1379,9 @@ void JPGDatasetCommon::ReadICCProfile()
13781379/* ***********************************************************************/
13791380bool JPGDatasetCommon::EXIFInit (VSILFILE *fp)
13801381{
1381- if (nTiffDirStart == 0 )
1382- return false ;
1383- if (nTiffDirStart > 0 )
1384- return true ;
1382+ if (m_bTiffDirStartInit)
1383+ return nTiffDirStart > 0 ;
1384+ m_bTiffDirStartInit = true ;
13851385 nTiffDirStart = 0 ;
13861386
13871387#ifdef CPL_MSB
@@ -1392,7 +1392,7 @@ bool JPGDatasetCommon::EXIFInit(VSILFILE *fp)
13921392
13931393 // Search for APP1 chunk.
13941394 GByte abyChunkHeader[10 ] = {};
1395- int nChunkLoc = 2 ;
1395+ vsi_l_offset nChunkLoc = 2 ;
13961396
13971397 while (true )
13981398 {
@@ -1431,25 +1431,27 @@ bool JPGDatasetCommon::EXIFInit(VSILFILE *fp)
14311431 STARTS_WITH (reinterpret_cast <char *>(abyChunkHeader) + 4 ,
14321432 " Exif" ))
14331433 {
1434- if (nTIFFHEADER < 0 )
1434+ if (nTIFFHEADER == 0 )
14351435 {
14361436 nTIFFHEADER = nChunkLoc + 10 ;
14371437 }
14381438 else
14391439 {
1440- CPLDebug (
1441- " JPEG" ,
1442- " Another Exif directory found at offset %u. Ignoring "
1443- " it and only taking into account the one at offset %u" ,
1444- unsigned (nChunkLoc + 10 ), unsigned (nTIFFHEADER));
1440+ CPLDebug (" JPEG" ,
1441+ " Another Exif directory found at offset %" PRIu64
1442+ " . Ignoring "
1443+ " it and only taking into account the one at "
1444+ " offset %" PRIu64,
1445+ static_cast <uint64_t >(nChunkLoc + 10 ),
1446+ static_cast <uint64_t >(nTIFFHEADER));
14451447 }
14461448 }
14471449 }
14481450
14491451 nChunkLoc += 2 + nChunkLength;
14501452 }
14511453
1452- if (nTIFFHEADER < 0 )
1454+ if (nTIFFHEADER == 0 )
14531455 return false ;
14541456
14551457 // Read TIFF header.
@@ -1897,8 +1899,7 @@ GDALDataset *JPGDatasetCommon::InitEXIFOverview()
18971899
18981900 // Read number of entry in directory.
18991901 GUInt16 nEntryCount = 0 ;
1900- if (nTiffDirStart > (INT_MAX - nTIFFHEADER) ||
1901- VSIFSeekL (m_fpImage, nTiffDirStart + nTIFFHEADER, SEEK_SET) != 0 ||
1902+ if (VSIFSeekL (m_fpImage, nTiffDirStart + nTIFFHEADER, SEEK_SET) != 0 ||
19021903 VSIFReadL (&nEntryCount, 1 , sizeof (GUInt16), m_fpImage) !=
19031904 sizeof (GUInt16))
19041905 {
@@ -1930,7 +1931,7 @@ GDALDataset *JPGDatasetCommon::InitEXIFOverview()
19301931 return nullptr ;
19311932 if (bSwabflag)
19321933 CPL_SWAP32PTR (&nNextDirOff);
1933- if (nNextDirOff == 0 || nNextDirOff > UINT_MAX - nTIFFHEADER )
1934+ if (nNextDirOff == 0 )
19341935 return nullptr ;
19351936
19361937 // Seek to IFD1.
@@ -1939,8 +1940,8 @@ GDALDataset *JPGDatasetCommon::InitEXIFOverview()
19391940 sizeof (GUInt16))
19401941 {
19411942 CPLError (CE_Failure, CPLE_AppDefined,
1942- " Error reading IFD1 Directory count at %d ." ,
1943- nTIFFHEADER + nNextDirOff);
1943+ " Error reading IFD1 Directory count at %" PRIu64 " ." ,
1944+ static_cast < uint64_t >( nTIFFHEADER + nNextDirOff) );
19441945 return nullptr ;
19451946 }
19461947
@@ -2012,14 +2013,15 @@ GDALDataset *JPGDatasetCommon::InitEXIFOverview()
20122013 }
20132014 if (nCompression != 6 || nImageWidth >= nRasterXSize ||
20142015 nImageHeight >= nRasterYSize || nJpegIFOffset == 0 ||
2015- nJpegIFOffset > UINT_MAX - nTIFFHEADER ||
2016+ nTIFFHEADER > UINT_MAX || nJpegIFOffset > UINT_MAX - nTIFFHEADER ||
20162017 static_cast <int >(nJpegIFByteCount) <= 0 )
20172018 {
20182019 return nullptr ;
20192020 }
20202021
20212022 const char *pszSubfile =
2022- CPLSPrintf (" JPEG_SUBFILE:%u,%d,%s" , nTIFFHEADER + nJpegIFOffset,
2023+ CPLSPrintf (" JPEG_SUBFILE:%" PRIu64 " ,%d,%s" ,
2024+ static_cast <uint64_t >(nTIFFHEADER + nJpegIFOffset),
20232025 nJpegIFByteCount, GetDescription ());
20242026 JPGDatasetOpenArgs sArgs ;
20252027 sArgs .pszFilename = pszSubfile;
0 commit comments