Page MenuHomePhabricator

imlib2: Unable to load extension-less images
Closed, ResolvedPublic

Description

Hi,

Currently the imlib_load_image function is unable to handle extension-less gif, webp and tiff files. However extension-less jpg and png works fine.

I did not delve too deep into the imlib codebase so I might be wrong, but are we checking filetype via extension? If so I suggest switching to a more robust method. Libwebp has a WebPGetInfo function for this. This is currently what we are doing on nsxiv.

bool is_webp(const char *path)
{
	/* The size (in bytes) of the largest amount of data required to verify a WebP image. */
	const unsigned int max = 30;
	const unsigned char fmt[max];
	bool ret = false;
	FILE *f;

	if ((f = fopen(path, "rb")) == NULL)
		return false;
	if (fread((unsigned char *) fmt, 1, max, f) == max)
		ret = WebPGetInfo(fmt, max, NULL, NULL);
	fclose(f);
	return ret;
}

I don't think giflib has any similar function but it can be very trivially written.

bool is_gif(const char *path)
{
	FILE *f;
	bool ret = false;
	const unsigned int max = 6;
	const unsigned char fmt[max];
	if ((f = fopen(path, "rb")) == NULL)
		return false;
	if (fread((unsigned char *) fmt, 1, max, f) == max)
		ret = memcmp(fmt, "GIF89a", max) == 0 || memcmp(fmt, "GIF87a", max) == 0;
	fclose(f);
	return ret;
}

It would make our life much easier if imlib2 is able to properly cope with extension-less files. If needed I'm willing to work on this and provide necessary patches.

NRK created this task.Sep 15 2021, 3:21 PM
kwo added a comment.Sep 15 2021, 8:28 PM

This problem was just fixed in git (yesterday). It was a regression since 1.6.x.
Imlib2 uses the file name extension only as a first guess. After that it tries all loaders.
All loaders do signature checks like you describe.
Thanks for reporting :)

NRK added a comment.Sep 16 2021, 2:53 AM

Thank you, that makes things much simpler on our end!

kwo closed this task as Resolved.Sep 16 2021, 8:39 AM

Already fixed in git. Will be in v1.7.4.