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