Home | History | Annotate | Line # | Download | only in rpc
xdr_reference.c revision 1.15.8.2
      1  1.15.8.2  christos /*	$NetBSD: xdr_reference.c,v 1.15.8.2 2008/04/25 17:44:45 christos Exp $	*/
      2  1.15.8.2  christos 
      3  1.15.8.2  christos /*
      4  1.15.8.2  christos  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      5  1.15.8.2  christos  * unrestricted use provided that this legend is included on all tape
      6  1.15.8.2  christos  * media and as a part of the software program in whole or part.  Users
      7  1.15.8.2  christos  * may copy or modify Sun RPC without charge, but are not authorized
      8  1.15.8.2  christos  * to license or distribute it to anyone else except as part of a product or
      9  1.15.8.2  christos  * program developed by the user.
     10  1.15.8.2  christos  *
     11  1.15.8.2  christos  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     12  1.15.8.2  christos  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     13  1.15.8.2  christos  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     14  1.15.8.2  christos  *
     15  1.15.8.2  christos  * Sun RPC is provided with no support and without any obligation on the
     16  1.15.8.2  christos  * part of Sun Microsystems, Inc. to assist in its use, correction,
     17  1.15.8.2  christos  * modification or enhancement.
     18  1.15.8.2  christos  *
     19  1.15.8.2  christos  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     20  1.15.8.2  christos  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     21  1.15.8.2  christos  * OR ANY PART THEREOF.
     22  1.15.8.2  christos  *
     23  1.15.8.2  christos  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     24  1.15.8.2  christos  * or profits or other special, indirect and consequential damages, even if
     25  1.15.8.2  christos  * Sun has been advised of the possibility of such damages.
     26  1.15.8.2  christos  *
     27  1.15.8.2  christos  * Sun Microsystems, Inc.
     28  1.15.8.2  christos  * 2550 Garcia Avenue
     29  1.15.8.2  christos  * Mountain View, California  94043
     30  1.15.8.2  christos  */
     31  1.15.8.2  christos 
     32  1.15.8.2  christos #include <sys/cdefs.h>
     33  1.15.8.2  christos #if defined(LIBC_SCCS) && !defined(lint)
     34  1.15.8.2  christos #if 0
     35  1.15.8.2  christos static char *sccsid = "@(#)xdr_reference.c 1.11 87/08/11 SMI";
     36  1.15.8.2  christos static char *sccsid = "@(#)xdr_reference.c	2.1 88/07/29 4.0 RPCSRC";
     37  1.15.8.2  christos #else
     38  1.15.8.2  christos __RCSID("$NetBSD: xdr_reference.c,v 1.15.8.2 2008/04/25 17:44:45 christos Exp $");
     39  1.15.8.2  christos #endif
     40  1.15.8.2  christos #endif
     41  1.15.8.2  christos 
     42  1.15.8.2  christos /*
     43  1.15.8.2  christos  * xdr_reference.c, Generic XDR routines implementation.
     44  1.15.8.2  christos  *
     45  1.15.8.2  christos  * Copyright (C) 1987, Sun Microsystems, Inc.
     46  1.15.8.2  christos  *
     47  1.15.8.2  christos  * These are the "non-trivial" xdr primitives used to serialize and de-serialize
     48  1.15.8.2  christos  * "pointers".  See xdr.h for more info on the interface to xdr.
     49  1.15.8.2  christos  */
     50  1.15.8.2  christos 
     51  1.15.8.2  christos #include "namespace.h"
     52  1.15.8.2  christos 
     53  1.15.8.2  christos #include <err.h>
     54  1.15.8.2  christos #include <stdio.h>
     55  1.15.8.2  christos #include <stdlib.h>
     56  1.15.8.2  christos #include <string.h>
     57  1.15.8.2  christos 
     58  1.15.8.2  christos #include <rpc/types.h>
     59  1.15.8.2  christos #include <rpc/xdr.h>
     60  1.15.8.2  christos 
     61  1.15.8.2  christos #ifdef __weak_alias
     62  1.15.8.2  christos __weak_alias(xdr_pointer,_xdr_pointer)
     63  1.15.8.2  christos __weak_alias(xdr_reference,_xdr_reference)
     64  1.15.8.2  christos #endif
     65  1.15.8.2  christos 
     66  1.15.8.2  christos /*
     67  1.15.8.2  christos  * XDR an indirect pointer
     68  1.15.8.2  christos  * xdr_reference is for recursively translating a structure that is
     69  1.15.8.2  christos  * referenced by a pointer inside the structure that is currently being
     70  1.15.8.2  christos  * translated.  pp references a pointer to storage. If *pp is null
     71  1.15.8.2  christos  * the  necessary storage is allocated.
     72  1.15.8.2  christos  * size is the sizeof the referneced structure.
     73  1.15.8.2  christos  * proc is the routine to handle the referenced structure.
     74  1.15.8.2  christos  */
     75  1.15.8.2  christos bool_t
     76  1.15.8.2  christos xdr_reference(xdrs, pp, size, proc)
     77  1.15.8.2  christos 	XDR *xdrs;
     78  1.15.8.2  christos 	caddr_t *pp;		/* the pointer to work on */
     79  1.15.8.2  christos 	u_int size;		/* size of the object pointed to */
     80  1.15.8.2  christos 	xdrproc_t proc;		/* xdr routine to handle the object */
     81  1.15.8.2  christos {
     82  1.15.8.2  christos 	caddr_t loc = *pp;
     83  1.15.8.2  christos 	bool_t stat;
     84  1.15.8.2  christos 
     85  1.15.8.2  christos 	if (loc == NULL)
     86  1.15.8.2  christos 		switch (xdrs->x_op) {
     87  1.15.8.2  christos 		case XDR_FREE:
     88  1.15.8.2  christos 			return (TRUE);
     89  1.15.8.2  christos 
     90  1.15.8.2  christos 		case XDR_DECODE:
     91  1.15.8.2  christos 			*pp = loc = mem_alloc(size);
     92  1.15.8.2  christos 			if (loc == NULL) {
     93  1.15.8.2  christos 				warnx("xdr_reference: out of memory");
     94  1.15.8.2  christos 				return (FALSE);
     95  1.15.8.2  christos 			}
     96  1.15.8.2  christos 			memset(loc, 0, size);
     97  1.15.8.2  christos 			break;
     98  1.15.8.2  christos 
     99  1.15.8.2  christos 		case XDR_ENCODE:
    100  1.15.8.2  christos 			break;
    101  1.15.8.2  christos 		}
    102  1.15.8.2  christos 
    103  1.15.8.2  christos 	stat = (*proc)(xdrs, loc);
    104  1.15.8.2  christos 
    105  1.15.8.2  christos 	if (xdrs->x_op == XDR_FREE) {
    106  1.15.8.2  christos 		mem_free(loc, size);
    107  1.15.8.2  christos 		*pp = NULL;
    108  1.15.8.2  christos 	}
    109  1.15.8.2  christos 	return (stat);
    110  1.15.8.2  christos }
    111  1.15.8.2  christos 
    112  1.15.8.2  christos 
    113  1.15.8.2  christos /*
    114  1.15.8.2  christos  * xdr_pointer():
    115  1.15.8.2  christos  *
    116  1.15.8.2  christos  * XDR a pointer to a possibly recursive data structure. This
    117  1.15.8.2  christos  * differs with xdr_reference in that it can serialize/deserialiaze
    118  1.15.8.2  christos  * trees correctly.
    119  1.15.8.2  christos  *
    120  1.15.8.2  christos  *  What's sent is actually a union:
    121  1.15.8.2  christos  *
    122  1.15.8.2  christos  *  union object_pointer switch (boolean b) {
    123  1.15.8.2  christos  *  case TRUE: object_data data;
    124  1.15.8.2  christos  *  case FALSE: void nothing;
    125  1.15.8.2  christos  *  }
    126  1.15.8.2  christos  *
    127  1.15.8.2  christos  * > objpp: Pointer to the pointer to the object.
    128  1.15.8.2  christos  * > obj_size: size of the object.
    129  1.15.8.2  christos  * > xdr_obj: routine to XDR an object.
    130  1.15.8.2  christos  *
    131  1.15.8.2  christos  */
    132  1.15.8.2  christos bool_t
    133  1.15.8.2  christos xdr_pointer(xdrs,objpp,obj_size,xdr_obj)
    134  1.15.8.2  christos 	XDR *xdrs;
    135  1.15.8.2  christos 	char **objpp;
    136  1.15.8.2  christos 	u_int obj_size;
    137  1.15.8.2  christos 	xdrproc_t xdr_obj;
    138  1.15.8.2  christos {
    139  1.15.8.2  christos 
    140  1.15.8.2  christos 	bool_t more_data;
    141  1.15.8.2  christos 
    142  1.15.8.2  christos 	more_data = (*objpp != NULL);
    143  1.15.8.2  christos 	if (! xdr_bool(xdrs,&more_data)) {
    144  1.15.8.2  christos 		return (FALSE);
    145  1.15.8.2  christos 	}
    146  1.15.8.2  christos 	if (! more_data) {
    147  1.15.8.2  christos 		*objpp = NULL;
    148  1.15.8.2  christos 		return (TRUE);
    149  1.15.8.2  christos 	}
    150  1.15.8.2  christos 	return (xdr_reference(xdrs,objpp,obj_size,xdr_obj));
    151  1.15.8.2  christos }
    152