xdr_mem.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * xdr_mem.c, XDR implementation using memory buffers.
  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. * If you have some data to be interpreted as external data representation
  34. * or to be converted to external data representation in a memory buffer,
  35. * then this is the package for you.
  36. */
  37. #include <string.h>
  38. #include <limits.h>
  39. #include <rpc/rpc.h>
  40. #include <shlib-compat.h>
  41. static bool_t xdrmem_getlong (XDR *, long *);
  42. static bool_t xdrmem_putlong (XDR *, const long *);
  43. static bool_t xdrmem_getbytes (XDR *, caddr_t, u_int);
  44. static bool_t xdrmem_putbytes (XDR *, const char *, u_int);
  45. static u_int xdrmem_getpos (const XDR *);
  46. static bool_t xdrmem_setpos (XDR *, u_int);
  47. static int32_t *xdrmem_inline (XDR *, u_int);
  48. static void xdrmem_destroy (XDR *);
  49. static bool_t xdrmem_getint32 (XDR *, int32_t *);
  50. static bool_t xdrmem_putint32 (XDR *, const int32_t *);
  51. static const struct xdr_ops xdrmem_ops =
  52. {
  53. xdrmem_getlong,
  54. xdrmem_putlong,
  55. xdrmem_getbytes,
  56. xdrmem_putbytes,
  57. xdrmem_getpos,
  58. xdrmem_setpos,
  59. xdrmem_inline,
  60. xdrmem_destroy,
  61. xdrmem_getint32,
  62. xdrmem_putint32
  63. };
  64. /*
  65. * The procedure xdrmem_create initializes a stream descriptor for a
  66. * memory buffer.
  67. */
  68. void
  69. xdrmem_create (XDR *xdrs, const caddr_t addr, u_int size, enum xdr_op op)
  70. {
  71. xdrs->x_op = op;
  72. /* We have to add the const since the `struct xdr_ops' in `struct XDR'
  73. is not `const'. */
  74. xdrs->x_ops = (struct xdr_ops *) &xdrmem_ops;
  75. xdrs->x_private = xdrs->x_base = addr;
  76. xdrs->x_handy = size;
  77. }
  78. #ifdef EXPORT_RPC_SYMBOLS
  79. libc_hidden_def (xdrmem_create)
  80. #else
  81. libc_hidden_nolink_sunrpc (xdrmem_create, GLIBC_2_0)
  82. #endif
  83. /*
  84. * Nothing needs to be done for the memory case. The argument is clearly
  85. * const.
  86. */
  87. static void
  88. xdrmem_destroy (XDR *xdrs)
  89. {
  90. }
  91. /*
  92. * Gets the next word from the memory referenced by xdrs and places it
  93. * in the long pointed to by lp. It then increments the private word to
  94. * point at the next element. Neither object pointed to is const
  95. */
  96. static bool_t
  97. xdrmem_getlong (XDR *xdrs, long *lp)
  98. {
  99. if (xdrs->x_handy < 4)
  100. return FALSE;
  101. xdrs->x_handy -= 4;
  102. *lp = (int32_t) ntohl ((*((int32_t *) (xdrs->x_private))));
  103. xdrs->x_private += 4;
  104. return TRUE;
  105. }
  106. /*
  107. * Puts the long pointed to by lp in the memory referenced by xdrs. It
  108. * then increments the private word to point at the next element. The
  109. * long pointed at is const
  110. */
  111. static bool_t
  112. xdrmem_putlong (XDR *xdrs, const long *lp)
  113. {
  114. if (xdrs->x_handy < 4)
  115. return FALSE;
  116. xdrs->x_handy -= 4;
  117. *(int32_t *) xdrs->x_private = htonl (*lp);
  118. xdrs->x_private += 4;
  119. return TRUE;
  120. }
  121. /*
  122. * Gets an unaligned number of bytes from the xdrs structure and writes them
  123. * to the address passed in addr. Be very careful when calling this routine
  124. * as it could leave the xdrs pointing to an unaligned structure which is not
  125. * a good idea. None of the things pointed to are const.
  126. */
  127. static bool_t
  128. xdrmem_getbytes (XDR *xdrs, caddr_t addr, u_int len)
  129. {
  130. if (xdrs->x_handy < len)
  131. return FALSE;
  132. xdrs->x_handy -= len;
  133. memcpy (addr, xdrs->x_private, len);
  134. xdrs->x_private += len;
  135. return TRUE;
  136. }
  137. /*
  138. * The complementary function to the above. The same warnings apply about
  139. * unaligned data. The source address is const.
  140. */
  141. static bool_t
  142. xdrmem_putbytes (XDR *xdrs, const char *addr, u_int len)
  143. {
  144. if (xdrs->x_handy < len)
  145. return FALSE;
  146. xdrs->x_handy -= len;
  147. memcpy (xdrs->x_private, addr, len);
  148. xdrs->x_private += len;
  149. return TRUE;
  150. }
  151. /*
  152. * Not sure what this one does. But it clearly doesn't modify the contents
  153. * of xdrs. **FIXME** does this not assume u_int == u_long?
  154. */
  155. static u_int
  156. xdrmem_getpos (const XDR *xdrs)
  157. {
  158. return (u_long) xdrs->x_private - (u_long) xdrs->x_base;
  159. }
  160. /*
  161. * xdrs modified
  162. */
  163. static bool_t
  164. xdrmem_setpos (XDR *xdrs, u_int pos)
  165. {
  166. caddr_t newaddr = xdrs->x_base + pos;
  167. caddr_t lastaddr = xdrs->x_private + xdrs->x_handy;
  168. size_t handy = lastaddr - newaddr;
  169. if (newaddr > lastaddr
  170. || newaddr < xdrs->x_base
  171. || handy != (u_int) handy)
  172. return FALSE;
  173. xdrs->x_private = newaddr;
  174. xdrs->x_handy = (u_int) handy;
  175. return TRUE;
  176. }
  177. /*
  178. * xdrs modified
  179. */
  180. static int32_t *
  181. xdrmem_inline (XDR *xdrs, u_int len)
  182. {
  183. int32_t *buf = 0;
  184. if (xdrs->x_handy >= len)
  185. {
  186. xdrs->x_handy -= len;
  187. buf = (int32_t *) xdrs->x_private;
  188. xdrs->x_private += len;
  189. }
  190. return buf;
  191. }
  192. /*
  193. * Gets the next word from the memory referenced by xdrs and places it
  194. * in the int pointed to by ip. It then increments the private word to
  195. * point at the next element. Neither object pointed to is const
  196. */
  197. static bool_t
  198. xdrmem_getint32 (XDR *xdrs, int32_t *ip)
  199. {
  200. if (xdrs->x_handy < 4)
  201. return FALSE;
  202. xdrs->x_handy -= 4;
  203. *ip = ntohl ((*((int32_t *) (xdrs->x_private))));
  204. xdrs->x_private += 4;
  205. return TRUE;
  206. }
  207. /*
  208. * Puts the long pointed to by lp in the memory referenced by xdrs. It
  209. * then increments the private word to point at the next element. The
  210. * long pointed at is const
  211. */
  212. static bool_t
  213. xdrmem_putint32 (XDR *xdrs, const int32_t *ip)
  214. {
  215. if (xdrs->x_handy < 4)
  216. return FALSE;
  217. xdrs->x_handy -= 4;
  218. *(int32_t *) xdrs->x_private = htonl (*ip);
  219. xdrs->x_private += 4;
  220. return TRUE;
  221. }