0
|
1 /*
|
|
2 * $Id: revdic.c,v 1.5 2004/05/21 16:39:32 aono Exp $
|
|
3 */
|
|
4
|
|
5 /*
|
|
6 * FreeWnn is a network-extensible Kana-to-Kanji conversion system.
|
|
7 * This file is part of FreeWnn.
|
|
8 *
|
|
9 * Copyright Kyoto University Research Institute for Mathematical Sciences
|
|
10 * 1987, 1988, 1989, 1990, 1991, 1992
|
|
11 * Copyright OMRON Corporation. 1987, 1988, 1989, 1990, 1991, 1992, 1999
|
|
12 * Copyright ASTEC, Inc. 1987, 1988, 1989, 1990, 1991, 1992
|
|
13 * Copyright FreeWnn Project 1999, 2000, 2002
|
|
14 *
|
|
15 * Maintainer: FreeWnn Project <freewnn@tomo.gr.jp>
|
|
16 *
|
|
17 * This program is free software; you can redistribute it and/or modify
|
|
18 * it under the terms of the GNU General Public License as published by
|
|
19 * the Free Software Foundation; either version 2 of the License, or
|
|
20 * (at your option) any later version.
|
|
21 *
|
|
22 * This program is distributed in the hope that it will be useful,
|
|
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
25 * GNU General Public License for more details.
|
|
26 *
|
|
27 * You should have received a copy of the GNU General Public License
|
|
28 * along with this program; if not, write to the Free Software
|
|
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
30 */
|
|
31
|
|
32 #include <stdio.h>
|
|
33 #include "commonhd.h"
|
|
34 #include "jslib.h"
|
|
35 #include "jh.h"
|
|
36 #include "jdata.h"
|
|
37
|
|
38
|
|
39 #ifndef min
|
|
40 #define min(a, b) ((a > b)? b:a)
|
|
41 #define max(a, b) ((a < b)? b:a)
|
|
42 #endif
|
|
43
|
|
44
|
|
45 /*
|
|
46 * rev_ud_hontai(hostart, maxhontai)
|
|
47 * rev_ud_table(tary, maxtable)
|
|
48 * rev_sd_hontai(hostart)
|
|
49 * rev_hinsi(hinsi, maxserial)
|
|
50 */
|
|
51
|
|
52 static int tmptmp;
|
|
53
|
|
54 #define rev_int(x) (tmptmp = *(x), *(x) = ((tmptmp >> 24) & 0xff) | \
|
|
55 (((tmptmp >> 16) & 0xff) << 8) | \
|
|
56 (((tmptmp >> 8) & 0xff) << 16) | \
|
|
57 (((tmptmp & 0xff) << 24)))
|
|
58
|
|
59 #define rev_short(x) (tmptmp = *(x), *(x) = ((tmptmp >> 8) & 0xff) | \
|
|
60 ((tmptmp & 0xff) << 8))
|
|
61
|
|
62 #define rev_int_org(x) (tmptmp = *(x), *(x) = ((tmptmp >> 24) & 0xff) | \
|
|
63 (((tmptmp >> 16) & 0xff) << 8) | \
|
|
64 (((tmptmp >> 8) & 0xff) << 16) | \
|
|
65 ((tmptmp & 0xff) << 24), tmptmp)
|
|
66
|
|
67 #define rev_short_org(x) (tmptmp = *(x), *(x) = ((tmptmp >> 8) & 0xff) | \
|
|
68 ((tmptmp & 0xff) << 8), tmptmp)
|
|
69
|
|
70
|
|
71
|
|
72 void
|
|
73 rev_ud_hontai (hostart, maxhontai, match)
|
|
74 UCHAR *hostart;
|
|
75 int maxhontai;
|
|
76 int match; /* whether the cpu type match the dictionary */
|
|
77 {
|
|
78 UCHAR *hoend = hostart + maxhontai;
|
|
79 struct uind2 *hop, *hop1;
|
|
80 int k;
|
|
81 int len;
|
|
82
|
|
83 /* Skip first 4 bytes for some reason. (cf. ujistoud() in atod.c) */
|
|
84 for (hop = (struct uind2 *)((int *) hostart + 1); (UCHAR *) hop < hoend;)
|
|
85 {
|
|
86 rev_int (&(hop->next));
|
|
87 rev_int (&(hop->serial));
|
|
88 rev_int (&(hop->kanjipter));
|
|
89 rev_short (&(hop->kosuu));
|
|
90 if (match)
|
|
91 {
|
|
92 len = hop->yomi[0];
|
|
93 hop1 = (struct uind2 *) (AL_INT (&(hop->yomi[0]) + 1 + max ((len - 4), 0)));
|
|
94 rev_short (&(hop->yomi[0]));
|
|
95 for (k = 0; k < len - 4; k++)
|
|
96 {
|
|
97 rev_short (&(hop->yomi[k + 1]));
|
|
98 }
|
|
99 hop = hop1;
|
|
100 }
|
|
101 else
|
|
102 {
|
|
103 rev_short (&(hop->yomi[0]));
|
|
104 len = hop->yomi[0];
|
|
105 for (k = 0; k < len - 4; k++)
|
|
106 {
|
|
107 rev_short (&(hop->yomi[k + 1]));
|
|
108 }
|
|
109 hop = (struct uind2 *) (AL_INT (&(hop->yomi[0]) + 1 + max ((len - 4), 0)));
|
|
110 }
|
|
111 }
|
|
112 }
|
|
113
|
|
114
|
|
115 void
|
|
116 rev_ud_table (tary, maxtable, match)
|
|
117 struct uind1 *tary;
|
|
118 int maxtable;
|
|
119 int match;
|
|
120 {
|
|
121 int k;
|
|
122
|
|
123 for (k = 0; k < maxtable; k++)
|
|
124 {
|
|
125 rev_int (&(tary[k].pter1));
|
|
126 rev_int (&(tary[k].pter));
|
|
127 rev_int (&(tary[k].yomi1));
|
|
128 rev_int (&(tary[k].yomi2));
|
|
129 }
|
|
130 }
|
|
131
|
|
132 void
|
|
133 rev_hinsi (hinsi, maxserial, match)
|
|
134 unsigned short *hinsi;
|
|
135 int maxserial;
|
|
136 int match;
|
|
137 {
|
|
138 int k;
|
|
139
|
|
140 for (k = 0; k < maxserial; k++)
|
|
141 {
|
|
142 rev_short (&(hinsi[k]));
|
|
143 }
|
|
144 }
|
|
145
|
|
146 /*
|
|
147 * first element of each node is syurui,
|
|
148 * which is unsigned short less than 0xff.
|
|
149 */
|
|
150
|
|
151 #define match_machine(x) (*(unsigned short *)(x) & 0xff)
|
|
152
|
|
153 void travel_next_nodes ();
|
|
154 void rev_sd_node ();
|
|
155
|
|
156 void
|
|
157 rev_sd_hontai0 (hopter, hostart, match)
|
|
158 UCHAR *hostart;
|
|
159 UCHAR *hopter;
|
|
160 int match;
|
|
161 {
|
|
162 if (match)
|
|
163 {
|
|
164 travel_next_nodes (hopter, hostart, match);
|
|
165 rev_sd_node (hopter, hostart, match);
|
|
166 }
|
|
167 else
|
|
168 {
|
|
169 rev_sd_node (hopter, hostart, match);
|
|
170 travel_next_nodes (hopter, hostart, match);
|
|
171 }
|
|
172 }
|
|
173
|
|
174 void
|
|
175 rev_sd_hontai (hostart, match)
|
|
176 UCHAR *hostart;
|
|
177 int match;
|
|
178 {
|
|
179 rev_sd_hontai0 (hostart, hostart, match);
|
|
180 }
|
|
181
|
|
182 void
|
|
183 travel_next_nodes (hopter, hostart, match)
|
|
184 UCHAR *hostart, *hopter;
|
|
185 int match;
|
|
186 {
|
|
187 int k;
|
|
188 int tsize;
|
|
189 w_char *charst;
|
|
190 w_char *sumst;
|
|
191 int *ptrst;
|
|
192
|
|
193 switch (*(unsigned short *) hopter)
|
|
194 {
|
|
195 case ST_NORMAL:
|
|
196 tsize = *(w_char *) (hopter + 2);
|
|
197 charst = (w_char *) (hopter + 12);
|
|
198 sumst = ((w_char *) charst + tsize + 2); /* + 2 keeps two zero words */
|
|
199 ptrst = (int *) ((w_char *) sumst + tsize);
|
|
200 for (k = 0; k < tsize; k++)
|
|
201 {
|
|
202 if (ptrst[k] != ENDPTR)
|
|
203 {
|
|
204 rev_sd_hontai0 (hostart + ptrst[k], hostart, match);
|
|
205 }
|
|
206 }
|
|
207 break;
|
|
208 case ST_NOPTER:
|
|
209 break;
|
|
210 case ST_NOENT:
|
|
211 tsize = *(w_char *) (hopter + 2);
|
|
212 charst = (w_char *) (hopter + 4);
|
|
213 ptrst = (int *) AL_INT ((w_char *) charst + tsize);
|
|
214 for (k = 0; k < tsize; k++)
|
|
215 {
|
|
216 rev_sd_hontai0 (hostart + ptrst[k], hostart, match);
|
|
217 }
|
|
218 break;
|
|
219 case ST_SMALL:
|
|
220 rev_sd_hontai0 (hopter + 4, hostart, match);
|
|
221 break;
|
|
222 }
|
|
223 }
|
|
224
|
|
225 #define rev_if_short(x) (match? rev_short_org(x): rev_short(x))
|
|
226 #define rev_if_int(x) (match? rev_int_org(x): rev_int(x))
|
|
227
|
|
228 void
|
|
229 rev_sd_node (hopter, hostart, match)
|
|
230 UCHAR *hostart, *hopter;
|
|
231 int match;
|
|
232 {
|
|
233 /* whether current state of dict matches the cpu type of the machine */
|
|
234 int k;
|
|
235 int tsize;
|
|
236 w_char *charst;
|
|
237 w_char *sumst;
|
|
238 int *ptrst;
|
|
239 int state;
|
|
240
|
|
241 state = rev_if_short ((unsigned short *) hopter);
|
|
242 switch (state)
|
|
243 {
|
|
244 case ST_NORMAL:
|
|
245 case ST_NOPTER:
|
|
246 tsize = rev_if_short ((w_char *) (hopter + 2));
|
|
247 rev_int ((int *) (hopter + 4));
|
|
248 rev_int ((int *) (hopter + 8));
|
|
249 charst = (w_char *) (hopter + 12);
|
|
250 sumst = ((w_char *) charst + tsize + 2); /* + 2 keeps two zero words */
|
|
251 ptrst = (int *) ((w_char *) sumst + tsize);
|
|
252 for (k = 0; k < tsize; k++)
|
|
253 {
|
|
254 rev_short (charst + k);
|
|
255 rev_short (sumst + k);
|
|
256 }
|
|
257 if (state == ST_NORMAL)
|
|
258 {
|
|
259 for (k = 0; k < tsize; k++)
|
|
260 {
|
|
261 rev_int (ptrst + k);
|
|
262 }
|
|
263 }
|
|
264 break;
|
|
265 case ST_NOENT:
|
|
266 tsize = rev_if_short ((w_char *) (hopter + 2));
|
|
267 charst = (w_char *) (hopter + 4);
|
|
268 ptrst = (int *) AL_INT ((w_char *) charst + tsize);
|
|
269 for (k = 0; k < tsize; k++)
|
|
270 {
|
|
271 rev_short (charst + k);
|
|
272 rev_int (ptrst + k);
|
|
273 }
|
|
274 break;
|
|
275 case ST_SMALL:
|
|
276 rev_short ((unsigned short *) (hopter + 2));
|
|
277 break;
|
|
278 }
|
|
279 }
|
|
280
|
|
281
|
|
282 int
|
|
283 little_endian ()
|
|
284 {
|
|
285 int a = 1;
|
|
286 #ifdef BYTE_SWAP
|
|
287 return (1);
|
|
288 #else
|
|
289 return (*(char *) &a == 1);
|
|
290 #endif
|
|
291 }
|
|
292
|
|
293 void
|
|
294 rev_rd_rind1 (ri1, maxri1, match)
|
|
295 struct rind1 *ri1;
|
|
296 int maxri1;
|
|
297 int match;
|
|
298 {
|
|
299 int k;
|
|
300
|
|
301 for (k = 0; k < maxri1; k++)
|
|
302 {
|
|
303 rev_int (&(ri1[k].pter1));
|
|
304 rev_int (&(ri1[k].pter));
|
|
305 }
|
|
306 }
|
|
307
|
|
308 void
|
|
309 rev_rd_rind2 (ri2, maxri2, match)
|
|
310 struct rind2 *ri2;
|
|
311 int maxri2;
|
|
312 int match;
|
|
313 {
|
|
314 int k;
|
|
315
|
|
316 for (k = 0; k < maxri2; k++)
|
|
317 {
|
|
318 rev_int (&(ri2[k].kanjipter));
|
|
319 rev_int (&(ri2[k].next[D_YOMI]));
|
|
320 rev_int (&(ri2[k].next[D_KANJI]));
|
|
321 }
|
|
322 }
|
|
323
|
|
324 void rev_w_char ();
|
|
325
|
|
326 void
|
|
327 rev_kanji (kpter, maxk, match)
|
|
328 UCHAR *kpter;
|
|
329 int maxk;
|
|
330 int match;
|
|
331 {
|
|
332 UCHAR *kend = kpter + maxk;
|
|
333
|
|
334 while (kpter < kend && *kpter)
|
|
335 {
|
|
336 rev_w_char ((w_char *) (kpter + 2), *kpter / 2 - 1);
|
|
337 kpter += *kpter;
|
|
338 }
|
|
339 }
|
|
340
|
|
341 void
|
|
342 rev_w_char (p, maxp)
|
|
343 w_char *p;
|
|
344 int maxp;
|
|
345 {
|
|
346 for (; maxp > 0; maxp--)
|
|
347 {
|
|
348 rev_short (p);
|
|
349 p++;
|
|
350 }
|
|
351 }
|
|
352
|
|
353 /*
|
|
354 * Change the byte-order of dictionary except for the header area.
|
|
355 * Header and hindo area don't depend on byte-order.
|
|
356 * If match is nonzero, the dict-type matches CPU type before conversion.
|
|
357 */
|
|
358
|
|
359 int
|
|
360 revdic (jtl, match)
|
|
361 struct JT *jtl;
|
|
362 int match;
|
|
363 {
|
|
364 extern int rev_ud (), rev_rd (), rev_sd ();
|
|
365 int syurui = jtl->syurui;
|
|
366
|
|
367 syurui = jtl->syurui & 0xff;
|
|
368
|
|
369 if (syurui == WNN_UD_DICT)
|
|
370 {
|
|
371 if (rev_ud (jtl, match) == -1)
|
|
372 return (-1);
|
|
373 }
|
|
374 else if (syurui == WNN_REV_DICT)
|
|
375 {
|
|
376 if (rev_rd (jtl, match) == -1)
|
|
377 return (-1);
|
|
378 }
|
|
379 else
|
|
380 {
|
|
381 if (rev_sd (jtl, match) == -1)
|
|
382 return (-1);
|
|
383 }
|
|
384 return (0);
|
|
385 }
|
|
386
|
|
387 void rev_common ();
|
|
388
|
|
389 int
|
|
390 rev_ud (jtl, match)
|
|
391 struct JT *jtl;
|
|
392 int match;
|
|
393 {
|
|
394 rev_common (jtl, match);
|
|
395 rev_ud_hontai (jtl->hontai, jtl->maxhontai, match);
|
|
396 rev_ud_table (jtl->table, jtl->maxtable, match);
|
|
397 return (0);
|
|
398 }
|
|
399
|
|
400 int
|
|
401 rev_sd (jtl, match)
|
|
402 struct JT *jtl;
|
|
403 int match;
|
|
404 {
|
|
405 rev_common (jtl, match);
|
|
406 rev_sd_hontai (jtl->hontai, match);
|
|
407 return (0);
|
|
408 }
|
|
409
|
|
410 int
|
|
411 rev_rd (jtl, match)
|
|
412 struct JT *jtl;
|
|
413 int match;
|
|
414 {
|
|
415 rev_common (jtl, match);
|
|
416 rev_rd_rind1 (jtl->ri1[D_YOMI], jtl->maxri1[D_YOMI], match);
|
|
417 rev_rd_rind1 (jtl->ri1[D_KANJI], jtl->maxri1[D_KANJI], match);
|
|
418 rev_rd_rind2 (jtl->ri2, jtl->maxri2, match);
|
|
419 return (0);
|
|
420 }
|
|
421
|
|
422 void
|
|
423 rev_common (jtl, match)
|
|
424 struct JT *jtl;
|
|
425 int match;
|
|
426 {
|
|
427 if (jtl->hinsi)
|
|
428 rev_hinsi (jtl->hinsi, jtl->maxserial, match);
|
|
429 if (jtl->kanji)
|
|
430 rev_kanji (jtl->kanji, jtl->maxkanji, match);
|
|
431 if (jtl->comment)
|
|
432 rev_w_char (jtl->comment, jtl->maxcomment);
|
|
433 if (jtl->hinsi_list)
|
|
434 rev_w_char (jtl->hinsi_list, jtl->maxhinsi_list);
|
|
435 }
|
|
436
|
|
437 /* rev_short is called from atod.c.... kanapiiii */
|
|
438 void
|
|
439 rev_short_fun (sp)
|
|
440 w_char *sp;
|
|
441 {
|
|
442 rev_short (sp);
|
|
443 }
|