| 1 | /* $Id: unicode.cpp,v 1.8 1999-06-23 19:36:24 phaller Exp $ */
|
|---|
| 2 |
|
|---|
| 3 | /*
|
|---|
| 4 | * Project Odin Software License can be found in LICENSE.TXT
|
|---|
| 5 | * Unicode functions
|
|---|
| 6 | * Copyright 1998 Joel Troster
|
|---|
| 7 | * Copyright 1999 Patrick haller
|
|---|
| 8 | * Copyright 1999 Achim Hasenmueller
|
|---|
| 9 | */
|
|---|
| 10 | #include <os2win.h>
|
|---|
| 11 | #include <winnls.h>
|
|---|
| 12 | #include <stdlib.h>
|
|---|
| 13 | #include <string.h>
|
|---|
| 14 | #include "misc.h"
|
|---|
| 15 |
|
|---|
| 16 | static UconvObject uconv_object = NULL;
|
|---|
| 17 | BOOL getUconvObject( void )
|
|---|
| 18 | {
|
|---|
| 19 | int rc;
|
|---|
| 20 | BOOL ret;
|
|---|
| 21 | if ( uconv_object )
|
|---|
| 22 | ret = TRUE;
|
|---|
| 23 | else
|
|---|
| 24 | {
|
|---|
| 25 | rc = UniCreateUconvObject( (UniChar*)L"", &uconv_object );
|
|---|
| 26 | if ( rc == ULS_SUCCESS )
|
|---|
| 27 | ret = TRUE;
|
|---|
| 28 | else
|
|---|
| 29 | {
|
|---|
| 30 | uconv_object = NULL; // to make sure
|
|---|
| 31 | return FALSE;
|
|---|
| 32 | }
|
|---|
| 33 | dprintf(("UniCreateUconvObject(%d)\n", rc ));
|
|---|
| 34 | }
|
|---|
| 35 | return ret;
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | //******************************************************************************
|
|---|
| 39 | //Not identical, but close enough
|
|---|
| 40 | //******************************************************************************
|
|---|
| 41 | int WIN32API MultiByteToWideChar(UINT uCodePage, DWORD dwFlags, LPCSTR lpMultiByteStr,
|
|---|
| 42 | int cchMultiByte, LPWSTR lpWideCharStr, int cchWideChar)
|
|---|
| 43 | {
|
|---|
| 44 | int i, len;
|
|---|
| 45 |
|
|---|
| 46 | // dprintf(("MultiByteToWideChar %s\n", lpMultiByteStr));
|
|---|
| 47 |
|
|---|
| 48 | if((int)lpMultiByteStr == (int)lpWideCharStr) {//not allowed
|
|---|
| 49 | SetLastError(ERROR_INVALID_PARAMETER);
|
|---|
| 50 | return(FALSE);
|
|---|
| 51 | }
|
|---|
| 52 | if(cchWideChar == 0) {//return space required for conversion
|
|---|
| 53 | if(cchMultiByte == -1) cchMultiByte = strlen(lpMultiByteStr);
|
|---|
| 54 | return(cchMultiByte); //return length in wide chars
|
|---|
| 55 | }
|
|---|
| 56 | if(cchMultiByte == -1)
|
|---|
| 57 | cchMultiByte = strlen(lpMultiByteStr);
|
|---|
| 58 |
|
|---|
| 59 | len = min(cchWideChar, cchMultiByte);
|
|---|
| 60 | for(i=0;i<=len;i++) { //including 0 terminator
|
|---|
| 61 | lpWideCharStr[i] = lpMultiByteStr[i];
|
|---|
| 62 | }
|
|---|
| 63 | return(len);
|
|---|
| 64 | }
|
|---|
| 65 | //******************************************************************************
|
|---|
| 66 | //******************************************************************************
|
|---|
| 67 | //Not identical, close enough
|
|---|
| 68 | //Forget about characters that can't be mapped; just do it
|
|---|
| 69 | //******************************************************************************
|
|---|
| 70 | int WIN32API WideCharToMultiByte(UINT uCodePage, DWORD dwFlags, LPCWSTR lpWideCharStr,
|
|---|
| 71 | int cchWideChar, LPSTR lpMultiByteStr,
|
|---|
| 72 | int cchMultiByte, LPCSTR lpDefaultChar,
|
|---|
| 73 | LPBOOL lpfUsedDefaultChar)
|
|---|
| 74 | {
|
|---|
| 75 | int i, len;
|
|---|
| 76 |
|
|---|
| 77 | // dprintf(("WideCharToMultiByte\n"));
|
|---|
| 78 |
|
|---|
| 79 | if((int)lpMultiByteStr == (int)lpWideCharStr) {//not allowed
|
|---|
| 80 | SetLastError(ERROR_INVALID_PARAMETER);
|
|---|
| 81 | return(FALSE);
|
|---|
| 82 | }
|
|---|
| 83 | if(cchMultiByte == 0) {//return space required for conversion
|
|---|
| 84 | if(cchWideChar == -1) cchWideChar = UniStrlen((UniChar *)lpWideCharStr);
|
|---|
| 85 | return(cchWideChar);
|
|---|
| 86 | }
|
|---|
| 87 | if(cchWideChar == -1)
|
|---|
| 88 | cchWideChar = UniStrlen((UniChar*)lpWideCharStr);
|
|---|
| 89 |
|
|---|
| 90 | len = min(cchWideChar, cchMultiByte);
|
|---|
| 91 | for(i=0;i<=len;i++) { //including 0 terminator
|
|---|
| 92 | lpMultiByteStr[i] = (UCHAR)lpWideCharStr[i];
|
|---|
| 93 | }
|
|---|
| 94 | return(len);
|
|---|
| 95 | }
|
|---|
| 96 | //******************************************************************************
|
|---|
| 97 | //******************************************************************************
|
|---|
| 98 | BOOL WIN32API GetCPInfo(UINT uCodePage, CPINFO *lpCPInfo)
|
|---|
| 99 | {
|
|---|
| 100 | dprintf(("GetCPInfo (%d), not correctly implemented\n", uCodePage));
|
|---|
| 101 | memset(lpCPInfo, 0, sizeof(CPINFO));
|
|---|
| 102 | lpCPInfo->MaxCharSize = 1;
|
|---|
| 103 | lpCPInfo->DefaultChar[0] = 'a';
|
|---|
| 104 |
|
|---|
| 105 | return(TRUE);
|
|---|
| 106 | }
|
|---|
| 107 | //******************************************************************************
|
|---|
| 108 | //******************************************************************************
|
|---|
| 109 | int WIN32API UnicodeToAsciiN(WCHAR *ustring, char *astring, int unilen)
|
|---|
| 110 | {
|
|---|
| 111 | int i;
|
|---|
| 112 | int rc;
|
|---|
| 113 | size_t uni_chars_left;
|
|---|
| 114 | size_t out_bytes_left;
|
|---|
| 115 | size_t num_subs;
|
|---|
| 116 | UniChar * in_buf;
|
|---|
| 117 | char * out_buf;
|
|---|
| 118 |
|
|---|
| 119 | if(ustring == NULL)
|
|---|
| 120 | return(NULL);
|
|---|
| 121 |
|
|---|
| 122 | // dprintf(("KERNEL32: UnicodeToAsciiN\n"));
|
|---|
| 123 | if (getUconvObject())
|
|---|
| 124 | {
|
|---|
| 125 | uni_chars_left = unilen + 1;
|
|---|
| 126 | out_bytes_left = uni_chars_left;
|
|---|
| 127 | in_buf = (UniChar*)ustring;
|
|---|
| 128 | out_buf = astring;
|
|---|
| 129 | rc = UniUconvFromUcs(uconv_object,
|
|---|
| 130 | &in_buf, &uni_chars_left,
|
|---|
| 131 | (void**)&out_buf, &out_bytes_left,
|
|---|
| 132 | &num_subs);
|
|---|
| 133 | // dprintf(("KERNEL32: UnicodeToAsciiN(%d) '%s'\n", rc, astring ));
|
|---|
| 134 | }
|
|---|
| 135 | else
|
|---|
| 136 | /* idiots unicode conversion :) */
|
|---|
| 137 | for(i = 0; i < unilen; i++)
|
|---|
| 138 | astring[i] = (char)ustring[i];
|
|---|
| 139 |
|
|---|
| 140 | astring[unilen] = 0; // @@@PH: 1999/06/09 fix - always terminate string
|
|---|
| 141 |
|
|---|
| 142 | return(unilen);
|
|---|
| 143 | }
|
|---|
| 144 | //******************************************************************************
|
|---|
| 145 | //******************************************************************************
|
|---|
| 146 | int WIN32API UnicodeToAscii(WCHAR *ustring, char *astring)
|
|---|
| 147 | {
|
|---|
| 148 | /* forward to function with len parameter */
|
|---|
| 149 | return UnicodeToAsciiN(ustring, astring, UniStrlen((UniChar*)ustring));
|
|---|
| 150 | }
|
|---|
| 151 | //******************************************************************************
|
|---|
| 152 | //******************************************************************************
|
|---|
| 153 | char * WIN32API UnicodeToAsciiString(WCHAR *ustring)
|
|---|
| 154 | {
|
|---|
| 155 | char *astring;
|
|---|
| 156 |
|
|---|
| 157 | if(ustring == NULL) return(NULL);
|
|---|
| 158 |
|
|---|
| 159 | astring = (char *)malloc( 1 + UniStrlen((UniChar*)ustring) );
|
|---|
| 160 | UnicodeToAscii( ustring, astring );
|
|---|
| 161 | return(astring);
|
|---|
| 162 | }
|
|---|
| 163 | //******************************************************************************
|
|---|
| 164 | //SvL: 24-6-'97 - Added
|
|---|
| 165 | //******************************************************************************
|
|---|
| 166 | void WIN32API FreeAsciiString(char *astring)
|
|---|
| 167 | {
|
|---|
| 168 | if( astring )
|
|---|
| 169 | free( astring );
|
|---|
| 170 | }
|
|---|
| 171 | //******************************************************************************
|
|---|
| 172 | //******************************************************************************
|
|---|
| 173 | void WIN32API AsciiToUnicodeN(char *ascii, WCHAR *unicode, int asciilen)
|
|---|
| 174 | {
|
|---|
| 175 | int rc;
|
|---|
| 176 | int i;
|
|---|
| 177 | size_t uni_chars_left;
|
|---|
| 178 | size_t in_bytes_left;
|
|---|
| 179 | size_t num_subs;
|
|---|
| 180 | UniChar * out_buf;
|
|---|
| 181 | char * in_buf;
|
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 | dprintf(("KERNEL32: AsciiToUnicodeN(%s,%08xh)\n",
|
|---|
| 185 | ascii,
|
|---|
| 186 | unicode));
|
|---|
| 187 |
|
|---|
| 188 | /* @@@PH 98/06/07 */
|
|---|
| 189 | if ( (ascii == NULL) || /* garbage in, garbage out ! */
|
|---|
| 190 | (unicode == NULL) )
|
|---|
| 191 | return;
|
|---|
| 192 |
|
|---|
| 193 | // dprintf(("KERNEL32: AsciiToUnicodeN %s\n", ascii));
|
|---|
| 194 | if (getUconvObject())
|
|---|
| 195 | {
|
|---|
| 196 | in_buf = ascii;
|
|---|
| 197 | in_bytes_left = asciilen;
|
|---|
| 198 | out_buf = (UniChar*)unicode;
|
|---|
| 199 |
|
|---|
| 200 | uni_chars_left = in_bytes_left +1;
|
|---|
| 201 | dprintf(("KERNEL32: AsciiToUnicode %d\n", in_bytes_left));
|
|---|
| 202 |
|
|---|
| 203 | rc = UniUconvToUcs( uconv_object,
|
|---|
| 204 | (void**)&in_buf, &in_bytes_left,
|
|---|
| 205 | &out_buf, &uni_chars_left,
|
|---|
| 206 | &num_subs );
|
|---|
| 207 | }
|
|---|
| 208 | else
|
|---|
| 209 | {
|
|---|
| 210 | for(i=0;
|
|---|
| 211 | i < asciilen;
|
|---|
| 212 | i++)
|
|---|
| 213 | unicode[i] = ascii[i];
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | unicode[asciilen] = 0;
|
|---|
| 217 | //SvL: Messes up the heap
|
|---|
| 218 | // unicode[len+1] = 0; /* @@@PH 98/06/07 */
|
|---|
| 219 | }
|
|---|
| 220 | //******************************************************************************
|
|---|
| 221 | //******************************************************************************
|
|---|
| 222 | void WIN32API AsciiToUnicode(char *ascii, WCHAR *unicode)
|
|---|
| 223 | {
|
|---|
| 224 | /* achimha for security, strlen might trap if garbage in */
|
|---|
| 225 | /* @@@PH 98/06/07 */
|
|---|
| 226 | if ( (ascii == NULL) || /* garbage in, garbage out ! */
|
|---|
| 227 | (unicode == NULL) )
|
|---|
| 228 | return;
|
|---|
| 229 | /* forward to call with length parameter */
|
|---|
| 230 | AsciiToUnicodeN(ascii, unicode, strlen(ascii));
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|