/// Decode a whole blocked dictionary (see `codec::encode_dict`), checking that every block /// holds the expected number of strings and that the uncompressed first-string table agrees /// with what each block actually starts with, since the reader trusts that table to search. pub(crate) fn decode_dict(body: &[u8]) -> io::Result> { let bad = |m: &str| io::Error::new(io::ErrorKind::InvalidData, m.to_string()); let rd32 = |p: usize| -> io::Result { let b = body.get(p..p + 4).ok_or_else(|| bad("dictionary section truncated"))?; Ok(u32::from_be_bytes(b.try_into().map_err(|_| bad("short dict"))?) as usize) }; let (count, per, blocks) = (rd32(0)?, rd32(4)?, rd32(8)?); if per == 0 || blocks != count.div_ceil(per) { return Err(bad("dictionary block count does not match its string count")); } let first_dir = 12; let firsts = first_dir + blocks * 4; let firsts_len = if blocks == 0 { 0 } else { rd32(firsts - 4)? }; let block_dir = firsts + firsts_len; let data = block_dir + blocks * 4; let mut out = Vec::with_capacity(count); for b in 0..blocks { let start = if b == 0 { 0 } else { rd32(block_dir + (b - 1) * 4)? }; let end = rd32(block_dir + b * 4)?; let comp = body.get(data + start..data + end).ok_or_else(|| bad("dictionary block truncated"))?; let raw = zstd::decode_all(comp)?; let want = per.min(count - b * per); let mut p = 0usize; for k in 0..want { let len = raw.get(p..p + 4).ok_or_else(|| bad("dictionary block ran out"))?; let len = u32::from_be_bytes(len.try_into().map_err(|_| bad("short dict"))?) as usize; p += 4; let s = raw.get(p..p + len).ok_or_else(|| bad("dictionary block ran out"))?; let s = String::from_utf8(s.to_vec()).map_err(|_| bad("dictionary entry is not UTF-8"))?; p += len; if k == 0 { let f0 = if b == 0 { 0 } else { rd32(first_dir + (b - 1) * 4)? }; let f1 = rd32(first_dir + b * 4)?; if body.get(firsts + f0..firsts + f1) != Some(s.as_bytes()) { return Err(bad("first-string table disagrees with its block")); } } out.push(s); } if p != raw.len() { return Err(bad("dictionary block has trailing bytes")); } } if data + if blocks == 0 { 0 } else { rd32(data - 4)? } != body.len() { return Err(bad("dictionary section has trailing bytes")); } Ok(out) } pub(crate) fn decode_column(body: &[u8], delta: bool) -> io::Result> { decode_column_with(body, delta, None) } fn decode_column_with( body: &[u8], delta: bool, progress: Option<&Progress>, ) -> io::Result> { let bad = |m: &str| io::Error::new(io::ErrorKind::InvalidData, m.to_string()); let rd32 = |p: usize| -> u32 { u32::from_be_bytes([body[p], body[p + 1], body[p + 2], body[p + 3]]) }; if body.len() < 8 { return Err(bad("column section too short")); } let n = rd32(0) as usize; let blocks = rd32(4) as usize; let data = 8 + blocks * 8; let mut out = Vec::with_capacity(n); let mut start = 0usize; for b in 0..blocks { let end = rd32(8 + b * 8 + 4) as usize; let comp = body.get(data + start..data + end).ok_or_else(|| bad("column block out of range"))?; let raw = zstd::decode_all(comp)?; start = end; let mut p = 0usize; let mut prev = 0i32; let count = n.saturating_sub(b * BLOCK).min(BLOCK); for _ in 0..count { let mut v = 0u32; let mut shift = 0; loop { let byte = *raw.get(p).ok_or_else(|| bad("block ran out of bytes"))?; p += 1; v |= ((byte & 0x7F) as u32) << shift; if byte & 0x80 == 0 { break; } shift += 7; } let dec = ((v >> 1) as i32) ^ -((v & 1) as i32); let value = if delta { prev.wrapping_add(dec) } else { dec }; out.push(value); prev = value; } if let Some(p) = progress { p.inc(); } } Ok(out) } fn decode_grid(body: &[u8]) -> io::Result> { let count = u32::from_be_bytes(body[0..4].try_into().map_err(|_| io::Error::other("short grid"))?) as usize; let mut out = Vec::with_capacity(count); let mut p = 4usize; for _ in 0..count { let cell = i64::from_be_bytes( body[p..p + 8].try_into().map_err(|_| io::Error::other("short grid"))?, ); let start = u32::from_be_bytes( body[p + 8..p + 12].try_into().map_err(|_| io::Error::other("short grid"))?, ); out.push((cell, start)); p += 12; } Ok(out) }