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 & LCASE_BASE, 274 pmp); 275 if (c & 0xff00) { 276 *un++ = c >> 8; 277 thislong++; 278 } 279 *un++ = c; 280 thislong++; 281 } 282 dn += i; 283 284 /* 285 * Now, if there is an extension then put in a period and copy in 286 * the extension. 287 */ 288 if (*dn != ' ') { 289 *un++ = '.'; 290 thislong++; 291 for (i = 3; i > 0 && *dn != ' ';) { 292 c = dos2unixchr((const u_char **)&dn, &i, 293 lower & LCASE_EXT, pmp); 294 if (c & 0xff00) { 295 *un++ = c >> 8; 296 thislong++; 297 } 298 *un++ = c; 299 thislong++; 300 } 301 } 302 *un++ = 0; 303 304 return (thislong); 305 } 306 307 /* 308 * Convert a unix filename to a DOS filename according to Win95 rules. 309 * If applicable and gen is not 0, it is inserted into the converted 310 * filename as a generation number. 311 * Returns 312 * 0 if name couldn't be converted 313 * 1 if the converted name is the same as the original 314 * (no long filename entry necessary for Win95) 315 * 2 if conversion was successful 316 * 3 if conversion was successful and generation number was inserted 317 */ 318 int 319 unix2dosfn(un, dn, unlen, gen, pmp) 320 const u_char *un; 321 u_char dn[12]; 322 size_t unlen; 323 u_int gen; 324 struct msdosfsmount *pmp; 325 { 326 ssize_t i, j; 327 int l; 328 int conv = 1; 329 const u_char *cp, *dp, *dp1; 330 u_char gentext[6], *wcp; 331 u_int16_t c; 332 333 /* 334 * Fill the dos filename string with blanks. These are DOS's pad 335 * characters. 336 */ 337 for (i = 0; i < 11; i++) 338 dn[i] = ' '; 339 dn[11] = 0; 340 341 /* 342 * The filenames "." and ".." are handled specially, since they 343 * don't follow dos filename rules. 344 */ 345 if (un[0] == '.' && unlen == 1) { 346 dn[0] = '.'; 347 return gen <= 1; 348 } 349 if (un[0] == '.' && un[1] == '.' && unlen == 2) { 350 dn[0] = '.'; 351 dn[1] = '.'; 352 return gen <= 1; 353 } 354 355 /* 356 * Filenames with only blanks and dots are not allowed! 357 */ 358 for (cp = un, i = unlen; --i >= 0; cp++) 359 if (*cp != ' ' && *cp != '.') 360 break; 361 if (i < 0) 362 return 0; 363 364 365 /* 366 * Filenames with some characters are not allowed! 367 */ 368 for (cp = un, i = unlen; i > 0;) 369 if (unix2doschr(&cp, (size_t *)&i, pmp) == 0) 370 return 0; 371 372 /* 373 * Now find the extension 374 * Note: dot as first char doesn't start extension 375 * and trailing dots and blanks are ignored 376 * Note(2003/7): It seems recent Windows has 377 * defferent rule than this code, that Windows 378 * ignores all dots before extension, and use all 379 * chars as filename except for dots. 380 */ 381 dp = dp1 = 0; 382 for (cp = un + 1, i = unlen - 1; --i >= 0;) { 383 switch (*cp++) { 384 case '.': 385 if (!dp1) 386 dp1 = cp; 387 break; 388 case ' ': 389 break; 390 default: 391 if (dp1) 392 dp = dp1; 393 dp1 = 0; 394 break; 395 } 396 } 397 398 /* 399 * Now convert it (this part is for extension). 400 * As Windows XP do, if it's not ascii char, 401 * this function should return 2 or 3, so that checkng out Unicode name. 402 */ 403 if (dp) { 404 if (dp1) 405 l = dp1 - dp; 406 else 407 l = unlen - (dp - un); 408 for (cp = dp, i = l, j = 8; i > 0 && j < 11; j++) { 409 c = unix2doschr(&cp, (size_t *)&i, pmp); 410 if (c & 0xff00) { 411 dn[j] = c >> 8; 412 if (++j < 11) { 413 dn[j] = c; 414 if (conv != 3) 415 conv = 2; 416 continue; 417 } else { 418 conv = 3; 419 dn[j-1] = ' '; 420 break; 421 } 422 } else { 423 dn[j] = c; 424 } 425 if (((dn[j] & 0x80) || *(cp - 1) != dn[j]) && conv != 3) 426 conv = 2; 427 if (dn[j] == 1) { 428 conv = 3; 429 dn[j] = '_'; 430 } 431 if (dn[j] == 2) { 432 conv = 3; 433 dn[j--] = ' '; 434 } 435 } 436 if (i > 0) 437 conv = 3; 438 dp--; 439 } else { 440 for (dp = cp; *--dp == ' ' || *dp == '.';); 441 dp++; 442 } 443 444 /* 445 * Now convert the rest of the name 446 */ 447 for (i = dp - un, j = 0; un < dp && j < 8; j++) { 448 c = unix2doschr(&un, &i, pmp); 449 if (c & 0xff00) { 450 dn[j] = c >> 8; 451 if (++j < 8) { 452 dn[j] = c; 453 if (conv != 3) 454 conv = 2; 455 continue; 456 } else { 457 conv = 3; 458 dn[j-1] = ' '; 459 break; 460 } 461 } else { 462 dn[j] = c; 463 } 464 if (((dn[j] & 0x80) || *(un - 1) != dn[j]) && conv != 3) 465 conv = 2; 466 if (dn[j] == 1) { 467 conv = 3; 468 dn[j] = '_'; 469 } 470 if (dn[j] == 2) { 471 conv = 3; 472 dn[j--] = ' '; 473 } 474 } 475 if (un < dp) 476 conv = 3; 477 /* 478 * If we didn't have any chars in filename, 479 * generate a default 480 */ 481 if (!j) 482 dn[0] = '_'; 483 484 /* 485 * If there wasn't any char dropped, 486 * there is no place for generation numbers 487 */ 488 if (conv != 3) { 489 if (gen > 1) 490 conv = 0; 491 goto done; 492 } 493 494 /* 495 * Now insert the generation number into the filename part 496 */ 497 if (gen == 0) 498 goto done; 499 for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10) 500 *--wcp = gen % 10 + '0'; 501 if (gen) { 502 conv = 0; 503 goto done; 504 } 505 for (i = 8; dn[--i] == ' ';); 506 i++; 507 if (gentext + sizeof(gentext) - wcp + 1 > 8 - i) 508 i = 8 - (gentext + sizeof(gentext) - wcp + 1); 509 /* 510 * Correct posision to where insert the generation number 511 */ 512 cp = dn; 513 i -= mbsadjpos((const char**)&cp, i, unlen, 1, pmp->pm_flags, pmp->pm_d2u); 514 515 dn[i++] = '~'; 516 while (wcp < gentext + sizeof(gentext)) 517 dn[i++] = *wcp++; 518 519 /* 520 * Tail of the filename should be space 521 */ 522 while (i < 8) 523 dn[i++] = ' '; 524 conv = 3; 525 526 done: 527 /* 528 * The first character cannot be E5, 529 * because that means a deleted entry 530 */ 531 if (dn[0] == 0xe5) 532 dn[0] = SLOT_E5; 533 534 return conv; 535 } 536 537 /* 538 * Create a Win95 long name directory entry 539 * Note: assumes that the filename is valid, 540 * i.e. doesn't consist solely of blanks and dots 541 */ 542 int 543 unix2winfn(un, unlen, wep, cnt, chksum, pmp) 544 const u_char *un; 545 size_t unlen; 546 struct winentry *wep; 547 int cnt; 548 int chksum; 549 struct msdosfsmount *pmp; 550 { 551 u_int8_t *wcp; 552 int i, end; 553 u_int16_t code; 554 555 /* 556 * Drop trailing blanks and dots 557 */ 558 unlen = winLenFixup(un, unlen); 559 560 /* 561 * Cut *un for this slot 562 */ 563 unlen = mbsadjpos((const char **)&un, unlen, (cnt - 1) * WIN_CHARS, 2, 564 pmp->pm_flags, pmp->pm_u2w); 565 566 /* 567 * Initialize winentry to some useful default 568 */ 569 for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff); 570 wep->weCnt = cnt; 571 wep->weAttributes = ATTR_WIN95; 572 wep->weReserved1 = 0; 573 wep->weChksum = chksum; 574 wep->weReserved2 = 0; 575 576 /* 577 * Now convert the filename parts 578 */ 579 end = 0; 580 for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0 && !end;) { 581 code = unix2winchr(&un, &unlen, 0, pmp); 582 *wcp++ = code; 583 *wcp++ = code >> 8; 584 if (!code) 585 end = WIN_LAST; 586 } 587 for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0 && !end;) { 588 code = unix2winchr(&un, &unlen, 0, pmp); 589 *wcp++ = code; 590 *wcp++ = code >> 8; 591 if (!code) 592 end = WIN_LAST; 593 } 594 for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0 && !end;) { 595 code = unix2winchr(&un, &unlen, 0, pmp); 596 *wcp++ = code; 597 *wcp++ = code >> 8; 598 if (!code) 599 end = WIN_LAST; 600 } 601 if (*un == '\0') 602 end = WIN_LAST; 603 wep->weCnt |= end; 604 return !end; 605 } 606 607 /* 608 * Compare our filename to the one in the Win95 entry 609 * Returns the checksum or -1 if no match 610 */ 611 int 612 winChkName(un, unlen, chksum, pmp) 613 const u_char *un; 614 size_t unlen; 615 int chksum; 616 struct msdosfsmount *pmp; 617 { 618 size_t len; 619 u_int16_t c1, c2; 620 u_char *np; 621 struct dirent dirbuf; 622 623 /* 624 * We alread have winentry in mbnambuf 625 */ 626 if (!mbnambuf_flush(&dirbuf) || !dirbuf.d_namlen) 627 return -1; 628 629 #ifdef MSDOSFS_DEBUG 630 printf("winChkName(): un=%s:%d,d_name=%s:%d\n", un, unlen, 631 dirbuf.d_name, 632 dirbuf.d_namlen); 633 #endif 634 635 /* 636 * Compare the name parts 637 */ 638 len = dirbuf.d_namlen; 639 if (unlen != len) 640 return -2; 641 642 for (np = dirbuf.d_name; unlen > 0 && len > 0;) { 643 /* 644 * Comparison must be case insensitive, because FAT disallows 645 * to look up or create files in case sensitive even when 646 * it's a long file name. 647 */ 648 c1 = unix2winchr((const u_char **)&np, &len, LCASE_BASE, pmp); 649 c2 = unix2winchr(&un, &unlen, LCASE_BASE, pmp); 650 if (c1 != c2) 651 return -2; 652 } 653 return chksum; 654 } 655 656 /* 657 * Convert Win95 filename to dirbuf. 658 * Returns the checksum or -1 if impossible 659 */ 660 int 661 win2unixfn(wep, chksum, pmp) 662 struct winentry *wep; 663 int chksum; 664 struct msdosfsmount *pmp; 665 { 666 u_int8_t *cp; 667 u_int8_t *np, name[WIN_CHARS * 2 + 1]; 668 u_int16_t code; 669 int i; 670 671 if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS) 672 || !(wep->weCnt&WIN_CNT)) 673 return -1; 674 675 /* 676 * First compare checksums 677 */ 678 if (wep->weCnt&WIN_LAST) { 679 chksum = wep->weChksum; 680 } else if (chksum != wep->weChksum) 681 chksum = -1; 682 if (chksum == -1) 683 return -1; 684 685 /* 686 * Convert the name parts 687 */ 688 np = name; 689 for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) { 690 code = (cp[1] << 8) | cp[0]; 691 switch (code) { 692 case 0: 693 *np = '\0'; 694 mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1); 695 return chksum; 696 case '/': 697 *np = '\0'; 698 return -1; 699 default: 700 code = win2unixchr(code, pmp); 701 if (code & 0xff00) 702 *np++ = code >> 8; 703 *np++ = code; 704 break; 705 } 706 cp += 2; 707 } 708 for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) { 709 code = (cp[1] << 8) | cp[0]; 710 switch (code) { 711 case 0: 712 *np = '\0'; 713 mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1); 714 return chksum; 715 case '/': 716 *np = '\0'; 717 return -1; 718 default: 719 code = win2unixchr(code, pmp); 720 if (code & 0xff00) 721 *np++ = code >> 8; 722 *np++ = code; 723 break; 724 } 725 cp += 2; 726 } 727 for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) { 728 code = (cp[1] << 8) | cp[0]; 729 switch (code) { 730 case 0: 731 *np = '\0'; 732 mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1); 733 return chksum; 734 case '/': 735 *np = '\0'; 736 return -1; 737 default: 738 code = win2unixchr(code, pmp); 739 if (code & 0xff00) 740 *np++ = code >> 8; 741 *np++ = code; 742 break; 743 } 744 cp += 2; 745 } 746 *np = '\0'; 747 mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1); 748 return chksum; 749 } 750 751 /* 752 * Compute the unrolled checksum of a DOS filename for Win95 LFN use. 753 */ 754 u_int8_t 755 winChksum(struct direntry *dep) 756 { 757 u_int8_t s; 758 759 s = dep->deName[0]; 760 s = ((s << 7) | (s >> 1)) + dep->deName[1]; 761 s = ((s << 7) | (s >> 1)) + dep->deName[2]; 762 s = ((s << 7) | (s >> 1)) + dep->deName[3]; 763 s = ((s << 7) | (s >> 1)) + dep->deName[4]; 764 s = ((s << 7) | (s >> 1)) + dep->deName[5]; 765 s = ((s << 7) | (s >> 1)) + dep->deName[6]; 766 s = ((s << 7) | (s >> 1)) + dep->deName[7]; 767 s = ((s << 7) | (s >> 1)) + dep->deExtension[0]; 768 s = ((s << 7) | (s >> 1)) + dep->deExtension[1]; 769 s = ((s << 7) | (s >> 1)) + dep->deExtension[2]; 770 771 return (s); 772 } 773 774 /* 775 * Determine the number of slots necessary for Win95 names 776 */ 777 int 778 winSlotCnt(un, unlen, pmp) 779 const u_char *un; 780 size_t unlen; 781 struct msdosfsmount *pmp; 782 { 783 size_t wlen; 784 char wn[WIN_MAXLEN * 2 + 1], *wnp; 785 786 unlen = winLenFixup(un, unlen); 787 788 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 789 wlen = WIN_MAXLEN * 2; 790 wnp = wn; 791 msdosfs_iconv->conv(pmp->pm_u2w, (const char **)&un, &unlen, &wnp, &wlen); 792 if (unlen > 0) 793 return 0; 794 return howmany(WIN_MAXLEN - wlen/2, WIN_CHARS); 795 } 796 797 if (unlen > WIN_MAXLEN) 798 return 0; 799 return howmany(unlen, WIN_CHARS); 800 } 801 802 /* 803 * Determine the number of bytes neccesary for Win95 names 804 */ 805 size_t 806 winLenFixup(un, unlen) 807 const u_char* un; 808 size_t unlen; 809 { 810 for (un += unlen; unlen > 0; unlen--) 811 if (*--un != ' ' && *un != '.') 812 break; 813 return unlen; 814 } 815 816 /* 817 * Store an area with multi byte string instr, and reterns left 818 * byte of instr and moves pointer forward. The area's size is 819 * inlen or outlen. 820 */ 821 static int 822 mbsadjpos(const char **instr, size_t inlen, size_t outlen, int weight, int flag, void *handle) 823 { 824 char *outp, outstr[outlen * weight + 1]; 825 826 if (flag & MSDOSFSMNT_KICONV && msdosfs_iconv) { 827 outp = outstr; 828 outlen *= weight; 829 msdosfs_iconv->conv(handle, instr, &inlen, &outp, &outlen); 830 return (inlen); 831 } 832 833 (*instr) += min(inlen, outlen); 834 return (inlen - min(inlen, outlen)); 835 } 836 837 /* 838 * Convert DOS char to Local char 839 */ 840 static u_int16_t 841 dos2unixchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp) 842 { 843 u_char c; 844 char *outp, outbuf[3]; 845 u_int16_t wc; 846 size_t len, olen; 847 848 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 849 olen = len = 2; 850 outp = outbuf; 851 852 if (lower & (LCASE_BASE | LCASE_EXT)) 853 msdosfs_iconv->convchr_case(pmp->pm_d2u, (const char **)instr, 854 ilen, &outp, &olen, KICONV_LOWER); 855 else 856 msdosfs_iconv->convchr(pmp->pm_d2u, (const char **)instr, 857 ilen, &outp, &olen); 858 len -= olen; 859 860 /* 861 * return '?' if failed to convert 862 */ 863 if (len == 0) { 864 (*ilen)--; 865 (*instr)++; 866 return ('?'); 867 } 868 869 wc = 0; 870 while(len--) 871 wc |= (*(outp - len - 1) & 0xff) << (len << 3); 872 return (wc); 873 } 874 875 (*ilen)--; 876 c = *(*instr)++; 877 c = dos2unix[c]; 878 if (lower & (LCASE_BASE | LCASE_EXT)) 879 c = u2l[c]; 880 return ((u_int16_t)c); 881 } 882 883 /* 884 * Convert Local char to DOS char 885 */ 886 static u_int16_t 887 unix2doschr(const u_char **instr, size_t *ilen, struct msdosfsmount *pmp) 888 { 889 u_char c; 890 char *up, *outp, unicode[3], outbuf[3]; 891 u_int16_t wc; 892 size_t len, ucslen, unixlen, olen; 893 894 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 895 /* 896 * to hide an invisible character, using a unicode filter 897 */ 898 ucslen = 2; 899 len = *ilen; 900 up = unicode; 901 msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr, 902 ilen, &up, &ucslen); 903 unixlen = len - *ilen; 904 905 /* 906 * cannot be converted 907 */ 908 if (unixlen == 0) { 909 (*ilen)--; 910 (*instr)++; 911 return (0); 912 } 913 914 /* 915 * return magic number for ascii char 916 */ 917 if (unixlen == 1) { 918 c = *(*instr -1); 919 if (! (c & 0x80)) { 920 c = unix2dos[c]; 921 if (c <= 2) 922 return (c); 923 } 924 } 925 926 /* 927 * now convert using libiconv 928 */ 929 *instr -= unixlen; 930 *ilen = len; 931 932 olen = len = 2; 933 outp = outbuf; 934 msdosfs_iconv->convchr_case(pmp->pm_u2d, (const char **)instr, 935 ilen, &outp, &olen, KICONV_FROM_UPPER); 936 len -= olen; 937 938 /* 939 * cannot be converted, but has unicode char, should return magic number 940 */ 941 if (len == 0) { 942 (*ilen) -= unixlen; 943 (*instr) += unixlen; 944 return (1); 945 } 946 947 wc = 0; 948 while(len--) 949 wc |= (*(outp - len - 1) & 0xff) << (len << 3); 950 return (wc); 951 } 952 953 (*ilen)--; 954 c = *(*instr)++; 955 c = l2u[c]; 956 c = unix2dos[c]; 957 return ((u_int16_t)c); 958 } 959 960 /* 961 * Convert Windows char to Local char 962 */ 963 static u_int16_t 964 win2unixchr(u_int16_t wc, struct msdosfsmount *pmp) 965 { 966 u_char *inp, *outp, inbuf[3], outbuf[3]; 967 size_t ilen, olen, len; 968 969 if (wc == 0) 970 return (0); 971 972 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 973 inbuf[0] = (u_char)(wc>>8); 974 inbuf[1] = (u_char)wc; 975 inbuf[2] = '\0'; 976 977 ilen = olen = len = 2; 978 inp = inbuf; 979 outp = outbuf; 980 msdosfs_iconv->convchr(pmp->pm_w2u, (const char **)&inp, &ilen, 981 (char **)&outp, &olen); 982 len -= olen; 983 984 /* 985 * return '?' if failed to convert 986 */ 987 if (len == 0) { 988 wc = '?'; 989 return (wc); 990 } 991 992 wc = 0; 993 while(len--) 994 wc |= (*(outp - len - 1) & 0xff) << (len << 3); 995 return (wc); 996 } 997 998 if (wc & 0xff00) 999 wc = '?'; 1000 1001 return (wc); 1002 } 1003 1004 /* 1005 * Convert Local char to Windows char 1006 */ 1007 static u_int16_t 1008 unix2winchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp) 1009 { 1010 u_char *outp, outbuf[3]; 1011 u_int16_t wc; 1012 size_t olen; 1013 1014 if (*ilen == 0) 1015 return (0); 1016 1017 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 1018 outp = outbuf; 1019 olen = 2; 1020 if (lower & (LCASE_BASE | LCASE_EXT)) 1021 msdosfs_iconv->convchr_case(pmp->pm_u2w, (const char **)instr, 1022 ilen, (char **)&outp, &olen, 1023 KICONV_FROM_LOWER); 1024 else 1025 msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr, 1026 ilen, (char **)&outp, &olen); 1027 1028 /* 1029 * return '0' if end of filename 1030 */ 1031 if (olen == 2) 1032 return (0); 1033 1034 wc = (outbuf[0]<<8) | outbuf[1]; 1035 1036 return (wc); 1037 } 1038 1039 (*ilen)--; 1040 wc = (*instr)[0]; 1041 if (lower & (LCASE_BASE | LCASE_EXT)) 1042 wc = u2l[wc]; 1043 (*instr)++; 1044 return (wc); 1045 } 1046 1047 /* 1048 * Initialize the temporary concatenation buffer (once) and mark it as 1049 * empty on subsequent calls. 1050 */ 1051 void 1052 mbnambuf_init(void) 1053 { 1054 1055 if (nambuf_ptr == NULL) { 1056 nambuf_ptr = malloc(MAXNAMLEN + 1, M_MSDOSFSMNT, M_WAITOK); 1057 nambuf_ptr[MAXNAMLEN] = '\0'; 1058 } 1059 nambuf_len = 0; 1060 nambuf_last_id = -1; 1061 } 1062 1063 /* 1064 * Fill out our concatenation buffer with the given substring, at the offset 1065 * specified by its id. Since this function must be called with ids in 1066 * descending order, we take advantage of the fact that ASCII substrings are 1067 * exactly WIN_CHARS in length. For non-ASCII substrings, we shift all 1068 * previous (i.e. higher id) substrings upwards to make room for this one. 1069 * This only penalizes portions of substrings that contain more than 1070 * WIN_CHARS bytes when they are first encountered. 1071 */ 1072 void 1073 mbnambuf_write(char *name, int id) 1074 { 1075 size_t count; 1076 char *slot; 1077 1078 KASSERT(nambuf_len == 0 || id == nambuf_last_id - 1, 1079 ("non-decreasing id, id %d last id %d", id, nambuf_last_id)); 1080 1081 /* Store this substring in a WIN_CHAR-aligned slot. */ 1082 slot = nambuf_ptr + (id * WIN_CHARS); 1083 count = strlen(name); 1084 if (nambuf_len + count > MAXNAMLEN) { 1085 printf("msdosfs: file name %zu too long\n", nambuf_len + count); 1086 return; 1087 } 1088 1089 /* Shift suffix upwards by the amount length exceeds WIN_CHARS. */ 1090 if (count > WIN_CHARS && nambuf_len != 0) 1091 bcopy(slot + WIN_CHARS, slot + count, nambuf_len); 1092 1093 /* Copy in the substring to its slot and update length so far. */ 1094 bcopy(name, slot, count); 1095 nambuf_len += count; 1096 nambuf_last_id = id; 1097 } 1098 1099 /* 1100 * Take the completed string and use it to setup the struct dirent. 1101 * Be sure to always nul-terminate the d_name and then copy the string 1102 * from our buffer. Note that this function assumes the full string has 1103 * been reassembled in the buffer. If it's called before all substrings 1104 * have been written via mbnambuf_write(), the result will be incorrect. 1105 */ 1106 char * 1107 mbnambuf_flush(struct dirent *dp) 1108 { 1109 1110 if (nambuf_len > sizeof(dp->d_name) - 1) { 1111 mbnambuf_init(); 1112 return (NULL); 1113 } 1114 bcopy(nambuf_ptr, dp->d_name, nambuf_len); 1115 dp->d_name[nambuf_len] = '\0'; 1116 dp->d_namlen = nambuf_len; 1117 1118 mbnambuf_init(); 1119 return (dp->d_name); 1120 } 1121