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