xref: /freebsd/sys/fs/cd9660/cd9660_util.c (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
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(u_char *isofn, u_char *isoend, int joliet_level, u_short *c, int *clen,
61     int flags, void *handle)
62 {
63       size_t i, j, len;
64       char inbuf[3], outbuf[3], *inp, *outp;
65 
66       *c = *isofn++;
67       if (clen) *clen = 1;
68       if (joliet_level == 0 || isofn == isoend)
69               /* (00) and (01) are one byte in Joliet, too */
70               return 1;
71 
72       if (flags & ISOFSMNT_KICONV && cd9660_iconv) {
73               i = j = len = 2;
74               inbuf[0]=(char)*(isofn - 1);
75               inbuf[1]=(char)*isofn;
76               inbuf[2]='\0';
77               inp = inbuf;
78               outp = outbuf;
79               cd9660_iconv->convchr(handle, __DECONST(const char **, &inp), &i,
80                   &outp, &j);
81               len -= j;
82               if (clen) *clen = len;
83               *c = '\0';
84               while(len--)
85                       *c |= (*(outp - len - 1) & 0xff) << (len << 3);
86       } else {
87               switch (*c) {
88               default:
89                       *c = '?';
90                       break;
91               case '\0':
92                       *c = *isofn;
93                       break;
94               }
95       }
96 
97       return 2;
98 }
99 
100 /*
101  * translate and compare a filename
102  * returns (fn - isofn)
103  * Note: Version number plus ';' may be omitted.
104  */
105 int
106 isofncmp(u_char *fn, int fnlen, u_char *isofn, int isolen, int joliet_level,
107     int flags, void *handle, void *lhandle)
108 {
109 	int i, j;
110 	u_short c, d;
111 	u_char *fnend = fn + fnlen, *isoend = isofn + isolen;
112 
113 	for (; fn < fnend; ) {
114 		d = sgetrune(fn, fnend - fn, __DECONST(const char **, &fn),
115 		    flags, lhandle);
116 		if (isofn == isoend)
117 			return d;
118 		isofn += isochar(isofn, isoend, joliet_level, &c, NULL, flags, handle);
119 		if (c == ';') {
120 			if (d != ';')
121 				return d;
122 			for (i = 0; fn < fnend; i = i * 10 + *fn++ - '0') {
123 				if (*fn < '0' || *fn > '9') {
124 					return -1;
125 				}
126 			}
127 			for (j = 0; isofn != isoend; j = j * 10 + c - '0')
128 				isofn += isochar(isofn, isoend,
129 						 joliet_level, &c,
130 						 NULL, flags, handle);
131 			return i - j;
132 		}
133 		if (c != d) {
134 			if (c >= 'A' && c <= 'Z') {
135 				if (c + ('a' - 'A') != d) {
136 					if (d >= 'a' && d <= 'z')
137 						return d - ('a' - 'A') - c;
138 					else
139 						return d - c;
140 				}
141 			} else
142 				return d - c;
143 		}
144 	}
145 	if (isofn != isoend) {
146 		isofn += isochar(isofn, isoend, joliet_level, &c, NULL, flags, handle);
147 		switch (c) {
148 		default:
149 			return -c;
150 		case '.':
151 			if (isofn != isoend) {
152 				isochar(isofn, isoend, joliet_level, &c,
153 					NULL, flags, handle);
154 				if (c == ';')
155 					return 0;
156 			}
157 			return -1;
158 		case ';':
159 			return 0;
160 		}
161 	}
162 	return 0;
163 }
164 
165 /*
166  * translate a filename of length > 0
167  */
168 void
169 isofntrans(u_char *infn, int infnlen, u_char *outfn, u_short *outfnlen,
170     int original, int assoc, int joliet_level, int flags, void *handle)
171 {
172 	u_short c, d = '\0';
173 	u_char *outp = outfn, *infnend = infn + infnlen;
174 	int clen;
175 
176 	if (assoc) {
177 		*outp++ = ASSOCCHAR;
178 	}
179 	for (; infn != infnend; ) {
180 		infn += isochar(infn, infnend, joliet_level, &c, &clen, flags, handle);
181 
182 		if (!original && !joliet_level && c >= 'A' && c <= 'Z')
183 			c += ('a' - 'A');
184 		else if (!original && c == ';') {
185 			outp -= (d == '.');
186 			break;
187 		}
188 		d = c;
189 		while(clen--)
190 			*outp++ = c >> (clen << 3);
191 	}
192 	*outfnlen = outp - outfn;
193 }
194 
195 /*
196  * same as sgetrune(3)
197  */
198 u_short
199 sgetrune(const char *string, size_t n, char const **result, int flags,
200     void *handle)
201 {
202 	size_t i, j, len;
203 	char outbuf[3], *outp;
204 	u_short c = '\0';
205 
206 	len = i = (n < 2) ? n : 2;
207 	j = 2;
208 	outp = outbuf;
209 
210 	if (flags & ISOFSMNT_KICONV && cd9660_iconv) {
211 		cd9660_iconv->convchr(handle, (const char **)&string,
212 			&i, &outp, &j);
213 		len -= i;
214 	} else {
215 		len = 1;
216 		string++;
217 	}
218 
219 	if (result) *result = string;
220 	while(len--) c |= (*(string - len - 1) & 0xff) << (len << 3);
221 	return (c);
222 }
223