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

  1. /*
  2.  *      $Id: xdr_array.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_array.c    2.1 88/07/29 4.0 RPCSRC */
  12. #if !defined(lint) && defined(SCCSIDS)
  13. static char sccsid[] = "@(#)xdr_array.c 1.10 87/08/11 Copyr 1984 Sun Micro";
  14. #endif
  15.  
  16. /*
  17.  * Copyright (C) 1984, Sun Microsystems, Inc.
  18.  *
  19.  * These are the "non-trivial" xdr primitives used to serialize and de-serialize
  20.  * arrays.  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 array of arbitrary elements
  31.  * *addrp is a pointer to the array, *sizep is the number of elements.
  32.  * If addrp is NULL (*sizep * elsize) bytes are allocated.
  33.  * elsize is the size (in bytes) of each element, and elproc is the
  34.  * xdr procedure to call to handle each element of the array.
  35.  */
  36. bool_t XDRFUN
  37. xdr_array(xdrs, addrp, sizep, maxsize, elsize, elproc)
  38.     register XDR *xdrs;
  39.     caddr_t *addrp;        /* array pointer */
  40.     u_int *sizep;        /* number of elements */
  41.     u_int maxsize;        /* max numberof elements */
  42.     u_int elsize;        /* size in bytes of each element */
  43.     xdrproc_t elproc;    /* xdr routine to handle each element */
  44. {
  45.     register u_int i;
  46.     register caddr_t target = *addrp;
  47.     register u_int c;  /* the actual element count */
  48.     register bool_t stat = TRUE;
  49.     register u_int nodesize;
  50.  
  51.     /* like strings, arrays are really counted arrays */
  52.     if (! xdr_u_int(xdrs, sizep)) {
  53.         return (FALSE);
  54.     }
  55.     c = *sizep;
  56.     if ((c > maxsize) && (xdrs->x_op != XDR_FREE)) {
  57.         return (FALSE);
  58.     }
  59.     nodesize = c * elsize;
  60.  
  61.     /*
  62.      * if we are deserializing, we may need to allocate an array.
  63.      * We also save time by checking for a null array if we are freeing.
  64.      */
  65.     if (target == NULL)
  66.         switch (xdrs->x_op) {
  67.         case XDR_DECODE:
  68.             if (c == 0)
  69.                 return (TRUE);
  70.             *addrp = target = mem_alloc(nodesize);
  71.             if (target == NULL) {
  72.                 (void) fprintf(stderr, 
  73.                     "xdr_array: out of memory\n");
  74.                 return (FALSE);
  75.             }
  76.             bzero(target, nodesize);
  77.             break;
  78.  
  79.         case XDR_FREE:
  80.             return (TRUE);
  81.     }
  82.     
  83.     /*
  84.      * now we xdr each element of array
  85.      */
  86.     for (i = 0; (i < c) && stat; i++) {
  87.         stat = (*(xdr_string_t)elproc)(xdrs, target, UINT_MAX);
  88.         target += elsize;
  89.     }
  90.  
  91.     /*
  92.      * the array may need freeing
  93.      */
  94.     if (xdrs->x_op == XDR_FREE) {
  95.         mem_free(*addrp, nodesize);
  96.         *addrp = NULL;
  97.     }
  98.     return (stat);
  99. }
  100.  
  101. /*
  102.  * xdr_vector():
  103.  *
  104.  * XDR a fixed length array. Unlike variable-length arrays,
  105.  * the storage of fixed length arrays is static and unfreeable.
  106.  * > basep: base of the array
  107.  * > size: size of the array
  108.  * > elemsize: size of each element
  109.  * > xdr_elem: routine to XDR each element
  110.  */
  111. bool_t XDRFUN
  112. xdr_vector(xdrs, basep, nelem, elemsize, xdr_elem)
  113.     register XDR *xdrs;
  114.     register char *basep;
  115.     register u_int nelem;
  116.     register u_int elemsize;
  117.     register xdrproc_t xdr_elem;    
  118. {
  119.     register u_int i;
  120.     register char *elptr;
  121.  
  122.     elptr = basep;
  123.     for (i = 0; i < nelem; i++) {
  124.         if (! (*(xdr_string_t)xdr_elem)(xdrs, elptr, UINT_MAX)) {
  125.             return(FALSE);
  126.         }
  127.         elptr += elemsize;
  128.     }
  129.     return(TRUE);    
  130. }
  131.  
  132.