sj4/lib/sj4core/mkkouho.c

428 lines
9 KiB
C
Raw Normal View History

2008-01-11 11:57:08 +00:00
/*
* Copyright (c) 1991-1994 Sony Corporation
2026-06-19 01:49:53 +09:00
*
2008-01-11 11:57:08 +00:00
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
2026-06-19 01:49:53 +09:00
*
2008-01-11 11:57:08 +00:00
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
2026-06-19 01:49:53 +09:00
*
2008-01-11 11:57:08 +00:00
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL SONY CORPORATION BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2026-06-19 01:49:53 +09:00
*
2008-01-11 11:57:08 +00:00
* Except as contained in this notice, the name of Sony Corporation
* shall not be used in advertising or otherwise to promote the sale, use
* or other dealings in this Software without prior written authorization
* from Sony Corporation.
*
*/
/*
2026-06-19 01:49:53 +09:00
* $SonyRCSfile: mkkouho.c,v $
* $SonyRevision: 1.1 $
2008-01-11 11:57:08 +00:00
* $SonyDate: 1994/06/03 08:02:04 $
*/
#include "sj_euc.h"
#include "sj_kcnv.h"
#include "sj_hinsi.h"
#include "sj_suuji.h"
#include "sj_dict.h"
#include "sj_kanakan.h"
2026-06-20 02:12:04 +09:00
static void getkhtbl(SJ4_CONTEXT CLREC*), cl_kanji(SJ4_CONTEXT JREC*, CLREC*);
static int diffknj(SJ4_CONTEXT JREC*, u_char*, int);
static void cl_numcmn(SJ4_CONTEXT JREC*, CLREC*);
static u_char* makekan(SJ4_CONTEXT u_char*, u_char*, int);
2008-01-11 11:57:08 +00:00
2026-06-20 02:12:04 +09:00
void mkkouho(SJ4_CONTEXT2) {
2026-06-19 01:49:53 +09:00
CLREC* clrec;
int keeplen;
2008-01-11 11:57:08 +00:00
khcount = nkhcount = 0;
2026-06-19 01:49:53 +09:00
keeplen = clt1st->cllen;
2008-01-11 11:57:08 +00:00
clrec = clt1st;
do {
2026-06-20 02:12:04 +09:00
getkhtbl(SJ4_CONTEXT_PASS clrec);
2026-06-19 01:49:53 +09:00
clrec = clrec->clsort;
} while(clrec && (keeplen == clrec->cllen));
2008-01-11 11:57:08 +00:00
}
static void
2026-06-20 02:12:04 +09:00
getkhtbl(SJ4_CONTEXT CLREC* clrec) {
2026-06-19 01:49:53 +09:00
JREC* jrec;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
jrec = clrec->jnode;
2008-01-11 11:57:08 +00:00
2026-06-24 11:39:52 +09:00
switch(jrec->jclass) {
2008-01-11 11:57:08 +00:00
case C_DICT:
2026-06-20 02:12:04 +09:00
cl_kanji(SJ4_CONTEXT_PASS jrec, clrec);
2008-01-11 11:57:08 +00:00
break;
case C_N_ARABIA:
case C_N_ARACMA:
case C_N_KAZU:
case C_N_SUUJI:
case C_N_NANSUU:
case C_N_KANKURA:
case C_N_ARAKURA:
case C_N_KAZULONG:
case C_N_KAZULCMA:
case C_N_SUUJILONG:
2026-06-20 02:12:04 +09:00
cl_numcmn(SJ4_CONTEXT_PASS jrec, clrec);
2008-01-11 11:57:08 +00:00
break;
case C_BUNSETU:
break;
case C_MINASI:
case C_WAKACHI:
2026-06-20 02:12:04 +09:00
setkouho(SJ4_CONTEXT_PASS clrec, (TypeDicOfs)0, 0);
2008-01-11 11:57:08 +00:00
break;
}
}
static void
2026-06-20 02:12:04 +09:00
cl_kanji(SJ4_CONTEXT JREC* jrec, CLREC* clrec) {
2026-06-19 01:49:53 +09:00
u_char* ptr;
2008-01-11 11:57:08 +00:00
int kcount = khcount;
2026-06-20 02:12:04 +09:00
if(seldict(SJ4_CONTEXT_PASS jrec->dicid)) {
(*curdict->getdic)(SJ4_CONTEXT_PASS curdict, jrec->jseg);
2008-01-11 11:57:08 +00:00
ptr = dicbuf + jrec->jofsst + 1;
2026-06-20 02:12:04 +09:00
get_askknj(SJ4_CONTEXT_PASS2);
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
while(*ptr != HINSIBLKTERM) {
2026-06-20 02:12:04 +09:00
if(diffknj(SJ4_CONTEXT_PASS jrec, ptr, kcount)) {
setkouho(SJ4_CONTEXT_PASS clrec, (TypeDicOfs)(ptr - dicbuf), 0);
2008-01-11 11:57:08 +00:00
}
2026-06-19 23:45:47 +09:00
ptr = skipkstr(ptr);
2008-01-11 11:57:08 +00:00
}
2026-06-19 01:49:53 +09:00
} else {
2026-06-20 02:12:04 +09:00
setkouho(SJ4_CONTEXT_PASS clrec, (TypeDicOfs)1, 0);
2008-01-11 11:57:08 +00:00
}
}
2026-06-19 01:49:53 +09:00
u_char*
2026-06-20 02:12:04 +09:00
makekan_none(SJ4_CONTEXT u_char* s, u_char* d, int flg) {
2008-01-11 11:57:08 +00:00
return d;
}
2026-06-19 01:49:53 +09:00
u_char*
2026-06-20 02:12:04 +09:00
makekan_1byte(SJ4_CONTEXT u_char* s, u_char* d, int flg) {
2026-06-19 01:49:53 +09:00
if((*s & KANJIMODEMASK) == ZENHIRAASSYUKU)
*d++ = 0x10 | (*s & KNJASSYUKUMASK);
2008-01-11 11:57:08 +00:00
else
2026-06-19 01:49:53 +09:00
*d++ = 0x90 | (*s & KNJASSYUKUMASK);
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
if(flg) *d++ = KANJISTREND;
2008-01-11 11:57:08 +00:00
return d;
}
2026-06-19 01:49:53 +09:00
u_char*
2026-06-20 02:12:04 +09:00
makekan_knj(SJ4_CONTEXT u_char* s, u_char* d, int flg) {
return makekan(SJ4_CONTEXT_PASS askknj[*s & KNJASSYUKUMASK], d, flg);
2008-01-11 11:57:08 +00:00
}
2026-06-20 02:12:04 +09:00
u_char* makekan_ofs(SJ4_CONTEXT u_char* s, u_char* d, int flg) {
return makekan(SJ4_CONTEXT_PASS dicbuf + ((*s & KANJICODEMASK) << 8) + *(s + 1),
2026-06-19 01:49:53 +09:00
d, flg);
2008-01-11 11:57:08 +00:00
}
2026-06-19 01:49:53 +09:00
u_char*
2026-06-20 02:12:04 +09:00
makekan_norm(SJ4_CONTEXT u_char* s, u_char* d, int flg) {
2008-01-11 11:57:08 +00:00
u_char c;
2026-06-19 01:49:53 +09:00
if(*s != 0) {
2008-01-11 11:57:08 +00:00
c = s[1];
2026-06-19 01:49:53 +09:00
if(c & 0x80)
*d++ = SS3;
2008-01-11 11:57:08 +00:00
*d++ = *s | 0x80;
2026-06-19 01:49:53 +09:00
*d++ = c | 0x80;
2008-01-11 11:57:08 +00:00
} else {
*d++ = *s;
}
2026-06-19 01:49:53 +09:00
if(flg) *d++ = KANJISTREND;
2008-01-11 11:57:08 +00:00
return d;
}
2026-06-19 01:49:53 +09:00
u_char*
2026-06-20 02:12:04 +09:00
makekan_ascii(SJ4_CONTEXT u_char* s, u_char* d, int flg) {
2008-01-11 11:57:08 +00:00
u_char c;
c = s[1];
2026-06-19 01:49:53 +09:00
if(c & 0x80)
*d++ = SS2;
2008-01-11 11:57:08 +00:00
*d++ = c;
2026-06-19 01:49:53 +09:00
if(flg) *d++ = KANJISTREND;
2008-01-11 11:57:08 +00:00
return d;
}
2026-06-19 01:49:53 +09:00
static u_char*
2026-06-20 02:12:04 +09:00
makekan(SJ4_CONTEXT u_char* s, u_char* d, int flg) {
2008-01-11 11:57:08 +00:00
int csize;
2026-06-19 01:49:53 +09:00
for(;;) {
2008-01-11 11:57:08 +00:00
csize = codesize(*s);
2026-06-19 01:49:53 +09:00
if(s[csize] == KANJISTREND) {
2008-01-11 11:57:08 +00:00
switch(*s & KANJIMODEMASK) {
2026-06-19 01:49:53 +09:00
case ZENHIRAASSYUKU:
2026-06-20 02:12:04 +09:00
d = makekan_1byte(SJ4_CONTEXT_PASS s, d, flg);
2026-06-19 01:49:53 +09:00
break;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
case ZENKATAASSYUKU:
2026-06-20 02:12:04 +09:00
d = makekan_1byte(SJ4_CONTEXT_PASS s, d, flg);
2026-06-19 01:49:53 +09:00
break;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
case OFFSETASSYUKU:
2026-06-20 02:12:04 +09:00
d = makekan_ofs(SJ4_CONTEXT_PASS s, d, flg);
2026-06-19 01:49:53 +09:00
break;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
case AIATTRIBUTE:
2026-06-21 08:00:28 +09:00
case KANAKANCOST:
2026-06-20 02:12:04 +09:00
d = makekan_none(SJ4_CONTEXT_PASS s, d, flg);
2026-06-19 01:49:53 +09:00
break;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
case LEADINGHANKAKU:
2026-06-20 02:12:04 +09:00
d = makekan_ascii(SJ4_CONTEXT_PASS s, d, flg);
2026-06-19 01:49:53 +09:00
break;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
case KANJIASSYUKU:
2026-06-20 02:12:04 +09:00
d = makekan_knj(SJ4_CONTEXT_PASS s, d, flg);
2026-06-19 01:49:53 +09:00
break;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
default:
2026-06-20 02:12:04 +09:00
d = makekan_norm(SJ4_CONTEXT_PASS s, d, flg);
2008-01-11 11:57:08 +00:00
}
break;
2026-06-19 01:49:53 +09:00
} else
2008-01-11 11:57:08 +00:00
switch(*s & KANJIMODEMASK) {
2026-06-19 01:49:53 +09:00
case ZENHIRAASSYUKU:
2026-06-20 02:12:04 +09:00
d = makekan_1byte(SJ4_CONTEXT_PASS s, d, FALSE);
2026-06-19 01:49:53 +09:00
break;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
case ZENKATAASSYUKU:
2026-06-20 02:12:04 +09:00
d = makekan_1byte(SJ4_CONTEXT_PASS s, d, FALSE);
2026-06-19 01:49:53 +09:00
break;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
case OFFSETASSYUKU:
2026-06-20 02:12:04 +09:00
d = makekan_ofs(SJ4_CONTEXT_PASS s, d, FALSE);
2026-06-19 01:49:53 +09:00
break;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
case AIATTRIBUTE:
2026-06-21 08:00:28 +09:00
case KANAKANCOST:
2026-06-20 02:12:04 +09:00
d = makekan_none(SJ4_CONTEXT_PASS s, d, FALSE);
2026-06-19 01:49:53 +09:00
break;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
case LEADINGHANKAKU:
2026-06-20 02:12:04 +09:00
d = makekan_ascii(SJ4_CONTEXT_PASS s, d, FALSE);
2026-06-19 01:49:53 +09:00
break;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
case KANJIASSYUKU:
2026-06-20 02:12:04 +09:00
d = makekan_knj(SJ4_CONTEXT_PASS s, d, FALSE);
2026-06-19 01:49:53 +09:00
break;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
default:
2026-06-20 02:12:04 +09:00
d = makekan_norm(SJ4_CONTEXT_PASS s, d, FALSE);
2008-01-11 11:57:08 +00:00
}
s += codesize(*s);
}
return d;
}
2026-06-19 01:49:53 +09:00
#define PEND 2
#define QEND 1
2008-01-11 11:57:08 +00:00
static int
2026-06-19 01:49:53 +09:00
sameknj(u_char* p, u_char plen, u_char* q, u_char qlen) {
int i;
int j;
int endf = 0;
int brkf;
2008-01-11 11:57:08 +00:00
do {
2026-06-19 01:49:53 +09:00
if((i = euc_codesize(*p)) != euc_codesize(*q)) return FALSE;
2008-01-11 11:57:08 +00:00
brkf = 0;
2026-06-19 01:49:53 +09:00
for(j = 0; j < i; j++) {
if(p[j] != q[j]) {
2008-01-11 11:57:08 +00:00
brkf = 1;
break;
}
}
2026-06-19 01:49:53 +09:00
if(brkf) break;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
if(p[i] == KANJISTREND) endf += PEND;
if(q[i] == KANJISTREND) endf += QEND;
2008-01-11 11:57:08 +00:00
p += i;
q += i;
2026-06-19 01:49:53 +09:00
} while(!endf);
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
switch(endf) {
case(PEND | QEND):
if(plen == qlen) return TRUE;
2008-01-11 11:57:08 +00:00
break;
case PEND:
2026-06-19 01:49:53 +09:00
i = plen;
2008-01-11 11:57:08 +00:00
plen = qlen;
qlen = i;
2026-06-19 01:49:53 +09:00
p = q;
2008-01-11 11:57:08 +00:00
case QEND:
2026-06-19 01:49:53 +09:00
if(p[euc_codesize(*p)] != KANJISTREND) break;
if((*p & KANJIMODEMASK) != 0x10) break;
if(qlen != plen - (*p & KANJICODEMASK) - 1) break;
2008-01-11 11:57:08 +00:00
return TRUE;
default:
2026-06-19 01:49:53 +09:00
if(q[euc_codesize(*q)] != KANJISTREND) break;
if(p[euc_codesize(*p)] != KANJISTREND) break;
if((*q & KANJIMODEMASK) != 0x10) break;
if((*p & KANJIMODEMASK) != 0x10) break;
if(plen - (*p & KANJICODEMASK) != qlen - (*q & KANJICODEMASK))
2008-01-11 11:57:08 +00:00
break;
return TRUE;
}
return FALSE;
}
2026-06-19 01:49:53 +09:00
#undef PEND
#undef QEND
2008-01-11 11:57:08 +00:00
static int
2026-06-20 02:12:04 +09:00
diffknj(SJ4_CONTEXT JREC* jrec, u_char* ptr, int num) {
2026-06-19 01:49:53 +09:00
KHREC* kptr;
JREC* jptr;
int i;
u_char kbuf1[MAXWDKANJILEN];
u_char kbuf2[MAXWDKANJILEN];
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
if(jrec->hinsi == TANKANJI) return TRUE;
2008-01-11 11:57:08 +00:00
2026-06-20 02:12:04 +09:00
makekan(SJ4_CONTEXT_PASS ptr, kbuf1, TRUE);
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
for(i = 0, kptr = kouhotbl; i < num; i++, kptr++) {
if((jptr = kptr->clrec->jnode) == jrec)
2008-01-11 11:57:08 +00:00
return FALSE;
2026-06-19 01:49:53 +09:00
if(jptr->hinsi == TANKANJI) continue;
2008-01-11 11:57:08 +00:00
2026-06-24 11:39:52 +09:00
if(jptr->jclass != jrec->jclass) continue;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
if(jptr->dicid != jrec->dicid) continue;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
if(jptr->jseg != jrec->jseg) continue;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
if(jptr->sttofs != jrec->sttofs) continue;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
if(jptr->stbofs != jrec->stbofs) continue;
2008-01-11 11:57:08 +00:00
2026-06-20 02:12:04 +09:00
makekan(SJ4_CONTEXT_PASS dicbuf + kptr->offs, kbuf2, TRUE);
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
if(sameknj(kbuf1, jrec->jlen, kbuf2, jptr->jlen))
2008-01-11 11:57:08 +00:00
return FALSE;
}
return TRUE;
}
static int
2026-06-19 01:49:53 +09:00
chrck_numtbl(u_short flg, u_short cond) {
u_short must;
2008-01-11 11:57:08 +00:00
2026-06-19 02:57:26 +09:00
if((cond = SelNumCond(cond))) {
if((must = (cond & SELNUMMUST))) {
2026-06-19 01:49:53 +09:00
if((flg & must) != must) return FALSE;
2008-01-11 11:57:08 +00:00
cond &= ~must;
}
2026-06-19 01:49:53 +09:00
if(cond && !(cond & flg)) return FALSE;
2008-01-11 11:57:08 +00:00
}
return TRUE;
}
2026-06-19 01:49:53 +09:00
int sel_sjmode(JREC* jrec) {
u_short i;
u_short* p;
u_short* q;
2008-01-11 11:57:08 +00:00
2026-06-24 11:39:52 +09:00
switch(jrec->jclass) {
2008-01-11 11:57:08 +00:00
case C_N_ARABIA:
case C_N_ARACMA:
case C_N_KAZU:
case C_N_SUUJI:
case C_N_NANSUU:
case C_N_KANKURA:
case C_N_ARAKURA:
case C_N_KAZULONG:
case C_N_KAZULCMA:
case C_N_SUUJILONG:
2026-06-24 11:39:52 +09:00
p = q = Selsjadrs(jrec->jclass - C_N_ARABIA);
2008-01-11 11:57:08 +00:00
break;
default:
return SELARB;
}
2026-06-19 01:49:53 +09:00
while((i = *p++) != SELNUMTERM) {
if(chrck_numtbl(jrec->flags, i)) break;
2008-01-11 11:57:08 +00:00
}
2026-06-19 01:49:53 +09:00
if(i == SELNUMTERM) i = *q;
2008-01-11 11:57:08 +00:00
return SelNumFunc(i);
}
static void
2026-06-20 02:12:04 +09:00
cl_numcmn(SJ4_CONTEXT JREC* jrec, CLREC* clrec) {
2026-06-19 01:49:53 +09:00
u_char* p;
int i;
u_short j;
u_short* tbl;
2008-01-11 11:57:08 +00:00
p = cnvstart;
2026-06-19 01:49:53 +09:00
if((i = jrec->sttofs) == SETTOU_DAI)
2008-01-11 11:57:08 +00:00
p += SttYomiLen(Settou_ptr(i));
2026-06-19 01:49:53 +09:00
else if(i == SETTOU_KIGOU)
2008-01-11 11:57:08 +00:00
p++;
2026-06-19 01:49:53 +09:00
if(jrec->jofsst) {
2008-01-11 11:57:08 +00:00
i = sel_sjmode(jrec);
2026-06-20 02:12:04 +09:00
if(seldict(SJ4_CONTEXT_PASS jrec->dicid)) {
(*curdict->getdic)(SJ4_CONTEXT_PASS curdict, jrec->jseg);
2008-01-11 11:57:08 +00:00
p = dicbuf + jrec->jofsst + 1;
2026-06-19 01:49:53 +09:00
while(*p != HINSIBLKTERM) {
2026-06-20 02:12:04 +09:00
setkouho(SJ4_CONTEXT_PASS clrec, (TypeDicOfs)(p - dicbuf), i);
2026-06-19 23:45:47 +09:00
p = skipkstr(p);
2008-01-11 11:57:08 +00:00
}
2026-06-19 01:49:53 +09:00
} else {
2026-06-20 02:12:04 +09:00
setkouho(SJ4_CONTEXT_PASS clrec, (TypeDicOfs)1, i);
2008-01-11 11:57:08 +00:00
}
2026-06-19 01:49:53 +09:00
} else {
2026-06-24 11:39:52 +09:00
tbl = Selsjadrs(jrec->jclass - C_N_ARABIA);
2026-06-19 01:49:53 +09:00
while((j = *tbl++) != SELNUMTERM) {
if(!chrck_numtbl(jrec->flags, j)) continue;
2008-01-11 11:57:08 +00:00
2026-06-20 02:12:04 +09:00
setkouho(SJ4_CONTEXT_PASS clrec, (TypeDicOfs)0, (int)SelNumFunc(j));
2008-01-11 11:57:08 +00:00
}
}
}