Annotation of OpenXM_contrib2/asir2000/io/io.c, Revision 1.1.1.1
1.1 noro 1: /* $OpenXM: OpenXM/src/asir99/io/io.c,v 1.1.1.1 1999/11/10 08:12:30 noro Exp $ */
2: #include <stdio.h>
3: #include "ca.h"
4: #if defined(VISUAL) || MPI
5: #include "wsio.h"
6: #endif
7:
8: extern int little_endian;
9: extern int ox_do_copy, ox_do_count, ox_count_length, ox_file_io, ox_need_conv;
10: extern char *ox_copy_bptr;
11:
12: void reset_io()
13: {
14: ox_file_io = 0;
15: }
16:
17: void endian_init()
18: {
19: unsigned int et = 0xff;
20:
21: if ( *((char *)&et) )
22: little_endian = 1;
23: else
24: little_endian = 0;
25: ox_need_conv = 1;
26: }
27:
28: int countobj(p)
29: Obj p;
30: {
31: ox_count_length = 0;
32: ox_do_count = 1; saveobj(0,p); ox_do_count = 0;
33: return ox_count_length;
34: }
35:
36: int countvl(vl)
37: VL vl;
38: {
39: ox_count_length = 0;
40: ox_do_count = 1; savevl(0,vl); ox_do_count = 0;
41: return ox_count_length;
42: }
43:
44: void ox_copy_init(s)
45: char *s;
46: {
47: ox_copy_bptr = s;
48: }
49:
50: void ox_obj_to_buf(p)
51: Obj p;
52: {
53: ox_do_copy = 1; saveobj(0,p); ox_do_copy = 0;
54: }
55:
56: void ox_buf_to_obj(p)
57: Obj *p;
58: {
59: ox_do_copy = 1; loadobj(0,p); ox_do_copy = 0;
60: }
61:
62: void ox_vl_to_buf(vl)
63: VL vl;
64: {
65: ox_do_copy = 1; savevl(0,vl); ox_do_copy = 0;
66: }
67:
68: int gen_fread (ptr,size,nitems,stream)
69: char *ptr;
70: int size,nitems;
71: FILE *stream;
72: {
73: int n;
74:
75: if ( ox_do_copy ) {
76: n = size*nitems;
77: memcpy(ptr,ox_copy_bptr,n);
78: ox_copy_bptr += n;
79: } else {
80: #if defined(VISUAL)
81: if ( _fileno(stream) < 0 )
82: n = cread(ptr,size,nitems,(STREAM *)stream);
83: else
84: #elif MPI
85: if ( (char)fileno(stream) < 0 )
86: n = cread(ptr,size,nitems,(STREAM *)stream);
87: else
88: #endif
89: n = fread(ptr,size,nitems,stream);
90: if ( !n )
91: ExitAsir();
92: else
93: return n;
94: }
95: }
96:
97: int gen_fwrite (ptr,size,nitems,stream)
98: char *ptr;
99: int size,nitems;
100: FILE *stream;
101: {
102: int n;
103:
104: if ( ox_do_count )
105: ox_count_length += size*nitems;
106: else if ( ox_do_copy ) {
107: n = size*nitems;
108: memcpy(ox_copy_bptr,ptr,n);
109: ox_copy_bptr += n;
110: } else
111: #if defined(VISUAL)
112: if ( _fileno(stream) < 0 )
113: return cwrite(ptr,size,nitems,(STREAM *)stream);
114: else
115: #elif MPI
116: if ( (char)fileno(stream) < 0 )
117: return cwrite(ptr,size,nitems,(STREAM *)stream);
118: else
119: #endif
120: return fwrite(ptr,size,nitems,stream);
121: }
122:
123: void write_char(f,p)
124: FILE *f;
125: unsigned char *p;
126: {
127: gen_fwrite(p,sizeof(unsigned char),1,f);
128: }
129:
130: void write_short(f,p)
131: FILE *f;
132: unsigned short *p;
133: {
134: unsigned short t;
135:
136: if ( little_endian && (ox_file_io || ox_need_conv) ) {
137: t = htons(*p);
138: gen_fwrite(&t,sizeof(unsigned short),1,f);
139: } else
140: gen_fwrite(p,sizeof(unsigned short),1,f);
141: }
142:
143: void write_int(f,p)
144: FILE *f;
145: unsigned int *p;
146: {
147: unsigned int t;
148:
149: if ( little_endian && (ox_file_io || ox_need_conv) ) {
150: t = htonl(*p);
151: gen_fwrite(&t,sizeof(unsigned int),1,f);
152: } else
153: gen_fwrite(p,sizeof(unsigned int),1,f);
154: }
155:
156: int des_encryption;
157: static unsigned char asir_deskey[8] = {0xc7,0xe0,0xfc,0xb5,0xc3,0xad,0x8e,0x3a};
158: static unsigned char deskey_string[96];
159:
160: void init_deskey()
161: {
162: static int deskey_initialized = 0;
163:
164: if ( !deskey_initialized ) {
165: key_schedule(asir_deskey,deskey_string);
166: deskey_initialized = 1;
167: }
168: }
169:
170: void write_intarray(f,p,l)
171: FILE *f;
172: unsigned int *p;
173: int l;
174: {
175: int i;
176: unsigned int t;
177: #if defined(VISUAL)
178: int l2;
179: unsigned int plain[2],encrypted[2];
180:
181: if ( des_encryption ) {
182: l2 = l>>1;
183: for ( i = 0; i < l2; i++ ) {
184: plain[0] = *p++;
185: plain[1] = *p++;
186: des_enc(plain,deskey_string,encrypted);
187: encrypted[0] = htonl(encrypted[0]);
188: encrypted[1] = htonl(encrypted[1]);
189: gen_fwrite(encrypted,sizeof(unsigned int),2,f);
190: }
191: if ( (l2<<1) < l ) {
192: plain[0] = *p;
193: plain[1] = 0;
194: des_enc(plain,deskey_string,encrypted);
195: encrypted[0] = htonl(encrypted[0]);
196: encrypted[1] = htonl(encrypted[1]);
197: gen_fwrite(encrypted,sizeof(unsigned int),2,f);
198: }
199: } else
200: #endif
201: if ( little_endian && (ox_file_io || ox_need_conv) )
202: for ( i = 0; i < l; i++, p++) {
203: t = htonl(*p);
204: gen_fwrite(&t,sizeof(unsigned int),1,f);
205: }
206: else
207: gen_fwrite(p,sizeof(unsigned int),l,f);
208: }
209:
210: #if defined(LONG_IS_64BIT)
211: void write_longarray(f,p,l)
212: FILE *f;
213: unsigned long *p;
214: int l;
215: {
216: int i;
217: unsigned long w;
218: unsigned int hi,lo;
219:
220: if ( little_endian && (ox_file_io || ox_need_conv) )
221: for ( i = 0; i < l; i++, p++) {
222: w = *p; hi = w>>32; lo = w&0xffffffff;
223: hi = htonl(hi); lo = htonl(lo);
224: gen_fwrite(&hi,sizeof(unsigned int),1,f);
225: gen_fwrite(&lo,sizeof(unsigned int),1,f);
226: }
227: else
228: gen_fwrite(p,sizeof(unsigned long),l,f);
229: }
230: #endif
231:
232: void write_double(f,p)
233: FILE *f;
234: double *p;
235: {
236: unsigned int t;
237:
238: if ( little_endian && (ox_file_io || ox_need_conv) ) {
239: t = htonl(((unsigned int *)p)[1]);
240: gen_fwrite(&t,sizeof(unsigned int),1,f);
241: t = htonl(((unsigned int *)p)[0]);
242: gen_fwrite(&t,sizeof(unsigned int),1,f);
243: } else
244: gen_fwrite(p,sizeof(double),1,f);
245: }
246:
247: void write_string(f,p,l)
248: FILE *f;
249: unsigned char *p;
250: {
251: gen_fwrite(p,sizeof(unsigned char),l,f);
252: }
253:
254: void read_char(f,p)
255: FILE *f;
256: unsigned char *p;
257: {
258: gen_fread((char *)p,sizeof(unsigned char),1,f);
259: }
260:
261: void read_short(f,p)
262: FILE *f;
263: unsigned short *p;
264: {
265: gen_fread(p,sizeof(unsigned short),1,f);
266: if ( little_endian && (ox_file_io || ox_need_conv) )
267: *p = ntohs(*p);
268: }
269:
270: void read_int(f,p)
271: FILE *f;
272: unsigned int *p;
273: {
274: gen_fread(p,sizeof(unsigned int),1,f);
275: if ( little_endian && (ox_file_io || ox_need_conv) )
276: *p = ntohl(*p);
277: }
278:
279: void read_intarray(f,p,l)
280: FILE *f;
281: unsigned int *p;
282: int l;
283: {
284: int i;
285: unsigned int t;
286: #if defined(VISUAL)
287: int l2;
288: unsigned int plain[2],encrypted[2];
289:
290: if ( des_encryption ) {
291: l2 = l>>1;
292: for ( i = 0; i < l2; i++ ) {
293: gen_fread((char *)encrypted,sizeof(unsigned int),2,f);
294: encrypted[0] = ntohl(encrypted[0]);
295: encrypted[1] = ntohl(encrypted[1]);
296: des_dec(encrypted,deskey_string,plain);
297: *p++ = plain[0];
298: *p++ = plain[1];
299: }
300: if ( (l2<<1) < l ) {
301: gen_fread((char *)encrypted,sizeof(unsigned int),2,f);
302: encrypted[0] = ntohl(encrypted[0]);
303: encrypted[1] = ntohl(encrypted[1]);
304: des_dec(encrypted,deskey_string,plain);
305: *p = plain[0];
306: }
307: } else
308: #endif
309: {
310: gen_fread(p,sizeof(unsigned int),l,f);
311: if ( little_endian && (ox_file_io || ox_need_conv) )
312: for ( i = 0; i < l; i++, p++ )
313: *p = ntohl(*p);
314: }
315: }
316:
317: #if defined(LONG_IS_64BIT)
318: void read_longarray(f,p,l)
319: FILE *f;
320: unsigned long *p;
321: int l;
322: {
323: int i;
324: unsigned int hi,lo;
325:
326: if ( little_endian && (ox_file_io || ox_need_conv) ) {
327: for ( i = l; i > 1; i -= 2, p++ ) {
328: gen_fread(&hi,sizeof(unsigned int),1,f);
329: gen_fread(&lo,sizeof(unsigned int),1,f);
330: hi = ntohl(hi); lo = ntohl(lo);
331: *p = (((unsigned long)hi)<<32)|((unsigned long)lo);
332: }
333: if ( i == 1 ) {
334: gen_fread(&hi,sizeof(unsigned int),1,f);
335: hi = ntohl(hi); *p = (((unsigned long)hi)<<32);
336: }
337: } else
338: gen_fread(p,sizeof(unsigned int),l,f);
339: }
340: #endif
341:
342: void read_string(f,p,l)
343: FILE *f;
344: unsigned char *p;
345: {
346: gen_fread((char *)p,sizeof(unsigned char),l,f);
347: }
348:
349: void read_double(f,p)
350: FILE *f;
351: double *p;
352: {
353: unsigned int t;
354:
355: if ( little_endian && (ox_file_io || ox_need_conv) ) {
356: gen_fread((char *)&t,sizeof(unsigned int),1,f);
357: ((unsigned int *)p)[1] = ntohl(t);
358: gen_fread((char *)&t,sizeof(unsigned int),1,f);
359: ((unsigned int *)p)[0] = ntohl(t);
360: } else
361: gen_fread((char *)p,sizeof(double),1,f);
362: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>