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

Annotation of OpenXM_contrib2/asir2018/io/io.c, Revision 1.1

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

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