home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / rpc / xdr_ref.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-01  |  3.9 KB  |  151 lines

  1. /* @(#)xdr_reference.c    2.1 88/07/29 4.0 RPCSRC */
  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. #if !defined(lint) && defined(SCCSIDS)
  31. static char sccsid[] = "@(#)xdr_reference.c 1.11 87/08/11 SMI";
  32. #endif
  33.  
  34. /*
  35.  * xdr_reference.c, Generic XDR routines impelmentation.
  36.  *
  37.  * Copyright (C) 1987, Sun Microsystems, Inc.
  38.  *
  39.  * These are the "non-trivial" xdr primitives used to serialize and de-serialize
  40.  * "pointers".  See xdr.h for more info on the interface to xdr.
  41.  */
  42.  
  43. #include <stdio.h>
  44. #include <rpc/types.h>
  45. #include <rpc/xdr.h>
  46. #include <strings.h>
  47.  
  48. #ifdef __STDC__
  49. #include <stdlib.h>
  50. #endif
  51.  
  52. #if NLS
  53. #include "nl_types.h"
  54. #endif
  55.  
  56. #define LASTUNSIGNED    ((u_int)0-1)
  57.  
  58. /*
  59.  * XDR an indirect pointer
  60.  * xdr_reference is for recursively translating a structure that is
  61.  * referenced by a pointer inside the structure that is currently being
  62.  * translated.  pp references a pointer to storage. If *pp is null
  63.  * the  necessary storage is allocated.
  64.  * size is the sizeof the referneced structure.
  65.  * proc is the routine to handle the referenced structure.
  66.  */
  67. bool_t
  68. xdr_reference(xdrs, pp, size, proc)
  69.     register XDR *xdrs;
  70.     caddr_t *pp;        /* the pointer to work on */
  71.     u_int size;        /* size of the object pointed to */
  72.     xdrproc_t proc;        /* xdr routine to handle the object */
  73. {
  74.     register caddr_t loc = *pp;
  75.     register bool_t stat;
  76.  
  77. #if NLS
  78.     libc_nls_init();
  79. #endif
  80.     if (loc == NULL)
  81.         switch (xdrs->x_op) {
  82.         case XDR_FREE:
  83.             return (TRUE);
  84.  
  85.         case XDR_DECODE:
  86.             *pp = loc = (caddr_t) mem_alloc(size);
  87.             if (loc == NULL) {
  88.                 (void) fprintf(stderr,
  89. #if NLS
  90.                 "xdr_reference: %s\n",
  91.                 catgets(_libc_cat, RpcMiscSet,
  92.                     RpcMiscOutOfMemory, "out of memory"));
  93. #else
  94.                     "xdr_reference: out of memory\n");
  95. #endif
  96.                 return (FALSE);
  97.             }
  98.             bzero(loc, (int)size);
  99.             break;
  100.     }
  101.  
  102.     stat = (*proc)(xdrs, loc, LASTUNSIGNED);
  103.  
  104.     if (xdrs->x_op == XDR_FREE) {
  105.         mem_free(loc, size);
  106.         *pp = NULL;
  107.     }
  108.     return (stat);
  109. }
  110.  
  111.  
  112. /*
  113.  * xdr_pointer():
  114.  *
  115.  * XDR a pointer to a possibly recursive data structure. This
  116.  * differs with xdr_reference in that it can serialize/deserialiaze
  117.  * trees correctly.
  118.  *
  119.  *  What's sent is actually a union:
  120.  *
  121.  *  union object_pointer switch (boolean b) {
  122.  *  case TRUE: object_data data;
  123.  *  case FALSE: void nothing;
  124.  *  }
  125.  *
  126.  * > objpp: Pointer to the pointer to the object.
  127.  * > obj_size: size of the object.
  128.  * > xdr_obj: routine to XDR an object.
  129.  *
  130.  */
  131. bool_t
  132. xdr_pointer(xdrs,objpp,obj_size,xdr_obj)
  133.     register XDR *xdrs;
  134.     char **objpp;
  135.     u_int obj_size;
  136.     xdrproc_t xdr_obj;
  137. {
  138.  
  139.     bool_t more_data;
  140.  
  141.     more_data = (*objpp != NULL);
  142.     if (! xdr_bool(xdrs,&more_data)) {
  143.         return (FALSE);
  144.     }
  145.     if (! more_data) {
  146.         *objpp = NULL;
  147.         return (TRUE);
  148.     }
  149.     return (xdr_reference(xdrs,objpp,obj_size,xdr_obj));
  150. }
  151.