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.11 2004/08/14 03:45:45 kientzle Exp $");
33 /* #include <stdint.h> */ /* See archive_platform.h */
39 #include "archive_entry.h"
40 #include "archive_private.h"
42 struct cpio_bin_header {
43 unsigned char c_magic[2];
44 unsigned char c_dev[2];
45 unsigned char c_ino[2];
46 unsigned char c_mode[2];
47 unsigned char c_uid[2];
48 unsigned char c_gid[2];
49 unsigned char c_nlink[2];
50 unsigned char c_rdev[2];
51 unsigned char c_mtime[4];
52 unsigned char c_namesize[2];
53 unsigned char c_filesize[4];
56 struct cpio_odc_header {
70 struct cpio_newc_header {
88 struct links_entry *next;
89 struct links_entry *previous;
96 #define CPIO_MAGIC 0x13141516
99 int (*read_header)(struct archive *, struct cpio *,
100 struct stat *, size_t *, size_t *);
101 struct links_entry *links_head;
102 struct archive_string entry_name;
103 struct archive_string entry_linkname;
104 off_t entry_bytes_remaining;
109 static int64_t atol16(const char *, unsigned);
110 static int64_t atol8(const char *, unsigned);
111 static int archive_read_format_cpio_bid(struct archive *);
112 static int archive_read_format_cpio_cleanup(struct archive *);
113 static int archive_read_format_cpio_read_data(struct archive *,
114 const void **, size_t *, off_t *);
115 static int archive_read_format_cpio_read_header(struct archive *,
116 struct archive_entry *);
117 static int be4(const unsigned char *);
118 static int header_bin_be(struct archive *, struct cpio *, struct stat *,
120 static int header_bin_le(struct archive *, struct cpio *, struct stat *,
122 static int header_newc(struct archive *, struct cpio *, struct stat *,
124 static int header_odc(struct archive *, struct cpio *, struct stat *,
126 static int le4(const unsigned char *);
127 static void record_hardlink(struct cpio *cpio, struct archive_entry *entry,
128 const struct stat *st);
131 archive_read_support_format_cpio(struct archive *a)
136 cpio = malloc(sizeof(*cpio));
137 memset(cpio, 0, sizeof(*cpio));
138 cpio->magic = CPIO_MAGIC;
140 r = __archive_read_register_format(a,
142 archive_read_format_cpio_bid,
143 archive_read_format_cpio_read_header,
144 archive_read_format_cpio_read_data,
145 archive_read_format_cpio_cleanup);
154 archive_read_format_cpio_bid(struct archive *a)
158 const unsigned char *p;
161 cpio = *(a->pformat_data);
163 bytes_read = (a->compression_read_ahead)(a, &h, 6);
168 if (memcmp(p, "070707", 6) == 0) {
169 /* ASCII cpio archive (odc, POSIX.1) */
170 cpio->read_header = header_odc;
173 * XXX TODO: More verification; Could check that only octal
174 * digits appear in appropriate header locations. XXX
176 } else if (memcmp(p, "070701", 6) == 0) {
177 /* ASCII cpio archive (SVR4 without CRC) */
178 cpio->read_header = header_newc;
181 * XXX TODO: More verification; Could check that only hex
182 * digits appear in appropriate header locations. XXX
184 } else if (memcmp(p, "070702", 6) == 0) {
185 /* ASCII cpio archive (SVR4 with CRC) */
186 /* XXX TODO: Flag that we should check the CRC. XXX */
187 cpio->read_header = header_newc;
190 * XXX TODO: More verification; Could check that only hex
191 * digits appear in appropriate header locations. XXX
193 } else if (p[0] * 256 + p[1] == 070707) {
194 /* big-endian binary cpio archives */
195 cpio->read_header = header_bin_be;
197 /* Is more verification possible here? */
198 } else if (p[0] + p[1] * 256 == 070707) {
199 /* little-endian binary cpio archives */
200 cpio->read_header = header_bin_le;
202 /* Is more verification possible here? */
204 return (ARCHIVE_WARN);
210 archive_read_format_cpio_read_header(struct archive *a,
211 struct archive_entry *entry)
221 memset(&st, 0, sizeof(st));
223 cpio = *(a->pformat_data);
224 r = (cpio->read_header(a, cpio, &st, &namelength, &name_pad));
229 /* Assign all of the 'stat' fields at once. */
230 archive_entry_copy_stat(entry, &st);
232 /* Read name from buffer. */
233 bytes = (a->compression_read_ahead)(a, &h, namelength + name_pad);
234 if (bytes < namelength + name_pad)
235 return (ARCHIVE_FATAL);
236 (a->compression_read_consume)(a, namelength + name_pad);
237 archive_strncpy(&cpio->entry_name, h, namelength);
238 archive_entry_set_pathname(entry, cpio->entry_name.s);
239 cpio->entry_offset = 0;
241 /* If this is a symlink, read the link contents. */
242 if (S_ISLNK(st.st_mode)) {
243 bytes = (a->compression_read_ahead)(a, &h,
244 cpio->entry_bytes_remaining);
245 if ((off_t)bytes < cpio->entry_bytes_remaining)
246 return (ARCHIVE_FATAL);
247 (a->compression_read_consume)(a, cpio->entry_bytes_remaining);
248 archive_strncpy(&cpio->entry_linkname, h,
249 cpio->entry_bytes_remaining);
250 archive_entry_set_symlink(entry, cpio->entry_linkname.s);
251 cpio->entry_bytes_remaining = 0;
254 /* Compare name to "TRAILER!!!" to test for end-of-archive. */
255 if (namelength == 11 && strcmp(h,"TRAILER!!!")==0) {
256 /* TODO: Store file location of start of block. */
257 archive_set_error(a, 0, NULL);
258 return (ARCHIVE_EOF);
261 /* Detect and record hardlinks to previously-extracted entries. */
262 record_hardlink(cpio, entry, &st);
268 archive_read_format_cpio_read_data(struct archive *a,
269 const void **buff, size_t *size, off_t *offset)
274 cpio = *(a->pformat_data);
275 if (cpio->entry_bytes_remaining > 0) {
276 bytes_read = (a->compression_read_ahead)(a, buff, 1);
278 return (ARCHIVE_FATAL);
279 if (bytes_read > cpio->entry_bytes_remaining)
280 bytes_read = cpio->entry_bytes_remaining;
282 *offset = cpio->entry_offset;
283 cpio->entry_offset += bytes_read;
284 cpio->entry_bytes_remaining -= bytes_read;
285 (a->compression_read_consume)(a, bytes_read);
288 while (cpio->entry_padding > 0) {
289 bytes_read = (a->compression_read_ahead)(a, buff, 1);
291 return (ARCHIVE_FATAL);
292 if (bytes_read > cpio->entry_padding)
293 bytes_read = cpio->entry_padding;
294 (a->compression_read_consume)(a, bytes_read);
295 cpio->entry_padding -= bytes_read;
299 *offset = cpio->entry_offset;
300 return (ARCHIVE_EOF);
305 header_newc(struct archive *a, struct cpio *cpio, struct stat *st,
306 size_t *namelength, size_t *name_pad)
309 const struct cpio_newc_header *header;
312 a->archive_format = ARCHIVE_FORMAT_CPIO;
313 a->archive_format_name = "ASCII cpio (SVR4 with no CRC)";
315 /* Read fixed-size portion of header. */
316 bytes = (a->compression_read_ahead)(a, &h, sizeof(struct cpio_newc_header));
317 if (bytes < sizeof(struct cpio_newc_header))
318 return (ARCHIVE_FATAL);
319 (a->compression_read_consume)(a, sizeof(struct cpio_newc_header));
321 /* Parse out hex fields into struct stat. */
323 st->st_ino = atol16(header->c_ino, sizeof(header->c_ino));
324 st->st_mode = atol16(header->c_mode, sizeof(header->c_mode));
325 st->st_uid = atol16(header->c_uid, sizeof(header->c_uid));
326 st->st_gid = atol16(header->c_gid, sizeof(header->c_gid));
327 st->st_nlink = atol16(header->c_nlink, sizeof(header->c_nlink));
328 st->st_mtime = atol16(header->c_mtime, sizeof(header->c_mtime));
329 *namelength = atol16(header->c_namesize, sizeof(header->c_namesize));
330 /* Pad name to 2 more than a multiple of 4. */
331 *name_pad = (2 - *namelength) & 3;
334 * Note: entry_bytes_remaining is at least 64 bits and
335 * therefore gauranteed to be big enough for a 33-bit file
336 * size. struct stat.st_size may only be 32 bits, so
337 * assigning there first could lose information.
339 cpio->entry_bytes_remaining =
340 atol16(header->c_filesize, sizeof(header->c_filesize));
341 st->st_size = cpio->entry_bytes_remaining;
342 /* Pad file contents to a multiple of 4. */
343 cpio->entry_padding = 3 & -cpio->entry_bytes_remaining;
348 header_odc(struct archive *a, struct cpio *cpio, struct stat *st,
349 size_t *namelength, size_t *name_pad)
352 const struct cpio_odc_header *header;
355 a->archive_format = ARCHIVE_FORMAT_CPIO;
356 a->archive_format_name = "POSIX octet-oriented cpio";
358 /* Read fixed-size portion of header. */
359 bytes = (a->compression_read_ahead)(a, &h, sizeof(struct cpio_odc_header));
360 if (bytes < sizeof(struct cpio_odc_header))
361 return (ARCHIVE_FATAL);
362 (a->compression_read_consume)(a, sizeof(struct cpio_odc_header));
364 /* Parse out octal fields into struct stat. */
367 st->st_dev = atol8(header->c_dev, sizeof(header->c_dev));
368 st->st_ino = atol8(header->c_ino, sizeof(header->c_ino));
369 st->st_mode = atol8(header->c_mode, sizeof(header->c_mode));
370 st->st_uid = atol8(header->c_uid, sizeof(header->c_uid));
371 st->st_gid = atol8(header->c_gid, sizeof(header->c_gid));
372 st->st_nlink = atol8(header->c_nlink, sizeof(header->c_nlink));
373 st->st_rdev = atol8(header->c_rdev, sizeof(header->c_rdev));
374 st->st_mtime = atol8(header->c_mtime, sizeof(header->c_mtime));
375 *namelength = atol8(header->c_namesize, sizeof(header->c_namesize));
376 *name_pad = 0; /* No padding of filename. */
379 * Note: entry_bytes_remaining is at least 64 bits and
380 * therefore gauranteed to be big enough for a 33-bit file
381 * size. struct stat.st_size may only be 32 bits, so
382 * assigning there first could lose information.
384 cpio->entry_bytes_remaining =
385 atol8(header->c_filesize, sizeof(header->c_filesize));
386 st->st_size = cpio->entry_bytes_remaining;
387 cpio->entry_padding = 0;
392 header_bin_le(struct archive *a, struct cpio *cpio, struct stat *st,
393 size_t *namelength, size_t *name_pad)
396 const struct cpio_bin_header *header;
399 a->archive_format = ARCHIVE_FORMAT_CPIO;
400 a->archive_format_name = "cpio (little-endian binary)";
402 /* Read fixed-size portion of header. */
403 bytes = (a->compression_read_ahead)(a, &h, sizeof(struct cpio_bin_header));
404 if (bytes < sizeof(struct cpio_bin_header))
405 return (ARCHIVE_FATAL);
406 (a->compression_read_consume)(a, sizeof(struct cpio_bin_header));
408 /* Parse out binary fields into struct stat. */
411 st->st_dev = header->c_dev[0] + header->c_dev[1] * 256;
412 st->st_ino = header->c_ino[0] + header->c_ino[1] * 256;
413 st->st_mode = header->c_mode[0] + header->c_mode[1] * 256;
414 st->st_uid = header->c_uid[0] + header->c_uid[1] * 256;
415 st->st_gid = header->c_gid[0] + header->c_gid[1] * 256;
416 st->st_nlink = header->c_nlink[0] + header->c_nlink[1] * 256;
417 st->st_rdev = header->c_rdev[0] + header->c_rdev[1] * 256;
418 st->st_mtime = le4(header->c_mtime);
419 *namelength = header->c_namesize[0] + header->c_namesize[1] * 256;
420 *name_pad = *namelength & 1; /* Pad to even. */
422 cpio->entry_bytes_remaining = le4(header->c_filesize);
423 st->st_size = cpio->entry_bytes_remaining;
424 cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
429 header_bin_be(struct archive *a, struct cpio *cpio, struct stat *st,
430 size_t *namelength, size_t *name_pad)
433 const struct cpio_bin_header *header;
436 a->archive_format = ARCHIVE_FORMAT_CPIO;
437 a->archive_format_name = "cpio (big-endian binary)";
439 /* Read fixed-size portion of header. */
440 bytes = (a->compression_read_ahead)(a, &h,
441 sizeof(struct cpio_bin_header));
442 if (bytes < sizeof(struct cpio_bin_header))
443 return (ARCHIVE_FATAL);
444 (a->compression_read_consume)(a, sizeof(struct cpio_bin_header));
446 /* Parse out binary fields into struct stat. */
448 st->st_dev = header->c_dev[0] * 256 + header->c_dev[1];
449 st->st_ino = header->c_ino[0] * 256 + header->c_ino[1];
450 st->st_mode = header->c_mode[0] * 256 + header->c_mode[1];
451 st->st_uid = header->c_uid[0] * 256 + header->c_uid[1];
452 st->st_gid = header->c_gid[0] * 256 + header->c_gid[1];
453 st->st_nlink = header->c_nlink[0] * 256 + header->c_nlink[1];
454 st->st_rdev = header->c_rdev[0] * 256 + header->c_rdev[1];
455 st->st_mtime = be4(header->c_mtime);
456 *namelength = header->c_namesize[0] * 256 + header->c_namesize[1];
457 *name_pad = *namelength & 1; /* Pad to even. */
459 cpio->entry_bytes_remaining = be4(header->c_filesize);
460 st->st_size = cpio->entry_bytes_remaining;
461 cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
466 archive_read_format_cpio_cleanup(struct archive *a)
470 cpio = *(a->pformat_data);
471 /* Free inode->name map */
472 while (cpio->links_head != NULL) {
473 struct links_entry *lp = cpio->links_head->next;
475 if (cpio->links_head->name)
476 free(cpio->links_head->name);
477 free(cpio->links_head);
478 cpio->links_head = lp;
482 *(a->pformat_data) = NULL;
487 le4(const unsigned char *p)
489 return ((p[0]<<16) + (p[1]<<24) + (p[2]<<0) + (p[3]<<8));
494 be4(const unsigned char *p)
496 return (p[0] + (p[1]<<8) + (p[2]<<16) + (p[3]<<24));
500 * Note that this implementation does not (and should not!) obey
501 * locale settings; you cannot simply substitute strtol here, since
502 * it does obey locale.
505 atol8(const char *p, unsigned char_cnt)
511 while (char_cnt-- > 0) {
512 if (*p >= '0' && *p <= '7')
524 atol16(const char *p, unsigned char_cnt)
530 while (char_cnt-- > 0) {
531 if (*p >= 'a' && *p <= 'f')
532 digit = *p - 'a' + 10;
533 else if (*p >= 'A' && *p <= 'F')
534 digit = *p - 'A' + 10;
535 else if (*p >= '0' && *p <= '9')
547 record_hardlink(struct cpio *cpio, struct archive_entry *entry,
548 const struct stat *st)
550 struct links_entry *le;
553 * First look in the list of multiply-linked files. If we've
554 * already dumped it, convert this entry to a hard link entry.
556 for (le = cpio->links_head; le; le = le->next) {
557 if (le->dev == st->st_dev && le->ino == st->st_ino) {
558 archive_entry_set_hardlink(entry, le->name);
560 if (--le->links <= 0) {
561 if (le->previous != NULL)
562 le->previous->next = le->next;
563 if (le->next != NULL)
564 le->next->previous = le->previous;
565 if (cpio->links_head == le)
566 cpio->links_head = le->next;
574 le = malloc(sizeof(struct links_entry));
575 if (cpio->links_head != NULL)
576 cpio->links_head->previous = le;
577 le->next = cpio->links_head;
579 cpio->links_head = le;
580 le->dev = st->st_dev;
581 le->ino = st->st_ino;
582 le->links = st->st_nlink - 1;
583 le->name = strdup(archive_entry_pathname(entry));