xref: /freebsd/sys/fs/cd9660/cd9660_util.c (revision ce834215a70ff69e7e222827437116eee2f9ac6f)
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).
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. 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  * $Id: cd9660_util.c,v 1.9 1997/02/22 09:38:50 peter Exp $
40  */
41 
42 #include <sys/param.h>
43 #include <sys/mount.h>
44 #include <sys/vnode.h>
45 
46 #include <isofs/cd9660/iso.h>
47 
48 /*
49  * translate and compare a filename
50  * Note: Version number plus ';' may be omitted.
51  */
52 int
53 isofncmp(fn, fnlen, isofn, isolen)
54 	u_char *fn;
55 	int fnlen;
56 	u_char *isofn;
57 	int isolen;
58 {
59 	int i, j;
60 	unsigned char c;
61 
62 	while (--fnlen >= 0) {
63 		if (--isolen < 0)
64 			return *fn;
65 		if ((c = *isofn++) == ';') {
66 			switch (*fn++) {
67 			default:
68 				return *--fn;
69 			case 0:
70 				return 0;
71 			case ';':
72 				break;
73 			}
74 			for (i = 0; --fnlen >= 0; i = i * 10 + *fn++ - '0') {
75 				if (*fn < '0' || *fn > '9') {
76 					return -1;
77 				}
78 			}
79 			for (j = 0; --isolen >= 0; j = j * 10 + *isofn++ - '0');
80 			return i - j;
81 		}
82 		if (c != *fn) {
83 			if (c >= 'A' && c <= 'Z') {
84 				if (c + ('a' - 'A') != *fn) {
85 					if (*fn >= 'a' && *fn <= 'z')
86 						return *fn - ('a' - 'A') - c;
87 					else
88 						return *fn - c;
89 				}
90 			} else
91 				return *fn - c;
92 		}
93 		fn++;
94 	}
95 	if (isolen > 0) {
96 		switch (*isofn) {
97 		default:
98 			return -1;
99 		case '.':
100 			if (isofn[1] != ';')
101 				return -1;
102 		case ';':
103 			return 0;
104 		}
105 	}
106 	return 0;
107 }
108 
109 /*
110  * translate a filename
111  */
112 void
113 isofntrans(infn, infnlen, outfn, outfnlen, original, assoc)
114 	u_char *infn;
115 	int infnlen;
116 	u_char *outfn;
117 	u_short *outfnlen;
118 	int original;
119 	int assoc;
120 {
121 	int fnidx = 0;
122 
123 	if (assoc) {
124 		*outfn++ = ASSOCCHAR;
125 		fnidx++;
126 		infnlen++;
127 	}
128 	for (; fnidx < infnlen; fnidx++) {
129 		char c = *infn++;
130 
131 		if (!original && c >= 'A' && c <= 'Z')
132 			*outfn++ = c + ('a' - 'A');
133 		else if (!original && c == '.' && *infn == ';')
134 			break;
135 		else if (!original && c == ';')
136 			break;
137 		else
138 			*outfn++ = c;
139 	}
140 	*outfnlen = fnidx;
141 }
142