]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/cd9660/cd9660_util.c
zfs: merge openzfs/zfs@e13538856
[FreeBSD/FreeBSD.git] / sys / fs / cd9660 / cd9660_util.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1994
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley
8  * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
9  * Support code is derived from software contributed to Berkeley
10  * by Atsushi Murai (amurai@spec.co.jp). Joliet support was added by
11  * Joachim Kuebart (joki@kuebart.stuttgart.netsurf.de).
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *      @(#)cd9660_util.c       8.3 (Berkeley) 12/5/94
38  */
39
40 #include <sys/cdefs.h>
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/mount.h>
44 #include <sys/vnode.h>
45 #include <sys/iconv.h>
46
47 #include <fs/cd9660/iso.h>
48 #include <fs/cd9660/cd9660_mount.h>
49
50 extern struct iconv_functions *cd9660_iconv;
51
52 /*
53  * Get one character out of an iso filename
54  * Obey joliet_level
55  * Return number of bytes consumed
56  */
57 int
58 isochar(u_char *isofn, u_char *isoend, int joliet_level, u_short *c, int *clen,
59     int flags, void *handle)
60 {
61       size_t i, j, len;
62       char inbuf[3], outbuf[3], *inp, *outp;
63
64       *c = *isofn++;
65       if (clen) *clen = 1;
66       if (joliet_level == 0 || isofn == isoend)
67               /* (00) and (01) are one byte in Joliet, too */
68               return 1;
69
70       if (flags & ISOFSMNT_KICONV && cd9660_iconv) {
71               i = j = len = 2;
72               inbuf[0]=(char)*(isofn - 1);
73               inbuf[1]=(char)*isofn;
74               inbuf[2]='\0';
75               inp = inbuf;
76               outp = outbuf;
77               cd9660_iconv->convchr(handle, __DECONST(const char **, &inp), &i,
78                   &outp, &j);
79               len -= j;
80               if (clen) *clen = len;
81               *c = '\0';
82               while(len--)
83                       *c |= (*(outp - len - 1) & 0xff) << (len << 3);
84       } else {
85               switch (*c) {
86               default:
87                       *c = '?';
88                       break;
89               case '\0':
90                       *c = *isofn;
91                       break;
92               }
93       }
94
95       return 2;
96 }
97
98 /*
99  * translate and compare a filename
100  * returns (fn - isofn)
101  * Note: Version number plus ';' may be omitted.
102  */
103 int
104 isofncmp(u_char *fn, int fnlen, u_char *isofn, int isolen, int joliet_level,
105     int flags, void *handle, void *lhandle)
106 {
107         int i, j;
108         u_short c, d;
109         u_char *fnend = fn + fnlen, *isoend = isofn + isolen;
110
111         for (; fn < fnend; ) {
112                 d = sgetrune(fn, fnend - fn, __DECONST(const char **, &fn),
113                     flags, lhandle);
114                 if (isofn == isoend)
115                         return d;
116                 isofn += isochar(isofn, isoend, joliet_level, &c, NULL, flags, handle);
117                 if (c == ';') {
118                         if (d != ';')
119                                 return d;
120                         for (i = 0; fn < fnend; i = i * 10 + *fn++ - '0') {
121                                 if (*fn < '0' || *fn > '9') {
122                                         return -1;
123                                 }
124                         }
125                         for (j = 0; isofn != isoend; j = j * 10 + c - '0')
126                                 isofn += isochar(isofn, isoend,
127                                                  joliet_level, &c,
128                                                  NULL, flags, handle);
129                         return i - j;
130                 }
131                 if (c != d) {
132                         if (c >= 'A' && c <= 'Z') {
133                                 if (c + ('a' - 'A') != d) {
134                                         if (d >= 'a' && d <= 'z')
135                                                 return d - ('a' - 'A') - c;
136                                         else
137                                                 return d - c;
138                                 }
139                         } else
140                                 return d - c;
141                 }
142         }
143         if (isofn != isoend) {
144                 isofn += isochar(isofn, isoend, joliet_level, &c, NULL, flags, handle);
145                 switch (c) {
146                 default:
147                         return -c;
148                 case '.':
149                         if (isofn != isoend) {
150                                 isochar(isofn, isoend, joliet_level, &c,
151                                         NULL, flags, handle);
152                                 if (c == ';')
153                                         return 0;
154                         }
155                         return -1;
156                 case ';':
157                         return 0;
158                 }
159         }
160         return 0;
161 }
162
163 /*
164  * translate a filename of length > 0
165  */
166 void
167 isofntrans(u_char *infn, int infnlen, u_char *outfn, u_short *outfnlen,
168     int original, int assoc, int joliet_level, int flags, void *handle)
169 {
170         u_short c, d = '\0';
171         u_char *outp = outfn, *infnend = infn + infnlen;
172         int clen;
173
174         if (assoc) {
175                 *outp++ = ASSOCCHAR;
176         }
177         for (; infn != infnend; ) {
178                 infn += isochar(infn, infnend, joliet_level, &c, &clen, flags, handle);
179
180                 if (!original && !joliet_level && c >= 'A' && c <= 'Z')
181                         c += ('a' - 'A');
182                 else if (!original && c == ';') {
183                         outp -= (d == '.');
184                         break;
185                 }
186                 d = c;
187                 while(clen--)
188                         *outp++ = c >> (clen << 3);
189         }
190         *outfnlen = outp - outfn;
191 }
192
193 /*
194  * same as sgetrune(3)
195  */
196 u_short
197 sgetrune(const char *string, size_t n, char const **result, int flags,
198     void *handle)
199 {
200         size_t i, j, len;
201         char outbuf[3], *outp;
202         u_short c = '\0';
203
204         len = i = (n < 2) ? n : 2;
205         j = 2;
206         outp = outbuf;
207
208         if (flags & ISOFSMNT_KICONV && cd9660_iconv) {
209                 cd9660_iconv->convchr(handle, (const char **)&string,
210                         &i, &outp, &j);
211                 len -= i;
212         } else {
213                 len = 1;
214                 string++;
215         }
216
217         if (result) *result = string;
218         while(len--) c |= (*(string - len - 1) & 0xff) << (len << 3);
219         return (c);
220 }