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