17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0a701b1eSRobert Gordon * Common Development and Distribution License (the "License").
6*0a701b1eSRobert Gordon * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*0a701b1eSRobert Gordon * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /* All Rights Reserved */
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD
317c478bd9Sstevel@tonic-gate * under license from the Regents of the University of California.
327c478bd9Sstevel@tonic-gate */
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate * xdr_array.c, Generic XDR routines impelmentation.
367c478bd9Sstevel@tonic-gate * These are the "non-trivial" xdr primitives used to serialize and de-serialize
377c478bd9Sstevel@tonic-gate * arrays. See xdr.h for more info on the interface to xdr.
387c478bd9Sstevel@tonic-gate */
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate #include <sys/param.h>
417c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate #include <sys/systm.h>
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate #include <rpc/types.h>
467c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate #define LASTUNSIGNED ((uint_t)0-1)
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate * XDR an array of arbitrary elements
527c478bd9Sstevel@tonic-gate * *addrp is a pointer to the array, *sizep is the number of elements.
537c478bd9Sstevel@tonic-gate * If addrp is NULL (*sizep * elsize) bytes are allocated.
547c478bd9Sstevel@tonic-gate * elsize is the size (in bytes) of each element, and elproc is the
557c478bd9Sstevel@tonic-gate * xdr procedure to call to handle each element of the array.
567c478bd9Sstevel@tonic-gate */
577c478bd9Sstevel@tonic-gate bool_t
xdr_array(XDR * xdrs,caddr_t * addrp,uint_t * sizep,const uint_t maxsize,const uint_t elsize,const xdrproc_t elproc)587c478bd9Sstevel@tonic-gate xdr_array(XDR *xdrs, caddr_t *addrp, uint_t *sizep, const uint_t maxsize,
597c478bd9Sstevel@tonic-gate const uint_t elsize, const xdrproc_t elproc)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate uint_t i;
627c478bd9Sstevel@tonic-gate caddr_t target = *addrp;
637c478bd9Sstevel@tonic-gate uint_t c; /* the actual element count */
647c478bd9Sstevel@tonic-gate bool_t stat = TRUE;
657c478bd9Sstevel@tonic-gate uint_t nodesize;
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate /* like strings, arrays are really counted arrays */
687c478bd9Sstevel@tonic-gate if (!xdr_u_int(xdrs, sizep)) {
697c478bd9Sstevel@tonic-gate return (FALSE);
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate c = *sizep;
727c478bd9Sstevel@tonic-gate if ((c > maxsize || LASTUNSIGNED / elsize < c) &&
737c478bd9Sstevel@tonic-gate xdrs->x_op != XDR_FREE) {
747c478bd9Sstevel@tonic-gate return (FALSE);
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate nodesize = c * elsize;
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate * if we are deserializing, we may need to allocate an array.
807c478bd9Sstevel@tonic-gate * We also save time by checking for a null array if we are freeing.
817c478bd9Sstevel@tonic-gate */
827c478bd9Sstevel@tonic-gate if (target == NULL)
837c478bd9Sstevel@tonic-gate switch (xdrs->x_op) {
847c478bd9Sstevel@tonic-gate case XDR_DECODE:
857c478bd9Sstevel@tonic-gate if (c == 0)
867c478bd9Sstevel@tonic-gate return (TRUE);
877c478bd9Sstevel@tonic-gate *addrp = target = (char *)mem_alloc(nodesize);
887c478bd9Sstevel@tonic-gate bzero(target, nodesize);
897c478bd9Sstevel@tonic-gate break;
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate case XDR_FREE:
927c478bd9Sstevel@tonic-gate return (TRUE);
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate case XDR_ENCODE:
957c478bd9Sstevel@tonic-gate break;
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate * now we xdr each element of array
1007c478bd9Sstevel@tonic-gate */
1017c478bd9Sstevel@tonic-gate for (i = 0; (i < c) && stat; i++) {
1027c478bd9Sstevel@tonic-gate stat = (*elproc)(xdrs, target, LASTUNSIGNED);
1037c478bd9Sstevel@tonic-gate target += elsize;
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate * the array may need freeing
1087c478bd9Sstevel@tonic-gate */
1097c478bd9Sstevel@tonic-gate if (xdrs->x_op == XDR_FREE) {
1107c478bd9Sstevel@tonic-gate mem_free(*addrp, nodesize);
1117c478bd9Sstevel@tonic-gate *addrp = NULL;
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate return (stat);
1147c478bd9Sstevel@tonic-gate }
115