GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-112dd97adf
fpxdr.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <unistd.h>
6 
7 #include <grass/raster.h>
8 
9 #include "raster3d_intern.h"
10 
11 /*---------------------------------------------------------------------------*/
12 
13 int Rast3d_is_xdr_null_num(const void *num, int isFloat)
14 {
15  static const char null_bytes[8] = {(char)0xFF, (char)0xFF, (char)0xFF,
16  (char)0xFF, (char)0xFF, (char)0xFF,
17  (char)0xFF, (char)0xFF};
18 
19  return memcmp(num, null_bytes, isFloat ? 4 : 8) == 0;
20 }
21 
22 /*---------------------------------------------------------------------------*/
23 
24 int Rast3d_is_xdr_null_float(const float *f)
25 {
26  return Rast3d_is_xdr_null_num(f, 1);
27 }
28 
29 /*---------------------------------------------------------------------------*/
30 
31 int Rast3d_is_xdr_null_double(const double *d)
32 {
33  return Rast3d_is_xdr_null_num(d, 0);
34 }
35 
36 /*---------------------------------------------------------------------------*/
37 
38 void Rast3d_set_xdr_null_num(void *num, int isFloat)
39 {
40  static const char null_bytes[8] = {(char)0xFF, (char)0xFF, (char)0xFF,
41  (char)0xFF, (char)0xFF, (char)0xFF,
42  (char)0xFF, (char)0xFF};
43 
44  memcpy(num, null_bytes, isFloat ? 4 : 8);
45 }
46 
47 /*---------------------------------------------------------------------------*/
48 
50 {
52 }
53 
54 /*---------------------------------------------------------------------------*/
55 
57 {
59 }
60 
61 /*---------------------------------------------------------------------------*/
62 
63 static size_t xdr_off;
64 
65 int Rast3d_init_fp_xdr(RASTER3D_Map *map, int misuseBytes)
66 /* nof addtl bytes allocated for the xdr array so that */
67 /* the array can also be (mis)used for other purposes */
68 {
69  if (xdr == NULL) {
71  map->numLengthIntern) +
72  misuseBytes;
74  if (xdr == NULL) {
75  Rast3d_error("Rast3d_init_fp_xdr: error in Rast3d_malloc");
76  return 0;
77  }
78  }
79  else if (map->tileSize *
81  misuseBytes >
82  xdrLength) {
84  map->numLengthIntern) +
85  misuseBytes;
87  if (xdr == NULL) {
88  Rast3d_error("Rast3d_init_fp_xdr: error in Rast3d_realloc");
89  return 0;
90  }
91  }
92 
93  return 1;
94 }
95 
96 /*---------------------------------------------------------------------------*/
97 
98 static void *xdrTmp;
99 static int dstType, srcType, type, externLength, eltLength, isFloat, useXdr;
100 static double tmpValue, *tmp;
101 
103 {
104  xdrTmp = xdr;
105  useXdr = map->useXdr;
106  srcType = sType;
107 
108  if (map->useXdr == RASTER3D_USE_XDR)
109  xdr_off = 0;
110 
111  type = map->type;
112  isFloat = (type == FCELL_TYPE);
113  externLength = Rast3d_extern_length(type);
114  eltLength = Rast3d_length(srcType);
115  tmp = &tmpValue;
116 
117  return 1;
118 }
119 
120 /*---------------------------------------------------------------------------*/
121 
122 static int xdr_put(const void *src)
123 {
124  if (isFloat) {
125  if (xdr_off + RASTER3D_XDR_FLOAT_LENGTH > (size_t)xdrLength)
126  return 0;
127  G_xdr_put_float((char *)xdr + xdr_off, src);
128  xdr_off += RASTER3D_XDR_FLOAT_LENGTH;
129  }
130  else {
131  if (xdr_off + RASTER3D_XDR_DOUBLE_LENGTH > (size_t)xdrLength)
132  return 0;
133  G_xdr_put_double((char *)xdr + xdr_off, src);
134  xdr_off += RASTER3D_XDR_DOUBLE_LENGTH;
135  }
136  return 1;
137 }
138 
139 /*---------------------------------------------------------------------------*/
140 
141 int Rast3d_copy_to_xdr(const void *src, int nofNum)
142 {
143  int i;
144 
145  if (useXdr == RASTER3D_NO_XDR) {
146  Rast3d_copy_values(src, 0, srcType, xdrTmp, 0, type, nofNum);
147  xdrTmp = G_incr_void_ptr(xdrTmp, nofNum * Rast3d_extern_length(type));
148  return 1;
149  }
150 
151  for (i = 0; i < nofNum; i++, src = G_incr_void_ptr(src, eltLength)) {
152 
153  if (Rast3d_is_null_value_num(src, srcType)) {
154  Rast3d_set_xdr_null_num(xdrTmp, isFloat);
155  xdr_off += externLength;
156  }
157  else {
158  if (type == srcType) {
159  if (!xdr_put(src)) {
160  Rast3d_error("Rast3d_copy_to_xdr: writing xdr failed");
161  return 0;
162  }
163  }
164  else {
165  if (type == FCELL_TYPE)
166  *((float *)tmp) = (float)*((double *)src);
167  else
168  *((double *)tmp) = (double)*((float *)src);
169  if (!xdr_put(tmp)) {
170  Rast3d_error("Rast3d_copy_to_xdr: writing xdr failed");
171  return 0;
172  }
173  }
174  }
175 
176  xdrTmp = G_incr_void_ptr(xdrTmp, externLength);
177  }
178 
179  return 1;
180 }
181 
182 /*---------------------------------------------------------------------------*/
183 
185 {
186  xdrTmp = xdr;
187  useXdr = map->useXdr;
188  dstType = dType;
189 
190  if (useXdr == RASTER3D_USE_XDR)
191  xdr_off = 0;
192 
193  type = map->type;
194  isFloat = (type == FCELL_TYPE);
195  externLength = Rast3d_extern_length(type);
196  eltLength = Rast3d_length(dstType);
197  tmp = &tmpValue;
198 
199  return 1;
200 }
201 
202 /*---------------------------------------------------------------------------*/
203 
204 static int xdr_get(void *src)
205 {
206  if (isFloat) {
207  if (xdr_off + RASTER3D_XDR_FLOAT_LENGTH > (size_t)xdrLength)
208  return 0;
209  G_xdr_get_float(src, (char *)xdr + xdr_off);
210  xdr_off += RASTER3D_XDR_FLOAT_LENGTH;
211  }
212  else {
213  if (xdr_off + RASTER3D_XDR_DOUBLE_LENGTH > (size_t)xdrLength)
214  return 0;
215  G_xdr_get_double(src, (char *)xdr + xdr_off);
216  xdr_off += RASTER3D_XDR_DOUBLE_LENGTH;
217  }
218  return 1;
219 }
220 
221 /*---------------------------------------------------------------------------*/
222 
223 int Rast3d_copy_from_xdr(int nofNum, void *dst)
224 {
225  int i;
226 
227  if (useXdr == RASTER3D_NO_XDR) {
228  Rast3d_copy_values(xdrTmp, 0, type, dst, 0, dstType, nofNum);
229  xdrTmp = G_incr_void_ptr(xdrTmp, nofNum * Rast3d_extern_length(type));
230  return 1;
231  }
232 
233  for (i = 0; i < nofNum; i++, dst = G_incr_void_ptr(dst, eltLength)) {
234 
235  if (Rast3d_is_xdr_null_num(xdrTmp, isFloat)) {
236  Rast3d_set_null_value(dst, 1, dstType);
237  xdr_off += externLength;
238  }
239  else {
240  if (type == dstType) {
241  if (!xdr_get(dst)) {
242  Rast3d_error("Rast3d_copy_from_xdr: reading xdr failed");
243  return 0;
244  }
245  }
246  else {
247  if (!xdr_get(tmp)) {
248  Rast3d_error("Rast3d_copy_from_xdr: reading xdr failed");
249  return 0;
250  }
251  if (type == FCELL_TYPE)
252  *((double *)dst) = (double)*((float *)tmp);
253  else
254  *((float *)dst) = (float)*((double *)tmp);
255  }
256  }
257 
258  xdrTmp = G_incr_void_ptr(xdrTmp, externLength);
259  }
260 
261  return 1;
262 }
#define NULL
Definition: ccmath.h:32
void G_xdr_put_float(void *, const float *)
Definition: gis/xdr.c:84
void G_xdr_get_float(float *, const void *)
Definition: gis/xdr.c:79
void G_xdr_put_double(void *, const double *)
Definition: gis/xdr.c:94
void G_xdr_get_double(double *, const void *)
Definition: gis/xdr.c:89
#define G_incr_void_ptr(ptr, size)
Definition: defs/gis.h:81
void Rast3d_copy_values(const void *, int, int, void *, int, int, int)
Definition: raster3d/misc.c:50
void * Rast3d_realloc(void *, int)
Same as realloc (ptr, nBytes), except that in case of error Rast3d_error() is invoked.
void Rast3d_set_null_value(void *, int, int)
Fills the vector pointed to by c with nofElts NULL-values of type.
Definition: null.c:34
int Rast3d_is_null_value_num(const void *, int)
Definition: null.c:12
int Rast3d_length(int)
Definition: raster3d/misc.c:75
int Rast3d_extern_length(int)
Definition: raster3d/misc.c:87
void Rast3d_error(const char *,...) __attribute__((format(printf
void * Rast3d_malloc(int)
Same as malloc (nBytes), except that in case of error Rast3d_error() is invoked.
int Rast3d_init_copy_from_xdr(RASTER3D_Map *map, int dType)
Definition: fpxdr.c:184
int Rast3d_init_fp_xdr(RASTER3D_Map *map, int misuseBytes)
Definition: fpxdr.c:65
void Rast3d_set_xdr_null_num(void *num, int isFloat)
Definition: fpxdr.c:38
int Rast3d_init_copy_to_xdr(RASTER3D_Map *map, int sType)
Definition: fpxdr.c:102
void Rast3d_set_xdr_null_float(float *f)
Definition: fpxdr.c:56
void Rast3d_set_xdr_null_double(double *d)
Definition: fpxdr.c:49
int Rast3d_is_xdr_null_float(const float *f)
Definition: fpxdr.c:24
int Rast3d_is_xdr_null_num(const void *num, int isFloat)
Definition: fpxdr.c:13
int Rast3d_copy_from_xdr(int nofNum, void *dst)
Definition: fpxdr.c:223
int Rast3d_is_xdr_null_double(const double *d)
Definition: fpxdr.c:31
int Rast3d_copy_to_xdr(const void *src, int nofNum)
Definition: fpxdr.c:141
void * xdr
int xdrLength
#define RASTER3D_USE_XDR
#define RASTER3D_XDR_DOUBLE_LENGTH
#define RASTER3D_NO_XDR
#define RASTER3D_XDR_FLOAT_LENGTH
#define RASTER3D_MAX(a, b)
#define FCELL_TYPE
Definition: raster.h:12
int numLengthExtern
Definition: raster3d.h:173
int tileSize
Definition: raster3d.h:180
int numLengthIntern
Definition: raster3d.h:176