Annotation of OpenXM/src/ox_math/ox.c, Revision 1.5
1.1 ohara 1: /* -*- mode: C; coding: euc-japan -*- */
1.5 ! ohara 2: /* $OpenXM: OpenXM/src/ox_math/ox.c,v 1.4 1999/11/02 21:15:02 ohara Exp $ */
1.1 ohara 3:
4: /*
5: 関数の名前付け規約(その2):
6: (1) receive_cmo 関数はCMOタグとデータ本体を受信する. この関数は CMOタグの
7: 値が事前に分からないときに使用する. 返り値として、cmo へのポインタを返す.
8: (2) receive_cmo_XXX 関数は, CMOタグを親の関数で受信してから呼び出される関
9: 数で、データ本体のみを受信し、cmo_XXX へのポインタを返す. しかも、
10: 関数内部で new_cmo_XXX 関数を呼び出す.
11: (3) send_cmo 関数はCMOタグとデータ本体を送信する.
12: (4) send_cmo_XXX 関数はCMOタグを親の関数で送信してから呼び出される関数で、
13: データ本体のみを送信する.
14:
15: ----
16: (5) receive_ox_XXX 関数は存在しない(作らない). receive_cmo を利用する.
17: (6) send_ox_XXX 関数は OX タグを含めて送信する.
18: (7) ox_XXX 関数は一連の送受信を含むより抽象的な操作を表現する.
19: ox_XXX 関数は、第一引数として、ox_file_t型の変数 sv をとる.
20:
21: (8) YYY_cmo 関数と YYY_cmo_XXX 関数の関係は次の通り:
22: まず YYY_cmo 関数で cmo のタグを処理し、タグを除いた残りの部分を
23: YYY_cmo_XXX 関数が処理する. cmo の内部に cmo_ZZZ へのポインタが
24: あるときには、その種類によらずに YYY_cmo 関数を呼び出す.
25: */
26:
27: #include <stdio.h>
28: #include <stdlib.h>
29: #include <string.h>
30: #include <unistd.h>
31: #include <errno.h>
32: #include <fcntl.h>
33: #include <gmp.h>
34:
35: #include "mysocket.h"
36: #include "ox.h"
37: #include "parse.h"
38:
39: static int cmolen_cmo_int32(cmo_int32* c);
40: static int cmolen_cmo_list(cmo_list* c);
41: static int cmolen_cmo_mathcap(cmo_mathcap* c);
42: static int cmolen_cmo_null(cmo_null* c);
43: static int cmolen_cmo_string(cmo_string* c);
44: static int cmolen_cmo_zz(cmo_zz* c);
1.2 ohara 45: static int cmolen_cmo_monomial32(cmo_monomial32* c);
1.1 ohara 46:
1.4 ohara 47: static int dump_cmo_int32(cmo_int32* m);
48: static int dump_cmo_list(cmo_list* m);
49: static int dump_cmo_mathcap(cmo_mathcap* m);
50: static int dump_cmo_null(cmo_null* m);
51: static int dump_cmo_string(cmo_string* m);
52: static int dump_cmo_monomial32(cmo_monomial32* c);
53: static int dump_cmo_zz(cmo_zz* c);
54: static int dump_string(char *s, int len);
55: static int dump_integer(int x);
56: static int dump_mpz(mpz_ptr mpz);
1.1 ohara 57:
58: static int funcs(int cmo_type);
59:
1.2 ohara 60: /* CMO_xxx の値順にならべること(デバッグのため) */
1.5 ! ohara 61: static int login_with_otp(int fd, char* passwd);
1.2 ohara 62: static cmo_null* receive_cmo_null(int fd);
1.1 ohara 63: static cmo_int32* receive_cmo_int32(int fd);
1.2 ohara 64: static cmo_string* receive_cmo_string(int fd);
65: static cmo_mathcap* receive_cmo_mathcap(int fd);
1.1 ohara 66: static cmo_list* receive_cmo_list(int fd);
67: static cmo_zz* receive_cmo_zz(int fd);
1.2 ohara 68: static cmo_zero* receive_cmo_zero(int fd);
69: static cmo_dms_generic* receive_cmo_dms_generic(int fd);
70: static cmo_error2* receive_cmo_error2(int fd);
1.1 ohara 71: static void receive_mpz(int fd, mpz_ptr mpz);
72:
1.2 ohara 73: static int send_cmo_null(int fd, cmo_null* c);
1.1 ohara 74: static int send_cmo_int32(int fd, cmo_int32* m);
1.2 ohara 75: static int send_cmo_string(int fd, cmo_string* m);
76: static int send_cmo_mathcap(int fd, cmo_mathcap* c);
1.1 ohara 77: static int send_cmo_list(int fd, cmo_list* c);
78: static int send_cmo_zz(int fd, cmo_zz* c);
1.2 ohara 79: static int send_cmo_error2(int fd, cmo_error2* c);
1.1 ohara 80: static int send_mpz(int fd, mpz_ptr mpz);
81:
82:
83: /* エラーハンドリングのため */
84: static int current_received_serial = 0;
85:
86: /* エラーを起こしたときはサーバは次のようにすればよい. */
1.2 ohara 87: cmo_error2* gen_error_object(int err_code)
1.1 ohara 88: {
89: cmo_list* li = new_cmo_list();
90: append_cmo_list(li, (cmo *)new_cmo_int32(current_received_serial));
91: append_cmo_list(li, (cmo *)new_cmo_int32(err_code));
92: /* 他の情報を加えるならココ */
1.2 ohara 93: return new_cmo_error2((cmo *)li);
1.1 ohara 94: }
95:
96: /* add at Mon Sep 7 15:51:28 JST 1998 */
97: #define DEFAULT_SERIAL_NUMBER 0x0000ffff
98: #define receive_serial_number(x) (receive_int32(x))
99:
100: /* 新しいシリアル番号を得る */
101: int next_serial()
102: {
103: static int serial_number = DEFAULT_SERIAL_NUMBER;
104: return serial_number++;
105: }
106:
107: /* int32 型のオブジェクトを送信する. */
108: int send_int32(int fd, int int32)
109: {
110: int32 = htonl(int32);
111: return write(fd, &int32, sizeof(int));
112: }
113:
114: /* int32 型のオブジェクトを受信する. */
115: int receive_int32(int fd)
116: {
117: int tag;
118: read(fd, &tag, sizeof(int));
119: return ntohl(tag);
120: }
121:
122: /* (OX_tag, serial number) を受信する. */
123: int receive_ox_tag(int fd)
124: {
125: int serial;
126: int tag = receive_int32(fd);
127: current_received_serial = receive_serial_number(fd);
128: return tag;
129: }
130:
131: /* (OX_tag, serial number) を送信する. */
132: int send_ox_tag(int fd, int tag)
133: {
134: send_int32(fd, tag);
135: return send_int32(fd, next_serial());
136: }
137:
138:
139: /* CMO_LIST 関係の関数群 */
140: cell* new_cell(cmo* newcmo)
141: {
142: cell* h = malloc(sizeof(cell));
143: h->next = NULL;
144: h->cmo = newcmo;
145: return h;
146: }
147:
1.2 ohara 148: cell* next_cell(cell* this)
149: {
150: return this->next;
151: }
152:
1.1 ohara 153: static cell* *tailp(cmo_list* this) {
154: cell **cp = &this->head;
155: while (*cp != NULL) {
156: cp = &((*cp)->next);
157: }
158: return cp;
159: }
160:
161: int length_cmo_list(cmo_list* this)
162: {
163: return this->length;
164: }
165:
166: int append_cmo_list(cmo_list* this, cmo* newcmo)
167: {
168: /* リストの最後尾のNULLを保持している変数へのポインタ */
169: cell **cp = tailp(this);
170: *cp = new_cell(newcmo);
171: this->length++;
172: return 0;
173: }
174:
175: /** receive_cmo_XXX 関数群 **/
1.2 ohara 176: static cmo_null* receive_cmo_null(int fd)
1.1 ohara 177: {
1.2 ohara 178: return new_cmo_null();
1.1 ohara 179: }
180:
181: static cmo_int32* receive_cmo_int32(int fd)
182: {
183: int i = receive_int32(fd);
184: return new_cmo_int32(i);
185: }
186:
1.2 ohara 187: static cmo_string* receive_cmo_string(int fd)
188: {
189: int len = receive_int32(fd);
190: char* s = malloc(len+1);
191: memset(s, '\0', len+1);
192: if (len > 0) {
193: read(fd, s, len);
194: }
195: return new_cmo_string(s);
196: }
197:
198: static cmo_mathcap* receive_cmo_mathcap(int fd)
199: {
200: cmo* ob = receive_cmo(fd);
201: return new_cmo_mathcap(ob);
202: }
203:
1.1 ohara 204: static cmo_list* receive_cmo_list(int fd)
205: {
206: cmo* newcmo;
207: cmo_list* c = new_cmo_list();
208: int len = receive_int32(fd);
209:
210: while (len>0) {
211: newcmo = receive_cmo(fd);
212: append_cmo_list(c, newcmo);
213: len--;
214: }
215: return c;
216: }
217:
1.2 ohara 218: static cmo_zz* receive_cmo_zz(int fd)
219: {
220: cmo_zz* c = new_cmo_zz();
221: receive_mpz(fd, c->mpz);
222: return c;
223: }
224:
225: static cmo_zero* receive_cmo_zero(int fd)
1.1 ohara 226: {
1.2 ohara 227: return new_cmo_zero();
1.1 ohara 228: }
229:
1.2 ohara 230: static cmo_dms_generic* receive_cmo_dms_generic(int fd)
1.1 ohara 231: {
1.2 ohara 232: return new_cmo_dms_generic();
1.1 ohara 233: }
234:
1.2 ohara 235: static cmo_ring_by_name* receive_cmo_ring_by_name(int fd)
1.1 ohara 236: {
1.2 ohara 237: cmo* ob = receive_cmo(fd);
238: /* 意味的チェックが必要 */
239: return new_cmo_ring_by_name(ob);
1.1 ohara 240: }
241:
1.2 ohara 242: static cmo_error2* receive_cmo_error2(int fd)
1.1 ohara 243: {
1.2 ohara 244: cmo* ob = receive_cmo(fd);
245: return new_cmo_error2(ob);
1.1 ohara 246: }
247:
248: static void receive_mpz(int fd, mpz_ptr mpz)
249: {
250: int i;
251: int size = receive_int32(fd);
252: int len = abs(size);
253: resize_mpz(mpz, size);
254:
255: for(i=0; i<len; i++) {
256: mpz->_mp_d[i] = receive_int32(fd);
257: }
258: }
259:
260: void resize_mpz(mpz_ptr mpz, int size)
261: {
262: _mpz_realloc(mpz, abs(size));
263: mpz->_mp_size = size;
264: }
265:
266: /** new_cmo_XXX 関数群 **/
1.2 ohara 267: cmo_null* new_cmo_null()
1.1 ohara 268: {
1.2 ohara 269: cmo_null* m = malloc(sizeof(cmo_null));
270: m->tag = CMO_NULL;
271: return m;
1.1 ohara 272: }
273:
274: cmo_int32* new_cmo_int32(int i)
275: {
276: cmo_int32* c;
277: c = malloc(sizeof(cmo_int32));
278: c->tag = CMO_INT32;
279: c->i = i;
280: return c;
281: }
282:
1.2 ohara 283: cmo_string* new_cmo_string(char* s)
284: {
285: cmo_string* c = malloc(sizeof(cmo_string));
286: char *s2 = malloc(strlen(s)+1);
287: strcpy(s2, s);
288:
289: c->tag = CMO_STRING;
290: c->s = s2;
291: return c;
292: }
293:
294: cmo_mathcap* new_cmo_mathcap(cmo* ob)
295: {
296: cmo_mathcap* c = malloc(sizeof(cmo_mathcap));
297: c->tag = CMO_MATHCAP;
298: c->ob = ob;
299: return c;
300: }
301:
1.1 ohara 302: cmo_list* new_cmo_list()
303: {
304: cmo_list* c = malloc(sizeof(cmo_list));
305: c->tag = CMO_LIST;
306: c->length = 0;
307: c->head = NULL;
308: return c;
309: }
310:
1.2 ohara 311: cmo_monomial32* new_cmo_monomial32()
1.1 ohara 312: {
1.2 ohara 313: cmo_monomial32* c = malloc(sizeof(cmo_monomial32));
314: c->tag = CMO_MONOMIAL32;
1.1 ohara 315: return c;
316: }
317:
1.2 ohara 318: cmo_monomial32* new_cmo_monomial32_size(int size)
1.1 ohara 319: {
1.2 ohara 320: cmo_monomial32* c = new_cmo_monomial32();
321: if (size>0) {
322: c->length = size;
323: c->exps = malloc(sizeof(int)*size);
324: }
1.1 ohara 325: return c;
326: }
327:
328: cmo_zz* new_cmo_zz()
329: {
330: cmo_zz* c = malloc(sizeof(cmo_zz));
331: c->tag = CMO_ZZ;
332: mpz_init(c->mpz);
333: return c;
334: }
335:
336: cmo_zz* new_cmo_zz_noinit()
337: {
338: cmo_zz* c = malloc(sizeof(cmo_zz));
339: c->tag = CMO_ZZ;
340: return c;
341: }
342:
343: cmo_zz* new_cmo_zz_set_si(int i)
344: {
345: cmo_zz* c = new_cmo_zz();
346: mpz_set_si(c->mpz, i);
347: return c;
348: }
349:
1.5 ! ohara 350: cmo_zz *new_cmo_zz_set_string(char *s)
! 351: {
! 352: cmo_zz* c = new_cmo_zz_noinit();
! 353: mpz_init_set_str(c->mpz, s, 10);
! 354: return c;
! 355: }
! 356:
1.1 ohara 357: cmo_zz* new_cmo_zz_size(int size)
358: {
359: cmo_zz* c = new_cmo_zz();
360: resize_mpz(c->mpz, size);
361: return c;
362: }
363:
1.2 ohara 364: cmo_zero* new_cmo_zero()
365: {
366: cmo_zero* m = malloc(sizeof(cmo_zero));
367: m->tag = CMO_ZERO;
368: return m;
369: }
370:
371: cmo_dms_generic* new_cmo_dms_generic()
372: {
373: cmo_dms_generic* m = malloc(sizeof(cmo_dms_generic));
374: m->tag = CMO_DMS_GENERIC;
375: return m;
376: }
377:
378: cmo_ring_by_name* new_cmo_ring_by_name(cmo* ob)
379: {
380: cmo_ring_by_name* c = malloc(sizeof(cmo_ring_by_name));
381: c->tag = CMO_RING_BY_NAME;
382: c->ob = ob;
383: return c;
384: }
385:
386: cmo_indeterminate* new_cmo_indeterminate(cmo* ob)
387: {
388: cmo_indeterminate* c = malloc(sizeof(cmo_indeterminate));
389: c->tag = CMO_INDETERMINATE;
390: c->ob = ob;
391: return c;
392: }
393:
1.3 ohara 394: cmo_distributed_polynomial* new_cmo_distributed_polynomial()
395: {
396: cmo_distributed_polynomial* c = malloc(sizeof(cmo_distributed_polynomial));
397: c->tag = CMO_DISTRIBUTED_POLYNOMIAL;
398: c->length = 0;
399: c->head = NULL;
400: c->ringdef = NULL;
401: return c;
402: }
403:
1.2 ohara 404: cmo_error2* new_cmo_error2(cmo* ob)
405: {
406: cmo_error2* c = malloc(sizeof(cmo_error2));
407: c->tag = CMO_ERROR2;
408: c->ob = ob;
409: return c;
410: }
411:
1.1 ohara 412: /* receive_ox_tag() == OX_DATA の後に呼び出される */
413: /* 関数ポインタを使った方がきれいに書けるような気がする. */
414: /* if (foo[tag] != NULL) foo[tag](fd); とか */
415:
416: cmo* receive_cmo(int fd)
417: {
418: cmo* m;
419: int tag;
420: tag = receive_int32(fd);
421:
422: switch(tag) {
423: case CMO_NULL:
424: m = receive_cmo_null(fd);
425: break;
426: case CMO_INT32:
427: m = (cmo *)receive_cmo_int32(fd);
428: break;
429: case CMO_STRING:
430: m = (cmo *)receive_cmo_string(fd);
431: break;
1.2 ohara 432: case CMO_MATHCAP:
433: m = (cmo *)receive_cmo_mathcap(fd);
1.1 ohara 434: break;
435: case CMO_LIST:
436: m = (cmo *)receive_cmo_list(fd);
437: break;
1.2 ohara 438: case CMO_ZZ:
439: m = (cmo *)receive_cmo_zz(fd);
1.1 ohara 440: break;
441: case CMO_ERROR2:
1.2 ohara 442: m = (cmo *)receive_cmo_error2(fd);
1.1 ohara 443: break;
444: case CMO_DATUM:
445: case CMO_QQ:
446: case CMO_ZERO:
447: case CMO_DMS:
448: default:
449: fprintf(stderr, "unknown cmo-type: tag = (%d)\n", m->tag);
450: }
451: return m;
452: }
453:
454: void send_ox_command(int fd, int sm_command)
455: {
456: send_ox_tag(fd, OX_COMMAND);
457: send_int32(fd, sm_command);
458: }
459:
460: int print_cmo(cmo* c)
461: {
462: int tag = c->tag;
463: fprintf(stderr, "local::tag = (%d): ", tag);
464: switch(tag) {
465: case CMO_LIST:
466: print_cmo_list((cmo_list *)c);
467: break;
468: case CMO_INT32:
469: print_cmo_int32((cmo_int32 *)c);
470: break;
471: case CMO_MATHCAP:
472: print_cmo_mathcap((cmo_mathcap *)c);
473: break;
474: case CMO_STRING:
475: print_cmo_string((cmo_string *)c);
476: break;
477: case CMO_NULL:
478: fprintf(stderr, "\n");
479: break;
480: default:
481: fprintf(stderr, "print_cmo() does not know how to print.\n");
482: }
483: }
484:
485: int print_cmo_int32(cmo_int32* c)
486: {
487: fprintf(stderr, "cmo_int32 = (%d)\n", c->i);
488: }
489:
490: int print_cmo_list(cmo_list* li)
491: {
492: cell* cp = li->head;
493: fprintf(stderr, "length = (%d)\nlist:\n", li->length);
494: while(cp != NULL) {
495: print_cmo(cp->cmo);
496: cp=cp->next;
497: }
498: fprintf(stderr, "end of list\n");
499: }
500:
501: int print_cmo_mathcap(cmo_mathcap* c)
502: {
503: fprintf(stderr, "\n");
1.2 ohara 504: print_cmo(c->ob);
1.1 ohara 505: }
506:
507: int print_cmo_string(cmo_string* c)
508: {
509: fprintf(stderr, "cmo_string = (%s)\n", c->s);
510: }
511:
512: void ox_close(ox_file_t sv)
513: {
514: send_ox_command(sv->control, SM_control_kill);
515: #if DEBUG
516: sleep(2); /* OpenXM server の終了を待つ. あまり意味はない. */
517: fprintf(stderr, "I have closed an Open XM server.\n");
518: #endif
519: }
520:
521: void ox_executeStringByLocalParser(ox_file_t sv, char* s)
522: {
523: /* 文字列ををスタックにプッシュ. */
524: send_ox_cmo(sv->stream, (cmo *)new_cmo_string(s));
525:
526: /* サーバに実行させる. */
527: send_ox_command(sv->stream, SM_executeStringByLocalParser);
528: }
529:
530: /* ox_mathcap() をコールする. */
531: cmo_mathcap* ox_mathcap(ox_file_t sv)
532: {
533: send_ox_command(sv->stream, SM_mathcap);
534: send_ox_command(sv->stream, SM_popCMO);
535: receive_ox_tag(sv->stream); /* OX_DATA */
536: return (cmo_mathcap *)receive_cmo(sv->stream);
537: }
538:
539: char* ox_popString(ox_file_t sv, int fd)
540: {
541: cmo_string* m = NULL;
542:
543: send_ox_command(fd, SM_popString);
544: receive_ox_tag(fd); /* OX_DATA */
545: m = (cmo_string *)receive_cmo(fd);
546: return m->s;
547: }
548:
549: cmo* ox_pop_cmo(ox_file_t sv, int fd)
550: {
551: send_ox_command(fd, SM_popCMO);
552: receive_ox_tag(fd); /* OX_DATA */
553: return receive_cmo(fd);
554: }
555:
1.5 ! ohara 556: /* 手抜き. (後で改善しよう...) */
! 557: static char *create_otp()
! 558: {
! 559: static char otp[] = "otpasswd";
! 560: return otp;
! 561: }
! 562:
! 563: /* OneTimePassword の処理 */
! 564: static int login_with_otp(int fd, char* passwd)
! 565: {
! 566: char buff[1024];
! 567: int n = read(fd, buff, 1024);
! 568: int len = strlen(passwd)+1;
! 569: if (n != len) {
! 570: fprintf(stderr, "Socket#%d: Login incorrect.\n", fd);
! 571: fprintf(stderr, "password = (%s), length = (%d).\n", passwd, len);
! 572: fprintf(stderr, "received = (%d), length = (%d).\n", buff, n);
! 573: fflush(stderr);
! 574: }
! 575: }
! 576:
1.1 ohara 577: /*
1.5 ! ohara 578: (-reverse 版の ox_start)
1.1 ohara 579: ox_start は クライアントが呼び出すための関数である.
580: サーバでは使われない. prog1 は コントロールサーバであり,
581: -ox, -reverse, -data, -control, -pass, -host
582: というオプションを理解することを仮定する. prog2 は計算サーバである.
583: 接続時には, sv->control を先にオープンする.
584: */
585:
586: ox_file_t ox_start(char* host, char* prog1, char* prog2)
587: {
1.5 ! ohara 588: char *pass = create_otp();
1.1 ohara 589: char ctl[16], dat[16];
590: short portControl = 0; /* short であることに注意 */
591: short portStream = 0;
592: ox_file_t sv = malloc(sizeof(__ox_file_struct));
593:
594: sv->control = mysocketListen(host, &portControl);
595: sv->stream = mysocketListen(host, &portStream);
596: sprintf(ctl, "%d", portControl);
597: sprintf(dat, "%d", portStream);
598:
599: if (fork() == 0) {
600: dup2(2, 1);
601: dup2(open(DEFAULT_LOGFILE, O_RDWR|O_CREAT|O_TRUNC, 0644), 2);
602: execl(prog1, prog1, "-reverse", "-ox", prog2,
603: "-data", dat, "-control", ctl, "-pass", pass,
604: "-host", host, NULL);
605: }
606:
607: sv->control = mysocketAccept(sv->control);
1.5 ! ohara 608: login_with_otp(sv->control, pass);
! 609: decideByteOrderClient(sv->control, 0);
! 610: /* 10マイクロ秒, 時間稼ぎする. */
1.1 ohara 611: usleep(10);
612: sv->stream = mysocketAccept(sv->stream);
1.5 ! ohara 613: login_with_otp(sv->stream, pass);
! 614: decideByteOrderClient(sv->stream, 0);
1.1 ohara 615:
616: return sv;
617: }
618:
1.5 ! ohara 619: /*
! 620: (-insecure 版の ox_start) まだ、中身はありません。
! 621: ox_start_insecure_nonreverse は クライアントが呼び出すための関数である.
! 622: 接続時には, sv->control を先にオープンする.
! 623: 既定値:
! 624: portControl = 1200
! 625: portStream = 1300
! 626: */
! 627:
! 628: ox_file_t ox_start_insecure_nonreverse(char* host, short portControl, short portStream)
! 629: {
! 630: ox_file_t sv = malloc(sizeof(__ox_file_struct));
! 631:
! 632: sv->control = mysocketOpen(host, portControl);
! 633: /* 10マイクロ秒, 時間稼ぎする. */
! 634: usleep(10);
! 635: sv->stream = mysocketOpen(host, portStream);
! 636: return sv;
! 637: }
! 638:
1.1 ohara 639: void ox_reset(ox_file_t sv)
640: {
641: send_ox_command(sv->control, SM_control_reset_connection);
642:
643: receive_ox_tag(sv->control); /* OX_DATA */
644: receive_cmo(sv->control); /* (CMO_INT32, 0) */
645:
646: while(receive_ox_tag(sv->stream) != OX_SYNC_BALL) {
647: receive_cmo(sv->stream); /* skipping a message. */
648: }
649:
650: send_ox_tag(sv->stream, OX_SYNC_BALL);
651: #if DEBUG
652: fprintf(stderr, "I have reset an Open XM server.\n");
653: #endif
654: }
655:
1.2 ohara 656: /* 以下は bconv.c で必要とする関数群である. */
657:
658: /* cmolen 関数は cmo の(送信時の)バイト長を返す. */
659: /* cmolen_XXX 関数は cmo_XXX の tag を除いたバイト長を返す. */
660:
661: static int cmolen_cmo_null(cmo_null* c)
662: {
663: return 0;
664: }
1.1 ohara 665:
666: static int cmolen_cmo_int32(cmo_int32* c)
667: {
668: return sizeof(int);
669: }
670:
1.2 ohara 671: static int cmolen_cmo_string(cmo_string* c)
672: {
673: return sizeof(int)+strlen(c->s);
674: }
675:
676: static int cmolen_cmo_mathcap(cmo_mathcap* c)
677: {
678: return cmolen_cmo(c->ob);
679: }
680:
1.1 ohara 681: static int cmolen_cmo_list(cmo_list* c)
682: {
683: int size = sizeof(c->head);
684: cell* cp = c->head;
685:
686: while(cp != NULL) {
687: size += cmolen_cmo(cp->cmo);
688: cp = cp->next;
689: }
690: return size;
691: }
692:
1.2 ohara 693: static int cmolen_cmo_monomial32(cmo_monomial32* c)
1.1 ohara 694: {
1.2 ohara 695: int len = (c->length + 1)*sizeof(int);
696: return len + cmolen_cmo(c->coef);
1.1 ohara 697: }
698:
699: static int cmolen_cmo_zz(cmo_zz* c)
700: {
701: int len = abs(c->mpz->_mp_size);
702: return sizeof(len) + len*sizeof(int);
703: }
704:
1.3 ohara 705: static int cmolen_cmo_distributed_polynomial(cmo_distributed_polynomial* c)
706: {
707: return cmolen_cmo_list((cmo_list *)c) + cmolen_cmo(c->ringdef);
708: }
709:
1.1 ohara 710: /* CMO がバイトエンコードされた場合のバイト列の長さを求める */
711: int cmolen_cmo(cmo* c)
712: {
713: int size = sizeof(int);
714:
715: switch(c->tag) {
716: case CMO_NULL:
1.2 ohara 717: case CMO_ZERO:
718: case CMO_DMS_GENERIC:
1.1 ohara 719: size += cmolen_cmo_null(c);
720: break;
721: case CMO_INT32:
722: size += cmolen_cmo_int32((cmo_int32 *)c);
723: break;
724: case CMO_STRING:
725: size += cmolen_cmo_string((cmo_string *)c);
726: break;
1.2 ohara 727: case CMO_MATHCAP:
728: case CMO_RING_BY_NAME:
729: case CMO_ERROR2:
730: size += cmolen_cmo_mathcap((cmo_mathcap *)c);
731: break;
1.1 ohara 732: case CMO_LIST:
733: size += cmolen_cmo_list((cmo_list *)c);
734: break;
1.2 ohara 735: case CMO_MONOMIAL32:
736: size += cmolen_cmo_monomial32((cmo_monomial32 *)c);
737: break;
1.1 ohara 738: case CMO_ZZ:
739: size += cmolen_cmo_zz((cmo_zz *)c);
740: break;
1.3 ohara 741: case CMO_DISTRIBUTED_POLYNOMIAL:
742: size += cmolen_cmo_distributed_polynomial((cmo_distributed_polynomial *)c);
743: break;
1.1 ohara 744: default:
745: }
746: return size;
747: }
748:
1.4 ohara 749: static int d_ptr;
750: static char *d_buf;
751:
752: int init_dump_buffer(char *s)
1.2 ohara 753: {
1.4 ohara 754: d_buf = s;
755: d_ptr = 0;
1.2 ohara 756: }
757:
1.4 ohara 758: static int dump_cmo_null(cmo_null* m)
1.1 ohara 759: {
1.4 ohara 760: return 0;
1.1 ohara 761: }
762:
1.4 ohara 763: static int dump_cmo_int32(cmo_int32* m)
764: {
765: dump_integer(m->i);
766: }
767:
768: static int dump_cmo_string(cmo_string* m)
1.2 ohara 769: {
770: int len = strlen(m->s);
1.4 ohara 771: dump_integer(len);
772: dump_string(m->s, len);
1.2 ohara 773: }
774:
1.4 ohara 775: static int dump_cmo_mathcap(cmo_mathcap* c)
1.2 ohara 776: {
1.4 ohara 777: dump_cmo(c->ob);
1.2 ohara 778: }
779:
1.4 ohara 780: static int dump_cmo_list(cmo_list* m)
1.1 ohara 781: {
782: cell* cp = m->head;
783: int len = length_cmo_list(m);
1.4 ohara 784: dump_integer(len);
1.1 ohara 785:
786: while(cp != NULL) {
1.4 ohara 787: dump_cmo(cp->cmo);
1.1 ohara 788: cp = cp->next;
789: }
790: }
791:
1.4 ohara 792: static int dump_cmo_monomial32(cmo_monomial32* c)
1.1 ohara 793: {
1.2 ohara 794: int i;
795: int length = c->length;
1.4 ohara 796: dump_integer(c->length);
1.2 ohara 797: for(i=0; i<length; i++) {
1.4 ohara 798: dump_integer(c->exps[i]);
1.2 ohara 799: }
1.4 ohara 800: dump_cmo(c->coef);
1.1 ohara 801: }
802:
1.4 ohara 803: static int dump_cmo_zz(cmo_zz* c)
1.1 ohara 804: {
1.4 ohara 805: dump_mpz(c->mpz);
1.1 ohara 806: }
807:
1.4 ohara 808: static int dump_cmo_distributed_polynomial(cmo_distributed_polynomial* m)
1.3 ohara 809: {
810: cell* cp = m->head;
1.4 ohara 811: int len = length_cmo_list((cmo_list *)m);
812: dump_integer(len);
813: dump_cmo(m->ringdef);
1.3 ohara 814: while(cp != NULL) {
1.4 ohara 815: dump_cmo(cp->cmo);
1.3 ohara 816: cp = cp->next;
817: }
818: }
819:
1.1 ohara 820: /* タグを書き出してから、各関数を呼び出す */
1.4 ohara 821: int dump_cmo(cmo* m)
1.1 ohara 822: {
1.4 ohara 823: dump_integer(m->tag);
1.1 ohara 824: switch(m->tag) {
825: case CMO_NULL:
1.2 ohara 826: case CMO_ZERO:
827: case CMO_DMS_GENERIC:
1.4 ohara 828: dump_cmo_null(m);
829: break;
1.1 ohara 830: case CMO_INT32:
1.4 ohara 831: dump_cmo_int32((cmo_int32 *)m);
832: break;
1.1 ohara 833: case CMO_STRING:
1.4 ohara 834: dump_cmo_string((cmo_string *)m);
835: break;
1.2 ohara 836: case CMO_MATHCAP:
837: case CMO_RING_BY_NAME:
838: case CMO_ERROR2:
1.4 ohara 839: dump_cmo_mathcap((cmo_mathcap *)m);
840: break;
1.1 ohara 841: case CMO_LIST:
1.4 ohara 842: dump_cmo_list((cmo_list *)m);
843: break;
1.2 ohara 844: case CMO_MONOMIAL32:
1.4 ohara 845: dump_cmo_monomial32((cmo_monomial32 *)m);
846: break;
1.1 ohara 847: case CMO_ZZ:
1.4 ohara 848: dump_cmo_zz((cmo_zz *)m);
849: break;
1.3 ohara 850: case CMO_DISTRIBUTED_POLYNOMIAL:
1.4 ohara 851: dump_cmo_distributed_polynomial((cmo_distributed_polynomial *)m);
852: break;
1.1 ohara 853: default:
854: }
855: }
856:
1.4 ohara 857: static int dump_mpz(mpz_ptr mpz)
858: {
859: int i;
860: int len = abs(mpz->_mp_size);
861: dump_integer(mpz->_mp_size);
862: for(i=0; i<len; i++) {
863: dump_integer(mpz->_mp_d[i]);
864: }
865: return;
866: }
867:
868: static int dump_string(char *s, int len)
869: {
870: memcpy(&d_buf[d_ptr], s, len);
871: d_ptr += len;
872: }
873:
874: static int dump_integer(int x)
1.1 ohara 875: {
876: int nx = htonl(x);
1.4 ohara 877: dump_string((char *)&nx, sizeof(int));
1.1 ohara 878: }
879:
1.4 ohara 880: int dump_ox_data(ox_data* m)
1.1 ohara 881: {
1.4 ohara 882: dump_integer(OX_DATA);
883: dump_integer(-1);
884: dump_cmo(m->cmo);
1.1 ohara 885: }
886:
1.4 ohara 887: int dump_ox_command(ox_command* m)
888: {
889: dump_integer(OX_COMMAND);
890: dump_integer(-1);
891: dump_integer(m->command);
892: }
1.1 ohara 893:
894: int send_ox(ox_file_t s, ox *m)
895: {
896: int tag = m->tag;
897: int code;
898: if (tag == OX_DATA) {
899: send_ox_cmo(s->stream, ((ox_data *)m)->cmo);
900: }else if (tag == OX_COMMAND) {
901: code = ((ox_command *)m)->command;
902: if (code >= 1024) {
903: /* control command */
904: send_ox_command(s->control, code);
905: }else {
906: send_ox_command(s->stream, code);
907: }
908: }else {
909: /* CMO?? */
910: send_ox_cmo(s->stream, (cmo *)m);
911: }
912: }
913:
914: int send_ox_cmo(int fd, cmo* m)
915: {
916: send_ox_tag(fd, OX_DATA);
917: send_cmo(fd, m);
918: }
919:
1.2 ohara 920: /* send_cmo_xxx 関数群 */
921: static int send_cmo_null(int fd, cmo_null* c)
922: {
923: return 0;
924: }
1.1 ohara 925:
1.2 ohara 926: static int send_cmo_int32(int fd, cmo_int32* m)
1.1 ohara 927: {
1.2 ohara 928: send_int32(fd, m->i);
1.1 ohara 929: }
930:
1.2 ohara 931: static int send_cmo_string(int fd, cmo_string* m)
1.1 ohara 932: {
1.2 ohara 933: int len = strlen(m->s);
934: send_int32(fd, len);
935: if (len > 0) {
936: write(fd, m->s, len);
937: }
1.1 ohara 938: return 0;
939: }
940:
1.2 ohara 941: static int send_cmo_mathcap(int fd, cmo_mathcap* c)
1.1 ohara 942: {
1.2 ohara 943: send_cmo(fd, c->ob);
944: return 0;
1.1 ohara 945: }
946:
947: static int send_cmo_list(int fd, cmo_list* c)
948: {
949: cell* cp = c->head;
950: int len = length_cmo_list(c);
951: send_int32(fd, len);
952:
953: while(cp != NULL) {
954: send_cmo(fd, cp->cmo);
955: cp = cp->next;
956: }
957: return 0;
958: }
959:
1.2 ohara 960: static int send_cmo_zz(int fd, cmo_zz* c)
1.1 ohara 961: {
1.2 ohara 962: send_mpz(fd, c->mpz);
1.1 ohara 963: }
964:
1.2 ohara 965: static int send_cmo_error2(int fd, cmo_error2* c)
1.1 ohara 966: {
1.2 ohara 967: send_cmo(fd, c->ob);
1.1 ohara 968: return 0;
969: }
970:
971: /* CMOを送る. OX_tag は送信済*/
972: int send_cmo(int fd, cmo* c)
973: {
974: int tag = c->tag;
975:
976: send_int32(fd, tag);
977: switch(tag) {
978: case CMO_NULL:
979: send_cmo_null(fd, c); /* 空の関数 */
980: break;
981: case CMO_INT32:
982: send_cmo_int32(fd, (cmo_int32 *)c);
983: break;
984: case CMO_STRING:
985: send_cmo_string(fd, (cmo_string *)c);
986: break;
987: case CMO_MATHCAP:
988: send_cmo_mathcap(fd, (cmo_mathcap *)c);
989: break;
990: case CMO_LIST:
991: send_cmo_list(fd, (cmo_list *)c);
992: break;
993: case CMO_ZZ:
994: send_cmo_zz(fd, (cmo_zz *)c);
995: break;
996: case CMO_ERROR2:
1.2 ohara 997: send_cmo_error2(fd, (cmo_error2 *)c);
1.1 ohara 998: break;
999: default:
1000: }
1001: }
1002:
1003: static int send_mpz(int fd, mpz_ptr mpz)
1004: {
1005: int i;
1006: int len = abs(mpz->_mp_size);
1007: send_int32(fd, mpz->_mp_size);
1008: for(i=0; i<len; i++) {
1009: send_int32(fd, mpz->_mp_d[i]);
1010: }
1011: return 0;
1012: }
1013:
1014: ox_data* new_ox_data(cmo* c)
1015: {
1016: ox_data* m = malloc(sizeof(ox_data));
1017: m->tag = OX_DATA;
1018: m->cmo = c;
1019: return m;
1020: }
1021:
1022: ox_command* new_ox_command(int sm_code)
1023: {
1024: ox_command* m = malloc(sizeof(ox_command));
1025: m->tag = OX_COMMAND;
1026: m->command = sm_code;
1027: return m;
1028: }
1029:
1030: #define MAX_TYPES 8
1031: static int known_types[] = {
1032: -1, /* gate keeper */
1033: CMO_NULL,
1034: CMO_INT32,
1035: CMO_STRING,
1036: CMO_MATHCAP,
1037: CMO_LIST,
1038: CMO_ZZ,
1039: CMO_ERROR2,
1040: };
1041:
1.2 ohara 1042: #define ID_TEMP "(CMO_MATHCAP, (CMO_LIST, (CMO_LIST, (CMO_INT32, %d), (CMO_STRING, \"%s\"), (CMO_STRING, \"%s\"), (CMO_STRING, \"%s\")), (CMO_LIST, (CMO_INT32, 1), (CMO_INT32, 2), (CMO_INT32, 4), (CMO_INT32, 5), (CMO_INT32, 17), (CMO_INT32, 20), (CMO_INT32, 2130706434))))\n"
1.1 ohara 1043:
1.2 ohara 1044: cmo* make_mathcap_object2(int ver, char* ver_s, char* sysname)
1.1 ohara 1045: {
1.2 ohara 1046: cmo *cap;
1047: char buff[8192];
1.1 ohara 1048:
1049: setgetc(mygetc);
1.2 ohara 1050: sprintf(buff, ID_TEMP, ver, sysname, ver_s, getenv("HOSTTYPE"));
1051: setmode_mygetc(buff, 8192);
1052: cap = parse();
1.1 ohara 1053: resetgetc();
1054:
1.2 ohara 1055: return cap;
1.1 ohara 1056: }
1057:
1058: cmo* make_mathcap_object(int version, char* id_string)
1059: {
1060: cmo_list *li_ver, *li_cmo, *li;
1061:
1062: int i;
1063: li_ver = new_cmo_list();
1064: append_cmo_list(li_ver, (cmo *)new_cmo_int32(version));
1065: append_cmo_list(li_ver, (cmo *)new_cmo_string(id_string));
1066:
1067: li_cmo = new_cmo_list();
1068: for(i=0; i<MAX_TYPES; i++) {
1069: if (known_types[i] != -1) {
1070: append_cmo_list(li_cmo, (cmo *)new_cmo_int32(known_types[i]));
1071: }
1072: }
1073:
1074: li = new_cmo_list();
1075: append_cmo_list(li, (cmo *)li_ver);
1076: append_cmo_list(li, (cmo *)li_cmo);
1077:
1.2 ohara 1078: return (cmo *)new_cmo_mathcap((cmo *)li);
1.1 ohara 1079: }
1080:
1081: static int funcs(int cmo_type)
1082: {
1083: int i;
1084: for(i=0; i<MAX_TYPES; i++) {
1085: if (known_types[i] == cmo_type) {
1086: return i;
1087: }
1088: }
1089: return 0;
1090: }
1091:
1092: void setCmotypeDisable(int type)
1093: {
1094: int i = funcs(type);
1095: known_types[i] = -1;
1096: }
1097: #if 0
1098: cmo* (*received_funcs[])(int fd) = {
1099: NULL, /* gate keeper */
1100: receive_cmo_null,
1101: receive_cmo_int32,
1102: receive_cmo_string,
1103: receive_cmo_mathcap,
1104: receive_cmo_list,
1105: receive_cmo_zz,
1.2 ohara 1106: receive_cmo_error2
1.1 ohara 1107: };
1108:
1109: cmo* receive_cmo2(int fd)
1110: {
1111: int tag;
1112: cmo* (*foo)() = received_funcs[funcs(tag)];
1113: if (foo != NULL) {
1114: return foo(fd);
1115: }
1116: }
1.3 ohara 1117: #endif
1118:
1119: /* ファイルディスクリプタ fd の通信路での integer の byte order を決定する */
1120: /* 実際には order (0,1,or 0xFF)をみてはいない */
1.5 ! ohara 1121: int decideByteOrderClient(oxfd fd, int order)
1.1 ohara 1122: {
1.3 ohara 1123: char zero = OX_BYTE_NETWORK_BYTE_ORDER;
1.1 ohara 1124: char dest;
1.5 ! ohara 1125: read(fd, &dest, sizeof(char));
! 1126: write(fd, &zero, sizeof(char));
1.1 ohara 1127: return 0;
1128: }
1129:
1130: /* Server 側ではこちらを用いる */
1.5 ! ohara 1131: /* いまの実装は dup されていることが前提になっている */
! 1132: int decideByteOrderServer(oxfd fd, int order)
1.1 ohara 1133: {
1.3 ohara 1134: char zero = OX_BYTE_NETWORK_BYTE_ORDER;
1.1 ohara 1135: char dest;
1.5 ! ohara 1136: write(fd, &zero, sizeof(char));
! 1137: read(fd, &dest, sizeof(char));
1.1 ohara 1138: return 0;
1139: }
1140:
1.5 ! ohara 1141: /* cmo と string (ここではC言語のstring) の変換関数群 */
! 1142: char *convert_zz_to_string(cmo_zz *c)
1.1 ohara 1143: {
1144: return mpz_get_str(NULL, 10, c->mpz);
1145: }
1146:
1.5 ! ohara 1147: char *convert_cmo_to_string(cmo *m)
1.1 ohara 1148: {
1149: switch(m->tag) {
1150: case CMO_ZZ:
1.5 ! ohara 1151: return convert_zz_to_string((cmo_zz *)m);
1.1 ohara 1152: case CMO_INT32:
1.5 ! ohara 1153: return convert_int_to_string(((cmo_int32 *)m)->i);
1.1 ohara 1154: case CMO_STRING:
1155: return ((cmo_string *)m)->s;
1156: case CMO_NULL:
1.5 ! ohara 1157: return convert_null_to_string();
1.1 ohara 1158: default:
1159: fprintf(stderr, "sorry, not implemented CMO\n");
1160: /* まだ実装していません. */
1161: return NULL;
1162: }
1163: }
1164:
1.5 ! ohara 1165: char *convert_null_to_string()
1.1 ohara 1166: {
1167: static char* null_string = "";
1168: return null_string;
1169: }
1170:
1.5 ! ohara 1171: char *convert_int_to_string(int integer)
1.1 ohara 1172: {
1173: char buff[1024];
1174: char *s;
1175:
1176: sprintf(buff, "%d", integer);
1177: s = malloc(strlen(buff)+1);
1178: strcpy(s, buff);
1179:
1180: return s;
1181: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>