Home | History | Annotate | Line # | Download | only in cd9660
cd9660_util.c revision 1.13
      1 /*	$NetBSD: cd9660_util.c,v 1.13 2016/03/09 15:45:37 christos 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 #ifdef _KERNEL
     41 __KERNEL_RCSID(0, "$NetBSD: cd9660_util.c,v 1.13 2016/03/09 15:45:37 christos Exp $");
     42 #else
     43 /* used by macppc_installboot */
     44 #if HAVE_NBTOOL_CONFIG_H
     45 #include "nbtool_config.h"
     46 #endif
     47 #endif
     48 
     49 #include <sys/param.h>
     50 #ifdef _KERNEL
     51 #include <sys/systm.h>
     52 #include <sys/namei.h>
     53 #include <sys/resourcevar.h>
     54 #include <sys/kernel.h>
     55 #include <sys/file.h>
     56 #include <sys/stat.h>
     57 #include <sys/buf.h>
     58 #include <sys/proc.h>
     59 #include <sys/mount.h>
     60 #include <sys/vnode.h>
     61 #include <sys/dirent.h>
     62 #else
     63 #include <assert.h>
     64 #include <dirent.h>
     65 #define KASSERT(x)	assert(x)	/* XXX for <fs/unicode.h> */
     66 #endif
     67 
     68 #include <fs/cd9660/iso.h>
     69 #ifdef _KERNEL
     70 #include <fs/cd9660/cd9660_extern.h>
     71 #else
     72 #include "installboot.h"
     73 static int isochar(const u_char *, const u_char *, int, uint16_t *);
     74 #endif
     75 
     76 #include <fs/unicode.h>
     77 
     78 static uint16_t wget(const u_char **, size_t *, int);
     79 static int wput(u_char *, size_t, uint16_t, int);
     80 
     81 int cd9660_utf8_joliet = 1;
     82 
     83 /*
     84  * Get one character out of an iso filename
     85  * Return number of bytes consumed
     86  */
     87 int
     88 isochar(const u_char *isofn, const u_char *isoend, int joliet_level,
     89     uint16_t *c)
     90 {
     91 
     92 	*c = isofn[0];
     93 	if (joliet_level == 0 || isofn + 1 == isoend) {
     94 		/* (00) and (01) are one byte in Joliet, too */
     95 		return 1;
     96 	}
     97 
     98 	if (cd9660_utf8_joliet) {
     99 		*c = (*c << 8) + isofn[1];
    100 	} else {
    101 		/* characters outside ISO-8859-1 subset replaced with '?' */
    102 		if (*c != 0)
    103 			*c = '?';
    104 		else
    105 			*c = isofn[1];
    106 	}
    107 
    108 	return 2;
    109 }
    110 
    111 /*
    112  * translate and compare a filename
    113  * Note: Version number plus ';' may be omitted.
    114  */
    115 int
    116 isofncmp(const u_char *fn, size_t fnlen, const u_char *isofn, size_t isolen,
    117     int joliet_level)
    118 {
    119 	int i, j;
    120 	uint16_t fc, ic;
    121 	const u_char *isoend = isofn + isolen;
    122 
    123 #ifdef ISOFNCMPDEBUG
    124 	printf("fn = %s, fnlen = %zu, isofn = %s, isolen = %zu\n",
    125 	    fn, fnlen, isofn, isolen);
    126 #endif
    127 
    128 	while (fnlen > 0) {
    129 		fc = wget(&fn, &fnlen, joliet_level);
    130 
    131 		if (isofn == isoend)
    132 			return fc;
    133 		isofn += isochar(isofn, isoend, joliet_level, &ic);
    134 		if (ic == ';') {
    135 			switch (fc) {
    136 			default:
    137 				return fc;
    138 			case 0:
    139 				return 0;
    140 			case ';':
    141 				break;
    142 			}
    143 			for (i = 0; fnlen-- != 0; i = i * 10 + *fn++ - '0') {
    144 				if (*fn < '0' || *fn > '9') {
    145 					return -1;
    146 				}
    147 			}
    148 			for (j = 0; isofn != isoend; j = j * 10 + ic - '0')
    149 				isofn += isochar(isofn, isoend,
    150 						 joliet_level, &ic);
    151 			return i - j;
    152 		}
    153 		if (ic != fc) {
    154 			if (ic >= 'A' && ic <= 'Z') {
    155 				if (ic + ('a' - 'A') != fc) {
    156 					if (fc >= 'a' && fc <= 'z')
    157 						fc -= 'a' - 'A';
    158 
    159 					return (int)fc - (int)ic;
    160 				}
    161 			} else
    162 				return (int)fc - (int)ic;
    163 		}
    164 	}
    165 	if (isofn != isoend) {
    166 		isofn += isochar(isofn, isoend, joliet_level, &ic);
    167 		switch (ic) {
    168 		default:
    169 			return -1;
    170 		case '.':
    171 			if (isofn != isoend) {
    172 				isochar(isofn, isoend, joliet_level, &ic);
    173 				if (ic == ';')
    174 					return 0;
    175 			}
    176 			return -1;
    177 		case ';':
    178 			return 0;
    179 		}
    180 	}
    181 	return 0;
    182 }
    183 
    184 /*
    185  * translate a filename
    186  */
    187 void
    188 isofntrans(const u_char *infn, int infnlen, u_char *outfn, u_short *outfnlen,
    189     int original, int casetrans, int assoc, int joliet_level)
    190 {
    191 	int fnidx = 0;
    192 	const u_char *infnend = infn + infnlen;
    193 	uint16_t c;
    194 	int sz;
    195 
    196 	if (assoc) {
    197 		*outfn++ = ASSOCCHAR;
    198 		fnidx++;
    199 	}
    200 
    201 	for(; infn != infnend; fnidx += sz) {
    202 		infn += isochar(infn, infnend, joliet_level, &c);
    203 
    204 		if (casetrans && joliet_level == 0 && c >= 'A' && c <= 'Z')
    205 			c = c + ('a' - 'A');
    206 		else if (!original && c == ';') {
    207 			if (fnidx > 0 && outfn[-1] == '.')
    208 				fnidx--;
    209 			break;
    210 		}
    211 
    212 		sz = wput(outfn, ISO_MAXNAMLEN - fnidx, c, joliet_level);
    213 		if (sz == 0) {
    214 			/* not enough space to write the character */
    215 			if (fnidx < ISO_MAXNAMLEN) {
    216 				*outfn = '?';
    217 				fnidx++;
    218 			}
    219 			break;
    220 		}
    221 		outfn += sz;
    222 	}
    223 	*outfnlen = fnidx;
    224 }
    225 
    226 static uint16_t
    227 wget(const u_char **str, size_t *sz, int joliet_level)
    228 {
    229 	if (joliet_level > 0 && cd9660_utf8_joliet) {
    230 		/* decode UTF-8 sequence */
    231 		return wget_utf8((const char **) str, sz);
    232 	} else {
    233 		/*
    234 		 * Raw 8-bit characters without any conversion. For Joliet,
    235 		 * this effectively assumes provided file name is using
    236 		 * ISO-8859-1 subset.
    237 		 */
    238 		uint16_t c = *str[0];
    239 		(*str)++;
    240 		(*sz)--;
    241 
    242 		return c;
    243 	}
    244 }
    245 
    246 static int
    247 wput(u_char *s, size_t n, uint16_t c, int joliet_level)
    248 {
    249 	if (joliet_level > 0 && cd9660_utf8_joliet) {
    250 		/* Store Joliet file name encoded into UTF-8 */
    251 		return wput_utf8((char *)s, n, c);
    252 	} else {
    253 		/*
    254 		 * Store raw 8-bit characters without any conversion.
    255 		 * For Joliet case, this filters the Unicode characters
    256 		 * to ISO-8859-1 subset.
    257 		 */
    258 		*s = (u_char)c;
    259 		return 1;
    260 	}
    261 }
    262