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