xref: /freebsd/sys/fs/cd9660/cd9660_util.c (revision fed1ca4b719c56c930f2259d80663cd34be812bb)
1 /*-
2  * Copyright (c) 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley
6  * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
7  * Support code is derived from software contributed to Berkeley
8  * by Atsushi Murai (amurai@spec.co.jp). Joliet support was added by
9  * Joachim Kuebart (joki@kuebart.stuttgart.netsurf.de).
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)cd9660_util.c	8.3 (Berkeley) 12/5/94
36  */
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/mount.h>
44 #include <sys/vnode.h>
45 #include <sys/iconv.h>
46 
47 #include <fs/cd9660/iso.h>
48 #include <fs/cd9660/cd9660_mount.h>
49 
50 extern struct iconv_functions *cd9660_iconv;
51 
52 /*
53  * Get one character out of an iso filename
54  * Obey joliet_level
55  * Return number of bytes consumed
56  */
57 int
58 isochar(isofn, isoend, joliet_level, c, clen, flags, handle)
59       u_char *isofn;
60       u_char *isoend;
61       int joliet_level;
62       u_short *c;
63       int *clen;
64       int flags;
65       void *handle;
66 {
67       size_t i, j, len;
68       char inbuf[3], outbuf[3], *inp, *outp;
69 
70       *c = *isofn++;
71       if (clen) *clen = 1;
72       if (joliet_level == 0 || isofn == isoend)
73               /* (00) and (01) are one byte in Joliet, too */
74               return 1;
75 
76       if (flags & ISOFSMNT_KICONV && cd9660_iconv) {
77               i = j = len = 2;
78               inbuf[0]=(char)*(isofn - 1);
79               inbuf[1]=(char)*isofn;
80               inbuf[2]='\0';
81               inp = inbuf;
82               outp = outbuf;
83               cd9660_iconv->convchr(handle, __DECONST(const char **, &inp), &i,
84                   &outp, &j);
85               len -= j;
86               if (clen) *clen = len;
87               *c = '\0';
88               while(len--)
89                       *c |= (*(outp - len - 1) & 0xff) << (len << 3);
90       } else {
91               switch (*c) {
92               default:
93                       *c = '?';
94                       break;
95               case '\0':
96                       *c = *isofn;
97                       break;
98               }
99       }
100 
101       return 2;
102 }
103 
104 /*
105  * translate and compare a filename
106  * returns (fn - isofn)
107  * Note: Version number plus ';' may be omitted.
108  */
109 int
110 isofncmp(fn, fnlen, isofn, isolen, joliet_level, flags, handle, lhandle)
111 	u_char *fn;
112 	int fnlen;
113 	u_char *isofn;
114 	int isolen;
115 	int joliet_level;
116 	int flags;
117 	void *handle;
118 	void *lhandle;
119 {
120 	int i, j;
121 	u_short c, d;
122 	u_char *fnend = fn + fnlen, *isoend = isofn + isolen;
123 
124 	for (; fn < fnend; ) {
125 		d = sgetrune(fn, fnend - fn, __DECONST(const char **, &fn),
126 		    flags, lhandle);
127 		if (isofn == isoend)
128 			return d;
129 		isofn += isochar(isofn, isoend, joliet_level, &c, NULL, flags, handle);
130 		if (c == ';') {
131 			if (d != ';')
132 				return d;
133 			for (i = 0; fn < fnend; i = i * 10 + *fn++ - '0') {
134 				if (*fn < '0' || *fn > '9') {
135 					return -1;
136 				}
137 			}
138 			for (j = 0; isofn != isoend; j = j * 10 + c - '0')
139 				isofn += isochar(isofn, isoend,
140 						 joliet_level, &c,
141 						 NULL, flags, handle);
142 			return i - j;
143 		}
144 		if (c != d) {
145 			if (c >= 'A' && c <= 'Z') {
146 				if (c + ('a' - 'A') != d) {
147 					if (d >= 'a' && d <= 'z')
148 						return d - ('a' - 'A') - c;
149 					else
150 						return d - c;
151 				}
152 			} else
153 				return d - c;
154 		}
155 	}
156 	if (isofn != isoend) {
157 		isofn += isochar(isofn, isoend, joliet_level, &c, NULL, flags, handle);
158 		switch (c) {
159 		default:
160 			return -c;
161 		case '.':
162 			if (isofn != isoend) {
163 				isochar(isofn, isoend, joliet_level, &c,
164 					NULL, flags, handle);
165 				if (c == ';')
166 					return 0;
167 			}
168 			return -1;
169 		case ';':
170 			return 0;
171 		}
172 	}
173 	return 0;
174 }
175 
176 /*
177  * translate a filename of length > 0
178  */
179 void
180 isofntrans(infn, infnlen, outfn, outfnlen, original, assoc, joliet_level, flags, handle)
181 	u_char *infn;
182 	int infnlen;
183 	u_char *outfn;
184 	u_short *outfnlen;
185 	int original;
186 	int assoc;
187 	int joliet_level;
188 	int flags;
189 	void *handle;
190 {
191 	u_short c, d = '\0';
192 	u_char *outp = outfn, *infnend = infn + infnlen;
193 	int clen;
194 
195 	if (assoc) {
196 		*outp++ = ASSOCCHAR;
197 	}
198 	for (; infn != infnend; ) {
199 		infn += isochar(infn, infnend, joliet_level, &c, &clen, flags, handle);
200 
201 		if (!original && !joliet_level && c >= 'A' && c <= 'Z')
202 			c += ('a' - 'A');
203 		else if (!original && c == ';') {
204 			outp -= (d == '.');
205 			break;
206 		}
207 		d = c;
208 		while(clen--)
209 			*outp++ = c >> (clen << 3);
210 	}
211 	*outfnlen = outp - outfn;
212 }
213 
214 /*
215  * same as sgetrune(3)
216  */
217 u_short
218 sgetrune(string, n, result, flags, handle)
219 	const char *string;
220 	size_t n;
221 	char const **result;
222 	int flags;
223 	void *handle;
224 {
225 	size_t i, j, len;
226 	char outbuf[3], *outp;
227 	u_short c = '\0';
228 
229 	len = i = (n < 2) ? n : 2;
230 	j = 2;
231 	outp = outbuf;
232 
233 	if (flags & ISOFSMNT_KICONV && cd9660_iconv) {
234 		cd9660_iconv->convchr(handle, (const char **)&string,
235 			&i, &outp, &j);
236 		len -= i;
237 	} else {
238 		len = 1;
239 		string++;
240 	}
241 
242 	if (result) *result = string;
243 	while(len--) c |= (*(string - len - 1) & 0xff) << (len << 3);
244 	return (c);
245 }
246