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