xdr_stdio.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * xdr_stdio.c, XDR implementation on standard i/o file.
  3. *
  4. * Copyright (c) 2010, Oracle America, Inc.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following
  14. * disclaimer in the documentation and/or other materials
  15. * provided with the distribution.
  16. * * Neither the name of the "Oracle America, Inc." nor the names of its
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  27. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. * This set of routines implements a XDR on a stdio stream.
  34. * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
  35. * from the stream.
  36. */
  37. #include <rpc/types.h>
  38. #include <stdio.h>
  39. #include <rpc/xdr.h>
  40. #include <libio/iolibio.h>
  41. #include <shlib-compat.h>
  42. #define fflush(s) _IO_fflush (s)
  43. #define fread(p, m, n, s) _IO_fread (p, m, n, s)
  44. #define ftell(s) _IO_ftell (s)
  45. #define fwrite(p, m, n, s) _IO_fwrite (p, m, n, s)
  46. static bool_t xdrstdio_getlong (XDR *, long *);
  47. static bool_t xdrstdio_putlong (XDR *, const long *);
  48. static bool_t xdrstdio_getbytes (XDR *, caddr_t, u_int);
  49. static bool_t xdrstdio_putbytes (XDR *, const char *, u_int);
  50. static u_int xdrstdio_getpos (const XDR *);
  51. static bool_t xdrstdio_setpos (XDR *, u_int);
  52. static int32_t *xdrstdio_inline (XDR *, u_int);
  53. static void xdrstdio_destroy (XDR *);
  54. static bool_t xdrstdio_getint32 (XDR *, int32_t *);
  55. static bool_t xdrstdio_putint32 (XDR *, const int32_t *);
  56. /*
  57. * Ops vector for stdio type XDR
  58. */
  59. static const struct xdr_ops xdrstdio_ops =
  60. {
  61. xdrstdio_getlong, /* deserialize a long int */
  62. xdrstdio_putlong, /* serialize a long int */
  63. xdrstdio_getbytes, /* deserialize counted bytes */
  64. xdrstdio_putbytes, /* serialize counted bytes */
  65. xdrstdio_getpos, /* get offset in the stream */
  66. xdrstdio_setpos, /* set offset in the stream */
  67. xdrstdio_inline, /* prime stream for inline macros */
  68. xdrstdio_destroy, /* destroy stream */
  69. xdrstdio_getint32, /* deserialize a int */
  70. xdrstdio_putint32 /* serialize a int */
  71. };
  72. /*
  73. * Initialize a stdio xdr stream.
  74. * Sets the xdr stream handle xdrs for use on the stream file.
  75. * Operation flag is set to op.
  76. */
  77. void
  78. xdrstdio_create (XDR *xdrs, FILE *file, enum xdr_op op)
  79. {
  80. xdrs->x_op = op;
  81. /* We have to add the const since the `struct xdr_ops' in `struct XDR'
  82. is not `const'. */
  83. xdrs->x_ops = (struct xdr_ops *) &xdrstdio_ops;
  84. xdrs->x_private = (caddr_t) file;
  85. xdrs->x_handy = 0;
  86. xdrs->x_base = 0;
  87. }
  88. /*
  89. * Destroy a stdio xdr stream.
  90. * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
  91. */
  92. static void
  93. xdrstdio_destroy (XDR *xdrs)
  94. {
  95. (void) fflush ((FILE *) xdrs->x_private);
  96. /* xx should we close the file ?? */
  97. };
  98. static bool_t
  99. xdrstdio_getlong (XDR *xdrs, long *lp)
  100. {
  101. uint32_t mycopy;
  102. if (fread ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
  103. return FALSE;
  104. *lp = (long) ntohl (mycopy);
  105. return TRUE;
  106. }
  107. static bool_t
  108. xdrstdio_putlong (XDR *xdrs, const long *lp)
  109. {
  110. int32_t mycopy = htonl ((uint32_t) *lp);
  111. if (fwrite ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
  112. return FALSE;
  113. return TRUE;
  114. }
  115. static bool_t
  116. xdrstdio_getbytes (XDR *xdrs, const caddr_t addr, u_int len)
  117. {
  118. if ((len != 0) && (fread (addr, (int) len, 1,
  119. (FILE *) xdrs->x_private) != 1))
  120. return FALSE;
  121. return TRUE;
  122. }
  123. static bool_t
  124. xdrstdio_putbytes (XDR *xdrs, const char *addr, u_int len)
  125. {
  126. if ((len != 0) && (fwrite (addr, (int) len, 1,
  127. (FILE *) xdrs->x_private) != 1))
  128. return FALSE;
  129. return TRUE;
  130. }
  131. static u_int
  132. xdrstdio_getpos (const XDR *xdrs)
  133. {
  134. return (u_int) ftell ((FILE *) xdrs->x_private);
  135. }
  136. static bool_t
  137. xdrstdio_setpos (XDR *xdrs, u_int pos)
  138. {
  139. return fseek ((FILE *) xdrs->x_private, (long) pos, 0) < 0 ? FALSE : TRUE;
  140. }
  141. static int32_t *
  142. xdrstdio_inline (XDR *xdrs, u_int len)
  143. {
  144. /*
  145. * Must do some work to implement this: must insure
  146. * enough data in the underlying stdio buffer,
  147. * that the buffer is aligned so that we can indirect through a
  148. * long *, and stuff this pointer in xdrs->x_buf. Doing
  149. * a fread or fwrite to a scratch buffer would defeat
  150. * most of the gains to be had here and require storage
  151. * management on this buffer, so we don't do this.
  152. */
  153. return NULL;
  154. }
  155. static bool_t
  156. xdrstdio_getint32 (XDR *xdrs, int32_t *ip)
  157. {
  158. int32_t mycopy;
  159. if (fread ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
  160. return FALSE;
  161. *ip = ntohl (mycopy);
  162. return TRUE;
  163. }
  164. static bool_t
  165. xdrstdio_putint32 (XDR *xdrs, const int32_t *ip)
  166. {
  167. int32_t mycopy = htonl (*ip);
  168. ip = &mycopy;
  169. if (fwrite ((caddr_t) ip, 4, 1, (FILE *) xdrs->x_private) != 1)
  170. return FALSE;
  171. return TRUE;
  172. }
  173. #ifdef EXPORT_RPC_SYMBOLS
  174. libc_hidden_def (xdrstdio_create)
  175. #else
  176. libc_hidden_nolink_sunrpc (xdrstdio_create, GLIBC_2_0)
  177. #endif