Deleted Added
full compact
cd9660_util.c (256281) cd9660_util.c (278060)
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 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)cd9660_util.c 8.3 (Berkeley) 12/5/94
36 */
37
38#include <sys/cdefs.h>
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 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)cd9660_util.c 8.3 (Berkeley) 12/5/94
36 */
37
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: stable/10/sys/fs/cd9660/cd9660_util.c 166639 2007-02-11 13:54:25Z rodrigc $");
39__FBSDID("$FreeBSD: stable/10/sys/fs/cd9660/cd9660_util.c 278060 2015-02-02 07:42:03Z dim $");
40
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
50extern 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 */
57int
58isochar(isofn, isoend, joliet_level, c, clen, flags, handle)
59 u_char *isofn;
60 u_char *isoend;
61 int joliet_level;
62 u_short *c;
63 int *clen;
64 int flags;
65 void *handle;
66{
67 size_t i, j, len;
68 char inbuf[3], outbuf[3], *inp, *outp;
69
70 *c = *isofn++;
71 if (clen) *clen = 1;
72 if (joliet_level == 0 || isofn == isoend)
73 /* (00) and (01) are one byte in Joliet, too */
74 return 1;
75
76 if (flags & ISOFSMNT_KICONV && cd9660_iconv) {
77 i = j = len = 2;
78 inbuf[0]=(char)*(isofn - 1);
79 inbuf[1]=(char)*isofn;
80 inbuf[2]='\0';
81 inp = inbuf;
82 outp = outbuf;
40
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
50extern 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 */
57int
58isochar(isofn, isoend, joliet_level, c, clen, flags, handle)
59 u_char *isofn;
60 u_char *isoend;
61 int joliet_level;
62 u_short *c;
63 int *clen;
64 int flags;
65 void *handle;
66{
67 size_t i, j, len;
68 char inbuf[3], outbuf[3], *inp, *outp;
69
70 *c = *isofn++;
71 if (clen) *clen = 1;
72 if (joliet_level == 0 || isofn == isoend)
73 /* (00) and (01) are one byte in Joliet, too */
74 return 1;
75
76 if (flags & ISOFSMNT_KICONV && cd9660_iconv) {
77 i = j = len = 2;
78 inbuf[0]=(char)*(isofn - 1);
79 inbuf[1]=(char)*isofn;
80 inbuf[2]='\0';
81 inp = inbuf;
82 outp = outbuf;
83 cd9660_iconv->convchr(handle, (const char **)&inp, &i, &outp, &j);
83 cd9660_iconv->convchr(handle, __DECONST(const char **, &inp), &i,
84 &outp, &j);
84 len -= j;
85 if (clen) *clen = len;
86 *c = '\0';
87 while(len--)
88 *c |= (*(outp - len - 1) & 0xff) << (len << 3);
89 } else {
90 switch (*c) {
91 default:
92 *c = '?';
93 break;
94 case '\0':
95 *c = *isofn;
96 break;
97 }
98 }
99
100 return 2;
101}
102
103/*
104 * translate and compare a filename
105 * returns (fn - isofn)
106 * Note: Version number plus ';' may be omitted.
107 */
108int
109isofncmp(fn, fnlen, isofn, isolen, joliet_level, flags, handle, lhandle)
110 u_char *fn;
111 int fnlen;
112 u_char *isofn;
113 int isolen;
114 int joliet_level;
115 int flags;
116 void *handle;
117 void *lhandle;
118{
119 int i, j;
120 u_short c, d;
121 u_char *fnend = fn + fnlen, *isoend = isofn + isolen;
122
123 for (; fn < fnend; ) {
85 len -= j;
86 if (clen) *clen = len;
87 *c = '\0';
88 while(len--)
89 *c |= (*(outp - len - 1) & 0xff) << (len << 3);
90 } else {
91 switch (*c) {
92 default:
93 *c = '?';
94 break;
95 case '\0':
96 *c = *isofn;
97 break;
98 }
99 }
100
101 return 2;
102}
103
104/*
105 * translate and compare a filename
106 * returns (fn - isofn)
107 * Note: Version number plus ';' may be omitted.
108 */
109int
110isofncmp(fn, fnlen, isofn, isolen, joliet_level, flags, handle, lhandle)
111 u_char *fn;
112 int fnlen;
113 u_char *isofn;
114 int isolen;
115 int joliet_level;
116 int flags;
117 void *handle;
118 void *lhandle;
119{
120 int i, j;
121 u_short c, d;
122 u_char *fnend = fn + fnlen, *isoend = isofn + isolen;
123
124 for (; fn < fnend; ) {
124 d = sgetrune(fn, fnend - fn, (char const **)&fn, flags, lhandle);
125 d = sgetrune(fn, fnend - fn, __DECONST(const char **, &fn),
126 flags, lhandle);
125 if (isofn == isoend)
126 return d;
127 isofn += isochar(isofn, isoend, joliet_level, &c, NULL, flags, handle);
128 if (c == ';') {
129 if (d != ';')
130 return d;
131 for (i = 0; fn < fnend; i = i * 10 + *fn++ - '0') {
132 if (*fn < '0' || *fn > '9') {
133 return -1;
134 }
135 }
136 for (j = 0; isofn != isoend; j = j * 10 + c - '0')
137 isofn += isochar(isofn, isoend,
138 joliet_level, &c,
139 NULL, flags, handle);
140 return i - j;
141 }
142 if (c != d) {
143 if (c >= 'A' && c <= 'Z') {
144 if (c + ('a' - 'A') != d) {
145 if (d >= 'a' && d <= 'z')
146 return d - ('a' - 'A') - c;
147 else
148 return d - c;
149 }
150 } else
151 return d - c;
152 }
153 }
154 if (isofn != isoend) {
155 isofn += isochar(isofn, isoend, joliet_level, &c, NULL, flags, handle);
156 switch (c) {
157 default:
158 return -c;
159 case '.':
160 if (isofn != isoend) {
161 isochar(isofn, isoend, joliet_level, &c,
162 NULL, flags, handle);
163 if (c == ';')
164 return 0;
165 }
166 return -1;
167 case ';':
168 return 0;
169 }
170 }
171 return 0;
172}
173
174/*
175 * translate a filename of length > 0
176 */
177void
178isofntrans(infn, infnlen, outfn, outfnlen, original, assoc, joliet_level, flags, handle)
179 u_char *infn;
180 int infnlen;
181 u_char *outfn;
182 u_short *outfnlen;
183 int original;
184 int assoc;
185 int joliet_level;
186 int flags;
187 void *handle;
188{
189 u_short c, d = '\0';
190 u_char *outp = outfn, *infnend = infn + infnlen;
191 int clen;
192
193 if (assoc) {
194 *outp++ = ASSOCCHAR;
195 }
196 for (; infn != infnend; ) {
197 infn += isochar(infn, infnend, joliet_level, &c, &clen, flags, handle);
198
199 if (!original && !joliet_level && c >= 'A' && c <= 'Z')
200 c += ('a' - 'A');
201 else if (!original && c == ';') {
202 outp -= (d == '.');
203 break;
204 }
205 d = c;
206 while(clen--)
207 *outp++ = c >> (clen << 3);
208 }
209 *outfnlen = outp - outfn;
210}
211
212/*
213 * same as sgetrune(3)
214 */
215u_short
216sgetrune(string, n, result, flags, handle)
217 const char *string;
218 size_t n;
219 char const **result;
220 int flags;
221 void *handle;
222{
223 size_t i, j, len;
224 char outbuf[3], *outp;
225 u_short c = '\0';
226
227 len = i = (n < 2) ? n : 2;
228 j = 2;
229 outp = outbuf;
230
231 if (flags & ISOFSMNT_KICONV && cd9660_iconv) {
232 cd9660_iconv->convchr(handle, (const char **)&string,
233 &i, &outp, &j);
234 len -= i;
235 } else {
236 len = 1;
237 string++;
238 }
239
240 if (result) *result = string;
241 while(len--) c |= (*(string - len - 1) & 0xff) << (len << 3);
242 return (c);
243}
127 if (isofn == isoend)
128 return d;
129 isofn += isochar(isofn, isoend, joliet_level, &c, NULL, flags, handle);
130 if (c == ';') {
131 if (d != ';')
132 return d;
133 for (i = 0; fn < fnend; i = i * 10 + *fn++ - '0') {
134 if (*fn < '0' || *fn > '9') {
135 return -1;
136 }
137 }
138 for (j = 0; isofn != isoend; j = j * 10 + c - '0')
139 isofn += isochar(isofn, isoend,
140 joliet_level, &c,
141 NULL, flags, handle);
142 return i - j;
143 }
144 if (c != d) {
145 if (c >= 'A' && c <= 'Z') {
146 if (c + ('a' - 'A') != d) {
147 if (d >= 'a' && d <= 'z')
148 return d - ('a' - 'A') - c;
149 else
150 return d - c;
151 }
152 } else
153 return d - c;
154 }
155 }
156 if (isofn != isoend) {
157 isofn += isochar(isofn, isoend, joliet_level, &c, NULL, flags, handle);
158 switch (c) {
159 default:
160 return -c;
161 case '.':
162 if (isofn != isoend) {
163 isochar(isofn, isoend, joliet_level, &c,
164 NULL, flags, handle);
165 if (c == ';')
166 return 0;
167 }
168 return -1;
169 case ';':
170 return 0;
171 }
172 }
173 return 0;
174}
175
176/*
177 * translate a filename of length > 0
178 */
179void
180isofntrans(infn, infnlen, outfn, outfnlen, original, assoc, joliet_level, flags, handle)
181 u_char *infn;
182 int infnlen;
183 u_char *outfn;
184 u_short *outfnlen;
185 int original;
186 int assoc;
187 int joliet_level;
188 int flags;
189 void *handle;
190{
191 u_short c, d = '\0';
192 u_char *outp = outfn, *infnend = infn + infnlen;
193 int clen;
194
195 if (assoc) {
196 *outp++ = ASSOCCHAR;
197 }
198 for (; infn != infnend; ) {
199 infn += isochar(infn, infnend, joliet_level, &c, &clen, flags, handle);
200
201 if (!original && !joliet_level && c >= 'A' && c <= 'Z')
202 c += ('a' - 'A');
203 else if (!original && c == ';') {
204 outp -= (d == '.');
205 break;
206 }
207 d = c;
208 while(clen--)
209 *outp++ = c >> (clen << 3);
210 }
211 *outfnlen = outp - outfn;
212}
213
214/*
215 * same as sgetrune(3)
216 */
217u_short
218sgetrune(string, n, result, flags, handle)
219 const char *string;
220 size_t n;
221 char const **result;
222 int flags;
223 void *handle;
224{
225 size_t i, j, len;
226 char outbuf[3], *outp;
227 u_short c = '\0';
228
229 len = i = (n < 2) ? n : 2;
230 j = 2;
231 outp = outbuf;
232
233 if (flags & ISOFSMNT_KICONV && cd9660_iconv) {
234 cd9660_iconv->convchr(handle, (const char **)&string,
235 &i, &outp, &j);
236 len -= i;
237 } else {
238 len = 1;
239 string++;
240 }
241
242 if (result) *result = string;
243 while(len--) c |= (*(string - len - 1) & 0xff) << (len << 3);
244 return (c);
245}