From f18a7c68b97165c09603ff66a106b942765c2bb3 Mon Sep 17 00:00:00 2001 From: Masanori Ogino Date: Wed, 29 Nov 2023 16:45:47 +0900 Subject: [PATCH] sj3serv: Vendor sj3lib Signed-off-by: Masanori Ogino --- CMakeLists.txt | 7 +- sj3serv/level1.c | 1246 ++++++++++++++++++++++++++++++++++++++++++++++ sj3serv/sj.c | 1184 +++++++++++++++++++++++++++++++++++++++++++ sj3serv/sj3lib.h | 235 +++++++++ sj3serv/string.c | 355 +++++++++++++ 5 files changed, 3025 insertions(+), 2 deletions(-) create mode 100644 sj3serv/level1.c create mode 100644 sj3serv/sj.c create mode 100644 sj3serv/sj3lib.h create mode 100644 sj3serv/string.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 8b4485b..d606264 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -111,13 +111,16 @@ target_include_directories(kanakan ) add_executable(sj3serv + sj3serv/level1.c + sj3serv/sj.c + sj3serv/string.c sj3serv/sj3serv.c ) target_include_directories(sj3serv - PRIVATE include sj3lib + PRIVATE sj3serv include ) target_link_libraries(sj3serv - sj3lib kanakan + kanakan ) add_executable(sj3dic diff --git a/sj3serv/level1.c b/sj3serv/level1.c new file mode 100644 index 0000000..8872ad0 --- /dev/null +++ b/sj3serv/level1.c @@ -0,0 +1,1246 @@ +/*- + * SPDX-License-Identifier: MIT-open-group + * + * Copyright (c) 1991-1994 Sony Corporation + * + * 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: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * 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. + * + * 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. + */ + +#include "sj_sysvdef.h" + +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include + +#include "Const.h" +#include "sj3cmd.h" +#include "sj3err.h" +#include "sj3lib.h" + +#ifdef SVR4 +#define signal sigset +#endif + +int sj3_error_number; +char *sj3_socket_name = SocketName; +char *sj3_port_name = PortName; +int sj3_port_number = PortNumber; + +#define BUFFER_SIZE BUFSIZ +static char *af_unix_str = "unix"; +static u_char putbuf[BUFFER_SIZE]; +static u_char getbuf[BUFFER_SIZE]; +static int putpos = 0; +static int getlen = 0; +static int getpos = 0; +static SJ3_CLIENT_ENV *cliptr; +static int server_fd = -1; + +static int sj3_timeout = 0; + +static int INTLEN = sizeof(int); +static int CMDLEN = sizeof(int); +static int ReadErrorFlag = FALSE; + +#define client_init(p) \ + { \ + cliptr = (p); \ + if ((server_fd = cliptr->fd) == -1) { \ + sj3_error_number = SJ3_NotOpened; \ + return ERROR; \ + } \ + } + +static int +put_flush(void) +{ + int i, j; + u_char *p; + + for (i = putpos, p = putbuf; i > 0; i -= j, p += j) { + if ((j = write(server_fd, p, i)) <= 0) { + shutdown(server_fd, 2); + close(server_fd); + cliptr->fd = server_fd = -1; + sj3_error_number = SJ3_ServerDown; + return (ERROR); + } + } + putpos = 0; + + return 0; +} + +static void +put_byte(int c) +{ + putbuf[putpos++] = c; +} + +static void +put_word(int c) +{ + put_byte(c >> 8); + put_byte(c & 0xff); +} + +static void +put_int(int c) +{ + put_byte(c >> (8 * 3)); + put_byte(c >> (8 * 2)); + put_byte(c >> (8 * 1)); + put_byte(c); +} + +static void +put_cmd(int cmd) +{ + ReadErrorFlag = FALSE; + putpos = getlen = 0; + put_int(cmd); +} + +#define put_string put_ndata + +static u_char * +put_ndata(u_char *p, int n) +{ + while (n-- > 0) + put_byte(p ? *p++ : 0); + return p; +} + +static int +put_over(int buflen, int n, u_char *(*func1)(), u_char *str1, + int len1, u_char *(*func2)(), u_char *str2, int len2, + u_char *(*func3)(), u_char *str3, int len3, + u_char *(*func4)(), u_char *str4, int len4) +{ +#define ARGNUM 4 + u_char *(*func[ARGNUM])(); + u_char *data[ARGNUM]; + int len[ARGNUM]; + int i; + + func[0] = func1; + data[0] = str1; + len[0] = len1; + func[1] = func2; + data[1] = str2; + len[1] = len2; + func[2] = func3; + data[2] = str3; + len[2] = len3; + func[3] = func4; + data[3] = str4; + len[3] = len4; + + for (i = 0; i < n; i++) { + if (len[i] < buflen) { + (*func[i])(data[i], len[i]); + buflen -= len[i]; + } else { + while (len[i] > 0) { + data[i] = (*func[i])(data[i], + (len[i] < buflen) ? len[i] : buflen); + if (put_flush() == ERROR) + return ERROR; + len[i] -= buflen; + buflen = BUFFER_SIZE; + } + } + } + if ((buflen != BUFFER_SIZE) && (put_flush() == ERROR)) + return ERROR; + + return 0; +} + +static int +get_buffer(void) +{ + if (ReadErrorFlag) + return ERROR; + getpos = getlen = 0; + + getlen = read(server_fd, getbuf, sizeof(getbuf)); + if (getlen <= 0) { + shutdown(server_fd, 2); + close(server_fd); + cliptr->fd = server_fd = -1; + sj3_error_number = SJ3_ServerDown; + return ERROR; + } + return getlen; +} + +static u_char +get_byte(void) +{ + if ((getpos >= getlen) && (get_buffer() == ERROR)) { + ReadErrorFlag = TRUE; + return 0; + } + return getbuf[getpos++] & 0xff; +} + +static int +get_word(void) +{ + int i; + + i = get_byte(); + return (i << 8) | get_byte(); +} + +static int +get_int(void) +{ + int i0, i1, i2; + + i0 = get_byte(); + i1 = get_byte(); + i2 = get_byte(); + return (i0 << (8 * 3)) | (i1 << (8 * 2)) | (i2 << (8 * 1)) | get_byte(); +} + +static u_char * +get_string(u_char *p) +{ + int c; + + do { + *p++ = c = get_byte(); + } while (c); + + return p; +} + +static int +get_nstring(u_char *p, int n) +{ + int c; + + c = get_byte(); + while (c) { + if (n > 1) { + *p++ = c; + n--; + } + c = get_byte(); + } + if (n > 0) + *p++ = 0; + return n; +} + +static u_char * +get_ndata(u_char *p, int n) +{ + while (n-- > 0) + *p++ = get_byte(); + return p; +} + +static void +skip_string(void) +{ + while (get_byte()) + ; +} + +static void +skip_ndata(int n) +{ + while (n-- > 0) + get_byte(); +} + +static int +open_unix(void) +{ + int fd, len; + struct sockaddr_un sunix; + + sunix.sun_family = AF_UNIX; + strcpy(sunix.sun_path, sj3_socket_name); + + if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == ERROR) { + sj3_error_number = SJ3_OpenSocket; + return ERROR; + } + + len = strlen(sunix.sun_path) + sizeof(sunix.sun_family); +#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) + len += sizeof(sunix.sun_len); +#endif + if (connect(fd, (struct sockaddr *)&sunix, len) == ERROR) { + sj3_error_number = SJ3_ConnectSocket; + return ERROR; + } + + return fd; +} + +void +sj3_set_timeout(int timeout) +{ + sj3_timeout = timeout; +} + +static void +connect_timeout(void) +{ +} + +static int +open_inet(char *host) +{ + struct hostent *hp; + struct servent *sp; + int fd, port, ret; + struct sockaddr_in sin; + + if (!(hp = gethostbyname(host))) { + sj3_error_number = SJ3_GetHostByName; + return ERROR; + } + + if (sp = getservbyname(sj3_port_name, "tcp")) + port = ntohs(sp->s_port); + else + port = sj3_port_number; + + memset((char *)&sin, '\0', sizeof(sin)); + sin.sin_family = AF_INET; + sin.sin_port = htons(port); + memcpy(&sin.sin_addr, hp->h_addr, hp->h_length); + + if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == ERROR) { + sj3_error_number = SJ3_OpenSocket; + return ERROR; + } + + if (sj3_timeout > 0) { + signal(SIGALRM, (void (*)())connect_timeout); + alarm(sj3_timeout); + } + ret = connect(fd, (struct sockaddr *)&sin, sizeof(sin)); + if (sj3_timeout > 0) { + alarm(0); + signal(SIGALRM, SIG_IGN); + } + if (ret == ERROR) { + sj3_error_number = SJ3_ConnectSocket; + return ERROR; + } + + return fd; +} + +int +sj3_make_connection(SJ3_CLIENT_ENV *client, char *serv, char *user, + char *prog) +{ + char host[MAXHOSTNAMELEN]; + char *curdata; + int buflen, curlen, datalen, hostlen, proglen, tmp, userlen; + + client->fd = -1; + + if (!serv || *serv == '\0' || !strcmp(serv, af_unix_str)) { + if ((server_fd = open_unix()) == ERROR) + return ERROR; + strcpy(host, af_unix_str); + } else { + if ((server_fd = open_inet(serv)) == ERROR) + return ERROR; + gethostname(host, sizeof(host)); + } + client->fd = server_fd; + client_init(client); + + hostlen = strlen(host) + 1; + userlen = strlen(user) + 1; + proglen = strlen(prog) + 1; + datalen = hostlen + userlen + proglen; + + put_cmd(SJ3_CONNECT); + put_int(SJ3SERV_VERSION_NO); + if (datalen <= (buflen = BUFFER_SIZE - CMDLEN - INTLEN)) { + put_string(host, hostlen); + put_string(user, userlen); + put_string(prog, proglen); + if (put_flush() == ERROR) + return ERROR; + } else { + if (put_over(buflen, 3, put_string, host, hostlen, put_string, + user, userlen, put_string, prog, proglen, NULL, NULL, 0) == + ERROR) + return ERROR; + } + + if ((tmp = get_int()) == SJ3_DifferentVersion) { + if (ReadErrorFlag) + return ERROR; + put_cmd(SJ3_CONNECT); + put_int(1); + if (datalen <= (buflen = BUFFER_SIZE - CMDLEN - INTLEN)) { + put_string(host, hostlen); + put_string(user, userlen); + put_string(prog, proglen); + if (put_flush() == ERROR) + return ERROR; + } else { + if (put_over(buflen, 3, put_string, host, hostlen, + put_string, user, userlen, put_string, prog, + proglen, NULL, NULL, 0) == ERROR) + return ERROR; + } + if (tmp = get_int()) { + sj3_erase_connection(client); + sj3_error_number = tmp; + return ERROR; + } + } else if (tmp && (-2 < tmp)) { + sj3_erase_connection(client); + sj3_error_number = tmp; + return ERROR; + } + cliptr->svr_version = ((tmp) ? -tmp : 1); + cliptr->cli_version = SJ3SERV_VERSION_NO; + cliptr->default_char[0] = 0x81; + cliptr->default_char[1] = 0x40; + sj3_error_number = 0; + + if (ReadErrorFlag) + return ERROR; + put_cmd(SJ3_STDYSIZE); + if (put_flush() == ERROR) + return ERROR; + + if (tmp = get_int()) { + sj3_erase_connection(client); + sj3_error_number = tmp; + return ERROR; + } + + cliptr->stdy_size = get_int(); + cliptr->fd = server_fd; + + return ReadErrorFlag ? ERROR : 0; +} + +int +sj3_erase_connection(SJ3_CLIENT_ENV *client) +{ + client_init(client); + + put_cmd(SJ3_DISCONNECT); + if (put_flush() == ERROR) + return ERROR; + + sj3_error_number = get_int(); + close(server_fd); + cliptr->fd = -1; + if (ReadErrorFlag) + return ERROR; + return sj3_error_number ? ERROR : 0; +} + +long +sj3_open_dictionary(SJ3_CLIENT_ENV *client, char *dictname, + char *password) +{ + int buflen, datalen, dictlen, passlen, res; + + client_init(client); + + dictlen = strlen(dictname) + 1; + passlen = ((password) ? strlen(password) : 0) + 1; + datalen = dictlen + passlen; + + put_cmd(SJ3_OPENDICT); + if (datalen < (buflen = BUFFER_SIZE - CMDLEN)) { + put_string(dictname, dictlen); + put_string(password, passlen); + if (put_flush() == ERROR) + return ERROR; + } else { + if (put_over(buflen, 2, put_string, dictname, dictlen, + put_string, password, passlen, NULL, NULL, 0, NULL, NULL, + 0) == ERROR) + return ERROR; + } + + if (sj3_error_number = get_int()) + return 0; + res = get_int(); + return ReadErrorFlag ? ERROR : res; +} + +int +sj3_close_dictionary(SJ3_CLIENT_ENV *client, long dicid) +{ + client_init(client); + + put_cmd(SJ3_CLOSEDICT); + put_int(dicid); + if (put_flush() == ERROR) + return ERROR; + + if (sj3_error_number = get_int()) + return ERROR; + return ReadErrorFlag ? ERROR : 0; +} + +int +sj3_open_study_file(SJ3_CLIENT_ENV *client, char *stdyname, + char *password) +{ + int buflen, datalen, passlen, stdylen; + + client_init(client); + + stdylen = strlen(stdyname) + 1; + passlen = strlen(password) + 1; + datalen = stdylen + passlen; + + put_cmd(SJ3_OPENSTDY); + if (datalen < (buflen = BUFFER_SIZE - CMDLEN)) { + put_string(stdyname, stdylen); + put_string(password, passlen); + if (put_flush() == ERROR) + return ERROR; + } else { + if (put_over(buflen, 2, put_string, stdyname, stdylen, + put_string, password, passlen, NULL, NULL, 0, NULL, NULL, + 0) == ERROR) + return ERROR; + } + + if (sj3_error_number = get_int()) + return ERROR; + return ReadErrorFlag ? ERROR : 0; +} + +int +sj3_close_study_file(SJ3_CLIENT_ENV *client) +{ + client_init(client); + + put_cmd(SJ3_CLOSESTDY); + if (put_flush() == ERROR) + return ERROR; + + if (sj3_error_number = get_int()) + return ERROR; + return ReadErrorFlag ? ERROR : 0; +} + +int +sj3_get_id_size(SJ3_CLIENT_ENV *client) +{ + client_init(client); + + put_cmd(SJ3_STDYSIZE); + if (put_flush() == ERROR) + return ERROR; + + if (sj3_error_number = get_int()) + return ERROR; + cliptr->stdy_size = get_int(); + return ReadErrorFlag ? ERROR : cliptr->stdy_size; +} + +int +sj3_lock_server(SJ3_CLIENT_ENV *client) +{ + client_init(client); + + put_cmd(SJ3_LOCK); + if (put_flush() == ERROR) + return ERROR; + + if (sj3_error_number = get_int()) + return ERROR; + return ReadErrorFlag ? ERROR : 0; +} + +int +sj3_unlock_server(SJ3_CLIENT_ENV *client) +{ + client_init(client); + + put_cmd(SJ3_UNLOCK); + if (put_flush() == ERROR) + return ERROR; + + if (sj3_error_number = get_int()) + return ERROR; + return ReadErrorFlag ? ERROR : 0; +} + +int +sj3_ikkatu_henkan(SJ3_CLIENT_ENV *client, u_char *src, u_char *dst, + int dstsiz, int mb_flag) +{ + int c, len, len1, result, stysiz; + u_char *top; + + int buflen, srclen; + + client_init(client); + + srclen = strlen(src) + 1; + + if (mb_flag == MBCODE_SJIS) + put_cmd(SJ3_PH2KNJ); + else + put_cmd(SJ3_PH2KNJ_EUC); + if (srclen < (buflen = BUFFER_SIZE - CMDLEN)) { + put_string(src, srclen); + if (put_flush() == ERROR) + return ERROR; + } else { + if (put_over(buflen, 1, put_string, src, srclen, NULL, NULL, 0, + NULL, NULL, 0, NULL, NULL, 0) == ERROR) + return ERROR; + } + + if (sj3_error_number = get_int()) + return ERROR; + + result = get_int(); + + result = 0; + stysiz = cliptr->stdy_size; + len1 = 1 + stysiz + 1 + 1; + while (c = get_byte()) { + top = dst; + if (dstsiz < len1) + goto error1; + + *dst++ = len = c; + dst = get_ndata(dst, stysiz); + dstsiz -= (stysiz + 1); + + while (c = get_byte()) { + if (dstsiz-- < 3) + goto error2; + *dst++ = c; + } + *dst++ = 0; + dstsiz--; + result += len; + } + + *dst = 0; + return ReadErrorFlag ? ERROR : result; + +error1: + do { + skip_ndata(stysiz); + error2: + while (get_byte()) + ; + } while (get_byte()); + + *top = 0; + return ReadErrorFlag ? ERROR : result; +} + +int +sj3_bunsetu_henkan(SJ3_CLIENT_ENV *client, u_char *yomi, int len, + u_char *kanji, int mb_flag) +{ + int buflen, result; + + client_init(client); + + if (mb_flag == MBCODE_SJIS) + put_cmd(SJ3_CL2KNJ); + else + put_cmd(SJ3_CL2KNJ_EUC); + put_int(len); + if ((len + 1) <= (buflen = BUFFER_SIZE - CMDLEN - INTLEN)) { + put_ndata(yomi, len); + put_byte(0); + if (put_flush() == ERROR) + return ERROR; + } else { + if (put_over(buflen, 2, put_ndata, yomi, len, put_ndata, NULL, + 1, NULL, NULL, 0, NULL, NULL, 0) == ERROR) + return ERROR; + } + + if (sj3_error_number = get_int()) + return ERROR; + + result = get_int(); + get_string(get_ndata(kanji, cliptr->stdy_size)); + return ReadErrorFlag ? ERROR : result; +} + +int +sj3_bunsetu_jikouho(SJ3_CLIENT_ENV *client, u_char *kanji, int mode, + int mb_flag) +{ + int result; + + client_init(client); + + if (mb_flag == MBCODE_SJIS) + put_cmd(SJ3_NEXTCL); + else + put_cmd(SJ3_NEXTCL_EUC); + put_int(mode); + if (put_flush() == ERROR) + return ERROR; + + if (sj3_error_number = get_int()) + return ERROR; + + result = get_int(); + get_string(get_ndata(kanji, cliptr->stdy_size)); + return ReadErrorFlag ? ERROR : result; +} + +int +sj3_bunsetu_maekouho(SJ3_CLIENT_ENV *client, u_char *kanji, int mode, + int mb_flag) +{ + int result; + + client_init(client); + + if (mb_flag == MBCODE_SJIS) + put_cmd(SJ3_PREVCL); + else + put_cmd(SJ3_PREVCL_EUC); + put_int(mode); + if (put_flush() == ERROR) + return ERROR; + + if (sj3_error_number = get_int()) + return ERROR; + + result = get_int(); + get_string(get_ndata(kanji, cliptr->stdy_size)); + return ReadErrorFlag ? ERROR : result; +} + +int +sj3_bunsetu_kouhosuu(SJ3_CLIENT_ENV *client, u_char *yomi, int len, + int mb_flag) +{ + int buflen, result; + + client_init(client); + + if (mb_flag == MBCODE_SJIS) + put_cmd(SJ3_CL2KNJ_CNT); + else + put_cmd(SJ3_CL2KNJ_CNT_EUC); + put_int(len); + if ((len + 1) <= (buflen = BUFFER_SIZE - CMDLEN - INTLEN)) { + put_ndata(yomi, len); + put_byte(0); + if (put_flush() == ERROR) + return ERROR; + } else { + if (put_over(buflen, 2, put_ndata, yomi, len, put_ndata, NULL, + 1, NULL, NULL, 0, NULL, NULL, 0) == ERROR) + return ERROR; + } + + if (sj3_error_number = get_int()) + return ERROR; + + result = get_int(); + return ReadErrorFlag ? ERROR : result; +} + +int +sj3_bunsetu_zenkouho(SJ3_CLIENT_ENV *client, u_char *yomi, int len, + SJ3_DOUON *douon, int mb_flag) +{ + int cnt = 0; + int buflen; + + client_init(client); + + if (mb_flag == MBCODE_SJIS) + put_cmd(SJ3_CL2KNJ_ALL); + else + put_cmd(SJ3_CL2KNJ_ALL_EUC); + put_int(len); + if ((len + 1) <= (buflen = BUFFER_SIZE - CMDLEN - INTLEN)) { + put_ndata(yomi, len); + put_byte(0); + if (put_flush() == ERROR) + return ERROR; + } else { + if (put_over(buflen, 2, put_ndata, yomi, len, put_ndata, NULL, + 1, NULL, NULL, 0, NULL, NULL, 0) == ERROR) + return ERROR; + } + + if (sj3_error_number = get_int()) + return ERROR; + + while (get_int()) { + get_ndata(&(douon->dcid), cliptr->stdy_size); + get_string(douon->ddata); + douon->dlen = strlen(douon->ddata); + douon++; + cnt++; + } + + return ReadErrorFlag ? ERROR : cnt; +} + +int +sj3_tango_gakusyuu(SJ3_CLIENT_ENV *client, SJ3_STUDYREC *stdy) +{ + int buflen; + + client_init(client); + + put_cmd(SJ3_STUDY); + if (cliptr->stdy_size <= (buflen = BUFFER_SIZE - CMDLEN)) { + put_ndata(stdy, cliptr->stdy_size); + if (put_flush() == ERROR) + return ERROR; + } else { + if (put_over(buflen, 1, put_ndata, stdy, cliptr->stdy_size, + NULL, NULL, 0, NULL, NULL, 0, NULL, NULL, 0) == ERROR) + return ERROR; + } + + if (sj3_error_number = get_int()) + return ERROR; + return ReadErrorFlag ? ERROR : 0; +} + +int +sj3_bunsetu_gakusyuu(SJ3_CLIENT_ENV *client, u_char *yomi1, u_char *yomi2, + SJ3_STUDYREC *stdy, int mb_flag) +{ + int buflen, datalen, yomilen1, yomilen2; + + client_init(client); + + yomilen1 = strlen(yomi1) + 1; + yomilen2 = strlen(yomi2) + 1; + datalen = yomilen1 + yomilen2 + cliptr->stdy_size; + + if (mb_flag == MBCODE_SJIS) + put_cmd(SJ3_CLSTUDY); + else + put_cmd(SJ3_CLSTUDY_EUC); + if (datalen <= (buflen = BUFFER_SIZE - CMDLEN)) { + put_string(yomi1, yomilen1); + put_string(yomi2, yomilen2); + put_ndata(stdy, cliptr->stdy_size); + if (put_flush() == ERROR) + return ERROR; + } else { + if (put_over(buflen, 3, put_string, yomi1, yomilen1, + put_string, yomi2, yomilen2, put_ndata, stdy, + cliptr->stdy_size, NULL, NULL, 0) == ERROR) + return ERROR; + } + + if (sj3_error_number = get_int()) + return ERROR; + return ReadErrorFlag ? ERROR : 0; +} + +int +sj3_tango_touroku(SJ3_CLIENT_ENV *client, long dicid, u_char *yomi, + u_char *kanji, int code, int mb_flag) +{ + int buflen, datalen, kanjilen, yomilen; + + client_init(client); + + yomilen = strlen(yomi) + 1; + kanjilen = strlen(kanji) + 1; + datalen = yomilen + kanjilen + INTLEN; + + if (mb_flag == MBCODE_SJIS) + put_cmd(SJ3_ADDDICT); + else + put_cmd(SJ3_ADDDICT_EUC); + put_int(dicid); + if (datalen <= (buflen = BUFFER_SIZE - CMDLEN - INTLEN)) { + put_string(yomi, yomilen); + put_string(kanji, kanjilen); + put_int(code); + if (put_flush() == ERROR) + return ERROR; + } else { + if (put_over(buflen, 3, put_string, yomi, yomilen, put_string, + kanji, kanjilen, put_ndata, &code, INTLEN, NULL, NULL, 0) == + ERROR) + return ERROR; + } + + if (sj3_error_number = get_int()) + return ERROR; + return ReadErrorFlag ? ERROR : 0; +} + +int +sj3_tango_sakujo(SJ3_CLIENT_ENV *client, long dicid, u_char *yomi, + u_char *kanji, int code, int mb_flag) +{ + int buflen, datalen, kanjilen, yomilen; + + client_init(client); + + yomilen = strlen(yomi) + 1; + kanjilen = strlen(kanji) + 1; + datalen = yomilen + kanjilen + INTLEN; + + if (mb_flag == MBCODE_SJIS) + put_cmd(SJ3_DELDICT); + else + put_cmd(SJ3_DELDICT_EUC); + put_int(dicid); + if (datalen <= (buflen = BUFFER_SIZE - CMDLEN - INTLEN)) { + put_string(yomi, yomilen); + put_string(kanji, kanjilen); + put_int(code); + if (put_flush() == ERROR) + return ERROR; + } else { + if (put_over(buflen, 3, put_string, yomi, yomilen, put_string, + kanji, kanjilen, put_ndata, &code, INTLEN, NULL, NULL, 0) == + ERROR) + return ERROR; + } + + if (sj3_error_number = get_int()) + return ERROR; + return ReadErrorFlag ? ERROR : 0; +} + +int +sj3_tango_syutoku(SJ3_CLIENT_ENV *client, int dicid, u_char *buf, + int mb_flag) +{ + u_char *p; + + client_init(client); + + if (mb_flag == MBCODE_SJIS) + put_cmd(SJ3_GETDICT); + else + put_cmd(SJ3_GETDICT_EUC); + put_int(dicid); + if (put_flush() == ERROR) + return ERROR; + + if (sj3_error_number = get_int()) + return ERROR; + + p = get_string(buf); + p = get_string(p); + *p = get_int(); + return ReadErrorFlag ? ERROR : 0; +} + +int +sj3_tango_jikouho(SJ3_CLIENT_ENV *client, int dicid, u_char *buf, + int mb_flag) +{ + u_char *p; + + client_init(client); + + if (mb_flag == MBCODE_SJIS) + put_cmd(SJ3_NEXTDICT); + else + put_cmd(SJ3_NEXTDICT_EUC); + put_int(dicid); + if (put_flush() == ERROR) + return ERROR; + + if (sj3_error_number = get_int()) + return ERROR; + + p = get_string(buf); + p = get_string(p); + *p = get_int(); + return ReadErrorFlag ? ERROR : 0; +} + +int +sj3_tango_maekouho(SJ3_CLIENT_ENV *client, int dicid, u_char *buf, + int mb_flag) +{ + u_char *p; + + client_init(client); + + if (mb_flag == MBCODE_SJIS) + put_cmd(SJ3_PREVDICT); + else + put_cmd(SJ3_PREVDICT_EUC); + put_int(dicid); + if (put_flush() == ERROR) + return ERROR; + + if (sj3_error_number = get_int()) + return ERROR; + + p = get_string(buf); + p = get_string(p); + *p = get_int(); + return ReadErrorFlag ? ERROR : 0; +} + +int +sj3_make_dict_file(SJ3_CLIENT_ENV *client, char *path, int idxlen, + int seglen, int segnum) +{ + int buflen, datalen, pathlen; + + client_init(client); + + pathlen = strlen(path) + 1; + datalen = pathlen + INTLEN + INTLEN + INTLEN; + + put_cmd(SJ3_MAKEDICT); + if (datalen <= (buflen = BUFFER_SIZE - CMDLEN)) { + put_string(path, pathlen); + put_int(idxlen); + put_int(seglen); + put_int(segnum); + if (put_flush() == ERROR) + return ERROR; + } else { + if (put_over(buflen, 4, put_string, path, pathlen, put_ndata, + &idxlen, INTLEN, put_ndata, &seglen, INTLEN, put_ndata, + &segnum, 0) == ERROR) + return ERROR; + } + + if (sj3_error_number = get_int()) + return ERROR; + return ReadErrorFlag ? ERROR : 0; +} + +int +sj3_make_study_file(SJ3_CLIENT_ENV *client, char *path, int stynum, + int clstep, int cllen) +{ + int buflen, datalen, pathlen; + + client_init(client); + + pathlen = strlen(path) + 1; + datalen = pathlen + INTLEN + INTLEN + INTLEN; + + put_cmd(SJ3_MAKESTDY); + if (datalen <= (buflen = BUFFER_SIZE - CMDLEN)) { + put_string(path, pathlen); + put_int(stynum); + put_int(clstep); + put_int(cllen); + if (put_flush() == ERROR) + return ERROR; + } else { + if (put_over(buflen, 4, put_string, path, pathlen, put_ndata, + &stynum, INTLEN, put_ndata, &clstep, INTLEN, put_ndata, + &cllen, 0) == ERROR) + return ERROR; + } + + if (sj3_error_number = get_int()) + return ERROR; + return ReadErrorFlag ? ERROR : 0; +} + +int +sj3_make_directory(SJ3_CLIENT_ENV *client, char *path) +{ + int buflen, pathlen; + + client_init(client); + + pathlen = strlen(path) + 1; + + put_cmd(SJ3_MAKEDIR); + if (pathlen <= (buflen = BUFFER_SIZE - CMDLEN)) { + put_string(path, pathlen); + if (put_flush() == ERROR) + return ERROR; + } else { + if (put_over(buflen, 1, put_string, path, pathlen, NULL, NULL, + 0, NULL, NULL, 0, NULL, NULL, 0) == ERROR) + return ERROR; + } + + if (sj3_error_number = get_int()) + return ERROR; + return ReadErrorFlag ? ERROR : 0; +} + +int +sj3_access(SJ3_CLIENT_ENV *client, char *path, int mode) +{ + int buflen, datalen, pathlen, result; + + client_init(client); + + pathlen = strlen(path) + 1; + datalen = pathlen + INTLEN; + + put_cmd(SJ3_ACCESS); + if (datalen <= (buflen = BUFFER_SIZE - CMDLEN)) { + put_string(path, pathlen); + put_int(mode); + if (put_flush() == ERROR) + return ERROR; + } else { + if (put_over(buflen, 2, put_string, path, pathlen, put_ndata, + &mode, INTLEN, NULL, NULL, 0, NULL, NULL, 0) == ERROR) + return ERROR; + } + + sj3_error_number = 0; + result = get_int(); + return ReadErrorFlag ? ERROR : result; +} + +int +sj3_who(SJ3_CLIENT_ENV *client, SJ3_WHO_STRUCT *ret, int num) +{ + int i, j; + + client_init(client); + + put_cmd(SJ3_WHO); + if (put_flush() == ERROR) + return ERROR; + + if ((i = get_int()) < 0) { + sj3_error_number = SJ3_InternalError; + return -1; + } + + sj3_error_number = 0; + for (j = 0; j < i; j++) { + if (j < num) { + ret->fd = get_int(); + get_nstring(ret->hostname, SJ3_NAME_LENGTH); + get_nstring(ret->username, SJ3_NAME_LENGTH); + get_nstring(ret->progname, SJ3_NAME_LENGTH); + ret++; + } else { + get_int(); + skip_string(); + skip_string(); + skip_string(); + } + } + + return ReadErrorFlag ? ERROR : ((i < num) ? i : num); +} + +int +sj3_quit(SJ3_CLIENT_ENV *client) +{ + client_init(client); + + put_cmd(SJ3_QUIT); + if (put_flush() == ERROR) + return ERROR; + + if (sj3_error_number = get_int()) + return ERROR; + return ReadErrorFlag ? ERROR : 0; +} + +int +sj3_kill(SJ3_CLIENT_ENV *client) +{ + client_init(client); + + put_cmd(SJ3_KILL); + if (put_flush() == ERROR) + return ERROR; + + if (sj3_error_number = get_int()) + return ERROR; + return ReadErrorFlag ? ERROR : 0; +} + +int +sj3_version(SJ3_CLIENT_ENV *client, char *dst, int dstsiz) +{ + int c; + + client_init(client); + + put_cmd(SJ3_VERSION); + if (put_flush() == ERROR) + return ERROR; + + sj3_error_number = get_int(); + c = get_byte(); + while (c) { + while (c) { + if (dstsiz > 2) { + *dst++ = c; + dstsiz--; + } + c = get_byte(); + } + if (dstsiz > 1) { + *dst++ = 0; + dstsiz--; + } + c = get_byte(); + } + if (dstsiz > 0) { + *dst++ = 0; + dstsiz--; + } + return ReadErrorFlag ? ERROR : 0; +} diff --git a/sj3serv/sj.c b/sj3serv/sj.c new file mode 100644 index 0000000..e78b8fa --- /dev/null +++ b/sj3serv/sj.c @@ -0,0 +1,1184 @@ +/*- + * SPDX-License-Identifier: MIT-open-group + * + * Copyright (c) 1991-1994 Sony Corporation + * + * 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: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * 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. + * + * 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. + */ + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "Const.h" +#include "sj3err.h" +#include "sj3lib.h" +#include "sj_const.h" +#include "sj_hinsi.h" + +extern int sj3_error_number; + +#define SYS_SJIS 0 +#define SYS_EUC 1 +#define SYS_NOTDEF -1 +static int _sys_code = SYS_NOTDEF; + +char *sj3_user_dir = "user"; +static char *path_delimiter = "/"; +static SJ3_CLIENT_ENV client = {-1, 0}; +static long mdicid = 0; +static long udicid = 0; +static int defuse = 0; +static long *dicid_list = NULL; +static int dicid_num = 0; + +static u_char buf1[YomiBufSize]; +static u_char buf2[YomiBufSize]; +static u_char kbuf[KanjiBufSize]; + +static int +set_sys_code(void) +{ + char *loc; + + loc = setlocale(LC_CTYPE, NULL); + if (strcmp(loc, "ja_JP.SJIS") == 0) + return SYS_SJIS; + + return SYS_EUC; +} + +static int +make_dirs(char *path) +{ + char tmp[PathNameLen]; + char *p; + int i; + + for (p = path; *p; p++) { + if (*p != *path_delimiter) + continue; + + strncpy(tmp, path, (i = p - path)); + tmp[i] = '\0'; + if (sj3_access(&client, tmp, F_OK) != ERROR) + continue; + if (sj3_error_number == SJ3_ServerDown) + return ERROR; + + if (sj3_make_directory(&client, tmp) == ERROR) + return ERROR; + } + return 0; +} + +int +sj3_open(char *host, char *user) +{ + char tmp[PathNameLen]; + char *p; + int err = SJ3_NORMAL_END; + + if (client.fd != -1) + return SJ3_ALREADY_CONNECTED; + + sprintf(tmp, "%d.sj3lib", getpid()); + if (sj3_make_connection(&client, host, user, tmp) == ERROR) { + if (sj3_error_number == SJ3_ServerDown) + goto server_dead; + client.fd = -1; + return SJ3_CONNECT_ERROR; + } + + if (client.stdy_size > SJ3_WORD_ID_SIZE) { + sj3_erase_connection(&client); + return SJ3_CONNECT_ERROR; + } + + mdicid = sj3_open_dictionary(&client, MainDictionary, NULL); + if (mdicid == 0) { + if (sj3_error_number == SJ3_ServerDown) + goto server_dead; + err |= SJ3_CANNOT_OPEN_MDICT; + } + + strcpy(tmp, sj3_user_dir); + if (tmp[strlen(tmp) - 1] != *path_delimiter) + strcat(tmp, path_delimiter); + strcat(tmp, user); + strcat(tmp, path_delimiter); + if (make_dirs(tmp) == ERROR) { + if (sj3_error_number == SJ3_ServerDown) + goto server_dead; + return err | SJ3_CANNOT_MAKE_UDIR; + } + + for (p = tmp; *p; p++) + ; + strcpy(p, UserDictionary); + if (sj3_access(&client, tmp, F_OK) == ERROR) { + if (sj3_error_number == SJ3_ServerDown) + goto server_dead; + if (sj3_make_dict_file(&client, tmp, DefIdxLen, DefSegLen, + DefSegNum) == ERROR) { + if (sj3_error_number == SJ3_ServerDown) + goto server_dead; + err |= SJ3_CANNOT_MAKE_UDICT; + } + } + udicid = sj3_open_dictionary(&client, tmp, ""); + if (udicid == 0) { + if (sj3_error_number == SJ3_ServerDown) + goto server_dead; + err |= SJ3_CANNOT_OPEN_UDICT; + } + + strcpy(p, StudyFile); + if (sj3_access(&client, tmp, F_OK) == ERROR) { + if (sj3_error_number == SJ3_ServerDown) + goto server_dead; + if (sj3_make_study_file(&client, tmp, DefStyNum, DefClStep, + DefClLen) == ERROR) { + if (sj3_error_number == SJ3_ServerDown) + goto server_dead; + err |= SJ3_CANNOT_MAKE_STUDY; + } + } + if (sj3_open_study_file(&client, tmp, "") == ERROR) { + if (sj3_error_number == SJ3_ServerDown) + goto server_dead; + err |= SJ3_CANNOT_OPEN_STUDY; + } + + return err; + +server_dead: + mdicid = udicid = 0; + return SJ3_SERVER_DEAD; +} + +int +sj3_open_with_list(char *host, char *user, int dicts_num, char **dicts, + int *error_num, int **error_index) +{ + char tmp[PathNameLen]; + char *p; + int err = SJ3_NORMAL_END; + int err_id_num = 0; + int i; + long *dict_idx = NULL; + + if (client.fd != -1) + return SJ3_ALREADY_CONNECTED; + + sprintf(tmp, "%d.sj3lib", getpid()); + if (sj3_make_connection(&client, host, user, tmp) == ERROR) { + if (sj3_error_number == SJ3_ServerDown) + goto server_dead; + client.fd = -1; + return SJ3_CONNECT_ERROR; + } + + if (client.stdy_size > SJ3_WORD_ID_SIZE) { + sj3_erase_connection(&client); + return SJ3_CONNECT_ERROR; + } + + if ((dicts_num > 0) && dicts) { + if (!(dict_idx = (long *)malloc(sizeof(long) * dicts_num * 2))) + return SJ3_CONNECT_ERROR; + memset(dict_idx, 0, sizeof(long) * dicts_num * 2); + dicid_num = dicts_num; + dicid_list = dict_idx; + err_id_num = 0; + for (i = 0; i < dicts_num; i++) { + dict_idx[i] = sj3_open_dictionary(&client, dicts[i], + NULL); + if (dict_idx[i] == 0) { + if (sj3_error_number == SJ3_ServerDown) + goto server_dead; + dict_idx[dicts_num + err_id_num] = i; + err_id_num++; + } else if (mdicid == 0) { + mdicid = dict_idx[i]; + } + } + + if (err_id_num == dicts_num) { + err |= SJ3_CANNOT_OPEN_MDICT; + mdicid = 0; + } + if (error_num) + *error_num = err_id_num; + if (error_index) + *error_index = (int *)&(dict_idx[dicts_num]); + } + + strcpy(tmp, sj3_user_dir); + if (tmp[strlen(tmp) - 1] != *path_delimiter) + strcat(tmp, path_delimiter); + strcat(tmp, user); + strcat(tmp, path_delimiter); + if (make_dirs(tmp) == ERROR) { + if (sj3_error_number == SJ3_ServerDown) + goto server_dead; + return err | SJ3_CANNOT_MAKE_UDIR; + } + + for (p = tmp; *p; p++) + ; + strcpy(p, UserDictionary); + if (sj3_access(&client, tmp, F_OK) == ERROR) { + if (sj3_error_number == SJ3_ServerDown) + goto server_dead; + if (sj3_make_dict_file(&client, tmp, DefIdxLen, DefSegLen, + DefSegNum) == ERROR) { + if (sj3_error_number == SJ3_ServerDown) + goto server_dead; + err |= SJ3_CANNOT_MAKE_UDICT; + } + } + + udicid = sj3_open_dictionary(&client, tmp, ""); + if (udicid == 0) { + if (sj3_error_number == SJ3_ServerDown) + goto server_dead; + err |= SJ3_CANNOT_OPEN_UDICT; + } + + strcpy(p, StudyFile); + if (sj3_access(&client, tmp, F_OK) == ERROR) { + if (sj3_error_number == SJ3_ServerDown) + goto server_dead; + if (sj3_make_study_file(&client, tmp, DefStyNum, DefClStep, + DefClLen) == ERROR) { + if (sj3_error_number == SJ3_ServerDown) + goto server_dead; + err |= SJ3_CANNOT_MAKE_STUDY; + } + } + if (sj3_open_study_file(&client, tmp, "") == ERROR) { + if (sj3_error_number == SJ3_ServerDown) + goto server_dead; + err |= SJ3_CANNOT_OPEN_STUDY; + } + + return err; + +server_dead: + if (dict_idx) { + free(dict_idx); + dict_idx = NULL; + if (error_num) + *error_num = 0; + if (error_index) + *error_index = NULL; + } + mdicid = udicid = 0; + return SJ3_SERVER_DEAD; +} + +int +sj3_close(void) +{ + int err = SJ3_NORMAL_END; + int i; + + if (client.fd == -1) + return SJ3_NOT_CONNECTED; + + if (mdicid == 0) { + err |= SJ3_NOT_OPENED_MDICT; + } else if (dicid_list) { + for (i = 0; i < dicid_num; i++) { + if (sj3_close_dictionary(&client, dicid_list[i]) == + ERROR) { + if (sj3_error_number == SJ3_ServerDown) + goto server_dead; + err |= SJ3_CLOSE_MDICT_ERROR; + } + } + free(dicid_list); + dicid_list = NULL; + dicid_num = 0; + } else if (sj3_close_dictionary(&client, mdicid) == ERROR) { + if (sj3_error_number == SJ3_ServerDown) + goto server_dead; + err |= SJ3_CLOSE_MDICT_ERROR; + } + mdicid = 0; + + if (udicid == 0) { + err |= SJ3_NOT_OPENED_UDICT; + } else if (sj3_close_dictionary(&client, udicid) == ERROR) { + if (sj3_error_number == SJ3_ServerDown) + goto server_dead; + err |= SJ3_CLOSE_UDICT_ERROR; + } + udicid = 0; + + if (sj3_close_study_file(&client) == ERROR) { + switch (sj3_error_number) { + case SJ3_ServerDown: + goto server_dead; + + case SJ3_StdyFileNotOpened: + err |= SJ3_NOT_OPENED_STUDY; + break; + + default: + err |= SJ3_CLOSE_STUDY_ERROR; + break; + } + } + + if (sj3_erase_connection(&client)) { + if (sj3_error_number == SJ3_ServerDown) + goto server_dead; + err |= SJ3_DISCONNECT_ERROR; + } + + return err; + +server_dead: + if (dicid_list) { + free(dicid_list); + dicid_list = NULL; + } + dicid_num = 0; + mdicid = udicid = 0; + return SJ3_SERVER_DEAD; +} + +int +sj3_getkan(u_char *yomi, SJ3_BUNSETU *bun, u_char *knj, int knjsiz) +{ + u_char *src; + int buncnt = 0; + int stysiz = client.stdy_size; + int len; + + if ((len = strlen(yomi)) > SJ3_IKKATU_YOMI) + return 0; + + while (*yomi) { + len = sj3_ikkatu_henkan(&client, yomi, knj, knjsiz, + MBCODE_SJIS); + if (len == ERROR) { + if (client.fd < 0) { + mdicid = udicid = 0; + return -1; + } + + return 0; + } else if (len == 0) { + break; + } + + src = knj; + while (*src) { + bun->srclen = *src++; + memcpy(&(bun->dcid), src, stysiz); + src += stysiz; + bun->destlen = strlen(src); + bun->srcstr = yomi; + bun->deststr = knj; + while (*src) + *knj++ = *src++; + knjsiz -= bun->destlen; + src++; + yomi += bun->srclen; + bun++; + buncnt++; + } + *knj = 0; + } + + if (*yomi) { + bun->srclen = strlen(yomi); + bun->srcstr = yomi; + bun->destlen = 0; + bun->deststr = NULL; + memset(&(bun->dcid), '\0', sizeof(bun->dcid)); + buncnt++; + } + + return buncnt; +} + +int +sj3_getkan_euc(u_char *yomi, SJ3_BUNSETU *bun, u_char *knj, int knjsiz) +{ + u_char *khp, *kp, *src, *yp; + int buncnt = 0; + int mflag = 0; + int stysiz = client.stdy_size; + int knjorg = knjsiz; + int flag, i, len, saigo, sentou; + SJ3_BUNSETU *hbun = bun; + + if ((len = strlen(yomi)) > SJ3_IKKATU_YOMI) + return 0; + if (client.svr_version == 1) { + defuse = 0; + len = sj3_str_euctosjis(buf1, sizeof(buf1), yomi, + client.default_char, &defuse); + if ((len < 0) || defuse) + return 0; + yp = buf1; + if (knjsiz > sizeof(kbuf)) { + mflag = 1; + kp = khp = (u_char *)malloc(knjsiz); + } else { + kp = khp = kbuf; + } + flag = MBCODE_SJIS; + } else { + yp = yomi; + kp = knj; + flag = MBCODE_EUC; + } + + while (*yp) { + len = sj3_ikkatu_henkan(&client, yp, kp, knjsiz, flag); + if (len == ERROR) { + if (client.fd < 0) { + mdicid = udicid = 0; + return -1; + } + + return 0; + } else if (len == 0) { + break; + } + + src = kp; + while (*src) { + bun->srclen = *src++; + memcpy(&(bun->dcid), src, stysiz); + src += stysiz; + bun->destlen = strlen(src); + bun->srcstr = yp; + bun->deststr = kp; + while (*src) + *kp++ = *src++; + knjsiz -= bun->destlen; + src++; + yp += bun->srclen; + bun++; + buncnt++; + } + *kp = 0; + } + + if (*yp) { + bun->srclen = strlen(yp); + bun->srcstr = yp; + bun->destlen = 0; + bun->deststr = NULL; + memset(&(bun->dcid), '\0', sizeof(bun->dcid)); + buncnt++; + } + + if (client.svr_version == 1) { + kp = khp; + yp = buf1; + bun = hbun; + defuse = 0; + len = sj3_str_sjistoeuc(knj, knjorg, kp, client.default_char, + &defuse); + if ((len < 0) || defuse) + return 0; + for (i = 0; i < buncnt; i++) { + sentou = sj3_sjistoeuclen(yp, bun[i].srcstr - yp); + saigo = sj3_sjistoeuclen(bun[i].srcstr, bun[i].srclen); + bun[i].srclen = saigo; + bun[i].srcstr = yomi + sentou; + sentou = sj3_sjistoeuclen(kp, bun[i].deststr - kp); + saigo = sj3_sjistoeuclen(bun[i].deststr, + bun[i].destlen); + bun[i].destlen = saigo; + bun[i].deststr = knj + sentou; + } + if (mflag) { + free(khp); + mflag = 0; + } + } + + return buncnt; +} + +int +sj3_getkan_mb(u_char *yomi, SJ3_BUNSETU *bun, u_char *knj, int knjsiz) +{ + if (_sys_code == SYS_NOTDEF) + _sys_code = set_sys_code(); + if (_sys_code == SYS_EUC) + return sj3_getkan_euc(yomi, bun, knj, knjsiz); + else + return sj3_getkan(yomi, bun, knj, knjsiz); +} + +int +sj3_douoncnt(u_char *yomi) +{ + int i; + + if ((i = strlen(yomi)) > SJ3_BUNSETU_YOMI) + return 0; + + i = sj3_bunsetu_kouhosuu(&client, yomi, i, MBCODE_SJIS); + if (i == ERROR) { + if (client.fd < 0) { + mdicid = udicid = 0; + return -1; + } + + return 0; + } + + return i; +} + +int +sj3_douoncnt_euc(u_char *yomi) +{ + int flag, i, l; + u_char *yp; + + if ((i = strlen(yomi)) > SJ3_BUNSETU_YOMI) + return 0; + + if (client.svr_version == 1) { + defuse = 0; + l = sj3_str_euctosjis(buf1, sizeof(buf1), yomi, + client.default_char, &defuse); + if ((l < 0) || defuse) + return 0; + yp = buf1; + flag = MBCODE_SJIS; + } else { + yp = yomi; + flag = MBCODE_EUC; + } + i = sj3_bunsetu_kouhosuu(&client, yp, i, flag); + if (i == ERROR) { + if (client.fd < 0) { + mdicid = udicid = 0; + return -1; + } + + return 0; + } + + return i; +} + +int +sj3_douoncnt_mb(u_char *yomi) +{ + if (_sys_code == SYS_NOTDEF) + _sys_code = set_sys_code(); + if (_sys_code == SYS_EUC) + return sj3_douoncnt_euc(yomi); + else + return sj3_douoncnt(yomi); +} + +int +sj3_getdouon(u_char *yomi, SJ3_DOUON *dou) +{ + int i; + + if ((i = strlen(yomi)) > SJ3_BUNSETU_YOMI) + return 0; + + i = sj3_bunsetu_zenkouho(&client, yomi, i, dou, MBCODE_SJIS); + if (i == ERROR) { + if (client.fd < 0) { + mdicid = udicid = 0; + return -1; + } + return 0; + } + + return i; +} + +int +sj3_getdouon_euc(u_char *yomi, SJ3_DOUON *dou) +{ + int i, j, l; + + if ((i = strlen(yomi)) > SJ3_BUNSETU_YOMI) + return 0; + + if (client.svr_version == 1) { + defuse = 0; + l = sj3_str_euctosjis(buf1, sizeof(buf1), yomi, + client.default_char, &defuse); + if ((l < 0) || defuse) + return 0; + i = sj3_bunsetu_zenkouho(&client, buf1, i, dou, MBCODE_SJIS); + if (i == ERROR) { + if (client.fd < 0) { + mdicid = udicid = 0; + return -1; + } + return 0; + } + for (j = 0; j < i; j++) { + defuse = 0; + l = sj3_str_sjistoeuc(kbuf, sizeof(kbuf), dou[j].ddata, + client.default_char, &defuse); + if ((l < 0) || defuse) + return 0; + (void)memcpy(dou[j].ddata, kbuf, l + 1); + dou[j].dlen = l; + } + } else { + i = sj3_bunsetu_zenkouho(&client, yomi, i, dou, MBCODE_EUC); + if (i == ERROR) { + if (client.fd < 0) { + mdicid = udicid = 0; + return -1; + } + return 0; + } + } + return i; +} + +int +sj3_getdouon_mb(u_char *yomi, SJ3_DOUON *dou) +{ + if (_sys_code == SYS_NOTDEF) + _sys_code = set_sys_code(); + if (_sys_code == SYS_EUC) + return sj3_getdouon_euc(yomi, dou); + else + return sj3_getdouon(yomi, dou); +} + +int +sj3_gakusyuu(SJ3_STUDYREC *dcid) +{ + if (sj3_tango_gakusyuu(&client, dcid) == ERROR) { + if (client.fd < 0) { + mdicid = udicid = 0; + return -1; + } + return 1; + } + return 0; +} + +int +sj3_gakusyuu2(u_char *yomi1, u_char *yomi2, SJ3_STUDYREC *dcid) +{ + if (sj3_bunsetu_gakusyuu(&client, yomi1, yomi2, dcid, MBCODE_SJIS) == + ERROR) { + if (client.fd < 0) { + mdicid = udicid = 0; + return -1; + } + return 1; + } + return 0; +} + +int +sj3_gakusyuu2_euc(u_char *yomi1, u_char *yomi2, SJ3_STUDYREC *dcid) +{ + int flag, l; + u_char *y1p, *y2p; + + if (client.svr_version == 1) { + defuse = 0; + l = sj3_str_euctosjis(buf1, sizeof(buf1), yomi1, + client.default_char, &defuse); + if ((l < 0) || defuse) + return 1; + defuse = 0; + l = sj3_str_euctosjis(buf2, sizeof(buf2), yomi2, + client.default_char, &defuse); + if ((l < 0) || defuse) + return 1; + y1p = buf1; + y2p = buf2; + flag = MBCODE_SJIS; + } else { + y1p = yomi1; + y2p = yomi2; + flag = MBCODE_EUC; + } + if (sj3_bunsetu_gakusyuu(&client, y1p, y2p, dcid, flag) == ERROR) { + if (client.fd < 0) { + mdicid = udicid = 0; + return -1; + } + return 1; + } + return 0; +} + +int +sj3_gakusyuu2_mb(u_char *yomi1, u_char *yomi2, SJ3_STUDYREC *dcid) +{ + if (_sys_code == SYS_NOTDEF) + _sys_code = set_sys_code(); + if (_sys_code == SYS_EUC) + return sj3_gakusyuu2_euc(yomi1, yomi2, dcid); + else + return sj3_gakusyuu2(yomi1, yomi2, dcid); +} + +int +sj3_touroku(u_char *yomi, u_char *kanji, int code) +{ + if (sj3_tango_touroku(&client, udicid, yomi, kanji, code, + MBCODE_SJIS)) { + if (client.fd < 0) { + mdicid = udicid = 0; + return -1; + } + switch (sj3_error_number) { + case SJ3_NoSuchDict: + case SJ3_ReadOnlyDict: + return SJ3_DICT_ERROR; + case SJ3_DictLocked: + return SJ3_DICT_LOCKED; + case SJ3_BadYomiString: + return SJ3_BAD_YOMI_STR; + case SJ3_BadKanjiString: + return SJ3_BAD_KANJI_STR; + case SJ3_BadHinsiCode: + return SJ3_BAD_HINSI_CODE; + case SJ3_AlreadyExistWord: + return SJ3_WORD_EXIST; + case SJ3_NoMoreDouonWord: + return SJ3_DOUON_FULL; + case SJ3_NoMoreUserDict: + return SJ3_DICT_FULL; + case SJ3_NoMoreIndexBlock: + return SJ3_INDEX_FULL; + default: + return SJ3_TOUROKU_FAILED; + } + } + + return 0; +} + +int +sj3_touroku_euc(u_char *yomi, u_char *kanji, int code) +{ + int flag, l; + u_char *kp, *yp; + if (client.svr_version == 1) { + defuse = 0; + l = sj3_str_euctosjis(buf1, sizeof(buf1), yomi, + client.default_char, &defuse); + if ((l < 0) || defuse) + return SJ3_BAD_YOMI_STR; + defuse = 0; + l = sj3_str_euctosjis(kbuf, sizeof(kbuf), kanji, + client.default_char, &defuse); + if ((l < 0) || defuse) + return SJ3_BAD_KANJI_STR; + yp = buf1; + kp = kbuf; + flag = MBCODE_SJIS; + } else { + yp = yomi; + kp = kanji; + flag = MBCODE_EUC; + } + if (sj3_tango_touroku(&client, udicid, yp, kp, code, flag)) { + if (client.fd < 0) { + mdicid = udicid = 0; + return -1; + } + switch (sj3_error_number) { + case SJ3_NoSuchDict: + case SJ3_ReadOnlyDict: + return SJ3_DICT_ERROR; + case SJ3_DictLocked: + return SJ3_DICT_LOCKED; + case SJ3_BadYomiString: + return SJ3_BAD_YOMI_STR; + case SJ3_BadKanjiString: + return SJ3_BAD_KANJI_STR; + case SJ3_BadHinsiCode: + return SJ3_BAD_HINSI_CODE; + case SJ3_AlreadyExistWord: + return SJ3_WORD_EXIST; + case SJ3_NoMoreDouonWord: + return SJ3_DOUON_FULL; + case SJ3_NoMoreUserDict: + return SJ3_DICT_FULL; + case SJ3_NoMoreIndexBlock: + return SJ3_INDEX_FULL; + default: + return SJ3_TOUROKU_FAILED; + } + } + + return 0; +} + +int +sj3_touroku_mb(u_char *yomi, u_char *kanji, int code) +{ + if (_sys_code == SYS_NOTDEF) + _sys_code = set_sys_code(); + if (_sys_code == SYS_EUC) + return sj3_touroku_euc(yomi, kanji, code); + else + return sj3_touroku(yomi, kanji, code); +} + +int +sj3_syoukyo(u_char *yomi, u_char *kanji, int code) +{ + if (sj3_tango_sakujo(&client, udicid, yomi, kanji, code, MBCODE_SJIS)) { + if (client.fd < 0) { + mdicid = udicid = 0; + return -1; + } + switch (sj3_error_number) { + case SJ3_NoSuchDict: + case SJ3_ReadOnlyDict: + return SJ3_DICT_ERROR; + case SJ3_DictLocked: + return SJ3_DICT_LOCKED; + case SJ3_BadYomiString: + return SJ3_BAD_YOMI_STR; + case SJ3_BadKanjiString: + return SJ3_BAD_KANJI_STR; + case SJ3_BadHinsiCode: + return SJ3_BAD_HINSI_CODE; + case SJ3_NoSuchWord: + return SJ3_WORD_NOT_EXIST; + default: + return SJ3_SYOUKYO_FAILED; + } + } + return 0; +} + +int +sj3_syoukyo_euc(u_char *yomi, u_char *kanji, int code) +{ + int flag, l; + u_char *kp, *yp; + if (client.svr_version == 1) { + defuse = 0; + l = sj3_str_euctosjis(buf1, sizeof(buf1), yomi, + client.default_char, &defuse); + if ((l < 0) || defuse) + return SJ3_BAD_YOMI_STR; + defuse = 0; + l = sj3_str_euctosjis(kbuf, sizeof(kbuf), kanji, + client.default_char, &defuse); + if ((l < 0) || defuse) + return SJ3_BAD_KANJI_STR; + yp = buf1; + kp = kbuf; + flag = MBCODE_SJIS; + } else { + yp = yomi; + kp = kanji; + flag = MBCODE_EUC; + } + if (sj3_tango_sakujo(&client, udicid, yp, kp, code, flag)) { + if (client.fd < 0) { + mdicid = udicid = 0; + return -1; + } + switch (sj3_error_number) { + case SJ3_NoSuchDict: + case SJ3_ReadOnlyDict: + return SJ3_DICT_ERROR; + case SJ3_DictLocked: + return SJ3_DICT_LOCKED; + case SJ3_BadYomiString: + return SJ3_BAD_YOMI_STR; + case SJ3_BadKanjiString: + return SJ3_BAD_KANJI_STR; + case SJ3_BadHinsiCode: + return SJ3_BAD_HINSI_CODE; + case SJ3_NoSuchWord: + return SJ3_WORD_NOT_EXIST; + default: + return SJ3_SYOUKYO_FAILED; + } + } + return 0; +} + +int +sj3_syoukyo_mb(u_char *yomi, u_char *kanji, int code) +{ + if (_sys_code == SYS_NOTDEF) + _sys_code = set_sys_code(); + if (_sys_code == SYS_EUC) + return sj3_syoukyo_euc(yomi, kanji, code); + else + return sj3_syoukyo(yomi, kanji, code); +} + +int +sj3_getdict(u_char *buf) +{ + if (sj3_tango_syutoku(&client, udicid, buf, MBCODE_SJIS)) { + if (client.fd < 0) { + mdicid = udicid = 0; + return -1; + } + return 1; + } + return 0; +} + +int +sj3_getdict_euc(u_char *buf) +{ + int l, ll, slen; + + if (client.svr_version == 1) { + if (sj3_tango_syutoku(&client, udicid, buf, MBCODE_SJIS)) { + if (client.fd < 0) { + mdicid = udicid = 0; + return -1; + } + return 1; + } + defuse = 0; + slen = strlen(buf) + 1; + l = sj3_str_sjistoeuc(kbuf, sizeof(kbuf), buf, + client.default_char, &defuse); + if ((l < 0) || defuse) + return 1; + l++; + ll = sj3_str_sjistoeuc(&(kbuf[l]), sizeof(kbuf) - l, + &(buf[slen]), client.default_char, &defuse); + if ((ll < 0) || defuse) + return 1; + ll++; + slen += strlen(&(buf[slen])) + 1; + l = l + ll; + memcpy(&(kbuf[l]), &(buf[slen]), 4); + l += 4; + memcpy(buf, kbuf, l); + } else { + if (sj3_tango_syutoku(&client, udicid, buf, MBCODE_EUC)) { + if (client.fd < 0) { + mdicid = udicid = 0; + return -1; + } + return 1; + } + } + return 0; +} + +int +sj3_getdict_mb(u_char *buf) +{ + if (_sys_code == SYS_NOTDEF) + _sys_code = set_sys_code(); + if (_sys_code == SYS_EUC) + return sj3_getdict_euc(buf); + else + return sj3_getdict(buf); +} + +int +sj3_nextdict(u_char *buf) +{ + if (sj3_tango_jikouho(&client, udicid, buf, MBCODE_SJIS)) { + if (client.fd < 0) { + mdicid = udicid = 0; + return -1; + } + return 1; + } + return 0; +} + +int +sj3_nextdict_euc(u_char *buf) +{ + int l, ll, slen; + + if (client.svr_version == 1) { + if (sj3_tango_jikouho(&client, udicid, buf, MBCODE_SJIS)) { + if (client.fd < 0) { + mdicid = udicid = 0; + return -1; + } + return 1; + } + defuse = 0; + slen = strlen(buf) + 1; + l = sj3_str_sjistoeuc(kbuf, sizeof(kbuf), buf, + client.default_char, &defuse); + if ((l < 0) || defuse) + return 1; + l++; + ll = sj3_str_sjistoeuc(&(kbuf[l]), sizeof(kbuf) - l, + &(buf[slen]), client.default_char, &defuse); + if ((ll < 0) || defuse) + return 1; + ll++; + slen += strlen(&(buf[slen])) + 1; + l = l + ll; + memcpy(&(kbuf[l]), &(buf[slen]), 4); + l += 4; + memcpy(buf, kbuf, l); + } else { + if (sj3_tango_jikouho(&client, udicid, buf, MBCODE_EUC)) { + if (client.fd < 0) { + mdicid = udicid = 0; + return -1; + } + return 1; + } + } + return 0; +} + +int +sj3_nextdict_mb(u_char *buf) +{ + if (_sys_code == SYS_NOTDEF) + _sys_code = set_sys_code(); + if (_sys_code == SYS_EUC) + return sj3_nextdict_euc(buf); + else + return sj3_nextdict(buf); +} + +int +sj3_prevdict(u_char *buf) +{ + if (sj3_tango_maekouho(&client, udicid, buf, MBCODE_SJIS)) { + if (client.fd < 0) { + mdicid = udicid = 0; + return -1; + } + return 1; + } + return 0; +} + +int +sj3_prevdict_euc(u_char *buf) +{ + int l, ll, slen; + + if (client.svr_version == 1) { + if (sj3_tango_maekouho(&client, udicid, buf, MBCODE_SJIS)) { + if (client.fd < 0) { + mdicid = udicid = 0; + return -1; + } + return 1; + } + defuse = 0; + slen = strlen(buf) + 1; + l = sj3_str_sjistoeuc(kbuf, sizeof(kbuf), buf, + client.default_char, &defuse); + if ((l < 0) || defuse) + return 1; + l++; + ll = sj3_str_sjistoeuc(&(kbuf[l]), sizeof(kbuf) - l, + &(buf[slen]), client.default_char, &defuse); + if ((ll < 0) || defuse) + return 1; + ll++; + slen += strlen(&(buf[slen])) + 1; + l = l + ll; + memcpy(&(kbuf[l]), &(buf[slen]), 4); + l += 4; + memcpy(buf, kbuf, l); + } else { + if (sj3_tango_maekouho(&client, udicid, buf, MBCODE_EUC)) { + if (client.fd < 0) { + mdicid = udicid = 0; + return -1; + } + return 1; + } + } + return 0; +} + +int +sj3_prevdict_mb(u_char *buf) +{ + if (_sys_code == SYS_NOTDEF) + _sys_code = set_sys_code(); + if (_sys_code == SYS_EUC) + return sj3_prevdict_euc(buf); + else + return sj3_prevdict(buf); +} + +int +sj3_lockserv(void) +{ + if (sj3_lock_server(&client) == ERROR) { + if (client.fd < 0) { + mdicid = udicid = 0; + return -1; + } + return 1; + } + return 0; +} + +int +sj3_unlockserv(void) +{ + if (sj3_unlock_server(&client)) { + if (client.fd < 0) { + mdicid = udicid = 0; + return -1; + } + return 1; + } + return 0; +} diff --git a/sj3serv/sj3lib.h b/sj3serv/sj3lib.h new file mode 100644 index 0000000..f047d2f --- /dev/null +++ b/sj3serv/sj3lib.h @@ -0,0 +1,235 @@ +/*- + * SPDX-License-Identifier: MIT-open-group + * + * Copyright (c) 1991-1994 Sony Corporation + * + * 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: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * 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. + * + * 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. + */ + +#ifndef SJ3_SJ3LIB_H_ +#define SJ3_SJ3LIB_H_ + +#include + +#define SJ3_NORMAL_END 0 +#define SJ3_SERVER_DEAD (1 << 0) + +#define SJ3_CONNECT_ERROR (1 << 1) +#define SJ3_ALREADY_CONNECTED (1 << 2) +#define SJ3_CANNOT_OPEN_MDICT (1 << 3) +#define SJ3_CANNOT_OPEN_UDICT (1 << 4) +#define SJ3_CANNOT_OPEN_STUDY (1 << 5) +#define SJ3_CANNOT_MAKE_UDIR (1 << 6) +#define SJ3_CANNOT_MAKE_UDICT (1 << 7) +#define SJ3_CANNOT_MAKE_STUDY (1 << 8) + +#define SJ3_DISCONNECT_ERROR (1 << 1) +#define SJ3_NOT_CONNECTED (1 << 2) +#define SJ3_NOT_OPENED_MDICT (1 << 3) +#define SJ3_NOT_OPENED_UDICT (1 << 4) +#define SJ3_NOT_OPENED_STUDY (1 << 5) +#define SJ3_CLOSE_MDICT_ERROR (1 << 6) +#define SJ3_CLOSE_UDICT_ERROR (1 << 7) +#define SJ3_CLOSE_STUDY_ERROR (1 << 8) + +#define SJ3_DICT_ERROR 1 +#define SJ3_DICT_LOCKED 2 +#define SJ3_BAD_YOMI_STR 3 +#define SJ3_BAD_KANJI_STR 4 +#define SJ3_BAD_HINSI_CODE 5 + +#define SJ3_WORD_EXIST 6 +#define SJ3_DOUON_FULL 7 +#define SJ3_DICT_FULL 8 +#define SJ3_INDEX_FULL 9 +#define SJ3_TOUROKU_FAILED 10 + +#define SJ3_WORD_NOT_EXIST 6 +#define SJ3_SYOUKYO_FAILED 10 + +#define SJ3_IKKATU_YOMI 512 + +#define SJ3_BUNSETU_YOMI 128 +#define SJ3_BUNSETU_KANJI 512 + +#ifndef SJ3_WORD_ID_SIZE +#define SJ3_WORD_ID_SIZE 32 +#endif + +typedef struct studyrec { + unsigned char dummy[SJ3_WORD_ID_SIZE]; +} SJ3_STUDYREC; + +typedef struct bunsetu { + int srclen; + int destlen; + unsigned char *srcstr; + unsigned char *deststr; + struct studyrec dcid; +} SJ3_BUNSETU; + +typedef struct douon { + unsigned char ddata[SJ3_BUNSETU_KANJI]; + int dlen; + SJ3_STUDYREC dcid; +} SJ3_DOUON; + +#define SJ3_H_NRMNOUN 1 +#define SJ3_H_PRONOUN 12 +#define SJ3_H_LNAME 21 +#define SJ3_H_FNAME 22 +#define SJ3_H_LOCNAME 24 +#define SJ3_H_PREFIC 25 +#define SJ3_H_RENTAI 26 +#define SJ3_H_CONJUNC 27 +#define SJ3_H_SUBNUM 29 +#define SJ3_H_NUMERAL 30 +#define SJ3_H_PREFIX 31 +#define SJ3_H_POSTFIX 36 +#define SJ3_H_ADVERB 45 +#define SJ3_H_ADJECT 60 +#define SJ3_H_ADJVERB 71 +#define SJ3_H_SILVERB 80 +#define SJ3_H_ZILVERB 81 +#define SJ3_H_ONEVERB 90 +#define SJ3_H_KAVERB 91 +#define SJ3_H_GAVERB 92 +#define SJ3_H_SAVERB 93 +#define SJ3_H_TAVERB 94 +#define SJ3_H_NAVERB 95 +#define SJ3_H_BAVERB 96 +#define SJ3_H_MAVERB 97 +#define SJ3_H_RAVERB 98 +#define SJ3_H_WAVERB 99 +#define SJ3_H_SINGLE 189 + +typedef struct sj3_client_env { + int fd; + int serv_dead_flg; + int stdy_size; + short svr_version; + short cli_version; + char default_char[2]; +} SJ3_CLIENT_ENV; + +#ifndef SJ3_NAME_LENGTH +#define SJ3_NAME_LENGTH 128 +#endif +typedef struct sj3_who_struct { + int fd; + char hostname[SJ3_NAME_LENGTH]; + char username[SJ3_NAME_LENGTH]; + char progname[SJ3_NAME_LENGTH]; +} SJ3_WHO_STRUCT; + +#define MBCODE_SJIS 1 +#define MBCODE_EUC 2 + +/* level1.c */ +void sj3_set_timeout(int timeout); +int sj3_make_connection(SJ3_CLIENT_ENV *client, char *serv, char *user, + char *prog); +int sj3_erase_connection(SJ3_CLIENT_ENV *client); +long sj3_open_dictionary(SJ3_CLIENT_ENV *client, char *dictname, + char *password); +int sj3_close_dictionary(SJ3_CLIENT_ENV *client, long dicid); +int sj3_open_study_file(SJ3_CLIENT_ENV *client, char *stdyname, char *password); +int sj3_close_study_file(SJ3_CLIENT_ENV *client); +int sj3_get_id_size(SJ3_CLIENT_ENV *client); +int sj3_lock_server(SJ3_CLIENT_ENV *client); +int sj3_unlock_server(SJ3_CLIENT_ENV *client); +int sj3_ikkatu_henkan(SJ3_CLIENT_ENV *client, u_char *src, u_char *dst, + int dstsiz, int mb_flag); +int sj3_bunsetu_henkan(SJ3_CLIENT_ENV *client, u_char *yomi, int len, + u_char *kanji, int mb_flag); +int sj3_bunsetu_jikouho(SJ3_CLIENT_ENV *client, u_char *kanji, int mode, + int mb_flag); +int sj3_bunsetu_maekouho(SJ3_CLIENT_ENV *client, u_char *kanji, int mode, + int mb_flag); +int sj3_bunsetu_kouhosuu(SJ3_CLIENT_ENV *client, u_char *yomi, int len, + int mb_flag); +int sj3_bunsetu_zenkouho(SJ3_CLIENT_ENV *client, u_char *yomi, int len, + SJ3_DOUON *douon, int mb_flag); +int sj3_tango_gakusyuu(SJ3_CLIENT_ENV *client, SJ3_STUDYREC *stdy); +int sj3_bunsetu_gakusyuu(SJ3_CLIENT_ENV *client, u_char *yomi1, u_char *yomi2, + SJ3_STUDYREC *stdy, int mb_flag); +int sj3_tango_touroku(SJ3_CLIENT_ENV *client, long dicid, u_char *yomi, + u_char *kanji, int code, int mb_flag); +int sj3_tango_sakujo(SJ3_CLIENT_ENV *client, long dicid, u_char *yomi, + u_char *kanji, int code, int mb_flag); +int sj3_tango_syutoku(SJ3_CLIENT_ENV *client, int dicid, u_char *buf, + int mb_flag); +int sj3_tango_jikouho(SJ3_CLIENT_ENV *client, int dicid, u_char *buf, + int mb_flag); +int sj3_tango_maekouho(SJ3_CLIENT_ENV *client, int dicid, u_char *buf, + int mb_flag); +int sj3_make_dict_file(SJ3_CLIENT_ENV *client, char *path, int idxlen, + int seglen, int segnum); +int sj3_make_study_file(SJ3_CLIENT_ENV *client, char *path, int stynum, + int clstep, int cllen); +int sj3_make_directory(SJ3_CLIENT_ENV *client, char *path); +int sj3_access(SJ3_CLIENT_ENV *client, char *path, int mode); +int sj3_who(SJ3_CLIENT_ENV *client, SJ3_WHO_STRUCT *ret, int num); +int sj3_quit(SJ3_CLIENT_ENV *client); +int sj3_kill(SJ3_CLIENT_ENV *client); +int sj3_version(SJ3_CLIENT_ENV *client, char *dst, int dstsiz); + +/* sj.c */ +int sj3_open(char *host, char *user); +int sj3_open_with_list(char *host, char *user, int dicts_num, char **dicts, + int *error_num, int **error_index); +int sj3_close(void); +int sj3_getkan(u_char *yomi, SJ3_BUNSETU *bun, u_char *knj, int knjsiz); +int sj3_getkan_euc(u_char *yomi, SJ3_BUNSETU *bun, u_char *knj, int knjsiz); +int sj3_getkan_mb(u_char *yomi, SJ3_BUNSETU *bun, u_char *knj, int knjsiz); +int sj3_douoncnt(u_char *yomi); +int sj3_douoncnt_euc(u_char *yomi); +int sj3_douoncnt_mb(u_char *yomi); +int sj3_getdouon(u_char *yomi, SJ3_DOUON *dou); +int sj3_getdouon_euc(u_char *yomi, SJ3_DOUON *dou); +int sj3_getdouon_mb(u_char *yomi, SJ3_DOUON *dou); +int sj3_gakusyuu(SJ3_STUDYREC *dcid); +int sj3_gakusyuu2(u_char *yomi1, u_char *yomi2, SJ3_STUDYREC *dcid); +int sj3_gakusyuu2_euc(u_char *yomi1, u_char *yomi2, SJ3_STUDYREC *dcid); +int sj3_gakusyuu2_mb(u_char *yomi1, u_char *yomi2, SJ3_STUDYREC *dcid); +int sj3_touroku(u_char *yomi, u_char *kanji, int code); +int sj3_touroku_euc(u_char *yomi, u_char *kanji, int code); +int sj3_touroku_mb(u_char *yomi, u_char *kanji, int code); +int sj3_syoukyo(u_char *yomi, u_char *kanji, int code); +int sj3_syoukyo_euc(u_char *yomi, u_char *kanji, int code); +int sj3_syoukyo_mb(u_char *yomi, u_char *kanji, int code); +int sj3_getdict(u_char *buf); +int sj3_getdict_euc(u_char *buf); +int sj3_getdict_mb(u_char *buf); +int sj3_nextdict(u_char *buf); +int sj3_nextdict_euc(u_char *buf); +int sj3_nextdict_mb(u_char *buf); +int sj3_prevdict(u_char *buf); +int sj3_prevdict_euc(u_char *buf); +int sj3_prevdict_mb(u_char *buf); +int sj3_lockserv(void); +int sj3_unlockserv(void); + +#endif diff --git a/sj3serv/string.c b/sj3serv/string.c new file mode 100644 index 0000000..44c8cd5 --- /dev/null +++ b/sj3serv/string.c @@ -0,0 +1,355 @@ +/*- + * SPDX-License-Identifier: MIT-open-group + * + * Copyright (c) 1994 Sony Corporation + * + * 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: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * 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. + * + * 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. + */ + +#include "sj_sysvdef.h" + +#include +#include +#include +#include + +#include "sj_euc.h" + +#ifndef TRUE +#define TRUE (1) +#define FALSE (0) +#endif + +#define sjis2euc sj3_sjis2euc +#define euc2sjis sj3_euc2sjis + +#define issjis1(c) \ + (((0x81 <= (c)) && ((c) <= 0x9f)) || ((0xe0 <= (c)) && ((c) <= 0xfc))) +#define iseuc(c) ((0xa1 <= (c)) && ((c) <= 0xfe)) +#define iskana(c) ((0xa1 <= (c)) && ((c) <= 0xdf)) + +int sj3_str_sjistoeuc(unsigned char *, int, unsigned char *, unsigned char *, + int *); +int sj3_str_euctosjis(unsigned char *, int, unsigned char *, unsigned char *, + int *); +int sj3_sjistoeuclen(unsigned char *, int); + +int sj3_sjistoeuc(unsigned char *, int, unsigned char *, int); +int sj3_euctosjis(unsigned char *, int, unsigned char *, int); + +void sj_euc2sjis(unsigned char *); +void sj_jis2sjis(unsigned char *); +void sj_sjis2euc(unsigned char *); +void sj_sjis2jis(unsigned char *); + +unsigned short sj3_jis2sjis(unsigned short); +unsigned short sj3_jis2euc(unsigned short); +unsigned short sj3_sjis2jis(unsigned short); +unsigned short sj3_euc2sjis(unsigned short); +unsigned short sj3_sjis2euc(unsigned short); + +int +sj3_str_sjistoeuc(unsigned char *out, int outlen, unsigned char *in, + unsigned char *sjis_default, int *useflag) +{ + unsigned char *sjis = in, *euc = out; + int n = 0; + unsigned short code, default_code; + + euc[0] = '\0'; + *useflag = 0; + + if (!sjis) + return 0; + + while (*sjis && n < outlen) { + if (issjis1(*sjis)) { + code = sjis2euc((*sjis << 8) | *(sjis + 1)); + if (n + 2 > outlen) + return -1; + if (code == 0) { + default_code = sjis2euc((sjis_default[0] << 8) | + sjis_default[1]); + euc[n++] = default_code >> 8; + euc[n++] = default_code & 0xff; + (*useflag)++; + } else { + euc[n++] = code >> 8; + euc[n++] = code & 0xff; + } + sjis += 2; + } else { + if (*sjis >= 0xa1 && *sjis <= 0xdf) { + if (n + 2 > outlen) + return -1; + euc[n++] = SS2; + } + euc[n++] = *sjis; + sjis++; + } + } + if (n > outlen) + return -1; + + euc[n] = '\0'; + return n; +} + +int +sj3_str_euctosjis(unsigned char *out, int outlen, unsigned char *in, + unsigned char *sjis_default, int *useflag) +{ + unsigned char *euc = in, *sjis = out; + int n = 0; + unsigned short code, default_code; + + sjis[0] = '\0'; + *useflag = 0; + + if (!euc) + return 0; + + while (*euc && n < outlen) { + if (iseuc(*euc)) { + code = euc2sjis((*euc << 8) | *(euc + 1)); + if (n + 2 > outlen) + return -1; + if (code == 0) { + sjis[n++] = sjis_default[0]; + sjis[n++] = sjis_default[1]; + (*useflag)++; + } else { + sjis[n++] = code >> 8; + sjis[n++] = code & 0xff; + } + euc += 2; + } else if (*euc == SS3) { + if (n + 2 > outlen) + return -1; + sjis[n++] = sjis_default[0]; + sjis[n++] = sjis_default[1]; + euc += 3; + (*useflag)++; + } else if (*euc == SS2) { + sjis[n++] = *(euc + 1); + euc += 2; + } else { + sjis[n++] = *euc; + euc++; + } + } + if (n > outlen) + return -1; + + sjis[n] = '\0'; + return n; +} + +int +sj3_sjistoeuclen(unsigned char *str, int max) +{ + register int len = 0, bytes = 1; + + if (!max) + return 0; + while ((bytes <= max) && *str) { + if (issjis1(*str)) { + str++; + bytes++; + if (!*str) + break; + len += 2; + } else if (*str & 0x80) { + len += 2; + } else { + len++; + } + str++; + bytes++; + } + return len; +} + +#define MASK 0x7f +#define MSB 0x80 + +static unsigned char def_char[] = {0x81, 0x40}; + +int +sj3_sjistoeuc(unsigned char *e, int elen, unsigned char *s, int slen) +{ + int dummy; + + return sj3_str_sjistoeuc(e, elen, s, def_char, &dummy); +} + +int +sj3_euctosjis(unsigned char *s, int slen, unsigned char *e, int elen) +{ + int dummy; + + return sj3_str_euctosjis(s, slen, e, def_char, &dummy); +} + +void +sj_euc2sjis(unsigned char *s) +{ + s[0] &= MASK; + s[1] &= MASK; + sj_jis2sjis(s); +} + +void +sj_jis2sjis(unsigned char *s) +{ + register int high, low, nh, nl; + + high = s[0]; + low = s[1]; + nh = ((high - 0x21) >> 1) + 0x81; + if (nh > 0x9f) + nh += 0x40; + if (high & 1) { + nl = low + 0x1f; + if (low > 0x5f) + nl++; + } else { + nl = low + 0x7e; + } + s[0] = nh; + s[1] = nl; +} + +void +sj_sjis2euc(unsigned char *s) +{ + sj_sjis2jis(s); + s[0] |= MSB; + s[1] |= MSB; +} + +void +sj_sjis2jis(unsigned char *s) +{ + register int byte1, byte2; + register unsigned char *sp; + + sp = s; + byte1 = *sp++; + byte2 = *sp--; + + if (byte1 <= 0x80 || byte1 >= 0xf0 || + (byte1 >= 0xa0 && byte1 <= 0xdf) || byte2 <= 0x3f || + byte2 >= 0xfe || byte2 == 0x7f) { + byte1 = 0x81; + byte2 = 0x40; + } + byte1 -= (byte1 >= 0xa0) ? 0xc1 : 0x81; + if (byte2 >= 0x9f) { + *sp++ = (byte1 << 1) + 0x22; + *sp = byte2 - 0x7e; + } else { + *sp++ = (byte1 << 1) + 0x21; + *sp = byte2 - ((byte2 <= 0x7e) ? 0x1f : 0x20); + } +} + +unsigned short +sj3_jis2sjis(unsigned short code) +{ + unsigned char tmp[3]; + + tmp[0] = (code >> 8) & 0xff; + tmp[1] = code & 0xff; + tmp[2] = '\0'; + + sj_jis2sjis((unsigned char *)tmp); + + code = (tmp[0] << 8) + tmp[1]; + + return (code); +} + +unsigned short +sj3_jis2euc(unsigned short code) +{ + unsigned char tmp[3]; + + tmp[0] = (code >> 8) & 0xff; + tmp[1] = code & 0xff; + tmp[2] = '\0'; + + code = ((tmp[0] | 0x80) << 8) + (tmp[1] | 0x80); + + return (code); +} + +unsigned short +sj3_sjis2jis(unsigned short code) +{ + unsigned char tmp[3]; + + tmp[0] = (code >> 8) & 0xff; + tmp[1] = code & 0xff; + tmp[2] = '\0'; + + sj_sjis2jis((unsigned char *)tmp); + + code = (tmp[0] << 8) + tmp[1]; + + return (code); +} + +unsigned short +sj3_euc2sjis(unsigned short code) +{ + unsigned char tmp[3]; + + tmp[0] = (code >> 8) & 0xff; + tmp[1] = code & 0xff; + tmp[2] = '\0'; + + sj_euc2sjis((unsigned char *)tmp); + + code = (tmp[0] << 8) + tmp[1]; + + return (code); +} + +unsigned short +sj3_sjis2euc(unsigned short code) +{ + unsigned char tmp[3]; + + tmp[0] = (code >> 8) & 0xff; + tmp[1] = code & 0xff; + tmp[2] = '\0'; + + sj_sjis2euc((unsigned char *)tmp); + + code = (tmp[0] << 8) + tmp[1]; + + return (code); +}