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

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.20    ! noro       47:  * $OpenXM: OpenXM_contrib2/asir2000/io/io.c,v 1.19 2015/08/14 13:51:55 fujimoto Exp $
1.4       noro       48: */
1.1       noro       49: #include <stdio.h>
                     50: #include "ca.h"
1.9       noro       51: #include "parse.h"
1.19      fujimoto   52: #if defined(VISUAL) || defined(__MINGW32__) || defined(MPI)
1.1       noro       53: #include "wsio.h"
                     54: #endif
                     55:
1.3       noro       56: extern int little_endian,lib_ox_need_conv;
1.1       noro       57: extern int ox_do_copy, ox_do_count, ox_count_length, ox_file_io, ox_need_conv;
                     58: extern char *ox_copy_bptr;
                     59:
1.9       noro       60: /* XXX */
                     61: void write_cmo(pointer,Obj);
                     62:
1.1       noro       63: void reset_io()
                     64: {
                     65:        ox_file_io = 0;
                     66: }
                     67:
                     68: void endian_init()
                     69: {
                     70:        unsigned int et = 0xff;
                     71:
                     72:        if ( *((char *)&et) )
                     73:                little_endian = 1;
                     74:        else
                     75:                little_endian = 0;
                     76:        ox_need_conv = 1;
                     77: }
                     78:
1.9       noro       79: int countobj(Obj p)
1.1       noro       80: {
                     81:        ox_count_length = 0;
                     82:        ox_do_count = 1; saveobj(0,p); ox_do_count = 0;
                     83:        return ox_count_length;
                     84: }
                     85:
1.9       noro       86: int count_as_cmo(Obj p)
1.2       noro       87: {
                     88:        ox_count_length = 0;
                     89:        ox_do_count = 1; write_cmo(0,p); ox_do_count = 0;
                     90:        return ox_count_length;
                     91: }
                     92:
1.9       noro       93: int countvl(VL vl)
1.1       noro       94: {
                     95:        ox_count_length = 0;
                     96:        ox_do_count = 1; savevl(0,vl); ox_do_count = 0;
                     97:        return ox_count_length;
                     98: }
                     99:
1.9       noro      100: void ox_copy_init(char *s)
1.1       noro      101: {
                    102:        ox_copy_bptr = s;
                    103: }
                    104:
1.19      fujimoto  105: #if !defined(VISUAL) && !defined(__MINGW32__)
1.3       noro      106: /*
                    107:  * library mode functions
                    108:  * byte order is controlled by lib_ox_need_conv.
                    109:  */
1.1       noro      110:
1.9       noro      111: void ox_obj_to_buf_as_cmo(Obj p)
1.2       noro      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:
1.9       noro      117: void ox_buf_to_obj_as_cmo(Obj *p)
1.2       noro      118: {
1.3       noro      119:        ox_need_conv = lib_ox_need_conv;
1.2       noro      120:        ox_do_copy = 1; read_cmo(0,p); ox_do_copy = 0;
                    121: }
                    122:
1.9       noro      123: void ox_vl_to_buf(VL vl)
1.1       noro      124: {
                    125:        ox_do_copy = 1; savevl(0,vl); ox_do_copy = 0;
                    126: }
1.8       noro      127: #endif
1.1       noro      128:
1.9       noro      129: int gen_fread (char *ptr,int size,int nitems,FILE *stream)
1.1       noro      130: {
                    131:        int n;
                    132:
                    133:        if ( ox_do_copy ) {
                    134:                n = size*nitems;
                    135:                memcpy(ptr,ox_copy_bptr,n);
                    136:                ox_copy_bptr += n;
1.9       noro      137:                /* dummy return */
                    138:                return 0;
1.1       noro      139:        } else {
1.19      fujimoto  140: #if defined(VISUAL) || defined(__MINGW32__)
1.1       noro      141:                if ( _fileno(stream) < 0 )
                    142:                        n = cread(ptr,size,nitems,(STREAM *)stream);
                    143:                else
1.12      noro      144: #elif defined(MPI)
1.1       noro      145:                if ( (char)fileno(stream) < 0 )
                    146:                        n = cread(ptr,size,nitems,(STREAM *)stream);
                    147:                else
                    148: #endif
                    149:                n = fread(ptr,size,nitems,stream);
1.9       noro      150:                if ( !n ) {
1.1       noro      151:                        ExitAsir();
1.9       noro      152:                        /* NOTREACHED */
                    153:                        return 0;
                    154:                } else
1.1       noro      155:                        return n;
                    156:        }
                    157: }
                    158:
