geometry-io-wkb 0.0.8

OGC Well-Known Binary (WKB) reader and writer for the geometry model.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
//! The WKB recursive-descent parser.
//!
//! Reads a byte buffer laid out per OGC Simple Feature Access 06-103r4
//! §8.2 and emits a [`DynGeometry`]. Each record opens with an
//! endianness flag and a 32-bit type tag (§8.2.3–8.2.4); the multi and
//! collection kinds nest *complete* WKB records (each with its own
//! byte-order flag and header), so the parser recurses through
//! [`Parser::parse_geometry`] for every member. This port emits a
//! [`DynGeometry`] because WKB is heterogeneous by construction (a
//! `GeometryCollection` mixes kinds), matching the sibling WKT reader.
//!
//! # Dimension handling (2D only)
//!
//! This is a strictly-2D reader. The 32-bit type tag is inspected for
//! `Z`/`M` markers in both encodings the wild uses: the EWKB/OGC high
//! bits (`0x8000_0000` = Z, `0x4000_0000` = M) and the ISO SQL/MM
//! ranges (`1000`+ = Z, `2000`+ = M, `3000`+ = ZM). Any such marker is
//! **rejected** with [`WkbError::UnsupportedDimension`] rather than
//! silently dropping the extra ordinates — a WKB reader cannot re-emit
//! bytes it discarded, so a lossy read would break round-trip parity.
//! Only the seven base codes `1..=7` are accepted.
//!
//! Reference: OGC 06-103r4 §8.2.

use alloc::vec::Vec;

use geometry_cs::Cartesian;
use geometry_model::{
    DynGeometry, Linestring, MultiLinestring, MultiPoint, MultiPolygon, Point2D, Polygon, Ring,
};

use crate::header::{ByteOrder, Cursor, WkbError};

/// A concrete 2D Cartesian point — the coordinate type every parsed
/// geometry is built from.
type Pt = Point2D<f64, Cartesian>;

/// OGC base type code for `Point` (06-103r4 §8.2.4).
const WKB_POINT: u32 = 1;
/// OGC base type code for `LineString`.
const WKB_LINESTRING: u32 = 2;
/// OGC base type code for `Polygon`.
const WKB_POLYGON: u32 = 3;
/// OGC base type code for `MultiPoint`.
const WKB_MULTIPOINT: u32 = 4;
/// OGC base type code for `MultiLineString`.
const WKB_MULTILINESTRING: u32 = 5;
/// OGC base type code for `MultiPolygon`.
const WKB_MULTIPOLYGON: u32 = 6;
/// OGC base type code for `GeometryCollection`.
const WKB_GEOMETRYCOLLECTION: u32 = 7;

/// EWKB high bit marking a `Z` (3D) geometry-type tag.
const EWKB_Z: u32 = 0x8000_0000;
/// EWKB high bit marking an `M` (measured) geometry-type tag.
const EWKB_M: u32 = 0x4000_0000;
/// EWKB high bit marking a spatial-reference id in the tag.
const EWKB_SRID: u32 = 0x2000_0000;

/// The OGC base type code of an already-parsed geometry — used to
/// report the *found* kind when a multi-geometry member has the wrong
/// type.
fn dyn_code(g: &DynGeometry<f64, Cartesian>) -> u32 {
    match g {
        DynGeometry::Point(_) => WKB_POINT,
        DynGeometry::LineString(_) => WKB_LINESTRING,
        DynGeometry::Polygon(_) => WKB_POLYGON,
        DynGeometry::MultiPoint(_) => WKB_MULTIPOINT,
        DynGeometry::MultiLineString(_) => WKB_MULTILINESTRING,
        DynGeometry::MultiPolygon(_) => WKB_MULTIPOLYGON,
        DynGeometry::GeometryCollection(_) => WKB_GEOMETRYCOLLECTION,
    }
}

