1 /* $OpenBSD: cd9660_util.c,v 1.10 2017/12/30 20:47:00 guenther 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 70 isochar(isofn, isoend, joliet_level, c) 71 const u_char *isofn; 72 const u_char *isoend; 73 int joliet_level; 74 u_char *c; 75 { 76 *c = *isofn++; 77 if (joliet_level == 0 || isofn == isoend) 78 /* (00) and (01) are one byte in Joliet, too */ 79 return 1; 80 81 /* No Unicode support yet :-( */ 82 switch (*c) { 83 default: 84 *c = '?'; 85 break; 86 case '\0': 87 *c = *isofn; 88 break; 89 } 90 91 /* XXX: if Unicode conversion routine is loaded then use it */ 92 if (cd9660_wchar2char != NULL) 93 *c = cd9660_wchar2char((*(isofn - 1) << 8) | *isofn); 94 95 return 2; 96 } 97 98 /* 99 * translate and compare a filename 100 * returns (fn - isofn) 101 * Note: Version number plus ';' may be omitted. 102 */ 103 int 104 isofncmp(fn, fnlen, isofn, isolen, joliet_level) 105 const u_char *fn, *isofn; 106 int fnlen, isolen, joliet_level; 107 { 108 int i, j; 109 u_char c; 110 const u_char *fnend = fn + fnlen, *isoend = isofn + isolen; 111 112 for (; fn != fnend; fn++) { 113 if (isofn == isoend) 114 return *fn; 115 isofn += isochar(isofn, isoend, joliet_level, &c); 116 if (c == ';') { 117 if (*fn++ != ';') 118 return fn[-1]; 119 for (i = 0; fn != fnend; i = i * 10 + *fn++ - '0') { 120 if (*fn < '0' || *fn > '9') { 121 return -1; 122 } 123 } 124 for (j = 0; isofn != isoend; j = j * 10 + c - '0') 125 isofn += isochar(isofn, isoend, 126 joliet_level, &c); 127 return i - j; 128 } 129 if (((u_char) c) != *fn) { 130 if (c >= 'A' && c <= 'Z') { 131 if (c + ('a' - 'A') != *fn) { 132 if (*fn >= 'a' && *fn <= 'z') 133 return *fn - ('a' - 'A') - c; 134 else 135 return *fn - c; 136 } 137 } else 138 return *fn - c; 139 } 140 } 141 if (isofn != isoend) { 142 isofn += isochar(isofn, isoend, joliet_level, &c); 143 switch (c) { 144 default: 145 return -c; 146 case '.': 147 if (isofn != isoend) { 148 isochar(isofn, isoend, joliet_level, &c); 149 if (c == ';') 150 return 0; 151 } 152 return -1; 153 case ';': 154 return 0; 155 } 156 } 157 return 0; 158 } 159 160 /* 161 * translate a filename of length > 0 162 */ 163 void 164 isofntrans(infn, infnlen, outfn, outfnlen, original, assoc, joliet_level) 165 u_char *infn, *outfn; 166 int infnlen; 167 u_short *outfnlen; 168 int original; 169 int assoc; 170 int joliet_level; 171 { 172 int fnidx = 0; 173 u_char c, d = '\0', *infnend = infn + infnlen; 174 175 if (assoc) { 176 *outfn++ = ASSOCCHAR; 177 fnidx++; 178 } 179 for (; infn != infnend; fnidx++) { 180 infn += isochar(infn, infnend, joliet_level, &c); 181 182 if (!original && c == ';') { 183 fnidx -= (d == '.'); 184 break; 185 } else 186 *outfn++ = c; 187 d = c; 188 } 189 *outfnlen = fnidx; 190 } 191