Skip to content

Commit

Permalink
heif: fix errors in deferred driver loading
Browse files Browse the repository at this point in the history
  • Loading branch information
bradh committed Dec 5, 2023
1 parent ad07518 commit 69584bb
Showing 1 changed file with 24 additions and 26 deletions.
50 changes: 24 additions & 26 deletions frmts/heif/heifdrivercore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,42 +33,37 @@
/* HEIFDriverIdentifySimplified() */
/************************************************************************/

int HEIFDriverIdentifySimplified(GDALOpenInfo *poOpenInfo)
constexpr const char *FTYP_BOX_SIGNATURE = "ftyp";
constexpr const char *MAJOR_BRANDS[] = {"heic", "heix", "avif", "jpeg", "j2ki"};
constexpr const char *MAJOR_BRANDS_MAYBE[] = {"mif1", "mif2"};

int HEIFDriverIdentifySimplified(GDALOpenInfo *poOpenInfo)
{
if (STARTS_WITH_CI(poOpenInfo->pszFilename, "HEIF:"))
return true;

if (poOpenInfo->nHeaderBytes < 12 || poOpenInfo->fpL == nullptr)
return false;

#if LIBHEIF_HAVE_VERSION(1, 4, 0)
const auto res =
heif_check_filetype(poOpenInfo->pabyHeader, poOpenInfo->nHeaderBytes);
if (res == heif_filetype_yes_supported)
return TRUE;
if (res == heif_filetype_maybe)
return -1;
if (res == heif_filetype_yes_unsupported)
if (memcmp(poOpenInfo->pabyHeader + 4, FTYP_BOX_SIGNATURE, 4) != 0)
{
CPLDebug("HEIF", "HEIF file, but not supported by libheif");
return GDAL_IDENTIFY_FALSE;
}
return FALSE;
#else
// Simplistic test...
const unsigned char abySig1[] = "ftypheic";
const unsigned char abySig2[] = "ftypmif1"
"\x00"
"\x00"
"\x00"
"\x00"
"mif1heic";
return (poOpenInfo->nHeaderBytes >= static_cast<int>(sizeof(abySig1)) &&
memcmp(poOpenInfo->pabyHeader + 4, abySig1, sizeof(abySig1)) ==
0) ||
(poOpenInfo->nHeaderBytes >= static_cast<int>(sizeof(abySig2)) &&
memcmp(poOpenInfo->pabyHeader + 4, abySig2, sizeof(abySig2)) == 0);
#endif
for (const char *brand : MAJOR_BRANDS)
{
if (memcmp(poOpenInfo->pabyHeader + 8, brand, 4) == 0)
{
return GDAL_IDENTIFY_TRUE;
}
}
for (const char *brand : MAJOR_BRANDS_MAYBE)
{
if (memcmp(poOpenInfo->pabyHeader + 8, brand, 4) == 0)
{
return GDAL_IDENTIFY_UNKNOWN;
}
}
return GDAL_IDENTIFY_FALSE;
}

/************************************************************************/
Expand All @@ -93,6 +88,9 @@ void HEIFDriverSetCommonMetadata(GDALDriver *poDriver)

poDriver->pfnIdentify = HEIFDriverIdentifySimplified;
poDriver->SetMetadataItem(GDAL_DCAP_OPEN, "YES");
#ifdef HAS_CUSTOM_FILE_WRITER
poDriver->SetMetadataItem(GDAL_DCAP_CREATECOPY, "YES");
#endif
}

/************************************************************************/
Expand Down

0 comments on commit 69584bb

Please sign in to comment.