Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: OGR
4 : * Purpose: WKB geometry related methods
5 : * Author: Even Rouault <even dot rouault at spatialys.com>
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 2022, Even Rouault <even dot rouault at spatialys.com>
9 : *
10 : * SPDX-License-Identifier: MIT
11 : ****************************************************************************/
12 :
13 : #include "cpl_error.h"
14 : #include "ogr_wkb.h"
15 : #include "ogr_core.h"
16 : #include "ogr_geometry.h"
17 : #include "ogr_p.h"
18 :
19 : #include <algorithm>
20 : #include <cmath>
21 : #include <climits>
22 : #include <limits>
23 :
24 : #include <algorithm>
25 : #include <limits>
26 :
27 : #define USE_FAST_FLOAT
28 : #ifdef USE_FAST_FLOAT
29 : #include "include_fast_float.h"
30 : #endif
31 :
32 : /************************************************************************/
33 : /* OGRWKBNeedSwap() */
34 : /************************************************************************/
35 :
36 212 : static inline bool OGRWKBNeedSwap(GByte b)
37 : {
38 : if constexpr (CPL_IS_LSB)
39 212 : return b != 1;
40 : else
41 : return b != 0;
42 : }
43 :
44 : /************************************************************************/
45 : /* OGRWKBReadUInt32() */
46 : /************************************************************************/
47 :
48 235 : static inline uint32_t OGRWKBReadUInt32(const GByte *pabyWkb, bool bNeedSwap)
49 : {
50 : uint32_t nVal;
51 235 : memcpy(&nVal, pabyWkb, sizeof(nVal));
52 235 : if (bNeedSwap)
53 0 : CPL_SWAP32PTR(&nVal);
54 235 : return nVal;
55 : }
56 :
57 : /************************************************************************/
58 : /* OGRWKBReadFloat64() */
59 : /************************************************************************/
60 :
61 100 : static inline double OGRWKBReadFloat64(const GByte *pabyWkb, bool bNeedSwap)
62 : {
63 : double dfVal;
64 100 : memcpy(&dfVal, pabyWkb, sizeof(dfVal));
65 100 : if (bNeedSwap)
66 0 : CPL_SWAP64PTR(&dfVal);
67 100 : return dfVal;
68 : }
69 :
70 : /************************************************************************/
71 : /* OGRWKBRingGetArea() */
72 : /************************************************************************/
73 :
74 12 : static bool OGRWKBRingGetArea(const GByte *&pabyWkb, size_t &nWKBSize, int nDim,
75 : bool bNeedSwap, double &dfArea)
76 : {
77 12 : const uint32_t nPoints = OGRWKBReadUInt32(pabyWkb, bNeedSwap);
78 12 : if (nPoints >= 4 &&
79 12 : (nWKBSize - sizeof(uint32_t)) / (nDim * sizeof(double)) >= nPoints)
80 : {
81 12 : nWKBSize -= sizeof(uint32_t) + nDim * sizeof(double);
82 12 : pabyWkb += sizeof(uint32_t);
83 : // Computation according to Green's Theorem
84 : // Cf OGRSimpleCurve::get_LinearArea()
85 12 : double x_m1 = OGRWKBReadFloat64(pabyWkb, bNeedSwap);
86 12 : double y_m1 = OGRWKBReadFloat64(pabyWkb + sizeof(double), bNeedSwap);
87 12 : double y_m2 = y_m1;
88 12 : dfArea = 0;
89 12 : pabyWkb += nDim * sizeof(double);
90 50 : for (uint32_t i = 1; i < nPoints; ++i)
91 : {
92 38 : const double x = OGRWKBReadFloat64(pabyWkb, bNeedSwap);
93 : const double y =
94 38 : OGRWKBReadFloat64(pabyWkb + sizeof(double), bNeedSwap);
95 38 : pabyWkb += nDim * sizeof(double);
96 38 : dfArea += x_m1 * (y - y_m2);
97 38 : y_m2 = y_m1;
98 38 : x_m1 = x;
99 38 : y_m1 = y;
100 : }
101 12 : dfArea += x_m1 * (y_m1 - y_m2);
102 12 : dfArea = 0.5 * std::fabs(dfArea);
103 12 : return true;
104 : }
105 0 : return false;
106 : }
107 :
108 : /************************************************************************/
109 : /* OGRWKBGetGeomType() */
110 : /************************************************************************/
111 :
112 207 : bool OGRWKBGetGeomType(const GByte *pabyWkb, size_t nWKBSize, bool &bNeedSwap,
113 : uint32_t &nType)
114 : {
115 207 : if (nWKBSize >= 5)
116 : {
117 207 : bNeedSwap = OGRWKBNeedSwap(pabyWkb[0]);
118 207 : nType = OGRWKBReadUInt32(pabyWkb + 1, bNeedSwap);
119 207 : return true;
120 : }
121 0 : return false;
122 : }
123 :
124 : /************************************************************************/
125 : /* OGRWKBPolygonGetArea() */
126 : /************************************************************************/
127 :
128 11 : bool OGRWKBPolygonGetArea(const GByte *&pabyWkb, size_t &nWKBSize,
129 : double &dfArea)
130 : {
131 : bool bNeedSwap;
132 : uint32_t nType;
133 11 : if (nWKBSize < 9 || !OGRWKBGetGeomType(pabyWkb, nWKBSize, bNeedSwap, nType))
134 0 : return false;
135 :
136 11 : int nDims = 2;
137 11 : if (nType == wkbPolygon)
138 : {
139 : // do nothing
140 : }
141 6 : else if (nType == wkbPolygon + 1000 || // wkbPolygonZ
142 4 : nType == wkbPolygon25D || nType == wkbPolygonM)
143 : {
144 4 : nDims = 3;
145 : }
146 2 : else if (nType == wkbPolygonZM)
147 : {
148 2 : nDims = 4;
149 : }
150 : else
151 : {
152 0 : return false;
153 : }
154 :
155 11 : const uint32_t nRings = OGRWKBReadUInt32(pabyWkb + 5, bNeedSwap);
156 11 : if ((nWKBSize - 9) / sizeof(uint32_t) >= nRings)
157 : {
158 11 : pabyWkb += 9;
159 11 : nWKBSize -= 9;
160 11 : dfArea = 0;
161 11 : if (nRings > 0)
162 : {
163 11 : if (!OGRWKBRingGetArea(pabyWkb, nWKBSize, nDims, bNeedSwap, dfArea))
164 0 : return false;
165 12 : for (uint32_t i = 1; i < nRings; ++i)
166 : {
167 : double dfRingArea;
168 1 : if (!OGRWKBRingGetArea(pabyWkb, nWKBSize, nDims, bNeedSwap,
169 : dfRingArea))
170 0 : return false;
171 1 : dfArea -= dfRingArea;
172 : }
173 : }
174 11 : return true;
175 : }
176 0 : return false;
177 : }
178 :
179 : /************************************************************************/
180 : /* OGRWKBMultiPolygonGetArea() */
181 : /************************************************************************/
182 :
183 5 : bool OGRWKBMultiPolygonGetArea(const GByte *&pabyWkb, size_t &nWKBSize,
184 : double &dfArea)
185 : {
186 5 : if (nWKBSize < 9)
187 0 : return false;
188 :
189 5 : const bool bNeedSwap = OGRWKBNeedSwap(pabyWkb[0]);
190 5 : const uint32_t nPolys = OGRWKBReadUInt32(pabyWkb + 5, bNeedSwap);
191 5 : if ((nWKBSize - 9) / 9 >= nPolys)
192 : {
193 5 : pabyWkb += 9;
194 5 : nWKBSize -= 9;
195 5 : dfArea = 0;
196 11 : for (uint32_t i = 0; i < nPolys; ++i)
197 : {
198 : double dfPolyArea;
199 6 : if (!OGRWKBPolygonGetArea(pabyWkb, nWKBSize, dfPolyArea))
200 0 : return false;
201 6 : dfArea += dfPolyArea;
202 : }
203 5 : return true;
204 : }
205 0 : return false;
206 : }
207 :
208 : /************************************************************************/
209 : /* WKBFromEWKB() */
210 : /************************************************************************/
211 :
212 1445 : const GByte *WKBFromEWKB(GByte *pabyEWKB, size_t nEWKBSize, size_t &nWKBSizeOut,
213 : int *pnSRIDOut)
214 : {
215 1445 : if (nEWKBSize < 5U)
216 : {
217 0 : CPLError(CE_Failure, CPLE_AppDefined, "Invalid EWKB content : %u bytes",
218 : static_cast<unsigned>(nEWKBSize));
219 0 : return nullptr;
220 : }
221 :
222 1445 : const GByte *pabyWKB = pabyEWKB;
223 :
224 : /* -------------------------------------------------------------------- */
225 : /* PostGIS EWKB format includes an SRID, but this won't be */
226 : /* understood by OGR, so if the SRID flag is set, we remove the */
227 : /* SRID (bytes at offset 5 to 8). */
228 : /* -------------------------------------------------------------------- */
229 1445 : if (nEWKBSize > 9 &&
230 1438 : ((pabyEWKB[0] == 0 /* big endian */ && (pabyEWKB[1] & 0x20)) ||
231 1438 : (pabyEWKB[0] != 0 /* little endian */ && (pabyEWKB[4] & 0x20))))
232 : {
233 526 : if (pnSRIDOut)
234 : {
235 0 : memcpy(pnSRIDOut, pabyEWKB + 5, 4);
236 0 : const OGRwkbByteOrder eByteOrder =
237 0 : (pabyEWKB[0] == 0 ? wkbXDR : wkbNDR);
238 0 : if (OGR_SWAP(eByteOrder))
239 0 : *pnSRIDOut = CPL_SWAP32(*pnSRIDOut);
240 : }
241 :
242 : // Drop the SRID flag
243 526 : if (pabyEWKB[0] == 0)
244 0 : pabyEWKB[1] &= (~0x20);
245 : else
246 526 : pabyEWKB[4] &= (~0x20);
247 :
248 : // Move 5 first bytes of EWKB 4 bytes later to create regular WKB
249 526 : memmove(pabyEWKB + 4, pabyEWKB, 5);
250 526 : memset(pabyEWKB, 0, 4);
251 : // and make pabyWKB point to that
252 526 : pabyWKB += 4;
253 526 : nWKBSizeOut = nEWKBSize - 4;
254 : }
255 : else
256 : {
257 919 : if (pnSRIDOut)
258 : {
259 0 : *pnSRIDOut = INT_MIN;
260 : }
261 919 : nWKBSizeOut = nEWKBSize;
262 : }
263 :
264 1445 : return pabyWKB;
265 : }
266 :
267 : /************************************************************************/
268 : /* OGRWKBReadUInt32AtOffset() */
269 : /************************************************************************/
270 :
271 8632 : static uint32_t OGRWKBReadUInt32AtOffset(const uint8_t *data,
272 : OGRwkbByteOrder eByteOrder,
273 : size_t &iOffset)
274 : {
275 : uint32_t v;
276 8632 : memcpy(&v, data + iOffset, sizeof(v));
277 8632 : iOffset += sizeof(v);
278 8632 : if (OGR_SWAP(eByteOrder))
279 : {
280 1666 : CPL_SWAP32PTR(&v);
281 : }
282 8632 : return v;
283 : }
284 :
285 : /************************************************************************/
286 : /* ReadWKBPointSequence() */
287 : /************************************************************************/
288 :
289 : template <bool INCLUDE_Z, typename EnvelopeType>
290 856 : static bool ReadWKBPointSequence(const uint8_t *data, size_t size,
291 : OGRwkbByteOrder eByteOrder, int nDim,
292 : bool bHasZ, size_t &iOffset,
293 : EnvelopeType &sEnvelope)
294 : {
295 : const uint32_t nPoints =
296 856 : OGRWKBReadUInt32AtOffset(data, eByteOrder, iOffset);
297 856 : if (nPoints > (size - iOffset) / (nDim * sizeof(double)))
298 0 : return false;
299 856 : double dfX = 0;
300 856 : double dfY = 0;
301 856 : [[maybe_unused]] double dfZ = 0;
302 14564 : for (uint32_t j = 0; j < nPoints; j++)
303 : {
304 13708 : memcpy(&dfX, data + iOffset, sizeof(double));
305 13708 : memcpy(&dfY, data + iOffset + sizeof(double), sizeof(double));
306 : if constexpr (INCLUDE_Z)
307 : {
308 52 : if (bHasZ)
309 34 : memcpy(&dfZ, data + iOffset + 2 * sizeof(double),
310 : sizeof(double));
311 : }
312 13708 : iOffset += nDim * sizeof(double);
313 13708 : if (OGR_SWAP(eByteOrder))
314 : {
315 560 : CPL_SWAP64PTR(&dfX);
316 560 : CPL_SWAP64PTR(&dfY);
317 : if constexpr (INCLUDE_Z)
318 : {
319 0 : CPL_SWAP64PTR(&dfZ);
320 : }
321 : }
322 13708 : sEnvelope.MinX = std::min(sEnvelope.MinX, dfX);
323 13708 : sEnvelope.MinY = std::min(sEnvelope.MinY, dfY);
324 13708 : sEnvelope.MaxX = std::max(sEnvelope.MaxX, dfX);
325 13708 : sEnvelope.MaxY = std::max(sEnvelope.MaxY, dfY);
326 : if constexpr (INCLUDE_Z)
327 : {
328 52 : if (bHasZ)
329 : {
330 34 : sEnvelope.MinZ = std::min(sEnvelope.MinZ, dfZ);
331 34 : sEnvelope.MaxZ = std::max(sEnvelope.MaxZ, dfZ);
332 : }
333 : }
334 : }
335 856 : return true;
336 : }
337 :
338 : /************************************************************************/
339 : /* ReadWKBRingSequence() */
340 : /************************************************************************/
341 :
342 : template <bool INCLUDE_Z, typename EnvelopeType>
343 604 : static bool ReadWKBRingSequence(const uint8_t *data, size_t size,
344 : OGRwkbByteOrder eByteOrder, int nDim,
345 : bool bHasZ, size_t &iOffset,
346 : EnvelopeType &sEnvelope)
347 : {
348 604 : const uint32_t nRings = OGRWKBReadUInt32AtOffset(data, eByteOrder, iOffset);
349 604 : if (nRings > (size - iOffset) / sizeof(uint32_t))
350 0 : return false;
351 1266 : for (uint32_t i = 0; i < nRings; i++)
352 : {
353 662 : if (iOffset + sizeof(uint32_t) > size)
354 0 : return false;
355 662 : if (!ReadWKBPointSequence<INCLUDE_Z>(data, size, eByteOrder, nDim,
356 : bHasZ, iOffset, sEnvelope))
357 0 : return false;
358 : }
359 604 : return true;
360 : }
361 :
362 : /************************************************************************/
363 : /* OGRWKBGetBoundingBox() */
364 : /************************************************************************/
365 :
366 : constexpr uint32_t WKB_PREFIX_SIZE = 1 + sizeof(uint32_t);
367 : constexpr uint32_t MIN_WKB_SIZE = WKB_PREFIX_SIZE + sizeof(uint32_t);
368 :
369 : template <bool INCLUDE_Z, typename EnvelopeType>
370 991589 : static bool OGRWKBGetBoundingBox(const uint8_t *data, size_t size,
371 : size_t &iOffset, EnvelopeType &sEnvelope,
372 : int nRec)
373 : {
374 991589 : if (size - iOffset < MIN_WKB_SIZE)
375 0 : return false;
376 991589 : const int nByteOrder = DB2_V72_FIX_BYTE_ORDER(data[iOffset]);
377 991589 : if (!(nByteOrder == wkbXDR || nByteOrder == wkbNDR))
378 0 : return false;
379 991589 : const OGRwkbByteOrder eByteOrder = static_cast<OGRwkbByteOrder>(nByteOrder);
380 :
381 991589 : OGRwkbGeometryType eGeometryType = wkbUnknown;
382 991589 : OGRReadWKBGeometryType(data + iOffset, wkbVariantIso, &eGeometryType);
383 991589 : iOffset += 5;
384 991589 : const auto eFlatType = wkbFlatten(eGeometryType);
385 991589 : const bool bHasZ = CPL_TO_BOOL(OGR_GT_HasZ(eGeometryType));
386 991589 : const int nDim = 2 + (bHasZ ? 1 : 0) + (OGR_GT_HasM(eGeometryType) ? 1 : 0);
387 :
388 991589 : if (eFlatType == wkbPoint)
389 : {
390 991013 : if (size - iOffset < nDim * sizeof(double))
391 0 : return false;
392 991013 : double dfX = 0;
393 991013 : double dfY = 0;
394 991013 : [[maybe_unused]] double dfZ = 0;
395 991013 : memcpy(&dfX, data + iOffset, sizeof(double));
396 991013 : memcpy(&dfY, data + iOffset + sizeof(double), sizeof(double));
397 : if constexpr (INCLUDE_Z)
398 : {
399 11 : if (bHasZ)
400 4 : memcpy(&dfZ, data + iOffset + 2 * sizeof(double),
401 : sizeof(double));
402 : }
403 991013 : iOffset += nDim * sizeof(double);
404 991013 : if (OGR_SWAP(eByteOrder))
405 : {
406 40 : CPL_SWAP64PTR(&dfX);
407 40 : CPL_SWAP64PTR(&dfY);
408 : if constexpr (INCLUDE_Z)
409 : {
410 0 : CPL_SWAP64PTR(&dfZ);
411 : }
412 : }
413 991013 : if (std::isnan(dfX))
414 : {
415 : // Point empty
416 6 : sEnvelope = EnvelopeType();
417 : }
418 : else
419 : {
420 991007 : sEnvelope.MinX = dfX;
421 991007 : sEnvelope.MinY = dfY;
422 991007 : sEnvelope.MaxX = dfX;
423 991007 : sEnvelope.MaxY = dfY;
424 : if constexpr (INCLUDE_Z)
425 : {
426 10 : if (bHasZ)
427 : {
428 4 : sEnvelope.MinZ = dfZ;
429 4 : sEnvelope.MaxZ = dfZ;
430 : }
431 : }
432 : }
433 991013 : return true;
434 : }
435 :
436 576 : if (eFlatType == wkbLineString || eFlatType == wkbCircularString)
437 : {
438 106 : sEnvelope = EnvelopeType();
439 :
440 106 : return ReadWKBPointSequence<INCLUDE_Z>(data, size, eByteOrder, nDim,
441 106 : bHasZ, iOffset, sEnvelope);
442 : }
443 :
444 470 : if (eFlatType == wkbPolygon || eFlatType == wkbTriangle)
445 : {
446 164 : sEnvelope = EnvelopeType();
447 :
448 164 : return ReadWKBRingSequence<INCLUDE_Z>(data, size, eByteOrder, nDim,
449 164 : bHasZ, iOffset, sEnvelope);
450 : }
451 :
452 306 : if (eFlatType == wkbMultiPoint)
453 : {
454 57 : sEnvelope = EnvelopeType();
455 :
456 57 : uint32_t nParts = OGRWKBReadUInt32AtOffset(data, eByteOrder, iOffset);
457 57 : if (nParts >
458 57 : (size - iOffset) / (WKB_PREFIX_SIZE + nDim * sizeof(double)))
459 0 : return false;
460 57 : double dfX = 0;
461 57 : double dfY = 0;
462 57 : [[maybe_unused]] double dfZ = 0;
463 170 : for (uint32_t k = 0; k < nParts; k++)
464 : {
465 113 : iOffset += WKB_PREFIX_SIZE;
466 113 : memcpy(&dfX, data + iOffset, sizeof(double));
467 113 : memcpy(&dfY, data + iOffset + sizeof(double), sizeof(double));
468 : if constexpr (INCLUDE_Z)
469 : {
470 5 : if (bHasZ)
471 3 : memcpy(&dfZ, data + iOffset + 2 * sizeof(double),
472 : sizeof(double));
473 : }
474 113 : iOffset += nDim * sizeof(double);
475 113 : if (OGR_SWAP(eByteOrder))
476 : {
477 40 : CPL_SWAP64PTR(&dfX);
478 40 : CPL_SWAP64PTR(&dfY);
479 : if constexpr (INCLUDE_Z)
480 : {
481 0 : CPL_SWAP64PTR(&dfZ);
482 : }
483 : }
484 113 : sEnvelope.MinX = std::min(sEnvelope.MinX, dfX);
485 113 : sEnvelope.MinY = std::min(sEnvelope.MinY, dfY);
486 113 : sEnvelope.MaxX = std::max(sEnvelope.MaxX, dfX);
487 113 : sEnvelope.MaxY = std::max(sEnvelope.MaxY, dfY);
488 : if constexpr (INCLUDE_Z)
489 : {
490 5 : if (bHasZ)
491 : {
492 3 : sEnvelope.MinZ = std::min(sEnvelope.MinZ, dfZ);
493 3 : sEnvelope.MaxZ = std::max(sEnvelope.MaxZ, dfZ);
494 : }
495 : }
496 : }
497 57 : return true;
498 : }
499 :
500 249 : if (eFlatType == wkbMultiLineString)
501 : {
502 59 : sEnvelope = EnvelopeType();
503 :
504 : const uint32_t nParts =
505 59 : OGRWKBReadUInt32AtOffset(data, eByteOrder, iOffset);
506 59 : if (nParts > (size - iOffset) / MIN_WKB_SIZE)
507 0 : return false;
508 147 : for (uint32_t k = 0; k < nParts; k++)
509 : {
510 88 : if (iOffset + MIN_WKB_SIZE > size)
511 0 : return false;
512 88 : iOffset += WKB_PREFIX_SIZE;
513 88 : if (!ReadWKBPointSequence<INCLUDE_Z>(data, size, eByteOrder, nDim,
514 : bHasZ, iOffset, sEnvelope))
515 0 : return false;
516 : }
517 59 : return true;
518 : }
519 :
520 190 : if (eFlatType == wkbMultiPolygon)
521 : {
522 112 : sEnvelope = EnvelopeType();
523 :
524 : const uint32_t nParts =
525 112 : OGRWKBReadUInt32AtOffset(data, eByteOrder, iOffset);
526 112 : if (nParts > (size - iOffset) / MIN_WKB_SIZE)
527 0 : return false;
528 552 : for (uint32_t k = 0; k < nParts; k++)
529 : {
530 440 : if (iOffset + MIN_WKB_SIZE > size)
531 0 : return false;
532 440 : CPLAssert(data[iOffset] == eByteOrder);
533 440 : iOffset += WKB_PREFIX_SIZE;
534 440 : if (!ReadWKBRingSequence<INCLUDE_Z>(data, size, eByteOrder, nDim,
535 : bHasZ, iOffset, sEnvelope))
536 0 : return false;
537 : }
538 112 : return true;
539 : }
540 :
541 78 : if (eFlatType == wkbGeometryCollection || eFlatType == wkbCompoundCurve ||
542 4 : eFlatType == wkbCurvePolygon || eFlatType == wkbMultiCurve ||
543 2 : eFlatType == wkbMultiSurface || eFlatType == wkbPolyhedralSurface ||
544 : eFlatType == wkbTIN)
545 : {
546 78 : if (nRec == 128)
547 0 : return false;
548 78 : sEnvelope = EnvelopeType();
549 :
550 : const uint32_t nParts =
551 78 : OGRWKBReadUInt32AtOffset(data, eByteOrder, iOffset);
552 78 : if (nParts > (size - iOffset) / MIN_WKB_SIZE)
553 0 : return false;
554 78 : EnvelopeType sEnvelopeSubGeom;
555 176 : for (uint32_t k = 0; k < nParts; k++)
556 : {
557 98 : if (!OGRWKBGetBoundingBox<INCLUDE_Z>(data, size, iOffset,
558 : sEnvelopeSubGeom, nRec + 1))
559 0 : return false;
560 98 : sEnvelope.Merge(sEnvelopeSubGeom);
561 : }
562 78 : return true;
563 : }
564 :
565 0 : return false;
566 : }
567 :
568 : /************************************************************************/
569 : /* OGRWKBGetBoundingBox() */
570 : /************************************************************************/
571 :
572 991456 : bool OGRWKBGetBoundingBox(const GByte *pabyWkb, size_t nWKBSize,
573 : OGREnvelope &sEnvelope)
574 : {
575 991456 : size_t iOffset = 0;
576 991456 : return OGRWKBGetBoundingBox<false>(pabyWkb, nWKBSize, iOffset, sEnvelope,
577 1982910 : 0);
578 : }
579 :
580 : /************************************************************************/
581 : /* OGRWKBGetBoundingBox() */
582 : /************************************************************************/
583 :
584 35 : bool OGRWKBGetBoundingBox(const GByte *pabyWkb, size_t nWKBSize,
585 : OGREnvelope3D &sEnvelope)
586 : {
587 35 : size_t iOffset = 0;
588 70 : return OGRWKBGetBoundingBox<true>(pabyWkb, nWKBSize, iOffset, sEnvelope, 0);
589 : }
590 :
591 : /************************************************************************/
592 : /* OGRWKBIntersectsPointSequencePessimistic() */
593 : /************************************************************************/
594 :
595 199 : static bool OGRWKBIntersectsPointSequencePessimistic(
596 : const uint8_t *data, const size_t size, const OGRwkbByteOrder eByteOrder,
597 : const int nDim, size_t &iOffsetInOut, const OGREnvelope &sEnvelope,
598 : bool &bErrorOut)
599 : {
600 : const uint32_t nPoints =
601 199 : OGRWKBReadUInt32AtOffset(data, eByteOrder, iOffsetInOut);
602 199 : if (nPoints > (size - iOffsetInOut) / (nDim * sizeof(double)))
603 : {
604 12 : bErrorOut = true;
605 12 : return false;
606 : }
607 :
608 187 : double dfX = 0;
609 187 : double dfY = 0;
610 1318 : for (uint32_t j = 0; j < nPoints; j++)
611 : {
612 1256 : memcpy(&dfX, data + iOffsetInOut, sizeof(double));
613 1256 : memcpy(&dfY, data + iOffsetInOut + sizeof(double), sizeof(double));
614 1256 : iOffsetInOut += nDim * sizeof(double);
615 1256 : if (OGR_SWAP(eByteOrder))
616 : {
617 3 : CPL_SWAP64PTR(&dfX);
618 3 : CPL_SWAP64PTR(&dfY);
619 : }
620 1256 : if (dfX >= sEnvelope.MinX && dfY >= sEnvelope.MinY &&
621 775 : dfX <= sEnvelope.MaxX && dfY <= sEnvelope.MaxY)
622 : {
623 125 : return true;
624 : }
625 : }
626 :
627 62 : return false;
628 : }
629 :
630 : /************************************************************************/
631 : /* OGRWKBIntersectsRingSequencePessimistic() */
632 : /************************************************************************/
633 :
634 149 : static bool OGRWKBIntersectsRingSequencePessimistic(
635 : const uint8_t *data, const size_t size, const OGRwkbByteOrder eByteOrder,
636 : const int nDim, size_t &iOffsetInOut, const OGREnvelope &sEnvelope,
637 : bool &bErrorOut)
638 : {
639 : const uint32_t nRings =
640 149 : OGRWKBReadUInt32AtOffset(data, eByteOrder, iOffsetInOut);
641 149 : if (nRings > (size - iOffsetInOut) / sizeof(uint32_t))
642 : {
643 16 : bErrorOut = true;
644 16 : return false;
645 : }
646 133 : if (nRings == 0)
647 2 : return false;
648 131 : if (iOffsetInOut + sizeof(uint32_t) > size)
649 : {
650 0 : bErrorOut = true;
651 0 : return false;
652 : }
653 131 : if (OGRWKBIntersectsPointSequencePessimistic(
654 : data, size, eByteOrder, nDim, iOffsetInOut, sEnvelope, bErrorOut))
655 : {
656 98 : return true;
657 : }
658 33 : if (bErrorOut)
659 0 : return false;
660 :
661 : // skip inner rings
662 39 : for (uint32_t i = 1; i < nRings; ++i)
663 : {
664 6 : if (iOffsetInOut + sizeof(uint32_t) > size)
665 : {
666 0 : bErrorOut = true;
667 0 : return false;
668 : }
669 : const uint32_t nPoints =
670 6 : OGRWKBReadUInt32AtOffset(data, eByteOrder, iOffsetInOut);
671 6 : if (nPoints > (size - iOffsetInOut) / (nDim * sizeof(double)))
672 : {
673 0 : bErrorOut = true;
674 0 : return false;
675 : }
676 6 : iOffsetInOut += sizeof(double) * nPoints * nDim;
677 : }
678 33 : return false;
679 : }
680 :
681 : /************************************************************************/
682 : /* OGRWKBIntersectsPessimistic() */
683 : /************************************************************************/
684 :
685 23543 : static bool OGRWKBIntersectsPessimistic(const GByte *data, const size_t size,
686 : size_t &iOffsetInOut,
687 : const OGREnvelope &sEnvelope,
688 : const int nRec, bool &bErrorOut)
689 : {
690 23543 : if (size - iOffsetInOut < MIN_WKB_SIZE)
691 : {
692 0 : bErrorOut = true;
693 0 : return false;
694 : }
695 23543 : const int nByteOrder = DB2_V72_FIX_BYTE_ORDER(data[iOffsetInOut]);
696 23543 : if (!(nByteOrder == wkbXDR || nByteOrder == wkbNDR))
697 : {
698 0 : bErrorOut = true;
699 0 : return false;
700 : }
701 23543 : const OGRwkbByteOrder eByteOrder = static_cast<OGRwkbByteOrder>(nByteOrder);
702 :
703 23543 : OGRwkbGeometryType eGeometryType = wkbUnknown;
704 23543 : OGRReadWKBGeometryType(data + iOffsetInOut, wkbVariantIso, &eGeometryType);
705 23543 : iOffsetInOut += 5;
706 23543 : const auto eFlatType = wkbFlatten(eGeometryType);
707 23543 : const int nDim = 2 + (OGR_GT_HasZ(eGeometryType) ? 1 : 0) +
708 23543 : (OGR_GT_HasM(eGeometryType) ? 1 : 0);
709 :
710 23543 : if (eFlatType == wkbPoint)
711 : {
712 23171 : if (size - iOffsetInOut < nDim * sizeof(double))
713 8 : return false;
714 23163 : double dfX = 0;
715 23163 : double dfY = 0;
716 23163 : memcpy(&dfX, data + iOffsetInOut, sizeof(double));
717 23163 : memcpy(&dfY, data + iOffsetInOut + sizeof(double), sizeof(double));
718 23163 : iOffsetInOut += nDim * sizeof(double);
719 23163 : if (OGR_SWAP(eByteOrder))
720 : {
721 0 : CPL_SWAP64PTR(&dfX);
722 0 : CPL_SWAP64PTR(&dfY);
723 : }
724 23163 : if (std::isnan(dfX))
725 : {
726 1 : return false;
727 : }
728 : else
729 : {
730 23155 : return dfX >= sEnvelope.MinX && dfX <= sEnvelope.MaxX &&
731 46317 : dfY >= sEnvelope.MinY && dfY <= sEnvelope.MaxY;
732 : }
733 : }
734 :
735 372 : if (eFlatType == wkbLineString || eFlatType == wkbCircularString)
736 : {
737 68 : return OGRWKBIntersectsPointSequencePessimistic(
738 68 : data, size, eByteOrder, nDim, iOffsetInOut, sEnvelope, bErrorOut);
739 : }
740 :
741 304 : if (eFlatType == wkbPolygon || eFlatType == wkbTriangle)
742 : {
743 149 : return OGRWKBIntersectsRingSequencePessimistic(
744 149 : data, size, eByteOrder, nDim, iOffsetInOut, sEnvelope, bErrorOut);
745 : }
746 :
747 155 : if (eFlatType == wkbMultiPoint || eFlatType == wkbMultiLineString ||
748 81 : eFlatType == wkbMultiPolygon || eFlatType == wkbGeometryCollection ||
749 65 : eFlatType == wkbCompoundCurve || eFlatType == wkbCurvePolygon ||
750 39 : eFlatType == wkbMultiCurve || eFlatType == wkbMultiSurface ||
751 13 : eFlatType == wkbPolyhedralSurface || eFlatType == wkbTIN)
752 : {
753 155 : if (nRec == 128)
754 : {
755 0 : bErrorOut = true;
756 0 : return false;
757 : }
758 : const uint32_t nParts =
759 155 : OGRWKBReadUInt32AtOffset(data, eByteOrder, iOffsetInOut);
760 155 : if (nParts > (size - iOffsetInOut) / MIN_WKB_SIZE)
761 : {
762 80 : bErrorOut = true;
763 80 : return false;
764 : }
765 120 : for (uint32_t k = 0; k < nParts; k++)
766 : {
767 84 : if (OGRWKBIntersectsPessimistic(data, size, iOffsetInOut, sEnvelope,
768 : nRec + 1, bErrorOut))
769 : {
770 39 : return true;
771 : }
772 45 : else if (bErrorOut)
773 : {
774 0 : return false;
775 : }
776 : }
777 36 : return false;
778 : }
779 :
780 0 : bErrorOut = true;
781 0 : return false;
782 : }
783 :
784 : /************************************************************************/
785 : /* OGRWKBIntersectsPessimistic() */
786 : /************************************************************************/
787 :
788 : /* Returns whether the geometry (pabyWkb, nWKBSize) intersects, for sure,
789 : * the passed envelope.
790 : * When it returns true, the geometry intersects the envelope.
791 : * When it returns false, the geometry may or may not intersect the envelope.
792 : */
793 23459 : bool OGRWKBIntersectsPessimistic(const GByte *pabyWkb, size_t nWKBSize,
794 : const OGREnvelope &sEnvelope)
795 : {
796 23459 : size_t iOffsetInOut = 0;
797 23459 : bool bErrorOut = false;
798 23459 : bool bRet = OGRWKBIntersectsPessimistic(pabyWkb, nWKBSize, iOffsetInOut,
799 : sEnvelope, 0, bErrorOut);
800 23459 : if (!bRet && !bErrorOut)
801 : {
802 : // The following assert only holds if there is no trailing data
803 : // after the WKB
804 : // CPLAssert(iOffsetInOut == nWKBSize);
805 : }
806 23459 : return bRet;
807 : }
808 :
809 : /************************************************************************/
810 : /* epsilonEqual() */
811 : /************************************************************************/
812 :
813 168 : static inline bool epsilonEqual(double a, double b, double eps)
814 : {
815 168 : return ::fabs(a - b) < eps;
816 : }
817 :
818 : /************************************************************************/
819 : /* OGRWKBIsClockwiseRing() */
820 : /************************************************************************/
821 :
822 722 : static inline double GetX(const GByte *data, uint32_t i, int nDim,
823 : bool bNeedSwap)
824 : {
825 : double dfX;
826 722 : memcpy(&dfX, data + static_cast<size_t>(i) * nDim * sizeof(double),
827 : sizeof(double));
828 722 : if (bNeedSwap)
829 0 : CPL_SWAP64PTR(&dfX);
830 722 : return dfX;
831 : }
832 :
833 1246 : static inline double GetY(const GByte *data, uint32_t i, int nDim,
834 : bool bNeedSwap)
835 : {
836 : double dfY;
837 1246 : memcpy(&dfY, data + (static_cast<size_t>(i) * nDim + 1) * sizeof(double),
838 : sizeof(double));
839 1246 : if (bNeedSwap)
840 0 : CPL_SWAP64PTR(&dfY);
841 1246 : return dfY;
842 : }
843 :
844 69 : static bool OGRWKBIsClockwiseRing(const GByte *data, const uint32_t nPoints,
845 : const int nDim, const bool bNeedSwap)
846 : {
847 69 : constexpr double EPSILON = 1.0E-5;
848 :
849 : // WARNING: keep in sync OGRLineString::isClockwise(),
850 : // OGRCurve::isClockwise() and OGRWKBIsClockwiseRing()
851 :
852 69 : bool bUseFallback = false;
853 :
854 : // Find the lowest rightmost vertex.
855 69 : uint32_t v = 0; // Used after for.
856 69 : double vX = GetX(data, v, nDim, bNeedSwap);
857 69 : double vY = GetY(data, v, nDim, bNeedSwap);
858 1052 : for (uint32_t i = 1; i < nPoints - 1; i++)
859 : {
860 : // => v < end.
861 983 : const double y = GetY(data, i, nDim, bNeedSwap);
862 983 : if (y < vY)
863 : {
864 324 : v = i;
865 324 : vX = GetX(data, i, nDim, bNeedSwap);
866 324 : vY = y;
867 324 : bUseFallback = false;
868 : }
869 659 : else if (y == vY)
870 : {
871 37 : const double x = GetX(data, i, nDim, bNeedSwap);
872 37 : if (x > vX)
873 : {
874 19 : v = i;
875 19 : vX = x;
876 : // vY = y;
877 19 : bUseFallback = false;
878 : }
879 18 : else if (x == vX)
880 : {
881 : // Two vertex with same coordinates are the lowest rightmost
882 : // vertex. Cannot use that point as the pivot (#5342).
883 2 : bUseFallback = true;
884 : }
885 : }
886 : }
887 :
888 : // Previous.
889 69 : uint32_t next = (v == 0) ? nPoints - 2 : v - 1;
890 91 : if (epsilonEqual(GetX(data, next, nDim, bNeedSwap), vX, EPSILON) &&
891 22 : epsilonEqual(GetY(data, next, nDim, bNeedSwap), vY, EPSILON))
892 : {
893 : // Don't try to be too clever by retrying with a next point.
894 : // This can lead to false results as in the case of #3356.
895 0 : bUseFallback = true;
896 : }
897 :
898 69 : const double dx0 = GetX(data, next, nDim, bNeedSwap) - vX;
899 69 : const double dy0 = GetY(data, next, nDim, bNeedSwap) - vY;
900 :
901 : // Following.
902 69 : next = v + 1;
903 69 : if (next >= nPoints - 1)
904 : {
905 6 : next = 0;
906 : }
907 :
908 77 : if (epsilonEqual(GetX(data, next, nDim, bNeedSwap), vX, EPSILON) &&
909 8 : epsilonEqual(GetY(data, next, nDim, bNeedSwap), vY, EPSILON))
910 : {
911 : // Don't try to be too clever by retrying with a next point.
912 : // This can lead to false results as in the case of #3356.
913 2 : bUseFallback = true;
914 : }
915 :
916 69 : const double dx1 = GetX(data, next, nDim, bNeedSwap) - vX;
917 69 : const double dy1 = GetY(data, next, nDim, bNeedSwap) - vY;
918 :
919 69 : const double crossproduct = dx1 * dy0 - dx0 * dy1;
920 :
921 69 : if (!bUseFallback)
922 : {
923 67 : if (crossproduct > 0) // CCW
924 44 : return false;
925 23 : else if (crossproduct < 0) // CW
926 23 : return true;
927 : }
928 :
929 : // This is a degenerate case: the extent of the polygon is less than EPSILON
930 : // or 2 nearly identical points were found.
931 : // Try with Green Formula as a fallback, but this is not a guarantee
932 : // as we'll probably be affected by numerical instabilities.
933 :
934 2 : double dfSum = GetX(data, 0, nDim, bNeedSwap) *
935 2 : (GetY(data, 1, nDim, bNeedSwap) -
936 2 : GetY(data, nPoints - 1, nDim, bNeedSwap));
937 :
938 12 : for (uint32_t i = 1; i < nPoints - 1; i++)
939 : {
940 10 : dfSum += GetX(data, i, nDim, bNeedSwap) *
941 20 : (GetY(data, i + 1, nDim, bNeedSwap) -
942 10 : GetY(data, i - 1, nDim, bNeedSwap));
943 : }
944 :
945 2 : dfSum += GetX(data, nPoints - 1, nDim, bNeedSwap) *
946 2 : (GetY(data, 0, nDim, bNeedSwap) -
947 2 : GetX(data, nPoints - 2, nDim, bNeedSwap));
948 :
949 2 : return dfSum < 0;
950 : }
951 :
952 : /************************************************************************/
953 : /* OGRWKBFixupCounterClockWiseExternalRing() */
954 : /************************************************************************/
955 :
956 117 : static bool OGRWKBFixupCounterClockWiseExternalRingInternal(
957 : GByte *data, size_t size, size_t &iOffsetInOut, const int nRec, bool *isCCW)
958 : {
959 117 : if (isCCW)
960 : {
961 52 : *isCCW = true;
962 : }
963 :
964 117 : if (size - iOffsetInOut < MIN_WKB_SIZE)
965 : {
966 0 : return false;
967 : }
968 117 : const int nByteOrder = DB2_V72_FIX_BYTE_ORDER(data[iOffsetInOut]);
969 117 : if (!(nByteOrder == wkbXDR || nByteOrder == wkbNDR))
970 : {
971 0 : return false;
972 : }
973 117 : const OGRwkbByteOrder eByteOrder = static_cast<OGRwkbByteOrder>(nByteOrder);
974 :
975 117 : OGRwkbGeometryType eGeometryType = wkbUnknown;
976 117 : OGRReadWKBGeometryType(data + iOffsetInOut, wkbVariantIso, &eGeometryType);
977 117 : iOffsetInOut += 5;
978 117 : const auto eFlatType = wkbFlatten(eGeometryType);
979 117 : const int nDim = 2 + (OGR_GT_HasZ(eGeometryType) ? 1 : 0) +
980 117 : (OGR_GT_HasM(eGeometryType) ? 1 : 0);
981 :
982 117 : if (eFlatType == wkbPolygon)
983 : {
984 : const uint32_t nRings =
985 66 : OGRWKBReadUInt32AtOffset(data, eByteOrder, iOffsetInOut);
986 66 : if (nRings > (size - iOffsetInOut) / sizeof(uint32_t))
987 : {
988 0 : return false;
989 : }
990 125 : for (uint32_t iRing = 0; iRing < nRings; ++iRing)
991 : {
992 69 : if (iOffsetInOut + sizeof(uint32_t) > size)
993 0 : return false;
994 : const uint32_t nPoints =
995 69 : OGRWKBReadUInt32AtOffset(data, eByteOrder, iOffsetInOut);
996 69 : const size_t sizeOfPoint = nDim * sizeof(double);
997 69 : if (nPoints > (size - iOffsetInOut) / sizeOfPoint)
998 : {
999 0 : return false;
1000 : }
1001 :
1002 69 : if (nPoints >= 4)
1003 : {
1004 69 : const bool bIsClockwiseRing = OGRWKBIsClockwiseRing(
1005 69 : data + iOffsetInOut, nPoints, nDim, OGR_SWAP(eByteOrder));
1006 69 : if ((bIsClockwiseRing && iRing == 0) ||
1007 46 : (!bIsClockwiseRing && iRing > 0))
1008 : {
1009 25 : if (isCCW)
1010 : {
1011 10 : *isCCW = false;
1012 10 : return true;
1013 : }
1014 :
1015 : GByte abyTmp[4 * sizeof(double)];
1016 46 : for (uint32_t i = 0; i < nPoints / 2; ++i)
1017 : {
1018 31 : GByte *pBegin = data + iOffsetInOut + i * sizeOfPoint;
1019 31 : GByte *pEnd = data + iOffsetInOut +
1020 31 : (nPoints - 1 - i) * sizeOfPoint;
1021 31 : memcpy(abyTmp, pBegin, sizeOfPoint);
1022 31 : memcpy(pBegin, pEnd, sizeOfPoint);
1023 31 : memcpy(pEnd, abyTmp, sizeOfPoint);
1024 : }
1025 : }
1026 : }
1027 :
1028 59 : iOffsetInOut += nPoints * sizeOfPoint;
1029 : }
1030 : }
1031 :
1032 107 : if (eFlatType == wkbGeometryCollection || eFlatType == wkbMultiPolygon ||
1033 : eFlatType == wkbMultiSurface)
1034 : {
1035 6 : if (nRec == 128)
1036 : {
1037 0 : return false;
1038 : }
1039 : const uint32_t nParts =
1040 6 : OGRWKBReadUInt32AtOffset(data, eByteOrder, iOffsetInOut);
1041 6 : if (nParts > (size - iOffsetInOut) / MIN_WKB_SIZE)
1042 : {
1043 0 : return false;
1044 : }
1045 11 : for (uint32_t k = 0; k < nParts; k++)
1046 : {
1047 5 : if (!OGRWKBFixupCounterClockWiseExternalRingInternal(
1048 : data, size, iOffsetInOut, nRec, isCCW))
1049 : {
1050 0 : return false;
1051 : }
1052 : }
1053 : }
1054 :
1055 107 : return true;
1056 : }
1057 :
1058 : /** Modifies the geometry such that exterior rings of polygons are
1059 : * counter-clockwise oriented and inner rings clockwise oriented.
1060 : */
1061 60 : void OGRWKBFixupCounterClockWiseExternalRing(GByte *pabyWkb, size_t nWKBSize)
1062 : {
1063 60 : size_t iOffsetInOut = 0;
1064 60 : OGRWKBFixupCounterClockWiseExternalRingInternal(
1065 : pabyWkb, nWKBSize, iOffsetInOut, /* nRec = */ 0, nullptr);
1066 60 : }
1067 :
1068 52 : bool OGRWKBIsCounterClockWiseExternalRing(const GByte *pabyWkb, size_t nWKBSize)
1069 : {
1070 52 : size_t iOffsetInOut = 0;
1071 52 : bool isCCW = true;
1072 52 : OGRWKBFixupCounterClockWiseExternalRingInternal(
1073 : const_cast<GByte *>(pabyWkb), nWKBSize, iOffsetInOut, /* nRec = */ 0,
1074 : &isCCW);
1075 52 : return isCCW;
1076 : }
1077 :
1078 : /************************************************************************/
1079 : /* OGRWKBPointUpdater() */
1080 : /************************************************************************/
1081 :
1082 : OGRWKBPointUpdater::OGRWKBPointUpdater() = default;
1083 :
1084 : OGRWKBPointUpdater::~OGRWKBPointUpdater() = default;
1085 :
1086 : /************************************************************************/
1087 : /* OGRWKBIntersectsPointSequencePessimistic() */
1088 : /************************************************************************/
1089 :
1090 3029 : static bool OGRWKBUpdatePointsSequence(uint8_t *data, const size_t size,
1091 : OGRWKBPointUpdater &oUpdater,
1092 : const OGRwkbByteOrder eByteOrder,
1093 : const int nDim, const bool bHasZ,
1094 : const bool bHasM, size_t &iOffsetInOut)
1095 : {
1096 : const uint32_t nPoints =
1097 3029 : OGRWKBReadUInt32AtOffset(data, eByteOrder, iOffsetInOut);
1098 3029 : if (nPoints > (size - iOffsetInOut) / (nDim * sizeof(double)))
1099 : {
1100 1497 : return false;
1101 : }
1102 1532 : const bool bNeedSwap = OGR_SWAP(eByteOrder);
1103 6067 : for (uint32_t j = 0; j < nPoints; j++)
1104 : {
1105 4537 : void *pdfX = data + iOffsetInOut;
1106 4537 : void *pdfY = data + iOffsetInOut + sizeof(double);
1107 4537 : void *pdfZ = bHasZ ? data + iOffsetInOut + 2 * sizeof(double) : nullptr;
1108 4537 : void *pdfM =
1109 4537 : bHasM ? data + iOffsetInOut + (bHasZ ? 3 : 2) * sizeof(double)
1110 : : nullptr;
1111 4537 : if (!oUpdater.update(bNeedSwap, pdfX, pdfY, pdfZ, pdfM))
1112 2 : return false;
1113 :
1114 4535 : iOffsetInOut += nDim * sizeof(double);
1115 : }
1116 :
1117 1530 : return true;
1118 : }
1119 :
1120 : /************************************************************************/
1121 : /* OGRWKBVisitRingSequence() */
1122 : /************************************************************************/
1123 :
1124 1776 : static bool OGRWKBVisitRingSequence(uint8_t *data, const size_t size,
1125 : OGRWKBPointUpdater &oUpdater,
1126 : const OGRwkbByteOrder eByteOrder,
1127 : const int nDim, const bool bHasZ,
1128 : const bool bHasM, size_t &iOffsetInOut)
1129 : {
1130 : const uint32_t nRings =
1131 1776 : OGRWKBReadUInt32AtOffset(data, eByteOrder, iOffsetInOut);
1132 1776 : if (nRings > (size - iOffsetInOut) / sizeof(uint32_t))
1133 : {
1134 150 : return false;
1135 : }
1136 :
1137 2534 : for (uint32_t i = 0; i < nRings; ++i)
1138 : {
1139 1727 : if (iOffsetInOut + sizeof(uint32_t) > size)
1140 : {
1141 4 : return false;
1142 : }
1143 1723 : if (!OGRWKBUpdatePointsSequence(data, size, oUpdater, eByteOrder, nDim,
1144 : bHasZ, bHasM, iOffsetInOut))
1145 : {
1146 815 : return false;
1147 : }
1148 : }
1149 807 : return true;
1150 : }
1151 :
1152 : /************************************************************************/
1153 : /* OGRWKBUpdatePoints() */
1154 : /************************************************************************/
1155 :
1156 26323 : static bool OGRWKBUpdatePoints(uint8_t *data, const size_t size,
1157 : OGRWKBPointUpdater &oUpdater,
1158 : size_t &iOffsetInOut, const int nRec)
1159 : {
1160 26323 : if (size - iOffsetInOut < MIN_WKB_SIZE)
1161 : {
1162 514 : return false;
1163 : }
1164 25809 : const int nByteOrder = DB2_V72_FIX_BYTE_ORDER(data[iOffsetInOut]);
1165 25809 : if (!(nByteOrder == wkbXDR || nByteOrder == wkbNDR))
1166 : {
1167 68 : return false;
1168 : }
1169 25741 : const OGRwkbByteOrder eByteOrder = static_cast<OGRwkbByteOrder>(nByteOrder);
1170 :
1171 25741 : OGRwkbGeometryType eGeometryType = wkbUnknown;
1172 25741 : OGRReadWKBGeometryType(data + iOffsetInOut, wkbVariantIso, &eGeometryType);
1173 25741 : iOffsetInOut += 5;
1174 25741 : const auto eFlatType = wkbFlatten(eGeometryType);
1175 :
1176 25741 : if (eFlatType == wkbGeometryCollection || eFlatType == wkbCompoundCurve ||
1177 25270 : eFlatType == wkbCurvePolygon || eFlatType == wkbMultiPoint ||
1178 24959 : eFlatType == wkbMultiLineString || eFlatType == wkbMultiPolygon ||
1179 24751 : eFlatType == wkbMultiCurve || eFlatType == wkbMultiSurface ||
1180 24491 : eFlatType == wkbPolyhedralSurface || eFlatType == wkbTIN)
1181 : {
1182 1412 : if (nRec == 128)
1183 1 : return false;
1184 :
1185 : const uint32_t nParts =
1186 1411 : OGRWKBReadUInt32AtOffset(data, eByteOrder, iOffsetInOut);
1187 1411 : if (nParts > (size - iOffsetInOut) / MIN_WKB_SIZE)
1188 : {
1189 176 : return false;
1190 : }
1191 1896 : for (uint32_t k = 0; k < nParts; k++)
1192 : {
1193 1378 : if (!OGRWKBUpdatePoints(data, size, oUpdater, iOffsetInOut,
1194 : nRec + 1))
1195 717 : return false;
1196 : }
1197 518 : return true;
1198 : }
1199 :
1200 24329 : const bool bHasZ = CPL_TO_BOOL(OGR_GT_HasZ(eGeometryType));
1201 24329 : const bool bHasM = CPL_TO_BOOL(OGR_GT_HasM(eGeometryType));
1202 24329 : const int nDim = 2 + (bHasZ ? 1 : 0) + (bHasM ? 1 : 0);
1203 :
1204 24329 : if (eFlatType == wkbPoint)
1205 : {
1206 21044 : if (size - iOffsetInOut < nDim * sizeof(double))
1207 376 : return false;
1208 20668 : void *pdfX = data + iOffsetInOut;
1209 20668 : void *pdfY = data + iOffsetInOut + sizeof(double);
1210 20668 : void *pdfZ = bHasZ ? data + iOffsetInOut + 2 * sizeof(double) : nullptr;
1211 20668 : void *pdfM =
1212 20668 : bHasM ? data + iOffsetInOut + (bHasZ ? 3 : 2) * sizeof(double)
1213 : : nullptr;
1214 20668 : const bool bNeedSwap = OGR_SWAP(eByteOrder);
1215 20668 : if (!oUpdater.update(bNeedSwap, pdfX, pdfY, pdfZ, pdfM))
1216 1 : return false;
1217 20667 : iOffsetInOut += nDim * sizeof(double);
1218 20667 : return true;
1219 : }
1220 :
1221 3285 : if (eFlatType == wkbLineString || eFlatType == wkbCircularString)
1222 : {
1223 1306 : return OGRWKBUpdatePointsSequence(data, size, oUpdater, eByteOrder,
1224 1306 : nDim, bHasZ, bHasM, iOffsetInOut);
1225 : }
1226 :
1227 1979 : if (eFlatType == wkbPolygon || eFlatType == wkbTriangle)
1228 : {
1229 1776 : return OGRWKBVisitRingSequence(data, size, oUpdater, eByteOrder, nDim,
1230 1776 : bHasZ, bHasM, iOffsetInOut);
1231 : }
1232 :
1233 203 : CPLDebug("OGR", "Unknown WKB geometry type");
1234 203 : return false;
1235 : }
1236 :
1237 : /** Visit all points of a WKB geometry to update them.
1238 : */
1239 24945 : bool OGRWKBUpdatePoints(GByte *pabyWkb, size_t nWKBSize,
1240 : OGRWKBPointUpdater &oUpdater)
1241 : {
1242 24945 : size_t iOffsetInOut = 0;
1243 24945 : return OGRWKBUpdatePoints(pabyWkb, nWKBSize, oUpdater, iOffsetInOut,
1244 49890 : /* nRec = */ 0);
1245 : }
1246 :
1247 : /************************************************************************/
1248 : /* OGRWKBTransformCache::clear() */
1249 : /************************************************************************/
1250 :
1251 : #ifdef OGR_WKB_TRANSFORM_ALL_AT_ONCE
1252 : void OGRWKBTransformCache::clear()
1253 : {
1254 : abNeedSwap.clear();
1255 : abIsEmpty.clear();
1256 : apdfX.clear();
1257 : apdfY.clear();
1258 : apdfZ.clear();
1259 : apdfM.clear();
1260 : adfX.clear();
1261 : adfY.clear();
1262 : adfZ.clear();
1263 : adfM.clear();
1264 : anErrorCodes.clear();
1265 : }
1266 : #endif
1267 :
1268 : /************************************************************************/
1269 : /* OGRWKBTransform() */
1270 : /************************************************************************/
1271 :
1272 : /** Visit all points of a WKB geometry to transform them.
1273 : */
1274 14918 : bool OGRWKBTransform(GByte *pabyWkb, size_t nWKBSize,
1275 : OGRCoordinateTransformation *poCT,
1276 : [[maybe_unused]] OGRWKBTransformCache &oCache,
1277 : OGREnvelope3D &sEnvelope)
1278 : {
1279 : #ifndef OGR_WKB_TRANSFORM_ALL_AT_ONCE
1280 : struct OGRWKBPointUpdaterReproj final : public OGRWKBPointUpdater
1281 : {
1282 : OGRCoordinateTransformation *m_poCT;
1283 : OGREnvelope3D &m_sEnvelope;
1284 :
1285 14918 : explicit OGRWKBPointUpdaterReproj(OGRCoordinateTransformation *poCTIn,
1286 : OGREnvelope3D &sEnvelopeIn)
1287 14918 : : m_poCT(poCTIn), m_sEnvelope(sEnvelopeIn)
1288 : {
1289 14918 : }
1290 :
1291 14943 : bool update(bool bNeedSwap, void *x, void *y, void *z,
1292 : void * /* m */) override
1293 : {
1294 : double dfX, dfY, dfZ;
1295 14943 : memcpy(&dfX, x, sizeof(double));
1296 14943 : memcpy(&dfY, y, sizeof(double));
1297 14943 : if (bNeedSwap)
1298 : {
1299 1116 : CPL_SWAP64PTR(&dfX);
1300 1116 : CPL_SWAP64PTR(&dfY);
1301 : }
1302 14943 : if (!(std::isnan(dfX) && std::isnan(dfY)))
1303 : {
1304 14743 : if (z)
1305 : {
1306 1166 : memcpy(&dfZ, z, sizeof(double));
1307 1166 : if (bNeedSwap)
1308 : {
1309 646 : CPL_SWAP64PTR(&dfZ);
1310 : }
1311 : }
1312 : else
1313 13577 : dfZ = 0;
1314 14743 : int nErrorCode = 0;
1315 14743 : m_poCT->TransformWithErrorCodes(1, &dfX, &dfY, &dfZ, nullptr,
1316 14743 : &nErrorCode);
1317 14743 : if (nErrorCode)
1318 3 : return false;
1319 14740 : m_sEnvelope.Merge(dfX, dfY, dfZ);
1320 14740 : if (bNeedSwap)
1321 : {
1322 1016 : CPL_SWAP64PTR(&dfX);
1323 1016 : CPL_SWAP64PTR(&dfY);
1324 1016 : CPL_SWAP64PTR(&dfZ);
1325 : }
1326 14740 : memcpy(x, &dfX, sizeof(double));
1327 14740 : memcpy(y, &dfY, sizeof(double));
1328 14740 : if (z)
1329 1166 : memcpy(z, &dfZ, sizeof(double));
1330 : }
1331 14940 : return true;
1332 : }
1333 :
1334 : private:
1335 : OGRWKBPointUpdaterReproj(const OGRWKBPointUpdaterReproj &) = delete;
1336 : OGRWKBPointUpdaterReproj &
1337 : operator=(const OGRWKBPointUpdaterReproj &) = delete;
1338 : };
1339 :
1340 14918 : sEnvelope = OGREnvelope3D();
1341 29836 : OGRWKBPointUpdaterReproj oUpdater(poCT, sEnvelope);
1342 29836 : return OGRWKBUpdatePoints(pabyWkb, nWKBSize, oUpdater);
1343 :
1344 : #else
1345 : struct OGRWKBPointUpdaterReproj final : public OGRWKBPointUpdater
1346 : {
1347 : OGRWKBTransformCache &m_oCache;
1348 :
1349 : explicit OGRWKBPointUpdaterReproj(OGRWKBTransformCache &oCacheIn)
1350 : : m_oCache(oCacheIn)
1351 : {
1352 : }
1353 :
1354 : bool update(bool bNeedSwap, void *x, void *y, void *z,
1355 : void * /* m */) override
1356 : {
1357 : m_oCache.abNeedSwap.push_back(bNeedSwap);
1358 : m_oCache.apdfX.push_back(x);
1359 : m_oCache.apdfY.push_back(y);
1360 : m_oCache.apdfZ.push_back(z);
1361 : return true;
1362 : }
1363 : };
1364 :
1365 : oCache.clear();
1366 : OGRWKBPointUpdaterReproj oUpdater(oCache);
1367 : if (!OGRWKBUpdatePoints(pabyWkb, nWKBSize, oUpdater))
1368 : return false;
1369 :
1370 : oCache.adfX.resize(oCache.apdfX.size());
1371 : oCache.adfY.resize(oCache.apdfX.size());
1372 : oCache.adfZ.resize(oCache.apdfX.size());
1373 :
1374 : for (size_t i = 0; i < oCache.apdfX.size(); ++i)
1375 : {
1376 : memcpy(&oCache.adfX[i], oCache.apdfX[i], sizeof(double));
1377 : memcpy(&oCache.adfY[i], oCache.apdfY[i], sizeof(double));
1378 : if (oCache.apdfZ[i])
1379 : memcpy(&oCache.adfZ[i], oCache.apdfZ[i], sizeof(double));
1380 : if (oCache.abNeedSwap[i])
1381 : {
1382 : CPL_SWAP64PTR(&oCache.adfX[i]);
1383 : CPL_SWAP64PTR(&oCache.adfY[i]);
1384 : CPL_SWAP64PTR(&oCache.adfZ[i]);
1385 : }
1386 : oCache.abIsEmpty.push_back(std::isnan(oCache.adfX[i]) &&
1387 : std::isnan(oCache.adfY[i]));
1388 : }
1389 :
1390 : oCache.anErrorCodes.resize(oCache.apdfX.size());
1391 : poCT->TransformWithErrorCodes(static_cast<int>(oCache.apdfX.size()),
1392 : oCache.adfX.data(), oCache.adfY.data(),
1393 : oCache.adfZ.data(), nullptr,
1394 : oCache.anErrorCodes.data());
1395 :
1396 : for (size_t i = 0; i < oCache.apdfX.size(); ++i)
1397 : {
1398 : if (!oCache.abIsEmpty[i] && oCache.anErrorCodes[i])
1399 : return false;
1400 : }
1401 :
1402 : sEnvelope = OGREnvelope3D();
1403 : for (size_t i = 0; i < oCache.apdfX.size(); ++i)
1404 : {
1405 : if (oCache.abIsEmpty[i])
1406 : {
1407 : oCache.adfX[i] = std::numeric_limits<double>::quiet_NaN();
1408 : oCache.adfY[i] = std::numeric_limits<double>::quiet_NaN();
1409 : oCache.adfZ[i] = std::numeric_limits<double>::quiet_NaN();
1410 : }
1411 : else
1412 : {
1413 : sEnvelope.Merge(oCache.adfX[i], oCache.adfY[i], oCache.adfZ[i]);
1414 : }
1415 : if (oCache.abNeedSwap[i])
1416 : {
1417 : CPL_SWAP64PTR(&oCache.adfX[i]);
1418 : CPL_SWAP64PTR(&oCache.adfY[i]);
1419 : CPL_SWAP64PTR(&oCache.adfZ[i]);
1420 : }
1421 : memcpy(oCache.apdfX[i], &oCache.adfX[i], sizeof(double));
1422 : memcpy(oCache.apdfY[i], &oCache.adfY[i], sizeof(double));
1423 : if (oCache.apdfZ[i])
1424 : memcpy(oCache.apdfZ[i], &oCache.adfZ[i], sizeof(double));
1425 : }
1426 :
1427 : return true;
1428 : #endif
1429 : }
1430 :
1431 : /************************************************************************/
1432 : /* OGRAppendBuffer() */
1433 : /************************************************************************/
1434 :
1435 : OGRAppendBuffer::OGRAppendBuffer() = default;
1436 :
1437 : /************************************************************************/
1438 : /* ~OGRAppendBuffer() */
1439 : /************************************************************************/
1440 :
1441 : OGRAppendBuffer::~OGRAppendBuffer() = default;
1442 :
1443 : /************************************************************************/
1444 : /* OGRWKTToWKBTranslator() */
1445 : /************************************************************************/
1446 :
1447 18 : OGRWKTToWKBTranslator::OGRWKTToWKBTranslator(OGRAppendBuffer &oAppendBuffer)
1448 18 : : m_oAppendBuffer(oAppendBuffer)
1449 : {
1450 : #ifndef USE_FAST_FLOAT
1451 : // Test if current locale decimal separator is decimal point
1452 : char szTest[10];
1453 : snprintf(szTest, sizeof(szTest), "%f", 1.5);
1454 : m_bCanUseStrtod = strchr(szTest, '.') != nullptr;
1455 : #endif
1456 18 : CPL_IGNORE_RET_VAL(m_bCanUseStrtod);
1457 18 : }
1458 :
1459 : /************************************************************************/
1460 : /* TranslateWKT() */
1461 : /************************************************************************/
1462 :
1463 138 : size_t OGRWKTToWKBTranslator::TranslateWKT(void *pabyWKTStart, size_t nLength,
1464 : bool bCanAlterByteAfter)
1465 : {
1466 138 : const char *pszPtrStart = static_cast<const char *>(pabyWKTStart);
1467 : // Optimize single-part single-ring multipolygon WKT->WKB translation
1468 138 : if (bCanAlterByteAfter && nLength > strlen("MULTIPOLYGON") &&
1469 28 : EQUALN(pszPtrStart, "MULTIPOLYGON", strlen("MULTIPOLYGON")))
1470 : {
1471 28 : int nCountOpenPar = 0;
1472 28 : size_t nCountComma = 0;
1473 28 : bool bHasZ = false;
1474 28 : bool bHasM = false;
1475 :
1476 28 : char *pszEnd = static_cast<char *>(pabyWKTStart) + nLength;
1477 28 : const char chBackup = *pszEnd;
1478 28 : *pszEnd = 0;
1479 :
1480 : // Checks that the multipolygon consists of a single part with
1481 : // only an exterior ring.
1482 852 : for (const char *pszPtr = pszPtrStart + strlen("MULTIPOLYGON"); *pszPtr;
1483 : ++pszPtr)
1484 : {
1485 832 : const char ch = *pszPtr;
1486 832 : if (ch == 'Z')
1487 8 : bHasZ = true;
1488 824 : else if (ch == 'M')
1489 8 : bHasM = true;
1490 832 : if (ch == '(')
1491 : {
1492 84 : nCountOpenPar++;
1493 84 : if (nCountOpenPar == 4)
1494 0 : break;
1495 : }
1496 748 : else if (ch == ')')
1497 : {
1498 72 : nCountOpenPar--;
1499 72 : if (nCountOpenPar < 0)
1500 0 : break;
1501 : }
1502 676 : else if (ch == ',')
1503 : {
1504 92 : if (nCountOpenPar < 3)
1505 : {
1506 : // multipart / multi-ring
1507 8 : break;
1508 : }
1509 84 : nCountComma++;
1510 : }
1511 : }
1512 28 : const int nDim = 2 + (bHasZ ? 1 : 0) + (bHasM ? 1 : 0);
1513 48 : if (nCountOpenPar == 0 && nCountComma > 0 &&
1514 20 : nCountComma < std::numeric_limits<uint32_t>::max())
1515 : {
1516 20 : const uint32_t nVerticesCount =
1517 20 : static_cast<uint32_t>(nCountComma + 1);
1518 20 : const size_t nWKBSize =
1519 : sizeof(GByte) + // Endianness
1520 : sizeof(uint32_t) + // multipolygon WKB geometry type
1521 : sizeof(uint32_t) + // number of parts
1522 : sizeof(GByte) + // Endianness
1523 : sizeof(uint32_t) + // polygon WKB geometry type
1524 : sizeof(uint32_t) + // number of rings
1525 : sizeof(uint32_t) + // number of vertices
1526 20 : nDim * sizeof(double) * nVerticesCount;
1527 : GByte *const pabyCurStart = static_cast<GByte *>(
1528 20 : m_oAppendBuffer.GetPtrForNewBytes(nWKBSize));
1529 20 : if (!pabyCurStart)
1530 : {
1531 0 : return static_cast<size_t>(-1);
1532 : }
1533 20 : GByte *pabyCur = pabyCurStart;
1534 : // Multipolygon byte order
1535 : {
1536 20 : *pabyCur = wkbNDR;
1537 20 : pabyCur++;
1538 : }
1539 : // Multipolygon geometry type
1540 : {
1541 20 : uint32_t nWKBGeomType =
1542 20 : wkbMultiPolygon + (bHasZ ? 1000 : 0) + (bHasM ? 2000 : 0);
1543 20 : CPL_LSBPTR32(&nWKBGeomType);
1544 20 : memcpy(pabyCur, &nWKBGeomType, sizeof(uint32_t));
1545 20 : pabyCur += sizeof(uint32_t);
1546 : }
1547 : // Number of parts
1548 : {
1549 20 : uint32_t nOne = 1;
1550 20 : CPL_LSBPTR32(&nOne);
1551 20 : memcpy(pabyCur, &nOne, sizeof(uint32_t));
1552 20 : pabyCur += sizeof(uint32_t);
1553 : }
1554 : // Polygon byte order
1555 : {
1556 20 : *pabyCur = wkbNDR;
1557 20 : pabyCur++;
1558 : }
1559 : // Polygon geometry type
1560 : {
1561 20 : uint32_t nWKBGeomType =
1562 20 : wkbPolygon + (bHasZ ? 1000 : 0) + (bHasM ? 2000 : 0);
1563 20 : CPL_LSBPTR32(&nWKBGeomType);
1564 20 : memcpy(pabyCur, &nWKBGeomType, sizeof(uint32_t));
1565 20 : pabyCur += sizeof(uint32_t);
1566 : }
1567 : // Number of rings
1568 : {
1569 20 : uint32_t nOne = 1;
1570 20 : CPL_LSBPTR32(&nOne);
1571 20 : memcpy(pabyCur, &nOne, sizeof(uint32_t));
1572 20 : pabyCur += sizeof(uint32_t);
1573 : }
1574 : // Number of vertices
1575 : {
1576 20 : uint32_t nVerticesCountToWrite = nVerticesCount;
1577 20 : CPL_LSBPTR32(&nVerticesCountToWrite);
1578 20 : memcpy(pabyCur, &nVerticesCountToWrite, sizeof(uint32_t));
1579 20 : pabyCur += sizeof(uint32_t);
1580 : }
1581 20 : uint32_t nDoubleCount = 0;
1582 20 : const uint32_t nExpectedDoubleCount = nVerticesCount * nDim;
1583 616 : for (const char *pszPtr = pszPtrStart + strlen("MULTIPOLYGON");
1584 616 : *pszPtr;
1585 : /* nothing */)
1586 : {
1587 596 : const char ch = *pszPtr;
1588 596 : if (ch == '-' || ch == '.' || (ch >= '0' && ch <= '9'))
1589 : {
1590 224 : nDoubleCount++;
1591 224 : if (nDoubleCount > nExpectedDoubleCount)
1592 : {
1593 0 : break;
1594 : }
1595 : #ifdef USE_FAST_FLOAT
1596 : double dfVal;
1597 224 : auto answer = fast_float::from_chars(pszPtr, pszEnd, dfVal);
1598 224 : if (answer.ec != std::errc())
1599 : {
1600 0 : nDoubleCount = 0;
1601 0 : break;
1602 : }
1603 224 : pszPtr = answer.ptr;
1604 : #else
1605 : char *endptr = nullptr;
1606 : const double dfVal =
1607 : m_bCanUseStrtod ? strtod(pszPtr, &endptr)
1608 : : CPLStrtodDelim(pszPtr, &endptr, '.');
1609 : pszPtr = endptr;
1610 : #endif
1611 224 : CPL_LSBPTR64(&dfVal);
1612 224 : memcpy(pabyCur, &dfVal, sizeof(double));
1613 224 : pabyCur += sizeof(double);
1614 : }
1615 : else
1616 : {
1617 372 : ++pszPtr;
1618 : }
1619 : }
1620 20 : if (nDoubleCount == nExpectedDoubleCount)
1621 : {
1622 20 : CPLAssert(static_cast<size_t>(pabyCur - pabyCurStart) ==
1623 : nWKBSize);
1624 : // cppcheck-suppress selfAssignment
1625 20 : *pszEnd = chBackup;
1626 20 : return nWKBSize;
1627 : }
1628 : else
1629 : {
1630 0 : CPLError(CE_Failure, CPLE_AppDefined,
1631 : "Invalid WKT geometry: %s", pszPtrStart);
1632 : // cppcheck-suppress selfAssignment
1633 0 : *pszEnd = chBackup;
1634 0 : return static_cast<size_t>(-1);
1635 : }
1636 : }
1637 : // cppcheck-suppress selfAssignment
1638 8 : *pszEnd = chBackup;
1639 : }
1640 :
1641 : // General case going through a OGRGeometry
1642 236 : std::unique_ptr<OGRGeometry> poGeometry = nullptr;
1643 118 : if (bCanAlterByteAfter)
1644 : {
1645 : // Slight optimization for all geometries but the final one, to
1646 : // avoid creating a new string each time.
1647 : // We set the ending byte to '\0' and restore it back after parsing
1648 : // the WKT
1649 100 : char *pszEnd = static_cast<char *>(pabyWKTStart) + nLength;
1650 100 : const char chBackup = *pszEnd;
1651 100 : *pszEnd = 0;
1652 100 : poGeometry = OGRGeometryFactory::createFromWkt(pszPtrStart).first;
1653 : // cppcheck-suppress selfAssignment
1654 100 : *pszEnd = chBackup;
1655 : }
1656 : else
1657 : {
1658 18 : std::string osTmp;
1659 18 : osTmp.assign(pszPtrStart, nLength);
1660 18 : poGeometry = OGRGeometryFactory::createFromWkt(osTmp.c_str()).first;
1661 : }
1662 118 : if (!poGeometry)
1663 : {
1664 0 : CPLError(CE_Failure, CPLE_AppDefined, "Invalid WKT geometry");
1665 0 : return static_cast<size_t>(-1);
1666 : }
1667 118 : const size_t nWKBSize = poGeometry->WkbSize();
1668 : GByte *pabyWKB =
1669 118 : static_cast<GByte *>(m_oAppendBuffer.GetPtrForNewBytes(nWKBSize));
1670 118 : if (!pabyWKB)
1671 : {
1672 0 : return static_cast<size_t>(-1);
1673 : }
1674 118 : poGeometry->exportToWkb(wkbNDR, pabyWKB, wkbVariantIso);
1675 118 : return nWKBSize;
1676 : }
|