1*da6c28aaSamw /* 2*da6c28aaSamw * CDDL HEADER START 3*da6c28aaSamw * 4*da6c28aaSamw * The contents of this file are subject to the terms of the 5*da6c28aaSamw * Common Development and Distribution License (the "License"). 6*da6c28aaSamw * You may not use this file except in compliance with the License. 7*da6c28aaSamw * 8*da6c28aaSamw * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*da6c28aaSamw * or http://www.opensolaris.org/os/licensing. 10*da6c28aaSamw * See the License for the specific language governing permissions 11*da6c28aaSamw * and limitations under the License. 12*da6c28aaSamw * 13*da6c28aaSamw * When distributing Covered Code, include this CDDL HEADER in each 14*da6c28aaSamw * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*da6c28aaSamw * If applicable, add the following below this CDDL HEADER, with the 16*da6c28aaSamw * fields enclosed by brackets "[]" replaced with your own identifying 17*da6c28aaSamw * information: Portions Copyright [yyyy] [name of copyright owner] 18*da6c28aaSamw * 19*da6c28aaSamw * CDDL HEADER END 20*da6c28aaSamw */ 21*da6c28aaSamw /* 22*da6c28aaSamw * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23*da6c28aaSamw * Use is subject to license terms. 24*da6c28aaSamw */ 25*da6c28aaSamw 26*da6c28aaSamw #pragma ident "%Z%%M% %I% %E% SMI" 27*da6c28aaSamw 28*da6c28aaSamw /* 29*da6c28aaSamw * Support for oem <-> unicode translations. 30*da6c28aaSamw */ 31*da6c28aaSamw 32*da6c28aaSamw #ifndef _KERNEL 33*da6c28aaSamw #include <stdio.h> 34*da6c28aaSamw #include <stdlib.h> 35*da6c28aaSamw #include <thread.h> 36*da6c28aaSamw #include <synch.h> 37*da6c28aaSamw #include <string.h> 38*da6c28aaSamw #endif /* _KERNEL */ 39*da6c28aaSamw #include <smbsrv/alloc.h> 40*da6c28aaSamw #include <smbsrv/string.h> 41*da6c28aaSamw #include <smbsrv/oem.h> 42*da6c28aaSamw #include <sys/byteorder.h> 43*da6c28aaSamw /* 44*da6c28aaSamw * name: Name used to show on the telnet/GUI. 45*da6c28aaSamw * filename: The actual filename contains the codepage. 46*da6c28aaSamw * doublebytes: The codepage is double or single byte. 47*da6c28aaSamw * oempage: The oempage is used to convert Unicode to OEM chars. 48*da6c28aaSamw * Memory needs to be allocated for value field of oempage 49*da6c28aaSamw * to store the entire table. 50*da6c28aaSamw * unipage: The unipage is used to convert OEM to Unicode chars. 51*da6c28aaSamw * Memory needs to be allocated for value field of unipage 52*da6c28aaSamw * to store the entire table. 53*da6c28aaSamw * valid: This field indicates if the page is valid or not. 54*da6c28aaSamw * ref: This ref count is used to keep track of the usage of BOTH 55*da6c28aaSamw * oempage and unipage. 56*da6c28aaSamw * Note: If the cpid of the table is changed, please change the 57*da6c28aaSamw * codepage_id in oem.h as well. 58*da6c28aaSamw */ 59*da6c28aaSamw typedef struct oem_codepage { 60*da6c28aaSamw char *filename; 61*da6c28aaSamw unsigned int bytesperchar; 62*da6c28aaSamw oempage_t oempage; 63*da6c28aaSamw oempage_t unicodepage; 64*da6c28aaSamw unsigned int valid; 65*da6c28aaSamw unsigned int ref; 66*da6c28aaSamw } oem_codepage_t; 67*da6c28aaSamw 68*da6c28aaSamw static oem_codepage_t oemcp_table[] = { 69*da6c28aaSamw {"850.cpg", 1, {0, 0}, {0, 0}, 0, 0}, /* Multilingual Latin1 */ 70*da6c28aaSamw {"950.cpg", 2, {1, 0}, {1, 0}, 0, 0}, /* Chinese Traditional */ 71*da6c28aaSamw {"1252.cpg", 1, {2, 0}, {2, 0}, 0, 0}, /* MS Latin1 */ 72*da6c28aaSamw {"949.cpg", 2, {3, 0}, {3, 0}, 0, 0}, /* Korean */ 73*da6c28aaSamw {"936.cpg", 2, {4, 0}, {4, 0}, 0, 0}, /* Chinese Simplified */ 74*da6c28aaSamw {"932.cpg", 2, {5, 0}, {5, 0}, 0, 0}, /* Japanese */ 75*da6c28aaSamw {"852.cpg", 1, {6, 0}, {6, 0}, 0, 0}, /* Multilingual Latin2 */ 76*da6c28aaSamw {"1250.cpg", 1, {7, 0}, {7, 0}, 0, 0}, /* MS Latin2 */ 77*da6c28aaSamw {"1253.cpg", 1, {8, 0}, {8, 0}, 0, 0}, /* MS Greek */ 78*da6c28aaSamw {"737.cpg", 1, {9, 0}, {9, 0}, 0, 0}, /* Greek */ 79*da6c28aaSamw {"1254.cpg", 1, {10, 0}, {10, 0}, 0, 0}, /* MS Turkish */ 80*da6c28aaSamw {"857.cpg", 1, {11, 0}, {11, 0}, 0, 0}, /* Multilingual Latin5 */ 81*da6c28aaSamw {"1251.cpg", 1, {12, 0}, {12, 0}, 0, 0}, /* MS Cyrillic */ 82*da6c28aaSamw {"866.cpg", 1, {13, 0}, {13, 0}, 0, 0}, /* Cyrillic II */ 83*da6c28aaSamw {"1255.cpg", 1, {14, 0}, {14, 0}, 0, 0}, /* MS Hebrew */ 84*da6c28aaSamw {"862.cpg", 1, {15, 0}, {15, 0}, 0, 0}, /* Hebrew */ 85*da6c28aaSamw {"1256.cpg", 1, {16, 0}, {16, 0}, 0, 0}, /* MS Arabic */ 86*da6c28aaSamw {"720.cpg", 1, {17, 0}, {17, 0}, 0, 0} /* Arabic */ 87*da6c28aaSamw }; 88*da6c28aaSamw 89*da6c28aaSamw static language lang_table[] = { 90*da6c28aaSamw {"Arabic", OEM_CP_IND_720, OEM_CP_IND_1256}, 91*da6c28aaSamw {"Brazilian", OEM_CP_IND_850, OEM_CP_IND_1252}, 92*da6c28aaSamw {"Chinese Traditional", OEM_CP_IND_950, OEM_CP_IND_950}, 93*da6c28aaSamw {"Chinese Simplified", OEM_CP_IND_936, OEM_CP_IND_936}, 94*da6c28aaSamw {"Czech", OEM_CP_IND_852, OEM_CP_IND_1250}, 95*da6c28aaSamw {"Danish", OEM_CP_IND_850, OEM_CP_IND_1252}, 96*da6c28aaSamw {"Dutch", OEM_CP_IND_850, OEM_CP_IND_1252}, 97*da6c28aaSamw {"English", OEM_CP_IND_850, OEM_CP_IND_1252}, 98*da6c28aaSamw {"Finnish", OEM_CP_IND_850, OEM_CP_IND_1252}, 99*da6c28aaSamw {"French", OEM_CP_IND_850, OEM_CP_IND_1252}, 100*da6c28aaSamw {"German", OEM_CP_IND_850, OEM_CP_IND_1252}, 101*da6c28aaSamw {"Greek", OEM_CP_IND_737, OEM_CP_IND_1253}, 102*da6c28aaSamw {"Hebrew", OEM_CP_IND_862, OEM_CP_IND_1255}, 103*da6c28aaSamw {"Hungarian", OEM_CP_IND_852, OEM_CP_IND_1250}, 104*da6c28aaSamw {"Italian", OEM_CP_IND_850, OEM_CP_IND_1252}, 105*da6c28aaSamw {"Japanese", OEM_CP_IND_932, OEM_CP_IND_932}, 106*da6c28aaSamw {"Korean", OEM_CP_IND_949, OEM_CP_IND_949}, 107*da6c28aaSamw {"Norwegian", OEM_CP_IND_850, OEM_CP_IND_1252}, 108*da6c28aaSamw {"Polish", OEM_CP_IND_852, OEM_CP_IND_1250}, 109*da6c28aaSamw {"Russian", OEM_CP_IND_866, OEM_CP_IND_1251}, 110*da6c28aaSamw {"Slovak", OEM_CP_IND_852, OEM_CP_IND_1250}, 111*da6c28aaSamw {"Slovenian", OEM_CP_IND_852, OEM_CP_IND_1250}, 112*da6c28aaSamw {"Spanish", OEM_CP_IND_850, OEM_CP_IND_1252}, 113*da6c28aaSamw {"Swedish", OEM_CP_IND_850, OEM_CP_IND_1252}, 114*da6c28aaSamw {"Turkish", OEM_CP_IND_857, OEM_CP_IND_1254} 115*da6c28aaSamw }; 116*da6c28aaSamw 117*da6c28aaSamw 118*da6c28aaSamw 119*da6c28aaSamw /* 120*da6c28aaSamw * The oem_default_smb_cp is the default smb codepage for English. 121*da6c28aaSamw * It is actually codepage 850. 122*da6c28aaSamw */ 123*da6c28aaSamw mts_wchar_t oem_default_smb_cp[256] = { 124*da6c28aaSamw 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 125*da6c28aaSamw 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, 126*da6c28aaSamw 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, 127*da6c28aaSamw 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, 128*da6c28aaSamw 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, 129*da6c28aaSamw 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, 130*da6c28aaSamw 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 131*da6c28aaSamw 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, 132*da6c28aaSamw 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 133*da6c28aaSamw 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, 134*da6c28aaSamw 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 135*da6c28aaSamw 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, 136*da6c28aaSamw 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 137*da6c28aaSamw 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, 138*da6c28aaSamw 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, 139*da6c28aaSamw 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, 140*da6c28aaSamw 0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7, 141*da6c28aaSamw 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5, 142*da6c28aaSamw 0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9, 143*da6c28aaSamw 0x00FF, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x00D7, 0x0192, 144*da6c28aaSamw 0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA, 145*da6c28aaSamw 0x00BF, 0x00AE, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB, 146*da6c28aaSamw 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x00C1, 0x00C2, 0x00C0, 147*da6c28aaSamw 0x00A9, 0x2563, 0x2551, 0x2557, 0x255D, 0x00A2, 0x00A5, 0x2510, 148*da6c28aaSamw 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x00E3, 0x00C3, 149*da6c28aaSamw 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x00A4, 150*da6c28aaSamw 0x00F0, 0x00D0, 0x00CA, 0x00CB, 0x00C8, 0x0131, 0x00CD, 0x00CE, 151*da6c28aaSamw 0x00CF, 0x2518, 0x250C, 0x2588, 0x2584, 0x00A6, 0x00CC, 0x2580, 152*da6c28aaSamw 0x00D3, 0x00DF, 0x00D4, 0x00D2, 0x00F5, 0x00D5, 0x00B5, 0x00FE, 153*da6c28aaSamw 0x00DE, 0x00DA, 0x00DB, 0x00D9, 0x00FD, 0x00DD, 0x00AF, 0x00B4, 154*da6c28aaSamw 0x00AD, 0x00B1, 0x2017, 0x00BE, 0x00B6, 0x00A7, 0x00F7, 0x00B8, 155*da6c28aaSamw 0x00B0, 0x00A8, 0x00B7, 0x00B9, 0x00B3, 0x00B2, 0x25A0, 0x00A0 156*da6c28aaSamw }; 157*da6c28aaSamw 158*da6c28aaSamw 159*da6c28aaSamw 160*da6c28aaSamw /* 161*da6c28aaSamw * The oem_default_telnet_cp is the default telnet codepage for English. 162*da6c28aaSamw * It is actually codepage 1252. 163*da6c28aaSamw */ 164*da6c28aaSamw mts_wchar_t oem_default_telnet_cp[256] = { 165*da6c28aaSamw 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 166*da6c28aaSamw 0x9, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, 0x10, 167*da6c28aaSamw 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 168*da6c28aaSamw 0x19, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, 0x20, 169*da6c28aaSamw 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 170*da6c28aaSamw 0x29, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, 0x30, 171*da6c28aaSamw 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 172*da6c28aaSamw 0x39, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, 0x40, 173*da6c28aaSamw 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 174*da6c28aaSamw 0x49, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, 0x50, 175*da6c28aaSamw 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 176*da6c28aaSamw 0x59, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, 0x60, 177*da6c28aaSamw 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 178*da6c28aaSamw 0x69, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, 0x70, 179*da6c28aaSamw 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 180*da6c28aaSamw 0x79, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, 0x20AC, 181*da6c28aaSamw 0x81, 0x201A, 0x192, 0x201E, 0x2026, 0x2020, 0x2021, 0x02C6, 182*da6c28aaSamw 0x2030, 0x160, 0x2039, 0x152, 0x8D, 0x017D, 0x8F, 0x90, 183*da6c28aaSamw 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x02DC, 184*da6c28aaSamw 0x2122, 0x161, 0x203A, 0x153, 0x9D, 0x017E, 0x178, 0x00A0, 185*da6c28aaSamw 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7, 0x00A8, 186*da6c28aaSamw 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF, 0x00B0, 187*da6c28aaSamw 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7, 0x00B8, 188*da6c28aaSamw 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF, 0x00C0, 189*da6c28aaSamw 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7, 0x00C8, 190*da6c28aaSamw 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF, 0x00D0, 191*da6c28aaSamw 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7, 0x00D8, 192*da6c28aaSamw 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x00DE, 0x00DF, 0x00E0, 193*da6c28aaSamw 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7, 0x00E8, 194*da6c28aaSamw 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF, 0x00F0, 195*da6c28aaSamw 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7, 0x00F8, 196*da6c28aaSamw 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x00FF 197*da6c28aaSamw }; 198*da6c28aaSamw 199*da6c28aaSamw 200*da6c28aaSamw #define MAX_OEMPAGES (sizeof (oemcp_table) / sizeof (oemcp_table[0])) 201*da6c28aaSamw #define MAX_UNI_IDX 65536 202*da6c28aaSamw 203*da6c28aaSamw 204*da6c28aaSamw 205*da6c28aaSamw /* 206*da6c28aaSamw * oem_codepage_bytesperchar 207*da6c28aaSamw * 208*da6c28aaSamw * This function returns the max bytes per oem char for the specified 209*da6c28aaSamw * oem table. This basically shows if the oem codepage is single or 210*da6c28aaSamw * double bytes. 211*da6c28aaSamw */ 212*da6c28aaSamw static unsigned int 213*da6c28aaSamw oem_codepage_bytesperchar(unsigned int cpid) 214*da6c28aaSamw { 215*da6c28aaSamw if (cpid >= MAX_OEMPAGES) 216*da6c28aaSamw return (0); 217*da6c28aaSamw else 218*da6c28aaSamw return (oemcp_table[cpid].bytesperchar); 219*da6c28aaSamw } 220*da6c28aaSamw 221*da6c28aaSamw 222*da6c28aaSamw 223*da6c28aaSamw /* 224*da6c28aaSamw * oem_get_codepage_path 225*da6c28aaSamw * 226*da6c28aaSamw * This function will get the codepage path. 227*da6c28aaSamw */ 228*da6c28aaSamw const char * 229*da6c28aaSamw oem_get_codepage_path(void) 230*da6c28aaSamw { 231*da6c28aaSamw #ifdef PBSHORTCUT /* */ 232*da6c28aaSamw const char *path = getenv("codepage.oem.directory"); 233*da6c28aaSamw if (path == 0) 234*da6c28aaSamw return ("/"); 235*da6c28aaSamw else 236*da6c28aaSamw return (path); 237*da6c28aaSamw #else /* PBSHORTCUT */ 238*da6c28aaSamw return ("/"); 239*da6c28aaSamw #endif /* PBSHORTCUT */ 240*da6c28aaSamw } 241*da6c28aaSamw 242*da6c28aaSamw /* 243*da6c28aaSamw * oem_codepage_init 244*da6c28aaSamw * 245*da6c28aaSamw * This function will init oem page via the cpid of the oem table. 246*da6c28aaSamw * The function oem_codepage_free must be called when the oempage is 247*da6c28aaSamw * no longer needed to free up the allocated memory. If the codepage is 248*da6c28aaSamw * successfully initialized, zero will be the return value; otherwise 249*da6c28aaSamw * -1 will be the return value. 250*da6c28aaSamw */ 251*da6c28aaSamw int 252*da6c28aaSamw oem_codepage_init(unsigned int cpid) 253*da6c28aaSamw { 254*da6c28aaSamw #ifndef _KERNEL 255*da6c28aaSamw FILE *fp; 256*da6c28aaSamw static mutex_t mutex; 257*da6c28aaSamw char buf[32]; 258*da6c28aaSamw char filePath[100]; 259*da6c28aaSamw #endif /* _KERNEL */ 260*da6c28aaSamw unsigned int max_oem_index; 261*da6c28aaSamw const char *codepagePath = oem_get_codepage_path(); 262*da6c28aaSamw mts_wchar_t *default_oem_cp = 0; 263*da6c28aaSamw oem_codepage_t *oemcp; 264*da6c28aaSamw 265*da6c28aaSamw /* 266*da6c28aaSamw * The OEM codepages 850 and 1252 are stored in kernel; therefore, 267*da6c28aaSamw * no need for codepagePath to be defined to work. 268*da6c28aaSamw */ 269*da6c28aaSamw if (cpid >= MAX_OEMPAGES || 270*da6c28aaSamw (codepagePath == 0 && 271*da6c28aaSamw cpid != oem_default_smb_cpid && cpid != oem_default_telnet_cpid)) 272*da6c28aaSamw return (-1); 273*da6c28aaSamw 274*da6c28aaSamw max_oem_index = 1 << oem_codepage_bytesperchar(cpid) * 8; 275*da6c28aaSamw /* 276*da6c28aaSamw * Use mutex so no two same index can be initialize 277*da6c28aaSamw * at the same time. 278*da6c28aaSamw */ 279*da6c28aaSamw #ifndef _KERNEL 280*da6c28aaSamw (void) mutex_lock(&mutex); 281*da6c28aaSamw #endif /* _KERNEL */ 282*da6c28aaSamw 283*da6c28aaSamw oemcp = &oemcp_table[cpid]; 284*da6c28aaSamw if (oemcp->valid) { 285*da6c28aaSamw oemcp->valid++; 286*da6c28aaSamw #ifndef _KERNEL 287*da6c28aaSamw (void) mutex_unlock(&mutex); 288*da6c28aaSamw #endif /* _KERNEL */ 289*da6c28aaSamw return (0); 290*da6c28aaSamw } 291*da6c28aaSamw 292*da6c28aaSamw oemcp->oempage.value = 293*da6c28aaSamw MEM_ZALLOC("oem", max_oem_index * sizeof (mts_wchar_t)); 294*da6c28aaSamw if (oemcp->oempage.value == 0) { 295*da6c28aaSamw #ifndef _KERNEL 296*da6c28aaSamw (void) mutex_unlock(&mutex); 297*da6c28aaSamw #endif /* _KERNEL */ 298*da6c28aaSamw return (-1); 299*da6c28aaSamw } 300*da6c28aaSamw 301*da6c28aaSamw oemcp->unicodepage.value = 302*da6c28aaSamw MEM_ZALLOC("oem", MAX_UNI_IDX * sizeof (mts_wchar_t)); 303*da6c28aaSamw if (oemcp->unicodepage.value == 0) { 304*da6c28aaSamw MEM_FREE("oem", oemcp->oempage.value); 305*da6c28aaSamw oemcp->oempage.value = 0; 306*da6c28aaSamw #ifndef _KERNEL 307*da6c28aaSamw (void) mutex_unlock(&mutex); 308*da6c28aaSamw #endif /* _KERNEL */ 309*da6c28aaSamw return (-1); 310*da6c28aaSamw } 311*da6c28aaSamw 312*da6c28aaSamw /* 313*da6c28aaSamw * The default English page is stored in kernel. 314*da6c28aaSamw * Therefore, no need to go to codepage files. 315*da6c28aaSamw */ 316*da6c28aaSamw #ifndef _KERNEL 317*da6c28aaSamw if (cpid == oem_default_smb_cpid) 318*da6c28aaSamw default_oem_cp = oem_default_smb_cp; 319*da6c28aaSamw else if (cpid == oem_default_telnet_cpid) 320*da6c28aaSamw default_oem_cp = oem_default_telnet_cp; 321*da6c28aaSamw else 322*da6c28aaSamw default_oem_cp = 0; 323*da6c28aaSamw #else /* _KERNEL */ 324*da6c28aaSamw default_oem_cp = oem_default_smb_cp; 325*da6c28aaSamw #endif /* _KERNEL */ 326*da6c28aaSamw 327*da6c28aaSamw if (default_oem_cp) { 328*da6c28aaSamw int i; 329*da6c28aaSamw 330*da6c28aaSamw for (i = 0; i < max_oem_index; i++) { 331*da6c28aaSamw oemcp->oempage.value[i] = default_oem_cp[i]; 332*da6c28aaSamw oemcp->unicodepage.value[default_oem_cp[i]] = 333*da6c28aaSamw (mts_wchar_t)i; 334*da6c28aaSamw } 335*da6c28aaSamw #ifdef _KERNEL 336*da6c28aaSamw } 337*da6c28aaSamw /* 338*da6c28aaSamw * XXX This doesn't seem right. How do we handle the situation 339*da6c28aaSamw * where default_oem_cp == 0 in the kernel? 340*da6c28aaSamw * Is this a PBSHORTCUT? 341*da6c28aaSamw */ 342*da6c28aaSamw #else 343*da6c28aaSamw } else { 344*da6c28aaSamw 345*da6c28aaSamw /* 346*da6c28aaSamw * The codepage is not one of the default that stores 347*da6c28aaSamw * in the include 348*da6c28aaSamw * file; therefore, we need to read from the file. 349*da6c28aaSamw */ 350*da6c28aaSamw (void) snprintf(filePath, sizeof (filePath), 351*da6c28aaSamw "%s/%s", codepagePath, oemcp->filename); 352*da6c28aaSamw fp = fopen(filePath, "r"); 353*da6c28aaSamw 354*da6c28aaSamw if (fp == 0) { 355*da6c28aaSamw MEM_FREE("oem", oemcp->oempage.value); 356*da6c28aaSamw MEM_FREE("oem", oemcp->unicodepage.value); 357*da6c28aaSamw #ifndef _KERNEL 358*da6c28aaSamw (void) mutex_unlock(&mutex); 359*da6c28aaSamw #endif /* _KERNEL */ 360*da6c28aaSamw return (-1); 361*da6c28aaSamw } 362*da6c28aaSamw 363*da6c28aaSamw while (fgets(buf, 32, fp) != 0) { 364*da6c28aaSamw char *endptr; 365*da6c28aaSamw unsigned int oemval, unival; 366*da6c28aaSamw 367*da6c28aaSamw endptr = (char *)strchr(buf, ' '); 368*da6c28aaSamw if (endptr == 0) { 369*da6c28aaSamw continue; 370*da6c28aaSamw } 371*da6c28aaSamw 372*da6c28aaSamw oemval = strtol(buf, &endptr, 0); 373*da6c28aaSamw unival = strtol(endptr+1, 0, 0); 374*da6c28aaSamw 375*da6c28aaSamw if (oemval >= max_oem_index || unival >= MAX_UNI_IDX) { 376*da6c28aaSamw continue; 377*da6c28aaSamw } 378*da6c28aaSamw 379*da6c28aaSamw oemcp->oempage.value[oemval] = unival; 380*da6c28aaSamw oemcp->unicodepage.value[unival] = oemval; 381*da6c28aaSamw } 382*da6c28aaSamw (void) fclose(fp); 383*da6c28aaSamw } 384*da6c28aaSamw #endif /* _KERNEL */ 385*da6c28aaSamw 386*da6c28aaSamw oemcp->valid = 1; 387*da6c28aaSamw #ifndef _KERNEL 388*da6c28aaSamw (void) mutex_unlock(&mutex); 389*da6c28aaSamw #endif /* _KERNEL */ 390*da6c28aaSamw return (0); 391*da6c28aaSamw } 392*da6c28aaSamw 393*da6c28aaSamw 394*da6c28aaSamw 395*da6c28aaSamw 396*da6c28aaSamw /* 397*da6c28aaSamw * oem_codepage_free 398*da6c28aaSamw * 399*da6c28aaSamw * This function will clear the valid bit and free the memory 400*da6c28aaSamw * allocated to the oem/unipage by oem_codepage_init if the ref count 401*da6c28aaSamw * is zero. 402*da6c28aaSamw */ 403*da6c28aaSamw void 404*da6c28aaSamw oem_codepage_free(unsigned int cpid) 405*da6c28aaSamw { 406*da6c28aaSamw oem_codepage_t *oemcp; 407*da6c28aaSamw 408*da6c28aaSamw if (cpid >= MAX_OEMPAGES || !oemcp_table[cpid].valid) 409*da6c28aaSamw return; 410*da6c28aaSamw 411*da6c28aaSamw oemcp = &oemcp_table[cpid]; 412*da6c28aaSamw oemcp->valid--; 413*da6c28aaSamw 414*da6c28aaSamw if (oemcp->ref != 0 || oemcp->valid != 0) 415*da6c28aaSamw return; 416*da6c28aaSamw 417*da6c28aaSamw if (oemcp->oempage.value != 0) { 418*da6c28aaSamw MEM_FREE("oem", oemcp->oempage.value); 419*da6c28aaSamw oemcp->oempage.value = 0; 420*da6c28aaSamw } 421*da6c28aaSamw 422*da6c28aaSamw if (oemcp->unicodepage.value != 0) { 423*da6c28aaSamw MEM_FREE("oem", oemcp->unicodepage.value); 424*da6c28aaSamw oemcp->unicodepage.value = 0; 425*da6c28aaSamw } 426*da6c28aaSamw } 427*da6c28aaSamw 428*da6c28aaSamw 429*da6c28aaSamw 430*da6c28aaSamw /* 431*da6c28aaSamw * oem_get_oempage 432*da6c28aaSamw * 433*da6c28aaSamw * This function will return the current oempage and increment 434*da6c28aaSamw * the ref count. The function oem_release_page should always 435*da6c28aaSamw * be called when finish using the oempage to decrement the 436*da6c28aaSamw * ref count. 437*da6c28aaSamw */ 438*da6c28aaSamw static oempage_t * 439*da6c28aaSamw oem_get_oempage(unsigned int cpid) 440*da6c28aaSamw { 441*da6c28aaSamw if (cpid >= MAX_OEMPAGES) 442*da6c28aaSamw return (0); 443*da6c28aaSamw 444*da6c28aaSamw if (oemcp_table[cpid].valid) { 445*da6c28aaSamw oemcp_table[cpid].ref++; 446*da6c28aaSamw return (&oemcp_table[cpid].oempage); 447*da6c28aaSamw } 448*da6c28aaSamw return (0); 449*da6c28aaSamw } 450*da6c28aaSamw 451*da6c28aaSamw 452*da6c28aaSamw 453*da6c28aaSamw /* 454*da6c28aaSamw * oem_get_unipage 455*da6c28aaSamw * 456*da6c28aaSamw * This function will return the current unipage and increment 457*da6c28aaSamw * the ref count. The function oem_release_page should always 458*da6c28aaSamw * be called when finish using the unipage to decrement the 459*da6c28aaSamw * ref count. 460*da6c28aaSamw */ 461*da6c28aaSamw static oempage_t * 462*da6c28aaSamw oem_get_unipage(unsigned int cpid) 463*da6c28aaSamw { 464*da6c28aaSamw if (cpid >= MAX_OEMPAGES) 465*da6c28aaSamw return (0); 466*da6c28aaSamw 467*da6c28aaSamw if (oemcp_table[cpid].valid) { 468*da6c28aaSamw oemcp_table[cpid].ref++; 469*da6c28aaSamw return (&oemcp_table[cpid].unicodepage); 470*da6c28aaSamw } 471*da6c28aaSamw return (0); 472*da6c28aaSamw } 473*da6c28aaSamw 474*da6c28aaSamw 475*da6c28aaSamw 476*da6c28aaSamw /* 477*da6c28aaSamw * oem_release_page 478*da6c28aaSamw * 479*da6c28aaSamw * This function will decrement the ref count and check the valid 480*da6c28aaSamw * bit. It will free the memory allocated for the pages 481*da6c28aaSamw * if the 482*da6c28aaSamw * valid bit is not set, ref count is zero and the page is not 483*da6c28aaSamw * already freed. 484*da6c28aaSamw */ 485*da6c28aaSamw static void 486*da6c28aaSamw oem_release_page(oempage_t *page) 487*da6c28aaSamw { 488*da6c28aaSamw oem_codepage_t *oemcp = &oemcp_table[page->cpid]; 489*da6c28aaSamw 490*da6c28aaSamw page = 0; 491*da6c28aaSamw 492*da6c28aaSamw if (oemcp->ref > 0) 493*da6c28aaSamw oemcp->ref--; 494*da6c28aaSamw 495*da6c28aaSamw if (oemcp->ref != 0 || oemcp->valid) 496*da6c28aaSamw return; 497*da6c28aaSamw 498*da6c28aaSamw if (oemcp->oempage.value != 0) { 499*da6c28aaSamw MEM_FREE("oem", oemcp->oempage.value); 500*da6c28aaSamw oemcp->oempage.value = 0; 501*da6c28aaSamw } 502*da6c28aaSamw 503*da6c28aaSamw if (oemcp->unicodepage.value != 0) { 504*da6c28aaSamw MEM_FREE("oem", oemcp->unicodepage.value); 505*da6c28aaSamw oemcp->unicodepage.value = 0; 506*da6c28aaSamw } 507*da6c28aaSamw } 508*da6c28aaSamw 509*da6c28aaSamw 510*da6c28aaSamw 511*da6c28aaSamw /* 512*da6c28aaSamw * unicodestooems 513*da6c28aaSamw * 514*da6c28aaSamw * Convert unicode string to oem string. The function will stop 515*da6c28aaSamw * converting the unicode string when size nbytes - 1 is reached 516*da6c28aaSamw * or when there is not enough room to store another unicode. 517*da6c28aaSamw * If the function is called when the codepage is not initialized 518*da6c28aaSamw * or when the codepage initialize failed, it will return 0. 519*da6c28aaSamw * Otherwise, the total # of the converted unicode is returned. 520*da6c28aaSamw */ 521*da6c28aaSamw size_t 522*da6c28aaSamw unicodestooems( 523*da6c28aaSamw char *oemstring, 524*da6c28aaSamw const mts_wchar_t *unicodestring, 525*da6c28aaSamw size_t nbytes, 526*da6c28aaSamw unsigned int cpid) 527*da6c28aaSamw { 528*da6c28aaSamw oempage_t *unipage; 529*da6c28aaSamw unsigned int count = 0; 530*da6c28aaSamw mts_wchar_t oemchar; 531*da6c28aaSamw 532*da6c28aaSamw if (cpid >= MAX_OEMPAGES) 533*da6c28aaSamw return (0); 534*da6c28aaSamw 535*da6c28aaSamw if (unicodestring == 0 || oemstring == 0) 536*da6c28aaSamw return (0); 537*da6c28aaSamw 538*da6c28aaSamw if ((unipage = oem_get_unipage(cpid)) == 0) 539*da6c28aaSamw return (0); 540*da6c28aaSamw 541*da6c28aaSamw while ((oemchar = unipage->value[*unicodestring]) != 0) { 542*da6c28aaSamw if (oemchar & 0xff00 && nbytes >= MTS_MB_CHAR_MAX) { 543*da6c28aaSamw *oemstring++ = oemchar >> 8; 544*da6c28aaSamw *oemstring++ = (char)oemchar; 545*da6c28aaSamw nbytes -= 2; 546*da6c28aaSamw } else if (nbytes > 1) { 547*da6c28aaSamw *oemstring++ = (char)oemchar; 548*da6c28aaSamw nbytes--; 549*da6c28aaSamw } else 550*da6c28aaSamw break; 551*da6c28aaSamw 552*da6c28aaSamw count++; 553*da6c28aaSamw unicodestring++; 554*da6c28aaSamw } 555*da6c28aaSamw 556*da6c28aaSamw *oemstring = 0; 557*da6c28aaSamw 558*da6c28aaSamw oem_release_page(unipage); 559*da6c28aaSamw 560*da6c28aaSamw return (count); 561*da6c28aaSamw } 562*da6c28aaSamw 563*da6c28aaSamw 564*da6c28aaSamw 565*da6c28aaSamw /* 566*da6c28aaSamw * oemstounicodes 567*da6c28aaSamw * 568*da6c28aaSamw * Convert oem string to unicode string. The function will stop 569*da6c28aaSamw * converting the oem string when unicodestring len reaches nwchars - 1. 570*da6c28aaSamw * or when there is not enough room to store another oem char. 571*da6c28aaSamw * If the function is called when the codepage is not initialized 572*da6c28aaSamw * or when the codepage initialize failed, it will return 0. 573*da6c28aaSamw * Otherwise, the total # of the converted oem chars is returned. 574*da6c28aaSamw * The oem char can be either 1 or 2 bytes. 575*da6c28aaSamw */ 576*da6c28aaSamw size_t 577*da6c28aaSamw oemstounicodes( 578*da6c28aaSamw mts_wchar_t *unicodestring, 579*da6c28aaSamw const char *oemstring, 580*da6c28aaSamw size_t nwchars, 581*da6c28aaSamw unsigned int cpid) 582*da6c28aaSamw { 583*da6c28aaSamw oempage_t *oempage; 584*da6c28aaSamw size_t count = nwchars; 585*da6c28aaSamw mts_wchar_t oemchar; 586*da6c28aaSamw 587*da6c28aaSamw if (cpid >= MAX_OEMPAGES) 588*da6c28aaSamw return (0); 589*da6c28aaSamw 590*da6c28aaSamw if (unicodestring == 0 || oemstring == 0) 591*da6c28aaSamw return (0); 592*da6c28aaSamw 593*da6c28aaSamw if ((oempage = oem_get_oempage(cpid)) == 0) 594*da6c28aaSamw return (0); 595*da6c28aaSamw 596*da6c28aaSamw while ((oemchar = (mts_wchar_t)*oemstring++ & 0xff) != 0) { 597*da6c28aaSamw /* 598*da6c28aaSamw * Cannot find one byte oemchar in table. Must be 599*da6c28aaSamw * a lead byte. Try two bytes. 600*da6c28aaSamw */ 601*da6c28aaSamw 602*da6c28aaSamw if ((oempage->value[oemchar] == 0) && (oemchar != 0)) { 603*da6c28aaSamw oemchar = oemchar << 8 | (*oemstring++ & 0xff); 604*da6c28aaSamw if (oempage->value[oemchar] == 0) { 605*da6c28aaSamw *unicodestring = 0; 606*da6c28aaSamw break; 607*da6c28aaSamw } 608*da6c28aaSamw } 609*da6c28aaSamw #ifdef _BIG_ENDIAN 610*da6c28aaSamw *unicodestring = LE_IN16(&oempage->value[oemchar]); 611*da6c28aaSamw #else 612*da6c28aaSamw *unicodestring = oempage->value[oemchar]; 613*da6c28aaSamw #endif 614*da6c28aaSamw count--; 615*da6c28aaSamw unicodestring++; 616*da6c28aaSamw } 617*da6c28aaSamw 618*da6c28aaSamw *unicodestring = 0; 619*da6c28aaSamw 620*da6c28aaSamw oem_release_page(oempage); 621*da6c28aaSamw 622*da6c28aaSamw return (nwchars - count); 623*da6c28aaSamw } 624*da6c28aaSamw 625*da6c28aaSamw /* 626*da6c28aaSamw * oem_get_lang_table 627*da6c28aaSamw * 628*da6c28aaSamw * This function returns a pointer to the language table. 629*da6c28aaSamw */ 630*da6c28aaSamw language * 631*da6c28aaSamw oem_get_lang_table(void) 632*da6c28aaSamw { 633*da6c28aaSamw return (lang_table); 634*da6c28aaSamw } 635*da6c28aaSamw 636*da6c28aaSamw /* 637*da6c28aaSamw * oem_no_of_languages 638*da6c28aaSamw * 639*da6c28aaSamw * This function returns total languages support in the system. 640*da6c28aaSamw */ 641*da6c28aaSamw int 642*da6c28aaSamw oem_no_of_languages(void) 643*da6c28aaSamw { 644*da6c28aaSamw return (sizeof (lang_table)/sizeof (lang_table[0])); 645*da6c28aaSamw } 646*da6c28aaSamw 647*da6c28aaSamw 648*da6c28aaSamw #ifndef _KERNEL 649*da6c28aaSamw #if 1 650*da6c28aaSamw /* 651*da6c28aaSamw * TESTING Functions 652*da6c28aaSamw */ 653*da6c28aaSamw void 654*da6c28aaSamw oemcp_print(unsigned int cpid) 655*da6c28aaSamw { 656*da6c28aaSamw unsigned int bytesperchar, max_index, i; 657*da6c28aaSamw oempage_t *oempage, *unipage; 658*da6c28aaSamw unsigned int counter = 0; 659*da6c28aaSamw 660*da6c28aaSamw if (cpid >= MAX_OEMPAGES) { 661*da6c28aaSamw (void) printf("oemcp cpid %d is invalid\n", cpid); 662*da6c28aaSamw return; 663*da6c28aaSamw } 664*da6c28aaSamw 665*da6c28aaSamw if ((oempage = oem_get_oempage(cpid)) == 0) { 666*da6c28aaSamw (void) printf("oemcp of cpid %d is invalid\n", cpid); 667*da6c28aaSamw return; 668*da6c28aaSamw } 669*da6c28aaSamw 670*da6c28aaSamw if ((unipage = oem_get_unipage(cpid)) == 0) { 671*da6c28aaSamw (void) printf("unicp of cpid %d is invalid\n", cpid); 672*da6c28aaSamw return; 673*da6c28aaSamw } 674*da6c28aaSamw 675*da6c28aaSamw if ((bytesperchar = oem_codepage_bytesperchar(cpid)) == 0) { 676*da6c28aaSamw (void) printf("bytesperchar of cpid %d is not correct\n", cpid); 677*da6c28aaSamw return; 678*da6c28aaSamw } 679*da6c28aaSamw 680*da6c28aaSamw max_index = 1 << bytesperchar * 8; 681*da6c28aaSamw 682*da6c28aaSamw (void) printf("OEMPAGE:\n"); 683*da6c28aaSamw for (i = 0; i < max_index; i++) { 684*da6c28aaSamw if ((counter + 1) % 4 == 0 && 685*da6c28aaSamw (oempage->value[i] != 0 || i == 0)) { 686*da6c28aaSamw (void) printf("%x %x\n", i, oempage->value[i]); 687*da6c28aaSamw counter++; 688*da6c28aaSamw } else if (oempage->value[i] != 0 || i == 0) { 689*da6c28aaSamw (void) printf("%x %x, ", i, oempage->value[i]); 690*da6c28aaSamw counter++; 691*da6c28aaSamw } 692*da6c28aaSamw } 693*da6c28aaSamw counter = 0; 694*da6c28aaSamw (void) printf("\n\nUNIPAGE:\n"); 695*da6c28aaSamw for (i = 0; i < 65536; i++) { 696*da6c28aaSamw if ((counter + 1) % 8 == 0 && 697*da6c28aaSamw (unipage->value[i] != 0 || i == 0)) { 698*da6c28aaSamw (void) printf("%x %x\n", i, unipage->value[i]); 699*da6c28aaSamw counter++; 700*da6c28aaSamw } else if (unipage->value[i] != 0 || i == 0) { 701*da6c28aaSamw (void) printf("%x %x, ", i, unipage->value[i]); 702*da6c28aaSamw counter++; 703*da6c28aaSamw } 704*da6c28aaSamw } 705*da6c28aaSamw (void) printf("\n"); 706*da6c28aaSamw oem_release_page(oempage); 707*da6c28aaSamw oem_release_page(unipage); 708*da6c28aaSamw } 709*da6c28aaSamw 710*da6c28aaSamw 711*da6c28aaSamw 712*da6c28aaSamw void 713*da6c28aaSamw oemstringtest(unsigned int cpid) 714*da6c28aaSamw { 715*da6c28aaSamw unsigned char *c, *cbuf; 716*da6c28aaSamw unsigned char cbuf1[100] = {0xfe, 0xfd, 0xf2, 0xe9, 717*da6c28aaSamw 0x63, 0xce, 0xdb, 0x8c, 0x9c, 0x21, 0}; 718*da6c28aaSamw unsigned char cbuf2[100] = {0xfe, 0xfc, 0x63, 0x81, 0x42, 719*da6c28aaSamw 0x91, 0x40, 0x24, 0xff, 0x49}; 720*da6c28aaSamw mts_wchar_t buf[100], *wc; 721*da6c28aaSamw 722*da6c28aaSamw if (cpid == 1) 723*da6c28aaSamw cbuf = cbuf1; 724*da6c28aaSamw else if (cpid == 2) 725*da6c28aaSamw cbuf = cbuf2; 726*da6c28aaSamw 727*da6c28aaSamw /* 728*da6c28aaSamw * Before oem->uni conversion. 729*da6c28aaSamw */ 730*da6c28aaSamw (void) printf("Before oem->uni conversion: "); 731*da6c28aaSamw for (c = cbuf; *c != 0; c++) 732*da6c28aaSamw (void) printf("%x ", *c); 733*da6c28aaSamw (void) printf("\n"); 734*da6c28aaSamw 735*da6c28aaSamw /* 736*da6c28aaSamw * oem->uni conversion 737*da6c28aaSamw */ 738*da6c28aaSamw (void) oemstounicodes(buf, (const char *)cbuf, 100, cpid); 739*da6c28aaSamw 740*da6c28aaSamw /* 741*da6c28aaSamw * After oem->uni conversion. 742*da6c28aaSamw */ 743*da6c28aaSamw (void) printf("After oem->uni conversion: "); 744*da6c28aaSamw for (wc = buf; *wc != 0; wc++) 745*da6c28aaSamw (void) printf("%x ", *wc); 746*da6c28aaSamw (void) printf("\n"); 747*da6c28aaSamw 748*da6c28aaSamw /* 749*da6c28aaSamw * uni->oem conversion 750*da6c28aaSamw */ 751*da6c28aaSamw (void) unicodestooems((char *)cbuf, buf, 100, cpid); 752*da6c28aaSamw 753*da6c28aaSamw /* 754*da6c28aaSamw * After uni->oem conversion. 755*da6c28aaSamw */ 756*da6c28aaSamw (void) printf("After uni->oem conversion: "); 757*da6c28aaSamw for (c = cbuf; *c != 0; c++) 758*da6c28aaSamw (void) printf("%x ", *c); 759*da6c28aaSamw (void) printf("\n"); 760*da6c28aaSamw } 761*da6c28aaSamw #endif 762*da6c28aaSamw #endif /* _KERNEL */ 763