2c0388394f93862394a77bbccb598716b771291a
[platform/upstream/rpm.git] / file / file.h
1 /*
2  * file.h - definitions for file(1) program
3  * @(#)Id: file.h,v 1.43 2002/07/03 18:57:52 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
29 #ifndef __file_h__
30 #define __file_h__
31
32 #ifndef HOWMANY
33 # define HOWMANY 65536          /* how much of the file to look at */
34 #endif
35 #define MAXMAGIS 4096           /* max entries in /etc/magic */
36 #define MAXDESC 50              /* max leng of text description */
37 #define MAXstring 32            /* max leng of "string" types */
38
39 #define MAGICNO         0xF11E041C
40 #define VERSIONNO       1
41
42 #define CHECK   1
43 #define COMPILE 2
44
45 struct magic {
46         uint16_t cont_level;    /* level of ">" */
47         uint8_t nospflag;       /* supress space character */
48         uint8_t flag;
49 #define INDIR   1               /* if '>(...)' appears,  */
50 #define UNSIGNED 2              /* comparison is unsigned */
51 #define OFFADD  4               /* if '>&' appears,  */
52         uint8_t reln;           /* relation (0=eq, '>'=gt, etc) */
53         uint8_t vallen;         /* length of string value, if any */
54         uint8_t type;           /* int, short, long or string. */
55         uint8_t in_type;        /* type of indirrection */
56 #define                         BYTE    1
57 #define                         SHORT   2
58 #define                         LONG    4
59 #define                         STRING  5
60 #define                         DATE    6
61 #define                         BESHORT 7
62 #define                         BELONG  8
63 #define                         BEDATE  9
64 #define                         LESHORT 10
65 #define                         LELONG  11
66 #define                         LEDATE  12
67 #define                         PSTRING 13
68 #define                         LDATE   14
69 #define                         BELDATE 15
70 #define                         LELDATE 16
71 #define                         REGEX   17
72         uint8_t in_op;          /* operator for indirection */
73         uint8_t mask_op;        /* operator for mask */
74 #define                         OPAND   1
75 #define                         OPOR    2
76 #define                         OPXOR   3
77 #define                         OPADD   4
78 #define                         OPMINUS 5
79 #define                         OPMULTIPLY      6
80 #define                         OPDIVIDE        7
81 #define                         OPMODULO        8
82 #define                         OPINVERSE       0x80
83         int32_t offset;         /* offset to magic number */
84         int32_t in_offset;      /* offset from indirection */
85         union VALUETYPE {
86                 uint8_t b;
87                 uint16_t h;
88                 uint32_t l;
89                 char s[MAXstring];
90                 char *buf;
91                 uint8_t hs[2];  /* 2 bytes of a fixed-endian "short" */
92                 uint8_t hl[4];  /* 4 bytes of a fixed-endian "long" */
93         } value;                /* either number or string */
94         uint32_t mask;  /* mask before comparison with value */
95         char desc[MAXDESC];     /* description */
96 } __attribute__((__packed__));
97
98 #define BIT(A)   (1 << (A))
99 #define STRING_IGNORE_LOWERCASE         BIT(0)
100 #define STRING_COMPACT_BLANK            BIT(1)
101 #define STRING_COMPACT_OPTIONAL_BLANK   BIT(2)
102 #define CHAR_IGNORE_LOWERCASE           'c'
103 #define CHAR_COMPACT_BLANK              'B'
104 #define CHAR_COMPACT_OPTIONAL_BLANK     'b'
105
106
107 /* list of magic entries */
108 struct mlist {
109         struct magic *magic;            /* array of magic entries */
110         uint32_t nmagic;                /* number of entries in array */
111 /*@null@*/
112         struct mlist *next;
113 /*@null@*/
114         struct mlist *prev;
115 };
116
117 /*@unchecked@*/ /*@observer@*/ /*@null@*/
118 extern char *progname;          /* the program name                     */
119 /*@unchecked@*/ /*@dependent@*/ /*@observer@*/
120 extern const char *magicfile;   /* name of the magic file               */
121 /*@unchecked@*/
122 extern int lineno;              /* current line number in magic file    */
123
124 /*@unchecked@*/
125 extern struct mlist mlist;      /* list of arrays of magic entries      */
126
127 /*@-exportlocal@*/
128 /*@unchecked@*/
129 extern int debug;               /* enable debugging?                    */
130 /*@unchecked@*/
131 extern int bflag;               /* brief output format                  */
132 /*@unchecked@*/
133 extern int iflag;               /* Output types as mime-types           */
134 /*@unchecked@*/
135 extern int kflag;               /* Keep going after the first match     */
136 /*@unchecked@*/
137 extern int lflag;               /* follow symbolic links?               */
138 /*@unchecked@*/
139 extern int sflag;               /* read/analyze block special files?    */
140 /*@unchecked@*/
141 extern int zflag;               /* process compressed files?            */
142 /*@=exportlocal@*/
143
144 /*@mayexit@*/
145 extern int   apprentice(const char *fn, int action)
146         /*@globals lineno, mlist, fileSystem, internalState @*/
147         /*@modifies lineno, mlist, fileSystem, internalState @*/;
148 extern int   ascmagic(unsigned char *buf, int nbytes)
149         /*@globals fileSystem @*/
150         /*@modifies fileSystem @*/;
151 /*@exits@*/
152 extern void  error(const char *f, ...)
153         /*@globals fileSystem @*/
154         /*@modifies fileSystem @*/;
155 extern void  ckfputs(const char *str, FILE *fil)
156         /*@globals fileSystem @*/
157         /*@modifies fil, fileSystem @*/;
158 struct stat;
159 extern int   fsmagic(const char *fn, /*@out@*/ struct stat *sb)
160         /*@globals fileSystem, internalState @*/
161         /*@modifies *sb, fileSystem, internalState @*/;
162 /*@observer@*/
163 extern char *fmttime(long v, int local)
164         /*@*/;
165 extern int   is_tar(unsigned char *buf, int nbytes)
166         /*@*/;
167 extern void  magwarn(const char *f, ...)
168         /*@globals fileSystem @*/
169         /*@modifies fileSystem @*/;
170 extern void  mdump(struct magic *m)
171         /*@globals fileSystem @*/
172         /*@modifies fileSystem @*/;
173 extern void  process(const char *inname, int wid)
174         /*@globals fileSystem, internalState @*/
175         /*@modifies fileSystem, internalState @*/;
176 extern void  showstr(FILE *fp, const char *s, int len)
177         /*@globals fileSystem @*/
178         /*@modifies fp, fileSystem @*/;
179 extern int   softmagic(unsigned char *buf, int nbytes)
180         /*@globals fileSystem @*/
181         /*@modifies buf, fileSystem @*/;
182 extern int   tryit(const char *fn, unsigned char *buf, int nb, int zfl)
183         /*@globals fileSystem, internalState @*/
184         /*@modifies buf, fileSystem, internalState @*/;
185 /**
186  */
187 /*@unused@*/ /*@exits@*/ /*@only@*/
188 static inline void * vmefail(/*@unused@*/ size_t nb)
189         /*@globals fileSystem @*/
190         /*@modifies fileSystem @*/
191 {
192         error("out of memory");
193         /*@notreached@*/
194 /*@-nullret@*/
195         return NULL;
196 /*@=nullret@*/
197 }
198 extern int   zmagic(const char *fname, unsigned char *buf, int nbytes)
199         /*@globals fileSystem, internalState @*/
200         /*@modifies buf, fileSystem, internalState @*/;
201 extern void  ckfprintf(FILE *f, const char *fmt, ...)
202         /*@globals fileSystem @*/
203         /*@modifies f, fileSystem @*/;
204 extern uint32_t signextend(struct magic *m, uint32_t v)
205         /*@globals fileSystem @*/
206         /*@modifies fileSystem @*/;
207 extern void tryelf(int fd, unsigned char *buf, int nbytes)
208         /*@globals fileSystem, internalState @*/
209         /*@modifies fileSystem, internalState @*/;
210 extern int pipe2file(int fd, void *startbuf, size_t nbytes)
211         /*@globals errno, fileSystem, internalState @*/
212         /*@modifies errno, fileSystem, internalState @*/;
213
214 #endif /* __file_h__ */