file.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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.70 2005/07/29 17:57:20 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. #ifdef HAVE_STDINT_H
  40. #include <stdint.h>
  41. #endif
  42. #ifdef HAVE_INTTYPES_H
  43. #include <inttypes.h>
  44. #endif
  45. /* Do this here and now, because struct stat gets re-defined on solaris */
  46. #include <sys/stat.h>
  47. #ifndef MAGIC
  48. #define MAGIC "/etc/magic"
  49. #endif
  50. #ifdef __EMX__
  51. #define PATHSEP ';'
  52. #else
  53. #define PATHSEP ':'
  54. #endif
  55. #define private static
  56. #ifndef protected
  57. #define protected
  58. #endif
  59. #define public
  60. #ifndef HOWMANY
  61. # define HOWMANY (256 * 1024) /* how much of the file to look at */
  62. #endif
  63. #define MAXMAGIS 8192 /* max entries in /etc/magic */
  64. #define MAXDESC 64 /* max leng of text description */
  65. #define MAXstring 32 /* max leng of "string" types */
  66. #define MAGICNO 0xF11E041C
  67. #define VERSIONNO 2
  68. #define FILE_MAGICSIZE (32 * 4)
  69. #define FILE_LOAD 0
  70. #define FILE_CHECK 1
  71. #define FILE_COMPILE 2
  72. struct magic {
  73. /* Word 1 */
  74. uint16_t cont_level; /* level of ">" */
  75. uint8_t nospflag; /* supress space character */
  76. uint8_t flag;
  77. #define INDIR 1 /* if '>(...)' appears, */
  78. #define UNSIGNED 2 /* comparison is unsigned */
  79. #define OFFADD 4 /* if '>&' appears, */
  80. #define INDIROFFADD 8 /* if '>&(' appears, */
  81. /* Word 2 */
  82. uint8_t reln; /* relation (0=eq, '>'=gt, etc) */
  83. uint8_t vallen; /* length of string value, if any */
  84. uint8_t type; /* int, short, long or string. */
  85. uint8_t in_type; /* type of indirrection */
  86. #define FILE_BYTE 1
  87. #define FILE_SHORT 2
  88. #define FILE_LONG 4
  89. #define FILE_STRING 5
  90. #define FILE_DATE 6
  91. #define FILE_BESHORT 7
  92. #define FILE_BELONG 8
  93. #define FILE_BEDATE 9
  94. #define FILE_LESHORT 10
  95. #define FILE_LELONG 11
  96. #define FILE_LEDATE 12
  97. #define FILE_PSTRING 13
  98. #define FILE_LDATE 14
  99. #define FILE_BELDATE 15
  100. #define FILE_LELDATE 16
  101. #define FILE_REGEX 17
  102. #define FILE_BESTRING16 18
  103. #define FILE_LESTRING16 19
  104. #define FILE_SEARCH 20
  105. #define FILE_FORMAT_NAME \
  106. /* 0 */ "invalid 0", \
  107. /* 1 */ "byte", \
  108. /* 2 */ "short", \
  109. /* 3 */ "invalid 3", \
  110. /* 4 */ "long", \
  111. /* 5 */ "string", \
  112. /* 6 */ "date", \
  113. /* 7 */ "beshort", \
  114. /* 8 */ "belong", \
  115. /* 9 */ "bedate", \
  116. /* 10 */ "leshort", \
  117. /* 11 */ "lelong", \
  118. /* 12 */ "ledate", \
  119. /* 13 */ "pstring", \
  120. /* 14 */ "ldate", \
  121. /* 15 */ "beldate", \
  122. /* 16 */ "leldate", \
  123. /* 17 */ "regex", \
  124. /* 18 */ "bestring16", \
  125. /* 19 */ "lestring16", \
  126. /* 20 */ "search",
  127. #define FILE_FMT_NUM "cduxXi"
  128. #define FILE_FMT_STR "s"
  129. #define FILE_FORMAT_STRING \
  130. /* 0 */ NULL, \
  131. /* 1 */ FILE_FMT_NUM, \
  132. /* 2 */ FILE_FMT_NUM, \
  133. /* 3 */ NULL, \
  134. /* 4 */ FILE_FMT_NUM, \
  135. /* 5 */ FILE_FMT_STR, \
  136. /* 6 */ FILE_FMT_STR, \
  137. /* 7 */ FILE_FMT_NUM, \
  138. /* 8 */ FILE_FMT_NUM, \
  139. /* 9 */ FILE_FMT_STR, \
  140. /* 10 */ FILE_FMT_NUM, \
  141. /* 11 */ FILE_FMT_NUM, \
  142. /* 12 */ FILE_FMT_STR, \
  143. /* 13 */ FILE_FMT_STR, \
  144. /* 14 */ FILE_FMT_STR, \
  145. /* 15 */ FILE_FMT_STR, \
  146. /* 16 */ FILE_FMT_STR, \
  147. /* 17 */ FILE_FMT_STR, \
  148. /* 18 */ FILE_FMT_STR, \
  149. /* 19 */ FILE_FMT_STR, \
  150. /* 20 */ FILE_FMT_STR,
  151. /* Word 3 */
  152. uint8_t in_op; /* operator for indirection */
  153. uint8_t mask_op; /* operator for mask */
  154. uint8_t dummy1;
  155. uint8_t dummy2;
  156. #define FILE_OPS "&|^+-*/%"
  157. #define FILE_OPAND 0
  158. #define FILE_OPOR 1
  159. #define FILE_OPXOR 2
  160. #define FILE_OPADD 3
  161. #define FILE_OPMINUS 4
  162. #define FILE_OPMULTIPLY 5
  163. #define FILE_OPDIVIDE 6
  164. #define FILE_OPMODULO 7
  165. #define FILE_OPINVERSE 0x40
  166. #define FILE_OPINDIRECT 0x80
  167. /* Word 4 */
  168. uint32_t offset; /* offset to magic number */
  169. /* Word 5 */
  170. int32_t in_offset; /* offset from indirection */
  171. /* Word 6 */
  172. uint32_t mask; /* mask before comparison with value */
  173. /* Word 7 */
  174. uint32_t dummy3;
  175. /* Word 8 */
  176. uint32_t dummp4;
  177. /* Words 9-16 */
  178. union VALUETYPE {
  179. uint8_t b;
  180. uint16_t h;
  181. uint32_t l;
  182. char s[MAXstring];
  183. struct {
  184. char *buf;
  185. size_t buflen;
  186. } search;
  187. uint8_t hs[2]; /* 2 bytes of a fixed-endian "short" */
  188. uint8_t hl[4]; /* 4 bytes of a fixed-endian "long" */
  189. } value; /* either number or string */
  190. /* Words 17..31 */
  191. char desc[MAXDESC]; /* description */
  192. };
  193. #define BIT(A) (1 << (A))
  194. #define STRING_IGNORE_LOWERCASE BIT(0)
  195. #define STRING_COMPACT_BLANK BIT(1)
  196. #define STRING_COMPACT_OPTIONAL_BLANK BIT(2)
  197. #define CHAR_IGNORE_LOWERCASE 'c'
  198. #define CHAR_COMPACT_BLANK 'B'
  199. #define CHAR_COMPACT_OPTIONAL_BLANK 'b'
  200. /* list of magic entries */
  201. struct mlist {
  202. struct magic *magic; /* array of magic entries */
  203. uint32_t nmagic; /* number of entries in array */
  204. int mapped; /* allocation type: 0 => apprentice_file
  205. * 1 => apprentice_map + malloc
  206. * 2 => apprentice_map + mmap */
  207. struct mlist *next, *prev;
  208. };
  209. struct magic_set {
  210. struct mlist *mlist;
  211. struct cont {
  212. size_t len;
  213. int32_t *off;
  214. } c;
  215. struct out {
  216. /* Accumulation buffer */
  217. char *buf;
  218. char *ptr;
  219. size_t len;
  220. size_t size;
  221. /* Printable buffer */
  222. char *pbuf;
  223. size_t psize;
  224. } o;
  225. int error;
  226. int flags;
  227. int haderr;
  228. const char *file;
  229. size_t line;
  230. };
  231. struct stat;
  232. protected const char *file_fmttime(uint32_t, int);
  233. protected int file_buffer(struct magic_set *, int, const void *, size_t);
  234. protected int file_fsmagic(struct magic_set *, const char *, struct stat *);
  235. protected int file_pipe2file(struct magic_set *, int, const void *, size_t);
  236. protected int file_printf(struct magic_set *, const char *, ...);
  237. protected int file_reset(struct magic_set *);
  238. protected int file_tryelf(struct magic_set *, int, const unsigned char *, size_t);
  239. protected int file_zmagic(struct magic_set *, int, const unsigned char *, size_t);
  240. protected int file_ascmagic(struct magic_set *, const unsigned char *, size_t);
  241. protected int file_is_tar(struct magic_set *, const unsigned char *, size_t);
  242. protected int file_softmagic(struct magic_set *, const unsigned char *, size_t);
  243. protected struct mlist *file_apprentice(struct magic_set *, const char *, int);
  244. protected uint32_t file_signextend(struct magic_set *, struct magic *, uint32_t);
  245. protected void file_delmagic(struct magic *, int type, size_t entries);
  246. protected void file_badread(struct magic_set *);
  247. protected void file_badseek(struct magic_set *);
  248. protected void file_oomem(struct magic_set *);
  249. protected void file_error(struct magic_set *, int, const char *, ...);
  250. protected void file_magwarn(struct magic_set *, const char *, ...);
  251. protected void file_mdump(struct magic *);
  252. protected void file_showstr(FILE *, const char *, size_t);
  253. protected size_t file_mbswidth(const char *);
  254. protected const char *file_getbuffer(struct magic_set *);
  255. #ifndef HAVE_STRERROR
  256. extern int sys_nerr;
  257. extern char *sys_errlist[];
  258. #define strerror(e) \
  259. (((e) >= 0 && (e) < sys_nerr) ? sys_errlist[(e)] : "Unknown error")
  260. #endif
  261. #ifndef HAVE_STRTOUL
  262. #define strtoul(a, b, c) strtol(a, b, c)
  263. #endif
  264. #ifndef HAVE_SNPRINTF
  265. int snprintf(char *, size_t, const char *, ...);
  266. #endif
  267. #if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
  268. #define QUICK
  269. #endif
  270. #define FILE_RCSID(id) \
  271. static const char *rcsid(const char *p) { \
  272. return rcsid(p = id); \
  273. }
  274. #else
  275. #endif /* __file_h__ */