xdr_intXX_t.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* Copyright (c) 1998, 1999, 2000 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1998.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <rpc/types.h>
  17. #include <rpc/xdr.h>
  18. /* XDR 64bit integers */
  19. bool_t
  20. xdr_int64_t (XDR *xdrs, int64_t *ip)
  21. {
  22. int32_t t1;
  23. /* This must be unsigned, otherwise we get problems with sign
  24. extension in the DECODE case. */
  25. uint32_t t2;
  26. switch (xdrs->x_op)
  27. {
  28. case XDR_ENCODE:
  29. t1 = (int32_t) ((*ip) >> 32);
  30. t2 = (int32_t) (*ip);
  31. return (XDR_PUTINT32(xdrs, &t1) && XDR_PUTINT32(xdrs, &t2));
  32. case XDR_DECODE:
  33. if (!XDR_GETINT32(xdrs, &t1) || !XDR_GETINT32(xdrs, &t2))
  34. return FALSE;
  35. *ip = ((int64_t) t1) << 32;
  36. *ip |= t2;
  37. return TRUE;
  38. case XDR_FREE:
  39. return TRUE;
  40. default:
  41. return FALSE;
  42. }
  43. }
  44. /* XDR 64bit unsigned integers */
  45. bool_t
  46. xdr_uint64_t (XDR *xdrs, uint64_t *uip)
  47. {
  48. uint32_t t1;
  49. uint32_t t2;
  50. switch (xdrs->x_op)
  51. {
  52. case XDR_ENCODE:
  53. t1 = (uint32_t) ((*uip) >> 32);
  54. t2 = (uint32_t) (*uip);
  55. return (XDR_PUTINT32 (xdrs, (int32_t *) &t1) &&
  56. XDR_PUTINT32(xdrs, (int32_t *) &t2));
  57. case XDR_DECODE:
  58. if (!XDR_GETINT32(xdrs, (int32_t *) &t1) ||
  59. !XDR_GETINT32(xdrs, (int32_t *) &t2))
  60. return FALSE;
  61. *uip = ((uint64_t) t1) << 32;
  62. *uip |= t2;
  63. return TRUE;
  64. case XDR_FREE:
  65. return TRUE;
  66. default:
  67. return FALSE;
  68. }
  69. }
  70. /* XDR 32bit integers */
  71. bool_t
  72. xdr_int32_t (XDR *xdrs, int32_t *lp)
  73. {
  74. switch (xdrs->x_op)
  75. {
  76. case XDR_ENCODE:
  77. return XDR_PUTINT32 (xdrs, lp);
  78. case XDR_DECODE:
  79. return XDR_GETINT32 (xdrs, lp);
  80. case XDR_FREE:
  81. return TRUE;
  82. default:
  83. return FALSE;
  84. }
  85. }
  86. /* XDR 32bit unsigned integers */
  87. bool_t
  88. xdr_uint32_t (XDR *xdrs, uint32_t *ulp)
  89. {
  90. switch (xdrs->x_op)
  91. {
  92. case XDR_ENCODE:
  93. return XDR_PUTINT32 (xdrs, (int32_t *) ulp);
  94. case XDR_DECODE:
  95. return XDR_GETINT32 (xdrs, (int32_t *) ulp);
  96. case XDR_FREE:
  97. return TRUE;
  98. default:
  99. return FALSE;
  100. }
  101. }
  102. /* XDR 16bit integers */
  103. bool_t
  104. xdr_int16_t (XDR *xdrs, int16_t *ip)
  105. {
  106. int32_t t;
  107. switch (xdrs->x_op)
  108. {
  109. case XDR_ENCODE:
  110. t = (int32_t) *ip;
  111. return XDR_PUTINT32 (xdrs, &t);
  112. case XDR_DECODE:
  113. if (!XDR_GETINT32 (xdrs, &t))
  114. return FALSE;
  115. *ip = (int16_t) t;
  116. return TRUE;
  117. case XDR_FREE:
  118. return TRUE;
  119. default:
  120. return FALSE;
  121. }
  122. }
  123. /* XDR 16bit unsigned integers */
  124. bool_t
  125. xdr_uint16_t (XDR *xdrs, uint16_t *uip)
  126. {
  127. uint32_t ut;
  128. switch (xdrs->x_op)
  129. {
  130. case XDR_ENCODE:
  131. ut = (uint32_t) *uip;
  132. return XDR_PUTINT32 (xdrs, (int32_t *) &ut);
  133. case XDR_DECODE:
  134. if (!XDR_GETINT32 (xdrs, (int32_t *) &ut))
  135. return FALSE;
  136. *uip = (uint16_t) ut;
  137. return TRUE;
  138. case XDR_FREE:
  139. return TRUE;
  140. default:
  141. return FALSE;
  142. }
  143. }
  144. /* XDR 8bit integers */
  145. bool_t
  146. xdr_int8_t (XDR *xdrs, int8_t *ip)
  147. {
  148. int32_t t;
  149. switch (xdrs->x_op)
  150. {
  151. case XDR_ENCODE:
  152. t = (int32_t) *ip;
  153. return XDR_PUTINT32 (xdrs, &t);
  154. case XDR_DECODE:
  155. if (!XDR_GETINT32 (xdrs, &t))
  156. return FALSE;
  157. *ip = (int8_t) t;
  158. return TRUE;
  159. case XDR_FREE:
  160. return TRUE;
  161. default:
  162. return FALSE;
  163. }
  164. }
  165. /* XDR 8bit unsigned integers */
  166. bool_t
  167. xdr_uint8_t (XDR *xdrs, uint8_t *uip)
  168. {
  169. uint32_t ut;
  170. switch (xdrs->x_op)
  171. {
  172. case XDR_ENCODE:
  173. ut = (uint32_t) *uip;
  174. return XDR_PUTINT32 (xdrs, (int32_t *) &ut);
  175. case XDR_DECODE:
  176. if (!XDR_GETINT32 (xdrs, (int32_t *) &ut))
  177. return FALSE;
  178. *uip = (uint8_t) ut;
  179. return TRUE;
  180. case XDR_FREE:
  181. return TRUE;
  182. default:
  183. return FALSE;
  184. }
  185. }