maps: v9 section helpers in graph_zsec.rs; graph_part4.rs under 500 (#planet5)
- Commit
1cff964880317635425b72be1f2f6c74b2001923- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-14 21:07 UTC (21 days ago)
- Parents
167ff58
gbuild's :maps:rustFileLength failed: the graph v9 loader code took graph_part4.rs to 512 lines (limit 500). The three v9 checks both loaders repeated (ZSEC intermediate blob, ZSEC road-name pool and the edges.bin name-offset table, with the v6-v8 exact-length fallback) move into ZsecRegion::v9_blob / v9_names / v9_name_offsets; both loaders call them. graph_part4.rs 488 lines, graph_part2.rs 419. Same behaviour. Verified: cargo test -p offlinerouter (93 pass); the US West v9 route report is byte-identical before and after.
Diff
maps/src/main/rust/src/graph_part2.rs
diff --git a/maps/src/main/rust/src/graph_part2.rs b/maps/src/main/rust/src/graph_part2.rsindex 46cfc19825..2caf653454 100644--- a/maps/src/main/rust/src/graph_part2.rs+++ b/maps/src/main/rust/src/graph_part2.rs@@ -144,29 +144,9 @@ impl Graph { SECTION_ALIGN, ); let name_off_off = names_off + EdgeBitmap::bytes(edge_count);- let want_edges = name_off_off + named_edges * std::mem::size_of::<u32>() as u64; // Graph v9: the name-offset table (the file's tail) is a ZSEC section of that table.- let name_off_zsec = if zsec_v9 && named_edges > 0 {- if (edges_region.len as u64) <= name_off_off {- return None;- }- let tail = unsafe {- std::slice::from_raw_parts(- edges_region.base().add(name_off_off as usize),- edges_region.len - name_off_off as usize,- )- };- let z = ZsecRegion::parse(tail)?;- if z.raw_len() as u64 != named_edges * 4 {- return None;- }- Some(z)- } else {- if edges_region.len as u64 != want_edges {- return None;- }- None- };+ let edges_all = unsafe { std::slice::from_raw_parts(edges_region.base(), edges_region.len) };+ let name_off_zsec = ZsecRegion::v9_name_offsets(edges_all, name_off_off, named_edges, zsec_v9)?; // Table base is the file base unconditionally: only the record array // itself is blocked-compressed under v7 (see the single-archive // `assemble` — same fix, same on-device SIGSEGV). The per-record@@ -249,29 +229,18 @@ impl Graph { ) + u64::from(read_at::<u16>(intermediate_within, geometry_edges as usize)) }; // Graph v9: the blob is a ZSEC section of the raw blob the trailer addresses.- let intermediate_zsec = if zsec_v9 {- let z = ZsecRegion::parse(unsafe { std::slice::from_raw_parts(intermediate_data, blob_bytes as usize) })?;- Some(z)- } else {- None- };+ let intermediate_zsec = ZsecRegion::v9_blob(intermediate_data, blob_bytes, zsec_v9)?; let raw_blob = intermediate_zsec.as_ref().map_or(blob_bytes, |z| z.raw_len() as u64); if sentinel != raw_blob || geometry.total(edge_count) != geometry_edges { return None; } let road_names_region = MmapRegion::map(&format!("{base}road_names.bin"));- let (road_names, mut road_names_size) = match &road_names_region {+ let (road_names, road_names_size) = match &road_names_region { Some(r) => (r.base(), r.len), None => (ptr::null(), 0), };- let road_names_zsec = if zsec_v9 && !road_names.is_null() && road_names_size > 0 {- let z = ZsecRegion::parse(unsafe { std::slice::from_raw_parts(road_names, road_names_size) })?;- road_names_size = z.raw_len();- Some(z)- } else {- None- };+ let (road_names_zsec, road_names_size) = ZsecRegion::v9_names(road_names, road_names_size, zsec_v9)?; // lanes.bin: [ u32 n ][ LaneEntry[n + 1] ][ u16 lane-mask blob ]. The index // is sized by its own header rather than by `edge_count`, so it is validated
maps/src/main/rust/src/graph_part4.rs
diff --git a/maps/src/main/rust/src/graph_part4.rs b/maps/src/main/rust/src/graph_part4.rsindex cd05ae839c..04899dbb13 100644--- a/maps/src/main/rust/src/graph_part4.rs+++ b/maps/src/main/rust/src/graph_part4.rs@@ -171,23 +171,8 @@ impl Graph { SECTION_ALIGN, ); let name_off_off = names_off + EdgeBitmap::bytes(edge_count);- let want_edges = name_off_off + named_edges * std::mem::size_of::<u32>() as u64; // Graph v9: the name-offset table (the payload's tail) is a ZSEC section of that table.- let name_off_zsec = if zsec_v9 && named_edges > 0 {- if (edges_bytes.len() as u64) <= name_off_off {- return None;- }- let z = ZsecRegion::parse(&edges_bytes[name_off_off as usize..])?;- if z.raw_len() as u64 != named_edges * 4 {- return None;- }- Some(z)- } else {- if edges_bytes.len() as u64 != want_edges {- return None;- }- None- };+ let name_off_zsec = ZsecRegion::v9_name_offsets(edges_bytes, name_off_off, named_edges, zsec_v9)?; // The escape index, escape rows and name tables live behind the // record array in the same payload, raw in both v6 and v7 — only the // record array itself is blocked-compressed. So the table base is the@@ -286,28 +271,17 @@ impl Graph { ) + u64::from(read_at::<u16>(intermediate_within, geometry_edges as usize)) }; // Graph v9: the blob is a ZSEC section of the raw blob the trailer addresses.- let intermediate_zsec = if zsec_v9 {- let z = ZsecRegion::parse(unsafe { std::slice::from_raw_parts(intermediate_data, blob_bytes as usize) })?;- Some(z)- } else {- None- };+ let intermediate_zsec = ZsecRegion::v9_blob(intermediate_data, blob_bytes, zsec_v9)?; let raw_blob = intermediate_zsec.as_ref().map_or(blob_bytes, |z| z.raw_len() as u64); if sentinel != raw_blob || geometry.total(edge_count) != geometry_edges { return None; } - let (road_names, mut road_names_size) = match sections.road_names {+ let (road_names, road_names_size) = match sections.road_names { Some(b) => (b.as_ptr(), b.len()), None => (ptr::null(), 0), };- let road_names_zsec = if zsec_v9 && !road_names.is_null() && road_names_size > 0 {- let z = ZsecRegion::parse(unsafe { std::slice::from_raw_parts(road_names, road_names_size) })?;- road_names_size = z.raw_len();- Some(z)- } else {- None- };+ let (road_names_zsec, road_names_size) = ZsecRegion::v9_names(road_names, road_names_size, zsec_v9)?; // Lanes payload: [ u32 n ][ LaneEntry[n + 1] ][ u16 lane-mask blob ]. // The index is sized by its own header rather than by `edge_count`, so
maps/src/main/rust/src/graph_zsec.rs
diff --git a/maps/src/main/rust/src/graph_zsec.rs b/maps/src/main/rust/src/graph_zsec.rsindex 1622e2c296..c0468232a0 100644--- a/maps/src/main/rust/src/graph_zsec.rs+++ b/maps/src/main/rust/src/graph_zsec.rs@@ -37,6 +37,36 @@ thread_local! { } impl ZsecRegion {+ /// Graph v9's intermediate blob (`data[..blob_bytes]`) as a ZSEC region; `None` inside the+ /// `Some` for v6-v8 (raw blob). The outer `None` is a corrupt section.+ pub(crate) fn v9_blob(data: *const u8, blob_bytes: u64, v9: bool) -> Option<Option<ZsecRegion>> {+ if !v9 {+ return Some(None);+ }+ ZsecRegion::parse(unsafe { std::slice::from_raw_parts(data, blob_bytes as usize) }).map(Some)+ }++ /// Graph v9's road-name pool as a ZSEC region, with the raw pool length the name lookups bound+ /// against; v6-v8 (or no pool): `(None, len)`.+ pub(crate) fn v9_names(base: *const u8, len: usize, v9: bool) -> Option<(Option<ZsecRegion>, usize)> {+ if !v9 || base.is_null() || len == 0 {+ return Some((None, len));+ }+ let z = ZsecRegion::parse(unsafe { std::slice::from_raw_parts(base, len) })?;+ let raw = z.raw_len();+ Some((Some(z), raw))+ }++ /// `edges.bin`'s name-offset table (from `name_off_off` to the end of `edges`): a ZSEC section+ /// of `named * 4` raw bytes under v9, exactly `named * 4` raw bytes before it.+ pub(crate) fn v9_name_offsets(edges: &[u8], name_off_off: u64, named: u64, v9: bool) -> Option<Option<ZsecRegion>> {+ if v9 && named > 0 {+ let z = ZsecRegion::parse(edges.get(name_off_off as usize..)?)?;+ return (z.raw_len() as u64 == named * 4).then_some(Some(z));+ }+ (edges.len() as u64 == name_off_off + named * 4).then_some(None)+ }+ /// Validate `bytes` as a whole ZSEC section (header, codec, full offset table). pub(crate) fn parse(bytes: &[u8]) -> Option<ZsecRegion> { let z = tilecodec::zsec::ZSec::parse(bytes).ok()?;