aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/9660/ichar.c
blob: 3b2b5367faa608b691c1937cbec7fd02507291aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <libsec.h>
#include <ctype.h>

#include "iso9660.h"

/*
 * ISO 9660 file names must be uppercase, digits, or underscore.
 * We use lowercase, digits, and underscore, translating lower to upper
 * in mkisostring, and upper to lower in isostring.
 * Files with uppercase letters in their names are thus nonconforming.
 * Conforming files also must have a basename
 * at most 8 letters and at most one suffix of at most 3 letters.
 */
char*
isostring(uchar *buf, int len)
{
	char *p, *q;

	p = emalloc(len+1);
	memmove(p, buf, len);
	p[len] = '\0';
	while(len > 0 && p[len-1] == ' ')
		p[--len] = '\0';
	for(q=p; *q; q++)
		*q = tolower((uchar)*q);
	q = atom(p);
	free(p);
	return q;
}

int 
isisofrog(char c)
{
	if(c >= '0' && c <= '9')
		return 0;
	if(c >= 'a' && c <= 'z')
		return 0;
	if(c == '_')
		return 0;

	return 1;
}

int
isbadiso9660(char *s)
{
	char *p, *q;
	int i;

	if((p = strchr(s, '.')) != nil) {
		if(p-s > 8)
			return 1;
		for(q=s; q<p; q++)
			if(isisofrog(*q))
				return 1;
		if(strlen(p+1) > 3)
			return 1;
		for(q=p+1; *q; q++)
			if(isisofrog(*q))
				return 1;
	} else {
		if(strlen(s) > 8)
			return 1;
		for(q=s; *q; q++)
			if(isisofrog(*q))
				return 1;

		/*
		 * we rename files of the form [FD]dddddd
		 * so they don't interfere with us.
		 */
		if(strlen(s) == 7 && (s[0] == 'D' || s[0] == 'F')) {
			for(i=1; i<7; i++)
				if(s[i] < '0' || s[i] > '9')
					break;
			if(i == 7)
				return 1;
		}
	}
	return 0;
}

/*
 * ISO9660 name comparison
 * 
 * The standard algorithm is as follows:
 *   Take the filenames without extensions, pad the shorter with 0x20s (spaces),
 *   and do strcmp.  If they are equal, go on.
 *   Take the extensions, pad the shorter with 0x20s (spaces),
 *   and do strcmp.  If they are equal, go on.
 *   Compare the version numbers.
 *
 * Since Plan 9 names are not allowed to contain characters 0x00-0x1F,
 * the padded comparisons are equivalent to using strcmp directly.
 * We still need to handle the base and extension differently,
 * so that .foo sorts before !foo.foo.
 */
int
isocmp(const void *va, const void *vb)
{
	int i;
	char s1[32], s2[32], *b1, *b2, *e1, *e2;
	const Direc *a, *b;

	a = va;
	b = vb;

	strecpy(s1, s1+sizeof s1, a->confname);
	b1 = s1;
	strecpy(s2, s2+sizeof s2, b->confname);
	b2 = s2;
	if((e1 = strchr(b1, '.')) != nil)
		*e1++ = '\0';
	else
		e1 = "";
	if((e2 = strchr(b2, '.')) != nil)
		*e2++ = '\0';
	else
		e2 = "";

	if((i = strcmp(b1, b2)) != 0)
		return i;

	return strcmp(e1, e2);
}

static char*
mkisostring(char *isobuf, int n, char *s)
{
	char *p, *q, *eq;

	eq = isobuf+n;
	for(p=s, q=isobuf; *p && q < eq; p++)
		if('a' <= *p && *p <= 'z')
			*q++ = *p+'A'-'a';
		else
			*q++ = *p;

	while(q < eq)
		*q++ = ' ';

	return isobuf;
}

void
Cputisopvd(Cdimg *cd, Cdinfo info)
{
	char buf[130];

	Cputc(cd, 1);				/* primary volume descriptor */
	Cputs(cd, "CD001", 5);			/* standard identifier */
	Cputc(cd, 1);				/* volume descriptor version */
	Cputc(cd, 0);				/* unused */

	assert(~info.flags & (CDplan9|CDrockridge));

	/* system identifier */
	strcpy(buf, "");
	if(info.flags & CDplan9)
		strcat(buf, "plan 9 ");
	if(info.flags & CDrockridge)
		strcat(buf, "rrip ");
	if(info.flags & CDbootable)
		strcat(buf, "boot ");
	if(info.flags & CDconform)
		strcat(buf, "iso9660");
	else
		strcat(buf, "utf8");
	
	struprcpy(buf, buf);
	Cputs(cd, buf, 32);

	Cputs(cd, mkisostring(buf, 32, info.volumename), 32);			/* volume identifier */

	Crepeat(cd, 0, 8);				/* unused */
	Cputn(cd, 0, 4);				/* volume space size */
	Crepeat(cd, 0, 32);				/* unused */
	Cputn(cd, 1, 2);				/* volume set size */
	Cputn(cd, 1, 2);				/* volume sequence number */
	Cputn(cd, Blocksize, 2);			/* logical block size */
	Cputn(cd, 0, 4);				/* path table size */
	Cputnl(cd, 0, 4);				/* location of Lpath */
	Cputnl(cd, 0, 4);				/* location of optional Lpath */
	Cputnm(cd, 0, 4);				/* location of Mpath */
	Cputnm(cd, 0, 4);				/* location of optional Mpath */
	Cputisodir(cd, nil, DTroot, 1, Cwoffset(cd));			/* root directory */

	Cputs(cd, mkisostring(buf, 128, info.volumeset), 128);		/* volume set identifier */
	Cputs(cd, mkisostring(buf, 128, info.publisher), 128);			/* publisher identifier */
	Cputs(cd, mkisostring(buf, 128, info.preparer), 128);			/* data preparer identifier */
	Cputs(cd, mkisostring(buf, 128, info.application), 128);		/* application identifier */

	Cputs(cd, "", 37);			/* copyright notice */
	Cputs(cd, "", 37);			/* abstract */
	Cputs(cd, "", 37);			/* bibliographic file */
	Cputdate1(cd, now);				/* volume creation date */
	Cputdate1(cd, now);				/* volume modification date */
	Cputdate1(cd, 0);				/* volume expiration date */
	Cputdate1(cd, 0);				/* volume effective date */
	Cputc(cd, 1);				/* file structure version */
	Cpadblock(cd);
}