home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / comm / amitcp-3.0ß2.lha / AmiTCP / src / rpclib / xdr_reference.c < prev    next >
C/C++ Source or Header  |  1994-03-09  |  4KB  |  142 lines

  1. /*
  2.  * $Id: xdr_reference.c,v 1.2 1993/11/14 16:56:41 jraja Exp $
  3.  *
  4.  * $Log: xdr_reference.c,v $
  5.  * Revision 1.2  1993/11/14  16:56:41  jraja
  6.  * Fixed includes, added XDRFUN to xdr function definitions.
  7.  * Changed LASTUNSIGNED to ANSI UINT_MAX.
  8.  *
  9.  */
  10. /* @(#)xdr_reference.c    2.1 88/07/29 4.0 RPCSRC */
  11. /*
  12.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  13.  * unrestricted use provided that this legend is included on all tape
  14.  * media and as a part of the software program in whole or part.  Users
  15.  * may copy or modify Sun RPC without charge, but are not authorized
  16.  * to license or distribute it to anyone else except as part of a product or
  17.  * program developed by the user.
  18.  * 
  19.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  20.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  21.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  22.  * 
  23.  * Sun RPC is provided with no support and without any obligation on the
  24.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  25.  * modification or enhancement.
  26.  * 
  27.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  28.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  29.  * OR ANY PART THEREOF.
  30.  * 
  31.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  32.  * or profits or other special, indirect and consequential damages, even if
  33.  * Sun has been advised of the possibility of such damages.
  34.  * 
  35.  * Sun Microsystems, Inc.
  36.  * 2550 Garcia Avenue
  37.  * Mountain View, California  94043
  38.  */
  39. #if !defined(lint) && defined(SCCSIDS)
  40. static char sccsid[] = "@(#)xdr_reference.c 1.11 87/08/11 SMI";
  41. #endif
  42.  
  43. /*
  44.  * xdr_reference.c, Generic XDR routines impelmentation.
  45.  *
  46.  * Copyright (C) 1987, Sun Microsystems, Inc.
  47.  *
  48.  * These are the "non-trivial" xdr primitives used to serialize and de-serialize
  49.  * "pointers".  See xdr.h for more info on the interface to xdr.
  50.  */
  51.  
  52. #include <sys/param.h>
  53. #include <stdio.h>
  54. #include <rpc/types.h>
  55. #include <rpc/xdr.h>
  56. #include <limits.h>
  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 XDRFUN
  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 (loc == NULL)
  78.         switch (xdrs->x_op) {
  79.         case XDR_FREE:
  80.             return (TRUE);
  81.  
  82.         case XDR_DECODE:
  83.             *pp = loc = (caddr_t) mem_alloc(size);
  84.             if (loc == NULL) {
  85.                 (void) fprintf(stderr,
  86.                     "xdr_reference: out of memory\n");
  87.                 return (FALSE);
  88.             }
  89.             bzero(loc, (int)size);
  90.             break;
  91.     }
  92.  
  93.     stat = (*(xdr_string_t)proc)(xdrs, loc, UINT_MAX);
  94.  
  95.     if (xdrs->x_op == XDR_FREE) {
  96.         mem_free(loc, size);
  97.         *pp = NULL;
  98.     }
  99.     return (stat);
  100. }
  101.  
  102.  
  103. /*
  104.  * xdr_pointer():
  105.  *
  106.  * XDR a pointer to a possibly recursive data structure. This
  107.  * differs with xdr_reference in that it can serialize/deserialiaze
  108.  * trees correctly.
  109.  *
  110.  *  What's sent is actually a union:
  111.  *
  112.  *  union object_pointer switch (boolean b) {
  113.  *  case TRUE: object_data data;
  114.  *  case FALSE: void nothing;
  115.  *  }
  116.  *
  117.  * > objpp: Pointer to the pointer to the object.
  118.  * > obj_size: size of the object.
  119.  * > xdr_obj: routine to XDR an object.
  120.  *
  121.  */
  122. bool_t XDRFUN
  123. xdr_pointer(xdrs,objpp,obj_size,xdr_obj)
  124.     register XDR *xdrs;
  125.     char **objpp;
  126.     u_int obj_size;
  127.     xdrproc_t xdr_obj;
  128. {
  129.  
  130.     bool_t more_data;
  131.  
  132.     more_data = (*objpp != NULL);
  133.     if (! xdr_bool(xdrs,&more_data)) {
  134.         return (FALSE);
  135.     }
  136.     if (! more_data) {
  137.         *objpp = NULL;
  138.         return (TRUE);
  139.     }
  140.     return (xdr_reference(xdrs,objpp,obj_size,xdr_obj));
  141. }
  142.