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