1.9       noro      159: int gen_fwrite (char *ptr,int size,int nitems,FILE *stream)
1.1       noro      160: {
                    161:        int n;
                    162:
1.9       noro      163:        if ( ox_do_count ) {
1.1       noro      164:                ox_count_length += size*nitems;
1.9       noro      165:                /* dummy return  */
                    166:                return 0;
                    167:        } else if ( ox_do_copy ) {
1.1       noro      168:                n = size*nitems;
                    169:                memcpy(ox_copy_bptr,ptr,n);
                    170:                ox_copy_bptr += n;
1.9       noro      171:                /* dummy return  */
                    172:                return 0;
1.1       noro      173:        } else
1.19      fujimoto  174: #if defined(VISUAL) || defined(__MINGW32__)
1.1       noro      175:        if ( _fileno(stream) < 0 )
                    176:                return cwrite(ptr,size,nitems,(STREAM *)stream);
                    177:        else
1.12      noro      178: #elif defined(MPI)
1.1       noro      179:        if ( (char)fileno(stream) < 0 )
                    180:                return cwrite(ptr,size,nitems,(STREAM *)stream);
                    181:        else
                    182: #endif
                    183:                return fwrite(ptr,size,nitems,stream);
                    184: }
                    185:
1.9       noro      186: void write_char(FILE *f,unsigned char *p)
1.1       noro      187: {
                    188:        gen_fwrite(p,sizeof(unsigned char),1,f);
                    189: }
                    190:
1.9       noro      191: void write_short(FILE *f,unsigned short *p)
1.1       noro      192: {
                    193:        unsigned short t;
                    194:
                    195:        if ( little_endian && (ox_file_io || ox_need_conv) ) {
                    196:                t = htons(*p);
1.9       noro      197:                gen_fwrite((char *)&t,sizeof(unsigned short),1,f);
1.1       noro      198:        } else
1.9       noro      199:                gen_fwrite((char *)p,sizeof(unsigned short),1,f);
1.1       noro      200: }
                    201:
1.9       noro      202: void write_int(FILE *f,unsigned int *p)
1.1       noro      203: {
                    204:        unsigned int t;
                    205:
                    206:        if ( little_endian && (ox_file_io || ox_need_conv) ) {
                    207:                t = htonl(*p);
1.9       noro      208:                gen_fwrite((char *)&t,sizeof(unsigned int),1,f);
1.1       noro      209:        } else
1.9       noro      210:                gen_fwrite((char *)p,sizeof(unsigned int),1,f);
1.1       noro      211: }
                    212:
1.15      noro      213: void write_int64(FILE *f,UL *p)
                    214: {
                    215:   unsigned int t;
                    216:
                    217:        if ( little_endian && (ox_file_io || ox_need_conv) ) {
                    218:                t = htonl(((unsigned int *)p)[1]);
                    219:                gen_fwrite((char *)&t,sizeof(unsigned int),1,f);
                    220:                t = htonl(((unsigned int *)p)[0]);
                    221:                gen_fwrite((char *)&t,sizeof(unsigned int),1,f);
                    222:        } else
                    223:                gen_fwrite((char *)p,sizeof(UL),1,f);
                    224: }
                    225:
1.13      ohara     226: #if defined(DES_ENC)
1.1       noro      227: int des_encryption;
                    228: static unsigned char asir_deskey[8] = {0xc7,0xe0,0xfc,0xb5,0xc3,0xad,0x8e,0x3a};
                    229: static unsigned char deskey_string[96];
                    230:
                    231: void init_deskey()
                    232: {
                    233:        static int deskey_initialized = 0;
                    234:
                    235:        if ( !deskey_initialized ) {
                    236:                key_schedule(asir_deskey,deskey_string);
                    237:                deskey_initialized = 1;
                    238:        }
                    239: }
