g3dfpxdr.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #include <rpc/types.h>
  7. #include <rpc/xdr.h>
  8. #include <grass/raster.h>
  9. #include "G3d_intern.h"
  10. /*---------------------------------------------------------------------------*/
  11. int G3d_isXdrNullNum(const void *num, int isFloat)
  12. {
  13. static const char null_bytes[8] = {
  14. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
  15. };
  16. return memcmp(num, null_bytes, isFloat ? 4 : 8) == 0;
  17. }
  18. /*---------------------------------------------------------------------------*/
  19. int G3d_isXdrNullFloat(const float *f)
  20. {
  21. return G3d_isXdrNullNum(f, 1);
  22. }
  23. /*---------------------------------------------------------------------------*/
  24. int G3d_isXdrNullDouble(const double *d)
  25. {
  26. return G3d_isXdrNullNum(d, 0);
  27. }
  28. /*---------------------------------------------------------------------------*/
  29. void G3d_setXdrNullNum(void *num, int isFloat)
  30. {
  31. static const char null_bytes[8] = {
  32. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
  33. };
  34. memcpy(num, null_bytes, isFloat ? 4 : 8);
  35. }
  36. /*---------------------------------------------------------------------------*/
  37. void G3d_setXdrNullDouble(double *d)
  38. {
  39. G3d_setXdrNullNum(d, 0);
  40. }
  41. /*---------------------------------------------------------------------------*/
  42. void G3d_setXdrNullFloat(float *f)
  43. {
  44. G3d_setXdrNullNum(f, 1);
  45. }
  46. /*---------------------------------------------------------------------------*/
  47. XDR xdrEncodeStream, xdrDecodeStream; /* xdr support structures */
  48. int G3d_initFpXdr(G3D_Map * map, int misuseBytes)
  49. /* nof addtl bytes allocated for the xdr array so that */
  50. /* the array can also be (mis)used for other purposes */
  51. {
  52. int doAlloc;
  53. doAlloc = 0;
  54. if (xdr == NULL) {
  55. xdrLength = map->tileSize * G3D_MAX(map->numLengthExtern,
  56. map->numLengthIntern) +
  57. misuseBytes;
  58. xdr = G3d_malloc(xdrLength);
  59. if (xdr == NULL) {
  60. G3d_error("G3d_initFpXdr: error in G3d_malloc");
  61. return 0;
  62. }
  63. doAlloc = 1;
  64. }
  65. else if (map->tileSize * G3D_MAX(map->numLengthExtern,
  66. map->numLengthIntern) + misuseBytes
  67. > xdrLength) {
  68. xdrLength = map->tileSize * G3D_MAX(map->numLengthExtern,
  69. map->numLengthIntern) +
  70. misuseBytes;
  71. xdr = G3d_realloc(xdr, xdrLength);
  72. if (xdr == NULL) {
  73. G3d_error("G3d_initFpXdr: error in G3d_realloc");
  74. return 0;
  75. }
  76. doAlloc = 1;
  77. }
  78. if (doAlloc) {
  79. xdrmem_create(&(xdrEncodeStream), xdr, (u_int) xdrLength, XDR_ENCODE);
  80. xdrmem_create(&(xdrDecodeStream), xdr, (u_int) xdrLength, XDR_DECODE);
  81. }
  82. return 1;
  83. }
  84. /*---------------------------------------------------------------------------*/
  85. static void *xdrTmp;
  86. static int dstType, srcType, type, externLength, eltLength, isFloat, useXdr;
  87. static int (*xdrFun) ();
  88. static XDR *xdrs;
  89. static double tmpValue, *tmp;
  90. int G3d_initCopyToXdr(G3D_Map * map, int sType)
  91. {
  92. xdrTmp = xdr;
  93. useXdr = map->useXdr;
  94. srcType = sType;
  95. if (map->useXdr == G3D_USE_XDR) {
  96. if (!xdr_setpos(&(xdrEncodeStream), 0)) {
  97. G3d_error("G3d_InitCopyToXdr: positioning xdr failed");
  98. return 0;
  99. }
  100. xdrs = &(xdrEncodeStream);
  101. }
  102. type = map->type;
  103. isFloat = (type == FCELL_TYPE);
  104. externLength = G3d_externLength(type);
  105. eltLength = G3d_length(srcType);
  106. if (isFloat)
  107. xdrFun = xdr_float;
  108. else
  109. xdrFun = xdr_double;
  110. tmp = &tmpValue;
  111. return 1;
  112. }
  113. /*---------------------------------------------------------------------------*/
  114. int G3d_copyToXdr(const void *src, int nofNum)
  115. {
  116. int i;
  117. if (useXdr == G3D_NO_XDR) {
  118. G3d_copyValues(src, 0, srcType, xdrTmp, 0, type, nofNum);
  119. xdrTmp = G_incr_void_ptr(xdrTmp, nofNum * G3d_externLength(type));
  120. return 1;
  121. }
  122. for (i = 0; i < nofNum; i++, src = G_incr_void_ptr(src, eltLength)) {
  123. if (G3d_isNullValueNum(src, srcType)) {
  124. G3d_setXdrNullNum(xdrTmp, isFloat);
  125. if (!xdr_setpos(xdrs, xdr_getpos(xdrs) + externLength)) {
  126. G3d_error("G3d_copyToXdr: positioning xdr failed");
  127. return 0;
  128. }
  129. }
  130. else {
  131. if (type == srcType) {
  132. if (xdrFun(xdrs, src) < 0) {
  133. G3d_error("G3d_copyToXdr: writing xdr failed");
  134. return 0;
  135. }
  136. }
  137. else {
  138. if (type == FCELL_TYPE)
  139. *((float *)tmp) = (float)*((double *)src);
  140. else
  141. *((double *)tmp) = (double)*((float *)src);
  142. if (xdrFun(xdrs, tmp) < 0) {
  143. G3d_error("G3d_copyToXdr: writing xdr failed");
  144. return 0;
  145. }
  146. }
  147. }
  148. xdrTmp = G_incr_void_ptr(xdrTmp, externLength);
  149. }
  150. return 1;
  151. }
  152. /*---------------------------------------------------------------------------*/
  153. int G3d_initCopyFromXdr(G3D_Map * map, int dType)
  154. {
  155. xdrTmp = xdr;
  156. useXdr = map->useXdr;
  157. dstType = dType;
  158. if (useXdr == G3D_USE_XDR) {
  159. if (!xdr_setpos(&(xdrDecodeStream), 0)) {
  160. G3d_error("G3d_initCopyFromXdr: positioning xdr failed");
  161. return 0;
  162. }
  163. xdrs = &(xdrDecodeStream);
  164. }
  165. type = map->type;
  166. isFloat = (type == FCELL_TYPE);
  167. externLength = G3d_externLength(type);
  168. eltLength = G3d_length(dstType);
  169. if (isFloat)
  170. xdrFun = xdr_float;
  171. else
  172. xdrFun = xdr_double;
  173. tmp = &tmpValue;
  174. return 1;
  175. }
  176. /*---------------------------------------------------------------------------*/
  177. int G3d_copyFromXdr(int nofNum, void *dst)
  178. {
  179. int i;
  180. if (useXdr == G3D_NO_XDR) {
  181. G3d_copyValues(xdrTmp, 0, type, dst, 0, dstType, nofNum);
  182. xdrTmp = G_incr_void_ptr(xdrTmp, nofNum * G3d_externLength(type));
  183. return 1;
  184. }
  185. for (i = 0; i < nofNum; i++, dst = G_incr_void_ptr(dst, eltLength)) {
  186. if (G3d_isXdrNullNum(xdrTmp, isFloat)) {
  187. G3d_setNullValue(dst, 1, dstType);
  188. if (!xdr_setpos(xdrs, xdr_getpos(xdrs) + externLength)) {
  189. G3d_error("G3d_copyFromXdr: positioning xdr failed");
  190. return 0;
  191. }
  192. }
  193. else {
  194. if (type == dstType) {
  195. if (xdrFun(xdrs, dst) < 0) {
  196. G3d_error("G3d_copyFromXdr: reading xdr failed");
  197. return 0;
  198. }
  199. }
  200. else {
  201. if (xdrFun(xdrs, tmp) < 0) {
  202. G3d_error("G3d_copyFromXdr: reading xdr failed");
  203. return 0;
  204. }
  205. if (type == FCELL_TYPE)
  206. *((double *)dst) = (double)*((float *)tmp);
  207. else
  208. *((float *)dst) = (float)*((double *)tmp);
  209. }
  210. }
  211. xdrTmp = G_incr_void_ptr(xdrTmp, externLength);
  212. }
  213. return 1;
  214. }