10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 21*132Srobinson */ 22*132Srobinson 23*132Srobinson /* 24*132Srobinson * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 250Sstevel@tonic-gate * Use is subject to license terms. 260Sstevel@tonic-gate */ 270Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 280Sstevel@tonic-gate /* All Rights Reserved */ 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 310Sstevel@tonic-gate * 4.3 BSD under license from the Regents of the University of 320Sstevel@tonic-gate * California. 330Sstevel@tonic-gate */ 340Sstevel@tonic-gate 350Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 360Sstevel@tonic-gate 370Sstevel@tonic-gate /* 380Sstevel@tonic-gate * xdr_array.c, Generic XDR routines impelmentation. 390Sstevel@tonic-gate * 400Sstevel@tonic-gate * These are the "non-trivial" xdr primitives used to serialize and de-serialize 410Sstevel@tonic-gate * arrays. See xdr.h for more info on the interface to xdr. 420Sstevel@tonic-gate */ 430Sstevel@tonic-gate 440Sstevel@tonic-gate #include <sys/types.h> 450Sstevel@tonic-gate #include <syslog.h> 460Sstevel@tonic-gate #include <stdio.h> 470Sstevel@tonic-gate #include <stdlib.h> 480Sstevel@tonic-gate 490Sstevel@tonic-gate #include <rpc/types.h> 500Sstevel@tonic-gate #include <rpc/xdr.h> 510Sstevel@tonic-gate #include <memory.h> 520Sstevel@tonic-gate 530Sstevel@tonic-gate #define LASTUNSIGNED ((uint_t)0-1) 540Sstevel@tonic-gate 550Sstevel@tonic-gate char mem_err_msg_arr[] = "xdr_array: out of memory"; 56*132Srobinson 570Sstevel@tonic-gate /* 580Sstevel@tonic-gate * XDR an array of arbitrary elements 590Sstevel@tonic-gate * *addrp is a pointer to the array, *sizep is the number of elements. 600Sstevel@tonic-gate * If *addrp is NULL (*sizep * elsize) bytes are allocated. 610Sstevel@tonic-gate * elsize is the size (in bytes) of each element, and elproc is the 620Sstevel@tonic-gate * xdr procedure to call to handle each element of the array. 630Sstevel@tonic-gate */ 640Sstevel@tonic-gate bool_t 65*132Srobinson xdr_array(XDR *xdrs, caddr_t *addrp, uint_t *sizep, const uint_t maxsize, 66*132Srobinson const uint_t elsize, const xdrproc_t elproc) 670Sstevel@tonic-gate { 68*132Srobinson uint_t i; 69*132Srobinson caddr_t target = *addrp; 70*132Srobinson uint_t c; /* the actual element count */ 71*132Srobinson bool_t stat = TRUE; 72*132Srobinson uint_t nodesize; 730Sstevel@tonic-gate 740Sstevel@tonic-gate /* like strings, arrays are really counted arrays */ 75*132Srobinson if (!xdr_u_int(xdrs, sizep)) 760Sstevel@tonic-gate return (FALSE); 770Sstevel@tonic-gate c = *sizep; 780Sstevel@tonic-gate if ((c > maxsize || LASTUNSIGNED / elsize < c) && 79*132Srobinson xdrs->x_op != XDR_FREE) 800Sstevel@tonic-gate return (FALSE); 810Sstevel@tonic-gate nodesize = c * elsize; 820Sstevel@tonic-gate 830Sstevel@tonic-gate /* 840Sstevel@tonic-gate * if we are deserializing, we may need to allocate an array. 850Sstevel@tonic-gate * We also save time by checking for a null array if we are freeing. 860Sstevel@tonic-gate */ 870Sstevel@tonic-gate if (target == NULL) 880Sstevel@tonic-gate switch (xdrs->x_op) { 890Sstevel@tonic-gate case XDR_DECODE: 90*132Srobinson if (c == 0) 910Sstevel@tonic-gate return (TRUE); 92*132Srobinson *addrp = target = malloc(nodesize); 930Sstevel@tonic-gate if (target == NULL) { 940Sstevel@tonic-gate (void) syslog(LOG_ERR, mem_err_msg_arr); 950Sstevel@tonic-gate return (FALSE); 960Sstevel@tonic-gate } 970Sstevel@tonic-gate (void) memset(target, 0, nodesize); 980Sstevel@tonic-gate break; 990Sstevel@tonic-gate 1000Sstevel@tonic-gate case XDR_FREE: 1010Sstevel@tonic-gate return (TRUE); 1020Sstevel@tonic-gate } 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate /* 1050Sstevel@tonic-gate * now we xdr each element of array 1060Sstevel@tonic-gate */ 1070Sstevel@tonic-gate for (i = 0; (i < c) && stat; i++) { 1080Sstevel@tonic-gate stat = (*elproc)(xdrs, target); 1090Sstevel@tonic-gate target += elsize; 1100Sstevel@tonic-gate } 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate /* 1130Sstevel@tonic-gate * the array may need freeing 1140Sstevel@tonic-gate */ 1150Sstevel@tonic-gate if (xdrs->x_op == XDR_FREE) { 116*132Srobinson free(*addrp); 1170Sstevel@tonic-gate *addrp = NULL; 1180Sstevel@tonic-gate } 1190Sstevel@tonic-gate return (stat); 1200Sstevel@tonic-gate } 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate /* 1230Sstevel@tonic-gate * xdr_vector(): 1240Sstevel@tonic-gate * 1250Sstevel@tonic-gate * XDR a fixed length array. Unlike variable-length arrays, 1260Sstevel@tonic-gate * the storage of fixed length arrays is static and unfreeable. 1270Sstevel@tonic-gate * > basep: base of the array 1280Sstevel@tonic-gate * > size: size of the array 1290Sstevel@tonic-gate * > elemsize: size of each element 1300Sstevel@tonic-gate * > xdr_elem: routine to XDR each element 1310Sstevel@tonic-gate */ 1320Sstevel@tonic-gate bool_t 133*132Srobinson xdr_vector(XDR *xdrs, char *basep, const uint_t nelem, 134*132Srobinson const uint_t elemsize, const xdrproc_t xdr_elem) 1350Sstevel@tonic-gate { 136*132Srobinson uint_t i; 137*132Srobinson char *elptr; 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate elptr = basep; 1400Sstevel@tonic-gate for (i = 0; i < nelem; i++) { 141*132Srobinson if (!(*xdr_elem)(xdrs, elptr, LASTUNSIGNED)) 1420Sstevel@tonic-gate return (FALSE); 1430Sstevel@tonic-gate elptr += elemsize; 1440Sstevel@tonic-gate } 1450Sstevel@tonic-gate return (TRUE); 1460Sstevel@tonic-gate } 147