1.7       noro      240: #endif
1.1       noro      241:
1.9       noro      242: void write_intarray(FILE *f,unsigned int *p,int l)
1.1       noro      243: {
                    244:        int i;
                    245:        unsigned int t;
1.13      ohara     246: #if defined(DES_ENC)
1.1       noro      247:        int l2;
                    248:        unsigned int plain[2],encrypted[2];
                    249:
                    250:        if ( des_encryption ) {
                    251:                l2 = l>>1;
                    252:                for ( i = 0; i < l2; i++ ) {
                    253:                        plain[0] = *p++;
                    254:                        plain[1] = *p++;
                    255:                        des_enc(plain,deskey_string,encrypted);
                    256:                        encrypted[0] = htonl(encrypted[0]);
                    257:                        encrypted[1] = htonl(encrypted[1]);
1.9       noro      258:                        gen_fwrite((char *)encrypted,sizeof(unsigned int),2,f);
1.1       noro      259:                }
                    260:                if ( (l2<<1) < l ) {
                    261:                        plain[0] = *p;
                    262:                        plain[1] = 0;
                    263:                        des_enc(plain,deskey_string,encrypted);
                    264:                        encrypted[0] = htonl(encrypted[0]);
                    265:                        encrypted[1] = htonl(encrypted[1]);
1.9       noro      266:                        gen_fwrite((char *)encrypted,sizeof(unsigned int),2,f);
1.1       noro      267:                }
                    268:        } else
                    269: #endif
                    270:        if ( little_endian && (ox_file_io || ox_need_conv) )
                    271:                for ( i = 0; i < l; i++, p++) {
                    272:                        t = htonl(*p);
1.9       noro      273:                        gen_fwrite((char *)&t,sizeof(unsigned int),1,f);
1.1       noro      274:                }
                    275:        else
1.9       noro      276:                gen_fwrite((char *)p,sizeof(unsigned int),l,f);
1.1       noro      277: }
                    278:
1.14      ohara     279: #if SIZEOF_LONG == 8
1.18      noro      280: /* write l word (1word=4bytes) */
                    281: /* l even : low 32of p[0] | high32 of p[0] | ... */
                    282: /* l odd :  high32 of p[0] | ... */
1.9       noro      283: void write_longarray(FILE *f,unsigned long *p,int l)
1.1       noro      284: {
1.18      noro      285:   int i;
                    286:   unsigned long w;
                    287:   unsigned int hi,lo;
                    288:
                    289:   if ( l%2 ) {
                    290:     w = p[0]; hi = w>>32;
1.20    ! noro      291:        if ( little_endian && (ox_file_io || ox_need_conv) )
        !           292:       hi = htonl(hi);
1.18      noro      293:     gen_fwrite((char *)&hi,sizeof(unsigned int),1,f);
                    294:     i = 1;
                    295:   } else
                    296:     i = 0;
                    297:   l = (l+1)/2;
                    298:   for ( ; i < l; i++ ) {
                    299:     w = p[i]; hi = w>>32; lo = w&0xffffffff;
1.20    ! noro      300:        if ( little_endian && (ox_file_io || ox_need_conv) ) {
        !           301:       hi = htonl(hi); lo = htonl(lo);
        !           302:        }
1.18      noro      303:     gen_fwrite((char *)&lo,sizeof(unsigned int),1,f);
                    304:     gen_fwrite((char *)&hi,sizeof(unsigned int),1,f);
                    305:   }
1.1       noro      306: }
                    307: #endif
                    308:
1.9       noro      309: void write_double(FILE *f,double *p)
1.1       noro      310: {
                    311:        unsigned int t;
                    312:
                    313:        if ( little_endian && (ox_file_io || ox_need_conv) ) {
                    314:                t = htonl(((unsigned int *)p)[1]);
1.9       noro      315:                gen_fwrite((char *)&t,sizeof(unsigned int),1,f);
1.1       noro      316:                t = htonl(((unsigned int *)p)[0]);
1.9       noro      317:                gen_fwrite((char *)&t,sizeof(unsigned int),1,f);
1.1       noro      318:        } else
1.9       noro      319:                gen_fwrite((char *)p,sizeof(double),1,f);
1.1       noro      320: }
                    321:
1.9       noro      322: void write_string(FILE *f,unsigned char *p,int l)
1.1       noro      323: {
                    324:        gen_fwrite(p,sizeof(unsigned char),l,f);
                    325: }
                    326:
1.9       noro      327: void read_char(FILE *f,unsigned char *p)
1.1       noro      328: {
                    329:        gen_fread((char *)p,sizeof(unsigned char),1,f);
                    330: }
                    331:
1.9       noro      332: void read_short(FILE *f,unsigned short *p)
1.1       noro      333: {
1.9       noro      334:        gen_fread((char *)p,sizeof(unsigned short),1,f);
1.1       noro      335:        if ( little_endian && (ox_file_io || ox_need_conv) )
                    336:                *p = ntohs(*p);
                    337: }
                    338:
1.9       noro      339: void read_int(FILE *f,unsigned int *p)
1.1       noro      340: {
1.9       noro      341:        gen_fread((char *)p,sizeof(unsigned int),1,f);
1.1       noro      342:        if ( little_endian && (ox_file_io || ox_need_conv) )
                    343:                *p = ntohl(*p);
                    344: }
                    345:
