Annotation of OpenXM_contrib2/asir2000/io/io.c, Revision 1.4
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
! 26: * e-mail at risa-admin@flab.fujitsu.co.jp of the detailed specification
! 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.
! 47: * $OpenXM: OpenXM_contrib2/asir2000/io/io.c,v 1.3 2000/03/16 04:55:20 noro Exp $
! 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:
218: int des_encryption;
219: static unsigned char asir_deskey[8] = {0xc7,0xe0,0xfc,0xb5,0xc3,0xad,0x8e,0x3a};
220: static unsigned char deskey_string[96];
221:
222: void init_deskey()
223: {
224: static int deskey_initialized = 0;
225:
226: if ( !deskey_initialized ) {
227: key_schedule(asir_deskey,deskey_string);
228: deskey_initialized = 1;
229: }
230: }
231:
232: void write_intarray(f,p,l)
233: FILE *f;
234: unsigned int *p;
235: int l;
236: {
237: int i;
238: unsigned int t;
239: #if defined(VISUAL)
240: int l2;
241: unsigned int plain[2],encrypted[2];
242:
243: if ( des_encryption ) {
244: l2 = l>>1;
245: for ( i = 0; i < l2; i++ ) {
246: plain[0] = *p++;
247: plain[1] = *p++;
248: des_enc(plain,deskey_string,encrypted);
249: encrypted[0] = htonl(encrypted[0]);
250: encrypted[1] = htonl(encrypted[1]);
251: gen_fwrite(encrypted,sizeof(unsigned int),2,f);
252: }
253: if ( (l2<<1) < l ) {
254: plain[0] = *p;
255: plain[1] = 0;
256: des_enc(plain,deskey_string,encrypted);
257: encrypted[0] = htonl(encrypted[0]);
258: encrypted[1] = htonl(encrypted[1]);
259: gen_fwrite(encrypted,sizeof(unsigned int),2,f);
260: }
261: } else
262: #endif
263: if ( little_endian && (ox_file_io || ox_need_conv) )
264: for ( i = 0; i < l; i++, p++) {
265: t = htonl(*p);
266: gen_fwrite(&t,sizeof(unsigned int),1,f);
267: }
268: else
269: gen_fwrite(p,sizeof(unsigned int),l,f);
270: }
271:
272: #if defined(LONG_IS_64BIT)
273: void write_longarray(f,p,l)
274: FILE *f;
275: unsigned long *p;
276: int l;
277: {
278: int i;
279: unsigned long w;
280: unsigned int hi,lo;
281:
282: if ( little_endian && (ox_file_io || ox_need_conv) )
283: for ( i = 0; i < l; i++, p++) {
284: w = *p; hi = w>>32; lo = w&0xffffffff;
285: hi = htonl(hi); lo = htonl(lo);
286: gen_fwrite(&hi,sizeof(unsigned int),1,f);
287: gen_fwrite(&lo,sizeof(unsigned int),1,f);
288: }
289: else
290: gen_fwrite(p,sizeof(unsigned long),l,f);
291: }
292: #endif
293:
294: void write_double(f,p)
295: FILE *f;
296: double *p;
297: {
298: unsigned int t;
299:
300: if ( little_endian && (ox_file_io || ox_need_conv) ) {
301: t = htonl(((unsigned int *)p)[1]);
302: gen_fwrite(&t,sizeof(unsigned int),1,f);
303: t = htonl(((unsigned int *)p)[0]);
304: gen_fwrite(&t,sizeof(unsigned int),1,f);
305: } else
306: gen_fwrite(p,sizeof(double),1,f);
307: }
308:
309: void write_string(f,p,l)
310: FILE *f;
311: unsigned char *p;
312: {
313: gen_fwrite(p,sizeof(unsigned char),l,f);
314: }
315:
316: void read_char(f,p)
317: FILE *f;
318: unsigned char *p;
319: {
320: gen_fread((char *)p,sizeof(unsigned char),1,f);
321: }
322:
323: void read_short(f,p)
324: FILE *f;
325: unsigned short *p;
326: {
327: gen_fread(p,sizeof(unsigned short),1,f);
328: if ( little_endian && (ox_file_io || ox_need_conv) )
329: *p = ntohs(*p);
330: }
331:
332: void read_int(f,p)
333: FILE *f;
334: unsigned int *p;
335: {
336: gen_fread(p,sizeof(unsigned int),1,f);
337: if ( little_endian && (ox_file_io || ox_need_conv) )
338: *p = ntohl(*p);
339: }
340:
341: void read_intarray(f,p,l)
342: FILE *f;
343: unsigned int *p;
344: int l;
345: {
346: int i;
347: unsigned int t;
348: #if defined(VISUAL)
349: int l2;
350: unsigned int plain[2],encrypted[2];
351:
352: if ( des_encryption ) {
353: l2 = l>>1;
354: for ( i = 0; i < l2; i++ ) {
355: gen_fread((char *)encrypted,sizeof(unsigned int),2,f);
356: encrypted[0] = ntohl(encrypted[0]);
357: encrypted[1] = ntohl(encrypted[1]);
358: des_dec(encrypted,deskey_string,plain);
359: *p++ = plain[0];
360: *p++ = plain[1];
361: }
362: if ( (l2<<1) < l ) {
363: gen_fread((char *)encrypted,sizeof(unsigned int),2,f);
364: encrypted[0] = ntohl(encrypted[0]);
365: encrypted[1] = ntohl(encrypted[1]);
366: des_dec(encrypted,deskey_string,plain);
367: *p = plain[0];
368: }
369: } else
370: #endif
371: {
372: gen_fread(p,sizeof(unsigned int),l,f);
373: if ( little_endian && (ox_file_io || ox_need_conv) )
374: for ( i = 0; i < l; i++, p++ )
375: *p = ntohl(*p);
376: }
377: }
378:
379: #if defined(LONG_IS_64BIT)
380: void read_longarray(f,p,l)
381: FILE *f;
382: unsigned long *p;
383: int l;
384: {
385: int i;
386: unsigned int hi,lo;
387:
388: if ( little_endian && (ox_file_io || ox_need_conv) ) {
389: for ( i = l; i > 1; i -= 2, p++ ) {
390: gen_fread(&hi,sizeof(unsigned int),1,f);
391: gen_fread(&lo,sizeof(unsigned int),1,f);
392: hi = ntohl(hi); lo = ntohl(lo);
393: *p = (((unsigned long)hi)<<32)|((unsigned long)lo);
394: }
395: if ( i == 1 ) {
396: gen_fread(&hi,sizeof(unsigned int),1,f);
397: hi = ntohl(hi); *p = (((unsigned long)hi)<<32);
398: }
399: } else
400: gen_fread(p,sizeof(unsigned int),l,f);
401: }
402: #endif
403:
404: void read_string(f,p,l)
405: FILE *f;
406: unsigned char *p;
407: {
408: gen_fread((char *)p,sizeof(unsigned char),l,f);
409: }
410:
411: void read_double(f,p)
412: FILE *f;
413: double *p;
414: {
415: unsigned int t;
416:
417: if ( little_endian && (ox_file_io || ox_need_conv) ) {
418: gen_fread((char *)&t,sizeof(unsigned int),1,f);
419: ((unsigned int *)p)[1] = ntohl(t);
420: gen_fread((char *)&t,sizeof(unsigned int),1,f);
421: ((unsigned int *)p)[0] = ntohl(t);
422: } else
423: gen_fread((char *)p,sizeof(double),1,f);
424: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>