Annotation of OpenXM_contrib2/asir2000/io/io.c, Revision 1.7
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.7 ! noro 47: * $OpenXM: OpenXM_contrib2/asir2000/io/io.c,v 1.6 2000/08/25 07:59:24 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.3 noro 105: /*
106: * library mode functions
107: * byte order is controlled by lib_ox_need_conv.
108: */
1.1 noro 109:
1.2 noro 110: void ox_obj_to_buf_as_cmo(p)
111: Obj p;
112: {
1.3 noro 113: ox_need_conv = lib_ox_need_conv;
1.2 noro 114: ox_do_copy = 1; write_cmo(0,p); ox_do_copy = 0;
115: }
116:
117: void ox_buf_to_obj_as_cmo(p)
118: Obj *p;
119: {
1.3 noro 120: ox_need_conv = lib_ox_need_conv;
1.2 noro 121: ox_do_copy = 1; read_cmo(0,p); ox_do_copy = 0;
122: }
123:
1.1 noro 124: void ox_vl_to_buf(vl)
125: VL vl;
126: {
127: ox_do_copy = 1; savevl(0,vl); ox_do_copy = 0;
128: }
129:
130: int gen_fread (ptr,size,nitems,stream)
131: char *ptr;
132: int size,nitems;
133: FILE *stream;
134: {
135: int n;
136:
137: if ( ox_do_copy ) {
138: n = size*nitems;
139: memcpy(ptr,ox_copy_bptr,n);
140: ox_copy_bptr += n;
141: } else {
142: #if defined(VISUAL)
143: if ( _fileno(stream) < 0 )
144: n = cread(ptr,size,nitems,(STREAM *)stream);
145: else
146: #elif MPI
147: if ( (char)fileno(stream) < 0 )
148: n = cread(ptr,size,nitems,(STREAM *)stream);
149: else
150: #endif
151: n = fread(ptr,size,nitems,stream);
152: if ( !n )
153: ExitAsir();
154: else
155: return n;
156: }
157: }
158:
159: int gen_fwrite (ptr,size,nitems,stream)
160: char *ptr;
161: int size,nitems;
162: FILE *stream;
163: {
164: int n;
165:
166: if ( ox_do_count )
167: ox_count_length += size*nitems;
168: else if ( ox_do_copy ) {
169: n = size*nitems;
170: memcpy(ox_copy_bptr,ptr,n);
171: ox_copy_bptr += n;
172: } else
173: #if defined(VISUAL)
174: if ( _fileno(stream) < 0 )
175: return cwrite(ptr,size,nitems,(STREAM *)stream);
176: else
177: #elif MPI
178: if ( (char)fileno(stream) < 0 )
179: return cwrite(ptr,size,nitems,(STREAM *)stream);
180: else
181: #endif
182: return fwrite(ptr,size,nitems,stream);
183: }
184:
185: void write_char(f,p)
186: FILE *f;
187: unsigned char *p;
188: {
189: gen_fwrite(p,sizeof(unsigned char),1,f);
190: }
191:
192: void write_short(f,p)
193: FILE *f;
194: unsigned short *p;
195: {
196: unsigned short t;
197:
198: if ( little_endian && (ox_file_io || ox_need_conv) ) {
199: t = htons(*p);
200: gen_fwrite(&t,sizeof(unsigned short),1,f);
201: } else
202: gen_fwrite(p,sizeof(unsigned short),1,f);
203: }
204:
205: void write_int(f,p)
206: FILE *f;
207: unsigned int *p;
208: {
209: unsigned int t;
210:
211: if ( little_endian && (ox_file_io || ox_need_conv) ) {
212: t = htonl(*p);
213: gen_fwrite(&t,sizeof(unsigned int),1,f);
214: } else
215: gen_fwrite(p,sizeof(unsigned int),1,f);
216: }
217:
1.7 ! noro 218: #if defined(VISUAL) && defined(DES_ENC)
1.1 noro 219: int des_encryption;
220: static unsigned char asir_deskey[8] = {0xc7,0xe0,0xfc,0xb5,0xc3,0xad,0x8e,0x3a};
221: static unsigned char deskey_string[96];
222:
223: void init_deskey()
224: {
225: static int deskey_initialized = 0;
226:
227: if ( !deskey_initialized ) {
228: key_schedule(asir_deskey,deskey_string);
229: deskey_initialized = 1;
230: }
231: }
1.7 ! noro 232: #endif
1.1 noro 233:
234: void write_intarray(f,p,l)
235: FILE *f;
236: unsigned int *p;
237: int l;
238: {
239: int i;
240: unsigned int t;
1.6 noro 241: #if defined(VISUAL) && defined(DES_ENC)
1.1 noro 242: int l2;
243: unsigned int plain[2],encrypted[2];
244:
245: if ( des_encryption ) {
246: l2 = l>>1;
247: for ( i = 0; i < l2; i++ ) {
248: plain[0] = *p++;
249: plain[1] = *p++;
250: des_enc(plain,deskey_string,encrypted);
251: encrypted[0] = htonl(encrypted[0]);
252: encrypted[1] = htonl(encrypted[1]);
253: gen_fwrite(encrypted,sizeof(unsigned int),2,f);
254: }
255: if ( (l2<<1) < l ) {
256: plain[0] = *p;
257: plain[1] = 0;
258: des_enc(plain,deskey_string,encrypted);
259: encrypted[0] = htonl(encrypted[0]);
260: encrypted[1] = htonl(encrypted[1]);
261: gen_fwrite(encrypted,sizeof(unsigned int),2,f);
262: }
263: } else
264: #endif
265: if ( little_endian && (ox_file_io || ox_need_conv) )
266: for ( i = 0; i < l; i++, p++) {
267: t = htonl(*p);
268: gen_fwrite(&t,sizeof(unsigned int),1,f);
269: }
270: else
271: gen_fwrite(p,sizeof(unsigned int),l,f);
272: }
273:
274: #if defined(LONG_IS_64BIT)
275: void write_longarray(f,p,l)
276: FILE *f;
277: unsigned long *p;
278: int l;
279: {
280: int i;
281: unsigned long w;
282: unsigned int hi,lo;
283:
284: if ( little_endian && (ox_file_io || ox_need_conv) )
285: for ( i = 0; i < l; i++, p++) {
286: w = *p; hi = w>>32; lo = w&0xffffffff;
287: hi = htonl(hi); lo = htonl(lo);
288: gen_fwrite(&hi,sizeof(unsigned int),1,f);
289: gen_fwrite(&lo,sizeof(unsigned int),1,f);
290: }
291: else
292: gen_fwrite(p,sizeof(unsigned long),l,f);
293: }
294: #endif
295:
296: void write_double(f,p)
297: FILE *f;
298: double *p;
299: {
300: unsigned int t;
301:
302: if ( little_endian && (ox_file_io || ox_need_conv) ) {
303: t = htonl(((unsigned int *)p)[1]);
304: gen_fwrite(&t,sizeof(unsigned int),1,f);
305: t = htonl(((unsigned int *)p)[0]);
306: gen_fwrite(&t,sizeof(unsigned int),1,f);
307: } else
308: gen_fwrite(p,sizeof(double),1,f);
309: }
310:
311: void write_string(f,p,l)
312: FILE *f;
313: unsigned char *p;
314: {
315: gen_fwrite(p,sizeof(unsigned char),l,f);
316: }
317:
318: void read_char(f,p)
319: FILE *f;
320: unsigned char *p;
321: {
322: gen_fread((char *)p,sizeof(unsigned char),1,f);
323: }
324:
325: void read_short(f,p)
326: FILE *f;
327: unsigned short *p;
328: {
329: gen_fread(p,sizeof(unsigned short),1,f);
330: if ( little_endian && (ox_file_io || ox_need_conv) )
331: *p = ntohs(*p);
332: }
333:
334: void read_int(f,p)
335: FILE *f;
336: unsigned int *p;
337: {
338: gen_fread(p,sizeof(unsigned int),1,f);
339: if ( little_endian && (ox_file_io || ox_need_conv) )
340: *p = ntohl(*p);
341: }
342:
343: void read_intarray(f,p,l)
344: FILE *f;
345: unsigned int *p;
346: int l;
347: {
348: int i;
349: unsigned int t;
1.6 noro 350: #if defined(VISUAL) && defined(DES_ENC)
1.1 noro 351: int l2;
352: unsigned int plain[2],encrypted[2];
353:
354: if ( des_encryption ) {
355: l2 = l>>1;
356: for ( i = 0; i < l2; i++ ) {
357: gen_fread((char *)encrypted,sizeof(unsigned int),2,f);
358: encrypted[0] = ntohl(encrypted[0]);
359: encrypted[1] = ntohl(encrypted[1]);
360: des_dec(encrypted,deskey_string,plain);
361: *p++ = plain[0];
362: *p++ = plain[1];
363: }
364: if ( (l2<<1) < l ) {
365: gen_fread((char *)encrypted,sizeof(unsigned int),2,f);
366: encrypted[0] = ntohl(encrypted[0]);
367: encrypted[1] = ntohl(encrypted[1]);
368: des_dec(encrypted,deskey_string,plain);
369: *p = plain[0];
370: }
371: } else
372: #endif
373: {
374: gen_fread(p,sizeof(unsigned int),l,f);
375: if ( little_endian && (ox_file_io || ox_need_conv) )
376: for ( i = 0; i < l; i++, p++ )
377: *p = ntohl(*p);
378: }
379: }
380:
381: #if defined(LONG_IS_64BIT)
382: void read_longarray(f,p,l)
383: FILE *f;
384: unsigned long *p;
385: int l;
386: {
387: int i;
388: unsigned int hi,lo;
389:
390: if ( little_endian && (ox_file_io || ox_need_conv) ) {
391: for ( i = l; i > 1; i -= 2, p++ ) {
392: gen_fread(&hi,sizeof(unsigned int),1,f);
393: gen_fread(&lo,sizeof(unsigned int),1,f);
394: hi = ntohl(hi); lo = ntohl(lo);
395: *p = (((unsigned long)hi)<<32)|((unsigned long)lo);
396: }
397: if ( i == 1 ) {
398: gen_fread(&hi,sizeof(unsigned int),1,f);
399: hi = ntohl(hi); *p = (((unsigned long)hi)<<32);
400: }
401: } else
402: gen_fread(p,sizeof(unsigned int),l,f);
403: }
404: #endif
405:
406: void read_string(f,p,l)
407: FILE *f;
408: unsigned char *p;
409: {
410: gen_fread((char *)p,sizeof(unsigned char),l,f);
411: }
412:
413: void read_double(f,p)
414: FILE *f;
415: double *p;
416: {
417: unsigned int t;
418:
419: if ( little_endian && (ox_file_io || ox_need_conv) ) {
420: gen_fread((char *)&t,sizeof(unsigned int),1,f);
421: ((unsigned int *)p)[1] = ntohl(t);
422: gen_fread((char *)&t,sizeof(unsigned int),1,f);
423: ((unsigned int *)p)[0] = ntohl(t);
424: } else
425: gen_fread((char *)p,sizeof(double),1,f);
426: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>