xdr_float.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* @(#)xdr_float.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 0
  31. static char sccsid[] = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
  32. #endif
  33. /*
  34. * xdr_float.c, Generic XDR routines implementation.
  35. *
  36. * Copyright (C) 1984, Sun Microsystems, Inc.
  37. *
  38. * These are the "floating point" xdr routines used to (de)serialize
  39. * most common data items. See xdr.h for more info on the interface to
  40. * xdr.
  41. */
  42. #include <stdio.h>
  43. #include <endian.h>
  44. #include <rpc/types.h>
  45. #include <rpc/xdr.h>
  46. /*
  47. * NB: Not portable.
  48. * This routine works on Suns (Sky / 68000's) and Vaxen.
  49. */
  50. #define LSW (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
  51. bool_t
  52. xdr_float(XDR *xdrs, float *fp)
  53. {
  54. switch (xdrs->x_op) {
  55. case XDR_ENCODE:
  56. if (sizeof(float) == sizeof(long))
  57. return (XDR_PUTLONG(xdrs, (long *)fp));
  58. else if (sizeof(float) == sizeof(int)) {
  59. long tmp = *(int *)fp;
  60. return (XDR_PUTLONG(xdrs, &tmp));
  61. }
  62. break;
  63. case XDR_DECODE:
  64. if (sizeof(float) == sizeof(long))
  65. return (XDR_GETLONG(xdrs, (long *)fp));
  66. else if (sizeof(float) == sizeof(int)) {
  67. long tmp;
  68. if (XDR_GETLONG(xdrs, &tmp)) {
  69. *(int *)fp = tmp;
  70. return (TRUE);
  71. }
  72. }
  73. break;
  74. case XDR_FREE:
  75. return (TRUE);
  76. }
  77. return (FALSE);
  78. }
  79. bool_t
  80. xdr_double(XDR *xdrs, double *dp)
  81. {
  82. switch (xdrs->x_op) {
  83. case XDR_ENCODE:
  84. if (2*sizeof(long) == sizeof(double)) {
  85. long *lp = (long *)dp;
  86. return (XDR_PUTLONG(xdrs, lp+!LSW) &&
  87. XDR_PUTLONG(xdrs, lp+LSW));
  88. } else if (2*sizeof(int) == sizeof(double)) {
  89. int *ip = (int *)dp;
  90. long tmp[2];
  91. tmp[0] = ip[!LSW];
  92. tmp[1] = ip[LSW];
  93. return (XDR_PUTLONG(xdrs, tmp) &&
  94. XDR_PUTLONG(xdrs, tmp+1));
  95. }
  96. break;
  97. case XDR_DECODE:
  98. if (2*sizeof(long) == sizeof(double)) {
  99. long *lp = (long *)dp;
  100. return (XDR_GETLONG(xdrs, lp+!LSW) &&
  101. XDR_GETLONG(xdrs, lp+LSW));
  102. } else if (2*sizeof(int) == sizeof(double)) {
  103. int *ip = (int *)dp;
  104. long tmp[2];
  105. if (XDR_GETLONG(xdrs, tmp+!LSW) &&
  106. XDR_GETLONG(xdrs, tmp+LSW)) {
  107. ip[0] = tmp[0];
  108. ip[1] = tmp[1];
  109. return (TRUE);
  110. }
  111. }
  112. break;
  113. case XDR_FREE:
  114. return (TRUE);
  115. }
  116. return (FALSE);
  117. }