Debian Codegraph @

mcs/class/I18N/tools/uni2tab.c

raw symbols SCIP
1 /*
2 * uni2tab.c - Convert Unicode data files into CJK conversion tables.
3 *
4 * Copyright (c) 2002 Southern Storm Software, Pty Ltd
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /*
26 *
27 * Usage: uni2tab
28 *
29 * Required files from ftp.unicode.org: Unihan.txt, CP932.TXT
30 *
31 * Unihan.txt and CP932.TXT can be found at:
32 * ftp://www.unicode.org/Public/5.0.0/ucd/Unihan.txt
33 * ftp://ftp.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP932.TXT
34 */
35
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>
39
40 /*
41 * Forward declarations.
42 */
43 static void convertLine(char *buf);
44 static void convertSJISLine(char *buf);
45 static int createTables(void);
46
47 int main(int argc, char *argv[])
48 {
49 FILE *file;
50 char buffer[BUFSIZ];
51 int error;
52
53 /* Load the relevant contents from the Unihan.txt file */
54 if((file = fopen("Unihan.txt", "r")) == NULL)
55 {
56 perror("Unihan.txt");
57 return 1;
58 }
59 while(fgets(buffer, sizeof(buffer), file))
60 {
61 if(buffer[0] == 'U' && buffer[1] == '+')
62 {
63 convertLine(buffer + 2);
64 }
65 }
66 fclose(file);
67
68 /* Load the relevant contents from the CP932.TXT file,
69 to get mappings for non-CJK characters */
70 if((file = fopen("CP932.TXT", "r")) == NULL)
71 {
72 perror("CP932.TXT");
73 return 1;
74 }
75 while(fgets(buffer, sizeof(buffer), file))
76 {
77 if(buffer[0] == '0' && buffer[1] == 'x')
78 {
79 convertSJISLine(buffer + 2);
80 }
81 }
82 fclose(file);
83
84 /* Create the output tables */
85 error = createTables();
86
87 /* Clean up and exit */
88 return error;
89 }
90
91 /*
92 * Parse a hexadecimal value. Returns the length
93 * of the value that was parsed.
94 */
95 static int parseHex(const char *buf, unsigned long *value)
96 {
97 int len = 0;
98 char ch;
99 *value = 0;
100 while((ch = buf[len]) != '\0')
101 {
102 if(ch >= '0' && ch <= '9')
103 {
104 *value = *value * 16 + (unsigned long)(ch - '0');
105 }
106 else if(ch >= 'A' && ch <= 'F')
107 {
108 *value = *value * 16 + (unsigned long)(ch - 'A' + 10);
109 }
110 else if(ch >= 'a' && ch <= 'f')
111 {
112 *value = *value * 16 + (unsigned long)(ch - 'a' + 10);
113 }
114 else
115 {
116 break;
117 }
118 ++len;
119 }
120 return len;
121 }
122
123 /*
124 * Parse "ku" and "ten" values from a buffer.
125 */
126 static void parseKuTen(const char *buf, int *ku, int *ten)
127 {
128 int value = 0;
129 while(*buf >= '0' && *buf <= '9')
130 {
131 value = value * 10 + (*buf++ - '0');
132 }
133 *ku = value / 100;
134 *ten = value % 100;
135 }
136
137 /*
138 * Tables.
139 */
140 static unsigned short jisx0208ToUnicode[94*94];
141 static unsigned short jisx0212ToUnicode[94*94];
142 static unsigned short unicodeToJis[65536];
143 static unsigned short greekToJis[0x451 - 0x0391 + 1];
144 static unsigned short extraToJis[0xFFEF - 0xFF01 + 1];
145 static unsigned long lowJis = 0xFFFF;
146 static unsigned long highJis = 0x0000;
147
148 /*
149 * Process a JIS X 0208 sequence by ku and ten values.
150 */
151 static void processJis0208(unsigned long code, int ku, int ten)
152 {
153 int offset = (ku - 1) * 94 + (ten - 1);
154 jisx0208ToUnicode[offset] = (unsigned short)code;
155 unicodeToJis[code] = (unsigned short)(offset + 0x0100);
156 if(code < lowJis)
157 {
158 lowJis = code;
159 }
160 if(code > highJis)
161 {
162 highJis = code;
163 }
164 }
165
166 /*
167 * Process a JIS X 0212 sequence by ku and ten values.
168 */
169 static void processJis0212(unsigned long code, int ku, int ten)
170 {
171 int offset = (ku - 1) * 94 + (ten - 1);
172 jisx0212ToUnicode[offset] = (unsigned short)code;
173 unicodeToJis[code] = (unsigned short)(offset + 0x8000);
174 if(code < lowJis)
175 {
176 lowJis = code;
177 }
178 if(code > highJis)
179 {
180 highJis = code;
181 }
182 }
183
184 /*
185 * Convert an input line into table entries.
186 */
187 static void convertLine(char *buf)
188 {
189 unsigned long code;
190 const char *key;
191 int ku, ten;
192
193 /* Parse the hex name of the Unicode character */
194 buf += parseHex(buf, &code);
195 if(code >= 0x10000)
196 {
197 /* Cannot handle surrogate-based CJK characters yet */
198 return;
199 }
200
201 /* Skip to the key name */
202 while(*buf != '\0' && *buf != 'k')
203 {
204 ++buf;
205 }
206 if(*buf == '\0')
207 {
208 return;
209 }
210
211 /* Extract the key name from the buffer */
212 key = buf;
213 while(*buf != '\0' && *buf != ' ' && *buf != '\t')
214 {
215 ++buf;
216 }
217 if(*buf == '\0')
218 {
219 return;
220 }
221 *buf++ = '\0';
222
223 /* Skip to the value field */
224 while(*buf != '\0' && (*buf == ' ' || *buf == '\t' ||
225 *buf == '\r' || *buf == '\n'))
226 {
227 ++buf;
228 }
229 if(*buf == '\0')
230 {
231 return;
232 }
233
234 /* Determine what to do based on the key */
235 if(!strcmp(key, "kJis0"))
236 {
237 parseKuTen(buf, &ku, &ten);
238 processJis0208(code, ku, ten);
239 }
240 else if(!strcmp(key, "kJis1"))
241 {
242 parseKuTen(buf, &ku, &ten);
243 processJis0212(code, ku, ten);
244 }
245 }
246
247 /*
248 * Convert a line from the "CP932.TXT" file.
249 */
250 static void convertSJISLine(char *buf)
251 {
252 unsigned long sjis;
253 unsigned long code;
254 int ch1, ch2;
255 int offset;
256
257 /* Read the Shift-JIS code point */
258 buf += parseHex(buf, &sjis);
259 if(sjis < 0x8000)
260 {
261 return;
262 }
263 while(*buf != '\0' && (*buf == ' ' || *buf == '\t' ||
264 *buf == '\r' || *buf == '\n'))
265 {
266 ++buf;
267 }
268 if(*buf != '0' || buf[1] != 'x')
269 {
270 return;
271 }
272 buf += 2;
273
274 /* Read the Unicode code point */
275 buf += parseHex(buf, &code);
276
277 /* Convert the Shift-JIS code point into a JIS kuten value */
278 ch1 = (int)(sjis >> 8);
279 ch2 = (int)(sjis & 0xFF);
280 if(ch1 >= 0x81 && ch1 <= 0x9F)
281 {
282 offset = (ch1 - 0x81) * 0xBC;
283 }
284 else if(ch1 >= 0xE0 && ch1 <= 0xEF)
285 {
286 offset = (ch1 - 0xE0 + (0xA0 - 0x81)) * 0xBC;
287 }
288 else
289 {
290 /* Invalid first byte */
291 return;
292 }
293 if(ch2 >= 0x40 && ch2 <= 0x7E)
294 {
295 offset += (ch2 - 0x40);
296 }
297 else if(ch2 >= 0x80 && ch2 <= 0xFC)
298 {
299 offset += (ch2 - 0x80 + 0x3F);
300 }
301 else
302 {
303 /* Invalid second byte */
304 return;
305 }
306
307 /* Process the kuten value */
308 if(code >= 0x0391 && code <= 0x0451)
309 {
310 /* Greek subset */
311 greekToJis[code - 0x0391] = (unsigned short)(offset + 0x0100);
312 /* This is required to decode Extra subset to Unicode!! */
313 jisx0208ToUnicode[offset] = (unsigned short)code;
314 }
315 else if(code >= 0xFF01 && code <= 0xFFEF)
316 {
317 /* Extra subset */
318 extraToJis[code - 0xFF01] = (unsigned short)(offset + 0x0100);
319 /* This is required to decode Extra subset to Unicode!! */
320 jisx0208ToUnicode[offset] = (unsigned short)code;
321 }
322 else if(code >= 0x0100 && code < 0x4E00)
323 {
324 /* Non-CJK characters within JIS */
325 processJis0208(code, (offset / 94) + 1, (offset % 94) + 1);
326 }
327 else if(code >= 0x00A7 && code <= 0x00F7)
328 {
329 /* Non-CJK characters within JIS for which unicodeToJis should not be
330 * edited. In addition to this, do not track lowJis and highJis. */
331 jisx0208ToUnicode[offset] = (unsigned short)(code & 0xFF);
332 jisx0208ToUnicode[offset + 1] = (((unsigned short)(code & 0x00FF)) >> 8);
333 }
334 }
335
336 /*
337 * Write a section header.
338 */
339 static void writeSection(FILE *file, unsigned long num, unsigned long size)
340 {
341 putc((int)(num & 0xFF), file);
342 putc((int)((num >> 8) & 0xFF), file);
343 putc((int)((num >> 16) & 0xFF), file);
344 putc((int)((num >> 24) & 0xFF), file);
345 putc((int)(size & 0xFF), file);
346 putc((int)((size >> 8) & 0xFF), file);
347 putc((int)((size >> 16) & 0xFF), file);
348 putc((int)((size >> 24) & 0xFF), file);
349 }
350
351 /*
352 * Write an array of 16-bit data values.
353 */
354 static void writeData(FILE *file, unsigned short *data, unsigned long size)
355 {
356 while(size > 0)
357 {
358 putc((int)(*data & 0xFF), file);
359 putc((int)((*data >> 8) & 0xFF), file);
360 ++data;
361 --size;
362 }
363 }
364
365 /*
366 * Section numbers for the JIS table.
367 */
368 #define JISX0208_To_Unicode 1
369 #define JISX0212_To_Unicode 2
370 #define CJK_To_JIS 3
371 #define Greek_To_JIS 4
372 #define Extra_To_JIS 5
373
374 /*
375 * Write the JIS table file.
376 */
377 static void writeJis(FILE *file)
378 {
379 unsigned long size;
380
381 /* Write the JIS X 0208 to Unicode conversion table */
382 writeSection(file, JISX0208_To_Unicode, 94 * 94 * 2);
383 writeData(file, jisx0208ToUnicode, 94 * 94);
384
385 /* Write the JIS X 0212 to Unicode conversion table */
386 writeSection(file, JISX0212_To_Unicode, 94 * 94 * 2);
387 writeData(file, jisx0212ToUnicode, 94 * 94);
388
389 /* Write the Unicode to JIS conversion table */
390 size = highJis - lowJis + 1;
391 writeSection(file, CJK_To_JIS, size * 2);
392 writeData(file, unicodeToJis + lowJis, size);
393 printf("JIS: U+%04lX to U+%04lX\n", lowJis, highJis);
394
395 /* Write the Greek to JIS conversion table */
396 writeSection(file, Greek_To_JIS, sizeof(greekToJis));
397 writeData(file, greekToJis, sizeof(greekToJis) / 2);
398
399 /* Write the Extra to JIS conversion table */
400 writeSection(file, Extra_To_JIS, sizeof(extraToJis));
401 writeData(file, extraToJis, sizeof(extraToJis) / 2);
402 }
403
404 /*
405 * Create all of the tables that we need based on the Unihan.txt file.
406 */
407 static int createTables(void)
408 {
409 FILE *file;
410
411 /* Create the JIS conversion table */
412 if((file = fopen("jis.table", "wb")) == NULL)
413 {
414 if((file = fopen("jis.table", "wb")) == NULL)
415 {
416 perror("jis.table");
417 return 1;
418 }
419 }
420 writeJis(file);
421 fclose(file);
422
423 /* Done */
424 return 0;
425 }