2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
33 #include <sys/mkdev.h>
39 /* #include <stdint.h> */ /* See archive_platform.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
{
83 struct cpio_newc_header
{
101 struct links_entry
*next
;
102 struct links_entry
*previous
;
109 #define CPIO_MAGIC 0x13141516
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
;
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
*,
133 static int header_bin_le(struct archive_read
*, struct cpio
*, struct stat
*,
135 static int header_newc(struct archive_read
*, struct cpio
*, struct stat
*,
137 static int header_odc(struct archive_read
*, struct cpio
*, struct stat
*,
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
;
150 cpio
= (struct cpio
*)malloc(sizeof(*cpio
));
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
,
160 archive_read_format_cpio_bid
,
161 archive_read_format_cpio_read_header
,
162 archive_read_format_cpio_read_data
,
164 archive_read_format_cpio_cleanup
);
173 archive_read_format_cpio_bid(struct archive_read
*a
)
177 const unsigned char *p
;
180 cpio
= (struct cpio
*)*(a
->pformat_data
);
182 bytes_read
= (a
->compression_read_ahead
)(a
, &h
, 6);
183 /* Convert error code into error return. */
185 return ((int)bytes_read
);
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
;
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
;
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
;
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
;
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
;
224 /* Is more verification possible here? */
226 return (ARCHIVE_WARN
);
232 archive_read_format_cpio_read_header(struct archive_read
*a
,
233 struct archive_entry
*entry
)
243 memset(&st
, 0, sizeof(st
));
245 cpio
= (struct cpio
*)*(a
->pformat_data
);
246 r
= (cpio
->read_header(a
, cpio
, &st
, &namelength
, &name_pad
));
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
);
290 archive_read_format_cpio_read_data(struct archive_read
*a
,
291 const void **buff
, size_t *size
, off_t
*offset
)
296 cpio
= (struct cpio
*)*(a
->pformat_data
);
297 if (cpio
->entry_bytes_remaining
> 0) {
298 bytes_read
= (a
->compression_read_ahead
)(a
, buff
, 1);
300 return (ARCHIVE_FATAL
);
301 if (bytes_read
> cpio
->entry_bytes_remaining
)
302 bytes_read
= cpio
->entry_bytes_remaining
;
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
);
310 while (cpio
->entry_padding
> 0) {
311 bytes_read
= (a
->compression_read_ahead
)(a
, buff
, 1);
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
;
321 *offset
= cpio
->entry_offset
;
322 return (ARCHIVE_EOF
);
327 header_newc(struct archive_read
*a
, struct cpio
*cpio
, struct stat
*st
,
328 size_t *namelength
, size_t *name_pad
)
331 const struct cpio_newc_header
*header
;
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)";
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
;
384 header_odc(struct archive_read
*a
, struct cpio
*cpio
, struct stat
*st
,
385 size_t *namelength
, size_t *name_pad
)
388 const struct cpio_odc_header
*header
;
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;
428 header_bin_le(struct archive_read
*a
, struct cpio
*cpio
, struct stat
*st
,
429 size_t *namelength
, size_t *name_pad
)
432 const struct cpio_bin_header
*header
;
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. */
465 header_bin_be(struct archive_read
*a
, struct cpio
*cpio
, struct stat
*st
,
466 size_t *namelength
, size_t *name_pad
)
469 const struct cpio_bin_header
*header
;
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. */
502 archive_read_format_cpio_cleanup(struct archive_read
*a
)
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
;
518 *(a
->pformat_data
) = NULL
;
523 le4(const unsigned char *p
)
525 return ((p
[0]<<16) + (p
[1]<<24) + (p
[2]<<0) + (p
[3]<<8));
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.
541 atol8(const char *p
, unsigned char_cnt
)
547 while (char_cnt
-- > 0) {
548 if (*p
>= '0' && *p
<= '7')
560 atol16(const char *p
, unsigned char_cnt
)
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')
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
;
610 le
= (struct links_entry
*)malloc(sizeof(struct links_entry
));
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
;
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");