/// Reduce a raw 32-bit type tag to its base OGC code (`1..=7`),
/// rejecting any `Z`/`M`/`ZM` dimension marker.
///
/// Handles both encodings seen in real WKB:
/// * EWKB flag bits (`0x8000_0000` Z, `0x4000_0000` M, `0x2000_0000`
///   SRID), and
/// * ISO SQL/MM code ranges (`1000`+ Z, `2000`+ M, `3000`+ ZM).
fn base_type_code(tag: u32) -> Result<u32, WkbError> {
    // Any EWKB Z/M flag → higher dimension, unsupported. An SRID flag
    // would also prefix extra bytes this 2D reader does not consume.
    if tag & (EWKB_Z | EWKB_M | EWKB_SRID) != 0 {
        return Err(WkbError::UnsupportedDimension);
    }
    // ISO SQL/MM: strip the thousands digit; anything above the 2D
    // range (base + 1000/2000/3000) carries Z and/or M.
    let base = tag % 1000;
    if tag != base {
        return Err(WkbError::UnsupportedDimension);
    }
    Ok(base)
}

/// Maximum WKB record nesting depth accepted while parsing. The multi
/// and collection kinds recurse through [`Parser::parse_geometry`] per
/// member, so an adversarial buffer of tens of thousands of nested
/// `GeometryCollection` headers would otherwise overflow the native
/// stack and **abort the process** (a stack overflow is not catchable).
/// A bounded depth turns that denial-of-service into a recoverable
/// error. `128` mirrors the sibling `GeoJSON` reader's cap and the
/// `serde_json` default; real WKB nests only a few levels (a `Point`
/// inside a `MultiPoint` inside a `GeometryCollection`).
const MAX_DEPTH: usize = 128;

/// Bytes in one 2D point body: two `f64` ordinates.
const POINT_BYTES: usize = 16;

/// Smallest number of bytes a nested WKB record occupies: a one-byte
/// byte-order flag plus a 4-byte type tag (§8.2.3–8.2.4). A multi /
/// collection count cannot describe more members than `remaining / 5`.
const MIN_RECORD_BYTES: usize = 5;

/// Pre-reserve capacity for `count` elements, but never more than the
/// remaining buffer could actually contain (`remaining / min_elem_bytes`).
///
/// A raw WKB count is an untrusted `u32`; reserving `count` directly lets
/// a tiny corrupt buffer (e.g. a `MultiPolygon` header claiming
/// `0xFFFF_FFFF` members with no body) drive a multi-gigabyte
/// `Vec::with_capacity`, which aborts the process under a non-overcommit
/// allocator (this crate is `no_std`-capable). Clamping to what the
/// buffer can hold makes the reservation self-limiting; the read loop
/// then errors with [`WkbError::UnexpectedEof`] on the missing bytes.
fn reserve_bounded<T>(count: u32, remaining: usize, min_elem_bytes: usize) -> Vec<T> {
    let cap = (count as usize).min(remaining / min_elem_bytes);
    Vec::with_capacity(cap)
}

/// A cursor over a WKB buffer plus the recursive-descent readers.
struct Parser<'a> {
    cursor: Cursor<'a>,
}

impl<'a> Parser<'a> {
    fn new(bytes: &'a [u8]) -> Self {
        Self {
            cursor: Cursor::new(bytes),
        }
    }

    /// Read one point body: two `f64` ordinates in `order`.
    fn read_point(&mut self, order: ByteOrder) -> Result<Pt, WkbError> {
        let x = self.cursor.read_f64(order)?;
        let y = self.cursor.read_f64(order)?;
        Ok(Point2D::new(x, y))
    }

