msdosfs_conv.c revision 33744
1/*	$Id: msdosfs_conv.c,v 1.15 1998/02/18 09:28:31 jkh Exp $ */
2/*	$NetBSD: msdosfs_conv.c,v 1.25 1997/11/17 15:36:40 ws Exp $	*/
3
4/*-
5 * Copyright (C) 1995, 1997 Wolfgang Solfrank.
6 * Copyright (C) 1995, 1997 TooLs GmbH.
7 * All rights reserved.
8 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by TooLs GmbH.
21 * 4. The name of TooLs GmbH may not be used to endorse or promote products
22 *    derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35/*
36 * Written by Paul Popelka (paulp@uts.amdahl.com)
37 *
38 * You can do anything you want with this software, just don't say you wrote
39 * it, and don't remove this notice.
40 *
41 * This software is provided "as is".
42 *
43 * The author supplies this software to be publicly redistributed on the
44 * understanding that the author is not responsible for the correct
45 * functioning of this software in any circumstances and is not liable for
46 * any damages caused by this software.
47 *
48 * October 1992
49 */
50
51/*
52 * System include files.
53 */
54#include <sys/param.h>
55#include <sys/time.h>
56#include <sys/kernel.h>		/* defines tz */
57#include <sys/systm.h>
58#include <machine/clock.h>
59#include <sys/dirent.h>
60
61/*
62 * MSDOSFS include files.
63 */
64#include <msdosfs/direntry.h>
65
66/*
67 * Total number of days that have passed for each month in a regular year.
68 */
69static u_short regyear[] = {
70	31, 59, 90, 120, 151, 181,
71	212, 243, 273, 304, 334, 365
72};
73
74/*
75 * Total number of days that have passed for each month in a leap year.
76 */
77static u_short leapyear[] = {
78	31, 60, 91, 121, 152, 182,
79	213, 244, 274, 305, 335, 366
80};
81
82/*
83 * Variables used to remember parts of the last time conversion.  Maybe we
84 * can avoid a full conversion.
85 */
86static u_long  lasttime;
87static u_long  lastday;
88static u_short lastddate;
89static u_short lastdtime;
90
91/*
92 * Convert the unix version of time to dos's idea of time to be used in
93 * file timestamps. The passed in unix time is assumed to be in GMT.
94 */
95void
96unix2dostime(tsp, ddp, dtp, dhp)
97	struct timespec *tsp;
98	u_int16_t *ddp;
99	u_int16_t *dtp;
100	u_int8_t *dhp;
101{
102	u_long t;
103	u_long days;
104	u_long inc;
105	u_long year;
106	u_long month;
107	u_short *months;
108
109	/*
110	 * If the time from the last conversion is the same as now, then
111	 * skip the computations and use the saved result.
112	 */
113	t = tsp->tv_sec - (tz.tz_minuteswest * 60)
114	    - (wall_cmos_clock ? adjkerntz : 0);
115	    /* - daylight savings time correction */
116	t &= ~1;
117	if (lasttime != t) {
118		lasttime = t;
119		lastdtime = (((t / 2) % 30) << DT_2SECONDS_SHIFT)
120		    + (((t / 60) % 60) << DT_MINUTES_SHIFT)
121		    + (((t / 3600) % 24) << DT_HOURS_SHIFT);
122
123		/*
124		 * If the number of days since 1970 is the same as the last
125		 * time we did the computation then skip all this leap year
126		 * and month stuff.
127		 */
128		days = t / (24 * 60 * 60);
129		if (days != lastday) {
130			lastday = days;
131			for (year = 1970;; year++) {
132				inc = year & 0x03 ? 365 : 366;
133				if (days < inc)
134					break;
135				days -= inc;
136			}
137			months = year & 0x03 ? regyear : leapyear;
138			for (month = 0; days >= months[month]; month++)
139				;
140			if (month > 0)
141				days -= months[month - 1];
142			lastddate = ((days + 1) << DD_DAY_SHIFT)
143			    + ((month + 1) << DD_MONTH_SHIFT);
144			/*
145			 * Remember dos's idea of time is relative to 1980.
146			 * unix's is relative to 1970.  If somehow we get a
147			 * time before 1980 then don't give totally crazy
148			 * results.
149			 */
150			if (year > 1980)
151				lastddate += (year - 1980) << DD_YEAR_SHIFT;
152		}
153	}
154	if (dtp)
155		*dtp = lastdtime;
156	if (dhp)
157		*dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000;
158
159	*ddp = lastddate;
160}
161
162/*
163 * The number of seconds between Jan 1, 1970 and Jan 1, 1980. In that
164 * interval there were 8 regular years and 2 leap years.
165 */
166#define	SECONDSTO1980	(((8 * 365) + (2 * 366)) * (24 * 60 * 60))
167
168static u_short lastdosdate;
169static u_long  lastseconds;
170
171/*
172 * Convert from dos' idea of time to unix'. This will probably only be
173 * called from the stat(), and fstat() system calls and so probably need
174 * not be too efficient.
175 */
176void
177dos2unixtime(dd, dt, dh, tsp)
178	u_int dd;
179	u_int dt;
180	u_int dh;
181	struct timespec *tsp;
182{
183	u_long seconds;
184	u_long month;
185	u_long year;
186	u_long days;
187	u_short *months;
188
189	if (dd == 0) {
190		/*
191		 * Uninitialized field, return the epoch.
192		 */
193		tsp->tv_sec = 0;
194		tsp->tv_nsec = 0;
195		return;
196	}
197	seconds = (((dt & DT_2SECONDS_MASK) >> DT_2SECONDS_SHIFT) << 1)
198	    + ((dt & DT_MINUTES_MASK) >> DT_MINUTES_SHIFT) * 60
199	    + ((dt & DT_HOURS_MASK) >> DT_HOURS_SHIFT) * 3600
200	    + dh / 100;
201	/*
202	 * If the year, month, and day from the last conversion are the
203	 * same then use the saved value.
204	 */
205	if (lastdosdate != dd) {
206		lastdosdate = dd;
207		days = 0;
208		year = (dd & DD_YEAR_MASK) >> DD_YEAR_SHIFT;
209		days = year * 365;
210		days += year / 4 + 1;	/* add in leap days */
211		if ((year & 0x03) == 0)
212			days--;		/* if year is a leap year */
213		months = year & 0x03 ? regyear : leapyear;
214		month = (dd & DD_MONTH_MASK) >> DD_MONTH_SHIFT;
215		if (month < 1 || month > 12) {
216			printf("dos2unixtime(): month value out of range (%ld)\n",
217			    month);
218			month = 1;
219		}
220		if (month > 1)
221			days += months[month - 2];
222		days += ((dd & DD_DAY_MASK) >> DD_DAY_SHIFT) - 1;
223		lastseconds = (days * 24 * 60 * 60) + SECONDSTO1980;
224	}
225	tsp->tv_sec = seconds + lastseconds + (tz.tz_minuteswest * 60)
226	     + adjkerntz;
227	     /* + daylight savings time correction */
228	tsp->tv_nsec = (dh % 100) * 10000000;
229}
230
231static u_char
232unix2dos[256] = {
233	0,    0,    0,    0,    0,    0,    0,    0,	/* 00-07 */
234	0,    0,    0,    0,    0,    0,    0,    0,	/* 08-0f */
235	0,    0,    0,    0,    0,    0,    0,    0,	/* 10-17 */
236	0,    0,    0,    0,    0,    0,    0,    0,	/* 18-1f */
237	0,    0x21, 0,    0x23, 0x24, 0x25, 0x26, 0x27,	/* 20-27 */
238	0x28, 0x29, 0,    0,    0,    0x2d, 0,    0,	/* 28-2f */
239	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,	/* 30-37 */
240	0x38, 0x39, 0,    0,    0,    0,    0,    0,	/* 38-3f */
241	0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 40-47 */
242	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 48-4f */
243	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 50-57 */
244	0x58, 0x59, 0x5a, 0,    0,    0,    0x5e, 0x5f,	/* 58-5f */
245	0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 60-67 */
246	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 68-6f */
247	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 70-77 */
248	0x58, 0x59, 0x5a, 0x7b, 0,    0x7d, 0x7e, 0,	/* 78-7f */
249	0,    0,    0,    0,    0,    0,    0,    0,	/* 80-87 */
250	0,    0,    0,    0,    0,    0,    0,    0,	/* 88-8f */
251	0,    0,    0,    0,    0,    0,    0,    0,	/* 90-97 */
252	0,    0,    0,    0,    0,    0,    0,    0,	/* 98-9f */
253	0,    0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5,	/* a0-a7 */
254	0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee,	/* a8-af */
255	0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa,	/* b0-b7 */
256	0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8,	/* b8-bf */
257	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* c0-c7 */
258	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* c8-cf */
259	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e,	/* d0-d7 */
260	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1,	/* d8-df */
261	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* e0-e7 */
262	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* e8-ef */
263	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6,	/* f0-f7 */
264	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98,	/* f8-ff */
265};
266
267static u_char
268dos2unix[256] = {
269	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 00-07 */
270	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 08-0f */
271	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 10-17 */
272	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,	/* 18-1f */
273	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,	/* 20-27 */
274	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,	/* 28-2f */
275	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,	/* 30-37 */
276	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,	/* 38-3f */
277	0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,	/* 40-47 */
278	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,	/* 48-4f */
279	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,	/* 50-57 */
280	0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,	/* 58-5f */
281	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,	/* 60-67 */
282	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,	/* 68-6f */
283	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,	/* 70-77 */
284	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,	/* 78-7f */
285	0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,	/* 80-87 */
286	0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5,	/* 88-8f */
287	0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9,	/* 90-97 */
288	0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, 0x3f,	/* 98-9f */
289	0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba,	/* a0-a7 */
290	0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb,	/* a8-af */
291	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xc1, 0xc2, 0xc0,	/* b0-b7 */
292	0xa9, 0x3f, 0x3f, 0x3f, 0x3f, 0xa2, 0xa5, 0x3f,	/* b8-bf */
293	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xe3, 0xc3,	/* c0-c7 */
294	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xa4,	/* c8-cf */
295	0xf0, 0xd0, 0xca, 0xcb, 0xc8, 0x3f, 0xcd, 0xce,	/* d0-d7 */
296	0xcf, 0x3f, 0x3f, 0x3f, 0x3f, 0xa6, 0xcc, 0x3f,	/* d8-df */
297	0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe,	/* e0-e7 */
298	0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f,	/* e8-ef */
299	0xad, 0xb1, 0x3f, 0xbe, 0xb6, 0xa7, 0xf7, 0xb8,	/* f0-f7 */
300	0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2, 0x3f, 0x3f,	/* f8-ff */
301};
302
303static u_char
304u2l[256] = {
305	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
306	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
307	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
308	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
309	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
310	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
311	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
312	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
313	0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
314	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
315	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
316	0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
317	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
318	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
319	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
320	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
321	0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
322	0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
323	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
324	0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
325	0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
326	0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
327	0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
328	0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
329	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
330	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
331	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
332	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
333	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
334	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
335	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
336	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
337};
338
339/* UNICODE Cyrillic to local code table conversion */
340/* will be loadable in future */
341
342static u_char
343cyr2u[256] = {  /* defaulted to KOI8-R */
3440x00, /* */
3450xb3, /* CYRILLIC CAPITAL LETTER IO */
3460x00, /* CYRILLIC CAPITAL LETTER DJE */
3470x00, /* CYRILLIC CAPITAL LETTER GJE */
3480x00, /* CYRILLIC CAPITAL LETTER UKRAINIAN IE */
3490x00, /* CYRILLIC CAPITAL LETTER DZE */
3500x00, /* CYRILLIC CAPITAL LETTER BYELORUSSIAN-UKRAINIAN I */
3510x00, /* CYRILLIC CAPITAL LETTER YI */
3520x00, /* CYRILLIC CAPITAL LETTER JE */
3530x00, /* CYRILLIC CAPITAL LETTER LJE */
3540x00, /* CYRILLIC CAPITAL LETTER NJE */
3550x00, /* CYRILLIC CAPITAL LETTER TSHE */
3560x00, /* CYRILLIC CAPITAL LETTER KJE */
3570x00, /* */
3580x00, /* CYRILLIC CAPITAL LETTER SHORT U */
3590x00, /* CYRILLIC CAPITAL LETTER DZHE */
3600xe1, /* CYRILLIC CAPITAL LETTER A */
3610xe2, /* CYRILLIC CAPITAL LETTER BE */
3620xf7, /* CYRILLIC CAPITAL LETTER VE */
3630xe7, /* CYRILLIC CAPITAL LETTER GHE */
3640xe4, /* CYRILLIC CAPITAL LETTER DE */
3650xe5, /* CYRILLIC CAPITAL LETTER IE */
3660xf6, /* CYRILLIC CAPITAL LETTER ZHE */
3670xfa, /* CYRILLIC CAPITAL LETTER ZE */
3680xe9, /* CYRILLIC CAPITAL LETTER I */
3690xea, /* CYRILLIC CAPITAL LETTER SHORT I */
3700xeb, /* CYRILLIC CAPITAL LETTER KA */
3710xe4, /* CYRILLIC CAPITAL LETTER EL */
3720xed, /* CYRILLIC CAPITAL LETTER EM */
3730xee, /* CYRILLIC CAPITAL LETTER EN */
3740xef, /* CYRILLIC CAPITAL LETTER O */
3750xf0, /* CYRILLIC CAPITAL LETTER PE */
3760xf2, /* CYRILLIC CAPITAL LETTER ER */
3770xf3, /* CYRILLIC CAPITAL LETTER ES */
3780xf4, /* CYRILLIC CAPITAL LETTER TE */
3790xf5, /* CYRILLIC CAPITAL LETTER U */
3800xe6, /* CYRILLIC CAPITAL LETTER EF */
3810xe8, /* CYRILLIC CAPITAL LETTER HA */
3820xe3, /* CYRILLIC CAPITAL LETTER TSE */
3830xfe, /* CYRILLIC CAPITAL LETTER CHE */
3840xfb, /* CYRILLIC CAPITAL LETTER SHA */
3850xfd, /* CYRILLIC CAPITAL LETTER SHCHA */
3860xff, /* CYRILLIC CAPITAL LETTER HARD SIGN */
3870xf9, /* CYRILLIC CAPITAL LETTER YERU */
3880xf8, /* CYRILLIC CAPITAL LETTER SOFT SIGN */
3890xfc, /* CYRILLIC CAPITAL LETTER E */
3900xe0, /* CYRILLIC CAPITAL LETTER YU */
3910xf1, /* CYRILLIC CAPITAL LETTER YA */
3920xc1, /* CYRILLIC SMALL LETTER A */
3930xc2, /* CYRILLIC SMALL LETTER BE */
3940xd7, /* CYRILLIC SMALL LETTER VE */
3950xc7, /* CYRILLIC SMALL LETTER GHE */
3960xc4, /* CYRILLIC SMALL LETTER DE */
3970xc5, /* CYRILLIC SMALL LETTER IE */
3980xd6, /* CYRILLIC SMALL LETTER ZHE */
3990xda, /* CYRILLIC SMALL LETTER ZE */
4000xc9, /* CYRILLIC SMALL LETTER I */
4010xca, /* CYRILLIC SMALL LETTER SHORT I */
4020xcb, /* CYRILLIC SMALL LETTER KA */
4030xcc, /* CYRILLIC SMALL LETTER EL */
4040xcd, /* CYRILLIC SMALL LETTER EM */
4050xce, /* CYRILLIC SMALL LETTER EN */
4060xcf, /* CYRILLIC SMALL LETTER O */
4070xd0, /* CYRILLIC SMALL LETTER PE */
4080xd2, /* CYRILLIC SMALL LETTER ER */
4090xd3, /* CYRILLIC SMALL LETTER ES */
4100xd4, /* CYRILLIC SMALL LETTER TE */
4110xd5, /* CYRILLIC SMALL LETTER U */
4120xc6, /* CYRILLIC SMALL LETTER EF */
4130xc8, /* CYRILLIC SMALL LETTER HA */
4140xc3, /* CYRILLIC SMALL LETTER TSE */
4150xde, /* CYRILLIC SMALL LETTER CHE */
4160xdb, /* CYRILLIC SMALL LETTER SHA */
4170xdd, /* CYRILLIC SMALL LETTER SHCHA */
4180xdf, /* CYRILLIC SMALL LETTER HARD SIGN */
4190xd9, /* CYRILLIC SMALL LETTER YERU */
4200xd8, /* CYRILLIC SMALL LETTER SOFT SIGN */
4210xdc, /* CYRILLIC SMALL LETTER E */
4220xc0, /* CYRILLIC SMALL LETTER YU */
4230xd1, /* CYRILLIC SMALL LETTER YA */
4240x00, /* */
4250xa3, /* CYRILLIC SMALL LETTER IO */
4260x00, /* CYRILLIC SMALL LETTER DJE */
4270x00, /* CYRILLIC SMALL LETTER GJE */
4280x00, /* CYRILLIC SMALL LETTER UKRAINIAN IE */
4290x00, /* CYRILLIC SMALL LETTER DZE */
4300x00, /* CYRILLIC SMALL LETTER BYELORUSSIAN-UKRAINIAN I */
4310x00, /* CYRILLIC SMALL LETTER YI */
4320x00, /* CYRILLIC SMALL LETTER JE */
4330x00, /* CYRILLIC SMALL LETTER LJE */
4340x00, /* CYRILLIC SMALL LETTER NJE */
4350x00, /* CYRILLIC SMALL LETTER TSHE */
4360x00, /* CYRILLIC SMALL LETTER KJE */
4370x00, /* */
4380x00, /* CYRILLIC SMALL LETTER SHORT U */
4390x00, /* CYRILLIC SMALL LETTER DZHE */
4400x00, /* CYRILLIC CAPITAL LETTER OMEGA */
4410x00, /* CYRILLIC SMALL LETTER OMEGA */
4420x00, /* CYRILLIC CAPITAL LETTER YAT */
4430x00, /* CYRILLIC SMALL LETTER YAT */
4440x00, /* CYRILLIC CAPITAL LETTER IOTIFIED E */
4450x00, /* CYRILLIC SMALL LETTER IOTIFIED E */
4460x00, /* CYRILLIC CAPITAL LETTER LITTLE YUS */
4470x00, /* CYRILLIC SMALL LETTER LITTLE YUS */
4480x00, /* CYRILLIC CAPITAL LETTER IOTIFIED LITTLE YUS */
4490x00, /* CYRILLIC SMALL LETTER IOTIFIED LITTLE YUS */
4500x00, /* CYRILLIC CAPITAL LETTER BIG YUS */
4510x00, /* CYRILLIC SMALL LETTER BIG YUS */
4520x00, /* CYRILLIC CAPITAL LETTER IOTIFIED BIG YUS */
4530x00, /* CYRILLIC SMALL LETTER IOTIFIED BIG YUS */
4540x00, /* CYRILLIC CAPITAL LETTER KSI */
4550x00, /* CYRILLIC SMALL LETTER KSI */
4560x00, /* CYRILLIC CAPITAL LETTER PSI */
4570x00, /* CYRILLIC SMALL LETTER PSI */
4580x00, /* CYRILLIC CAPITAL LETTER FITA */
4590x00, /* CYRILLIC SMALL LETTER FITA */
4600x00, /* CYRILLIC CAPITAL LETTER IZHITSA */
4610x00, /* CYRILLIC SMALL LETTER IZHITSA */
4620x00, /* CYRILLIC CAPITAL LETTER IZHITSA WITH DOUBLE GRAVE ACCENT */
4630x00, /* CYRILLIC SMALL LETTER IZHITSA WITH DOUBLE GRAVE ACCENT */
4640x00, /* CYRILLIC CAPITAL LETTER UK */
4650x00, /* CYRILLIC SMALL LETTER UK */
4660x00, /* CYRILLIC CAPITAL LETTER ROUND OMEGA */
4670x00, /* CYRILLIC SMALL LETTER ROUND OMEGA */
4680x00, /* CYRILLIC CAPITAL LETTER OMEGA WITH TITLO */
4690x00, /* CYRILLIC SMALL LETTER OMEGA WITH TITLO */
4700x00, /* CYRILLIC CAPITAL LETTER OT */
4710x00, /* CYRILLIC SMALL LETTER OT */
4720x00, /* CYRILLIC CAPITAL LETTER KOPPA */
4730x00, /* CYRILLIC SMALL LETTER KOPPA */
4740x00, /* CYRILLIC THOUSANDS SIGN */
4750x00, /* */
4760x00, /* */
4770x00, /* */
4780x00, /* */
4790x00, /* */
4800x00, /* */
4810x00, /* */
4820x00, /* */
4830x00, /* */
4840x00, /* */
4850x00, /* */
4860x00, /* */
4870x00, /* */
4880x00, /* CYRILLIC CAPITAL LETTER GHE WITH UPTURN */
4890x00, /* CYRILLIC SMALL LETTER GHE WITH UPTURN */
4900x00, /* CYRILLIC CAPITAL LETTER GHE WITH STROKE */
4910x00, /* CYRILLIC SMALL LETTER GHE WITH STROKE */
4920x00, /* CYRILLIC CAPITAL LETTER GHE WITH MIDDLE HOOK */
4930x00, /* CYRILLIC SMALL LETTER GHE WITH MIDDLE HOOK */
4940x00, /* CYRILLIC CAPITAL LETTER ZHE WITH DESCENDER */
4950x00, /* CYRILLIC SMALL LETTER ZHE WITH DESCENDER */
4960x00, /* CYRILLIC CAPITAL LETTER ZE WITH DESCENDER */
4970x00, /* CYRILLIC SMALL LETTER ZE WITH DESCENDER */
4980x00, /* CYRILLIC CAPITAL LETTER KA WITH DESCENDER */
4990x00, /* CYRILLIC SMALL LETTER KA WITH DESCENDER */
5000x00, /* CYRILLIC CAPITAL LETTER KA WITH VERTICAL STROKE */
5010x00, /* CYRILLIC SMALL LETTER KA WITH VERTICAL STROKE */
5020x00, /* CYRILLIC CAPITAL LETTER KA WITH STROKE */
5030x00, /* CYRILLIC SMALL LETTER KA WITH STROKE */
5040x00, /* CYRILLIC CAPITAL LETTER BASHKIR KA */
5050x00, /* CYRILLIC SMALL LETTER BASHKIR KA */
5060x00, /* CYRILLIC CAPITAL LETTER EN WITH DESCENDER */
5070x00, /* CYRILLIC SMALL LETTER EN WITH DESCENDER */
5080x00, /* CYRILLIC CAPITAL LIGATURE EN GHE */
5090x00, /* CYRILLIC SMALL LIGATURE EN GHE */
5100x00, /* CYRILLIC CAPITAL LETTER PE WITH MIDDLE HOOK */
5110x00, /* CYRILLIC SMALL LETTER PE WITH MIDDLE HOOK */
5120x00, /* CYRILLIC CAPITAL LETTER ABKHASIAN HA */
5130x00, /* CYRILLIC SMALL LETTER ABKHASIAN HA */
5140x00, /* CYRILLIC CAPITAL LETTER ES WITH DESCENDER */
5150x00, /* CYRILLIC SMALL LETTER ES WITH DESCENDER */
5160x00, /* CYRILLIC CAPITAL LETTER TE WITH DESCENDER */
5170x00, /* CYRILLIC SMALL LETTER TE WITH DESCENDER */
5180x00, /* CYRILLIC CAPITAL LETTER STRAIGHT U */
5190x00, /* CYRILLIC SMALL LETTER STRAIGHT U */
5200x00, /* CYRILLIC CAPITAL LETTER STRAIGHT U WITH STROKE */
5210x00, /* CYRILLIC SMALL LETTER STRAIGHT U WITH STROKE */
5220x00, /* CYRILLIC CAPITAL LETTER HA WITH DESCENDER */
5230x00, /* CYRILLIC SMALL LETTER HA WITH DESCENDER */
5240x00, /* CYRILLIC CAPITAL LIGATURE TE TSE */
5250x00, /* CYRILLIC SMALL LIGATURE TE TSE */
5260x00, /* CYRILLIC CAPITAL LETTER CHE WITH DESCENDER */
5270x00, /* CYRILLIC SMALL LETTER CHE WITH DESCENDER */
5280x00, /* CYRILLIC CAPITAL LETTER CHE WITH VERTICAL STROKE */
5290x00, /* CYRILLIC SMALL LETTER CHE WITH VERTICAL STROKE */
5300x00, /* CYRILLIC CAPITAL LETTER SHHA */
5310x00, /* CYRILLIC SMALL LETTER SHHA */
5320x00, /* CYRILLIC CAPITAL LETTER ABKHASIAN CHE */
5330x00, /* CYRILLIC SMALL LETTER ABKHASIAN CHE */
5340x00, /* CYRILLIC CAPITAL LETTER ABKHASIAN CHE WITH DESCENDER */
5350x00, /* CYRILLIC SMALL LETTER ABKHASIAN CHE WITH DESCENDER */
5360x00, /* CYRILLIC LETTER PALOCHKA */
5370x00, /* CYRILLIC CAPITAL LETTER ZHE WITH BREVE */
5380x00, /* CYRILLIC SMALL LETTER ZHE WITH BREVE */
5390x00, /* CYRILLIC CAPITAL LETTER KA WITH HOOK */
5400x00, /* CYRILLIC SMALL LETTER KA WITH HOOK */
5410x00, /* */
5420x00, /* */
5430x00, /* CYRILLIC CAPITAL LETTER EN WITH HOOK */
5440x00, /* CYRILLIC SMALL LETTER EN WITH HOOK */
5450x00, /* */
5460x00, /* */
5470x00, /* CYRILLIC CAPITAL LETTER KHAKASSIAN CHE */
5480x00, /* CYRILLIC SMALL LETTER KHAKASSIAN CHE */
5490x00, /* */
5500x00, /* */
5510x00, /* */
5520x00, /* CYRILLIC CAPITAL LETTER A WITH BREVE */
5530x00, /* CYRILLIC SMALL LETTER A WITH BREVE */
5540x00, /* CYRILLIC CAPITAL LETTER A WITH DIAERESIS */
5550x00, /* CYRILLIC SMALL LETTER A WITH DIAERESIS */
5560x00, /* CYRILLIC CAPITAL LIGATURE A IE */
5570x00, /* CYRILLIC SMALL LIGATURE A IE */
5580x00, /* CYRILLIC CAPITAL LETTER IE WITH BREVE */
5590x00, /* CYRILLIC SMALL LETTER IE WITH BREVE */
5600x00, /* CYRILLIC CAPITAL LETTER SCHWA */
5610x00, /* CYRILLIC SMALL LETTER SCHWA */
5620x00, /* CYRILLIC CAPITAL LETTER SCHWA WITH DIAERESIS */
5630x00, /* CYRILLIC SMALL LETTER SCHWA WITH DIAERESIS */
5640x00, /* CYRILLIC CAPITAL LETTER ZHE WITH DIAERESIS */
5650x00, /* CYRILLIC SMALL LETTER ZHE WITH DIAERESIS */
5660x00, /* CYRILLIC CAPITAL LETTER ZE WITH DIAERESIS */
5670x00, /* CYRILLIC SMALL LETTER ZE WITH DIAERESIS */
5680x00, /* CYRILLIC CAPITAL LETTER ABKHASIAN DZE */
5690x00, /* CYRILLIC SMALL LETTER ABKHASIAN DZE */
5700x00, /* CYRILLIC CAPITAL LETTER I WITH MACRON */
5710x00, /* CYRILLIC SMALL LETTER I WITH MACRON */
5720x00, /* CYRILLIC CAPITAL LETTER I WITH DIAERESIS */
5730x00, /* CYRILLIC SMALL LETTER I WITH DIAERESIS */
5740x00, /* CYRILLIC CAPITAL LETTER O WITH DIAERESIS */
5750x00, /* CYRILLIC SMALL LETTER O WITH DIAERESIS */
5760x00, /* CYRILLIC CAPITAL LETTER BARRED O */
5770x00, /* CYRILLIC SMALL LETTER BARRED O */
5780x00, /* CYRILLIC CAPITAL LETTER BARRED O WITH DIAERESIS */
5790x00, /* CYRILLIC SMALL LETTER BARRED O WITH DIAERESIS */
5800x00, /* */
5810x00, /* */
5820x00, /* CYRILLIC CAPITAL LETTER U WITH MACRON */
5830x00, /* CYRILLIC SMALL LETTER U WITH MACRON */
5840x00, /* CYRILLIC CAPITAL LETTER U WITH DIAERESIS */
5850x00, /* CYRILLIC SMALL LETTER U WITH DIAERESIS */
5860x00, /* CYRILLIC CAPITAL LETTER U WITH DOUBLE ACUTE */
5870x00, /* CYRILLIC SMALL LETTER U WITH DOUBLE ACUTE */
5880x00, /* CYRILLIC CAPITAL LETTER CHE WITH DIAERESIS */
5890x00, /* CYRILLIC SMALL LETTER CHE WITH DIAERESIS */
5900x00, /* */
5910x00, /* */
5920x00, /* CYRILLIC CAPITAL LETTER YERU WITH DIAERESIS */
5930x00, /* CYRILLIC SMALL LETTER YERU WITH DIAERESIS */
5940x00, /* */
5950x00, /* */
5960x00, /* */
5970x00, /* */
5980x00, /* */
5990x00  /* */
600};
601
602
603/*
604 * DOS filenames are made of 2 parts, the name part and the extension part.
605 * The name part is 8 characters long and the extension part is 3
606 * characters long.  They may contain trailing blanks if the name or
607 * extension are not long enough to fill their respective fields.
608 */
609
610/*
611 * Convert a DOS filename to a unix filename. And, return the number of
612 * characters in the resulting unix filename excluding the terminating
613 * null.
614 */
615int
616dos2unixfn(dn, un, lower)
617	u_char dn[11];
618	u_char *un;
619	int lower;
620{
621	int i;
622	int thislong = 1;
623	u_char c;
624
625	/*
626	 * If first char of the filename is SLOT_E5 (0x05), then the real
627	 * first char of the filename should be 0xe5. But, they couldn't
628	 * just have a 0xe5 mean 0xe5 because that is used to mean a freed
629	 * directory slot. Another dos quirk.
630	 */
631	if (*dn == SLOT_E5)
632		c = dos2unix[0xe5];
633	else
634		c = dos2unix[*dn];
635	*un++ = lower ? u2l[c] : c;
636	dn++;
637
638	/*
639	 * Copy the name portion into the unix filename string.
640	 */
641	for (i = 1; i < 8 && *dn != ' '; i++) {
642		c = dos2unix[*dn++];
643		*un++ = lower ? u2l[c] : c;
644		thislong++;
645	}
646	dn += 8 - i;
647
648	/*
649	 * Now, if there is an extension then put in a period and copy in
650	 * the extension.
651	 */
652	if (*dn != ' ') {
653		*un++ = '.';
654		thislong++;
655		for (i = 0; i < 3 && *dn != ' '; i++) {
656			c = dos2unix[*dn++];
657			*un++ = lower ? u2l[c] : c;
658			thislong++;
659		}
660	}
661	*un++ = 0;
662
663	return (thislong);
664}
665
666/*
667 * Convert a unix filename to a DOS filename according to Win95 rules.
668 * If applicable and gen is not 0, it is inserted into the converted
669 * filename as a generation number.
670 * Returns
671 *	0 if name couldn't be converted
672 *	1 if the converted name is the same as the original
673 *	  (no long filename entry necessary for Win95)
674 *	2 if conversion was successful
675 *	3 if conversion was successful and generation number was inserted
676 */
677int
678unix2dosfn(un, dn, unlen, gen)
679	const u_char *un;
680	u_char dn[12];
681	int unlen;
682	u_int gen;
683{
684	int i, j, l;
685	int conv = 1;
686	const u_char *cp, *dp, *dp1;
687	u_char gentext[6], *wcp;
688
689	/*
690	 * Fill the dos filename string with blanks. These are DOS's pad
691	 * characters.
692	 */
693	for (i = 0; i < 11; i++)
694		dn[i] = ' ';
695	dn[11] = 0;
696
697	/*
698	 * The filenames "." and ".." are handled specially, since they
699	 * don't follow dos filename rules.
700	 */
701	if (un[0] == '.' && unlen == 1) {
702		dn[0] = '.';
703		return gen <= 1;
704	}
705	if (un[0] == '.' && un[1] == '.' && unlen == 2) {
706		dn[0] = '.';
707		dn[1] = '.';
708		return gen <= 1;
709	}
710
711	/*
712	 * Filenames with only blanks and dots are not allowed!
713	 */
714	for (cp = un, i = unlen; --i >= 0; cp++)
715		if (*cp != ' ' && *cp != '.')
716			break;
717	if (i < 0)
718		return 0;
719
720	/*
721	 * Now find the extension
722	 * Note: dot as first char doesn't start extension
723	 *	 and trailing dots and blanks are ignored
724	 */
725	dp = dp1 = 0;
726	for (cp = un + 1, i = unlen - 1; --i >= 0;) {
727		switch (*cp++) {
728		case '.':
729			if (!dp1)
730				dp1 = cp;
731			break;
732		case ' ':
733			break;
734		default:
735			if (dp1)
736				dp = dp1;
737			dp1 = 0;
738			break;
739		}
740	}
741
742	/*
743	 * Now convert it
744	 */
745	if (dp) {
746		if (dp1)
747			l = dp1 - dp;
748		else
749			l = unlen - (dp - un);
750		for (i = 0, j = 8; i < l && j < 11; i++, j++) {
751			if (dp[i] != (dn[j] = unix2dos[dp[i]])
752			    && conv != 3)
753				conv = 2;
754			if (!dn[j]) {
755				conv = 3;
756				dn[j--] = ' ';
757			}
758		}
759		if (i < l)
760			conv = 3;
761		dp--;
762	} else {
763		for (dp = cp; *--dp == ' ' || *dp == '.';);
764		dp++;
765	}
766
767	/*
768	 * Now convert the rest of the name
769	 */
770	for (i = j = 0; un < dp && j < 8; i++, j++, un++) {
771		if (*un != (dn[j] = unix2dos[*un])
772		    && conv != 3)
773			conv = 2;
774		if (!dn[j]) {
775			conv = 3;
776			dn[j--] = ' ';
777		}
778	}
779	if (un < dp)
780		conv = 3;
781	/*
782	 * If we didn't have any chars in filename,
783	 * generate a default
784	 */
785	if (!j)
786		dn[0] = '_';
787
788	/*
789	 * The first character cannot be E5,
790	 * because that means a deleted entry
791	 */
792	if (dn[0] == 0xe5)
793		dn[0] = SLOT_E5;
794
795	/*
796	 * If there wasn't any char dropped,
797	 * there is no place for generation numbers
798	 */
799	if (conv != 3) {
800		if (gen > 1)
801			return 0;
802		return conv;
803	}
804
805	/*
806	 * Now insert the generation number into the filename part
807	 */
808	for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10)
809		*--wcp = gen % 10 + '0';
810	if (gen)
811		return 0;
812	for (i = 8; dn[--i] == ' ';);
813	i++;
814	if (gentext + sizeof(gentext) - wcp + 1 > 8 - i)
815		i = 8 - (gentext + sizeof(gentext) - wcp + 1);
816	dn[i++] = '~';
817	while (wcp < gentext + sizeof(gentext))
818		dn[i++] = *wcp++;
819	return 3;
820}
821
822/*
823 * Create a Win95 long name directory entry
824 * Note: assumes that the filename is valid,
825 *	 i.e. doesn't consist solely of blanks and dots
826 */
827int
828unix2winfn(un, unlen, wep, cnt, chksum)
829	const u_char *un;
830	int unlen;
831	struct winentry *wep;
832	int cnt;
833	int chksum;
834{
835	const u_int8_t *cp;
836	u_int8_t *wcp;
837	int i;
838
839	/*
840	 * Drop trailing blanks and dots
841	 */
842	for (cp = un + unlen; *--cp == ' ' || *cp == '.'; unlen--);
843
844	un += (cnt - 1) * WIN_CHARS;
845	unlen -= (cnt - 1) * WIN_CHARS;
846
847	/*
848	 * Initialize winentry to some useful default
849	 */
850	for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff);
851	wep->weCnt = cnt;
852	wep->weAttributes = ATTR_WIN95;
853	wep->weReserved1 = 0;
854	wep->weChksum = chksum;
855	wep->weReserved2 = 0;
856
857	/*
858	 * Now convert the filename parts
859	 */
860	for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
861		if (--unlen < 0)
862			goto done;
863		*wcp++ = *un++;
864		*wcp++ = 0;
865	}
866	for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
867		if (--unlen < 0)
868			goto done;
869		*wcp++ = *un++;
870		*wcp++ = 0;
871	}
872	for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
873		if (--unlen < 0)
874			goto done;
875		*wcp++ = *un++;
876		*wcp++ = 0;
877	}
878	if (!unlen)
879		wep->weCnt |= WIN_LAST;
880	return unlen;
881
882done:
883	*wcp++ = 0;
884	*wcp++ = 0;
885	wep->weCnt |= WIN_LAST;
886	return 0;
887}
888
889/*
890 * Compare our filename to the one in the Win95 entry
891 * Returns the checksum or -1 if no match
892 */
893int
894winChkName(un, unlen, wep, chksum)
895	const u_char *un;
896	int unlen;
897	struct winentry *wep;
898	int chksum;
899{
900	u_int8_t *cp;
901	int i;
902
903	/*
904	 * First compare checksums
905	 */
906	if (wep->weCnt&WIN_LAST)
907		chksum = wep->weChksum;
908	else if (chksum != wep->weChksum)
909		chksum = -1;
910	if (chksum == -1)
911		return -1;
912
913	/*
914	 * Offset of this entry
915	 */
916	i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS;
917	un += i;
918	if ((unlen -= i) <= 0)
919		return -1;
920	if ((wep->weCnt&WIN_LAST) && unlen > WIN_CHARS)
921		return -1;
922
923	/*
924	 * Compare the name parts
925	 */
926	for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
927		if (--unlen < 0) {
928			if (!*cp++ && !*cp)
929				return chksum;
930			return -1;
931		}
932		if (u2l[*cp++] != u2l[*un++] || *cp++)
933			return -1;
934	}
935	for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
936		if (--unlen < 0) {
937			if (!*cp++ && !*cp)
938				return chksum;
939			return -1;
940		}
941		if (u2l[*cp++] != u2l[*un++] || *cp++)
942			return -1;
943	}
944	for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
945		if (--unlen < 0) {
946			if (!*cp++ && !*cp)
947				return chksum;
948			return -1;
949		}
950		if (u2l[*cp++] != u2l[*un++] || *cp++)
951			return -1;
952	}
953	return chksum;
954}
955
956/*
957 * Convert Win95 filename to dirbuf.
958 * Returns the checksum or -1 if impossible
959 */
960int
961win2unixfn(wep, dp, chksum)
962	struct winentry *wep;
963	struct dirent *dp;
964	int chksum;
965{
966	u_int8_t *cp;
967	u_int8_t *np, *ep = dp->d_name + WIN_MAXLEN;
968	u_int16_t code;
969	int i;
970
971	if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS)
972	    || !(wep->weCnt&WIN_CNT))
973		return -1;
974
975	/*
976	 * First compare checksums
977	 */
978	if (wep->weCnt&WIN_LAST) {
979		chksum = wep->weChksum;
980		/*
981		 * This works even though d_namlen is one byte!
982		 */
983		dp->d_namlen = (wep->weCnt&WIN_CNT) * WIN_CHARS;
984	} else if (chksum != wep->weChksum)
985		chksum = -1;
986	if (chksum == -1)
987		return -1;
988
989	/*
990	 * Offset of this entry
991	 */
992	i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS;
993	np = (u_int8_t *)dp->d_name + i;
994
995	/*
996	 * Convert the name parts
997	 */
998	for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
999		code = (cp[1] << 8) | cp[0];
1000		switch (code) {
1001		case 0:
1002			*np = '\0';
1003			dp->d_namlen -= sizeof(wep->wePart2)/2
1004			    + sizeof(wep->wePart3)/2 + i + 1;
1005			return chksum;
1006		case '/':
1007			*np = '\0';
1008			return -1;
1009		default:
1010			if (code & 0xff00) {
1011				if ((code &~ 0xff) == 0x400)
1012					code = cyr2u[code & 0xff];
1013				else
1014					return -1;
1015			}
1016			*np++ = code;
1017			break;
1018		}
1019		/*
1020		 * The size comparison should result in the compiler
1021		 * optimizing the whole if away
1022		 */
1023		if (WIN_MAXLEN % WIN_CHARS < sizeof(wep->wePart1) / 2
1024		    && np > ep) {
1025			np[-1] = 0;
1026			return -1;
1027		}
1028		cp += 2;
1029	}
1030	for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
1031		code = (cp[1] << 8) | cp[0];
1032		switch (code) {
1033		case 0:
1034			*np = '\0';
1035			dp->d_namlen -= sizeof(wep->wePart3)/2 + i + 1;
1036			return chksum;
1037		case '/':
1038			*np = '\0';
1039			return -1;
1040		default:
1041			if (code & 0xff00) {
1042				if ((code &~ 0xff) == 0x400)
1043					code = cyr2u[code & 0xff];
1044				else
1045					return -1;
1046			}
1047			*np++ = code;
1048			break;
1049		}
1050		/*
1051		 * The size comparisons should be optimized away
1052		 */
1053		if (WIN_MAXLEN % WIN_CHARS >= sizeof(wep->wePart1) / 2
1054		    && WIN_MAXLEN % WIN_CHARS < (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2
1055		    && np > ep) {
1056			np[-1] = 0;
1057			return -1;
1058		}
1059		cp += 2;
1060	}
1061	for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
1062		code = (cp[1] << 8) | cp[0];
1063		switch (code) {
1064		case 0:
1065			*np = '\0';
1066			dp->d_namlen -= i + 1;
1067			return chksum;
1068		case '/':
1069			*np = '\0';
1070			return -1;
1071		default:
1072			if (code & 0xff00) {
1073				if ((code &~ 0xff) == 0x400)
1074					code = cyr2u[code & 0xff];
1075				else
1076					return -1;
1077			}
1078			*np++ = code;
1079			break;
1080		}
1081		/*
1082		 * See above
1083		 */
1084		if (WIN_MAXLEN % WIN_CHARS >= (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2
1085		    && np > ep) {
1086			np[-1] = 0;
1087			return -1;
1088		}
1089		cp += 2;
1090	}
1091	return chksum;
1092}
1093
1094/*
1095 * Compute the checksum of a DOS filename for Win95 use
1096 */
1097u_int8_t
1098winChksum(name)
1099	u_int8_t *name;
1100{
1101	int i;
1102	u_int8_t s;
1103
1104	for (s = 0, i = 11; --i >= 0; s += *name++)
1105		s = (s << 7)|(s >> 1);
1106	return s;
1107}
1108
1109/*
1110 * Determine the number of slots necessary for Win95 names
1111 */
1112int
1113winSlotCnt(un, unlen)
1114	const u_char *un;
1115	int unlen;
1116{
1117	for (un += unlen; unlen > 0; unlen--)
1118		if (*--un != ' ' && *un != '.')
1119			break;
1120	if (unlen > WIN_MAXLEN)
1121		return 0;
1122	return howmany(unlen, WIN_CHARS);
1123}
1124