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