xdr_mem.c 6.2 KB

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