Annotation of 43BSD/contrib/xns/xnslib/misc.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * This file implements functions used by both client and servers in the
        !             3:  * XNS courier library
        !             4:  */
        !             5: 
        !             6: /*
        !             7:  $Log: misc.c,v $
        !             8:  * Revision 2.0  85/11/21  07:22:12  jqj
        !             9:  * 4.3BSD standard release
        !            10:  * 
        !            11:  * Revision 1.2  85/10/21  12:55:26  root
        !            12:  * Gould version:  added MoveLong routine.
        !            13:  * 
        !            14:  * Revision 1.4  85/03/26  06:08:42  jqj
        !            15:  * Revised public alpha-test version, released 26 March 1985
        !            16:  * 
        !            17:  * Revision 1.3  85/03/11  16:37:08  jqj
        !            18:  * Public alpha-test version, released 11 March 1985
        !            19:  * 
        !            20:  * Revision 1.2  85/01/27  07:37:27  jqj
        !            21:  * finished but undebugged version
        !            22:  * 
        !            23:  */
        !            24: 
        !            25: #ifndef lint
        !            26: static char rcsid[] = "$Header: misc.c,v 2.0 85/11/21 07:22:12 jqj Exp $";
        !            27: #endif
        !            28: 
        !            29: #include <stdio.h>
        !            30: #include "courier.h"
        !            31: 
        !            32: 
        !            33: Unspecified *
        !            34: Allocate(n)
        !            35:        LongInteger n;
        !            36: {
        !            37:        Unspecified *p;
        !            38:        extern char *malloc();
        !            39: 
        !            40:        if (n > 100000 || n < 0) {
        !            41:                fprintf(stderr,
        !            42:                        "Ridiculous request to memory allocator (%d words).\n",
        !            43:                        n);
        !            44:                exit(1);
        !            45:                /* NOTREACHED */
        !            46:        }
        !            47:        if (n == 0)
        !            48:                return (0);
        !            49:        p = (Unspecified *) malloc((unsigned) n*sizeof(Unspecified));
        !            50:        if (p == 0) {
        !            51:                fprintf(stderr, "Out of memory.\n");
        !            52:                exit(1);
        !            53:        }
        !            54:        return (p);
        !            55: }
        !            56: 
        !            57: Deallocate(p)
        !            58:        Unspecified *p;
        !            59: {
        !            60:        if (p != 0)
        !            61:                free((char *) p);
        !            62: }
        !            63: 
        !            64: 
        !            65: /*
        !            66:  * Unfortunately, Courier strings are 8 bits and frequently contain
        !            67:  * NULs ('\000').  Hence we use an encoding scheme in which '\0200'
        !            68:  * is used as a substitute for '\000'.
        !            69:  * Note that 0200 is not routinely used in Xerox character encodings.
        !            70:  * If it does happen to appear in an incoming Courier string, it will
        !            71:  * be confusing!  More importantly, there is no way to generate a
        !            72:  * Courier string containing a '\200'.
        !            73:  */
        !            74: 
        !            75: 
        !            76: int
        !            77: sizeof_String(p)
        !            78: /* the size in 16-bit words of the serialized data structure */
        !            79:        String *p;
        !            80: {
        !            81:        return ((strlen(*p)+3)/2);
        !            82: }
        !            83: 
        !            84: clear_String(p)
        !            85:        String *p;
        !            86: {
        !            87:        Deallocate((Unspecified*) *p);
        !            88:        *p = (String) 0;
        !            89:        return;
        !            90: }
        !            91: 
        !            92: int
        !            93: externalize_String(p, buf)
        !            94: /* convert to counted-sequence format, translating \0200 to \000 */
        !            95:        String *p;
        !            96:        Unspecified *buf;
        !            97: {
        !            98:        Cardinal len;
        !            99:        register int n;
        !           100:        register int ch;
        !           101:        register String from, to;
        !           102: 
        !           103:        if (*p == (String) 0) n = 0;
        !           104:        else {
        !           105:                n = strlen(*p);
        !           106:                if (n > 65535) n = 0;
        !           107:        }
        !           108:        len = n;
        !           109:        buf += externalize_Cardinal(&len, buf);
        !           110:        to = (String) buf;
        !           111:        from = *p;
        !           112:        while (--n >= 0) {
        !           113:                if ((ch = *from++) == '\0200') *to++ = '\000';
        !           114:                else *to++ = ch;
        !           115:        }
        !           116:        return ((len+3)/2);
        !           117: }
        !           118: 
        !           119: int
        !           120: internalize_String(p, buf)
        !           121: /* convert to C format, translating \000 to \0200 and terminating on count */
        !           122:        String *p;
        !           123:        Unspecified *buf;
        !           124: {
        !           125:        Cardinal len;
        !           126:        register int n, ch;
        !           127:        register String from, to;
        !           128: 
        !           129:        buf += internalize_Cardinal(&len, buf);
        !           130:        *p = to = (String) Allocate(len/2 + 1);
        !           131:        from = (String) buf;
        !           132:        for (n = len; n > 0; --n) {
        !           133:                if ((ch = *from++) == '\000') *to++ = '\0200';
        !           134:                else *to++ = ch;
        !           135:        }
        !           136:        *to = '\000';           /* tie off with a null, finally */
        !           137:        return ((len+3)/2);
        !           138: }
        !           139: 
        !           140: int
        !           141: sizeof_enumeration(p)
        !           142:        unsigned int *p;
        !           143: {
        !           144:        return(sizeof_Cardinal(p));
        !           145: }
        !           146: 
        !           147: clear_enumeration(p)
        !           148:        unsigned int *p;
        !           149: {
        !           150:        return;                         /* no-op */
        !           151: }
        !           152: 
        !           153: int
        !           154: externalize_enumeration(p, buf)
        !           155:        unsigned int *p;                /* enums are same size as int */
        !           156:        Unspecified *buf;
        !           157: {
        !           158:        Cardinal c;
        !           159:        
        !           160:        /*
        !           161:         * If this truncates, and it will on a Vax or a Sun,
        !           162:         * that's because Courier Enumerations are  defined to be
        !           163:         * "in the closed interval [0, 65535]."  So, if the value
        !           164:         * of the C enum is too big, there's nothing we can do here ...
        !           165:         * except possibly to raise an exception.
        !           166:         */
        !           167:        c = *p;
        !           168:        return(externalize_Cardinal(&c, buf));
        !           169: }
        !           170: 
        !           171: int
        !           172: internalize_enumeration(p, buf)
        !           173:        unsigned int *p;
        !           174:        Unspecified *buf;
        !           175: {
        !           176:        Cardinal c;
        !           177:        register int s;
        !           178:        
        !           179:        s = internalize_Cardinal(&c, buf);
        !           180:        *p = c;                 /* (possibly) expand from short to long */
        !           181:        
        !           182:        return(s);
        !           183: }
        !           184: 
        !           185: 
        !           186: #if sel
        !           187: int MoveLong(a, b)
        !           188:        Unspecified *a, *b;
        !           189: {
        !           190:        *b++ = *a++;
        !           191:        *b = *a;
        !           192:        return(2);
        !           193: }
        !           194: #endif

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.