1.15      noro      346: void read_int64(FILE *f,UL *p)
                    347: {
                    348:        unsigned int t;
                    349:
                    350:        if ( little_endian && (ox_file_io || ox_need_conv) ) {
                    351:                gen_fread((char *)&t,sizeof(unsigned int),1,f);
                    352:                ((unsigned int *)p)[1] = ntohl(t);
                    353:                gen_fread((char *)&t,sizeof(unsigned int),1,f);
                    354:                ((unsigned int *)p)[0] = ntohl(t);
                    355:        } else
                    356:                gen_fread((char *)p,sizeof(UL),1,f);
                    357: }
                    358:
1.9       noro      359: void read_intarray(FILE *f,unsigned int *p,int l)
1.1       noro      360: {
                    361:        int i;
1.13      ohara     362: #if defined(DES_ENC)
1.1       noro      363:        int l2;
                    364:        unsigned int plain[2],encrypted[2];
                    365:
                    366:        if ( des_encryption ) {
                    367:                l2 = l>>1;
                    368:                for ( i = 0; i < l2; i++ ) {
                    369:                        gen_fread((char *)encrypted,sizeof(unsigned int),2,f);
                    370:                        encrypted[0] = ntohl(encrypted[0]);
                    371:                        encrypted[1] = ntohl(encrypted[1]);
                    372:                        des_dec(encrypted,deskey_string,plain);
                    373:                        *p++ = plain[0];
                    374:                        *p++ = plain[1];
                    375:                }
                    376:                if ( (l2<<1) < l ) {
                    377:                        gen_fread((char *)encrypted,sizeof(unsigned int),2,f);
                    378:                        encrypted[0] = ntohl(encrypted[0]);
                    379:                        encrypted[1] = ntohl(encrypted[1]);
                    380:                        des_dec(encrypted,deskey_string,plain);
                    381:                        *p = plain[0];
                    382:                }
                    383:        } else
                    384: #endif
                    385:        {
1.9       noro      386:                gen_fread((char *)p,sizeof(unsigned int),l,f);
1.1       noro      387:                if ( little_endian && (ox_file_io || ox_need_conv) )
                    388:                        for ( i = 0; i < l; i++, p++ )
                    389:                                *p = ntohl(*p);
                    390:        }
                    391: }
                    392:
1.14      ohara     393: #if SIZEOF_LONG == 8
1.15      noro      394: /* read l word (1word=4bytes) */
1.17      noro      395: /* pad 0 at low 32 of p[0] if l is odd */
                    396: /* low 32 of p[0] | high32 of p[0] | ... */
1.9       noro      397: void read_longarray(FILE *f,unsigned long *p,int l)
1.1       noro      398: {
                    399:        int i;
                    400:        unsigned int hi,lo;
1.17      noro      401:     unsigned int *q;
1.1       noro      402:
1.17      noro      403:        q = (unsigned int *)p;
                    404:        if ( l%2 ) {
                    405:          gen_fread((char *)&hi,sizeof(unsigned int),1,f);
1.20    ! noro      406:          if ( little_endian && (ox_file_io || ox_need_conv) )
        !           407:            hi = ntohl(hi);
1.17      noro      408:          *p = (((unsigned long)hi)<<32);
                    409:          p++; l--;
                    410:        }
                    411:        for ( i = 0; i < l; i += 2, p++ ) {
                    412:                gen_fread((char *)&lo,sizeof(unsigned int),1,f);
                    413:                gen_fread((char *)&hi,sizeof(unsigned int),1,f);
1.20    ! noro      414:            if ( little_endian && (ox_file_io || ox_need_conv) ) {
        !           415:              hi = ntohl(hi); lo = ntohl(lo);
        !           416:                }
1.17      noro      417:                *p = (((unsigned long)hi)<<32)|((unsigned long)lo);
                    418:        }
1.1       noro      419: }
                    420: #endif
                    421:
1.9       noro      422: void read_string(FILE *f,unsigned char *p,int l)
1.1       noro      423: {
                    424:        gen_fread((char *)p,sizeof(unsigned char),l,f);
                    425: }
                    426:
1.9       noro      427: void read_double(FILE *f,double *p)
1.1       noro      428: {
                    429:        unsigned int t;
                    430:
                    431:        if ( little_endian && (ox_file_io || ox_need_conv) ) {
                    432:                gen_fread((char *)&t,sizeof(unsigned int),1,f);
                    433:                ((unsigned int *)p)[1] = ntohl(t);
                    434:                gen_fread((char *)&t,sizeof(unsigned int),1,f);
                    435:                ((unsigned int *)p)[0] = ntohl(t);
                    436:        } else
                    437:                gen_fread((char *)p,sizeof(double),1,f);
                    438: }

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