56247dd1c6d47eaeefc0cbdfcee20b04a72bd58d
[dragonfly.git] / contrib / libarchive / libarchive / archive_read_support_format_cpio.c
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
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.
13  *
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.
24  */
25
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: src/lib/libarchive/archive_read_support_format_cpio.c,v 1.27 2008/12/06 06:45:15 kientzle Exp $");
28
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
39
40 #include "archive.h"
41 #include "archive_entry.h"
42 #include "archive_private.h"
43 #include "archive_read_private.h"
44
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];
57 };
58
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];
71 };
72
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];
88 };
89
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;
97 };
98
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;
110 };
111
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);
135
136 int
137 archive_read_support_format_cpio(struct archive *_a)
138 {
139         struct archive_read *a = (struct archive_read *)_a;
140         struct cpio *cpio;
141         int r;
142
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);
147         }
148         memset(cpio, 0, sizeof(*cpio));
149         cpio->magic = CPIO_MAGIC;
150
151         r = __archive_read_register_format(a,
152             cpio,
153             "cpio",
154             archive_read_format_cpio_bid,
155             NULL,
156             archive_read_format_cpio_read_header,
157             archive_read_format_cpio_read_data,
158             NULL,
159             archive_read_format_cpio_cleanup);
160
161         if (r != ARCHIVE_OK)
162                 free(cpio);
163         return (ARCHIVE_OK);
164 }
165
166
167 static int
168 archive_read_format_cpio_bid(struct archive_read *a)
169 {
170         const void *h;
171         const unsigned char *p;
172         struct cpio *cpio;
173         int bid;
174
175         cpio = (struct cpio *)(a->format->data);
176
177         if ((h = __archive_read_ahead(a, 6, NULL)) == NULL)
178                 return (-1);
179
180         p = (const unsigned char *)h;
181         bid = 0;
182         if (memcmp(p, "070707", 6) == 0) {
183                 /* ASCII cpio archive (odc, POSIX.1) */
184                 cpio->read_header = header_odc;
185                 bid += 48;
186                 /*
187                  * XXX TODO:  More verification; Could check that only octal
188                  * digits appear in appropriate header locations. XXX
189                  */
190         } else if (memcmp(p, "070701", 6) == 0) {
191                 /* ASCII cpio archive (SVR4 without CRC) */
192                 cpio->read_header = header_newc;
193                 bid += 48;
194                 /*
195                  * XXX TODO:  More verification; Could check that only hex
196                  * digits appear in appropriate header locations. XXX
197                  */
198         } else if (memcmp(p, "070702", 6) == 0) {
199                 /* ASCII cpio archive (SVR4 with CRC) */
200                 /* XXX TODO: Flag that we should check the CRC. XXX */
201                 cpio->read_header = header_newc;
202                 bid += 48;
203                 /*
204                  * XXX TODO:  More verification; Could check that only hex
205                  * digits appear in appropriate header locations. XXX
206                  */
207         } else if (p[0] * 256 + p[1] == 070707) {
208                 /* big-endian binary cpio archives */
209                 cpio->read_header = header_bin_be;
210                 bid += 16;
211                 /* Is more verification possible here? */
212         } else if (p[0] + p[1] * 256 == 070707) {
213                 /* little-endian binary cpio archives */
214                 cpio->read_header = header_bin_le;
215                 bid += 16;
216                 /* Is more verification possible here? */
217         } else
218                 return (ARCHIVE_WARN);
219
220         return (bid);
221 }
222
223 static int
224 archive_read_format_cpio_read_header(struct archive_read *a,
225     struct archive_entry *entry)
226 {
227         struct cpio *cpio;
228         const void *h;
229         size_t namelength;
230         size_t name_pad;
231         int r;
232
233         cpio = (struct cpio *)(a->format->data);
234         r = (cpio->read_header(a, cpio, entry, &namelength, &name_pad));
235
236         if (r < ARCHIVE_WARN)
237                 return (r);
238
239         /* Read name from buffer. */
240         h = __archive_read_ahead(a, namelength + name_pad, NULL);
241         if (h == NULL)
242             return (ARCHIVE_FATAL);
243         __archive_read_consume(a, namelength + name_pad);
244         archive_strncpy(&cpio->entry_name, (const char *)h, namelength);
245         archive_entry_set_pathname(entry, cpio->entry_name.s);
246         cpio->entry_offset = 0;
247
248         /* If this is a symlink, read the link contents. */
249         if (archive_entry_filetype(entry) == AE_IFLNK) {
250                 h = __archive_read_ahead(a, cpio->entry_bytes_remaining, NULL);
251                 if (h == NULL)
252                         return (ARCHIVE_FATAL);
253                 __archive_read_consume(a, cpio->entry_bytes_remaining);
254                 archive_strncpy(&cpio->entry_linkname, (const char *)h,
255                     cpio->entry_bytes_remaining);
256                 archive_entry_set_symlink(entry, cpio->entry_linkname.s);
257                 cpio->entry_bytes_remaining = 0;
258         }
259
260         /* Compare name to "TRAILER!!!" to test for end-of-archive. */
261         if (namelength == 11 && strcmp((const char *)h, "TRAILER!!!") == 0) {
262             /* TODO: Store file location of start of block. */
263             archive_set_error(&a->archive, 0, NULL);
264             return (ARCHIVE_EOF);
265         }
266
267         /* Detect and record hardlinks to previously-extracted entries. */
268         record_hardlink(cpio, entry);
269
270         return (r);
271 }
272
273 static int
274 archive_read_format_cpio_read_data(struct archive_read *a,
275     const void **buff, size_t *size, off_t *offset)
276 {
277         ssize_t bytes_read;
278         struct cpio *cpio;
279
280         cpio = (struct cpio *)(a->format->data);
281         if (cpio->entry_bytes_remaining > 0) {
282                 *buff = __archive_read_ahead(a, 1, &bytes_read);
283                 if (bytes_read <= 0)
284                         return (ARCHIVE_FATAL);
285                 if (bytes_read > cpio->entry_bytes_remaining)
286                         bytes_read = cpio->entry_bytes_remaining;
287                 *size = bytes_read;
288                 *offset = cpio->entry_offset;
289                 cpio->entry_offset += bytes_read;
290                 cpio->entry_bytes_remaining -= bytes_read;
291                 __archive_read_consume(a, bytes_read);
292                 return (ARCHIVE_OK);
293         } else {
294                 while (cpio->entry_padding > 0) {
295                         *buff = __archive_read_ahead(a, 1, &bytes_read);
296                         if (bytes_read <= 0)
297                                 return (ARCHIVE_FATAL);
298                         if (bytes_read > cpio->entry_padding)
299                                 bytes_read = cpio->entry_padding;
300                         __archive_read_consume(a, bytes_read);
301                         cpio->entry_padding -= bytes_read;
302                 }
303                 *buff = NULL;
304                 *size = 0;
305                 *offset = cpio->entry_offset;
306                 return (ARCHIVE_EOF);
307         }
308 }
309
310 /*
311  * Skip forward to the next cpio newc header by searching for the
312  * 07070[12] string.  This should be generalized and merged with
313  * find_odc_header below.
314  */
315 static int
316 is_hex(const char *p, size_t len)
317 {
318         while (len-- > 0) {
319                 if ((*p >= '0' && *p <= '9')
320                     || (*p >= 'a' && *p <= 'f')
321                     || (*p >= 'A' && *p <= 'F'))
322                         ++p;
323                 else
324                         return (0);
325         }
326         return (1);
327 }
328
329 static int
330 find_newc_header(struct archive_read *a)
331 {
332         const void *h;
333         const char *p, *q;
334         size_t skip, skipped = 0;
335         ssize_t bytes;
336
337         for (;;) {
338                 h = __archive_read_ahead(a, sizeof(struct cpio_newc_header), &bytes);
339                 if (h == NULL)
340                         return (ARCHIVE_FATAL);
341                 p = h;
342                 q = p + bytes;
343
344                 /* Try the typical case first, then go into the slow search.*/
345                 if (memcmp("07070", p, 5) == 0
346                     && (p[5] == '1' || p[5] == '2')
347                     && is_hex(p, sizeof(struct cpio_newc_header)))
348                         return (ARCHIVE_OK);
349
350                 /*
351                  * Scan ahead until we find something that looks
352                  * like an odc header.
353                  */
354                 while (p + sizeof(struct cpio_newc_header) < q) {
355                         switch (p[5]) {
356                         case '1':
357                         case '2':
358                                 if (memcmp("07070", p, 5) == 0
359                                         && is_hex(p, sizeof(struct cpio_newc_header))) {
360                                         skip = p - (const char *)h;
361                                         __archive_read_consume(a, skip);
362                                         skipped += skip;
363                                         if (skipped > 0) {
364                                                 archive_set_error(&a->archive,
365                                                     0,
366                                                     "Skipped %d bytes before "
367                                                     "finding valid header",
368                                                     (int)skipped);
369                                                 return (ARCHIVE_WARN);
370                                         }
371                                         return (ARCHIVE_OK);
372                                 }
373                                 p += 2;
374                                 break;
375                         case '0':
376                                 p++;
377                                 break;
378                         default:
379                                 p += 6;
380                                 break;
381                         }
382                 }
383                 skip = p - (const char *)h;
384                 __archive_read_consume(a, skip);
385                 skipped += skip;
386         }
387 }
388
389 static int
390 header_newc(struct archive_read *a, struct cpio *cpio,
391     struct archive_entry *entry, size_t *namelength, size_t *name_pad)
392 {
393         const void *h;
394         const struct cpio_newc_header *header;
395         int r;
396
397         r = find_newc_header(a);
398         if (r < ARCHIVE_WARN)
399                 return (r);
400
401         /* Read fixed-size portion of header. */
402         h = __archive_read_ahead(a, sizeof(struct cpio_newc_header), NULL);
403         if (h == NULL)
404             return (ARCHIVE_FATAL);
405         __archive_read_consume(a, sizeof(struct cpio_newc_header));
406
407         /* Parse out hex fields. */
408         header = (const struct cpio_newc_header *)h;
409
410         if (memcmp(header->c_magic, "070701", 6) == 0) {
411                 a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_NOCRC;
412                 a->archive.archive_format_name = "ASCII cpio (SVR4 with no CRC)";
413         } else if (memcmp(header->c_magic, "070702", 6) == 0) {
414                 a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_CRC;
415                 a->archive.archive_format_name = "ASCII cpio (SVR4 with CRC)";
416         } else {
417                 /* TODO: Abort here? */
418         }
419
420         archive_entry_set_devmajor(entry, atol16(header->c_devmajor, sizeof(header->c_devmajor)));
421         archive_entry_set_devminor(entry, atol16(header->c_devminor, sizeof(header->c_devminor)));
422         archive_entry_set_ino(entry, atol16(header->c_ino, sizeof(header->c_ino)));
423         archive_entry_set_mode(entry, atol16(header->c_mode, sizeof(header->c_mode)));
424         archive_entry_set_uid(entry, atol16(header->c_uid, sizeof(header->c_uid)));
425         archive_entry_set_gid(entry, atol16(header->c_gid, sizeof(header->c_gid)));
426         archive_entry_set_nlink(entry, atol16(header->c_nlink, sizeof(header->c_nlink)));
427         archive_entry_set_rdevmajor(entry, atol16(header->c_rdevmajor, sizeof(header->c_rdevmajor)));
428         archive_entry_set_rdevminor(entry, atol16(header->c_rdevminor, sizeof(header->c_rdevminor)));
429         archive_entry_set_mtime(entry, atol16(header->c_mtime, sizeof(header->c_mtime)), 0);
430         *namelength = atol16(header->c_namesize, sizeof(header->c_namesize));
431         /* Pad name to 2 more than a multiple of 4. */
432         *name_pad = (2 - *namelength) & 3;
433
434         /*
435          * Note: entry_bytes_remaining is at least 64 bits and
436          * therefore guaranteed to be big enough for a 33-bit file
437          * size.
438          */
439         cpio->entry_bytes_remaining =
440             atol16(header->c_filesize, sizeof(header->c_filesize));
441         archive_entry_set_size(entry, cpio->entry_bytes_remaining);
442         /* Pad file contents to a multiple of 4. */
443         cpio->entry_padding = 3 & -cpio->entry_bytes_remaining;
444         return (r);
445 }
446
447 /*
448  * Skip forward to the next cpio odc header by searching for the
449  * 070707 string.  This is a hand-optimized search that could
450  * probably be easily generalized to handle all character-based
451  * cpio variants.
452  */
453 static int
454 is_octal(const char *p, size_t len)
455 {
456         while (len-- > 0) {
457                 if (*p < '0' || *p > '7')
458                         return (0);
459                 ++p;
460         }
461         return (1);
462 }
463
464 static int
465 find_odc_header(struct archive_read *a)
466 {
467         const void *h;
468         const char *p, *q;
469         size_t skip, skipped = 0;
470         ssize_t bytes;
471
472         for (;;) {
473                 h = __archive_read_ahead(a, sizeof(struct cpio_odc_header), &bytes);
474                 if (h == NULL)
475                         return (ARCHIVE_FATAL);
476                 p = h;
477                 q = p + bytes;
478
479                 /* Try the typical case first, then go into the slow search.*/
480                 if (memcmp("070707", p, 6) == 0
481                     && is_octal(p, sizeof(struct cpio_odc_header)))
482                         return (ARCHIVE_OK);
483
484                 /*
485                  * Scan ahead until we find something that looks
486                  * like an odc header.
487                  */
488                 while (p + sizeof(struct cpio_odc_header) < q) {
489                         switch (p[5]) {
490                         case '7':
491                                 if (memcmp("070707", p, 6) == 0
492                                         && is_octal(p, sizeof(struct cpio_odc_header))) {
493                                         skip = p - (const char *)h;
494                                         __archive_read_consume(a, skip);
495                                         skipped += skip;
496                                         if (skipped > 0) {
497                                                 archive_set_error(&a->archive,
498                                                     0,
499                                                     "Skipped %d bytes before "
500                                                     "finding valid header",
501                                                     (int)skipped);
502                                                 return (ARCHIVE_WARN);
503                                         }
504                                         return (ARCHIVE_OK);
505                                 }
506                                 p += 2;
507                                 break;
508                         case '0':
509                                 p++;
510                                 break;
511                         default:
512                                 p += 6;
513                                 break;
514                         }
515                 }
516                 skip = p - (const char *)h;
517                 __archive_read_consume(a, skip);
518                 skipped += skip;
519         }
520 }
521
522 static int
523 header_odc(struct archive_read *a, struct cpio *cpio,
524     struct archive_entry *entry, size_t *namelength, size_t *name_pad)
525 {
526         const void *h;
527         int r;
528         const struct cpio_odc_header *header;
529
530         a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
531         a->archive.archive_format_name = "POSIX octet-oriented cpio";
532
533         /* Find the start of the next header. */
534         r = find_odc_header(a);
535         if (r < ARCHIVE_WARN)
536                 return (r);
537
538         /* Read fixed-size portion of header. */
539         h = __archive_read_ahead(a, sizeof(struct cpio_odc_header), NULL);
540         if (h == NULL)
541             return (ARCHIVE_FATAL);
542         __archive_read_consume(a, sizeof(struct cpio_odc_header));
543
544         /* Parse out octal fields. */
545         header = (const struct cpio_odc_header *)h;
546
547         archive_entry_set_dev(entry, atol8(header->c_dev, sizeof(header->c_dev)));
548         archive_entry_set_ino(entry, atol8(header->c_ino, sizeof(header->c_ino)));
549         archive_entry_set_mode(entry, atol8(header->c_mode, sizeof(header->c_mode)));
550         archive_entry_set_uid(entry, atol8(header->c_uid, sizeof(header->c_uid)));
551         archive_entry_set_gid(entry, atol8(header->c_gid, sizeof(header->c_gid)));
552         archive_entry_set_nlink(entry, atol8(header->c_nlink, sizeof(header->c_nlink)));
553         archive_entry_set_rdev(entry, atol8(header->c_rdev, sizeof(header->c_rdev)));
554         archive_entry_set_mtime(entry, atol8(header->c_mtime, sizeof(header->c_mtime)), 0);
555         *namelength = atol8(header->c_namesize, sizeof(header->c_namesize));
556         *name_pad = 0; /* No padding of filename. */
557
558         /*
559          * Note: entry_bytes_remaining is at least 64 bits and
560          * therefore guaranteed to be big enough for a 33-bit file
561          * size.
562          */
563         cpio->entry_bytes_remaining =
564             atol8(header->c_filesize, sizeof(header->c_filesize));
565         archive_entry_set_size(entry, cpio->entry_bytes_remaining);
566         cpio->entry_padding = 0;
567         return (r);
568 }
569
570 static int
571 header_bin_le(struct archive_read *a, struct cpio *cpio,
572     struct archive_entry *entry, size_t *namelength, size_t *name_pad)
573 {
574         const void *h;
575         const struct cpio_bin_header *header;
576
577         a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_LE;
578         a->archive.archive_format_name = "cpio (little-endian binary)";
579
580         /* Read fixed-size portion of header. */
581         h = __archive_read_ahead(a, sizeof(struct cpio_bin_header), NULL);
582         if (h == NULL)
583             return (ARCHIVE_FATAL);
584         __archive_read_consume(a, sizeof(struct cpio_bin_header));
585
586         /* Parse out binary fields. */
587         header = (const struct cpio_bin_header *)h;
588
589         archive_entry_set_dev(entry, header->c_dev[0] + header->c_dev[1] * 256);
590         archive_entry_set_ino(entry, header->c_ino[0] + header->c_ino[1] * 256);
591         archive_entry_set_mode(entry, header->c_mode[0] + header->c_mode[1] * 256);
592         archive_entry_set_uid(entry, header->c_uid[0] + header->c_uid[1] * 256);
593         archive_entry_set_gid(entry, header->c_gid[0] + header->c_gid[1] * 256);
594         archive_entry_set_nlink(entry, header->c_nlink[0] + header->c_nlink[1] * 256);
595         archive_entry_set_rdev(entry, header->c_rdev[0] + header->c_rdev[1] * 256);
596         archive_entry_set_mtime(entry, le4(header->c_mtime), 0);
597         *namelength = header->c_namesize[0] + header->c_namesize[1] * 256;
598         *name_pad = *namelength & 1; /* Pad to even. */
599
600         cpio->entry_bytes_remaining = le4(header->c_filesize);
601         archive_entry_set_size(entry, cpio->entry_bytes_remaining);
602         cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
603         return (ARCHIVE_OK);
604 }
605
606 static int
607 header_bin_be(struct archive_read *a, struct cpio *cpio,
608     struct archive_entry *entry, size_t *namelength, size_t *name_pad)
609 {
610         const void *h;
611         const struct cpio_bin_header *header;
612
613         a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_BE;
614         a->archive.archive_format_name = "cpio (big-endian binary)";
615
616         /* Read fixed-size portion of header. */
617         h = __archive_read_ahead(a, sizeof(struct cpio_bin_header), NULL);
618         if (h == NULL)
619             return (ARCHIVE_FATAL);
620         __archive_read_consume(a, sizeof(struct cpio_bin_header));
621
622         /* Parse out binary fields. */
623         header = (const struct cpio_bin_header *)h;
624         archive_entry_set_dev(entry, header->c_dev[0] * 256 + header->c_dev[1]);
625         archive_entry_set_ino(entry, header->c_ino[0] * 256 + header->c_ino[1]);
626         archive_entry_set_mode(entry, header->c_mode[0] * 256 + header->c_mode[1]);
627         archive_entry_set_uid(entry, header->c_uid[0] * 256 + header->c_uid[1]);
628         archive_entry_set_gid(entry, header->c_gid[0] * 256 + header->c_gid[1]);
629         archive_entry_set_nlink(entry, header->c_nlink[0] * 256 + header->c_nlink[1]);
630         archive_entry_set_rdev(entry, header->c_rdev[0] * 256 + header->c_rdev[1]);
631         archive_entry_set_mtime(entry, be4(header->c_mtime), 0);
632         *namelength = header->c_namesize[0] * 256 + header->c_namesize[1];
633         *name_pad = *namelength & 1; /* Pad to even. */
634
635         cpio->entry_bytes_remaining = be4(header->c_filesize);
636         archive_entry_set_size(entry, cpio->entry_bytes_remaining);
637         cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
638         return (ARCHIVE_OK);
639 }
640
641 static int
642 archive_read_format_cpio_cleanup(struct archive_read *a)
643 {
644         struct cpio *cpio;
645
646         cpio = (struct cpio *)(a->format->data);
647         /* Free inode->name map */
648         while (cpio->links_head != NULL) {
649                 struct links_entry *lp = cpio->links_head->next;
650
651                 if (cpio->links_head->name)
652                         free(cpio->links_head->name);
653                 free(cpio->links_head);
654                 cpio->links_head = lp;
655         }
656         archive_string_free(&cpio->entry_name);
657         free(cpio);
658         (a->format->data) = NULL;
659         return (ARCHIVE_OK);
660 }
661
662 static int
663 le4(const unsigned char *p)
664 {
665         return ((p[0]<<16) + (p[1]<<24) + (p[2]<<0) + (p[3]<<8));
666 }
667
668
669 static int
670 be4(const unsigned char *p)
671 {
672         return (p[0] + (p[1]<<8) + (p[2]<<16) + (p[3]<<24));
673 }
674
675 /*
676  * Note that this implementation does not (and should not!) obey
677  * locale settings; you cannot simply substitute strtol here, since
678  * it does obey locale.
679  */
680 static int64_t
681 atol8(const char *p, unsigned char_cnt)
682 {
683         int64_t l;
684         int digit;
685
686         l = 0;
687         while (char_cnt-- > 0) {
688                 if (*p >= '0' && *p <= '7')
689                         digit = *p - '0';
690                 else
691                         return (l);
692                 p++;
693                 l <<= 3;
694                 l |= digit;
695         }
696         return (l);
697 }
698
699 static int64_t
700 atol16(const char *p, unsigned char_cnt)
701 {
702         int64_t l;
703         int digit;
704
705         l = 0;
706         while (char_cnt-- > 0) {
707                 if (*p >= 'a' && *p <= 'f')
708                         digit = *p - 'a' + 10;
709                 else if (*p >= 'A' && *p <= 'F')
710                         digit = *p - 'A' + 10;
711                 else if (*p >= '0' && *p <= '9')
712                         digit = *p - '0';
713                 else
714                         return (l);
715                 p++;
716                 l <<= 4;
717                 l |= digit;
718         }
719         return (l);
720 }
721
722 static void
723 record_hardlink(struct cpio *cpio, struct archive_entry *entry)
724 {
725         struct links_entry      *le;
726         dev_t dev;
727         ino_t ino;
728
729         dev = archive_entry_dev(entry);
730         ino = archive_entry_ino(entry);
731
732         /*
733          * First look in the list of multiply-linked files.  If we've
734          * already dumped it, convert this entry to a hard link entry.
735          */
736         for (le = cpio->links_head; le; le = le->next) {
737                 if (le->dev == dev && le->ino == ino) {
738                         archive_entry_copy_hardlink(entry, le->name);
739
740                         if (--le->links <= 0) {
741                                 if (le->previous != NULL)
742                                         le->previous->next = le->next;
743                                 if (le->next != NULL)
744                                         le->next->previous = le->previous;
745                                 if (cpio->links_head == le)
746                                         cpio->links_head = le->next;
747                                 free(le->name);
748                                 free(le);
749                         }
750
751                         return;
752                 }
753         }
754
755         le = (struct links_entry *)malloc(sizeof(struct links_entry));
756         if (le == NULL)
757                 __archive_errx(1, "Out of memory adding file to list");
758         if (cpio->links_head != NULL)
759                 cpio->links_head->previous = le;
760         le->next = cpio->links_head;
761         le->previous = NULL;
762         cpio->links_head = le;
763         le->dev = dev;
764         le->ino = ino;
765         le->links = archive_entry_nlink(entry) - 1;
766         le->name = strdup(archive_entry_pathname(entry));
767         if (le->name == NULL)
768                 __archive_errx(1, "Out of memory adding file to list");
769 }