xdgmime: Add better detection for text vs. binary and zero-sized files

This detects zero-sized files to return the special-case
"application/x-zerosize" mime-type, as well as trying to differentiate
unknown file types based on their first 128 bytes of data, so that text
editors can automatically handle unknown text files.

Based on:
https://cgit.freedesktop.org/xdg/xdgmime/commit/?id=5181175d5fdaa3832b0fd094cda0120b1fe92af6
https://cgit.freedesktop.org/xdg/xdgmime/commit/?id=9c5802b8da56187c5c6abaf70042d14b12d832a9

https://bugzilla.gnome.org/show_bug.cgi?id=795544
This commit is contained in:
David Faure
2018-05-02 15:46:25 +02:00
committed by Emmanuele Bassi
parent d0a48f26c7
commit 1c177ce0ab
5 changed files with 42 additions and 7 deletions

View File

@@ -185,3 +185,18 @@ _xdg_reverse_ucs4 (xdg_unichar_t *source, int len)
}
}
const char *
_xdg_binary_or_text_fallback(const void *data, size_t len)
{
unsigned char *chardata;
int i;
chardata = (unsigned char *) data;
for (i = 0; i < 128 && i < len; ++i)
{
if (chardata[i] < 32 && chardata[i] != 9 && chardata[i] != 10 && chardata[i] != 13)
return XDG_MIME_TYPE_UNKNOWN; /* binary data */
}
return XDG_MIME_TYPE_TEXTPLAIN;
}