b311480e |
1 | /* |
2 | Copyright (c) 1998-1999 Matra Datavision |
973c2be1 |
3 | Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e |
4 | |
973c2be1 |
5 | This file is part of Open CASCADE Technology software library. |
b311480e |
6 | |
d5f74e42 |
7 | This library is free software; you can redistribute it and/or modify it under |
8 | the terms of the GNU Lesser General Public License version 2.1 as published |
973c2be1 |
9 | by the Free Software Foundation, with special exception defined in the file |
10 | OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
11 | distribution for complete text of the license and disclaimer of any warranty. |
b311480e |
12 | |
973c2be1 |
13 | Alternatively, this file may be used under the terms of Open CASCADE |
14 | commercial license or contractual agreement. |
b311480e |
15 | */ |
16 | |
7fd59977 |
17 | #include <stdio.h> |
18 | #include <string.h> |
19 | #include <sys/types.h> |
20 | |
21 | typedef unsigned short char16 ; |
22 | |
baf60a87 |
23 | #include "Resource_Shiftjis.pxx" |
24 | #include "Resource_GB2312.pxx" |
7fd59977 |
25 | |
26 | #define isjis(c) (((c)>=0x21 && (c)<=0x7e)) |
27 | #define iseuc(c) (((c)>=0xa1 && (c)<=0xfe)) |
28 | #define issjis1(c) (((c)>=0x81 && (c)<=0x9f) || ((c)>=0xe0 && (c)<=0xef)) |
29 | #define issjis2(c) ((c)>=0x40 && (c)<=0xfc && (c)!=0x7f) |
30 | #define ishankana(c) ((c)>=0xa0 && (c)<=0xdf) |
31 | #define isshift(c) (((c)>=0x80 && (c)<=0xff)) |
32 | |
33 | |
34 | static void sjis_to_jis (unsigned int *ph, unsigned int *pl) |
35 | { |
36 | |
37 | if ( ! issjis1 ( *ph ) || ! issjis2 ( *pl ) ) { |
38 | return ; |
39 | } |
40 | |
41 | if (*ph <= 0x9f) |
42 | { |
43 | if (*pl < 0x9f) |
44 | *ph = (*ph << 1) - 0xe1; |
45 | else |
46 | *ph = (*ph << 1) - 0xe0; |
47 | } |
48 | else |
49 | { |
50 | if (*pl < 0x9f) |
51 | *ph = (*ph << 1) - 0x161; |
52 | else |
53 | *ph = (*ph << 1) - 0x160; |
54 | } |
55 | if (*pl < 0x7f) |
56 | *pl -= 0x1f; |
57 | else if (*pl < 0x9f) |
58 | *pl -= 0x20; |
59 | else |
60 | *pl -= 0x7e; |
61 | } |
62 | |
63 | static void jis_to_sjis (unsigned int *ph, unsigned int *pl) |
64 | { |
65 | if (*ph & 1) |
66 | { |
67 | if (*pl < 0x60) |
68 | *pl += 0x1f; |
69 | else |
70 | *pl += 0x20; |
71 | } |
72 | else |
73 | *pl += 0x7e; |
74 | if (*ph < 0x5f) |
75 | *ph = (*ph + 0xe1) >> 1; |
76 | else |
77 | *ph = (*ph + 0x161) >> 1; |
78 | } |
79 | |
80 | static void euc_to_sjis (unsigned int *ph, unsigned int *pl) |
81 | { |
82 | if ( (*ph & 0xFFFFFF00) || (*pl & 0xFFFFFF00) ) { |
83 | *ph = 0 ; |
84 | *pl = 0 ; |
85 | return ; |
86 | } |
87 | |
88 | if ( ! iseuc ( *ph ) || ! iseuc ( *pl ) ) { |
89 | return ; |
90 | } |
91 | |
92 | |
93 | *ph &= 0x7F ; |
94 | *pl &= 0x7F ; |
95 | |
96 | jis_to_sjis ( ph , pl ) ; |
97 | |
98 | } |
99 | |
100 | static void sjis_to_euc (unsigned int *ph, unsigned int *pl) |
101 | { |
102 | if ( (*ph & 0xFFFFFF00) || (*pl & 0xFFFFFF00) ) { |
103 | *ph = 0 ; |
104 | *pl = 0 ; |
105 | return ; |
106 | } |
107 | |
108 | if ( ! issjis1 ( *ph ) || ! issjis2 ( *pl ) ) { |
109 | return ; |
110 | } |
111 | |
112 | if ( *ph == 0 && *pl == 0 ) |
113 | return ; |
114 | |
115 | sjis_to_jis ( ph , pl ) ; |
116 | |
117 | *ph |= 0x80 ; |
118 | *pl |= 0x80 ; |
119 | |
120 | } |
121 | |
122 | void Resource_sjis_to_unicode (unsigned int *ph, unsigned int *pl) |
123 | { |
124 | char16 sjis ; |
125 | char16 uni ; |
126 | |
127 | if ( (*ph & 0xFFFFFF00) || (*pl & 0xFFFFFF00) ) { |
128 | *ph = 0 ; |
129 | *pl = 0 ; |
130 | return ; |
131 | } |
132 | |
133 | if ( ! issjis1 ( *ph ) || ! issjis2 ( *pl ) ) { |
134 | return ; |
135 | } |
136 | |
8263fcd3 |
137 | sjis = (char16)(((*ph) << 8) | (*pl)) ; |
7fd59977 |
138 | uni = sjisuni [sjis] ; |
139 | *ph = uni >> 8 ; |
140 | *pl = uni & 0xFF ; |
141 | } |
142 | |
143 | void Resource_unicode_to_sjis (unsigned int *ph, unsigned int *pl) |
144 | { |
145 | char16 sjis ; |
146 | char16 uni ; |
147 | |
148 | if ( (*ph & 0xFFFFFF00) || (*pl & 0xFFFFFF00) ) { |
149 | *ph = 0 ; |
150 | *pl = 0 ; |
151 | return ; |
152 | } |
153 | if ( *ph == 0 && *pl == 0 ) |
154 | return ; |
155 | |
8263fcd3 |
156 | uni = (char16)(((*ph) << 8) | (*pl)) ; |
7fd59977 |
157 | sjis = unisjis [uni] ; |
158 | *ph = sjis >> 8 ; |
159 | *pl = sjis & 0xFF ; |
160 | } |
161 | |
162 | void Resource_unicode_to_euc (unsigned int *ph, unsigned int *pl) |
163 | { |
164 | |
165 | if ( *ph == 0 && *pl == 0 ) |
166 | return ; |
167 | |
168 | Resource_unicode_to_sjis ( ph , pl ) ; |
169 | if (issjis1(*ph)) { /* let's believe it is ANSI code if it is not sjis*/ |
170 | sjis_to_euc ( ph , pl ) ; |
171 | } |
172 | |
173 | } |
174 | |
175 | void Resource_euc_to_unicode (unsigned int *ph, unsigned int *pl) |
176 | { |
177 | |
178 | if ( ! iseuc ( *ph ) || ! iseuc ( *pl ) ) { |
179 | return ; |
180 | } |
181 | |
182 | |
183 | if ( *ph == 0 && *pl == 0 ) |
184 | return ; |
185 | |
186 | euc_to_sjis ( ph , pl ) ; |
187 | Resource_sjis_to_unicode ( ph , pl ) ; |
188 | |
189 | } |
190 | |
191 | |
192 | void Resource_gb_to_unicode (unsigned int *ph, unsigned int *pl) |
193 | { |
194 | char16 gb ; |
195 | char16 uni ; |
196 | |
197 | |
198 | if ( (*ph & 0xFFFFFF00) || (*pl & 0xFFFFFF00) ) { |
199 | *ph = 0 ; |
200 | *pl = 0 ; |
201 | return ; |
202 | } |
203 | |
204 | if ( ! isshift ( *ph ) || ! isshift ( *pl ) ) { |
205 | return ; |
206 | } |
207 | |
208 | *ph = (*ph) & 0x7f ; |
209 | *pl = (*pl) & 0x7f ; |
210 | |
8263fcd3 |
211 | gb = (char16)(((*ph) << 8) | (*pl)) ; |
7fd59977 |
212 | uni = gbuni [gb] ; |
213 | *ph = uni >> 8 ; |
214 | *pl = uni & 0xFF ; |
215 | } |
216 | |
217 | void Resource_unicode_to_gb (unsigned int *ph, unsigned int *pl) |
218 | { |
219 | char16 gb ; |
220 | char16 uni ; |
221 | |
222 | if ( (*ph & 0xFFFFFF00) || (*pl & 0xFFFFFF00) ) { |
223 | *ph = 0 ; |
224 | *pl = 0 ; |
225 | return ; |
226 | } |
227 | if ( *ph == 0 && *pl == 0 ) |
228 | return ; |
229 | |
8263fcd3 |
230 | uni = (char16)(((*ph) << 8) | (*pl)); |
7fd59977 |
231 | gb = unigb [uni] ; |
232 | if (gb != 0) { |
233 | *ph = ( gb >> 8 ) | 0x80 ; |
234 | *pl = ( gb & 0xFF ) | 0x80 ; |
235 | } |
236 | else { |
237 | *ph = 0; |
238 | *pl = 0 ; |
239 | } |
240 | } |