1 | /* $Id: misc.cpp,v 1.5 1999-07-06 08:50:11 sandervl 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 | #define INCL_DOSFILEMGR /* File Manager values */
|
---|
13 | #define INCL_DOSERRORS /* DOS Error values */
|
---|
14 | #define INCL_DOSPROCESS /* DOS Process values */
|
---|
15 | #define INCL_DOSMISC /* DOS Miscellanous values */
|
---|
16 | #define INCL_WIN
|
---|
17 | #include <os2.h>
|
---|
18 | #include <stdio.h>
|
---|
19 | #include <string.h>
|
---|
20 | #include <stdlib.h>
|
---|
21 | #include <iostream.h>
|
---|
22 | #include <string.h>
|
---|
23 | #include "pefile.h"
|
---|
24 | #include "lx.h"
|
---|
25 | #include "misc.h"
|
---|
26 | #include <versionos2.h> /*PLF Wed 98-03-18 01:47:26*/
|
---|
27 | #include <uniconv.h>
|
---|
28 |
|
---|
29 | void convertCP(int cp, char *str);
|
---|
30 |
|
---|
31 | //******************************************************************************
|
---|
32 | //******************************************************************************
|
---|
33 | char *UnicodeToAscii(int length, WCHAR *NameString, int cp)
|
---|
34 | {
|
---|
35 | static char asciistring[256];
|
---|
36 | int i;
|
---|
37 |
|
---|
38 | if(length >= 255) length = 255;
|
---|
39 | for(i=0;i<length;i++) {
|
---|
40 | asciistring[i] = NameString[i] & 0xFF;
|
---|
41 | }
|
---|
42 | asciistring[length] = 0;
|
---|
43 |
|
---|
44 | convertCP(cp, asciistring);
|
---|
45 | return(asciistring);
|
---|
46 | }
|
---|
47 |
|
---|
48 | //******************************************************************************
|
---|
49 | //******************************************************************************
|
---|
50 | char *UnicodeToAscii(WCHAR *wstring, int cp)
|
---|
51 | {
|
---|
52 | static char astring[512];
|
---|
53 | int i;
|
---|
54 |
|
---|
55 | memset(astring, 0, sizeof(astring));
|
---|
56 |
|
---|
57 | for(i=0;i<=UniStrlen(wstring);i++) { //including 0 terminator
|
---|
58 | astring[i] = (UCHAR)wstring[i];
|
---|
59 | }
|
---|
60 | convertCP(cp, astring);
|
---|
61 | return(astring);
|
---|
62 | }
|
---|
63 | //******************************************************************************
|
---|
64 | //******************************************************************************
|
---|
65 | int UniStrlen(WCHAR *wstring)
|
---|
66 | {
|
---|
67 | int i = 0;
|
---|
68 |
|
---|
69 | while(wstring[i] != 0) i++;
|
---|
70 | return(i);
|
---|
71 | }
|
---|
72 | //******************************************************************************
|
---|
73 | //******************************************************************************
|
---|
74 | void UpCase(char *mixedcase)
|
---|
75 | {
|
---|
76 | int i;
|
---|
77 |
|
---|
78 | for(i=0;i<strlen(mixedcase);i++) {
|
---|
79 | if(mixedcase[i] >= 'a' && mixedcase[i] <= 'z') {
|
---|
80 | mixedcase[i] += 'A' - 'a';
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 | //******************************************************************************
|
---|
85 | char transCP[256];
|
---|
86 | int CodePage=-1;
|
---|
87 | //******************************************************************************
|
---|
88 | BOOL prepareCP(int cp)
|
---|
89 | {
|
---|
90 | #define SIZE_ucs_code_page 20
|
---|
91 |
|
---|
92 | static UconvObject uconv_objFrom = NULL;
|
---|
93 | static UconvObject uconv_objTo = NULL;
|
---|
94 |
|
---|
95 | UniChar ucs_code_page[SIZE_ucs_code_page];
|
---|
96 | int rc, i, j;
|
---|
97 | BOOL ret;
|
---|
98 | size_t uni_chars_left;
|
---|
99 | size_t in_bytes_left;
|
---|
100 | size_t num_subs;
|
---|
101 | UniChar *pout_uni_str;
|
---|
102 | char *pin_char_str;
|
---|
103 | char table[256];
|
---|
104 | UniChar fromCP[256];
|
---|
105 | UniChar toCP[256];
|
---|
106 |
|
---|
107 | if(cp != 0)
|
---|
108 | {
|
---|
109 | if(uconv_objTo == NULL)
|
---|
110 | {
|
---|
111 | rc = UniCreateUconvObject( (UniChar*)L"", &uconv_objTo);
|
---|
112 | if ( rc != ULS_SUCCESS )
|
---|
113 | {
|
---|
114 | printf("ERROR UniCreateUconvObject, return code = %u\n", rc);
|
---|
115 | return FALSE;
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | if(cp != CodePage)
|
---|
120 | {
|
---|
121 | if(uconv_objFrom != NULL)
|
---|
122 | {
|
---|
123 | rc = UniFreeUconvObject(uconv_objFrom);
|
---|
124 | if (rc != ULS_SUCCESS)
|
---|
125 | {
|
---|
126 | printf("ERROR UniFreeUconvObject error: return code = %u\n", rc);
|
---|
127 | return FALSE;
|
---|
128 | }
|
---|
129 | }
|
---|
130 | rc = UniMapCpToUcsCp(cp, ucs_code_page, SIZE_ucs_code_page);
|
---|
131 | if (rc != ULS_SUCCESS)
|
---|
132 | {
|
---|
133 | printf("ERROR couldn't translate Codepage ID to unistring, return code = %u\n", rc);
|
---|
134 | return FALSE;
|
---|
135 | }
|
---|
136 | rc = UniCreateUconvObject( ucs_code_page, &uconv_objFrom);
|
---|
137 | if ( rc != ULS_SUCCESS )
|
---|
138 | {
|
---|
139 | printf("ERROR UniCreateUconvObject, return code = %u\n", rc);
|
---|
140 | return FALSE;
|
---|
141 | }
|
---|
142 | for(i=0;i<256;i++)
|
---|
143 | table[i] = i;
|
---|
144 |
|
---|
145 | in_bytes_left = 256;
|
---|
146 | uni_chars_left = 256;
|
---|
147 | pout_uni_str = fromCP;
|
---|
148 | pin_char_str = table;
|
---|
149 | rc = UniUconvToUcs(uconv_objFrom, (void**)&pin_char_str, &in_bytes_left,
|
---|
150 | &pout_uni_str, &uni_chars_left, &num_subs);
|
---|
151 | if ( rc != ULS_SUCCESS )
|
---|
152 | {
|
---|
153 | printf("ERROR UniUconvToUcs, return code = %u\n", rc);
|
---|
154 | return FALSE;
|
---|
155 | }
|
---|
156 |
|
---|
157 | in_bytes_left = 256;
|
---|
158 | uni_chars_left = 256;
|
---|
159 | pout_uni_str = toCP;
|
---|
160 | pin_char_str = table;
|
---|
161 | rc = UniUconvToUcs(uconv_objTo, (void**)&pin_char_str, &in_bytes_left,
|
---|
162 | &pout_uni_str, &uni_chars_left, &num_subs);
|
---|
163 | if ( rc != ULS_SUCCESS )
|
---|
164 | {
|
---|
165 | printf("ERROR UniUconvToUcs, return code = %u\n", rc);
|
---|
166 | return FALSE;
|
---|
167 | }
|
---|
168 |
|
---|
169 | CodePage = cp;
|
---|
170 |
|
---|
171 | for(i=0;i<256;i++)
|
---|
172 | {
|
---|
173 | transCP[i] = i;
|
---|
174 | for(j=0;j<256;j++)
|
---|
175 | {
|
---|
176 | if(fromCP[i] == toCP[j])
|
---|
177 | {
|
---|
178 | transCP[i] = j;
|
---|
179 | break;
|
---|
180 | }
|
---|
181 | }
|
---|
182 | }
|
---|
183 | }
|
---|
184 | }
|
---|
185 | else return FALSE;
|
---|
186 |
|
---|
187 | return TRUE;
|
---|
188 | }
|
---|
189 | //******************************************************************************
|
---|
190 | //******************************************************************************
|
---|
191 | void convertCP(int cp, char *str)
|
---|
192 | {
|
---|
193 | if(str == NULL)
|
---|
194 | return;
|
---|
195 |
|
---|
196 | if(prepareCP(cp) == TRUE && CodePage > 0)
|
---|
197 | while(*str != 0)
|
---|
198 | {
|
---|
199 | *str = transCP[*str];
|
---|
200 | str++;
|
---|
201 | }
|
---|
202 | }
|
---|
203 | //******************************************************************************
|
---|
204 | //******************************************************************************
|
---|