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 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * @(#)cd9660_util.c 8.3 (Berkeley) 12/5/94
40 */
41
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD: releng/5.2/sys/isofs/cd9660/cd9660_util.c 120492 2003-09-26 20:26:25Z fjoe $");
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/mount.h>
48 #include <sys/vnode.h>
49 #include <sys/iconv.h>
50
51 #include <isofs/cd9660/iso.h>
52 #include <isofs/cd9660/cd9660_mount.h>
53
54 extern struct iconv_functions *cd9660_iconv;
55
56 /*
57 * Get one character out of an iso filename
58 * Obey joliet_level
59 * Return number of bytes consumed
60 */
61 int
62 isochar(isofn, isoend, joliet_level, c, clen, flags, handle)
63 u_char *isofn;
64 u_char *isoend;
65 int joliet_level;
66 u_short *c;
67 int *clen;
68 int flags;
69 void *handle;
70 {
71 size_t i, j, len;
72 char inbuf[3], outbuf[3], *inp, *outp;
73
74 *c = *isofn++;
75 if (clen) *clen = 1;
76 if (joliet_level == 0 || isofn == isoend)
77 /* (00) and (01) are one byte in Joliet, too */
78 return 1;
79
80 if (flags & ISOFSMNT_KICONV && cd9660_iconv) {
81 i = j = len = 2;
82 inbuf[0]=(char)*(isofn - 1);
83 inbuf[1]=(char)*isofn;
84 inbuf[2]='\0';
85 inp = inbuf;
86 outp = outbuf;
87 cd9660_iconv->convchr(handle, (const char **)&inp, &i, &outp, &j);
88 len -= j;
89 if (clen) *clen = len;
90 *c = '\0';
91 while(len--)
92 *c |= (*(outp - len - 1) & 0xff) << (len << 3);
93 } else {
94 switch (*c) {
95 default:
96 *c = '?';
97 break;
98 case '\0':
99 *c = *isofn;
100 break;
101 }
102 }
103
104 return 2;
105 }
106
107 /*
108 * translate and compare a filename
109 * returns (fn - isofn)
110 * Note: Version number plus ';' may be omitted.
111 */
112 int
113 isofncmp(fn, fnlen, isofn, isolen, joliet_level, flags, handle, lhandle)
114 u_char *fn;
115 int fnlen;
116 u_char *isofn;
117 int isolen;
118 int joliet_level;
119 int flags;
120 void *handle;
121 void *lhandle;
122 {
123 int i, j;
124 u_short c, d;
125 u_char *fnend = fn + fnlen, *isoend = isofn + isolen;
126
127 for (; fn < fnend; ) {
128 d = sgetrune(fn, fnend - fn, (char const **)&fn, 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++ - '') {
136 if (*fn < '' || *fn > '9') {
137 return -1;
138 }
139 }
140 for (j = 0; isofn != isoend; j = j * 10 + c - '')
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 }
Cache object: 29083571251b9ad5baf1616672dffdb3
|