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