cd9660_util.c revision 1.16 1 1.16 tsutsui /* $NetBSD: cd9660_util.c,v 1.16 2024/05/25 06:27:57 tsutsui Exp $ */
2 1.1 jdolecek
3 1.1 jdolecek /*-
4 1.1 jdolecek * Copyright (c) 1994
5 1.1 jdolecek * The Regents of the University of California. All rights reserved.
6 1.1 jdolecek *
7 1.1 jdolecek * This code is derived from software contributed to Berkeley
8 1.1 jdolecek * by Pace Willisson (pace (at) blitz.com). The Rock Ridge Extension
9 1.1 jdolecek * Support code is derived from software contributed to Berkeley
10 1.1 jdolecek * by Atsushi Murai (amurai (at) spec.co.jp).
11 1.1 jdolecek *
12 1.1 jdolecek * Redistribution and use in source and binary forms, with or without
13 1.1 jdolecek * modification, are permitted provided that the following conditions
14 1.1 jdolecek * are met:
15 1.1 jdolecek * 1. Redistributions of source code must retain the above copyright
16 1.1 jdolecek * notice, this list of conditions and the following disclaimer.
17 1.1 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 jdolecek * notice, this list of conditions and the following disclaimer in the
19 1.1 jdolecek * documentation and/or other materials provided with the distribution.
20 1.2 agc * 3. Neither the name of the University nor the names of its contributors
21 1.1 jdolecek * may be used to endorse or promote products derived from this software
22 1.1 jdolecek * without specific prior written permission.
23 1.1 jdolecek *
24 1.1 jdolecek * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 jdolecek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 jdolecek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 jdolecek * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 jdolecek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 jdolecek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 jdolecek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 jdolecek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 jdolecek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 jdolecek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 jdolecek * SUCH DAMAGE.
35 1.1 jdolecek *
36 1.1 jdolecek * @(#)cd9660_util.c 8.3 (Berkeley) 12/5/94
37 1.1 jdolecek */
38 1.1 jdolecek
39 1.1 jdolecek #include <sys/cdefs.h>
40 1.13 christos #ifdef _KERNEL
41 1.16 tsutsui __KERNEL_RCSID(0, "$NetBSD: cd9660_util.c,v 1.16 2024/05/25 06:27:57 tsutsui Exp $");
42 1.13 christos #else
43 1.13 christos /* used by macppc_installboot */
44 1.13 christos #if HAVE_NBTOOL_CONFIG_H
45 1.13 christos #include "nbtool_config.h"
46 1.13 christos #endif
47 1.13 christos #endif
48 1.1 jdolecek
49 1.1 jdolecek #include <sys/param.h>
50 1.13 christos #ifdef _KERNEL
51 1.1 jdolecek #include <sys/systm.h>
52 1.1 jdolecek #include <sys/namei.h>
53 1.1 jdolecek #include <sys/resourcevar.h>
54 1.1 jdolecek #include <sys/kernel.h>
55 1.1 jdolecek #include <sys/file.h>
56 1.1 jdolecek #include <sys/stat.h>
57 1.1 jdolecek #include <sys/buf.h>
58 1.1 jdolecek #include <sys/proc.h>
59 1.1 jdolecek #include <sys/mount.h>
60 1.1 jdolecek #include <sys/vnode.h>
61 1.1 jdolecek #include <sys/dirent.h>
62 1.13 christos #else
63 1.13 christos #include <assert.h>
64 1.13 christos #include <dirent.h>
65 1.13 christos #define KASSERT(x) assert(x) /* XXX for <fs/unicode.h> */
66 1.16 tsutsui
67 1.16 tsutsui #if !HAVE_NBTOOL_CONFIG_H || HAVE_SYS_ENDIAN_H
68 1.16 tsutsui #include <sys/endian.h> /* for le16dec(9) etc. in iso.h */
69 1.16 tsutsui #endif
70 1.13 christos #endif
71 1.1 jdolecek
72 1.1 jdolecek #include <fs/cd9660/iso.h>
73 1.1 jdolecek #include <fs/cd9660/cd9660_extern.h>
74 1.1 jdolecek
75 1.3 jdolecek #include <fs/unicode.h>
76 1.3 jdolecek
77 1.13 christos static uint16_t wget(const u_char **, size_t *, int);
78 1.13 christos static int wput(u_char *, size_t, uint16_t, int);
79 1.3 jdolecek
80 1.3 jdolecek int cd9660_utf8_joliet = 1;
81 1.3 jdolecek
82 1.1 jdolecek /*
83 1.1 jdolecek * Get one character out of an iso filename
84 1.1 jdolecek * Return number of bytes consumed
85 1.1 jdolecek */
86 1.1 jdolecek int
87 1.9 matt isochar(const u_char *isofn, const u_char *isoend, int joliet_level,
88 1.13 christos uint16_t *c)
89 1.1 jdolecek {
90 1.13 christos
91 1.3 jdolecek *c = isofn[0];
92 1.3 jdolecek if (joliet_level == 0 || isofn + 1 == isoend) {
93 1.1 jdolecek /* (00) and (01) are one byte in Joliet, too */
94 1.1 jdolecek return 1;
95 1.3 jdolecek }
96 1.1 jdolecek
97 1.3 jdolecek if (cd9660_utf8_joliet) {
98 1.3 jdolecek *c = (*c << 8) + isofn[1];
99 1.3 jdolecek } else {
100 1.3 jdolecek /* characters outside ISO-8859-1 subset replaced with '?' */
101 1.3 jdolecek if (*c != 0)
102 1.3 jdolecek *c = '?';
103 1.3 jdolecek else
104 1.3 jdolecek *c = isofn[1];
105 1.1 jdolecek }
106 1.3 jdolecek
107 1.1 jdolecek return 2;
108 1.1 jdolecek }
109 1.1 jdolecek
110 1.1 jdolecek /*
111 1.1 jdolecek * translate and compare a filename
112 1.1 jdolecek * Note: Version number plus ';' may be omitted.
113 1.1 jdolecek */
114 1.1 jdolecek int
115 1.9 matt isofncmp(const u_char *fn, size_t fnlen, const u_char *isofn, size_t isolen,
116 1.13 christos int joliet_level)
117 1.1 jdolecek {
118 1.1 jdolecek int i, j;
119 1.13 christos uint16_t fc, ic;
120 1.1 jdolecek const u_char *isoend = isofn + isolen;
121 1.1 jdolecek
122 1.13 christos #ifdef ISOFNCMPDEBUG
123 1.13 christos printf("fn = %s, fnlen = %zu, isofn = %s, isolen = %zu\n",
124 1.13 christos fn, fnlen, isofn, isolen);
125 1.13 christos #endif
126 1.13 christos
127 1.4 jdolecek while (fnlen > 0) {
128 1.4 jdolecek fc = wget(&fn, &fnlen, joliet_level);
129 1.3 jdolecek
130 1.1 jdolecek if (isofn == isoend)
131 1.3 jdolecek return fc;
132 1.3 jdolecek isofn += isochar(isofn, isoend, joliet_level, &ic);
133 1.3 jdolecek if (ic == ';') {
134 1.3 jdolecek switch (fc) {
135 1.1 jdolecek default:
136 1.3 jdolecek return fc;
137 1.1 jdolecek case 0:
138 1.1 jdolecek return 0;
139 1.1 jdolecek case ';':
140 1.1 jdolecek break;
141 1.1 jdolecek }
142 1.7 christos for (i = 0; fnlen-- != 0; i = i * 10 + *fn++ - '0') {
143 1.1 jdolecek if (*fn < '0' || *fn > '9') {
144 1.1 jdolecek return -1;
145 1.1 jdolecek }
146 1.1 jdolecek }
147 1.3 jdolecek for (j = 0; isofn != isoend; j = j * 10 + ic - '0')
148 1.1 jdolecek isofn += isochar(isofn, isoend,
149 1.3 jdolecek joliet_level, &ic);
150 1.1 jdolecek return i - j;
151 1.1 jdolecek }
152 1.3 jdolecek if (ic != fc) {
153 1.3 jdolecek if (ic >= 'A' && ic <= 'Z') {
154 1.3 jdolecek if (ic + ('a' - 'A') != fc) {
155 1.3 jdolecek if (fc >= 'a' && fc <= 'z')
156 1.3 jdolecek fc -= 'a' - 'A';
157 1.3 jdolecek
158 1.13 christos return (int)fc - (int)ic;
159 1.1 jdolecek }
160 1.1 jdolecek } else
161 1.13 christos return (int)fc - (int)ic;
162 1.1 jdolecek }
163 1.1 jdolecek }
164 1.1 jdolecek if (isofn != isoend) {
165 1.3 jdolecek isofn += isochar(isofn, isoend, joliet_level, &ic);
166 1.3 jdolecek switch (ic) {
167 1.1 jdolecek default:
168 1.1 jdolecek return -1;
169 1.1 jdolecek case '.':
170 1.1 jdolecek if (isofn != isoend) {
171 1.3 jdolecek isochar(isofn, isoend, joliet_level, &ic);
172 1.3 jdolecek if (ic == ';')
173 1.1 jdolecek return 0;
174 1.1 jdolecek }
175 1.1 jdolecek return -1;
176 1.1 jdolecek case ';':
177 1.1 jdolecek return 0;
178 1.1 jdolecek }
179 1.1 jdolecek }
180 1.1 jdolecek return 0;
181 1.1 jdolecek }
182 1.1 jdolecek
183 1.1 jdolecek /*
184 1.1 jdolecek * translate a filename
185 1.1 jdolecek */
186 1.1 jdolecek void
187 1.9 matt isofntrans(const u_char *infn, int infnlen, u_char *outfn, u_short *outfnlen,
188 1.13 christos int original, int casetrans, int assoc, int joliet_level)
189 1.1 jdolecek {
190 1.1 jdolecek int fnidx = 0;
191 1.9 matt const u_char *infnend = infn + infnlen;
192 1.13 christos uint16_t c;
193 1.3 jdolecek int sz;
194 1.3 jdolecek
195 1.1 jdolecek if (assoc) {
196 1.1 jdolecek *outfn++ = ASSOCCHAR;
197 1.1 jdolecek fnidx++;
198 1.1 jdolecek }
199 1.1 jdolecek
200 1.3 jdolecek for(; infn != infnend; fnidx += sz) {
201 1.1 jdolecek infn += isochar(infn, infnend, joliet_level, &c);
202 1.1 jdolecek
203 1.1 jdolecek if (casetrans && joliet_level == 0 && c >= 'A' && c <= 'Z')
204 1.3 jdolecek c = c + ('a' - 'A');
205 1.1 jdolecek else if (!original && c == ';') {
206 1.1 jdolecek if (fnidx > 0 && outfn[-1] == '.')
207 1.1 jdolecek fnidx--;
208 1.1 jdolecek break;
209 1.3 jdolecek }
210 1.3 jdolecek
211 1.10 christos sz = wput(outfn, ISO_MAXNAMLEN - fnidx, c, joliet_level);
212 1.3 jdolecek if (sz == 0) {
213 1.3 jdolecek /* not enough space to write the character */
214 1.10 christos if (fnidx < ISO_MAXNAMLEN) {
215 1.3 jdolecek *outfn = '?';
216 1.3 jdolecek fnidx++;
217 1.3 jdolecek }
218 1.3 jdolecek break;
219 1.3 jdolecek }
220 1.3 jdolecek outfn += sz;
221 1.1 jdolecek }
222 1.1 jdolecek *outfnlen = fnidx;
223 1.1 jdolecek }
224 1.3 jdolecek
225 1.13 christos static uint16_t
226 1.4 jdolecek wget(const u_char **str, size_t *sz, int joliet_level)
227 1.3 jdolecek {
228 1.3 jdolecek if (joliet_level > 0 && cd9660_utf8_joliet) {
229 1.3 jdolecek /* decode UTF-8 sequence */
230 1.4 jdolecek return wget_utf8((const char **) str, sz);
231 1.3 jdolecek } else {
232 1.3 jdolecek /*
233 1.3 jdolecek * Raw 8-bit characters without any conversion. For Joliet,
234 1.3 jdolecek * this effectively assumes provided file name is using
235 1.3 jdolecek * ISO-8859-1 subset.
236 1.3 jdolecek */
237 1.13 christos uint16_t c = *str[0];
238 1.3 jdolecek (*str)++;
239 1.8 enami (*sz)--;
240 1.3 jdolecek
241 1.3 jdolecek return c;
242 1.3 jdolecek }
243 1.3 jdolecek }
244 1.3 jdolecek
245 1.3 jdolecek static int
246 1.13 christos wput(u_char *s, size_t n, uint16_t c, int joliet_level)
247 1.3 jdolecek {
248 1.3 jdolecek if (joliet_level > 0 && cd9660_utf8_joliet) {
249 1.3 jdolecek /* Store Joliet file name encoded into UTF-8 */
250 1.3 jdolecek return wput_utf8((char *)s, n, c);
251 1.3 jdolecek } else {
252 1.3 jdolecek /*
253 1.3 jdolecek * Store raw 8-bit characters without any conversion.
254 1.3 jdolecek * For Joliet case, this filters the Unicode characters
255 1.3 jdolecek * to ISO-8859-1 subset.
256 1.3 jdolecek */
257 1.3 jdolecek *s = (u_char)c;
258 1.3 jdolecek return 1;
259 1.3 jdolecek }
260 1.3 jdolecek }
261