Path: blob/main/crypto/krb5/src/lib/rpc/xdr_array.c
39536 views
/* @(#)xdr_array.c 2.1 88/07/29 4.0 RPCSRC */1/*2* Copyright (c) 2010, Oracle America, Inc.3*4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions are met:8*9* * Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* * Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in14* the documentation and/or other materials provided with the15* distribution.16*17* * Neither the name of the "Oracle America, Inc." nor the names of18* its contributors may be used to endorse or promote products19* derived from this software without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS22* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED23* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A24* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT25* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,26* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED27* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR28* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.32*/33#if !defined(lint) && defined(SCCSIDS)34static char sccsid[] = "@(#)xdr_array.c 1.10 87/08/11 Copyr 1984 Sun Micro";35#endif3637/*38* xdr_array.c, Generic XDR routines impelmentation.39*40* These are the "non-trivial" xdr primitives used to serialize and de-serialize41* arrays. See xdr.h for more info on the interface to xdr.42*/4344#include <stdio.h>45#include <string.h>46#include <gssrpc/types.h>47#include <gssrpc/xdr.h>4849#define LASTUNSIGNED ((u_int)0-1)505152/*53* XDR an array of arbitrary elements54* *addrp is a pointer to the array, *sizep is the number of elements.55* If addrp is NULL (*sizep * elsize) bytes are allocated.56* elsize is the size (in bytes) of each element, and elproc is the57* xdr procedure to call to handle each element of the array.58*/59bool_t60xdr_array(61XDR *xdrs,62caddr_t *addrp, /* array pointer */63u_int *sizep, /* number of elements */64u_int maxsize, /* max numberof elements */65u_int elsize, /* size in bytes of each element */66xdrproc_t elproc /* xdr routine to handle each element */67)68{69u_int i;70caddr_t target = *addrp;71u_int c; /* the actual element count */72bool_t stat = TRUE;73u_int nodesize;7475/* like strings, arrays are really counted arrays */76if (! xdr_u_int(xdrs, sizep)) {77return (FALSE);78}79c = *sizep;80if ((c > maxsize || c > LASTUNSIGNED / elsize)81&& (xdrs->x_op != XDR_FREE)) {82return (FALSE);83}84nodesize = c * elsize;8586/*87* if we are deserializing, we may need to allocate an array.88* We also save time by checking for a null array if we are freeing.89*/90if (target == NULL)91switch (xdrs->x_op) {92case XDR_DECODE:93if (c == 0)94return (TRUE);95*addrp = target = mem_alloc(nodesize);96if (target == NULL) {97(void) fprintf(stderr,98"xdr_array: out of memory\n");99return (FALSE);100}101memset(target, 0, nodesize);102break;103104case XDR_FREE:105return (TRUE);106107case XDR_ENCODE:108break;109}110111/*112* now we xdr each element of array113*/114for (i = 0; (i < c) && stat; i++) {115stat = (*elproc)(xdrs, target);116target += elsize;117}118119/*120* the array may need freeing121*/122if (xdrs->x_op == XDR_FREE) {123mem_free(*addrp, nodesize);124*addrp = NULL;125}126return (stat);127}128129/*130* xdr_vector():131*132* XDR a fixed length array. Unlike variable-length arrays,133* the storage of fixed length arrays is static and unfreeable.134* > basep: base of the array135* > size: size of the array136* > elemsize: size of each element137* > xdr_elem: routine to XDR each element138*/139bool_t140xdr_vector(141XDR *xdrs,142char *basep,143u_int nelem,144u_int elemsize,145xdrproc_t xdr_elem)146{147u_int i;148char *elptr;149150elptr = basep;151for (i = 0; i < nelem; i++) {152if (! (*xdr_elem)(xdrs, elptr)) {153return(FALSE);154}155elptr += elemsize;156}157return(TRUE);158}159160161