xdr_mem.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* @(#)xdr_mem.c 2.1 88/07/29 4.0 RPCSRC */
  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. #if !defined(lint) && defined(SCCSIDS)
  31. static char sccsid[] = "@(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro";
  32. #endif
  33. /*
  34. * xdr_mem.h, XDR implementation using memory buffers.
  35. *
  36. * Copyright (C) 1984, Sun Microsystems, Inc.
  37. *
  38. * If you have some data to be interpreted as external data representation
  39. * or to be converted to external data representation in a memory buffer,
  40. * then this is the package for you.
  41. *
  42. */
  43. #include <rpc/types.h>
  44. #include <rpc/xdr.h>
  45. #include <string.h>
  46. #include <limits.h>
  47. static bool_t xdrmem_getlong (XDR *, long *);
  48. static bool_t xdrmem_putlong (XDR *, const long *);
  49. static bool_t xdrmem_getbytes (XDR *, char *, unsigned int);
  50. static bool_t xdrmem_putbytes (XDR *, const char *, unsigned int);
  51. static unsigned int xdrmem_getpos (const XDR *);
  52. static bool_t xdrmem_setpos (XDR *, unsigned int);
  53. static int32_t *xdrmem_inline (XDR *, unsigned int);
  54. static void xdrmem_destroy (XDR *);
  55. static struct xdr_ops xdrmem_ops = {
  56. xdrmem_getlong,
  57. xdrmem_putlong,
  58. xdrmem_getbytes,
  59. xdrmem_putbytes,
  60. xdrmem_getpos,
  61. xdrmem_setpos,
  62. xdrmem_inline,
  63. xdrmem_destroy,
  64. NULL,
  65. NULL
  66. };
  67. /*
  68. * The procedure xdrmem_create initializes a stream descriptor for a
  69. * memory buffer.
  70. */
  71. void
  72. xdrmem_create (XDR *xdrs, const char* addr, unsigned int size, enum xdr_op op)
  73. {
  74. xdrs->x_op = op;
  75. xdrs->x_ops = &xdrmem_ops;
  76. xdrs->x_private = xdrs->x_base = (char*)addr;
  77. xdrs->x_handy = size;
  78. }
  79. static void
  80. xdrmem_destroy (XDR *xdrs)
  81. {
  82. }
  83. static bool_t
  84. xdrmem_getlong (XDR *xdrs, long *lp)
  85. {
  86. if (xdrs->x_handy < 4) return FALSE;
  87. xdrs->x_handy -= 4;
  88. *lp = (int32_t) ntohl((*((int32_t *) (xdrs->x_private))));
  89. xdrs->x_private += 4;
  90. return TRUE;
  91. }
  92. static bool_t
  93. xdrmem_putlong (XDR *xdrs, const long *lp)
  94. {
  95. if (xdrs->x_handy < 4) return FALSE;
  96. xdrs->x_handy -= 4;
  97. *(int32_t *) xdrs->x_private = htonl(*lp);
  98. xdrs->x_private += 4;
  99. return (TRUE);
  100. }
  101. static bool_t
  102. xdrmem_getbytes (XDR *xdrs, char *addr, unsigned int len)
  103. {
  104. if (xdrs->x_handy < len) return FALSE;
  105. xdrs->x_handy -= len;
  106. memmove(addr, xdrs->x_private, len);
  107. xdrs->x_private += len;
  108. return TRUE;
  109. }
  110. static bool_t
  111. xdrmem_putbytes (XDR *xdrs, const char *addr, unsigned int len)
  112. {
  113. if (xdrs->x_handy < len) return FALSE;
  114. xdrs->x_handy -= len;
  115. memmove(xdrs->x_private, addr, len);
  116. xdrs->x_private += len;
  117. return (TRUE);
  118. }
  119. static unsigned int xdrmem_getpos (const XDR *xdrs)
  120. {
  121. return ((unsigned long) xdrs->x_private - (unsigned long) xdrs->x_base);
  122. }
  123. static bool_t xdrmem_setpos(xdrs, pos)
  124. register XDR *xdrs;
  125. unsigned int pos;
  126. {
  127. register char* newaddr = xdrs->x_base + pos;
  128. register char* lastaddr = xdrs->x_private + xdrs->x_handy;
  129. if ((long) newaddr > (long) lastaddr
  130. || (UINT_MAX < LONG_MAX
  131. && (long) UINT_MAX < (long) lastaddr - (long) newaddr))
  132. return (FALSE);
  133. xdrs->x_private = newaddr;
  134. xdrs->x_handy = (long) lastaddr - (long) newaddr;
  135. return (TRUE);
  136. }
  137. static int32_t *
  138. xdrmem_inline (XDR *xdrs, unsigned int len)
  139. {
  140. int32_t *buf = 0;
  141. if (xdrs->x_handy >= len) {
  142. xdrs->x_handy -= len;
  143. buf = (int32_t *) xdrs->x_private;
  144. xdrs->x_private += len;
  145. }
  146. return (buf);
  147. }