libheif icon indicating copy to clipboard operation
libheif copied to clipboard

heif_check_filetype returns heif_filetype_maybe incorrectly?

Open ziriax opened this issue 3 years ago • 3 comments

It seems that heif_check_filetype returns heif_filetype_maybe when the header is too small? Is this correct?

Shouldn't this code

heif_filetype_result heif_check_filetype(const uint8_t* data, int len)
{
  if (len < 8) {
    return heif_filetype_maybe;
  }

  if (data[4] != 'f' ||
      data[5] != 't' ||
      data[6] != 'y' ||
      data[7] != 'p') {
    return heif_filetype_no;
  }

  if (len >= 12) {
    heif_brand brand = heif_main_brand(data, len);

    if (brand == heif_heic) {
      return heif_filetype_yes_supported;
    }
    else if (brand == heif_heix) {
      return heif_filetype_yes_supported;
    }
    else if (brand == heif_avif) {
      return heif_filetype_yes_supported;
    }
    else if (brand == heif_unknown_brand) {
      return heif_filetype_no;
    }
    else if (brand == heif_mif1) {
      return heif_filetype_maybe;
    }
    else {
      return heif_filetype_yes_unsupported;
    }
  }

  return heif_filetype_maybe;
}

become like this

heif_filetype_result heif_check_filetype(const uint8_t* data, int len)
{
  if (len < 12) {
    return heif_filetype_no;
  }

  if (data[4] != 'f' ||
      data[5] != 't' ||
      data[6] != 'y' ||
      data[7] != 'p') {
    return heif_filetype_no;
  }

  heif_brand brand = heif_main_brand(data, len);
  switch (brand) {
    case heif_heix:
    case heif_avif:
      return heif_filetype_yes_supported;
    case heif_mif1:
      return heif_filetype_maybe;
    case heif_unknown_brand: 
      return heif_filetype_no;
    default:
      return heif_filetype_yes_unsupported;
  }
}

ziriax avatar Jun 02 '21 12:06 ziriax

The original code looks good to me. It returns 'maybe' if the provided data is not enough to know and 'no'/'yes' if it can make a decision.

farindk avatar Jun 02 '21 14:06 farindk

Are you sure? It returns heif_filetype_maybe when the file is only 8 bytes long. Surely that can't be a valid HEIF file?

ziriax avatar Jun 15 '21 17:06 ziriax

len is not the total file size, but the number of bytes from the beginning that are considered for the check.

farindk avatar Jun 15 '21 17:06 farindk

Okay got it, closing

ziriax avatar Sep 10 '22 12:09 ziriax