Expand PMF_FN_* macros.
[netbsd-mini2440.git] / distrib / cdrom / macppc_installboot / cd9660_util.c
blob8f61b5fb5ddf2e61aa1d0daa500f936148fb94bd
1 /* $NetBSD: cd9660_util.c,v 1.1 2006/09/17 03:56:03 tsutsui Exp $ */
3 /*-
4 * Copyright (c) 1994
5 * The Regents of the University of California. All rights reserved.
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).
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
36 * @(#)cd9660_util.c 8.3 (Berkeley) 12/5/94
39 /* from NetBSD: cd9660_util.c,v 1.5 2004/12/28 01:12:26 jdolecek Exp */
41 #if HAVE_NBTOOL_CONFIG_H
42 #include "nbtool_config.h"
43 #endif
45 #include <assert.h>
46 #include <sys/param.h>
47 #include <sys/dirent.h>
49 #include <fs/cd9660/iso.h>
50 #define KASSERT(x) assert(x) /* XXX for <fs/unicode.h> */
51 #include <fs/unicode.h>
53 #include "installboot.h"
55 static int isochar(const u_char *, const u_char *, int, uint16_t *);
56 static uint16_t wget(const u_char **, size_t *, int);
57 static int wput(u_char *, size_t, uint16_t, int);
59 int cd9660_utf8_joliet = 1;
62 * Get one character out of an iso filename
63 * Return number of bytes consumed
65 int
66 isochar(const u_char *isofn, const u_char *isoend, int joliet_level,
67 uint16_t *c)
70 *c = isofn[0];
71 if (joliet_level == 0 || isofn + 1 == isoend) {
72 /* (00) and (01) are one byte in Joliet, too */
73 return 1;
76 if (cd9660_utf8_joliet) {
77 *c = (*c << 8) + isofn[1];
78 } else {
79 /* characters outside ISO-8859-1 subset replaced with '?' */
80 if (*c != 0)
81 *c = '?';
82 else
83 *c = isofn[1];
86 return 2;
90 * translate and compare a filename
91 * Note: Version number plus ';' may be omitted.
93 int
94 isofncmp(const u_char *fn, size_t fnlen, const u_char *isofn, size_t isolen,
95 int joliet_level)
97 int i, j;
98 uint16_t fc, ic;
99 const u_char *isoend = isofn + isolen;
101 #ifdef DEBUG
102 printf("fn = %s, fnlen = %d, isofn = %s, isolen = %d\n",
103 fn, fnlen, isofn, isolen);
104 #endif
106 while (fnlen > 0) {
107 fc = wget(&fn, &fnlen, joliet_level);
109 if (isofn == isoend)
110 return fc;
111 isofn += isochar(isofn, isoend, joliet_level, &ic);
112 if (ic == ';') {
113 switch (fc) {
114 default:
115 return fc;
116 case 0:
117 return 0;
118 case ';':
119 break;
121 fn++;
122 for (i = 0; --fnlen >= 0; i = i * 10 + *fn++ - '0') {
123 if (*fn < '0' || *fn > '9') {
124 return -1;
127 for (j = 0; isofn != isoend; j = j * 10 + ic - '0')
128 isofn += isochar(isofn, isoend,
129 joliet_level, &ic);
130 return i - j;
132 if (ic != fc) {
133 if (ic >= 'A' && ic <= 'Z') {
134 if (ic + ('a' - 'A') != fc) {
135 if (fc >= 'a' && fc <= 'z')
136 fc -= 'a' - 'A';
138 return (int)fc - (int)ic;
140 } else
141 return (int)fc - (int)ic;
144 if (isofn != isoend) {
145 isofn += isochar(isofn, isoend, joliet_level, &ic);
146 switch (ic) {
147 default:
148 return -1;
149 case '.':
150 if (isofn != isoend) {
151 isochar(isofn, isoend, joliet_level, &ic);
152 if (ic == ';')
153 return 0;
155 return -1;
156 case ';':
157 return 0;
160 return 0;
164 * translate a filename
166 void
167 isofntrans(u_char *infn, int infnlen, u_char *outfn, u_short *outfnlen,
168 int original, int casetrans, int assoc, int joliet_level)
170 int fnidx = 0;
171 u_char *infnend = infn + infnlen;
172 uint16_t c;
173 int sz;
175 if (assoc) {
176 *outfn++ = ASSOCCHAR;
177 fnidx++;
180 for(; infn != infnend; fnidx += sz) {
181 infn += isochar(infn, infnend, joliet_level, &c);
183 if (casetrans && joliet_level == 0 && c >= 'A' && c <= 'Z')
184 c = c + ('a' - 'A');
185 else if (!original && c == ';') {
186 if (fnidx > 0 && outfn[-1] == '.')
187 fnidx--;
188 break;
191 sz = wput(outfn, MAXNAMLEN - fnidx, c, joliet_level);
192 if (sz == 0) {
193 /* not enough space to write the character */
194 if (fnidx < MAXNAMLEN) {
195 *outfn = '?';
196 fnidx++;
198 break;
200 outfn += sz;
202 *outfnlen = fnidx;
205 static uint16_t
206 wget(const u_char **str, size_t *sz, int joliet_level)
208 if (joliet_level > 0 && cd9660_utf8_joliet) {
209 /* decode UTF-8 sequence */
210 return wget_utf8((const char **) str, sz);
211 } else {
213 * Raw 8-bit characters without any conversion. For Joliet,
214 * this effectively assumes provided file name is using
215 * ISO-8859-1 subset.
217 uint16_t c = *str[0];
218 (*str)++;
220 return c;
224 static int
225 wput(u_char *s, size_t n, uint16_t c, int joliet_level)
227 if (joliet_level > 0 && cd9660_utf8_joliet) {
228 /* Store Joliet file name encoded into UTF-8 */
229 return wput_utf8((char *)s, n, c);
230 } else {
232 * Store raw 8-bit characters without any conversion.
233 * For Joliet case, this filters the Unicode characters
234 * to ISO-8859-1 subset.
236 *s = (u_char)c;
237 return 1;