xref: /dflybsd-src/usr.sbin/makefs/msdos/msdosfs_conv.c (revision 20f6ddd0df90767e1eba2d12dfa8e1769be7cec7)
1*20f6ddd0STomohiro Kusumi /*-
2*20f6ddd0STomohiro Kusumi  * SPDX-License-Identifier: BSD-4-Clause
3*20f6ddd0STomohiro Kusumi  *
4*20f6ddd0STomohiro Kusumi  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
5*20f6ddd0STomohiro Kusumi  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
6*20f6ddd0STomohiro Kusumi  * All rights reserved.
7*20f6ddd0STomohiro Kusumi  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
8*20f6ddd0STomohiro Kusumi  *
9*20f6ddd0STomohiro Kusumi  * Redistribution and use in source and binary forms, with or without
10*20f6ddd0STomohiro Kusumi  * modification, are permitted provided that the following conditions
11*20f6ddd0STomohiro Kusumi  * are met:
12*20f6ddd0STomohiro Kusumi  * 1. Redistributions of source code must retain the above copyright
13*20f6ddd0STomohiro Kusumi  *    notice, this list of conditions and the following disclaimer.
14*20f6ddd0STomohiro Kusumi  * 2. Redistributions in binary form must reproduce the above copyright
15*20f6ddd0STomohiro Kusumi  *    notice, this list of conditions and the following disclaimer in the
16*20f6ddd0STomohiro Kusumi  *    documentation and/or other materials provided with the distribution.
17*20f6ddd0STomohiro Kusumi  * 3. All advertising materials mentioning features or use of this software
18*20f6ddd0STomohiro Kusumi  *    must display the following acknowledgement:
19*20f6ddd0STomohiro Kusumi  *	This product includes software developed by TooLs GmbH.
20*20f6ddd0STomohiro Kusumi  * 4. The name of TooLs GmbH may not be used to endorse or promote products
21*20f6ddd0STomohiro Kusumi  *    derived from this software without specific prior written permission.
22*20f6ddd0STomohiro Kusumi  *
23*20f6ddd0STomohiro Kusumi  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24*20f6ddd0STomohiro Kusumi  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25*20f6ddd0STomohiro Kusumi  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26*20f6ddd0STomohiro Kusumi  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27*20f6ddd0STomohiro Kusumi  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28*20f6ddd0STomohiro Kusumi  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29*20f6ddd0STomohiro Kusumi  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30*20f6ddd0STomohiro Kusumi  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31*20f6ddd0STomohiro Kusumi  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32*20f6ddd0STomohiro Kusumi  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33*20f6ddd0STomohiro Kusumi  */
34*20f6ddd0STomohiro Kusumi /*-
35*20f6ddd0STomohiro Kusumi  * Written by Paul Popelka (paulp@uts.amdahl.com)
36*20f6ddd0STomohiro Kusumi  *
37*20f6ddd0STomohiro Kusumi  * You can do anything you want with this software, just don't say you wrote
38*20f6ddd0STomohiro Kusumi  * it, and don't remove this notice.
39*20f6ddd0STomohiro Kusumi  *
40*20f6ddd0STomohiro Kusumi  * This software is provided "as is".
41*20f6ddd0STomohiro Kusumi  *
42*20f6ddd0STomohiro Kusumi  * The author supplies this software to be publicly redistributed on the
43*20f6ddd0STomohiro Kusumi  * understanding that the author is not responsible for the correct
44*20f6ddd0STomohiro Kusumi  * functioning of this software in any circumstances and is not liable for
45*20f6ddd0STomohiro Kusumi  * any damages caused by this software.
46*20f6ddd0STomohiro Kusumi  *
47*20f6ddd0STomohiro Kusumi  * October 1992
48*20f6ddd0STomohiro Kusumi  */
49*20f6ddd0STomohiro Kusumi 
50*20f6ddd0STomohiro Kusumi #include <sys/cdefs.h>
51*20f6ddd0STomohiro Kusumi __FBSDID("$FreeBSD$");
52*20f6ddd0STomohiro Kusumi 
53*20f6ddd0STomohiro Kusumi #include <sys/param.h>
54*20f6ddd0STomohiro Kusumi #include <sys/endian.h>
55*20f6ddd0STomohiro Kusumi 
56*20f6ddd0STomohiro Kusumi #include <dirent.h>
57*20f6ddd0STomohiro Kusumi #include <stdio.h>
58*20f6ddd0STomohiro Kusumi #include <string.h>
59*20f6ddd0STomohiro Kusumi 
60*20f6ddd0STomohiro Kusumi #include <vfs/msdosfs/bpb.h>
61*20f6ddd0STomohiro Kusumi #include "msdos/direntry.h"
62*20f6ddd0STomohiro Kusumi #include <vfs/msdosfs/msdosfsmount.h>
63*20f6ddd0STomohiro Kusumi 
64*20f6ddd0STomohiro Kusumi #include "makefs.h"
65*20f6ddd0STomohiro Kusumi #include "msdos.h"
66*20f6ddd0STomohiro Kusumi 
67*20f6ddd0STomohiro Kusumi static int char8ucs2str(const uint8_t *in, int n, uint16_t *out, int m);
68*20f6ddd0STomohiro Kusumi static void ucs2pad(uint16_t *buf, int len, int size);
69*20f6ddd0STomohiro Kusumi static int char8match(uint16_t *w1, uint16_t *w2, int n);
70*20f6ddd0STomohiro Kusumi 
71*20f6ddd0STomohiro Kusumi static const u_char unix2dos[256] = {
72*20f6ddd0STomohiro Kusumi 	0,    0,    0,    0,    0,    0,    0,    0,	/* 00-07 */
73*20f6ddd0STomohiro Kusumi 	0,    0,    0,    0,    0,    0,    0,    0,	/* 08-0f */
74*20f6ddd0STomohiro Kusumi 	0,    0,    0,    0,    0,    0,    0,    0,	/* 10-17 */
75*20f6ddd0STomohiro Kusumi 	0,    0,    0,    0,    0,    0,    0,    0,	/* 18-1f */
76*20f6ddd0STomohiro Kusumi 	0,    '!',  0,    '#',  '$',  '%',  '&',  '\'',	/* 20-27 */
77*20f6ddd0STomohiro Kusumi 	'(',  ')',  0,    '+',  0,    '-',  0,    0,	/* 28-2f */
78*20f6ddd0STomohiro Kusumi 	'0',  '1',  '2',  '3',  '4',  '5',  '6',  '7',	/* 30-37 */
79*20f6ddd0STomohiro Kusumi 	'8',  '9',  0,    0,    0,    0,    0,    0,	/* 38-3f */
80*20f6ddd0STomohiro Kusumi 	'@',  'A',  'B',  'C',  'D',  'E',  'F',  'G',	/* 40-47 */
81*20f6ddd0STomohiro Kusumi 	'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',	/* 48-4f */
82*20f6ddd0STomohiro Kusumi 	'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',	/* 50-57 */
83*20f6ddd0STomohiro Kusumi 	'X',  'Y',  'Z',  0,    0,    0,    '^',  '_',	/* 58-5f */
84*20f6ddd0STomohiro Kusumi 	'`',  'A',  'B',  'C',  'D',  'E',  'F',  'G',	/* 60-67 */
85*20f6ddd0STomohiro Kusumi 	'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',	/* 68-6f */
86*20f6ddd0STomohiro Kusumi 	'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',	/* 70-77 */
87*20f6ddd0STomohiro Kusumi 	'X',  'Y',  'Z',  '{',  0,    '}',  '~',  0,	/* 78-7f */
88*20f6ddd0STomohiro Kusumi 	0,    0,    0,    0,    0,    0,    0,    0,	/* 80-87 */
89*20f6ddd0STomohiro Kusumi 	0,    0,    0,    0,    0,    0,    0,    0,	/* 88-8f */
90*20f6ddd0STomohiro Kusumi 	0,    0,    0,    0,    0,    0,    0,    0,	/* 90-97 */
91*20f6ddd0STomohiro Kusumi 	0,    0,    0,    0,    0,    0,    0,    0,	/* 98-9f */
92*20f6ddd0STomohiro Kusumi 	0,    0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5,	/* a0-a7 */
93*20f6ddd0STomohiro Kusumi 	0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee,	/* a8-af */
94*20f6ddd0STomohiro Kusumi 	0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa,	/* b0-b7 */
95*20f6ddd0STomohiro Kusumi 	0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8,	/* b8-bf */
96*20f6ddd0STomohiro Kusumi 	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* c0-c7 */
97*20f6ddd0STomohiro Kusumi 	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* c8-cf */
98*20f6ddd0STomohiro Kusumi 	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e,	/* d0-d7 */
99*20f6ddd0STomohiro Kusumi 	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1,	/* d8-df */
100*20f6ddd0STomohiro Kusumi 	0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80,	/* e0-e7 */
101*20f6ddd0STomohiro Kusumi 	0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8,	/* e8-ef */
102*20f6ddd0STomohiro Kusumi 	0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6,	/* f0-f7 */
103*20f6ddd0STomohiro Kusumi 	0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98,	/* f8-ff */
104*20f6ddd0STomohiro Kusumi };
105*20f6ddd0STomohiro Kusumi 
106*20f6ddd0STomohiro Kusumi static const u_char u2l[256] = {
107*20f6ddd0STomohiro Kusumi 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
108*20f6ddd0STomohiro Kusumi 	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
109*20f6ddd0STomohiro Kusumi 	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
110*20f6ddd0STomohiro Kusumi 	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
111*20f6ddd0STomohiro Kusumi 	' ',  '!',  '"',  '#',  '$',  '%',  '&', '\'', /* 20-27 */
112*20f6ddd0STomohiro Kusumi 	'(',  ')',  '*',  '+',  ',',  '-',  '.',  '/', /* 28-2f */
113*20f6ddd0STomohiro Kusumi 	'0',  '1',  '2',  '3',  '4',  '5',  '6',  '7', /* 30-37 */
114*20f6ddd0STomohiro Kusumi 	'8',  '9',  ':',  ';',  '<',  '=',  '>',  '?', /* 38-3f */
115*20f6ddd0STomohiro Kusumi 	'@',  'a',  'b',  'c',  'd',  'e',  'f',  'g', /* 40-47 */
116*20f6ddd0STomohiro Kusumi 	'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o', /* 48-4f */
117*20f6ddd0STomohiro Kusumi 	'p',  'q',  'r',  's',  't',  'u',  'v',  'w', /* 50-57 */
118*20f6ddd0STomohiro Kusumi 	'x',  'y',  'z',  '[', '\\',  ']',  '^',  '_', /* 58-5f */
119*20f6ddd0STomohiro Kusumi 	'`',  'a',  'b',  'c',  'd',  'e',  'f',  'g', /* 60-67 */
120*20f6ddd0STomohiro Kusumi 	'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o', /* 68-6f */
121*20f6ddd0STomohiro Kusumi 	'p',  'q',  'r',  's',  't',  'u',  'v',  'w', /* 70-77 */
122*20f6ddd0STomohiro Kusumi 	'x',  'y',  'z',  '{',  '|',  '}',  '~', 0x7f, /* 78-7f */
123*20f6ddd0STomohiro Kusumi 	0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
124*20f6ddd0STomohiro Kusumi 	0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
125*20f6ddd0STomohiro Kusumi 	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
126*20f6ddd0STomohiro Kusumi 	0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
127*20f6ddd0STomohiro Kusumi 	0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
128*20f6ddd0STomohiro Kusumi 	0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
129*20f6ddd0STomohiro Kusumi 	0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
130*20f6ddd0STomohiro Kusumi 	0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
131*20f6ddd0STomohiro Kusumi 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
132*20f6ddd0STomohiro Kusumi 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
133*20f6ddd0STomohiro Kusumi 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
134*20f6ddd0STomohiro Kusumi 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
135*20f6ddd0STomohiro Kusumi 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
136*20f6ddd0STomohiro Kusumi 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
137*20f6ddd0STomohiro Kusumi 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
138*20f6ddd0STomohiro Kusumi 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
139*20f6ddd0STomohiro Kusumi };
140*20f6ddd0STomohiro Kusumi 
141*20f6ddd0STomohiro Kusumi /*
142*20f6ddd0STomohiro Kusumi  * Determine the number of slots necessary for Win95 names
143*20f6ddd0STomohiro Kusumi  */
144*20f6ddd0STomohiro Kusumi int
winSlotCnt(const u_char * un,size_t unlen)145*20f6ddd0STomohiro Kusumi winSlotCnt(const u_char *un, size_t unlen)
146*20f6ddd0STomohiro Kusumi {
147*20f6ddd0STomohiro Kusumi 	const u_char *cp;
148*20f6ddd0STomohiro Kusumi 
149*20f6ddd0STomohiro Kusumi 	/*
150*20f6ddd0STomohiro Kusumi 	 * Drop trailing blanks and dots
151*20f6ddd0STomohiro Kusumi 	 */
152*20f6ddd0STomohiro Kusumi 	for (cp = un + unlen; unlen > 0; unlen--)
153*20f6ddd0STomohiro Kusumi 		if (*--cp != ' ' && *cp != '.')
154*20f6ddd0STomohiro Kusumi 			break;
155*20f6ddd0STomohiro Kusumi 
156*20f6ddd0STomohiro Kusumi 	return howmany(unlen, WIN_CHARS);
157*20f6ddd0STomohiro Kusumi }
158*20f6ddd0STomohiro Kusumi 
159*20f6ddd0STomohiro Kusumi /*
160*20f6ddd0STomohiro Kusumi  * Compare our filename to the one in the Win95 entry
161*20f6ddd0STomohiro Kusumi  * Returns the checksum or -1 if no match
162*20f6ddd0STomohiro Kusumi  */
163*20f6ddd0STomohiro Kusumi int
winChkName(const u_char * un,size_t unlen,struct winentry * wep,int chksum)164*20f6ddd0STomohiro Kusumi winChkName(const u_char *un, size_t unlen, struct winentry *wep, int chksum)
165*20f6ddd0STomohiro Kusumi {
166*20f6ddd0STomohiro Kusumi 	uint16_t wn[WIN_MAXLEN], *p;
167*20f6ddd0STomohiro Kusumi 	uint16_t buf[WIN_CHARS];
168*20f6ddd0STomohiro Kusumi 	int i, len;
169*20f6ddd0STomohiro Kusumi 
170*20f6ddd0STomohiro Kusumi 	/*
171*20f6ddd0STomohiro Kusumi 	 * First compare checksums
172*20f6ddd0STomohiro Kusumi 	 */
173*20f6ddd0STomohiro Kusumi 	if (wep->weCnt & WIN_LAST)
174*20f6ddd0STomohiro Kusumi 		chksum = wep->weChksum;
175*20f6ddd0STomohiro Kusumi 	else if (chksum != wep->weChksum)
176*20f6ddd0STomohiro Kusumi 		chksum = -1;
177*20f6ddd0STomohiro Kusumi 	if (chksum == -1)
178*20f6ddd0STomohiro Kusumi 		return -1;
179*20f6ddd0STomohiro Kusumi 
180*20f6ddd0STomohiro Kusumi 	/*
181*20f6ddd0STomohiro Kusumi 	 * Offset of this entry
182*20f6ddd0STomohiro Kusumi 	 */
183*20f6ddd0STomohiro Kusumi 	i = ((wep->weCnt & WIN_CNT) - 1) * WIN_CHARS;
184*20f6ddd0STomohiro Kusumi 
185*20f6ddd0STomohiro Kusumi 	/*
186*20f6ddd0STomohiro Kusumi 	 * Translate UNIX name to ucs-2
187*20f6ddd0STomohiro Kusumi 	 */
188*20f6ddd0STomohiro Kusumi 	len = char8ucs2str(un, unlen, wn, WIN_MAXLEN);
189*20f6ddd0STomohiro Kusumi 	ucs2pad(wn, len, WIN_MAXLEN);
190*20f6ddd0STomohiro Kusumi 
191*20f6ddd0STomohiro Kusumi 	if (i >= len + 1)
192*20f6ddd0STomohiro Kusumi 		return -1;
193*20f6ddd0STomohiro Kusumi 	if ((wep->weCnt & WIN_LAST) && (len - i > WIN_CHARS))
194*20f6ddd0STomohiro Kusumi 		return -1;
195*20f6ddd0STomohiro Kusumi 
196*20f6ddd0STomohiro Kusumi 	/*
197*20f6ddd0STomohiro Kusumi 	 * Fetch name segment from directory entry
198*20f6ddd0STomohiro Kusumi 	 */
199*20f6ddd0STomohiro Kusumi 	p = &buf[0];
200*20f6ddd0STomohiro Kusumi 	memcpy(p, wep->wePart1, sizeof(wep->wePart1));
201*20f6ddd0STomohiro Kusumi 	p += sizeof(wep->wePart1) / sizeof(*p);
202*20f6ddd0STomohiro Kusumi 	memcpy(p, wep->wePart2, sizeof(wep->wePart2));
203*20f6ddd0STomohiro Kusumi 	p += sizeof(wep->wePart2) / sizeof(*p);
204*20f6ddd0STomohiro Kusumi 	memcpy(p, wep->wePart3, sizeof(wep->wePart3));
205*20f6ddd0STomohiro Kusumi 
206*20f6ddd0STomohiro Kusumi 	/*
207*20f6ddd0STomohiro Kusumi 	 * And compare name segment
208*20f6ddd0STomohiro Kusumi 	 */
209*20f6ddd0STomohiro Kusumi 	if (!(char8match(&wn[i], buf, WIN_CHARS)))
210*20f6ddd0STomohiro Kusumi 		return -1;
211*20f6ddd0STomohiro Kusumi 
212*20f6ddd0STomohiro Kusumi 	return chksum;
213*20f6ddd0STomohiro Kusumi }
214*20f6ddd0STomohiro Kusumi 
215*20f6ddd0STomohiro Kusumi /*
216*20f6ddd0STomohiro Kusumi  * Compute the checksum of a DOS filename for Win95 use
217*20f6ddd0STomohiro Kusumi  */
218*20f6ddd0STomohiro Kusumi uint8_t
winChksum(uint8_t * name)219*20f6ddd0STomohiro Kusumi winChksum(uint8_t *name)
220*20f6ddd0STomohiro Kusumi {
221*20f6ddd0STomohiro Kusumi 	int i;
222*20f6ddd0STomohiro Kusumi 	uint8_t s;
223*20f6ddd0STomohiro Kusumi 
224*20f6ddd0STomohiro Kusumi 	for (s = 0, i = 11; --i >= 0; s += *name++)
225*20f6ddd0STomohiro Kusumi 		s = (s << 7) | (s >> 1);
226*20f6ddd0STomohiro Kusumi 	return s;
227*20f6ddd0STomohiro Kusumi }
228*20f6ddd0STomohiro Kusumi 
229*20f6ddd0STomohiro Kusumi /*
230*20f6ddd0STomohiro Kusumi  * Create a Win95 long name directory entry
231*20f6ddd0STomohiro Kusumi  * Note: assumes that the filename is valid,
232*20f6ddd0STomohiro Kusumi  *	 i.e. doesn't consist solely of blanks and dots
233*20f6ddd0STomohiro Kusumi  */
234*20f6ddd0STomohiro Kusumi int
unix2winfn(const u_char * un,size_t unlen,struct winentry * wep,int cnt,int chksum)235*20f6ddd0STomohiro Kusumi unix2winfn(const u_char *un, size_t unlen, struct winentry *wep, int cnt,
236*20f6ddd0STomohiro Kusumi     int chksum)
237*20f6ddd0STomohiro Kusumi {
238*20f6ddd0STomohiro Kusumi 	uint16_t wn[WIN_MAXLEN], *p;
239*20f6ddd0STomohiro Kusumi 	int i, len;
240*20f6ddd0STomohiro Kusumi 	const u_char *cp;
241*20f6ddd0STomohiro Kusumi 
242*20f6ddd0STomohiro Kusumi 	/*
243*20f6ddd0STomohiro Kusumi 	 * Drop trailing blanks and dots
244*20f6ddd0STomohiro Kusumi 	 */
245*20f6ddd0STomohiro Kusumi 	for (cp = un + unlen; unlen > 0; unlen--)
246*20f6ddd0STomohiro Kusumi 		if (*--cp != ' ' && *cp != '.')
247*20f6ddd0STomohiro Kusumi 			break;
248*20f6ddd0STomohiro Kusumi 
249*20f6ddd0STomohiro Kusumi 	/*
250*20f6ddd0STomohiro Kusumi 	 * Offset of this entry
251*20f6ddd0STomohiro Kusumi 	 */
252*20f6ddd0STomohiro Kusumi 	i = (cnt - 1) * WIN_CHARS;
253*20f6ddd0STomohiro Kusumi 
254*20f6ddd0STomohiro Kusumi 	/*
255*20f6ddd0STomohiro Kusumi 	 * Translate UNIX name to ucs-2
256*20f6ddd0STomohiro Kusumi 	 */
257*20f6ddd0STomohiro Kusumi 	len = char8ucs2str(un, unlen, wn, WIN_MAXLEN);
258*20f6ddd0STomohiro Kusumi 	ucs2pad(wn, len, WIN_MAXLEN);
259*20f6ddd0STomohiro Kusumi 
260*20f6ddd0STomohiro Kusumi 	/*
261*20f6ddd0STomohiro Kusumi 	 * Initialize winentry to some useful default
262*20f6ddd0STomohiro Kusumi 	 */
263*20f6ddd0STomohiro Kusumi 	memset(wep, 0xff, sizeof(*wep));
264*20f6ddd0STomohiro Kusumi 	wep->weCnt = cnt;
265*20f6ddd0STomohiro Kusumi 	wep->weAttributes = ATTR_WIN95;
266*20f6ddd0STomohiro Kusumi 	wep->weReserved1 = 0;
267*20f6ddd0STomohiro Kusumi 	wep->weChksum = chksum;
268*20f6ddd0STomohiro Kusumi 	wep->weReserved2 = 0;
269*20f6ddd0STomohiro Kusumi 
270*20f6ddd0STomohiro Kusumi 	/*
271*20f6ddd0STomohiro Kusumi 	 * Store name segment into directory entry
272*20f6ddd0STomohiro Kusumi 	 */
273*20f6ddd0STomohiro Kusumi 	p = &wn[i];
274*20f6ddd0STomohiro Kusumi 	memcpy(wep->wePart1, p, sizeof(wep->wePart1));
275*20f6ddd0STomohiro Kusumi 	p += sizeof(wep->wePart1) / sizeof(*p);
276*20f6ddd0STomohiro Kusumi 	memcpy(wep->wePart2, p, sizeof(wep->wePart2));
277*20f6ddd0STomohiro Kusumi 	p += sizeof(wep->wePart2) / sizeof(*p);
278*20f6ddd0STomohiro Kusumi 	memcpy(wep->wePart3, p, sizeof(wep->wePart3));
279*20f6ddd0STomohiro Kusumi 
280*20f6ddd0STomohiro Kusumi 	if (len > i + WIN_CHARS)
281*20f6ddd0STomohiro Kusumi 		return 1;
282*20f6ddd0STomohiro Kusumi 
283*20f6ddd0STomohiro Kusumi 	wep->weCnt |= WIN_LAST;
284*20f6ddd0STomohiro Kusumi 	return 0;
285*20f6ddd0STomohiro Kusumi }
286*20f6ddd0STomohiro Kusumi 
287*20f6ddd0STomohiro Kusumi /*
288*20f6ddd0STomohiro Kusumi  * Convert a unix filename to a DOS filename according to Win95 rules.
289*20f6ddd0STomohiro Kusumi  * If applicable and gen is not 0, it is inserted into the converted
290*20f6ddd0STomohiro Kusumi  * filename as a generation number.
291*20f6ddd0STomohiro Kusumi  * Returns
292*20f6ddd0STomohiro Kusumi  *	0 if name couldn't be converted
293*20f6ddd0STomohiro Kusumi  *	1 if the converted name is the same as the original
294*20f6ddd0STomohiro Kusumi  *	  (no long filename entry necessary for Win95)
295*20f6ddd0STomohiro Kusumi  *	2 if conversion was successful
296*20f6ddd0STomohiro Kusumi  *	3 if conversion was successful and generation number was inserted
297*20f6ddd0STomohiro Kusumi  */
298*20f6ddd0STomohiro Kusumi int
unix2dosfn(const u_char * un,u_char dn[12],size_t unlen,u_int gen)299*20f6ddd0STomohiro Kusumi unix2dosfn(const u_char *un, u_char dn[12], size_t unlen, u_int gen)
300*20f6ddd0STomohiro Kusumi {
301*20f6ddd0STomohiro Kusumi 	int i, j, l;
302*20f6ddd0STomohiro Kusumi 	int conv = 1;
303*20f6ddd0STomohiro Kusumi 	const u_char *cp, *dp, *dp1;
304*20f6ddd0STomohiro Kusumi 	u_char gentext[6], *wcp;
305*20f6ddd0STomohiro Kusumi 	int shortlen;
306*20f6ddd0STomohiro Kusumi 
307*20f6ddd0STomohiro Kusumi 	/*
308*20f6ddd0STomohiro Kusumi 	 * Fill the dos filename string with blanks. These are DOS's pad
309*20f6ddd0STomohiro Kusumi 	 * characters.
310*20f6ddd0STomohiro Kusumi 	 */
311*20f6ddd0STomohiro Kusumi 	for (i = 0; i < 11; i++)
312*20f6ddd0STomohiro Kusumi 		dn[i] = ' ';
313*20f6ddd0STomohiro Kusumi 	dn[11] = 0;
314*20f6ddd0STomohiro Kusumi 
315*20f6ddd0STomohiro Kusumi 	/*
316*20f6ddd0STomohiro Kusumi 	 * The filenames "." and ".." are handled specially, since they
317*20f6ddd0STomohiro Kusumi 	 * don't follow dos filename rules.
318*20f6ddd0STomohiro Kusumi 	 */
319*20f6ddd0STomohiro Kusumi 	if (un[0] == '.' && unlen == 1) {
320*20f6ddd0STomohiro Kusumi 		dn[0] = '.';
321*20f6ddd0STomohiro Kusumi 		return gen <= 1;
322*20f6ddd0STomohiro Kusumi 	}
323*20f6ddd0STomohiro Kusumi 	if (un[0] == '.' && un[1] == '.' && unlen == 2) {
324*20f6ddd0STomohiro Kusumi 		dn[0] = '.';
325*20f6ddd0STomohiro Kusumi 		dn[1] = '.';
326*20f6ddd0STomohiro Kusumi 		return gen <= 1;
327*20f6ddd0STomohiro Kusumi 	}
328*20f6ddd0STomohiro Kusumi 
329*20f6ddd0STomohiro Kusumi 	/*
330*20f6ddd0STomohiro Kusumi 	 * Filenames with only blanks and dots are not allowed!
331*20f6ddd0STomohiro Kusumi 	 */
332*20f6ddd0STomohiro Kusumi 	for (cp = un, i = unlen; --i >= 0; cp++)
333*20f6ddd0STomohiro Kusumi 		if (*cp != ' ' && *cp != '.')
334*20f6ddd0STomohiro Kusumi 			break;
335*20f6ddd0STomohiro Kusumi 	if (i < 0)
336*20f6ddd0STomohiro Kusumi 		return 0;
337*20f6ddd0STomohiro Kusumi 
338*20f6ddd0STomohiro Kusumi 	/*
339*20f6ddd0STomohiro Kusumi 	 * Now find the extension
340*20f6ddd0STomohiro Kusumi 	 * Note: dot as first char doesn't start extension
341*20f6ddd0STomohiro Kusumi 	 *	 and trailing dots and blanks are ignored
342*20f6ddd0STomohiro Kusumi 	 */
343*20f6ddd0STomohiro Kusumi 	dp = dp1 = 0;
344*20f6ddd0STomohiro Kusumi 	for (cp = un + 1, i = unlen - 1; --i >= 0;) {
345*20f6ddd0STomohiro Kusumi 		switch (*cp++) {
346*20f6ddd0STomohiro Kusumi 		case '.':
347*20f6ddd0STomohiro Kusumi 			if (!dp1)
348*20f6ddd0STomohiro Kusumi 				dp1 = cp;
349*20f6ddd0STomohiro Kusumi 			break;
350*20f6ddd0STomohiro Kusumi 		case ' ':
351*20f6ddd0STomohiro Kusumi 			break;
352*20f6ddd0STomohiro Kusumi 		default:
353*20f6ddd0STomohiro Kusumi 			if (dp1)
354*20f6ddd0STomohiro Kusumi 				dp = dp1;
355*20f6ddd0STomohiro Kusumi 			dp1 = 0;
356*20f6ddd0STomohiro Kusumi 			break;
357*20f6ddd0STomohiro Kusumi 		}
358*20f6ddd0STomohiro Kusumi 	}
359*20f6ddd0STomohiro Kusumi 
360*20f6ddd0STomohiro Kusumi 	/*
361*20f6ddd0STomohiro Kusumi 	 * Now convert it
362*20f6ddd0STomohiro Kusumi 	 */
363*20f6ddd0STomohiro Kusumi 	if (dp) {
364*20f6ddd0STomohiro Kusumi 		if (dp1)
365*20f6ddd0STomohiro Kusumi 			l = dp1 - dp;
366*20f6ddd0STomohiro Kusumi 		else
367*20f6ddd0STomohiro Kusumi 			l = unlen - (dp - un);
368*20f6ddd0STomohiro Kusumi 		for (i = 0, j = 8; i < l && j < 11; i++, j++) {
369*20f6ddd0STomohiro Kusumi 			if (dp[i] != (dn[j] = unix2dos[dp[i]])
370*20f6ddd0STomohiro Kusumi 			    && conv != 3)
371*20f6ddd0STomohiro Kusumi 				conv = 2;
372*20f6ddd0STomohiro Kusumi 			if (!dn[j]) {
373*20f6ddd0STomohiro Kusumi 				conv = 3;
374*20f6ddd0STomohiro Kusumi 				dn[j--] = ' ';
375*20f6ddd0STomohiro Kusumi 			}
376*20f6ddd0STomohiro Kusumi 		}
377*20f6ddd0STomohiro Kusumi 		if (i < l)
378*20f6ddd0STomohiro Kusumi 			conv = 3;
379*20f6ddd0STomohiro Kusumi 		dp--;
380*20f6ddd0STomohiro Kusumi 	} else {
381*20f6ddd0STomohiro Kusumi 		for (dp = cp; *--dp == ' ' || *dp == '.';);
382*20f6ddd0STomohiro Kusumi 		dp++;
383*20f6ddd0STomohiro Kusumi 	}
384*20f6ddd0STomohiro Kusumi 
385*20f6ddd0STomohiro Kusumi 	shortlen = (dp - un) <= 8;
386*20f6ddd0STomohiro Kusumi 
387*20f6ddd0STomohiro Kusumi 	/*
388*20f6ddd0STomohiro Kusumi 	 * Now convert the rest of the name
389*20f6ddd0STomohiro Kusumi 	 */
390*20f6ddd0STomohiro Kusumi 	for (i = j = 0; un < dp && j < 8; i++, j++, un++) {
391*20f6ddd0STomohiro Kusumi 		if ((*un == ' ') && shortlen)
392*20f6ddd0STomohiro Kusumi 			dn[j] = ' ';
393*20f6ddd0STomohiro Kusumi 		else
394*20f6ddd0STomohiro Kusumi 			dn[j] = unix2dos[*un];
395*20f6ddd0STomohiro Kusumi 		if ((*un != dn[j])
396*20f6ddd0STomohiro Kusumi 		    && conv != 3)
397*20f6ddd0STomohiro Kusumi 			conv = 2;
398*20f6ddd0STomohiro Kusumi 		if (!dn[j]) {
399*20f6ddd0STomohiro Kusumi 			conv = 3;
400*20f6ddd0STomohiro Kusumi 			dn[j--] = ' ';
401*20f6ddd0STomohiro Kusumi 		}
402*20f6ddd0STomohiro Kusumi 	}
403*20f6ddd0STomohiro Kusumi 	if (un < dp)
404*20f6ddd0STomohiro Kusumi 		conv = 3;
405*20f6ddd0STomohiro Kusumi 	/*
406*20f6ddd0STomohiro Kusumi 	 * If we didn't have any chars in filename,
407*20f6ddd0STomohiro Kusumi 	 * generate a default
408*20f6ddd0STomohiro Kusumi 	 */
409*20f6ddd0STomohiro Kusumi 	if (!j)
410*20f6ddd0STomohiro Kusumi 		dn[0] = '_';
411*20f6ddd0STomohiro Kusumi 
412*20f6ddd0STomohiro Kusumi 	/*
413*20f6ddd0STomohiro Kusumi 	 * The first character cannot be E5,
414*20f6ddd0STomohiro Kusumi 	 * because that means a deleted entry
415*20f6ddd0STomohiro Kusumi 	 */
416*20f6ddd0STomohiro Kusumi 	if (dn[0] == 0xe5)
417*20f6ddd0STomohiro Kusumi 		dn[0] = SLOT_E5;
418*20f6ddd0STomohiro Kusumi 
419*20f6ddd0STomohiro Kusumi 	/*
420*20f6ddd0STomohiro Kusumi 	 * If there wasn't any char dropped,
421*20f6ddd0STomohiro Kusumi 	 * there is no place for generation numbers
422*20f6ddd0STomohiro Kusumi 	 */
423*20f6ddd0STomohiro Kusumi 	if (conv != 3) {
424*20f6ddd0STomohiro Kusumi 		if (gen > 1)
425*20f6ddd0STomohiro Kusumi 			return 0;
426*20f6ddd0STomohiro Kusumi 		return conv;
427*20f6ddd0STomohiro Kusumi 	}
428*20f6ddd0STomohiro Kusumi 
429*20f6ddd0STomohiro Kusumi 	/*
430*20f6ddd0STomohiro Kusumi 	 * Now insert the generation number into the filename part
431*20f6ddd0STomohiro Kusumi 	 */
432*20f6ddd0STomohiro Kusumi 	for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10)
433*20f6ddd0STomohiro Kusumi 		*--wcp = gen % 10 + '0';
434*20f6ddd0STomohiro Kusumi 	if (gen)
435*20f6ddd0STomohiro Kusumi 		return 0;
436*20f6ddd0STomohiro Kusumi 	for (i = 8; dn[--i] == ' ';);
437*20f6ddd0STomohiro Kusumi 	i++;
438*20f6ddd0STomohiro Kusumi 	if (gentext + sizeof(gentext) - wcp + 1 > 8 - i)
439*20f6ddd0STomohiro Kusumi 		i = 8 - (gentext + sizeof(gentext) - wcp + 1);
440*20f6ddd0STomohiro Kusumi 	dn[i++] = '~';
441*20f6ddd0STomohiro Kusumi 	while (wcp < gentext + sizeof(gentext))
442*20f6ddd0STomohiro Kusumi 		dn[i++] = *wcp++;
443*20f6ddd0STomohiro Kusumi 	return 3;
444*20f6ddd0STomohiro Kusumi }
445*20f6ddd0STomohiro Kusumi 
446*20f6ddd0STomohiro Kusumi /*
447*20f6ddd0STomohiro Kusumi  * Convert 8bit character string into UCS-2 string
448*20f6ddd0STomohiro Kusumi  * return total number of output chacters
449*20f6ddd0STomohiro Kusumi  */
450*20f6ddd0STomohiro Kusumi static int
char8ucs2str(const uint8_t * in,int n,uint16_t * out,int m)451*20f6ddd0STomohiro Kusumi char8ucs2str(const uint8_t *in, int n, uint16_t *out, int m)
452*20f6ddd0STomohiro Kusumi {
453*20f6ddd0STomohiro Kusumi 	uint16_t *p;
454*20f6ddd0STomohiro Kusumi 
455*20f6ddd0STomohiro Kusumi 	p = out;
456*20f6ddd0STomohiro Kusumi 	while (n > 0 && in[0] != 0) {
457*20f6ddd0STomohiro Kusumi 		if (m < 1)
458*20f6ddd0STomohiro Kusumi 			break;
459*20f6ddd0STomohiro Kusumi 		if (p)
460*20f6ddd0STomohiro Kusumi 			p[0] = htole16(in[0]);
461*20f6ddd0STomohiro Kusumi 		p += 1;
462*20f6ddd0STomohiro Kusumi 		m -= 1;
463*20f6ddd0STomohiro Kusumi 		in += 1;
464*20f6ddd0STomohiro Kusumi 		n -= 1;
465*20f6ddd0STomohiro Kusumi 	}
466*20f6ddd0STomohiro Kusumi 
467*20f6ddd0STomohiro Kusumi 	return p - out;
468*20f6ddd0STomohiro Kusumi }
469*20f6ddd0STomohiro Kusumi 
470*20f6ddd0STomohiro Kusumi static void
ucs2pad(uint16_t * buf,int len,int size)471*20f6ddd0STomohiro Kusumi ucs2pad(uint16_t *buf, int len, int size)
472*20f6ddd0STomohiro Kusumi {
473*20f6ddd0STomohiro Kusumi 
474*20f6ddd0STomohiro Kusumi 	if (len < size-1)
475*20f6ddd0STomohiro Kusumi 		buf[len++] = 0x0000;
476*20f6ddd0STomohiro Kusumi 	while (len < size)
477*20f6ddd0STomohiro Kusumi 		buf[len++] = 0xffff;
478*20f6ddd0STomohiro Kusumi }
479*20f6ddd0STomohiro Kusumi 
480*20f6ddd0STomohiro Kusumi /*
481*20f6ddd0STomohiro Kusumi  * Compare two 8bit char conversions case-insensitive
482*20f6ddd0STomohiro Kusumi  *
483*20f6ddd0STomohiro Kusumi  * uses the DOS case folding table
484*20f6ddd0STomohiro Kusumi  */
485*20f6ddd0STomohiro Kusumi static int
char8match(uint16_t * w1,uint16_t * w2,int n)486*20f6ddd0STomohiro Kusumi char8match(uint16_t *w1, uint16_t *w2, int n)
487*20f6ddd0STomohiro Kusumi {
488*20f6ddd0STomohiro Kusumi 	uint16_t u1, u2;
489*20f6ddd0STomohiro Kusumi 
490*20f6ddd0STomohiro Kusumi 	while (n > 0) {
491*20f6ddd0STomohiro Kusumi 		u1 = le16toh(*w1);
492*20f6ddd0STomohiro Kusumi 		u2 = le16toh(*w2);
493*20f6ddd0STomohiro Kusumi 		if (u1 == 0 || u2 == 0)
494*20f6ddd0STomohiro Kusumi 			return u1 == u2;
495*20f6ddd0STomohiro Kusumi 		if (u1 > 255 || u2 > 255)
496*20f6ddd0STomohiro Kusumi 			return 0;
497*20f6ddd0STomohiro Kusumi 		u1 = u2l[u1 & 0xff];
498*20f6ddd0STomohiro Kusumi 		u2 = u2l[u2 & 0xff];
499*20f6ddd0STomohiro Kusumi 		if (u1 != u2)
500*20f6ddd0STomohiro Kusumi 			return 0;
501*20f6ddd0STomohiro Kusumi 		++w1;
502*20f6ddd0STomohiro Kusumi 		++w2;
503*20f6ddd0STomohiro Kusumi 		--n;
504*20f6ddd0STomohiro Kusumi 	}
505*20f6ddd0STomohiro Kusumi 
506*20f6ddd0STomohiro Kusumi 	return 1;
507*20f6ddd0STomohiro Kusumi }
508