/src/postgis/liblwgeom/lwin_wkb.c
Line | Count | Source |
1 | | /********************************************************************** |
2 | | * |
3 | | * PostGIS - Spatial Types for PostgreSQL |
4 | | * http://postgis.net |
5 | | * |
6 | | * PostGIS is free software: you can redistribute it and/or modify |
7 | | * it under the terms of the GNU General Public License as published by |
8 | | * the Free Software Foundation, either version 2 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * PostGIS is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU General Public License |
17 | | * along with PostGIS. If not, see <http://www.gnu.org/licenses/>. |
18 | | * |
19 | | ********************************************************************** |
20 | | * |
21 | | * Copyright (C) 2009 Paul Ramsey <pramsey@cleverelephant.ca> |
22 | | * |
23 | | **********************************************************************/ |
24 | | |
25 | | |
26 | | #include "../postgis_config.h" |
27 | | /*#define POSTGIS_DEBUG_LEVEL 4*/ |
28 | | #include "liblwgeom_internal.h" /* NOTE: includes lwgeom_log.h */ |
29 | | #include "lwgeom_log.h" |
30 | | #include <math.h> |
31 | | #include <limits.h> |
32 | | |
33 | | /** |
34 | | * Used for passing the parse state between the parsing functions. |
35 | | */ |
36 | | typedef struct |
37 | | { |
38 | | const uint8_t *wkb; /* Points to start of WKB */ |
39 | | int32_t srid; /* Current SRID we are handling */ |
40 | | size_t wkb_size; /* Expected size of WKB */ |
41 | | int8_t swap_bytes; /* Do an endian flip? */ |
42 | | int8_t check; /* Simple validity checks on geometries */ |
43 | | int8_t lwtype; /* Current type we are handling */ |
44 | | int8_t has_z; /* Z? */ |
45 | | int8_t has_m; /* M? */ |
46 | | int8_t has_srid; /* SRID? */ |
47 | | int8_t error; /* An error was found (not enough bytes to read) */ |
48 | | uint8_t depth; /* Current recursion level (to prevent stack overflows). Maxes at LW_PARSER_MAX_DEPTH */ |
49 | | size_t pos_offset; /* Current parse offset from wkb */ |
50 | | const uint8_t *pos; /* Current parse position */ |
51 | | } wkb_parse_state; |
52 | | |
53 | | |
54 | | /** |
55 | | * Internal function declarations. |
56 | | */ |
57 | | LWGEOM* lwgeom_from_wkb_state(wkb_parse_state *s); |
58 | | |
59 | | |
60 | | |
61 | | /**********************************************************************/ |
62 | | |
63 | | /* Our static character->number map. Anything > 15 is invalid */ |
64 | | static uint8_t hex2char[256] = { |
65 | | /* not Hex characters */ |
66 | | 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20, |
67 | | 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20, |
68 | | 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20, |
69 | | /* 0-9 */ |
70 | | 0,1,2,3,4,5,6,7,8,9,20,20,20,20,20,20, |
71 | | /* A-F */ |
72 | | 20,10,11,12,13,14,15,20,20,20,20,20,20,20,20,20, |
73 | | /* not Hex characters */ |
74 | | 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20, |
75 | | /* a-f */ |
76 | | 20,10,11,12,13,14,15,20,20,20,20,20,20,20,20,20, |
77 | | 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20, |
78 | | /* not Hex characters (upper 128 characters) */ |
79 | | 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20, |
80 | | 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20, |
81 | | 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20, |
82 | | 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20, |
83 | | 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20, |
84 | | 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20, |
85 | | 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20, |
86 | | 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20 |
87 | | }; |
88 | | |
89 | | |
90 | | uint8_t* bytes_from_hexbytes(const char *hexbuf, size_t hexsize) |
91 | 0 | { |
92 | 0 | uint8_t *buf = NULL; |
93 | 0 | register uint8_t h1, h2; |
94 | 0 | uint32_t i; |
95 | |
|
96 | 0 | if( hexsize % 2 ) |
97 | 0 | lwerror("Invalid hex string, length (%zu) has to be a multiple of two!", hexsize); |
98 | |
|
99 | 0 | buf = lwalloc(hexsize/2); |
100 | |
|
101 | 0 | if( ! buf ) |
102 | 0 | lwerror("Unable to allocate memory buffer."); |
103 | |
|
104 | 0 | for( i = 0; i < hexsize/2; i++ ) |
105 | 0 | { |
106 | 0 | h1 = hex2char[(uint8_t)hexbuf[2*i]]; |
107 | 0 | h2 = hex2char[(uint8_t)hexbuf[2*i+1]]; |
108 | 0 | if( h1 > 15 ) |
109 | 0 | lwerror("Invalid hex character (%c) encountered", hexbuf[2*i]); |
110 | 0 | if( h2 > 15 ) |
111 | 0 | lwerror("Invalid hex character (%c) encountered", hexbuf[2*i+1]); |
112 | | /* First character is high bits, second is low bits */ |
113 | 0 | buf[i] = ((h1 & 0x0F) << 4) | (h2 & 0x0F); |
114 | 0 | } |
115 | 0 | return buf; |
116 | 0 | } |
117 | | |
118 | | |
119 | | /**********************************************************************/ |
120 | | |
121 | | |
122 | | |
123 | | /** |
124 | | * Check that we are not about to read off the end of the WKB |
125 | | * array. |
126 | | */ |
127 | | static inline void wkb_parse_state_check(wkb_parse_state *s, size_t next) |
128 | 19.1M | { |
129 | 19.1M | if (next > s->wkb_size || s->pos_offset > s->wkb_size - next) |
130 | 2.43k | { |
131 | 2.43k | lwerror("WKB structure does not match expected size!"); |
132 | 2.43k | s->error = LW_TRUE; |
133 | 2.43k | } |
134 | 19.1M | } |
135 | | |
136 | | static inline void |
137 | | wkb_parse_state_advance(wkb_parse_state *s, size_t next) |
138 | 19.0M | { |
139 | 19.0M | s->pos_offset += next; |
140 | 19.0M | s->pos += next; |
141 | 19.0M | } |
142 | | |
143 | | /** |
144 | | * Resolve a raw WKB type number into the parser state' internal type and flags. |
145 | | * |
146 | | * Interprets WKB type encodings (including extended flag bits and ISO-style |
147 | | * 1000/2000/3000 modifiers) and updates the provided parse state: |
148 | | * - sets s->has_z, s->has_m, s->has_srid according to detected flags, |
149 | | * - sets s->lwtype to the corresponding internal lwgeom type enum. |
150 | | * |
151 | | * Recognizes standard WKB types, ISO extended encodings (where 1000/2000/3000 |
152 | | * ranges indicate Z/M presence), explicit high-bit flag encodings, and a few |
153 | | * legacy PostGIS values (e.g., old Curve/Surface codes). On unrecognized or |
154 | | * out-of-range type numbers an error is reported via lwerror and the state is |
155 | | * left with whatever values were set before the error. |
156 | | */ |
157 | | static void lwtype_from_wkb_state(wkb_parse_state *s, uint32_t wkb_type) |
158 | 406k | { |
159 | 406k | uint32_t wkb_simple_type; |
160 | | |
161 | 406k | LWDEBUG(4, "Entered function"); |
162 | | |
163 | 406k | s->has_z = LW_FALSE; |
164 | 406k | s->has_m = LW_FALSE; |
165 | 406k | s->has_srid = LW_FALSE; |
166 | | |
167 | | /* If any of the higher bits are set, this is probably an extended type. */ |
168 | 406k | if( wkb_type & 0xF0000000 ) |
169 | 104k | { |
170 | 104k | if( wkb_type & WKBZOFFSET ) s->has_z = LW_TRUE; |
171 | 104k | if( wkb_type & WKBMOFFSET ) s->has_m = LW_TRUE; |
172 | 104k | if( wkb_type & WKBSRIDFLAG ) s->has_srid = LW_TRUE; |
173 | 104k | LWDEBUGF(4, "Extended type: has_z=%d has_m=%d has_srid=%d", s->has_z, s->has_m, s->has_srid); |
174 | 104k | } |
175 | | |
176 | | /* Mask off the flags */ |
177 | 406k | wkb_type = wkb_type & 0x0FFFFFFF; |
178 | | |
179 | | /* Catch strange Oracle WKB type numbers */ |
180 | 406k | if ( wkb_type >= 4000 ) { |
181 | 273 | lwerror("Unknown WKB type (%d)!", wkb_type); |
182 | 273 | return; |
183 | 273 | } |
184 | | |
185 | | /* Strip out just the type number (1-12) from the ISO number (eg 3001-3012) */ |
186 | 406k | wkb_simple_type = wkb_type % 1000; |
187 | | |
188 | | /* Extract the Z/M information from ISO style numbers */ |
189 | 406k | if( wkb_type >= 3000 && wkb_type < 4000 ) |
190 | 1.43k | { |
191 | 1.43k | s->has_z = LW_TRUE; |
192 | 1.43k | s->has_m = LW_TRUE; |
193 | 1.43k | } |
194 | 405k | else if ( wkb_type >= 2000 && wkb_type < 3000 ) |
195 | 811 | { |
196 | 811 | s->has_m = LW_TRUE; |
197 | 811 | } |
198 | 404k | else if ( wkb_type >= 1000 && wkb_type < 2000 ) |
199 | 3.88k | { |
200 | 3.88k | s->has_z = LW_TRUE; |
201 | 3.88k | } |
202 | | |
203 | 406k | switch (wkb_simple_type) |
204 | 406k | { |
205 | 14.0k | case WKB_POINT_TYPE: |
206 | 14.0k | s->lwtype = POINTTYPE; |
207 | 14.0k | break; |
208 | 83.5k | case WKB_LINESTRING_TYPE: |
209 | 83.5k | s->lwtype = LINETYPE; |
210 | 83.5k | break; |
211 | 29.0k | case WKB_POLYGON_TYPE: |
212 | 29.0k | s->lwtype = POLYGONTYPE; |
213 | 29.0k | break; |
214 | 2.67k | case WKB_MULTIPOINT_TYPE: |
215 | 2.67k | s->lwtype = MULTIPOINTTYPE; |
216 | 2.67k | break; |
217 | 2.90k | case WKB_MULTILINESTRING_TYPE: |
218 | 2.90k | s->lwtype = MULTILINETYPE; |
219 | 2.90k | break; |
220 | 1.96k | case WKB_MULTIPOLYGON_TYPE: |
221 | 1.96k | s->lwtype = MULTIPOLYGONTYPE; |
222 | 1.96k | break; |
223 | 38.0k | case WKB_GEOMETRYCOLLECTION_TYPE: |
224 | 38.0k | s->lwtype = COLLECTIONTYPE; |
225 | 38.0k | break; |
226 | 68.0k | case WKB_CIRCULARSTRING_TYPE: |
227 | 68.0k | s->lwtype = CIRCSTRINGTYPE; |
228 | 68.0k | break; |
229 | 46.4k | case WKB_COMPOUNDCURVE_TYPE: |
230 | 46.4k | s->lwtype = COMPOUNDTYPE; |
231 | 46.4k | break; |
232 | 13.4k | case WKB_CURVEPOLYGON_TYPE: |
233 | 13.4k | s->lwtype = CURVEPOLYTYPE; |
234 | 13.4k | break; |
235 | 6.06k | case WKB_MULTICURVE_TYPE: |
236 | 6.06k | s->lwtype = MULTICURVETYPE; |
237 | 6.06k | break; |
238 | 8.62k | case WKB_MULTISURFACE_TYPE: |
239 | 8.62k | s->lwtype = MULTISURFACETYPE; |
240 | 8.62k | break; |
241 | 4.71k | case WKB_POLYHEDRALSURFACE_TYPE: |
242 | 4.71k | s->lwtype = POLYHEDRALSURFACETYPE; |
243 | 4.71k | break; |
244 | 3.14k | case WKB_TIN_TYPE: |
245 | 3.14k | s->lwtype = TINTYPE; |
246 | 3.14k | break; |
247 | 31.5k | case WKB_TRIANGLE_TYPE: |
248 | 31.5k | s->lwtype = TRIANGLETYPE; |
249 | 31.5k | break; |
250 | 39.6k | case WKB_NURBSCURVE_TYPE: |
251 | 39.6k | s->lwtype = NURBSCURVETYPE; |
252 | 39.6k | break; |
253 | | |
254 | | /* PostGIS 1.5 emits 13, 14 for CurvePolygon, MultiCurve */ |
255 | | /* These numbers aren't SQL/MM (numbers currently only */ |
256 | | /* go up to 12. We can handle the old data here (for now??) */ |
257 | | /* converting them into the lwtypes that are intended. */ |
258 | 3.65k | case WKB_CURVE_TYPE: |
259 | 3.65k | s->lwtype = CURVEPOLYTYPE; |
260 | 3.65k | break; |
261 | 8.82k | case WKB_SURFACE_TYPE: |
262 | 8.82k | s->lwtype = MULTICURVETYPE; |
263 | 8.82k | break; |
264 | | |
265 | 189 | default: /* Error! */ |
266 | 189 | lwerror("Unknown WKB type (%d)! Full WKB type number was (%d).", wkb_simple_type, wkb_type); |
267 | 189 | break; |
268 | 406k | } |
269 | | |
270 | 406k | LWDEBUGF(4,"Got lwtype %s (%u)", lwtype_name(s->lwtype), s->lwtype); |
271 | | |
272 | 406k | return; |
273 | 406k | } |
274 | | |
275 | | /** |
276 | | * Byte |
277 | | * Read a byte and advance the parse state forward. |
278 | | */ |
279 | | static char byte_from_wkb_state(wkb_parse_state *s) |
280 | 6.80M | { |
281 | 6.80M | char char_value = 0; |
282 | 6.80M | LWDEBUG(4, "Entered function"); |
283 | | |
284 | 6.80M | wkb_parse_state_check(s, WKB_BYTE_SIZE); |
285 | 6.80M | if (s->error) |
286 | 0 | return 0; |
287 | 6.80M | LWDEBUG(4, "Passed state check"); |
288 | | |
289 | 6.80M | char_value = s->pos[0]; |
290 | 6.80M | LWDEBUGF(4, "Read byte value: %x", char_value); |
291 | 6.80M | wkb_parse_state_advance(s, WKB_BYTE_SIZE); |
292 | | |
293 | 6.80M | return char_value; |
294 | 6.80M | } |
295 | | |
296 | | |
297 | | /** |
298 | | * Int32 |
299 | | * Read 4-byte integer and advance the parse state forward. |
300 | | */ |
301 | | static uint32_t integer_from_wkb_state(wkb_parse_state *s) |
302 | 916k | { |
303 | 916k | uint32_t i = 0; |
304 | | |
305 | 916k | wkb_parse_state_check(s, WKB_INT_SIZE); |
306 | 916k | if (s->error) |
307 | 0 | return 0; |
308 | | |
309 | 916k | memcpy(&i, s->pos, WKB_INT_SIZE); |
310 | | |
311 | | /* Swap? Copy into a stack-allocated integer. */ |
312 | 916k | if( s->swap_bytes ) |
313 | 340k | { |
314 | 340k | int j = 0; |
315 | 340k | uint8_t tmp; |
316 | | |
317 | 1.02M | for( j = 0; j < WKB_INT_SIZE/2; j++ ) |
318 | 680k | { |
319 | 680k | tmp = ((uint8_t*)(&i))[j]; |
320 | 680k | ((uint8_t*)(&i))[j] = ((uint8_t*)(&i))[WKB_INT_SIZE - j - 1]; |
321 | 680k | ((uint8_t*)(&i))[WKB_INT_SIZE - j - 1] = tmp; |
322 | 680k | } |
323 | 340k | } |
324 | | |
325 | 916k | wkb_parse_state_advance(s, WKB_INT_SIZE); |
326 | 916k | return i; |
327 | 916k | } |
328 | | |
329 | | /** |
330 | | * Double |
331 | | * Read an 8-byte double and advance the parse state forward. |
332 | | */ |
333 | | static double double_from_wkb_state(wkb_parse_state *s) |
334 | 11.2M | { |
335 | 11.2M | double d = 0; |
336 | | |
337 | | /* Check bounds before reading */ |
338 | 11.2M | wkb_parse_state_check(s, WKB_DOUBLE_SIZE); |
339 | 11.2M | if (s->error) return 0.0; |
340 | | |
341 | 11.2M | memcpy(&d, s->pos, WKB_DOUBLE_SIZE); |
342 | | |
343 | | /* Swap? Copy into a stack-allocated integer. */ |
344 | 11.2M | if( s->swap_bytes ) |
345 | 371k | { |
346 | 371k | int i = 0; |
347 | 371k | uint8_t tmp; |
348 | | |
349 | 1.85M | for( i = 0; i < WKB_DOUBLE_SIZE/2; i++ ) |
350 | 1.48M | { |
351 | 1.48M | tmp = ((uint8_t*)(&d))[i]; |
352 | 1.48M | ((uint8_t*)(&d))[i] = ((uint8_t*)(&d))[WKB_DOUBLE_SIZE - i - 1]; |
353 | 1.48M | ((uint8_t*)(&d))[WKB_DOUBLE_SIZE - i - 1] = tmp; |
354 | 1.48M | } |
355 | 371k | } |
356 | | |
357 | 11.2M | wkb_parse_state_advance(s, WKB_DOUBLE_SIZE); |
358 | 11.2M | return d; |
359 | 11.2M | } |
360 | | |
361 | | |
362 | | |
363 | | /** |
364 | | * Set the swap bytes flag depending on the endianness |
365 | | * of the machine and the endianness of the input data. |
366 | | * If they differ, we must swap, otherwise we can copy. |
367 | | */ |
368 | | static void wkb_swap_bytes(wkb_parse_state *s) |
369 | 3.60M | { |
370 | 3.60M | char wkb_little_endian; |
371 | | |
372 | | /* Fail when handed incorrect starting byte */ |
373 | 3.60M | wkb_little_endian = byte_from_wkb_state(s); |
374 | 3.60M | if (s->error) |
375 | 0 | lwerror("Invalid endian flag value encountered."); |
376 | | |
377 | 3.60M | if( wkb_little_endian != 1 && wkb_little_endian != 0 ) |
378 | 168 | { |
379 | 168 | s->error = LW_TRUE; |
380 | 168 | lwerror("Invalid endian flag value encountered."); |
381 | 168 | } |
382 | | |
383 | | /* Check the endianness of our input */ |
384 | 3.60M | s->swap_bytes = LW_FALSE; |
385 | | |
386 | | /* Machine arch is big endian, request is for little */ |
387 | 3.60M | if (IS_BIG_ENDIAN && wkb_little_endian) |
388 | 0 | s->swap_bytes = LW_TRUE; |
389 | | /* Machine arch is little endian, request is for big */ |
390 | 3.60M | else if ((!IS_BIG_ENDIAN) && (!wkb_little_endian)) |
391 | 177k | s->swap_bytes = LW_TRUE; |
392 | 3.60M | } |
393 | | |
394 | | |
395 | | |
396 | | /** |
397 | | * POINTARRAY |
398 | | * Read a dynamically sized point array and advance the parse state forward. |
399 | | * First read the number of points, then read the points. |
400 | | */ |
401 | | static POINTARRAY* ptarray_from_wkb_state(wkb_parse_state *s) |
402 | 183k | { |
403 | 183k | POINTARRAY *pa = NULL; |
404 | 183k | size_t pa_size; |
405 | 183k | uint32_t npoints = 0; |
406 | 183k | static uint32_t maxpoints = UINT_MAX / WKB_DOUBLE_SIZE / 4; |
407 | 183k | uint32_t ndims = 2 + s->has_z + s->has_m; |
408 | | |
409 | | /* Calculate the size of this point array. */ |
410 | 183k | npoints = integer_from_wkb_state(s); |
411 | 183k | if (s->error) |
412 | 0 | return NULL; |
413 | | |
414 | 183k | if (npoints > maxpoints) |
415 | 30 | { |
416 | 30 | s->error = LW_TRUE; |
417 | 30 | lwerror("Pointarray length (%d) is too large", npoints); |
418 | 30 | return NULL; |
419 | 30 | } |
420 | | |
421 | 183k | LWDEBUGF(4,"Pointarray has %d points", npoints); |
422 | | |
423 | | /* Empty! */ |
424 | 183k | if( npoints == 0 ) |
425 | 153k | return ptarray_construct(s->has_z, s->has_m, npoints); |
426 | | |
427 | | /* Does the data we want to read exist? */ |
428 | 29.3k | if (!lw_size_mul((size_t)npoints, ndims, &pa_size) || |
429 | 29.1k | !lw_size_mul(pa_size, WKB_DOUBLE_SIZE, &pa_size)) |
430 | 0 | { |
431 | 0 | s->error = LW_TRUE; |
432 | 0 | lwerror("Pointarray length (%d) is too large", npoints); |
433 | 0 | return NULL; |
434 | 0 | } |
435 | 29.3k | wkb_parse_state_check(s, pa_size); |
436 | 29.3k | if (s->error) |
437 | 0 | return NULL; |
438 | | |
439 | | /* If we're in a native endianness, we can just copy the data directly! */ |
440 | 29.3k | if( ! s->swap_bytes ) |
441 | 20.2k | { |
442 | 20.2k | pa = ptarray_construct_copy_data(s->has_z, s->has_m, npoints, (uint8_t*)s->pos); |
443 | 20.2k | wkb_parse_state_advance(s, pa_size); |
444 | 20.2k | } |
445 | | /* Otherwise we have to read each double, separately. */ |
446 | 9.13k | else |
447 | 9.13k | { |
448 | 9.13k | uint32_t i = 0; |
449 | 9.13k | double *dlist; |
450 | 9.13k | pa = ptarray_construct(s->has_z, s->has_m, npoints); |
451 | 9.13k | dlist = (double*)(pa->serialized_pointlist); |
452 | 317k | for( i = 0; i < npoints * ndims; i++ ) |
453 | 308k | { |
454 | 308k | dlist[i] = double_from_wkb_state(s); |
455 | 308k | } |
456 | 9.13k | } |
457 | | |
458 | 29.3k | return pa; |
459 | 29.3k | } |
460 | | |
461 | | /** |
462 | | * POINT |
463 | | * Read a WKB point, starting just after the endian byte, |
464 | | * type number and optional srid number. |
465 | | * Advance the parse state forward appropriately. |
466 | | * WKB point has just a set of doubles, with the quantity depending on the |
467 | | * dimension of the point, so this looks like a special case of the above |
468 | | * with only one point. |
469 | | */ |
470 | | static LWPOINT* lwpoint_from_wkb_state(wkb_parse_state *s) |
471 | 14.0k | { |
472 | 14.0k | static uint32_t npoints = 1; |
473 | 14.0k | POINTARRAY *pa = NULL; |
474 | 14.0k | size_t pa_size; |
475 | 14.0k | uint32_t ndims = 2; |
476 | 14.0k | const POINT2D *pt; |
477 | | |
478 | | /* Count the dimensions. */ |
479 | 14.0k | if( s->has_z ) ndims++; |
480 | 14.0k | if( s->has_m ) ndims++; |
481 | 14.0k | pa_size = ndims * WKB_DOUBLE_SIZE; |
482 | | |
483 | | /* Does the data we want to read exist? */ |
484 | 14.0k | wkb_parse_state_check(s, pa_size); |
485 | 14.0k | if (s->error) |
486 | 0 | return NULL; |
487 | | |
488 | | /* If we're in a native endianness, we can just copy the data directly! */ |
489 | 14.0k | if( ! s->swap_bytes ) |
490 | 6.26k | { |
491 | 6.26k | pa = ptarray_construct_copy_data(s->has_z, s->has_m, npoints, (uint8_t*)s->pos); |
492 | 6.26k | wkb_parse_state_advance(s, pa_size); |
493 | 6.26k | } |
494 | | /* Otherwise we have to read each double, separately */ |
495 | 7.79k | else |
496 | 7.79k | { |
497 | 7.79k | uint32_t i = 0; |
498 | 7.79k | double *dlist; |
499 | 7.79k | pa = ptarray_construct(s->has_z, s->has_m, npoints); |
500 | 7.79k | dlist = (double*)(pa->serialized_pointlist); |
501 | 27.0k | for( i = 0; i < ndims; i++ ) |
502 | 19.3k | { |
503 | 19.3k | dlist[i] = double_from_wkb_state(s); |
504 | 19.3k | } |
505 | 7.79k | } |
506 | | |
507 | | /* Check for POINT(NaN NaN) ==> POINT EMPTY */ |
508 | 14.0k | pt = getPoint2d_cp(pa, 0); |
509 | 14.0k | if ( isnan(pt->x) && isnan(pt->y) ) |
510 | 760 | { |
511 | 760 | ptarray_free(pa); |
512 | 760 | return lwpoint_construct_empty(s->srid, s->has_z, s->has_m); |
513 | 760 | } |
514 | 13.2k | else |
515 | 13.2k | { |
516 | 13.2k | return lwpoint_construct(s->srid, NULL, pa); |
517 | 13.2k | } |
518 | 14.0k | } |
519 | | |
520 | | /** |
521 | | * LINESTRING |
522 | | * Read a WKB linestring, starting just after the endian byte, |
523 | | * type number and optional srid number. Advance the parse state |
524 | | * forward appropriately. |
525 | | * There is only one pointarray in a linestring. Optionally |
526 | | * check for minimal following of rules (two point minimum). |
527 | | */ |
528 | | static LWLINE* lwline_from_wkb_state(wkb_parse_state *s) |
529 | 83.5k | { |
530 | 83.5k | POINTARRAY *pa = ptarray_from_wkb_state(s); |
531 | 83.5k | if (s->error) |
532 | 0 | return NULL; |
533 | | |
534 | 83.5k | if( pa == NULL || pa->npoints == 0 ) |
535 | 81.9k | { |
536 | 81.9k | if (pa) |
537 | 81.9k | ptarray_free(pa); |
538 | 81.9k | return lwline_construct_empty(s->srid, s->has_z, s->has_m); |
539 | 81.9k | } |
540 | | |
541 | 1.63k | if( s->check & LW_PARSER_CHECK_MINPOINTS && pa->npoints < 2 ) |
542 | 14 | { |
543 | 14 | lwerror("%s must have at least two points", lwtype_name(s->lwtype)); |
544 | 14 | return NULL; |
545 | 14 | } |
546 | | |
547 | 1.61k | return lwline_construct(s->srid, NULL, pa); |
548 | 1.63k | } |
549 | | |
550 | | /** |
551 | | * CIRCULARSTRING |
552 | | * Read a WKB circularstring, starting just after the endian byte, |
553 | | * type number and optional srid number. Advance the parse state |
554 | | * forward appropriately. |
555 | | * There is only one pointarray in a linestring. Optionally |
556 | | * check for minimal following of rules (three point minimum, |
557 | | * odd number of points). |
558 | | */ |
559 | | static LWCIRCSTRING* lwcircstring_from_wkb_state(wkb_parse_state *s) |
560 | 68.0k | { |
561 | 68.0k | POINTARRAY *pa = ptarray_from_wkb_state(s); |
562 | 68.0k | if (s->error) |
563 | 0 | return NULL; |
564 | | |
565 | 68.0k | if( pa == NULL || pa->npoints == 0 ) |
566 | 66.1k | { |
567 | 66.1k | if (pa) |
568 | 66.1k | ptarray_free(pa); |
569 | 66.1k | return lwcircstring_construct_empty(s->srid, s->has_z, s->has_m); |
570 | 66.1k | } |
571 | | |
572 | 1.87k | if( s->check & LW_PARSER_CHECK_MINPOINTS && pa->npoints < 3 ) |
573 | 3 | { |
574 | 3 | lwerror("%s must have at least three points", lwtype_name(s->lwtype)); |
575 | 3 | return NULL; |
576 | 3 | } |
577 | | |
578 | 1.86k | if( s->check & LW_PARSER_CHECK_ODD && ! (pa->npoints % 2) ) |
579 | 70 | { |
580 | 70 | lwerror("%s must have an odd number of points", lwtype_name(s->lwtype)); |
581 | 70 | return NULL; |
582 | 70 | } |
583 | | |
584 | 1.79k | return lwcircstring_construct(s->srid, NULL, pa); |
585 | 1.86k | } |
586 | | |
587 | | /** |
588 | | * POLYGON |
589 | | * Read a WKB polygon, starting just after the endian byte, |
590 | | * type number and optional srid number. Advance the parse state |
591 | | * forward appropriately. |
592 | | * First read the number of rings, then read each ring |
593 | | * (which are structured as point arrays) |
594 | | */ |
595 | | static LWPOLY* lwpoly_from_wkb_state(wkb_parse_state *s) |
596 | 29.0k | { |
597 | 29.0k | uint32_t nrings = integer_from_wkb_state(s); |
598 | 29.0k | if (s->error) |
599 | 0 | return NULL; |
600 | 29.0k | uint32_t i = 0; |
601 | 29.0k | LWPOLY *poly = lwpoly_construct_empty(s->srid, s->has_z, s->has_m); |
602 | | |
603 | 29.0k | LWDEBUGF(4,"Polygon has %d rings", nrings); |
604 | | |
605 | | /* Empty polygon? */ |
606 | 29.0k | if( nrings == 0 ) |
607 | 27.1k | return poly; |
608 | | |
609 | 28.9k | for( i = 0; i < nrings; i++ ) |
610 | 27.4k | { |
611 | 27.4k | POINTARRAY *pa = ptarray_from_wkb_state(s); |
612 | 27.4k | if (pa == NULL) |
613 | 0 | { |
614 | 0 | lwpoly_free(poly); |
615 | 0 | return NULL; |
616 | 0 | } |
617 | | |
618 | | /* Check for at least four points. */ |
619 | 27.4k | if (s->check & LW_PARSER_CHECK_MINPOINTS && pa->npoints < 4) |
620 | 130 | { |
621 | 130 | lwpoly_free(poly); |
622 | 130 | ptarray_free(pa); |
623 | 130 | LWDEBUGF(2, "%s must have at least four points in each ring", lwtype_name(s->lwtype)); |
624 | 130 | lwerror("%s must have at least four points in each ring", lwtype_name(s->lwtype)); |
625 | 130 | return NULL; |
626 | 130 | } |
627 | | |
628 | | /* Check that first and last points are the same. */ |
629 | 27.3k | if( s->check & LW_PARSER_CHECK_CLOSURE && ! ptarray_is_closed_2d(pa) ) |
630 | 285 | { |
631 | 285 | lwpoly_free(poly); |
632 | 285 | ptarray_free(pa); |
633 | 285 | LWDEBUGF(2, "%s must have closed rings", lwtype_name(s->lwtype)); |
634 | 285 | lwerror("%s must have closed rings", lwtype_name(s->lwtype)); |
635 | 285 | return NULL; |
636 | 285 | } |
637 | | |
638 | | /* Skip zero-member rings, they add nothing */ |
639 | 27.0k | if ( pa->npoints == 0) |
640 | 4.58k | { |
641 | 4.58k | LWDEBUGF(2, "Skipping empty ring [%d]", i); |
642 | 4.58k | ptarray_free(pa); |
643 | 4.58k | continue; |
644 | 4.58k | } |
645 | | |
646 | | /* Add ring to polygon */ |
647 | 22.4k | if ( lwpoly_add_ring(poly, pa) == LW_FAILURE ) |
648 | 0 | { |
649 | 0 | lwpoly_free(poly); |
650 | 0 | ptarray_free(pa); |
651 | 0 | LWDEBUG(2, "Unable to add ring to polygon"); |
652 | 0 | lwerror("Unable to add ring to polygon"); |
653 | 0 | return NULL; |
654 | 0 | } |
655 | | |
656 | 22.4k | } |
657 | 1.46k | return poly; |
658 | 1.88k | } |
659 | | |
660 | | /** |
661 | | * TRIANGLE |
662 | | * Read a WKB triangle, starting just after the endian byte, |
663 | | * type number and optional srid number. Advance the parse state |
664 | | * forward appropriately. |
665 | | * Triangles are encoded like polygons in WKB, but more like linestrings |
666 | | * as lwgeometries. |
667 | | */ |
668 | | static LWTRIANGLE* lwtriangle_from_wkb_state(wkb_parse_state *s) |
669 | 31.4k | { |
670 | 31.4k | uint32_t nrings = integer_from_wkb_state(s); |
671 | 31.4k | if (s->error) |
672 | 0 | return NULL; |
673 | | |
674 | | /* Empty triangle? */ |
675 | 31.4k | if( nrings == 0 ) |
676 | 27.1k | return lwtriangle_construct_empty(s->srid, s->has_z, s->has_m); |
677 | | |
678 | | /* Should be only one ring. */ |
679 | 4.34k | if (nrings != 1) |
680 | 121 | { |
681 | 121 | lwerror("Triangle has wrong number of rings: %d", nrings); |
682 | 121 | } |
683 | | |
684 | | /* There's only one ring, we hope? */ |
685 | 4.34k | POINTARRAY *pa = ptarray_from_wkb_state(s); |
686 | | |
687 | | /* If there's no points, return an empty triangle. */ |
688 | 4.34k | if (pa == NULL) |
689 | 0 | return lwtriangle_construct_empty(s->srid, s->has_z, s->has_m); |
690 | | |
691 | | /* Check for at least four points. */ |
692 | 4.34k | if (s->check & LW_PARSER_CHECK_MINPOINTS && pa->npoints < 4) |
693 | 82 | { |
694 | 82 | ptarray_free(pa); |
695 | 82 | lwerror("%s must have at least four points", lwtype_name(s->lwtype)); |
696 | 82 | return NULL; |
697 | 82 | } |
698 | | |
699 | 4.26k | if (s->check & LW_PARSER_CHECK_ZCLOSURE && !ptarray_is_closed_z(pa)) |
700 | 359 | { |
701 | 359 | ptarray_free(pa); |
702 | 359 | lwerror("%s must have closed rings", lwtype_name(s->lwtype)); |
703 | 359 | return NULL; |
704 | 359 | } |
705 | | |
706 | | /* Empty TRIANGLE starts w/ empty POINTARRAY, free it first */ |
707 | 3.90k | return lwtriangle_construct(s->srid, NULL, pa); |
708 | 4.26k | } |
709 | | |
710 | | /** |
711 | | * CURVEPOLYTYPE |
712 | | */ |
713 | | static LWCURVEPOLY* lwcurvepoly_from_wkb_state(wkb_parse_state *s) |
714 | 17.1k | { |
715 | 17.1k | uint32_t ngeoms = integer_from_wkb_state(s); |
716 | 17.1k | if (s->error) |
717 | 0 | return NULL; |
718 | 17.1k | LWCURVEPOLY *cp = lwcurvepoly_construct_empty(s->srid, s->has_z, s->has_m); |
719 | 17.1k | LWGEOM *geom = NULL; |
720 | 17.1k | uint32_t i; |
721 | | |
722 | | /* Empty collection? */ |
723 | 17.1k | if ( ngeoms == 0 ) |
724 | 10.1k | return cp; |
725 | | |
726 | 7.00k | s->depth++; |
727 | 7.00k | if (s->depth >= LW_PARSER_MAX_DEPTH) |
728 | 2 | { |
729 | 2 | lwgeom_free((LWGEOM *)cp); |
730 | 2 | lwerror("Geometry has too many chained curves"); |
731 | 2 | return NULL; |
732 | 2 | } |
733 | 124k | for ( i = 0; i < ngeoms; i++ ) |
734 | 117k | { |
735 | 117k | geom = lwgeom_from_wkb_state(s); |
736 | 117k | if ( lwcurvepoly_add_ring(cp, geom) == LW_FAILURE ) |
737 | 117 | { |
738 | 117 | lwgeom_free(geom); |
739 | 117 | lwgeom_free((LWGEOM *)cp); |
740 | 117 | lwerror("Unable to add geometry to curvepoly"); |
741 | 117 | return NULL; |
742 | 117 | } |
743 | 117k | } |
744 | 6.88k | s->depth--; |
745 | | |
746 | 6.88k | return cp; |
747 | 7.00k | } |
748 | | |
749 | | /** |
750 | | * POLYHEDRALSURFACETYPE |
751 | | */ |
752 | | |
753 | | static LWCOLLECTION* lwcollection_from_wkb_state(wkb_parse_state *s); |
754 | | static LWNURBSCURVE* lwnurbscurve_from_wkb_state(wkb_parse_state *s); |
755 | | |
756 | | /** |
757 | | * Parse a collection (MULTI types, COLLECTION, TINTYPE, COMPOUND, CURVEPOLY, etc.) from WKB state. |
758 | | * |
759 | | * Reads the number of component geometries from the WKB parse state, constructs an |
760 | | * empty LWCOLLECTION of the current s->lwtype/srid/dimension flags, then iteratively |
761 | | * parses and appends each component geometry using lwgeom_from_wkb_state(). |
762 | | * |
763 | | * Behavior and side effects: |
764 | | * - Returns a newly allocated LWCOLLECTION containing the parsed components, or NULL on error. |
765 | | * - For an empty collection (component count == 0) returns the empty collection. |
766 | | * - If s->lwtype == POLYHEDRALSURFACETYPE, enables strict Z-closure checking by setting |
767 | | * the LW_PARSER_CHECK_ZCLOSURE flag in s->check before parsing components. |
768 | | * - Increments s->depth while parsing to enforce recursion limits, if depth exceeds |
769 | | * LW_PARSER_MAX_DEPTH the function frees allocated resources, reports an error, and returns NULL. |
770 | | * - On failure to parse or to add a component, frees any allocated geometry/collection and returns NULL. |
771 | | * |
772 | | * Return: |
773 | | * Pointer to a newly constructed LWCOLLECTION on success, or NULL on failure. |
774 | | */ |
775 | | static LWCOLLECTION* lwcollection_from_wkb_state(wkb_parse_state *s) |
776 | 123k | { |
777 | 123k | uint32_t ngeoms = integer_from_wkb_state(s); |
778 | 123k | if (s->error) |
779 | 0 | return NULL; |
780 | 123k | LWCOLLECTION *col = lwcollection_construct_empty(s->lwtype, s->srid, s->has_z, s->has_m); |
781 | 123k | LWGEOM *geom = NULL; |
782 | 123k | uint32_t i; |
783 | 123k | int8_t prev_check = s->check; |
784 | 123k | uint8_t start_depth = s->depth; |
785 | | |
786 | 123k | LWDEBUGF(4,"Collection has %d components", ngeoms); |
787 | | |
788 | | /* Empty collection? */ |
789 | 123k | if ( ngeoms == 0 ) |
790 | 75.0k | { |
791 | 75.0k | s->check = prev_check; |
792 | 75.0k | return col; |
793 | 75.0k | } |
794 | | |
795 | | /* Be strict in polyhedral surface closures */ |
796 | 48.3k | if ( s->lwtype == POLYHEDRALSURFACETYPE ) |
797 | 3.38k | s->check |= LW_PARSER_CHECK_ZCLOSURE; |
798 | | |
799 | 48.3k | s->depth++; |
800 | 48.3k | if (s->depth >= LW_PARSER_MAX_DEPTH) |
801 | 2 | { |
802 | 2 | lwcollection_free(col); |
803 | 2 | lwerror("Geometry has too many chained collections"); |
804 | 2 | s->check = prev_check; |
805 | 2 | s->depth = start_depth; |
806 | 2 | return NULL; |
807 | 2 | } |
808 | 329k | for ( i = 0; i < ngeoms; i++ ) |
809 | 281k | { |
810 | 281k | geom = lwgeom_from_wkb_state(s); |
811 | 281k | if (!geom || lwcollection_add_lwgeom(col, geom) == NULL ) |
812 | 0 | { |
813 | 0 | lwgeom_free(geom); |
814 | 0 | lwgeom_free((LWGEOM *)col); |
815 | 0 | lwerror("Unable to add geometry to collection"); |
816 | 0 | s->check = prev_check; |
817 | 0 | s->depth = start_depth; |
818 | 0 | return NULL; |
819 | 0 | } |
820 | 281k | } |
821 | 48.3k | s->depth--; |
822 | 48.3k | s->check = prev_check; |
823 | | |
824 | 48.3k | return col; |
825 | 48.3k | } |
826 | | |
827 | | /** |
828 | | * Parse an ISO/IEC 13249-3:2016 NURBS curve from the current position of a WKB parse state. |
829 | | * |
830 | | * Reads the NURBSCURVE components in WKB order: degree, number of control points, |
831 | | * per-control-point byte-order marker, coordinates (X, Y, optional Z and M), a 1-byte |
832 | | * weight-present flag and optional weight value, then the knot count and knot values. |
833 | | * If control-point count is zero an empty POINTARRAY is constructed with the current |
834 | | * dimensionality. Knot count must be > 0 per the standard. |
835 | | * |
836 | | * On error (invalid data, unexpected EOF, invalid weight bit, allocation failure, or |
837 | | * missing required knots) the function logs an error, frees any partially-allocated |
838 | | * resources and returns NULL. |
839 | | * |
840 | | * Returns a newly-allocated LWNURBSCURVE on success; the caller owns the returned object. |
841 | | * |
842 | | * @return Pointer to constructed LWNURBSCURVE, or NULL on failure. |
843 | | */ |
844 | | static LWNURBSCURVE* lwnurbscurve_from_wkb_state(wkb_parse_state *s) |
845 | 39.6k | { |
846 | 39.6k | uint32_t degree = 0, nknots = 0, npoints = 0, ndims; |
847 | 39.6k | double *weights = NULL, *knots = NULL; |
848 | 39.6k | POINTARRAY *points = NULL; |
849 | 39.6k | int all_weights_one = 1; |
850 | 39.6k | size_t pa_size, point_payload_size, weights_size, point_tags_size; |
851 | 39.6k | static const uint32_t MAXPOINTS = (uint32_t)(UINT_MAX / (WKB_DOUBLE_SIZE * 4)); |
852 | | |
853 | | /* ISO/IEC 13249-3:2016 compliant parsing */ |
854 | 39.6k | degree = integer_from_wkb_state(s); |
855 | 39.6k | if (s->error) return NULL; |
856 | 39.6k | if (degree < 1 || degree > 10) |
857 | 128 | { |
858 | 128 | lwerror("WKB NURBSCURVE: degree %u outside valid range [1,10]", degree); |
859 | 128 | return NULL; |
860 | 128 | } |
861 | | |
862 | | /* Read control points count */ |
863 | 39.5k | npoints = integer_from_wkb_state(s); |
864 | 39.5k | if (s->error) return NULL; |
865 | | |
866 | | /* Defensive upper bound (worst-case 4 doubles/point) */ |
867 | 39.5k | if (npoints > MAXPOINTS) { |
868 | 19 | lwerror("WKB NURBSCURVE: control point count (%u) too large", npoints); |
869 | 19 | return NULL; |
870 | 19 | } |
871 | | |
872 | 39.5k | if (npoints > 0 && npoints <= degree) { |
873 | 4 | lwerror("WKB NURBSCURVE: degree %u requires at least %llu control points, got %u", |
874 | 4 | degree, (unsigned long long)degree + 1, npoints); |
875 | 4 | return NULL; |
876 | 4 | } |
877 | 39.5k | const size_t min_point_size = 2 + (2 + (s->has_z ? 1 : 0) + (s->has_m ? 1 : 0)) * WKB_DOUBLE_SIZE; |
878 | 39.5k | if (!lw_size_mul((size_t)npoints, min_point_size, &pa_size)) |
879 | 0 | { |
880 | 0 | lwerror("WKB NURBSCURVE: control point count (%u) too large", npoints); |
881 | 0 | return NULL; |
882 | 0 | } |
883 | 39.5k | wkb_parse_state_check(s, pa_size); |
884 | 39.5k | if (s->error) |
885 | 0 | return NULL; |
886 | | |
887 | | /* Does the data we want to read exist? */ |
888 | 39.5k | ndims = 2 + s->has_z + s->has_m; |
889 | 39.5k | if (!lw_size_mul((size_t)npoints, ndims, &point_payload_size) || |
890 | 39.2k | !lw_size_mul(point_payload_size, WKB_DOUBLE_SIZE, &point_payload_size) || |
891 | 39.2k | !lw_size_mul((size_t)npoints, sizeof(double), &weights_size) || |
892 | 39.2k | !lw_size_mul((size_t)npoints, sizeof(char) * 2, &point_tags_size) || |
893 | 39.2k | !lw_size_add(point_payload_size, weights_size, &pa_size) || |
894 | 39.2k | !lw_size_add(pa_size, point_tags_size, &pa_size)) |
895 | 0 | { |
896 | 0 | lwerror("WKB NURBSCURVE: control point count (%u) too large", npoints); |
897 | 0 | return NULL; |
898 | 0 | } |
899 | 39.5k | wkb_parse_state_check(s, pa_size); |
900 | 39.5k | if (s->error) |
901 | 0 | return NULL; |
902 | | |
903 | | /* Initialize points array */ |
904 | 39.5k | if (npoints > 0) { |
905 | 4.63k | int8_t save_swap_butes = s->swap_bytes; |
906 | | |
907 | 4.63k | points = ptarray_construct(s->has_z, s->has_m, npoints); |
908 | 4.63k | weights = lwalloc(sizeof(double) * npoints); |
909 | | |
910 | | /* ISO format: each control point has structure: |
911 | | * <byte order> <wkbweightedpoint> <bit> [<wkbweight>] |
912 | | */ |
913 | | |
914 | 3.20M | for (uint32_t i = 0; i < npoints; i++) { |
915 | 3.19M | POINT4D pt = {0, 0, 0, 0}; |
916 | | |
917 | | /* Read byte order for this point (ISO requirement) */ |
918 | 3.19M | wkb_swap_bytes(s); |
919 | | |
920 | | /* Read point coordinates */ |
921 | 3.19M | pt.x = double_from_wkb_state(s); |
922 | 3.19M | if (s->error) { |
923 | 0 | lwfree(weights); |
924 | 0 | ptarray_free(points); |
925 | 0 | return NULL; |
926 | 0 | } |
927 | | |
928 | 3.19M | pt.y = double_from_wkb_state(s); |
929 | 3.19M | if (s->error) { |
930 | 0 | lwfree(weights); |
931 | 0 | ptarray_free(points); |
932 | 0 | return NULL; |
933 | 0 | } |
934 | | |
935 | 3.19M | if (s->has_z) { |
936 | 899k | pt.z = double_from_wkb_state(s); |
937 | 899k | if (s->error) { |
938 | 0 | lwfree(weights); |
939 | 0 | ptarray_free(points); |
940 | 0 | return NULL; |
941 | 0 | } |
942 | 899k | } |
943 | | |
944 | 3.19M | if (s->has_m) { |
945 | 438k | pt.m = double_from_wkb_state(s); |
946 | 438k | if (s->error) { |
947 | 0 | lwfree(weights); |
948 | 0 | ptarray_free(points); |
949 | 0 | return NULL; |
950 | 0 | } |
951 | 438k | } |
952 | | |
953 | 3.19M | ptarray_set_point4d(points, i, &pt); |
954 | | |
955 | | /* Read weight bit flag */ |
956 | 3.19M | uint8_t has_weight = byte_from_wkb_state(s); |
957 | 3.19M | if (s->error) { |
958 | 0 | lwfree(weights); |
959 | 0 | ptarray_free(points); |
960 | 0 | return NULL; |
961 | 0 | } |
962 | | |
963 | 3.19M | if (has_weight == 0) { |
964 | | /* Default weight = 1.0 */ |
965 | 3.19M | weights[i] = 1.0; |
966 | 3.19M | } else if (has_weight == 1) { |
967 | | /* Custom weight follows */ |
968 | 4.41k | weights[i] = double_from_wkb_state(s); |
969 | 4.41k | if (weights[i] <= 0.0) { |
970 | 7 | lwerror("WKB NURBSCURVE: non-positive weight for point %d", i); |
971 | 7 | lwfree(weights); |
972 | 7 | ptarray_free(points); |
973 | 7 | return NULL; |
974 | 7 | } |
975 | 4.40k | if (s->error) { |
976 | 0 | lwfree(weights); |
977 | 0 | ptarray_free(points); |
978 | 0 | return NULL; |
979 | 0 | } |
980 | 4.40k | if (weights[i] != 1.0) all_weights_one = 0; |
981 | 4.40k | } else { |
982 | 94 | lwerror("WKB NURBSCURVE: invalid weight bit %d for point %d (must be 0 or 1)", has_weight, i); |
983 | 94 | lwfree(weights); |
984 | 94 | ptarray_free(points); |
985 | 94 | return NULL; |
986 | 94 | } |
987 | | |
988 | 3.19M | } |
989 | | /* Restore original swap state */ |
990 | 4.53k | s->swap_bytes = save_swap_butes; |
991 | 4.53k | } |
992 | | |
993 | | /* Read knots (required by WKB standard) */ |
994 | 39.4k | nknots = integer_from_wkb_state(s); |
995 | 39.4k | if (s->error) { |
996 | 0 | lwfree(weights); |
997 | 0 | ptarray_free(points); |
998 | 0 | return NULL; |
999 | 0 | } |
1000 | | |
1001 | 39.4k | if (npoints == 0) |
1002 | 34.4k | { |
1003 | 34.4k | if (nknots != 0) |
1004 | 114 | { |
1005 | 114 | lwerror("WKB NURBSCURVE EMPTY: expected 0 knots, got %u", nknots); |
1006 | 114 | return NULL; |
1007 | 114 | } |
1008 | 34.2k | return lwnurbscurve_construct_empty(s->srid, s->has_z, s->has_m); |
1009 | 34.4k | } |
1010 | | |
1011 | 5.02k | if (nknots == 0) { |
1012 | 45 | lwerror("WKB NURBSCURVE: knots required by standard (got 0)"); |
1013 | 45 | lwfree(weights); |
1014 | 45 | ptarray_free(points); |
1015 | 45 | return NULL; |
1016 | 45 | } |
1017 | | |
1018 | | /* Validate knot count against B-spline formula */ |
1019 | 4.98k | uint32_t expected_knots = npoints + degree + 1; |
1020 | 4.98k | if (nknots != expected_knots) { |
1021 | 228 | lwerror("WKB NURBSCURVE: expected %d knots for degree %d and %d points, got %d", |
1022 | 228 | expected_knots, degree, npoints, nknots); |
1023 | 228 | lwfree(weights); |
1024 | 228 | ptarray_free(points); |
1025 | 228 | return NULL; |
1026 | 228 | } |
1027 | | |
1028 | 4.75k | knots = lwalloc(sizeof(double) * nknots); |
1029 | 3.20M | for (uint32_t i = 0; i < nknots; i++) { |
1030 | 3.20M | knots[i] = double_from_wkb_state(s); |
1031 | 3.20M | if (s->error) { |
1032 | 0 | lwfree(weights); |
1033 | 0 | lwfree(knots); |
1034 | 0 | ptarray_free(points); |
1035 | 0 | return NULL; |
1036 | 0 | } |
1037 | 3.20M | } |
1038 | 3.20M | for (uint32_t i = 1; i < nknots; i++) { |
1039 | 3.19M | if (knots[i] < knots[i-1]) { |
1040 | 28 | lwerror("WKB NURBSCURVE: knot vector must be non-decreasing"); |
1041 | 28 | lwfree(weights); |
1042 | 28 | lwfree(knots); |
1043 | 28 | ptarray_free(points); |
1044 | 28 | return NULL; |
1045 | 28 | } |
1046 | 3.19M | } |
1047 | | |
1048 | 4.72k | uint32_t nweights = all_weights_one ? 0U : npoints; |
1049 | 4.72k | if (all_weights_one) { lwfree(weights); weights = NULL; } |
1050 | 4.72k | LWNURBSCURVE *curve = lwnurbscurve_construct(s->srid, NULL, degree, points, weights, knots, nweights, nknots); |
1051 | 4.72k | if (!curve) { |
1052 | 0 | if (weights) lwfree(weights); |
1053 | 0 | if (knots) lwfree(knots); |
1054 | 0 | if (points) ptarray_free(points); /* constructor did not take ownership on failure */ |
1055 | 0 | return NULL; |
1056 | 0 | } |
1057 | 4.72k | if (weights) lwfree(weights); |
1058 | 4.72k | if (knots) lwfree(knots); |
1059 | 4.72k | return curve; |
1060 | 4.72k | } |
1061 | | |
1062 | | /** |
1063 | | * Parse a single WKB geometry from the given parse state and return it as an LWGEOM. |
1064 | | * |
1065 | | * Reads the leading endian byte, the WKB type (and optional SRID), updates the parse |
1066 | | * state (including byte-swap flag, detected lwtype and srid), and dispatches to the |
1067 | | * specific geometry reader for the detected type. On success returns a newly |
1068 | | * allocated LWGEOM; on error or unsupported type returns NULL and sets s->error. |
1069 | | * |
1070 | | * Side effects: |
1071 | | * - Advances s->pos as data are consumed. |
1072 | | * - May set s->swap_bytes, s->lwtype, s->srid and s->error. |
1073 | | * |
1074 | | * Return: |
1075 | | * Pointer to the parsed LWGEOM, or NULL on error or unsupported geometry type. |
1076 | | */ |
1077 | | LWGEOM* lwgeom_from_wkb_state(wkb_parse_state *s) |
1078 | 407k | { |
1079 | 407k | uint32_t wkb_type; |
1080 | | |
1081 | 407k | LWDEBUG(4,"Entered function"); |
1082 | | |
1083 | 407k | wkb_swap_bytes(s); |
1084 | | |
1085 | | /* Read the type number */ |
1086 | 407k | wkb_type = integer_from_wkb_state(s); |
1087 | 407k | if (s->error) |
1088 | 0 | return NULL; |
1089 | 407k | LWDEBUGF(4,"Got WKB type number: 0x%X", wkb_type); |
1090 | 407k | lwtype_from_wkb_state(s, wkb_type); |
1091 | | |
1092 | | /* Read the SRID, if necessary */ |
1093 | 407k | if( s->has_srid ) |
1094 | 7.35k | { |
1095 | 7.35k | s->srid = clamp_srid(integer_from_wkb_state(s)); |
1096 | 7.35k | if (s->error) |
1097 | 0 | return NULL; |
1098 | | /* TODO: warn on explicit UNKNOWN srid ? */ |
1099 | 7.35k | LWDEBUGF(4,"Got SRID: %u", s->srid); |
1100 | 7.35k | } |
1101 | | |
1102 | | /* Do the right thing */ |
1103 | 407k | switch( s->lwtype ) |
1104 | 407k | { |
1105 | 14.0k | case POINTTYPE: |
1106 | 14.0k | return (LWGEOM*)lwpoint_from_wkb_state(s); |
1107 | 0 | break; |
1108 | 83.5k | case LINETYPE: |
1109 | 83.5k | return (LWGEOM*)lwline_from_wkb_state(s); |
1110 | 0 | break; |
1111 | 68.0k | case CIRCSTRINGTYPE: |
1112 | 68.0k | return (LWGEOM*)lwcircstring_from_wkb_state(s); |
1113 | 0 | break; |
1114 | 29.0k | case POLYGONTYPE: |
1115 | 29.0k | return (LWGEOM*)lwpoly_from_wkb_state(s); |
1116 | 0 | break; |
1117 | 31.4k | case TRIANGLETYPE: |
1118 | 31.4k | return (LWGEOM*)lwtriangle_from_wkb_state(s); |
1119 | 0 | break; |
1120 | 17.1k | case CURVEPOLYTYPE: |
1121 | 17.1k | return (LWGEOM*)lwcurvepoly_from_wkb_state(s); |
1122 | 0 | break; |
1123 | 2.66k | case MULTIPOINTTYPE: |
1124 | 5.57k | case MULTILINETYPE: |
1125 | 7.53k | case MULTIPOLYGONTYPE: |
1126 | 54.0k | case COMPOUNDTYPE: |
1127 | 68.9k | case MULTICURVETYPE: |
1128 | 77.5k | case MULTISURFACETYPE: |
1129 | 82.2k | case POLYHEDRALSURFACETYPE: |
1130 | 85.3k | case TINTYPE: |
1131 | 123k | case COLLECTIONTYPE: |
1132 | 123k | return (LWGEOM*)lwcollection_from_wkb_state(s); |
1133 | 0 | break; |
1134 | 39.6k | case NURBSCURVETYPE: |
1135 | 39.6k | return (LWGEOM*)lwnurbscurve_from_wkb_state(s); |
1136 | 0 | break; |
1137 | | /* Unknown type! */ |
1138 | 0 | default: |
1139 | 0 | lwerror("%s: Unsupported geometry type: %s", __func__, lwtype_name(s->lwtype)); |
1140 | 407k | } |
1141 | | |
1142 | | /* Return value to keep compiler happy. */ |
1143 | 0 | return NULL; |
1144 | | |
1145 | 407k | } |
1146 | | |
1147 | | /* TODO add check for SRID consistency */ |
1148 | | |
1149 | | /** |
1150 | | * WKB inputs *must* have a declared size, to prevent malformed WKB from reading |
1151 | | * off the end of the memory segment (this stops a malevolent user from declaring |
1152 | | * a one-ring polygon to have 10 rings, causing the WKB reader to walk off the |
1153 | | * end of the memory). |
1154 | | * |
1155 | | * Check is a bitmask of: LW_PARSER_CHECK_MINPOINTS, LW_PARSER_CHECK_ODD, |
1156 | | * LW_PARSER_CHECK_CLOSURE, LW_PARSER_CHECK_NONE, LW_PARSER_CHECK_ALL |
1157 | | */ |
1158 | | LWGEOM* lwgeom_from_wkb(const uint8_t *wkb, const size_t wkb_size, const char check) |
1159 | 8.49k | { |
1160 | 8.49k | wkb_parse_state s; |
1161 | | |
1162 | | /* Initialize the state appropriately */ |
1163 | 8.49k | s.wkb = wkb; |
1164 | 8.49k | s.wkb_size = wkb_size; |
1165 | 8.49k | s.swap_bytes = LW_FALSE; |
1166 | 8.49k | s.check = check; |
1167 | 8.49k | s.lwtype = 0; |
1168 | 8.49k | s.srid = SRID_UNKNOWN; |
1169 | 8.49k | s.has_z = LW_FALSE; |
1170 | 8.49k | s.has_m = LW_FALSE; |
1171 | 8.49k | s.has_srid = LW_FALSE; |
1172 | 8.49k | s.error = LW_FALSE; |
1173 | 8.49k | s.pos = wkb; |
1174 | 8.49k | s.pos_offset = 0; |
1175 | 8.49k | s.depth = 1; |
1176 | | |
1177 | 8.49k | if (!wkb || !wkb_size) |
1178 | 0 | return NULL; |
1179 | | |
1180 | 8.49k | return lwgeom_from_wkb_state(&s); |
1181 | 8.49k | } |
1182 | | |
1183 | | LWGEOM* lwgeom_from_hexwkb(const char *hexwkb, const char check) |
1184 | 0 | { |
1185 | 0 | int hexwkb_len; |
1186 | 0 | uint8_t *wkb; |
1187 | 0 | LWGEOM *lwgeom; |
1188 | |
|
1189 | 0 | if ( ! hexwkb ) |
1190 | 0 | { |
1191 | 0 | lwerror("lwgeom_from_hexwkb: null input"); |
1192 | 0 | return NULL; |
1193 | 0 | } |
1194 | | |
1195 | 0 | hexwkb_len = strlen(hexwkb); |
1196 | 0 | wkb = bytes_from_hexbytes(hexwkb, hexwkb_len); |
1197 | 0 | lwgeom = lwgeom_from_wkb(wkb, hexwkb_len/2, check); |
1198 | 0 | lwfree(wkb); |
1199 | 0 | return lwgeom; |
1200 | 0 | } |