Import libarchive-2.4.8:
[dragonfly.git] / contrib / libarchive-2 / libarchive / archive_read_support_format_cpio.c
blob8c531fa199bcf46860f61f8c06dffe6145cc957e
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.24 2007/05/29 01:00:19 kientzle Exp $");
29 #ifdef HAVE_ERRNO_H
30 #include <errno.h>
31 #endif
32 /* #include <stdint.h> */ /* See archive_platform.h */
33 #ifdef HAVE_STDLIB_H
34 #include <stdlib.h>
35 #endif
36 #ifdef HAVE_STRING_H
37 #include <string.h>
38 #endif
40 #include "archive.h"
41 #include "archive_entry.h"
42 #include "archive_private.h"
43 #include "archive_read_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_read *, struct cpio *,
103 struct archive_entry *, 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_read *);
115 static int archive_read_format_cpio_cleanup(struct archive_read *);
116 static int archive_read_format_cpio_read_data(struct archive_read *,
117 const void **, size_t *, off_t *);
118 static int archive_read_format_cpio_read_header(struct archive_read *,
119 struct archive_entry *);
120 static int be4(const unsigned char *);
121 static int find_odc_header(struct archive_read *);
122 static int find_newc_header(struct archive_read *);
123 static int header_bin_be(struct archive_read *, struct cpio *,
124 struct archive_entry *, size_t *, size_t *);
125 static int header_bin_le(struct archive_read *, struct cpio *,
126 struct archive_entry *, size_t *, size_t *);
127 static int header_newc(struct archive_read *, struct cpio *,
128 struct archive_entry *, size_t *, size_t *);
129 static int header_odc(struct archive_read *, struct cpio *,
130 struct archive_entry *, size_t *, size_t *);
131 static int is_octal(const char *, size_t);
132 static int is_hex(const char *, size_t);
133 static int le4(const unsigned char *);
134 static void record_hardlink(struct cpio *cpio, struct archive_entry *entry);
137 archive_read_support_format_cpio(struct archive *_a)
139 struct archive_read *a = (struct archive_read *)_a;
140 struct cpio *cpio;
141 int r;
143 cpio = (struct cpio *)malloc(sizeof(*cpio));
144 if (cpio == NULL) {
145 archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
146 return (ARCHIVE_FATAL);
148 memset(cpio, 0, sizeof(*cpio));
149 cpio->magic = CPIO_MAGIC;
151 r = __archive_read_register_format(a,
152 cpio,
153 archive_read_format_cpio_bid,
154 archive_read_format_cpio_read_header,
155 archive_read_format_cpio_read_data,
156 NULL,
157 archive_read_format_cpio_cleanup);
159 if (r != ARCHIVE_OK)
160 free(cpio);
161 return (ARCHIVE_OK);
165 static int
166 archive_read_format_cpio_bid(struct archive_read *a)
168 int bytes_read;
169 const void *h;
170 const unsigned char *p;
171 struct cpio *cpio;
172 int bid;
174 cpio = (struct cpio *)(a->format->data);
176 bytes_read = (a->decompressor->read_ahead)(a, &h, 6);
177 /* Convert error code into error return. */
178 if (bytes_read < 0)
179 return ((int)bytes_read);
180 if (bytes_read < 6)
181 return (-1);
183 p = (const unsigned char *)h;
184 bid = 0;
185 if (memcmp(p, "070707", 6) == 0) {
186 /* ASCII cpio archive (odc, POSIX.1) */
187 cpio->read_header = header_odc;
188 bid += 48;
190 * XXX TODO: More verification; Could check that only octal
191 * digits appear in appropriate header locations. XXX
193 } else if (memcmp(p, "070701", 6) == 0) {
194 /* ASCII cpio archive (SVR4 without CRC) */
195 cpio->read_header = header_newc;
196 bid += 48;
198 * XXX TODO: More verification; Could check that only hex
199 * digits appear in appropriate header locations. XXX
201 } else if (memcmp(p, "070702", 6) == 0) {
202 /* ASCII cpio archive (SVR4 with CRC) */
203 /* XXX TODO: Flag that we should check the CRC. XXX */
204 cpio->read_header = header_newc;
205 bid += 48;
207 * XXX TODO: More verification; Could check that only hex
208 * digits appear in appropriate header locations. XXX
210 } else if (p[0] * 256 + p[1] == 070707) {
211 /* big-endian binary cpio archives */
212 cpio->read_header = header_bin_be;
213 bid += 16;
214 /* Is more verification possible here? */
215 } else if (p[0] + p[1] * 256 == 070707) {
216 /* little-endian binary cpio archives */
217 cpio->read_header = header_bin_le;
218 bid += 16;
219 /* Is more verification possible here? */
220 } else
221 return (ARCHIVE_WARN);
223 return (bid);
226 static int
227 archive_read_format_cpio_read_header(struct archive_read *a,
228 struct archive_entry *entry)
230 struct cpio *cpio;
231 size_t bytes;
232 const void *h;
233 size_t namelength;
234 size_t name_pad;
235 int r;
237 cpio = (struct cpio *)(a->format->data);
238 r = (cpio->read_header(a, cpio, entry, &namelength, &name_pad));
240 if (r < ARCHIVE_WARN)
241 return (r);
243 /* Read name from buffer. */
244 bytes = (a->decompressor->read_ahead)(a, &h, namelength + name_pad);
245 if (bytes < namelength + name_pad)
246 return (ARCHIVE_FATAL);
247 (a->decompressor->consume)(a, namelength + name_pad);
248 archive_strncpy(&cpio->entry_name, (const char *)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 (archive_entry_filetype(entry) == AE_IFLNK) {
254 bytes = (a->decompressor->read_ahead)(a, &h,
255 cpio->entry_bytes_remaining);
256 if ((off_t)bytes < cpio->entry_bytes_remaining)
257 return (ARCHIVE_FATAL);
258 (a->decompressor->consume)(a, cpio->entry_bytes_remaining);
259 archive_strncpy(&cpio->entry_linkname, (const char *)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((const char *)h, "TRAILER!!!") == 0) {
267 /* TODO: Store file location of start of block. */
268 archive_set_error(&a->archive, 0, NULL);
269 return (ARCHIVE_EOF);
272 /* Detect and record hardlinks to previously-extracted entries. */
273 record_hardlink(cpio, entry);
275 return (r);
278 static int
279 archive_read_format_cpio_read_data(struct archive_read *a,
280 const void **buff, size_t *size, off_t *offset)
282 ssize_t bytes_read;
283 struct cpio *cpio;
285 cpio = (struct cpio *)(a->format->data);
286 if (cpio->entry_bytes_remaining > 0) {
287 bytes_read = (a->decompressor->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->decompressor->consume)(a, bytes_read);
297 return (ARCHIVE_OK);
298 } else {
299 while (cpio->entry_padding > 0) {
300 bytes_read = (a->decompressor->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->decompressor->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);
316 * Skip forward to the next cpio newc header by searching for the
317 * 07070[12] string. This should be generalized and merged with
318 * find_odc_header below.
320 static int
321 is_hex(const char *p, size_t len)
323 while (len-- > 0) {
324 if (*p < '0' || (*p > '9' && *p < 'a') || *p > 'f') {
325 return (0);
327 ++p;
329 return (1);
332 static int
333 find_newc_header(struct archive_read *a)
335 const void *h;
336 const char *p, *q;
337 size_t skip, bytes, skipped = 0;
339 for (;;) {
340 bytes = (a->decompressor->read_ahead)(a, &h, 2048);
341 if (bytes < sizeof(struct cpio_newc_header))
342 return (ARCHIVE_FATAL);
343 p = h;
344 q = p + bytes;
346 /* Try the typical case first, then go into the slow search.*/
347 if (memcmp("07070", p, 5) == 0
348 && (p[5] == '1' || p[5] == '2')
349 && is_hex(p, sizeof(struct cpio_newc_header)))
350 return (ARCHIVE_OK);
353 * Scan ahead until we find something that looks
354 * like an odc header.
356 while (p + sizeof(struct cpio_newc_header) < q) {
357 switch (p[5]) {
358 case '1':
359 case '2':
360 if (memcmp("07070", p, 5) == 0
361 && is_hex(p, sizeof(struct cpio_newc_header))) {
362 skip = p - (const char *)h;
363 (a->decompressor->consume)(a, skip);
364 skipped += skip;
365 if (skipped > 0) {
366 archive_set_error(&a->archive,
368 "Skipped %d bytes before "
369 "finding valid header",
370 (int)skipped);
371 return (ARCHIVE_WARN);
373 return (ARCHIVE_OK);
375 p += 2;
376 break;
377 case '0':
378 p++;
379 break;
380 default:
381 p += 6;
382 break;
385 skip = p - (const char *)h;
386 (a->decompressor->consume)(a, skip);
387 skipped += skip;
391 static int
392 header_newc(struct archive_read *a, struct cpio *cpio,
393 struct archive_entry *entry, size_t *namelength, size_t *name_pad)
395 const void *h;
396 const struct cpio_newc_header *header;
397 size_t bytes;
398 int r;
400 r = find_newc_header(a);
401 if (r < ARCHIVE_WARN)
402 return (r);
404 /* Read fixed-size portion of header. */
405 bytes = (a->decompressor->read_ahead)(a, &h, sizeof(struct cpio_newc_header));
406 if (bytes < sizeof(struct cpio_newc_header))
407 return (ARCHIVE_FATAL);
408 (a->decompressor->consume)(a, sizeof(struct cpio_newc_header));
410 /* Parse out hex fields. */
411 header = (const struct cpio_newc_header *)h;
413 if (memcmp(header->c_magic, "070701", 6) == 0) {
414 a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_NOCRC;
415 a->archive.archive_format_name = "ASCII cpio (SVR4 with no CRC)";
416 } else if (memcmp(header->c_magic, "070702", 6) == 0) {
417 a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_CRC;
418 a->archive.archive_format_name = "ASCII cpio (SVR4 with CRC)";
419 } else {
420 /* TODO: Abort here? */
423 archive_entry_set_devmajor(entry, atol16(header->c_devmajor, sizeof(header->c_devmajor)));
424 archive_entry_set_devminor(entry, atol16(header->c_devminor, sizeof(header->c_devminor)));
425 archive_entry_set_ino(entry, atol16(header->c_ino, sizeof(header->c_ino)));
426 archive_entry_set_mode(entry, atol16(header->c_mode, sizeof(header->c_mode)));
427 archive_entry_set_uid(entry, atol16(header->c_uid, sizeof(header->c_uid)));
428 archive_entry_set_gid(entry, atol16(header->c_gid, sizeof(header->c_gid)));
429 archive_entry_set_nlink(entry, atol16(header->c_nlink, sizeof(header->c_nlink)));
430 archive_entry_set_rdevmajor(entry, atol16(header->c_rdevmajor, sizeof(header->c_rdevmajor)));
431 archive_entry_set_rdevminor(entry, atol16(header->c_rdevminor, sizeof(header->c_rdevminor)));
432 archive_entry_set_mtime(entry, atol16(header->c_mtime, sizeof(header->c_mtime)), 0);
433 *namelength = atol16(header->c_namesize, sizeof(header->c_namesize));
434 /* Pad name to 2 more than a multiple of 4. */
435 *name_pad = (2 - *namelength) & 3;
438 * Note: entry_bytes_remaining is at least 64 bits and
439 * therefore guaranteed to be big enough for a 33-bit file
440 * size.
442 cpio->entry_bytes_remaining =
443 atol16(header->c_filesize, sizeof(header->c_filesize));
444 archive_entry_set_size(entry, cpio->entry_bytes_remaining);
445 /* Pad file contents to a multiple of 4. */
446 cpio->entry_padding = 3 & -cpio->entry_bytes_remaining;
447 return (r);
451 * Skip forward to the next cpio odc header by searching for the
452 * 070707 string. This is a hand-optimized search that could
453 * probably be easily generalized to handle all character-based
454 * cpio variants.
456 static int
457 is_octal(const char *p, size_t len)
459 while (len-- > 0) {
460 if (*p < '0' || *p > '7')
461 return (0);
462 ++p;
464 return (1);
467 static int
468 find_odc_header(struct archive_read *a)
470 const void *h;
471 const char *p, *q;
472 size_t skip, bytes, skipped = 0;
474 for (;;) {
475 bytes = (a->decompressor->read_ahead)(a, &h, 512);
476 if (bytes < sizeof(struct cpio_odc_header))
477 return (ARCHIVE_FATAL);
478 p = h;
479 q = p + bytes;
481 /* Try the typical case first, then go into the slow search.*/
482 if (memcmp("070707", p, 6) == 0
483 && is_octal(p, sizeof(struct cpio_odc_header)))
484 return (ARCHIVE_OK);
487 * Scan ahead until we find something that looks
488 * like an odc header.
490 while (p + sizeof(struct cpio_odc_header) < q) {
491 switch (p[5]) {
492 case '7':
493 if (memcmp("070707", p, 6) == 0
494 && is_octal(p, sizeof(struct cpio_odc_header))) {
495 skip = p - (const char *)h;
496 (a->decompressor->consume)(a, skip);
497 skipped += skip;
498 if (skipped > 0) {
499 archive_set_error(&a->archive,
501 "Skipped %d bytes before "
502 "finding valid header",
503 (int)skipped);
504 return (ARCHIVE_WARN);
506 return (ARCHIVE_OK);
508 p += 2;
509 break;
510 case '0':
511 p++;
512 break;
513 default:
514 p += 6;
515 break;
518 skip = p - (const char *)h;
519 (a->decompressor->consume)(a, skip);
520 skipped += skip;
524 static int
525 header_odc(struct archive_read *a, struct cpio *cpio,
526 struct archive_entry *entry, size_t *namelength, size_t *name_pad)
528 const void *h;
529 int r;
530 const struct cpio_odc_header *header;
531 size_t bytes;
533 a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
534 a->archive.archive_format_name = "POSIX octet-oriented cpio";
536 /* Find the start of the next header. */
537 r = find_odc_header(a);
538 if (r < ARCHIVE_WARN)
539 return (r);
541 /* Read fixed-size portion of header. */
542 bytes = (a->decompressor->read_ahead)(a, &h, sizeof(struct cpio_odc_header));
543 if (bytes < sizeof(struct cpio_odc_header))
544 return (ARCHIVE_FATAL);
545 (a->decompressor->consume)(a, sizeof(struct cpio_odc_header));
547 /* Parse out octal fields. */
548 header = (const struct cpio_odc_header *)h;
550 archive_entry_set_dev(entry, atol8(header->c_dev, sizeof(header->c_dev)));
551 archive_entry_set_ino(entry, atol8(header->c_ino, sizeof(header->c_ino)));
552 archive_entry_set_mode(entry, atol8(header->c_mode, sizeof(header->c_mode)));
553 archive_entry_set_uid(entry, atol8(header->c_uid, sizeof(header->c_uid)));
554 archive_entry_set_gid(entry, atol8(header->c_gid, sizeof(header->c_gid)));
555 archive_entry_set_nlink(entry, atol8(header->c_nlink, sizeof(header->c_nlink)));
556 archive_entry_set_rdev(entry, atol8(header->c_rdev, sizeof(header->c_rdev)));
557 archive_entry_set_mtime(entry, atol8(header->c_mtime, sizeof(header->c_mtime)), 0);
558 *namelength = atol8(header->c_namesize, sizeof(header->c_namesize));
559 *name_pad = 0; /* No padding of filename. */
562 * Note: entry_bytes_remaining is at least 64 bits and
563 * therefore guaranteed to be big enough for a 33-bit file
564 * size.
566 cpio->entry_bytes_remaining =
567 atol8(header->c_filesize, sizeof(header->c_filesize));
568 archive_entry_set_size(entry, cpio->entry_bytes_remaining);
569 cpio->entry_padding = 0;
570 return (r);
573 static int
574 header_bin_le(struct archive_read *a, struct cpio *cpio,
575 struct archive_entry *entry, size_t *namelength, size_t *name_pad)
577 const void *h;
578 const struct cpio_bin_header *header;
579 size_t bytes;
581 a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_LE;
582 a->archive.archive_format_name = "cpio (little-endian binary)";
584 /* Read fixed-size portion of header. */
585 bytes = (a->decompressor->read_ahead)(a, &h, sizeof(struct cpio_bin_header));
586 if (bytes < sizeof(struct cpio_bin_header))
587 return (ARCHIVE_FATAL);
588 (a->decompressor->consume)(a, sizeof(struct cpio_bin_header));
590 /* Parse out binary fields. */
591 header = (const struct cpio_bin_header *)h;
593 archive_entry_set_dev(entry, header->c_dev[0] + header->c_dev[1] * 256);
594 archive_entry_set_ino(entry, header->c_ino[0] + header->c_ino[1] * 256);
595 archive_entry_set_mode(entry, header->c_mode[0] + header->c_mode[1] * 256);
596 archive_entry_set_uid(entry, header->c_uid[0] + header->c_uid[1] * 256);
597 archive_entry_set_gid(entry, header->c_gid[0] + header->c_gid[1] * 256);
598 archive_entry_set_nlink(entry, header->c_nlink[0] + header->c_nlink[1] * 256);
599 archive_entry_set_rdev(entry, header->c_rdev[0] + header->c_rdev[1] * 256);
600 archive_entry_set_mtime(entry, le4(header->c_mtime), 0);
601 *namelength = header->c_namesize[0] + header->c_namesize[1] * 256;
602 *name_pad = *namelength & 1; /* Pad to even. */
604 cpio->entry_bytes_remaining = le4(header->c_filesize);
605 archive_entry_set_size(entry, cpio->entry_bytes_remaining);
606 cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
607 return (ARCHIVE_OK);
610 static int
611 header_bin_be(struct archive_read *a, struct cpio *cpio,
612 struct archive_entry *entry, size_t *namelength, size_t *name_pad)
614 const void *h;
615 const struct cpio_bin_header *header;
616 size_t bytes;
618 a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_BE;
619 a->archive.archive_format_name = "cpio (big-endian binary)";
621 /* Read fixed-size portion of header. */
622 bytes = (a->decompressor->read_ahead)(a, &h,
623 sizeof(struct cpio_bin_header));
624 if (bytes < sizeof(struct cpio_bin_header))
625 return (ARCHIVE_FATAL);
626 (a->decompressor->consume)(a, sizeof(struct cpio_bin_header));
628 /* Parse out binary fields. */
629 header = (const struct cpio_bin_header *)h;
630 archive_entry_set_dev(entry, header->c_dev[0] * 256 + header->c_dev[1]);
631 archive_entry_set_ino(entry, header->c_ino[0] * 256 + header->c_ino[1]);
632 archive_entry_set_mode(entry, header->c_mode[0] * 256 + header->c_mode[1]);
633 archive_entry_set_uid(entry, header->c_uid[0] * 256 + header->c_uid[1]);
634 archive_entry_set_gid(entry, header->c_gid[0] * 256 + header->c_gid[1]);
635 archive_entry_set_nlink(entry, header->c_nlink[0] * 256 + header->c_nlink[1]);
636 archive_entry_set_rdev(entry, header->c_rdev[0] * 256 + header->c_rdev[1]);
637 archive_entry_set_mtime(entry, be4(header->c_mtime), 0);
638 *namelength = header->c_namesize[0] * 256 + header->c_namesize[1];
639 *name_pad = *namelength & 1; /* Pad to even. */
641 cpio->entry_bytes_remaining = be4(header->c_filesize);
642 archive_entry_set_size(entry, cpio->entry_bytes_remaining);
643 cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
644 return (ARCHIVE_OK);
647 static int
648 archive_read_format_cpio_cleanup(struct archive_read *a)
650 struct cpio *cpio;
652 cpio = (struct cpio *)(a->format->data);
653 /* Free inode->name map */
654 while (cpio->links_head != NULL) {
655 struct links_entry *lp = cpio->links_head->next;
657 if (cpio->links_head->name)
658 free(cpio->links_head->name);
659 free(cpio->links_head);
660 cpio->links_head = lp;
662 archive_string_free(&cpio->entry_name);
663 free(cpio);
664 (a->format->data) = NULL;
665 return (ARCHIVE_OK);
668 static int
669 le4(const unsigned char *p)
671 return ((p[0]<<16) + (p[1]<<24) + (p[2]<<0) + (p[3]<<8));
675 static int
676 be4(const unsigned char *p)
678 return (p[0] + (p[1]<<8) + (p[2]<<16) + (p[3]<<24));
682 * Note that this implementation does not (and should not!) obey
683 * locale settings; you cannot simply substitute strtol here, since
684 * it does obey locale.
686 static int64_t
687 atol8(const char *p, unsigned char_cnt)
689 int64_t l;
690 int digit;
692 l = 0;
693 while (char_cnt-- > 0) {
694 if (*p >= '0' && *p <= '7')
695 digit = *p - '0';
696 else
697 return (l);
698 p++;
699 l <<= 3;
700 l |= digit;
702 return (l);
705 static int64_t
706 atol16(const char *p, unsigned char_cnt)
708 int64_t l;
709 int digit;
711 l = 0;
712 while (char_cnt-- > 0) {
713 if (*p >= 'a' && *p <= 'f')
714 digit = *p - 'a' + 10;
715 else if (*p >= 'A' && *p <= 'F')
716 digit = *p - 'A' + 10;
717 else if (*p >= '0' && *p <= '9')
718 digit = *p - '0';
719 else
720 return (l);
721 p++;
722 l <<= 4;
723 l |= digit;
725 return (l);
728 static void
729 record_hardlink(struct cpio *cpio, struct archive_entry *entry)
731 struct links_entry *le;
732 dev_t dev;
733 ino_t ino;
735 dev = archive_entry_dev(entry);
736 ino = archive_entry_ino(entry);
739 * First look in the list of multiply-linked files. If we've
740 * already dumped it, convert this entry to a hard link entry.
742 for (le = cpio->links_head; le; le = le->next) {
743 if (le->dev == dev && le->ino == ino) {
744 archive_entry_copy_hardlink(entry, le->name);
746 if (--le->links <= 0) {
747 if (le->previous != NULL)
748 le->previous->next = le->next;
749 if (le->next != NULL)
750 le->next->previous = le->previous;
751 if (cpio->links_head == le)
752 cpio->links_head = le->next;
753 free(le->name);
754 free(le);
757 return;
761 le = (struct links_entry *)malloc(sizeof(struct links_entry));
762 if (le == NULL)
763 __archive_errx(1, "Out of memory adding file to list");
764 if (cpio->links_head != NULL)
765 cpio->links_head->previous = le;
766 le->next = cpio->links_head;
767 le->previous = NULL;
768 cpio->links_head = le;
769 le->dev = dev;
770 le->ino = ino;
771 le->links = archive_entry_nlink(entry) - 1;
772 le->name = strdup(archive_entry_pathname(entry));
773 if (le->name == NULL)
774 __archive_errx(1, "Out of memory adding file to list");