diff options
author | cinap_lenrek <cinap_lenrek@gmx.de> | 2013-01-21 12:01:05 +0100 |
---|---|---|
committer | cinap_lenrek <cinap_lenrek@gmx.de> | 2013-01-21 12:01:05 +0100 |
commit | c4d7f179166b6471f9ca99208545e730a4ca9871 (patch) | |
tree | d145a5d2fb29ddb03e34265413ae4ccd37c79e9c | |
parent | e18b9f6fd2f37013abc6a1a2ae3b06f88df6426e (diff) | |
download | plan9front-c4d7f179166b6471f9ca99208545e730a4ca9871.tar.xz |
file: more sanity checking for tga image detection to avoid false positives
-rw-r--r-- | sys/src/cmd/file.c | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/sys/src/cmd/file.c b/sys/src/cmd/file.c index dd0c69737..480917168 100644 --- a/sys/src/cmd/file.c +++ b/sys/src/cmd/file.c @@ -1205,16 +1205,29 @@ istga(void) uchar *p; p = buf; - if(nbuf < 14) + if(nbuf < 18) return 0; - if(((p[2]|(1<<3)) & (~3)) != (1<<3)) /* rle flag */ - return 0; - if(p[1] == 0 && ((p[2]&3) != 2 && (p[2]&3) != 3)) /* non color-mapped */ + if((p[12] | p[13]<<8) == 0) /* width */ return 0; - if(p[1] == 1 && ((p[2]&3) != 1 || p[7] == 0)) /* color-mapped */ + if((p[14] | p[15]<<8) == 0) /* height */ return 0; if(p[16] != 8 && p[16] != 16 && p[16] != 24 && p[16] != 32) /* bpp */ return 0; + if(((p[2]|(1<<3)) & (~3)) != (1<<3)) /* rle flag */ + return 0; + if(p[1] == 0){ /* non color-mapped */ + if((p[2]&3) != 2 && (p[2]&3) != 3) + return 0; + if((p[5] | p[6]<<8) != 0) /* palette length */ + return 0; + } else + if(p[1] == 1){ /* color-mapped */ + if((p[2]&3) != 1 || p[7] == 0) + return 0; + if((p[5] | p[6]<<8) == 0) /* palette length */ + return 0; + } else + return 0; print("%s\n", mime ? "image/tga" : "targa image"); return 1; } |