- Back out rev 1.6 noticed by Matt Dillon. However, still keep the use
[dragonfly.git] / contrib / libarchive / archive_read_support_format_cpio.c
1 /*-
2  * Copyright (c) 2003-2004 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  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
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: src/lib/libarchive/archive_read_support_format_cpio.c,v 1.11 2004/08/14 03:45:45 kientzle Exp $");
29
30 #include <sys/stat.h>
31
32 #include <errno.h>
33 /* #include <stdint.h> */ /* See archive_platform.h */
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37
38 #include "archive.h"
39 #include "archive_entry.h"
40 #include "archive_private.h"
41
42 struct cpio_bin_header {
43         unsigned char   c_magic[2];
44         unsigned char   c_dev[2];
45         unsigned char   c_ino[2];
46         unsigned char   c_mode[2];
47         unsigned char   c_uid[2];
48         unsigned char   c_gid[2];
49         unsigned char   c_nlink[2];
50         unsigned char   c_rdev[2];
51         unsigned char   c_mtime[4];
52         unsigned char   c_namesize[2];
53         unsigned char   c_filesize[4];
54 };
55
56 struct cpio_odc_header {
57         char    c_magic[6];
58         char    c_dev[6];
59         char    c_ino[6];
60         char    c_mode[6];
61         char    c_uid[6];
62         char    c_gid[6];
63         char    c_nlink[6];
64         char    c_rdev[6];
65         char    c_mtime[11];
66         char    c_namesize[6];
67         char    c_filesize[11];
68 };
69
70 struct cpio_newc_header {
71         char    c_magic[6];
72         char    c_ino[8];
73         char    c_mode[8];
74         char    c_uid[8];
75         char    c_gid[8];
76         char    c_nlink[8];
77         char    c_mtime[8];
78         char    c_filesize[8];
79         char    c_devmajor[8];
80         char    c_devminor[8];
81         char    c_rdevmajor[8];
82         char    c_rdevminor[8];
83         char    c_namesize[8];
84         char    c_crc[8];
85 };
86
87 struct links_entry {
88         struct links_entry      *next;
89         struct links_entry      *previous;
90         int                      links;
91         dev_t                    dev;
92         ino_t                    ino;
93         char                    *name;
94 };
95
96 #define CPIO_MAGIC   0x13141516
97 struct cpio {
98         int                       magic;
99         int                     (*read_header)(struct archive *, struct cpio *,
100                                      struct stat *, size_t *, size_t *);
101         struct links_entry       *links_head;
102         struct archive_string     entry_name;
103         struct archive_string     entry_linkname;
104         off_t                     entry_bytes_remaining;
105         off_t                     entry_offset;
106         off_t                     entry_padding;
107 };
108
109 static int64_t  atol16(const char *, unsigned);
110 static int64_t  atol8(const char *, unsigned);
111 static int      archive_read_format_cpio_bid(struct archive *);
112 static int      archive_read_format_cpio_cleanup(struct archive *);
113 static int      archive_read_format_cpio_read_data(struct archive *,
114                     const void **, size_t *, off_t *);
115 static int      archive_read_format_cpio_read_header(struct archive *,
116                     struct archive_entry *);
117 static int      be4(const unsigned char *);
118 static int      header_bin_be(struct archive *, struct cpio *, struct stat *,
119                     size_t *, size_t *);
120 static int      header_bin_le(struct archive *, struct cpio *, struct stat *,
121                     size_t *, size_t *);
122 static int      header_newc(struct archive *, struct cpio *, struct stat *,
123                     size_t *, size_t *);
124 static int      header_odc(struct archive *, struct cpio *, struct stat *,
125                     size_t *, size_t *);
126 static int      le4(const unsigned char *);
127 static void     record_hardlink(struct cpio *cpio, struct archive_entry *entry,
128                     const struct stat *st);
129
130 int
131 archive_read_support_format_cpio(struct archive *a)
132 {
133         struct cpio *cpio;
134         int r;
135
136         cpio = malloc(sizeof(*cpio));
137         memset(cpio, 0, sizeof(*cpio));
138         cpio->magic = CPIO_MAGIC;
139
140         r = __archive_read_register_format(a,
141             cpio,
142             archive_read_format_cpio_bid,
143             archive_read_format_cpio_read_header,
144             archive_read_format_cpio_read_data,
145             archive_read_format_cpio_cleanup);
146
147         if (r != ARCHIVE_OK)
148                 free(cpio);
149         return (ARCHIVE_OK);
150 }
151
152
153 static int
154 archive_read_format_cpio_bid(struct archive *a)
155 {
156         int bid, bytes_read;
157         const void *h;
158         const unsigned char *p;
159         struct cpio *cpio;
160
161         cpio = *(a->pformat_data);
162         bid = 0;
163         bytes_read = (a->compression_read_ahead)(a, &h, 6);
164         if (bytes_read < 6)
165             return (-1);
166
167         p = h;
168         if (memcmp(p, "070707", 6) == 0) {
169                 /* ASCII cpio archive (odc, POSIX.1) */
170                 cpio->read_header = header_odc;
171                 bid += 48;
172                 /*
173                  * XXX TODO:  More verification; Could check that only octal
174                  * digits appear in appropriate header locations. XXX
175                  */
176         } else if (memcmp(p, "070701", 6) == 0) {
177                 /* ASCII cpio archive (SVR4 without CRC) */
178                 cpio->read_header = header_newc;
179                 bid += 48;
180                 /*
181                  * XXX TODO:  More verification; Could check that only hex
182                  * digits appear in appropriate header locations. XXX
183                  */
184         } else if (memcmp(p, "070702", 6) == 0) {
185                 /* ASCII cpio archive (SVR4 with CRC) */
186                 /* XXX TODO: Flag that we should check the CRC. XXX */
187                 cpio->read_header = header_newc;
188                 bid += 48;
189                 /*
190                  * XXX TODO:  More verification; Could check that only hex
191                  * digits appear in appropriate header locations. XXX
192                  */
193         } else if (p[0] * 256 + p[1] == 070707) {
194                 /* big-endian binary cpio archives */
195                 cpio->read_header = header_bin_be;
196                 bid += 16;
197                 /* Is more verification possible here? */
198         } else if (p[0] + p[1] * 256 == 070707) {
199                 /* little-endian binary cpio archives */
200                 cpio->read_header = header_bin_le;
201                 bid += 16;
202                 /* Is more verification possible here? */
203         } else
204                 return (ARCHIVE_WARN);
205
206         return (bid);
207 }
208
209 static int
210 archive_read_format_cpio_read_header(struct archive *a,
211     struct archive_entry *entry)
212 {
213         struct stat st;
214         struct cpio *cpio;
215         size_t bytes;
216         const void *h;
217         size_t namelength;
218         size_t name_pad;
219         int r;
220
221         memset(&st, 0, sizeof(st));
222
223         cpio = *(a->pformat_data);
224         r = (cpio->read_header(a, cpio, &st, &namelength, &name_pad));
225
226         if (r != ARCHIVE_OK)
227                 return (r);
228
229         /* Assign all of the 'stat' fields at once. */
230         archive_entry_copy_stat(entry, &st);
231
232         /* Read name from buffer. */
233         bytes = (a->compression_read_ahead)(a, &h, namelength + name_pad);
234         if (bytes < namelength + name_pad)
235             return (ARCHIVE_FATAL);
236         (a->compression_read_consume)(a, namelength + name_pad);
237         archive_strncpy(&cpio->entry_name, h, namelength);
238         archive_entry_set_pathname(entry, cpio->entry_name.s);
239         cpio->entry_offset = 0;
240
241         /* If this is a symlink, read the link contents. */
242         if (S_ISLNK(st.st_mode)) {
243                 bytes = (a->compression_read_ahead)(a, &h,
244                     cpio->entry_bytes_remaining);
245                 if ((off_t)bytes < cpio->entry_bytes_remaining)
246                         return (ARCHIVE_FATAL);
247                 (a->compression_read_consume)(a, cpio->entry_bytes_remaining);
248                 archive_strncpy(&cpio->entry_linkname, h,
249                     cpio->entry_bytes_remaining);
250                 archive_entry_set_symlink(entry, cpio->entry_linkname.s);
251                 cpio->entry_bytes_remaining = 0;
252         }
253
254         /* Compare name to "TRAILER!!!" to test for end-of-archive. */
255         if (namelength == 11 && strcmp(h,"TRAILER!!!")==0) {
256             /* TODO: Store file location of start of block. */
257             archive_set_error(a, 0, NULL);
258             return (ARCHIVE_EOF);
259         }
260
261         /* Detect and record hardlinks to previously-extracted entries. */
262         record_hardlink(cpio, entry, &st);
263
264         return (ARCHIVE_OK);
265 }
266
267 static int
268 archive_read_format_cpio_read_data(struct archive *a,
269     const void **buff, size_t *size, off_t *offset)
270 {
271         ssize_t bytes_read;
272         struct cpio *cpio;
273
274         cpio = *(a->pformat_data);
275         if (cpio->entry_bytes_remaining > 0) {
276                 bytes_read = (a->compression_read_ahead)(a, buff, 1);
277                 if (bytes_read <= 0)
278                         return (ARCHIVE_FATAL);
279                 if (bytes_read > cpio->entry_bytes_remaining)
280                         bytes_read = cpio->entry_bytes_remaining;
281                 *size = bytes_read;
282                 *offset = cpio->entry_offset;
283                 cpio->entry_offset += bytes_read;
284                 cpio->entry_bytes_remaining -= bytes_read;
285                 (a->compression_read_consume)(a, bytes_read);
286                 return (ARCHIVE_OK);
287         } else {
288                 while (cpio->entry_padding > 0) {
289                         bytes_read = (a->compression_read_ahead)(a, buff, 1);
290                         if (bytes_read <= 0)
291                                 return (ARCHIVE_FATAL);
292                         if (bytes_read > cpio->entry_padding)
293                                 bytes_read = cpio->entry_padding;
294                         (a->compression_read_consume)(a, bytes_read);
295                         cpio->entry_padding -= bytes_read;
296                 }
297                 *buff = NULL;
298                 *size = 0;
299                 *offset = cpio->entry_offset;
300                 return (ARCHIVE_EOF);
301         }
302 }
303
304 static int
305 header_newc(struct archive *a, struct cpio *cpio, struct stat *st,
306     size_t *namelength, size_t *name_pad)
307 {
308         const void *h;
309         const struct cpio_newc_header *header;
310         size_t bytes;
311
312         a->archive_format = ARCHIVE_FORMAT_CPIO;
313         a->archive_format_name = "ASCII cpio (SVR4 with no CRC)";
314
315         /* Read fixed-size portion of header. */
316         bytes = (a->compression_read_ahead)(a, &h, sizeof(struct cpio_newc_header));
317         if (bytes < sizeof(struct cpio_newc_header))
318             return (ARCHIVE_FATAL);
319         (a->compression_read_consume)(a, sizeof(struct cpio_newc_header));
320
321         /* Parse out hex fields into struct stat. */
322         header = h;
323         st->st_ino = atol16(header->c_ino, sizeof(header->c_ino));
324         st->st_mode = atol16(header->c_mode, sizeof(header->c_mode));
325         st->st_uid = atol16(header->c_uid, sizeof(header->c_uid));
326         st->st_gid = atol16(header->c_gid, sizeof(header->c_gid));
327         st->st_nlink = atol16(header->c_nlink, sizeof(header->c_nlink));
328         st->st_mtime = atol16(header->c_mtime, sizeof(header->c_mtime));
329         *namelength = atol16(header->c_namesize, sizeof(header->c_namesize));
330         /* Pad name to 2 more than a multiple of 4. */
331         *name_pad = (2 - *namelength) & 3;
332
333         /*
334          * Note: entry_bytes_remaining is at least 64 bits and
335          * therefore gauranteed to be big enough for a 33-bit file
336          * size.  struct stat.st_size may only be 32 bits, so
337          * assigning there first could lose information.
338          */
339         cpio->entry_bytes_remaining =
340             atol16(header->c_filesize, sizeof(header->c_filesize));
341         st->st_size = cpio->entry_bytes_remaining;
342         /* Pad file contents to a multiple of 4. */
343         cpio->entry_padding = 3 & -cpio->entry_bytes_remaining;
344         return (ARCHIVE_OK);
345 }
346
347 static int
348 header_odc(struct archive *a, struct cpio *cpio, struct stat *st,
349     size_t *namelength, size_t *name_pad)
350 {
351         const void *h;
352         const struct cpio_odc_header *header;
353         size_t bytes;
354
355         a->archive_format = ARCHIVE_FORMAT_CPIO;
356         a->archive_format_name = "POSIX octet-oriented cpio";
357
358         /* Read fixed-size portion of header. */
359         bytes = (a->compression_read_ahead)(a, &h, sizeof(struct cpio_odc_header));
360         if (bytes < sizeof(struct cpio_odc_header))
361             return (ARCHIVE_FATAL);
362         (a->compression_read_consume)(a, sizeof(struct cpio_odc_header));
363
364         /* Parse out octal fields into struct stat. */
365         header = h;
366
367         st->st_dev = atol8(header->c_dev, sizeof(header->c_dev));
368         st->st_ino = atol8(header->c_ino, sizeof(header->c_ino));
369         st->st_mode = atol8(header->c_mode, sizeof(header->c_mode));
370         st->st_uid = atol8(header->c_uid, sizeof(header->c_uid));
371         st->st_gid = atol8(header->c_gid, sizeof(header->c_gid));
372         st->st_nlink = atol8(header->c_nlink, sizeof(header->c_nlink));
373         st->st_rdev = atol8(header->c_rdev, sizeof(header->c_rdev));
374         st->st_mtime = atol8(header->c_mtime, sizeof(header->c_mtime));
375         *namelength = atol8(header->c_namesize, sizeof(header->c_namesize));
376         *name_pad = 0; /* No padding of filename. */
377
378         /*
379          * Note: entry_bytes_remaining is at least 64 bits and
380          * therefore gauranteed to be big enough for a 33-bit file
381          * size.  struct stat.st_size may only be 32 bits, so
382          * assigning there first could lose information.
383          */
384         cpio->entry_bytes_remaining =
385             atol8(header->c_filesize, sizeof(header->c_filesize));
386         st->st_size = cpio->entry_bytes_remaining;
387         cpio->entry_padding = 0;
388         return (ARCHIVE_OK);
389 }
390
391 static int
392 header_bin_le(struct archive *a, struct cpio *cpio, struct stat *st,
393     size_t *namelength, size_t *name_pad)
394 {
395         const void *h;
396         const struct cpio_bin_header *header;
397         size_t bytes;
398
399         a->archive_format = ARCHIVE_FORMAT_CPIO;
400         a->archive_format_name = "cpio (little-endian binary)";
401
402         /* Read fixed-size portion of header. */
403         bytes = (a->compression_read_ahead)(a, &h, sizeof(struct cpio_bin_header));
404         if (bytes < sizeof(struct cpio_bin_header))
405             return (ARCHIVE_FATAL);
406         (a->compression_read_consume)(a, sizeof(struct cpio_bin_header));
407
408         /* Parse out binary fields into struct stat. */
409         header = h;
410
411         st->st_dev = header->c_dev[0] + header->c_dev[1] * 256;
412         st->st_ino = header->c_ino[0] + header->c_ino[1] * 256;
413         st->st_mode = header->c_mode[0] + header->c_mode[1] * 256;
414         st->st_uid = header->c_uid[0] + header->c_uid[1] * 256;
415         st->st_gid = header->c_gid[0] + header->c_gid[1] * 256;
416         st->st_nlink = header->c_nlink[0] + header->c_nlink[1] * 256;
417         st->st_rdev = header->c_rdev[0] + header->c_rdev[1] * 256;
418         st->st_mtime = le4(header->c_mtime);
419         *namelength = header->c_namesize[0] + header->c_namesize[1] * 256;
420         *name_pad = *namelength & 1; /* Pad to even. */
421
422         cpio->entry_bytes_remaining = le4(header->c_filesize);
423         st->st_size = cpio->entry_bytes_remaining;
424         cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
425         return (ARCHIVE_OK);
426 }
427
428 static int
429 header_bin_be(struct archive *a, struct cpio *cpio, struct stat *st,
430     size_t *namelength, size_t *name_pad)
431 {
432         const void *h;
433         const struct cpio_bin_header *header;
434         size_t bytes;
435
436         a->archive_format = ARCHIVE_FORMAT_CPIO;
437         a->archive_format_name = "cpio (big-endian binary)";
438
439         /* Read fixed-size portion of header. */
440         bytes = (a->compression_read_ahead)(a, &h,
441             sizeof(struct cpio_bin_header));
442         if (bytes < sizeof(struct cpio_bin_header))
443             return (ARCHIVE_FATAL);
444         (a->compression_read_consume)(a, sizeof(struct cpio_bin_header));
445
446         /* Parse out binary fields into struct stat. */
447         header = h;
448         st->st_dev = header->c_dev[0] * 256 + header->c_dev[1];
449         st->st_ino = header->c_ino[0] * 256 + header->c_ino[1];
450         st->st_mode = header->c_mode[0] * 256 + header->c_mode[1];
451         st->st_uid = header->c_uid[0] * 256 + header->c_uid[1];
452         st->st_gid = header->c_gid[0] * 256 + header->c_gid[1];
453         st->st_nlink = header->c_nlink[0] * 256 + header->c_nlink[1];
454         st->st_rdev = header->c_rdev[0] * 256 + header->c_rdev[1];
455         st->st_mtime = be4(header->c_mtime);
456         *namelength = header->c_namesize[0] * 256 + header->c_namesize[1];
457         *name_pad = *namelength & 1; /* Pad to even. */
458
459         cpio->entry_bytes_remaining = be4(header->c_filesize);
460         st->st_size = cpio->entry_bytes_remaining;
461         cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
462         return (ARCHIVE_OK);
463 }
464
465 static int
466 archive_read_format_cpio_cleanup(struct archive *a)
467 {
468         struct cpio *cpio;
469
470         cpio = *(a->pformat_data);
471         /* Free inode->name map */
472         while (cpio->links_head != NULL) {
473                 struct links_entry *lp = cpio->links_head->next;
474
475                 if (cpio->links_head->name)
476                         free(cpio->links_head->name);
477                 free(cpio->links_head);
478                 cpio->links_head = lp;
479         }
480
481         free(cpio);
482         *(a->pformat_data) = NULL;
483         return (ARCHIVE_OK);
484 }
485
486 static int
487 le4(const unsigned char *p)
488 {
489         return ((p[0]<<16) + (p[1]<<24) + (p[2]<<0) + (p[3]<<8));
490 }
491
492
493 static int
494 be4(const unsigned char *p)
495 {
496         return (p[0] + (p[1]<<8) + (p[2]<<16) + (p[3]<<24));
497 }
498
499 /*
500  * Note that this implementation does not (and should not!) obey
501  * locale settings; you cannot simply substitute strtol here, since
502  * it does obey locale.
503  */
504 static int64_t
505 atol8(const char *p, unsigned char_cnt)
506 {
507         int64_t l;
508         int digit;
509
510         l = 0;
511         while (char_cnt-- > 0) {
512                 if (*p >= '0' && *p <= '7')
513                         digit = *p - '0';
514                 else
515                         return (l);
516                 p++;
517                 l <<= 3;
518                 l |= digit;
519         }
520         return (l);
521 }
522
523 static int64_t
524 atol16(const char *p, unsigned char_cnt)
525 {
526         int64_t l;
527         int digit;
528
529         l = 0;
530         while (char_cnt-- > 0) {
531                 if (*p >= 'a' && *p <= 'f')
532                         digit = *p - 'a' + 10;
533                 else if (*p >= 'A' && *p <= 'F')
534                         digit = *p - 'A' + 10;
535                 else if (*p >= '0' && *p <= '9')
536                         digit = *p - '0';
537                 else
538                         return (l);
539                 p++;
540                 l <<= 4;
541                 l |= digit;
542         }
543         return (l);
544 }
545
546 static void
547 record_hardlink(struct cpio *cpio, struct archive_entry *entry,
548     const struct stat *st)
549 {
550         struct links_entry      *le;
551
552         /*
553          * First look in the list of multiply-linked files.  If we've
554          * already dumped it, convert this entry to a hard link entry.
555          */
556         for (le = cpio->links_head; le; le = le->next) {
557                 if (le->dev == st->st_dev && le->ino == st->st_ino) {
558                         archive_entry_set_hardlink(entry, le->name);
559
560                         if (--le->links <= 0) {
561                                 if (le->previous != NULL)
562                                         le->previous->next = le->next;
563                                 if (le->next != NULL)
564                                         le->next->previous = le->previous;
565                                 if (cpio->links_head == le)
566                                         cpio->links_head = le->next;
567                                 free(le);
568                         }
569
570                         return;
571                 }
572         }
573
574         le = malloc(sizeof(struct links_entry));
575         if (cpio->links_head != NULL)
576                 cpio->links_head->previous = le;
577         le->next = cpio->links_head;
578         le->previous = NULL;
579         cpio->links_head = le;
580         le->dev = st->st_dev;
581         le->ino = st->st_ino;
582         le->links = st->st_nlink - 1;
583         le->name = strdup(archive_entry_pathname(entry));
584 }