1cb868ac5SDaniel Eischen /* $NetBSD: xdr_reference.c,v 1.13 2000/01/22 22:19:18 mycroft Exp $ */ 28360efbdSAlfred Perlstein 3a204967aSHiroki Sato /*- 48a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 58a16b7a1SPedro F. Giffuni * 6a204967aSHiroki Sato * Copyright (c) 2010, Oracle America, Inc. 7eae561b3SGarrett Wollman * 8a204967aSHiroki Sato * Redistribution and use in source and binary forms, with or without 9a204967aSHiroki Sato * modification, are permitted provided that the following conditions are 10a204967aSHiroki Sato * met: 11eae561b3SGarrett Wollman * 12a204967aSHiroki Sato * * Redistributions of source code must retain the above copyright 13a204967aSHiroki Sato * notice, this list of conditions and the following disclaimer. 14a204967aSHiroki Sato * * Redistributions in binary form must reproduce the above 15a204967aSHiroki Sato * copyright notice, this list of conditions and the following 16a204967aSHiroki Sato * disclaimer in the documentation and/or other materials 17a204967aSHiroki Sato * provided with the distribution. 18a204967aSHiroki Sato * * Neither the name of the "Oracle America, Inc." nor the names of its 19a204967aSHiroki Sato * contributors may be used to endorse or promote products derived 20a204967aSHiroki Sato * from this software without specific prior written permission. 21eae561b3SGarrett Wollman * 22a204967aSHiroki Sato * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23a204967aSHiroki Sato * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24a204967aSHiroki Sato * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25a204967aSHiroki Sato * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26a204967aSHiroki Sato * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27a204967aSHiroki Sato * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28a204967aSHiroki Sato * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 29a204967aSHiroki Sato * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30a204967aSHiroki Sato * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31a204967aSHiroki Sato * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32a204967aSHiroki Sato * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33a204967aSHiroki Sato * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34eae561b3SGarrett Wollman */ 35eae561b3SGarrett Wollman 36eae561b3SGarrett Wollman /* 3725126300SPedro F. Giffuni * xdr_reference.c, Generic XDR routines implementation. 38eae561b3SGarrett Wollman * 39eae561b3SGarrett Wollman * These are the "non-trivial" xdr primitives used to serialize and de-serialize 40eae561b3SGarrett Wollman * "pointers". See xdr.h for more info on the interface to xdr. 41eae561b3SGarrett Wollman */ 42eae561b3SGarrett Wollman 438360efbdSAlfred Perlstein #include "namespace.h" 448360efbdSAlfred Perlstein #include <err.h> 45eae561b3SGarrett Wollman #include <stdio.h> 4629285d6cSPoul-Henning Kamp #include <stdlib.h> 4729285d6cSPoul-Henning Kamp #include <string.h> 488360efbdSAlfred Perlstein 49eae561b3SGarrett Wollman #include <rpc/types.h> 50eae561b3SGarrett Wollman #include <rpc/xdr.h> 518360efbdSAlfred Perlstein #include "libc_private.h" 52eae561b3SGarrett Wollman 53eae561b3SGarrett Wollman /* 54eae561b3SGarrett Wollman * XDR an indirect pointer 55eae561b3SGarrett Wollman * xdr_reference is for recursively translating a structure that is 56eae561b3SGarrett Wollman * referenced by a pointer inside the structure that is currently being 57eae561b3SGarrett Wollman * translated. pp references a pointer to storage. If *pp is null 58eae561b3SGarrett Wollman * the necessary storage is allocated. 59*bdf5675aSrilysh * size is the sizeof the referenced structure. 60eae561b3SGarrett Wollman * proc is the routine to handle the referenced structure. 61eae561b3SGarrett Wollman */ 62eae561b3SGarrett Wollman bool_t 63d660d38dSCraig Rodrigues xdr_reference(XDR *xdrs, caddr_t *pp, u_int size, xdrproc_t proc) 64d660d38dSCraig Rodrigues /* 65d660d38dSCraig Rodrigues * XDR *xdrs; 66d660d38dSCraig Rodrigues * caddr_t *pp; // the pointer to work on 67d660d38dSCraig Rodrigues * u_int size; // size of the object pointed to 68d660d38dSCraig Rodrigues * xdrproc_t proc; // xdr routine to handle the object 69d660d38dSCraig Rodrigues */ 70eae561b3SGarrett Wollman { 718360efbdSAlfred Perlstein caddr_t loc = *pp; 728360efbdSAlfred Perlstein bool_t stat; 73eae561b3SGarrett Wollman 74eae561b3SGarrett Wollman if (loc == NULL) 75eae561b3SGarrett Wollman switch (xdrs->x_op) { 76eae561b3SGarrett Wollman case XDR_FREE: 77eae561b3SGarrett Wollman return (TRUE); 78eae561b3SGarrett Wollman 79eae561b3SGarrett Wollman case XDR_DECODE: 80eae561b3SGarrett Wollman *pp = loc = (caddr_t) mem_alloc(size); 81eae561b3SGarrett Wollman if (loc == NULL) { 828360efbdSAlfred Perlstein warnx("xdr_reference: out of memory"); 83eae561b3SGarrett Wollman return (FALSE); 84eae561b3SGarrett Wollman } 858360efbdSAlfred Perlstein memset(loc, 0, size); 868360efbdSAlfred Perlstein break; 878360efbdSAlfred Perlstein 888360efbdSAlfred Perlstein case XDR_ENCODE: 89eae561b3SGarrett Wollman break; 90eae561b3SGarrett Wollman } 91eae561b3SGarrett Wollman 928360efbdSAlfred Perlstein stat = (*proc)(xdrs, loc); 93eae561b3SGarrett Wollman 94eae561b3SGarrett Wollman if (xdrs->x_op == XDR_FREE) { 95eae561b3SGarrett Wollman mem_free(loc, size); 96eae561b3SGarrett Wollman *pp = NULL; 97eae561b3SGarrett Wollman } 98eae561b3SGarrett Wollman return (stat); 99eae561b3SGarrett Wollman } 100eae561b3SGarrett Wollman 101eae561b3SGarrett Wollman 102eae561b3SGarrett Wollman /* 103eae561b3SGarrett Wollman * xdr_pointer(): 104eae561b3SGarrett Wollman * 105eae561b3SGarrett Wollman * XDR a pointer to a possibly recursive data structure. This 106eae561b3SGarrett Wollman * differs with xdr_reference in that it can serialize/deserialiaze 107eae561b3SGarrett Wollman * trees correctly. 108eae561b3SGarrett Wollman * 109eae561b3SGarrett Wollman * What's sent is actually a union: 110eae561b3SGarrett Wollman * 111eae561b3SGarrett Wollman * union object_pointer switch (boolean b) { 112eae561b3SGarrett Wollman * case TRUE: object_data data; 113eae561b3SGarrett Wollman * case FALSE: void nothing; 114eae561b3SGarrett Wollman * } 115eae561b3SGarrett Wollman * 116eae561b3SGarrett Wollman * > objpp: Pointer to the pointer to the object. 117eae561b3SGarrett Wollman * > obj_size: size of the object. 118eae561b3SGarrett Wollman * > xdr_obj: routine to XDR an object. 119eae561b3SGarrett Wollman * 120eae561b3SGarrett Wollman */ 121eae561b3SGarrett Wollman bool_t 122d660d38dSCraig Rodrigues xdr_pointer(XDR *xdrs, char **objpp, u_int obj_size, xdrproc_t xdr_obj) 123eae561b3SGarrett Wollman { 124eae561b3SGarrett Wollman 125eae561b3SGarrett Wollman bool_t more_data; 126eae561b3SGarrett Wollman 127eae561b3SGarrett Wollman more_data = (*objpp != NULL); 128eae561b3SGarrett Wollman if (! xdr_bool(xdrs,&more_data)) { 129eae561b3SGarrett Wollman return (FALSE); 130eae561b3SGarrett Wollman } 131eae561b3SGarrett Wollman if (! more_data) { 132eae561b3SGarrett Wollman *objpp = NULL; 133eae561b3SGarrett Wollman return (TRUE); 134eae561b3SGarrett Wollman } 135eae561b3SGarrett Wollman return (xdr_reference(xdrs,objpp,obj_size,xdr_obj)); 136eae561b3SGarrett Wollman } 137