Home | History | Annotate | Line # | Download | only in cd9660
cd9660_util.c revision 1.4
      1 /*	$NetBSD: cd9660_util.c,v 1.4 2004/12/27 18:47:16 jdolecek Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley
      8  * by Pace Willisson (pace (at) blitz.com).  The Rock Ridge Extension
      9  * Support code is derived from software contributed to Berkeley
     10  * by Atsushi Murai (amurai (at) spec.co.jp).
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)cd9660_util.c	8.3 (Berkeley) 12/5/94
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: cd9660_util.c,v 1.4 2004/12/27 18:47:16 jdolecek Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/namei.h>
     45 #include <sys/resourcevar.h>
     46 #include <sys/kernel.h>
     47 #include <sys/file.h>
     48 #include <sys/stat.h>
     49 #include <sys/buf.h>
     50 #include <sys/proc.h>
     51 #include <sys/mount.h>
     52 #include <sys/vnode.h>
     53 #include <sys/malloc.h>
     54 #include <sys/dirent.h>
     55 
     56 #include <fs/cd9660/iso.h>
     57 #include <fs/cd9660/cd9660_extern.h>
     58 
     59 #include <fs/unicode.h>
     60 
     61 static u_int16_t wget(const u_char **, size_t *, int);
     62 static int wput(u_char *, size_t, u_int16_t, int);
     63 
     64 int cd9660_utf8_joliet = 1;
     65 
     66 /*
     67  * Get one character out of an iso filename
     68  * Return number of bytes consumed
     69  */
     70 int
     71 isochar(isofn, isoend, joliet_level, c)
     72 	const u_char *isofn;
     73 	const u_char *isoend;
     74 	int joliet_level;
     75 	u_int16_t *c;
     76 {
     77 	*c = isofn[0];
     78 	if (joliet_level == 0 || isofn + 1 == isoend) {
     79 		/* (00) and (01) are one byte in Joliet, too */
     80 		return 1;
     81 	}
     82 
     83 	if (cd9660_utf8_joliet) {
     84 		*c = (*c << 8) + isofn[1];
     85 	} else {
     86 		/* characters outside ISO-8859-1 subset replaced with '?' */
     87 		if (*c != 0)
     88 			*c = '?';
     89 		else
     90 			*c = isofn[1];
     91 	}
     92 
     93 	return 2;
     94 }
     95 
     96 /*
     97  * translate and compare a filename
     98  * Note: Version number plus ';' may be omitted.
     99  */
    100 int
    101 isofncmp(fn, fnlen, isofn, isolen, joliet_level)
    102 	const u_char *fn, *isofn;
    103 	int fnlen, isolen, joliet_level;
    104 {
    105 	int i, j;
    106 	u_int16_t fc, ic;
    107 	const u_char *isoend = isofn + isolen;
    108 
    109 	while (fnlen > 0) {
    110 		fc = wget(&fn, &fnlen, joliet_level);
    111 
    112 		if (isofn == isoend)
    113 			return fc;
    114 		isofn += isochar(isofn, isoend, joliet_level, &ic);
    115 		if (ic == ';') {
    116 			switch (fc) {
    117 			default:
    118 				return fc;
    119 			case 0:
    120 				return 0;
    121 			case ';':
    122 				break;
    123 			}
    124 			fn++;
    125 			for (i = 0; --fnlen >= 0; i = i * 10 + *fn++ - '0') {
    126 				if (*fn < '0' || *fn > '9') {
    127 					return -1;
    128 				}
    129 			}
    130 			for (j = 0; isofn != isoend; j = j * 10 + ic - '0')
    131 				isofn += isochar(isofn, isoend,
    132 						 joliet_level, &ic);
    133 			return i - j;
    134 		}
    135 		if (ic != fc) {
    136 			if (ic >= 'A' && ic <= 'Z') {
    137 				if (ic + ('a' - 'A') != fc) {
    138 					if (fc >= 'a' && fc <= 'z')
    139 						fc -= 'a' - 'A';
    140 
    141 					return (int) fc - (int) ic;
    142 				}
    143 			} else
    144 				return (int) fc - (int) ic;
    145 		}
    146 	}
    147 	if (isofn != isoend) {
    148 		isofn += isochar(isofn, isoend, joliet_level, &ic);
    149 		switch (ic) {
    150 		default:
    151 			return -1;
    152 		case '.':
    153 			if (isofn != isoend) {
    154 				isochar(isofn, isoend, joliet_level, &ic);
    155 				if (ic == ';')
    156 					return 0;
    157 			}
    158 			return -1;
    159 		case ';':
    160 			return 0;
    161 		}
    162 	}
    163 	return 0;
    164 }
    165 
    166 /*
    167  * translate a filename
    168  */
    169 void
    170 isofntrans(infn, infnlen, outfn, outfnlen, original, casetrans, assoc, joliet_level)
    171 	u_char *infn, *outfn;
    172 	int infnlen;
    173 	u_short *outfnlen;
    174 	int original;
    175 	int casetrans;
    176 	int assoc;
    177 	int joliet_level;
    178 {
    179 	int fnidx = 0;
    180 	u_char *infnend = infn + infnlen;
    181 	u_int16_t c;
    182 	int sz;
    183 
    184 	if (assoc) {
    185 		*outfn++ = ASSOCCHAR;
    186 		fnidx++;
    187 	}
    188 
    189 	for(; infn != infnend; fnidx += sz) {
    190 		infn += isochar(infn, infnend, joliet_level, &c);
    191 
    192 		if (casetrans && joliet_level == 0 && c >= 'A' && c <= 'Z')
    193 			c = c + ('a' - 'A');
    194 		else if (!original && c == ';') {
    195 			if (fnidx > 0 && outfn[-1] == '.')
    196 				fnidx--;
    197 			break;
    198 		}
    199 
    200 		sz = wput(outfn, MAXNAMLEN - fnidx, c, joliet_level);
    201 		if (sz == 0) {
    202 			/* not enough space to write the character */
    203 			if (fnidx < MAXNAMLEN) {
    204 				*outfn = '?';
    205 				fnidx++;
    206 			}
    207 			break;
    208 		}
    209 		outfn += sz;
    210 	}
    211 	*outfnlen = fnidx;
    212 }
    213 
    214 static u_int16_t
    215 wget(const u_char **str, size_t *sz, int joliet_level)
    216 {
    217 	if (joliet_level > 0 && cd9660_utf8_joliet) {
    218 		/* decode UTF-8 sequence */
    219 		return wget_utf8((const char **) str, sz);
    220 	} else {
    221 		/*
    222 		 * Raw 8-bit characters without any conversion. For Joliet,
    223 		 * this effectively assumes provided file name is using
    224 		 * ISO-8859-1 subset.
    225 		 */
    226 		u_int16_t c = *str[0];
    227 		(*str)++;
    228 
    229 		return c;
    230 	}
    231 }
    232 
    233 static int
    234 wput(u_char *s, size_t n, u_int16_t c, int joliet_level)
    235 {
    236 	if (joliet_level > 0 && cd9660_utf8_joliet) {
    237 		/* Store Joliet file name encoded into UTF-8 */
    238 		return wput_utf8((char *)s, n, c);
    239 	} else {
    240 		/*
    241 		 * Store raw 8-bit characters without any conversion.
    242 		 * For Joliet case, this filters the Unicode characters
    243 		 * to ISO-8859-1 subset.
    244 		 */
    245 		*s = (u_char)c;
    246 		return 1;
    247 	}
    248 }
    249