A real nice article that go me started with using libmagic or magic.h.
<code>#include <stdio.h>#include <magic.h>int main(void){ char *actual_file = "/file/you/want.yay"; const char *magic_full; magic_t magic_cookie; /*MAGIC_MIME tells magic to return a mime of the file, but you can specify different things*/ magic_cookie = magic_open(MAGIC_MIME); if (magic_cookie == NULL) { printf("unable to initialize magic library\n"); return 1; } printf("Loading default magic database\n"); if (magic_load(magic_cookie, NULL) != 0) { printf("cannot load magic database - %s\n", magic_error(magic_cookie)); magic_close(magic_cookie); return 1; } magic_full = magic_file(magic_cookie, actual_file); printf("%s\n", magic_full); magic_close(magic_cookie); return 0;}</code>To compile it do: "gcc magic_test.c -o magics -lmagic". Furthermore make sure the libmagic library is installed. "apt-get install libmagic-dev" should do it if you're using a machine with debian/ubuntu.
Below is my magic_buff testing:
<code>
#include <stdio.h>
#include <magic.h>
int main(void)
{
printf("In main()\n");
char *actual_file = "hello.c";
const char *magic_full;
magic_t magic_cookie;
int BUF_SIZE = 8388608; // 2**23
char buf[BUF_SIZE];
magic_cookie = magic_open(MAGIC_MIME);
if (magic_cookie == NULL)
{
printf("Unable to initialize magic library\n");
return 1;
}
if (magic_load(magic_cookie, NULL) != 0)
{
printf("Cannot load magic database - %s\n", magic_error(magic_cookie));
magic_close(magic_cookie);
return 1;
}
//magic_full = magic_file(magic_cookie, actual_file);
magic_full = magic_buffer(magic_cookie, (const void *) &buf, BUF_SIZE);
printf("%s\n", magic_full);
magic_close(magic_cookie);
return 0;
}
</code>

No comments:
Post a Comment