Annotation of OpenXM/src/ox_math/ox.c, Revision 1.6
1.1 ohara 1: /* -*- mode: C; coding: euc-japan -*- */
1.6 ! ohara 2: /* $OpenXM: OpenXM/src/ox_math/ox.c,v 1.5 1999/11/03 10:56:40 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: c->tag = CMO_STRING;
1.6 ! ohara 287: if (s != NULL) {
! 288: c->s = malloc(strlen(s)+1);
! 289: strcpy(c->s, s);
! 290: }else {
! 291: c->s = NULL;
! 292: }
1.2 ohara 293: return c;
294: }
295:
296: cmo_mathcap* new_cmo_mathcap(cmo* ob)
297: {
298: cmo_mathcap* c = malloc(sizeof(cmo_mathcap));
299: c->tag = CMO_MATHCAP;
300: c->ob = ob;
301: return c;
302: }
303:
1.1 ohara 304: cmo_list* new_cmo_list()
305: {
306: cmo_list* c = malloc(sizeof(cmo_list));
307: c->tag = CMO_LIST;
308: c->length = 0;
309: c->head = NULL;
310: return c;
311: }
312:
1.2 ohara 313: cmo_monomial32* new_cmo_monomial32()
1.1 ohara 314: {
1.2 ohara 315: cmo_monomial32* c = malloc(sizeof(cmo_monomial32));
316: c->tag = CMO_MONOMIAL32;
1.1 ohara 317: return c;
318: }
319:
1.2 ohara 320: cmo_monomial32* new_cmo_monomial32_size(int size)
1.1 ohara 321: {
1.2 ohara 322: cmo_monomial32* c = new_cmo_monomial32();
323: if (size>0) {
324: c->length = size;
325: c->exps = malloc(sizeof(int)*size);
326: }
1.1 ohara 327: return c;
328: }
329:
330: cmo_zz* new_cmo_zz()
331: {
332: cmo_zz* c = malloc(sizeof(cmo_zz));
333: c->tag = CMO_ZZ;
334: mpz_init(c->mpz);
335: return c;
336: }
337:
338: cmo_zz* new_cmo_zz_noinit()
339: {
340: cmo_zz* c = malloc(sizeof(cmo_zz));
341: c->tag = CMO_ZZ;
342: return c;
343: }
344:
345: cmo_zz* new_cmo_zz_set_si(int i)
346: {
347: cmo_zz* c = new_cmo_zz();
348: mpz_set_si(c->mpz, i);
349: return c;
350: }
351:
1.5 ohara 352: cmo_zz *new_cmo_zz_set_string(char *s)
353: {
354: cmo_zz* c = new_cmo_zz_noinit();
355: mpz_init_set_str(c->mpz, s, 10);
356: return c;
357: }
358:
1.1 ohara 359: cmo_zz* new_cmo_zz_size(int size)
360: {
361: cmo_zz* c = new_cmo_zz();
362: resize_mpz(c->mpz, size);
363: return c;
364: }
365:
1.2 ohara 366: cmo_zero* new_cmo_zero()
367: {
368: cmo_zero* m = malloc(sizeof(cmo_zero));
369: m->tag = CMO_ZERO;
370: return m;
371: }
372:
373: cmo_dms_generic* new_cmo_dms_generic()
374: {
375: cmo_dms_generic* m = malloc(sizeof(cmo_dms_generic));
376: m->tag = CMO_DMS_GENERIC;
377: return m;
378: }
379:
380: cmo_ring_by_name* new_cmo_ring_by_name(cmo* ob)
381: {
382: cmo_ring_by_name* c = malloc(sizeof(cmo_ring_by_name));
383: c->tag = CMO_RING_BY_NAME;
384: c->ob = ob;
385: return c;
386: }
387:
388: cmo_indeterminate* new_cmo_indeterminate(cmo* ob)
389: {
390: cmo_indeterminate* c = malloc(sizeof(cmo_indeterminate));
391: c->tag = CMO_INDETERMINATE;
392: c->ob = ob;
393: return c;
394: }
395:
1.3 ohara 396: cmo_distributed_polynomial* new_cmo_distributed_polynomial()
397: {
398: cmo_distributed_polynomial* c = malloc(sizeof(cmo_distributed_polynomial));
399: c->tag = CMO_DISTRIBUTED_POLYNOMIAL;
400: c->length = 0;
401: c->head = NULL;
402: c->ringdef = NULL;
403: return c;
404: }
405:
1.2 ohara 406: cmo_error2* new_cmo_error2(cmo* ob)
407: {
408: cmo_error2* c = malloc(sizeof(cmo_error2));
409: c->tag = CMO_ERROR2;
410: c->ob = ob;
411: return c;
412: }
413:
1.1 ohara 414: /* receive_ox_tag() == OX_DATA の後に呼び出される */
415: /* 関数ポインタを使った方がきれいに書けるような気がする. */
416: /* if (foo[tag] != NULL) foo[tag](fd); とか */
417:
418: cmo* receive_cmo(int fd)
419: {
420: cmo* m;
421: int tag;
422: tag = receive_int32(fd);
423:
424: switch(tag) {
425: case CMO_NULL:
426: m = receive_cmo_null(fd);
427: break;
428: case CMO_INT32:
429: m = (cmo *)receive_cmo_int32(fd);
430: break;
431: case CMO_STRING:
432: m = (cmo *)receive_cmo_string(fd);
433: break;
1.2 ohara 434: case CMO_MATHCAP:
435: m = (cmo *)receive_cmo_mathcap(fd);
1.1 ohara 436: break;
437: case CMO_LIST:
438: m = (cmo *)receive_cmo_list(fd);
439: break;
1.2 ohara 440: case CMO_ZZ:
441: m = (cmo *)receive_cmo_zz(fd);
1.1 ohara 442: break;
443: case CMO_ERROR2:
1.2 ohara 444: m = (cmo *)receive_cmo_error2(fd);
1.1 ohara 445: break;
446: case CMO_DATUM:
447: case CMO_QQ:
448: case CMO_ZERO:
449: case CMO_DMS:
450: default:
451: fprintf(stderr, "unknown cmo-type: tag = (%d)\n", m->tag);
452: }
453: return m;
454: }
455:
456: void send_ox_command(int fd, int sm_command)
457: {
458: send_ox_tag(fd, OX_COMMAND);
459: send_int32(fd, sm_command);
460: }
461:
462: int print_cmo(cmo* c)
463: {
464: int tag = c->tag;
465: fprintf(stderr, "local::tag = (%d): ", tag);
466: switch(tag) {
467: case CMO_LIST:
468: print_cmo_list((cmo_list *)c);
469: break;
470: case CMO_INT32:
471: print_cmo_int32((cmo_int32 *)c);
472: break;
473: case CMO_MATHCAP:
474: print_cmo_mathcap((cmo_mathcap *)c);
475: break;
476: case CMO_STRING:
477: print_cmo_string((cmo_string *)c);
478: break;
479: case CMO_NULL:
480: fprintf(stderr, "\n");
481: break;
482: default:
483: fprintf(stderr, "print_cmo() does not know how to print.\n");
484: }
485: }
486:
487: int print_cmo_int32(cmo_int32* c)
488: {
489: fprintf(stderr, "cmo_int32 = (%d)\n", c->i);
490: }
491:
492: int print_cmo_list(cmo_list* li)
493: {
494: cell* cp = li->head;
495: fprintf(stderr, "length = (%d)\nlist:\n", li->length);
496: while(cp != NULL) {
497: print_cmo(cp->cmo);
498: cp=cp->next;
499: }
500: fprintf(stderr, "end of list\n");
501: }
502:
503: int print_cmo_mathcap(cmo_mathcap* c)
504: {
505: fprintf(stderr, "\n");
1.2 ohara 506: print_cmo(c->ob);
1.1 ohara 507: }
508:
509: int print_cmo_string(cmo_string* c)
510: {
511: fprintf(stderr, "cmo_string = (%s)\n", c->s);
512: }
513:
514: void ox_close(ox_file_t sv)
515: {
516: send_ox_command(sv->control, SM_control_kill);
517: #if DEBUG
518: sleep(2); /* OpenXM server の終了を待つ. あまり意味はない. */
519: fprintf(stderr, "I have closed an Open XM server.\n");
520: #endif
521: }
522:
523: void ox_executeStringByLocalParser(ox_file_t sv, char* s)
524: {
525: /* 文字列ををスタックにプッシュ. */
526: send_ox_cmo(sv->stream, (cmo *)new_cmo_string(s));
527:
528: /* サーバに実行させる. */
529: send_ox_command(sv->stream, SM_executeStringByLocalParser);
530: }
531:
532: /* ox_mathcap() をコールする. */
533: cmo_mathcap* ox_mathcap(ox_file_t sv)
534: {
535: send_ox_command(sv->stream, SM_mathcap);
536: send_ox_command(sv->stream, SM_popCMO);
537: receive_ox_tag(sv->stream); /* OX_DATA */
538: return (cmo_mathcap *)receive_cmo(sv->stream);
539: }
540:
541: char* ox_popString(ox_file_t sv, int fd)
542: {
543: cmo_string* m = NULL;
544:
545: send_ox_command(fd, SM_popString);
546: receive_ox_tag(fd); /* OX_DATA */
547: m = (cmo_string *)receive_cmo(fd);
548: return m->s;
549: }
550:
551: cmo* ox_pop_cmo(ox_file_t sv, int fd)
552: {
553: send_ox_command(fd, SM_popCMO);
554: receive_ox_tag(fd); /* OX_DATA */
555: return receive_cmo(fd);
556: }
557:
1.5 ohara 558: /* 手抜き. (後で改善しよう...) */
559: static char *create_otp()
560: {
561: static char otp[] = "otpasswd";
562: return otp;
563: }
564:
565: /* OneTimePassword の処理 */
566: static int login_with_otp(int fd, char* passwd)
567: {
568: char buff[1024];
569: int n = read(fd, buff, 1024);
570: int len = strlen(passwd)+1;
571: if (n != len) {
572: fprintf(stderr, "Socket#%d: Login incorrect.\n", fd);
573: fprintf(stderr, "password = (%s), length = (%d).\n", passwd, len);
574: fprintf(stderr, "received = (%d), length = (%d).\n", buff, n);
575: fflush(stderr);
576: }
577: }
578:
1.1 ohara 579: /*
1.5 ohara 580: (-reverse 版の ox_start)
1.1 ohara 581: ox_start は クライアントが呼び出すための関数である.
582: サーバでは使われない. prog1 は コントロールサーバであり,
583: -ox, -reverse, -data, -control, -pass, -host
584: というオプションを理解することを仮定する. prog2 は計算サーバである.
585: 接続時には, sv->control を先にオープンする.
586: */
587:
588: ox_file_t ox_start(char* host, char* prog1, char* prog2)
589: {
1.5 ohara 590: char *pass = create_otp();
1.1 ohara 591: char ctl[16], dat[16];
592: short portControl = 0; /* short であることに注意 */
593: short portStream = 0;
594: ox_file_t sv = malloc(sizeof(__ox_file_struct));
595:
596: sv->control = mysocketListen(host, &portControl);
597: sv->stream = mysocketListen(host, &portStream);
598: sprintf(ctl, "%d", portControl);
599: sprintf(dat, "%d", portStream);
600:
601: if (fork() == 0) {
602: dup2(2, 1);
603: dup2(open(DEFAULT_LOGFILE, O_RDWR|O_CREAT|O_TRUNC, 0644), 2);
604: execl(prog1, prog1, "-reverse", "-ox", prog2,
605: "-data", dat, "-control", ctl, "-pass", pass,
606: "-host", host, NULL);
607: }
608:
609: sv->control = mysocketAccept(sv->control);
1.5 ohara 610: login_with_otp(sv->control, pass);
611: decideByteOrderClient(sv->control, 0);
612: /* 10マイクロ秒, 時間稼ぎする. */
1.1 ohara 613: usleep(10);
614: sv->stream = mysocketAccept(sv->stream);
1.5 ohara 615: login_with_otp(sv->stream, pass);
616: decideByteOrderClient(sv->stream, 0);
1.1 ohara 617:
618: return sv;
619: }
620:
1.5 ohara 621: /*
622: (-insecure 版の ox_start) まだ、中身はありません。
623: ox_start_insecure_nonreverse は クライアントが呼び出すための関数である.
624: 接続時には, sv->control を先にオープンする.
625: 既定値:
626: portControl = 1200
627: portStream = 1300
628: */
629:
630: ox_file_t ox_start_insecure_nonreverse(char* host, short portControl, short portStream)
631: {
632: ox_file_t sv = malloc(sizeof(__ox_file_struct));
633:
634: sv->control = mysocketOpen(host, portControl);
635: /* 10マイクロ秒, 時間稼ぎする. */
636: usleep(10);
637: sv->stream = mysocketOpen(host, portStream);
638: return sv;
639: }
640:
1.1 ohara 641: void ox_reset(ox_file_t sv)
642: {
643: send_ox_command(sv->control, SM_control_reset_connection);
644:
645: receive_ox_tag(sv->control); /* OX_DATA */
646: receive_cmo(sv->control); /* (CMO_INT32, 0) */
647:
648: while(receive_ox_tag(sv->stream) != OX_SYNC_BALL) {
649: receive_cmo(sv->stream); /* skipping a message. */
650: }
651:
652: send_ox_tag(sv->stream, OX_SYNC_BALL);
653: #if DEBUG
654: fprintf(stderr, "I have reset an Open XM server.\n");
655: #endif
656: }
657:
1.2 ohara 658: /* 以下は bconv.c で必要とする関数群である. */
659:
660: /* cmolen 関数は cmo の(送信時の)バイト長を返す. */
661: /* cmolen_XXX 関数は cmo_XXX の tag を除いたバイト長を返す. */
662:
663: static int cmolen_cmo_null(cmo_null* c)
664: {
665: return 0;
666: }
1.1 ohara 667:
668: static int cmolen_cmo_int32(cmo_int32* c)
669: {
670: return sizeof(int);
671: }
672:
1.2 ohara 673: static int cmolen_cmo_string(cmo_string* c)
674: {
675: return sizeof(int)+strlen(c->s);
676: }
677:
678: static int cmolen_cmo_mathcap(cmo_mathcap* c)
679: {
680: return cmolen_cmo(c->ob);
681: }
682:
1.1 ohara 683: static int cmolen_cmo_list(cmo_list* c)
684: {
685: int size = sizeof(c->head);
686: cell* cp = c->head;
687:
688: while(cp != NULL) {
689: size += cmolen_cmo(cp->cmo);
690: cp = cp->next;
691: }
692: return size;
693: }
694:
1.2 ohara 695: static int cmolen_cmo_monomial32(cmo_monomial32* c)
1.1 ohara 696: {
1.2 ohara 697: int len = (c->length + 1)*sizeof(int);
698: return len + cmolen_cmo(c->coef);
1.1 ohara 699: }
700:
701: static int cmolen_cmo_zz(cmo_zz* c)
702: {
703: int len = abs(c->mpz->_mp_size);
704: return sizeof(len) + len*sizeof(int);
705: }
706:
1.3 ohara 707: static int cmolen_cmo_distributed_polynomial(cmo_distributed_polynomial* c)
708: {
709: return cmolen_cmo_list((cmo_list *)c) + cmolen_cmo(c->ringdef);
710: }
711:
1.1 ohara 712: /* CMO がバイトエンコードされた場合のバイト列の長さを求める */
713: int cmolen_cmo(cmo* c)
714: {
715: int size = sizeof(int);
716:
717: switch(c->tag) {
718: case CMO_NULL:
1.2 ohara 719: case CMO_ZERO:
720: case CMO_DMS_GENERIC:
1.1 ohara 721: size += cmolen_cmo_null(c);
722: break;
723: case CMO_INT32:
724: size += cmolen_cmo_int32((cmo_int32 *)c);
725: break;
726: case CMO_STRING:
727: size += cmolen_cmo_string((cmo_string *)c);
728: break;
1.2 ohara 729: case CMO_MATHCAP:
730: case CMO_RING_BY_NAME:
731: case CMO_ERROR2:
732: size += cmolen_cmo_mathcap((cmo_mathcap *)c);
733: break;
1.1 ohara 734: case CMO_LIST:
735: size += cmolen_cmo_list((cmo_list *)c);
736: break;
1.2 ohara 737: case CMO_MONOMIAL32:
738: size += cmolen_cmo_monomial32((cmo_monomial32 *)c);
739: break;
1.1 ohara 740: case CMO_ZZ:
741: size += cmolen_cmo_zz((cmo_zz *)c);
742: break;
1.3 ohara 743: case CMO_DISTRIBUTED_POLYNOMIAL:
744: size += cmolen_cmo_distributed_polynomial((cmo_distributed_polynomial *)c);
745: break;
1.1 ohara 746: default:
747: }
748: return size;
749: }
750:
1.4 ohara 751: static int d_ptr;
752: static char *d_buf;
753:
754: int init_dump_buffer(char *s)
1.2 ohara 755: {
1.4 ohara 756: d_buf = s;
757: d_ptr = 0;
1.2 ohara 758: }
759:
1.4 ohara 760: static int dump_cmo_null(cmo_null* m)
1.1 ohara 761: {
1.4 ohara 762: return 0;
1.1 ohara 763: }
764:
1.4 ohara 765: static int dump_cmo_int32(cmo_int32* m)
766: {
767: dump_integer(m->i);
768: }
769:
770: static int dump_cmo_string(cmo_string* m)
1.2 ohara 771: {
772: int len = strlen(m->s);
1.4 ohara 773: dump_integer(len);
774: dump_string(m->s, len);
1.2 ohara 775: }
776:
1.4 ohara 777: static int dump_cmo_mathcap(cmo_mathcap* c)
1.2 ohara 778: {
1.4 ohara 779: dump_cmo(c->ob);
1.2 ohara 780: }
781:
1.4 ohara 782: static int dump_cmo_list(cmo_list* m)
1.1 ohara 783: {
784: cell* cp = m->head;
785: int len = length_cmo_list(m);
1.4 ohara 786: dump_integer(len);
1.1 ohara 787:
788: while(cp != NULL) {
1.4 ohara 789: dump_cmo(cp->cmo);
1.1 ohara 790: cp = cp->next;
791: }
792: }
793:
1.4 ohara 794: static int dump_cmo_monomial32(cmo_monomial32* c)
1.1 ohara 795: {
1.2 ohara 796: int i;
797: int length = c->length;
1.4 ohara 798: dump_integer(c->length);
1.2 ohara 799: for(i=0; i<length; i++) {
1.4 ohara 800: dump_integer(c->exps[i]);
1.2 ohara 801: }
1.4 ohara 802: dump_cmo(c->coef);
1.1 ohara 803: }
804:
1.4 ohara 805: static int dump_cmo_zz(cmo_zz* c)
1.1 ohara 806: {
1.4 ohara 807: dump_mpz(c->mpz);
1.1 ohara 808: }
809:
1.4 ohara 810: static int dump_cmo_distributed_polynomial(cmo_distributed_polynomial* m)
1.3 ohara 811: {
812: cell* cp = m->head;
1.4 ohara 813: int len = length_cmo_list((cmo_list *)m);
814: dump_integer(len);
815: dump_cmo(m->ringdef);
1.3 ohara 816: while(cp != NULL) {
1.4 ohara 817: dump_cmo(cp->cmo);
1.3 ohara 818: cp = cp->next;
819: }
820: }
821:
1.1 ohara 822: /* タグを書き出してから、各関数を呼び出す */
1.4 ohara 823: int dump_cmo(cmo* m)
1.1 ohara 824: {
1.4 ohara 825: dump_integer(m->tag);
1.1 ohara 826: switch(m->tag) {
827: case CMO_NULL:
1.2 ohara 828: case CMO_ZERO:
829: case CMO_DMS_GENERIC:
1.4 ohara 830: dump_cmo_null(m);
831: break;
1.1 ohara 832: case CMO_INT32:
1.4 ohara 833: dump_cmo_int32((cmo_int32 *)m);
834: break;
1.1 ohara 835: case CMO_STRING:
1.4 ohara 836: dump_cmo_string((cmo_string *)m);
837: break;
1.2 ohara 838: case CMO_MATHCAP:
839: case CMO_RING_BY_NAME:
840: case CMO_ERROR2:
1.4 ohara 841: dump_cmo_mathcap((cmo_mathcap *)m);
842: break;
1.1 ohara 843: case CMO_LIST:
1.4 ohara 844: dump_cmo_list((cmo_list *)m);
845: break;
1.2 ohara 846: case CMO_MONOMIAL32:
1.4 ohara 847: dump_cmo_monomial32((cmo_monomial32 *)m);
848: break;
1.1 ohara 849: case CMO_ZZ:
1.4 ohara 850: dump_cmo_zz((cmo_zz *)m);
851: break;
1.3 ohara 852: case CMO_DISTRIBUTED_POLYNOMIAL:
1.4 ohara 853: dump_cmo_distributed_polynomial((cmo_distributed_polynomial *)m);
854: break;
1.1 ohara 855: default:
856: }
857: }
858:
1.4 ohara 859: static int dump_mpz(mpz_ptr mpz)
860: {
861: int i;
862: int len = abs(mpz->_mp_size);
863: dump_integer(mpz->_mp_size);
864: for(i=0; i<len; i++) {
865: dump_integer(mpz->_mp_d[i]);
866: }
867: return;
868: }
869:
870: static int dump_string(char *s, int len)
871: {
872: memcpy(&d_buf[d_ptr], s, len);
873: d_ptr += len;
874: }
875:
876: static int dump_integer(int x)
1.1 ohara 877: {
878: int nx = htonl(x);
1.4 ohara 879: dump_string((char *)&nx, sizeof(int));
1.1 ohara 880: }
881:
1.4 ohara 882: int dump_ox_data(ox_data* m)
1.1 ohara 883: {
1.4 ohara 884: dump_integer(OX_DATA);
885: dump_integer(-1);
886: dump_cmo(m->cmo);
1.1 ohara 887: }
888:
1.4 ohara 889: int dump_ox_command(ox_command* m)
890: {
891: dump_integer(OX_COMMAND);
892: dump_integer(-1);
893: dump_integer(m->command);
894: }
1.1 ohara 895:
896: int send_ox(ox_file_t s, ox *m)
897: {
898: int tag = m->tag;
899: int code;
900: if (tag == OX_DATA) {
901: send_ox_cmo(s->stream, ((ox_data *)m)->cmo);
902: }else if (tag == OX_COMMAND) {
903: code = ((ox_command *)m)->command;
904: if (code >= 1024) {
905: /* control command */
906: send_ox_command(s->control, code);
907: }else {
908: send_ox_command(s->stream, code);
909: }
910: }else {
911: /* CMO?? */
912: send_ox_cmo(s->stream, (cmo *)m);
913: }
914: }
915:
916: int send_ox_cmo(int fd, cmo* m)
917: {
918: send_ox_tag(fd, OX_DATA);
919: send_cmo(fd, m);
920: }
921:
1.2 ohara 922: /* send_cmo_xxx 関数群 */
923: static int send_cmo_null(int fd, cmo_null* c)
924: {
925: return 0;
926: }
1.1 ohara 927:
1.2 ohara 928: static int send_cmo_int32(int fd, cmo_int32* m)
1.1 ohara 929: {
1.2 ohara 930: send_int32(fd, m->i);
1.1 ohara 931: }
932:
1.2 ohara 933: static int send_cmo_string(int fd, cmo_string* m)
1.1 ohara 934: {
1.6 ! ohara 935: int len = (m->s != NULL)? strlen(m->s): 0;
1.2 ohara 936: send_int32(fd, len);
937: if (len > 0) {
938: write(fd, m->s, len);
939: }
1.1 ohara 940: return 0;
941: }
942:
1.2 ohara 943: static int send_cmo_mathcap(int fd, cmo_mathcap* c)
1.1 ohara 944: {
1.2 ohara 945: send_cmo(fd, c->ob);
946: return 0;
1.1 ohara 947: }
948:
949: static int send_cmo_list(int fd, cmo_list* c)
950: {
951: cell* cp = c->head;
952: int len = length_cmo_list(c);
953: send_int32(fd, len);
954:
955: while(cp != NULL) {
956: send_cmo(fd, cp->cmo);
957: cp = cp->next;
958: }
959: return 0;
960: }
961:
1.2 ohara 962: static int send_cmo_zz(int fd, cmo_zz* c)
1.1 ohara 963: {
1.2 ohara 964: send_mpz(fd, c->mpz);
1.1 ohara 965: }
966:
1.2 ohara 967: static int send_cmo_error2(int fd, cmo_error2* c)
1.1 ohara 968: {
1.2 ohara 969: send_cmo(fd, c->ob);
1.1 ohara 970: return 0;
971: }
972:
973: /* CMOを送る. OX_tag は送信済*/
974: int send_cmo(int fd, cmo* c)
975: {
976: int tag = c->tag;
977:
978: send_int32(fd, tag);
979: switch(tag) {
980: case CMO_NULL:
981: send_cmo_null(fd, c); /* 空の関数 */
982: break;
983: case CMO_INT32:
984: send_cmo_int32(fd, (cmo_int32 *)c);
985: break;
986: case CMO_STRING:
987: send_cmo_string(fd, (cmo_string *)c);
988: break;
989: case CMO_MATHCAP:
990: send_cmo_mathcap(fd, (cmo_mathcap *)c);
991: break;
992: case CMO_LIST:
993: send_cmo_list(fd, (cmo_list *)c);
994: break;
995: case CMO_ZZ:
996: send_cmo_zz(fd, (cmo_zz *)c);
997: break;
998: case CMO_ERROR2:
1.2 ohara 999: send_cmo_error2(fd, (cmo_error2 *)c);
1.1 ohara 1000: break;
1001: default:
1002: }
1003: }
1004:
1005: static int send_mpz(int fd, mpz_ptr mpz)
1006: {
1007: int i;
1008: int len = abs(mpz->_mp_size);
1009: send_int32(fd, mpz->_mp_size);
1010: for(i=0; i<len; i++) {
1011: send_int32(fd, mpz->_mp_d[i]);
1012: }
1013: return 0;
1014: }
1015:
1016: ox_data* new_ox_data(cmo* c)
1017: {
1018: ox_data* m = malloc(sizeof(ox_data));
1019: m->tag = OX_DATA;
1020: m->cmo = c;
1021: return m;
1022: }
1023:
1024: ox_command* new_ox_command(int sm_code)
1025: {
1026: ox_command* m = malloc(sizeof(ox_command));
1027: m->tag = OX_COMMAND;
1028: m->command = sm_code;
1029: return m;
1030: }
1031:
1032: #define MAX_TYPES 8
1033: static int known_types[] = {
1034: -1, /* gate keeper */
1035: CMO_NULL,
1036: CMO_INT32,
1037: CMO_STRING,
1038: CMO_MATHCAP,
1039: CMO_LIST,
1040: CMO_ZZ,
1041: CMO_ERROR2,
1042: };
1043:
1.2 ohara 1044: #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 1045:
1.2 ohara 1046: cmo* make_mathcap_object2(int ver, char* ver_s, char* sysname)
1.1 ohara 1047: {
1.2 ohara 1048: cmo *cap;
1049: char buff[8192];
1.1 ohara 1050:
1051: setgetc(mygetc);
1.2 ohara 1052: sprintf(buff, ID_TEMP, ver, sysname, ver_s, getenv("HOSTTYPE"));
1053: setmode_mygetc(buff, 8192);
1054: cap = parse();
1.1 ohara 1055: resetgetc();
1056:
1.2 ohara 1057: return cap;
1.1 ohara 1058: }
1059:
1060: cmo* make_mathcap_object(int version, char* id_string)
1061: {
1062: cmo_list *li_ver, *li_cmo, *li;
1063:
1064: int i;
1065: li_ver = new_cmo_list();
1066: append_cmo_list(li_ver, (cmo *)new_cmo_int32(version));
1067: append_cmo_list(li_ver, (cmo *)new_cmo_string(id_string));
1068:
1069: li_cmo = new_cmo_list();
1070: for(i=0; i<MAX_TYPES; i++) {
1071: if (known_types[i] != -1) {
1072: append_cmo_list(li_cmo, (cmo *)new_cmo_int32(known_types[i]));
1073: }
1074: }
1075:
1076: li = new_cmo_list();
1077: append_cmo_list(li, (cmo *)li_ver);
1078: append_cmo_list(li, (cmo *)li_cmo);
1079:
1.2 ohara 1080: return (cmo *)new_cmo_mathcap((cmo *)li);
1.1 ohara 1081: }
1082:
1083: static int funcs(int cmo_type)
1084: {
1085: int i;
1086: for(i=0; i<MAX_TYPES; i++) {
1087: if (known_types[i] == cmo_type) {
1088: return i;
1089: }
1090: }
1091: return 0;
1092: }
1093:
1094: void setCmotypeDisable(int type)
1095: {
1096: int i = funcs(type);
1097: known_types[i] = -1;
1098: }
1099: #if 0
1100: cmo* (*received_funcs[])(int fd) = {
1101: NULL, /* gate keeper */
1102: receive_cmo_null,
1103: receive_cmo_int32,
1104: receive_cmo_string,
1105: receive_cmo_mathcap,
1106: receive_cmo_list,
1107: receive_cmo_zz,
1.2 ohara 1108: receive_cmo_error2
1.1 ohara 1109: };
1110:
1111: cmo* receive_cmo2(int fd)
1112: {
1113: int tag;
1114: cmo* (*foo)() = received_funcs[funcs(tag)];
1115: if (foo != NULL) {
1116: return foo(fd);
1117: }
1118: }
1.3 ohara 1119: #endif
1120:
1121: /* ファイルディスクリプタ fd の通信路での integer の byte order を決定する */
1122: /* 実際には order (0,1,or 0xFF)をみてはいない */
1.5 ohara 1123: int decideByteOrderClient(oxfd fd, int order)
1.1 ohara 1124: {
1.3 ohara 1125: char zero = OX_BYTE_NETWORK_BYTE_ORDER;
1.1 ohara 1126: char dest;
1.5 ohara 1127: read(fd, &dest, sizeof(char));
1128: write(fd, &zero, sizeof(char));
1.1 ohara 1129: return 0;
1130: }
1131:
1132: /* Server 側ではこちらを用いる */
1.5 ohara 1133: /* いまの実装は dup されていることが前提になっている */
1134: int decideByteOrderServer(oxfd fd, int order)
1.1 ohara 1135: {
1.3 ohara 1136: char zero = OX_BYTE_NETWORK_BYTE_ORDER;
1.1 ohara 1137: char dest;
1.5 ohara 1138: write(fd, &zero, sizeof(char));
1139: read(fd, &dest, sizeof(char));
1.1 ohara 1140: return 0;
1141: }
1142:
1.5 ohara 1143: /* cmo と string (ここではC言語のstring) の変換関数群 */
1144: char *convert_zz_to_string(cmo_zz *c)
1.1 ohara 1145: {
1146: return mpz_get_str(NULL, 10, c->mpz);
1147: }
1148:
1.5 ohara 1149: char *convert_cmo_to_string(cmo *m)
1.1 ohara 1150: {
1151: switch(m->tag) {
1152: case CMO_ZZ:
1.5 ohara 1153: return convert_zz_to_string((cmo_zz *)m);
1.1 ohara 1154: case CMO_INT32:
1.5 ohara 1155: return convert_int_to_string(((cmo_int32 *)m)->i);
1.1 ohara 1156: case CMO_STRING:
1157: return ((cmo_string *)m)->s;
1158: case CMO_NULL:
1.5 ohara 1159: return convert_null_to_string();
1.1 ohara 1160: default:
1161: fprintf(stderr, "sorry, not implemented CMO\n");
1162: /* まだ実装していません. */
1163: return NULL;
1164: }
1165: }
1166:
1.5 ohara 1167: char *convert_null_to_string()
1.1 ohara 1168: {
1169: static char* null_string = "";
1170: return null_string;
1171: }
1172:
1.5 ohara 1173: char *convert_int_to_string(int integer)
1.1 ohara 1174: {
1175: char buff[1024];
1176: char *s;
1177:
1178: sprintf(buff, "%d", integer);
1179: s = malloc(strlen(buff)+1);
1180: strcpy(s, buff);
1181:
1182: return s;
1183: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>