1c3aac50fSPeter Wemm /* $FreeBSD$ */ 2952a6212SJordan K. Hubbard /* $NetBSD: msdosfs_conv.c,v 1.25 1997/11/17 15:36:40 ws Exp $ */ 327a0bc89SDoug Rabson 4952a6212SJordan K. Hubbard /*- 5952a6212SJordan K. Hubbard * Copyright (C) 1995, 1997 Wolfgang Solfrank. 6952a6212SJordan K. Hubbard * Copyright (C) 1995, 1997 TooLs GmbH. 7952a6212SJordan K. Hubbard * All rights reserved. 8952a6212SJordan K. Hubbard * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 9952a6212SJordan K. Hubbard * 10952a6212SJordan K. Hubbard * Redistribution and use in source and binary forms, with or without 11952a6212SJordan K. Hubbard * modification, are permitted provided that the following conditions 12952a6212SJordan K. Hubbard * are met: 13952a6212SJordan K. Hubbard * 1. Redistributions of source code must retain the above copyright 14952a6212SJordan K. Hubbard * notice, this list of conditions and the following disclaimer. 15952a6212SJordan K. Hubbard * 2. Redistributions in binary form must reproduce the above copyright 16952a6212SJordan K. Hubbard * notice, this list of conditions and the following disclaimer in the 17952a6212SJordan K. Hubbard * documentation and/or other materials provided with the distribution. 18952a6212SJordan K. Hubbard * 3. All advertising materials mentioning features or use of this software 19952a6212SJordan K. Hubbard * must display the following acknowledgement: 20952a6212SJordan K. Hubbard * This product includes software developed by TooLs GmbH. 21952a6212SJordan K. Hubbard * 4. The name of TooLs GmbH may not be used to endorse or promote products 22952a6212SJordan K. Hubbard * derived from this software without specific prior written permission. 23952a6212SJordan K. Hubbard * 24952a6212SJordan K. Hubbard * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 25952a6212SJordan K. Hubbard * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26952a6212SJordan K. Hubbard * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27952a6212SJordan K. Hubbard * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28952a6212SJordan K. Hubbard * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29952a6212SJordan K. Hubbard * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30952a6212SJordan K. Hubbard * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31952a6212SJordan K. Hubbard * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32952a6212SJordan K. Hubbard * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 33952a6212SJordan K. Hubbard * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34952a6212SJordan K. Hubbard */ 35d167cf6fSWarner Losh /*- 3627a0bc89SDoug Rabson * Written by Paul Popelka (paulp@uts.amdahl.com) 3727a0bc89SDoug Rabson * 3827a0bc89SDoug Rabson * You can do anything you want with this software, just don't say you wrote 3927a0bc89SDoug Rabson * it, and don't remove this notice. 4027a0bc89SDoug Rabson * 4127a0bc89SDoug Rabson * This software is provided "as is". 4227a0bc89SDoug Rabson * 4327a0bc89SDoug Rabson * The author supplies this software to be publicly redistributed on the 4427a0bc89SDoug Rabson * understanding that the author is not responsible for the correct 4527a0bc89SDoug Rabson * functioning of this software in any circumstances and is not liable for 4627a0bc89SDoug Rabson * any damages caused by this software. 4727a0bc89SDoug Rabson * 4827a0bc89SDoug Rabson * October 1992 4927a0bc89SDoug Rabson */ 5027a0bc89SDoug Rabson 5127a0bc89SDoug Rabson #include <sys/param.h> 52952a6212SJordan K. Hubbard #include <sys/systm.h> 53952a6212SJordan K. Hubbard #include <sys/dirent.h> 54c4f02a89SMax Khon #include <sys/iconv.h> 559b0802c9SBruce Evans #include <sys/mount.h> 569b0802c9SBruce Evans 579b0802c9SBruce Evans #include <fs/msdosfs/bpb.h> 589b0802c9SBruce Evans #include <fs/msdosfs/direntry.h> 599b0802c9SBruce Evans #include <fs/msdosfs/msdosfsmount.h> 60c4f02a89SMax Khon 61c4f02a89SMax Khon extern struct iconv_functions *msdosfs_iconv; 6227a0bc89SDoug Rabson 63bddcdc51STim J. Robbins static int mbsadjpos(const char **, size_t, size_t, int, int, void *handle); 64*e2ee19e3SKevin Lo static u_char * dos2unixchr(u_char *, const u_char **, size_t *, int, struct msdosfsmount *); 65c4f02a89SMax Khon static u_int16_t unix2doschr(const u_char **, size_t *, struct msdosfsmount *); 66*e2ee19e3SKevin Lo static u_char * win2unixchr(u_char *, u_int16_t, struct msdosfsmount *); 67c4f02a89SMax Khon static u_int16_t unix2winchr(const u_char **, size_t *, int, struct msdosfsmount *); 68c4f02a89SMax Khon 6927a0bc89SDoug Rabson /* 70cce0eefdSMike Smith * 0 - character disallowed in long file name. 71cce0eefdSMike Smith * 1 - character should be replaced by '_' in DOS file name, 72cce0eefdSMike Smith * and generation number inserted. 73cce0eefdSMike Smith * 2 - character ('.' and ' ') should be skipped in DOS file name, 74cce0eefdSMike Smith * and generation number inserted. 75cce0eefdSMike Smith */ 76952a6212SJordan K. Hubbard static u_char 77952a6212SJordan K. Hubbard unix2dos[256] = { 78c4f02a89SMax Khon /* iso8859-1 -> cp850 */ 79952a6212SJordan K. Hubbard 0, 0, 0, 0, 0, 0, 0, 0, /* 00-07 */ 80952a6212SJordan K. Hubbard 0, 0, 0, 0, 0, 0, 0, 0, /* 08-0f */ 81952a6212SJordan K. Hubbard 0, 0, 0, 0, 0, 0, 0, 0, /* 10-17 */ 82952a6212SJordan K. Hubbard 0, 0, 0, 0, 0, 0, 0, 0, /* 18-1f */ 83cce0eefdSMike Smith 2, 0x21, 0, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 84cce0eefdSMike Smith 0x28, 0x29, 0, 1, 1, 0x2d, 2, 0, /* 28-2f */ 85952a6212SJordan K. Hubbard 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 86cce0eefdSMike Smith 0x38, 0x39, 0, 1, 0, 1, 0, 0, /* 38-3f */ 87952a6212SJordan K. Hubbard 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */ 88952a6212SJordan K. Hubbard 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */ 89952a6212SJordan K. Hubbard 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */ 90cce0eefdSMike Smith 0x58, 0x59, 0x5a, 1, 0, 1, 0x5e, 0x5f, /* 58-5f */ 91952a6212SJordan K. Hubbard 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 60-67 */ 92952a6212SJordan K. Hubbard 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 68-6f */ 93952a6212SJordan K. Hubbard 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 70-77 */ 94952a6212SJordan K. Hubbard 0x58, 0x59, 0x5a, 0x7b, 0, 0x7d, 0x7e, 0, /* 78-7f */ 95952a6212SJordan K. Hubbard 0, 0, 0, 0, 0, 0, 0, 0, /* 80-87 */ 96952a6212SJordan K. Hubbard 0, 0, 0, 0, 0, 0, 0, 0, /* 88-8f */ 97952a6212SJordan K. Hubbard 0, 0, 0, 0, 0, 0, 0, 0, /* 90-97 */ 98952a6212SJordan K. Hubbard 0, 0, 0, 0, 0, 0, 0, 0, /* 98-9f */ 99952a6212SJordan K. Hubbard 0, 0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5, /* a0-a7 */ 100952a6212SJordan K. Hubbard 0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee, /* a8-af */ 101952a6212SJordan K. Hubbard 0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa, /* b0-b7 */ 102952a6212SJordan K. Hubbard 0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8, /* b8-bf */ 103952a6212SJordan K. Hubbard 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* c0-c7 */ 104952a6212SJordan K. Hubbard 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* c8-cf */ 105952a6212SJordan K. Hubbard 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e, /* d0-d7 */ 106952a6212SJordan K. Hubbard 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1, /* d8-df */ 107952a6212SJordan K. Hubbard 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* e0-e7 */ 108952a6212SJordan K. Hubbard 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* e8-ef */ 109952a6212SJordan K. Hubbard 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6, /* f0-f7 */ 110952a6212SJordan K. Hubbard 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98, /* f8-ff */ 111952a6212SJordan K. Hubbard }; 112952a6212SJordan K. Hubbard 113952a6212SJordan K. Hubbard static u_char 114952a6212SJordan K. Hubbard dos2unix[256] = { 115c4f02a89SMax Khon /* cp850 -> iso8859-1 */ 116952a6212SJordan K. Hubbard 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 00-07 */ 117952a6212SJordan K. Hubbard 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 08-0f */ 118952a6212SJordan K. Hubbard 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 10-17 */ 119952a6212SJordan K. Hubbard 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 18-1f */ 120952a6212SJordan K. Hubbard 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 121952a6212SJordan K. Hubbard 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */ 122952a6212SJordan K. Hubbard 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 123952a6212SJordan K. Hubbard 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */ 124952a6212SJordan K. Hubbard 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */ 125952a6212SJordan K. Hubbard 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */ 126952a6212SJordan K. Hubbard 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */ 127952a6212SJordan K. Hubbard 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */ 128952a6212SJordan K. Hubbard 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */ 129952a6212SJordan K. Hubbard 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */ 130952a6212SJordan K. Hubbard 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */ 131952a6212SJordan K. Hubbard 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */ 132952a6212SJordan K. Hubbard 0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7, /* 80-87 */ 133952a6212SJordan K. Hubbard 0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5, /* 88-8f */ 134952a6212SJordan K. Hubbard 0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9, /* 90-97 */ 135952a6212SJordan K. Hubbard 0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, 0x3f, /* 98-9f */ 136952a6212SJordan K. Hubbard 0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba, /* a0-a7 */ 137952a6212SJordan K. Hubbard 0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb, /* a8-af */ 138952a6212SJordan K. Hubbard 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xc1, 0xc2, 0xc0, /* b0-b7 */ 139952a6212SJordan K. Hubbard 0xa9, 0x3f, 0x3f, 0x3f, 0x3f, 0xa2, 0xa5, 0x3f, /* b8-bf */ 140952a6212SJordan K. Hubbard 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xe3, 0xc3, /* c0-c7 */ 141952a6212SJordan K. Hubbard 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xa4, /* c8-cf */ 142952a6212SJordan K. Hubbard 0xf0, 0xd0, 0xca, 0xcb, 0xc8, 0x3f, 0xcd, 0xce, /* d0-d7 */ 143952a6212SJordan K. Hubbard 0xcf, 0x3f, 0x3f, 0x3f, 0x3f, 0xa6, 0xcc, 0x3f, /* d8-df */ 144952a6212SJordan K. Hubbard 0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe, /* e0-e7 */ 145952a6212SJordan K. Hubbard 0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f, /* e8-ef */ 146952a6212SJordan K. Hubbard 0xad, 0xb1, 0x3f, 0xbe, 0xb6, 0xa7, 0xf7, 0xb8, /* f0-f7 */ 147952a6212SJordan K. Hubbard 0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2, 0x3f, 0x3f, /* f8-ff */ 148952a6212SJordan K. Hubbard }; 149952a6212SJordan K. Hubbard 150952a6212SJordan K. Hubbard static u_char 151952a6212SJordan K. Hubbard u2l[256] = { 152c4f02a89SMax Khon /* tolower */ 153952a6212SJordan K. Hubbard 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */ 154952a6212SJordan K. Hubbard 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */ 155952a6212SJordan K. Hubbard 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */ 156952a6212SJordan K. Hubbard 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */ 157952a6212SJordan K. Hubbard 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 158952a6212SJordan K. Hubbard 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */ 159952a6212SJordan K. Hubbard 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 160952a6212SJordan K. Hubbard 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */ 161952a6212SJordan K. Hubbard 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */ 162952a6212SJordan K. Hubbard 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */ 163952a6212SJordan K. Hubbard 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */ 164952a6212SJordan K. Hubbard 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */ 165952a6212SJordan K. Hubbard 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */ 166952a6212SJordan K. Hubbard 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */ 167952a6212SJordan K. Hubbard 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */ 168952a6212SJordan K. Hubbard 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */ 169952a6212SJordan K. Hubbard 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */ 170952a6212SJordan K. Hubbard 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */ 171952a6212SJordan K. Hubbard 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */ 172952a6212SJordan K. Hubbard 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */ 173952a6212SJordan K. Hubbard 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */ 174952a6212SJordan K. Hubbard 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */ 175952a6212SJordan K. Hubbard 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */ 176952a6212SJordan K. Hubbard 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */ 177952a6212SJordan K. Hubbard 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */ 178952a6212SJordan K. Hubbard 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */ 179952a6212SJordan K. Hubbard 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */ 180952a6212SJordan K. Hubbard 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */ 181952a6212SJordan K. Hubbard 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */ 182952a6212SJordan K. Hubbard 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */ 183952a6212SJordan K. Hubbard 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */ 184952a6212SJordan K. Hubbard 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */ 185952a6212SJordan K. Hubbard }; 18627a0bc89SDoug Rabson 1877391f611SAndrey A. Chernov static u_char 1887391f611SAndrey A. Chernov l2u[256] = { 189c4f02a89SMax Khon /* toupper */ 1907391f611SAndrey A. Chernov 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */ 1917391f611SAndrey A. Chernov 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */ 1927391f611SAndrey A. Chernov 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */ 1937391f611SAndrey A. Chernov 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */ 1947391f611SAndrey A. Chernov 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 1957391f611SAndrey A. Chernov 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */ 1967391f611SAndrey A. Chernov 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 1977391f611SAndrey A. Chernov 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */ 1987391f611SAndrey A. Chernov 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */ 1997391f611SAndrey A. Chernov 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */ 2007391f611SAndrey A. Chernov 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */ 2017391f611SAndrey A. Chernov 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */ 2027391f611SAndrey A. Chernov 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */ 2037391f611SAndrey A. Chernov 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */ 2047391f611SAndrey A. Chernov 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */ 2057391f611SAndrey A. Chernov 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */ 2067391f611SAndrey A. Chernov 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */ 2077391f611SAndrey A. Chernov 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */ 2087391f611SAndrey A. Chernov 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */ 2097391f611SAndrey A. Chernov 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */ 2107391f611SAndrey A. Chernov 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */ 2117391f611SAndrey A. Chernov 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */ 2127391f611SAndrey A. Chernov 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */ 2137391f611SAndrey A. Chernov 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */ 2147391f611SAndrey A. Chernov 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */ 2157391f611SAndrey A. Chernov 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */ 2167391f611SAndrey A. Chernov 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */ 2177391f611SAndrey A. Chernov 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */ 2187391f611SAndrey A. Chernov 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */ 2197391f611SAndrey A. Chernov 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */ 2207391f611SAndrey A. Chernov 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */ 2217391f611SAndrey A. Chernov 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */ 2227391f611SAndrey A. Chernov }; 2237391f611SAndrey A. Chernov 22427a0bc89SDoug Rabson /* 22527a0bc89SDoug Rabson * DOS filenames are made of 2 parts, the name part and the extension part. 22627a0bc89SDoug Rabson * The name part is 8 characters long and the extension part is 3 22727a0bc89SDoug Rabson * characters long. They may contain trailing blanks if the name or 22827a0bc89SDoug Rabson * extension are not long enough to fill their respective fields. 22927a0bc89SDoug Rabson */ 23027a0bc89SDoug Rabson 23127a0bc89SDoug Rabson /* 23227a0bc89SDoug Rabson * Convert a DOS filename to a unix filename. And, return the number of 23327a0bc89SDoug Rabson * characters in the resulting unix filename excluding the terminating 23427a0bc89SDoug Rabson * null. 23527a0bc89SDoug Rabson */ 23627a0bc89SDoug Rabson int 237c4f02a89SMax Khon dos2unixfn(dn, un, lower, pmp) 23827a0bc89SDoug Rabson u_char dn[11]; 23927a0bc89SDoug Rabson u_char *un; 2405114f1d7SAndrey A. Chernov int lower; 241c4f02a89SMax Khon struct msdosfsmount *pmp; 24227a0bc89SDoug Rabson { 243bddcdc51STim J. Robbins size_t i; 244c4f02a89SMax Khon int thislong = 0; 245*e2ee19e3SKevin Lo u_char *c, tmpbuf[5]; 24627a0bc89SDoug Rabson 24727a0bc89SDoug Rabson /* 24827a0bc89SDoug Rabson * If first char of the filename is SLOT_E5 (0x05), then the real 24927a0bc89SDoug Rabson * first char of the filename should be 0xe5. But, they couldn't 25027a0bc89SDoug Rabson * just have a 0xe5 mean 0xe5 because that is used to mean a freed 25127a0bc89SDoug Rabson * directory slot. Another dos quirk. 25227a0bc89SDoug Rabson */ 253952a6212SJordan K. Hubbard if (*dn == SLOT_E5) 254c4f02a89SMax Khon *dn = 0xe5; 25527a0bc89SDoug Rabson 256952a6212SJordan K. Hubbard /* 257952a6212SJordan K. Hubbard * Copy the name portion into the unix filename string. 258952a6212SJordan K. Hubbard */ 259c4f02a89SMax Khon for (i = 8; i > 0 && *dn != ' ';) { 260*e2ee19e3SKevin Lo c = dos2unixchr(tmpbuf, (const u_char **)&dn, &i, 261*e2ee19e3SKevin Lo lower & LCASE_BASE, pmp); 26241f1dcccSKevin Lo while (*c != '\0') { 26341f1dcccSKevin Lo *un++ = *c++; 264952a6212SJordan K. Hubbard thislong++; 265952a6212SJordan K. Hubbard } 266c4f02a89SMax Khon } 267c4f02a89SMax Khon dn += i; 268952a6212SJordan K. Hubbard 269952a6212SJordan K. Hubbard /* 270952a6212SJordan K. Hubbard * Now, if there is an extension then put in a period and copy in 271952a6212SJordan K. Hubbard * the extension. 272952a6212SJordan K. Hubbard */ 273952a6212SJordan K. Hubbard if (*dn != ' ') { 274952a6212SJordan K. Hubbard *un++ = '.'; 275952a6212SJordan K. Hubbard thislong++; 276c4f02a89SMax Khon for (i = 3; i > 0 && *dn != ' ';) { 277*e2ee19e3SKevin Lo c = dos2unixchr(tmpbuf, (const u_char **)&dn, &i, 278cc005bb9SMaxim Konovalov lower & LCASE_EXT, pmp); 27941f1dcccSKevin Lo while (*c != '\0') { 28041f1dcccSKevin Lo *un++ = *c++; 281c4f02a89SMax Khon thislong++; 282c4f02a89SMax Khon } 283952a6212SJordan K. Hubbard } 284952a6212SJordan K. Hubbard } 285952a6212SJordan K. Hubbard *un++ = 0; 286952a6212SJordan K. Hubbard 287952a6212SJordan K. Hubbard return (thislong); 28827a0bc89SDoug Rabson } 28927a0bc89SDoug Rabson 29027a0bc89SDoug Rabson /* 291952a6212SJordan K. Hubbard * Convert a unix filename to a DOS filename according to Win95 rules. 292952a6212SJordan K. Hubbard * If applicable and gen is not 0, it is inserted into the converted 293952a6212SJordan K. Hubbard * filename as a generation number. 294952a6212SJordan K. Hubbard * Returns 295952a6212SJordan K. Hubbard * 0 if name couldn't be converted 296952a6212SJordan K. Hubbard * 1 if the converted name is the same as the original 297952a6212SJordan K. Hubbard * (no long filename entry necessary for Win95) 298952a6212SJordan K. Hubbard * 2 if conversion was successful 299952a6212SJordan K. Hubbard * 3 if conversion was successful and generation number was inserted 30027a0bc89SDoug Rabson */ 301952a6212SJordan K. Hubbard int 302c4f02a89SMax Khon unix2dosfn(un, dn, unlen, gen, pmp) 303952a6212SJordan K. Hubbard const u_char *un; 304952a6212SJordan K. Hubbard u_char dn[12]; 305bddcdc51STim J. Robbins size_t unlen; 306952a6212SJordan K. Hubbard u_int gen; 307c4f02a89SMax Khon struct msdosfsmount *pmp; 30827a0bc89SDoug Rabson { 309bddcdc51STim J. Robbins ssize_t i, j; 310bddcdc51STim J. Robbins int l; 311952a6212SJordan K. Hubbard int conv = 1; 312952a6212SJordan K. Hubbard const u_char *cp, *dp, *dp1; 313952a6212SJordan K. Hubbard u_char gentext[6], *wcp; 314c4f02a89SMax Khon u_int16_t c; 31527a0bc89SDoug Rabson 31627a0bc89SDoug Rabson /* 31727a0bc89SDoug Rabson * Fill the dos filename string with blanks. These are DOS's pad 31827a0bc89SDoug Rabson * characters. 31927a0bc89SDoug Rabson */ 320952a6212SJordan K. Hubbard for (i = 0; i < 11; i++) 32127a0bc89SDoug Rabson dn[i] = ' '; 322952a6212SJordan K. Hubbard dn[11] = 0; 32327a0bc89SDoug Rabson 32427a0bc89SDoug Rabson /* 32527a0bc89SDoug Rabson * The filenames "." and ".." are handled specially, since they 32627a0bc89SDoug Rabson * don't follow dos filename rules. 32727a0bc89SDoug Rabson */ 32827a0bc89SDoug Rabson if (un[0] == '.' && unlen == 1) { 32927a0bc89SDoug Rabson dn[0] = '.'; 330952a6212SJordan K. Hubbard return gen <= 1; 33127a0bc89SDoug Rabson } 33227a0bc89SDoug Rabson if (un[0] == '.' && un[1] == '.' && unlen == 2) { 33327a0bc89SDoug Rabson dn[0] = '.'; 33427a0bc89SDoug Rabson dn[1] = '.'; 335952a6212SJordan K. Hubbard return gen <= 1; 33627a0bc89SDoug Rabson } 33727a0bc89SDoug Rabson 33827a0bc89SDoug Rabson /* 339952a6212SJordan K. Hubbard * Filenames with only blanks and dots are not allowed! 34027a0bc89SDoug Rabson */ 341952a6212SJordan K. Hubbard for (cp = un, i = unlen; --i >= 0; cp++) 342952a6212SJordan K. Hubbard if (*cp != ' ' && *cp != '.') 343952a6212SJordan K. Hubbard break; 344952a6212SJordan K. Hubbard if (i < 0) 345952a6212SJordan K. Hubbard return 0; 346952a6212SJordan K. Hubbard 347cce0eefdSMike Smith 348cce0eefdSMike Smith /* 349cce0eefdSMike Smith * Filenames with some characters are not allowed! 350cce0eefdSMike Smith */ 351c4f02a89SMax Khon for (cp = un, i = unlen; i > 0;) 352c4f02a89SMax Khon if (unix2doschr(&cp, (size_t *)&i, pmp) == 0) 353cce0eefdSMike Smith return 0; 354cce0eefdSMike Smith 355952a6212SJordan K. Hubbard /* 356952a6212SJordan K. Hubbard * Now find the extension 357952a6212SJordan K. Hubbard * Note: dot as first char doesn't start extension 358952a6212SJordan K. Hubbard * and trailing dots and blanks are ignored 359c4f02a89SMax Khon * Note(2003/7): It seems recent Windows has 360c4f02a89SMax Khon * defferent rule than this code, that Windows 361c4f02a89SMax Khon * ignores all dots before extension, and use all 362c4f02a89SMax Khon * chars as filename except for dots. 363952a6212SJordan K. Hubbard */ 364952a6212SJordan K. Hubbard dp = dp1 = 0; 365952a6212SJordan K. Hubbard for (cp = un + 1, i = unlen - 1; --i >= 0;) { 366952a6212SJordan K. Hubbard switch (*cp++) { 367952a6212SJordan K. Hubbard case '.': 368952a6212SJordan K. Hubbard if (!dp1) 369952a6212SJordan K. Hubbard dp1 = cp; 370952a6212SJordan K. Hubbard break; 371952a6212SJordan K. Hubbard case ' ': 372952a6212SJordan K. Hubbard break; 373952a6212SJordan K. Hubbard default: 374952a6212SJordan K. Hubbard if (dp1) 375952a6212SJordan K. Hubbard dp = dp1; 376952a6212SJordan K. Hubbard dp1 = 0; 377952a6212SJordan K. Hubbard break; 378952a6212SJordan K. Hubbard } 37927a0bc89SDoug Rabson } 38027a0bc89SDoug Rabson 38127a0bc89SDoug Rabson /* 382697ab829SR. Imura * Now convert it (this part is for extension). 383697ab829SR. Imura * As Windows XP do, if it's not ascii char, 384697ab829SR. Imura * this function should return 2 or 3, so that checkng out Unicode name. 38527a0bc89SDoug Rabson */ 386952a6212SJordan K. Hubbard if (dp) { 387952a6212SJordan K. Hubbard if (dp1) 388952a6212SJordan K. Hubbard l = dp1 - dp; 389952a6212SJordan K. Hubbard else 390952a6212SJordan K. Hubbard l = unlen - (dp - un); 391c4f02a89SMax Khon for (cp = dp, i = l, j = 8; i > 0 && j < 11; j++) { 392c4f02a89SMax Khon c = unix2doschr(&cp, (size_t *)&i, pmp); 393c4f02a89SMax Khon if (c & 0xff00) { 394c4f02a89SMax Khon dn[j] = c >> 8; 395c4f02a89SMax Khon if (++j < 11) { 396c4f02a89SMax Khon dn[j] = c; 397697ab829SR. Imura if (conv != 3) 398697ab829SR. Imura conv = 2; 399c4f02a89SMax Khon continue; 400c4f02a89SMax Khon } else { 401c4f02a89SMax Khon conv = 3; 402c4f02a89SMax Khon dn[j-1] = ' '; 403c4f02a89SMax Khon break; 404c4f02a89SMax Khon } 405c4f02a89SMax Khon } else { 406c4f02a89SMax Khon dn[j] = c; 407c4f02a89SMax Khon } 408697ab829SR. Imura if (((dn[j] & 0x80) || *(cp - 1) != dn[j]) && conv != 3) 409952a6212SJordan K. Hubbard conv = 2; 410cce0eefdSMike Smith if (dn[j] == 1) { 411cce0eefdSMike Smith conv = 3; 412cce0eefdSMike Smith dn[j] = '_'; 413cce0eefdSMike Smith } 414cce0eefdSMike Smith if (dn[j] == 2) { 415952a6212SJordan K. Hubbard conv = 3; 416952a6212SJordan K. Hubbard dn[j--] = ' '; 417952a6212SJordan K. Hubbard } 418952a6212SJordan K. Hubbard } 419c4f02a89SMax Khon if (i > 0) 420952a6212SJordan K. Hubbard conv = 3; 421952a6212SJordan K. Hubbard dp--; 422952a6212SJordan K. Hubbard } else { 423952a6212SJordan K. Hubbard for (dp = cp; *--dp == ' ' || *dp == '.';); 424952a6212SJordan K. Hubbard dp++; 425952a6212SJordan K. Hubbard } 426952a6212SJordan K. Hubbard 427952a6212SJordan K. Hubbard /* 428952a6212SJordan K. Hubbard * Now convert the rest of the name 429952a6212SJordan K. Hubbard */ 430c4f02a89SMax Khon for (i = dp - un, j = 0; un < dp && j < 8; j++) { 431bddcdc51STim J. Robbins c = unix2doschr(&un, &i, pmp); 432c4f02a89SMax Khon if (c & 0xff00) { 433c4f02a89SMax Khon dn[j] = c >> 8; 434c4f02a89SMax Khon if (++j < 8) { 435c4f02a89SMax Khon dn[j] = c; 436697ab829SR. Imura if (conv != 3) 437697ab829SR. Imura conv = 2; 438c4f02a89SMax Khon continue; 439c4f02a89SMax Khon } else { 440c4f02a89SMax Khon conv = 3; 441c4f02a89SMax Khon dn[j-1] = ' '; 442c4f02a89SMax Khon break; 443c4f02a89SMax Khon } 444c4f02a89SMax Khon } else { 445c4f02a89SMax Khon dn[j] = c; 446c4f02a89SMax Khon } 447697ab829SR. Imura if (((dn[j] & 0x80) || *(un - 1) != dn[j]) && conv != 3) 448952a6212SJordan K. Hubbard conv = 2; 449cce0eefdSMike Smith if (dn[j] == 1) { 450cce0eefdSMike Smith conv = 3; 451cce0eefdSMike Smith dn[j] = '_'; 452cce0eefdSMike Smith } 453cce0eefdSMike Smith if (dn[j] == 2) { 454952a6212SJordan K. Hubbard conv = 3; 455952a6212SJordan K. Hubbard dn[j--] = ' '; 456952a6212SJordan K. Hubbard } 457952a6212SJordan K. Hubbard } 458952a6212SJordan K. Hubbard if (un < dp) 459952a6212SJordan K. Hubbard conv = 3; 460952a6212SJordan K. Hubbard /* 461952a6212SJordan K. Hubbard * If we didn't have any chars in filename, 462952a6212SJordan K. Hubbard * generate a default 463952a6212SJordan K. Hubbard */ 464952a6212SJordan K. Hubbard if (!j) 465952a6212SJordan K. Hubbard dn[0] = '_'; 466952a6212SJordan K. Hubbard 467952a6212SJordan K. Hubbard /* 468952a6212SJordan K. Hubbard * If there wasn't any char dropped, 469952a6212SJordan K. Hubbard * there is no place for generation numbers 47027a0bc89SDoug Rabson */ 471952a6212SJordan K. Hubbard if (conv != 3) { 472952a6212SJordan K. Hubbard if (gen > 1) 473c4f02a89SMax Khon conv = 0; 474c4f02a89SMax Khon goto done; 475952a6212SJordan K. Hubbard } 476952a6212SJordan K. Hubbard 477952a6212SJordan K. Hubbard /* 478952a6212SJordan K. Hubbard * Now insert the generation number into the filename part 479952a6212SJordan K. Hubbard */ 480011cdb57SDmitrij Tejblum if (gen == 0) 481c4f02a89SMax Khon goto done; 482952a6212SJordan K. Hubbard for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10) 483952a6212SJordan K. Hubbard *--wcp = gen % 10 + '0'; 484c4f02a89SMax Khon if (gen) { 485c4f02a89SMax Khon conv = 0; 486c4f02a89SMax Khon goto done; 487c4f02a89SMax Khon } 488952a6212SJordan K. Hubbard for (i = 8; dn[--i] == ' ';); 489952a6212SJordan K. Hubbard i++; 490952a6212SJordan K. Hubbard if (gentext + sizeof(gentext) - wcp + 1 > 8 - i) 491952a6212SJordan K. Hubbard i = 8 - (gentext + sizeof(gentext) - wcp + 1); 492c4f02a89SMax Khon /* 493c4f02a89SMax Khon * Correct posision to where insert the generation number 494c4f02a89SMax Khon */ 495c4f02a89SMax Khon cp = dn; 496c4f02a89SMax Khon i -= mbsadjpos((const char**)&cp, i, unlen, 1, pmp->pm_flags, pmp->pm_d2u); 497c4f02a89SMax Khon 498952a6212SJordan K. Hubbard dn[i++] = '~'; 499952a6212SJordan K. Hubbard while (wcp < gentext + sizeof(gentext)) 500952a6212SJordan K. Hubbard dn[i++] = *wcp++; 501c4f02a89SMax Khon 502c4f02a89SMax Khon /* 503c4f02a89SMax Khon * Tail of the filename should be space 504c4f02a89SMax Khon */ 505c4f02a89SMax Khon while (i < 8) 506c4f02a89SMax Khon dn[i++] = ' '; 507c4f02a89SMax Khon conv = 3; 508c4f02a89SMax Khon 509c4f02a89SMax Khon done: 510c4f02a89SMax Khon /* 511c4f02a89SMax Khon * The first character cannot be E5, 512c4f02a89SMax Khon * because that means a deleted entry 513c4f02a89SMax Khon */ 514c4f02a89SMax Khon if (dn[0] == 0xe5) 515c4f02a89SMax Khon dn[0] = SLOT_E5; 516c4f02a89SMax Khon 517c4f02a89SMax Khon return conv; 518952a6212SJordan K. Hubbard } 519952a6212SJordan K. Hubbard 520952a6212SJordan K. Hubbard /* 521952a6212SJordan K. Hubbard * Create a Win95 long name directory entry 522952a6212SJordan K. Hubbard * Note: assumes that the filename is valid, 523952a6212SJordan K. Hubbard * i.e. doesn't consist solely of blanks and dots 524952a6212SJordan K. Hubbard */ 525952a6212SJordan K. Hubbard int 526c4f02a89SMax Khon unix2winfn(un, unlen, wep, cnt, chksum, pmp) 527952a6212SJordan K. Hubbard const u_char *un; 528bddcdc51STim J. Robbins size_t unlen; 529952a6212SJordan K. Hubbard struct winentry *wep; 530952a6212SJordan K. Hubbard int cnt; 531952a6212SJordan K. Hubbard int chksum; 532c4f02a89SMax Khon struct msdosfsmount *pmp; 533952a6212SJordan K. Hubbard { 534952a6212SJordan K. Hubbard u_int8_t *wcp; 535c4f02a89SMax Khon int i, end; 53613df76f2SAndrey A. Chernov u_int16_t code; 537952a6212SJordan K. Hubbard 538952a6212SJordan K. Hubbard /* 539952a6212SJordan K. Hubbard * Drop trailing blanks and dots 540952a6212SJordan K. Hubbard */ 541c4f02a89SMax Khon unlen = winLenFixup(un, unlen); 542952a6212SJordan K. Hubbard 543c4f02a89SMax Khon /* 544c4f02a89SMax Khon * Cut *un for this slot 545c4f02a89SMax Khon */ 546c4f02a89SMax Khon unlen = mbsadjpos((const char **)&un, unlen, (cnt - 1) * WIN_CHARS, 2, 547c4f02a89SMax Khon pmp->pm_flags, pmp->pm_u2w); 548952a6212SJordan K. Hubbard 549952a6212SJordan K. Hubbard /* 550952a6212SJordan K. Hubbard * Initialize winentry to some useful default 551952a6212SJordan K. Hubbard */ 552952a6212SJordan K. Hubbard for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff); 553952a6212SJordan K. Hubbard wep->weCnt = cnt; 554952a6212SJordan K. Hubbard wep->weAttributes = ATTR_WIN95; 555952a6212SJordan K. Hubbard wep->weReserved1 = 0; 556952a6212SJordan K. Hubbard wep->weChksum = chksum; 557952a6212SJordan K. Hubbard wep->weReserved2 = 0; 558952a6212SJordan K. Hubbard 559952a6212SJordan K. Hubbard /* 560952a6212SJordan K. Hubbard * Now convert the filename parts 561952a6212SJordan K. Hubbard */ 562c4f02a89SMax Khon end = 0; 563c4f02a89SMax Khon for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0 && !end;) { 564bddcdc51STim J. Robbins code = unix2winchr(&un, &unlen, 0, pmp); 56513df76f2SAndrey A. Chernov *wcp++ = code; 56613df76f2SAndrey A. Chernov *wcp++ = code >> 8; 567c4f02a89SMax Khon if (!code) 568c4f02a89SMax Khon end = WIN_LAST; 569952a6212SJordan K. Hubbard } 570c4f02a89SMax Khon for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0 && !end;) { 571bddcdc51STim J. Robbins code = unix2winchr(&un, &unlen, 0, pmp); 57213df76f2SAndrey A. Chernov *wcp++ = code; 57313df76f2SAndrey A. Chernov *wcp++ = code >> 8; 574c4f02a89SMax Khon if (!code) 575c4f02a89SMax Khon end = WIN_LAST; 576952a6212SJordan K. Hubbard } 577c4f02a89SMax Khon for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0 && !end;) { 578bddcdc51STim J. Robbins code = unix2winchr(&un, &unlen, 0, pmp); 57913df76f2SAndrey A. Chernov *wcp++ = code; 58013df76f2SAndrey A. Chernov *wcp++ = code >> 8; 581c4f02a89SMax Khon if (!code) 582c4f02a89SMax Khon end = WIN_LAST; 583952a6212SJordan K. Hubbard } 584c4f02a89SMax Khon if (*un == '\0') 585c4f02a89SMax Khon end = WIN_LAST; 586c4f02a89SMax Khon wep->weCnt |= end; 587c4f02a89SMax Khon return !end; 5884d63452fSAndrey A. Chernov } 5894d63452fSAndrey A. Chernov 590952a6212SJordan K. Hubbard /* 591952a6212SJordan K. Hubbard * Compare our filename to the one in the Win95 entry 592952a6212SJordan K. Hubbard * Returns the checksum or -1 if no match 593952a6212SJordan K. Hubbard */ 594952a6212SJordan K. Hubbard int 595c2819440SBruce Evans winChkName(nbp, un, unlen, chksum, pmp) 596c2819440SBruce Evans struct mbnambuf *nbp; 597952a6212SJordan K. Hubbard const u_char *un; 598bddcdc51STim J. Robbins size_t unlen; 599952a6212SJordan K. Hubbard int chksum; 600c4f02a89SMax Khon struct msdosfsmount *pmp; 601952a6212SJordan K. Hubbard { 602bddcdc51STim J. Robbins size_t len; 603c4f02a89SMax Khon u_int16_t c1, c2; 604c4f02a89SMax Khon u_char *np; 605c4f02a89SMax Khon struct dirent dirbuf; 606952a6212SJordan K. Hubbard 607952a6212SJordan K. Hubbard /* 608c2819440SBruce Evans * We already have winentry in *nbp. 609952a6212SJordan K. Hubbard */ 610c2819440SBruce Evans if (!mbnambuf_flush(nbp, &dirbuf) || dirbuf.d_namlen == 0) 611952a6212SJordan K. Hubbard return -1; 612952a6212SJordan K. Hubbard 613c4f02a89SMax Khon #ifdef MSDOSFS_DEBUG 614c4f02a89SMax Khon printf("winChkName(): un=%s:%d,d_name=%s:%d\n", un, unlen, 615c4f02a89SMax Khon dirbuf.d_name, 616c4f02a89SMax Khon dirbuf.d_namlen); 617c4f02a89SMax Khon #endif 618952a6212SJordan K. Hubbard 619952a6212SJordan K. Hubbard /* 620952a6212SJordan K. Hubbard * Compare the name parts 621952a6212SJordan K. Hubbard */ 622c4f02a89SMax Khon len = dirbuf.d_namlen; 623c4f02a89SMax Khon if (unlen != len) 624c4f02a89SMax Khon return -2; 625c4f02a89SMax Khon 626c4f02a89SMax Khon for (np = dirbuf.d_name; unlen > 0 && len > 0;) { 627c4f02a89SMax Khon /* 6280f4e4130SMax Khon * Comparison must be case insensitive, because FAT disallows 6290f4e4130SMax Khon * to look up or create files in case sensitive even when 6300f4e4130SMax Khon * it's a long file name. 631c4f02a89SMax Khon */ 632bddcdc51STim J. Robbins c1 = unix2winchr((const u_char **)&np, &len, LCASE_BASE, pmp); 633bddcdc51STim J. Robbins c2 = unix2winchr(&un, &unlen, LCASE_BASE, pmp); 634b998efa9SAndrey A. Chernov if (c1 != c2) 635c4f02a89SMax Khon return -2; 636952a6212SJordan K. Hubbard } 637952a6212SJordan K. Hubbard return chksum; 638952a6212SJordan K. Hubbard } 639952a6212SJordan K. Hubbard 640952a6212SJordan K. Hubbard /* 641952a6212SJordan K. Hubbard * Convert Win95 filename to dirbuf. 642952a6212SJordan K. Hubbard * Returns the checksum or -1 if impossible 643952a6212SJordan K. Hubbard */ 644952a6212SJordan K. Hubbard int 645c2819440SBruce Evans win2unixfn(nbp, wep, chksum, pmp) 646c2819440SBruce Evans struct mbnambuf *nbp; 647952a6212SJordan K. Hubbard struct winentry *wep; 648952a6212SJordan K. Hubbard int chksum; 649c4f02a89SMax Khon struct msdosfsmount *pmp; 650952a6212SJordan K. Hubbard { 651*e2ee19e3SKevin Lo u_char *c, tmpbuf[5]; 652952a6212SJordan K. Hubbard u_int8_t *cp; 65341f1dcccSKevin Lo u_int8_t *np, name[WIN_CHARS * 3 + 1]; 6542b5b6623SAndrey A. Chernov u_int16_t code; 655952a6212SJordan K. Hubbard int i; 656952a6212SJordan K. Hubbard 657952a6212SJordan K. Hubbard if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS) 658952a6212SJordan K. Hubbard || !(wep->weCnt&WIN_CNT)) 659952a6212SJordan K. Hubbard return -1; 660952a6212SJordan K. Hubbard 661952a6212SJordan K. Hubbard /* 662952a6212SJordan K. Hubbard * First compare checksums 663952a6212SJordan K. Hubbard */ 664952a6212SJordan K. Hubbard if (wep->weCnt&WIN_LAST) { 665952a6212SJordan K. Hubbard chksum = wep->weChksum; 666952a6212SJordan K. Hubbard } else if (chksum != wep->weChksum) 667952a6212SJordan K. Hubbard chksum = -1; 668952a6212SJordan K. Hubbard if (chksum == -1) 669952a6212SJordan K. Hubbard return -1; 670952a6212SJordan K. Hubbard 671952a6212SJordan K. Hubbard /* 672952a6212SJordan K. Hubbard * Convert the name parts 673952a6212SJordan K. Hubbard */ 674c4f02a89SMax Khon np = name; 675952a6212SJordan K. Hubbard for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) { 6762b5b6623SAndrey A. Chernov code = (cp[1] << 8) | cp[0]; 6772b5b6623SAndrey A. Chernov switch (code) { 678952a6212SJordan K. Hubbard case 0: 6792b5b6623SAndrey A. Chernov *np = '\0'; 680c2819440SBruce Evans mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1); 681952a6212SJordan K. Hubbard return chksum; 682952a6212SJordan K. Hubbard case '/': 6832b5b6623SAndrey A. Chernov *np = '\0'; 684952a6212SJordan K. Hubbard return -1; 6852b5b6623SAndrey A. Chernov default: 686*e2ee19e3SKevin Lo c = win2unixchr(tmpbuf, code, pmp); 68741f1dcccSKevin Lo while (*c != '\0') 68841f1dcccSKevin Lo *np++ = *c++; 6892b5b6623SAndrey A. Chernov break; 690952a6212SJordan K. Hubbard } 6912b5b6623SAndrey A. Chernov cp += 2; 692952a6212SJordan K. Hubbard } 693952a6212SJordan K. Hubbard for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) { 6942b5b6623SAndrey A. Chernov code = (cp[1] << 8) | cp[0]; 6952b5b6623SAndrey A. Chernov switch (code) { 696952a6212SJordan K. Hubbard case 0: 6972b5b6623SAndrey A. Chernov *np = '\0'; 698c2819440SBruce Evans mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1); 699952a6212SJordan K. Hubbard return chksum; 700952a6212SJordan K. Hubbard case '/': 7012b5b6623SAndrey A. Chernov *np = '\0'; 702952a6212SJordan K. Hubbard return -1; 7032b5b6623SAndrey A. Chernov default: 704*e2ee19e3SKevin Lo c = win2unixchr(tmpbuf, code, pmp); 70541f1dcccSKevin Lo while (*c != '\0') 70641f1dcccSKevin Lo *np++ = *c++; 7072b5b6623SAndrey A. Chernov break; 708952a6212SJordan K. Hubbard } 7092b5b6623SAndrey A. Chernov cp += 2; 710952a6212SJordan K. Hubbard } 711952a6212SJordan K. Hubbard for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) { 7122b5b6623SAndrey A. Chernov code = (cp[1] << 8) | cp[0]; 7132b5b6623SAndrey A. Chernov switch (code) { 714952a6212SJordan K. Hubbard case 0: 7152b5b6623SAndrey A. Chernov *np = '\0'; 716c2819440SBruce Evans mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1); 717952a6212SJordan K. Hubbard return chksum; 718952a6212SJordan K. Hubbard case '/': 7192b5b6623SAndrey A. Chernov *np = '\0'; 720952a6212SJordan K. Hubbard return -1; 7212b5b6623SAndrey A. Chernov default: 722*e2ee19e3SKevin Lo c = win2unixchr(tmpbuf, code, pmp); 72341f1dcccSKevin Lo while (*c != '\0') 72441f1dcccSKevin Lo *np++ = *c++; 7252b5b6623SAndrey A. Chernov break; 726952a6212SJordan K. Hubbard } 7272b5b6623SAndrey A. Chernov cp += 2; 728952a6212SJordan K. Hubbard } 729c4f02a89SMax Khon *np = '\0'; 730c2819440SBruce Evans mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1); 731952a6212SJordan K. Hubbard return chksum; 732952a6212SJordan K. Hubbard } 733952a6212SJordan K. Hubbard 734952a6212SJordan K. Hubbard /* 7352a05fbb9SNate Lawson * Compute the unrolled checksum of a DOS filename for Win95 LFN use. 736952a6212SJordan K. Hubbard */ 737952a6212SJordan K. Hubbard u_int8_t 73848d1bcf8SKonstantin Belousov winChksum(u_int8_t *name) 739952a6212SJordan K. Hubbard { 74048d1bcf8SKonstantin Belousov int i; 741952a6212SJordan K. Hubbard u_int8_t s; 742952a6212SJordan K. Hubbard 74348d1bcf8SKonstantin Belousov for (s = 0, i = 11; --i >= 0; s += *name++) 74448d1bcf8SKonstantin Belousov s = (s << 7)|(s >> 1); 7452a05fbb9SNate Lawson return (s); 746952a6212SJordan K. Hubbard } 747952a6212SJordan K. Hubbard 748952a6212SJordan K. Hubbard /* 749952a6212SJordan K. Hubbard * Determine the number of slots necessary for Win95 names 750952a6212SJordan K. Hubbard */ 751952a6212SJordan K. Hubbard int 752c4f02a89SMax Khon winSlotCnt(un, unlen, pmp) 753952a6212SJordan K. Hubbard const u_char *un; 754bddcdc51STim J. Robbins size_t unlen; 755c4f02a89SMax Khon struct msdosfsmount *pmp; 756952a6212SJordan K. Hubbard { 757c4f02a89SMax Khon size_t wlen; 758c4f02a89SMax Khon char wn[WIN_MAXLEN * 2 + 1], *wnp; 759c4f02a89SMax Khon 76069a36f24SMike Smith unlen = winLenFixup(un, unlen); 761c4f02a89SMax Khon 762c4f02a89SMax Khon if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 763c4f02a89SMax Khon wlen = WIN_MAXLEN * 2; 764c4f02a89SMax Khon wnp = wn; 765bddcdc51STim J. Robbins msdosfs_iconv->conv(pmp->pm_u2w, (const char **)&un, &unlen, &wnp, &wlen); 766c4f02a89SMax Khon if (unlen > 0) 767c4f02a89SMax Khon return 0; 768c4f02a89SMax Khon return howmany(WIN_MAXLEN - wlen/2, WIN_CHARS); 769c4f02a89SMax Khon } 770c4f02a89SMax Khon 771952a6212SJordan K. Hubbard if (unlen > WIN_MAXLEN) 772952a6212SJordan K. Hubbard return 0; 773952a6212SJordan K. Hubbard return howmany(unlen, WIN_CHARS); 77427a0bc89SDoug Rabson } 77569a36f24SMike Smith 77669a36f24SMike Smith /* 77769a36f24SMike Smith * Determine the number of bytes neccesary for Win95 names 77869a36f24SMike Smith */ 779bddcdc51STim J. Robbins size_t 78069a36f24SMike Smith winLenFixup(un, unlen) 78169a36f24SMike Smith const u_char* un; 782bddcdc51STim J. Robbins size_t unlen; 78369a36f24SMike Smith { 78469a36f24SMike Smith for (un += unlen; unlen > 0; unlen--) 78569a36f24SMike Smith if (*--un != ' ' && *un != '.') 78669a36f24SMike Smith break; 78769a36f24SMike Smith return unlen; 78869a36f24SMike Smith } 789c4f02a89SMax Khon 790c4f02a89SMax Khon /* 791c4f02a89SMax Khon * Store an area with multi byte string instr, and reterns left 792c4f02a89SMax Khon * byte of instr and moves pointer forward. The area's size is 793c4f02a89SMax Khon * inlen or outlen. 794c4f02a89SMax Khon */ 795c4f02a89SMax Khon static int 796bddcdc51STim J. Robbins mbsadjpos(const char **instr, size_t inlen, size_t outlen, int weight, int flag, void *handle) 797c4f02a89SMax Khon { 798c4f02a89SMax Khon char *outp, outstr[outlen * weight + 1]; 799c4f02a89SMax Khon 800c4f02a89SMax Khon if (flag & MSDOSFSMNT_KICONV && msdosfs_iconv) { 801c4f02a89SMax Khon outp = outstr; 802c4f02a89SMax Khon outlen *= weight; 803bddcdc51STim J. Robbins msdosfs_iconv->conv(handle, instr, &inlen, &outp, &outlen); 804c4f02a89SMax Khon return (inlen); 805c4f02a89SMax Khon } 806c4f02a89SMax Khon 807c4f02a89SMax Khon (*instr) += min(inlen, outlen); 808c4f02a89SMax Khon return (inlen - min(inlen, outlen)); 809c4f02a89SMax Khon } 810c4f02a89SMax Khon 811c4f02a89SMax Khon /* 812c4f02a89SMax Khon * Convert DOS char to Local char 813c4f02a89SMax Khon */ 81441f1dcccSKevin Lo static u_char * 815*e2ee19e3SKevin Lo dos2unixchr(u_char *outbuf, const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp) 816c4f02a89SMax Khon { 817*e2ee19e3SKevin Lo u_char c, *outp; 818c4f02a89SMax Khon size_t len, olen; 819c4f02a89SMax Khon 820c4f02a89SMax Khon outp = outbuf; 82141f1dcccSKevin Lo if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 82241f1dcccSKevin Lo olen = len = 4; 823c4f02a89SMax Khon 824c4f02a89SMax Khon if (lower & (LCASE_BASE | LCASE_EXT)) 825c4f02a89SMax Khon msdosfs_iconv->convchr_case(pmp->pm_d2u, (const char **)instr, 82641f1dcccSKevin Lo ilen, (char **)&outp, &olen, KICONV_LOWER); 827c4f02a89SMax Khon else 828c4f02a89SMax Khon msdosfs_iconv->convchr(pmp->pm_d2u, (const char **)instr, 82941f1dcccSKevin Lo ilen, (char **)&outp, &olen); 830c4f02a89SMax Khon len -= olen; 831c4f02a89SMax Khon 832c4f02a89SMax Khon /* 833c4f02a89SMax Khon * return '?' if failed to convert 834c4f02a89SMax Khon */ 835c4f02a89SMax Khon if (len == 0) { 836c4f02a89SMax Khon (*ilen)--; 837c4f02a89SMax Khon (*instr)++; 83841f1dcccSKevin Lo *outp++ = '?'; 839c4f02a89SMax Khon } 84041f1dcccSKevin Lo } else { 841c4f02a89SMax Khon (*ilen)--; 842c4f02a89SMax Khon c = *(*instr)++; 843c4f02a89SMax Khon c = dos2unix[c]; 844c4f02a89SMax Khon if (lower & (LCASE_BASE | LCASE_EXT)) 845c4f02a89SMax Khon c = u2l[c]; 84641f1dcccSKevin Lo *outp++ = c; 84741f1dcccSKevin Lo outbuf[1] = '\0'; 84841f1dcccSKevin Lo } 84941f1dcccSKevin Lo 85041f1dcccSKevin Lo *outp = '\0'; 85141f1dcccSKevin Lo outp = outbuf; 85241f1dcccSKevin Lo return (outp); 853c4f02a89SMax Khon } 854c4f02a89SMax Khon 855c4f02a89SMax Khon /* 856c4f02a89SMax Khon * Convert Local char to DOS char 857c4f02a89SMax Khon */ 858c4f02a89SMax Khon static u_int16_t 859c4f02a89SMax Khon unix2doschr(const u_char **instr, size_t *ilen, struct msdosfsmount *pmp) 860c4f02a89SMax Khon { 861c4f02a89SMax Khon u_char c; 862c4f02a89SMax Khon char *up, *outp, unicode[3], outbuf[3]; 863c4f02a89SMax Khon u_int16_t wc; 864697ab829SR. Imura size_t len, ucslen, unixlen, olen; 865c4f02a89SMax Khon 866c4f02a89SMax Khon if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 867c4f02a89SMax Khon /* 868c4f02a89SMax Khon * to hide an invisible character, using a unicode filter 869c4f02a89SMax Khon */ 870697ab829SR. Imura ucslen = 2; 871c4f02a89SMax Khon len = *ilen; 872c4f02a89SMax Khon up = unicode; 873c4f02a89SMax Khon msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr, 874697ab829SR. Imura ilen, &up, &ucslen); 875697ab829SR. Imura unixlen = len - *ilen; 876c4f02a89SMax Khon 877c4f02a89SMax Khon /* 878c4f02a89SMax Khon * cannot be converted 879c4f02a89SMax Khon */ 880697ab829SR. Imura if (unixlen == 0) { 881c4f02a89SMax Khon (*ilen)--; 882c4f02a89SMax Khon (*instr)++; 883c4f02a89SMax Khon return (0); 884c4f02a89SMax Khon } 885c4f02a89SMax Khon 886c4f02a89SMax Khon /* 887c4f02a89SMax Khon * return magic number for ascii char 888c4f02a89SMax Khon */ 889697ab829SR. Imura if (unixlen == 1) { 890c4f02a89SMax Khon c = *(*instr -1); 891c4f02a89SMax Khon if (! (c & 0x80)) { 892c4f02a89SMax Khon c = unix2dos[c]; 893c4f02a89SMax Khon if (c <= 2) 894c4f02a89SMax Khon return (c); 895c4f02a89SMax Khon } 896c4f02a89SMax Khon } 897c4f02a89SMax Khon 898c4f02a89SMax Khon /* 899c4f02a89SMax Khon * now convert using libiconv 900c4f02a89SMax Khon */ 901697ab829SR. Imura *instr -= unixlen; 902697ab829SR. Imura *ilen = len; 903c4f02a89SMax Khon 904c4f02a89SMax Khon olen = len = 2; 905c4f02a89SMax Khon outp = outbuf; 906c4f02a89SMax Khon msdosfs_iconv->convchr_case(pmp->pm_u2d, (const char **)instr, 907c4f02a89SMax Khon ilen, &outp, &olen, KICONV_FROM_UPPER); 908c4f02a89SMax Khon len -= olen; 909697ab829SR. Imura 910697ab829SR. Imura /* 911697ab829SR. Imura * cannot be converted, but has unicode char, should return magic number 912697ab829SR. Imura */ 913697ab829SR. Imura if (len == 0) { 914697ab829SR. Imura (*ilen) -= unixlen; 915697ab829SR. Imura (*instr) += unixlen; 916697ab829SR. Imura return (1); 917697ab829SR. Imura } 918697ab829SR. Imura 919c4f02a89SMax Khon wc = 0; 920c4f02a89SMax Khon while(len--) 921c4f02a89SMax Khon wc |= (*(outp - len - 1) & 0xff) << (len << 3); 922c4f02a89SMax Khon return (wc); 923c4f02a89SMax Khon } 924c4f02a89SMax Khon 925c4f02a89SMax Khon (*ilen)--; 926c4f02a89SMax Khon c = *(*instr)++; 927c4f02a89SMax Khon c = l2u[c]; 928c4f02a89SMax Khon c = unix2dos[c]; 929c4f02a89SMax Khon return ((u_int16_t)c); 930c4f02a89SMax Khon } 931c4f02a89SMax Khon 932c4f02a89SMax Khon /* 933c4f02a89SMax Khon * Convert Windows char to Local char 934c4f02a89SMax Khon */ 93541f1dcccSKevin Lo static u_char * 936*e2ee19e3SKevin Lo win2unixchr(u_char *outbuf, u_int16_t wc, struct msdosfsmount *pmp) 937c4f02a89SMax Khon { 938*e2ee19e3SKevin Lo u_char *inp, *outp, inbuf[3]; 939c4f02a89SMax Khon size_t ilen, olen, len; 940c4f02a89SMax Khon 94141f1dcccSKevin Lo outp = outbuf; 942c4f02a89SMax Khon if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 943c4f02a89SMax Khon inbuf[0] = (u_char)(wc>>8); 944c4f02a89SMax Khon inbuf[1] = (u_char)wc; 945c4f02a89SMax Khon inbuf[2] = '\0'; 946c4f02a89SMax Khon 94741f1dcccSKevin Lo ilen = 2; 94841f1dcccSKevin Lo olen = len = 4; 949c4f02a89SMax Khon inp = inbuf; 950c4f02a89SMax Khon msdosfs_iconv->convchr(pmp->pm_w2u, (const char **)&inp, &ilen, 951c4f02a89SMax Khon (char **)&outp, &olen); 952c4f02a89SMax Khon len -= olen; 953c4f02a89SMax Khon 954c4f02a89SMax Khon /* 955c4f02a89SMax Khon * return '?' if failed to convert 956c4f02a89SMax Khon */ 95741f1dcccSKevin Lo if (len == 0) 95841f1dcccSKevin Lo *outp++ = '?'; 95941f1dcccSKevin Lo } else { 96041f1dcccSKevin Lo *outp++ = (wc & 0xff00) ? '?' : (u_char)(wc & 0xff); 961c4f02a89SMax Khon } 962c4f02a89SMax Khon 96341f1dcccSKevin Lo *outp = '\0'; 96441f1dcccSKevin Lo outp = outbuf; 96541f1dcccSKevin Lo return (outp); 966c4f02a89SMax Khon } 967c4f02a89SMax Khon 968c4f02a89SMax Khon /* 969c4f02a89SMax Khon * Convert Local char to Windows char 970c4f02a89SMax Khon */ 971c4f02a89SMax Khon static u_int16_t 972c4f02a89SMax Khon unix2winchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp) 973c4f02a89SMax Khon { 974c4f02a89SMax Khon u_char *outp, outbuf[3]; 975c4f02a89SMax Khon u_int16_t wc; 976c4f02a89SMax Khon size_t olen; 977c4f02a89SMax Khon 978c4f02a89SMax Khon if (*ilen == 0) 979c4f02a89SMax Khon return (0); 980c4f02a89SMax Khon 981c4f02a89SMax Khon if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 982c4f02a89SMax Khon outp = outbuf; 983c4f02a89SMax Khon olen = 2; 984c4f02a89SMax Khon if (lower & (LCASE_BASE | LCASE_EXT)) 985c4f02a89SMax Khon msdosfs_iconv->convchr_case(pmp->pm_u2w, (const char **)instr, 986c4f02a89SMax Khon ilen, (char **)&outp, &olen, 987c4f02a89SMax Khon KICONV_FROM_LOWER); 988c4f02a89SMax Khon else 989c4f02a89SMax Khon msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr, 990c4f02a89SMax Khon ilen, (char **)&outp, &olen); 991c4f02a89SMax Khon 992c4f02a89SMax Khon /* 993c4f02a89SMax Khon * return '0' if end of filename 994c4f02a89SMax Khon */ 995c4f02a89SMax Khon if (olen == 2) 996c4f02a89SMax Khon return (0); 997c4f02a89SMax Khon 998c4f02a89SMax Khon wc = (outbuf[0]<<8) | outbuf[1]; 999c4f02a89SMax Khon 1000c4f02a89SMax Khon return (wc); 1001c4f02a89SMax Khon } 1002c4f02a89SMax Khon 1003c4f02a89SMax Khon (*ilen)--; 1004c4f02a89SMax Khon wc = (*instr)[0]; 1005c4f02a89SMax Khon if (lower & (LCASE_BASE | LCASE_EXT)) 1006c4f02a89SMax Khon wc = u2l[wc]; 1007c4f02a89SMax Khon (*instr)++; 1008c4f02a89SMax Khon return (wc); 1009c4f02a89SMax Khon } 1010c4f02a89SMax Khon 1011c4f02a89SMax Khon /* 1012c2819440SBruce Evans * Initialize the temporary concatenation buffer. 1013c4f02a89SMax Khon */ 1014c4f02a89SMax Khon void 1015c2819440SBruce Evans mbnambuf_init(struct mbnambuf *nbp) 1016c4f02a89SMax Khon { 1017c4f02a89SMax Khon 1018c2819440SBruce Evans nbp->nb_len = 0; 1019c2819440SBruce Evans nbp->nb_last_id = -1; 1020c2819440SBruce Evans nbp->nb_buf[sizeof(nbp->nb_buf) - 1] = '\0'; 1021c4f02a89SMax Khon } 1022c4f02a89SMax Khon 1023c4f02a89SMax Khon /* 102458ad326bSNate Lawson * Fill out our concatenation buffer with the given substring, at the offset 102558ad326bSNate Lawson * specified by its id. Since this function must be called with ids in 102658ad326bSNate Lawson * descending order, we take advantage of the fact that ASCII substrings are 102758ad326bSNate Lawson * exactly WIN_CHARS in length. For non-ASCII substrings, we shift all 102858ad326bSNate Lawson * previous (i.e. higher id) substrings upwards to make room for this one. 102958ad326bSNate Lawson * This only penalizes portions of substrings that contain more than 103058ad326bSNate Lawson * WIN_CHARS bytes when they are first encountered. 1031c4f02a89SMax Khon */ 1032c4f02a89SMax Khon void 1033c2819440SBruce Evans mbnambuf_write(struct mbnambuf *nbp, char *name, int id) 1034c4f02a89SMax Khon { 103558ad326bSNate Lawson char *slot; 1036c2819440SBruce Evans size_t count, newlen; 10374cdb1483SNate Lawson 1038abb0cbf9SEdward Tomasz Napierala if (nbp->nb_len != 0 && id != nbp->nb_last_id - 1) { 103954cf9198SKonstantin Belousov #ifdef MSDOSFS_DEBUG 1040abb0cbf9SEdward Tomasz Napierala printf("msdosfs: non-decreasing id: id %d, last id %d\n", 1041abb0cbf9SEdward Tomasz Napierala id, nbp->nb_last_id); 104254cf9198SKonstantin Belousov #endif 1043abb0cbf9SEdward Tomasz Napierala return; 1044abb0cbf9SEdward Tomasz Napierala } 104558ad326bSNate Lawson 1046c2819440SBruce Evans /* Will store this substring in a WIN_CHARS-aligned slot. */ 1047c2819440SBruce Evans slot = &nbp->nb_buf[id * WIN_CHARS]; 10484cdb1483SNate Lawson count = strlen(name); 1049c2819440SBruce Evans newlen = nbp->nb_len + count; 1050c2819440SBruce Evans if (newlen > WIN_MAXLEN || newlen > MAXNAMLEN) { 105154cf9198SKonstantin Belousov #ifdef MSDOSFS_DEBUG 1052c2819440SBruce Evans printf("msdosfs: file name length %zu too large\n", newlen); 105354cf9198SKonstantin Belousov #endif 10544cdb1483SNate Lawson return; 1055c4f02a89SMax Khon } 105658ad326bSNate Lawson 105758ad326bSNate Lawson /* Shift suffix upwards by the amount length exceeds WIN_CHARS. */ 1058c2819440SBruce Evans if (count > WIN_CHARS && nbp->nb_len != 0) 1059c2819440SBruce Evans bcopy(slot + WIN_CHARS, slot + count, nbp->nb_len); 106058ad326bSNate Lawson 106158ad326bSNate Lawson /* Copy in the substring to its slot and update length so far. */ 106258ad326bSNate Lawson bcopy(name, slot, count); 1063c2819440SBruce Evans nbp->nb_len = newlen; 1064c2819440SBruce Evans nbp->nb_last_id = id; 1065c4f02a89SMax Khon } 1066c4f02a89SMax Khon 1067c4f02a89SMax Khon /* 10684cdb1483SNate Lawson * Take the completed string and use it to setup the struct dirent. 10694cdb1483SNate Lawson * Be sure to always nul-terminate the d_name and then copy the string 10704cdb1483SNate Lawson * from our buffer. Note that this function assumes the full string has 10714cdb1483SNate Lawson * been reassembled in the buffer. If it's called before all substrings 10724cdb1483SNate Lawson * have been written via mbnambuf_write(), the result will be incorrect. 1073c4f02a89SMax Khon */ 1074c4f02a89SMax Khon char * 1075c2819440SBruce Evans mbnambuf_flush(struct mbnambuf *nbp, struct dirent *dp) 1076c4f02a89SMax Khon { 1077c4f02a89SMax Khon 1078c2819440SBruce Evans if (nbp->nb_len > sizeof(dp->d_name) - 1) { 1079c2819440SBruce Evans mbnambuf_init(nbp); 1080c4f02a89SMax Khon return (NULL); 1081c4f02a89SMax Khon } 1082c2819440SBruce Evans bcopy(&nbp->nb_buf[0], dp->d_name, nbp->nb_len); 1083c2819440SBruce Evans dp->d_name[nbp->nb_len] = '\0'; 1084c2819440SBruce Evans dp->d_namlen = nbp->nb_len; 10854cdb1483SNate Lawson 1086c2819440SBruce Evans mbnambuf_init(nbp); 1087c4f02a89SMax Khon return (dp->d_name); 1088c4f02a89SMax Khon } 1089