| version 1.11, 1999/11/06 21:39:36 |
version 1.16, 1999/11/28 16:43:21 |
|
|
| /* -*- mode: C; coding: euc-japan -*- */ |
/* -*- mode: C; coding: euc-japan -*- */ |
| /* $OpenXM: OpenXM/src/ox_math/ox.c,v 1.10 1999/11/05 12:34:25 ohara Exp $ */ |
/* $OpenXM: OpenXM/src/ox_math/ox.c,v 1.15 1999/11/18 21:57:56 ohara Exp $ */ |
| |
|
| /* |
/* |
| 関数の名前付け規約(その2): |
関数の名前付け規約(その2): |
| Line 57 static int dump_string(char *s, int len); |
|
| Line 57 static int dump_string(char *s, int len); |
|
| static int dump_integer(int x); |
static int dump_integer(int x); |
| static int dump_mpz(mpz_ptr mpz); |
static int dump_mpz(mpz_ptr mpz); |
| |
|
| static int funcs(int cmo_type); |
|
| |
|
| static int login_with_otp(int fd, char* passwd); |
static int login_with_otp(int fd, char* passwd); |
| static char *create_otp(); |
static char *create_otp(); |
| |
|
| /* CMO_xxx の値順にならべること(デバッグのため) */ |
/* CMO_xxx の値順にならべること(デバッグのため) */ |
| static cmo_null* receive_cmo_null(int fd); |
static cmo_null* receive_cmo_null(int fd); |
| static cmo_int32* receive_cmo_int32(int fd); |
static cmo_int32* receive_cmo_int32(int fd); |
| static cmo_string* receive_cmo_string(int fd); |
static cmo_string* receive_cmo_string(int fd); |
| static cmo_mathcap* receive_cmo_mathcap(int fd); |
static cmo_mathcap* receive_cmo_mathcap(int fd); |
| static cmo_list* receive_cmo_list(int fd); |
static cmo_list* receive_cmo_list(int fd); |
| static cmo_monomial32* receive_cmo_monomial32(int fd); |
static cmo_monomial32* receive_cmo_monomial32(int fd); |
| static cmo_zz* receive_cmo_zz(int fd); |
static cmo_zz* receive_cmo_zz(int fd); |
| static cmo_zero* receive_cmo_zero(int fd); |
static cmo_zero* receive_cmo_zero(int fd); |
| static cmo_dms_generic* receive_cmo_dms_generic(int fd); |
static cmo_dms_generic* receive_cmo_dms_generic(int fd); |
| static cmo_ring_by_name* receive_cmo_ring_by_name(int fd); |
static cmo_ring_by_name* receive_cmo_ring_by_name(int fd); |
| static cmo_distributed_polynomial* receive_cmo_distributed_polynomial(int fd); |
static cmo_distributed_polynomial* receive_cmo_distributed_polynomial(int fd); |
| |
|
| static cmo_error2* receive_cmo_error2(int fd); |
static cmo_error2* receive_cmo_error2(int fd); |
| static void receive_mpz(int fd, mpz_ptr mpz); |
static void receive_mpz(int fd, mpz_ptr mpz); |
| |
|
| static int send_cmo_null(int fd, cmo_null* c); |
static int send_cmo_null(int fd, cmo_null* c); |
| static int send_cmo_int32(int fd, cmo_int32* m); |
static int send_cmo_int32(int fd, cmo_int32* m); |
| Line 92 static int send_cmo_distributed_polynomial(in |
|
| Line 90 static int send_cmo_distributed_polynomial(in |
|
| /* エラーハンドリングのため */ |
/* エラーハンドリングのため */ |
| static int current_received_serial = 0; |
static int current_received_serial = 0; |
| |
|
| /* エラーを起こしたときはサーバは次のようにすればよい. */ |
/* エラーを起こしたときにサーバは次を呼び出す. */ |
| cmo_error2* gen_error_object(int err_code) |
cmo_error2* make_error_object(int err_code, cmo *ob) |
| { |
{ |
| cmo_list* li = new_cmo_list(); |
cmo_list* li = new_cmo_list(); |
| append_cmo_list(li, (cmo *)new_cmo_int32(current_received_serial)); |
append_cmo_list(li, (cmo *)new_cmo_int32(current_received_serial)); |
| append_cmo_list(li, (cmo *)new_cmo_int32(err_code)); |
append_cmo_list(li, (cmo *)new_cmo_int32(err_code)); |
| |
append_cmo_list(li, ob); |
| /* 他の情報を加えるならココ */ |
/* 他の情報を加えるならココ */ |
| return new_cmo_error2((cmo *)li); |
return new_cmo_error2((cmo *)li); |
| } |
} |
| Line 169 static cell *tail(cmo_list* this) { |
|
| Line 168 static cell *tail(cmo_list* this) { |
|
| int append_cmo_list(cmo_list* this, cmo* newcmo) |
int append_cmo_list(cmo_list* this, cmo* newcmo) |
| { |
{ |
| cell *cp = tail(this); |
cell *cp = tail(this); |
| cp->cmo = newcmo; |
cp->cmo = newcmo; |
| cp->next = new_cell(); |
cp->next = new_cell(); |
| this->length++; |
this->length++; |
| return 0; |
return 0; |
| Line 225 static cmo_list* receive_cmo_list(int fd) |
|
| Line 224 static cmo_list* receive_cmo_list(int fd) |
|
| |
|
| static cmo_monomial32* receive_cmo_monomial32(int fd) |
static cmo_monomial32* receive_cmo_monomial32(int fd) |
| { |
{ |
| int i; |
int i; |
| int len = receive_int32(fd); |
int len = receive_int32(fd); |
| cmo_monomial32* c = new_cmo_monomial32(len); |
cmo_monomial32* c = new_cmo_monomial32(len); |
| |
|
| for(i=0; i<len; i++) { |
for(i=0; i<len; i++) { |
| c->exps[i] = receive_int32(fd); |
c->exps[i] = receive_int32(fd); |
| } |
} |
| c->coef = receive_cmo(fd); |
c->coef = receive_cmo(fd); |
| return c; |
return c; |
| } |
} |
| |
|
| Line 256 static cmo_dms_generic* receive_cmo_dms_generic(int fd |
|
| Line 255 static cmo_dms_generic* receive_cmo_dms_generic(int fd |
|
| static cmo_ring_by_name* receive_cmo_ring_by_name(int fd) |
static cmo_ring_by_name* receive_cmo_ring_by_name(int fd) |
| { |
{ |
| cmo* ob = receive_cmo(fd); |
cmo* ob = receive_cmo(fd); |
| /* 意味的チェックが必要 */ |
/* 意味的チェックが必要 */ |
| return new_cmo_ring_by_name(ob); |
return new_cmo_ring_by_name(ob); |
| } |
} |
| |
|
| Line 265 static cmo_distributed_polynomial* receive_cmo_distrib |
|
| Line 264 static cmo_distributed_polynomial* receive_cmo_distrib |
|
| cmo* ob; |
cmo* ob; |
| cmo_distributed_polynomial* c = new_cmo_distributed_polynomial(); |
cmo_distributed_polynomial* c = new_cmo_distributed_polynomial(); |
| int len = receive_int32(fd); |
int len = receive_int32(fd); |
| c->ringdef = receive_cmo(fd); |
c->ringdef = receive_cmo(fd); |
| |
|
| while (len>0) { |
while (len>0) { |
| ob = receive_cmo(fd); |
ob = receive_cmo(fd); |
| append_cmo_list(c, ob); |
append_cmo_list((cmo_list *)c, ob); |
| len--; |
len--; |
| } |
} |
| return c; |
return c; |
| Line 317 cmo* receive_cmo(int fd) |
|
| Line 316 cmo* receive_cmo(int fd) |
|
| m = (cmo *)receive_cmo_zero(fd); |
m = (cmo *)receive_cmo_zero(fd); |
| break; |
break; |
| case CMO_DMS_GENERIC: |
case CMO_DMS_GENERIC: |
| m = (cmo *)receive_cmo_dms_generic(fd); |
m = (cmo *)receive_cmo_dms_generic(fd); |
| break; |
break; |
| case CMO_RING_BY_NAME: |
case CMO_RING_BY_NAME: |
| m = (cmo *)receive_cmo_ring_by_name(fd); |
m = (cmo *)receive_cmo_ring_by_name(fd); |
| break; |
break; |
| case CMO_DISTRIBUTED_POLYNOMIAL: |
case CMO_DISTRIBUTED_POLYNOMIAL: |
| m = (cmo *)receive_cmo_distributed_polynomial(fd); |
m = (cmo *)receive_cmo_distributed_polynomial(fd); |
| break; |
break; |
| case CMO_ERROR2: |
case CMO_ERROR2: |
| m = (cmo *)receive_cmo_error2(fd); |
m = (cmo *)receive_cmo_error2(fd); |
| break; |
break; |
| case CMO_DATUM: |
case CMO_DATUM: |
| case CMO_QQ: |
case CMO_QQ: |
| default: |
default: |
| fprintf(stderr, "unknown cmo-type: tag = (%d)\n", m->tag); |
fprintf(stderr, "the CMO (%d) is not implemented.\n", m->tag); |
| } |
} |
| return m; |
return m; |
| } |
} |
| Line 375 cmo_string* new_cmo_string(char* s) |
|
| Line 374 cmo_string* new_cmo_string(char* s) |
|
| { |
{ |
| cmo_string* c = malloc(sizeof(cmo_string)); |
cmo_string* c = malloc(sizeof(cmo_string)); |
| c->tag = CMO_STRING; |
c->tag = CMO_STRING; |
| if (s != NULL) { |
if (s != NULL) { |
| c->s = malloc(strlen(s)+1); |
c->s = malloc(strlen(s)+1); |
| strcpy(c->s, s); |
strcpy(c->s, s); |
| }else { |
}else { |
| c->s = NULL; |
c->s = NULL; |
| } |
} |
| return c; |
return c; |
| } |
} |
| |
|
| Line 411 cmo_monomial32* new_cmo_monomial32() |
|
| Line 410 cmo_monomial32* new_cmo_monomial32() |
|
| cmo_monomial32* new_cmo_monomial32_size(int size) |
cmo_monomial32* new_cmo_monomial32_size(int size) |
| { |
{ |
| cmo_monomial32* c = new_cmo_monomial32(); |
cmo_monomial32* c = new_cmo_monomial32(); |
| if (size>0) { |
if (size>0) { |
| c->length = size; |
c->length = size; |
| c->exps = malloc(sizeof(int)*size); |
c->exps = malloc(sizeof(int)*size); |
| } |
} |
| return c; |
return c; |
| } |
} |
| |
|
| Line 490 cmo_distributed_polynomial* new_cmo_distributed_polyno |
|
| Line 489 cmo_distributed_polynomial* new_cmo_distributed_polyno |
|
| c->tag = CMO_DISTRIBUTED_POLYNOMIAL; |
c->tag = CMO_DISTRIBUTED_POLYNOMIAL; |
| c->length = 0; |
c->length = 0; |
| c->head->next = NULL; |
c->head->next = NULL; |
| c->ringdef = NULL; |
c->ringdef = NULL; |
| return c; |
return c; |
| } |
} |
| |
|
| Line 512 int print_cmo(cmo* c) |
|
| Line 511 int print_cmo(cmo* c) |
|
| { |
{ |
| int tag = c->tag; |
int tag = c->tag; |
| |
|
| #ifdef DEBUG |
symbol* symp = lookup_by_tag(tag); |
| symbol* symp = lookup_by_tag(tag); |
if (symp != NULL) { |
| if (symp != NULL) { |
fprintf(stderr, "(%s", symp->key); |
| fprintf(stderr, "local::tag = %s: ", symp->key); |
}else { |
| }else { |
fprintf(stderr, "(%d", tag); |
| fprintf(stderr, "local::tag = %d: ", tag); |
} |
| } |
|
| #endif |
|
| |
|
| switch(tag) { |
switch(tag) { |
| case CMO_LIST: |
case CMO_LIST: |
| Line 529 int print_cmo(cmo* c) |
|
| Line 526 int print_cmo(cmo* c) |
|
| print_cmo_int32((cmo_int32 *)c); |
print_cmo_int32((cmo_int32 *)c); |
| break; |
break; |
| case CMO_MATHCAP: |
case CMO_MATHCAP: |
| |
case CMO_INDETERMINATE: |
| |
case CMO_RING_BY_NAME: |
| |
case CMO_ERROR2: |
| print_cmo_mathcap((cmo_mathcap *)c); |
print_cmo_mathcap((cmo_mathcap *)c); |
| break; |
break; |
| case CMO_STRING: |
case CMO_STRING: |
| print_cmo_string((cmo_string *)c); |
print_cmo_string((cmo_string *)c); |
| break; |
break; |
| case CMO_NULL: |
case CMO_NULL: |
| fprintf(stderr, "\n"); |
case CMO_ZERO: |
| |
case CMO_DMS_GENERIC: |
| |
fprintf(stderr, ")"); |
| break; |
break; |
| default: |
default: |
| fprintf(stderr, "print_cmo() does not know how to print.\n"); |
fprintf(stderr, "print_cmo() does not know how to print.\n"); |
| Line 544 int print_cmo(cmo* c) |
|
| Line 546 int print_cmo(cmo* c) |
|
| |
|
| int print_cmo_int32(cmo_int32* c) |
int print_cmo_int32(cmo_int32* c) |
| { |
{ |
| fprintf(stderr, "cmo_int32 = (%d)\n", c->i); |
fprintf(stderr, ", %d)", c->i); |
| } |
} |
| |
|
| int print_cmo_list(cmo_list* li) |
int print_cmo_list(cmo_list* li) |
| { |
{ |
| cell* cp = li->head; |
cell* cp = li->head; |
| fprintf(stderr, "length = (%d)\nlist:\n", li->length); |
while(cp->next != NULL) { |
| while(cp != NULL) { |
fprintf(stderr, ", "); |
| print_cmo(cp->cmo); |
print_cmo(cp->cmo); |
| cp=cp->next; |
cp=cp->next; |
| } |
} |
| fprintf(stderr, "end of list\n"); |
fprintf(stderr, ")"); |
| } |
} |
| |
|
| int print_cmo_mathcap(cmo_mathcap* c) |
int print_cmo_mathcap(cmo_mathcap* c) |
| { |
{ |
| fprintf(stderr, "\n"); |
fprintf(stderr, ", "); |
| print_cmo(c->ob); |
print_cmo(c->ob); |
| |
fprintf(stderr, ")"); |
| } |
} |
| |
|
| int print_cmo_string(cmo_string* c) |
int print_cmo_string(cmo_string* c) |
| { |
{ |
| fprintf(stderr, "cmo_string = (%s)\n", c->s); |
fprintf(stderr, ", \"%s\")", c->s); |
| } |
} |
| |
|
| void ox_close(ox_file_t sv) |
void ox_close(ox_file_t sv) |
| Line 580 void ox_close(ox_file_t sv) |
|
| Line 583 void ox_close(ox_file_t sv) |
|
| |
|
| void ox_executeStringByLocalParser(ox_file_t sv, char* s) |
void ox_executeStringByLocalParser(ox_file_t sv, char* s) |
| { |
{ |
| if (s != NULL) { |
if (s != NULL) { |
| /* 文字列ををスタックにプッシュ. */ |
/* 文字列ををスタックにプッシュ. */ |
| send_ox_cmo(sv->stream, (cmo *)new_cmo_string(s)); |
send_ox_cmo(sv->stream, (cmo *)new_cmo_string(s)); |
| /* サーバに実行させる. */ |
/* サーバに実行させる. */ |
| send_ox_command(sv->stream, SM_executeStringByLocalParser); |
send_ox_command(sv->stream, SM_executeStringByLocalParser); |
| } |
} |
| } |
} |
| |
|
| /* ox_mathcap() をコールする. */ |
/* ox_mathcap() をコールする. */ |
| Line 617 cmo* ox_pop_cmo(ox_file_t sv, int fd) |
|
| Line 620 cmo* ox_pop_cmo(ox_file_t sv, int fd) |
|
| /* 手抜き. (後で改善しよう...) */ |
/* 手抜き. (後で改善しよう...) */ |
| static char *create_otp() |
static char *create_otp() |
| { |
{ |
| static char otp[] = "otpasswd"; |
static char otp[] = "otpasswd"; |
| return otp; |
return otp; |
| } |
} |
| |
|
| /* OneTimePassword の処理 */ |
/* OneTimePassword の処理 */ |
| static int login_with_otp(int fd, char* passwd) |
static int login_with_otp(int fd, char* passwd) |
| { |
{ |
| int len = strlen(passwd)+1; |
int len = strlen(passwd)+1; |
| char *buf = alloca(len); |
char *buf = alloca(len); |
| int n = read(fd, buf, len); |
int n = read(fd, buf, len); |
| int ret = strcmp(passwd, buf); |
int ret = strcmp(passwd, buf); |
| |
|
| #ifdef DEBUG |
#ifdef DEBUG |
| if (ret != 0) { |
if (ret != 0) { |
| fprintf(stderr, "Socket#%d: Login incorrect.\n", fd); |
fprintf(stderr, "Socket#%d: Login incorrect.\n", fd); |
| }else { |
}else { |
| fprintf(stderr, "Socket#%d: login!.\n", fd); |
fprintf(stderr, "Socket#%d: login!.\n", fd); |
| } |
} |
| fprintf(stderr, "password = (%s), %d bytes.\n", passwd, len); |
fprintf(stderr, "password = (%s), %d bytes.\n", passwd, len); |
| fprintf(stderr, "received = (%s), %d bytes.\n", buf, n); |
fprintf(stderr, "received = (%s), %d bytes.\n", buf, n); |
| fflush(stderr); |
fflush(stderr); |
| #endif |
#endif |
| |
|
| return ret; |
return ret; |
| } |
} |
| |
|
| static int exists_ox(char *dir, char *prog) |
static int exists_ox(char *dir, char *prog) |
| { |
{ |
| char *path = alloca(strlen(dir)+strlen(prog)+6); |
char *path = alloca(strlen(dir)+strlen(prog)+6); |
| sprintf(path, "%s/%s", dir, prog); |
sprintf(path, "%s/%s", dir, prog); |
| return access(path, X_OK|R_OK); |
return access(path, X_OK|R_OK); |
| } |
} |
| |
|
| static char *search_ox(char *prog) |
static char *search_ox(char *prog) |
| { |
{ |
| char *env = getenv("OpenXM_HOME"); |
char *env = getenv("OpenXM_HOME"); |
| char *dir; |
char *dir; |
| if (env != NULL) { |
if (env != NULL) { |
| dir = malloc(strlen(env)+5); |
dir = malloc(strlen(env)+5); |
| sprintf(dir, "%s/bin", env); |
sprintf(dir, "%s/bin", env); |
| if (exists_ox(dir, prog) == 0) { |
if (exists_ox(dir, prog) == 0) { |
| return dir; |
return dir; |
| } |
} |
| free(dir); |
free(dir); |
| } |
} |
| dir = "/usr/local/OpenXM/bin"; |
dir = "/usr/local/OpenXM/bin"; |
| if (exists_ox(dir, prog) == 0) { |
if (exists_ox(dir, prog) == 0) { |
| return dir; |
return dir; |
| } |
} |
| dir = "."; |
dir = "."; |
| if (exists_ox(dir, prog) == 0) { |
if (exists_ox(dir, prog) == 0) { |
| return dir; |
return dir; |
| } |
} |
| return NULL; |
return NULL; |
| } |
} |
| |
|
| static int mysocketAccept2(int fd, char *pass) |
static int mysocketAccept2(int fd, char *pass) |
| { |
{ |
| fd = mysocketAccept(fd); |
fd = mysocketAccept(fd); |
| if(login_with_otp(fd, pass)==0) { |
if(login_with_otp(fd, pass)==0) { |
| decideByteOrderClient(fd, 0); |
decideByteOrderClient(fd, 0); |
| return fd; |
return fd; |
| } |
} |
| close(fd); |
close(fd); |
| return -1; |
return -1; |
| } |
} |
| |
|
| /* |
/* |
| Line 695 static int mysocketAccept2(int fd, char *pass) |
|
| Line 698 static int mysocketAccept2(int fd, char *pass) |
|
| |
|
| ox_file_t ox_start(char* host, char* ctl_prog, char* dat_prog) |
ox_file_t ox_start(char* host, char* ctl_prog, char* dat_prog) |
| { |
{ |
| char *pass; |
char *pass; |
| char ctl[16], dat[16]; |
char ctl[16], dat[16]; |
| short portControl = 0; /* short であることに注意 */ |
short portControl = 0; /* short であることに注意 */ |
| short portStream = 0; |
short portStream = 0; |
| ox_file_t sv = NULL; |
ox_file_t sv = NULL; |
| char *dir; |
char *dir; |
| |
|
| if ((dir = search_ox(ctl_prog)) == NULL) { |
if ((dir = search_ox(ctl_prog)) == NULL) { |
| fprintf(stderr, "client:: %s not found.\n", ctl_prog); |
fprintf(stderr, "client:: %s not found.\n", ctl_prog); |
| return NULL; |
return NULL; |
| } |
} |
| sv = malloc(sizeof(__ox_file_struct)); |
sv = malloc(sizeof(__ox_file_struct)); |
| sv->control = mysocketListen(host, &portControl); |
sv->control = mysocketListen(host, &portControl); |
| sv->stream = mysocketListen(host, &portStream); |
sv->stream = mysocketListen(host, &portStream); |
| |
|
| sprintf(ctl, "%d", portControl); |
sprintf(ctl, "%d", portControl); |
| sprintf(dat, "%d", portStream); |
sprintf(dat, "%d", portStream); |
| pass = create_otp(); |
pass = create_otp(); |
| |
|
| if (fork() == 0) { |
if (fork() == 0) { |
| dup2(2, 1); |
dup2(2, 1); |
| dup2(open(DEFAULT_LOGFILE, O_RDWR|O_CREAT|O_TRUNC, 0644), 2); |
dup2(open(DEFAULT_LOGFILE, O_RDWR|O_CREAT|O_TRUNC, 0644), 2); |
| chdir(dir); |
chdir(dir); |
| execl(ctl_prog, ctl_prog, "-reverse", "-ox", dat_prog, |
execl(ctl_prog, ctl_prog, "-reverse", "-ox", dat_prog, |
| "-data", dat, "-control", ctl, "-pass", pass, |
"-data", dat, "-control", ctl, "-pass", pass, |
| "-host", host, NULL); |
"-host", host, NULL); |
| } |
} |
| |
|
| if ((sv->control = mysocketAccept2(sv->control, pass)) == -1) { |
if ((sv->control = mysocketAccept2(sv->control, pass)) == -1) { |
| close(sv->stream); |
close(sv->stream); |
| return NULL; |
return NULL; |
| } |
} |
| /* 10マイクロ秒, 時間稼ぎする. */ |
/* 10マイクロ秒, 時間稼ぎする. */ |
| usleep(10); |
usleep(10); |
| if((sv->stream = mysocketAccept2(sv->stream, pass)) == -1) { |
if((sv->stream = mysocketAccept2(sv->stream, pass)) == -1) { |
| return NULL; |
return NULL; |
| } |
} |
| return sv; |
return sv; |
| } |
} |
| |
|
| Line 748 ox_file_t ox_start_insecure_nonreverse(char* host, sho |
|
| Line 751 ox_file_t ox_start_insecure_nonreverse(char* host, sho |
|
| { |
{ |
| ox_file_t sv = malloc(sizeof(__ox_file_struct)); |
ox_file_t sv = malloc(sizeof(__ox_file_struct)); |
| |
|
| sv->control = mysocketOpen(host, portControl); |
sv->control = mysocketOpen(host, portControl); |
| #if 0 |
#if 0 |
| /* ox は insecure のとき byte order の決定が正しくできないようだ... */ |
/* ox は insecure のとき byte order の決定が正しくできないようだ... */ |
| decideByteOrderClient(sv->control, 0); |
decideByteOrderClient(sv->control, 0); |
| #endif |
#endif |
| /* 10マイクロ秒, 時間稼ぎする. */ |
/* 10マイクロ秒, 時間稼ぎする. */ |
| usleep(10); |
usleep(10); |
| sv->stream = mysocketOpen(host, portStream); |
sv->stream = mysocketOpen(host, portStream); |
| decideByteOrderClient(sv->stream, 0); |
decideByteOrderClient(sv->stream, 0); |
| return sv; |
return sv; |
| } |
} |
| |
|
| void ox_reset(ox_file_t sv) |
void ox_reset(ox_file_t sv) |
| Line 816 static int cmolen_cmo_list(cmo_list* c) |
|
| Line 819 static int cmolen_cmo_list(cmo_list* c) |
|
| |
|
| static int cmolen_cmo_monomial32(cmo_monomial32* c) |
static int cmolen_cmo_monomial32(cmo_monomial32* c) |
| { |
{ |
| int len = (c->length + 1)*sizeof(int); |
int len = (c->length + 1)*sizeof(int); |
| return len + cmolen_cmo(c->coef); |
return len + cmolen_cmo(c->coef); |
| } |
} |
| |
|
| static int cmolen_cmo_zz(cmo_zz* c) |
static int cmolen_cmo_zz(cmo_zz* c) |
| Line 828 static int cmolen_cmo_zz(cmo_zz* c) |
|
| Line 831 static int cmolen_cmo_zz(cmo_zz* c) |
|
| |
|
| static int cmolen_cmo_distributed_polynomial(cmo_distributed_polynomial* c) |
static int cmolen_cmo_distributed_polynomial(cmo_distributed_polynomial* c) |
| { |
{ |
| return cmolen_cmo_list((cmo_list *)c) + cmolen_cmo(c->ringdef); |
return cmolen_cmo_list((cmo_list *)c) + cmolen_cmo(c->ringdef); |
| } |
} |
| |
|
| /* CMO がバイトエンコードされた場合のバイト列の長さを求める */ |
/* CMO がバイトエンコードされた場合のバイト列の長さを求める */ |
| Line 863 int cmolen_cmo(cmo* c) |
|
| Line 866 int cmolen_cmo(cmo* c) |
|
| case CMO_ZZ: |
case CMO_ZZ: |
| size += cmolen_cmo_zz((cmo_zz *)c); |
size += cmolen_cmo_zz((cmo_zz *)c); |
| break; |
break; |
| case CMO_DISTRIBUTED_POLYNOMIAL: |
case CMO_DISTRIBUTED_POLYNOMIAL: |
| size += cmolen_cmo_distributed_polynomial((cmo_distributed_polynomial *)c); |
size += cmolen_cmo_distributed_polynomial((cmo_distributed_polynomial *)c); |
| break; |
break; |
| default: |
default: |
| } |
} |
| return size; |
return size; |
| Line 876 static char *d_buf; |
|
| Line 879 static char *d_buf; |
|
| |
|
| int init_dump_buffer(char *s) |
int init_dump_buffer(char *s) |
| { |
{ |
| d_buf = s; |
d_buf = s; |
| d_ptr = 0; |
d_ptr = 0; |
| } |
} |
| |
|
| static int dump_cmo_null(cmo_null* m) |
static int dump_cmo_null(cmo_null* m) |
| { |
{ |
| return 0; |
return 0; |
| } |
} |
| |
|
| static int dump_cmo_int32(cmo_int32* m) |
static int dump_cmo_int32(cmo_int32* m) |
| Line 894 static int dump_cmo_string(cmo_string* m) |
|
| Line 897 static int dump_cmo_string(cmo_string* m) |
|
| { |
{ |
| int len = strlen(m->s); |
int len = strlen(m->s); |
| dump_integer(len); |
dump_integer(len); |
| dump_string(m->s, len); |
dump_string(m->s, len); |
| } |
} |
| |
|
| static int dump_cmo_mathcap(cmo_mathcap* c) |
static int dump_cmo_mathcap(cmo_mathcap* c) |
| Line 916 static int dump_cmo_list(cmo_list* m) |
|
| Line 919 static int dump_cmo_list(cmo_list* m) |
|
| |
|
| static int dump_cmo_monomial32(cmo_monomial32* c) |
static int dump_cmo_monomial32(cmo_monomial32* c) |
| { |
{ |
| int i; |
int i; |
| int length = c->length; |
int length = c->length; |
| dump_integer(c->length); |
dump_integer(c->length); |
| for(i=0; i<length; i++) { |
for(i=0; i<length; i++) { |
| dump_integer(c->exps[i]); |
dump_integer(c->exps[i]); |
| } |
} |
| dump_cmo(c->coef); |
dump_cmo(c->coef); |
| } |
} |
| |
|
| static int dump_cmo_zz(cmo_zz* c) |
static int dump_cmo_zz(cmo_zz* c) |
| Line 935 static int dump_cmo_distributed_polynomial(cmo_distrib |
|
| Line 938 static int dump_cmo_distributed_polynomial(cmo_distrib |
|
| cell* cp = m->head; |
cell* cp = m->head; |
| int len = length_cmo_list((cmo_list *)m); |
int len = length_cmo_list((cmo_list *)m); |
| dump_integer(len); |
dump_integer(len); |
| dump_cmo(m->ringdef); |
dump_cmo(m->ringdef); |
| while(cp != NULL) { |
while(cp != NULL) { |
| dump_cmo(cp->cmo); |
dump_cmo(cp->cmo); |
| cp = cp->next; |
cp = cp->next; |
| Line 951 int dump_cmo(cmo* m) |
|
| Line 954 int dump_cmo(cmo* m) |
|
| case CMO_ZERO: |
case CMO_ZERO: |
| case CMO_DMS_GENERIC: |
case CMO_DMS_GENERIC: |
| dump_cmo_null(m); |
dump_cmo_null(m); |
| break; |
break; |
| case CMO_INT32: |
case CMO_INT32: |
| dump_cmo_int32((cmo_int32 *)m); |
dump_cmo_int32((cmo_int32 *)m); |
| break; |
break; |
| case CMO_STRING: |
case CMO_STRING: |
| dump_cmo_string((cmo_string *)m); |
dump_cmo_string((cmo_string *)m); |
| break; |
break; |
| case CMO_MATHCAP: |
case CMO_MATHCAP: |
| case CMO_RING_BY_NAME: |
case CMO_RING_BY_NAME: |
| case CMO_INDETERMINATE: |
case CMO_INDETERMINATE: |
| case CMO_ERROR2: |
case CMO_ERROR2: |
| dump_cmo_mathcap((cmo_mathcap *)m); |
dump_cmo_mathcap((cmo_mathcap *)m); |
| break; |
break; |
| case CMO_LIST: |
case CMO_LIST: |
| dump_cmo_list((cmo_list *)m); |
dump_cmo_list((cmo_list *)m); |
| break; |
break; |
| case CMO_MONOMIAL32: |
case CMO_MONOMIAL32: |
| dump_cmo_monomial32((cmo_monomial32 *)m); |
dump_cmo_monomial32((cmo_monomial32 *)m); |
| break; |
break; |
| case CMO_ZZ: |
case CMO_ZZ: |
| dump_cmo_zz((cmo_zz *)m); |
dump_cmo_zz((cmo_zz *)m); |
| break; |
break; |
| case CMO_DISTRIBUTED_POLYNOMIAL: |
case CMO_DISTRIBUTED_POLYNOMIAL: |
| dump_cmo_distributed_polynomial((cmo_distributed_polynomial *)m); |
dump_cmo_distributed_polynomial((cmo_distributed_polynomial *)m); |
| break; |
break; |
| default: |
default: |
| } |
} |
| } |
} |
| Line 1000 static int dump_string(char *s, int len) |
|
| Line 1003 static int dump_string(char *s, int len) |
|
| static int dump_integer(int x) |
static int dump_integer(int x) |
| { |
{ |
| int nx = htonl(x); |
int nx = htonl(x); |
| dump_string((char *)&nx, sizeof(int)); |
dump_string((char *)&nx, sizeof(int)); |
| } |
} |
| |
|
| int dump_ox_data(ox_data* m) |
int dump_ox_data(ox_data* m) |
| Line 1088 static int send_cmo_distributed_polynomial(int fd, cmo |
|
| Line 1091 static int send_cmo_distributed_polynomial(int fd, cmo |
|
| cell* cp = c->head; |
cell* cp = c->head; |
| int len = length_cmo_list((cmo_list *)c); |
int len = length_cmo_list((cmo_list *)c); |
| send_int32(fd, len); |
send_int32(fd, len); |
| send_cmo(fd, c->ringdef); |
send_cmo(fd, c->ringdef); |
| |
|
| while(cp->next != NULL) { |
while(cp->next != NULL) { |
| send_cmo(fd, cp->cmo); |
send_cmo(fd, cp->cmo); |
| Line 1099 static int send_cmo_distributed_polynomial(int fd, cmo |
|
| Line 1102 static int send_cmo_distributed_polynomial(int fd, cmo |
|
| |
|
| static int send_cmo_monomial32(int fd, cmo_monomial32* c) |
static int send_cmo_monomial32(int fd, cmo_monomial32* c) |
| { |
{ |
| int i; |
int i; |
| int len = c->length; |
int len = c->length; |
| send_int32(fd, len); |
send_int32(fd, len); |
| for(i=0; i<len; i++) { |
for(i=0; i<len; i++) { |
| send_int32(fd, c->exps[i]); |
send_int32(fd, c->exps[i]); |
| } |
} |
| send_cmo(fd, c->coef); |
send_cmo(fd, c->coef); |
| return 0; |
return 0; |
| } |
} |
| |
|
| static int send_cmo_zz(int fd, cmo_zz* c) |
static int send_cmo_zz(int fd, cmo_zz* c) |
| { |
{ |
| send_mpz(fd, c->mpz); |
send_mpz(fd, c->mpz); |
| return 0; |
return 0; |
| } |
} |
| |
|
| static int send_cmo_error2(int fd, cmo_error2* c) |
static int send_cmo_error2(int fd, cmo_error2* c) |
| Line 1188 ox_command* new_ox_command(int sm_code) |
|
| Line 1191 ox_command* new_ox_command(int sm_code) |
|
| return m; |
return m; |
| } |
} |
| |
|
| #define MAX_TYPES 8 |
ox_sync_ball* new_ox_sync_ball() |
| static int known_types[] = { |
{ |
| -1, /* gate keeper */ |
ox_sync_ball *m = malloc(sizeof(ox_sync_ball)); |
| CMO_NULL, |
m->tag = OX_SYNC_BALL; |
| CMO_INT32, |
return m; |
| CMO_STRING, |
} |
| CMO_MATHCAP, |
|
| CMO_LIST, |
|
| CMO_ZZ, |
|
| CMO_ERROR2, |
|
| }; |
|
| |
|
| #define ID_TEMP "(CMO_LIST, (CMO_INT32, %d), (CMO_STRING, \"%s\"), (CMO_STRING, \"%s\"), (CMO_STRING, \"%s\"))" |
#define ID_TEMP "(CMO_LIST, (CMO_INT32, %d), (CMO_STRING, \"%s\"), (CMO_STRING, \"%s\"), (CMO_STRING, \"%s\"))" |
| |
|
| |
|
| static cmo_list* make_list_of_id(int ver, char* ver_s, char* sysname) |
static cmo_list* make_list_of_id(int ver, char* ver_s, char* sysname) |
| { |
{ |
| cmo_list *cap; |
cmo_list *cap; |
| Line 1219 static cmo_list* make_list_of_id(int ver, char* ver_s, |
|
| Line 1216 static cmo_list* make_list_of_id(int ver, char* ver_s, |
|
| |
|
| static cmo_list *make_list_of_tag(int type) |
static cmo_list *make_list_of_tag(int type) |
| { |
{ |
| cmo_list *li = new_cmo_list(); |
cmo_list *li = new_cmo_list(); |
| symbol *symp; |
symbol *symp; |
| int i = 0; |
int i = 0; |
| while((symp = lookup(i++))->key != NULL) { |
while((symp = lookup(i++))->key != NULL) { |
| if (symp->type == type) { |
if (symp->type == type) { |
| append_cmo_list(li, (cmo *)new_cmo_int32(symp->tag)); |
append_cmo_list(li, (cmo *)new_cmo_int32(symp->tag)); |
| } |
} |
| } |
} |
| return li; |
return li; |
| } |
} |
| |
|
| cmo* make_mathcap_object(int version, char *id_string) |
cmo* make_mathcap_object(int version, char *id_string) |
| { |
{ |
| char sysname[] = "ox_math"; |
char *sysname = "ox_math"; |
| cmo_list *li = new_cmo_list(); |
cmo_list *li = new_cmo_list(); |
| cmo_list *li_ver = make_list_of_id(version, id_string, sysname); |
cmo_list *li_1st = make_list_of_id(version, id_string, sysname); |
| cmo_list *li_cmo = make_list_of_tag(IS_CMO); |
cmo_list *li_2nd = make_list_of_tag(IS_SM); |
| cmo_list *li_sm = make_list_of_tag(IS_SM); |
cmo_list *li_3rd = new_cmo_list(); |
| |
cmo_list *li_cmo = make_list_of_tag(IS_CMO); |
| |
|
| append_cmo_list(li, (cmo *)li_ver); |
cmo_list *li_ox_data = new_cmo_list(); |
| append_cmo_list(li, (cmo *)li_cmo); |
append_cmo_list(li_ox_data, (cmo *)new_cmo_int32(OX_DATA)); |
| append_cmo_list(li, (cmo *)li_sm); |
append_cmo_list(li_ox_data, (cmo *)li_cmo); |
| |
append_cmo_list(li_3rd, (cmo *)li_ox_data); |
| |
|
| |
append_cmo_list(li, (cmo *)li_1st); |
| |
append_cmo_list(li, (cmo *)li_2nd); |
| |
append_cmo_list(li, (cmo *)li_3rd); |
| |
|
| return (cmo *)new_cmo_mathcap((cmo *)li); |
return (cmo *)new_cmo_mathcap((cmo *)li); |
| } |
} |
| |
|
| static int funcs(int cmo_type) |
|
| { |
|
| int i; |
|
| for(i=0; i<MAX_TYPES; i++) { |
|
| if (known_types[i] == cmo_type) { |
|
| return i; |
|
| } |
|
| } |
|
| return 0; |
|
| } |
|
| |
|
| void setCmotypeDisable(int type) |
|
| { |
|
| int i = funcs(type); |
|
| known_types[i] = -1; |
|
| } |
|
| |
|
| #if 0 |
|
| cmo* (*received_funcs[])(int fd) = { |
|
| NULL, /* gate keeper */ |
|
| receive_cmo_null, |
|
| receive_cmo_int32, |
|
| receive_cmo_string, |
|
| receive_cmo_mathcap, |
|
| receive_cmo_list, |
|
| receive_cmo_zz, |
|
| receive_cmo_error2 |
|
| }; |
|
| |
|
| cmo* receive_cmo2(int fd) |
|
| { |
|
| int tag; |
|
| cmo* (*foo)() = received_funcs[funcs(tag)]; |
|
| if (foo != NULL) { |
|
| return foo(fd); |
|
| } |
|
| } |
|
| #endif |
|
| |
|
| /* ファイルディスクリプタ fd の通信路での integer の byte order を決定する */ |
/* ファイルディスクリプタ fd の通信路での integer の byte order を決定する */ |
| /* 実際には order (0,1,or 0xFF)をみてはいない */ |
/* 実際には order (0,1,or 0xFF)をみてはいない */ |
| int decideByteOrderClient(oxfd fd, int order) |
int decideByteOrderClient(oxfd fd, int order) |
| Line 1312 char *convert_zz_to_string(cmo_zz *c) |
|
| Line 1276 char *convert_zz_to_string(cmo_zz *c) |
|
| return mpz_get_str(NULL, 10, c->mpz); |
return mpz_get_str(NULL, 10, c->mpz); |
| } |
} |
| |
|
| char *convert_cmo_to_string(cmo *m) |
|
| { |
|
| symbol *symp; |
|
| switch(m->tag) { |
|
| case CMO_ZZ: |
|
| return convert_zz_to_string((cmo_zz *)m); |
|
| case CMO_INT32: |
|
| return convert_int_to_string(((cmo_int32 *)m)->i); |
|
| case CMO_STRING: |
|
| return ((cmo_string *)m)->s; |
|
| case CMO_NULL: |
|
| return convert_null_to_string(); |
|
| default: |
|
| symp = lookup_by_tag(m->tag); |
|
| fprintf(stderr, "I do not know how to convert %s to a string.\n", symp->key); |
|
| /* まだ実装していません. */ |
|
| return NULL; |
|
| } |
|
| } |
|
| |
|
| char *convert_null_to_string() |
char *convert_null_to_string() |
| { |
{ |
| static char* null_string = ""; |
static char* null_string = ""; |
| Line 1348 char *convert_int_to_string(int integer) |
|
| Line 1292 char *convert_int_to_string(int integer) |
|
| strcpy(s, buff); |
strcpy(s, buff); |
| |
|
| return s; |
return s; |
| |
} |
| |
|
| |
char *convert_cmo_list_to_string(cmo_list *m) |
| |
{ |
| |
char *s; |
| |
int i; |
| |
int size = 0; |
| |
int len = length_cmo_list(m); |
| |
char **sp = malloc(len*sizeof(cmo *)); |
| |
|
| |
cell *cp = m->head; |
| |
for(i = 0; i < len; i++) { |
| |
sp[i] = convert_cmo_to_string(cp->cmo); |
| |
size += strlen(sp[i]) + 3; |
| |
cp = cp->next; |
| |
} |
| |
s = malloc(size+2); |
| |
strcpy(s, "[ "); |
| |
for(i = 0; i < len - 1; i++) { |
| |
strcat(s, sp[i]); |
| |
strcat(s, " , "); |
| |
} |
| |
strcat(s, sp[len-1]); |
| |
strcat(s, " ]"); |
| |
free(sp); |
| |
return s; |
| |
} |
| |
|
| |
char *convert_cmo_to_string(cmo *m) |
| |
{ |
| |
symbol *symp; |
| |
switch(m->tag) { |
| |
case CMO_ZZ: |
| |
return convert_zz_to_string((cmo_zz *)m); |
| |
case CMO_INT32: |
| |
return convert_int_to_string(((cmo_int32 *)m)->i); |
| |
case CMO_STRING: |
| |
return ((cmo_string *)m)->s; |
| |
case CMO_NULL: |
| |
return convert_null_to_string(); |
| |
case CMO_LIST: |
| |
return convert_cmo_list_to_string((cmo_list *)m); |
| |
default: |
| |
#ifdef DEBUG |
| |
symp = lookup_by_tag(m->tag); |
| |
fprintf(stderr, "I do not know how to convert %s to a string.\n", symp->key); |
| |
#endif |
| |
/* まだ実装していません. */ |
| |
return NULL; |
| |
} |
| } |
} |