Annotation of OpenXM_contrib2/asir2000/io/io.c, Revision 1.8
1.4 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.5 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.4 noro 27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
1.8 ! noro 47: * $OpenXM: OpenXM_contrib2/asir2000/io/io.c,v 1.7 2000/08/25 08:06:19 noro Exp $
1.4 noro 48: */
1.1 noro 49: #include <stdio.h>
50: #include "ca.h"
51: #if defined(VISUAL) || MPI
52: #include "wsio.h"
53: #endif
54:
1.3 noro 55: extern int little_endian,lib_ox_need_conv;
1.1 noro 56: extern int ox_do_copy, ox_do_count, ox_count_length, ox_file_io, ox_need_conv;
57: extern char *ox_copy_bptr;
58:
59: void reset_io()
60: {
61: ox_file_io = 0;
62: }
63:
64: void endian_init()
65: {
66: unsigned int et = 0xff;
67:
68: if ( *((char *)&et) )
69: little_endian = 1;
70: else
71: little_endian = 0;
72: ox_need_conv = 1;
73: }
74:
75: int countobj(p)
76: Obj p;
77: {
78: ox_count_length = 0;
79: ox_do_count = 1; saveobj(0,p); ox_do_count = 0;
80: return ox_count_length;
81: }
82:
1.2 noro 83: int count_as_cmo(p)
84: Obj p;
85: {
86: ox_count_length = 0;
87: ox_do_count = 1; write_cmo(0,p); ox_do_count = 0;
88: return ox_count_length;
89: }
90:
1.1 noro 91: int countvl(vl)
92: VL vl;
93: {
94: ox_count_length = 0;
95: ox_do_count = 1; savevl(0,vl); ox_do_count = 0;
96: return ox_count_length;
97: }
98:
99: void ox_copy_init(s)
100: char *s;
101: {
102: ox_copy_bptr = s;
103: }
104:
1.8 ! noro 105: #if !defined(VISUAL)
1.3 noro 106: /*
107: * library mode functions
108: * byte order is controlled by lib_ox_need_conv.
109: */
1.1 noro 110:
1.2 noro 111: void ox_obj_to_buf_as_cmo(p)
112: Obj p;
113: {
1.3 noro 114: ox_need_conv = lib_ox_need_conv;
1.2 noro 115: ox_do_copy = 1; write_cmo(0,p); ox_do_copy = 0;
116: }
117:
118: void ox_buf_to_obj_as_cmo(p)
119: Obj *p;
120: {
1.3 noro 121: ox_need_conv = lib_ox_need_conv;
1.2 noro 122: ox_do_copy = 1; read_cmo(0,p); ox_do_copy = 0;
123: }
124:
1.1 noro 125: void ox_vl_to_buf(vl)
126: VL vl;
127: {
128: ox_do_copy = 1; savevl(0,vl); ox_do_copy = 0;
129: }
1.8 ! noro 130: #endif
1.1 noro 131:
132: int gen_fread (ptr,size,nitems,stream)
133: char *ptr;
134: int size,nitems;
135: FILE *stream;
136: {
137: int n;
138:
139: if ( ox_do_copy ) {
140: n = size*nitems;
141: memcpy(ptr,ox_copy_bptr,n);
142: ox_copy_bptr += n;
143: } else {
144: #if defined(VISUAL)
145: if ( _fileno(stream) < 0 )
146: n = cread(ptr,size,nitems,(STREAM *)stream);
147: else
148: #elif MPI
149: if ( (char)fileno(stream) < 0 )
150: n = cread(ptr,size,nitems,(STREAM *)stream);
151: else
152: #endif
153: n = fread(ptr,size,nitems,stream);
154: if ( !n )
155: ExitAsir();
156: else
157: return n;
158: }
159: }
160:
161: int gen_fwrite (ptr,size,nitems,stream)
162: char *ptr;
163: int size,nitems;
164: FILE *stream;
165: {
166: int n;
167:
168: if ( ox_do_count )
169: ox_count_length += size*nitems;
170: else if ( ox_do_copy ) {
171: n = size*nitems;
172: memcpy(ox_copy_bptr,ptr,n);
173: ox_copy_bptr += n;
174: } else
175: #if defined(VISUAL)
176: if ( _fileno(stream) < 0 )
177: return cwrite(ptr,size,nitems,(STREAM *)stream);
178: else
179: #elif MPI
180: if ( (char)fileno(stream) < 0 )
181: return cwrite(ptr,size,nitems,(STREAM *)stream);
182: else
183: #endif
184: return fwrite(ptr,size,nitems,stream);
185: }
186:
187: void write_char(f,p)
188: FILE *f;
189: unsigned char *p;
190: {
191: gen_fwrite(p,sizeof(unsigned char),1,f);
192: }
193:
194: void write_short(f,p)
195: FILE *f;
196: unsigned short *p;
197: {
198: unsigned short t;
199:
200: if ( little_endian && (ox_file_io || ox_need_conv) ) {
201: t = htons(*p);
202: gen_fwrite(&t,sizeof(unsigned short),1,f);
203: } else
204: gen_fwrite(p,sizeof(unsigned short),1,f);
205: }
206:
207: void write_int(f,p)
208: FILE *f;
209: unsigned int *p;
210: {
211: unsigned int t;
212:
213: if ( little_endian && (ox_file_io || ox_need_conv) ) {
214: t = htonl(*p);
215: gen_fwrite(&t,sizeof(unsigned int),1,f);
216: } else
217: gen_fwrite(p,sizeof(unsigned int),1,f);
218: }
219:
1.7 noro 220: #if defined(VISUAL) && defined(DES_ENC)
1.1 noro 221: int des_encryption;
222: static unsigned char asir_deskey[8] = {0xc7,0xe0,0xfc,0xb5,0xc3,0xad,0x8e,0x3a};
223: static unsigned char deskey_string[96];
224:
225: void init_deskey()
226: {
227: static int deskey_initialized = 0;
228:
229: if ( !deskey_initialized ) {
230: key_schedule(asir_deskey,deskey_string);
231: deskey_initialized = 1;
232: }
233: }
1.7 noro 234: #endif
1.1 noro 235:
236: void write_intarray(f,p,l)
237: FILE *f;
238: unsigned int *p;
239: int l;
240: {
241: int i;
242: unsigned int t;
1.6 noro 243: #if defined(VISUAL) && defined(DES_ENC)
1.1 noro 244: int l2;
245: unsigned int plain[2],encrypted[2];
246:
247: if ( des_encryption ) {
248: l2 = l>>1;
249: for ( i = 0; i < l2; i++ ) {
250: plain[0] = *p++;
251: plain[1] = *p++;
252: des_enc(plain,deskey_string,encrypted);
253: encrypted[0] = htonl(encrypted[0]);
254: encrypted[1] = htonl(encrypted[1]);
255: gen_fwrite(encrypted,sizeof(unsigned int),2,f);
256: }
257: if ( (l2<<1) < l ) {
258: plain[0] = *p;
259: plain[1] = 0;
260: des_enc(plain,deskey_string,encrypted);
261: encrypted[0] = htonl(encrypted[0]);
262: encrypted[1] = htonl(encrypted[1]);
263: gen_fwrite(encrypted,sizeof(unsigned int),2,f);
264: }
265: } else
266: #endif
267: if ( little_endian && (ox_file_io || ox_need_conv) )
268: for ( i = 0; i < l; i++, p++) {
269: t = htonl(*p);
270: gen_fwrite(&t,sizeof(unsigned int),1,f);
271: }
272: else
273: gen_fwrite(p,sizeof(unsigned int),l,f);
274: }
275:
276: #if defined(LONG_IS_64BIT)
277: void write_longarray(f,p,l)
278: FILE *f;
279: unsigned long *p;
280: int l;
281: {
282: int i;
283: unsigned long w;
284: unsigned int hi,lo;
285:
286: if ( little_endian && (ox_file_io || ox_need_conv) )
287: for ( i = 0; i < l; i++, p++) {
288: w = *p; hi = w>>32; lo = w&0xffffffff;
289: hi = htonl(hi); lo = htonl(lo);
290: gen_fwrite(&hi,sizeof(unsigned int),1,f);
291: gen_fwrite(&lo,sizeof(unsigned int),1,f);
292: }
293: else
294: gen_fwrite(p,sizeof(unsigned long),l,f);
295: }
296: #endif
297:
298: void write_double(f,p)
299: FILE *f;
300: double *p;
301: {
302: unsigned int t;
303:
304: if ( little_endian && (ox_file_io || ox_need_conv) ) {
305: t = htonl(((unsigned int *)p)[1]);
306: gen_fwrite(&t,sizeof(unsigned int),1,f);
307: t = htonl(((unsigned int *)p)[0]);
308: gen_fwrite(&t,sizeof(unsigned int),1,f);
309: } else
310: gen_fwrite(p,sizeof(double),1,f);
311: }
312:
313: void write_string(f,p,l)
314: FILE *f;
315: unsigned char *p;
316: {
317: gen_fwrite(p,sizeof(unsigned char),l,f);
318: }
319:
320: void read_char(f,p)
321: FILE *f;
322: unsigned char *p;
323: {
324: gen_fread((char *)p,sizeof(unsigned char),1,f);
325: }
326:
327: void read_short(f,p)
328: FILE *f;
329: unsigned short *p;
330: {
331: gen_fread(p,sizeof(unsigned short),1,f);
332: if ( little_endian && (ox_file_io || ox_need_conv) )
333: *p = ntohs(*p);
334: }
335:
336: void read_int(f,p)
337: FILE *f;
338: unsigned int *p;
339: {
340: gen_fread(p,sizeof(unsigned int),1,f);
341: if ( little_endian && (ox_file_io || ox_need_conv) )
342: *p = ntohl(*p);
343: }
344:
345: void read_intarray(f,p,l)
346: FILE *f;
347: unsigned int *p;
348: int l;
349: {
350: int i;
351: unsigned int t;
1.6 noro 352: #if defined(VISUAL) && defined(DES_ENC)
1.1 noro 353: int l2;
354: unsigned int plain[2],encrypted[2];
355:
356: if ( des_encryption ) {
357: l2 = l>>1;
358: for ( i = 0; i < l2; i++ ) {
359: gen_fread((char *)encrypted,sizeof(unsigned int),2,f);
360: encrypted[0] = ntohl(encrypted[0]);
361: encrypted[1] = ntohl(encrypted[1]);
362: des_dec(encrypted,deskey_string,plain);
363: *p++ = plain[0];
364: *p++ = plain[1];
365: }
366: if ( (l2<<1) < l ) {
367: gen_fread((char *)encrypted,sizeof(unsigned int),2,f);
368: encrypted[0] = ntohl(encrypted[0]);
369: encrypted[1] = ntohl(encrypted[1]);
370: des_dec(encrypted,deskey_string,plain);
371: *p = plain[0];
372: }
373: } else
374: #endif
375: {
376: gen_fread(p,sizeof(unsigned int),l,f);
377: if ( little_endian && (ox_file_io || ox_need_conv) )
378: for ( i = 0; i < l; i++, p++ )
379: *p = ntohl(*p);
380: }
381: }
382:
383: #if defined(LONG_IS_64BIT)
384: void read_longarray(f,p,l)
385: FILE *f;
386: unsigned long *p;
387: int l;
388: {
389: int i;
390: unsigned int hi,lo;
391:
392: if ( little_endian && (ox_file_io || ox_need_conv) ) {
393: for ( i = l; i > 1; i -= 2, p++ ) {
394: gen_fread(&hi,sizeof(unsigned int),1,f);
395: gen_fread(&lo,sizeof(unsigned int),1,f);
396: hi = ntohl(hi); lo = ntohl(lo);
397: *p = (((unsigned long)hi)<<32)|((unsigned long)lo);
398: }
399: if ( i == 1 ) {
400: gen_fread(&hi,sizeof(unsigned int),1,f);
401: hi = ntohl(hi); *p = (((unsigned long)hi)<<32);
402: }
403: } else
404: gen_fread(p,sizeof(unsigned int),l,f);
405: }
406: #endif
407:
408: void read_string(f,p,l)
409: FILE *f;
410: unsigned char *p;
411: {
412: gen_fread((char *)p,sizeof(unsigned char),l,f);
413: }
414:
415: void read_double(f,p)
416: FILE *f;
417: double *p;
418: {
419: unsigned int t;
420:
421: if ( little_endian && (ox_file_io || ox_need_conv) ) {
422: gen_fread((char *)&t,sizeof(unsigned int),1,f);
423: ((unsigned int *)p)[1] = ntohl(t);
424: gen_fread((char *)&t,sizeof(unsigned int),1,f);
425: ((unsigned int *)p)[0] = ntohl(t);
426: } else
427: gen_fread((char *)p,sizeof(double),1,f);
428: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>