]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/libarchive/libarchive/archive_read_support_format_cpio.c
MFhead_mfi r227068
[FreeBSD/FreeBSD.git] / contrib / libarchive / libarchive / archive_read_support_format_cpio.c
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * Copyright (c) 2010-2012 Michihiro NAKAJIMA
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
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.
14  *
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.
25  */
26
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD$");
29
30 #ifdef HAVE_ERRNO_H
31 #include <errno.h>
32 #endif
33 /* #include <stdint.h> */ /* See archive_platform.h */
34 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif
37 #ifdef HAVE_STRING_H
38 #include <string.h>
39 #endif
40
41 #include "archive.h"
42 #include "archive_entry.h"
43 #include "archive_entry_locale.h"
44 #include "archive_private.h"
45 #include "archive_read_private.h"
46
47 #define bin_magic_offset 0
48 #define bin_magic_size 2
49 #define bin_dev_offset 2
50 #define bin_dev_size 2
51 #define bin_ino_offset 4
52 #define bin_ino_size 2
53 #define bin_mode_offset 6
54 #define bin_mode_size 2
55 #define bin_uid_offset 8
56 #define bin_uid_size 2
57 #define bin_gid_offset 10
58 #define bin_gid_size 2
59 #define bin_nlink_offset 12
60 #define bin_nlink_size 2
61 #define bin_rdev_offset 14
62 #define bin_rdev_size 2
63 #define bin_mtime_offset 16
64 #define bin_mtime_size 4
65 #define bin_namesize_offset 20
66 #define bin_namesize_size 2
67 #define bin_filesize_offset 22
68 #define bin_filesize_size 4
69 #define bin_header_size 26
70
71 #define odc_magic_offset 0
72 #define odc_magic_size 6
73 #define odc_dev_offset 6
74 #define odc_dev_size 6
75 #define odc_ino_offset 12
76 #define odc_ino_size 6
77 #define odc_mode_offset 18
78 #define odc_mode_size 6
79 #define odc_uid_offset 24
80 #define odc_uid_size 6
81 #define odc_gid_offset 30
82 #define odc_gid_size 6
83 #define odc_nlink_offset 36
84 #define odc_nlink_size 6
85 #define odc_rdev_offset 42
86 #define odc_rdev_size 6
87 #define odc_mtime_offset 48
88 #define odc_mtime_size 11
89 #define odc_namesize_offset 59
90 #define odc_namesize_size 6
91 #define odc_filesize_offset 65
92 #define odc_filesize_size 11
93 #define odc_header_size 76
94
95 #define newc_magic_offset 0
96 #define newc_magic_size 6
97 #define newc_ino_offset 6
98 #define newc_ino_size 8
99 #define newc_mode_offset 14
100 #define newc_mode_size 8
101 #define newc_uid_offset 22
102 #define newc_uid_size 8
103 #define newc_gid_offset 30
104 #define newc_gid_size 8
105 #define newc_nlink_offset 38
106 #define newc_nlink_size 8
107 #define newc_mtime_offset 46
108 #define newc_mtime_size 8
109 #define newc_filesize_offset 54
110 #define newc_filesize_size 8
111 #define newc_devmajor_offset 62
112 #define newc_devmajor_size 8
113 #define newc_devminor_offset 70
114 #define newc_devminor_size 8
115 #define newc_rdevmajor_offset 78
116 #define newc_rdevmajor_size 8
117 #define newc_rdevminor_offset 86
118 #define newc_rdevminor_size 8
119 #define newc_namesize_offset 94
120 #define newc_namesize_size 8
121 #define newc_checksum_offset 102
122 #define newc_checksum_size 8
123 #define newc_header_size 110
124
125 /*
126  * An afio large ASCII header, which they named itself.
127  * afio utility uses this header, if a file size is larger than 2G bytes
128  * or inode/uid/gid is bigger than 65535(0xFFFF) or mtime is bigger than
129  * 0x7fffffff, which we cannot record to odc header because of its limit.
130  * If not, uses odc header.
131  */
132 #define afiol_magic_offset 0
133 #define afiol_magic_size 6
134 #define afiol_dev_offset 6
135 #define afiol_dev_size 8        /* hex */
136 #define afiol_ino_offset 14
137 #define afiol_ino_size 16       /* hex */
138 #define afiol_ino_m_offset 30   /* 'm' */
139 #define afiol_mode_offset 31
140 #define afiol_mode_size 6       /* oct */
141 #define afiol_uid_offset 37
142 #define afiol_uid_size 8        /* hex */
143 #define afiol_gid_offset 45
144 #define afiol_gid_size 8        /* hex */
145 #define afiol_nlink_offset 53
146 #define afiol_nlink_size 8      /* hex */
147 #define afiol_rdev_offset 61
148 #define afiol_rdev_size 8       /* hex */
149 #define afiol_mtime_offset 69
150 #define afiol_mtime_size 16     /* hex */
151 #define afiol_mtime_n_offset 85 /* 'n' */
152 #define afiol_namesize_offset 86
153 #define afiol_namesize_size 4   /* hex */
154 #define afiol_flag_offset 90
155 #define afiol_flag_size 4       /* hex */
156 #define afiol_xsize_offset 94
157 #define afiol_xsize_size 4      /* hex */
158 #define afiol_xsize_s_offset 98 /* 's' */
159 #define afiol_filesize_offset 99
160 #define afiol_filesize_size 16  /* hex */
161 #define afiol_filesize_c_offset 115     /* ':' */
162 #define afiol_header_size 116
163
164
165 struct links_entry {
166         struct links_entry      *next;
167         struct links_entry      *previous;
168         int                      links;
169         dev_t                    dev;
170         int64_t                  ino;
171         char                    *name;
172 };
173
174 #define CPIO_MAGIC   0x13141516
175 struct cpio {
176         int                       magic;
177         int                     (*read_header)(struct archive_read *, struct cpio *,
178                                      struct archive_entry *, size_t *, size_t *);
179         struct links_entry       *links_head;
180         int64_t                   entry_bytes_remaining;
181         int64_t                   entry_bytes_unconsumed;
182         int64_t                   entry_offset;
183         int64_t                   entry_padding;
184
185         struct archive_string_conv *opt_sconv;
186         struct archive_string_conv *sconv_default;
187         int                       init_default_conversion;
188 };
189
190 static int64_t  atol16(const char *, unsigned);
191 static int64_t  atol8(const char *, unsigned);
192 static int      archive_read_format_cpio_bid(struct archive_read *, int);
193 static int      archive_read_format_cpio_options(struct archive_read *,
194                     const char *, const char *);
195 static int      archive_read_format_cpio_cleanup(struct archive_read *);
196 static int      archive_read_format_cpio_read_data(struct archive_read *,
197                     const void **, size_t *, int64_t *);
198 static int      archive_read_format_cpio_read_header(struct archive_read *,
199                     struct archive_entry *);
200 static int      archive_read_format_cpio_skip(struct archive_read *);
201 static int      be4(const unsigned char *);
202 static int      find_odc_header(struct archive_read *);
203 static int      find_newc_header(struct archive_read *);
204 static int      header_bin_be(struct archive_read *, struct cpio *,
205                     struct archive_entry *, size_t *, size_t *);
206 static int      header_bin_le(struct archive_read *, struct cpio *,
207                     struct archive_entry *, size_t *, size_t *);
208 static int      header_newc(struct archive_read *, struct cpio *,
209                     struct archive_entry *, size_t *, size_t *);
210 static int      header_odc(struct archive_read *, struct cpio *,
211                     struct archive_entry *, size_t *, size_t *);
212 static int      header_afiol(struct archive_read *, struct cpio *,
213                     struct archive_entry *, size_t *, size_t *);
214 static int      is_octal(const char *, size_t);
215 static int      is_hex(const char *, size_t);
216 static int      le4(const unsigned char *);
217 static int      record_hardlink(struct archive_read *a,
218                     struct cpio *cpio, struct archive_entry *entry);
219
220 int
221 archive_read_support_format_cpio(struct archive *_a)
222 {
223         struct archive_read *a = (struct archive_read *)_a;
224         struct cpio *cpio;
225         int r;
226
227         archive_check_magic(_a, ARCHIVE_READ_MAGIC,
228             ARCHIVE_STATE_NEW, "archive_read_support_format_cpio");
229
230         cpio = (struct cpio *)calloc(1, sizeof(*cpio));
231         if (cpio == NULL) {
232                 archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
233                 return (ARCHIVE_FATAL);
234         }
235         cpio->magic = CPIO_MAGIC;
236
237         r = __archive_read_register_format(a,
238             cpio,
239             "cpio",
240             archive_read_format_cpio_bid,
241             archive_read_format_cpio_options,
242             archive_read_format_cpio_read_header,
243             archive_read_format_cpio_read_data,
244             archive_read_format_cpio_skip,
245             archive_read_format_cpio_cleanup);
246
247         if (r != ARCHIVE_OK)
248                 free(cpio);
249         return (ARCHIVE_OK);
250 }
251
252
253 static int
254 archive_read_format_cpio_bid(struct archive_read *a, int best_bid)
255 {
256         const unsigned char *p;
257         struct cpio *cpio;
258         int bid;
259
260         (void)best_bid; /* UNUSED */
261
262         cpio = (struct cpio *)(a->format->data);
263
264         if ((p = __archive_read_ahead(a, 6, NULL)) == NULL)
265                 return (-1);
266
267         bid = 0;
268         if (memcmp(p, "070707", 6) == 0) {
269                 /* ASCII cpio archive (odc, POSIX.1) */
270                 cpio->read_header = header_odc;
271                 bid += 48;
272                 /*
273                  * XXX TODO:  More verification; Could check that only octal
274                  * digits appear in appropriate header locations. XXX
275                  */
276         } else if (memcmp(p, "070727", 6) == 0) {
277                 /* afio large ASCII cpio archive */
278                 cpio->read_header = header_odc;
279                 bid += 48;
280                 /*
281                  * XXX TODO:  More verification; Could check that almost hex
282                  * digits appear in appropriate header locations. XXX
283                  */
284         } else if (memcmp(p, "070701", 6) == 0) {
285                 /* ASCII cpio archive (SVR4 without CRC) */
286                 cpio->read_header = header_newc;
287                 bid += 48;
288                 /*
289                  * XXX TODO:  More verification; Could check that only hex
290                  * digits appear in appropriate header locations. XXX
291                  */
292         } else if (memcmp(p, "070702", 6) == 0) {
293                 /* ASCII cpio archive (SVR4 with CRC) */
294                 /* XXX TODO: Flag that we should check the CRC. XXX */
295                 cpio->read_header = header_newc;
296                 bid += 48;
297                 /*
298                  * XXX TODO:  More verification; Could check that only hex
299                  * digits appear in appropriate header locations. XXX
300                  */
301         } else if (p[0] * 256 + p[1] == 070707) {
302                 /* big-endian binary cpio archives */
303                 cpio->read_header = header_bin_be;
304                 bid += 16;
305                 /* Is more verification possible here? */
306         } else if (p[0] + p[1] * 256 == 070707) {
307                 /* little-endian binary cpio archives */
308                 cpio->read_header = header_bin_le;
309                 bid += 16;
310                 /* Is more verification possible here? */
311         } else
312                 return (ARCHIVE_WARN);
313
314         return (bid);
315 }
316
317 static int
318 archive_read_format_cpio_options(struct archive_read *a,
319     const char *key, const char *val)
320 {
321         struct cpio *cpio;
322         int ret = ARCHIVE_FAILED;
323
324         cpio = (struct cpio *)(a->format->data);
325         if (strcmp(key, "compat-2x")  == 0) {
326                 /* Handle filnames as libarchive 2.x */
327                 cpio->init_default_conversion = (val != NULL)?1:0;
328                 return (ARCHIVE_OK);
329         } else if (strcmp(key, "hdrcharset")  == 0) {
330                 if (val == NULL || val[0] == 0)
331                         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
332                             "cpio: hdrcharset option needs a character-set name");
333                 else {
334                         cpio->opt_sconv =
335                             archive_string_conversion_from_charset(
336                                 &a->archive, val, 0);
337                         if (cpio->opt_sconv != NULL)
338                                 ret = ARCHIVE_OK;
339                         else
340                                 ret = ARCHIVE_FATAL;
341                 }
342                 return (ret);
343         }
344
345         /* Note: The "warn" return is just to inform the options
346          * supervisor that we didn't handle it.  It will generate
347          * a suitable error if no one used this option. */
348         return (ARCHIVE_WARN);
349 }
350
351 static int
352 archive_read_format_cpio_read_header(struct archive_read *a,
353     struct archive_entry *entry)
354 {
355         struct cpio *cpio;
356         const void *h;
357         struct archive_string_conv *sconv;
358         size_t namelength;
359         size_t name_pad;
360         int r;
361
362         cpio = (struct cpio *)(a->format->data);
363         sconv = cpio->opt_sconv;
364         if (sconv == NULL) {
365                 if (!cpio->init_default_conversion) {
366                         cpio->sconv_default =
367                             archive_string_default_conversion_for_read(
368                               &(a->archive));
369                         cpio->init_default_conversion = 1;
370                 }
371                 sconv = cpio->sconv_default;
372         }
373         
374         r = (cpio->read_header(a, cpio, entry, &namelength, &name_pad));
375
376         if (r < ARCHIVE_WARN)
377                 return (r);
378
379         /* Read name from buffer. */
380         h = __archive_read_ahead(a, namelength + name_pad, NULL);
381         if (h == NULL)
382             return (ARCHIVE_FATAL);
383         if (archive_entry_copy_pathname_l(entry,
384             (const char *)h, namelength, sconv) != 0) {
385                 if (errno == ENOMEM) {
386                         archive_set_error(&a->archive, ENOMEM,
387                             "Can't allocate memory for Pathname");
388                         return (ARCHIVE_FATAL);
389                 }
390                 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
391                     "Pathname can't be converted from %s to current locale.",
392                     archive_string_conversion_charset_name(sconv));
393                 r = ARCHIVE_WARN;
394         }
395         cpio->entry_offset = 0;
396
397         __archive_read_consume(a, namelength + name_pad);
398
399         /* If this is a symlink, read the link contents. */
400         if (archive_entry_filetype(entry) == AE_IFLNK) {
401                 h = __archive_read_ahead(a, cpio->entry_bytes_remaining, NULL);
402                 if (h == NULL)
403                         return (ARCHIVE_FATAL);
404                 if (archive_entry_copy_symlink_l(entry, (const char *)h,
405                     cpio->entry_bytes_remaining, sconv) != 0) {
406                         if (errno == ENOMEM) {
407                                 archive_set_error(&a->archive, ENOMEM,
408                                     "Can't allocate memory for Linkname");
409                                 return (ARCHIVE_FATAL);
410                         }
411                         archive_set_error(&a->archive,
412                             ARCHIVE_ERRNO_FILE_FORMAT,
413                             "Linkname can't be converted from %s to "
414                             "current locale.",
415                             archive_string_conversion_charset_name(sconv));
416                         r = ARCHIVE_WARN;
417                 }
418                 __archive_read_consume(a, cpio->entry_bytes_remaining);
419                 cpio->entry_bytes_remaining = 0;
420         }
421
422         /* XXX TODO: If the full mode is 0160200, then this is a Solaris
423          * ACL description for the following entry.  Read this body
424          * and parse it as a Solaris-style ACL, then read the next
425          * header.  XXX */
426
427         /* Compare name to "TRAILER!!!" to test for end-of-archive. */
428         if (namelength == 11 && strcmp((const char *)h, "TRAILER!!!") == 0) {
429                 /* TODO: Store file location of start of block. */
430                 archive_clear_error(&a->archive);
431                 return (ARCHIVE_EOF);
432         }
433
434         /* Detect and record hardlinks to previously-extracted entries. */
435         if (record_hardlink(a, cpio, entry) != ARCHIVE_OK) {
436                 return (ARCHIVE_FATAL);
437         }
438
439         return (r);
440 }
441
442 static int
443 archive_read_format_cpio_read_data(struct archive_read *a,
444     const void **buff, size_t *size, int64_t *offset)
445 {
446         ssize_t bytes_read;
447         struct cpio *cpio;
448
449         cpio = (struct cpio *)(a->format->data);
450
451         if (cpio->entry_bytes_unconsumed) {
452                 __archive_read_consume(a, cpio->entry_bytes_unconsumed);
453                 cpio->entry_bytes_unconsumed = 0;
454         }
455
456         if (cpio->entry_bytes_remaining > 0) {
457                 *buff = __archive_read_ahead(a, 1, &bytes_read);
458                 if (bytes_read <= 0)
459                         return (ARCHIVE_FATAL);
460                 if (bytes_read > cpio->entry_bytes_remaining)
461                         bytes_read = cpio->entry_bytes_remaining;
462                 *size = bytes_read;
463                 cpio->entry_bytes_unconsumed = bytes_read;
464                 *offset = cpio->entry_offset;
465                 cpio->entry_offset += bytes_read;
466                 cpio->entry_bytes_remaining -= bytes_read;
467                 return (ARCHIVE_OK);
468         } else {
469                 if (cpio->entry_padding !=
470                         __archive_read_consume(a, cpio->entry_padding)) {
471                         return (ARCHIVE_FATAL);
472                 }
473                 cpio->entry_padding = 0;
474                 *buff = NULL;
475                 *size = 0;
476                 *offset = cpio->entry_offset;
477                 return (ARCHIVE_EOF);
478         }
479 }
480
481 static int
482 archive_read_format_cpio_skip(struct archive_read *a)
483 {
484         struct cpio *cpio = (struct cpio *)(a->format->data);
485         int64_t to_skip = cpio->entry_bytes_remaining + cpio->entry_padding +
486                 cpio->entry_bytes_unconsumed;
487
488         if (to_skip != __archive_read_consume(a, to_skip)) {
489                 return (ARCHIVE_FATAL);
490         }
491         cpio->entry_bytes_remaining = 0;
492         cpio->entry_padding = 0;
493         cpio->entry_bytes_unconsumed = 0;
494         return (ARCHIVE_OK);
495 }
496
497 /*
498  * Skip forward to the next cpio newc header by searching for the
499  * 07070[12] string.  This should be generalized and merged with
500  * find_odc_header below.
501  */
502 static int
503 is_hex(const char *p, size_t len)
504 {
505         while (len-- > 0) {
506                 if ((*p >= '0' && *p <= '9')
507                     || (*p >= 'a' && *p <= 'f')
508                     || (*p >= 'A' && *p <= 'F'))
509                         ++p;
510                 else
511                         return (0);
512         }
513         return (1);
514 }
515
516 static int
517 find_newc_header(struct archive_read *a)
518 {
519         const void *h;
520         const char *p, *q;
521         size_t skip, skipped = 0;
522         ssize_t bytes;
523
524         for (;;) {
525                 h = __archive_read_ahead(a, newc_header_size, &bytes);
526                 if (h == NULL)
527                         return (ARCHIVE_FATAL);
528                 p = h;
529                 q = p + bytes;
530
531                 /* Try the typical case first, then go into the slow search.*/
532                 if (memcmp("07070", p, 5) == 0
533                     && (p[5] == '1' || p[5] == '2')
534                     && is_hex(p, newc_header_size))
535                         return (ARCHIVE_OK);
536
537                 /*
538                  * Scan ahead until we find something that looks
539                  * like a newc header.
540                  */
541                 while (p + newc_header_size <= q) {
542                         switch (p[5]) {
543                         case '1':
544                         case '2':
545                                 if (memcmp("07070", p, 5) == 0
546                                     && is_hex(p, newc_header_size)) {
547                                         skip = p - (const char *)h;
548                                         __archive_read_consume(a, skip);
549                                         skipped += skip;
550                                         if (skipped > 0) {
551                                                 archive_set_error(&a->archive,
552                                                     0,
553                                                     "Skipped %d bytes before "
554                                                     "finding valid header",
555                                                     (int)skipped);
556                                                 return (ARCHIVE_WARN);
557                                         }
558                                         return (ARCHIVE_OK);
559                                 }
560                                 p += 2;
561                                 break;
562                         case '0':
563                                 p++;
564                                 break;
565                         default:
566                                 p += 6;
567                                 break;
568                         }
569                 }
570                 skip = p - (const char *)h;
571                 __archive_read_consume(a, skip);
572                 skipped += skip;
573         }
574 }
575
576 static int
577 header_newc(struct archive_read *a, struct cpio *cpio,
578     struct archive_entry *entry, size_t *namelength, size_t *name_pad)
579 {
580         const void *h;
581         const char *header;
582         int r;
583
584         r = find_newc_header(a);
585         if (r < ARCHIVE_WARN)
586                 return (r);
587
588         /* Read fixed-size portion of header. */
589         h = __archive_read_ahead(a, newc_header_size, NULL);
590         if (h == NULL)
591             return (ARCHIVE_FATAL);
592
593         /* Parse out hex fields. */
594         header = (const char *)h;
595
596         if (memcmp(header + newc_magic_offset, "070701", 6) == 0) {
597                 a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_NOCRC;
598                 a->archive.archive_format_name = "ASCII cpio (SVR4 with no CRC)";
599         } else if (memcmp(header + newc_magic_offset, "070702", 6) == 0) {
600                 a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_CRC;
601                 a->archive.archive_format_name = "ASCII cpio (SVR4 with CRC)";
602         } else {
603                 /* TODO: Abort here? */
604         }
605
606         archive_entry_set_devmajor(entry, atol16(header + newc_devmajor_offset, newc_devmajor_size));
607         archive_entry_set_devminor(entry, atol16(header + newc_devminor_offset, newc_devminor_size));
608         archive_entry_set_ino(entry, atol16(header + newc_ino_offset, newc_ino_size));
609         archive_entry_set_mode(entry, atol16(header + newc_mode_offset, newc_mode_size));
610         archive_entry_set_uid(entry, atol16(header + newc_uid_offset, newc_uid_size));
611         archive_entry_set_gid(entry, atol16(header + newc_gid_offset, newc_gid_size));
612         archive_entry_set_nlink(entry, atol16(header + newc_nlink_offset, newc_nlink_size));
613         archive_entry_set_rdevmajor(entry, atol16(header + newc_rdevmajor_offset, newc_rdevmajor_size));
614         archive_entry_set_rdevminor(entry, atol16(header + newc_rdevminor_offset, newc_rdevminor_size));
615         archive_entry_set_mtime(entry, atol16(header + newc_mtime_offset, newc_mtime_size), 0);
616         *namelength = atol16(header + newc_namesize_offset, newc_namesize_size);
617         /* Pad name to 2 more than a multiple of 4. */
618         *name_pad = (2 - *namelength) & 3;
619
620         /*
621          * Note: entry_bytes_remaining is at least 64 bits and
622          * therefore guaranteed to be big enough for a 33-bit file
623          * size.
624          */
625         cpio->entry_bytes_remaining =
626             atol16(header + newc_filesize_offset, newc_filesize_size);
627         archive_entry_set_size(entry, cpio->entry_bytes_remaining);
628         /* Pad file contents to a multiple of 4. */
629         cpio->entry_padding = 3 & -cpio->entry_bytes_remaining;
630         __archive_read_consume(a, newc_header_size);
631         return (r);
632 }
633
634 /*
635  * Skip forward to the next cpio odc header by searching for the
636  * 070707 string.  This is a hand-optimized search that could
637  * probably be easily generalized to handle all character-based
638  * cpio variants.
639  */
640 static int
641 is_octal(const char *p, size_t len)
642 {
643         while (len-- > 0) {
644                 if (*p < '0' || *p > '7')
645                         return (0);
646                 ++p;
647         }
648         return (1);
649 }
650
651 static int
652 is_afio_large(const char *h, size_t len)
653 {
654         if (len < afiol_header_size)
655                 return (0);
656         if (h[afiol_ino_m_offset] != 'm'
657             || h[afiol_mtime_n_offset] != 'n'
658             || h[afiol_xsize_s_offset] != 's'
659             || h[afiol_filesize_c_offset] != ':')
660                 return (0);
661         if (!is_hex(h + afiol_dev_offset, afiol_ino_m_offset - afiol_dev_offset))
662                 return (0);
663         if (!is_hex(h + afiol_mode_offset, afiol_mtime_n_offset - afiol_mode_offset))
664                 return (0);
665         if (!is_hex(h + afiol_namesize_offset, afiol_xsize_s_offset - afiol_namesize_offset))
666                 return (0);
667         if (!is_hex(h + afiol_filesize_offset, afiol_filesize_size))
668                 return (0);
669         return (1);
670 }
671
672 static int
673 find_odc_header(struct archive_read *a)
674 {
675         const void *h;
676         const char *p, *q;
677         size_t skip, skipped = 0;
678         ssize_t bytes;
679
680         for (;;) {
681                 h = __archive_read_ahead(a, odc_header_size, &bytes);
682                 if (h == NULL)
683                         return (ARCHIVE_FATAL);
684                 p = h;
685                 q = p + bytes;
686
687                 /* Try the typical case first, then go into the slow search.*/
688                 if (memcmp("070707", p, 6) == 0 && is_octal(p, odc_header_size))
689                         return (ARCHIVE_OK);
690                 if (memcmp("070727", p, 6) == 0 && is_afio_large(p, bytes)) {
691                         a->archive.archive_format = ARCHIVE_FORMAT_CPIO_AFIO_LARGE;
692                         return (ARCHIVE_OK);
693                 }
694
695                 /*
696                  * Scan ahead until we find something that looks
697                  * like an odc header.
698                  */
699                 while (p + odc_header_size <= q) {
700                         switch (p[5]) {
701                         case '7':
702                                 if ((memcmp("070707", p, 6) == 0
703                                     && is_octal(p, odc_header_size))
704                                     || (memcmp("070727", p, 6) == 0
705                                         && is_afio_large(p, q - p))) {
706                                         skip = p - (const char *)h;
707                                         __archive_read_consume(a, skip);
708                                         skipped += skip;
709                                         if (p[4] == '2')
710                                                 a->archive.archive_format =
711                                                     ARCHIVE_FORMAT_CPIO_AFIO_LARGE;
712                                         if (skipped > 0) {
713                                                 archive_set_error(&a->archive,
714                                                     0,
715                                                     "Skipped %d bytes before "
716                                                     "finding valid header",
717                                                     (int)skipped);
718                                                 return (ARCHIVE_WARN);
719                                         }
720                                         return (ARCHIVE_OK);
721                                 }
722                                 p += 2;
723                                 break;
724                         case '0':
725                                 p++;
726                                 break;
727                         default:
728                                 p += 6;
729                                 break;
730                         }
731                 }
732                 skip = p - (const char *)h;
733                 __archive_read_consume(a, skip);
734                 skipped += skip;
735         }
736 }
737
738 static int
739 header_odc(struct archive_read *a, struct cpio *cpio,
740     struct archive_entry *entry, size_t *namelength, size_t *name_pad)
741 {
742         const void *h;
743         int r;
744         const char *header;
745
746         a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
747         a->archive.archive_format_name = "POSIX octet-oriented cpio";
748
749         /* Find the start of the next header. */
750         r = find_odc_header(a);
751         if (r < ARCHIVE_WARN)
752                 return (r);
753
754         if (a->archive.archive_format == ARCHIVE_FORMAT_CPIO_AFIO_LARGE) {
755                 int r2 = (header_afiol(a, cpio, entry, namelength, name_pad));
756                 if (r2 == ARCHIVE_OK)
757                         return (r);
758                 else
759                         return (r2);
760         }
761
762         /* Read fixed-size portion of header. */
763         h = __archive_read_ahead(a, odc_header_size, NULL);
764         if (h == NULL)
765             return (ARCHIVE_FATAL);
766
767         /* Parse out octal fields. */
768         header = (const char *)h;
769
770         archive_entry_set_dev(entry, atol8(header + odc_dev_offset, odc_dev_size));
771         archive_entry_set_ino(entry, atol8(header + odc_ino_offset, odc_ino_size));
772         archive_entry_set_mode(entry, atol8(header + odc_mode_offset, odc_mode_size));
773         archive_entry_set_uid(entry, atol8(header + odc_uid_offset, odc_uid_size));
774         archive_entry_set_gid(entry, atol8(header + odc_gid_offset, odc_gid_size));
775         archive_entry_set_nlink(entry, atol8(header + odc_nlink_offset, odc_nlink_size));
776         archive_entry_set_rdev(entry, atol8(header + odc_rdev_offset, odc_rdev_size));
777         archive_entry_set_mtime(entry, atol8(header + odc_mtime_offset, odc_mtime_size), 0);
778         *namelength = atol8(header + odc_namesize_offset, odc_namesize_size);
779         *name_pad = 0; /* No padding of filename. */
780
781         /*
782          * Note: entry_bytes_remaining is at least 64 bits and
783          * therefore guaranteed to be big enough for a 33-bit file
784          * size.
785          */
786         cpio->entry_bytes_remaining =
787             atol8(header + odc_filesize_offset, odc_filesize_size);
788         archive_entry_set_size(entry, cpio->entry_bytes_remaining);
789         cpio->entry_padding = 0;
790         __archive_read_consume(a, odc_header_size);
791         return (r);
792 }
793
794 /*
795  * NOTE: if a filename suffix is ".z", it is the file gziped by afio.
796  * it would be nice that we can show uncompressed file size and we can
797  * uncompressed file contents automatically, unfortunately we have nothing
798  * to get a uncompressed file size while reading each header. it means
799  * we also cannot uncompressed file contens under the our framework.
800  */
801 static int
802 header_afiol(struct archive_read *a, struct cpio *cpio,
803     struct archive_entry *entry, size_t *namelength, size_t *name_pad)
804 {
805         const void *h;
806         const char *header;
807
808         a->archive.archive_format = ARCHIVE_FORMAT_CPIO_AFIO_LARGE;
809         a->archive.archive_format_name = "afio large ASCII";
810
811         /* Read fixed-size portion of header. */
812         h = __archive_read_ahead(a, afiol_header_size, NULL);
813         if (h == NULL)
814             return (ARCHIVE_FATAL);
815
816         /* Parse out octal fields. */
817         header = (const char *)h;
818
819         archive_entry_set_dev(entry, atol16(header + afiol_dev_offset, afiol_dev_size));
820         archive_entry_set_ino(entry, atol16(header + afiol_ino_offset, afiol_ino_size));
821         archive_entry_set_mode(entry, atol8(header + afiol_mode_offset, afiol_mode_size));
822         archive_entry_set_uid(entry, atol16(header + afiol_uid_offset, afiol_uid_size));
823         archive_entry_set_gid(entry, atol16(header + afiol_gid_offset, afiol_gid_size));
824         archive_entry_set_nlink(entry, atol16(header + afiol_nlink_offset, afiol_nlink_size));
825         archive_entry_set_rdev(entry, atol16(header + afiol_rdev_offset, afiol_rdev_size));
826         archive_entry_set_mtime(entry, atol16(header + afiol_mtime_offset, afiol_mtime_size), 0);
827         *namelength = atol16(header + afiol_namesize_offset, afiol_namesize_size);
828         *name_pad = 0; /* No padding of filename. */
829
830         cpio->entry_bytes_remaining =
831             atol16(header + afiol_filesize_offset, afiol_filesize_size);
832         archive_entry_set_size(entry, cpio->entry_bytes_remaining);
833         cpio->entry_padding = 0;
834         __archive_read_consume(a, afiol_header_size);
835         return (ARCHIVE_OK);
836 }
837
838
839 static int
840 header_bin_le(struct archive_read *a, struct cpio *cpio,
841     struct archive_entry *entry, size_t *namelength, size_t *name_pad)
842 {
843         const void *h;
844         const unsigned char *header;
845
846         a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_LE;
847         a->archive.archive_format_name = "cpio (little-endian binary)";
848
849         /* Read fixed-size portion of header. */
850         h = __archive_read_ahead(a, bin_header_size, NULL);
851         if (h == NULL)
852             return (ARCHIVE_FATAL);
853
854         /* Parse out binary fields. */
855         header = (const unsigned char *)h;
856
857         archive_entry_set_dev(entry, header[bin_dev_offset] + header[bin_dev_offset + 1] * 256);
858         archive_entry_set_ino(entry, header[bin_ino_offset] + header[bin_ino_offset + 1] * 256);
859         archive_entry_set_mode(entry, header[bin_mode_offset] + header[bin_mode_offset + 1] * 256);
860         archive_entry_set_uid(entry, header[bin_uid_offset] + header[bin_uid_offset + 1] * 256);
861         archive_entry_set_gid(entry, header[bin_gid_offset] + header[bin_gid_offset + 1] * 256);
862         archive_entry_set_nlink(entry, header[bin_nlink_offset] + header[bin_nlink_offset + 1] * 256);
863         archive_entry_set_rdev(entry, header[bin_rdev_offset] + header[bin_rdev_offset + 1] * 256);
864         archive_entry_set_mtime(entry, le4(header + bin_mtime_offset), 0);
865         *namelength = header[bin_namesize_offset] + header[bin_namesize_offset + 1] * 256;
866         *name_pad = *namelength & 1; /* Pad to even. */
867
868         cpio->entry_bytes_remaining = le4(header + bin_filesize_offset);
869         archive_entry_set_size(entry, cpio->entry_bytes_remaining);
870         cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
871         __archive_read_consume(a, bin_header_size);
872         return (ARCHIVE_OK);
873 }
874
875 static int
876 header_bin_be(struct archive_read *a, struct cpio *cpio,
877     struct archive_entry *entry, size_t *namelength, size_t *name_pad)
878 {
879         const void *h;
880         const unsigned char *header;
881
882         a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_BE;
883         a->archive.archive_format_name = "cpio (big-endian binary)";
884
885         /* Read fixed-size portion of header. */
886         h = __archive_read_ahead(a, bin_header_size, NULL);
887         if (h == NULL)
888             return (ARCHIVE_FATAL);
889
890         /* Parse out binary fields. */
891         header = (const unsigned char *)h;
892
893         archive_entry_set_dev(entry, header[bin_dev_offset] * 256 + header[bin_dev_offset + 1]);
894         archive_entry_set_ino(entry, header[bin_ino_offset] * 256 + header[bin_ino_offset + 1]);
895         archive_entry_set_mode(entry, header[bin_mode_offset] * 256 + header[bin_mode_offset + 1]);
896         archive_entry_set_uid(entry, header[bin_uid_offset] * 256 + header[bin_uid_offset + 1]);
897         archive_entry_set_gid(entry, header[bin_gid_offset] * 256 + header[bin_gid_offset + 1]);
898         archive_entry_set_nlink(entry, header[bin_nlink_offset] * 256 + header[bin_nlink_offset + 1]);
899         archive_entry_set_rdev(entry, header[bin_rdev_offset] * 256 + header[bin_rdev_offset + 1]);
900         archive_entry_set_mtime(entry, be4(header + bin_mtime_offset), 0);
901         *namelength = header[bin_namesize_offset] * 256 + header[bin_namesize_offset + 1];
902         *name_pad = *namelength & 1; /* Pad to even. */
903
904         cpio->entry_bytes_remaining = be4(header + bin_filesize_offset);
905         archive_entry_set_size(entry, cpio->entry_bytes_remaining);
906         cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
907             __archive_read_consume(a, bin_header_size);
908         return (ARCHIVE_OK);
909 }
910
911 static int
912 archive_read_format_cpio_cleanup(struct archive_read *a)
913 {
914         struct cpio *cpio;
915
916         cpio = (struct cpio *)(a->format->data);
917         /* Free inode->name map */
918         while (cpio->links_head != NULL) {
919                 struct links_entry *lp = cpio->links_head->next;
920
921                 if (cpio->links_head->name)
922                         free(cpio->links_head->name);
923                 free(cpio->links_head);
924                 cpio->links_head = lp;
925         }
926         free(cpio);
927         (a->format->data) = NULL;
928         return (ARCHIVE_OK);
929 }
930
931 static int
932 le4(const unsigned char *p)
933 {
934         return ((p[0]<<16) + (p[1]<<24) + (p[2]<<0) + (p[3]<<8));
935 }
936
937
938 static int
939 be4(const unsigned char *p)
940 {
941         return ((p[0]<<24) + (p[1]<<16) + (p[2]<<8) + (p[3]));
942 }
943
944 /*
945  * Note that this implementation does not (and should not!) obey
946  * locale settings; you cannot simply substitute strtol here, since
947  * it does obey locale.
948  */
949 static int64_t
950 atol8(const char *p, unsigned char_cnt)
951 {
952         int64_t l;
953         int digit;
954
955         l = 0;
956         while (char_cnt-- > 0) {
957                 if (*p >= '0' && *p <= '7')
958                         digit = *p - '0';
959                 else
960                         return (l);
961                 p++;
962                 l <<= 3;
963                 l |= digit;
964         }
965         return (l);
966 }
967
968 static int64_t
969 atol16(const char *p, unsigned char_cnt)
970 {
971         int64_t l;
972         int digit;
973
974         l = 0;
975         while (char_cnt-- > 0) {
976                 if (*p >= 'a' && *p <= 'f')
977                         digit = *p - 'a' + 10;
978                 else if (*p >= 'A' && *p <= 'F')
979                         digit = *p - 'A' + 10;
980                 else if (*p >= '0' && *p <= '9')
981                         digit = *p - '0';
982                 else
983                         return (l);
984                 p++;
985                 l <<= 4;
986                 l |= digit;
987         }
988         return (l);
989 }
990
991 static int
992 record_hardlink(struct archive_read *a,
993     struct cpio *cpio, struct archive_entry *entry)
994 {
995         struct links_entry      *le;
996         dev_t dev;
997         int64_t ino;
998
999         if (archive_entry_nlink(entry) <= 1)
1000                 return (ARCHIVE_OK);
1001
1002         dev = archive_entry_dev(entry);
1003         ino = archive_entry_ino64(entry);
1004
1005         /*
1006          * First look in the list of multiply-linked files.  If we've
1007          * already dumped it, convert this entry to a hard link entry.
1008          */
1009         for (le = cpio->links_head; le; le = le->next) {
1010                 if (le->dev == dev && le->ino == ino) {
1011                         archive_entry_copy_hardlink(entry, le->name);
1012
1013                         if (--le->links <= 0) {
1014                                 if (le->previous != NULL)
1015                                         le->previous->next = le->next;
1016                                 if (le->next != NULL)
1017                                         le->next->previous = le->previous;
1018                                 if (cpio->links_head == le)
1019                                         cpio->links_head = le->next;
1020                                 free(le->name);
1021                                 free(le);
1022                         }
1023
1024                         return (ARCHIVE_OK);
1025                 }
1026         }
1027
1028         le = (struct links_entry *)malloc(sizeof(struct links_entry));
1029         if (le == NULL) {
1030                 archive_set_error(&a->archive,
1031                     ENOMEM, "Out of memory adding file to list");
1032                 return (ARCHIVE_FATAL);
1033         }
1034         if (cpio->links_head != NULL)
1035                 cpio->links_head->previous = le;
1036         le->next = cpio->links_head;
1037         le->previous = NULL;
1038         cpio->links_head = le;
1039         le->dev = dev;
1040         le->ino = ino;
1041         le->links = archive_entry_nlink(entry) - 1;
1042         le->name = strdup(archive_entry_pathname(entry));
1043         if (le->name == NULL) {
1044                 archive_set_error(&a->archive,
1045                     ENOMEM, "Out of memory adding file to list");
1046                 return (ARCHIVE_FATAL);
1047         }
1048
1049         return (ARCHIVE_OK);
1050 }