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

  1. /*
  2.  *      $Id: xdr_stdio.c,v 4.2 1994/09/29 23:48:50 jraja Exp $
  3.  *
  4.  *      XDR implementation on standard i/o file.
  5.  *
  6.  *      Copyright © 1994 AmiTCP/IP Group,
  7.  *                       Network Solutions Development Inc.
  8.  *                       All rights reserved. 
  9.  */
  10.  
  11. /* @(#)xdr_stdio.c    2.1 88/07/29 4.0 RPCSRC */
  12. #if !defined(lint) && defined(SCCSIDS)
  13. static char sccsid[] = "@(#)xdr_stdio.c 1.16 87/08/11 Copyr 1984 Sun Micro";
  14. #endif
  15.  
  16. /*
  17.  * Copyright (C) 1984, Sun Microsystems, Inc.
  18.  *
  19.  * This set of routines implements a XDR on a stdio stream.
  20.  * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
  21.  * from the stream.
  22.  */
  23.  
  24. #include <sys/param.h>
  25. #include <rpc/types.h>
  26. #include <stdio.h>
  27. #include <rpc/xdr.h>
  28.  
  29. static bool_t    xdrstdio_getlong(XDR * xdrs, long * lp);
  30. static bool_t    xdrstdio_putlong(XDR * xdrs, long * lp);
  31. static bool_t    xdrstdio_getbytes(XDR * xdrs, caddr_t addr, u_int len);
  32. static bool_t    xdrstdio_putbytes(XDR * xdrs, caddr_t addr, u_int len);
  33. static u_int    xdrstdio_getpos(XDR * xdrs);
  34. static bool_t    xdrstdio_setpos(XDR * xdrs, u_int pos);
  35. static long *    xdrstdio_inline(XDR * xdrs, u_int len);
  36. static void    xdrstdio_destroy(XDR * xdrs);
  37.  
  38. /*
  39.  * Ops vector for stdio type XDR
  40.  */
  41. static struct xdr_ops    xdrstdio_ops = {
  42.     xdrstdio_getlong,    /* deseraialize a long int */
  43.     xdrstdio_putlong,    /* seraialize a long int */
  44.     xdrstdio_getbytes,    /* deserialize counted bytes */
  45.     xdrstdio_putbytes,    /* serialize counted bytes */
  46.     xdrstdio_getpos,    /* get offset in the stream */
  47.     xdrstdio_setpos,    /* set offset in the stream */
  48.     xdrstdio_inline,    /* prime stream for inline macros */
  49.     xdrstdio_destroy    /* destroy stream */
  50. };
  51.  
  52. /*
  53.  * AmiTCP/IP specific
  54.  */
  55. #ifdef USE_DOSIO
  56. /* dos.library file */
  57. #define FILETYPE BPTR
  58. #else
  59. /* C runtime file */
  60. #define FILETYPE FILE *
  61. #endif
  62.  
  63. /*
  64.  * Initialize a stdio xdr stream.
  65.  * Sets the xdr stream handle xdrs for use on the stream file.
  66.  * Operation flag is set to op.
  67.  */
  68. void
  69. xdrstdio_create(xdrs, file, op)
  70.     register XDR *xdrs;
  71.     FILETYPE file;
  72.     enum xdr_op op;
  73. {
  74.  
  75.     xdrs->x_op = op;
  76.     xdrs->x_ops = &xdrstdio_ops;
  77.     xdrs->x_private = (caddr_t)file;
  78.     xdrs->x_handy = 0;
  79.     xdrs->x_base = 0;
  80. }
  81.  
  82. /*
  83.  * Destroy a stdio xdr stream.
  84.  * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
  85.  */
  86. static void
  87. xdrstdio_destroy(xdrs)
  88.     register XDR *xdrs;
  89. {
  90.     (void)fflush((FILETYPE)xdrs->x_private);
  91.     /* xx should we close the file ?? */
  92. }
  93.  
  94. static bool_t
  95. xdrstdio_getlong(xdrs, lp)
  96.     XDR *xdrs;
  97.     register long *lp;
  98. {
  99.  
  100.     if (fread((caddr_t)lp, sizeof(long), 1, (FILETYPE)xdrs->x_private) != 1)
  101.         return (FALSE);
  102. #if !(defined(mc68000) || defined(_M68000))
  103.     *lp = ntohl(*lp);
  104. #endif
  105.     return (TRUE);
  106. }
  107.  
  108. static bool_t
  109. xdrstdio_putlong(xdrs, lp)
  110.     XDR *xdrs;
  111.     long *lp;
  112. {
  113.  
  114. #if !(defined(mc68000) || defined(_M68000))
  115.     long mycopy = htonl(*lp);
  116.     lp = &mycopy;
  117. #endif
  118.     if (fwrite((caddr_t)lp, sizeof(long), 1, (FILETYPE)xdrs->x_private) != 1)
  119.         return (FALSE);
  120.     return (TRUE);
  121. }
  122.  
  123. static bool_t
  124. xdrstdio_getbytes(xdrs, addr, len)
  125.     XDR *xdrs;
  126.     caddr_t addr;
  127.     u_int len;
  128. {
  129.  
  130.     if ((len != 0) && (fread(addr, (int)len, 1, (FILETYPE)xdrs->x_private) != 1))
  131.         return (FALSE);
  132.     return (TRUE);
  133. }
  134.  
  135. static bool_t
  136. xdrstdio_putbytes(xdrs, addr, len)
  137.     XDR *xdrs;
  138.     caddr_t addr;
  139.     u_int len;
  140. {
  141.  
  142.     if ((len != 0) && (fwrite(addr, (int)len, 1, (FILETYPE)xdrs->x_private) != 1))
  143.         return (FALSE);
  144.     return (TRUE);
  145. }
  146.  
  147. static u_int
  148. xdrstdio_getpos(xdrs)
  149.     XDR *xdrs;
  150. {
  151.  
  152.     return ((u_int) ftell((FILETYPE)xdrs->x_private));
  153. }
  154.  
  155. static bool_t
  156. xdrstdio_setpos(xdrs, pos) 
  157.     XDR *xdrs;
  158.     u_int pos;
  159.  
  160.     return ((fseek((FILETYPE)xdrs->x_private, (long)pos, SEEK_SET) < 0) ?
  161.         FALSE : TRUE);
  162. }
  163.  
  164. static long *
  165. xdrstdio_inline(xdrs, len)
  166.     XDR *xdrs;
  167.     u_int len;
  168. {
  169.  
  170.     /*
  171.      * Must do some work to implement this: must insure
  172.      * enough data in the underlying stdio buffer,
  173.      * that the buffer is aligned so that we can indirect through a
  174.      * long *, and stuff this pointer in xdrs->x_buf.  Doing
  175.      * a fread or fwrite to a scratch buffer would defeat
  176.      * most of the gains to be had here and require storage
  177.      * management on this buffer, so we don't do this.
  178.      */
  179.     return (NULL);
  180. }
  181.  
  182.