/* $NetBSD: xdr_reference.c,v 1.13 2000/01/22 22:19:18 mycroft Exp $ */12/*-3* SPDX-License-Identifier: BSD-3-Clause4*5* Copyright (c) 2010, Oracle America, Inc.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions are9* met:10*11* * Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* * Redistributions in binary form must reproduce the above14* copyright notice, this list of conditions and the following15* disclaimer in the documentation and/or other materials16* provided with the distribution.17* * Neither the name of the "Oracle America, Inc." nor the names of its18* contributors may be used to endorse or promote products derived19* from this software without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS22* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT23* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS24* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE25* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,26* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL27* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE28* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS29* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,30* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING31* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE32* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.33*/3435/*36* xdr_reference.c, Generic XDR routines implementation.37*38* These are the "non-trivial" xdr primitives used to serialize and de-serialize39* "pointers". See xdr.h for more info on the interface to xdr.40*/4142#include "namespace.h"43#include <err.h>44#include <stdio.h>45#include <stdlib.h>46#include <string.h>4748#include <rpc/types.h>49#include <rpc/xdr.h>50#include "libc_private.h"5152/*53* XDR an indirect pointer54* xdr_reference is for recursively translating a structure that is55* referenced by a pointer inside the structure that is currently being56* translated. pp references a pointer to storage. If *pp is null57* the necessary storage is allocated.58* size is the sizeof the referenced structure.59* proc is the routine to handle the referenced structure.60*/61bool_t62xdr_reference(XDR *xdrs, caddr_t *pp, u_int size, xdrproc_t proc)63/*64* XDR *xdrs;65* caddr_t *pp; // the pointer to work on66* u_int size; // size of the object pointed to67* xdrproc_t proc; // xdr routine to handle the object68*/69{70caddr_t loc = *pp;71bool_t stat;7273if (loc == NULL)74switch (xdrs->x_op) {75case XDR_FREE:76return (TRUE);7778case XDR_DECODE:79*pp = loc = (caddr_t) mem_alloc(size);80if (loc == NULL) {81warnx("xdr_reference: out of memory");82return (FALSE);83}84memset(loc, 0, size);85break;8687case XDR_ENCODE:88break;89}9091stat = (*proc)(xdrs, loc);9293if (xdrs->x_op == XDR_FREE) {94mem_free(loc, size);95*pp = NULL;96}97return (stat);98}99100101/*102* xdr_pointer():103*104* XDR a pointer to a possibly recursive data structure. This105* differs with xdr_reference in that it can serialize/deserialiaze106* trees correctly.107*108* What's sent is actually a union:109*110* union object_pointer switch (boolean b) {111* case TRUE: object_data data;112* case FALSE: void nothing;113* }114*115* > objpp: Pointer to the pointer to the object.116* > obj_size: size of the object.117* > xdr_obj: routine to XDR an object.118*119*/120bool_t121xdr_pointer(XDR *xdrs, char **objpp, u_int obj_size, xdrproc_t xdr_obj)122{123124bool_t more_data;125126more_data = (*objpp != NULL);127if (! xdr_bool(xdrs,&more_data)) {128return (FALSE);129}130if (! more_data) {131*objpp = NULL;132return (TRUE);133}134return (xdr_reference(xdrs,objpp,obj_size,xdr_obj));135}136137138