ref: c4d7f179166b6471f9ca99208545e730a4ca9871
parent: e18b9f6fd2f37013abc6a1a2ae3b06f88df6426e
author: cinap_lenrek <[email protected]>
date: Mon Jan 21 07:01:05 EST 2013
file: more sanity checking for tga image detection to avoid false positives
--- a/sys/src/cmd/file.c
+++ b/sys/src/cmd/file.c
@@ -1205,15 +1205,28 @@
uchar *p;
p = buf;
- if(nbuf < 14)
+ if(nbuf < 18)
return 0;
- if(((p[2]|(1<<3)) & (~3)) != (1<<3)) /* rle flag */
+ if((p[12] | p[13]<<8) == 0) /* width */
return 0;
- if(p[1] == 0 && ((p[2]&3) != 2 && (p[2]&3) != 3)) /* non color-mapped */
+ if((p[14] | p[15]<<8) == 0) /* height */
return 0;
- if(p[1] == 1 && ((p[2]&3) != 1 || p[7] == 0)) /* color-mapped */
- 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;