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$");
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);
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; ) {
124		d = sgetrune(fn, fnend - fn, (char const **)&fn, 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}
244