xref: /netbsd-src/sys/fs/cd9660/cd9660_util.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: cd9660_util.c,v 1.1 2002/12/23 17:52:09 jdolecek Exp $	*/
2 
3 /*-
4  * Copyright (c) 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley
8  * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
9  * Support code is derived from software contributed to Berkeley
10  * by Atsushi Murai (amurai@spec.co.jp).
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	@(#)cd9660_util.c	8.3 (Berkeley) 12/5/94
41  */
42 
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: cd9660_util.c,v 1.1 2002/12/23 17:52:09 jdolecek Exp $");
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/namei.h>
49 #include <sys/resourcevar.h>
50 #include <sys/kernel.h>
51 #include <sys/file.h>
52 #include <sys/stat.h>
53 #include <sys/buf.h>
54 #include <sys/proc.h>
55 #include <sys/mount.h>
56 #include <sys/vnode.h>
57 #include <sys/malloc.h>
58 #include <sys/dirent.h>
59 
60 #include <fs/cd9660/iso.h>
61 #include <fs/cd9660/cd9660_extern.h>
62 
63 /*
64  * Get one character out of an iso filename
65  * Return number of bytes consumed
66  */
67 int
68 isochar(isofn, isoend, joliet_level, c)
69 	const u_char *isofn;
70 	const u_char *isoend;
71 	int joliet_level;
72 	u_char *c;
73 {
74 	*c = *isofn++;
75 	if (joliet_level == 0 || isofn == isoend)
76 		/* (00) and (01) are one byte in Joliet, too */
77 		return 1;
78 
79 	/* No Unicode support yet :-( */
80 	switch (*c) {
81 	default:
82 		*c = '?';
83 		break;
84 	case '\0':
85 		*c = *isofn;
86 		break;
87 	}
88 	return 2;
89 }
90 
91 /*
92  * translate and compare a filename
93  * Note: Version number plus ';' may be omitted.
94  */
95 int
96 isofncmp(fn, fnlen, isofn, isolen, joliet_level)
97 	const u_char *fn, *isofn;
98 	int fnlen, isolen, joliet_level;
99 {
100 	int i, j;
101 	char c;
102 	const u_char *isoend = isofn + isolen;
103 
104 	while (--fnlen >= 0) {
105 		if (isofn == isoend)
106 			return *fn;
107 		isofn += isochar(isofn, isoend, joliet_level, &c);
108 		if (c == ';') {
109 			switch (*fn++) {
110 			default:
111 				return *--fn;
112 			case 0:
113 				return 0;
114 			case ';':
115 				break;
116 			}
117 			for (i = 0; --fnlen >= 0; i = i * 10 + *fn++ - '0') {
118 				if (*fn < '0' || *fn > '9') {
119 					return -1;
120 				}
121 			}
122 			for (j = 0; isofn != isoend; j = j * 10 + c - '0')
123 				isofn += isochar(isofn, isoend,
124 						 joliet_level, &c);
125 			return i - j;
126 		}
127 		if (((u_char) c) != *fn) {
128 			if (c >= 'A' && c <= 'Z') {
129 				if (c + ('a' - 'A') != *fn) {
130 					if (*fn >= 'a' && *fn <= 'z')
131 						return *fn - ('a' - 'A') - c;
132 					else
133 						return *fn - c;
134 				}
135 			} else
136 				return *fn - c;
137 		}
138 		fn++;
139 	}
140 	if (isofn != isoend) {
141 		isofn += isochar(isofn, isoend, joliet_level, &c);
142 		switch (c) {
143 		default:
144 			return -1;
145 		case '.':
146 			if (isofn != isoend) {
147 				isochar(isofn, isoend, joliet_level, &c);
148 				if (c == ';')
149 					return 0;
150 			}
151 			return -1;
152 		case ';':
153 			return 0;
154 		}
155 	}
156 	return 0;
157 }
158 
159 /*
160  * translate a filename
161  */
162 void
163 isofntrans(infn, infnlen, outfn, outfnlen, original, casetrans, assoc, joliet_level)
164 	u_char *infn, *outfn;
165 	int infnlen;
166 	u_short *outfnlen;
167 	int original;
168 	int casetrans;
169 	int assoc;
170 	int joliet_level;
171 {
172 	int fnidx = 0;
173 	u_char *infnend = infn + infnlen;
174 
175 	if (assoc) {
176 		*outfn++ = ASSOCCHAR;
177 		fnidx++;
178 	}
179 	for (; infn != infnend; fnidx++) {
180 		char c;
181 
182 		infn += isochar(infn, infnend, joliet_level, &c);
183 
184 		if (casetrans && joliet_level == 0 && c >= 'A' && c <= 'Z')
185 			*outfn++ = c + ('a' - 'A');
186 		else if (!original && c == ';') {
187 			if (fnidx > 0 && outfn[-1] == '.')
188 				fnidx--;
189 			break;
190 		} else
191 			*outfn++ = c;
192 	}
193 	*outfnlen = fnidx;
194 }
195