1 /*
2  * cpio - copy file archives in and out
3  *
4  * Gunnar Ritter, Freiburg i. Br., Germany, April 2003.
5  */
6 /*
7  * Copyright (c) 2003 Gunnar Ritter
8  *
9  * This software is provided 'as-is', without any express or implied
10  * warranty. In no event will the authors be held liable for any damages
11  * arising from the use of this software.
12  *
13  * Permission is granted to anyone to use this software for any purpose,
14  * including commercial applications, and to alter it and redistribute
15  * it freely, subject to the following restrictions:
16  *
17  * 1. The origin of this software must not be misrepresented; you must not
18  *    claim that you wrote the original software. If you use this software
19  *    in a product, an acknowledgment in the product documentation would be
20  *    appreciated but is not required.
21  *
22  * 2. Altered source versions must be plainly marked as such, and must not be
23  *    misrepresented as being the original software.
24  *
25  * 3. This notice may not be removed or altered from any source distribution.
26  */
27 
28 /*	Sccsid @(#)cpio.h	1.29 (gritter) 3/26/07	*/
29 
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <inttypes.h>
33 
34 enum	{
35 	FMT_NONE	= 00000000,	/* no format chosen yet */
36 
37 	TYP_PAX		= 00000010,	/* uses pax-like extended headers */
38 	TYP_BE		= 00000100,	/* this binary archive is big-endian */
39 	TYP_SGI		= 00000200,	/* SGI cpio -K flag binary archive */
40 	TYP_SCO		= 00000200,	/* SCO UnixWare 7.1 extended archive */
41 	TYP_CRC		= 00000400,	/* this has a SVR4 'crc' checksum */
42 	TYP_BINARY	= 00001000,	/* this is a binary cpio type */
43 	TYP_OCPIO	= 00002000,	/* this is an old cpio type */
44 	TYP_NCPIO	= 00004000,	/* this is a SVR4 cpio type */
45 	TYP_CRAY	= 00010000,	/* this is a Cray cpio archive */
46 	TYP_CPIO	= 00077000,	/* this is a cpio type */
47 	TYP_OTAR	= 00100000,	/* this is an old tar type */
48 	TYP_USTAR	= 00200000,	/* this is a ustar type */
49 	TYP_BAR		= 00400000,	/* this is a bar type */
50 	TYP_TAR		= 00700000,	/* this is a tar type */
51 
52 	FMT_ODC		= 00002001,	/* POSIX ASCII cpio format */
53 	FMT_DEC		= 00002002,	/* DEC extended cpio format */
54 	FMT_BINLE	= 00003001,	/* binary (default) cpio format LE */
55 	FMT_BINBE	= 00003101,	/* binary (default) cpio format BE */
56 	FMT_SGILE	= 00003201,	/* IRIX-style -K binary format LE */
57 	FMT_SGIBE	= 00003301,	/* IRIX-style -K binary format BE */
58 	FMT_ASC		= 00004001,	/* SVR4 ASCII cpio format */
59 	FMT_SCOASC	= 00004201,	/* UnixWare 7.1 ASCII cpio format */
60 	FMT_CRC		= 00004401,	/* SVR4 ASCII cpio format w/checksum */
61 	FMT_SCOCRC	= 00004601,	/* UnixWare 7.1 ASCII cpio w/checksum */
62 	FMT_CRAY	= 00010001,	/* Cray cpio, UNICOS 6 and later */
63 	FMT_CRAY5	= 00010002,	/* Cray cpio, UNICOS 5 and earlier */
64 	FMT_OTAR	= 00100001,	/* obsolete tar format */
65 	FMT_TAR		= 00200001,	/* our tar format type */
66 	FMT_USTAR	= 00200002,	/* ustar format */
67 	FMT_GNUTAR	= 00200003,	/* GNU tar format type */
68 	FMT_PAX		= 00200011,	/* POSIX.1-2001 pax format type */
69 	FMT_SUN		= 00200012,	/* Sun extended tar format type */
70 	FMT_BAR		= 00400001,	/* bar format type */
71 
72 	FMT_ZIP		= 01000000	/* zip format */
73 } fmttype;
74 
75 /*
76  * Zip compression method.
77  */
78 enum	cmethod {
79 	C_STORED	= 0,		/* no compression */
80 	C_SHRUNK	= 1,
81 	C_REDUCED1	= 2,
82 	C_REDUCED2	= 3,
83 	C_REDUCED3	= 4,
84 	C_REDUCED4	= 5,
85 	C_IMPLODED	= 6,
86 	C_TOKENIZED	= 7,
87 	C_DEFLATED	= 8,
88 	C_ENHDEFLD	= 9,
89 	C_DCLIMPLODED	= 10,
90 	C_PKRESERVED	= 11,
91 	C_BZIP2		= 12,
92 };
93 
94 /*
95  * A collection of the interesting facts about a file in copy-in mode.
96  */
97 struct	file {
98 	struct stat	f_st;		/* file stat */
99 	long long	f_rmajor;	/* st_rdev major */
100 	long long	f_rminor;	/* st_rdev minor */
101 	long long	f_dsize;	/* display size */
102 	long long	f_csize;	/* compressed size */
103 	long long	f_Kbase;	/* base size for -K */
104 	long long	f_Krest;	/* rest size for -K */
105 	long long	f_Ksize;	/* faked -K size field */
106 	char		*f_name;	/* file name */
107 	size_t		f_nsiz;		/* file name size */
108 	char		*f_lnam;	/* link name */
109 	size_t		f_lsiz;		/* link name size */
110 	uint32_t	f_chksum;	/* checksum */
111 	int		f_pad;		/* padding size */
112 	int		f_fd;		/* file descriptor (for pass mode) */
113 	enum cmethod	f_cmethod;	/* zip compression method */
114 	enum {
115 		FG_CRYPT	= 0001,	/* encrypted zip file */
116 		FG_BIT1		= 0002,
117 		FG_BIT2		= 0004,
118 		FG_DESC		= 0010	/* zip file with data descriptor */
119 	}		f_gflag;	/* zip general flag */
120 	enum {
121 		OF_ZIP64	= 0001	/* is a zip64 archive entry */
122 	}		f_oflag;	/* other flags */
123 };
124 
125 /*
126  * Patterns for gmatch().
127  */
128 struct glist {
129 	struct glist	*g_nxt;
130 	const char	*g_pat;
131 	unsigned	g_gotcha : 1;
132 	unsigned	g_not    : 1;
133 	unsigned	g_art    : 1;
134 };
135 
136 extern int		aflag;
137 extern int		Aflag;
138 extern int		bflag;
139 extern int		Bflag;
140 extern int		cflag;
141 extern int		Cflag;
142 extern int		dflag;
143 extern int		Dflag;
144 extern int		eflag;
145 extern int		cray_eflag;
146 extern const char	*Eflag;
147 extern int		fflag;
148 extern int		Hflag;
149 extern const char	*Iflag;
150 extern int		kflag;
151 extern int		Kflag;
152 extern int		lflag;
153 extern int		Lflag;
154 extern int		mflag;
155 extern const char	*Mflag;
156 extern const char	*Oflag;
157 extern int		Pflag;
158 extern int		rflag;
159 extern const char	*Rflag;
160 extern int		sflag;
161 extern int		Sflag;
162 extern int		tflag;
163 extern int		uflag;
164 extern int		hp_Uflag;
165 extern int		vflag;
166 extern int		Vflag;
167 extern int		sixflag;
168 extern int		action;
169 extern long long	errcnt;
170 extern int		blksiz;
171 extern int		sysv3;
172 extern int		printsev;
173 extern char		*progname;
174 extern struct glist	*patterns;
175 
176 enum {			/* type of pax command this is */
177 	PAX_TYPE_CPIO		= 0,	/* not a pax command */
178 	PAX_TYPE_PAX1992	= 1,	/* POSIX.2 pax command */
179 	PAX_TYPE_PAX2001	= 2	/* POSIX.1-2001 pax command */
180 } pax;
181 extern int		pax_dflag;
182 extern int		pax_kflag;
183 extern int		pax_nflag;
184 extern int		pax_sflag;
185 extern int		pax_uflag;
186 extern int		pax_Xflag;
187 
188 enum {
189 	PAX_P_NONE	= 0000,
190 	PAX_P_ATIME	= 0001,
191 	PAX_P_MTIME	= 0004,
192 	PAX_P_OWNER	= 0010,
193 	PAX_P_MODE	= 0020,
194 	PAX_P_EVERY	= 0400
195 } pax_preserve;
196 
197 extern size_t		(*ofiles)(char **, size_t *);
198 extern void		(*prtime)(time_t);
199 
200 extern ssize_t	bread(char *, size_t);
201 extern void	bunread(const char *, size_t);
202 extern void	swap(char *, size_t, int, int);
203 extern void	msg(int, int, const char *, ...);
204 extern void	emsg(int, const char *, ...);
205 extern void	unexeoa(void);
206 extern int	setfmt(char *);
207 extern char	*oneintfmt(const char *);
208 extern int	setreassign(const char *);
209 extern void	addg(const char *, int);
210 extern void	*srealloc(void *, size_t);
211 extern void	*smalloc(size_t);
212 extern void	*scalloc(size_t, size_t);
213 extern void	*svalloc(size_t, int);
214 extern char	*sstrdup(const char *);
215 extern int	pax_options(char *, int);
216 
217 extern int	zipunshrink(struct file *, const char *, int, int, uint32_t *);
218 extern int	zipexplode(struct file *, const char *, int, int, uint32_t *);
219 extern int	zipexpand(struct file *, const char *, int, int, uint32_t *);
220 extern int	zipinflate(struct file *, const char *, int, int, uint32_t *);
221 extern int	zipunbz2(struct file *, const char *, int, int, uint32_t *);
222 extern int	zipblast(struct file *, const char *, int, int, uint32_t *);
223 
224 extern uint32_t	zipcrc(uint32_t, const uint8_t *, size_t);
225 
226 extern void	flags(int, char **);
227 extern void	usage(void);
228 
229 extern int	pax_track(const char *, time_t);
230 extern void	pax_prlink(struct file *);
231 extern int	pax_sname(char **, size_t *);
232 extern void	pax_onexit(void);
233