1 /* $OpenBSD: cd9660_util.c,v 1.11 2021/03/05 07:01:36 jsg Exp $ */
2 /* $NetBSD: cd9660_util.c,v 1.12 1997/01/24 00:27:33 cgd Exp $ */
3
4 /*-
5 * Copyright (c) 1994
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley
9 * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension
10 * Support code is derived from software contributed to Berkeley
11 * by Atsushi Murai (amurai@spec.co.jp). Joliet support was added by
12 * Joachim Kuebart (joki@kuebart.stuttgart.netsurf.de).
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)cd9660_util.c 8.3 (Berkeley) 12/5/94
39 */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/namei.h>
44 #include <sys/resourcevar.h>
45 #include <sys/kernel.h>
46 #include <sys/stat.h>
47 #include <sys/buf.h>
48 #include <sys/conf.h>
49 #include <sys/mount.h>
50 #include <sys/vnode.h>
51 #include <sys/malloc.h>
52 #include <sys/dirent.h>
53
54 #include <isofs/cd9660/iso.h>
55
56 /*
57 * XXX: limited support for loading of Unicode
58 * conversion routine as a kld at a run-time.
59 * Should be removed when native Unicode kernel
60 * interfaces have been introduced.
61 */
62 u_char (*cd9660_wchar2char)(u_int32_t wchar) = NULL;
63
64 /*
65 * Get one character out of an iso filename
66 * Obey joliet_level
67 * Return number of bytes consumed
68 */
69 int
isochar(const u_char * isofn,const u_char * isoend,int joliet_level,u_char * c)70 isochar(const u_char *isofn, const u_char *isoend, int joliet_level, u_char *c)
71 {
72 *c = *isofn++;
73 if (joliet_level == 0 || isofn == isoend)
74 /* (00) and (01) are one byte in Joliet, too */
75 return 1;
76
77 /* No Unicode support yet :-( */
78 switch (*c) {
79 default:
80 *c = '?';
81 break;
82 case '\0':
83 *c = *isofn;
84 break;
85 }
86
87 /* XXX: if Unicode conversion routine is loaded then use it */
88 if (cd9660_wchar2char != NULL)
89 *c = cd9660_wchar2char((*(isofn - 1) << 8) | *isofn);
90
91 return 2;
92 }
93
94 /*
95 * translate and compare a filename
96 * returns (fn - isofn)
97 * Note: Version number plus ';' may be omitted.
98 */
99 int
isofncmp(const u_char * fn,int fnlen,const u_char * isofn,int isolen,int joliet_level)100 isofncmp(const u_char *fn, int fnlen, const u_char *isofn, int isolen,
101 int joliet_level)
102 {
103 int i, j;
104 u_char c;
105 const u_char *fnend = fn + fnlen, *isoend = isofn + isolen;
106
107 for (; fn != fnend; fn++) {
108 if (isofn == isoend)
109 return *fn;
110 isofn += isochar(isofn, isoend, joliet_level, &c);
111 if (c == ';') {
112 if (*fn++ != ';')
113 return fn[-1];
114 for (i = 0; fn != fnend; i = i * 10 + *fn++ - '0') {
115 if (*fn < '0' || *fn > '9') {
116 return -1;
117 }
118 }
119 for (j = 0; isofn != isoend; j = j * 10 + c - '0')
120 isofn += isochar(isofn, isoend,
121 joliet_level, &c);
122 return i - j;
123 }
124 if (((u_char) c) != *fn) {
125 if (c >= 'A' && c <= 'Z') {
126 if (c + ('a' - 'A') != *fn) {
127 if (*fn >= 'a' && *fn <= 'z')
128 return *fn - ('a' - 'A') - c;
129 else
130 return *fn - c;
131 }
132 } else
133 return *fn - c;
134 }
135 }
136 if (isofn != isoend) {
137 isofn += isochar(isofn, isoend, joliet_level, &c);
138 switch (c) {
139 default:
140 return -c;
141 case '.':
142 if (isofn != isoend) {
143 isochar(isofn, isoend, joliet_level, &c);
144 if (c == ';')
145 return 0;
146 }
147 return -1;
148 case ';':
149 return 0;
150 }
151 }
152 return 0;
153 }
154
155 /*
156 * translate a filename of length > 0
157 */
158 void
isofntrans(u_char * infn,int infnlen,u_char * outfn,u_short * outfnlen,int original,int assoc,int joliet_level)159 isofntrans(u_char *infn, int infnlen, u_char *outfn, u_short *outfnlen,
160 int original, int assoc, int joliet_level)
161 {
162 int fnidx = 0;
163 u_char c, d = '\0', *infnend = infn + infnlen;
164
165 if (assoc) {
166 *outfn++ = ASSOCCHAR;
167 fnidx++;
168 }
169 for (; infn != infnend; fnidx++) {
170 infn += isochar(infn, infnend, joliet_level, &c);
171
172 if (!original && c == ';') {
173 fnidx -= (d == '.');
174 break;
175 } else
176 *outfn++ = c;
177 d = c;
178 }
179 *outfnlen = fnidx;
180 }
181