fn encode_entry(tile: u64, layer_id: u8, entry: &ChunkEntry, out: &mut Vec) { let layer = &entry.layer; let base = out.len(); out.resize(base + ENTRY_HEADER_BYTES, 0); out[base..base + 8].copy_from_slice(&tile.to_le_bytes()); out[base + 8..base + 12].copy_from_slice(&(layer.features.len() as u32).to_le_bytes()); out[base + 12..base + 16].copy_from_slice(&(layer.parts.len() as u32).to_le_bytes()); out[base + 16..base + 20].copy_from_slice(&(layer.coords.len() as u32).to_le_bytes()); out[base + 20] = layer_id; // Byte 21 flags a turn-lane section after the names; byte 22 a building section after that; // byte 23 a carriageway section after that. The building section is `features` records (one per // feature, dense), each a BUILDING_BYTES packed [`BuildingAttrs`]. Present only for a // `buildings` tile that had S3DB attributes in it. // // The carriageway section is the same shape for the `roads` layer. It has to be here rather // than reconstructed later: a side table that never reaches the spill is dropped on every // build that spills a zoom, and the archive comes back with no lane markings and no error. let has_turns = !entry.turn_lanes.is_empty(); let has_buildings = !entry.buildings.is_empty(); let has_carriageways = !entry.carriageways.is_empty(); out[base + 21] = has_turns as u8; out[base + 22] = has_buildings as u8; out[base + 23] = has_carriageways as u8; out[base + 24..base + 28].copy_from_slice(&(entry.names.len() as u32).to_le_bytes()); out[base + 28..base + 32].copy_from_slice(&(entry.ids.len() as u32).to_le_bytes()); for feature in &layer.features { // The spill carries the v2 index, not the v1 wire: name_idx, transit_color and the three // transit lane bytes ride the entry and are re-encoded by the body serializer, so the // scratch format never lags the codec by a version. out.extend_from_slice(&feature.kind.to_le_bytes()); out.extend_from_slice(&feature.kind_detail.to_le_bytes()); out.push(feature.geom_type); out.push(feature.flags); out.extend_from_slice(&feature.name_idx.to_le_bytes()); out.extend_from_slice(&feature.parts_offset.to_le_bytes()); out.extend_from_slice(&feature.part_count.to_le_bytes()); out.extend_from_slice(&feature.transit_color.to_le_bytes()); out.push(feature.transit_ordinal); out.push(feature.transit_lanes); out.push(feature.transit_taper); out.push(feature.lane_count); } for part in &layer.parts { out.extend_from_slice(&part.coord_start.to_le_bytes()); out.extend_from_slice(&part.point_count.to_le_bytes()); out.extend_from_slice(&part.winding.to_le_bytes()); } for (x, y) in &layer.coords { out.extend_from_slice(&x.to_le_bytes()); out.extend_from_slice(&y.to_le_bytes()); } for id in &entry.ids { out.extend_from_slice(&id.to_le_bytes()); } for name in &entry.names { out.extend_from_slice(&(name.len() as u32).to_le_bytes()); out.extend_from_slice(name.as_bytes()); } // The turn-lane section, after the names, when present. One record per feature, dense. if has_turns { for turns in &entry.turn_lanes { out.push(turns.forward.len() as u8); out.push(turns.backward.len() as u8); for m in turns.forward.iter().chain(&turns.backward) { out.extend_from_slice(&m.to_le_bytes()); } } } // The building section, after the turn-lane section, when present. One record per feature. if has_buildings { for a in &entry.buildings { out.extend_from_slice(&a.height.to_le_bytes()); out.extend_from_slice(&a.min_height.to_le_bytes()); out.extend_from_slice(&a.roof_height.to_le_bytes()); out.push(a.roof_shape); out.push(a.roof_direction); out.push(a.roof_orientation); out.extend_from_slice(&a.building_colour.to_le_bytes()); out.extend_from_slice(&a.roof_colour.to_le_bytes()); } } // The carriageway section, last, when present. One record per feature, in the same field order // the body's own [`CARRIAGEWAY_RECORD_LEN`] record uses. if has_carriageways { for c in &entry.carriageways { out.push(c.forward); out.push(c.backward); out.extend_from_slice(&c.solid_dividers.to_le_bytes()); } } } struct EntryHeader { tile: u64, layer_id: u8, features: usize, parts: usize, coords: usize, names: usize, ids: usize, has_turns: bool, has_buildings: bool, has_carriageways: bool, } fn entry_header(head: &[u8; ENTRY_HEADER_BYTES]) -> Result { // Bytes 21 (turn-lane flag), 22 (building flag) and 23 (carriageway flag) are 0 or 1; anything // else means the reader is the wrong version for the file, because a newer writer would be // using the spare values. Guessing would decode a field that moved. if head[21] > 1 { return err("a tile chunk entry has an unknown turn-lane flag"); } if head[22] > 1 { return err("a tile chunk entry has an unknown building flag"); } if head[23] > 1 { return err("a tile chunk entry has an unknown carriageway flag"); } let u32_at = |o: usize| u32::from_le_bytes(head[o..o + 4].try_into().expect("4 bytes")) as usize; let header = EntryHeader { tile: u64::from_le_bytes(head[0..8].try_into().expect("8 bytes")), features: u32_at(8), parts: u32_at(12), coords: u32_at(16), layer_id: head[20], names: u32_at(24), ids: u32_at(28), has_turns: head[21] == 1, has_buildings: head[22] == 1, has_carriageways: head[23] == 1, }; // A layer either has an id per feature or none at all. Checked before the length so a garbled // count is refused as the desync it is rather than as a size that happens not to fit. if header.ids != 0 && header.ids != header.features { return err(format!( "a tile chunk entry has {} id(s) for {} feature(s)", header.ids, header.features, )); } let len = ENTRY_HEADER_BYTES as u64 + payload_bytes(header.features, header.parts, header.coords, header.ids); if len > MAX_ENTRY_BYTES { return err(format!("a tile chunk entry is {len} byte(s), which is corruption")); } Ok(header) } /// Decode an entry's fixed arenas. `payload` is exactly the fixed bytes the header accounted /// for; names are pulled separately by the reader, one inline length at a time. /// /// `spare` is a previously-yielded entry the merge handed back, its arenas emptied but their /// capacity kept. Reusing it turns four `Vec` allocations per entry into four `clear`/`reserve` /// pairs on the serial merge thread. The decoded values are identical either way: every arena is /// cleared before it is refilled, so nothing of the old entry survives. fn decode_fixed( header: &EntryHeader, payload: &[u8], spare: Option, ) -> Result { let u16_at = |b: &[u8], o: usize| u16::from_le_bytes(b[o..o + 2].try_into().expect("2 bytes")); let u32_at = |b: &[u8], o: usize| u32::from_le_bytes(b[o..o + 4].try_into().expect("4 bytes")); let i16_at = |b: &[u8], o: usize| i16::from_le_bytes(b[o..o + 2].try_into().expect("2 bytes")); let mut entry = spare.unwrap_or_else(|| ChunkEntry::new(header.layer_id)); entry.layer.layer_id = header.layer_id; entry.layer.features.clear(); entry.layer.features.reserve(header.features); entry.layer.parts.clear(); entry.layer.parts.reserve(header.parts); entry.layer.coords.clear(); entry.layer.coords.reserve(header.coords); entry.ids.clear(); entry.ids.reserve(header.ids); // The reader fills these; clear so a reused entry starts empty. Names are reserved here (the // count is in the header); the section arenas are reserved by the reader when their flag is set. entry.names.clear(); entry.names.reserve(header.names); entry.turn_lanes.clear(); entry.buildings.clear(); entry.carriageways.clear(); let mut at = 0usize; for _ in 0..header.features { let b = payload .get(at..at + FEATURE_BYTES) .ok_or_else(|| Error("a tile chunk entry's features run past its payload".to_string()))?; entry.layer.features.push(BodyFeature { kind: u16_at(b, 0), kind_detail: u16_at(b, 2), geom_type: b[4], flags: b[5], name_idx: u16_at(b, 6), parts_offset: u32_at(b, 8), part_count: u32_at(b, 12), transit_color: u32_at(b, 16), transit_ordinal: b[20], transit_lanes: b[21], transit_taper: b[22], lane_count: b[23], }); at += FEATURE_BYTES; } for _ in 0..header.parts { let b = payload .get(at..at + PART_BYTES) .ok_or_else(|| Error("a tile chunk entry's parts run past its payload".to_string()))?; entry.layer.parts.push(Part { coord_start: u32_at(b, 0), point_count: u32_at(b, 4), winding: u16_at(b, 8), }); at += PART_BYTES; } for _ in 0..header.coords { let b = payload .get(at..at + COORD_BYTES) .ok_or_else(|| Error("a tile chunk entry's coords run past its payload".to_string()))?; entry.layer.coords.push((i16_at(b, 0), i16_at(b, 2))); at += COORD_BYTES; } for _ in 0..header.ids { let b = payload .get(at..at + ID_BYTES) .ok_or_else(|| Error("a tile chunk entry's ids run past its payload".to_string()))?; entry.ids.push(u64::from_le_bytes(b.try_into().expect("8 bytes"))); at += ID_BYTES; } debug_assert_eq!(at, payload.len(), "the fixed arenas must consume their payload exactly"); Ok(entry) }