fpxdr.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #include <grass/raster.h>
  7. #include "raster3d_intern.h"
  8. /*---------------------------------------------------------------------------*/
  9. int Rast3d_is_xdr_null_num(const void *num, int isFloat)
  10. {
  11. static const char null_bytes[8] = {
  12. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
  13. };
  14. return memcmp(num, null_bytes, isFloat ? 4 : 8) == 0;
  15. }
  16. /*---------------------------------------------------------------------------*/
  17. int Rast3d_is_xdr_null_float(const float *f)
  18. {
  19. return Rast3d_is_xdr_null_num(f, 1);
  20. }
  21. /*---------------------------------------------------------------------------*/
  22. int Rast3d_is_xdr_null_double(const double *d)
  23. {
  24. return Rast3d_is_xdr_null_num(d, 0);
  25. }
  26. /*---------------------------------------------------------------------------*/
  27. void Rast3d_set_xdr_null_num(void *num, int isFloat)
  28. {
  29. static const char null_bytes[8] = {
  30. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
  31. };
  32. memcpy(num, null_bytes, isFloat ? 4 : 8);
  33. }
  34. /*---------------------------------------------------------------------------*/
  35. void Rast3d_set_xdr_null_double(double *d)
  36. {
  37. Rast3d_set_xdr_null_num(d, 0);
  38. }
  39. /*---------------------------------------------------------------------------*/
  40. void Rast3d_set_xdr_null_float(float *f)
  41. {
  42. Rast3d_set_xdr_null_num(f, 1);
  43. }
  44. /*---------------------------------------------------------------------------*/
  45. static size_t xdr_off;
  46. int Rast3d_init_fp_xdr(RASTER3D_Map * map, int misuseBytes)
  47. /* nof addtl bytes allocated for the xdr array so that */
  48. /* the array can also be (mis)used for other purposes */
  49. {
  50. if (xdr == NULL) {
  51. xdrLength = map->tileSize * RASTER3D_MAX(map->numLengthExtern,
  52. map->numLengthIntern) +
  53. misuseBytes;
  54. xdr = Rast3d_malloc(xdrLength);
  55. if (xdr == NULL) {
  56. Rast3d_error("Rast3d_init_fp_xdr: error in Rast3d_malloc");
  57. return 0;
  58. }
  59. }
  60. else if (map->tileSize * RASTER3D_MAX(map->numLengthExtern,
  61. map->numLengthIntern) + misuseBytes
  62. > xdrLength) {
  63. xdrLength = map->tileSize * RASTER3D_MAX(map->numLengthExtern,
  64. map->numLengthIntern) +
  65. misuseBytes;
  66. xdr = Rast3d_realloc(xdr, xdrLength);
  67. if (xdr == NULL) {
  68. Rast3d_error("Rast3d_init_fp_xdr: error in Rast3d_realloc");
  69. return 0;
  70. }
  71. }
  72. return 1;
  73. }
  74. /*---------------------------------------------------------------------------*/
  75. static void *xdrTmp;
  76. static int dstType, srcType, type, externLength, eltLength, isFloat, useXdr;
  77. static double tmpValue, *tmp;
  78. int Rast3d_init_copy_to_xdr(RASTER3D_Map * map, int sType)
  79. {
  80. xdrTmp = xdr;
  81. useXdr = map->useXdr;
  82. srcType = sType;
  83. if (map->useXdr == RASTER3D_USE_XDR)
  84. xdr_off = 0;
  85. type = map->type;
  86. isFloat = (type == FCELL_TYPE);
  87. externLength = Rast3d_extern_length(type);
  88. eltLength = Rast3d_length(srcType);
  89. tmp = &tmpValue;
  90. return 1;
  91. }
  92. /*---------------------------------------------------------------------------*/
  93. static int xdr_put(const void *src)
  94. {
  95. if (isFloat) {
  96. if (xdr_off + RASTER3D_XDR_FLOAT_LENGTH > xdrLength)
  97. return 0;
  98. G_xdr_put_float((char*)xdr + xdr_off, src);
  99. xdr_off += RASTER3D_XDR_FLOAT_LENGTH;
  100. }
  101. else {
  102. if (xdr_off + RASTER3D_XDR_DOUBLE_LENGTH > xdrLength)
  103. return 0;
  104. G_xdr_put_double((char*)xdr + xdr_off, src);
  105. xdr_off += RASTER3D_XDR_DOUBLE_LENGTH;
  106. }
  107. return 1;
  108. }
  109. /*---------------------------------------------------------------------------*/
  110. int Rast3d_copy_to_xdr(const void *src, int nofNum)
  111. {
  112. int i;
  113. if (useXdr == RASTER3D_NO_XDR) {
  114. Rast3d_copy_values(src, 0, srcType, xdrTmp, 0, type, nofNum);
  115. xdrTmp = G_incr_void_ptr(xdrTmp, nofNum * Rast3d_extern_length(type));
  116. return 1;
  117. }
  118. for (i = 0; i < nofNum; i++, src = G_incr_void_ptr(src, eltLength)) {
  119. if (Rast3d_is_null_value_num(src, srcType)) {
  120. Rast3d_set_xdr_null_num(xdrTmp, isFloat);
  121. xdr_off += externLength;
  122. }
  123. else {
  124. if (type == srcType) {
  125. if (!xdr_put(src)) {
  126. Rast3d_error("Rast3d_copy_to_xdr: writing xdr failed");
  127. return 0;
  128. }
  129. }
  130. else {
  131. if (type == FCELL_TYPE)
  132. *((float *)tmp) = (float)*((double *)src);
  133. else
  134. *((double *)tmp) = (double)*((float *)src);
  135. if (!xdr_put(tmp)) {
  136. Rast3d_error("Rast3d_copy_to_xdr: writing xdr failed");
  137. return 0;
  138. }
  139. }
  140. }
  141. xdrTmp = G_incr_void_ptr(xdrTmp, externLength);
  142. }
  143. return 1;
  144. }
  145. /*---------------------------------------------------------------------------*/
  146. int Rast3d_init_copy_from_xdr(RASTER3D_Map * map, int dType)
  147. {
  148. xdrTmp = xdr;
  149. useXdr = map->useXdr;
  150. dstType = dType;
  151. if (useXdr == RASTER3D_USE_XDR)
  152. xdr_off = 0;
  153. type = map->type;
  154. isFloat = (type == FCELL_TYPE);
  155. externLength = Rast3d_extern_length(type);
  156. eltLength = Rast3d_length(dstType);
  157. tmp = &tmpValue;
  158. return 1;
  159. }
  160. /*---------------------------------------------------------------------------*/
  161. static int xdr_get(void *src)
  162. {
  163. if (isFloat) {
  164. if (xdr_off + RASTER3D_XDR_FLOAT_LENGTH > xdrLength)
  165. return 0;
  166. G_xdr_get_float(src, (char*)xdr + xdr_off);
  167. xdr_off += RASTER3D_XDR_FLOAT_LENGTH;
  168. }
  169. else {
  170. if (xdr_off + RASTER3D_XDR_DOUBLE_LENGTH > xdrLength)
  171. return 0;
  172. G_xdr_get_double(src, (char*)xdr + xdr_off);
  173. xdr_off += RASTER3D_XDR_DOUBLE_LENGTH;
  174. }
  175. return 1;
  176. }
  177. /*---------------------------------------------------------------------------*/
  178. int Rast3d_copy_from_xdr(int nofNum, void *dst)
  179. {
  180. int i;
  181. if (useXdr == RASTER3D_NO_XDR) {
  182. Rast3d_copy_values(xdrTmp, 0, type, dst, 0, dstType, nofNum);
  183. xdrTmp = G_incr_void_ptr(xdrTmp, nofNum * Rast3d_extern_length(type));
  184. return 1;
  185. }
  186. for (i = 0; i < nofNum; i++, dst = G_incr_void_ptr(dst, eltLength)) {
  187. if (Rast3d_is_xdr_null_num(xdrTmp, isFloat)) {
  188. Rast3d_set_null_value(dst, 1, dstType);
  189. xdr_off += externLength;
  190. }
  191. else {
  192. if (type == dstType) {
  193. if (!xdr_get(dst)) {
  194. Rast3d_error("Rast3d_copy_from_xdr: reading xdr failed");
  195. return 0;
  196. }
  197. }
  198. else {
  199. if (!xdr_get(tmp)) {
  200. Rast3d_error("Rast3d_copy_from_xdr: reading xdr failed");
  201. return 0;
  202. }
  203. if (type == FCELL_TYPE)
  204. *((double *)dst) = (double)*((float *)tmp);
  205. else
  206. *((float *)dst) = (float)*((double *)tmp);
  207. }
  208. }
  209. xdrTmp = G_incr_void_ptr(xdrTmp, externLength);
  210. }
  211. return 1;
  212. }