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