Debian Codegraph @

ar.c

raw symbols SCIP
1 /*-
2 * Copyright © 2011, 2017, 2019
3 * mirabilos <[email protected]>
4 *
5 * Provided that these terms and disclaimer and all copyright notices
6 * are retained or reproduced in an accompanying document, permission
7 * is granted to deal in this work without restriction, including un‐
8 * limited rights to use, publicly perform, distribute, sell, modify,
9 * merge, give away, or sublicence.
10 *
11 * This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
12 * the utmost extent permitted by applicable law, neither express nor
13 * implied; without malicious intent or gross negligence. In no event
14 * may a licensor, author or contributor be held liable for indirect,
15 * direct, other damage, loss, or other issues arising in any way out
16 * of dealing in the work, even if advised of the possibility of such
17 * damage or existence of a defect, except proven that it results out
18 * of said person’s immediate fault when using the work as intended.
19 */
20
21 #include <sys/types.h>
22 #if HAVE_BOTH_TIME_H
23 #include <sys/time.h>
24 #include <time.h>
25 #elif HAVE_SYS_TIME_H
26 #include <sys/time.h>
27 #elif HAVE_TIME_H
28 #include <time.h>
29 #endif
30 #include <sys/stat.h>
31 #include <err.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #if HAVE_STRINGS_H
36 #include <strings.h>
37 #endif
38 #include <unistd.h>
39
40 #include "pax.h"
41 #include "ar.h"
42 #include "extern.h"
43
44 __RCSID("$MirOS: src/bin/pax/ar.c,v 1.12 2019/02/10 21:50:06 tg Exp $");
45
46 /*
47 * Routines for reading and writing Unix Archiver format libraries
48 */
49
50 static const char magic[8] = {
51 0x21, 0x3C, 0x61, 0x72, 0x63, 0x68, 0x3E, 0x0A
52 };
53
54 /*
55 * initialisation for ar write
56 * returns 0 if ok, -1 otherwise
57 */
58 int
59 uar_stwr(int is_app)
60 {
61 return (is_app ? 0 : wr_rdbuf(magic, 8));
62 }
63
64 /*
65 * check for ar magic
66 * returns 0 if ok, -1 otherwise
67 */
68 int
69 uar_ismagic(char *buf)
70 {
71 return (memcmp(buf, magic, 8) ? -1 : 0);
72 }
73
74 /*
75 * used during format identification, but we differ
76 */
77 int
78 uar_id(char *buf MKSH_A_UNUSED, int len MKSH_A_UNUSED)
79 {
80 errx(1, "internal error: %s should never have been called",
81 "uar_id");
82 }
83
84 /* internal parsing functions */
85 static uint64_t
86 uar_atoi64(const char *buf, size_t len)
87 {
88 char c;
89 uint64_t res = 0;
90
91 loop:
92 if (!len-- || (c = *buf++) < '0' || c > '9')
93 return (res);
94 res = (res * 10) + (c - '0');
95 goto loop;
96 }
97
98 static uint32_t
99 uar_atoi32(const char *buf, size_t len)
100 {
101 char c;
102 uint32_t res = 0;
103
104 loop:
105 if (!len-- || (c = *buf++) < '0' || c > '9')
106 return (res);
107 res = (res * 10) + (c - '0');
108 goto loop;
109 }
110
111 static uint32_t
112 uar_otoi32(const char *buf, size_t len)
113 {
114 char c;
115 uint32_t res = 0;
116
117 loop:
118 if (!len-- || (c = *buf++) < '0' || c > '7')
119 return (res);
120 res = (res << 3) | (c & 7);
121 goto loop;
122 }
123
124 /*
125 * parse header
126 */
127 int
128 uar_rd(ARCHD *arcn, char *buf)
129 {
130 HD_AR *h = (HD_AR *)buf;
131 uint64_t i;
132
133 if (h->ar_magic[0] != 0x60 || h->ar_magic[1] != 0x0A)
134 return (-1);
135
136 memset(arcn, 0, sizeof(*arcn));
137 arcn->org_name = arcn->name;
138 arcn->sb.st_nlink = 1;
139 arcn->type = PAX_REG;
140
141 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime =
142 uar_atoi64(h->ar_mtime, sizeof(h->ar_mtime));
143 arcn->sb.st_uid = uar_atoi32(h->ar_uid, sizeof(h->ar_uid));
144 arcn->sb.st_gid = uar_atoi32(h->ar_gid, sizeof(h->ar_gid));
145 arcn->sb.st_mode = uar_otoi32(h->ar_mode, sizeof(h->ar_mode)) |
146 S_IFREG;
147 i = uar_atoi64(h->ar_size, sizeof(h->ar_size));
148 arcn->pad = i & 1;
149
150 if (h->ar_name[0] == 0x23 &&
151 h->ar_name[1] == 0x31 &&
152 h->ar_name[2] == 0x2F) {
153 arcn->nlen = uar_atoi32(&(h->ar_name[3]),
154 sizeof(h->ar_name) - 3);
155 if (arcn->nlen < 0 || arcn->nlen > PAXPATHLEN)
156 /*XXX just skip over this file */
157 return (-1);
158 if (rd_wrbuf(arcn->name, arcn->nlen) != arcn->nlen)
159 return (-1);
160 i -= arcn->nlen;
161 } else {
162 register char c;
163
164 /*arcn->nlen = 0;*/
165 while (arcn->nlen < (int)sizeof(h->ar_name)) {
166 c = h->ar_name[arcn->nlen];
167 if (c == ' ' || c == '/' || c == '\0')
168 break;
169 arcn->name[arcn->nlen++] = c;
170 }
171 }
172 arcn->name[arcn->nlen] = '\0';
173 arcn->sb.st_size = i;
174 arcn->skip = i;
175 return (0);
176 }
177
178 /* internal emission functions */
179 static char *
180 uar_itoa64(char *dst, uint64_t num)
181 {
182 if (num >= 10)
183 dst = uar_itoa64(dst, num / 10);
184 *dst++ = '0' + (num % 10);
185 return (dst);
186 }
187
188 static char *
189 uar_itoa32(char *dst, uint32_t num)
190 {
191 if (num >= 10)
192 dst = uar_itoa32(dst, num / 10);
193 *dst++ = '0' + (num % 10);
194 return (dst);
195 }
196
197 static char *
198 uar_itoo32(char *dst, uint32_t num)
199 {
200 if (num & ~7)
201 dst = uar_itoo32(dst, num >> 3);
202 *dst++ = '0' | (num & 7);
203 return (dst);
204 }
205
206 /*
207 * write a header
208 */
209 int
210 uar_wr(ARCHD *arcn)
211 {
212 HD_AR h;
213 u_long t_uid, t_gid;
214 time_t t_mtime = 0;
215 char *extname;
216 size_t n;
217 u_long t_mode[sizeof(arcn->sb.st_mode) <= sizeof(u_long) ? 1 : -1];
218
219 anonarch_init();
220
221 switch (arcn->type) {
222 case PAX_CTG:
223 case PAX_REG:
224 case PAX_HRG:
225 /* regular files, more or less */
226 break;
227 case PAX_DIR:
228 /* directory, ignore silently */
229 return (1);
230 default:
231 paxwarn(1, "ar can only archive regular files, which %s is not",
232 arcn->org_name);
233 return (1);
234 }
235
236 /* trim trailing slashes */
237 n = strlen(arcn->name) - 1;
238 while (n && arcn->name[n] == '/')
239 --n;
240 arcn->name[++n] = '\0';
241 /* find out basename */
242 if ((extname = strrchr(arcn->name, '/')) == NULL)
243 extname = arcn->name;
244 else
245 ++extname;
246
247 t_uid = (anonarch & ANON_UIDGID) ? 0UL : (u_long)arcn->sb.st_uid;
248 t_gid = (anonarch & ANON_UIDGID) ? 0UL : (u_long)arcn->sb.st_gid;
249 t_mode[0] = arcn->sb.st_mode;
250 if (!(anonarch & ANON_MTIME))
251 t_mtime = arcn->sb.st_mtime;
252
253 #if HAVE_TIMET_LARGE
254 if (t_mtime > (time_t)999999999999ULL) {
255 paxwarn(1, "%s overflow for %s", "mtime", arcn->org_name);
256 t_mtime = (time_t)999999999999ULL;
257 }
258 #endif
259 if (t_uid > 999999UL) {
260 paxwarn(1, "%s overflow for %s", "uid", arcn->org_name);
261 t_uid = 999999UL;
262 }
263 if (t_gid > 999999UL) {
264 paxwarn(1, "%s overflow for %s", "gid", arcn->org_name);
265 t_gid = 999999UL;
266 }
267 if (t_mode[0] > 077777777UL) {
268 paxwarn(1, "%s overflow for %s", "mode", arcn->org_name);
269 t_mode[0] &= 077777777UL;
270 }
271 if ((uint64_t)arcn->sb.st_size > ((uint64_t)9999999999ULL)) {
272 paxwarn(1, "%s overflow for %s", "size", arcn->org_name);
273 return (1);
274 }
275
276 #ifndef SMALL
277 if (anonarch & ANON_DEBUG)
278 paxwarn(0, "writing mode %8lo user %ld:%ld "
279 "mtime %08lX name '%s'", t_mode[0],
280 t_uid, t_gid, (u_long)t_mtime, extname);
281 #endif
282
283 memset(&h, ' ', sizeof(HD_AR));
284
285 if ((n = strlen(extname)) <= sizeof(h.ar_name)) {
286 while (n)
287 if (extname[--n] == ' ')
288 break;
289 if (n == 0) {
290 memcpy(h.ar_name, extname, strlen(extname));
291 extname = NULL;
292 goto got_name;
293 }
294 }
295 n = strlen(extname);
296 /* assert: n <= PAXPATHLEN <= 9999999999999 */
297 h.ar_name[0] = 0x23;
298 h.ar_name[1] = 0x31;
299 h.ar_name[2] = 0x2F;
300 uar_itoa32(&(h.ar_name[3]), n);
301 got_name:
302 uar_itoa64(h.ar_mtime, t_mtime);
303 uar_itoa32(h.ar_uid, t_uid);
304 uar_itoa32(h.ar_gid, t_gid);
305 uar_itoo32(h.ar_mode, t_mode[0]);
306 uar_itoa64(h.ar_size, arcn->sb.st_size +
307 (extname ? strlen(extname) : 0));
308 h.ar_magic[0] = 0x60;
309 h.ar_magic[1] = 0x0A;
310 arcn->pad = (arcn->sb.st_size + (extname ? strlen(extname) : 0)) & 1;
311
312 if (wr_rdbuf((void *)&h, sizeof(HD_AR)) < 0)
313 return (-1);
314 if (extname) {
315 if (wr_rdbuf(extname, strlen(extname)) < 0)
316 return (-1);
317 }
318 /* so let the data follow */
319 return (0);
320 }
321
322 /*
323 * return size of trailer
324 */
325 off_t
326 uar_endrd(void)
327 {
328 return (0);
329 }
330
331 /*
332 * another artefact of paxtar integration
333 */
334 int
335 uar_trail(ARCHD *ignore MKSH_A_UNUSED, char *buf MKSH_A_UNUSED,
336 int in_resync MKSH_A_UNUSED, int *cnt MKSH_A_UNUSED)
337 {
338 errx(1, "internal error: %s should never have been called",
339 "uar_trail");
340 }