using System; using System.Collections.Generic; using System.Collections; namespace AmicEditor { public class Magic { public List ImageSignatures=new List(); public List ImageMaskSignatures=new List(); public List AudioSignatures=new List(); public List AudioMaskSignatures=new List(); public Magic () { // Llista completa a http://www.astro.keele.ac.uk/oldusers/rno/Computing/File_magic.html // Images PNG byte[] magicPNGBytes={137,80,78,71,13,10,26,10}; byte[] magicPNGMask={255,255,255,255,255,255,255,255}; ImageSignatures.Add(magicPNGBytes); ImageMaskSignatures.Add(magicPNGMask); // Images BMP byte[] magicBMPBytes={66,77,0,0,0,0,0,0}; byte[] magicBMPMask={255,255,0,0,0,0,0,0}; ImageSignatures.Add(magicBMPBytes); ImageMaskSignatures.Add(magicBMPMask); // Images GIF byte[] magicGIFBytes={71,73,70,56,0,0,0,0}; byte[] magicGIFMask={255,255,255,255,0,0,0,0}; ImageSignatures.Add(magicGIFBytes); ImageMaskSignatures.Add(magicGIFMask); // Images JPEG byte[] magicJPGBytes={255,216,255,224,0,0,0,0}; byte[] magicJPGMask={255,255,255,255,0,0,0,0}; ImageSignatures.Add(magicJPGBytes); ImageMaskSignatures.Add(magicJPGMask); // Images TIFF (Intel) byte[] magicTIFBytes={73,73,42,0,0,0,0,0}; byte[] magicTIFMask={255,255,255,255,0,0,0,0}; ImageSignatures.Add(magicTIFBytes); ImageMaskSignatures.Add(magicTIFMask); // Images TGA byte[] magicTGABytes={84,82,85,69,86,73,83,73}; byte[] magicTGAMask={255,255,255,255,255,255,255,255}; ImageSignatures.Add(magicTGABytes); ImageMaskSignatures.Add(magicTGAMask); // Arxius d'audio // Arxius MP3 byte[] magicMP3Bytes={73,68,51,0,0,0,0,0}; byte[] magicMP3Mask={255,255,255,0,0,0,0,0}; AudioSignatures.Add(magicMP3Bytes); AudioMaskSignatures.Add(magicMP3Mask); byte[] magicMP3MonoBytes={255,250,144,192,1,207,0,0}; byte[] magicMP3MonoMask={255,255,255,255,255,255,0,0}; AudioSignatures.Add(magicMP3MonoBytes); AudioMaskSignatures.Add(magicMP3MonoMask); byte[] magicMP3StereoBytes={255,251,144,108,0,0,0,0}; byte[] magicMP3StereoMask={255,255,255,255,0,0,0,0}; AudioSignatures.Add(magicMP3StereoBytes); AudioMaskSignatures.Add(magicMP3StereoMask); // Arxius OGG byte[] magicOGGBytes={79,103,103,83,0,2,0,0}; byte[] magicOGGMask={255,255,255,255,255,255,255,255}; AudioSignatures.Add(magicOGGBytes); AudioMaskSignatures.Add(magicOGGMask); // Arxius WAV byte[] magicWAVBytes={87,65,86,69,102,109,116,32}; byte[] magicWAVMask={255,255,255,255,255,255,255,255}; AudioSignatures.Add(magicWAVBytes); AudioMaskSignatures.Add(magicWAVMask); byte[] magicWAV2Bytes={82,73,70,70,168,173,3,0}; byte[] magicWAV2Mask={255,255,255,255,255,255,255,255}; AudioSignatures.Add(magicWAV2Bytes); AudioMaskSignatures.Add(magicWAV2Mask); } public bool Contains(List Signatures, List Masks, byte[] magic){ if (magic.Length<8) return false; //bool trobat=false; bool coincideix_el_byte; for(int i=0;i8) i=f.Read(magic,0,8); if (i>=8) for(i=0;i<8;i++) Console.Write(magic[i]+" "); Console.WriteLine(""); // I els comprovem amb les diferents signatures... if (Contains(ImageSignatures,ImageMaskSignatures, magic)) return true; else return false; /*Console.WriteLine("Magic: "+i); if (i>=8) for(i=0;i<8;i++) Console.Write(magic[i]+" "); Console.WriteLine(""); magic=ImageSignatures[0]; i=0; Console.WriteLine("MagicLIST: "+ImageSignatures); for(i=0;i<8;i++) Console.Write(magic[i]+" "); Console.WriteLine(""); */ //return true; } public bool isAudioFile(string Filename){ byte[] magic={0,0,0,0,0,0,0,0}; if (!System.IO.File.Exists(Filename)) { Console.WriteLine("Magic Error: File "+Filename+" does nos exists!"); return false; } // Si no existeix tornem fals // Capturem els 8 primers bytes System.IO.FileStream f; try{ f=System.IO.File.OpenRead(Filename); } catch (Exception ex){ Console.WriteLine("Exception Reading File "+ex); return false; } int i=0; if(f.Length>8) i=f.Read(magic,0,8); if (i>=8) for(i=0;i<8;i++) Console.Write(magic[i]+" "); Console.WriteLine(""); // I els comprovem amb les diferents signatures... if (Contains(AudioSignatures,AudioMaskSignatures, magic)) return true; else return false; /*Console.WriteLine("Magic: "+i); if (i>=8) for(i=0;i<8;i++) Console.Write(magic[i]+" "); Console.WriteLine(""); magic=ImageSignatures[0]; i=0; Console.WriteLine("MagicLIST: "+ImageSignatures); for(i=0;i<8;i++) Console.Write(magic[i]+" "); Console.WriteLine(""); return true;*/ } } }