magic.h (4533B)
1 /* see LICENSE file for copyright and license details */ 2 #ifndef ARUU_LIBMAGIC_H 3 #define ARUU_LIBMAGIC_H 4 5 #include <stddef.h> 6 7 /* 8 * libmagic: tell the type of a file by its bytes, as file(1) does 9 * 10 * reads the databases file(1) reads, both the magic(5) text sources 11 * and the compiled magic.mgc that file 5.40 to 5.48 write, and gives 12 * for a buffer the description file(1) prints and the mime type that 13 * file --mime-type prints 14 * 15 * the library is plain c99 and posix: it prints nothing, never exits 16 * and keeps no state outside the handle magic_open returns. a handle 17 * may be shared by threads only if they dont classify at the same 18 * time, regexes are compiled into it on first use 19 * 20 * usage: 21 * 22 * struct Magic *m; 23 * char desc[256], mime[64]; 24 * 25 * if (!(m = magic_open(NULL))) 26 * return -1; 27 * magic_buffer(m, buf, n, desc, sizeof(desc), mime, sizeof(mime)); 28 * magic_close(m); 29 */ 30 31 /* how much of a file file(1) 5.4x reads, and so what magic_buffer wants to see */ 32 #define MAGIC_BYTES_MAX (7 * 1024 * 1024) 33 34 /* how much of a file magic_charset and the text tests look at */ 35 #define MAGIC_CHARSET_MAX (64 * 1024) 36 37 /* 38 * databases tried when magic_open gets NULL and MAGIC is unset, as a 39 * colon separated list. each entry "x" loads "x.mgc" if that exists, 40 * else the text source "x", a file or a directory of files. override 41 * at build time with -DMAGIC_PATH_DEFAULT=\"...\" 42 */ 43 #ifndef MAGIC_PATH_DEFAULT 44 #define MAGIC_PATH_DEFAULT \ 45 "/usr/share/misc/magic:/usr/share/file/misc/magic:/usr/local/share/misc/magic:" \ 46 "/share/misc/magic:/share/file/magic" 47 #endif 48 49 struct Magic; 50 51 /* 52 * load the databases in path, a colon separated list where every 53 * entry that exists is loaded and entries that dont are skipped. 54 * NULL means MAGIC from the environment, else MAGIC_PATH_DEFAULT. 55 * returns NULL with errno set when nothing could be loaded: ENOENT 56 * if no entry exists, ENOEXEC if a file is not a magic database or is 57 * of an unsupported version, else the error of open, read or malloc 58 */ 59 struct Magic *magic_open(const char *path); 60 61 /* free a handle and everything loaded into it, NULL is fine */ 62 void magic_close(struct Magic *m); 63 64 /* 65 * classify the first n bytes of a file. desc gets what file(1) prints 66 * after the name, mime gets the type file --mime-type prints. either 67 * buffer may be NULL, both are truncated to fit and always terminated. 68 * "data" and application/octet-stream when nothing matches, "empty" 69 * and inode/x-empty when n is 0. returns 0, or -1 with errno set when 70 * memory runs out 71 */ 72 int magic_buffer( 73 struct Magic *m, const void *buf, size_t n, char *desc, size_t dn, char *mime, size_t mn 74 ); 75 76 /* 77 * as magic_buffer, but reads the first MAGIC_BYTES_MAX bytes of the 78 * open file fd itself and may read more of it later, as file(1) does 79 * for elf headers and for tests at offsets from the end of the file. 80 * the caller deals with what is not a regular file (directories, 81 * devices, symlinks), magic_fd only looks at the bytes. returns 0, or 82 * -1 with errno set when fd cant be read or memory runs out 83 */ 84 int magic_fd(struct Magic *m, int fd, char *desc, size_t dn, char *mime, size_t mn); 85 86 /* 87 * works out once what the loaded tests need of a file: how far each 88 * reads, and which tests give which mime type. after it the handle may 89 * be shared by threads classifying at the same time, as every regex is 90 * compiled. returns 0, or -1 with errno set when memory runs out 91 */ 92 int magic_prepare(struct Magic *m); 93 /* 94 * as magic_fd, reading no more of the file than the tests need. hint, 95 * a mime type the name suggests (NULL for none), is tried first: only 96 * the tests that can give it, over only the bytes they look at. when 97 * they give it, that is the answer; else every test runs, over the 98 * bytes already read and as many more as the tests with a known reach 99 * look at. without magic_prepare it reads as magic_fd does 100 */ 101 int magic_fd_hint( 102 struct Magic *m, int fd, const char *hint, char *desc, size_t dn, char *mime, size_t mn 103 ); 104 /* 105 * whether a test can give mime: when one can and the file did not come 106 * out so, its name was wrong to say it. 0 without magic_prepare 107 */ 108 int magic_knows(const struct Magic *m, const char *mime); 109 /* 110 * the charset file --mime prints for these bytes: "us-ascii", 111 * "utf-8", "iso-8859-1" and so on, or "binary". the result is a 112 * constant string 113 */ 114 const char *magic_charset(const void *buf, size_t n); 115 116 #endif