Add embedded dictionary

This commit is contained in:
Nishi 2026-06-21 17:52:12 +09:00
commit 2ac92827bc
8 changed files with 103204 additions and 58845 deletions

View file

@ -3,6 +3,8 @@ cmake_minimum_required(VERSION 3.11)
project(sj4) project(sj4)
option(SJ4_GLOBAL "Use global variables or not" OFF) option(SJ4_GLOBAL "Use global variables or not" OFF)
option(SJ4_EMBED "Embed main dictionary or not" OFF)
option(SJ4_EMBED_PLUS "Embed pubdic+ as main dictionary or not" OFF)
file(GLOB_RECURSE SJ4COMMON_SRCS lib/sj4common/**.c) file(GLOB_RECURSE SJ4COMMON_SRCS lib/sj4common/**.c)
add_library(sj4common STATIC ${SJ4COMMON_SRCS}) add_library(sj4common STATIC ${SJ4COMMON_SRCS})
@ -17,6 +19,11 @@ target_link_libraries(sj4core PUBLIC sj4common)
if(SJ4_GLOBAL) if(SJ4_GLOBAL)
target_compile_definitions(sj4core PUBLIC SJ4_GLOBAL) target_compile_definitions(sj4core PUBLIC SJ4_GLOBAL)
endif() endif()
if(SJ4_EMBED_PLUS)
target_compile_definitions(sj4core PUBLIC EMBED_PLUS)
elseif(SJ4_EMBED)
target_compile_definitions(sj4core PUBLIC EMBED)
endif()
file(GLOB_RECURSE SJ4RKCV_SRCS lib/sj4rkcv/**.c) file(GLOB_RECURSE SJ4RKCV_SRCS lib/sj4rkcv/**.c)
add_library(sj4rkcv STATIC ${SJ4RKCV_SRCS}) add_library(sj4rkcv STATIC ${SJ4RKCV_SRCS})

File diff suppressed because it is too large Load diff

View file

@ -113,6 +113,11 @@ typedef struct global {
TypeDicSeg Jpeepidx; TypeDicSeg Jpeepidx;
} Global; } Global;
typedef struct vfile {
u_char* buffer;
u_int size;
} VFile;
typedef struct dictfile { typedef struct dictfile {
DICT dict; DICT dict;
int refcnt; int refcnt;
@ -120,6 +125,7 @@ typedef struct dictfile {
fd_set lock; fd_set lock;
#endif #endif
FILE* fp; FILE* fp;
VFile vf;
int fd; int fd;
u_char* buffer; u_char* buffer;

25968
lib/sj4core/maindic.c Normal file

File diff suppressed because it is too large Load diff

34843
lib/sj4core/maindic2.c Normal file

File diff suppressed because it is too large Load diff

View file

@ -91,7 +91,22 @@ Put4byte(u_char* p, long n) {
#define put4byte(p, n) Put4byte((p), (long)(n)) #define put4byte(p, n) Put4byte((p), (long)(n))
static int static int
fgetfile(FILE* fp, long pos, long len, u_char* p) { fgetfile(VFile* vfp, FILE* fp, long pos, long len, u_char* p) {
if(vfp != NULL && vfp->buffer != NULL) {
if(pos >= vfp->size) {
serv_errno = SJ4_FileSeekError;
return ERROR;
}
if((pos + len) > vfp->size) {
serv_errno = SJ4_FileSeekError;
return ERROR;
}
memcpy(p, vfp->buffer + pos, len);
return SJ4_NormalEnd;
}
if(fseek(fp, pos, 0) == ERROR) { if(fseek(fp, pos, 0) == ERROR) {
serv_errno = SJ4_FileSeekError; serv_errno = SJ4_FileSeekError;
return ERROR; return ERROR;
@ -238,14 +253,28 @@ rszdic(SJ4_CONTEXT DictFile* dp, TypeDicSeg seg) {
DictFile* DictFile*
opendict(SJ4_CONTEXT char* name, char* passwd) { opendict(SJ4_CONTEXT char* name, char* passwd) {
FILE* fp; FILE* fp = NULL;
struct stat sbuf; struct stat sbuf;
DictFile* dfp; DictFile* dfp;
u_char tmp[HEADERLENGTH]; u_char tmp[HEADERLENGTH];
int i; int i;
u_char* dp; u_char* dp;
VFile vf;
if(stat(name, &sbuf) == ERROR) { #ifdef EMBED
memset(&vf, 0, sizeof(vf));
if(strcmp(name, "sj4main.dic") == 0) {
extern u_char sj4main_dic[];
extern u_int sj4main_dic_len;
vf.buffer = sj4main_dic;
vf.size = sj4main_dic_len;
sbuf.st_size = vf.size;
sbuf.st_ino = -1;
} else
#endif
if(stat(name, &sbuf) == ERROR) {
if(errno == ENOENT) if(errno == ENOENT)
serv_errno = SJ4_FileNotExist; serv_errno = SJ4_FileNotExist;
else else
@ -258,7 +287,8 @@ opendict(SJ4_CONTEXT char* name, char* passwd) {
return dfp; return dfp;
} }
if((fp = fopen(name, "r+b")) == NULL) { if(vf.buffer != NULL) {
} else if((fp = fopen(name, "r+b")) == NULL) {
if((fp = fopen(name, "rb")) == NULL) { if((fp = fopen(name, "rb")) == NULL) {
serv_errno = SJ4_CannotOpenFile; serv_errno = SJ4_CannotOpenFile;
return NULL; return NULL;
@ -267,7 +297,7 @@ opendict(SJ4_CONTEXT char* name, char* passwd) {
} else } else
i = TRUE; i = TRUE;
if(fgetfile(fp, 0L, sizeof(tmp), tmp) == ERROR) goto error1; if(fgetfile(&vf, fp, 0L, sizeof(tmp), tmp) == ERROR) goto error1;
if(!check_dictfile(tmp)) { if(!check_dictfile(tmp)) {
serv_errno = SJ4_IllegalDictFile; serv_errno = SJ4_IllegalDictFile;
@ -289,7 +319,7 @@ opendict(SJ4_CONTEXT char* name, char* passwd) {
} }
memset(dfp, '\0', sizeof(*dfp)); memset(dfp, '\0', sizeof(*dfp));
if(fgetfile(fp, 0L, (long)sbuf.st_size, dp) == ERROR) goto error3; if(fgetfile(&vf, fp, 0L, (long)sbuf.st_size, dp) == ERROR) goto error3;
dfp->dict.dicid = sbuf.st_ino; dfp->dict.dicid = sbuf.st_ino;
dfp->dict.idxlen = get4byte(dp + DICTIDXLEN); dfp->dict.idxlen = get4byte(dp + DICTIDXLEN);
@ -304,7 +334,8 @@ opendict(SJ4_CONTEXT char* name, char* passwd) {
dfp->dict.rszdic = (IFunc)rszdic; dfp->dict.rszdic = (IFunc)rszdic;
dfp->refcnt = 1; dfp->refcnt = 1;
dfp->fp = fp; dfp->fp = fp;
dfp->fd = fileno(fp); dfp->vf = vf;
dfp->fd = vf.buffer == NULL ? fileno(fp) : -1;
dfp->buffer = dp; dfp->buffer = dp;
dfp->bufsiz = sbuf.st_size; dfp->bufsiz = sbuf.st_size;
dfp->idxstrt = get4byte(dp + DICTIDXPOS); dfp->idxstrt = get4byte(dp + DICTIDXPOS);
@ -329,7 +360,7 @@ error3:
error2: error2:
free((char*)dp); free((char*)dp);
error1: error1:
fclose(fp); if(fp != NULL) fclose(fp);
return NULL; return NULL;
} }
@ -434,7 +465,7 @@ openstdy(SJ4_CONTEXT char* name, char* passwd) {
goto error0; goto error0;
} }
if(fgetfile(fp, 0L, HEADERLENGTH + COMMENTLENGTH, hd) == ERROR) if(fgetfile(NULL, fp, 0L, HEADERLENGTH + COMMENTLENGTH, hd) == ERROR)
goto error1; goto error1;
if(!check_stdyfile(hd)) { if(!check_stdyfile(hd)) {
@ -476,9 +507,9 @@ openstdy(SJ4_CONTEXT char* name, char* passwd) {
goto error4; goto error4;
} }
if(fgetfile(fp, clidxpos, clidxlen, (u_char*)cip) == ERROR) goto error5; /* XXX */ if(fgetfile(NULL, fp, clidxpos, clidxlen, (u_char*)cip) == ERROR) goto error5; /* XXX */
if(fgetfile(fp, clstdypos, clstdylen, (u_char*)clp) == ERROR) goto error5; /* XXX */ if(fgetfile(NULL, fp, clstdypos, clstdylen, (u_char*)clp) == ERROR) goto error5; /* XXX */
if(fgetfile(fp, stdypos, len, (u_char*)sp) == ERROR) stdycnt = 0; /* XXX */ if(fgetfile(NULL, fp, stdypos, len, (u_char*)sp) == ERROR) stdycnt = 0; /* XXX */
sfp->stdy.stdycnt = stdycnt; sfp->stdy.stdycnt = stdycnt;
sfp->stdy.stdymax = stdymax; sfp->stdy.stdymax = stdymax;

View file

@ -5,7 +5,7 @@ int main(int argc, char** argv) {
int i = 0; int i = 0;
char c[64 * 1024]; char c[64 * 1024];
char b; char b;
Sj4Context* ctx = alloccontext("dic.bin"); Sj4Context* ctx = alloccontext("sj4main.dic");
if(ctx == NULL) { if(ctx == NULL) {
fprintf(stderr, "Failed to create context\n"); fprintf(stderr, "Failed to create context\n");

View file

@ -2,4 +2,4 @@
while [ ! -d .git ]; do while [ ! -d .git ]; do
cd .. cd ..
done done
clang-format --verbose -i `find src lib include "(" -name "*.c" -or -name "*.h" ")" -and -not -name ucstable.h` clang-format --verbose -i `find src lib include "(" -name "*.c" -or -name "*.h" ")" -and -not -name ucstable.h -and -not -name maindic.c`