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); 64e2ee19e3SKevin 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 *); 66e2ee19e3SKevin 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 237*10c9700fSEd Maste dos2unixfn(u_char dn[11], u_char *un, int lower, struct msdosfsmount *pmp) 23827a0bc89SDoug Rabson { 239bddcdc51STim J. Robbins size_t i; 240c4f02a89SMax Khon int thislong = 0; 241e2ee19e3SKevin Lo u_char *c, tmpbuf[5]; 24227a0bc89SDoug Rabson 24327a0bc89SDoug Rabson /* 24427a0bc89SDoug Rabson * If first char of the filename is SLOT_E5 (0x05), then the real 24527a0bc89SDoug Rabson * first char of the filename should be 0xe5. But, they couldn't 24627a0bc89SDoug Rabson * just have a 0xe5 mean 0xe5 because that is used to mean a freed 24727a0bc89SDoug Rabson * directory slot. Another dos quirk. 24827a0bc89SDoug Rabson */ 249952a6212SJordan K. Hubbard if (*dn == SLOT_E5) 250c4f02a89SMax Khon *dn = 0xe5; 25127a0bc89SDoug Rabson 252952a6212SJordan K. Hubbard /* 253952a6212SJordan K. Hubbard * Copy the name portion into the unix filename string. 254952a6212SJordan K. Hubbard */ 255c4f02a89SMax Khon for (i = 8; i > 0 && *dn != ' ';) { 256e2ee19e3SKevin Lo c = dos2unixchr(tmpbuf, (const u_char **)&dn, &i, 257e2ee19e3SKevin Lo lower & LCASE_BASE, pmp); 25841f1dcccSKevin Lo while (*c != '\0') { 25941f1dcccSKevin Lo *un++ = *c++; 260952a6212SJordan K. Hubbard thislong++; 261952a6212SJordan K. Hubbard } 262c4f02a89SMax Khon } 263c4f02a89SMax Khon dn += i; 264952a6212SJordan K. Hubbard 265952a6212SJordan K. Hubbard /* 266952a6212SJordan K. Hubbard * Now, if there is an extension then put in a period and copy in 267952a6212SJordan K. Hubbard * the extension. 268952a6212SJordan K. Hubbard */ 269952a6212SJordan K. Hubbard if (*dn != ' ') { 270952a6212SJordan K. Hubbard *un++ = '.'; 271952a6212SJordan K. Hubbard thislong++; 272c4f02a89SMax Khon for (i = 3; i > 0 && *dn != ' ';) { 273e2ee19e3SKevin Lo c = dos2unixchr(tmpbuf, (const u_char **)&dn, &i, 274cc005bb9SMaxim Konovalov lower & LCASE_EXT, pmp); 27541f1dcccSKevin Lo while (*c != '\0') { 27641f1dcccSKevin Lo *un++ = *c++; 277c4f02a89SMax Khon thislong++; 278c4f02a89SMax Khon } 279952a6212SJordan K. Hubbard } 280952a6212SJordan K. Hubbard } 281952a6212SJordan K. Hubbard *un++ = 0; 282952a6212SJordan K. Hubbard 283952a6212SJordan K. Hubbard return (thislong); 28427a0bc89SDoug Rabson } 28527a0bc89SDoug Rabson 28627a0bc89SDoug Rabson /* 287952a6212SJordan K. Hubbard * Convert a unix filename to a DOS filename according to Win95 rules. 288952a6212SJordan K. Hubbard * If applicable and gen is not 0, it is inserted into the converted 289952a6212SJordan K. Hubbard * filename as a generation number. 290952a6212SJordan K. Hubbard * Returns 291952a6212SJordan K. Hubbard * 0 if name couldn't be converted 292952a6212SJordan K. Hubbard * 1 if the converted name is the same as the original 293952a6212SJordan K. Hubbard * (no long filename entry necessary for Win95) 294952a6212SJordan K. Hubbard * 2 if conversion was successful 295952a6212SJordan K. Hubbard * 3 if conversion was successful and generation number was inserted 29627a0bc89SDoug Rabson */ 297952a6212SJordan K. Hubbard int 298c4f02a89SMax Khon unix2dosfn(un, dn, unlen, gen, pmp) 299952a6212SJordan K. Hubbard const u_char *un; 300952a6212SJordan K. Hubbard u_char dn[12]; 301bddcdc51STim J. Robbins size_t unlen; 302952a6212SJordan K. Hubbard u_int gen; 303c4f02a89SMax Khon struct msdosfsmount *pmp; 30427a0bc89SDoug Rabson { 305bddcdc51STim J. Robbins ssize_t i, j; 306bddcdc51STim J. Robbins int l; 307952a6212SJordan K. Hubbard int conv = 1; 308952a6212SJordan K. Hubbard const u_char *cp, *dp, *dp1; 309952a6212SJordan K. Hubbard u_char gentext[6], *wcp; 310c4f02a89SMax Khon u_int16_t c; 31127a0bc89SDoug Rabson 31227a0bc89SDoug Rabson /* 31327a0bc89SDoug Rabson * Fill the dos filename string with blanks. These are DOS's pad 31427a0bc89SDoug Rabson * characters. 31527a0bc89SDoug Rabson */ 316952a6212SJordan K. Hubbard for (i = 0; i < 11; i++) 31727a0bc89SDoug Rabson dn[i] = ' '; 318952a6212SJordan K. Hubbard dn[11] = 0; 31927a0bc89SDoug Rabson 32027a0bc89SDoug Rabson /* 32127a0bc89SDoug Rabson * The filenames "." and ".." are handled specially, since they 32227a0bc89SDoug Rabson * don't follow dos filename rules. 32327a0bc89SDoug Rabson */ 32427a0bc89SDoug Rabson if (un[0] == '.' && unlen == 1) { 32527a0bc89SDoug Rabson dn[0] = '.'; 326952a6212SJordan K. Hubbard return gen <= 1; 32727a0bc89SDoug Rabson } 32827a0bc89SDoug Rabson if (un[0] == '.' && un[1] == '.' && unlen == 2) { 32927a0bc89SDoug Rabson dn[0] = '.'; 33027a0bc89SDoug Rabson dn[1] = '.'; 331952a6212SJordan K. Hubbard return gen <= 1; 33227a0bc89SDoug Rabson } 33327a0bc89SDoug Rabson 33427a0bc89SDoug Rabson /* 335952a6212SJordan K. Hubbard * Filenames with only blanks and dots are not allowed! 33627a0bc89SDoug Rabson */ 337952a6212SJordan K. Hubbard for (cp = un, i = unlen; --i >= 0; cp++) 338952a6212SJordan K. Hubbard if (*cp != ' ' && *cp != '.') 339952a6212SJordan K. Hubbard break; 340952a6212SJordan K. Hubbard if (i < 0) 341952a6212SJordan K. Hubbard return 0; 342952a6212SJordan K. Hubbard 343cce0eefdSMike Smith 344cce0eefdSMike Smith /* 345cce0eefdSMike Smith * Filenames with some characters are not allowed! 346cce0eefdSMike Smith */ 347c4f02a89SMax Khon for (cp = un, i = unlen; i > 0;) 348c4f02a89SMax Khon if (unix2doschr(&cp, (size_t *)&i, pmp) == 0) 349cce0eefdSMike Smith return 0; 350cce0eefdSMike Smith 351952a6212SJordan K. Hubbard /* 352952a6212SJordan K. Hubbard * Now find the extension 353952a6212SJordan K. Hubbard * Note: dot as first char doesn't start extension 354952a6212SJordan K. Hubbard * and trailing dots and blanks are ignored 355c4f02a89SMax Khon * Note(2003/7): It seems recent Windows has 356c4f02a89SMax Khon * defferent rule than this code, that Windows 357c4f02a89SMax Khon * ignores all dots before extension, and use all 358c4f02a89SMax Khon * chars as filename except for dots. 359952a6212SJordan K. Hubbard */ 360952a6212SJordan K. Hubbard dp = dp1 = 0; 361952a6212SJordan K. Hubbard for (cp = un + 1, i = unlen - 1; --i >= 0;) { 362952a6212SJordan K. Hubbard switch (*cp++) { 363952a6212SJordan K. Hubbard case '.': 364952a6212SJordan K. Hubbard if (!dp1) 365952a6212SJordan K. Hubbard dp1 = cp; 366952a6212SJordan K. Hubbard break; 367952a6212SJordan K. Hubbard case ' ': 368952a6212SJordan K. Hubbard break; 369952a6212SJordan K. Hubbard default: 370952a6212SJordan K. Hubbard if (dp1) 371952a6212SJordan K. Hubbard dp = dp1; 372952a6212SJordan K. Hubbard dp1 = 0; 373952a6212SJordan K. Hubbard break; 374952a6212SJordan K. Hubbard } 37527a0bc89SDoug Rabson } 37627a0bc89SDoug Rabson 37727a0bc89SDoug Rabson /* 378697ab829SR. Imura * Now convert it (this part is for extension). 379697ab829SR. Imura * As Windows XP do, if it's not ascii char, 380697ab829SR. Imura * this function should return 2 or 3, so that checkng out Unicode name. 38127a0bc89SDoug Rabson */ 382952a6212SJordan K. Hubbard if (dp) { 383952a6212SJordan K. Hubbard if (dp1) 384952a6212SJordan K. Hubbard l = dp1 - dp; 385952a6212SJordan K. Hubbard else 386952a6212SJordan K. Hubbard l = unlen - (dp - un); 387c4f02a89SMax Khon for (cp = dp, i = l, j = 8; i > 0 && j < 11; j++) { 388c4f02a89SMax Khon c = unix2doschr(&cp, (size_t *)&i, pmp); 389c4f02a89SMax Khon if (c & 0xff00) { 390c4f02a89SMax Khon dn[j] = c >> 8; 391c4f02a89SMax Khon if (++j < 11) { 392c4f02a89SMax Khon dn[j] = c; 393697ab829SR. Imura if (conv != 3) 394697ab829SR. Imura conv = 2; 395c4f02a89SMax Khon continue; 396c4f02a89SMax Khon } else { 397c4f02a89SMax Khon conv = 3; 398c4f02a89SMax Khon dn[j-1] = ' '; 399c4f02a89SMax Khon break; 400c4f02a89SMax Khon } 401c4f02a89SMax Khon } else { 402c4f02a89SMax Khon dn[j] = c; 403c4f02a89SMax Khon } 404697ab829SR. Imura if (((dn[j] & 0x80) || *(cp - 1) != dn[j]) && conv != 3) 405952a6212SJordan K. Hubbard conv = 2; 406cce0eefdSMike Smith if (dn[j] == 1) { 407cce0eefdSMike Smith conv = 3; 408cce0eefdSMike Smith dn[j] = '_'; 409cce0eefdSMike Smith } 410cce0eefdSMike Smith if (dn[j] == 2) { 411952a6212SJordan K. Hubbard conv = 3; 412952a6212SJordan K. Hubbard dn[j--] = ' '; 413952a6212SJordan K. Hubbard } 414952a6212SJordan K. Hubbard } 415c4f02a89SMax Khon if (i > 0) 416952a6212SJordan K. Hubbard conv = 3; 417952a6212SJordan K. Hubbard dp--; 418952a6212SJordan K. Hubbard } else { 419952a6212SJordan K. Hubbard for (dp = cp; *--dp == ' ' || *dp == '.';); 420952a6212SJordan K. Hubbard dp++; 421952a6212SJordan K. Hubbard } 422952a6212SJordan K. Hubbard 423952a6212SJordan K. Hubbard /* 424952a6212SJordan K. Hubbard * Now convert the rest of the name 425952a6212SJordan K. Hubbard */ 426c4f02a89SMax Khon for (i = dp - un, j = 0; un < dp && j < 8; j++) { 427bddcdc51STim J. Robbins c = unix2doschr(&un, &i, pmp); 428c4f02a89SMax Khon if (c & 0xff00) { 429c4f02a89SMax Khon dn[j] = c >> 8; 430c4f02a89SMax Khon if (++j < 8) { 431c4f02a89SMax Khon dn[j] = c; 432697ab829SR. Imura if (conv != 3) 433697ab829SR. Imura conv = 2; 434c4f02a89SMax Khon continue; 435c4f02a89SMax Khon } else { 436c4f02a89SMax Khon conv = 3; 437c4f02a89SMax Khon dn[j-1] = ' '; 438c4f02a89SMax Khon break; 439c4f02a89SMax Khon } 440c4f02a89SMax Khon } else { 441c4f02a89SMax Khon dn[j] = c; 442c4f02a89SMax Khon } 443697ab829SR. Imura if (((dn[j] & 0x80) || *(un - 1) != dn[j]) && conv != 3) 444952a6212SJordan K. Hubbard conv = 2; 445cce0eefdSMike Smith if (dn[j] == 1) { 446cce0eefdSMike Smith conv = 3; 447cce0eefdSMike Smith dn[j] = '_'; 448cce0eefdSMike Smith } 449cce0eefdSMike Smith if (dn[j] == 2) { 450952a6212SJordan K. Hubbard conv = 3; 451952a6212SJordan K. Hubbard dn[j--] = ' '; 452952a6212SJordan K. Hubbard } 453952a6212SJordan K. Hubbard } 454952a6212SJordan K. Hubbard if (un < dp) 455952a6212SJordan K. Hubbard conv = 3; 456952a6212SJordan K. Hubbard /* 457952a6212SJordan K. Hubbard * If we didn't have any chars in filename, 458952a6212SJordan K. Hubbard * generate a default 459952a6212SJordan K. Hubbard */ 460952a6212SJordan K. Hubbard if (!j) 461952a6212SJordan K. Hubbard dn[0] = '_'; 462952a6212SJordan K. Hubbard 463952a6212SJordan K. Hubbard /* 464952a6212SJordan K. Hubbard * If there wasn't any char dropped, 465952a6212SJordan K. Hubbard * there is no place for generation numbers 46627a0bc89SDoug Rabson */ 467952a6212SJordan K. Hubbard if (conv != 3) { 468952a6212SJordan K. Hubbard if (gen > 1) 469c4f02a89SMax Khon conv = 0; 470c4f02a89SMax Khon goto done; 471952a6212SJordan K. Hubbard } 472952a6212SJordan K. Hubbard 473952a6212SJordan K. Hubbard /* 474952a6212SJordan K. Hubbard * Now insert the generation number into the filename part 475952a6212SJordan K. Hubbard */ 476011cdb57SDmitrij Tejblum if (gen == 0) 477c4f02a89SMax Khon goto done; 478952a6212SJordan K. Hubbard for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10) 479952a6212SJordan K. Hubbard *--wcp = gen % 10 + '0'; 480c4f02a89SMax Khon if (gen) { 481c4f02a89SMax Khon conv = 0; 482c4f02a89SMax Khon goto done; 483c4f02a89SMax Khon } 484952a6212SJordan K. Hubbard for (i = 8; dn[--i] == ' ';); 485952a6212SJordan K. Hubbard i++; 486952a6212SJordan K. Hubbard if (gentext + sizeof(gentext) - wcp + 1 > 8 - i) 487952a6212SJordan K. Hubbard i = 8 - (gentext + sizeof(gentext) - wcp + 1); 488c4f02a89SMax Khon /* 489c4f02a89SMax Khon * Correct posision to where insert the generation number 490c4f02a89SMax Khon */ 491c4f02a89SMax Khon cp = dn; 492c4f02a89SMax Khon i -= mbsadjpos((const char**)&cp, i, unlen, 1, pmp->pm_flags, pmp->pm_d2u); 493c4f02a89SMax Khon 494952a6212SJordan K. Hubbard dn[i++] = '~'; 495952a6212SJordan K. Hubbard while (wcp < gentext + sizeof(gentext)) 496952a6212SJordan K. Hubbard dn[i++] = *wcp++; 497c4f02a89SMax Khon 498c4f02a89SMax Khon /* 499c4f02a89SMax Khon * Tail of the filename should be space 500c4f02a89SMax Khon */ 501c4f02a89SMax Khon while (i < 8) 502c4f02a89SMax Khon dn[i++] = ' '; 503c4f02a89SMax Khon conv = 3; 504c4f02a89SMax Khon 505c4f02a89SMax Khon done: 506c4f02a89SMax Khon /* 507c4f02a89SMax Khon * The first character cannot be E5, 508c4f02a89SMax Khon * because that means a deleted entry 509c4f02a89SMax Khon */ 510c4f02a89SMax Khon if (dn[0] == 0xe5) 511c4f02a89SMax Khon dn[0] = SLOT_E5; 512c4f02a89SMax Khon 513c4f02a89SMax Khon return conv; 514952a6212SJordan K. Hubbard } 515952a6212SJordan K. Hubbard 516952a6212SJordan K. Hubbard /* 517952a6212SJordan K. Hubbard * Create a Win95 long name directory entry 518952a6212SJordan K. Hubbard * Note: assumes that the filename is valid, 519952a6212SJordan K. Hubbard * i.e. doesn't consist solely of blanks and dots 520952a6212SJordan K. Hubbard */ 521952a6212SJordan K. Hubbard int 522c4f02a89SMax Khon unix2winfn(un, unlen, wep, cnt, chksum, pmp) 523952a6212SJordan K. Hubbard const u_char *un; 524bddcdc51STim J. Robbins size_t unlen; 525952a6212SJordan K. Hubbard struct winentry *wep; 526952a6212SJordan K. Hubbard int cnt; 527952a6212SJordan K. Hubbard int chksum; 528c4f02a89SMax Khon struct msdosfsmount *pmp; 529952a6212SJordan K. Hubbard { 530952a6212SJordan K. Hubbard u_int8_t *wcp; 531c4f02a89SMax Khon int i, end; 53213df76f2SAndrey A. Chernov u_int16_t code; 533952a6212SJordan K. Hubbard 534952a6212SJordan K. Hubbard /* 535952a6212SJordan K. Hubbard * Drop trailing blanks and dots 536952a6212SJordan K. Hubbard */ 537c4f02a89SMax Khon unlen = winLenFixup(un, unlen); 538952a6212SJordan K. Hubbard 539c4f02a89SMax Khon /* 540c4f02a89SMax Khon * Cut *un for this slot 541c4f02a89SMax Khon */ 542c4f02a89SMax Khon unlen = mbsadjpos((const char **)&un, unlen, (cnt - 1) * WIN_CHARS, 2, 543c4f02a89SMax Khon pmp->pm_flags, pmp->pm_u2w); 544952a6212SJordan K. Hubbard 545952a6212SJordan K. Hubbard /* 546952a6212SJordan K. Hubbard * Initialize winentry to some useful default 547952a6212SJordan K. Hubbard */ 548952a6212SJordan K. Hubbard for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff); 549952a6212SJordan K. Hubbard wep->weCnt = cnt; 550952a6212SJordan K. Hubbard wep->weAttributes = ATTR_WIN95; 551952a6212SJordan K. Hubbard wep->weReserved1 = 0; 552952a6212SJordan K. Hubbard wep->weChksum = chksum; 553952a6212SJordan K. Hubbard wep->weReserved2 = 0; 554952a6212SJordan K. Hubbard 555952a6212SJordan K. Hubbard /* 556952a6212SJordan K. Hubbard * Now convert the filename parts 557952a6212SJordan K. Hubbard */ 558c4f02a89SMax Khon end = 0; 559c4f02a89SMax Khon for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0 && !end;) { 560bddcdc51STim J. Robbins code = unix2winchr(&un, &unlen, 0, pmp); 56113df76f2SAndrey A. Chernov *wcp++ = code; 56213df76f2SAndrey A. Chernov *wcp++ = code >> 8; 563c4f02a89SMax Khon if (!code) 564c4f02a89SMax Khon end = WIN_LAST; 565952a6212SJordan K. Hubbard } 566c4f02a89SMax Khon for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0 && !end;) { 567bddcdc51STim J. Robbins code = unix2winchr(&un, &unlen, 0, pmp); 56813df76f2SAndrey A. Chernov *wcp++ = code; 56913df76f2SAndrey A. Chernov *wcp++ = code >> 8; 570c4f02a89SMax Khon if (!code) 571c4f02a89SMax Khon end = WIN_LAST; 572952a6212SJordan K. Hubbard } 573c4f02a89SMax Khon for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0 && !end;) { 574bddcdc51STim J. Robbins code = unix2winchr(&un, &unlen, 0, pmp); 57513df76f2SAndrey A. Chernov *wcp++ = code; 57613df76f2SAndrey A. Chernov *wcp++ = code >> 8; 577c4f02a89SMax Khon if (!code) 578c4f02a89SMax Khon end = WIN_LAST; 579952a6212SJordan K. Hubbard } 580c4f02a89SMax Khon if (*un == '\0') 581c4f02a89SMax Khon end = WIN_LAST; 582c4f02a89SMax Khon wep->weCnt |= end; 583c4f02a89SMax Khon return !end; 5844d63452fSAndrey A. Chernov } 5854d63452fSAndrey A. Chernov 586952a6212SJordan K. Hubbard /* 587952a6212SJordan K. Hubbard * Compare our filename to the one in the Win95 entry 588952a6212SJordan K. Hubbard * Returns the checksum or -1 if no match 589952a6212SJordan K. Hubbard */ 590952a6212SJordan K. Hubbard int 591c2819440SBruce Evans winChkName(nbp, un, unlen, chksum, pmp) 592c2819440SBruce Evans struct mbnambuf *nbp; 593952a6212SJordan K. Hubbard const u_char *un; 594bddcdc51STim J. Robbins size_t unlen; 595952a6212SJordan K. Hubbard int chksum; 596c4f02a89SMax Khon struct msdosfsmount *pmp; 597952a6212SJordan K. Hubbard { 598bddcdc51STim J. Robbins size_t len; 599c4f02a89SMax Khon u_int16_t c1, c2; 600c4f02a89SMax Khon u_char *np; 601c4f02a89SMax Khon struct dirent dirbuf; 602952a6212SJordan K. Hubbard 603952a6212SJordan K. Hubbard /* 604c2819440SBruce Evans * We already have winentry in *nbp. 605952a6212SJordan K. Hubbard */ 606c2819440SBruce Evans if (!mbnambuf_flush(nbp, &dirbuf) || dirbuf.d_namlen == 0) 607952a6212SJordan K. Hubbard return -1; 608952a6212SJordan K. Hubbard 609c4f02a89SMax Khon #ifdef MSDOSFS_DEBUG 610c4f02a89SMax Khon printf("winChkName(): un=%s:%d,d_name=%s:%d\n", un, unlen, 611c4f02a89SMax Khon dirbuf.d_name, 612c4f02a89SMax Khon dirbuf.d_namlen); 613c4f02a89SMax Khon #endif 614952a6212SJordan K. Hubbard 615952a6212SJordan K. Hubbard /* 616952a6212SJordan K. Hubbard * Compare the name parts 617952a6212SJordan K. Hubbard */ 618c4f02a89SMax Khon len = dirbuf.d_namlen; 619c4f02a89SMax Khon if (unlen != len) 620c4f02a89SMax Khon return -2; 621c4f02a89SMax Khon 622c4f02a89SMax Khon for (np = dirbuf.d_name; unlen > 0 && len > 0;) { 623c4f02a89SMax Khon /* 6240f4e4130SMax Khon * Comparison must be case insensitive, because FAT disallows 6250f4e4130SMax Khon * to look up or create files in case sensitive even when 6260f4e4130SMax Khon * it's a long file name. 627c4f02a89SMax Khon */ 628bddcdc51STim J. Robbins c1 = unix2winchr((const u_char **)&np, &len, LCASE_BASE, pmp); 629bddcdc51STim J. Robbins c2 = unix2winchr(&un, &unlen, LCASE_BASE, pmp); 630b998efa9SAndrey A. Chernov if (c1 != c2) 631c4f02a89SMax Khon return -2; 632952a6212SJordan K. Hubbard } 633952a6212SJordan K. Hubbard return chksum; 634952a6212SJordan K. Hubbard } 635952a6212SJordan K. Hubbard 636952a6212SJordan K. Hubbard /* 637952a6212SJordan K. Hubbard * Convert Win95 filename to dirbuf. 638952a6212SJordan K. Hubbard * Returns the checksum or -1 if impossible 639952a6212SJordan K. Hubbard */ 640952a6212SJordan K. Hubbard int 641c2819440SBruce Evans win2unixfn(nbp, wep, chksum, pmp) 642c2819440SBruce Evans struct mbnambuf *nbp; 643952a6212SJordan K. Hubbard struct winentry *wep; 644952a6212SJordan K. Hubbard int chksum; 645c4f02a89SMax Khon struct msdosfsmount *pmp; 646952a6212SJordan K. Hubbard { 647e2ee19e3SKevin Lo u_char *c, tmpbuf[5]; 648952a6212SJordan K. Hubbard u_int8_t *cp; 64941f1dcccSKevin Lo u_int8_t *np, name[WIN_CHARS * 3 + 1]; 6502b5b6623SAndrey A. Chernov u_int16_t code; 651952a6212SJordan K. Hubbard int i; 652952a6212SJordan K. Hubbard 653952a6212SJordan K. Hubbard if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS) 654952a6212SJordan K. Hubbard || !(wep->weCnt&WIN_CNT)) 655952a6212SJordan K. Hubbard return -1; 656952a6212SJordan K. Hubbard 657952a6212SJordan K. Hubbard /* 658952a6212SJordan K. Hubbard * First compare checksums 659952a6212SJordan K. Hubbard */ 660952a6212SJordan K. Hubbard if (wep->weCnt&WIN_LAST) { 661952a6212SJordan K. Hubbard chksum = wep->weChksum; 662952a6212SJordan K. Hubbard } else if (chksum != wep->weChksum) 663952a6212SJordan K. Hubbard chksum = -1; 664952a6212SJordan K. Hubbard if (chksum == -1) 665952a6212SJordan K. Hubbard return -1; 666952a6212SJordan K. Hubbard 667952a6212SJordan K. Hubbard /* 668952a6212SJordan K. Hubbard * Convert the name parts 669952a6212SJordan K. Hubbard */ 670c4f02a89SMax Khon np = name; 671952a6212SJordan K. Hubbard for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) { 6722b5b6623SAndrey A. Chernov code = (cp[1] << 8) | cp[0]; 6732b5b6623SAndrey A. Chernov switch (code) { 674952a6212SJordan K. Hubbard case 0: 6752b5b6623SAndrey A. Chernov *np = '\0'; 676c2819440SBruce Evans mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1); 677952a6212SJordan K. Hubbard return chksum; 678952a6212SJordan K. Hubbard case '/': 6792b5b6623SAndrey A. Chernov *np = '\0'; 680952a6212SJordan K. Hubbard return -1; 6812b5b6623SAndrey A. Chernov default: 682e2ee19e3SKevin Lo c = win2unixchr(tmpbuf, code, pmp); 68341f1dcccSKevin Lo while (*c != '\0') 68441f1dcccSKevin Lo *np++ = *c++; 6852b5b6623SAndrey A. Chernov break; 686952a6212SJordan K. Hubbard } 6872b5b6623SAndrey A. Chernov cp += 2; 688952a6212SJordan K. Hubbard } 689952a6212SJordan K. Hubbard for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) { 6902b5b6623SAndrey A. Chernov code = (cp[1] << 8) | cp[0]; 6912b5b6623SAndrey A. Chernov switch (code) { 692952a6212SJordan K. Hubbard case 0: 6932b5b6623SAndrey A. Chernov *np = '\0'; 694c2819440SBruce Evans mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1); 695952a6212SJordan K. Hubbard return chksum; 696952a6212SJordan K. Hubbard case '/': 6972b5b6623SAndrey A. Chernov *np = '\0'; 698952a6212SJordan K. Hubbard return -1; 6992b5b6623SAndrey A. Chernov default: 700e2ee19e3SKevin Lo c = win2unixchr(tmpbuf, code, pmp); 70141f1dcccSKevin Lo while (*c != '\0') 70241f1dcccSKevin Lo *np++ = *c++; 7032b5b6623SAndrey A. Chernov break; 704952a6212SJordan K. Hubbard } 7052b5b6623SAndrey A. Chernov cp += 2; 706952a6212SJordan K. Hubbard } 707952a6212SJordan K. Hubbard for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) { 7082b5b6623SAndrey A. Chernov code = (cp[1] << 8) | cp[0]; 7092b5b6623SAndrey A. Chernov switch (code) { 710952a6212SJordan K. Hubbard case 0: 7112b5b6623SAndrey A. Chernov *np = '\0'; 712c2819440SBruce Evans mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1); 713952a6212SJordan K. Hubbard return chksum; 714952a6212SJordan K. Hubbard case '/': 7152b5b6623SAndrey A. Chernov *np = '\0'; 716952a6212SJordan K. Hubbard return -1; 7172b5b6623SAndrey A. Chernov default: 718e2ee19e3SKevin Lo c = win2unixchr(tmpbuf, code, pmp); 71941f1dcccSKevin Lo while (*c != '\0') 72041f1dcccSKevin Lo *np++ = *c++; 7212b5b6623SAndrey A. Chernov break; 722952a6212SJordan K. Hubbard } 7232b5b6623SAndrey A. Chernov cp += 2; 724952a6212SJordan K. Hubbard } 725c4f02a89SMax Khon *np = '\0'; 726c2819440SBruce Evans mbnambuf_write(nbp, name, (wep->weCnt & WIN_CNT) - 1); 727952a6212SJordan K. Hubbard return chksum; 728952a6212SJordan K. Hubbard } 729952a6212SJordan K. Hubbard 730952a6212SJordan K. Hubbard /* 7312a05fbb9SNate Lawson * Compute the unrolled checksum of a DOS filename for Win95 LFN use. 732952a6212SJordan K. Hubbard */ 733952a6212SJordan K. Hubbard u_int8_t 73448d1bcf8SKonstantin Belousov winChksum(u_int8_t *name) 735952a6212SJordan K. Hubbard { 73648d1bcf8SKonstantin Belousov int i; 737952a6212SJordan K. Hubbard u_int8_t s; 738952a6212SJordan K. Hubbard 73948d1bcf8SKonstantin Belousov for (s = 0, i = 11; --i >= 0; s += *name++) 74048d1bcf8SKonstantin Belousov s = (s << 7)|(s >> 1); 7412a05fbb9SNate Lawson return (s); 742952a6212SJordan K. Hubbard } 743952a6212SJordan K. Hubbard 744952a6212SJordan K. Hubbard /* 745952a6212SJordan K. Hubbard * Determine the number of slots necessary for Win95 names 746952a6212SJordan K. Hubbard */ 747952a6212SJordan K. Hubbard int 748c4f02a89SMax Khon winSlotCnt(un, unlen, pmp) 749952a6212SJordan K. Hubbard const u_char *un; 750bddcdc51STim J. Robbins size_t unlen; 751c4f02a89SMax Khon struct msdosfsmount *pmp; 752952a6212SJordan K. Hubbard { 753c4f02a89SMax Khon size_t wlen; 754c4f02a89SMax Khon char wn[WIN_MAXLEN * 2 + 1], *wnp; 755c4f02a89SMax Khon 75669a36f24SMike Smith unlen = winLenFixup(un, unlen); 757c4f02a89SMax Khon 758c4f02a89SMax Khon if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 759c4f02a89SMax Khon wlen = WIN_MAXLEN * 2; 760c4f02a89SMax Khon wnp = wn; 761bddcdc51STim J. Robbins msdosfs_iconv->conv(pmp->pm_u2w, (const char **)&un, &unlen, &wnp, &wlen); 762c4f02a89SMax Khon if (unlen > 0) 763c4f02a89SMax Khon return 0; 764c4f02a89SMax Khon return howmany(WIN_MAXLEN - wlen/2, WIN_CHARS); 765c4f02a89SMax Khon } 766c4f02a89SMax Khon 767952a6212SJordan K. Hubbard if (unlen > WIN_MAXLEN) 768952a6212SJordan K. Hubbard return 0; 769952a6212SJordan K. Hubbard return howmany(unlen, WIN_CHARS); 77027a0bc89SDoug Rabson } 77169a36f24SMike Smith 77269a36f24SMike Smith /* 773a2098feaSGabor Kovesdan * Determine the number of bytes neccessary for Win95 names 77469a36f24SMike Smith */ 775bddcdc51STim J. Robbins size_t 77669a36f24SMike Smith winLenFixup(un, unlen) 77769a36f24SMike Smith const u_char* un; 778bddcdc51STim J. Robbins size_t unlen; 77969a36f24SMike Smith { 78069a36f24SMike Smith for (un += unlen; unlen > 0; unlen--) 78169a36f24SMike Smith if (*--un != ' ' && *un != '.') 78269a36f24SMike Smith break; 78369a36f24SMike Smith return unlen; 78469a36f24SMike Smith } 785c4f02a89SMax Khon 786c4f02a89SMax Khon /* 787a8b5c2a0SGabor Kovesdan * Store an area with multi byte string instr, and returns left 788c4f02a89SMax Khon * byte of instr and moves pointer forward. The area's size is 789c4f02a89SMax Khon * inlen or outlen. 790c4f02a89SMax Khon */ 791c4f02a89SMax Khon static int 792bddcdc51STim J. Robbins mbsadjpos(const char **instr, size_t inlen, size_t outlen, int weight, int flag, void *handle) 793c4f02a89SMax Khon { 794c4f02a89SMax Khon char *outp, outstr[outlen * weight + 1]; 795c4f02a89SMax Khon 796c4f02a89SMax Khon if (flag & MSDOSFSMNT_KICONV && msdosfs_iconv) { 797c4f02a89SMax Khon outp = outstr; 798c4f02a89SMax Khon outlen *= weight; 799bddcdc51STim J. Robbins msdosfs_iconv->conv(handle, instr, &inlen, &outp, &outlen); 800c4f02a89SMax Khon return (inlen); 801c4f02a89SMax Khon } 802c4f02a89SMax Khon 803c4f02a89SMax Khon (*instr) += min(inlen, outlen); 804c4f02a89SMax Khon return (inlen - min(inlen, outlen)); 805c4f02a89SMax Khon } 806c4f02a89SMax Khon 807c4f02a89SMax Khon /* 808c4f02a89SMax Khon * Convert DOS char to Local char 809c4f02a89SMax Khon */ 81041f1dcccSKevin Lo static u_char * 811e2ee19e3SKevin Lo dos2unixchr(u_char *outbuf, const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp) 812c4f02a89SMax Khon { 813e2ee19e3SKevin Lo u_char c, *outp; 814c4f02a89SMax Khon size_t len, olen; 815c4f02a89SMax Khon 816c4f02a89SMax Khon outp = outbuf; 81741f1dcccSKevin Lo if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 81841f1dcccSKevin Lo olen = len = 4; 819c4f02a89SMax Khon 820c4f02a89SMax Khon if (lower & (LCASE_BASE | LCASE_EXT)) 821c4f02a89SMax Khon msdosfs_iconv->convchr_case(pmp->pm_d2u, (const char **)instr, 82241f1dcccSKevin Lo ilen, (char **)&outp, &olen, KICONV_LOWER); 823c4f02a89SMax Khon else 824c4f02a89SMax Khon msdosfs_iconv->convchr(pmp->pm_d2u, (const char **)instr, 82541f1dcccSKevin Lo ilen, (char **)&outp, &olen); 826c4f02a89SMax Khon len -= olen; 827c4f02a89SMax Khon 828c4f02a89SMax Khon /* 829c4f02a89SMax Khon * return '?' if failed to convert 830c4f02a89SMax Khon */ 831c4f02a89SMax Khon if (len == 0) { 832c4f02a89SMax Khon (*ilen)--; 833c4f02a89SMax Khon (*instr)++; 83441f1dcccSKevin Lo *outp++ = '?'; 835c4f02a89SMax Khon } 83641f1dcccSKevin Lo } else { 837c4f02a89SMax Khon (*ilen)--; 838c4f02a89SMax Khon c = *(*instr)++; 839c4f02a89SMax Khon c = dos2unix[c]; 840c4f02a89SMax Khon if (lower & (LCASE_BASE | LCASE_EXT)) 841c4f02a89SMax Khon c = u2l[c]; 84241f1dcccSKevin Lo *outp++ = c; 84341f1dcccSKevin Lo outbuf[1] = '\0'; 84441f1dcccSKevin Lo } 84541f1dcccSKevin Lo 84641f1dcccSKevin Lo *outp = '\0'; 84741f1dcccSKevin Lo outp = outbuf; 84841f1dcccSKevin Lo return (outp); 849c4f02a89SMax Khon } 850c4f02a89SMax Khon 851c4f02a89SMax Khon /* 852c4f02a89SMax Khon * Convert Local char to DOS char 853c4f02a89SMax Khon */ 854c4f02a89SMax Khon static u_int16_t 855c4f02a89SMax Khon unix2doschr(const u_char **instr, size_t *ilen, struct msdosfsmount *pmp) 856c4f02a89SMax Khon { 857c4f02a89SMax Khon u_char c; 858c4f02a89SMax Khon char *up, *outp, unicode[3], outbuf[3]; 859c4f02a89SMax Khon u_int16_t wc; 860697ab829SR. Imura size_t len, ucslen, unixlen, olen; 861c4f02a89SMax Khon 862c4f02a89SMax Khon if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 863c4f02a89SMax Khon /* 864c4f02a89SMax Khon * to hide an invisible character, using a unicode filter 865c4f02a89SMax Khon */ 866697ab829SR. Imura ucslen = 2; 867c4f02a89SMax Khon len = *ilen; 868c4f02a89SMax Khon up = unicode; 869c4f02a89SMax Khon msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr, 870697ab829SR. Imura ilen, &up, &ucslen); 871697ab829SR. Imura unixlen = len - *ilen; 872c4f02a89SMax Khon 873c4f02a89SMax Khon /* 874c4f02a89SMax Khon * cannot be converted 875c4f02a89SMax Khon */ 876697ab829SR. Imura if (unixlen == 0) { 877c4f02a89SMax Khon (*ilen)--; 878c4f02a89SMax Khon (*instr)++; 879c4f02a89SMax Khon return (0); 880c4f02a89SMax Khon } 881c4f02a89SMax Khon 882c4f02a89SMax Khon /* 883c4f02a89SMax Khon * return magic number for ascii char 884c4f02a89SMax Khon */ 885697ab829SR. Imura if (unixlen == 1) { 886c4f02a89SMax Khon c = *(*instr -1); 887c4f02a89SMax Khon if (! (c & 0x80)) { 888c4f02a89SMax Khon c = unix2dos[c]; 889c4f02a89SMax Khon if (c <= 2) 890c4f02a89SMax Khon return (c); 891c4f02a89SMax Khon } 892c4f02a89SMax Khon } 893c4f02a89SMax Khon 894c4f02a89SMax Khon /* 895c4f02a89SMax Khon * now convert using libiconv 896c4f02a89SMax Khon */ 897697ab829SR. Imura *instr -= unixlen; 898697ab829SR. Imura *ilen = len; 899c4f02a89SMax Khon 900c4f02a89SMax Khon olen = len = 2; 901c4f02a89SMax Khon outp = outbuf; 902c4f02a89SMax Khon msdosfs_iconv->convchr_case(pmp->pm_u2d, (const char **)instr, 903c4f02a89SMax Khon ilen, &outp, &olen, KICONV_FROM_UPPER); 904c4f02a89SMax Khon len -= olen; 905697ab829SR. Imura 906697ab829SR. Imura /* 907697ab829SR. Imura * cannot be converted, but has unicode char, should return magic number 908697ab829SR. Imura */ 909697ab829SR. Imura if (len == 0) { 910697ab829SR. Imura (*ilen) -= unixlen; 911697ab829SR. Imura (*instr) += unixlen; 912697ab829SR. Imura return (1); 913697ab829SR. Imura } 914697ab829SR. Imura 915c4f02a89SMax Khon wc = 0; 916c4f02a89SMax Khon while(len--) 917c4f02a89SMax Khon wc |= (*(outp - len - 1) & 0xff) << (len << 3); 918c4f02a89SMax Khon return (wc); 919c4f02a89SMax Khon } 920c4f02a89SMax Khon 921c4f02a89SMax Khon (*ilen)--; 922c4f02a89SMax Khon c = *(*instr)++; 923c4f02a89SMax Khon c = l2u[c]; 924c4f02a89SMax Khon c = unix2dos[c]; 925c4f02a89SMax Khon return ((u_int16_t)c); 926c4f02a89SMax Khon } 927c4f02a89SMax Khon 928c4f02a89SMax Khon /* 929c4f02a89SMax Khon * Convert Windows char to Local char 930c4f02a89SMax Khon */ 93141f1dcccSKevin Lo static u_char * 932e2ee19e3SKevin Lo win2unixchr(u_char *outbuf, u_int16_t wc, struct msdosfsmount *pmp) 933c4f02a89SMax Khon { 934e2ee19e3SKevin Lo u_char *inp, *outp, inbuf[3]; 935c4f02a89SMax Khon size_t ilen, olen, len; 936c4f02a89SMax Khon 93741f1dcccSKevin Lo outp = outbuf; 938c4f02a89SMax Khon if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 939c4f02a89SMax Khon inbuf[0] = (u_char)(wc>>8); 940c4f02a89SMax Khon inbuf[1] = (u_char)wc; 941c4f02a89SMax Khon inbuf[2] = '\0'; 942c4f02a89SMax Khon 94341f1dcccSKevin Lo ilen = 2; 94441f1dcccSKevin Lo olen = len = 4; 945c4f02a89SMax Khon inp = inbuf; 946c4f02a89SMax Khon msdosfs_iconv->convchr(pmp->pm_w2u, (const char **)&inp, &ilen, 947c4f02a89SMax Khon (char **)&outp, &olen); 948c4f02a89SMax Khon len -= olen; 949c4f02a89SMax Khon 950c4f02a89SMax Khon /* 951c4f02a89SMax Khon * return '?' if failed to convert 952c4f02a89SMax Khon */ 95341f1dcccSKevin Lo if (len == 0) 95441f1dcccSKevin Lo *outp++ = '?'; 95541f1dcccSKevin Lo } else { 95641f1dcccSKevin Lo *outp++ = (wc & 0xff00) ? '?' : (u_char)(wc & 0xff); 957c4f02a89SMax Khon } 958c4f02a89SMax Khon 95941f1dcccSKevin Lo *outp = '\0'; 96041f1dcccSKevin Lo outp = outbuf; 96141f1dcccSKevin Lo return (outp); 962c4f02a89SMax Khon } 963c4f02a89SMax Khon 964c4f02a89SMax Khon /* 965c4f02a89SMax Khon * Convert Local char to Windows char 966c4f02a89SMax Khon */ 967c4f02a89SMax Khon static u_int16_t 968c4f02a89SMax Khon unix2winchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp) 969c4f02a89SMax Khon { 970c4f02a89SMax Khon u_char *outp, outbuf[3]; 971c4f02a89SMax Khon u_int16_t wc; 972c4f02a89SMax Khon size_t olen; 973c4f02a89SMax Khon 974c4f02a89SMax Khon if (*ilen == 0) 975c4f02a89SMax Khon return (0); 976c4f02a89SMax Khon 977c4f02a89SMax Khon if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 978c4f02a89SMax Khon outp = outbuf; 979c4f02a89SMax Khon olen = 2; 980c4f02a89SMax Khon if (lower & (LCASE_BASE | LCASE_EXT)) 981c4f02a89SMax Khon msdosfs_iconv->convchr_case(pmp->pm_u2w, (const char **)instr, 982c4f02a89SMax Khon ilen, (char **)&outp, &olen, 983c4f02a89SMax Khon KICONV_FROM_LOWER); 984c4f02a89SMax Khon else 985c4f02a89SMax Khon msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr, 986c4f02a89SMax Khon ilen, (char **)&outp, &olen); 987c4f02a89SMax Khon 988c4f02a89SMax Khon /* 989c4f02a89SMax Khon * return '0' if end of filename 990c4f02a89SMax Khon */ 991c4f02a89SMax Khon if (olen == 2) 992c4f02a89SMax Khon return (0); 993c4f02a89SMax Khon 994c4f02a89SMax Khon wc = (outbuf[0]<<8) | outbuf[1]; 995c4f02a89SMax Khon 996c4f02a89SMax Khon return (wc); 997c4f02a89SMax Khon } 998c4f02a89SMax Khon 999c4f02a89SMax Khon (*ilen)--; 1000c4f02a89SMax Khon wc = (*instr)[0]; 1001c4f02a89SMax Khon if (lower & (LCASE_BASE | LCASE_EXT)) 1002c4f02a89SMax Khon wc = u2l[wc]; 1003c4f02a89SMax Khon (*instr)++; 1004c4f02a89SMax Khon return (wc); 1005c4f02a89SMax Khon } 1006c4f02a89SMax Khon 1007c4f02a89SMax Khon /* 1008c2819440SBruce Evans * Initialize the temporary concatenation buffer. 1009c4f02a89SMax Khon */ 1010c4f02a89SMax Khon void 1011c2819440SBruce Evans mbnambuf_init(struct mbnambuf *nbp) 1012c4f02a89SMax Khon { 1013c4f02a89SMax Khon 1014c2819440SBruce Evans nbp->nb_len = 0; 1015c2819440SBruce Evans nbp->nb_last_id = -1; 1016c2819440SBruce Evans nbp->nb_buf[sizeof(nbp->nb_buf) - 1] = '\0'; 1017c4f02a89SMax Khon } 1018c4f02a89SMax Khon 1019c4f02a89SMax Khon /* 102058ad326bSNate Lawson * Fill out our concatenation buffer with the given substring, at the offset 102158ad326bSNate Lawson * specified by its id. Since this function must be called with ids in 102258ad326bSNate Lawson * descending order, we take advantage of the fact that ASCII substrings are 102358ad326bSNate Lawson * exactly WIN_CHARS in length. For non-ASCII substrings, we shift all 102458ad326bSNate Lawson * previous (i.e. higher id) substrings upwards to make room for this one. 102558ad326bSNate Lawson * This only penalizes portions of substrings that contain more than 102658ad326bSNate Lawson * WIN_CHARS bytes when they are first encountered. 1027c4f02a89SMax Khon */ 1028c4f02a89SMax Khon void 1029c2819440SBruce Evans mbnambuf_write(struct mbnambuf *nbp, char *name, int id) 1030c4f02a89SMax Khon { 103158ad326bSNate Lawson char *slot; 1032c2819440SBruce Evans size_t count, newlen; 10334cdb1483SNate Lawson 1034abb0cbf9SEdward Tomasz Napierala if (nbp->nb_len != 0 && id != nbp->nb_last_id - 1) { 103554cf9198SKonstantin Belousov #ifdef MSDOSFS_DEBUG 1036abb0cbf9SEdward Tomasz Napierala printf("msdosfs: non-decreasing id: id %d, last id %d\n", 1037abb0cbf9SEdward Tomasz Napierala id, nbp->nb_last_id); 103854cf9198SKonstantin Belousov #endif 1039abb0cbf9SEdward Tomasz Napierala return; 1040abb0cbf9SEdward Tomasz Napierala } 104158ad326bSNate Lawson 1042c2819440SBruce Evans /* Will store this substring in a WIN_CHARS-aligned slot. */ 1043c2819440SBruce Evans slot = &nbp->nb_buf[id * WIN_CHARS]; 10444cdb1483SNate Lawson count = strlen(name); 1045c2819440SBruce Evans newlen = nbp->nb_len + count; 1046c2819440SBruce Evans if (newlen > WIN_MAXLEN || newlen > MAXNAMLEN) { 104754cf9198SKonstantin Belousov #ifdef MSDOSFS_DEBUG 1048c2819440SBruce Evans printf("msdosfs: file name length %zu too large\n", newlen); 104954cf9198SKonstantin Belousov #endif 10504cdb1483SNate Lawson return; 1051c4f02a89SMax Khon } 105258ad326bSNate Lawson 105358ad326bSNate Lawson /* Shift suffix upwards by the amount length exceeds WIN_CHARS. */ 1054c2819440SBruce Evans if (count > WIN_CHARS && nbp->nb_len != 0) 1055c2819440SBruce Evans bcopy(slot + WIN_CHARS, slot + count, nbp->nb_len); 105658ad326bSNate Lawson 105758ad326bSNate Lawson /* Copy in the substring to its slot and update length so far. */ 105858ad326bSNate Lawson bcopy(name, slot, count); 1059c2819440SBruce Evans nbp->nb_len = newlen; 1060c2819440SBruce Evans nbp->nb_last_id = id; 1061c4f02a89SMax Khon } 1062c4f02a89SMax Khon 1063c4f02a89SMax Khon /* 10644cdb1483SNate Lawson * Take the completed string and use it to setup the struct dirent. 10654cdb1483SNate Lawson * Be sure to always nul-terminate the d_name and then copy the string 10664cdb1483SNate Lawson * from our buffer. Note that this function assumes the full string has 10674cdb1483SNate Lawson * been reassembled in the buffer. If it's called before all substrings 10684cdb1483SNate Lawson * have been written via mbnambuf_write(), the result will be incorrect. 1069c4f02a89SMax Khon */ 1070c4f02a89SMax Khon char * 1071c2819440SBruce Evans mbnambuf_flush(struct mbnambuf *nbp, struct dirent *dp) 1072c4f02a89SMax Khon { 1073c4f02a89SMax Khon 1074c2819440SBruce Evans if (nbp->nb_len > sizeof(dp->d_name) - 1) { 1075c2819440SBruce Evans mbnambuf_init(nbp); 1076c4f02a89SMax Khon return (NULL); 1077c4f02a89SMax Khon } 1078c2819440SBruce Evans bcopy(&nbp->nb_buf[0], dp->d_name, nbp->nb_len); 1079c2819440SBruce Evans dp->d_name[nbp->nb_len] = '\0'; 1080c2819440SBruce Evans dp->d_namlen = nbp->nb_len; 10814cdb1483SNate Lawson 1082c2819440SBruce Evans mbnambuf_init(nbp); 1083c4f02a89SMax Khon return (dp->d_name); 1084c4f02a89SMax Khon } 1085