1 | /* $Id: misc.cpp,v 1.1 1999-09-06 02:20:07 bird Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * PE2LX ascii to unicode conversion
|
---|
5 | *
|
---|
6 | * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
7 | *
|
---|
8 | *
|
---|
9 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
10 | *
|
---|
11 | */
|
---|
12 | /*******************************************************************************
|
---|
13 | * Header Files *
|
---|
14 | *******************************************************************************/
|
---|
15 | #include "pe2lx.h"
|
---|
16 | #ifndef RING0
|
---|
17 | #include <uniconv.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | /*******************************************************************************
|
---|
21 | * Internal Functions *
|
---|
22 | *******************************************************************************/
|
---|
23 | #ifndef RING0
|
---|
24 | static BOOL prepareCP(int cp);
|
---|
25 | static void convertCP(int cp, char *str);
|
---|
26 | #endif
|
---|
27 |
|
---|
28 |
|
---|
29 | //******************************************************************************
|
---|
30 | //******************************************************************************
|
---|
31 | char *UnicodeToAscii(int length, WCHAR *wstring, int cp)
|
---|
32 | {
|
---|
33 | static char *astring = NULL;
|
---|
34 | static int lenas = 0;
|
---|
35 |
|
---|
36 | int i;
|
---|
37 | int len;
|
---|
38 |
|
---|
39 | if (length < 0x10000) //one have to set some limits
|
---|
40 | {
|
---|
41 | if ((ULONG)wstring > MINPTR && (ULONG)wstring < MAXPTR)
|
---|
42 | {
|
---|
43 | if (length >= lenas)
|
---|
44 | {
|
---|
45 | char *p = (char*)realloc(astring,lenas = max(length+0x20,0x100));
|
---|
46 | if (p == NULL)
|
---|
47 | return "";
|
---|
48 | astring = p;
|
---|
49 | }
|
---|
50 |
|
---|
51 | for (i=0;i < length;i++)
|
---|
52 | astring[i] = (UCHAR)wstring[i];
|
---|
53 |
|
---|
54 | if (lenas-i > 0)
|
---|
55 | astring[i] = '\0';
|
---|
56 | #ifndef RING0
|
---|
57 | convertCP(cp, asciistring);
|
---|
58 | #else
|
---|
59 | cp = cp;
|
---|
60 | #endif
|
---|
61 | }
|
---|
62 | else
|
---|
63 | {
|
---|
64 | kprintf(("UnicodeToAscii: wstring is an invalid pointer, %p\n", wstring));
|
---|
65 | return "";
|
---|
66 | }
|
---|
67 | }
|
---|
68 | else
|
---|
69 | {
|
---|
70 | kprintf(("UnicodeToAscii: String requested is too long.(%d)\n",length));
|
---|
71 | return "";
|
---|
72 | }
|
---|
73 | return (astring);
|
---|
74 | }
|
---|
75 | //******************************************************************************
|
---|
76 | //******************************************************************************
|
---|
77 | char *UnicodeToAscii(WCHAR *wstring, int cp /*=0*/)
|
---|
78 | {
|
---|
79 | static char *astring = NULL;
|
---|
80 | static int lenas = 0;
|
---|
81 |
|
---|
82 | int i;
|
---|
83 | int len;
|
---|
84 |
|
---|
85 | //astring[0] = '\0'; //returns an empty string on error
|
---|
86 | if ((ULONG)wstring > MINPTR && (ULONG)wstring < MAXPTR)
|
---|
87 | {
|
---|
88 | len = UniStrlen(wstring);
|
---|
89 | if (len+1 >= lenas)
|
---|
90 | {
|
---|
91 | char *p = (char*)realloc(astring, lenas = max(len+0x20,0x100));
|
---|
92 | if (p == NULL)
|
---|
93 | return "";
|
---|
94 | astring = p;
|
---|
95 | }
|
---|
96 |
|
---|
97 | for (i=0; i < len; i++)
|
---|
98 | astring[i] = (UCHAR)wstring[i];
|
---|
99 |
|
---|
100 | if (lenas-i > 0)
|
---|
101 | astring[i] = '\0';
|
---|
102 | #ifndef RING0
|
---|
103 | convertCP(cp, astring);
|
---|
104 | #endif
|
---|
105 | }
|
---|
106 | else
|
---|
107 | {
|
---|
108 | kprintf(("UnicodeToAscii: wstring is an invalid pointer, %p\n", wstring));
|
---|
109 | return ("");
|
---|
110 | }
|
---|
111 |
|
---|
112 | return (astring);
|
---|
113 | }
|
---|
114 | //******************************************************************************
|
---|
115 | //******************************************************************************
|
---|
116 | int UniStrlen(WCHAR *wstring)
|
---|
117 | {
|
---|
118 | int i = 0;
|
---|
119 | ltassert((ULONG)wstring > MINPTR && (ULONG) wstring < MAXPTR);
|
---|
120 | while(wstring[i] != 0) i++;
|
---|
121 | return(i);
|
---|
122 | }
|
---|
123 | //******************************************************************************
|
---|
124 | //******************************************************************************
|
---|
125 | void UpCase(char *mixedcase)
|
---|
126 | {
|
---|
127 | int i;
|
---|
128 | vltassert((ULONG)mixedcase > MINPTR && (ULONG) mixedcase < MAXPTR);
|
---|
129 |
|
---|
130 | for(i=0;i<strlen(mixedcase);i++) {
|
---|
131 | if(mixedcase[i] >= 'a' && mixedcase[i] <= 'z') {
|
---|
132 | mixedcase[i] += 'A' - 'a';
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 | //******************************************************************************
|
---|
137 | #ifndef RING0
|
---|
138 | char transCP[256];
|
---|
139 | int CodePage=-1;
|
---|
140 | //******************************************************************************
|
---|
141 | static BOOL prepareCP(int cp)
|
---|
142 | {
|
---|
143 | #define SIZE_ucs_code_page 20
|
---|
144 |
|
---|
145 | static UconvObject uconv_objFrom = NULL;
|
---|
146 | static UconvObject uconv_objTo = NULL;
|
---|
147 |
|
---|
148 | UniChar ucs_code_page[SIZE_ucs_code_page];
|
---|
149 | int rc, i, j;
|
---|
150 | BOOL ret;
|
---|
151 | size_t uni_chars_left;
|
---|
152 | size_t in_bytes_left;
|
---|
153 | size_t num_subs;
|
---|
154 | UniChar *pout_uni_str;
|
---|
155 | char *pin_char_str;
|
---|
156 | char table[256];
|
---|
157 | UniChar fromCP[256];
|
---|
158 | UniChar toCP[256];
|
---|
159 |
|
---|
160 | if(cp != 0)
|
---|
161 | {
|
---|
162 | if(uconv_objTo == NULL)
|
---|
163 | {
|
---|
164 | rc = UniCreateUconvObject( (UniChar*)L"", &uconv_objTo);
|
---|
165 | if ( rc != ULS_SUCCESS )
|
---|
166 | {
|
---|
167 | printf("ERROR UniCreateUconvObject, return code = %u\n", rc);
|
---|
168 | return FALSE;
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | if(cp != CodePage)
|
---|
173 | {
|
---|
174 | if(uconv_objFrom != NULL)
|
---|
175 | {
|
---|
176 | rc = UniFreeUconvObject(uconv_objFrom);
|
---|
177 | if (rc != ULS_SUCCESS)
|
---|
178 | {
|
---|
179 | printf("ERROR UniFreeUconvObject error: return code = %u\n", rc);
|
---|
180 | return FALSE;
|
---|
181 | }
|
---|
182 | }
|
---|
183 | rc = UniMapCpToUcsCp(cp, ucs_code_page, SIZE_ucs_code_page);
|
---|
184 | if (rc != ULS_SUCCESS)
|
---|
185 | {
|
---|
186 | printf("ERROR couldn't translate Codepage ID to unistring, return code = %u\n", rc);
|
---|
187 | return FALSE;
|
---|
188 | }
|
---|
189 | rc = UniCreateUconvObject( ucs_code_page, &uconv_objFrom);
|
---|
190 | if ( rc != ULS_SUCCESS )
|
---|
191 | {
|
---|
192 | printf("ERROR UniCreateUconvObject, return code = %u\n", rc);
|
---|
193 | return FALSE;
|
---|
194 | }
|
---|
195 | for(i=0;i<256;i++)
|
---|
196 | table[i] = i;
|
---|
197 |
|
---|
198 | in_bytes_left = 256;
|
---|
199 | uni_chars_left = 256;
|
---|
200 | pout_uni_str = fromCP;
|
---|
201 | pin_char_str = table;
|
---|
202 | rc = UniUconvToUcs(uconv_objFrom, (void**)&pin_char_str, &in_bytes_left,
|
---|
203 | &pout_uni_str, &uni_chars_left, &num_subs);
|
---|
204 | if ( rc != ULS_SUCCESS )
|
---|
205 | {
|
---|
206 | printf("ERROR UniUconvToUcs, return code = %u\n", rc);
|
---|
207 | return FALSE;
|
---|
208 | }
|
---|
209 |
|
---|
210 | in_bytes_left = 256;
|
---|
211 | uni_chars_left = 256;
|
---|
212 | pout_uni_str = toCP;
|
---|
213 | pin_char_str = table;
|
---|
214 | rc = UniUconvToUcs(uconv_objTo, (void**)&pin_char_str, &in_bytes_left,
|
---|
215 | &pout_uni_str, &uni_chars_left, &num_subs);
|
---|
216 | if ( rc != ULS_SUCCESS )
|
---|
217 | {
|
---|
218 | printf("ERROR UniUconvToUcs, return code = %u\n", rc);
|
---|
219 | return FALSE;
|
---|
220 | }
|
---|
221 |
|
---|
222 | CodePage = cp;
|
---|
223 |
|
---|
224 | for(i=0;i<256;i++)
|
---|
225 | {
|
---|
226 | transCP[i] = i;
|
---|
227 | for(j=0;j<256;j++)
|
---|
228 | {
|
---|
229 | if(fromCP[i] == toCP[j])
|
---|
230 | {
|
---|
231 | transCP[i] = j;
|
---|
232 | break;
|
---|
233 | }
|
---|
234 | }
|
---|
235 | }
|
---|
236 | }
|
---|
237 | }
|
---|
238 | else return FALSE;
|
---|
239 |
|
---|
240 | return TRUE;
|
---|
241 | }
|
---|
242 | //******************************************************************************
|
---|
243 | //******************************************************************************
|
---|
244 | void convertCP(int cp, char *str)
|
---|
245 | {
|
---|
246 | if(str == NULL)
|
---|
247 | return;
|
---|
248 |
|
---|
249 | if(prepareCP(cp) == TRUE && CodePage > 0)
|
---|
250 | while(*str != 0)
|
---|
251 | {
|
---|
252 | *str = transCP[*str];
|
---|
253 | str++;
|
---|
254 | }
|
---|
255 | }
|
---|
256 | //******************************************************************************
|
---|
257 | //******************************************************************************
|
---|
258 | #endif /* ifndef RING 0 */
|
---|