xdr_mem.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* $NetBSD: xdr_mem.c,v 1.15 2000/01/22 22:19:18 mycroft Exp $ */
  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. #include <sys/cdefs.h>
  31. /*
  32. * xdr_mem.h, XDR implementation using memory buffers.
  33. *
  34. * Copyright (C) 1984, Sun Microsystems, Inc.
  35. *
  36. * If you have some data to be interpreted as external data representation
  37. * or to be converted to external data representation in a memory buffer,
  38. * then this is the package for you.
  39. *
  40. */
  41. #include <sys/param.h>
  42. #include <sys/systm.h>
  43. #include <sys/malloc.h>
  44. #include <rpc/types.h>
  45. #include <rpc/xdr.h>
  46. static void xdrmem_destroy(XDR *);
  47. static bool_t xdrmem_getlong_aligned(XDR *, long *);
  48. static bool_t xdrmem_putlong_aligned(XDR *, const long *);
  49. static bool_t xdrmem_getlong_unaligned(XDR *, long *);
  50. static bool_t xdrmem_putlong_unaligned(XDR *, const long *);
  51. static bool_t xdrmem_getbytes(XDR *, char *, u_int);
  52. static bool_t xdrmem_putbytes(XDR *, const char *, u_int);
  53. /* XXX: w/64-bit pointers, u_int not enough! */
  54. static u_int xdrmem_getpos(XDR *);
  55. static bool_t xdrmem_setpos(XDR *, u_int);
  56. static int32_t *xdrmem_inline_aligned(XDR *, u_int);
  57. static int32_t *xdrmem_inline_unaligned(XDR *, u_int);
  58. static bool_t xdrmem_control(XDR *xdrs, int request, void *info);
  59. static const struct xdr_ops xdrmem_ops_aligned = {
  60. xdrmem_getlong_aligned,
  61. xdrmem_putlong_aligned,
  62. xdrmem_getbytes,
  63. xdrmem_putbytes,
  64. xdrmem_getpos,
  65. xdrmem_setpos,
  66. xdrmem_inline_aligned,
  67. xdrmem_destroy,
  68. xdrmem_control
  69. };
  70. static const struct xdr_ops xdrmem_ops_unaligned = {
  71. xdrmem_getlong_unaligned,
  72. xdrmem_putlong_unaligned,
  73. xdrmem_getbytes,
  74. xdrmem_putbytes,
  75. xdrmem_getpos,
  76. xdrmem_setpos,
  77. xdrmem_inline_unaligned,
  78. xdrmem_destroy,
  79. xdrmem_control
  80. };
  81. /*
  82. * The procedure xdrmem_create initializes a stream descriptor for a
  83. * memory buffer.
  84. */
  85. void
  86. xdrmem_create(XDR *xdrs, char *addr, u_int size, enum xdr_op op)
  87. {
  88. xdrs->x_op = op;
  89. xdrs->x_ops = ((unsigned long)addr & (sizeof(int32_t) - 1))
  90. ? &xdrmem_ops_unaligned : &xdrmem_ops_aligned;
  91. xdrs->x_private = xdrs->x_base = addr;
  92. xdrs->x_handy = size;
  93. }
  94. /*ARGSUSED*/
  95. static void
  96. xdrmem_destroy(XDR *xdrs)
  97. {
  98. }
  99. static bool_t
  100. xdrmem_getlong_aligned(XDR *xdrs, long *lp)
  101. {
  102. if (xdrs->x_handy < sizeof(int32_t))
  103. return (FALSE);
  104. xdrs->x_handy -= sizeof(int32_t);
  105. *lp = ntohl(*(uint32_t *)xdrs->x_private);
  106. xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
  107. return (TRUE);
  108. }
  109. static bool_t
  110. xdrmem_putlong_aligned(XDR *xdrs, const long *lp)
  111. {
  112. if (xdrs->x_handy < sizeof(int32_t))
  113. return (FALSE);
  114. xdrs->x_handy -= sizeof(int32_t);
  115. *(uint32_t *)xdrs->x_private = htonl((uint32_t)*lp);
  116. xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
  117. return (TRUE);
  118. }
  119. static bool_t
  120. xdrmem_getlong_unaligned(XDR *xdrs, long *lp)
  121. {
  122. uint32_t l;
  123. if (xdrs->x_handy < sizeof(int32_t))
  124. return (FALSE);
  125. xdrs->x_handy -= sizeof(int32_t);
  126. memmove(&l, xdrs->x_private, sizeof(int32_t));
  127. *lp = ntohl(l);
  128. xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
  129. return (TRUE);
  130. }
  131. static bool_t
  132. xdrmem_putlong_unaligned(XDR *xdrs, const long *lp)
  133. {
  134. uint32_t l;
  135. if (xdrs->x_handy < sizeof(int32_t))
  136. return (FALSE);
  137. xdrs->x_handy -= sizeof(int32_t);
  138. l = htonl((uint32_t)*lp);
  139. memmove(xdrs->x_private, &l, sizeof(int32_t));
  140. xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
  141. return (TRUE);
  142. }
  143. static bool_t
  144. xdrmem_getbytes(XDR *xdrs, char *addr, u_int len)
  145. {
  146. if (xdrs->x_handy < len)
  147. return (FALSE);
  148. xdrs->x_handy -= len;
  149. memmove(addr, xdrs->x_private, len);
  150. xdrs->x_private = (char *)xdrs->x_private + len;
  151. return (TRUE);
  152. }
  153. static bool_t
  154. xdrmem_putbytes(XDR *xdrs, const char *addr, u_int len)
  155. {
  156. if (xdrs->x_handy < len)
  157. return (FALSE);
  158. xdrs->x_handy -= len;
  159. memmove(xdrs->x_private, addr, len);
  160. xdrs->x_private = (char *)xdrs->x_private + len;
  161. return (TRUE);
  162. }
  163. static u_int
  164. xdrmem_getpos(XDR *xdrs)
  165. {
  166. /* XXX w/64-bit pointers, u_int not enough! */
  167. return (u_int)((u_long)xdrs->x_private - (u_long)xdrs->x_base);
  168. }
  169. static bool_t
  170. xdrmem_setpos(XDR *xdrs, u_int pos)
  171. {
  172. char *newaddr = xdrs->x_base + pos;
  173. char *lastaddr = (char *)xdrs->x_private + xdrs->x_handy;
  174. if (newaddr > lastaddr)
  175. return (FALSE);
  176. xdrs->x_private = newaddr;
  177. xdrs->x_handy = (u_int)(lastaddr - newaddr); /* XXX sizeof(u_int) <? sizeof(ptrdiff_t) */
  178. return (TRUE);
  179. }
  180. static int32_t *
  181. xdrmem_inline_aligned(XDR *xdrs, u_int len)
  182. {
  183. int32_t *buf = NULL;
  184. if (xdrs->x_handy >= len) {
  185. xdrs->x_handy -= len;
  186. buf = (int32_t *)xdrs->x_private;
  187. xdrs->x_private = (char *)xdrs->x_private + len;
  188. }
  189. return (buf);
  190. }
  191. /* ARGSUSED */
  192. static int32_t *
  193. xdrmem_inline_unaligned(XDR *xdrs, u_int len)
  194. {
  195. return (0);
  196. }
  197. static bool_t
  198. xdrmem_control(XDR *xdrs, int request, void *info)
  199. {
  200. xdr_bytesrec *xptr;
  201. int32_t *l;
  202. int len;
  203. switch (request) {
  204. case XDR_GET_BYTES_AVAIL:
  205. xptr = (xdr_bytesrec *)info;
  206. xptr->xc_is_last_record = TRUE;
  207. xptr->xc_num_avail = xdrs->x_handy;
  208. return (TRUE);
  209. case XDR_PEEK:
  210. /*
  211. * Return the next 4 byte unit in the XDR stream.
  212. */
  213. if (xdrs->x_handy < sizeof (int32_t))
  214. return (FALSE);
  215. l = (int32_t *)info;
  216. *l = (int32_t)ntohl((uint32_t)
  217. (*((int32_t *)(xdrs->x_private))));
  218. return (TRUE);
  219. case XDR_SKIPBYTES:
  220. /*
  221. * Skip the next N bytes in the XDR stream.
  222. */
  223. l = (int32_t *)info;
  224. len = RNDUP((int)(*l));
  225. if (xdrs->x_handy < len)
  226. return (FALSE);
  227. xdrs->x_handy -= len;
  228. xdrs->x_private = (char *)xdrs->x_private + len;
  229. return (TRUE);
  230. }
  231. return (FALSE);
  232. }