1 /* $FreeBSD$ */ 2 /* $NetBSD: msdosfs_conv.c,v 1.25 1997/11/17 15:36:40 ws Exp $ */ 3 4 /*- 5 * Copyright (C) 1995, 1997 Wolfgang Solfrank. 6 * Copyright (C) 1995, 1997 TooLs GmbH. 7 * All rights reserved. 8 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by TooLs GmbH. 21 * 4. The name of TooLs GmbH may not be used to endorse or promote products 22 * derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35 /*- 36 * Written by Paul Popelka (paulp@uts.amdahl.com) 37 * 38 * You can do anything you want with this software, just don't say you wrote 39 * it, and don't remove this notice. 40 * 41 * This software is provided "as is". 42 * 43 * The author supplies this software to be publicly redistributed on the 44 * understanding that the author is not responsible for the correct 45 * functioning of this software in any circumstances and is not liable for 46 * any damages caused by this software. 47 * 48 * October 1992 49 */ 50 51 /* 52 * System include files. 53 */ 54 #include <sys/param.h> 55 #include <sys/clock.h> 56 #include <sys/kernel.h> /* defines tz */ 57 #include <sys/systm.h> 58 #include <sys/dirent.h> 59 #include <sys/iconv.h> 60 #include <sys/mount.h> 61 #include <sys/malloc.h> 62 63 extern struct iconv_functions *msdosfs_iconv; 64 65 /* 66 * MSDOSFS include files. 67 */ 68 #include <fs/msdosfs/bpb.h> 69 #include <fs/msdosfs/msdosfsmount.h> 70 #include <fs/msdosfs/direntry.h> 71 72 static int mbsadjpos(const char **, size_t, size_t, int, int, void *handle); 73 static u_int16_t dos2unixchr(const u_char **, size_t *, int, struct msdosfsmount *); 74 static u_int16_t unix2doschr(const u_char **, size_t *, struct msdosfsmount *); 75 static u_int16_t win2unixchr(u_int16_t, struct msdosfsmount *); 76 static u_int16_t unix2winchr(const u_char **, size_t *, int, struct msdosfsmount *); 77 78 static char *nambuf_ptr; 79 static size_t nambuf_len; 80 static int nambuf_last_id; 81 82 /* 83 * 0 - character disallowed in long file name. 84 * 1 - character should be replaced by '_' in DOS file name, 85 * and generation number inserted. 86 * 2 - character ('.' and ' ') should be skipped in DOS file name, 87 * and generation number inserted. 88 */ 89 static u_char 90 unix2dos[256] = { 91 /* iso8859-1 -> cp850 */ 92 0, 0, 0, 0, 0, 0, 0, 0, /* 00-07 */ 93 0, 0, 0, 0, 0, 0, 0, 0, /* 08-0f */ 94 0, 0, 0, 0, 0, 0, 0, 0, /* 10-17 */ 95 0, 0, 0, 0, 0, 0, 0, 0, /* 18-1f */ 96 2, 0x21, 0, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 97 0x28, 0x29, 0, 1, 1, 0x2d, 2, 0, /* 28-2f */ 98 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 99 0x38, 0x39, 0, 1, 0, 1, 0, 0, /* 38-3f */ 100 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */ 101 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */ 102 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */ 103 0x58, 0x59, 0x5a, 1, 0, 1, 0x5e, 0x5f, /* 58-5f */ 104 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 60-67 */ 105 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 68-6f */ 106 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 70-77 */ 107 0x58, 0x59, 0x5a, 0x7b, 0, 0x7d, 0x7e, 0, /* 78-7f */ 108 0, 0, 0, 0, 0, 0, 0, 0, /* 80-87 */ 109 0, 0, 0, 0, 0, 0, 0, 0, /* 88-8f */ 110 0, 0, 0, 0, 0, 0, 0, 0, /* 90-97 */ 111 0, 0, 0, 0, 0, 0, 0, 0, /* 98-9f */ 112 0, 0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5, /* a0-a7 */ 113 0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee, /* a8-af */ 114 0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa, /* b0-b7 */ 115 0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8, /* b8-bf */ 116 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* c0-c7 */ 117 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* c8-cf */ 118 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e, /* d0-d7 */ 119 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1, /* d8-df */ 120 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* e0-e7 */ 121 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* e8-ef */ 122 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6, /* f0-f7 */ 123 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98, /* f8-ff */ 124 }; 125 126 static u_char 127 dos2unix[256] = { 128 /* cp850 -> iso8859-1 */ 129 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 00-07 */ 130 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 08-0f */ 131 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 10-17 */ 132 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 18-1f */ 133 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 134 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */ 135 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 136 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */ 137 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */ 138 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */ 139 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */ 140 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */ 141 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */ 142 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */ 143 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */ 144 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */ 145 0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7, /* 80-87 */ 146 0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5, /* 88-8f */ 147 0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9, /* 90-97 */ 148 0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, 0x3f, /* 98-9f */ 149 0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba, /* a0-a7 */ 150 0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb, /* a8-af */ 151 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xc1, 0xc2, 0xc0, /* b0-b7 */ 152 0xa9, 0x3f, 0x3f, 0x3f, 0x3f, 0xa2, 0xa5, 0x3f, /* b8-bf */ 153 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xe3, 0xc3, /* c0-c7 */ 154 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xa4, /* c8-cf */ 155 0xf0, 0xd0, 0xca, 0xcb, 0xc8, 0x3f, 0xcd, 0xce, /* d0-d7 */ 156 0xcf, 0x3f, 0x3f, 0x3f, 0x3f, 0xa6, 0xcc, 0x3f, /* d8-df */ 157 0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe, /* e0-e7 */ 158 0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f, /* e8-ef */ 159 0xad, 0xb1, 0x3f, 0xbe, 0xb6, 0xa7, 0xf7, 0xb8, /* f0-f7 */ 160 0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2, 0x3f, 0x3f, /* f8-ff */ 161 }; 162 163 static u_char 164 u2l[256] = { 165 /* tolower */ 166 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */ 167 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */ 168 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */ 169 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */ 170 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 171 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */ 172 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 173 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */ 174 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */ 175 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */ 176 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */ 177 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */ 178 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */ 179 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */ 180 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */ 181 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */ 182 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */ 183 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */ 184 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */ 185 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */ 186 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */ 187 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */ 188 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */ 189 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */ 190 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */ 191 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */ 192 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */ 193 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */ 194 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */ 195 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */ 196 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */ 197 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */ 198 }; 199 200 static u_char 201 l2u[256] = { 202 /* toupper */ 203 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */ 204 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */ 205 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */ 206 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */ 207 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */ 208 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */ 209 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */ 210 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */ 211 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */ 212 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */ 213 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */ 214 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */ 215 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */ 216 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */ 217 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */ 218 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */ 219 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */ 220 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */ 221 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */ 222 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */ 223 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */ 224 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */ 225 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */ 226 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */ 227 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */ 228 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */ 229 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */ 230 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */ 231 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */ 232 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */ 233 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */ 234 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */ 235 }; 236 237 /* 238 * DOS filenames are made of 2 parts, the name part and the extension part. 239 * The name part is 8 characters long and the extension part is 3 240 * characters long. They may contain trailing blanks if the name or 241 * extension are not long enough to fill their respective fields. 242 */ 243 244 /* 245 * Convert a DOS filename to a unix filename. And, return the number of 246 * characters in the resulting unix filename excluding the terminating 247 * null. 248 */ 249 int 250 dos2unixfn(dn, un, lower, pmp) 251 u_char dn[11]; 252 u_char *un; 253 int lower; 254 struct msdosfsmount *pmp; 255 { 256 size_t i; 257 int thislong = 0; 258 u_int16_t c; 259 260 /* 261 * If first char of the filename is SLOT_E5 (0x05), then the real 262 * first char of the filename should be 0xe5. But, they couldn't 263 * just have a 0xe5 mean 0xe5 because that is used to mean a freed 264 * directory slot. Another dos quirk. 265 */ 266 if (*dn == SLOT_E5) 267 *dn = 0xe5; 268 269 /* 270 * Copy the name portion into the unix filename string. 271 */ 272 for (i = 8; i > 0 && *dn != ' ';) { 273 c = dos2unixchr((const u_char **)&dn, &i, lower, pmp); 274 if (c & 0xff00) { 275 *un++ = c >> 8; 276 thislong++; 277 } 278 *un++ = c; 279 thislong++; 280 } 281 dn += i; 282 283 /* 284 * Now, if there is an extension then put in a period and copy in 285 * the extension. 286 */ 287 if (*dn != ' ') { 288 *un++ = '.'; 289 thislong++; 290 for (i = 3; i > 0 && *dn != ' ';) { 291 c = dos2unixchr((const u_char **)&dn, &i, lower, pmp); 292 if (c & 0xff00) { 293 *un++ = c >> 8; 294 thislong++; 295 } 296 *un++ = c; 297 thislong++; 298 } 299 } 300 *un++ = 0; 301 302 return (thislong); 303 } 304 305 /* 306 * Convert a unix filename to a DOS filename according to Win95 rules. 307 * If applicable and gen is not 0, it is inserted into the converted 308 * filename as a generation number. 309 * Returns 310 * 0 if name couldn't be converted 311 * 1 if the converted name is the same as the original 312 * (no long filename entry necessary for Win95) 313 * 2 if conversion was successful 314 * 3 if conversion was successful and generation number was inserted 315 */ 316 int 317 unix2dosfn(un, dn, unlen, gen, pmp) 318 const u_char *un; 319 u_char dn[12]; 320 size_t unlen; 321 u_int gen; 322 struct msdosfsmount *pmp; 323 { 324 ssize_t i, j; 325 int l; 326 int conv = 1; 327 const u_char *cp, *dp, *dp1; 328 u_char gentext[6], *wcp; 329 u_int16_t c; 330 331 /* 332 * Fill the dos filename string with blanks. These are DOS's pad 333 * characters. 334 */ 335 for (i = 0; i < 11; i++) 336 dn[i] = ' '; 337 dn[11] = 0; 338 339 /* 340 * The filenames "." and ".." are handled specially, since they 341 * don't follow dos filename rules. 342 */ 343 if (un[0] == '.' && unlen == 1) { 344 dn[0] = '.'; 345 return gen <= 1; 346 } 347 if (un[0] == '.' && un[1] == '.' && unlen == 2) { 348 dn[0] = '.'; 349 dn[1] = '.'; 350 return gen <= 1; 351 } 352 353 /* 354 * Filenames with only blanks and dots are not allowed! 355 */ 356 for (cp = un, i = unlen; --i >= 0; cp++) 357 if (*cp != ' ' && *cp != '.') 358 break; 359 if (i < 0) 360 return 0; 361 362 363 /* 364 * Filenames with some characters are not allowed! 365 */ 366 for (cp = un, i = unlen; i > 0;) 367 if (unix2doschr(&cp, (size_t *)&i, pmp) == 0) 368 return 0; 369 370 /* 371 * Now find the extension 372 * Note: dot as first char doesn't start extension 373 * and trailing dots and blanks are ignored 374 * Note(2003/7): It seems recent Windows has 375 * defferent rule than this code, that Windows 376 * ignores all dots before extension, and use all 377 * chars as filename except for dots. 378 */ 379 dp = dp1 = 0; 380 for (cp = un + 1, i = unlen - 1; --i >= 0;) { 381 switch (*cp++) { 382 case '.': 383 if (!dp1) 384 dp1 = cp; 385 break; 386 case ' ': 387 break; 388 default: 389 if (dp1) 390 dp = dp1; 391 dp1 = 0; 392 break; 393 } 394 } 395 396 /* 397 * Now convert it (this part is for extension). 398 * As Windows XP do, if it's not ascii char, 399 * this function should return 2 or 3, so that checkng out Unicode name. 400 */ 401 if (dp) { 402 if (dp1) 403 l = dp1 - dp; 404 else 405 l = unlen - (dp - un); 406 for (cp = dp, i = l, j = 8; i > 0 && j < 11; j++) { 407 c = unix2doschr(&cp, (size_t *)&i, pmp); 408 if (c & 0xff00) { 409 dn[j] = c >> 8; 410 if (++j < 11) { 411 dn[j] = c; 412 if (conv != 3) 413 conv = 2; 414 continue; 415 } else { 416 conv = 3; 417 dn[j-1] = ' '; 418 break; 419 } 420 } else { 421 dn[j] = c; 422 } 423 if (((dn[j] & 0x80) || *(cp - 1) != dn[j]) && conv != 3) 424 conv = 2; 425 if (dn[j] == 1) { 426 conv = 3; 427 dn[j] = '_'; 428 } 429 if (dn[j] == 2) { 430 conv = 3; 431 dn[j--] = ' '; 432 } 433 } 434 if (i > 0) 435 conv = 3; 436 dp--; 437 } else { 438 for (dp = cp; *--dp == ' ' || *dp == '.';); 439 dp++; 440 } 441 442 /* 443 * Now convert the rest of the name 444 */ 445 for (i = dp - un, j = 0; un < dp && j < 8; j++) { 446 c = unix2doschr(&un, &i, pmp); 447 if (c & 0xff00) { 448 dn[j] = c >> 8; 449 if (++j < 8) { 450 dn[j] = c; 451 if (conv != 3) 452 conv = 2; 453 continue; 454 } else { 455 conv = 3; 456 dn[j-1] = ' '; 457 break; 458 } 459 } else { 460 dn[j] = c; 461 } 462 if (((dn[j] & 0x80) || *(un - 1) != dn[j]) && conv != 3) 463 conv = 2; 464 if (dn[j] == 1) { 465 conv = 3; 466 dn[j] = '_'; 467 } 468 if (dn[j] == 2) { 469 conv = 3; 470 dn[j--] = ' '; 471 } 472 } 473 if (un < dp) 474 conv = 3; 475 /* 476 * If we didn't have any chars in filename, 477 * generate a default 478 */ 479 if (!j) 480 dn[0] = '_'; 481 482 /* 483 * If there wasn't any char dropped, 484 * there is no place for generation numbers 485 */ 486 if (conv != 3) { 487 if (gen > 1) 488 conv = 0; 489 goto done; 490 } 491 492 /* 493 * Now insert the generation number into the filename part 494 */ 495 if (gen == 0) 496 goto done; 497 for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10) 498 *--wcp = gen % 10 + '0'; 499 if (gen) { 500 conv = 0; 501 goto done; 502 } 503 for (i = 8; dn[--i] == ' ';); 504 i++; 505 if (gentext + sizeof(gentext) - wcp + 1 > 8 - i) 506 i = 8 - (gentext + sizeof(gentext) - wcp + 1); 507 /* 508 * Correct posision to where insert the generation number 509 */ 510 cp = dn; 511 i -= mbsadjpos((const char**)&cp, i, unlen, 1, pmp->pm_flags, pmp->pm_d2u); 512 513 dn[i++] = '~'; 514 while (wcp < gentext + sizeof(gentext)) 515 dn[i++] = *wcp++; 516 517 /* 518 * Tail of the filename should be space 519 */ 520 while (i < 8) 521 dn[i++] = ' '; 522 conv = 3; 523 524 done: 525 /* 526 * The first character cannot be E5, 527 * because that means a deleted entry 528 */ 529 if (dn[0] == 0xe5) 530 dn[0] = SLOT_E5; 531 532 return conv; 533 } 534 535 /* 536 * Create a Win95 long name directory entry 537 * Note: assumes that the filename is valid, 538 * i.e. doesn't consist solely of blanks and dots 539 */ 540 int 541 unix2winfn(un, unlen, wep, cnt, chksum, pmp) 542 const u_char *un; 543 size_t unlen; 544 struct winentry *wep; 545 int cnt; 546 int chksum; 547 struct msdosfsmount *pmp; 548 { 549 u_int8_t *wcp; 550 int i, end; 551 u_int16_t code; 552 553 /* 554 * Drop trailing blanks and dots 555 */ 556 unlen = winLenFixup(un, unlen); 557 558 /* 559 * Cut *un for this slot 560 */ 561 unlen = mbsadjpos((const char **)&un, unlen, (cnt - 1) * WIN_CHARS, 2, 562 pmp->pm_flags, pmp->pm_u2w); 563 564 /* 565 * Initialize winentry to some useful default 566 */ 567 for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff); 568 wep->weCnt = cnt; 569 wep->weAttributes = ATTR_WIN95; 570 wep->weReserved1 = 0; 571 wep->weChksum = chksum; 572 wep->weReserved2 = 0; 573 574 /* 575 * Now convert the filename parts 576 */ 577 end = 0; 578 for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0 && !end;) { 579 code = unix2winchr(&un, &unlen, 0, pmp); 580 *wcp++ = code; 581 *wcp++ = code >> 8; 582 if (!code) 583 end = WIN_LAST; 584 } 585 for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0 && !end;) { 586 code = unix2winchr(&un, &unlen, 0, pmp); 587 *wcp++ = code; 588 *wcp++ = code >> 8; 589 if (!code) 590 end = WIN_LAST; 591 } 592 for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0 && !end;) { 593 code = unix2winchr(&un, &unlen, 0, pmp); 594 *wcp++ = code; 595 *wcp++ = code >> 8; 596 if (!code) 597 end = WIN_LAST; 598 } 599 if (*un == '\0') 600 end = WIN_LAST; 601 wep->weCnt |= end; 602 return !end; 603 } 604 605 /* 606 * Compare our filename to the one in the Win95 entry 607 * Returns the checksum or -1 if no match 608 */ 609 int 610 winChkName(un, unlen, chksum, pmp) 611 const u_char *un; 612 size_t unlen; 613 int chksum; 614 struct msdosfsmount *pmp; 615 { 616 size_t len; 617 u_int16_t c1, c2; 618 u_char *np; 619 struct dirent dirbuf; 620 621 /* 622 * We alread have winentry in mbnambuf 623 */ 624 if (!mbnambuf_flush(&dirbuf) || !dirbuf.d_namlen) 625 return -1; 626 627 #ifdef MSDOSFS_DEBUG 628 printf("winChkName(): un=%s:%d,d_name=%s:%d\n", un, unlen, 629 dirbuf.d_name, 630 dirbuf.d_namlen); 631 #endif 632 633 /* 634 * Compare the name parts 635 */ 636 len = dirbuf.d_namlen; 637 if (unlen != len) 638 return -2; 639 640 for (np = dirbuf.d_name; unlen > 0 && len > 0;) { 641 /* 642 * Comparison must be case insensitive, because FAT disallows 643 * to look up or create files in case sensitive even when 644 * it's a long file name. 645 */ 646 c1 = unix2winchr((const u_char **)&np, &len, LCASE_BASE, pmp); 647 c2 = unix2winchr(&un, &unlen, LCASE_BASE, pmp); 648 if (c1 != c2) 649 return -2; 650 } 651 return chksum; 652 } 653 654 /* 655 * Convert Win95 filename to dirbuf. 656 * Returns the checksum or -1 if impossible 657 */ 658 int 659 win2unixfn(wep, chksum, pmp) 660 struct winentry *wep; 661 int chksum; 662 struct msdosfsmount *pmp; 663 { 664 u_int8_t *cp; 665 u_int8_t *np, name[WIN_CHARS * 2 + 1]; 666 u_int16_t code; 667 int i; 668 669 if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS) 670 || !(wep->weCnt&WIN_CNT)) 671 return -1; 672 673 /* 674 * First compare checksums 675 */ 676 if (wep->weCnt&WIN_LAST) { 677 chksum = wep->weChksum; 678 } else if (chksum != wep->weChksum) 679 chksum = -1; 680 if (chksum == -1) 681 return -1; 682 683 /* 684 * Convert the name parts 685 */ 686 np = name; 687 for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) { 688 code = (cp[1] << 8) | cp[0]; 689 switch (code) { 690 case 0: 691 *np = '\0'; 692 mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1); 693 return chksum; 694 case '/': 695 *np = '\0'; 696 return -1; 697 default: 698 code = win2unixchr(code, pmp); 699 if (code & 0xff00) 700 *np++ = code >> 8; 701 *np++ = code; 702 break; 703 } 704 cp += 2; 705 } 706 for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) { 707 code = (cp[1] << 8) | cp[0]; 708 switch (code) { 709 case 0: 710 *np = '\0'; 711 mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1); 712 return chksum; 713 case '/': 714 *np = '\0'; 715 return -1; 716 default: 717 code = win2unixchr(code, pmp); 718 if (code & 0xff00) 719 *np++ = code >> 8; 720 *np++ = code; 721 break; 722 } 723 cp += 2; 724 } 725 for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) { 726 code = (cp[1] << 8) | cp[0]; 727 switch (code) { 728 case 0: 729 *np = '\0'; 730 mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1); 731 return chksum; 732 case '/': 733 *np = '\0'; 734 return -1; 735 default: 736 code = win2unixchr(code, pmp); 737 if (code & 0xff00) 738 *np++ = code >> 8; 739 *np++ = code; 740 break; 741 } 742 cp += 2; 743 } 744 *np = '\0'; 745 mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1); 746 return chksum; 747 } 748 749 /* 750 * Compute the unrolled checksum of a DOS filename for Win95 LFN use. 751 */ 752 u_int8_t 753 winChksum(struct direntry *dep) 754 { 755 u_int8_t s; 756 757 s = dep->deName[0]; 758 s = ((s << 7) | (s >> 1)) + dep->deName[1]; 759 s = ((s << 7) | (s >> 1)) + dep->deName[2]; 760 s = ((s << 7) | (s >> 1)) + dep->deName[3]; 761 s = ((s << 7) | (s >> 1)) + dep->deName[4]; 762 s = ((s << 7) | (s >> 1)) + dep->deName[5]; 763 s = ((s << 7) | (s >> 1)) + dep->deName[6]; 764 s = ((s << 7) | (s >> 1)) + dep->deName[7]; 765 s = ((s << 7) | (s >> 1)) + dep->deExtension[0]; 766 s = ((s << 7) | (s >> 1)) + dep->deExtension[1]; 767 s = ((s << 7) | (s >> 1)) + dep->deExtension[2]; 768 769 return (s); 770 } 771 772 /* 773 * Determine the number of slots necessary for Win95 names 774 */ 775 int 776 winSlotCnt(un, unlen, pmp) 777 const u_char *un; 778 size_t unlen; 779 struct msdosfsmount *pmp; 780 { 781 size_t wlen; 782 char wn[WIN_MAXLEN * 2 + 1], *wnp; 783 784 unlen = winLenFixup(un, unlen); 785 786 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 787 wlen = WIN_MAXLEN * 2; 788 wnp = wn; 789 msdosfs_iconv->conv(pmp->pm_u2w, (const char **)&un, &unlen, &wnp, &wlen); 790 if (unlen > 0) 791 return 0; 792 return howmany(WIN_MAXLEN - wlen/2, WIN_CHARS); 793 } 794 795 if (unlen > WIN_MAXLEN) 796 return 0; 797 return howmany(unlen, WIN_CHARS); 798 } 799 800 /* 801 * Determine the number of bytes neccesary for Win95 names 802 */ 803 size_t 804 winLenFixup(un, unlen) 805 const u_char* un; 806 size_t unlen; 807 { 808 for (un += unlen; unlen > 0; unlen--) 809 if (*--un != ' ' && *un != '.') 810 break; 811 return unlen; 812 } 813 814 /* 815 * Store an area with multi byte string instr, and reterns left 816 * byte of instr and moves pointer forward. The area's size is 817 * inlen or outlen. 818 */ 819 static int 820 mbsadjpos(const char **instr, size_t inlen, size_t outlen, int weight, int flag, void *handle) 821 { 822 char *outp, outstr[outlen * weight + 1]; 823 824 if (flag & MSDOSFSMNT_KICONV && msdosfs_iconv) { 825 outp = outstr; 826 outlen *= weight; 827 msdosfs_iconv->conv(handle, instr, &inlen, &outp, &outlen); 828 return (inlen); 829 } 830 831 (*instr) += min(inlen, outlen); 832 return (inlen - min(inlen, outlen)); 833 } 834 835 /* 836 * Convert DOS char to Local char 837 */ 838 static u_int16_t 839 dos2unixchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp) 840 { 841 u_char c; 842 char *outp, outbuf[3]; 843 u_int16_t wc; 844 size_t len, olen; 845 846 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 847 olen = len = 2; 848 outp = outbuf; 849 850 if (lower & (LCASE_BASE | LCASE_EXT)) 851 msdosfs_iconv->convchr_case(pmp->pm_d2u, (const char **)instr, 852 ilen, &outp, &olen, KICONV_LOWER); 853 else 854 msdosfs_iconv->convchr(pmp->pm_d2u, (const char **)instr, 855 ilen, &outp, &olen); 856 len -= olen; 857 858 /* 859 * return '?' if failed to convert 860 */ 861 if (len == 0) { 862 (*ilen)--; 863 (*instr)++; 864 return ('?'); 865 } 866 867 wc = 0; 868 while(len--) 869 wc |= (*(outp - len - 1) & 0xff) << (len << 3); 870 return (wc); 871 } 872 873 (*ilen)--; 874 c = *(*instr)++; 875 c = dos2unix[c]; 876 if (lower & (LCASE_BASE | LCASE_EXT)) 877 c = u2l[c]; 878 return ((u_int16_t)c); 879 } 880 881 /* 882 * Convert Local char to DOS char 883 */ 884 static u_int16_t 885 unix2doschr(const u_char **instr, size_t *ilen, struct msdosfsmount *pmp) 886 { 887 u_char c; 888 char *up, *outp, unicode[3], outbuf[3]; 889 u_int16_t wc; 890 size_t len, ucslen, unixlen, olen; 891 892 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 893 /* 894 * to hide an invisible character, using a unicode filter 895 */ 896 ucslen = 2; 897 len = *ilen; 898 up = unicode; 899 msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr, 900 ilen, &up, &ucslen); 901 unixlen = len - *ilen; 902 903 /* 904 * cannot be converted 905 */ 906 if (unixlen == 0) { 907 (*ilen)--; 908 (*instr)++; 909 return (0); 910 } 911 912 /* 913 * return magic number for ascii char 914 */ 915 if (unixlen == 1) { 916 c = *(*instr -1); 917 if (! (c & 0x80)) { 918 c = unix2dos[c]; 919 if (c <= 2) 920 return (c); 921 } 922 } 923 924 /* 925 * now convert using libiconv 926 */ 927 *instr -= unixlen; 928 *ilen = len; 929 930 olen = len = 2; 931 outp = outbuf; 932 msdosfs_iconv->convchr_case(pmp->pm_u2d, (const char **)instr, 933 ilen, &outp, &olen, KICONV_FROM_UPPER); 934 len -= olen; 935 936 /* 937 * cannot be converted, but has unicode char, should return magic number 938 */ 939 if (len == 0) { 940 (*ilen) -= unixlen; 941 (*instr) += unixlen; 942 return (1); 943 } 944 945 wc = 0; 946 while(len--) 947 wc |= (*(outp - len - 1) & 0xff) << (len << 3); 948 return (wc); 949 } 950 951 (*ilen)--; 952 c = *(*instr)++; 953 c = l2u[c]; 954 c = unix2dos[c]; 955 return ((u_int16_t)c); 956 } 957 958 /* 959 * Convert Windows char to Local char 960 */ 961 static u_int16_t 962 win2unixchr(u_int16_t wc, struct msdosfsmount *pmp) 963 { 964 u_char *inp, *outp, inbuf[3], outbuf[3]; 965 size_t ilen, olen, len; 966 967 if (wc == 0) 968 return (0); 969 970 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 971 inbuf[0] = (u_char)(wc>>8); 972 inbuf[1] = (u_char)wc; 973 inbuf[2] = '\0'; 974 975 ilen = olen = len = 2; 976 inp = inbuf; 977 outp = outbuf; 978 msdosfs_iconv->convchr(pmp->pm_w2u, (const char **)&inp, &ilen, 979 (char **)&outp, &olen); 980 len -= olen; 981 982 /* 983 * return '?' if failed to convert 984 */ 985 if (len == 0) { 986 wc = '?'; 987 return (wc); 988 } 989 990 wc = 0; 991 while(len--) 992 wc |= (*(outp - len - 1) & 0xff) << (len << 3); 993 return (wc); 994 } 995 996 if (wc & 0xff00) 997 wc = '?'; 998 999 return (wc); 1000 } 1001 1002 /* 1003 * Convert Local char to Windows char 1004 */ 1005 static u_int16_t 1006 unix2winchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp) 1007 { 1008 u_char *outp, outbuf[3]; 1009 u_int16_t wc; 1010 size_t olen; 1011 1012 if (*ilen == 0) 1013 return (0); 1014 1015 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 1016 outp = outbuf; 1017 olen = 2; 1018 if (lower & (LCASE_BASE | LCASE_EXT)) 1019 msdosfs_iconv->convchr_case(pmp->pm_u2w, (const char **)instr, 1020 ilen, (char **)&outp, &olen, 1021 KICONV_FROM_LOWER); 1022 else 1023 msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr, 1024 ilen, (char **)&outp, &olen); 1025 1026 /* 1027 * return '0' if end of filename 1028 */ 1029 if (olen == 2) 1030 return (0); 1031 1032 wc = (outbuf[0]<<8) | outbuf[1]; 1033 1034 return (wc); 1035 } 1036 1037 (*ilen)--; 1038 wc = (*instr)[0]; 1039 if (lower & (LCASE_BASE | LCASE_EXT)) 1040 wc = u2l[wc]; 1041 (*instr)++; 1042 return (wc); 1043 } 1044 1045 /* 1046 * Initialize the temporary concatenation buffer (once) and mark it as 1047 * empty on subsequent calls. 1048 */ 1049 void 1050 mbnambuf_init(void) 1051 { 1052 1053 if (nambuf_ptr == NULL) { 1054 nambuf_ptr = malloc(MAXNAMLEN + 1, M_MSDOSFSMNT, M_WAITOK); 1055 nambuf_ptr[MAXNAMLEN] = '\0'; 1056 } 1057 nambuf_len = 0; 1058 nambuf_last_id = -1; 1059 } 1060 1061 /* 1062 * Fill out our concatenation buffer with the given substring, at the offset 1063 * specified by its id. Since this function must be called with ids in 1064 * descending order, we take advantage of the fact that ASCII substrings are 1065 * exactly WIN_CHARS in length. For non-ASCII substrings, we shift all 1066 * previous (i.e. higher id) substrings upwards to make room for this one. 1067 * This only penalizes portions of substrings that contain more than 1068 * WIN_CHARS bytes when they are first encountered. 1069 */ 1070 void 1071 mbnambuf_write(char *name, int id) 1072 { 1073 size_t count; 1074 char *slot; 1075 1076 KASSERT(nambuf_len == 0 || id == nambuf_last_id - 1, 1077 ("non-decreasing id, id %d last id %d", id, nambuf_last_id)); 1078 1079 /* Store this substring in a WIN_CHAR-aligned slot. */ 1080 slot = nambuf_ptr + (id * WIN_CHARS); 1081 count = strlen(name); 1082 if (nambuf_len + count > MAXNAMLEN) { 1083 printf("msdosfs: file name %zu too long\n", nambuf_len + count); 1084 return; 1085 } 1086 1087 /* Shift suffix upwards by the amount length exceeds WIN_CHARS. */ 1088 if (count > WIN_CHARS && nambuf_len != 0) 1089 bcopy(slot + WIN_CHARS, slot + count, nambuf_len); 1090 1091 /* Copy in the substring to its slot and update length so far. */ 1092 bcopy(name, slot, count); 1093 nambuf_len += count; 1094 nambuf_last_id = id; 1095 } 1096 1097 /* 1098 * Take the completed string and use it to setup the struct dirent. 1099 * Be sure to always nul-terminate the d_name and then copy the string 1100 * from our buffer. Note that this function assumes the full string has 1101 * been reassembled in the buffer. If it's called before all substrings 1102 * have been written via mbnambuf_write(), the result will be incorrect. 1103 */ 1104 char * 1105 mbnambuf_flush(struct dirent *dp) 1106 { 1107 1108 if (nambuf_len > sizeof(dp->d_name) - 1) { 1109 mbnambuf_init(); 1110 return (NULL); 1111 } 1112 bcopy(nambuf_ptr, dp->d_name, nambuf_len); 1113 dp->d_name[nambuf_len] = '\0'; 1114 dp->d_namlen = nambuf_len; 1115 1116 mbnambuf_init(); 1117 return (dp->d_name); 1118 } 1119