libiconv: convert cd9660 to libiconv.
[dragonfly.git] / sys / vfs / isofs / cd9660 / cd9660_util.c
blob43527c0cf8f45e6747663735d84e041ff8650f3b
1 /*-
2 * Copyright (c) 1994
3 * The Regents of the University of California. All rights reserved.
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).
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.
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.
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.4 2004/04/12 23:18:55 cpressey Exp $
44 #include <sys/param.h>
45 #include <sys/mount.h>
46 #include <sys/vnode.h>
47 #include <sys/iconv.h>
49 #include "iso.h"
50 #include "cd9660_mount.h"
52 extern struct iconv_functions *cd9660_iconv;
55 * Get one character out of an iso filename
56 * Obey joliet_level
57 * Return number of bytes consumed
59 int
60 isochar(isofn, isoend, joliet_level, c, clen, flags, handle)
61 u_char *isofn;
62 u_char *isoend;
63 int joliet_level;
64 u_short *c;
65 int *clen;
66 int flags;
67 void *handle;
69 size_t i, j, len;
70 char inbuf[3], outbuf[3], *inp, *outp;
71 *c = *isofn++;
72 if (clen) *clen = 1;
73 if (joliet_level == 0 || isofn == isoend)
74 /* (00) and (01) are one byte in Joliet, too */
75 return 1;
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);
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;
99 return 2;
103 * translate and compare a filename
104 * returns (fn - isofn)
105 * Note: Version number plus ';' may be omitted.
108 isofncmp(fn, fnlen, isofn, isolen, joliet_level, flags, handle, lhandle)
109 u_char *fn;
110 int fnlen;
111 u_char *isofn;
112 int isolen;
113 int joliet_level;
114 int flags;
115 void *handle;
116 void *lhandle;
118 int i, j;
119 u_short c, d;
120 u_char *fnend = fn + fnlen, *isoend = isofn + isolen;
121 for (; fn < fnend; ) {
122 d = sgetrune(fn, fnend - fn, (char const **)&fn, flags, lhandle)
124 if (isofn == isoend)
125 return d;
126 isofn += isochar(isofn, isoend, joliet_level, &c, NULL, flags, handle);
127 if (c == ';') {
128 if (d != ';')
129 return d;
130 for (i = 0; fn < fnend; i = i * 10 + *fn++ - '0') {
131 if (*fn < '0' || *fn > '9') {
132 return -1;
135 for (j = 0; isofn != isoend; j = j * 10 + c - '0')
136 isofn += isochar(isofn, isoend,
137 joliet_level, &c,
138 NULL, flags, handle);
139 return i - j;
141 if (c != d) {
142 if (c >= 'A' && c <= 'Z') {
143 if (c + ('a' - 'A') != d) {
144 if (d >= 'a' && d <= 'z')
145 return d - ('a' - 'A') - c;
146 else
147 return d - c;
149 } else
150 return d - c;
153 if (isofn != isoend) {
154 isofn += isochar(isofn, isoend, joliet_level, &c, NULL, flags, handle);
155 switch (c) {
156 default:
157 return -c;
158 case '.':
159 if (isofn != isoend) {
160 isochar(isofn, isoend, joliet_level, &c,
161 NULL, flags, handle);
162 if (c == ';')
163 return 0;
165 return -1;
166 case ';':
167 return 0;
170 return 0;
174 * translate a filename of length > 0
176 isofntrans(infn, infnlen, outfn, outfnlen, original, assoc, joliet_level, flags,
177 handle)
178 u_char *infn;
179 int infnlen;
180 u_char *outfn;
181 u_short *outfnlen;
182 int original;
183 int assoc;
184 int joliet_level;
185 int flags;
186 void *handle;
188 u_short c, d = '\0';
189 u_char *outp = outfn, *infnend = infn + infnlen;
190 int clen;
191 if (assoc) {
192 *outp++ = ASSOCCHAR;
194 for (; infn != infnend; ) {
195 infn += isochar(infn, infnend, joliet_level, &c, &clen, flags, handle);
196 if (!original && !joliet_level && c >= 'A' && c <= 'Z')
197 c += ('a' - 'A');
198 else if (!original && c == ';') {
199 outp -= (d == '.');
200 break;
202 d = c;
203 while(clen--)
204 *outp++ = c >> (clen << 3);
206 *outfnlen = outp - outfn;
210 * same as sgetrune(3)
212 u_short
213 sgetrune(string, n, result, flags, handle)
214 const char *string;
215 size_t n;
216 char const **result;
217 int flags;
218 void *handle;
220 size_t i, j, len;
221 char outbuf[3], *outp;
222 u_short c = '\0';
223 len = i = (n < 2) ? n : 2;
224 j = 2;
225 outp = outbuf;
226 if (flags & ISOFSMNT_KICONV && cd9660_iconv) {
227 cd9660_iconv->convchr(handle, (const char **)&string,
228 &i, &outp, &j);
229 len -= i;
230 } else {
231 len = 1;
232 string++;
234 if (result) *result = string;
235 while(len--) c |= (*(string - len - 1) & 0xff) << (len << 3);
236 return (c);