Hey! I decided to create a library designed to simplify file type detection. By utilizing magic numbers to identify file types within your C# projects you can safely determine file types, rather than relying on file extensions which can be changed.
By using this you instead check against a database of known byte sequences to get the mime type of specific files. The library is designed to work with filestreams, memorystreams and a byte array for ease of use. And you can even add your own custom byte arrays.
Whether you're working on file upload validation, data parsing, or simply need to know the nature of a file, you can always reliably check using this library.
Simply pull the Nuget package from within Visual Studio and use the static MagicNumberAnalyzer.GetFileMimeType()
method.
using Martin.FileTools;
//This can also be a MemoryStream or a byte[]
using FileStream fileStream = File.OpenRead("Your-cool-file");
string fileMimeType = MagicNumberAnalyzer.GetFileMimeType(fileStream);
The following filetypes are registered by default:
.7z: application/x-7z-compressed
.avi: video/x-msvideo
.flv: video/x-flv
.m4v: video/x-m4v
.mp4: video/mp4
.svg: image/svg+xml
.pdf: application/pdf
.mp3: audio/mpeg
.wav: audio/wav
.gif: image/gif
.webm: video/webm
.webp: image/webp
.mov: video/quicktime
.png: image/png
.gz: application/gzip
.bmp: image/bmp
.ico: image/x-icon
.jpg: image/jpeg
.zip: application/zip
.rar: application/vnd.rar
.exe: application/x-msdownload
The library provides a comprehensive interface for adding custom magic numbers. You can include unique magic numbers to increase the file identification capabilities in case the default types are not enough.
Note: This only needs to be done once due to the static nature of the class and it's properties. A good place to do this would be your startup file or a method that is called once.
Suppose we have a specific magic number: 0x24 0x27 xx xx 0xF1 0xA1 0x41 xx 0xC3
. To include it into the library, follow these steps:
-
Instantiate a new
MagicNumber
class. -
We know 3 byte sequences
0x24 0x27
at offset 0,0xF1 0xA1 0x41
at offset 4 and0xC3
at offset 8. -
Add a new instance of
KnownByteSequence
for each known sequence we have. -
Utilize the
AddCustomMagicNumber
method available inMagicNumberAnalyzer
to register the custom magic number.
using Martin.FileTools;
using Martin.FileTools.Types;
//Make sure you register this only once either during startup or a method that gets called once.
MagicNumberAnalyzer.AddCustomMagicNumber(
new("Your custom mime type", [
new([0x24, 0x27], 0),
new([0xF1, 0xA1, 0x41], 4),
new([0xC3], 8)
])
);
Note that custom types are checked first so you could effectively override the registered types. That is so it doesn't interfere with your own custom magic number functionality.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also open an issue with the tag "suggestion". Don't forget to give the project a star! Thanks again!
Distributed under the MIT License. See LICENSE.txt
for more information.
Martin Yordanov - Linkedin
Project Link: https://github.com/Azmekk/Magic-Number-Analyzer