xdr_mem.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  3. * unrestricted use provided that this legend is included on all tape
  4. * media and as a part of the software program in whole or part. Users
  5. * may copy or modify Sun RPC without charge, but are not authorized
  6. * to license or distribute it to anyone else except as part of a product or
  7. * program developed by the user.
  8. *
  9. * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  10. * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  11. * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  12. *
  13. * Sun RPC is provided with no support and without any obligation on the
  14. * part of Sun Microsystems, Inc. to assist in its use, correction,
  15. * modification or enhancement.
  16. *
  17. * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  18. * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  19. * OR ANY PART THEREOF.
  20. *
  21. * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  22. * or profits or other special, indirect and consequential damages, even if
  23. * Sun has been advised of the possibility of such damages.
  24. *
  25. * Sun Microsystems, Inc.
  26. * 2550 Garcia Avenue
  27. * Mountain View, California 94043
  28. */
  29. /*
  30. * xdr_mem.h, XDR implementation using memory buffers.
  31. *
  32. * Copyright (C) 1984, Sun Microsystems, Inc.
  33. *
  34. * If you have some data to be interpreted as external data representation
  35. * or to be converted to external data representation in a memory buffer,
  36. * then this is the package for you.
  37. *
  38. */
  39. #include <features.h>
  40. #include <string.h>
  41. #include <limits.h>
  42. #include <rpc/rpc.h>
  43. static bool_t xdrmem_getlong (XDR *, long *);
  44. static bool_t xdrmem_putlong (XDR *, const long *);
  45. static bool_t xdrmem_getbytes (XDR *, caddr_t, u_int);
  46. static bool_t xdrmem_putbytes (XDR *, const char *, u_int);
  47. static u_int xdrmem_getpos (const XDR *);
  48. static bool_t xdrmem_setpos (XDR *, u_int);
  49. static int32_t *xdrmem_inline (XDR *, u_int);
  50. static void xdrmem_destroy (XDR *);
  51. static bool_t xdrmem_getint32 (XDR *, int32_t *);
  52. static bool_t xdrmem_putint32 (XDR *, const int32_t *);
  53. static const struct xdr_ops xdrmem_ops =
  54. {
  55. xdrmem_getlong,
  56. xdrmem_putlong,
  57. xdrmem_getbytes,
  58. xdrmem_putbytes,
  59. xdrmem_getpos,
  60. xdrmem_setpos,
  61. xdrmem_inline,
  62. xdrmem_destroy,
  63. xdrmem_getint32,
  64. xdrmem_putint32
  65. };
  66. /*
  67. * The procedure xdrmem_create initializes a stream descriptor for a
  68. * memory buffer.
  69. */
  70. void
  71. xdrmem_create (XDR *xdrs, const caddr_t addr, u_int size, enum xdr_op op)
  72. {
  73. xdrs->x_op = op;
  74. xdrs->x_ops = &xdrmem_ops;
  75. xdrs->x_private = xdrs->x_base = addr;
  76. xdrs->x_handy = size;
  77. }
  78. libc_hidden_def(xdrmem_create)
  79. /*
  80. * Nothing needs to be done for the memory case. The argument is clearly
  81. * const.
  82. */
  83. static void
  84. xdrmem_destroy (XDR *xdrs attribute_unused)
  85. {
  86. }
  87. /*
  88. * Gets the next word from the memory referenced by xdrs and places it
  89. * in the long pointed to by lp. It then increments the private word to
  90. * point at the next element. Neither object pointed to is const
  91. */
  92. static bool_t
  93. xdrmem_getlong (XDR *xdrs, long *lp)
  94. {
  95. if (xdrs->x_handy < 4)
  96. return FALSE;
  97. xdrs->x_handy -= 4;
  98. *lp = (int32_t) ntohl ((*((int32_t *) (xdrs->x_private))));
  99. xdrs->x_private += 4;
  100. return TRUE;
  101. }
  102. /*
  103. * Puts the long pointed to by lp in the memory referenced by xdrs. It
  104. * then increments the private word to point at the next element. The
  105. * long pointed at is const
  106. */
  107. static bool_t
  108. xdrmem_putlong (XDR *xdrs, const long *lp)
  109. {
  110. if (xdrs->x_handy < 4)
  111. return FALSE;
  112. xdrs->x_handy -= 4;
  113. *(int32_t *) xdrs->x_private = htonl (*lp);
  114. xdrs->x_private += 4;
  115. return TRUE;
  116. }
  117. /*
  118. * Gets an unaligned number of bytes from the xdrs structure and writes them
  119. * to the address passed in addr. Be very careful when calling this routine
  120. * as it could leave the xdrs pointing to an unaligned structure which is not
  121. * a good idea. None of the things pointed to are const.
  122. */
  123. static bool_t
  124. xdrmem_getbytes (XDR *xdrs, caddr_t addr, u_int len)
  125. {
  126. if (xdrs->x_handy < len)
  127. return FALSE;
  128. xdrs->x_handy -= len;
  129. memcpy (addr, xdrs->x_private, len);
  130. xdrs->x_private += len;
  131. return TRUE;
  132. }
  133. /*
  134. * The complementary function to the above. The same warnings apply about
  135. * unaligned data. The source address is const.
  136. */
  137. static bool_t
  138. xdrmem_putbytes (XDR *xdrs, const char *addr, u_int len)
  139. {
  140. if (xdrs->x_handy < len)
  141. return FALSE;
  142. xdrs->x_handy -= len;
  143. memcpy (xdrs->x_private, addr, len);
  144. xdrs->x_private += len;
  145. return TRUE;
  146. }
  147. /*
  148. * Not sure what this one does. But it clearly doesn't modify the contents
  149. * of xdrs. **FIXME** does this not assume u_int == u_long?
  150. */
  151. static u_int
  152. xdrmem_getpos (const XDR *xdrs)
  153. {
  154. return (u_long) xdrs->x_private - (u_long) xdrs->x_base;
  155. }
  156. /*
  157. * xdrs modified
  158. */
  159. static bool_t
  160. xdrmem_setpos (XDR *xdrs, u_int pos)
  161. {
  162. caddr_t newaddr = xdrs->x_base + pos;
  163. caddr_t lastaddr = xdrs->x_private + xdrs->x_handy;
  164. if ((long) newaddr > (long) lastaddr
  165. || (UINT_MAX < LONG_MAX
  166. && (long) UINT_MAX < (long) lastaddr - (long) newaddr))
  167. return FALSE;
  168. xdrs->x_private = newaddr;
  169. xdrs->x_handy = (long) lastaddr - (long) newaddr;
  170. return TRUE;
  171. }
  172. /*
  173. * xdrs modified
  174. */
  175. static int32_t *
  176. xdrmem_inline (XDR *xdrs, u_int len)
  177. {
  178. int32_t *buf = 0;
  179. if (xdrs->x_handy >= len)
  180. {
  181. xdrs->x_handy -= len;
  182. buf = (int32_t *) xdrs->x_private;
  183. xdrs->x_private += len;
  184. }
  185. return buf;
  186. }
  187. /*
  188. * Gets the next word from the memory referenced by xdrs and places it
  189. * in the int pointed to by ip. It then increments the private word to
  190. * point at the next element. Neither object pointed to is const
  191. */
  192. static bool_t
  193. xdrmem_getint32 (XDR *xdrs, int32_t *ip)
  194. {
  195. if (xdrs->x_handy < 4)
  196. return FALSE;
  197. xdrs->x_handy -= 4;
  198. *ip = ntohl ((*((int32_t *) (xdrs->x_private))));
  199. xdrs->x_private += 4;
  200. return TRUE;
  201. }
  202. /*
  203. * Puts the long pointed to by lp in the memory referenced by xdrs. It
  204. * then increments the private word to point at the next element. The
  205. * long pointed at is const
  206. */
  207. static bool_t
  208. xdrmem_putint32 (XDR *xdrs, const int32_t *ip)
  209. {
  210. if (xdrs->x_handy < 4)
  211. return FALSE;
  212. xdrs->x_handy -= 4;
  213. *(int32_t *) xdrs->x_private = htonl (*ip);
  214. xdrs->x_private += 4;
  215. return TRUE;
  216. }