MIME (Multipurpose Internet Mail Extensions) type is a standard used on the internet to indicate the type of content of a file.
Originally developed to identify the types of files attached to emails, the MIME type is now widely used in different contexts, such as on the web, to indicate the type of content of files transmitted via the HTTP protocol.
Each file type is associated with a specific MIME type, which is represented by a string. For example, the MIME type for plain text files is text/plain, while the MIME type for JPEG images is image/jpeg
. There are hundreds of standard MIME types that cover a variety of file types, from text documents to audio and video files.
In this article we will see how to identify the mime-type
of a file using C++ in both GNU+Linux and as a tip on Windows.
To achieve this in distribuições GNU+Linux we will use the library libmagic.
It is the library used by the file command, which will include the magic.h header /include/uapi/linux/magic.h) which is linked in a static(static).
To install you can use your system’s package manager, some examples below:
If you don’t find it in your system’s repository, you can compile from scratch:
Remember before you have the compilation tools: gcc, make, in addition to wget to download the tarball.
For this example, let’s see the mime-type
of this image below which is in PNG format, download it by right-clicking on the image and clicking: Save as in the directory where the code binary will be .
cpp-icon.png
Create a main.cpp
file and paste the code below:
The code is properly commented explaining each block of code to clarify actions.
Once that’s done, just compile it, pass the -lmagic
flag and run:
The possible and probable output will be:
Note that if we change the file extension to any extension, even though the file manager displays an icon referring to the extension, libmagic is safe and correct in this regard, it will show the true mime-type of that file.
This happens a lot in systems where malicious people want to run code on the Web and the page asks to load only the file: jpeg and png, but they only change the extension, but in fact the file is a script.
If I rename the file to .mp4
, for example:
And change the code to load cpp-icon.mp4
:
After compiling and running, you will see that libmagic will display the CORRECT Mime-Type! and not the extension.
On Windows, you can use urlmon.h
with the code below:
Remembering that for you to be able to compile, you need to enable urlmon.dll
in the Windows Registry, as described in this link.
There are several C++ “libraries” on GitHub that do a FAKE MIME-TYPE, that is, if we do the procedure we described above of renaming the file extension, these “libraries” tell us the incorrect mime-type. This is unsafe and dangerous!
For example, I found this one https://github.com/lasselukkari/MimeTypes, if you clone it:
Use this example after renaming the extension:
Compile your file and MimeTypes.cpp
from this repository:
After running, note that it will incorrectly report the MIME type of the file:
This does not happen with libmagic
!
Not all repositories on GitHub do a fake mime-type, but be aware of these cases!