Back to index

glibc  2.9
xdr_ref.c
Go to the documentation of this file.
00001 /* @(#)xdr_reference.c      2.1 88/07/29 4.0 RPCSRC */
00002 /*
00003  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
00004  * unrestricted use provided that this legend is included on all tape
00005  * media and as a part of the software program in whole or part.  Users
00006  * may copy or modify Sun RPC without charge, but are not authorized
00007  * to license or distribute it to anyone else except as part of a product or
00008  * program developed by the user.
00009  *
00010  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
00011  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
00012  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
00013  *
00014  * Sun RPC is provided with no support and without any obligation on the
00015  * part of Sun Microsystems, Inc. to assist in its use, correction,
00016  * modification or enhancement.
00017  *
00018  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
00019  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
00020  * OR ANY PART THEREOF.
00021  *
00022  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
00023  * or profits or other special, indirect and consequential damages, even if
00024  * Sun has been advised of the possibility of such damages.
00025  *
00026  * Sun Microsystems, Inc.
00027  * 2550 Garcia Avenue
00028  * Mountain View, California  94043
00029  */
00030 #if !defined(lint) && defined(SCCSIDS)
00031 static char sccsid[] = "@(#)xdr_reference.c 1.11 87/08/11 SMI";
00032 #endif
00033 
00034 /*
00035  * xdr_reference.c, Generic XDR routines implementation.
00036  *
00037  * Copyright (C) 1987, Sun Microsystems, Inc.
00038  *
00039  * These are the "non-trivial" xdr primitives used to serialize and de-serialize
00040  * "pointers".  See xdr.h for more info on the interface to xdr.
00041  */
00042 
00043 #include <stdio.h>
00044 #include <string.h>
00045 #include <rpc/types.h>
00046 #include <rpc/xdr.h>
00047 #include <libintl.h>
00048 
00049 #ifdef USE_IN_LIBIO
00050 # include <wchar.h>
00051 # include <libio/iolibio.h>
00052 #endif
00053 
00054 #define LASTUNSIGNED ((u_int)0-1)
00055 
00056 /*
00057  * XDR an indirect pointer
00058  * xdr_reference is for recursively translating a structure that is
00059  * referenced by a pointer inside the structure that is currently being
00060  * translated.  pp references a pointer to storage. If *pp is null
00061  * the  necessary storage is allocated.
00062  * size is the size of the referneced structure.
00063  * proc is the routine to handle the referenced structure.
00064  */
00065 bool_t
00066 xdr_reference (xdrs, pp, size, proc)
00067      XDR *xdrs;
00068      caddr_t *pp;           /* the pointer to work on */
00069      u_int size;            /* size of the object pointed to */
00070      xdrproc_t proc;        /* xdr routine to handle the object */
00071 {
00072   caddr_t loc = *pp;
00073   bool_t stat;
00074 
00075   if (loc == NULL)
00076     switch (xdrs->x_op)
00077       {
00078       case XDR_FREE:
00079        return TRUE;
00080 
00081       case XDR_DECODE:
00082        *pp = loc = (caddr_t) calloc (1, size);
00083        if (loc == NULL)
00084          {
00085            (void) __fxprintf (NULL, "%s: %s", __func__, _("out of memory\n"));
00086            return FALSE;
00087          }
00088        break;
00089       default:
00090        break;
00091       }
00092 
00093   stat = (*proc) (xdrs, loc, LASTUNSIGNED);
00094 
00095   if (xdrs->x_op == XDR_FREE)
00096     {
00097       mem_free (loc, size);
00098       *pp = NULL;
00099     }
00100   return stat;
00101 }
00102 INTDEF(xdr_reference)
00103 
00104 
00105 /*
00106  * xdr_pointer():
00107  *
00108  * XDR a pointer to a possibly recursive data structure. This
00109  * differs with xdr_reference in that it can serialize/deserialize
00110  * trees correctly.
00111  *
00112  *  What's sent is actually a union:
00113  *
00114  *  union object_pointer switch (boolean b) {
00115  *  case TRUE: object_data data;
00116  *  case FALSE: void nothing;
00117  *  }
00118  *
00119  * > objpp: Pointer to the pointer to the object.
00120  * > obj_size: size of the object.
00121  * > xdr_obj: routine to XDR an object.
00122  *
00123  */
00124 bool_t
00125 xdr_pointer (xdrs, objpp, obj_size, xdr_obj)
00126      XDR *xdrs;
00127      char **objpp;
00128      u_int obj_size;
00129      xdrproc_t xdr_obj;
00130 {
00131 
00132   bool_t more_data;
00133 
00134   more_data = (*objpp != NULL);
00135   if (!INTUSE(xdr_bool) (xdrs, &more_data))
00136     {
00137       return FALSE;
00138     }
00139   if (!more_data)
00140     {
00141       *objpp = NULL;
00142       return TRUE;
00143     }
00144   return INTUSE(xdr_reference) (xdrs, objpp, obj_size, xdr_obj);
00145 }