file.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * file.h - definitions for file(1) program
  3. * @(#)$Id: file.h,v 1.37 2001/07/22 21:04:15 christos Exp $
  4. *
  5. * Copyright (c) Ian F. Darwin, 1987.
  6. * Written by Ian F. Darwin.
  7. *
  8. * This software is not subject to any license of the American Telephone
  9. * and Telegraph Company or of the Regents of the University of California.
  10. *
  11. * Permission is granted to anyone to use this software for any purpose on
  12. * any computer system, and to alter it and redistribute it freely, subject
  13. * to the following restrictions:
  14. *
  15. * 1. The author is not responsible for the consequences of use of this
  16. * software, no matter how awful, even if they arise from flaws in it.
  17. *
  18. * 2. The origin of this software must not be misrepresented, either by
  19. * explicit claim or by omission. Since few users ever read sources,
  20. * credits must appear in the documentation.
  21. *
  22. * 3. Altered versions must be plainly marked as such, and must not be
  23. * misrepresented as being the original software. Since few users
  24. * ever read sources, credits must appear in the documentation.
  25. *
  26. * 4. This notice may not be removed or altered.
  27. */
  28. #ifndef __file_h__
  29. #define __file_h__
  30. #ifdef HAVE_CONFIG_H
  31. #include <config.h>
  32. #endif
  33. typedef int int32;
  34. typedef unsigned int uint32;
  35. typedef short int16;
  36. typedef unsigned short uint16;
  37. typedef char int8;
  38. typedef unsigned char uint8;
  39. #ifndef HOWMANY
  40. # define HOWMANY 16384 /* how much of the file to look at */
  41. #endif
  42. #define MAXMAGIS 1000 /* max entries in /etc/magic */
  43. #define MAXDESC 50 /* max leng of text description */
  44. #define MAXstring 32 /* max leng of "string" types */
  45. #define MAGICNO 0xF11E041C
  46. #define VERSIONNO 1
  47. #define CHECK 1
  48. #define COMPILE 2
  49. struct magic {
  50. uint16 cont_level;/* level of ">" */
  51. uint8 nospflag; /* supress space character */
  52. uint8 flag;
  53. #define INDIR 1 /* if '>(...)' appears, */
  54. #define UNSIGNED 2 /* comparison is unsigned */
  55. #define OFFADD 4 /* if '>&' appears, */
  56. uint8 reln; /* relation (0=eq, '>'=gt, etc) */
  57. uint8 vallen; /* length of string value, if any */
  58. uint8 type; /* int, short, long or string. */
  59. uint8 in_type; /* type of indirrection */
  60. #define BYTE 1
  61. #define SHORT 2
  62. #define LONG 4
  63. #define STRING 5
  64. #define DATE 6
  65. #define BESHORT 7
  66. #define BELONG 8
  67. #define BEDATE 9
  68. #define LESHORT 10
  69. #define LELONG 11
  70. #define LEDATE 12
  71. #define PSTRING 13
  72. #define LDATE 14
  73. #define BELDATE 15
  74. #define LELDATE 16
  75. uint8 in_op; /* operator for indirection */
  76. uint8 mask_op; /* operator for mask */
  77. #define OPAND 1
  78. #define OPOR 2
  79. #define OPXOR 3
  80. #define OPADD 4
  81. #define OPMINUS 5
  82. #define OPMULTIPLY 6
  83. #define OPDIVIDE 7
  84. #define OPMODULO 8
  85. #define OPINVERSE 0x80
  86. int32 offset; /* offset to magic number */
  87. int32 in_offset; /* offset from indirection */
  88. union VALUETYPE {
  89. unsigned char b;
  90. unsigned short h;
  91. uint32 l;
  92. char s[MAXstring];
  93. unsigned char hs[2]; /* 2 bytes of a fixed-endian "short" */
  94. unsigned char hl[4]; /* 4 bytes of a fixed-endian "long" */
  95. } value; /* either number or string */
  96. uint32 mask; /* mask before comparison with value */
  97. char desc[MAXDESC]; /* description */
  98. };
  99. #define BIT(A) (1 << (A))
  100. #define STRING_IGNORE_LOWERCASE BIT(0)
  101. #define STRING_COMPACT_BLANK BIT(1)
  102. #define STRING_COMPACT_OPTIONAL_BLANK BIT(2)
  103. #define CHAR_IGNORE_LOWERCASE 'c'
  104. #define CHAR_COMPACT_BLANK 'B'
  105. #define CHAR_COMPACT_OPTIONAL_BLANK 'b'
  106. /* list of magic entries */
  107. struct mlist {
  108. struct magic *magic; /* array of magic entries */
  109. uint32 nmagic; /* number of entries in array */
  110. struct mlist *next, *prev;
  111. };
  112. #include <stdio.h> /* Include that here, to make sure __P gets defined */
  113. #include <errno.h>
  114. #ifndef __P
  115. # if defined(__STDC__) || defined(__cplusplus)
  116. # define __P(a) a
  117. # else
  118. # define __P(a) ()
  119. # define const
  120. # endif
  121. #endif
  122. extern int apprentice __P((const char *, int));
  123. extern int ascmagic __P((unsigned char *, int));
  124. extern void error __P((const char *, ...));
  125. extern void ckfputs __P((const char *, FILE *));
  126. struct stat;
  127. extern int fsmagic __P((const char *, struct stat *));
  128. extern char *fmttime __P((long, int));
  129. extern int is_compress __P((const unsigned char *, int *));
  130. extern int is_tar __P((unsigned char *, int));
  131. extern void magwarn __P((const char *, ...));
  132. extern void mdump __P((struct magic *));
  133. extern void process __P((const char *, int));
  134. extern void showstr __P((FILE *, const char *, int));
  135. extern int softmagic __P((unsigned char *, int));
  136. extern int tryit __P((unsigned char *, int, int));
  137. extern int zmagic __P((unsigned char *, int));
  138. extern void ckfprintf __P((FILE *, const char *, ...));
  139. extern uint32 signextend __P((struct magic *, unsigned int32));
  140. extern void tryelf __P((int, unsigned char *, int));
  141. extern char *progname; /* the program name */
  142. extern const char *magicfile; /* name of the magic file */
  143. extern int lineno; /* current line number in magic file */
  144. extern struct mlist mlist; /* list of arrays of magic entries */
  145. extern int debug; /* enable debugging? */
  146. extern int zflag; /* process compressed files? */
  147. extern int lflag; /* follow symbolic links? */
  148. extern int sflag; /* read/analyze block special files? */
  149. extern int iflag; /* Output types as mime-types */
  150. extern int optind; /* From getopt(3) */
  151. extern char *optarg;
  152. #ifndef HAVE_STRERROR
  153. extern int sys_nerr;
  154. extern char *sys_errlist[];
  155. #define strerror(e) \
  156. (((e) >= 0 && (e) < sys_nerr) ? sys_errlist[(e)] : "Unknown error")
  157. #endif
  158. #ifndef HAVE_STRTOUL
  159. #define strtoul(a, b, c) strtol(a, b, c)
  160. #endif
  161. #if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
  162. #define QUICK
  163. #endif
  164. #ifdef __STDC__
  165. #define FILE_RCSID(id) \
  166. static const char *rcsid(const char *p) { \
  167. return rcsid(p = id); \
  168. }
  169. #else
  170. #define FILE_RCSID(id) static char rcsid[] = id;
  171. #endif
  172. #endif /* __file_h__ */