Friday, February 11, 2011

libmagic



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>

IBM's Watson to Play on Jeopardy



This is interesting because the computer will be listening and following the conversation to participate.  With the supporting computer is 10 racks  of IBM 750 servers supporting 2,880 processors and 15 terabytes of RAM, I think we can safely say, it may be a little while before you can do this at home.  However, they are using Linux for the OS.

Some interesting comparisons between the computers and the humans:

Computer                     Humans

80 teraflops                  100 petaflops
15 terabytes                  6.4 terabytes


Below is the link to IBM's page on Watson: