xref: /netbsd-src/sys/fs/cd9660/cd9660_util.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: cd9660_util.c,v 1.11 2014/06/01 11:01:18 martin 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. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)cd9660_util.c	8.3 (Berkeley) 12/5/94
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: cd9660_util.c,v 1.11 2014/06/01 11:01:18 martin Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/namei.h>
45 #include <sys/resourcevar.h>
46 #include <sys/kernel.h>
47 #include <sys/file.h>
48 #include <sys/stat.h>
49 #include <sys/buf.h>
50 #include <sys/proc.h>
51 #include <sys/mount.h>
52 #include <sys/vnode.h>
53 #include <sys/malloc.h>
54 #include <sys/dirent.h>
55 
56 #include <fs/cd9660/iso.h>
57 #include <fs/cd9660/cd9660_extern.h>
58 
59 #include <fs/unicode.h>
60 
61 static u_int16_t wget(const u_char **, size_t *, int);
62 static int wput(u_char *, size_t, u_int16_t, int);
63 
64 int cd9660_utf8_joliet = 1;
65 
66 /*
67  * Get one character out of an iso filename
68  * Return number of bytes consumed
69  */
70 int
71 isochar(const u_char *isofn, const u_char *isoend, int joliet_level,
72 	u_int16_t *c)
73 {
74 	*c = isofn[0];
75 	if (joliet_level == 0 || isofn + 1 == isoend) {
76 		/* (00) and (01) are one byte in Joliet, too */
77 		return 1;
78 	}
79 
80 	if (cd9660_utf8_joliet) {
81 		*c = (*c << 8) + isofn[1];
82 	} else {
83 		/* characters outside ISO-8859-1 subset replaced with '?' */
84 		if (*c != 0)
85 			*c = '?';
86 		else
87 			*c = isofn[1];
88 	}
89 
90 	return 2;
91 }
92 
93 /*
94  * translate and compare a filename
95  * Note: Version number plus ';' may be omitted.
96  */
97 int
98 isofncmp(const u_char *fn, size_t fnlen, const u_char *isofn, size_t isolen,
99 	int joliet_level)
100 {
101 	int i, j;
102 	u_int16_t fc, ic;
103 	const u_char *isoend = isofn + isolen;
104 
105 	while (fnlen > 0) {
106 		fc = wget(&fn, &fnlen, joliet_level);
107 
108 		if (isofn == isoend)
109 			return fc;
110 		isofn += isochar(isofn, isoend, joliet_level, &ic);
111 		if (ic == ';') {
112 			switch (fc) {
113 			default:
114 				return fc;
115 			case 0:
116 				return 0;
117 			case ';':
118 				break;
119 			}
120 			for (i = 0; fnlen-- != 0; i = i * 10 + *fn++ - '0') {
121 				if (*fn < '0' || *fn > '9') {
122 					return -1;
123 				}
124 			}
125 			for (j = 0; isofn != isoend; j = j * 10 + ic - '0')
126 				isofn += isochar(isofn, isoend,
127 						 joliet_level, &ic);
128 			return i - j;
129 		}
130 		if (ic != fc) {
131 			if (ic >= 'A' && ic <= 'Z') {
132 				if (ic + ('a' - 'A') != fc) {
133 					if (fc >= 'a' && fc <= 'z')
134 						fc -= 'a' - 'A';
135 
136 					return (int) fc - (int) ic;
137 				}
138 			} else
139 				return (int) fc - (int) ic;
140 		}
141 	}
142 	if (isofn != isoend) {
143 		isofn += isochar(isofn, isoend, joliet_level, &ic);
144 		switch (ic) {
145 		default:
146 			return -1;
147 		case '.':
148 			if (isofn != isoend) {
149 				isochar(isofn, isoend, joliet_level, &ic);
150 				if (ic == ';')
151 					return 0;
152 			}
153 			return -1;
154 		case ';':
155 			return 0;
156 		}
157 	}
158 	return 0;
159 }
160 
161 /*
162  * translate a filename
163  */
164 void
165 isofntrans(const u_char *infn, int infnlen, u_char *outfn, u_short *outfnlen,
166 	int original, int casetrans, int assoc, int joliet_level)
167 {
168 	int fnidx = 0;
169 	const u_char *infnend = infn + infnlen;
170 	u_int16_t c;
171 	int sz;
172 
173 	if (assoc) {
174 		*outfn++ = ASSOCCHAR;
175 		fnidx++;
176 	}
177 
178 	for(; infn != infnend; fnidx += sz) {
179 		infn += isochar(infn, infnend, joliet_level, &c);
180 
181 		if (casetrans && joliet_level == 0 && c >= 'A' && c <= 'Z')
182 			c = c + ('a' - 'A');
183 		else if (!original && c == ';') {
184 			if (fnidx > 0 && outfn[-1] == '.')
185 				fnidx--;
186 			break;
187 		}
188 
189 		sz = wput(outfn, ISO_MAXNAMLEN - fnidx, c, joliet_level);
190 		if (sz == 0) {
191 			/* not enough space to write the character */
192 			if (fnidx < ISO_MAXNAMLEN) {
193 				*outfn = '?';
194 				fnidx++;
195 			}
196 			break;
197 		}
198 		outfn += sz;
199 	}
200 	*outfnlen = fnidx;
201 }
202 
203 static u_int16_t
204 wget(const u_char **str, size_t *sz, int joliet_level)
205 {
206 	if (joliet_level > 0 && cd9660_utf8_joliet) {
207 		/* decode UTF-8 sequence */
208 		return wget_utf8((const char **) str, sz);
209 	} else {
210 		/*
211 		 * Raw 8-bit characters without any conversion. For Joliet,
212 		 * this effectively assumes provided file name is using
213 		 * ISO-8859-1 subset.
214 		 */
215 		u_int16_t c = *str[0];
216 		(*str)++;
217 		(*sz)--;
218 
219 		return c;
220 	}
221 }
222 
223 static int
224 wput(u_char *s, size_t n, u_int16_t c, int joliet_level)
225 {
226 	if (joliet_level > 0 && cd9660_utf8_joliet) {
227 		/* Store Joliet file name encoded into UTF-8 */
228 		return wput_utf8((char *)s, n, c);
229 	} else {
230 		/*
231 		 * Store raw 8-bit characters without any conversion.
232 		 * For Joliet case, this filters the Unicode characters
233 		 * to ISO-8859-1 subset.
234 		 */
235 		*s = (u_char)c;
236 		return 1;
237 	}
238 }
239