0

I am working in a project to process images, and I need to check the contents of the images, to make sure that the format is valid (eg: png, jpg, etc). So, I know that I can use the magic numbers or file signature. I have tried to use the libmagic-dev, but didn't work (I have Mac). I tried use the source code (I created a lib and reference in the project), but didn't work too. And I want do this in C. Someone can say me a way to get this?

int check_ext(char* filename) {
    magic_t handle = magic_open(MAGIC_NONE|MAGIC_COMPRESS);

    magic_load(handle, NULL);

    fgets(filename, sizeof(filename), stdin);
    size_t ln = strlen(filename) - 1;

    if (filename[ln] == '\n') {
        filename[ln] = '\0';
    }

    const char * type = magic_file(handle, filename);
    if (type) {
        if(strlen(type) > 3)
            return 0;
        else if(strcasecmp(type, "png") == 0)
            return 1;
        else if(strcasecmp(type, "bmp") == 0)
            return 1;
        else if(strcasecmp(type, "ppm") == 0)
            return 1;
        else if(strcasecmp(type, "pgm") == 0)
            return 1;
        else
            return 0;
    } else {
        return 0;
    }

    magic_close(handle);
    return 0;
}

Error:

Undefined symbols for architecture x86_64:
  "_magic_file", referenced from:
      _check_ext in arguments.c.o
  "_magic_load", referenced from:
      _check_ext in arguments.c.o
  "_magic_open", referenced from:
      _check_ext in arguments.c.o

0 Answers0