diff --git a/internal/cgen/base/magic-submodule.c b/internal/cgen/base/magic-submodule.c index 49b99f19..beb3e8a1 100644 --- a/internal/cgen/base/magic-submodule.c +++ b/internal/cgen/base/magic-submodule.c @@ -56,6 +56,69 @@ wuffs_base__magic_number_guess_fourcc__maybe_ico(wuffs_base__slice_u8 prefix) { return 0x49434F20; // 'ICO 'be } +// TGA doesn't start with a magic identifier. Instead, see if the opening bytes +// are plausibly TGA. +// +// Callers should have already verified that (prefix.len >= 2) and the second +// byte (prefix.ptr[1], the Color Map Type byte), is either 0x00 or 0x01. +// +// See: +// - 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) { + // Allow-list for the Image Type field. + if (prefix.len < 3) { + return -1; + } + switch (prefix.ptr[2]) { + case 0x01: + case 0x02: + case 0x03: + case 0x09: + case 0x0A: + case 0x0B: + break; + default: + // TODO: 0x20 and 0x21 are invalid, according to the spec, but are + // apparently unofficial extensions. + return 0; + } + + // Allow-list for the Color Map Entry Size field. + if (prefix.len < 8) { + return -1; + } + switch (prefix.ptr[7]) { + case 0x00: + case 0x0F: + case 0x10: + case 0x18: + case 0x20: + break; + default: + return 0; + } + + // Allow-list for the Pixel Depth field. + if (prefix.len < 17) { + return -1; + } + switch (prefix.ptr[16]) { + case 0x01: + case 0x08: + case 0x0F: + case 0x10: + case 0x18: + case 0x20: + break; + default: + return 0; + } + + return 0x54474120; // 'TGA 'be +} + WUFFS_BASE__MAYBE_STATIC int32_t // wuffs_base__magic_number_guess_fourcc(wuffs_base__slice_u8 prefix) { // This is similar to (but different from): @@ -122,6 +185,13 @@ 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 0; match: @@ -144,6 +214,10 @@ 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); + if (tga != 0) { + return tga; + } int32_t ico = wuffs_base__magic_number_guess_fourcc__maybe_ico(prefix); if (ico != 0) { return ico; diff --git a/lang/builtin/builtin.go b/lang/builtin/builtin.go index 19f6eb5e..e86124a1 100644 --- a/lang/builtin/builtin.go +++ b/lang/builtin/builtin.go @@ -76,6 +76,7 @@ var FourCCs = [...][2]string{ {"SVG ", "Scalable Vector Graphics"}, {"TAR ", "Tape Archive"}, {"TEXT", "Text"}, + {"TGA ", "Truevision Advanced Raster Graphics Adapter"}, {"TIFF", "Tagged Image File Format"}, {"TOML", "Tom's Obvious Minimal Language"}, {"WAVE", "Waveform"}, diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 453ca220..807e2388 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -716,6 +716,9 @@ typedef struct wuffs_base__transform__output__struct { // Text. #define WUFFS_BASE__FOURCC__TEXT 0x54455854 +// Truevision Advanced Raster Graphics Adapter. +#define WUFFS_BASE__FOURCC__TGA 0x54474120 + // Tagged Image File Format. #define WUFFS_BASE__FOURCC__TIFF 0x54494646 @@ -15614,6 +15617,69 @@ wuffs_base__magic_number_guess_fourcc__maybe_ico(wuffs_base__slice_u8 prefix) { return 0x49434F20; // 'ICO 'be } +// TGA doesn't start with a magic identifier. Instead, see if the opening bytes +// are plausibly TGA. +// +// Callers should have already verified that (prefix.len >= 2) and the second +// byte (prefix.ptr[1], the Color Map Type byte), is either 0x00 or 0x01. +// +// See: +// - 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) { + // Allow-list for the Image Type field. + if (prefix.len < 3) { + return -1; + } + switch (prefix.ptr[2]) { + case 0x01: + case 0x02: + case 0x03: + case 0x09: + case 0x0A: + case 0x0B: + break; + default: + // TODO: 0x20 and 0x21 are invalid, according to the spec, but are + // apparently unofficial extensions. + return 0; + } + + // Allow-list for the Color Map Entry Size field. + if (prefix.len < 8) { + return -1; + } + switch (prefix.ptr[7]) { + case 0x00: + case 0x0F: + case 0x10: + case 0x18: + case 0x20: + break; + default: + return 0; + } + + // Allow-list for the Pixel Depth field. + if (prefix.len < 17) { + return -1; + } + switch (prefix.ptr[16]) { + case 0x01: + case 0x08: + case 0x0F: + case 0x10: + case 0x18: + case 0x20: + break; + default: + return 0; + } + + return 0x54474120; // 'TGA 'be +} + WUFFS_BASE__MAYBE_STATIC int32_t // wuffs_base__magic_number_guess_fourcc(wuffs_base__slice_u8 prefix) { // This is similar to (but different from): @@ -15680,6 +15746,13 @@ 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 0; match: @@ -15702,6 +15775,10 @@ 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); + if (tga != 0) { + return tga; + } int32_t ico = wuffs_base__magic_number_guess_fourcc__maybe_ico(prefix); if (ico != 0) { return ico; diff --git a/test/data/bricks-color.tga b/test/data/bricks-color.tga new file mode 100644 index 00000000..653641a7 Binary files /dev/null and b/test/data/bricks-color.tga differ diff --git a/test/data/bricks-gray.tga b/test/data/bricks-gray.tga new file mode 100644 index 00000000..b9e3d2e8 Binary files /dev/null and b/test/data/bricks-gray.tga differ diff --git a/test/data/bricks-nodither.tga b/test/data/bricks-nodither.tga new file mode 100644 index 00000000..8e3d6623 Binary files /dev/null and b/test/data/bricks-nodither.tga differ