2 * Copyright (c) 2003-2004 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 * 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 $");
32 #include <sys/mkdev.h>
36 /* #include <stdint.h> */ /* See archive_platform.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
{
73 struct cpio_newc_header
{
91 struct links_entry
*next
;
92 struct links_entry
*previous
;
99 #define CPIO_MAGIC 0x13141516
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
;
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
*,
123 static int header_bin_le(struct archive
*, struct cpio
*, struct stat
*,
125 static int header_newc(struct archive
*, struct cpio
*, struct stat
*,
127 static int header_odc(struct archive
*, struct cpio
*, struct stat
*,
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
)
139 cpio
= malloc(sizeof(*cpio
));
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
,
149 archive_read_format_cpio_bid
,
150 archive_read_format_cpio_read_header
,
151 archive_read_format_cpio_read_data
,
153 archive_read_format_cpio_cleanup
);
162 archive_read_format_cpio_bid(struct archive
*a
)
166 const unsigned char *p
;
169 cpio
= *(a
->pformat_data
);
171 bytes_read
= (a
->compression_read_ahead
)(a
, &h
, 6);
172 /* Convert error code into error return. */
174 return ((int)bytes_read
);
179 if (memcmp(p
, "070707", 6) == 0) {
180 /* ASCII cpio archive (odc, POSIX.1) */
181 cpio
->read_header
= header_odc
;
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
;
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
;
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
;
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
;
213 /* Is more verification possible here? */
215 return (ARCHIVE_WARN
);
221 archive_read_format_cpio_read_header(struct archive
*a
,
222 struct archive_entry
*entry
)
232 memset(&st
, 0, sizeof(st
));
234 cpio
= *(a
->pformat_data
);
235 r
= (cpio
->read_header(a
, cpio
, &st
, &namelength
, &name_pad
));
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
);
279 archive_read_format_cpio_read_data(struct archive
*a
,
280 const void **buff
, size_t *size
, off_t
*offset
)
285 cpio
= *(a
->pformat_data
);
286 if (cpio
->entry_bytes_remaining
> 0) {
287 bytes_read
= (a
->compression_read_ahead
)(a
, buff
, 1);
289 return (ARCHIVE_FATAL
);
290 if (bytes_read
> cpio
->entry_bytes_remaining
)
291 bytes_read
= cpio
->entry_bytes_remaining
;
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
);
299 while (cpio
->entry_padding
> 0) {
300 bytes_read
= (a
->compression_read_ahead
)(a
, buff
, 1);
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
;
310 *offset
= cpio
->entry_offset
;
311 return (ARCHIVE_EOF
);
316 header_newc(struct archive
*a
, struct cpio
*cpio
, struct stat
*st
,
317 size_t *namelength
, size_t *name_pad
)
320 const struct cpio_newc_header
*header
;
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. */
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)";
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
;
373 header_odc(struct archive
*a
, struct cpio
*cpio
, struct stat
*st
,
374 size_t *namelength
, size_t *name_pad
)
377 const struct cpio_odc_header
*header
;
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. */
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;
417 header_bin_le(struct archive
*a
, struct cpio
*cpio
, struct stat
*st
,
418 size_t *namelength
, size_t *name_pad
)
421 const struct cpio_bin_header
*header
;
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. */
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. */
454 header_bin_be(struct archive
*a
, struct cpio
*cpio
, struct stat
*st
,
455 size_t *namelength
, size_t *name_pad
)
458 const struct cpio_bin_header
*header
;
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. */
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. */
491 archive_read_format_cpio_cleanup(struct archive
*a
)
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
;
507 *(a
->pformat_data
) = NULL
;
512 le4(const unsigned char *p
)
514 return ((p
[0]<<16) + (p
[1]<<24) + (p
[2]<<0) + (p
[3]<<8));
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.
530 atol8(const char *p
, unsigned char_cnt
)
536 while (char_cnt
-- > 0) {
537 if (*p
>= '0' && *p
<= '7')
549 atol16(const char *p
, unsigned char_cnt
)
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')
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
;
599 le
= malloc(sizeof(struct links_entry
));
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
;
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");