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. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)cd9660_util.c 8.3 (Berkeley) 12/5/94 36 * $FreeBSD: src/sys/isofs/cd9660/cd9660_util.c,v 1.13.2.1 2001/02/27 12:36:34 sobomax Exp $ 37 */ 38 39 #include <sys/param.h> 40 #include <sys/mount.h> 41 #include <sys/vnode.h> 42 #include <sys/iconv.h> 43 44 #include "iso.h" 45 #include "cd9660_mount.h" 46 47 extern struct iconv_functions *cd9660_iconv; 48 49 /* 50 * Get one character out of an iso filename 51 * Obey joliet_level 52 * Return number of bytes consumed 53 */ 54 int 55 isochar(u_char *isofn, u_char *isoend, int joliet_level, u_short *c, 56 int *clen, int flags, void *handle) 57 { 58 size_t i, j, len; 59 char inbuf[3], outbuf[3], *inp, *outp; 60 *c = *isofn++; 61 if (clen) *clen = 1; 62 if (joliet_level == 0 || isofn == isoend) 63 /* (00) and (01) are one byte in Joliet, too */ 64 return 1; 65 if (flags & ISOFSMNT_KICONV && cd9660_iconv) { 66 i = j = len = 2; 67 inbuf[0]=(char)*(isofn - 1); 68 inbuf[1]=(char)*isofn; 69 inbuf[2]='\0'; 70 inp = inbuf; 71 outp = outbuf; 72 cd9660_iconv->convchr(handle, (const char **)(void *)&inp, 73 &i, &outp, &j); 74 len -= j; 75 if (clen) *clen = len; 76 *c = '\0'; 77 while(len--) 78 *c |= (*(outp - len - 1) & 0xff) << (len << 3); 79 } else { 80 switch (*c) { 81 default: 82 *c = '?'; 83 break; 84 case '\0': 85 *c = *isofn; 86 break; 87 } 88 } 89 return 2; 90 } 91 92 /* 93 * translate and compare a filename 94 * returns (fn - isofn) 95 * Note: Version number plus ';' may be omitted. 96 */ 97 int 98 isofncmp(u_char *fn, int fnlen, u_char *isofn, int isolen, int joliet_level, 99 int flags, void *handle, void *lhandle) 100 { 101 int i, j; 102 u_short c, d; 103 u_char *fnend = fn + fnlen, *isoend = isofn + isolen; 104 for (; fn < fnend; ) { 105 d = sgetrune(fn, fnend - fn, (char const **)&fn, flags, lhandle); 106 if (isofn == isoend) 107 return d; 108 isofn += isochar(isofn, isoend, joliet_level, &c, NULL, flags, handle); 109 if (c == ';') { 110 if (d != ';') 111 return d; 112 for (i = 0; fn < fnend; i = i * 10 + *fn++ - '0') { 113 if (*fn < '0' || *fn > '9') { 114 return -1; 115 } 116 } 117 for (j = 0; isofn != isoend; j = j * 10 + c - '0') 118 isofn += isochar(isofn, isoend, 119 joliet_level, &c, 120 NULL, flags, handle); 121 return i - j; 122 } 123 if (c != d) { 124 if (c >= 'A' && c <= 'Z') { 125 if (c + ('a' - 'A') != d) { 126 if (d >= 'a' && d <= 'z') 127 return d - ('a' - 'A') - c; 128 else 129 return d - c; 130 } 131 } else 132 return d - c; 133 } 134 } 135 if (isofn != isoend) { 136 isofn += isochar(isofn, isoend, joliet_level, &c, NULL, flags, handle); 137 switch (c) { 138 default: 139 return -c; 140 case '.': 141 if (isofn != isoend) { 142 isochar(isofn, isoend, joliet_level, &c, 143 NULL, flags, handle); 144 if (c == ';') 145 return 0; 146 } 147 return -1; 148 case ';': 149 return 0; 150 } 151 } 152 return 0; 153 } 154 155 /* 156 * translate a filename of length > 0 157 */ 158 void 159 isofntrans(u_char *infn, int infnlen, u_char *outfn, u_short *outfnlen, int original, 160 int assoc, int joliet_level, int flags, void *handle) 161 { 162 u_short c, d = '\0'; 163 u_char *outp = outfn, *infnend = infn + infnlen; 164 int clen; 165 if (assoc) { 166 *outp++ = ASSOCCHAR; 167 } 168 for (; infn != infnend; ) { 169 infn += isochar(infn, infnend, joliet_level, &c, &clen, flags, handle); 170 if (!original && !joliet_level && c >= 'A' && c <= 'Z') 171 c += ('a' - 'A'); 172 else if (!original && c == ';') { 173 outp -= (d == '.'); 174 break; 175 } 176 d = c; 177 while(clen--) 178 *outp++ = c >> (clen << 3); 179 } 180 *outfnlen = outp - outfn; 181 } 182 183 /* 184 * same as sgetrune(3) 185 */ 186 u_short 187 sgetrune(const char *string, size_t n, char const **result, 188 int flags, void *handle) 189 { 190 size_t i, j, len; 191 char outbuf[3], *outp; 192 u_short c = '\0'; 193 len = i = (n < 2) ? n : 2; 194 j = 2; 195 outp = outbuf; 196 if (flags & ISOFSMNT_KICONV && cd9660_iconv) { 197 cd9660_iconv->convchr(handle, &string, &i, &outp, &j); 198 len -= i; 199 } else { 200 len = 1; 201 string++; 202 } 203 if (result) *result = string; 204 while(len--) c |= (*(string - len - 1) & 0xff) << (len << 3); 205 return (c); 206 } 207