    /// Read a `uint32` count followed by that many point bodies.
    /// Used by `LineString` and by each ring of a `Polygon`.
    fn read_point_run(&mut self, order: ByteOrder) -> Result<Vec<Pt>, WkbError> {
        let n = self.cursor.read_u32(order)? as usize;
        let byte_len = n.checked_mul(POINT_BYTES).ok_or(WkbError::UnexpectedEof)?;
        let bytes = self.cursor.read_slice(byte_len)?;
        let mut pts = Vec::with_capacity(n);
        for point in bytes.chunks_exact(POINT_BYTES) {
            let x_bytes: [u8; 8] = point[..8]
                .try_into()
                .expect("a point chunk contains its x ordinate");
            let y_bytes: [u8; 8] = point[8..]
                .try_into()
                .expect("a point chunk contains its y ordinate");
            let (x, y) = match order {
                ByteOrder::LittleEndian => {
                    (f64::from_le_bytes(x_bytes), f64::from_le_bytes(y_bytes))
                }
                ByteOrder::BigEndian => (f64::from_be_bytes(x_bytes), f64::from_be_bytes(y_bytes)),
            };
            pts.push(Point2D::new(x, y));
        }
        Ok(pts)
    }

    /// `Point` body.
    fn read_point_body(
        &mut self,
        order: ByteOrder,
    ) -> Result<DynGeometry<f64, Cartesian>, WkbError> {
        Ok(DynGeometry::Point(self.read_point(order)?))
    }

    /// `LineString` body: `uint32` numPoints, then the points.
    fn read_linestring_body(
        &mut self,
        order: ByteOrder,
    ) -> Result<DynGeometry<f64, Cartesian>, WkbError> {
        Ok(DynGeometry::LineString(Linestring(
            self.read_point_run(order)?,
        )))
    }

    /// Shared `Polygon` value: `uint32` numRings, then each ring is a
    /// `uint32` numPoints + points. The first ring is the exterior.
    fn read_polygon_value(&mut self, order: ByteOrder) -> Result<Polygon<Pt>, WkbError> {
        let ring_count = self.cursor.read_u32(order)?;
        let mut rings = (0..ring_count).map(|_| self.read_point_run(order).map(Ring::from_vec));
        let outer = match rings.next() {
            Some(r) => r?,
            None => Ring::new(),
        };
        let mut poly = Polygon::new(outer);
        for ring in rings {
            poly.inners.push(ring?);
        }
        Ok(poly)
    }

    /// `Polygon` body.
    fn read_polygon_body(
        &mut self,
        order: ByteOrder,
    ) -> Result<DynGeometry<f64, Cartesian>, WkbError> {
        Ok(DynGeometry::Polygon(self.read_polygon_value(order)?))
    }

    /// `MultiPoint` body: `uint32` numGeoms, then each member is a full
    /// nested WKB `Point` record (its own byte-order flag + header).
    fn read_multipoint_body(
        &mut self,
        order: ByteOrder,
        depth: usize,
    ) -> Result<DynGeometry<f64, Cartesian>, WkbError> {
        let n = self.cursor.read_u32(order)?;
        let mut pts = reserve_bounded(n, self.cursor.remaining(), MIN_RECORD_BYTES);
        for _ in 0..n {
            match self.parse_geometry(depth + 1)? {
                DynGeometry::Point(p) => pts.push(p),
                other => {
                    return Err(WkbError::MismatchedMemberType {
                        expected: WKB_POINT,
                        found: dyn_code(&other),
                    });
                }
            }
        }
        Ok(DynGeometry::MultiPoint(MultiPoint(pts)))
    }

    /// `MultiLineString` body: `uint32` count, then nested WKB
    /// `LineString` records.
    fn read_multilinestring_body(
        &mut self,
        order: ByteOrder,
        depth: usize,
    ) -> Result<DynGeometry<f64, Cartesian>, WkbError> {
        let n = self.cursor.read_u32(order)?;
        let mut lines = reserve_bounded(n, self.cursor.remaining(), MIN_RECORD_BYTES);
        for _ in 0..n {
            match self.parse_geometry(depth + 1)? {
                DynGeometry::LineString(ls) => lines.push(ls),
                other => {
                    return Err(WkbError::MismatchedMemberType {
                        expected: WKB_LINESTRING,
                        found: dyn_code(&other),
                    });
                }
            }
        }
        Ok(DynGeometry::MultiLineString(MultiLinestring(lines)))
    }

