xdr_stdio.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* @(#)xdr_stdio.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. #define __FORCE_GLIBC
  31. #include <features.h>
  32. /*
  33. * xdr_stdio.c, XDR implementation on standard i/o file.
  34. *
  35. * Copyright (C) 1984, Sun Microsystems, Inc.
  36. *
  37. * This set of routines implements a XDR on a stdio stream.
  38. * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
  39. * from the stream.
  40. */
  41. #include <rpc/types.h>
  42. #include <stdio.h>
  43. #include <rpc/xdr.h>
  44. static bool_t xdrstdio_getlong();
  45. static bool_t xdrstdio_putlong();
  46. static bool_t xdrstdio_getbytes();
  47. static bool_t xdrstdio_putbytes();
  48. static u_int xdrstdio_getpos();
  49. static bool_t xdrstdio_setpos();
  50. static int32_t *xdrstdio_inline();
  51. static void xdrstdio_destroy();
  52. /*
  53. * Ops vector for stdio type XDR
  54. */
  55. static struct xdr_ops xdrstdio_ops = {
  56. xdrstdio_getlong, /* deseraialize a long int */
  57. xdrstdio_putlong, /* seraialize a long int */
  58. xdrstdio_getbytes, /* deserialize counted bytes */
  59. xdrstdio_putbytes, /* serialize counted bytes */
  60. xdrstdio_getpos, /* get offset in the stream */
  61. xdrstdio_setpos, /* set offset in the stream */
  62. xdrstdio_inline, /* prime stream for inline macros */
  63. xdrstdio_destroy /* destroy stream */
  64. };
  65. /*
  66. * Initialize a stdio xdr stream.
  67. * Sets the xdr stream handle xdrs for use on the stream file.
  68. * Operation flag is set to op.
  69. */
  70. void xdrstdio_create(xdrs, file, op)
  71. register XDR *xdrs;
  72. FILE *file;
  73. enum xdr_op op;
  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. * Destroy a stdio xdr stream.
  83. * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
  84. */
  85. static void xdrstdio_destroy(xdrs)
  86. register XDR *xdrs;
  87. {
  88. (void) fflush((FILE *) xdrs->x_private);
  89. /* xx should we close the file ?? */
  90. };
  91. static bool_t xdrstdio_getlong(xdrs, lp)
  92. XDR *xdrs;
  93. register long *lp;
  94. {
  95. if (fread((caddr_t) lp, sizeof(long), 1, (FILE *) xdrs->x_private) !=
  96. 1) return (FALSE);
  97. #ifndef mc68000
  98. *lp = ntohl(*lp);
  99. #endif
  100. return (TRUE);
  101. }
  102. static bool_t xdrstdio_putlong(xdrs, lp)
  103. XDR *xdrs;
  104. long *lp;
  105. {
  106. #ifndef mc68000
  107. long mycopy = htonl(*lp);
  108. lp = &mycopy;
  109. #endif
  110. if (fwrite((caddr_t) lp, sizeof(long), 1, (FILE *) xdrs->x_private) !=
  111. 1) return (FALSE);
  112. return (TRUE);
  113. }
  114. static bool_t xdrstdio_getbytes(xdrs, addr, len)
  115. XDR *xdrs;
  116. caddr_t addr;
  117. u_int len;
  118. {
  119. if ((len != 0)
  120. && (fread(addr, (int) len, 1, (FILE *) xdrs->x_private) != 1))
  121. return (FALSE);
  122. return (TRUE);
  123. }
  124. static bool_t xdrstdio_putbytes(xdrs, addr, len)
  125. XDR *xdrs;
  126. caddr_t addr;
  127. u_int len;
  128. {
  129. if ((len != 0)
  130. && (fwrite(addr, (int) len, 1, (FILE *) xdrs->x_private) != 1))
  131. return (FALSE);
  132. return (TRUE);
  133. }
  134. static u_int xdrstdio_getpos(xdrs)
  135. XDR *xdrs;
  136. {
  137. return ((u_int) ftell((FILE *) xdrs->x_private));
  138. }
  139. static bool_t xdrstdio_setpos(xdrs, pos)
  140. XDR *xdrs;
  141. u_int pos;
  142. {
  143. return ((fseek((FILE *) xdrs->x_private, (long) pos, 0) < 0) ?
  144. FALSE : TRUE);
  145. }
  146. static int32_t *xdrstdio_inline(xdrs, len)
  147. XDR *xdrs;
  148. u_int len;
  149. {
  150. /*
  151. * Must do some work to implement this: must insure
  152. * enough data in the underlying stdio buffer,
  153. * that the buffer is aligned so that we can indirect through a
  154. * long *, and stuff this pointer in xdrs->x_buf. Doing
  155. * a fread or fwrite to a scratch buffer would defeat
  156. * most of the gains to be had here and require storage
  157. * management on this buffer, so we don't do this.
  158. */
  159. return (NULL);
  160. }