Upgrade to libarchive 2.0.25 which gives us a nice speed boost along with
[dragonfly/port-amd64.git] / contrib / libarchive-1.3.1 / libarchive / archive_read_support_format_cpio.c
blob87e2d0ee99b77c94ef93aecbe37d06a4d8787241
1 /*-
2 * Copyright (c) 2003-2004 Tim Kientzle
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD: src/lib/libarchive/archive_read_support_format_cpio.c,v 1.18 2006/07/30 18:33:20 kientzle Exp $");
30 #include <sys/stat.h>
31 #ifdef MAJOR_IN_MKDEV
32 #include <sys/mkdev.h>
33 #endif
35 #include <errno.h>
36 /* #include <stdint.h> */ /* See archive_platform.h */
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
41 #include "archive.h"
42 #include "archive_entry.h"
43 #include "archive_private.h"
45 struct cpio_bin_header {
46 unsigned char c_magic[2];
47 unsigned char c_dev[2];
48 unsigned char c_ino[2];
49 unsigned char c_mode[2];
50 unsigned char c_uid[2];
51 unsigned char c_gid[2];
52 unsigned char c_nlink[2];
53 unsigned char c_rdev[2];
54 unsigned char c_mtime[4];
55 unsigned char c_namesize[2];
56 unsigned char c_filesize[4];
59 struct cpio_odc_header {
60 char c_magic[6];
61 char c_dev[6];
62 char c_ino[6];
63 char c_mode[6];
64 char c_uid[6];
65 char c_gid[6];
66 char c_nlink[6];
67 char c_rdev[6];
68 char c_mtime[11];
69 char c_namesize[6];
70 char c_filesize[11];
73 struct cpio_newc_header {
74 char c_magic[6];
75 char c_ino[8];
76 char c_mode[8];
77 char c_uid[8];
78 char c_gid[8];
79 char c_nlink[8];
80 char c_mtime[8];
81 char c_filesize[8];
82 char c_devmajor[8];
83 char c_devminor[8];
84 char c_rdevmajor[8];
85 char c_rdevminor[8];
86 char c_namesize[8];
87 char c_crc[8];
90 struct links_entry {
91 struct links_entry *next;
92 struct links_entry *previous;
93 int links;
94 dev_t dev;
95 ino_t ino;
96 char *name;
99 #define CPIO_MAGIC 0x13141516
100 struct cpio {
101 int magic;
102 int (*read_header)(struct archive *, struct cpio *,
103 struct stat *, size_t *, size_t *);
104 struct links_entry *links_head;
105 struct archive_string entry_name;
106 struct archive_string entry_linkname;
107 off_t entry_bytes_remaining;
108 off_t entry_offset;
109 off_t entry_padding;
112 static int64_t atol16(const char *, unsigned);
113 static int64_t atol8(const char *, unsigned);
114 static int archive_read_format_cpio_bid(struct archive *);
115 static int archive_read_format_cpio_cleanup(struct archive *);
116 static int archive_read_format_cpio_read_data(struct archive *,
117 const void **, size_t *, off_t *);
118 static int archive_read_format_cpio_read_header(struct archive *,
119 struct archive_entry *);
120 static int be4(const unsigned char *);
121 static int header_bin_be(struct archive *, struct cpio *, struct stat *,
122 size_t *, size_t *);
123 static int header_bin_le(struct archive *, struct cpio *, struct stat *,
124 size_t *, size_t *);
125 static int header_newc(struct archive *, struct cpio *, struct stat *,
126 size_t *, size_t *);
127 static int header_odc(struct archive *, struct cpio *, struct stat *,
128 size_t *, size_t *);
129 static int le4(const unsigned char *);
130 static void record_hardlink(struct cpio *cpio, struct archive_entry *entry,
131 const struct stat *st);
134 archive_read_support_format_cpio(struct archive *a)
136 struct cpio *cpio;
137 int r;
139 cpio = malloc(sizeof(*cpio));
140 if (cpio == NULL) {
141 archive_set_error(a, ENOMEM, "Can't allocate cpio data");
142 return (ARCHIVE_FATAL);
144 memset(cpio, 0, sizeof(*cpio));
145 cpio->magic = CPIO_MAGIC;
147 r = __archive_read_register_format(a,
148 cpio,
149 archive_read_format_cpio_bid,
150 archive_read_format_cpio_read_header,
151 archive_read_format_cpio_read_data,
152 NULL,
153 archive_read_format_cpio_cleanup);
155 if (r != ARCHIVE_OK)
156 free(cpio);
157 return (ARCHIVE_OK);
161 static int
162 archive_read_format_cpio_bid(struct archive *a)
164 int bid, bytes_read;
165 const void *h;
166 const unsigned char *p;
167 struct cpio *cpio;
169 cpio = *(a->pformat_data);
170 bid = 0;
171 bytes_read = (a->compression_read_ahead)(a, &h, 6);
172 /* Convert error code into error return. */
173 if (bytes_read < 0)
174 return ((int)bytes_read);
175 if (bytes_read < 6)
176 return (-1);
178 p = h;
179 if (memcmp(p, "070707", 6) == 0) {
180 /* ASCII cpio archive (odc, POSIX.1) */
181 cpio->read_header = header_odc;
182 bid += 48;
184 * XXX TODO: More verification; Could check that only octal
185 * digits appear in appropriate header locations. XXX
187 } else if (memcmp(p, "070701", 6) == 0) {
188 /* ASCII cpio archive (SVR4 without CRC) */
189 cpio->read_header = header_newc;
190 bid += 48;
192 * XXX TODO: More verification; Could check that only hex
193 * digits appear in appropriate header locations. XXX
195 } else if (memcmp(p, "070702", 6) == 0) {
196 /* ASCII cpio archive (SVR4 with CRC) */
197 /* XXX TODO: Flag that we should check the CRC. XXX */
198 cpio->read_header = header_newc;
199 bid += 48;
201 * XXX TODO: More verification; Could check that only hex
202 * digits appear in appropriate header locations. XXX
204 } else if (p[0] * 256 + p[1] == 070707) {
205 /* big-endian binary cpio archives */
206 cpio->read_header = header_bin_be;
207 bid += 16;
208 /* Is more verification possible here? */
209 } else if (p[0] + p[1] * 256 == 070707) {
210 /* little-endian binary cpio archives */
211 cpio->read_header = header_bin_le;
212 bid += 16;
213 /* Is more verification possible here? */
214 } else
215 return (ARCHIVE_WARN);
217 return (bid);
220 static int
221 archive_read_format_cpio_read_header(struct archive *a,
222 struct archive_entry *entry)
224 struct stat st;
225 struct cpio *cpio;
226 size_t bytes;
227 const void *h;
228 size_t namelength;
229 size_t name_pad;
230 int r;
232 memset(&st, 0, sizeof(st));
234 cpio = *(a->pformat_data);
235 r = (cpio->read_header(a, cpio, &st, &namelength, &name_pad));
237 if (r != ARCHIVE_OK)
238 return (r);
240 /* Assign all of the 'stat' fields at once. */
241 archive_entry_copy_stat(entry, &st);
243 /* Read name from buffer. */
244 bytes = (a->compression_read_ahead)(a, &h, namelength + name_pad);
245 if (bytes < namelength + name_pad)
246 return (ARCHIVE_FATAL);
247 (a->compression_read_consume)(a, namelength + name_pad);
248 archive_strncpy(&cpio->entry_name, h, namelength);
249 archive_entry_set_pathname(entry, cpio->entry_name.s);
250 cpio->entry_offset = 0;
252 /* If this is a symlink, read the link contents. */
253 if (S_ISLNK(st.st_mode)) {
254 bytes = (a->compression_read_ahead)(a, &h,
255 cpio->entry_bytes_remaining);
256 if ((off_t)bytes < cpio->entry_bytes_remaining)
257 return (ARCHIVE_FATAL);
258 (a->compression_read_consume)(a, cpio->entry_bytes_remaining);
259 archive_strncpy(&cpio->entry_linkname, h,
260 cpio->entry_bytes_remaining);
261 archive_entry_set_symlink(entry, cpio->entry_linkname.s);
262 cpio->entry_bytes_remaining = 0;
265 /* Compare name to "TRAILER!!!" to test for end-of-archive. */
266 if (namelength == 11 && strcmp(h, "TRAILER!!!") == 0) {
267 /* TODO: Store file location of start of block. */
268 archive_set_error(a, 0, NULL);
269 return (ARCHIVE_EOF);
272 /* Detect and record hardlinks to previously-extracted entries. */
273 record_hardlink(cpio, entry, &st);
275 return (ARCHIVE_OK);
278 static int
279 archive_read_format_cpio_read_data(struct archive *a,
280 const void **buff, size_t *size, off_t *offset)
282 ssize_t bytes_read;
283 struct cpio *cpio;
285 cpio = *(a->pformat_data);
286 if (cpio->entry_bytes_remaining > 0) {
287 bytes_read = (a->compression_read_ahead)(a, buff, 1);
288 if (bytes_read <= 0)
289 return (ARCHIVE_FATAL);
290 if (bytes_read > cpio->entry_bytes_remaining)
291 bytes_read = cpio->entry_bytes_remaining;
292 *size = bytes_read;
293 *offset = cpio->entry_offset;
294 cpio->entry_offset += bytes_read;
295 cpio->entry_bytes_remaining -= bytes_read;
296 (a->compression_read_consume)(a, bytes_read);
297 return (ARCHIVE_OK);
298 } else {
299 while (cpio->entry_padding > 0) {
300 bytes_read = (a->compression_read_ahead)(a, buff, 1);
301 if (bytes_read <= 0)
302 return (ARCHIVE_FATAL);
303 if (bytes_read > cpio->entry_padding)
304 bytes_read = cpio->entry_padding;
305 (a->compression_read_consume)(a, bytes_read);
306 cpio->entry_padding -= bytes_read;
308 *buff = NULL;
309 *size = 0;
310 *offset = cpio->entry_offset;
311 return (ARCHIVE_EOF);
315 static int
316 header_newc(struct archive *a, struct cpio *cpio, struct stat *st,
317 size_t *namelength, size_t *name_pad)
319 const void *h;
320 const struct cpio_newc_header *header;
321 size_t bytes;
323 /* Read fixed-size portion of header. */
324 bytes = (a->compression_read_ahead)(a, &h, sizeof(struct cpio_newc_header));
325 if (bytes < sizeof(struct cpio_newc_header))
326 return (ARCHIVE_FATAL);
327 (a->compression_read_consume)(a, sizeof(struct cpio_newc_header));
329 /* Parse out hex fields into struct stat. */
330 header = h;
332 if (memcmp(header->c_magic, "070701", 6) == 0) {
333 a->archive_format = ARCHIVE_FORMAT_CPIO_SVR4_NOCRC;
334 a->archive_format_name = "ASCII cpio (SVR4 with no CRC)";
335 } else if (memcmp(header->c_magic, "070702", 6) == 0) {
336 a->archive_format = ARCHIVE_FORMAT_CPIO_SVR4_CRC;
337 a->archive_format_name = "ASCII cpio (SVR4 with CRC)";
338 } else {
339 /* TODO: Abort here? */
342 st->st_dev = makedev(
343 atol16(header->c_devmajor, sizeof(header->c_devmajor)),
344 atol16(header->c_devminor, sizeof(header->c_devminor)));
345 st->st_ino = atol16(header->c_ino, sizeof(header->c_ino));
346 st->st_mode = atol16(header->c_mode, sizeof(header->c_mode));
347 st->st_uid = atol16(header->c_uid, sizeof(header->c_uid));
348 st->st_gid = atol16(header->c_gid, sizeof(header->c_gid));
349 st->st_nlink = atol16(header->c_nlink, sizeof(header->c_nlink));
350 st->st_rdev = makedev(
351 atol16(header->c_rdevmajor, sizeof(header->c_rdevmajor)),
352 atol16(header->c_rdevminor, sizeof(header->c_rdevminor)));
353 st->st_mtime = atol16(header->c_mtime, sizeof(header->c_mtime));
354 *namelength = atol16(header->c_namesize, sizeof(header->c_namesize));
355 /* Pad name to 2 more than a multiple of 4. */
356 *name_pad = (2 - *namelength) & 3;
359 * Note: entry_bytes_remaining is at least 64 bits and
360 * therefore gauranteed to be big enough for a 33-bit file
361 * size. struct stat.st_size may only be 32 bits, so
362 * assigning there first could lose information.
364 cpio->entry_bytes_remaining =
365 atol16(header->c_filesize, sizeof(header->c_filesize));
366 st->st_size = cpio->entry_bytes_remaining;
367 /* Pad file contents to a multiple of 4. */
368 cpio->entry_padding = 3 & -cpio->entry_bytes_remaining;
369 return (ARCHIVE_OK);
372 static int
373 header_odc(struct archive *a, struct cpio *cpio, struct stat *st,
374 size_t *namelength, size_t *name_pad)
376 const void *h;
377 const struct cpio_odc_header *header;
378 size_t bytes;
380 a->archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
381 a->archive_format_name = "POSIX octet-oriented cpio";
383 /* Read fixed-size portion of header. */
384 bytes = (a->compression_read_ahead)(a, &h, sizeof(struct cpio_odc_header));
385 if (bytes < sizeof(struct cpio_odc_header))
386 return (ARCHIVE_FATAL);
387 (a->compression_read_consume)(a, sizeof(struct cpio_odc_header));
389 /* Parse out octal fields into struct stat. */
390 header = h;
392 st->st_dev = atol8(header->c_dev, sizeof(header->c_dev));
393 st->st_ino = atol8(header->c_ino, sizeof(header->c_ino));
394 st->st_mode = atol8(header->c_mode, sizeof(header->c_mode));
395 st->st_uid = atol8(header->c_uid, sizeof(header->c_uid));
396 st->st_gid = atol8(header->c_gid, sizeof(header->c_gid));
397 st->st_nlink = atol8(header->c_nlink, sizeof(header->c_nlink));
398 st->st_rdev = atol8(header->c_rdev, sizeof(header->c_rdev));
399 st->st_mtime = atol8(header->c_mtime, sizeof(header->c_mtime));
400 *namelength = atol8(header->c_namesize, sizeof(header->c_namesize));
401 *name_pad = 0; /* No padding of filename. */
404 * Note: entry_bytes_remaining is at least 64 bits and
405 * therefore gauranteed to be big enough for a 33-bit file
406 * size. struct stat.st_size may only be 32 bits, so
407 * assigning there first could lose information.
409 cpio->entry_bytes_remaining =
410 atol8(header->c_filesize, sizeof(header->c_filesize));
411 st->st_size = cpio->entry_bytes_remaining;
412 cpio->entry_padding = 0;
413 return (ARCHIVE_OK);
416 static int
417 header_bin_le(struct archive *a, struct cpio *cpio, struct stat *st,
418 size_t *namelength, size_t *name_pad)
420 const void *h;
421 const struct cpio_bin_header *header;
422 size_t bytes;
424 a->archive_format = ARCHIVE_FORMAT_CPIO_BIN_LE;
425 a->archive_format_name = "cpio (little-endian binary)";
427 /* Read fixed-size portion of header. */
428 bytes = (a->compression_read_ahead)(a, &h, sizeof(struct cpio_bin_header));
429 if (bytes < sizeof(struct cpio_bin_header))
430 return (ARCHIVE_FATAL);
431 (a->compression_read_consume)(a, sizeof(struct cpio_bin_header));
433 /* Parse out binary fields into struct stat. */
434 header = h;
436 st->st_dev = header->c_dev[0] + header->c_dev[1] * 256;
437 st->st_ino = header->c_ino[0] + header->c_ino[1] * 256;
438 st->st_mode = header->c_mode[0] + header->c_mode[1] * 256;
439 st->st_uid = header->c_uid[0] + header->c_uid[1] * 256;
440 st->st_gid = header->c_gid[0] + header->c_gid[1] * 256;
441 st->st_nlink = header->c_nlink[0] + header->c_nlink[1] * 256;
442 st->st_rdev = header->c_rdev[0] + header->c_rdev[1] * 256;
443 st->st_mtime = le4(header->c_mtime);
444 *namelength = header->c_namesize[0] + header->c_namesize[1] * 256;
445 *name_pad = *namelength & 1; /* Pad to even. */
447 cpio->entry_bytes_remaining = le4(header->c_filesize);
448 st->st_size = cpio->entry_bytes_remaining;
449 cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
450 return (ARCHIVE_OK);
453 static int
454 header_bin_be(struct archive *a, struct cpio *cpio, struct stat *st,
455 size_t *namelength, size_t *name_pad)
457 const void *h;
458 const struct cpio_bin_header *header;
459 size_t bytes;
461 a->archive_format = ARCHIVE_FORMAT_CPIO_BIN_BE;
462 a->archive_format_name = "cpio (big-endian binary)";
464 /* Read fixed-size portion of header. */
465 bytes = (a->compression_read_ahead)(a, &h,
466 sizeof(struct cpio_bin_header));
467 if (bytes < sizeof(struct cpio_bin_header))
468 return (ARCHIVE_FATAL);
469 (a->compression_read_consume)(a, sizeof(struct cpio_bin_header));
471 /* Parse out binary fields into struct stat. */
472 header = h;
473 st->st_dev = header->c_dev[0] * 256 + header->c_dev[1];
474 st->st_ino = header->c_ino[0] * 256 + header->c_ino[1];
475 st->st_mode = header->c_mode[0] * 256 + header->c_mode[1];
476 st->st_uid = header->c_uid[0] * 256 + header->c_uid[1];
477 st->st_gid = header->c_gid[0] * 256 + header->c_gid[1];
478 st->st_nlink = header->c_nlink[0] * 256 + header->c_nlink[1];
479 st->st_rdev = header->c_rdev[0] * 256 + header->c_rdev[1];
480 st->st_mtime = be4(header->c_mtime);
481 *namelength = header->c_namesize[0] * 256 + header->c_namesize[1];
482 *name_pad = *namelength & 1; /* Pad to even. */
484 cpio->entry_bytes_remaining = be4(header->c_filesize);
485 st->st_size = cpio->entry_bytes_remaining;
486 cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
487 return (ARCHIVE_OK);
490 static int
491 archive_read_format_cpio_cleanup(struct archive *a)
493 struct cpio *cpio;
495 cpio = *(a->pformat_data);
496 /* Free inode->name map */
497 while (cpio->links_head != NULL) {
498 struct links_entry *lp = cpio->links_head->next;
500 if (cpio->links_head->name)
501 free(cpio->links_head->name);
502 free(cpio->links_head);
503 cpio->links_head = lp;
506 free(cpio);
507 *(a->pformat_data) = NULL;
508 return (ARCHIVE_OK);
511 static int
512 le4(const unsigned char *p)
514 return ((p[0]<<16) + (p[1]<<24) + (p[2]<<0) + (p[3]<<8));
518 static int
519 be4(const unsigned char *p)
521 return (p[0] + (p[1]<<8) + (p[2]<<16) + (p[3]<<24));
525 * Note that this implementation does not (and should not!) obey
526 * locale settings; you cannot simply substitute strtol here, since
527 * it does obey locale.
529 static int64_t
530 atol8(const char *p, unsigned char_cnt)
532 int64_t l;
533 int digit;
535 l = 0;
536 while (char_cnt-- > 0) {
537 if (*p >= '0' && *p <= '7')
538 digit = *p - '0';
539 else
540 return (l);
541 p++;
542 l <<= 3;
543 l |= digit;
545 return (l);
548 static int64_t
549 atol16(const char *p, unsigned char_cnt)
551 int64_t l;
552 int digit;
554 l = 0;
555 while (char_cnt-- > 0) {
556 if (*p >= 'a' && *p <= 'f')
557 digit = *p - 'a' + 10;
558 else if (*p >= 'A' && *p <= 'F')
559 digit = *p - 'A' + 10;
560 else if (*p >= '0' && *p <= '9')
561 digit = *p - '0';
562 else
563 return (l);
564 p++;
565 l <<= 4;
566 l |= digit;
568 return (l);
571 static void
572 record_hardlink(struct cpio *cpio, struct archive_entry *entry,
573 const struct stat *st)
575 struct links_entry *le;
578 * First look in the list of multiply-linked files. If we've
579 * already dumped it, convert this entry to a hard link entry.
581 for (le = cpio->links_head; le; le = le->next) {
582 if (le->dev == st->st_dev && le->ino == st->st_ino) {
583 archive_entry_set_hardlink(entry, le->name);
585 if (--le->links <= 0) {
586 if (le->previous != NULL)
587 le->previous->next = le->next;
588 if (le->next != NULL)
589 le->next->previous = le->previous;
590 if (cpio->links_head == le)
591 cpio->links_head = le->next;
592 free(le);
595 return;
599 le = malloc(sizeof(struct links_entry));
600 if (le == NULL)
601 __archive_errx(1, "Out of memory adding file to list");
602 if (cpio->links_head != NULL)
603 cpio->links_head->previous = le;
604 le->next = cpio->links_head;
605 le->previous = NULL;
606 cpio->links_head = le;
607 le->dev = st->st_dev;
608 le->ino = st->st_ino;
609 le->links = st->st_nlink - 1;
610 le->name = strdup(archive_entry_pathname(entry));
611 if (le->name == NULL)
612 __archive_errx(1, "Out of memory adding file to list");