xref: /freebsd/sys/fs/cd9660/cd9660_util.c (revision 4cf49a43559ed9fdad601bdcccd2c55963008675)
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  * $FreeBSD$
41  */
42 
43 #include <sys/param.h>
44 #include <sys/mount.h>
45 #include <sys/vnode.h>
46 
47 #include <isofs/cd9660/iso.h>
48 
49 /*
50  * Get one character out of an iso filename
51  * Obey joliet_level
52  * Return number of bytes consumed
53  */
54 int
55 isochar(isofn, isoend, joliet_level, c)
56       u_char *isofn;
57       u_char *isoend;
58       int joliet_level;
59       u_char *c;
60 {
61       *c = *isofn++;
62       if (joliet_level == 0 || isofn == isoend)
63               /* (00) and (01) are one byte in Joliet, too */
64               return 1;
65 
66       /* No Unicode support yet :-( */
67       switch (*c) {
68       default:
69               *c = '?';
70               break;
71       case '\0':
72               *c = *isofn;
73               break;
74       }
75       return 2;
76 }
77 
78 /*
79  * translate and compare a filename
80  * returns (fn - isofn)
81  * Note: Version number plus ';' may be omitted.
82  */
83 int
84 isofncmp(fn, fnlen, isofn, isolen, joliet_level)
85 	u_char *fn;
86 	int fnlen;
87 	u_char *isofn;
88 	int isolen;
89 	int joliet_level;
90 {
91 	int i, j;
92 	u_char c, *fnend = fn + fnlen, *isoend = isofn + isolen;
93 
94 	for (; fn != fnend; fn++) {
95 		if (isofn == isoend)
96 			return *fn;
97 		isofn += isochar(isofn, isoend, joliet_level, &c);
98 		if (c == ';') {
99 			if (*fn++ != ';')
100 				return fn[-1];
101 			for (i = 0; fn != fnend; i = i * 10 + *fn++ - '0') {
102 				if (*fn < '0' || *fn > '9') {
103 					return -1;
104 				}
105 			}
106 			for (j = 0; isofn != isoend; j = j * 10 + c - '0')
107 				isofn += isochar(isofn, isoend,
108 						 joliet_level, &c);
109 			return i - j;
110 		}
111 		if (c != *fn) {
112 			if (c >= 'A' && c <= 'Z') {
113 				if (c + ('a' - 'A') != *fn) {
114 					if (*fn >= 'a' && *fn <= 'z')
115 						return *fn - ('a' - 'A') - c;
116 					else
117 						return *fn - c;
118 				}
119 			} else
120 				return *fn - c;
121 		}
122 	}
123 	if (isofn != isoend) {
124 		isofn += isochar(isofn, isoend, joliet_level, &c);
125 		switch (c) {
126 		default:
127 			return -c;
128 		case '.':
129 			if (isofn != isoend) {
130 				isochar(isofn, isoend, joliet_level, &c);
131 				if (c == ';')
132 					return 0;
133 			}
134 			return -1;
135 		case ';':
136 			return 0;
137 		}
138 	}
139 	return 0;
140 }
141 
142 /*
143  * translate a filename of length > 0
144  */
145 void
146 isofntrans(infn, infnlen, outfn, outfnlen, original, assoc, joliet_level)
147 	u_char *infn;
148 	int infnlen;
149 	u_char *outfn;
150 	u_short *outfnlen;
151 	int original;
152 	int assoc;
153 	int joliet_level;
154 {
155 	int fnidx = 0;
156 	u_char c, d = '\0', *infnend = infn + infnlen;
157 
158 	if (assoc) {
159 		*outfn++ = ASSOCCHAR;
160 		fnidx++;
161 	}
162 	for (; infn != infnend; fnidx++) {
163 		infn += isochar(infn, infnend, joliet_level, &c);
164 
165 		if (!original && !joliet_level && c >= 'A' && c <= 'Z')
166 			*outfn++ = c + ('a' - 'A');
167 		else if (!original && c == ';') {
168 			fnidx -= (d == '.');
169 			break;
170 		} else
171 			*outfn++ = c;
172 		d = c;
173 	}
174 	*outfnlen = fnidx;
175 }
176