Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / vfs / isofs / cd9660 / cd9660_util.c
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: src/sys/isofs/cd9660/cd9660_util.c,v 1.13.2.1 2001/02/27 12:36:34 sobomax Exp $
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  * XXX: limited support for loading of Unicode
51  * conversion routine as a kld at a run-time.
52  * Should be removed when native Unicode kernel
53  * interfaces have been introduced.
54  */
55 u_char (*cd9660_wchar2char)(u_int32_t wchar) = NULL;
56
57 /*
58  * Get one character out of an iso filename
59  * Obey joliet_level
60  * Return number of bytes consumed
61  */
62 int
63 isochar(isofn, isoend, joliet_level, c)
64       u_char *isofn;
65       u_char *isoend;
66       int joliet_level;
67       u_char *c;
68 {
69       *c = *isofn++;
70       if (joliet_level == 0 || isofn == isoend)
71               /* (00) and (01) are one byte in Joliet, too */
72               return 1;
73
74       /* No Unicode support yet :-( */
75       switch (*c) {
76       default:
77               *c = '?';
78               break;
79       case '\0':
80               *c = *isofn;
81               break;
82       }
83       /* XXX: if Unicode conversion routine is loaded then use it */
84       if (cd9660_wchar2char != NULL)
85             *c = cd9660_wchar2char((*(isofn - 1) << 8) | *isofn);
86
87       return 2;
88 }
89
90 /*
91  * translate and compare a filename
92  * returns (fn - isofn)
93  * Note: Version number plus ';' may be omitted.
94  */
95 int
96 isofncmp(fn, fnlen, isofn, isolen, joliet_level)
97         u_char *fn;
98         int fnlen;
99         u_char *isofn;
100         int isolen;
101         int joliet_level;
102 {
103         int i, j;
104         u_char c, *fnend = fn + fnlen, *isoend = isofn + isolen;
105
106         for (; fn != fnend; fn++) {
107                 if (isofn == isoend)
108                         return *fn;
109                 isofn += isochar(isofn, isoend, joliet_level, &c);
110                 if (c == ';') {
111                         if (*fn++ != ';')
112                                 return fn[-1];
113                         for (i = 0; fn != fnend; i = i * 10 + *fn++ - '0') {
114                                 if (*fn < '0' || *fn > '9') {
115                                         return -1;
116                                 }
117                         }
118                         for (j = 0; isofn != isoend; j = j * 10 + c - '0')
119                                 isofn += isochar(isofn, isoend,
120                                                  joliet_level, &c);
121                         return i - j;
122                 }
123                 if (c != *fn) {
124                         if (c >= 'A' && c <= 'Z') {
125                                 if (c + ('a' - 'A') != *fn) {
126                                         if (*fn >= 'a' && *fn <= 'z')
127                                                 return *fn - ('a' - 'A') - c;
128                                         else
129                                                 return *fn - c;
130                                 }
131                         } else
132                                 return *fn - c;
133                 }
134         }
135         if (isofn != isoend) {
136                 isofn += isochar(isofn, isoend, joliet_level, &c);
137                 switch (c) {
138                 default:
139                         return -c;
140                 case '.':
141                         if (isofn != isoend) {
142                                 isochar(isofn, isoend, joliet_level, &c);
143                                 if (c == ';')
144                                         return 0;
145                         }
146                         return -1;
147                 case ';':
148                         return 0;
149                 }
150         }
151         return 0;
152 }
153
154 /*
155  * translate a filename of length > 0
156  */
157 void
158 isofntrans(infn, infnlen, outfn, outfnlen, original, assoc, joliet_level)
159         u_char *infn;
160         int infnlen;
161         u_char *outfn;
162         u_short *outfnlen;
163         int original;
164         int assoc;
165         int joliet_level;
166 {
167         int fnidx = 0;
168         u_char c, d = '\0', *infnend = infn + infnlen;
169
170         if (assoc) {
171                 *outfn++ = ASSOCCHAR;
172                 fnidx++;
173         }
174         for (; infn != infnend; fnidx++) {
175                 infn += isochar(infn, infnend, joliet_level, &c);
176
177                 if (!original && !joliet_level && c >= 'A' && c <= 'Z')
178                         *outfn++ = c + ('a' - 'A');
179                 else if (!original && c == ';') {
180                         fnidx -= (d == '.');
181                         break;
182                 } else
183                         *outfn++ = c;
184                 d = c;
185         }
186         *outfnlen = fnidx;
187 }