Import libarchive and bsdtar version 2.0.25
[dragonfly/port-amd64.git] / contrib / libarchive-2.0 / libarchive / archive_read_support_format_cpio.c
blob2eb16568c6b43d10bea089bb0b8a32430ad40841
1 /*-
2 * Copyright (c) 2003-2007 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 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: src/lib/libarchive/archive_read_support_format_cpio.c,v 1.22 2007/03/03 07:37:36 kientzle Exp $");
29 #ifdef HAVE_SYS_STAT_H
30 #include <sys/stat.h>
31 #endif
32 #ifdef MAJOR_IN_MKDEV
33 #include <sys/mkdev.h>
34 #endif
36 #ifdef HAVE_ERRNO_H
37 #include <errno.h>
38 #endif
39 /* #include <stdint.h> */ /* See archive_platform.h */
40 #ifdef HAVE_STDLIB_H
41 #include <stdlib.h>
42 #endif
43 #ifdef HAVE_STRING_H
44 #include <string.h>
45 #endif
46 #ifdef HAVE_UNISTD_H
47 #include <unistd.h>
48 #endif
50 #include "archive.h"
51 #include "archive_entry.h"
52 #include "archive_private.h"
53 #include "archive_read_private.h"
55 struct cpio_bin_header {
56 unsigned char c_magic[2];
57 unsigned char c_dev[2];
58 unsigned char c_ino[2];
59 unsigned char c_mode[2];
60 unsigned char c_uid[2];
61 unsigned char c_gid[2];
62 unsigned char c_nlink[2];
63 unsigned char c_rdev[2];
64 unsigned char c_mtime[4];
65 unsigned char c_namesize[2];
66 unsigned char c_filesize[4];
69 struct cpio_odc_header {
70 char c_magic[6];
71 char c_dev[6];
72 char c_ino[6];
73 char c_mode[6];
74 char c_uid[6];
75 char c_gid[6];
76 char c_nlink[6];
77 char c_rdev[6];
78 char c_mtime[11];
79 char c_namesize[6];
80 char c_filesize[11];
83 struct cpio_newc_header {
84 char c_magic[6];
85 char c_ino[8];
86 char c_mode[8];
87 char c_uid[8];
88 char c_gid[8];
89 char c_nlink[8];
90 char c_mtime[8];
91 char c_filesize[8];
92 char c_devmajor[8];
93 char c_devminor[8];
94 char c_rdevmajor[8];
95 char c_rdevminor[8];
96 char c_namesize[8];
97 char c_crc[8];
100 struct links_entry {
101 struct links_entry *next;
102 struct links_entry *previous;
103 int links;
104 dev_t dev;
105 ino_t ino;
106 char *name;
109 #define CPIO_MAGIC 0x13141516
110 struct cpio {
111 int magic;
112 int (*read_header)(struct archive_read *, struct cpio *,
113 struct stat *, size_t *, size_t *);
114 struct links_entry *links_head;
115 struct archive_string entry_name;
116 struct archive_string entry_linkname;
117 off_t entry_bytes_remaining;
118 off_t entry_offset;
119 off_t entry_padding;
122 static int64_t atol16(const char *, unsigned);
123 static int64_t atol8(const char *, unsigned);
124 static int archive_read_format_cpio_bid(struct archive_read *);
125 static int archive_read_format_cpio_cleanup(struct archive_read *);
126 static int archive_read_format_cpio_read_data(struct archive_read *,
127 const void **, size_t *, off_t *);
128 static int archive_read_format_cpio_read_header(struct archive_read *,
129 struct archive_entry *);
130 static int be4(const unsigned char *);
131 static int header_bin_be(struct archive_read *, struct cpio *, struct stat *,
132 size_t *, size_t *);
133 static int header_bin_le(struct archive_read *, struct cpio *, struct stat *,
134 size_t *, size_t *);
135 static int header_newc(struct archive_read *, struct cpio *, struct stat *,
136 size_t *, size_t *);
137 static int header_odc(struct archive_read *, struct cpio *, struct stat *,
138 size_t *, size_t *);
139 static int le4(const unsigned char *);
140 static void record_hardlink(struct cpio *cpio, struct archive_entry *entry,
141 const struct stat *st);
144 archive_read_support_format_cpio(struct archive *_a)
146 struct archive_read *a = (struct archive_read *)_a;
147 struct cpio *cpio;
148 int r;
150 cpio = (struct cpio *)malloc(sizeof(*cpio));
151 if (cpio == NULL) {
152 archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
153 return (ARCHIVE_FATAL);
155 memset(cpio, 0, sizeof(*cpio));
156 cpio->magic = CPIO_MAGIC;
158 r = __archive_read_register_format(a,
159 cpio,
160 archive_read_format_cpio_bid,
161 archive_read_format_cpio_read_header,
162 archive_read_format_cpio_read_data,
163 NULL,
164 archive_read_format_cpio_cleanup);
166 if (r != ARCHIVE_OK)
167 free(cpio);
168 return (ARCHIVE_OK);
172 static int
173 archive_read_format_cpio_bid(struct archive_read *a)
175 int bid, bytes_read;
176 const void *h;
177 const unsigned char *p;
178 struct cpio *cpio;
180 cpio = (struct cpio *)*(a->pformat_data);
181 bid = 0;
182 bytes_read = (a->compression_read_ahead)(a, &h, 6);
183 /* Convert error code into error return. */
184 if (bytes_read < 0)
185 return ((int)bytes_read);
186 if (bytes_read < 6)
187 return (-1);
189 p = (const unsigned char *)h;
190 if (memcmp(p, "070707", 6) == 0) {
191 /* ASCII cpio archive (odc, POSIX.1) */
192 cpio->read_header = header_odc;
193 bid += 48;
195 * XXX TODO: More verification; Could check that only octal
196 * digits appear in appropriate header locations. XXX
198 } else if (memcmp(p, "070701", 6) == 0) {
199 /* ASCII cpio archive (SVR4 without CRC) */
200 cpio->read_header = header_newc;
201 bid += 48;
203 * XXX TODO: More verification; Could check that only hex
204 * digits appear in appropriate header locations. XXX
206 } else if (memcmp(p, "070702", 6) == 0) {
207 /* ASCII cpio archive (SVR4 with CRC) */
208 /* XXX TODO: Flag that we should check the CRC. XXX */
209 cpio->read_header = header_newc;
210 bid += 48;
212 * XXX TODO: More verification; Could check that only hex
213 * digits appear in appropriate header locations. XXX
215 } else if (p[0] * 256 + p[1] == 070707) {
216 /* big-endian binary cpio archives */
217 cpio->read_header = header_bin_be;
218 bid += 16;
219 /* Is more verification possible here? */
220 } else if (p[0] + p[1] * 256 == 070707) {
221 /* little-endian binary cpio archives */
222 cpio->read_header = header_bin_le;
223 bid += 16;
224 /* Is more verification possible here? */
225 } else
226 return (ARCHIVE_WARN);
228 return (bid);
231 static int
232 archive_read_format_cpio_read_header(struct archive_read *a,
233 struct archive_entry *entry)
235 struct stat st;
236 struct cpio *cpio;
237 size_t bytes;
238 const void *h;
239 size_t namelength;
240 size_t name_pad;
241 int r;
243 memset(&st, 0, sizeof(st));
245 cpio = (struct cpio *)*(a->pformat_data);
246 r = (cpio->read_header(a, cpio, &st, &namelength, &name_pad));
248 if (r != ARCHIVE_OK)
249 return (r);
251 /* Assign all of the 'stat' fields at once. */
252 archive_entry_copy_stat(entry, &st);
254 /* Read name from buffer. */
255 bytes = (a->compression_read_ahead)(a, &h, namelength + name_pad);
256 if (bytes < namelength + name_pad)
257 return (ARCHIVE_FATAL);
258 (a->compression_read_consume)(a, namelength + name_pad);
259 archive_strncpy(&cpio->entry_name, (const char *)h, namelength);
260 archive_entry_set_pathname(entry, cpio->entry_name.s);
261 cpio->entry_offset = 0;
263 /* If this is a symlink, read the link contents. */
264 if (S_ISLNK(st.st_mode)) {
265 bytes = (a->compression_read_ahead)(a, &h,
266 cpio->entry_bytes_remaining);
267 if ((off_t)bytes < cpio->entry_bytes_remaining)
268 return (ARCHIVE_FATAL);
269 (a->compression_read_consume)(a, cpio->entry_bytes_remaining);
270 archive_strncpy(&cpio->entry_linkname, (const char *)h,
271 cpio->entry_bytes_remaining);
272 archive_entry_set_symlink(entry, cpio->entry_linkname.s);
273 cpio->entry_bytes_remaining = 0;
276 /* Compare name to "TRAILER!!!" to test for end-of-archive. */
277 if (namelength == 11 && strcmp((const char *)h, "TRAILER!!!") == 0) {
278 /* TODO: Store file location of start of block. */
279 archive_set_error(&a->archive, 0, NULL);
280 return (ARCHIVE_EOF);
283 /* Detect and record hardlinks to previously-extracted entries. */
284 record_hardlink(cpio, entry, &st);
286 return (ARCHIVE_OK);
289 static int
290 archive_read_format_cpio_read_data(struct archive_read *a,
291 const void **buff, size_t *size, off_t *offset)
293 ssize_t bytes_read;
294 struct cpio *cpio;
296 cpio = (struct cpio *)*(a->pformat_data);
297 if (cpio->entry_bytes_remaining > 0) {
298 bytes_read = (a->compression_read_ahead)(a, buff, 1);
299 if (bytes_read <= 0)
300 return (ARCHIVE_FATAL);
301 if (bytes_read > cpio->entry_bytes_remaining)
302 bytes_read = cpio->entry_bytes_remaining;
303 *size = bytes_read;
304 *offset = cpio->entry_offset;
305 cpio->entry_offset += bytes_read;
306 cpio->entry_bytes_remaining -= bytes_read;
307 (a->compression_read_consume)(a, bytes_read);
308 return (ARCHIVE_OK);
309 } else {
310 while (cpio->entry_padding > 0) {
311 bytes_read = (a->compression_read_ahead)(a, buff, 1);
312 if (bytes_read <= 0)
313 return (ARCHIVE_FATAL);
314 if (bytes_read > cpio->entry_padding)
315 bytes_read = cpio->entry_padding;
316 (a->compression_read_consume)(a, bytes_read);
317 cpio->entry_padding -= bytes_read;
319 *buff = NULL;
320 *size = 0;
321 *offset = cpio->entry_offset;
322 return (ARCHIVE_EOF);
326 static int
327 header_newc(struct archive_read *a, struct cpio *cpio, struct stat *st,
328 size_t *namelength, size_t *name_pad)
330 const void *h;
331 const struct cpio_newc_header *header;
332 size_t bytes;
334 /* Read fixed-size portion of header. */
335 bytes = (a->compression_read_ahead)(a, &h, sizeof(struct cpio_newc_header));
336 if (bytes < sizeof(struct cpio_newc_header))
337 return (ARCHIVE_FATAL);
338 (a->compression_read_consume)(a, sizeof(struct cpio_newc_header));
340 /* Parse out hex fields into struct stat. */
341 header = (const struct cpio_newc_header *)h;
343 if (memcmp(header->c_magic, "070701", 6) == 0) {
344 a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_NOCRC;
345 a->archive.archive_format_name = "ASCII cpio (SVR4 with no CRC)";
346 } else if (memcmp(header->c_magic, "070702", 6) == 0) {
347 a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_CRC;
348 a->archive.archive_format_name = "ASCII cpio (SVR4 with CRC)";
349 } else {
350 /* TODO: Abort here? */
353 st->st_dev = makedev(
354 atol16(header->c_devmajor, sizeof(header->c_devmajor)),
355 atol16(header->c_devminor, sizeof(header->c_devminor)));
356 st->st_ino = atol16(header->c_ino, sizeof(header->c_ino));
357 st->st_mode = atol16(header->c_mode, sizeof(header->c_mode));
358 st->st_uid = atol16(header->c_uid, sizeof(header->c_uid));
359 st->st_gid = atol16(header->c_gid, sizeof(header->c_gid));
360 st->st_nlink = atol16(header->c_nlink, sizeof(header->c_nlink));
361 st->st_rdev = makedev(
362 atol16(header->c_rdevmajor, sizeof(header->c_rdevmajor)),
363 atol16(header->c_rdevminor, sizeof(header->c_rdevminor)));
364 st->st_mtime = atol16(header->c_mtime, sizeof(header->c_mtime));
365 *namelength = atol16(header->c_namesize, sizeof(header->c_namesize));
366 /* Pad name to 2 more than a multiple of 4. */
367 *name_pad = (2 - *namelength) & 3;
370 * Note: entry_bytes_remaining is at least 64 bits and
371 * therefore guaranteed to be big enough for a 33-bit file
372 * size. struct stat.st_size may only be 32 bits, so
373 * assigning there first could lose information.
375 cpio->entry_bytes_remaining =
376 atol16(header->c_filesize, sizeof(header->c_filesize));
377 st->st_size = cpio->entry_bytes_remaining;
378 /* Pad file contents to a multiple of 4. */
379 cpio->entry_padding = 3 & -cpio->entry_bytes_remaining;
380 return (ARCHIVE_OK);
383 static int
384 header_odc(struct archive_read *a, struct cpio *cpio, struct stat *st,
385 size_t *namelength, size_t *name_pad)
387 const void *h;
388 const struct cpio_odc_header *header;
389 size_t bytes;
391 a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
392 a->archive.archive_format_name = "POSIX octet-oriented cpio";
394 /* Read fixed-size portion of header. */
395 bytes = (a->compression_read_ahead)(a, &h, sizeof(struct cpio_odc_header));
396 if (bytes < sizeof(struct cpio_odc_header))
397 return (ARCHIVE_FATAL);
398 (a->compression_read_consume)(a, sizeof(struct cpio_odc_header));
400 /* Parse out octal fields into struct stat. */
401 header = (const struct cpio_odc_header *)h;
403 st->st_dev = atol8(header->c_dev, sizeof(header->c_dev));
404 st->st_ino = atol8(header->c_ino, sizeof(header->c_ino));
405 st->st_mode = atol8(header->c_mode, sizeof(header->c_mode));
406 st->st_uid = atol8(header->c_uid, sizeof(header->c_uid));
407 st->st_gid = atol8(header->c_gid, sizeof(header->c_gid));
408 st->st_nlink = atol8(header->c_nlink, sizeof(header->c_nlink));
409 st->st_rdev = atol8(header->c_rdev, sizeof(header->c_rdev));
410 st->st_mtime = atol8(header->c_mtime, sizeof(header->c_mtime));
411 *namelength = atol8(header->c_namesize, sizeof(header->c_namesize));
412 *name_pad = 0; /* No padding of filename. */
415 * Note: entry_bytes_remaining is at least 64 bits and
416 * therefore guaranteed to be big enough for a 33-bit file
417 * size. struct stat.st_size may only be 32 bits, so
418 * assigning there first could lose information.
420 cpio->entry_bytes_remaining =
421 atol8(header->c_filesize, sizeof(header->c_filesize));
422 st->st_size = cpio->entry_bytes_remaining;
423 cpio->entry_padding = 0;
424 return (ARCHIVE_OK);
427 static int
428 header_bin_le(struct archive_read *a, struct cpio *cpio, struct stat *st,
429 size_t *namelength, size_t *name_pad)
431 const void *h;
432 const struct cpio_bin_header *header;
433 size_t bytes;
435 a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_LE;
436 a->archive.archive_format_name = "cpio (little-endian binary)";
438 /* Read fixed-size portion of header. */
439 bytes = (a->compression_read_ahead)(a, &h, sizeof(struct cpio_bin_header));
440 if (bytes < sizeof(struct cpio_bin_header))
441 return (ARCHIVE_FATAL);
442 (a->compression_read_consume)(a, sizeof(struct cpio_bin_header));
444 /* Parse out binary fields into struct stat. */
445 header = (const struct cpio_bin_header *)h;
447 st->st_dev = header->c_dev[0] + header->c_dev[1] * 256;
448 st->st_ino = header->c_ino[0] + header->c_ino[1] * 256;
449 st->st_mode = header->c_mode[0] + header->c_mode[1] * 256;
450 st->st_uid = header->c_uid[0] + header->c_uid[1] * 256;
451 st->st_gid = header->c_gid[0] + header->c_gid[1] * 256;
452 st->st_nlink = header->c_nlink[0] + header->c_nlink[1] * 256;
453 st->st_rdev = header->c_rdev[0] + header->c_rdev[1] * 256;
454 st->st_mtime = le4(header->c_mtime);
455 *namelength = header->c_namesize[0] + header->c_namesize[1] * 256;
456 *name_pad = *namelength & 1; /* Pad to even. */
458 cpio->entry_bytes_remaining = le4(header->c_filesize);
459 st->st_size = cpio->entry_bytes_remaining;
460 cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
461 return (ARCHIVE_OK);
464 static int
465 header_bin_be(struct archive_read *a, struct cpio *cpio, struct stat *st,
466 size_t *namelength, size_t *name_pad)
468 const void *h;
469 const struct cpio_bin_header *header;
470 size_t bytes;
472 a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_BE;
473 a->archive.archive_format_name = "cpio (big-endian binary)";
475 /* Read fixed-size portion of header. */
476 bytes = (a->compression_read_ahead)(a, &h,
477 sizeof(struct cpio_bin_header));
478 if (bytes < sizeof(struct cpio_bin_header))
479 return (ARCHIVE_FATAL);
480 (a->compression_read_consume)(a, sizeof(struct cpio_bin_header));
482 /* Parse out binary fields into struct stat. */
483 header = (const struct cpio_bin_header *)h;
484 st->st_dev = header->c_dev[0] * 256 + header->c_dev[1];
485 st->st_ino = header->c_ino[0] * 256 + header->c_ino[1];
486 st->st_mode = header->c_mode[0] * 256 + header->c_mode[1];
487 st->st_uid = header->c_uid[0] * 256 + header->c_uid[1];
488 st->st_gid = header->c_gid[0] * 256 + header->c_gid[1];
489 st->st_nlink = header->c_nlink[0] * 256 + header->c_nlink[1];
490 st->st_rdev = header->c_rdev[0] * 256 + header->c_rdev[1];
491 st->st_mtime = be4(header->c_mtime);
492 *namelength = header->c_namesize[0] * 256 + header->c_namesize[1];
493 *name_pad = *namelength & 1; /* Pad to even. */
495 cpio->entry_bytes_remaining = be4(header->c_filesize);
496 st->st_size = cpio->entry_bytes_remaining;
497 cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
498 return (ARCHIVE_OK);
501 static int
502 archive_read_format_cpio_cleanup(struct archive_read *a)
504 struct cpio *cpio;
506 cpio = (struct cpio *)*(a->pformat_data);
507 /* Free inode->name map */
508 while (cpio->links_head != NULL) {
509 struct links_entry *lp = cpio->links_head->next;
511 if (cpio->links_head->name)
512 free(cpio->links_head->name);
513 free(cpio->links_head);
514 cpio->links_head = lp;
517 free(cpio);
518 *(a->pformat_data) = NULL;
519 return (ARCHIVE_OK);
522 static int
523 le4(const unsigned char *p)
525 return ((p[0]<<16) + (p[1]<<24) + (p[2]<<0) + (p[3]<<8));
529 static int
530 be4(const unsigned char *p)
532 return (p[0] + (p[1]<<8) + (p[2]<<16) + (p[3]<<24));
536 * Note that this implementation does not (and should not!) obey
537 * locale settings; you cannot simply substitute strtol here, since
538 * it does obey locale.
540 static int64_t
541 atol8(const char *p, unsigned char_cnt)
543 int64_t l;
544 int digit;
546 l = 0;
547 while (char_cnt-- > 0) {
548 if (*p >= '0' && *p <= '7')
549 digit = *p - '0';
550 else
551 return (l);
552 p++;
553 l <<= 3;
554 l |= digit;
556 return (l);
559 static int64_t
560 atol16(const char *p, unsigned char_cnt)
562 int64_t l;
563 int digit;
565 l = 0;
566 while (char_cnt-- > 0) {
567 if (*p >= 'a' && *p <= 'f')
568 digit = *p - 'a' + 10;
569 else if (*p >= 'A' && *p <= 'F')
570 digit = *p - 'A' + 10;
571 else if (*p >= '0' && *p <= '9')
572 digit = *p - '0';
573 else
574 return (l);
575 p++;
576 l <<= 4;
577 l |= digit;
579 return (l);
582 static void
583 record_hardlink(struct cpio *cpio, struct archive_entry *entry,
584 const struct stat *st)
586 struct links_entry *le;
589 * First look in the list of multiply-linked files. If we've
590 * already dumped it, convert this entry to a hard link entry.
592 for (le = cpio->links_head; le; le = le->next) {
593 if (le->dev == st->st_dev && le->ino == st->st_ino) {
594 archive_entry_set_hardlink(entry, le->name);
596 if (--le->links <= 0) {
597 if (le->previous != NULL)
598 le->previous->next = le->next;
599 if (le->next != NULL)
600 le->next->previous = le->previous;
601 if (cpio->links_head == le)
602 cpio->links_head = le->next;
603 free(le);
606 return;
610 le = (struct links_entry *)malloc(sizeof(struct links_entry));
611 if (le == NULL)
612 __archive_errx(1, "Out of memory adding file to list");
613 if (cpio->links_head != NULL)
614 cpio->links_head->previous = le;
615 le->next = cpio->links_head;
616 le->previous = NULL;
617 cpio->links_head = le;
618 le->dev = st->st_dev;
619 le->ino = st->st_ino;
620 le->links = st->st_nlink - 1;
621 le->name = strdup(archive_entry_pathname(entry));
622 if (le->name == NULL)
623 __archive_errx(1, "Out of memory adding file to list");