file.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Copyright (c) Ian F. Darwin 1986-1995.
  3. * Software written by Ian F. Darwin and others;
  4. * maintained 1995-present by Christos Zoulas and others.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice immediately at the beginning of the file, without modification,
  11. * this list of conditions, and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  20. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. /*
  29. * file.h - definitions for file(1) program
  30. * @(#)$Id: file.h,v 1.73 2005/10/20 14:59:01 christos Exp $
  31. */
  32. #ifndef __file_h__
  33. #define __file_h__
  34. #ifdef HAVE_CONFIG_H
  35. #include <config.h>
  36. #endif
  37. #include <stdio.h> /* Include that here, to make sure __P gets defined */
  38. #include <errno.h>
  39. #include <fcntl.h> /* For open and flags */
  40. #ifdef HAVE_STDINT_H
  41. #include <stdint.h>
  42. #endif
  43. #ifdef HAVE_INTTYPES_H
  44. #include <inttypes.h>
  45. #endif
  46. /* Do this here and now, because struct stat gets re-defined on solaris */
  47. #include <sys/stat.h>
  48. #ifndef MAGIC
  49. #define MAGIC "/etc/magic"
  50. #endif
  51. #ifdef __EMX__
  52. #define PATHSEP ';'
  53. #else
  54. #define PATHSEP ':'
  55. #endif
  56. #define private static
  57. #ifndef protected
  58. #define protected
  59. #endif
  60. #define public
  61. #ifndef HOWMANY
  62. # define HOWMANY (256 * 1024) /* how much of the file to look at */
  63. #endif
  64. #define MAXMAGIS 8192 /* max entries in /etc/magic */
  65. #define MAXDESC 64 /* max leng of text description */
  66. #define MAXstring 32 /* max leng of "string" types */
  67. #define MAGICNO 0xF11E041C
  68. #define VERSIONNO 2
  69. #define FILE_MAGICSIZE (32 * 4)
  70. #define FILE_LOAD 0
  71. #define FILE_CHECK 1
  72. #define FILE_COMPILE 2
  73. struct magic {
  74. /* Word 1 */
  75. uint16_t cont_level; /* level of ">" */
  76. uint8_t nospflag; /* supress space character */
  77. uint8_t flag;
  78. #define INDIR 1 /* if '>(...)' appears, */
  79. #define UNSIGNED 2 /* comparison is unsigned */
  80. #define OFFADD 4 /* if '>&' appears, */
  81. #define INDIROFFADD 8 /* if '>&(' appears, */
  82. /* Word 2 */
  83. uint8_t reln; /* relation (0=eq, '>'=gt, etc) */
  84. uint8_t vallen; /* length of string value, if any */
  85. uint8_t type; /* int, short, long or string. */
  86. uint8_t in_type; /* type of indirrection */
  87. #define FILE_BYTE 1
  88. #define FILE_SHORT 2
  89. #define FILE_LONG 4
  90. #define FILE_STRING 5
  91. #define FILE_DATE 6
  92. #define FILE_BESHORT 7
  93. #define FILE_BELONG 8
  94. #define FILE_BEDATE 9
  95. #define FILE_LESHORT 10
  96. #define FILE_LELONG 11
  97. #define FILE_LEDATE 12
  98. #define FILE_PSTRING 13
  99. #define FILE_LDATE 14
  100. #define FILE_BELDATE 15
  101. #define FILE_LELDATE 16
  102. #define FILE_REGEX 17
  103. #define FILE_BESTRING16 18
  104. #define FILE_LESTRING16 19
  105. #define FILE_SEARCH 20
  106. #define FILE_MEDATE 21
  107. #define FILE_MELDATE 22
  108. #define FILE_MELONG 23
  109. #define FILE_FORMAT_NAME \
  110. /* 0 */ "invalid 0", \
  111. /* 1 */ "byte", \
  112. /* 2 */ "short", \
  113. /* 3 */ "invalid 3", \
  114. /* 4 */ "long", \
  115. /* 5 */ "string", \
  116. /* 6 */ "date", \
  117. /* 7 */ "beshort", \
  118. /* 8 */ "belong", \
  119. /* 9 */ "bedate", \
  120. /* 10 */ "leshort", \
  121. /* 11 */ "lelong", \
  122. /* 12 */ "ledate", \
  123. /* 13 */ "pstring", \
  124. /* 14 */ "ldate", \
  125. /* 15 */ "beldate", \
  126. /* 16 */ "leldate", \
  127. /* 17 */ "regex", \
  128. /* 18 */ "bestring16", \
  129. /* 19 */ "lestring16", \
  130. /* 20 */ "search", \
  131. /* 21 */ "medate", \
  132. /* 22 */ "meldate", \
  133. /* 23 */ "melong",
  134. #define FILE_FMT_NUM "cduxXi"
  135. #define FILE_FMT_STR "s"
  136. #define FILE_FORMAT_STRING \
  137. /* 0 */ NULL, \
  138. /* 1 */ FILE_FMT_NUM, \
  139. /* 2 */ FILE_FMT_NUM, \
  140. /* 3 */ NULL, \
  141. /* 4 */ FILE_FMT_NUM, \
  142. /* 5 */ FILE_FMT_STR, \
  143. /* 6 */ FILE_FMT_STR, \
  144. /* 7 */ FILE_FMT_NUM, \
  145. /* 8 */ FILE_FMT_NUM, \
  146. /* 9 */ FILE_FMT_STR, \
  147. /* 10 */ FILE_FMT_NUM, \
  148. /* 11 */ FILE_FMT_NUM, \
  149. /* 12 */ FILE_FMT_STR, \
  150. /* 13 */ FILE_FMT_STR, \
  151. /* 14 */ FILE_FMT_STR, \
  152. /* 15 */ FILE_FMT_STR, \
  153. /* 16 */ FILE_FMT_STR, \
  154. /* 17 */ FILE_FMT_STR, \
  155. /* 18 */ FILE_FMT_STR, \
  156. /* 19 */ FILE_FMT_STR, \
  157. /* 20 */ FILE_FMT_STR, \
  158. /* 21 */ FILE_FMT_STR, \
  159. /* 22 */ FILE_FMT_STR, \
  160. /* 23 */ FILE_FMT_NUM,
  161. /* Word 3 */
  162. uint8_t in_op; /* operator for indirection */
  163. uint8_t mask_op; /* operator for mask */
  164. uint8_t dummy1;
  165. uint8_t dummy2;
  166. #define FILE_OPS "&|^+-*/%"
  167. #define FILE_OPAND 0
  168. #define FILE_OPOR 1
  169. #define FILE_OPXOR 2
  170. #define FILE_OPADD 3
  171. #define FILE_OPMINUS 4
  172. #define FILE_OPMULTIPLY 5
  173. #define FILE_OPDIVIDE 6
  174. #define FILE_OPMODULO 7
  175. #define FILE_OPINVERSE 0x40
  176. #define FILE_OPINDIRECT 0x80
  177. /* Word 4 */
  178. uint32_t offset; /* offset to magic number */
  179. /* Word 5 */
  180. int32_t in_offset; /* offset from indirection */
  181. /* Word 6 */
  182. uint32_t mask; /* mask before comparison with value */
  183. /* Word 7 */
  184. uint32_t dummy3;
  185. /* Word 8 */
  186. uint32_t dummp4;
  187. /* Words 9-16 */
  188. union VALUETYPE {
  189. uint8_t b;
  190. uint16_t h;
  191. uint32_t l;
  192. char s[MAXstring];
  193. struct {
  194. char *buf;
  195. size_t buflen;
  196. } search;
  197. uint8_t hs[2]; /* 2 bytes of a fixed-endian "short" */
  198. uint8_t hl[4]; /* 4 bytes of a fixed-endian "long" */
  199. } value; /* either number or string */
  200. /* Words 17..31 */
  201. char desc[MAXDESC]; /* description */
  202. };
  203. #define BIT(A) (1 << (A))
  204. #define STRING_IGNORE_LOWERCASE BIT(0)
  205. #define STRING_COMPACT_BLANK BIT(1)
  206. #define STRING_COMPACT_OPTIONAL_BLANK BIT(2)
  207. #define CHAR_IGNORE_LOWERCASE 'c'
  208. #define CHAR_COMPACT_BLANK 'B'
  209. #define CHAR_COMPACT_OPTIONAL_BLANK 'b'
  210. /* list of magic entries */
  211. struct mlist {
  212. struct magic *magic; /* array of magic entries */
  213. uint32_t nmagic; /* number of entries in array */
  214. int mapped; /* allocation type: 0 => apprentice_file
  215. * 1 => apprentice_map + malloc
  216. * 2 => apprentice_map + mmap */
  217. struct mlist *next, *prev;
  218. };
  219. struct magic_set {
  220. struct mlist *mlist;
  221. struct cont {
  222. size_t len;
  223. int32_t *off;
  224. } c;
  225. struct out {
  226. /* Accumulation buffer */
  227. char *buf;
  228. char *ptr;
  229. size_t len;
  230. size_t size;
  231. /* Printable buffer */
  232. char *pbuf;
  233. size_t psize;
  234. } o;
  235. int error;
  236. int flags;
  237. int haderr;
  238. const char *file;
  239. size_t line;
  240. };
  241. struct stat;
  242. protected const char *file_fmttime(uint32_t, int);
  243. protected int file_buffer(struct magic_set *, int, const void *, size_t);
  244. protected int file_fsmagic(struct magic_set *, const char *, struct stat *);
  245. protected int file_pipe2file(struct magic_set *, int, const void *, size_t);
  246. protected int file_printf(struct magic_set *, const char *, ...);
  247. protected int file_reset(struct magic_set *);
  248. protected int file_tryelf(struct magic_set *, int, const unsigned char *, size_t);
  249. protected int file_zmagic(struct magic_set *, int, const unsigned char *, size_t);
  250. protected int file_ascmagic(struct magic_set *, const unsigned char *, size_t);
  251. protected int file_is_tar(struct magic_set *, const unsigned char *, size_t);
  252. protected int file_softmagic(struct magic_set *, const unsigned char *, size_t);
  253. protected struct mlist *file_apprentice(struct magic_set *, const char *, int);
  254. protected uint32_t file_signextend(struct magic_set *, struct magic *, uint32_t);
  255. protected void file_delmagic(struct magic *, int type, size_t entries);
  256. protected void file_badread(struct magic_set *);
  257. protected void file_badseek(struct magic_set *);
  258. protected void file_oomem(struct magic_set *);
  259. protected void file_error(struct magic_set *, int, const char *, ...);
  260. protected void file_magwarn(struct magic_set *, const char *, ...);
  261. protected void file_mdump(struct magic *);
  262. protected void file_showstr(FILE *, const char *, size_t);
  263. protected size_t file_mbswidth(const char *);
  264. protected const char *file_getbuffer(struct magic_set *);
  265. #ifndef HAVE_STRERROR
  266. extern int sys_nerr;
  267. extern char *sys_errlist[];
  268. #define strerror(e) \
  269. (((e) >= 0 && (e) < sys_nerr) ? sys_errlist[(e)] : "Unknown error")
  270. #endif
  271. #ifndef HAVE_STRTOUL
  272. #define strtoul(a, b, c) strtol(a, b, c)
  273. #endif
  274. #ifndef HAVE_SNPRINTF
  275. int snprintf(char *, size_t, const char *, ...);
  276. #endif
  277. #if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
  278. #define QUICK
  279. #endif
  280. #ifndef O_BINARY
  281. #define O_BINARY 0
  282. #endif
  283. #define FILE_RCSID(id) \
  284. static const char *rcsid(const char *p) { \
  285. return rcsid(p = id); \
  286. }
  287. #else
  288. #endif /* __file_h__ */