mgint.h (8763B)
1 /* see LICENSE file for copyright and license details */ 2 #ifndef ARUU_LIBMAGIC_MGINT_H 3 #define ARUU_LIBMAGIC_MGINT_H 4 5 /* internal to libmagic: shared by its source files, never by callers */ 6 7 #include "magic.h" 8 9 #include <locale.h> 10 #include <regex.h> 11 #include <stddef.h> 12 #include <stdint.h> 13 14 /* sizes of the text fields of one test line, as file(1) stores them */ 15 #define MG_STRLEN 128 16 #define MG_DESCLEN 64 17 #define MG_MIMELEN 80 18 19 /* the entry size and magic number of a compiled magic.mgc */ 20 #define MG_MAGICNO 0xF11E041Cu 21 #define MG_OLDSIZE 376 22 #define MG_NEWSIZE 432 23 #define MG_LEVELS 256 24 #define MG_SETS 2 25 26 /* search limits file(1) applies by default */ 27 #define MG_INDIR_MAX 50 28 #define MG_NAME_MAX 150 29 #define MG_REGEX_MAX 8192 30 #define MG_ENCODING_MAX MAGIC_CHARSET_MAX 31 32 /* test types, numbered as file 5.48 (format version 21) numbers them */ 33 enum MagicType { 34 MT_INVALID, 35 MT_BYTE, 36 MT_SHORT, 37 MT_DEFAULT, 38 MT_LONG, 39 MT_STRING, 40 MT_DATE, 41 MT_BESHORT, 42 MT_BELONG, 43 MT_BEDATE, 44 MT_LESHORT, 45 MT_LELONG, 46 MT_LEDATE, 47 MT_PSTRING, 48 MT_LDATE, 49 MT_BELDATE, 50 MT_LELDATE, 51 MT_REGEX, 52 MT_BESTRING16, 53 MT_LESTRING16, 54 MT_SEARCH, 55 MT_MEDATE, 56 MT_MELDATE, 57 MT_MELONG, 58 MT_QUAD, 59 MT_LEQUAD, 60 MT_BEQUAD, 61 MT_QDATE, 62 MT_LEQDATE, 63 MT_BEQDATE, 64 MT_QLDATE, 65 MT_LEQLDATE, 66 MT_BEQLDATE, 67 MT_FLOAT, 68 MT_BEFLOAT, 69 MT_LEFLOAT, 70 MT_DOUBLE, 71 MT_BEDOUBLE, 72 MT_LEDOUBLE, 73 MT_BEID3, 74 MT_LEID3, 75 MT_INDIRECT, 76 MT_QWDATE, 77 MT_LEQWDATE, 78 MT_BEQWDATE, 79 MT_NAME, 80 MT_USE, 81 MT_CLEAR, 82 MT_DER, 83 MT_GUID, 84 MT_LEGUID, 85 MT_BEGUID, 86 MT_OFFSET, 87 MT_BEVARINT, 88 MT_LEVARINT, 89 MT_MSDOSDATE, 90 MT_LEMSDOSDATE, 91 MT_BEMSDOSDATE, 92 MT_MSDOSTIME, 93 MT_LEMSDOSTIME, 94 MT_BEMSDOSTIME, 95 MT_OCTAL, 96 MT_NTYPES 97 }; 98 99 /* line flags, bit for bit as stored in magic.mgc */ 100 enum MagicFlag { 101 MF_INDIR = 0x01, 102 MF_OFFADD = 0x02, 103 MF_INDIROFFADD = 0x04, 104 MF_UNSIGNED = 0x08, 105 MF_NOSPACE = 0x10, 106 MF_BINTEST = 0x20, 107 MF_TEXTTEST = 0x40, 108 MF_OFFNEG = 0x80, 109 MF_OFFPOS = 0x100 110 }; 111 112 /* string modifiers, bit for bit as stored in magic.mgc */ 113 enum MagicStrFlag { 114 SF_COMPACT_WS = 1 << 0, 115 SF_COMPACT_OPT_WS = 1 << 1, 116 SF_IGNORE_LOWER = 1 << 2, 117 SF_IGNORE_UPPER = 1 << 3, 118 SF_REGEX_START = 1 << 4, 119 SF_TEXTTEST = 1 << 5, 120 SF_BINTEST = 1 << 6, 121 SF_PSTRING_1 = 1 << 7, 122 SF_PSTRING_2_BE = 1 << 8, 123 SF_PSTRING_2_LE = 1 << 9, 124 SF_PSTRING_4_BE = 1 << 10, 125 SF_PSTRING_4_LE = 1 << 11, 126 SF_PSTRING_SELF = 1 << 12, 127 SF_TRIM = 1 << 13, 128 SF_FULL_WORD = 1 << 14 129 }; 130 131 /* regex /l shares the bit of the 4 byte little endian pascal length */ 132 #define SF_REGEX_LINES SF_PSTRING_4_LE 133 #define SF_PSTRING_LEN \ 134 (SF_PSTRING_1 | SF_PSTRING_2_BE | SF_PSTRING_2_LE | SF_PSTRING_4_BE | SF_PSTRING_4_LE) 135 #define SF_IGNORE_CASE (SF_IGNORE_LOWER | SF_IGNORE_UPPER) 136 #define SF_INDIRECT_REL (1 << 0) 137 138 /* mask and indirection operators */ 139 enum MagicOp { OP_AND, OP_OR, OP_XOR, OP_ADD, OP_MINUS, OP_MULTIPLY, OP_DIVIDE, OP_MODULO }; 140 141 #define OP_MASK 0x07 142 #define OP_SIGNED 0x20 143 #define OP_INVERSE 0x40 144 #define OP_INDIRECT 0x80 145 146 union MagicValue { 147 uint8_t b; 148 uint16_t h; 149 uint32_t l; 150 uint64_t q; 151 int8_t sb; 152 int16_t sh; 153 int32_t sl; 154 int64_t sq; 155 float f; 156 double d; 157 unsigned char hs[2]; 158 unsigned char hl[4]; 159 unsigned char hq[8]; 160 char s[MG_STRLEN]; 161 unsigned char us[MG_STRLEN]; 162 unsigned char guid[16]; 163 }; 164 165 /* one line of magic(5) */ 166 struct MagicLine { 167 int32_t offset; 168 int32_t in_offset; 169 uint64_t mask; /* numeric types */ 170 uint32_t range; /* string types */ 171 uint32_t sflags; /* string types */ 172 uint32_t lineno; 173 uint16_t flag; 174 uint8_t cont; 175 uint8_t factor; 176 uint8_t factor_op; 177 uint8_t reln; 178 uint8_t vallen; 179 uint8_t type; 180 uint8_t in_type; 181 uint8_t in_op; 182 uint8_t mask_op; 183 uint8_t rxbad; /* regcomp failed once, dont retry */ 184 regex_t *rx; /* compiled lazily on first use */ 185 union MagicValue value; 186 char desc[MG_DESCLEN]; 187 char mime[MG_MIMELEN]; 188 }; 189 190 /* one loaded database: set 0 holds the tests, set 1 the named tests */ 191 struct MagicDb { 192 struct MagicLine *v[MG_SETS]; 193 size_t n[MG_SETS]; 194 struct MagicDb *next; 195 }; 196 197 /* a top level test and its continuations, and how far into a file they 198 * read: -1 when that is known only once the file is read */ 199 struct MagicGroup { 200 struct MagicDb *db; 201 size_t first, n; 202 long long reach; 203 }; 204 /* a mime type a group can give */ 205 struct MagicMime { 206 const char *mime; 207 size_t group; 208 }; 209 /* what magic_prepare works out: the groups, the types by name, and the 210 * farthest any group with a known reach reads */ 211 struct MagicIndex { 212 struct MagicGroup *group; 213 size_t ngroup; 214 struct MagicMime *mime; 215 size_t nmime; 216 long long reach; 217 size_t *far; /* the groups that read past MAGIC_CHARSET_MAX */ 218 size_t nfar; 219 size_t tail; /* the most any test reads back from the end */ 220 }; 221 struct Magic { 222 struct MagicDb *db; 223 locale_t cloc; /* regex and ctype run in the posix locale */ 224 struct MagicIndex *ix; /* NULL until magic_prepare */ 225 }; 226 227 /* growing output string, empty on allocation failure with err set */ 228 struct MagicOut { 229 char *s; 230 size_t n; 231 size_t cap; 232 int err; 233 }; 234 235 /* the bytes a test can see: the head of a file, or a slice of it */ 236 struct MagicBuf { 237 const unsigned char *p; 238 size_t n; 239 int file; /* negative offsets read the tail of the file being classified */ 240 }; 241 242 /* state of one classification of one file */ 243 struct MagicRun { 244 struct Magic *m; 245 struct MagicBuf head; 246 int fd; /* -1 when only the buffer is known */ 247 long long fsize; /* -1 when unknown */ 248 unsigned char *tail; /* last bytes of a large file, for negative offsets */ 249 size_t tlen; 250 int tstate; /* 0 not read, 1 read, 2 part of head, -1 unreadable */ 251 int mime; /* 1 to collect the mime type instead of the description */ 252 int xmode; /* nonzero picks the first branch of ${x?a:b} */ 253 struct MagicOut out; 254 int32_t offset; 255 int32_t eoffset; 256 union MagicValue val; 257 const unsigned char *ss; /* search and regex window */ 258 size_t slen; 259 size_t soff; 260 size_t rmlen; 261 int32_t lioff[MG_LEVELS]; 262 int ligot[MG_LEVELS]; 263 unsigned nindir; 264 unsigned nnames; 265 const size_t *pick; /* when set, only these groups of the index are tried */ 266 size_t npick; 267 }; 268 269 /* mgindex.c */ 270 void mg_index_free(struct MagicIndex *); 271 size_t mg_index_pick(const struct MagicIndex *, const char *, size_t **, long long *); 272 size_t mg_index_probe(struct MagicRun *, unsigned char *, size_t, size_t, size_t *); 273 /* mgload.c */ 274 int mg_load(struct Magic *, const char *); 275 void mg_free(struct MagicDb *); 276 int mg_isstring(int); 277 size_t mg_typesize(int); 278 uint64_t mg_signextend(const struct MagicLine *, uint64_t); 279 280 /* mgmatch.c */ 281 int mg_softmagic(struct MagicRun *, const struct MagicBuf *, int, int); 282 int mg_top_holds(struct MagicRun *, struct MagicLine *, const struct MagicBuf *); 283 int mg_find(struct Magic *, const char *, struct MagicLine **, size_t *); 284 size_t mg_pstrlen_size(uint32_t); 285 286 /* mgtext.c */ 287 int mg_looks_utf8(const unsigned char *, size_t, uint32_t *, size_t *); 288 int mg_encoding( 289 const unsigned char *buf, 290 size_t n, 291 uint32_t **ubuf, 292 size_t *ulen, 293 const char **code, 294 const char **code_mime, 295 const char **type 296 ); 297 int mg_ascmagic(struct MagicRun *, int); 298 int mg_istar(const unsigned char *, size_t); 299 int mg_isjson(const unsigned char *, size_t); 300 int mg_iscsv(const unsigned char *, size_t); 301 302 /* mgelf.c */ 303 int mg_elf(struct MagicRun *, struct MagicOut *); 304 305 /* magic.c */ 306 int mg_add(struct MagicOut *, const char *, size_t); 307 int mg_puts(struct MagicOut *, const char *); 308 int mg_printf(struct MagicOut *, const char *, ...); 309 void mg_printable(char *, size_t, const char *, size_t); 310 int mg_varexpand(char *, size_t, const char *, int); 311 int mg_replace_end(struct MagicOut *, const char *, const char *); 312 long long mg_pread(struct MagicRun *, void *, size_t, long long); 313 314 #endif