xref: /minix/sys/fs/cd9660/cd9660_util.c (revision 0a6a1f1d)
1 /*	$NetBSD: cd9660_util.c,v 1.12 2014/11/10 18:46:33 maxv 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.12 2014/11/10 18:46:33 maxv 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/dirent.h>
54 
55 #include <fs/cd9660/iso.h>
56 #include <fs/cd9660/cd9660_extern.h>
57 
58 #include <fs/unicode.h>
59 
60 static u_int16_t wget(const u_char **, size_t *, int);
61 static int wput(u_char *, size_t, u_int16_t, int);
62 
63 int cd9660_utf8_joliet = 1;
64 
65 /*
66  * Get one character out of an iso filename
67  * Return number of bytes consumed
68  */
69 int
isochar(const u_char * isofn,const u_char * isoend,int joliet_level,u_int16_t * c)70 isochar(const u_char *isofn, const u_char *isoend, int joliet_level,
71 	u_int16_t *c)
72 {
73 	*c = isofn[0];
74 	if (joliet_level == 0 || isofn + 1 == isoend) {
75 		/* (00) and (01) are one byte in Joliet, too */
76 		return 1;
77 	}
78 
79 	if (cd9660_utf8_joliet) {
80 		*c = (*c << 8) + isofn[1];
81 	} else {
82 		/* characters outside ISO-8859-1 subset replaced with '?' */
83 		if (*c != 0)
84 			*c = '?';
85 		else
86 			*c = isofn[1];
87 	}
88 
89 	return 2;
90 }
91 
92 /*
93  * translate and compare a filename
94  * Note: Version number plus ';' may be omitted.
95  */
96 int
isofncmp(const u_char * fn,size_t fnlen,const u_char * isofn,size_t isolen,int joliet_level)97 isofncmp(const u_char *fn, size_t fnlen, const u_char *isofn, size_t isolen,
98 	int joliet_level)
99 {
100 	int i, j;
101 	u_int16_t fc, ic;
102 	const u_char *isoend = isofn + isolen;
103 
104 	while (fnlen > 0) {
105 		fc = wget(&fn, &fnlen, joliet_level);
106 
107 		if (isofn == isoend)
108 			return fc;
109 		isofn += isochar(isofn, isoend, joliet_level, &ic);
110 		if (ic == ';') {
111 			switch (fc) {
112 			default:
113 				return fc;
114 			case 0:
115 				return 0;
116 			case ';':
117 				break;
118 			}
119 			for (i = 0; fnlen-- != 0; i = i * 10 + *fn++ - '0') {
120 				if (*fn < '0' || *fn > '9') {
121 					return -1;
122 				}
123 			}
124 			for (j = 0; isofn != isoend; j = j * 10 + ic - '0')
125 				isofn += isochar(isofn, isoend,
126 						 joliet_level, &ic);
127 			return i - j;
128 		}
129 		if (ic != fc) {
130 			if (ic >= 'A' && ic <= 'Z') {
131 				if (ic + ('a' - 'A') != fc) {
132 					if (fc >= 'a' && fc <= 'z')
133 						fc -= 'a' - 'A';
134 
135 					return (int) fc - (int) ic;
136 				}
137 			} else
138 				return (int) fc - (int) ic;
139 		}
140 	}
141 	if (isofn != isoend) {
142 		isofn += isochar(isofn, isoend, joliet_level, &ic);
143 		switch (ic) {
144 		default:
145 			return -1;
146 		case '.':
147 			if (isofn != isoend) {
148 				isochar(isofn, isoend, joliet_level, &ic);
149 				if (ic == ';')
150 					return 0;
151 			}
152 			return -1;
153 		case ';':
154 			return 0;
155 		}
156 	}
157 	return 0;
158 }
159 
160 /*
161  * translate a filename
162  */
163 void
isofntrans(const u_char * infn,int infnlen,u_char * outfn,u_short * outfnlen,int original,int casetrans,int assoc,int joliet_level)164 isofntrans(const u_char *infn, int infnlen, u_char *outfn, u_short *outfnlen,
165 	int original, int casetrans, int assoc, int joliet_level)
166 {
167 	int fnidx = 0;
168 	const u_char *infnend = infn + infnlen;
169 	u_int16_t c;
170 	int sz;
171 
172 	if (assoc) {
173 		*outfn++ = ASSOCCHAR;
174 		fnidx++;
175 	}
176 
177 	for(; infn != infnend; fnidx += sz) {
178 		infn += isochar(infn, infnend, joliet_level, &c);
179 
180 		if (casetrans && joliet_level == 0 && c >= 'A' && c <= 'Z')
181 			c = c + ('a' - 'A');
182 		else if (!original && c == ';') {
183 			if (fnidx > 0 && outfn[-1] == '.')
184 				fnidx--;
185 			break;
186 		}
187 
188 		sz = wput(outfn, ISO_MAXNAMLEN - fnidx, c, joliet_level);
189 		if (sz == 0) {
190 			/* not enough space to write the character */
191 			if (fnidx < ISO_MAXNAMLEN) {
192 				*outfn = '?';
193 				fnidx++;
194 			}
195 			break;
196 		}
197 		outfn += sz;
198 	}
199 	*outfnlen = fnidx;
200 }
201 
202 static u_int16_t
wget(const u_char ** str,size_t * sz,int joliet_level)203 wget(const u_char **str, size_t *sz, int joliet_level)
204 {
205 	if (joliet_level > 0 && cd9660_utf8_joliet) {
206 		/* decode UTF-8 sequence */
207 		return wget_utf8((const char **) str, sz);
208 	} else {
209 		/*
210 		 * Raw 8-bit characters without any conversion. For Joliet,
211 		 * this effectively assumes provided file name is using
212 		 * ISO-8859-1 subset.
213 		 */
214 		u_int16_t c = *str[0];
215 		(*str)++;
216 		(*sz)--;
217 
218 		return c;
219 	}
220 }
221 
222 static int
wput(u_char * s,size_t n,u_int16_t c,int joliet_level)223 wput(u_char *s, size_t n, u_int16_t c, int joliet_level)
224 {
225 	if (joliet_level > 0 && cd9660_utf8_joliet) {
226 		/* Store Joliet file name encoded into UTF-8 */
227 		return wput_utf8((char *)s, n, c);
228 	} else {
229 		/*
230 		 * Store raw 8-bit characters without any conversion.
231 		 * For Joliet case, this filters the Unicode characters
232 		 * to ISO-8859-1 subset.
233 		 */
234 		*s = (u_char)c;
235 		return 1;
236 	}
237 }
238