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