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