home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / comm / tcp / amitcp-sdk / src / rpclib / xdr_reference.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-29  |  2.6 KB  |  113 lines

  1. /*
  2.  *      $Id: xdr_reference.c,v 4.2 1994/09/29 23:48:50 jraja Exp $
  3.  *
  4.  *      Generic XDR routines impelmentation.
  5.  *
  6.  *      Copyright © 1994 AmiTCP/IP Group,
  7.  *                       Network Solutions Development Inc.
  8.  *                       All rights reserved. 
  9.  *      
  10.  */
  11. /* @(#)xdr_reference.c    2.1 88/07/29 4.0 RPCSRC */
  12. #if !defined(lint) && defined(SCCSIDS)
  13. static char sccsid[] = "@(#)xdr_reference.c 1.11 87/08/11 SMI";
  14. #endif
  15.  
  16. /*
  17.  * Copyright (C) 1987, Sun Microsystems, Inc.
  18.  *
  19.  * These are the "non-trivial" xdr primitives used to serialize and de-serialize
  20.  * "pointers".  See xdr.h for more info on the interface to xdr.
  21.  */
  22.  
  23. #include <sys/param.h>
  24. #include <stdio.h>
  25. #include <rpc/types.h>
  26. #include <rpc/xdr.h>
  27. #include <limits.h>
  28.  
  29. /*
  30.  * XDR an indirect pointer
  31.  * xdr_reference is for recursively translating a structure that is
  32.  * referenced by a pointer inside the structure that is currently being
  33.  * translated.  pp references a pointer to storage. If *pp is null
  34.  * the  necessary storage is allocated.
  35.  * size is the sizeof the referneced structure.
  36.  * proc is the routine to handle the referenced structure.
  37.  */
  38. bool_t XDRFUN
  39. xdr_reference(xdrs, pp, size, proc)
  40.     register XDR *xdrs;
  41.     caddr_t *pp;        /* the pointer to work on */
  42.     u_int size;        /* size of the object pointed to */
  43.     xdrproc_t proc;        /* xdr routine to handle the object */
  44. {
  45.     register caddr_t loc = *pp;
  46.     register bool_t stat;
  47.  
  48.     if (loc == NULL)
  49.         switch (xdrs->x_op) {
  50.         case XDR_FREE:
  51.             return (TRUE);
  52.  
  53.         case XDR_DECODE:
  54.             *pp = loc = (caddr_t) mem_alloc(size);
  55.             if (loc == NULL) {
  56.                 (void) fprintf(stderr,
  57.                     "xdr_reference: out of memory\n");
  58.                 return (FALSE);
  59.             }
  60.             bzero(loc, (int)size);
  61.             break;
  62.     }
  63.  
  64.     stat = (*(xdr_string_t)proc)(xdrs, loc, UINT_MAX);
  65.  
  66.     if (xdrs->x_op == XDR_FREE) {
  67.         mem_free(loc, size);
  68.         *pp = NULL;
  69.     }
  70.     return (stat);
  71. }
  72.  
  73.  
  74. /*
  75.  * xdr_pointer():
  76.  *
  77.  * XDR a pointer to a possibly recursive data structure. This
  78.  * differs with xdr_reference in that it can serialize/deserialiaze
  79.  * trees correctly.
  80.  *
  81.  *  What's sent is actually a union:
  82.  *
  83.  *  union object_pointer switch (boolean b) {
  84.  *  case TRUE: object_data data;
  85.  *  case FALSE: void nothing;
  86.  *  }
  87.  *
  88.  * > objpp: Pointer to the pointer to the object.
  89.  * > obj_size: size of the object.
  90.  * > xdr_obj: routine to XDR an object.
  91.  *
  92.  */
  93. bool_t XDRFUN
  94. xdr_pointer(xdrs,objpp,obj_size,xdr_obj)
  95.     register XDR *xdrs;
  96.     char **objpp;
  97.     u_int obj_size;
  98.     xdrproc_t xdr_obj;
  99. {
  100.  
  101.     bool_t more_data;
  102.  
  103.     more_data = (*objpp != NULL);
  104.     if (! xdr_bool(xdrs,&more_data)) {
  105.         return (FALSE);
  106.     }
  107.     if (! more_data) {
  108.         *objpp = NULL;
  109.         return (TRUE);
  110.     }
  111.     return (xdr_reference(xdrs,objpp,obj_size,xdr_obj));
  112. }
  113.