    /// `MultiPolygon` body: `uint32` count, then nested WKB `Polygon`
    /// records.
    fn read_multipolygon_body(
        &mut self,
        order: ByteOrder,
        depth: usize,
    ) -> Result<DynGeometry<f64, Cartesian>, WkbError> {
        let n = self.cursor.read_u32(order)?;
        let mut polys = reserve_bounded(n, self.cursor.remaining(), MIN_RECORD_BYTES);
        for _ in 0..n {
            match self.parse_geometry(depth + 1)? {
                DynGeometry::Polygon(pg) => polys.push(pg),
                other => {
                    return Err(WkbError::MismatchedMemberType {
                        expected: WKB_POLYGON,
                        found: dyn_code(&other),
                    });
                }
            }
        }
        Ok(DynGeometry::MultiPolygon(MultiPolygon(polys)))
    }

    /// `GeometryCollection` body: `uint32` count, then that many full
    /// nested WKB records of any kind. Recurses into
    /// [`Parser::parse_geometry`].
    fn read_collection_body(
        &mut self,
        order: ByteOrder,
        depth: usize,
    ) -> Result<DynGeometry<f64, Cartesian>, WkbError> {
        let n = self.cursor.read_u32(order)?;
        let mut items = reserve_bounded(n, self.cursor.remaining(), MIN_RECORD_BYTES);
        for _ in 0..n {
            items.push(self.parse_geometry(depth + 1)?);
        }
        Ok(DynGeometry::GeometryCollection(items))
    }

    /// Parse one complete WKB record: byte-order flag, 32-bit type tag
    /// (read in that order), then the kind-specific body. Mirrors the
    /// header dispatch of OGC 06-103r4 §8.2. `depth` bounds the multi /
    /// collection recursion against [`MAX_DEPTH`] so adversarial nesting
    /// fails with a recoverable error instead of overflowing the stack.
    fn parse_geometry(&mut self, depth: usize) -> Result<DynGeometry<f64, Cartesian>, WkbError> {
        if depth >= MAX_DEPTH {
            return Err(WkbError::NestingTooDeep);
        }
        let order = self.cursor.read_byte_order()?;
        let tag = self.cursor.read_u32(order)?;
        let code = base_type_code(tag)?;
        match code {
            WKB_POINT => self.read_point_body(order),
            WKB_LINESTRING => self.read_linestring_body(order),
            WKB_POLYGON => self.read_polygon_body(order),
            WKB_MULTIPOINT => self.read_multipoint_body(order, depth),
            WKB_MULTILINESTRING => self.read_multilinestring_body(order, depth),
            WKB_MULTIPOLYGON => self.read_multipolygon_body(order, depth),
            WKB_GEOMETRYCOLLECTION => self.read_collection_body(order, depth),
            other => Err(WkbError::UnknownGeometryType(other)),
        }
    }
}

