[BACK]Return to io.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / io

Annotation of OpenXM_contrib2/asir2000/io/io.c, Revision 1.2

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>