1 /* $OpenBSD: cd9660_util.c,v 1.7 2003/11/04 21:54:01 mickey 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/file.h> 47 #include <sys/stat.h> 48 #include <sys/buf.h> 49 #include <sys/proc.h> 50 #include <sys/conf.h> 51 #include <sys/mount.h> 52 #include <sys/vnode.h> 53 #include <sys/malloc.h> 54 #include <sys/dirent.h> 55 56 #include <isofs/cd9660/iso.h> 57 #include <isofs/cd9660/cd9660_extern.h> 58 59 /* 60 * XXX: limited support for loading of Unicode 61 * conversion routine as a kld at a run-time. 62 * Should be removed when native Unicode kernel 63 * interfaces have been introduced. 64 */ 65 u_char (*cd9660_wchar2char)(u_int32_t wchar) = NULL; 66 67 /* 68 * Get one character out of an iso filename 69 * Obey joliet_level 70 * Return number of bytes consumed 71 */ 72 int 73 isochar(isofn, isoend, joliet_level, c) 74 const u_char *isofn; 75 const u_char *isoend; 76 int joliet_level; 77 u_char *c; 78 { 79 *c = *isofn++; 80 if (joliet_level == 0 || isofn == isoend) 81 /* (00) and (01) are one byte in Joliet, too */ 82 return 1; 83 84 /* No Unicode support yet :-( */ 85 switch (*c) { 86 default: 87 *c = '?'; 88 break; 89 case '\0': 90 *c = *isofn; 91 break; 92 } 93 94 /* XXX: if Unicode conversion routine is loaded then use it */ 95 if (cd9660_wchar2char != NULL) 96 *c = cd9660_wchar2char((*(isofn - 1) << 8) | *isofn); 97 98 return 2; 99 } 100 101 /* 102 * translate and compare a filename 103 * returns (fn - isofn) 104 * Note: Version number plus ';' may be omitted. 105 */ 106 int 107 isofncmp(fn, fnlen, isofn, isolen, joliet_level) 108 const u_char *fn, *isofn; 109 int fnlen, isolen, joliet_level; 110 { 111 int i, j; 112 u_char c; 113 const u_char *fnend = fn + fnlen, *isoend = isofn + isolen; 114 115 for (; fn != fnend; fn++) { 116 if (isofn == isoend) 117 return *fn; 118 isofn += isochar(isofn, isoend, joliet_level, &c); 119 if (c == ';') { 120 if (*fn++ != ';') 121 return fn[-1]; 122 for (i = 0; fn != fnend; i = i * 10 + *fn++ - '0') { 123 if (*fn < '0' || *fn > '9') { 124 return -1; 125 } 126 } 127 for (j = 0; isofn != isoend; j = j * 10 + c - '0') 128 isofn += isochar(isofn, isoend, 129 joliet_level, &c); 130 return i - j; 131 } 132 if (((u_char) c) != *fn) { 133 if (c >= 'A' && c <= 'Z') { 134 if (c + ('a' - 'A') != *fn) { 135 if (*fn >= 'a' && *fn <= 'z') 136 return *fn - ('a' - 'A') - c; 137 else 138 return *fn - c; 139 } 140 } else 141 return *fn - c; 142 } 143 } 144 if (isofn != isoend) { 145 isofn += isochar(isofn, isoend, joliet_level, &c); 146 switch (c) { 147 default: 148 return -c; 149 case '.': 150 if (isofn != isoend) { 151 isochar(isofn, isoend, joliet_level, &c); 152 if (c == ';') 153 return 0; 154 } 155 return -1; 156 case ';': 157 return 0; 158 } 159 } 160 return 0; 161 } 162 163 /* 164 * translate a filename of length > 0 165 */ 166 void 167 isofntrans(infn, infnlen, outfn, outfnlen, original, assoc, joliet_level) 168 u_char *infn, *outfn; 169 int infnlen; 170 u_short *outfnlen; 171 int original; 172 int assoc; 173 int joliet_level; 174 { 175 int fnidx = 0; 176 u_char c, d = '\0', *infnend = infn + infnlen; 177 178 if (assoc) { 179 *outfn++ = ASSOCCHAR; 180 fnidx++; 181 } 182 for (; infn != infnend; fnidx++) { 183 infn += isochar(infn, infnend, joliet_level, &c); 184 185 if (!original && c == ';') { 186 fnidx -= (d == '.'); 187 break; 188 } else 189 *outfn++ = c; 190 d = c; 191 } 192 *outfnlen = fnidx; 193 } 194