1138568Ssam/*-
2138568Ssam * Copyright (c) 1994
3178354Ssam *	The Regents of the University of California.  All rights reserved.
4138568Ssam *
5138568Ssam * This code is derived from software contributed to Berkeley
6138568Ssam * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
7138568Ssam * Support code is derived from software contributed to Berkeley
8138568Ssam * by Atsushi Murai (amurai@spec.co.jp). Joliet support was added by
9138568Ssam * Joachim Kuebart (joki@kuebart.stuttgart.netsurf.de).
10138568Ssam *
11138568Ssam * Redistribution and use in source and binary forms, with or without
12138568Ssam * modification, are permitted provided that the following conditions
13138568Ssam * are met:
14138568Ssam * 1. Redistributions of source code must retain the above copyright
15138568Ssam *    notice, this list of conditions and the following disclaimer.
16138568Ssam * 2. Redistributions in binary form must reproduce the above copyright
17138568Ssam *    notice, this list of conditions and the following disclaimer in the
18138568Ssam *    documentation and/or other materials provided with the distribution.
19138568Ssam * 4. Neither the name of the University nor the names of its contributors
20138568Ssam *    may be used to endorse or promote products derived from this software
21138568Ssam *    without specific prior written permission.
22138568Ssam *
23138568Ssam * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24138568Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25138568Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26138568Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27138568Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28138568Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29138568Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30138568Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31138568Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32138568Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33138568Ssam * SUCH DAMAGE.
34138568Ssam *
35138568Ssam *	@(#)cd9660_util.c	8.3 (Berkeley) 12/5/94
36138568Ssam */
37138568Ssam
38138568Ssam#include <sys/cdefs.h>
39138568Ssam__FBSDID("$FreeBSD$");
40138568Ssam
41138568Ssam#include <sys/param.h>
42178354Ssam#include <sys/systm.h>
43178354Ssam#include <sys/mount.h>
44138568Ssam#include <sys/vnode.h>
45138568Ssam#include <sys/iconv.h>
46138568Ssam
47138568Ssam#include <fs/cd9660/iso.h>
48138568Ssam#include <fs/cd9660/cd9660_mount.h>
49138568Ssam
50138568Ssamextern struct iconv_functions *cd9660_iconv;
51138568Ssam
52138568Ssam/*
53138568Ssam * Get one character out of an iso filename
54138568Ssam * Obey joliet_level
55138568Ssam * Return number of bytes consumed
56138568Ssam */
57138568Ssamint
58138568Ssamisochar(isofn, isoend, joliet_level, c, clen, flags, handle)
59178354Ssam      u_char *isofn;
60178354Ssam      u_char *isoend;
61178354Ssam      int joliet_level;
62138568Ssam      u_short *c;
63138568Ssam      int *clen;
64138568Ssam      int flags;
65138568Ssam      void *handle;
66138568Ssam{
67138568Ssam      size_t i, j, len;
68138568Ssam      char inbuf[3], outbuf[3], *inp, *outp;
69138568Ssam
70138568Ssam      *c = *isofn++;
71138568Ssam      if (clen) *clen = 1;
72138568Ssam      if (joliet_level == 0 || isofn == isoend)
73138568Ssam              /* (00) and (01) are one byte in Joliet, too */
74178354Ssam              return 1;
75178354Ssam
76178354Ssam      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, __DECONST(const char **, &inp), &i,
84                  &outp, &j);
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; ) {
125		d = sgetrune(fn, fnend - fn, __DECONST(const char **, &fn),
126		    flags, lhandle);
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}
246