xref: /openbsd-src/sys/isofs/cd9660/cd9660_util.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: cd9660_util.c,v 1.9 2015/03/14 03:38:50 jsg Exp $	*/
2 /*	$NetBSD: cd9660_util.c,v 1.12 1997/01/24 00:27:33 cgd Exp $	*/
3 
4 /*-
5  * Copyright (c) 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley
9  * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
10  * Support code is derived from software contributed to Berkeley
11  * by Atsushi Murai (amurai@spec.co.jp). Joliet support was added by
12  * Joachim Kuebart (joki@kuebart.stuttgart.netsurf.de).
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)cd9660_util.c	8.3 (Berkeley) 12/5/94
39  */
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/namei.h>
44 #include <sys/resourcevar.h>
45 #include <sys/kernel.h>
46 #include <sys/file.h>
47 #include <sys/stat.h>
48 #include <sys/buf.h>
49 #include <sys/conf.h>
50 #include <sys/mount.h>
51 #include <sys/vnode.h>
52 #include <sys/malloc.h>
53 #include <sys/dirent.h>
54 
55 #include <isofs/cd9660/iso.h>
56 
57 /*
58  * XXX: limited support for loading of Unicode
59  * conversion routine as a kld at a run-time.
60  * Should be removed when native Unicode kernel
61  * interfaces have been introduced.
62  */
63 u_char (*cd9660_wchar2char)(u_int32_t wchar) = NULL;
64 
65 /*
66  * Get one character out of an iso filename
67  * Obey joliet_level
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_char *c;
76 {
77       *c = *isofn++;
78       if (joliet_level == 0 || isofn == isoend)
79               /* (00) and (01) are one byte in Joliet, too */
80               return 1;
81 
82       /* No Unicode support yet :-( */
83       switch (*c) {
84       default:
85               *c = '?';
86               break;
87       case '\0':
88               *c = *isofn;
89               break;
90       }
91 
92       /* XXX: if Unicode conversion routine is loaded then use it */
93       if (cd9660_wchar2char != NULL)
94 	      *c = cd9660_wchar2char((*(isofn - 1) << 8) | *isofn);
95 
96       return 2;
97 }
98 
99 /*
100  * translate and compare a filename
101  * returns (fn - isofn)
102  * Note: Version number plus ';' may be omitted.
103  */
104 int
105 isofncmp(fn, fnlen, isofn, isolen, joliet_level)
106 	const u_char *fn, *isofn;
107 	int fnlen, isolen, joliet_level;
108 {
109 	int i, j;
110 	u_char c;
111 	const u_char *fnend = fn + fnlen, *isoend = isofn + isolen;
112 
113 	for (; fn != fnend; fn++) {
114 		if (isofn == isoend)
115 			return *fn;
116 		isofn += isochar(isofn, isoend, joliet_level, &c);
117 		if (c == ';') {
118 			if (*fn++ != ';')
119 				return fn[-1];
120 			for (i = 0; fn != fnend; i = i * 10 + *fn++ - '0') {
121 				if (*fn < '0' || *fn > '9') {
122 					return -1;
123 				}
124 			}
125 			for (j = 0; isofn != isoend; j = j * 10 + c - '0')
126 				isofn += isochar(isofn, isoend,
127 				    joliet_level, &c);
128 			return i - j;
129 		}
130 		if (((u_char) c) != *fn) {
131 			if (c >= 'A' && c <= 'Z') {
132 				if (c + ('a' - 'A') != *fn) {
133 					if (*fn >= 'a' && *fn <= 'z')
134 						return *fn - ('a' - 'A') - c;
135 					else
136 						return *fn - c;
137 				}
138 			} else
139 				return *fn - c;
140 		}
141 	}
142 	if (isofn != isoend) {
143 		isofn += isochar(isofn, isoend, joliet_level, &c);
144 		switch (c) {
145 		default:
146 			return -c;
147 		case '.':
148 			if (isofn != isoend) {
149 				isochar(isofn, isoend, joliet_level, &c);
150 				if (c == ';')
151 					return 0;
152 			}
153 			return -1;
154 		case ';':
155 			return 0;
156 		}
157 	}
158 	return 0;
159 }
160 
161 /*
162  * translate a filename of length > 0
163  */
164 void
165 isofntrans(infn, infnlen, outfn, outfnlen, original, assoc, joliet_level)
166 	u_char *infn, *outfn;
167 	int infnlen;
168 	u_short *outfnlen;
169 	int original;
170 	int assoc;
171 	int joliet_level;
172 {
173 	int fnidx = 0;
174 	u_char c, d = '\0', *infnend = infn + infnlen;
175 
176 	if (assoc) {
177 		*outfn++ = ASSOCCHAR;
178 		fnidx++;
179 	}
180 	for (; infn != infnend; fnidx++) {
181 		infn += isochar(infn, infnend, joliet_level, &c);
182 
183 		if (!original && c == ';') {
184 			fnidx -= (d == '.');
185 			break;
186 		} else
187 			*outfn++ = c;
188 		d = c;
189 	}
190 	*outfnlen = fnidx;
191 }
192