1 | /* $Id: unicode.cpp,v 1.25 2001-04-03 14:10:48 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 "codepage.h"
|
---|
20 | #include <unicode.h>
|
---|
21 |
|
---|
22 | #define DBG_LOCALLOG DBG_unicode
|
---|
23 | #include "dbglocal.h"
|
---|
24 |
|
---|
25 | /*static UconvObject uconv_object = NULL;*/
|
---|
26 | /*
|
---|
27 | BOOL getUconvObject( void )
|
---|
28 | {
|
---|
29 | int rc;
|
---|
30 | BOOL ret;
|
---|
31 | if ( uconv_object )
|
---|
32 | ret = TRUE;
|
---|
33 | else
|
---|
34 | {
|
---|
35 | rc = UniCreateUconvObject( (UniChar*)L"", &uconv_object );
|
---|
36 | if ( rc == ULS_SUCCESS )
|
---|
37 | ret = TRUE;
|
---|
38 | else
|
---|
39 | {
|
---|
40 | uconv_object = NULL; // to make sure
|
---|
41 | return FALSE;
|
---|
42 | }
|
---|
43 | dprintf(("UniCreateUconvObject(%d)\n", rc ));
|
---|
44 | }
|
---|
45 | return ret;
|
---|
46 | }
|
---|
47 | */
|
---|
48 | //******************************************************************************
|
---|
49 | //******************************************************************************
|
---|
50 | // unilen: length of astring buffer (including 0 terminator)
|
---|
51 | // returns string length
|
---|
52 | //******************************************************************************
|
---|
53 | int UnicodeToCodepageN(LPCWSTR ustring, char *astring, int unilen, UconvObject uconv_object)
|
---|
54 | {
|
---|
55 | int i;
|
---|
56 | int rc, length;
|
---|
57 | size_t uni_chars_left;
|
---|
58 | size_t out_bytes_left;
|
---|
59 | size_t num_subs;
|
---|
60 | UniChar * in_buf;
|
---|
61 | char * out_buf;
|
---|
62 |
|
---|
63 | if (ustring == NULL)
|
---|
64 | {
|
---|
65 | if (astring != NULL && unilen > 0) astring[0] = 0;
|
---|
66 | return 0;
|
---|
67 | }
|
---|
68 |
|
---|
69 | if (astring == NULL || unilen <= 0) return 0;
|
---|
70 |
|
---|
71 | // dprintf(("KERNEL32: UnicodeToAsciiN\n"));
|
---|
72 |
|
---|
73 | // length = UniStrlen(ustring)+1;
|
---|
74 | // unilen = min(length, unilen);
|
---|
75 |
|
---|
76 | if (uconv_object)
|
---|
77 | {
|
---|
78 | if (unilen == 1)
|
---|
79 | {
|
---|
80 | astring[0] = 0;
|
---|
81 | return 0; //no data
|
---|
82 | }
|
---|
83 |
|
---|
84 | uni_chars_left = unilen-1; //elements
|
---|
85 | out_bytes_left = uni_chars_left; //size in bytes == elements
|
---|
86 | in_buf = (UniChar*)ustring;
|
---|
87 | out_buf = astring;
|
---|
88 | rc = UniUconvFromUcs(uconv_object,
|
---|
89 | &in_buf, &uni_chars_left,
|
---|
90 | (void**)&out_buf, &out_bytes_left,
|
---|
91 | &num_subs);
|
---|
92 |
|
---|
93 | unilen -= 1+out_bytes_left; //end + left bytes
|
---|
94 | astring[unilen] = 0; //terminate
|
---|
95 |
|
---|
96 | return unilen;
|
---|
97 |
|
---|
98 | // dprintf(("KERNEL32: UnicodeToAsciiN(%d) '%s'\n", rc, astring ));
|
---|
99 | } else
|
---|
100 | {
|
---|
101 | /* idiots unicode conversion :) */
|
---|
102 | for (i = 0; i < unilen-1; i++)
|
---|
103 | {
|
---|
104 | astring[i] = (ustring[i] > 255) ? (char)20 : (char)ustring[i]; //CB: handle invalid characters as space
|
---|
105 | if (ustring[i] == 0) return i; //asta la vista, baby
|
---|
106 | }
|
---|
107 |
|
---|
108 | astring[unilen-1] = 0; // @@@PH: 1999/06/09 fix - always terminate string
|
---|
109 |
|
---|
110 | return(unilen-1);
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | int WIN32API UnicodeToAsciiN(LPCWSTR ustring, char *astring, int unilen)
|
---|
115 | {
|
---|
116 | return UnicodeToCodepageN(ustring, astring, unilen, GetWindowsUconvObject());
|
---|
117 | }
|
---|
118 |
|
---|
119 | //******************************************************************************
|
---|
120 | // Converts unicode string to ascii string
|
---|
121 | // returns length of ascii string
|
---|
122 | //******************************************************************************
|
---|
123 | int WIN32API UnicodeToAscii(LPCWSTR ustring, char *astring)
|
---|
124 | {
|
---|
125 | /* forward to function with len parameter */
|
---|
126 | return UnicodeToAsciiN(ustring, astring, UniStrlen((UniChar*)ustring)+1); //end included
|
---|
127 | }
|
---|
128 | //******************************************************************************
|
---|
129 | // Converts unicode string to ascii string
|
---|
130 | // returns pointer to ascii string
|
---|
131 | //******************************************************************************
|
---|
132 | char * WIN32API UnicodeToAsciiString(LPCWSTR ustring)
|
---|
133 | {
|
---|
134 | char *astring;
|
---|
135 |
|
---|
136 | if(ustring == NULL) return(NULL);
|
---|
137 |
|
---|
138 | astring = (char *)malloc( 1 + UniStrlen((UniChar*)ustring) );
|
---|
139 | UnicodeToAscii( ustring, astring );
|
---|
140 | return(astring);
|
---|
141 | }
|
---|
142 | //******************************************************************************
|
---|
143 | // length = characters to convert
|
---|
144 | //******************************************************************************
|
---|
145 | char * WIN32API UnicodeToAsciiStringN(LPCWSTR ustring, ULONG length)
|
---|
146 | {
|
---|
147 | char *astring;
|
---|
148 |
|
---|
149 | if(ustring == NULL) return(NULL);
|
---|
150 |
|
---|
151 | astring = (char *)malloc( 1 + length );
|
---|
152 | UnicodeToAsciiN( ustring, astring, length+1 ); //terminating 0 always added
|
---|
153 | return(astring);
|
---|
154 | }
|
---|
155 | //******************************************************************************
|
---|
156 | // Converts ascii string to unicode string
|
---|
157 | // returns pointer to unicode string
|
---|
158 | //******************************************************************************
|
---|
159 | WCHAR * WIN32API AsciiToUnicodeString(const char *astring)
|
---|
160 | {
|
---|
161 | WCHAR *ustring;
|
---|
162 |
|
---|
163 | if(astring == NULL)
|
---|
164 | return(NULL);
|
---|
165 |
|
---|
166 | ustring = (WCHAR *)malloc( 1 + strlen(astring) << 1 );
|
---|
167 | AsciiToUnicode( astring, ustring );
|
---|
168 | return(ustring);
|
---|
169 | }
|
---|
170 |
|
---|
171 | //******************************************************************************
|
---|
172 | //SvL: 24-6-'97 - Added
|
---|
173 | //******************************************************************************
|
---|
174 | void WIN32API FreeAsciiString(char *astring)
|
---|
175 | {
|
---|
176 | if( astring )
|
---|
177 | free( astring );
|
---|
178 | }
|
---|
179 | //******************************************************************************
|
---|
180 | // asciilen: max length of unicode buffer (including end 0)
|
---|
181 | //******************************************************************************
|
---|
182 | void CodepageToUnicodeN(const char *ascii, WCHAR *unicode, int asciilen, UconvObject uconv_object)
|
---|
183 | {
|
---|
184 | int rc;
|
---|
185 | int i;
|
---|
186 | size_t uni_chars_left;
|
---|
187 | size_t in_bytes_left;
|
---|
188 | size_t num_subs;
|
---|
189 | UniChar * out_buf;
|
---|
190 | const char * in_buf;
|
---|
191 |
|
---|
192 |
|
---|
193 | dprintf(("KERNEL32: AsciiToUnicodeN(%s,%08xh)\n",
|
---|
194 | ascii,
|
---|
195 | unicode));
|
---|
196 |
|
---|
197 | //CB: no input, set at least terminator
|
---|
198 | if (ascii == NULL)
|
---|
199 | {
|
---|
200 | if (unicode != NULL && asciilen > 0) unicode[0] = 0;
|
---|
201 | return;
|
---|
202 | }
|
---|
203 |
|
---|
204 | if (unicode == NULL || asciilen <= 0) return; //nothing to do
|
---|
205 |
|
---|
206 | // dprintf(("KERNEL32: AsciiToUnicodeN %s\n", ascii));
|
---|
207 | if (uconv_object)
|
---|
208 | {
|
---|
209 | if (asciilen == 1)
|
---|
210 | {
|
---|
211 | unicode[0] = 0;
|
---|
212 | return;
|
---|
213 | }
|
---|
214 |
|
---|
215 | in_buf = ascii;
|
---|
216 | in_bytes_left = asciilen-1; //buffer size in bytes
|
---|
217 | out_buf = (UniChar*)unicode;
|
---|
218 |
|
---|
219 | uni_chars_left = in_bytes_left; //elements
|
---|
220 | dprintf2(("KERNEL32: AsciiToUnicode %d\n", in_bytes_left));
|
---|
221 |
|
---|
222 | rc = UniUconvToUcs( uconv_object,
|
---|
223 | (void**)&in_buf, &in_bytes_left,
|
---|
224 | &out_buf, &uni_chars_left,
|
---|
225 | &num_subs );
|
---|
226 |
|
---|
227 | unicode[asciilen-1-in_bytes_left] = 0;
|
---|
228 |
|
---|
229 | //if (rc != ULS_SUCCESS && in_bytes_left > 0) //CB: never the case during my tests
|
---|
230 | // dprintf(("KERNEL32: AsciiToUnicode failed, %d bytes left!\n",in_bytes_left));
|
---|
231 |
|
---|
232 | } else
|
---|
233 | { //poor man's conversion
|
---|
234 |
|
---|
235 | for(i = 0;i < asciilen-1;i++)
|
---|
236 | {
|
---|
237 | unicode[i] = ascii[i];
|
---|
238 | if (ascii[i] == 0) return; //work done
|
---|
239 | }
|
---|
240 |
|
---|
241 | unicode[asciilen-1] = 0;
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 | void WIN32API AsciiToUnicodeN(const char *ascii, WCHAR *unicode, int asciilen)
|
---|
246 | {
|
---|
247 | CodepageToUnicodeN(ascii, unicode, asciilen, GetWindowsUconvObject());
|
---|
248 | }
|
---|
249 |
|
---|
250 |
|
---|
251 | //******************************************************************************
|
---|
252 | // Copies the full string from ascii to unicode
|
---|
253 | //******************************************************************************
|
---|
254 | void WIN32API AsciiToUnicode(const char *ascii, WCHAR *unicode)
|
---|
255 | {
|
---|
256 | /* achimha for security, strlen might trap if garbage in */
|
---|
257 | /* @@@PH 98/06/07 */
|
---|
258 | if (ascii == NULL)
|
---|
259 | {
|
---|
260 | if (unicode != NULL) unicode[0] = 0; //CB: set at least end
|
---|
261 | return;
|
---|
262 | }
|
---|
263 |
|
---|
264 | if (unicode == NULL) return; /* garbage in, garbage out ! */
|
---|
265 |
|
---|
266 | /* forward to call with length parameter */
|
---|
267 | AsciiToUnicodeN(ascii, unicode, strlen(ascii)+1); //end included
|
---|
268 | }
|
---|
269 |
|
---|
270 |
|
---|
271 |
|
---|
272 |
|
---|
273 |
|
---|