http://www.astro.keele.ac.uk/oldusers/rno/Computing/File_magic.html
http://en.wikipedia.org/wiki/List_of_file_signatures
Image files
Bitmap format | .bmp | 42 4d | BM |
FITS format | .fits | 53 49 4d 50 4c 45 | SIMPLE |
GIF format | .gif | 47 49 46 38 | GIF8 |
Graphics Kernel System | .gks | 47 4b 53 4d | GKSM |
IRIS rgb format | .rgb | 01 da | .. |
ITC (CMU WM) format | .itc | f1 00 40 bb | .... |
JPEG File Interchange Format | .jpg | ff d8 ff e0 | .... |
NIFF (Navy TIFF) | .nif | 49 49 4e 31 | IIN1 |
PM format | .pm | 56 49 45 57 | VIEW |
PNG format | .png | 89 50 4e 47 | .PNG |
Postscript format | .[e]ps | 25 21 | %! |
Sun Rasterfile | .ras | 59 a6 6a 95 | Y.j. |
Targa format | .tga | xx xx xx | ... |
TIFF format (Motorola - big endian) | .tif | 4d 4d 00 2a | MM.* |
TIFF format (Intel - little endian) | .tif | 49 49 2a 00 | II*. |
X11 Bitmap format | .xbm | xx xx | |
XCF Gimp file structure | .xcf | 67 69 6d 70 20 78 63 66 20 76 | gimp xcf |
Xfig format | .fig | 23 46 49 47 | #FIG |
XPM format | .xpm | 2f 2a 20 58 50 4d 20 2a 2f | /* XPM */ |
Compressed files
Bzip | .bz | 42 5a | BZ |
Compress | .Z | 1f 9d | .. |
gzip format | .gz | 1f 8b | .. |
pkzip format | .zip | 50 4b 03 04 | PK.. |
Archive files
TAR (pre-POSIX) | .tar | xx xx | (a filename) |
TAR (POSIX) | .tar | 75 73 74 61 72 | ustar (offset by 257 bytes) |
Excecutable files
MS-DOS, OS/2 or MS Windows | 4d 5a | MZ | |
Unix elf | 7f 45 4c 46 | .ELF |
Miscellaneous files
pgp public ring | 99 00 | .. | |
pgp security ring | 95 01 | .. | |
pgp security ring | 95 00 | .. | |
pgp encrypted data | a6 00 | ¦. |
由于二进制文件有自己的magic number,只需要根据magic number则可以判断是否为image。
def is_a_media_resource body data = Base64.decode64 body bytes = data.bytes.to_a bytes = bytes[0...9] hex_bytes = [] bytes.each{|e| hex_bytes << e.to_s(16)} return is_media? hex_bytes end
经过base64解密后,提取前9个字节进行比对,因为magic number最长是9个字节。
module ReceiverHelper MAGIC_NUMBERS ={ "PNG"=>["89","50","4e","47"], "GIF"=>["47","49","46","38"], "FITS"=>["53","49","4d","50","4c", "45"], "GKS"=>["47","4b","53","4d"], "RGB"=>["01","da"], "ITC"=>["f1","00" ,"40","bb"], "JPG"=>["ff","d8","ff","e0"], "NIF"=>["49","49","4e","31"], "PM"=>["56","49","45","57"], "EPS"=>["25","21"], "RAS"=>["59", "a6", "6a", "95"], "TIF_BIGENDIAN"=>["4d", "4d", "00", "2a"], "TIF_LITTLEEDIAN"=>["49" ,"49", "2a", "00"], "XCF"=>["67", "69", "6d", "70", "20", "78", "63", "66", "20", "76"], "FIG"=>["23", "46", "49", "47"], "XPM"=>["2f", "2a", "20", "58", "50", "4d", "20", "2a", "2f"] } def is_media? data MAGIC_NUMBERS.each_pair{|type,numbers| if equals(data,numbers) return true end } false end private def equals data,png_magic_numbers data[0...png_magic_numbers.length] == png_magic_numbers end end