file.h 8.4 KB

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