xdr_sizeof.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * xdr_sizeof.c
  3. *
  4. * Copyright (c) 2010, Oracle America, Inc.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following
  14. * disclaimer in the documentation and/or other materials
  15. * provided with the distribution.
  16. * * Neither the name of the "Oracle America, Inc." nor the names of its
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  27. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. * General purpose routine to see how much space something will use
  34. * when serialized using XDR.
  35. */
  36. #include <rpc/types.h>
  37. #include <rpc/xdr.h>
  38. #include <sys/types.h>
  39. #include <stdlib.h>
  40. #include <shlib-compat.h>
  41. /* ARGSUSED */
  42. static bool_t
  43. x_putlong (XDR *xdrs, const long *longp)
  44. {
  45. xdrs->x_handy += BYTES_PER_XDR_UNIT;
  46. return TRUE;
  47. }
  48. /* ARGSUSED */
  49. static bool_t
  50. x_putbytes (XDR *xdrs, const char *bp, u_int len)
  51. {
  52. xdrs->x_handy += len;
  53. return TRUE;
  54. }
  55. static u_int
  56. x_getpostn (const XDR *xdrs)
  57. {
  58. return xdrs->x_handy;
  59. }
  60. /* ARGSUSED */
  61. static bool_t
  62. x_setpostn (XDR *xdrs, u_int len)
  63. {
  64. /* This is not allowed */
  65. return FALSE;
  66. }
  67. static int32_t *
  68. x_inline (XDR *xdrs, u_int len)
  69. {
  70. if (len == 0)
  71. return NULL;
  72. if (xdrs->x_op != XDR_ENCODE)
  73. return NULL;
  74. if (len < (u_int) (long int) xdrs->x_base)
  75. {
  76. /* x_private was already allocated */
  77. xdrs->x_handy += len;
  78. return (int32_t *) xdrs->x_private;
  79. }
  80. else
  81. {
  82. /* Free the earlier space and allocate new area */
  83. free (xdrs->x_private);
  84. if ((xdrs->x_private = (caddr_t) malloc (len)) == NULL)
  85. {
  86. xdrs->x_base = 0;
  87. return NULL;
  88. }
  89. xdrs->x_base = (void *) (long) len;
  90. xdrs->x_handy += len;
  91. return (int32_t *) xdrs->x_private;
  92. }
  93. }
  94. static int
  95. harmless (void)
  96. {
  97. /* Always return FALSE/NULL, as the case may be */
  98. return 0;
  99. }
  100. static void
  101. x_destroy (XDR *xdrs)
  102. {
  103. xdrs->x_handy = 0;
  104. xdrs->x_base = 0;
  105. if (xdrs->x_private)
  106. {
  107. free (xdrs->x_private);
  108. xdrs->x_private = NULL;
  109. }
  110. return;
  111. }
  112. static bool_t
  113. x_putint32 (XDR *xdrs, const int32_t *int32p)
  114. {
  115. xdrs->x_handy += BYTES_PER_XDR_UNIT;
  116. return TRUE;
  117. }
  118. unsigned long
  119. xdr_sizeof (xdrproc_t func, void *data)
  120. {
  121. XDR x;
  122. struct xdr_ops ops;
  123. bool_t stat;
  124. /* to stop ANSI-C compiler from complaining */
  125. typedef bool_t (*dummyfunc1) (XDR *, long *);
  126. typedef bool_t (*dummyfunc2) (XDR *, caddr_t, u_int);
  127. typedef bool_t (*dummyfunc3) (XDR *, int32_t *);
  128. ops.x_putlong = x_putlong;
  129. ops.x_putbytes = x_putbytes;
  130. ops.x_inline = x_inline;
  131. ops.x_getpostn = x_getpostn;
  132. ops.x_setpostn = x_setpostn;
  133. ops.x_destroy = x_destroy;
  134. ops.x_putint32 = x_putint32;
  135. /* the other harmless ones */
  136. ops.x_getlong = (dummyfunc1) harmless;
  137. ops.x_getbytes = (dummyfunc2) harmless;
  138. ops.x_getint32 = (dummyfunc3) harmless;
  139. x.x_op = XDR_ENCODE;
  140. x.x_ops = &ops;
  141. x.x_handy = 0;
  142. x.x_private = (caddr_t) NULL;
  143. x.x_base = (caddr_t) 0;
  144. stat = func (&x, data);
  145. free (x.x_private);
  146. return stat == TRUE ? x.x_handy : 0;
  147. }
  148. #ifdef EXPORT_RPC_SYMBOLS
  149. libc_hidden_def (xdr_sizeof)
  150. #else
  151. libc_hidden_nolink_sunrpc (xdr_sizeof, GLIBC_2_1)
  152. #endif