/// Parse an OGC Well-Known Binary buffer into a runtime-tagged
/// [`DynGeometry`].
///
/// Implements the OGC kinds `Point` (1), `LineString` (2), `Polygon`
/// (3), `MultiPoint` (4), `MultiLineString` (5), `MultiPolygon` (6), and
/// `GeometryCollection` (7). The multi and collection kinds nest
/// *complete* WKB records — each nested member carries its own
/// byte-order flag and type header — and are read recursively. Mirrors
/// the read path implied by OGC 06-103r4 §8.2.
///
/// # Dimension handling
///
/// This reader is strictly 2D. A type tag carrying a `Z`/`M`/`ZM`
/// marker (EWKB flag bits or ISO SQL/MM `1000`+ codes) is **rejected**
/// with [`WkbError::UnsupportedDimension`]; extra ordinates are never
/// silently dropped.
///
/// # Errors
///
/// Returns a [`WkbError`] on a truncated buffer
/// ([`WkbError::UnexpectedEof`]), an invalid byte-order flag
/// ([`WkbError::InvalidByteOrder`]), an unknown or higher-dimension type
/// tag ([`WkbError::UnknownGeometryType`] /
/// [`WkbError::UnsupportedDimension`]), trailing bytes after the
/// top-level geometry ([`WkbError::TrailingBytes`]), or multi/collection
/// nesting past the recursion limit ([`WkbError::NestingTooDeep`]).
///
/// # Examples
///
/// ```
/// use geometry_io_wkb::from_wkb;
/// use geometry_model::DynKind;
///
/// // Little-endian POINT(1 2): 0x01, type 1, then x=1.0, y=2.0.
/// let bytes = [
///     0x01, // little-endian
///     0x01, 0x00, 0x00, 0x00, // type 1 = Point
///     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F, // 1.0
///     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, // 2.0
/// ];
/// let g = from_wkb(&bytes).unwrap();
/// assert_eq!(g.kind(), DynKind::Point);
/// ```
pub fn from_wkb(bytes: &[u8]) -> Result<DynGeometry<f64, Cartesian>, WkbError> {
    let mut parser = Parser::new(bytes);
    let g = parser.parse_geometry(0)?;
    if parser.cursor.is_empty() {
        Ok(g)
    } else {
        Err(WkbError::TrailingBytes)
    }
}

