]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/makefs/cd9660/cd9660_archimedes.c
Import zstandard 1.1.4 in base
[FreeBSD/FreeBSD.git] / usr.sbin / makefs / cd9660 / cd9660_archimedes.c
1 /* $NetBSD: cd9660_archimedes.c,v 1.1 2009/01/10 22:06:29 bjh21 Exp $ */
2
3 /*-
4  * Copyright (c) 1998, 2009 Ben Harris
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  * 
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 /*
30  * cd9660_archimedes.c - support for RISC OS "ARCHIMEDES" extension
31  *
32  * RISC OS CDFS looks for a special block at the end of the System Use
33  * Field for each file.  If present, this contains the RISC OS load
34  * and exec address (used to hold the file timestamp and type), the
35  * file attributes, and a flag indicating whether the first character
36  * of the filename should be replaced with '!' (since many special
37  * RISC OS filenames do).
38  */
39
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
43 #include <assert.h>
44 #include <stdint.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <util.h>
48
49 #include "makefs.h"
50 #include "cd9660.h"
51 #include "cd9660_archimedes.h"
52
53 /*
54  * Convert a Unix time_t (non-leap seconds since 1970-01-01) to a RISC
55  * OS time (non-leap(?) centiseconds since 1900-01-01(?)).
56  */
57
58 static u_int64_t
59 riscos_date(time_t unixtime)
60 {
61         u_int64_t base;
62
63         base = 31536000ULL * 70 + 86400 * 17;
64         return (((u_int64_t)unixtime) + base)*100;
65 }
66
67 /*
68  * Add "ARCHIMEDES" metadata to a node if that seems appropriate.
69  *
70  * We touch regular files with names matching /,[0-9a-f]{3}$/ and
71  * directories matching /^!/.
72  */
73 static void
74 archimedes_convert_node(cd9660node *node)
75 {
76         struct ISO_ARCHIMEDES *arc;
77         size_t len;
78         int type = -1;
79         uint64_t stamp;
80
81         if (node->su_tail_data != NULL)
82                 /* Something else already has the tail. */
83                 return;
84
85         len = strlen(node->node->name);
86         if (len < 1) return;
87
88         if (len >= 4 && node->node->name[len-4] == ',')
89                 /* XXX should support ,xxx and ,lxa */
90                 type = strtoul(node->node->name + len - 3, NULL, 16);
91         if (type == -1 && node->node->name[0] != '!')
92                 return;
93         if (type == -1) type = 0;
94
95         assert(sizeof(*arc) == 32);
96         arc = ecalloc(1, sizeof(*arc));
97
98         stamp = riscos_date(node->node->inode->st.st_mtime);
99
100         memcpy(arc->magic, "ARCHIMEDES", 10);
101         cd9660_731(0xfff00000 | (type << 8) | (stamp >> 32), arc->loadaddr);
102         cd9660_731(stamp & 0x00ffffffffULL, arc->execaddr);
103         arc->ro_attr = RO_ACCESS_UR | RO_ACCESS_OR;
104         arc->cdfs_attr = node->node->name[0] == '!' ? CDFS_PLING : 0;
105         node->su_tail_data = (void *)arc;
106         node->su_tail_size = sizeof(*arc);
107 }
108
109 /*
110  * Add "ARCHIMEDES" metadata to an entire tree recursively.
111  */
112 void
113 archimedes_convert_tree(cd9660node *node)
114 {
115         cd9660node *cn;
116
117         assert(node != NULL);
118
119         archimedes_convert_node(node);
120
121                 /* Recurse on children. */
122         TAILQ_FOREACH(cn, &node->cn_children, cn_next_child)
123                 archimedes_convert_tree(cn);
124 }