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