Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / networklocation / geodb_build / src / write_s1.rs
write_s1.rs126 lines · 4.9 KiBRaw
| 1 | /// Decode a whole blocked dictionary (see `codec::encode_dict`), checking that every block |
| 2 | /// holds the expected number of strings and that the uncompressed first-string table agrees |
| 3 | /// with what each block actually starts with, since the reader trusts that table to search. |
| 4 | pub(crate) fn decode_dict(body: &[u8]) -> io::Result<Vec<String>> { |
| 5 | let bad = |m: &str| io::Error::new(io::ErrorKind::InvalidData, m.to_string()); |
| 6 | let rd32 = |p: usize| -> io::Result<usize> { |
| 7 | let b = body.get(p..p + 4).ok_or_else(|| bad("dictionary section truncated"))?; |
| 8 | Ok(u32::from_be_bytes(b.try_into().map_err(|_| bad("short dict"))?) as usize) |
| 9 | }; |
| 10 | let (count, per, blocks) = (rd32(0)?, rd32(4)?, rd32(8)?); |
| 11 | if per == 0 || blocks != count.div_ceil(per) { |
| 12 | return Err(bad("dictionary block count does not match its string count")); |
| 13 | } |
| 14 | let first_dir = 12; |
| 15 | let firsts = first_dir + blocks * 4; |
| 16 | let firsts_len = if blocks == 0 { 0 } else { rd32(firsts - 4)? }; |
| 17 | let block_dir = firsts + firsts_len; |
| 18 | let data = block_dir + blocks * 4; |
| 19 | |
| 20 | let mut out = Vec::with_capacity(count); |
| 21 | for b in 0..blocks { |
| 22 | let start = if b == 0 { 0 } else { rd32(block_dir + (b - 1) * 4)? }; |
| 23 | let end = rd32(block_dir + b * 4)?; |
| 24 | let comp = |
| 25 | body.get(data + start..data + end).ok_or_else(|| bad("dictionary block truncated"))?; |
| 26 | let raw = zstd::decode_all(comp)?; |
| 27 | let want = per.min(count - b * per); |
| 28 | let mut p = 0usize; |
| 29 | for k in 0..want { |
| 30 | let len = raw.get(p..p + 4).ok_or_else(|| bad("dictionary block ran out"))?; |
| 31 | let len = u32::from_be_bytes(len.try_into().map_err(|_| bad("short dict"))?) as usize; |
| 32 | p += 4; |
| 33 | let s = raw.get(p..p + len).ok_or_else(|| bad("dictionary block ran out"))?; |
| 34 | let s = |
| 35 | String::from_utf8(s.to_vec()).map_err(|_| bad("dictionary entry is not UTF-8"))?; |
| 36 | p += len; |
| 37 | if k == 0 { |
| 38 | let f0 = if b == 0 { 0 } else { rd32(first_dir + (b - 1) * 4)? }; |
| 39 | let f1 = rd32(first_dir + b * 4)?; |
| 40 | if body.get(firsts + f0..firsts + f1) != Some(s.as_bytes()) { |
| 41 | return Err(bad("first-string table disagrees with its block")); |
| 42 | } |
| 43 | } |
| 44 | out.push(s); |
| 45 | } |
| 46 | if p != raw.len() { |
| 47 | return Err(bad("dictionary block has trailing bytes")); |
| 48 | } |
| 49 | } |
| 50 | if data + if blocks == 0 { 0 } else { rd32(data - 4)? } != body.len() { |
| 51 | return Err(bad("dictionary section has trailing bytes")); |
| 52 | } |
| 53 | Ok(out) |
| 54 | } |
| 55 | |
| 56 | pub(crate) fn decode_column(body: &[u8], delta: bool) -> io::Result<Vec<i32>> { |
| 57 | decode_column_with(body, delta, None) |
| 58 | } |
| 59 | |
| 60 | fn decode_column_with( |
| 61 | body: &[u8], |
| 62 | delta: bool, |
| 63 | progress: Option<&Progress>, |
| 64 | ) -> io::Result<Vec<i32>> { |
| 65 | let bad = |m: &str| io::Error::new(io::ErrorKind::InvalidData, m.to_string()); |
| 66 | let rd32 = |
| 67 | |p: usize| -> u32 { u32::from_be_bytes([body[p], body[p + 1], body[p + 2], body[p + 3]]) }; |
| 68 | if body.len() < 8 { |
| 69 | return Err(bad("column section too short")); |
| 70 | } |
| 71 | let n = rd32(0) as usize; |
| 72 | let blocks = rd32(4) as usize; |
| 73 | let data = 8 + blocks * 8; |
| 74 | let mut out = Vec::with_capacity(n); |
| 75 | let mut start = 0usize; |
| 76 | for b in 0..blocks { |
| 77 | let end = rd32(8 + b * 8 + 4) as usize; |
| 78 | let comp = |
| 79 | body.get(data + start..data + end).ok_or_else(|| bad("column block out of range"))?; |
| 80 | let raw = zstd::decode_all(comp)?; |
| 81 | start = end; |
| 82 | let mut p = 0usize; |
| 83 | let mut prev = 0i32; |
| 84 | let count = n.saturating_sub(b * BLOCK).min(BLOCK); |
| 85 | for _ in 0..count { |
| 86 | let mut v = 0u32; |
| 87 | let mut shift = 0; |
| 88 | loop { |
| 89 | let byte = *raw.get(p).ok_or_else(|| bad("block ran out of bytes"))?; |
| 90 | p += 1; |
| 91 | v |= ((byte & 0x7F) as u32) << shift; |
| 92 | if byte & 0x80 == 0 { |
| 93 | break; |
| 94 | } |
| 95 | shift += 7; |
| 96 | } |
| 97 | let dec = ((v >> 1) as i32) ^ -((v & 1) as i32); |
| 98 | let value = if delta { prev.wrapping_add(dec) } else { dec }; |
| 99 | out.push(value); |
| 100 | prev = value; |
| 101 | } |
| 102 | if let Some(p) = progress { |
| 103 | p.inc(); |
| 104 | } |
| 105 | } |
| 106 | Ok(out) |
| 107 | } |
| 108 | |
| 109 | fn decode_grid(body: &[u8]) -> io::Result<Vec<(i64, u32)>> { |
| 110 | let count = |
| 111 | u32::from_be_bytes(body[0..4].try_into().map_err(|_| io::Error::other("short grid"))?) |
| 112 | as usize; |
| 113 | let mut out = Vec::with_capacity(count); |
| 114 | let mut p = 4usize; |
| 115 | for _ in 0..count { |
| 116 | let cell = i64::from_be_bytes( |
| 117 | body[p..p + 8].try_into().map_err(|_| io::Error::other("short grid"))?, |
| 118 | ); |
| 119 | let start = u32::from_be_bytes( |
| 120 | body[p + 8..p + 12].try_into().map_err(|_| io::Error::other("short grid"))?, |
| 121 | ); |
| 122 | out.push((cell, start)); |
| 123 | p += 12; |
| 124 | } |
| 125 | Ok(out) |
| 126 | } |