[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.3

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

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