mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-24 08:52:18 +01:00
Add magic_number_guess_fourcc prefix_closed arg
This commit is contained in:
1 parent
546af5a7c3
commit
f0412122be
6 files changed
+91
-39
No files matched your search
@@ -1418,13 +1418,19 @@ wuffs_base__table__flattened_length(size_t width,
|
||||
// ---------------- Magic Numbers
|
||||
|
||||
// wuffs_base__magic_number_guess_fourcc guesses the file format of some data,
|
||||
// given its opening bytes. It returns a positive FourCC value on success.
|
||||
// given its starting bytes (the prefix_data argument) and whether or not there
|
||||
// may be further bytes (the prefix_closed argument; true means that
|
||||
// prefix_data is the entire data).
|
||||
//
|
||||
// It returns a positive FourCC value on success.
|
||||
//
|
||||
// It returns zero if nothing matches its hard-coded list of 'magic numbers'.
|
||||
//
|
||||
// It returns a negative value if a longer prefix is required for a conclusive
|
||||
// result. For example, seeing a single 'B' byte is not enough to discriminate
|
||||
// the BMP and BPG image file formats.
|
||||
// It returns a negative value if prefix_closed is false and a longer prefix is
|
||||
// required for a conclusive result. For example, a single 'B' byte (without
|
||||
// further data) is not enough to discriminate the BMP and BPG image file
|
||||
// formats. Similarly, a single '\xFF' byte might be the start of JPEG data or
|
||||
// it might be the start of some other binary data.
|
||||
//
|
||||
// It does not do a full validity check. Like any guess made from a short
|
||||
// prefix of the data, it may return false positives. Data that starts with 99
|
||||
@@ -1439,4 +1445,5 @@ wuffs_base__table__flattened_length(size_t width,
|
||||
// function requires the WUFFS_CONFIG__MODULE__BASE__MAGIC sub-module, not just
|
||||
// WUFFS_CONFIG__MODULE__BASE__CORE.
|
||||
WUFFS_BASE__MAYBE_STATIC int32_t //
|
||||
wuffs_base__magic_number_guess_fourcc(wuffs_base__slice_u8 prefix);
|
||||
wuffs_base__magic_number_guess_fourcc(wuffs_base__slice_u8 prefix_data,
|
||||
bool prefix_closed);
|
||||
@@ -23,7 +23,8 @@
|
||||
// See:
|
||||
// - https://docs.fileformat.com/image/ico/
|
||||
static int32_t //
|
||||
wuffs_base__magic_number_guess_fourcc__maybe_ico(wuffs_base__slice_u8 prefix) {
|
||||
wuffs_base__magic_number_guess_fourcc__maybe_ico(wuffs_base__slice_u8 prefix,
|
||||
bool prefix_closed) {
|
||||
// Allow-list for the Image Type field.
|
||||
if (prefix.len < 4) {
|
||||
return -1;
|
||||
@@ -66,7 +67,8 @@ wuffs_base__magic_number_guess_fourcc__maybe_ico(wuffs_base__slice_u8 prefix) {
|
||||
// - https://docs.fileformat.com/image/tga/
|
||||
// - https://www.dca.fee.unicamp.br/~martino/disciplinas/ea978/tgaffs.pdf
|
||||
static int32_t //
|
||||
wuffs_base__magic_number_guess_fourcc__maybe_tga(wuffs_base__slice_u8 prefix) {
|
||||
wuffs_base__magic_number_guess_fourcc__maybe_tga(wuffs_base__slice_u8 prefix,
|
||||
bool prefix_closed) {
|
||||
// Allow-list for the Image Type field.
|
||||
if (prefix.len < 3) {
|
||||
return -1;
|
||||
@@ -120,7 +122,8 @@ wuffs_base__magic_number_guess_fourcc__maybe_tga(wuffs_base__slice_u8 prefix) {
|
||||
}
|
||||
|
||||
WUFFS_BASE__MAYBE_STATIC int32_t //
|
||||
wuffs_base__magic_number_guess_fourcc(wuffs_base__slice_u8 prefix) {
|
||||
wuffs_base__magic_number_guess_fourcc(wuffs_base__slice_u8 prefix,
|
||||
bool prefix_closed) {
|
||||
// This is similar to (but different from):
|
||||
// - the magic/Magdir tables under https://github.com/file/file
|
||||
// - the MIME Sniffing algorithm at https://mimesniff.spec.whatwg.org/
|
||||
@@ -189,7 +192,8 @@ wuffs_base__magic_number_guess_fourcc(wuffs_base__slice_u8 prefix) {
|
||||
if (prefix.len < 2) {
|
||||
return -1;
|
||||
} else if ((prefix.ptr[1] == 0x00) || (prefix.ptr[1] == 0x01)) {
|
||||
return wuffs_base__magic_number_guess_fourcc__maybe_tga(prefix);
|
||||
return wuffs_base__magic_number_guess_fourcc__maybe_tga(prefix,
|
||||
prefix_closed);
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -214,11 +218,13 @@ match:
|
||||
// identifier, so we have to use heuristics (where the order matters, the
|
||||
// same as /usr/bin/file's magic/Magdir tables) as best we can. Maybe
|
||||
// it's TGA, ICO/CUR, etc. Maybe it's something else.
|
||||
int32_t tga = wuffs_base__magic_number_guess_fourcc__maybe_tga(prefix);
|
||||
int32_t tga = wuffs_base__magic_number_guess_fourcc__maybe_tga(
|
||||
prefix, prefix_closed);
|
||||
if (tga != 0) {
|
||||
return tga;
|
||||
}
|
||||
int32_t ico = wuffs_base__magic_number_guess_fourcc__maybe_ico(prefix);
|
||||
int32_t ico = wuffs_base__magic_number_guess_fourcc__maybe_ico(
|
||||
prefix, prefix_closed);
|
||||
if (ico != 0) {
|
||||
return ico;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user