1

It is possible to determine the file type from the magic number of file?

If I've understood, the magic number can have different size, maybe a reference dictionary or something like a library could help me?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
CheapD AKA Ju
  • 713
  • 2
  • 7
  • 21

4 Answers4

1

it is possible to determine the file type from the magic number of file

yes you can ,because each file format has different magic number.

for example FFD8 for .jpg files

See here Magic Numbers in Files

Gangadhar
  • 10,248
  • 3
  • 31
  • 50
0

The file command on Linux does precisely that. Study its internals to see how it identifies files using their magic-numbers(signature bytes). The complete source-code is available at darwinsys.com/file.

The following 2 lists are the most comprehensive ones with file-types and their magic-numbers:
- File Signature Table
- Linux Magic Numbers

TheCodeArtist
  • 21,479
  • 4
  • 69
  • 130
0

JmimeMagic is a java library for such

Monish Sen
  • 1,773
  • 3
  • 20
  • 32
0

Use libmagic (apt-get install libmagic-dev on Ubuntu systems).

Example below uses the default magic database to query the file passed on the command line. (Essentially an implementation of the file command. See man libmagic for more details/functions.

#include <iostream>
#include <magic.h>
#include <cassert>
int main(int argc, char **argv) {
    if (argc == 1) {
            std::cerr << "Usage "  << argv[0] << " [filename]" << std::endl;
            return -1;
    }
    const char * fname = argv[1];
    magic_t cookie = magic_open(0);
    assert (cookie !=nullptr);
    int rc = magic_load(cookie, nullptr);
    assert(rc == 0);
    auto f=  magic_file(cookie, fname);
    if (f ==nullptr) {
        std::cerr << magic_error(cookie) << std::endl;
    } else {
        std::cout << fname << ' ' << f << std::endl;
    }

}
gerardw
  • 5,822
  • 46
  • 39