xdr_mem.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. */
  9. /* @(#)xdr_mem.c 2.1 88/07/29 4.0 RPCSRC */
  10. /*
  11. * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  12. * unrestricted use provided that this legend is included on all tape
  13. * media and as a part of the software program in whole or part. Users
  14. * may copy or modify Sun RPC without charge, but are not authorized
  15. * to license or distribute it to anyone else except as part of a product or
  16. * program developed by the user.
  17. *
  18. * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  19. * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  20. * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  21. *
  22. * Sun RPC is provided with no support and without any obligation on the
  23. * part of Sun Microsystems, Inc. to assist in its use, correction,
  24. * modification or enhancement.
  25. *
  26. * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  27. * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  28. * OR ANY PART THEREOF.
  29. *
  30. * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  31. * or profits or other special, indirect and consequential damages, even if
  32. * Sun has been advised of the possibility of such damages.
  33. *
  34. * Sun Microsystems, Inc.
  35. * 2550 Garcia Avenue
  36. * Mountain View, California 94043
  37. */
  38. #if !defined(lint) && defined(SCCSIDS)
  39. static char sccsid[] = "@(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro";
  40. #endif
  41. /*
  42. * xdr_mem.h, XDR implementation using memory buffers.
  43. *
  44. * Copyright (C) 1984, Sun Microsystems, Inc.
  45. *
  46. * If you have some data to be interpreted as external data representation
  47. * or to be converted to external data representation in a memory buffer,
  48. * then this is the package for you.
  49. *
  50. */
  51. #include <rpc/types.h>
  52. #include <rpc/xdr.h>
  53. #include <string.h>
  54. #include <limits.h>
  55. static bool_t xdrmem_getlong (XDR *, long *);
  56. static bool_t xdrmem_putlong (XDR *, const long *);
  57. static bool_t xdrmem_getbytes (XDR *, char *, unsigned int);
  58. static bool_t xdrmem_putbytes (XDR *, const char *, unsigned int);
  59. static unsigned int xdrmem_getpos (const XDR *);
  60. static bool_t xdrmem_setpos (XDR *, unsigned int);
  61. static int32_t *xdrmem_inline (XDR *, unsigned int);
  62. static void xdrmem_destroy (XDR *);
  63. static struct xdr_ops xdrmem_ops = {
  64. xdrmem_getlong,
  65. xdrmem_putlong,
  66. xdrmem_getbytes,
  67. xdrmem_putbytes,
  68. xdrmem_getpos,
  69. xdrmem_setpos,
  70. xdrmem_inline,
  71. xdrmem_destroy,
  72. NULL,
  73. NULL
  74. };
  75. /*
  76. * The procedure xdrmem_create initializes a stream descriptor for a
  77. * memory buffer.
  78. */
  79. void
  80. xdrmem_create (XDR *xdrs, const char* addr, unsigned int size, enum xdr_op op)
  81. {
  82. xdrs->x_op = op;
  83. xdrs->x_ops = &xdrmem_ops;
  84. xdrs->x_private = xdrs->x_base = (char*)addr;
  85. xdrs->x_handy = size;
  86. }
  87. static void
  88. xdrmem_destroy (XDR *xdrs)
  89. {
  90. }
  91. static bool_t
  92. xdrmem_getlong (XDR *xdrs, long *lp)
  93. {
  94. if (xdrs->x_handy < 4) return FALSE;
  95. xdrs->x_handy -= 4;
  96. *lp = (int32_t) ntohl((*((int32_t *) (xdrs->x_private))));
  97. xdrs->x_private += 4;
  98. return TRUE;
  99. }
  100. static bool_t
  101. xdrmem_putlong (XDR *xdrs, const long *lp)
  102. {
  103. if (xdrs->x_handy < 4) return FALSE;
  104. xdrs->x_handy -= 4;
  105. *(int32_t *) xdrs->x_private = htonl(*lp);
  106. xdrs->x_private += 4;
  107. return (TRUE);
  108. }
  109. static bool_t
  110. xdrmem_getbytes (XDR *xdrs, char *addr, unsigned int len)
  111. {
  112. if (xdrs->x_handy < len) return FALSE;
  113. xdrs->x_handy -= len;
  114. memmove(addr, xdrs->x_private, len);
  115. xdrs->x_private += len;
  116. return TRUE;
  117. }
  118. static bool_t
  119. xdrmem_putbytes (XDR *xdrs, const char *addr, unsigned int len)
  120. {
  121. if (xdrs->x_handy < len) return FALSE;
  122. xdrs->x_handy -= len;
  123. memmove(xdrs->x_private, addr, len);
  124. xdrs->x_private += len;
  125. return (TRUE);
  126. }
  127. static unsigned int xdrmem_getpos (const XDR *xdrs)
  128. {
  129. return ((unsigned long) xdrs->x_private - (unsigned long) xdrs->x_base);
  130. }
  131. static bool_t xdrmem_setpos(XDR *xdrs, unsigned int pos)
  132. {
  133. register char* newaddr = xdrs->x_base + pos;
  134. register char* lastaddr = xdrs->x_private + xdrs->x_handy;
  135. if ((long) newaddr > (long) lastaddr
  136. || (UINT_MAX < LONG_MAX
  137. && (long) UINT_MAX < (long) lastaddr - (long) newaddr))
  138. return (FALSE);
  139. xdrs->x_private = newaddr;
  140. xdrs->x_handy = (long) lastaddr - (long) newaddr;
  141. return (TRUE);
  142. }
  143. static int32_t *
  144. xdrmem_inline (XDR *xdrs, unsigned int len)
  145. {
  146. int32_t *buf = 0;
  147. if (xdrs->x_handy >= len) {
  148. xdrs->x_handy -= len;
  149. buf = (int32_t *) xdrs->x_private;
  150. xdrs->x_private += len;
  151. }
  152. return (buf);
  153. }