2 * SPDX-License-Identifier: BSD-3-Clause
5 * The Regents of the University of California. All rights reserved.
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).
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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
37 * @(#)cd9660_util.c 8.3 (Berkeley) 12/5/94
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
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>
49 #include <fs/cd9660/iso.h>
50 #include <fs/cd9660/cd9660_mount.h>
52 extern struct iconv_functions *cd9660_iconv;
55 * Get one character out of an iso filename
57 * Return number of bytes consumed
60 isochar(isofn, isoend, joliet_level, c, clen, flags, handle)
70 char inbuf[3], outbuf[3], *inp, *outp;
74 if (joliet_level == 0 || isofn == isoend)
75 /* (00) and (01) are one byte in Joliet, too */
78 if (flags & ISOFSMNT_KICONV && cd9660_iconv) {
80 inbuf[0]=(char)*(isofn - 1);
81 inbuf[1]=(char)*isofn;
85 cd9660_iconv->convchr(handle, __DECONST(const char **, &inp), &i,
88 if (clen) *clen = len;
91 *c |= (*(outp - len - 1) & 0xff) << (len << 3);
107 * translate and compare a filename
108 * returns (fn - isofn)
109 * Note: Version number plus ';' may be omitted.
112 isofncmp(fn, fnlen, isofn, isolen, joliet_level, flags, handle, lhandle)
124 u_char *fnend = fn + fnlen, *isoend = isofn + isolen;
126 for (; fn < fnend; ) {
127 d = sgetrune(fn, fnend - fn, __DECONST(const char **, &fn),
131 isofn += isochar(isofn, isoend, joliet_level, &c, NULL, flags, handle);
135 for (i = 0; fn < fnend; i = i * 10 + *fn++ - '0') {
136 if (*fn < '0' || *fn > '9') {
140 for (j = 0; isofn != isoend; j = j * 10 + c - '0')
141 isofn += isochar(isofn, isoend,
143 NULL, flags, handle);
147 if (c >= 'A' && c <= 'Z') {
148 if (c + ('a' - 'A') != d) {
149 if (d >= 'a' && d <= 'z')
150 return d - ('a' - 'A') - c;
158 if (isofn != isoend) {
159 isofn += isochar(isofn, isoend, joliet_level, &c, NULL, flags, handle);
164 if (isofn != isoend) {
165 isochar(isofn, isoend, joliet_level, &c,
166 NULL, flags, handle);
179 * translate a filename of length > 0
182 isofntrans(infn, infnlen, outfn, outfnlen, original, assoc, joliet_level, flags, handle)
194 u_char *outp = outfn, *infnend = infn + infnlen;
200 for (; infn != infnend; ) {
201 infn += isochar(infn, infnend, joliet_level, &c, &clen, flags, handle);
203 if (!original && !joliet_level && c >= 'A' && c <= 'Z')
205 else if (!original && c == ';') {
211 *outp++ = c >> (clen << 3);
213 *outfnlen = outp - outfn;
217 * same as sgetrune(3)
220 sgetrune(string, n, result, flags, handle)
228 char outbuf[3], *outp;
231 len = i = (n < 2) ? n : 2;
235 if (flags & ISOFSMNT_KICONV && cd9660_iconv) {
236 cd9660_iconv->convchr(handle, (const char **)&string,
244 if (result) *result = string;
245 while(len--) c |= (*(string - len - 1) & 0xff) << (len << 3);