source: trunk/src/kernel32/unicode.cpp@ 571

Last change on this file since 571 was 571, checked in by sandervl, 26 years ago

PE loader resource fixes + ConvertNameId bugfix

File size: 9.5 KB
Line 
1/* $Id: unicode.cpp,v 1.13 1999-08-19 10:25:27 sandervl 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 * Copyright 1999 Christoph Bratschi
10 *
11 * Read comments about the implementation before using these functions
12 * Attention: length values include terminating 0
13 */
14#include <os2win.h>
15#include <winnls.h>
16#include <stdlib.h>
17#include <string.h>
18#include "misc.h"
19#include <unicode.h>
20
21static UconvObject uconv_object = NULL;
22BOOL getUconvObject( void )
23{
24 int rc;
25 BOOL ret;
26 if ( uconv_object )
27 ret = TRUE;
28 else
29 {
30 rc = UniCreateUconvObject( (UniChar*)L"", &uconv_object );
31 if ( rc == ULS_SUCCESS )
32 ret = TRUE;
33 else
34 {
35 uconv_object = NULL; // to make sure
36 return FALSE;
37 }
38 dprintf(("UniCreateUconvObject(%d)\n", rc ));
39 }
40 return ret;
41}
42
43//******************************************************************************
44//Not identical, but close enough
45//******************************************************************************
46int WIN32API MultiByteToWideChar(UINT uCodePage, DWORD dwFlags, LPCSTR lpMultiByteStr,
47 int cchMultiByte, LPWSTR lpWideCharStr, int cchWideChar)
48{
49 int i, len;
50
51// dprintf(("MultiByteToWideChar %s\n", lpMultiByteStr));
52
53 if((int)lpMultiByteStr == (int)lpWideCharStr) {//not allowed
54 SetLastError(ERROR_INVALID_PARAMETER);
55 return(FALSE);
56 }
57 if(cchWideChar == 0) {//return space required for conversion
58 if(cchMultiByte == -1) cchMultiByte = strlen(lpMultiByteStr);
59 return(cchMultiByte); //return length in wide chars
60 }
61 if(cchMultiByte == -1)
62 cchMultiByte = strlen(lpMultiByteStr);
63
64 len = min(cchWideChar, cchMultiByte);
65 for(i=0;i<=len;i++) { //including 0 terminator
66 lpWideCharStr[i] = lpMultiByteStr[i];
67 }
68 return(len);
69}
70//******************************************************************************
71//******************************************************************************
72//Not identical, close enough
73//Forget about characters that can't be mapped; just do it
74//******************************************************************************
75int WIN32API WideCharToMultiByte(UINT uCodePage, DWORD dwFlags, LPCWSTR lpWideCharStr,
76 int cchWideChar, LPSTR lpMultiByteStr,
77 int cchMultiByte, LPCSTR lpDefaultChar,
78 LPBOOL lpfUsedDefaultChar)
79{
80 int i, len;
81
82// dprintf(("WideCharToMultiByte\n"));
83
84 if((int)lpMultiByteStr == (int)lpWideCharStr) {//not allowed
85 SetLastError(ERROR_INVALID_PARAMETER);
86 return(FALSE);
87 }
88 if(cchMultiByte == 0) {//return space required for conversion
89 if(cchWideChar == -1) cchWideChar = UniStrlen((UniChar *)lpWideCharStr);
90 return(cchWideChar);
91 }
92 if(cchWideChar == -1)
93 cchWideChar = UniStrlen((UniChar*)lpWideCharStr);
94
95 len = min(cchWideChar, cchMultiByte);
96 for(i=0;i<=len;i++) { //including 0 terminator
97 lpMultiByteStr[i] = (UCHAR)lpWideCharStr[i];
98 }
99 return(len);
100}
101//******************************************************************************
102//******************************************************************************
103BOOL WIN32API GetCPInfo(UINT uCodePage, CPINFO *lpCPInfo)
104{
105 dprintf(("GetCPInfo (%d), not correctly implemented\n", uCodePage));
106 memset(lpCPInfo, 0, sizeof(CPINFO));
107 lpCPInfo->MaxCharSize = 1;
108 lpCPInfo->DefaultChar[0] = 'a';
109
110 return(TRUE);
111}
112//******************************************************************************
113// unilen: length of astring buffer (including 0 terminator)
114// returns string length
115//******************************************************************************
116int WIN32API UnicodeToAsciiN(WCHAR *ustring, char *astring, int unilen)
117{
118 int i;
119 int rc;
120 size_t uni_chars_left;
121 size_t out_bytes_left;
122 size_t num_subs;
123 UniChar * in_buf;
124 char * out_buf;
125
126 if (ustring == NULL)
127 {
128 if (astring != NULL && unilen > 0) astring[0] = 0;
129 return 0;
130 }
131
132 if (astring == NULL || unilen <= 0) return 0;
133
134// dprintf(("KERNEL32: UnicodeToAsciiN\n"));
135 if (getUconvObject())
136 {
137 if (unilen == 1)
138 {
139 astring[0] = 0;
140 return 0; //no data
141 }
142
143 uni_chars_left = unilen-1; //elements
144 out_bytes_left = uni_chars_left; //size in bytes == elements
145 in_buf = (UniChar*)ustring;
146 out_buf = astring;
147 rc = UniUconvFromUcs(uconv_object,
148 &in_buf, &uni_chars_left,
149 (void**)&out_buf, &out_bytes_left,
150 &num_subs);
151
152 unilen -= 1+out_bytes_left; //end + left bytes
153 astring[unilen] = 0; //terminate
154
155 return unilen;
156
157// dprintf(("KERNEL32: UnicodeToAsciiN(%d) '%s'\n", rc, astring ));
158 } else
159 {
160 /* idiots unicode conversion :) */
161 for (i = 0; i < unilen-1; i++)
162 {
163 astring[i] = (ustring[i] > 255) ? (char)20 : (char)ustring[i]; //CB: handle invalid characters as space
164 if (ustring[i] == 0) return i; //asta la vista, baby
165 }
166
167 astring[unilen-1] = 0; // @@@PH: 1999/06/09 fix - always terminate string
168
169 return(unilen-1);
170 }
171}
172//******************************************************************************
173// Converts unicode string to ascii string
174// returns length of ascii string
175//******************************************************************************
176int WIN32API UnicodeToAscii(WCHAR *ustring, char *astring)
177{
178 /* forward to function with len parameter */
179 return UnicodeToAsciiN(ustring, astring, UniStrlen((UniChar*)ustring)+1); //end included
180}
181//******************************************************************************
182// Converts unicode string to ascii string
183// returns pointer to ascii string
184//******************************************************************************
185char * WIN32API UnicodeToAsciiString(WCHAR *ustring)
186{
187 char *astring;
188
189 if(ustring == NULL) return(NULL);
190
191 astring = (char *)malloc( 1 + UniStrlen((UniChar*)ustring) );
192 UnicodeToAscii( ustring, astring );
193 return(astring);
194}
195//******************************************************************************
196//******************************************************************************
197char * WIN32API UnicodeToAsciiStringN(WCHAR *ustring, ULONG length)
198{
199 char *astring;
200
201 if(ustring == NULL) return(NULL);
202
203 astring = (char *)malloc( 1 + length );
204 UnicodeToAscii( ustring, astring );
205 return(astring);
206}
207//******************************************************************************
208// Converts ascii string to unicode string
209// returns pointer to unicode string
210//******************************************************************************
211WCHAR * WIN32API AsciiToUnicodeString(char *astring)
212{
213 WCHAR *ustring;
214
215 if(astring == NULL)
216 return(NULL);
217
218 ustring = (WCHAR *)malloc( 1 + strlen(astring) << 1 );
219 AsciiToUnicode( astring, ustring );
220 return(ustring);
221}
222
223//******************************************************************************
224//SvL: 24-6-'97 - Added
225//******************************************************************************
226void WIN32API FreeAsciiString(char *astring)
227{
228 if( astring )
229 free( astring );
230}
231//******************************************************************************
232// asciilen: max length of unicode buffer (including end 0)
233//******************************************************************************
234void WIN32API AsciiToUnicodeN(char *ascii, WCHAR *unicode, int asciilen)
235{
236 int rc;
237 int i;
238 size_t uni_chars_left;
239 size_t in_bytes_left;
240 size_t num_subs;
241 UniChar * out_buf;
242 char * in_buf;
243
244
245 dprintf(("KERNEL32: AsciiToUnicodeN(%s,%08xh)\n",
246 ascii,
247 unicode));
248
249 //CB: no input, set at least terminator
250 if (ascii == NULL)
251 {
252 if (unicode != NULL && asciilen > 0) unicode[0] = 0;
253 return;
254 }
255
256 if (unicode == NULL || asciilen <= 0) return; //nothing to do
257
258// dprintf(("KERNEL32: AsciiToUnicodeN %s\n", ascii));
259 if (getUconvObject())
260 {
261 if (asciilen == 1)
262 {
263 unicode[0] = 0;
264 return;
265 }
266
267 in_buf = ascii;
268 in_bytes_left = asciilen-1; //buffer size in bytes
269 out_buf = (UniChar*)unicode;
270
271 uni_chars_left = in_bytes_left; //elements
272 dprintf(("KERNEL32: AsciiToUnicode %d\n", in_bytes_left));
273
274 rc = UniUconvToUcs( uconv_object,
275 (void**)&in_buf, &in_bytes_left,
276 &out_buf, &uni_chars_left,
277 &num_subs );
278
279 unicode[asciilen-1-in_bytes_left] = 0;
280
281 //if (rc != ULS_SUCCESS && in_bytes_left > 0) //CB: never the case during my tests
282 // dprintf(("KERNEL32: AsciiToUnicode failed, %d bytes left!\n",in_bytes_left));
283
284 } else
285 { //poor man's conversion
286
287 for(i = 0;i < asciilen-1;i++)
288 {
289 unicode[i] = ascii[i];
290 if (ascii[i] == 0) return; //work done
291 }
292
293 unicode[asciilen-1] = 0;
294 }
295}
296//******************************************************************************
297// Copies the full string from ascii to unicode
298//******************************************************************************
299void WIN32API AsciiToUnicode(char *ascii, WCHAR *unicode)
300{
301 /* achimha for security, strlen might trap if garbage in */
302 /* @@@PH 98/06/07 */
303 if (ascii == NULL)
304 {
305 if (unicode != NULL) unicode[0] = 0; //CB: set at least end
306 return;
307 }
308
309 if (unicode == NULL) return; /* garbage in, garbage out ! */
310
311 /* forward to call with length parameter */
312 AsciiToUnicodeN(ascii, unicode, strlen(ascii)+1); //end included
313}
314
315
316
317
Note: See TracBrowser for help on using the repository browser.