1952a6212SJordan K. Hubbard /* $NetBSD: msdosfs_conv.c,v 1.25 1997/11/17 15:36:40 ws Exp $ */
227a0bc89SDoug Rabson
3952a6212SJordan K. Hubbard /*-
4d63027b6SPedro F. Giffuni * SPDX-License-Identifier: BSD-4-Clause
5d63027b6SPedro F. Giffuni *
6952a6212SJordan K. Hubbard * Copyright (C) 1995, 1997 Wolfgang Solfrank.
7952a6212SJordan K. Hubbard * Copyright (C) 1995, 1997 TooLs GmbH.
8952a6212SJordan K. Hubbard * All rights reserved.
9952a6212SJordan K. Hubbard * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
10952a6212SJordan K. Hubbard *
11952a6212SJordan K. Hubbard * Redistribution and use in source and binary forms, with or without
12952a6212SJordan K. Hubbard * modification, are permitted provided that the following conditions
13952a6212SJordan K. Hubbard * are met:
14952a6212SJordan K. Hubbard * 1. Redistributions of source code must retain the above copyright
15952a6212SJordan K. Hubbard * notice, this list of conditions and the following disclaimer.
16952a6212SJordan K. Hubbard * 2. Redistributions in binary form must reproduce the above copyright
17952a6212SJordan K. Hubbard * notice, this list of conditions and the following disclaimer in the
18952a6212SJordan K. Hubbard * documentation and/or other materials provided with the distribution.
19952a6212SJordan K. Hubbard * 3. All advertising materials mentioning features or use of this software
20952a6212SJordan K. Hubbard * must display the following acknowledgement:
21952a6212SJordan K. Hubbard * This product includes software developed by TooLs GmbH.
22952a6212SJordan K. Hubbard * 4. The name of TooLs GmbH may not be used to endorse or promote products
23952a6212SJordan K. Hubbard * derived from this software without specific prior written permission.
24952a6212SJordan K. Hubbard *
25952a6212SJordan K. Hubbard * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
26952a6212SJordan K. Hubbard * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27952a6212SJordan K. Hubbard * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28952a6212SJordan K. Hubbard * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29952a6212SJordan K. Hubbard * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30952a6212SJordan K. Hubbard * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
31952a6212SJordan K. Hubbard * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32952a6212SJordan K. Hubbard * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33952a6212SJordan K. Hubbard * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34952a6212SJordan K. Hubbard * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35952a6212SJordan K. Hubbard */
36d167cf6fSWarner Losh /*-
3727a0bc89SDoug Rabson * Written by Paul Popelka (paulp@uts.amdahl.com)
3827a0bc89SDoug Rabson *
3927a0bc89SDoug Rabson * You can do anything you want with this software, just don't say you wrote
4027a0bc89SDoug Rabson * it, and don't remove this notice.
4127a0bc89SDoug Rabson *
4227a0bc89SDoug Rabson * This software is provided "as is".
4327a0bc89SDoug Rabson *
4427a0bc89SDoug Rabson * The author supplies this software to be publicly redistributed on the
4527a0bc89SDoug Rabson * understanding that the author is not responsible for the correct
4627a0bc89SDoug Rabson * functioning of this software in any circumstances and is not liable for
4727a0bc89SDoug Rabson * any damages caused by this software.
4827a0bc89SDoug Rabson *
4927a0bc89SDoug Rabson * October 1992
5027a0bc89SDoug Rabson */
5127a0bc89SDoug Rabson
5227a0bc89SDoug Rabson #include <sys/param.h>
53952a6212SJordan K. Hubbard #include <sys/systm.h>
54952a6212SJordan K. Hubbard #include <sys/dirent.h>
55c4f02a89SMax Khon #include <sys/iconv.h>
569b0802c9SBruce Evans #include <sys/mount.h>
579b0802c9SBruce Evans
589b0802c9SBruce Evans #include <fs/msdosfs/bpb.h>
599b0802c9SBruce Evans #include <fs/msdosfs/direntry.h>
609b0802c9SBruce Evans #include <fs/msdosfs/msdosfsmount.h>
61c4f02a89SMax Khon
62c4f02a89SMax Khon extern struct iconv_functions *msdosfs_iconv;
6327a0bc89SDoug Rabson
64bddcdc51STim J. Robbins static int mbsadjpos(const char **, size_t, size_t, int, int, void *handle);
65e2ee19e3SKevin Lo static u_char * dos2unixchr(u_char *, const u_char **, size_t *, int, struct msdosfsmount *);
6623c53312SEd Maste static uint16_t unix2doschr(const u_char **, size_t *, struct msdosfsmount *);
6723c53312SEd Maste static u_char * win2unixchr(u_char *, uint16_t, struct msdosfsmount *);
6823c53312SEd Maste static uint16_t unix2winchr(const u_char **, size_t *, int, struct msdosfsmount *);
69c4f02a89SMax Khon
7027a0bc89SDoug Rabson /*
71cce0eefdSMike Smith * 0 - character disallowed in long file name.
72cce0eefdSMike Smith * 1 - character should be replaced by '_' in DOS file name,
73cce0eefdSMike Smith * and generation number inserted.
74cce0eefdSMike Smith * 2 - character ('.' and ' ') should be skipped in DOS file name,
75cce0eefdSMike Smith * and generation number inserted.
76cce0eefdSMike Smith */
776403a57bSEd Maste static const u_char
78952a6212SJordan K. Hubbard unix2dos[256] = {
79c4f02a89SMax Khon /* iso8859-1 -> cp850 */
80952a6212SJordan K. Hubbard 0, 0, 0, 0, 0, 0, 0, 0, /* 00-07 */
81952a6212SJordan K. Hubbard 0, 0, 0, 0, 0, 0, 0, 0, /* 08-0f */
82952a6212SJordan K. Hubbard 0, 0, 0, 0, 0, 0, 0, 0, /* 10-17 */
83952a6212SJordan K. Hubbard 0, 0, 0, 0, 0, 0, 0, 0, /* 18-1f */
84cce0eefdSMike Smith 2, 0x21, 0, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
85cce0eefdSMike Smith 0x28, 0x29, 0, 1, 1, 0x2d, 2, 0, /* 28-2f */
86952a6212SJordan K. Hubbard 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
87cce0eefdSMike Smith 0x38, 0x39, 0, 1, 0, 1, 0, 0, /* 38-3f */
88952a6212SJordan K. Hubbard 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */
89952a6212SJordan K. Hubbard 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */
90952a6212SJordan K. Hubbard 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */
91cce0eefdSMike Smith 0x58, 0x59, 0x5a, 1, 0, 1, 0x5e, 0x5f, /* 58-5f */
92952a6212SJordan K. Hubbard 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 60-67 */
93952a6212SJordan K. Hubbard 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 68-6f */
94952a6212SJordan K. Hubbard 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 70-77 */
95952a6212SJordan K. Hubbard 0x58, 0x59, 0x5a, 0x7b, 0, 0x7d, 0x7e, 0, /* 78-7f */
96952a6212SJordan K. Hubbard 0, 0, 0, 0, 0, 0, 0, 0, /* 80-87 */
97952a6212SJordan K. Hubbard 0, 0, 0, 0, 0, 0, 0, 0, /* 88-8f */
98952a6212SJordan K. Hubbard 0, 0, 0, 0, 0, 0, 0, 0, /* 90-97 */
99952a6212SJordan K. Hubbard 0, 0, 0, 0, 0, 0, 0, 0, /* 98-9f */
100952a6212SJordan K. Hubbard 0, 0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5, /* a0-a7 */
101952a6212SJordan K. Hubbard 0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee, /* a8-af */
102952a6212SJordan K. Hubbard 0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa, /* b0-b7 */
103952a6212SJordan K. Hubbard 0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8, /* b8-bf */
104952a6212SJordan K. Hubbard 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* c0-c7 */
105952a6212SJordan K. Hubbard 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* c8-cf */
106952a6212SJordan K. Hubbard 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e, /* d0-d7 */
107952a6212SJordan K. Hubbard 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1, /* d8-df */
108952a6212SJordan K. Hubbard 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* e0-e7 */
109952a6212SJordan K. Hubbard 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* e8-ef */
110952a6212SJordan K. Hubbard 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6, /* f0-f7 */
111952a6212SJordan K. Hubbard 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98, /* f8-ff */
112952a6212SJordan K. Hubbard };
113952a6212SJordan K. Hubbard
1146403a57bSEd Maste static const u_char
115952a6212SJordan K. Hubbard dos2unix[256] = {
116c4f02a89SMax Khon /* cp850 -> iso8859-1 */
117952a6212SJordan K. Hubbard 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 00-07 */
118952a6212SJordan K. Hubbard 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 08-0f */
119952a6212SJordan K. Hubbard 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 10-17 */
120952a6212SJordan K. Hubbard 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 18-1f */
121952a6212SJordan K. Hubbard 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
122952a6212SJordan K. Hubbard 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
123952a6212SJordan K. Hubbard 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
124952a6212SJordan K. Hubbard 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
125952a6212SJordan K. Hubbard 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */
126952a6212SJordan K. Hubbard 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */
127952a6212SJordan K. Hubbard 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */
128952a6212SJordan K. Hubbard 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
129952a6212SJordan K. Hubbard 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
130952a6212SJordan K. Hubbard 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
131952a6212SJordan K. Hubbard 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
132952a6212SJordan K. Hubbard 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
133952a6212SJordan K. Hubbard 0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7, /* 80-87 */
134952a6212SJordan K. Hubbard 0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5, /* 88-8f */
135952a6212SJordan K. Hubbard 0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9, /* 90-97 */
136952a6212SJordan K. Hubbard 0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, 0x3f, /* 98-9f */
137952a6212SJordan K. Hubbard 0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba, /* a0-a7 */
138952a6212SJordan K. Hubbard 0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb, /* a8-af */
139952a6212SJordan K. Hubbard 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xc1, 0xc2, 0xc0, /* b0-b7 */
140952a6212SJordan K. Hubbard 0xa9, 0x3f, 0x3f, 0x3f, 0x3f, 0xa2, 0xa5, 0x3f, /* b8-bf */
141952a6212SJordan K. Hubbard 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xe3, 0xc3, /* c0-c7 */
142952a6212SJordan K. Hubbard 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xa4, /* c8-cf */
143952a6212SJordan K. Hubbard 0xf0, 0xd0, 0xca, 0xcb, 0xc8, 0x3f, 0xcd, 0xce, /* d0-d7 */
144952a6212SJordan K. Hubbard 0xcf, 0x3f, 0x3f, 0x3f, 0x3f, 0xa6, 0xcc, 0x3f, /* d8-df */
145952a6212SJordan K. Hubbard 0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe, /* e0-e7 */
146952a6212SJordan K. Hubbard 0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f, /* e8-ef */
147952a6212SJordan K. Hubbard 0xad, 0xb1, 0x3f, 0xbe, 0xb6, 0xa7, 0xf7, 0xb8, /* f0-f7 */
148952a6212SJordan K. Hubbard 0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2, 0x3f, 0x3f, /* f8-ff */
149952a6212SJordan K. Hubbard };
150952a6212SJordan K. Hubbard
1516403a57bSEd Maste static const u_char
152952a6212SJordan K. Hubbard u2l[256] = {
153c4f02a89SMax Khon /* tolower */
154952a6212SJordan K. Hubbard 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
155952a6212SJordan K. Hubbard 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
156952a6212SJordan K. Hubbard 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
157952a6212SJordan K. Hubbard 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
158952a6212SJordan K. Hubbard 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
159952a6212SJordan K. Hubbard 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
160952a6212SJordan K. Hubbard 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
161952a6212SJordan K. Hubbard 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
162952a6212SJordan K. Hubbard 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
163952a6212SJordan K. Hubbard 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
164952a6212SJordan K. Hubbard 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
165952a6212SJordan K. Hubbard 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
166952a6212SJordan K. Hubbard 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
167952a6212SJordan K. Hubbard 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
168952a6212SJordan K. Hubbard 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
169952a6212SJordan K. Hubbard 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
170952a6212SJordan K. Hubbard 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
171952a6212SJordan K. Hubbard 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
172952a6212SJordan K. Hubbard 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
173952a6212SJordan K. Hubbard 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
174952a6212SJordan K. Hubbard 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
175952a6212SJordan K. Hubbard 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
176952a6212SJordan K. Hubbard 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
177952a6212SJordan K. Hubbard 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
178952a6212SJordan K. Hubbard 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
179952a6212SJordan K. Hubbard 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
180952a6212SJordan K. Hubbard 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
181952a6212SJordan K. Hubbard 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
182952a6212SJordan K. Hubbard 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
183952a6212SJordan K. Hubbard 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
184952a6212SJordan K. Hubbard 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
185952a6212SJordan K. Hubbard 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
186952a6212SJordan K. Hubbard };
18727a0bc89SDoug Rabson
1886403a57bSEd Maste static const u_char
1897391f611SAndrey A. Chernov l2u[256] = {
190c4f02a89SMax Khon /* toupper */
1917391f611SAndrey A. Chernov 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
1927391f611SAndrey A. Chernov 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
1937391f611SAndrey A. Chernov 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
1947391f611SAndrey A. Chernov 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
1957391f611SAndrey A. Chernov 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
1967391f611SAndrey A. Chernov 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
1977391f611SAndrey A. Chernov 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
1987391f611SAndrey A. Chernov 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
1997391f611SAndrey A. Chernov 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
2007391f611SAndrey A. Chernov 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
2017391f611SAndrey A. Chernov 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
2027391f611SAndrey A. Chernov 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
2037391f611SAndrey A. Chernov 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
2047391f611SAndrey A. Chernov 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
2057391f611SAndrey A. Chernov 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
2067391f611SAndrey A. Chernov 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
2077391f611SAndrey A. Chernov 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
2087391f611SAndrey A. Chernov 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
2097391f611SAndrey A. Chernov 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
2107391f611SAndrey A. Chernov 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
2117391f611SAndrey A. Chernov 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
2127391f611SAndrey A. Chernov 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
2137391f611SAndrey A. Chernov 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
2147391f611SAndrey A. Chernov 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
2157391f611SAndrey A. Chernov 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
2167391f611SAndrey A. Chernov 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
2177391f611SAndrey A. Chernov 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
2187391f611SAndrey A. Chernov 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
2197391f611SAndrey A. Chernov 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
2207391f611SAndrey A. Chernov 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
2217391f611SAndrey A. Chernov 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
2227391f611SAndrey A. Chernov 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
2237391f611SAndrey A. Chernov };
2247391f611SAndrey A. Chernov
22527a0bc89SDoug Rabson /*
22627a0bc89SDoug Rabson * DOS filenames are made of 2 parts, the name part and the extension part.
22727a0bc89SDoug Rabson * The name part is 8 characters long and the extension part is 3
22827a0bc89SDoug Rabson * characters long. They may contain trailing blanks if the name or
22927a0bc89SDoug Rabson * extension are not long enough to fill their respective fields.
23027a0bc89SDoug Rabson */
23127a0bc89SDoug Rabson
23227a0bc89SDoug Rabson /*
23327a0bc89SDoug Rabson * Convert a DOS filename to a unix filename. And, return the number of
23427a0bc89SDoug Rabson * characters in the resulting unix filename excluding the terminating
23527a0bc89SDoug Rabson * null.
23627a0bc89SDoug Rabson */
23727a0bc89SDoug Rabson int
dos2unixfn(u_char dn[11],u_char * un,int lower,struct msdosfsmount * pmp)23810c9700fSEd Maste dos2unixfn(u_char dn[11], u_char *un, int lower, struct msdosfsmount *pmp)
23927a0bc89SDoug Rabson {
240bddcdc51STim J. Robbins size_t i;
241c4f02a89SMax Khon int thislong = 0;
242e2ee19e3SKevin Lo u_char *c, tmpbuf[5];
24327a0bc89SDoug Rabson
24427a0bc89SDoug Rabson /*
24527a0bc89SDoug Rabson * If first char of the filename is SLOT_E5 (0x05), then the real
24627a0bc89SDoug Rabson * first char of the filename should be 0xe5. But, they couldn't
24727a0bc89SDoug Rabson * just have a 0xe5 mean 0xe5 because that is used to mean a freed
24827a0bc89SDoug Rabson * directory slot. Another dos quirk.
24927a0bc89SDoug Rabson */
250952a6212SJordan K. Hubbard if (*dn == SLOT_E5)
251c4f02a89SMax Khon *dn = 0xe5;
25227a0bc89SDoug Rabson
253952a6212SJordan K. Hubbard /*
254952a6212SJordan K. Hubbard * Copy the name portion into the unix filename string.
255952a6212SJordan K. Hubbard */
256c4f02a89SMax Khon for (i = 8; i > 0 && *dn != ' ';) {
25709bb4a31SDimitry Andric c = dos2unixchr(tmpbuf, __DECONST(const u_char **, &dn), &i,
258e2ee19e3SKevin Lo lower & LCASE_BASE, pmp);
25941f1dcccSKevin Lo while (*c != '\0') {
26041f1dcccSKevin Lo *un++ = *c++;
261952a6212SJordan K. Hubbard thislong++;
262952a6212SJordan K. Hubbard }
263c4f02a89SMax Khon }
264c4f02a89SMax Khon dn += i;
265952a6212SJordan K. Hubbard
266952a6212SJordan K. Hubbard /*
267952a6212SJordan K. Hubbard * Now, if there is an extension then put in a period and copy in
268952a6212SJordan K. Hubbard * the extension.
269952a6212SJordan K. Hubbard */
270952a6212SJordan K. Hubbard if (*dn != ' ') {
271952a6212SJordan K. Hubbard *un++ = '.';
272952a6212SJordan K. Hubbard thislong++;
273c4f02a89SMax Khon for (i = 3; i > 0 && *dn != ' ';) {
27409bb4a31SDimitry Andric c = dos2unixchr(tmpbuf, __DECONST(const u_char **, &dn),
27509bb4a31SDimitry Andric &i, lower & LCASE_EXT, pmp);
27641f1dcccSKevin Lo while (*c != '\0') {
27741f1dcccSKevin Lo *un++ = *c++;
278c4f02a89SMax Khon thislong++;
279c4f02a89SMax Khon }
280952a6212SJordan K. Hubbard }
281952a6212SJordan K. Hubbard }
282952a6212SJordan K. Hubbard *un++ = 0;
283952a6212SJordan K. Hubbard
284952a6212SJordan K. Hubbard return (thislong);
28527a0bc89SDoug Rabson }
28627a0bc89SDoug Rabson
28727a0bc89SDoug Rabson /*
288952a6212SJordan K. Hubbard * Convert a unix filename to a DOS filename according to Win95 rules.
289952a6212SJordan K. Hubbard * If applicable and gen is not 0, it is inserted into the converted
290952a6212SJordan K. Hubbard * filename as a generation number.
291952a6212SJordan K. Hubbard * Returns
292952a6212SJordan K. Hubbard * 0 if name couldn't be converted
293952a6212SJordan K. Hubbard * 1 if the converted name is the same as the original
294952a6212SJordan K. Hubbard * (no long filename entry necessary for Win95)
295952a6212SJordan K. Hubbard * 2 if conversion was successful
296952a6212SJordan K. Hubbard * 3 if conversion was successful and generation number was inserted
29727a0bc89SDoug Rabson */
298952a6212SJordan K. Hubbard int
unix2dosfn(const u_char * un,u_char dn[12],size_t unlen,u_int gen,struct msdosfsmount * pmp)2994882501bSEd Maste unix2dosfn(const u_char *un, u_char dn[12], size_t unlen, u_int gen,
3004882501bSEd Maste struct msdosfsmount *pmp)
30127a0bc89SDoug Rabson {
302bddcdc51STim J. Robbins ssize_t i, j;
303bddcdc51STim J. Robbins int l;
304952a6212SJordan K. Hubbard int conv = 1;
305952a6212SJordan K. Hubbard const u_char *cp, *dp, *dp1;
306952a6212SJordan K. Hubbard u_char gentext[6], *wcp;
30723c53312SEd Maste uint16_t c;
30827a0bc89SDoug Rabson
30927a0bc89SDoug Rabson /*
31027a0bc89SDoug Rabson * Fill the dos filename string with blanks. These are DOS's pad
31127a0bc89SDoug Rabson * characters.
31227a0bc89SDoug Rabson */
313952a6212SJordan K. Hubbard for (i = 0; i < 11; i++)
31427a0bc89SDoug Rabson dn[i] = ' ';
315952a6212SJordan K. Hubbard dn[11] = 0;
31627a0bc89SDoug Rabson
31727a0bc89SDoug Rabson /*
31827a0bc89SDoug Rabson * The filenames "." and ".." are handled specially, since they
31927a0bc89SDoug Rabson * don't follow dos filename rules.
32027a0bc89SDoug Rabson */
32127a0bc89SDoug Rabson if (un[0] == '.' && unlen == 1) {
32227a0bc89SDoug Rabson dn[0] = '.';
323952a6212SJordan K. Hubbard return gen <= 1;
32427a0bc89SDoug Rabson }
32527a0bc89SDoug Rabson if (un[0] == '.' && un[1] == '.' && unlen == 2) {
32627a0bc89SDoug Rabson dn[0] = '.';
32727a0bc89SDoug Rabson dn[1] = '.';
328952a6212SJordan K. Hubbard return gen <= 1;
32927a0bc89SDoug Rabson }
33027a0bc89SDoug Rabson
33127a0bc89SDoug Rabson /*
332952a6212SJordan K. Hubbard * Filenames with only blanks and dots are not allowed!
33327a0bc89SDoug Rabson */
334952a6212SJordan K. Hubbard for (cp = un, i = unlen; --i >= 0; cp++)
335952a6212SJordan K. Hubbard if (*cp != ' ' && *cp != '.')
336952a6212SJordan K. Hubbard break;
337952a6212SJordan K. Hubbard if (i < 0)
338952a6212SJordan K. Hubbard return 0;
339952a6212SJordan K. Hubbard
340cce0eefdSMike Smith /*
341cce0eefdSMike Smith * Filenames with some characters are not allowed!
342cce0eefdSMike Smith */
343c4f02a89SMax Khon for (cp = un, i = unlen; i > 0;)
344c4f02a89SMax Khon if (unix2doschr(&cp, (size_t *)&i, pmp) == 0)
345cce0eefdSMike Smith return 0;
346cce0eefdSMike Smith
347952a6212SJordan K. Hubbard /*
348952a6212SJordan K. Hubbard * Now find the extension
349952a6212SJordan K. Hubbard * Note: dot as first char doesn't start extension
350952a6212SJordan K. Hubbard * and trailing dots and blanks are ignored
351c4f02a89SMax Khon * Note(2003/7): It seems recent Windows has
352c4f02a89SMax Khon * defferent rule than this code, that Windows
353c4f02a89SMax Khon * ignores all dots before extension, and use all
354c4f02a89SMax Khon * chars as filename except for dots.
355952a6212SJordan K. Hubbard */
3560d3e502fSPedro F. Giffuni dp = dp1 = NULL;
357952a6212SJordan K. Hubbard for (cp = un + 1, i = unlen - 1; --i >= 0;) {
358952a6212SJordan K. Hubbard switch (*cp++) {
359952a6212SJordan K. Hubbard case '.':
360952a6212SJordan K. Hubbard if (!dp1)
361952a6212SJordan K. Hubbard dp1 = cp;
362952a6212SJordan K. Hubbard break;
363952a6212SJordan K. Hubbard case ' ':
364952a6212SJordan K. Hubbard break;
365952a6212SJordan K. Hubbard default:
366952a6212SJordan K. Hubbard if (dp1)
367952a6212SJordan K. Hubbard dp = dp1;
3680d3e502fSPedro F. Giffuni dp1 = NULL;
369952a6212SJordan K. Hubbard break;
370952a6212SJordan K. Hubbard }
37127a0bc89SDoug Rabson }
37227a0bc89SDoug Rabson
37327a0bc89SDoug Rabson /*
374697ab829SR. Imura * Now convert it (this part is for extension).
375697ab829SR. Imura * As Windows XP do, if it's not ascii char,
376697ab829SR. Imura * this function should return 2 or 3, so that checkng out Unicode name.
37727a0bc89SDoug Rabson */
378952a6212SJordan K. Hubbard if (dp) {
379952a6212SJordan K. Hubbard if (dp1)
380952a6212SJordan K. Hubbard l = dp1 - dp;
381952a6212SJordan K. Hubbard else
382952a6212SJordan K. Hubbard l = unlen - (dp - un);
383c4f02a89SMax Khon for (cp = dp, i = l, j = 8; i > 0 && j < 11; j++) {
384c4f02a89SMax Khon c = unix2doschr(&cp, (size_t *)&i, pmp);
385c4f02a89SMax Khon if (c & 0xff00) {
386c4f02a89SMax Khon dn[j] = c >> 8;
387c4f02a89SMax Khon if (++j < 11) {
388c4f02a89SMax Khon dn[j] = c;
389697ab829SR. Imura if (conv != 3)
390697ab829SR. Imura conv = 2;
391c4f02a89SMax Khon continue;
392c4f02a89SMax Khon } else {
393c4f02a89SMax Khon conv = 3;
394c4f02a89SMax Khon dn[j-1] = ' ';
395c4f02a89SMax Khon break;
396c4f02a89SMax Khon }
397c4f02a89SMax Khon } else {
398c4f02a89SMax Khon dn[j] = c;
399c4f02a89SMax Khon }
400697ab829SR. Imura if (((dn[j] & 0x80) || *(cp - 1) != dn[j]) && conv != 3)
401952a6212SJordan K. Hubbard conv = 2;
402cce0eefdSMike Smith if (dn[j] == 1) {
403cce0eefdSMike Smith conv = 3;
404cce0eefdSMike Smith dn[j] = '_';
405cce0eefdSMike Smith }
406cce0eefdSMike Smith if (dn[j] == 2) {
407952a6212SJordan K. Hubbard conv = 3;
408952a6212SJordan K. Hubbard dn[j--] = ' ';
409952a6212SJordan K. Hubbard }
410952a6212SJordan K. Hubbard }
411c4f02a89SMax Khon if (i > 0)
412952a6212SJordan K. Hubbard conv = 3;
413952a6212SJordan K. Hubbard dp--;
414952a6212SJordan K. Hubbard } else {
415952a6212SJordan K. Hubbard for (dp = cp; *--dp == ' ' || *dp == '.';);
416952a6212SJordan K. Hubbard dp++;
417952a6212SJordan K. Hubbard }
418952a6212SJordan K. Hubbard
419952a6212SJordan K. Hubbard /*
420952a6212SJordan K. Hubbard * Now convert the rest of the name
421952a6212SJordan K. Hubbard */
422c4f02a89SMax Khon for (i = dp - un, j = 0; un < dp && j < 8; j++) {
423bddcdc51STim J. Robbins c = unix2doschr(&un, &i, pmp);
424c4f02a89SMax Khon if (c & 0xff00) {
425c4f02a89SMax Khon dn[j] = c >> 8;
426c4f02a89SMax Khon if (++j < 8) {
427c4f02a89SMax Khon dn[j] = c;
428697ab829SR. Imura if (conv != 3)
429697ab829SR. Imura conv = 2;
430c4f02a89SMax Khon continue;
431c4f02a89SMax Khon } else {
432c4f02a89SMax Khon conv = 3;
433c4f02a89SMax Khon dn[j-1] = ' ';
434c4f02a89SMax Khon break;
435c4f02a89SMax Khon }
436c4f02a89SMax Khon } else {
437c4f02a89SMax Khon dn[j] = c;
438c4f02a89SMax Khon }
439697ab829SR. Imura if (((dn[j] & 0x80) || *(un - 1) != dn[j]) && conv != 3)
440952a6212SJordan K. Hubbard conv = 2;
441cce0eefdSMike Smith if (dn[j] == 1) {
442cce0eefdSMike Smith conv = 3;
443cce0eefdSMike Smith dn[j] = '_';
444cce0eefdSMike Smith }
445cce0eefdSMike Smith if (dn[j] == 2) {
446952a6212SJordan K. Hubbard conv = 3;
447952a6212SJordan K. Hubbard dn[j--] = ' ';
448952a6212SJordan K. Hubbard }
449952a6212SJordan K. Hubbard }
450952a6212SJordan K. Hubbard if (un < dp)
451952a6212SJordan K. Hubbard conv = 3;
452952a6212SJordan K. Hubbard /*
453952a6212SJordan K. Hubbard * If we didn't have any chars in filename,
454952a6212SJordan K. Hubbard * generate a default
455952a6212SJordan K. Hubbard */
456952a6212SJordan K. Hubbard if (!j)
457952a6212SJordan K. Hubbard dn[0] = '_';
458952a6212SJordan K. Hubbard
459952a6212SJordan K. Hubbard /*
460952a6212SJordan K. Hubbard * If there wasn't any char dropped,
461952a6212SJordan K. Hubbard * there is no place for generation numbers
46227a0bc89SDoug Rabson */
463952a6212SJordan K. Hubbard if (conv != 3) {
464952a6212SJordan K. Hubbard if (gen > 1)
465c4f02a89SMax Khon conv = 0;
466c4f02a89SMax Khon goto done;
467952a6212SJordan K. Hubbard }
468952a6212SJordan K. Hubbard
469952a6212SJordan K. Hubbard /*
470952a6212SJordan K. Hubbard * Now insert the generation number into the filename part
471952a6212SJordan K. Hubbard */
472011cdb57SDmitrij Tejblum if (gen == 0)
473c4f02a89SMax Khon goto done;
474952a6212SJordan K. Hubbard for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10)
475952a6212SJordan K. Hubbard *--wcp = gen % 10 + '0';
476c4f02a89SMax Khon if (gen) {
477c4f02a89SMax Khon conv = 0;
478c4f02a89SMax Khon goto done;
479c4f02a89SMax Khon }
480952a6212SJordan K. Hubbard for (i = 8; dn[--i] == ' ';);
481952a6212SJordan K. Hubbard i++;
482952a6212SJordan K. Hubbard if (gentext + sizeof(gentext) - wcp + 1 > 8 - i)
483952a6212SJordan K. Hubbard i = 8 - (gentext + sizeof(gentext) - wcp + 1);
484c4f02a89SMax Khon /*
485c4f02a89SMax Khon * Correct posision to where insert the generation number
486c4f02a89SMax Khon */
487c4f02a89SMax Khon cp = dn;
488c4f02a89SMax Khon i -= mbsadjpos((const char**)&cp, i, unlen, 1, pmp->pm_flags, pmp->pm_d2u);
489c4f02a89SMax Khon
490952a6212SJordan K. Hubbard dn[i++] = '~';
491952a6212SJordan K. Hubbard while (wcp < gentext + sizeof(gentext))
492952a6212SJordan K. Hubbard dn[i++] = *wcp++;
493c4f02a89SMax Khon
494c4f02a89SMax Khon /*
495c4f02a89SMax Khon * Tail of the filename should be space
496c4f02a89SMax Khon */
497c4f02a89SMax Khon while (i < 8)
498c4f02a89SMax Khon dn[i++] = ' ';
499c4f02a89SMax Khon conv = 3;
500c4f02a89SMax Khon
501c4f02a89SMax Khon done:
502c4f02a89SMax Khon /*
503c4f02a89SMax Khon * The first character cannot be E5,
504c4f02a89SMax Khon * because that means a deleted entry
505c4f02a89SMax Khon */
506c4f02a89SMax Khon if (dn[0] == 0xe5)
507c4f02a89SMax Khon dn[0] = SLOT_E5;
508c4f02a89SMax Khon
509c4f02a89SMax Khon return conv;
510952a6212SJordan K. Hubbard }
511952a6212SJordan K. Hubbard
512952a6212SJordan K. Hubbard /*
513952a6212SJordan K. Hubbard * Create a Win95 long name directory entry
514952a6212SJordan K. Hubbard * Note: assumes that the filename is valid,
515952a6212SJordan K. Hubbard * i.e. doesn't consist solely of blanks and dots
516952a6212SJordan K. Hubbard */
517952a6212SJordan K. Hubbard int
unix2winfn(const u_char * un,size_t unlen,struct winentry * wep,int cnt,int chksum,struct msdosfsmount * pmp)5184882501bSEd Maste unix2winfn(const u_char *un, size_t unlen, struct winentry *wep, int cnt,
5194882501bSEd Maste int chksum, struct msdosfsmount *pmp)
520952a6212SJordan K. Hubbard {
52123c53312SEd Maste uint8_t *wcp;
522c4f02a89SMax Khon int i, end;
52323c53312SEd Maste uint16_t code;
524952a6212SJordan K. Hubbard
525952a6212SJordan K. Hubbard /*
526952a6212SJordan K. Hubbard * Drop trailing blanks and dots
527952a6212SJordan K. Hubbard */
528c4f02a89SMax Khon unlen = winLenFixup(un, unlen);
529952a6212SJordan K. Hubbard
530c4f02a89SMax Khon /*
531c4f02a89SMax Khon * Cut *un for this slot
532c4f02a89SMax Khon */
533c4f02a89SMax Khon unlen = mbsadjpos((const char **)&un, unlen, (cnt - 1) * WIN_CHARS, 2,
534c4f02a89SMax Khon pmp->pm_flags, pmp->pm_u2w);
535952a6212SJordan K. Hubbard
536952a6212SJordan K. Hubbard /*
537952a6212SJordan K. Hubbard * Initialize winentry to some useful default
538952a6212SJordan K. Hubbard */
5396a1c2e1fSEd Maste memset(wep, 0xff, sizeof(*wep));
540952a6212SJordan K. Hubbard wep->weCnt = cnt;
541952a6212SJordan K. Hubbard wep->weAttributes = ATTR_WIN95;
542952a6212SJordan K. Hubbard wep->weReserved1 = 0;
543952a6212SJordan K. Hubbard wep->weChksum = chksum;
544952a6212SJordan K. Hubbard wep->weReserved2 = 0;
545952a6212SJordan K. Hubbard
546952a6212SJordan K. Hubbard /*
547952a6212SJordan K. Hubbard * Now convert the filename parts
548952a6212SJordan K. Hubbard */
549c4f02a89SMax Khon end = 0;
550c4f02a89SMax Khon for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0 && !end;) {
551bddcdc51STim J. Robbins code = unix2winchr(&un, &unlen, 0, pmp);
55213df76f2SAndrey A. Chernov *wcp++ = code;
55313df76f2SAndrey A. Chernov *wcp++ = code >> 8;
554c4f02a89SMax Khon if (!code)
555c4f02a89SMax Khon end = WIN_LAST;
556952a6212SJordan K. Hubbard }
557c4f02a89SMax Khon for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0 && !end;) {
558bddcdc51STim J. Robbins code = unix2winchr(&un, &unlen, 0, pmp);
55913df76f2SAndrey A. Chernov *wcp++ = code;
56013df76f2SAndrey A. Chernov *wcp++ = code >> 8;
561c4f02a89SMax Khon if (!code)
562c4f02a89SMax Khon end = WIN_LAST;
563952a6212SJordan K. Hubbard }
564c4f02a89SMax Khon for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0 && !end;) {
565bddcdc51STim J. Robbins code = unix2winchr(&un, &unlen, 0, pmp);
56613df76f2SAndrey A. Chernov *wcp++ = code;
56713df76f2SAndrey A. Chernov *wcp++ = code >> 8;
568c4f02a89SMax Khon if (!code)
569c4f02a89SMax Khon end = WIN_LAST;
570952a6212SJordan K. Hubbard }
571*c8586688SPedro F. Giffuni if (!unlen)
572c4f02a89SMax Khon end = WIN_LAST;
573c4f02a89SMax Khon wep->weCnt |= end;
574c4f02a89SMax Khon return !end;
5754d63452fSAndrey A. Chernov }
5764d63452fSAndrey A. Chernov
577952a6212SJordan K. Hubbard /*
578952a6212SJordan K. Hubbard * Compare our filename to the one in the Win95 entry
579952a6212SJordan K. Hubbard * Returns the checksum or -1 if no match
580952a6212SJordan K. Hubbard */
581952a6212SJordan K. Hubbard int
winChkName(struct mbnambuf * nbp,const u_char * un,size_t unlen,int chksum,struct msdosfsmount * pmp)5824882501bSEd Maste winChkName(struct mbnambuf *nbp, const u_char *un, size_t unlen, int chksum,
5834882501bSEd Maste struct msdosfsmount *pmp)
584952a6212SJordan K. Hubbard {
585bddcdc51STim J. Robbins size_t len;
58623c53312SEd Maste uint16_t c1, c2;
587c4f02a89SMax Khon u_char *np;
588c4f02a89SMax Khon struct dirent dirbuf;
589952a6212SJordan K. Hubbard
590952a6212SJordan K. Hubbard /*
591c2819440SBruce Evans * We already have winentry in *nbp.
592952a6212SJordan K. Hubbard */
593c2819440SBruce Evans if (!mbnambuf_flush(nbp, &dirbuf) || dirbuf.d_namlen == 0)
594952a6212SJordan K. Hubbard return -1;
595952a6212SJordan K. Hubbard
596c4f02a89SMax Khon #ifdef MSDOSFS_DEBUG
597027bebe8SEd Maste printf("winChkName(): un=%s:%zu,d_name=%s:%d\n", un, unlen,
598c4f02a89SMax Khon dirbuf.d_name,
599c4f02a89SMax Khon dirbuf.d_namlen);
600c4f02a89SMax Khon #endif
601952a6212SJordan K. Hubbard
602952a6212SJordan K. Hubbard /*
603952a6212SJordan K. Hubbard * Compare the name parts
604952a6212SJordan K. Hubbard */
605c4f02a89SMax Khon len = dirbuf.d_namlen;
606c4f02a89SMax Khon if (unlen != len)
607c4f02a89SMax Khon return -2;
608c4f02a89SMax Khon
609c4f02a89SMax Khon for (np = dirbuf.d_name; unlen > 0 && len > 0;) {
610c4f02a89SMax Khon /*
6110f4e4130SMax Khon * Comparison must be case insensitive, because FAT disallows
6120f4e4130SMax Khon * to look up or create files in case sensitive even when
6130f4e4130SMax Khon * it's a long file name.
614c4f02a89SMax Khon */
61509bb4a31SDimitry Andric c1 = unix2winchr(__DECONST(const u_char **, &np), &len,
61609bb4a31SDimitry Andric LCASE_BASE, pmp);
617bddcdc51STim J. Robbins c2 = unix2winchr(&un, &unlen, LCASE_BASE, pmp);
618b998efa9SAndrey A. Chernov if (c1 != c2)
619c4f02a89SMax Khon return -2;
620952a6212SJordan K. Hubbard }
621952a6212SJordan K. Hubbard return chksum;
622952a6212SJordan K. Hubbard }
623952a6212SJordan K. Hubbard
624952a6212SJordan K. Hubbard /*
625952a6212SJordan K. Hubbard * Convert Win95 filename to dirbuf.
626952a6212SJordan K. Hubbard * Returns the checksum or -1 if impossible
627952a6212SJordan K. Hubbard */
628952a6212SJordan K. Hubbard int
win2unixfn(struct mbnambuf * nbp,struct winentry * wep,int chksum,struct msdosfsmount * pmp)6294882501bSEd Maste win2unixfn(struct mbnambuf *nbp, struct winentry *wep, int chksum,
6304882501bSEd Maste struct msdosfsmount *pmp)
631952a6212SJordan K. Hubbard {
632e2ee19e3SKevin Lo u_char *c, tmpbuf[5];
63323c53312SEd Maste uint8_t *cp;
63423c53312SEd Maste uint8_t *np, name[WIN_CHARS * 3 + 1];
63523c53312SEd Maste uint16_t code;
636952a6212SJordan K. Hubbard int i;
637952a6212SJordan K. Hubbard
638952a6212SJordan K. Hubbard if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS)
639952a6212SJordan K. Hubbard || !(wep->weCnt&WIN_CNT))
640952a6212SJordan K. Hubbard return -1;
641952a6212SJordan K. Hubbard
642952a6212SJordan K. Hubbard /*
643952a6212SJordan K. Hubbard * First compare checksums
644952a6212SJordan K. Hubbard */
645952a6212SJordan K. Hubbard if (wep->weCnt&WIN_LAST) {
646952a6212SJordan K. Hubbard chksum = wep->weChksum;
647952a6212SJordan K. Hubbard } else if (chksum != wep->weChksum)
648952a6212SJordan K. Hubbard chksum = -1;
649952a6212SJordan K. Hubbard if (chksum == -1)
650952a6212SJordan K. Hubbard return -1;
651952a6212SJordan K. Hubbard
652952a6212SJordan K. Hubbard /*
653952a6212SJordan K. Hubbard * Convert the name parts
654952a6212SJordan K. Hubbard */
655c4f02a89SMax Khon np = name;
656952a6212SJordan K. Hubbard for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
6572b5b6623SAndrey A. Chernov code = (cp[1] << 8) | cp[0];
6582b5b6623SAndrey A. Chernov switch (code) {
659952a6212SJordan K. Hubbard case 0:
6602b5b6623SAndrey A. Chernov *np = '\0';
66166527f74SKristof Provost if (mbnambuf_write(nbp, name,
66266527f74SKristof Provost (wep->weCnt & WIN_CNT) - 1) != 0)
66366527f74SKristof Provost return -1;
664952a6212SJordan K. Hubbard return chksum;
665952a6212SJordan K. Hubbard case '/':
6662b5b6623SAndrey A. Chernov *np = '\0';
667952a6212SJordan K. Hubbard return -1;
6682b5b6623SAndrey A. Chernov default:
669e2ee19e3SKevin Lo c = win2unixchr(tmpbuf, code, pmp);
67041f1dcccSKevin Lo while (*c != '\0')
67141f1dcccSKevin Lo *np++ = *c++;
6722b5b6623SAndrey A. Chernov break;
673952a6212SJordan K. Hubbard }
6742b5b6623SAndrey A. Chernov cp += 2;
675952a6212SJordan K. Hubbard }
676952a6212SJordan K. Hubbard for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
6772b5b6623SAndrey A. Chernov code = (cp[1] << 8) | cp[0];
6782b5b6623SAndrey A. Chernov switch (code) {
679952a6212SJordan K. Hubbard case 0:
6802b5b6623SAndrey A. Chernov *np = '\0';
68166527f74SKristof Provost if (mbnambuf_write(nbp, name,
68266527f74SKristof Provost (wep->weCnt & WIN_CNT) - 1) != 0)
68366527f74SKristof Provost return -1;
684952a6212SJordan K. Hubbard return chksum;
685952a6212SJordan K. Hubbard case '/':
6862b5b6623SAndrey A. Chernov *np = '\0';
687952a6212SJordan K. Hubbard return -1;
6882b5b6623SAndrey A. Chernov default:
689e2ee19e3SKevin Lo c = win2unixchr(tmpbuf, code, pmp);
69041f1dcccSKevin Lo while (*c != '\0')
69141f1dcccSKevin Lo *np++ = *c++;
6922b5b6623SAndrey A. Chernov break;
693952a6212SJordan K. Hubbard }
6942b5b6623SAndrey A. Chernov cp += 2;
695952a6212SJordan K. Hubbard }
696952a6212SJordan K. Hubbard for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
6972b5b6623SAndrey A. Chernov code = (cp[1] << 8) | cp[0];
6982b5b6623SAndrey A. Chernov switch (code) {
699952a6212SJordan K. Hubbard case 0:
7002b5b6623SAndrey A. Chernov *np = '\0';
70166527f74SKristof Provost if (mbnambuf_write(nbp, name,
70266527f74SKristof Provost (wep->weCnt & WIN_CNT) - 1) != 0)
70366527f74SKristof Provost return -1;
704952a6212SJordan K. Hubbard return chksum;
705952a6212SJordan K. Hubbard case '/':
7062b5b6623SAndrey A. Chernov *np = '\0';
707952a6212SJordan K. Hubbard return -1;
7082b5b6623SAndrey A. Chernov default:
709e2ee19e3SKevin Lo c = win2unixchr(tmpbuf, code, pmp);
71041f1dcccSKevin Lo while (*c != '\0')
71141f1dcccSKevin Lo *np++ = *c++;
7122b5b6623SAndrey A. Chernov break;
713952a6212SJordan K. Hubbard }
7142b5b6623SAndrey A. Chernov cp += 2;
715952a6212SJordan K. Hubbard }
716c4f02a89SMax Khon *np = '\0';
71766527f74SKristof Provost if (mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1) != 0)
71866527f74SKristof Provost return -1;
719952a6212SJordan K. Hubbard return chksum;
720952a6212SJordan K. Hubbard }
721952a6212SJordan K. Hubbard
722952a6212SJordan K. Hubbard /*
7232a05fbb9SNate Lawson * Compute the unrolled checksum of a DOS filename for Win95 LFN use.
724952a6212SJordan K. Hubbard */
72523c53312SEd Maste uint8_t
winChksum(uint8_t * name)72623c53312SEd Maste winChksum(uint8_t *name)
727952a6212SJordan K. Hubbard {
72848d1bcf8SKonstantin Belousov int i;
72923c53312SEd Maste uint8_t s;
730952a6212SJordan K. Hubbard
73148d1bcf8SKonstantin Belousov for (s = 0, i = 11; --i >= 0; s += *name++)
73248d1bcf8SKonstantin Belousov s = (s << 7)|(s >> 1);
7332a05fbb9SNate Lawson return (s);
734952a6212SJordan K. Hubbard }
735952a6212SJordan K. Hubbard
736952a6212SJordan K. Hubbard /*
737952a6212SJordan K. Hubbard * Determine the number of slots necessary for Win95 names
738952a6212SJordan K. Hubbard */
739952a6212SJordan K. Hubbard int
winSlotCnt(const u_char * un,size_t unlen,struct msdosfsmount * pmp)7404882501bSEd Maste winSlotCnt(const u_char *un, size_t unlen, struct msdosfsmount *pmp)
741952a6212SJordan K. Hubbard {
742c4f02a89SMax Khon size_t wlen;
743c4f02a89SMax Khon char wn[WIN_MAXLEN * 2 + 1], *wnp;
744c4f02a89SMax Khon
74569a36f24SMike Smith unlen = winLenFixup(un, unlen);
746c4f02a89SMax Khon
747c4f02a89SMax Khon if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
748c4f02a89SMax Khon wlen = WIN_MAXLEN * 2;
749c4f02a89SMax Khon wnp = wn;
750bddcdc51STim J. Robbins msdosfs_iconv->conv(pmp->pm_u2w, (const char **)&un, &unlen, &wnp, &wlen);
751c4f02a89SMax Khon if (unlen > 0)
752c4f02a89SMax Khon return 0;
753c4f02a89SMax Khon return howmany(WIN_MAXLEN - wlen/2, WIN_CHARS);
754c4f02a89SMax Khon }
755c4f02a89SMax Khon
756952a6212SJordan K. Hubbard if (unlen > WIN_MAXLEN)
757952a6212SJordan K. Hubbard return 0;
758952a6212SJordan K. Hubbard return howmany(unlen, WIN_CHARS);
75927a0bc89SDoug Rabson }
76069a36f24SMike Smith
76169a36f24SMike Smith /*
762b3a15dddSPedro F. Giffuni * Determine the number of bytes necessary for Win95 names
76369a36f24SMike Smith */
764bddcdc51STim J. Robbins size_t
winLenFixup(const u_char * un,size_t unlen)7654882501bSEd Maste winLenFixup(const u_char *un, size_t unlen)
76669a36f24SMike Smith {
76769a36f24SMike Smith for (un += unlen; unlen > 0; unlen--)
76869a36f24SMike Smith if (*--un != ' ' && *un != '.')
76969a36f24SMike Smith break;
77069a36f24SMike Smith return unlen;
77169a36f24SMike Smith }
772c4f02a89SMax Khon
773c4f02a89SMax Khon /*
774a8b5c2a0SGabor Kovesdan * Store an area with multi byte string instr, and returns left
775c4f02a89SMax Khon * byte of instr and moves pointer forward. The area's size is
776c4f02a89SMax Khon * inlen or outlen.
777c4f02a89SMax Khon */
778c4f02a89SMax Khon static int
mbsadjpos(const char ** instr,size_t inlen,size_t outlen,int weight,int flag,void * handle)779bddcdc51STim J. Robbins mbsadjpos(const char **instr, size_t inlen, size_t outlen, int weight, int flag, void *handle)
780c4f02a89SMax Khon {
781c4f02a89SMax Khon char *outp, outstr[outlen * weight + 1];
782c4f02a89SMax Khon
783c4f02a89SMax Khon if (flag & MSDOSFSMNT_KICONV && msdosfs_iconv) {
784c4f02a89SMax Khon outp = outstr;
785c4f02a89SMax Khon outlen *= weight;
786bddcdc51STim J. Robbins msdosfs_iconv->conv(handle, instr, &inlen, &outp, &outlen);
787c4f02a89SMax Khon return (inlen);
788c4f02a89SMax Khon }
789c4f02a89SMax Khon
790c4f02a89SMax Khon (*instr) += min(inlen, outlen);
791c4f02a89SMax Khon return (inlen - min(inlen, outlen));
792c4f02a89SMax Khon }
793c4f02a89SMax Khon
794c4f02a89SMax Khon /*
795c4f02a89SMax Khon * Convert DOS char to Local char
796c4f02a89SMax Khon */
79741f1dcccSKevin Lo static u_char *
dos2unixchr(u_char * outbuf,const u_char ** instr,size_t * ilen,int lower,struct msdosfsmount * pmp)798e2ee19e3SKevin Lo dos2unixchr(u_char *outbuf, const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp)
799c4f02a89SMax Khon {
800e2ee19e3SKevin Lo u_char c, *outp;
801c4f02a89SMax Khon size_t len, olen;
802c4f02a89SMax Khon
803c4f02a89SMax Khon outp = outbuf;
80441f1dcccSKevin Lo if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
80541f1dcccSKevin Lo olen = len = 4;
806c4f02a89SMax Khon
807c4f02a89SMax Khon if (lower & (LCASE_BASE | LCASE_EXT))
808c4f02a89SMax Khon msdosfs_iconv->convchr_case(pmp->pm_d2u, (const char **)instr,
80941f1dcccSKevin Lo ilen, (char **)&outp, &olen, KICONV_LOWER);
810c4f02a89SMax Khon else
811c4f02a89SMax Khon msdosfs_iconv->convchr(pmp->pm_d2u, (const char **)instr,
81241f1dcccSKevin Lo ilen, (char **)&outp, &olen);
813c4f02a89SMax Khon len -= olen;
814c4f02a89SMax Khon
815c4f02a89SMax Khon /*
816c4f02a89SMax Khon * return '?' if failed to convert
817c4f02a89SMax Khon */
818c4f02a89SMax Khon if (len == 0) {
819c4f02a89SMax Khon (*ilen)--;
820c4f02a89SMax Khon (*instr)++;
82141f1dcccSKevin Lo *outp++ = '?';
822c4f02a89SMax Khon }
82341f1dcccSKevin Lo } else {
824c4f02a89SMax Khon (*ilen)--;
825c4f02a89SMax Khon c = *(*instr)++;
826c4f02a89SMax Khon c = dos2unix[c];
827c4f02a89SMax Khon if (lower & (LCASE_BASE | LCASE_EXT))
828c4f02a89SMax Khon c = u2l[c];
82941f1dcccSKevin Lo *outp++ = c;
83041f1dcccSKevin Lo outbuf[1] = '\0';
83141f1dcccSKevin Lo }
83241f1dcccSKevin Lo
83341f1dcccSKevin Lo *outp = '\0';
83441f1dcccSKevin Lo outp = outbuf;
83541f1dcccSKevin Lo return (outp);
836c4f02a89SMax Khon }
837c4f02a89SMax Khon
838c4f02a89SMax Khon /*
839c4f02a89SMax Khon * Convert Local char to DOS char
840c4f02a89SMax Khon */
84123c53312SEd Maste static uint16_t
unix2doschr(const u_char ** instr,size_t * ilen,struct msdosfsmount * pmp)842c4f02a89SMax Khon unix2doschr(const u_char **instr, size_t *ilen, struct msdosfsmount *pmp)
843c4f02a89SMax Khon {
844c4f02a89SMax Khon u_char c;
845c4f02a89SMax Khon char *up, *outp, unicode[3], outbuf[3];
84623c53312SEd Maste uint16_t wc;
847697ab829SR. Imura size_t len, ucslen, unixlen, olen;
848c4f02a89SMax Khon
849c4f02a89SMax Khon if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
850c4f02a89SMax Khon /*
851c4f02a89SMax Khon * to hide an invisible character, using a unicode filter
852c4f02a89SMax Khon */
853697ab829SR. Imura ucslen = 2;
854c4f02a89SMax Khon len = *ilen;
855c4f02a89SMax Khon up = unicode;
856c4f02a89SMax Khon msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr,
857697ab829SR. Imura ilen, &up, &ucslen);
858697ab829SR. Imura unixlen = len - *ilen;
859c4f02a89SMax Khon
860c4f02a89SMax Khon /*
861c4f02a89SMax Khon * cannot be converted
862c4f02a89SMax Khon */
863697ab829SR. Imura if (unixlen == 0) {
864c4f02a89SMax Khon (*ilen)--;
865c4f02a89SMax Khon (*instr)++;
866c4f02a89SMax Khon return (0);
867c4f02a89SMax Khon }
868c4f02a89SMax Khon
869c4f02a89SMax Khon /*
870c4f02a89SMax Khon * return magic number for ascii char
871c4f02a89SMax Khon */
872697ab829SR. Imura if (unixlen == 1) {
873c4f02a89SMax Khon c = *(*instr -1);
874c4f02a89SMax Khon if (! (c & 0x80)) {
875c4f02a89SMax Khon c = unix2dos[c];
876c4f02a89SMax Khon if (c <= 2)
877c4f02a89SMax Khon return (c);
878c4f02a89SMax Khon }
879c4f02a89SMax Khon }
880c4f02a89SMax Khon
881c4f02a89SMax Khon /*
882c4f02a89SMax Khon * now convert using libiconv
883c4f02a89SMax Khon */
884697ab829SR. Imura *instr -= unixlen;
885697ab829SR. Imura *ilen = len;
886c4f02a89SMax Khon
887c4f02a89SMax Khon olen = len = 2;
888c4f02a89SMax Khon outp = outbuf;
889c4f02a89SMax Khon msdosfs_iconv->convchr_case(pmp->pm_u2d, (const char **)instr,
890c4f02a89SMax Khon ilen, &outp, &olen, KICONV_FROM_UPPER);
891c4f02a89SMax Khon len -= olen;
892697ab829SR. Imura
893697ab829SR. Imura /*
894697ab829SR. Imura * cannot be converted, but has unicode char, should return magic number
895697ab829SR. Imura */
896697ab829SR. Imura if (len == 0) {
897697ab829SR. Imura (*ilen) -= unixlen;
898697ab829SR. Imura (*instr) += unixlen;
899697ab829SR. Imura return (1);
900697ab829SR. Imura }
901697ab829SR. Imura
902c4f02a89SMax Khon wc = 0;
903c4f02a89SMax Khon while(len--)
904c4f02a89SMax Khon wc |= (*(outp - len - 1) & 0xff) << (len << 3);
905c4f02a89SMax Khon return (wc);
906c4f02a89SMax Khon }
907c4f02a89SMax Khon
908c4f02a89SMax Khon (*ilen)--;
909c4f02a89SMax Khon c = *(*instr)++;
910c4f02a89SMax Khon c = l2u[c];
911c4f02a89SMax Khon c = unix2dos[c];
91223c53312SEd Maste return ((uint16_t)c);
913c4f02a89SMax Khon }
914c4f02a89SMax Khon
915c4f02a89SMax Khon /*
916c4f02a89SMax Khon * Convert Windows char to Local char
917c4f02a89SMax Khon */
91841f1dcccSKevin Lo static u_char *
win2unixchr(u_char * outbuf,uint16_t wc,struct msdosfsmount * pmp)91923c53312SEd Maste win2unixchr(u_char *outbuf, uint16_t wc, struct msdosfsmount *pmp)
920c4f02a89SMax Khon {
921e2ee19e3SKevin Lo u_char *inp, *outp, inbuf[3];
922c4f02a89SMax Khon size_t ilen, olen, len;
923c4f02a89SMax Khon
92441f1dcccSKevin Lo outp = outbuf;
925c4f02a89SMax Khon if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
926c4f02a89SMax Khon inbuf[0] = (u_char)(wc>>8);
927c4f02a89SMax Khon inbuf[1] = (u_char)wc;
928c4f02a89SMax Khon inbuf[2] = '\0';
929c4f02a89SMax Khon
93041f1dcccSKevin Lo ilen = 2;
93141f1dcccSKevin Lo olen = len = 4;
932c4f02a89SMax Khon inp = inbuf;
93309bb4a31SDimitry Andric msdosfs_iconv->convchr(pmp->pm_w2u, __DECONST(const char **,
93409bb4a31SDimitry Andric &inp), &ilen, (char **)&outp, &olen);
935c4f02a89SMax Khon len -= olen;
936c4f02a89SMax Khon
937c4f02a89SMax Khon /*
938c4f02a89SMax Khon * return '?' if failed to convert
939c4f02a89SMax Khon */
94041f1dcccSKevin Lo if (len == 0)
94141f1dcccSKevin Lo *outp++ = '?';
94241f1dcccSKevin Lo } else {
94341f1dcccSKevin Lo *outp++ = (wc & 0xff00) ? '?' : (u_char)(wc & 0xff);
944c4f02a89SMax Khon }
945c4f02a89SMax Khon
94641f1dcccSKevin Lo *outp = '\0';
94741f1dcccSKevin Lo outp = outbuf;
94841f1dcccSKevin Lo return (outp);
949c4f02a89SMax Khon }
950c4f02a89SMax Khon
951c4f02a89SMax Khon /*
952c4f02a89SMax Khon * Convert Local char to Windows char
953c4f02a89SMax Khon */
95423c53312SEd Maste static uint16_t
unix2winchr(const u_char ** instr,size_t * ilen,int lower,struct msdosfsmount * pmp)955c4f02a89SMax Khon unix2winchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp)
956c4f02a89SMax Khon {
957c4f02a89SMax Khon u_char *outp, outbuf[3];
95823c53312SEd Maste uint16_t wc;
959c4f02a89SMax Khon size_t olen;
960c4f02a89SMax Khon
961c4f02a89SMax Khon if (*ilen == 0)
962c4f02a89SMax Khon return (0);
963c4f02a89SMax Khon
964c4f02a89SMax Khon if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
965c4f02a89SMax Khon outp = outbuf;
966c4f02a89SMax Khon olen = 2;
967c4f02a89SMax Khon if (lower & (LCASE_BASE | LCASE_EXT))
968c4f02a89SMax Khon msdosfs_iconv->convchr_case(pmp->pm_u2w, (const char **)instr,
969c4f02a89SMax Khon ilen, (char **)&outp, &olen,
970c4f02a89SMax Khon KICONV_FROM_LOWER);
971c4f02a89SMax Khon else
972c4f02a89SMax Khon msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr,
973c4f02a89SMax Khon ilen, (char **)&outp, &olen);
974c4f02a89SMax Khon
975c4f02a89SMax Khon /*
976c4f02a89SMax Khon * return '0' if end of filename
977c4f02a89SMax Khon */
978c4f02a89SMax Khon if (olen == 2)
979c4f02a89SMax Khon return (0);
980c4f02a89SMax Khon
981c4f02a89SMax Khon wc = (outbuf[0]<<8) | outbuf[1];
982c4f02a89SMax Khon
983c4f02a89SMax Khon return (wc);
984c4f02a89SMax Khon }
985c4f02a89SMax Khon
986c4f02a89SMax Khon (*ilen)--;
987c4f02a89SMax Khon wc = (*instr)[0];
988c4f02a89SMax Khon if (lower & (LCASE_BASE | LCASE_EXT))
989c4f02a89SMax Khon wc = u2l[wc];
990c4f02a89SMax Khon (*instr)++;
991c4f02a89SMax Khon return (wc);
992c4f02a89SMax Khon }
993c4f02a89SMax Khon
994c4f02a89SMax Khon /*
995c2819440SBruce Evans * Initialize the temporary concatenation buffer.
996c4f02a89SMax Khon */
997c4f02a89SMax Khon void
mbnambuf_init(struct mbnambuf * nbp)998c2819440SBruce Evans mbnambuf_init(struct mbnambuf *nbp)
999c4f02a89SMax Khon {
1000c4f02a89SMax Khon
1001c2819440SBruce Evans nbp->nb_len = 0;
1002c2819440SBruce Evans nbp->nb_last_id = -1;
1003c2819440SBruce Evans nbp->nb_buf[sizeof(nbp->nb_buf) - 1] = '\0';
1004c4f02a89SMax Khon }
1005c4f02a89SMax Khon
1006c4f02a89SMax Khon /*
100758ad326bSNate Lawson * Fill out our concatenation buffer with the given substring, at the offset
100858ad326bSNate Lawson * specified by its id. Since this function must be called with ids in
100958ad326bSNate Lawson * descending order, we take advantage of the fact that ASCII substrings are
101058ad326bSNate Lawson * exactly WIN_CHARS in length. For non-ASCII substrings, we shift all
101158ad326bSNate Lawson * previous (i.e. higher id) substrings upwards to make room for this one.
101258ad326bSNate Lawson * This only penalizes portions of substrings that contain more than
101358ad326bSNate Lawson * WIN_CHARS bytes when they are first encountered.
1014c4f02a89SMax Khon */
101566527f74SKristof Provost int
mbnambuf_write(struct mbnambuf * nbp,char * name,int id)1016c2819440SBruce Evans mbnambuf_write(struct mbnambuf *nbp, char *name, int id)
1017c4f02a89SMax Khon {
101858ad326bSNate Lawson char *slot;
1019c2819440SBruce Evans size_t count, newlen;
10204cdb1483SNate Lawson
1021abb0cbf9SEdward Tomasz Napierala if (nbp->nb_len != 0 && id != nbp->nb_last_id - 1) {
102254cf9198SKonstantin Belousov #ifdef MSDOSFS_DEBUG
1023abb0cbf9SEdward Tomasz Napierala printf("msdosfs: non-decreasing id: id %d, last id %d\n",
1024abb0cbf9SEdward Tomasz Napierala id, nbp->nb_last_id);
102554cf9198SKonstantin Belousov #endif
102666527f74SKristof Provost return (EINVAL);
1027abb0cbf9SEdward Tomasz Napierala }
102858ad326bSNate Lawson
1029c2819440SBruce Evans /* Will store this substring in a WIN_CHARS-aligned slot. */
1030c2819440SBruce Evans slot = &nbp->nb_buf[id * WIN_CHARS];
10314cdb1483SNate Lawson count = strlen(name);
1032c2819440SBruce Evans newlen = nbp->nb_len + count;
1033c2819440SBruce Evans if (newlen > WIN_MAXLEN || newlen > MAXNAMLEN) {
103454cf9198SKonstantin Belousov #ifdef MSDOSFS_DEBUG
1035c2819440SBruce Evans printf("msdosfs: file name length %zu too large\n", newlen);
103654cf9198SKonstantin Belousov #endif
103766527f74SKristof Provost return (ENAMETOOLONG);
1038c4f02a89SMax Khon }
103958ad326bSNate Lawson
104058ad326bSNate Lawson /* Shift suffix upwards by the amount length exceeds WIN_CHARS. */
104166527f74SKristof Provost if (count > WIN_CHARS && nbp->nb_len != 0) {
104266527f74SKristof Provost if ((id * WIN_CHARS + count + nbp->nb_len) >
104366527f74SKristof Provost sizeof(nbp->nb_buf))
104466527f74SKristof Provost return (ENAMETOOLONG);
104566527f74SKristof Provost
10466a1c2e1fSEd Maste memmove(slot + count, slot + WIN_CHARS, nbp->nb_len);
104766527f74SKristof Provost }
104858ad326bSNate Lawson
104958ad326bSNate Lawson /* Copy in the substring to its slot and update length so far. */
10506a1c2e1fSEd Maste memcpy(slot, name, count);
1051c2819440SBruce Evans nbp->nb_len = newlen;
1052c2819440SBruce Evans nbp->nb_last_id = id;
105366527f74SKristof Provost
105466527f74SKristof Provost return (0);
1055c4f02a89SMax Khon }
1056c4f02a89SMax Khon
1057c4f02a89SMax Khon /*
10584cdb1483SNate Lawson * Take the completed string and use it to setup the struct dirent.
10594cdb1483SNate Lawson * Be sure to always nul-terminate the d_name and then copy the string
10604cdb1483SNate Lawson * from our buffer. Note that this function assumes the full string has
10614cdb1483SNate Lawson * been reassembled in the buffer. If it's called before all substrings
10624cdb1483SNate Lawson * have been written via mbnambuf_write(), the result will be incorrect.
1063c4f02a89SMax Khon */
1064c4f02a89SMax Khon char *
mbnambuf_flush(struct mbnambuf * nbp,struct dirent * dp)1065c2819440SBruce Evans mbnambuf_flush(struct mbnambuf *nbp, struct dirent *dp)
1066c4f02a89SMax Khon {
1067c4f02a89SMax Khon
1068c2819440SBruce Evans if (nbp->nb_len > sizeof(dp->d_name) - 1) {
1069c2819440SBruce Evans mbnambuf_init(nbp);
1070c4f02a89SMax Khon return (NULL);
1071c4f02a89SMax Khon }
10726a1c2e1fSEd Maste memcpy(dp->d_name, &nbp->nb_buf[0], nbp->nb_len);
1073c2819440SBruce Evans dp->d_name[nbp->nb_len] = '\0';
1074c2819440SBruce Evans dp->d_namlen = nbp->nb_len;
10754cdb1483SNate Lawson
1076c2819440SBruce Evans mbnambuf_init(nbp);
1077c4f02a89SMax Khon return (dp->d_name);
1078c4f02a89SMax Khon }
1079