17d0a5a39SBill Paul /* 27d0a5a39SBill Paul * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 37d0a5a39SBill Paul * unrestricted use provided that this legend is included on all tape 47d0a5a39SBill Paul * media and as a part of the software program in whole or part. Users 57d0a5a39SBill Paul * may copy or modify Sun RPC without charge, but are not authorized 67d0a5a39SBill Paul * to license or distribute it to anyone else except as part of a product or 77d0a5a39SBill Paul * program developed by the user. 87d0a5a39SBill Paul * 97d0a5a39SBill Paul * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 107d0a5a39SBill Paul * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 117d0a5a39SBill Paul * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 127d0a5a39SBill Paul * 137d0a5a39SBill Paul * Sun RPC is provided with no support and without any obligation on the 147d0a5a39SBill Paul * part of Sun Microsystems, Inc. to assist in its use, correction, 157d0a5a39SBill Paul * modification or enhancement. 167d0a5a39SBill Paul * 177d0a5a39SBill Paul * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 187d0a5a39SBill Paul * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 197d0a5a39SBill Paul * OR ANY PART THEREOF. 207d0a5a39SBill Paul * 217d0a5a39SBill Paul * In no event will Sun Microsystems, Inc. be liable for any lost revenue 227d0a5a39SBill Paul * or profits or other special, indirect and consequential damages, even if 237d0a5a39SBill Paul * Sun has been advised of the possibility of such damages. 247d0a5a39SBill Paul * 257d0a5a39SBill Paul * Sun Microsystems, Inc. 267d0a5a39SBill Paul * 2550 Garcia Avenue 277d0a5a39SBill Paul * Mountain View, California 94043 287d0a5a39SBill Paul */ 297d0a5a39SBill Paul /* 307d0a5a39SBill Paul * xdr_sizeof.c 317d0a5a39SBill Paul * 327d0a5a39SBill Paul * Copyright 1990 Sun Microsystems, Inc. 337d0a5a39SBill Paul * 347d0a5a39SBill Paul * General purpose routine to see how much space something will use 357d0a5a39SBill Paul * when serialized using XDR. 367d0a5a39SBill Paul */ 377d0a5a39SBill Paul 387d0a5a39SBill Paul #include <rpc/types.h> 397d0a5a39SBill Paul #include <rpc/xdr.h> 407d0a5a39SBill Paul #include <sys/types.h> 417d0a5a39SBill Paul #include <stdlib.h> 427d0a5a39SBill Paul 437d0a5a39SBill Paul /* ARGSUSED */ 447d0a5a39SBill Paul static bool_t 457d0a5a39SBill Paul x_putlong(xdrs, longp) 467d0a5a39SBill Paul XDR *xdrs; 477d0a5a39SBill Paul long *longp; 487d0a5a39SBill Paul { 497d0a5a39SBill Paul xdrs->x_handy += BYTES_PER_XDR_UNIT; 507d0a5a39SBill Paul return (TRUE); 517d0a5a39SBill Paul } 527d0a5a39SBill Paul 537d0a5a39SBill Paul /* ARGSUSED */ 547d0a5a39SBill Paul static bool_t 557d0a5a39SBill Paul x_putbytes(xdrs, bp, len) 567d0a5a39SBill Paul XDR *xdrs; 577d0a5a39SBill Paul char *bp; 587d0a5a39SBill Paul int len; 597d0a5a39SBill Paul { 607d0a5a39SBill Paul xdrs->x_handy += len; 617d0a5a39SBill Paul return (TRUE); 627d0a5a39SBill Paul } 637d0a5a39SBill Paul 647d0a5a39SBill Paul static u_int 657d0a5a39SBill Paul x_getpostn(xdrs) 667d0a5a39SBill Paul XDR *xdrs; 677d0a5a39SBill Paul { 687d0a5a39SBill Paul return (xdrs->x_handy); 697d0a5a39SBill Paul } 707d0a5a39SBill Paul 717d0a5a39SBill Paul /* ARGSUSED */ 727d0a5a39SBill Paul static bool_t 737d0a5a39SBill Paul x_setpostn(xdrs, pos) 747d0a5a39SBill Paul XDR *xdrs; 757d0a5a39SBill Paul u_int pos; 767d0a5a39SBill Paul { 777d0a5a39SBill Paul /* This is not allowed */ 787d0a5a39SBill Paul return (FALSE); 797d0a5a39SBill Paul } 807d0a5a39SBill Paul 817d0a5a39SBill Paul static int32_t * 827d0a5a39SBill Paul x_inline(xdrs, len) 837d0a5a39SBill Paul XDR *xdrs; 847d0a5a39SBill Paul int len; 857d0a5a39SBill Paul { 867d0a5a39SBill Paul if (len == 0) { 877d0a5a39SBill Paul return (NULL); 887d0a5a39SBill Paul } 897d0a5a39SBill Paul if (xdrs->x_op != XDR_ENCODE) { 907d0a5a39SBill Paul return (NULL); 917d0a5a39SBill Paul } 927d0a5a39SBill Paul if (len < (int) xdrs->x_base) { 937d0a5a39SBill Paul /* x_private was already allocated */ 947d0a5a39SBill Paul xdrs->x_handy += len; 957d0a5a39SBill Paul return ((int32_t *) xdrs->x_private); 967d0a5a39SBill Paul } else { 977d0a5a39SBill Paul /* Free the earlier space and allocate new area */ 987d0a5a39SBill Paul if (xdrs->x_private) 997d0a5a39SBill Paul free(xdrs->x_private); 1007d0a5a39SBill Paul if ((xdrs->x_private = (caddr_t) malloc(len)) == NULL) { 1017d0a5a39SBill Paul xdrs->x_base = 0; 1027d0a5a39SBill Paul return (NULL); 1037d0a5a39SBill Paul } 1047d0a5a39SBill Paul xdrs->x_base = (caddr_t) len; 1057d0a5a39SBill Paul xdrs->x_handy += len; 1067d0a5a39SBill Paul return ((int32_t *) xdrs->x_private); 1077d0a5a39SBill Paul } 1087d0a5a39SBill Paul } 1097d0a5a39SBill Paul 1107d0a5a39SBill Paul static int 1117d0a5a39SBill Paul harmless() 1127d0a5a39SBill Paul { 1137d0a5a39SBill Paul /* Always return FALSE/NULL, as the case may be */ 1147d0a5a39SBill Paul return (0); 1157d0a5a39SBill Paul } 1167d0a5a39SBill Paul 1177d0a5a39SBill Paul static void 1187d0a5a39SBill Paul x_destroy(xdrs) 1197d0a5a39SBill Paul XDR *xdrs; 1207d0a5a39SBill Paul { 1217d0a5a39SBill Paul xdrs->x_handy = 0; 1227d0a5a39SBill Paul xdrs->x_base = 0; 1237d0a5a39SBill Paul if (xdrs->x_private) { 1247d0a5a39SBill Paul free(xdrs->x_private); 1257d0a5a39SBill Paul xdrs->x_private = NULL; 1267d0a5a39SBill Paul } 1277d0a5a39SBill Paul return; 1287d0a5a39SBill Paul } 1297d0a5a39SBill Paul 1307d0a5a39SBill Paul unsigned long 1317d0a5a39SBill Paul xdr_sizeof(func, data) 1327d0a5a39SBill Paul xdrproc_t func; 1337d0a5a39SBill Paul void *data; 1347d0a5a39SBill Paul { 1357d0a5a39SBill Paul XDR x; 1367d0a5a39SBill Paul struct xdr_ops ops; 1377d0a5a39SBill Paul bool_t stat; 1387d0a5a39SBill Paul /* to stop ANSI-C compiler from complaining */ 1397d0a5a39SBill Paul typedef bool_t (* dummyfunc1)(XDR *, long *); 1407d0a5a39SBill Paul typedef bool_t (* dummyfunc2)(XDR *, caddr_t, u_int); 1417d0a5a39SBill Paul 1427d0a5a39SBill Paul ops.x_putlong = x_putlong; 1437d0a5a39SBill Paul ops.x_putbytes = x_putbytes; 1447d0a5a39SBill Paul ops.x_inline = x_inline; 1457d0a5a39SBill Paul ops.x_getpostn = x_getpostn; 1467d0a5a39SBill Paul ops.x_setpostn = x_setpostn; 1477d0a5a39SBill Paul ops.x_destroy = x_destroy; 1487d0a5a39SBill Paul 1497d0a5a39SBill Paul /* the other harmless ones */ 1507d0a5a39SBill Paul ops.x_getlong = (dummyfunc1) harmless; 1517d0a5a39SBill Paul ops.x_getbytes = (dummyfunc2) harmless; 1527d0a5a39SBill Paul 1537d0a5a39SBill Paul x.x_op = XDR_ENCODE; 1547d0a5a39SBill Paul x.x_ops = &ops; 1557d0a5a39SBill Paul x.x_handy = 0; 1567d0a5a39SBill Paul x.x_private = (caddr_t) NULL; 1577d0a5a39SBill Paul x.x_base = (caddr_t) 0; 1587d0a5a39SBill Paul 1597d0a5a39SBill Paul stat = func(&x, data); 1607d0a5a39SBill Paul if (x.x_private) 1617d0a5a39SBill Paul free(x.x_private); 1627d0a5a39SBill Paul return (stat == TRUE ? (unsigned) x.x_handy: 0); 1637d0a5a39SBill Paul } 164