#[cfg(test)]
mod tests {
    //! Hand-crafted little-endian byte fixtures per OGC 06-103r4 §8.2.
    #![allow(
        clippy::float_cmp,
        reason = "coordinate values come from exact integer byte literals"
    )]

    use super::*;
    use alloc::vec;

    /// The 8 little-endian bytes of the f64 `1.0`.
    const F1: [u8; 8] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F];
    /// The 8 little-endian bytes of the f64 `2.0`.
    const F2: [u8; 8] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40];
    /// The 8 little-endian bytes of the f64 `3.0`.
    const F3: [u8; 8] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x40];

    #[test]
    fn le_point() {
        // 0x01 LE, type 1, x=1.0, y=2.0.
        let mut b = vec![0x01, 0x01, 0x00, 0x00, 0x00];
        b.extend_from_slice(&F1);
        b.extend_from_slice(&F2);
        let g = from_wkb(&b).unwrap();
        assert_eq!(g, DynGeometry::Point(Pt::new(1.0, 2.0)));
    }

    #[test]
    fn le_linestring_two_points() {
        // 0x01 LE, type 2, numPoints=2, (1,2), (3,1).
        let mut b = vec![0x01, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00];
        b.extend_from_slice(&F1);
        b.extend_from_slice(&F2);
        b.extend_from_slice(&F3);
        b.extend_from_slice(&F1);
        let g = from_wkb(&b).unwrap();
        assert_eq!(
            g,
            DynGeometry::LineString(Linestring::from_vec(vec![
                Pt::new(1.0, 2.0),
                Pt::new(3.0, 1.0),
            ]))
        );
    }

    #[test]
    fn le_polygon_one_ring() {
        // 0x01 LE, type 3, numRings=1, numPoints=3, (1,2),(3,1),(1,2).
        let mut b = vec![
            0x01, 0x03, 0x00, 0x00, 0x00, // header
            0x01, 0x00, 0x00, 0x00, // numRings = 1
            0x03, 0x00, 0x00, 0x00, // numPoints = 3
        ];
        b.extend_from_slice(&F1);
        b.extend_from_slice(&F2);
        b.extend_from_slice(&F3);
        b.extend_from_slice(&F1);
        b.extend_from_slice(&F1);
        b.extend_from_slice(&F2);
        let g = from_wkb(&b).unwrap();
        assert_eq!(
            g,
            DynGeometry::Polygon(Polygon::new(Ring::from_vec(vec![
                Pt::new(1.0, 2.0),
                Pt::new(3.0, 1.0),
                Pt::new(1.0, 2.0),
            ])))
        );
    }

    #[test]
    fn trailing_bytes_rejected() {
        let mut b = vec![0x01, 0x01, 0x00, 0x00, 0x00];
        b.extend_from_slice(&F1);
        b.extend_from_slice(&F2);
        b.push(0xFF); // one byte too many
        assert_eq!(from_wkb(&b).unwrap_err(), WkbError::TrailingBytes);
    }

    #[test]
    fn z_dimension_rejected() {
        // Type tag 0x8000_0001 (EWKB Point Z).
        let b = vec![0x01, 0x01, 0x00, 0x00, 0x80];
        assert_eq!(from_wkb(&b).unwrap_err(), WkbError::UnsupportedDimension);
    }

    #[test]
    fn iso_z_dimension_rejected() {
        // ISO SQL/MM Point Z = 1001.
        let b = vec![0x01, 0xE9, 0x03, 0x00, 0x00];
        assert_eq!(from_wkb(&b).unwrap_err(), WkbError::UnsupportedDimension);
    }

    #[test]
    fn truncated_point_is_eof() {
        // Header says Point but no coordinate bytes follow.
        let b = vec![0x01, 0x01, 0x00, 0x00, 0x00];
        assert_eq!(from_wkb(&b).unwrap_err(), WkbError::UnexpectedEof);
    }

    #[test]
    fn hostile_count_does_not_over_reserve() {
        // Regression: a header claiming a `u32::MAX` element count with no
        // body must error gracefully, NOT drive a multi-gigabyte
        // `Vec::with_capacity` (which aborts under a non-overcommit
        // allocator). The reserve is clamped to `remaining / min_elem`,
        // and the read loop then hits EOF on the first missing element.
        // LineString with numPoints = 0xFFFF_FFFF, no points.
        let ls = vec![0x01, 0x02, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF];
        assert_eq!(from_wkb(&ls).unwrap_err(), WkbError::UnexpectedEof);
        // MultiPolygon with numGeoms = 0xFFFF_FFFF, no members.
        let mpg = vec![0x01, 0x06, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF];
        assert_eq!(from_wkb(&mpg).unwrap_err(), WkbError::UnexpectedEof);
        // GeometryCollection with the same hostile count.
        let gc = vec![0x01, 0x07, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF];
        assert_eq!(from_wkb(&gc).unwrap_err(), WkbError::UnexpectedEof);
    }

    #[test]
    fn deeply_nested_collections_are_rejected_without_overflow() {
        // Regression: a chain of nested GeometryCollection headers used to
        // recurse until the native stack overflowed (uncatchable SIGABRT).
        // Now the reader caps recursion and returns `NestingTooDeep`.
        // Each level is: LE flag, type 7 (GC), count = 1 (9 bytes).
        let mut b = vec![];
        for _ in 0..10_000 {
            b.push(0x01);
            b.extend_from_slice(&7u32.to_le_bytes());
            b.extend_from_slice(&1u32.to_le_bytes());
        }
        assert_eq!(from_wkb(&b).unwrap_err(), WkbError::NestingTooDeep);
    }

    #[test]
    fn multipoint_member_of_wrong_kind_reports_both_codes() {
        // MULTIPOINT header claiming 1 member, whose nested record is a
        // (valid, empty) LineString. The old reader reported
        // `UnknownGeometryType(1)` — the EXPECTED code, as if type 1 were
        // unknown. It must name both sides.
        let mut b = vec![0x01, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00];
        // nested record: LE, type 2 (LineString), numPoints = 0
        b.extend_from_slice(&[0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
        assert_eq!(
            from_wkb(&b).unwrap_err(),
            WkbError::MismatchedMemberType {
                expected: 1,
                found: 2
            }
        );
    }

    // ---- Building blocks for nested records --------------------------

    /// A little-endian `Point(1, 2)` WKB record (21 bytes).
    fn le_point_record() -> Vec<u8> {
        let mut b = vec![0x01, 0x01, 0x00, 0x00, 0x00];
        b.extend_from_slice(&F1);
        b.extend_from_slice(&F2);
        b
    }

    /// A little-endian empty `LineString` WKB record.
    fn le_empty_linestring_record() -> Vec<u8> {
        vec![0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
    }

    /// A little-endian empty `Polygon` WKB record (zero rings).
    fn le_empty_polygon_record() -> Vec<u8> {
        vec![0x01, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
    }

    /// A valid `MultiPoint` of two members parses to a two-point
    /// multipoint (the happy `pts.push` arm).
    #[test]
    fn le_multipoint_two_members() {
        let mut b = vec![0x01, 0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00];
        b.extend_from_slice(&le_point_record());
        b.extend_from_slice(&le_point_record());
        let g = from_wkb(&b).unwrap();
        assert_eq!(
            g,
            DynGeometry::MultiPoint(MultiPoint::from_vec(vec![
                Pt::new(1.0, 2.0),
                Pt::new(1.0, 2.0),
            ]))
        );
    }

    /// A valid `MultiLineString` of one empty member parses (the happy
    /// `lines.push` arm).
    #[test]
    fn le_multilinestring_one_member() {
        let mut b = vec![0x01, 0x05, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00];
        b.extend_from_slice(&le_empty_linestring_record());
        let g = from_wkb(&b).unwrap();
        assert_eq!(
            g,
            DynGeometry::MultiLineString(MultiLinestring::from_vec(vec![Linestring::from_vec(
                Vec::new()
            ),]))
        );
    }

    /// A valid `MultiPolygon` of one empty member parses (the happy
    /// `polys.push` arm).
    #[test]
    fn le_multipolygon_one_member() {
        let mut b = vec![0x01, 0x06, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00];
        b.extend_from_slice(&le_empty_polygon_record());
        let g = from_wkb(&b).unwrap();
        assert_eq!(
            g,
            DynGeometry::MultiPolygon(MultiPolygon::from_vec(vec![Polygon::new(Ring::new())]))
        );
    }

    /// A `GeometryCollection` mixing a point and a line string parses,
    /// preserving member kinds and order (the `items.push` arm).
    #[test]
    fn le_geometry_collection_mixed_members() {
        let mut b = vec![0x01, 0x07, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00];
        b.extend_from_slice(&le_point_record());
        b.extend_from_slice(&le_empty_linestring_record());
        let g = from_wkb(&b).unwrap();
        assert_eq!(
            g,
            DynGeometry::GeometryCollection(vec![
                DynGeometry::Point(Pt::new(1.0, 2.0)),
                DynGeometry::LineString(Linestring::from_vec(Vec::new())),
            ])
        );
    }

    /// A `MultiLineString` whose member is a `Point` reports the mismatch
    /// with both codes (expected 2, found 1) — the `dyn_code(Point)` arm.
    #[test]
    fn multilinestring_wrong_member_reports_both_codes() {
        let mut b = vec![0x01, 0x05, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00];
        b.extend_from_slice(&le_point_record());
        assert_eq!(
            from_wkb(&b).unwrap_err(),
            WkbError::MismatchedMemberType {
                expected: WKB_LINESTRING,
                found: WKB_POINT,
            }
        );
    }

    /// A `MultiPolygon` whose member is a `Point` reports the mismatch —
    /// the `dyn_code(Point)` arm through the polygon reader.
    #[test]
    fn multipolygon_wrong_member_reports_both_codes() {
        let mut b = vec![0x01, 0x06, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00];
        b.extend_from_slice(&le_point_record());
        assert_eq!(
            from_wkb(&b).unwrap_err(),
            WkbError::MismatchedMemberType {
                expected: WKB_POLYGON,
                found: WKB_POINT,
            }
        );
    }

    /// An unknown base type code (here 8, one past the OGC range) is
    /// rejected.
    #[test]
    fn unknown_base_type_is_rejected() {
        let b = vec![0x01, 0x08, 0x00, 0x00, 0x00];
        assert_eq!(from_wkb(&b).unwrap_err(), WkbError::UnknownGeometryType(8));
    }
}