1 /*- 2 * Copyright (c) 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley 6 * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension 7 * Support code is derived from software contributed to Berkeley 8 * by Atsushi Murai (amurai@spec.co.jp). Joliet support was added by 9 * Joachim Kuebart (joki@kuebart.stuttgart.netsurf.de). 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * @(#)cd9660_util.c 8.3 (Berkeley) 12/5/94 40 * $FreeBSD: src/sys/isofs/cd9660/cd9660_util.c,v 1.13.2.1 2001/02/27 12:36:34 sobomax Exp $ 41 * $DragonFly: src/sys/vfs/isofs/cd9660/cd9660_util.c,v 1.4 2004/04/12 23:18:55 cpressey Exp $ 42 */ 43 44 #include <sys/param.h> 45 #include <sys/mount.h> 46 #include <sys/vnode.h> 47 #include <sys/iconv.h> 48 49 #include "iso.h" 50 #include "cd9660_mount.h" 51 52 extern struct iconv_functions *cd9660_iconv; 53 54 /* 55 * Get one character out of an iso filename 56 * Obey joliet_level 57 * Return number of bytes consumed 58 */ 59 int 60 isochar(u_char *isofn, u_char *isoend, int joliet_level, u_short *c, 61 int *clen, int flags, void *handle) 62 { 63 size_t i, j, len; 64 char inbuf[3], outbuf[3], *inp, *outp; 65 *c = *isofn++; 66 if (clen) *clen = 1; 67 if (joliet_level == 0 || isofn == isoend) 68 /* (00) and (01) are one byte in Joliet, too */ 69 return 1; 70 if (flags & ISOFSMNT_KICONV && cd9660_iconv) { 71 i = j = len = 2; 72 inbuf[0]=(char)*(isofn - 1); 73 inbuf[1]=(char)*isofn; 74 inbuf[2]='\0'; 75 inp = inbuf; 76 outp = outbuf; 77 cd9660_iconv->convchr(handle, (const char **)&inp, &i, &outp, &j); 78 len -= j; 79 if (clen) *clen = len; 80 *c = '\0'; 81 while(len--) 82 *c |= (*(outp - len - 1) & 0xff) << (len << 3); 83 } else { 84 switch (*c) { 85 default: 86 *c = '?'; 87 break; 88 case '\0': 89 *c = *isofn; 90 break; 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 for (; fn < fnend; ) { 109 d = sgetrune(fn, fnend - fn, (char const **)&fn, flags, lhandle) 110 ; 111 if (isofn == isoend) 112 return d; 113 isofn += isochar(isofn, isoend, joliet_level, &c, NULL, flags, handle); 114 if (c == ';') { 115 if (d != ';') 116 return d; 117 for (i = 0; fn < fnend; i = i * 10 + *fn++ - '0') { 118 if (*fn < '0' || *fn > '9') { 119 return -1; 120 } 121 } 122 for (j = 0; isofn != isoend; j = j * 10 + c - '0') 123 isofn += isochar(isofn, isoend, 124 joliet_level, &c, 125 NULL, flags, handle); 126 return i - j; 127 } 128 if (c != d) { 129 if (c >= 'A' && c <= 'Z') { 130 if (c + ('a' - 'A') != d) { 131 if (d >= 'a' && d <= 'z') 132 return d - ('a' - 'A') - c; 133 else 134 return d - c; 135 } 136 } else 137 return d - c; 138 } 139 } 140 if (isofn != isoend) { 141 isofn += isochar(isofn, isoend, joliet_level, &c, NULL, flags, handle); 142 switch (c) { 143 default: 144 return -c; 145 case '.': 146 if (isofn != isoend) { 147 isochar(isofn, isoend, joliet_level, &c, 148 NULL, flags, handle); 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(u_char *infn, int infnlen, u_char *outfn, u_short *outfnlen, int original, 165 int assoc, int joliet_level, int flags, void *handle) 166 { 167 u_short c, d = '\0'; 168 u_char *outp = outfn, *infnend = infn + infnlen; 169 int clen; 170 if (assoc) { 171 *outp++ = ASSOCCHAR; 172 } 173 for (; infn != infnend; ) { 174 infn += isochar(infn, infnend, joliet_level, &c, &clen, flags, handle); 175 if (!original && !joliet_level && c >= 'A' && c <= 'Z') 176 c += ('a' - 'A'); 177 else if (!original && c == ';') { 178 outp -= (d == '.'); 179 break; 180 } 181 d = c; 182 while(clen--) 183 *outp++ = c >> (clen << 3); 184 } 185 *outfnlen = outp - outfn; 186 } 187 188 /* 189 * same as sgetrune(3) 190 */ 191 u_short 192 sgetrune(const char *string, size_t n, char const **result, 193 int flags, void *handle) 194 { 195 size_t i, j, len; 196 char outbuf[3], *outp; 197 u_short c = '\0'; 198 len = i = (n < 2) ? n : 2; 199 j = 2; 200 outp = outbuf; 201 if (flags & ISOFSMNT_KICONV && cd9660_iconv) { 202 cd9660_iconv->convchr(handle, (const char **)&string, 203 &i, &outp, &j); 204 len -= i; 205 } else { 206 len = 1; 207 string++; 208 } 209 if (result) *result = string; 210 while(len--) c |= (*(string - len - 1) & 0xff) << (len << 3); 211 return (c); 212 } 213