xdr_array.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * xdr_array.c, Generic XDR routines implementation.
  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. * These are the "non-trivial" xdr primitives used to serialize and
  34. * de-serialize arrays. See xdr.h for more info on the interface to xdr.
  35. */
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <rpc/types.h>
  39. #include <rpc/xdr.h>
  40. #include <libintl.h>
  41. #include <limits.h>
  42. #include <wchar.h>
  43. #include <shlib-compat.h>
  44. #define LASTUNSIGNED ((u_int)0-1)
  45. /*
  46. * XDR an array of arbitrary elements
  47. * *addrp is a pointer to the array, *sizep is the number of elements.
  48. * If addrp is NULL (*sizep * elsize) bytes are allocated.
  49. * elsize is the size (in bytes) of each element, and elproc is the
  50. * xdr procedure to call to handle each element of the array.
  51. */
  52. bool_t
  53. xdr_array (XDR *xdrs,
  54. /* array pointer */
  55. caddr_t *addrp,
  56. /* number of elements */
  57. u_int *sizep,
  58. /* max numberof elements */
  59. u_int maxsize,
  60. /* size in bytes of each element */
  61. u_int elsize,
  62. /* xdr routine to handle each element */
  63. xdrproc_t elproc)
  64. {
  65. u_int i;
  66. caddr_t target = *addrp;
  67. u_int c; /* the actual element count */
  68. bool_t stat = TRUE;
  69. /* like strings, arrays are really counted arrays */
  70. if (!xdr_u_int (xdrs, sizep))
  71. {
  72. return FALSE;
  73. }
  74. c = *sizep;
  75. /*
  76. * XXX: Let the overflow possibly happen with XDR_FREE because mem_free()
  77. * doesn't actually use its second argument anyway.
  78. */
  79. if ((c > maxsize || c > UINT_MAX / elsize) && (xdrs->x_op != XDR_FREE))
  80. {
  81. return FALSE;
  82. }
  83. /*
  84. * if we are deserializing, we may need to allocate an array.
  85. * We also save time by checking for a null array if we are freeing.
  86. */
  87. if (target == NULL)
  88. switch (xdrs->x_op)
  89. {
  90. case XDR_DECODE:
  91. if (c == 0)
  92. return TRUE;
  93. *addrp = target = calloc (c, elsize);
  94. if (target == NULL)
  95. {
  96. (void) __fxprintf (NULL, "%s: %s", __func__, _("out of memory\n"));
  97. return FALSE;
  98. }
  99. break;
  100. case XDR_FREE:
  101. return TRUE;
  102. default:
  103. break;
  104. }
  105. /*
  106. * now we xdr each element of array
  107. */
  108. for (i = 0; (i < c) && stat; i++)
  109. {
  110. stat = (*elproc) (xdrs, target, LASTUNSIGNED);
  111. target += elsize;
  112. }
  113. /*
  114. * the array may need freeing
  115. */
  116. if (xdrs->x_op == XDR_FREE)
  117. {
  118. mem_free (*addrp, c * elsize);
  119. *addrp = NULL;
  120. }
  121. return stat;
  122. }
  123. #ifdef EXPORT_RPC_SYMBOLS
  124. libc_hidden_def (xdr_array)
  125. #else
  126. libc_hidden_nolink_sunrpc (xdr_array, GLIBC_2_0)
  127. #endif
  128. /*
  129. * xdr_vector():
  130. *
  131. * XDR a fixed length array. Unlike variable-length arrays,
  132. * the storage of fixed length arrays is static and unfreeable.
  133. * > basep: base of the array
  134. * > size: size of the array
  135. * > elemsize: size of each element
  136. * > xdr_elem: routine to XDR each element
  137. */
  138. bool_t
  139. xdr_vector (XDR *xdrs, char *basep, u_int nelem, u_int elemsize,
  140. xdrproc_t xdr_elem)
  141. {
  142. u_int i;
  143. char *elptr;
  144. elptr = basep;
  145. for (i = 0; i < nelem; i++)
  146. {
  147. if (!(*xdr_elem) (xdrs, elptr, LASTUNSIGNED))
  148. {
  149. return FALSE;
  150. }
  151. elptr += elemsize;
  152. }
  153. return TRUE;
  154. }
  155. libc_hidden_nolink_sunrpc (xdr_vector, GLIBC_2_0)