use std::collections::HashMap;
use crate::error::{Error, Result};
use crate::io::BitReader;
use crate::limits::DecodeLimits;
const HISTORY_SIZE: usize = 32;
const HISTORY_BITMASK: usize = HISTORY_SIZE - 1;
const MAX_STRING_BITS: usize = 5;
const MAX_STRING_SIZE: usize = 1 << MAX_STRING_BITS;
const MAX_USERDATA_BITS: usize = 17;
const MAX_USERDATA_SIZE: usize = 1 << MAX_USERDATA_BITS;
pub const INSTANCE_BASELINE_TABLE_NAME: &str = "instancebaseline";
#[derive(Debug)]
#[non_exhaustive]
pub struct CreateStringTable {
pub name: String,
pub num_entries: i32,
pub user_data_fixed_size: bool,
pub user_data_size: i32,
pub user_data_size_bits: i32,
pub flags: i32,
pub string_data: Vec<u8>,
pub data_compressed: bool,
pub using_varint_bitcounts: bool,
}
impl CreateStringTable {
pub fn new(name: impl Into<String>, num_entries: i32, string_data: Vec<u8>) -> Self {
Self {
name: name.into(),
num_entries,
user_data_fixed_size: false,
user_data_size: 0,
user_data_size_bits: 0,
flags: 0,
string_data,
data_compressed: false,
using_varint_bitcounts: false,
}
}
#[must_use]
pub const fn with_fixed_user_data(mut self, size: i32, size_bits: i32) -> Self {
self.user_data_fixed_size = true;
self.user_data_size = size;
self.user_data_size_bits = size_bits;
self
}
#[must_use]
pub const fn with_flags(mut self, flags: i32) -> Self {
self.flags = flags;
self
}
#[must_use]
pub const fn with_compressed_data(mut self) -> Self {
self.data_compressed = true;
self
}
#[must_use]
pub const fn with_varint_bitcounts(mut self) -> Self {
self.using_varint_bitcounts = true;
self
}
}
#[derive(Debug)]
#[non_exhaustive]
pub struct UpdateStringTable {
pub table_id: i32,
pub num_changed_entries: i32,
pub string_data: Vec<u8>,
}
impl UpdateStringTable {
pub const fn new(table_id: i32, num_changed_entries: i32, string_data: Vec<u8>) -> Self {
Self {
table_id,
num_changed_entries,
string_data,
}
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct StringTableEntry {
pub string: Option<String>,
pub user_data: Option<Vec<u8>>,
}
impl StringTableEntry {
pub const fn new(string: Option<String>, user_data: Option<Vec<u8>>) -> Self {
Self { string, user_data }
}
}
#[derive(Debug, Clone)]
pub struct StringTable {
name: String,
user_data_fixed_size: bool,
user_data_size: i32,
user_data_size_bits: i32,
flags: i32,
using_varint_bitcounts: bool,
entries: Vec<StringTableEntry>,
dirty: Vec<usize>,
}
impl StringTable {
fn new(
name: &str,
user_data_fixed_size: bool,
user_data_size: i32,
user_data_size_bits: i32,
flags: i32,
using_varint_bitcounts: bool,
) -> Self {
Self {
name: name.to_string(),
user_data_fixed_size,
user_data_size,
user_data_size_bits,
flags,
using_varint_bitcounts,
entries: Vec::new(),
dirty: Vec::new(),
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn entries(&self) -> &[StringTableEntry] {
&self.entries
}
pub fn get(&self, index: usize) -> Option<&StringTableEntry> {
self.entries.get(index)
}
pub fn dirty_indices(&self) -> &[usize] {
&self.dirty
}
pub fn parse_update(&mut self, br: &mut BitReader, num_entries: i32) -> Result<()> {
self.parse_update_with_limits(br, num_entries, &DecodeLimits::default())
}
pub fn parse_update_with_limits(
&mut self,
br: &mut BitReader,
num_entries: i32,
limits: &DecodeLimits,
) -> Result<()> {
let entry_count = usize::try_from(num_entries).map_err(|_| Error::Parse {
context: format!("negative string-table entry count {num_entries}"),
})?;
limits.ensure(
"string table entries",
entry_count,
limits.max_string_table_entries(),
)?;
let mut entry_index: i64 = -1;
let mut history: Vec<[u8; MAX_STRING_SIZE]> = vec![[0u8; MAX_STRING_SIZE]; HISTORY_SIZE];
let mut history_delta_index: usize = 0;
let mut string_buf = vec![0u8; 1024];
let mut user_data_buf = vec![0u8; MAX_USERDATA_SIZE];
let mut user_data_uncompressed_buf = Vec::new();
for _ in 0..entry_count {
entry_index = if br.read_bool()? {
entry_index + 1
} else {
i64::from(br.read_uvarint32()?) + 1
};
let idx = usize::try_from(entry_index).map_err(|_| Error::Parse {
context: format!("negative string-table entry index {entry_index}"),
})?;
limits.ensure(
"string table entry index",
idx.saturating_add(1),
limits.max_string_table_entries(),
)?;
let has_string = br.read_bool()?;
let string = if has_string {
let mut size: usize = 0;
if br.read_bool()? {
let mut history_delta_zero = 0;
if history_delta_index > HISTORY_SIZE {
history_delta_zero = history_delta_index & HISTORY_BITMASK;
}
let index = (history_delta_zero + br.read_bits(5)? as usize) & HISTORY_BITMASK;
let bytes_to_copy = br.read_bits(MAX_STRING_BITS)? as usize;
size += bytes_to_copy;
string_buf[..bytes_to_copy].copy_from_slice(&history[index][..bytes_to_copy]);
size += br.read_string_into(&mut string_buf[bytes_to_copy..])?;
} else {
size += br.read_string_into(&mut string_buf)?;
}
let mut she = [0u8; MAX_STRING_SIZE];
let copy_len = size.min(MAX_STRING_SIZE);
she[..copy_len].copy_from_slice(&string_buf[..copy_len]);
history[history_delta_index & HISTORY_BITMASK] = she;
history_delta_index += 1;
Some(String::from_utf8_lossy(&string_buf[..size]).into_owned())
} else {
None
};
let has_user_data = br.read_bool()?;
let user_data = if has_user_data {
if self.user_data_fixed_size {
let size = usize::try_from(self.user_data_size).unwrap_or(usize::MAX);
let size_bits = usize::try_from(self.user_data_size_bits).unwrap_or(usize::MAX);
limits.ensure(
"string-table fixed user data",
size,
limits.max_string_table_user_data_bytes(),
)?;
limits.ensure(
"string-table fixed user data bits",
size_bits.div_ceil(8),
limits.max_string_table_user_data_bytes(),
)?;
if size > user_data_buf.len() || size_bits.div_ceil(8) > user_data_buf.len() {
return Err(Error::Parse {
context: format!(
"string-table fixed user data ({size} bytes / {size_bits} bits) exceeds {} bytes",
user_data_buf.len()
),
});
}
br.read_bits_to_bytes(&mut user_data_buf, size_bits)?;
Some(user_data_buf[..size].to_vec())
} else {
let mut is_compressed = false;
if (self.flags & 0x1) != 0 {
is_compressed = br.read_bool()?;
}
let size = if self.using_varint_bitcounts {
br.read_ubitvar()? as usize
} else {
br.read_bits(MAX_USERDATA_BITS)? as usize
};
limits.ensure(
"string-table user data",
size,
limits.max_string_table_user_data_bytes(),
)?;
if size > user_data_buf.len() {
return Err(Error::Parse {
context: format!(
"string-table user data size {size} exceeds {} bytes",
user_data_buf.len()
),
});
}
br.read_bytes(&mut user_data_buf[..size])?;
if is_compressed {
let decomp_len = snap::raw::decompress_len(&user_data_buf[..size])
.map_err(|e| Error::Decompress(e.to_string()))?;
limits.ensure(
"decompressed string-table user data",
decomp_len,
limits.max_string_table_user_data_bytes(),
)?;
user_data_uncompressed_buf.clear();
user_data_uncompressed_buf
.try_reserve(decomp_len)
.map_err(|_| Error::Allocation {
resource: "decompressed string-table user data",
requested: decomp_len,
})?;
user_data_uncompressed_buf.resize(decomp_len, 0);
snap::raw::Decoder::new()
.decompress(&user_data_buf[..size], &mut user_data_uncompressed_buf)
.map_err(|e| Error::Decompress(e.to_string()))?;
Some(user_data_uncompressed_buf[..decomp_len].to_vec())
} else {
Some(user_data_buf[..size].to_vec())
}
}
} else {
None
};
if idx < self.entries.len() {
if let Some(ud) = user_data {
self.entries[idx].user_data = Some(ud);
}
if let Some(s) = string {
self.entries[idx].string = Some(s);
}
} else {
while self.entries.len() < idx {
self.entries.push(StringTableEntry {
string: None,
user_data: None,
});
}
self.entries.push(StringTableEntry { string, user_data });
}
self.dirty.push(idx);
}
Ok(())
}
}
#[derive(Clone, Default)]
pub struct StringTableContainer {
tables: Vec<StringTable>,
instance_baselines: HashMap<i32, Vec<u8>>,
}
impl StringTableContainer {
pub fn new() -> Self {
Self::default()
}
pub fn handle_create(&mut self, msg: CreateStringTable) -> Result<bool> {
self.handle_create_with_limits(msg, &DecodeLimits::default())
}
pub fn handle_create_with_limits(
&mut self,
msg: CreateStringTable,
limits: &DecodeLimits,
) -> Result<bool> {
limits.ensure(
"string-table data",
msg.string_data.len(),
limits.max_string_table_data_bytes(),
)?;
let is_baseline = msg.name == INSTANCE_BASELINE_TABLE_NAME;
let mut table = StringTable::new(
&msg.name,
msg.user_data_fixed_size,
msg.user_data_size,
msg.user_data_size_bits,
msg.flags,
msg.using_varint_bitcounts,
);
let string_data = if msg.data_compressed {
let decompressed_len = snap::raw::decompress_len(&msg.string_data)
.map_err(|error| Error::Decompress(error.to_string()))?;
limits.ensure(
"decompressed string-table data",
decompressed_len,
limits.max_decompressed_string_table_bytes(),
)?;
let mut output = Vec::new();
output
.try_reserve(decompressed_len)
.map_err(|_| Error::Allocation {
resource: "decompressed string-table data",
requested: decompressed_len,
})?;
output.resize(decompressed_len, 0);
snap::raw::Decoder::new()
.decompress(&msg.string_data, &mut output)
.map_err(|error| Error::Decompress(error.to_string()))?;
output
} else {
msg.string_data
};
table.parse_update_with_limits(
&mut BitReader::new(&string_data),
msg.num_entries,
limits,
)?;
self.tables.push(table);
Ok(is_baseline)
}
pub fn handle_update(&mut self, msg: UpdateStringTable) -> Result<bool> {
self.handle_update_with_limits(msg, &DecodeLimits::default())
}
pub fn handle_update_with_limits(
&mut self,
msg: UpdateStringTable,
limits: &DecodeLimits,
) -> Result<bool> {
limits.ensure(
"string-table data",
msg.string_data.len(),
limits.max_string_table_data_bytes(),
)?;
let table_id = usize::try_from(msg.table_id).map_err(|_| Error::Parse {
context: format!("invalid negative string table ID {}", msg.table_id),
})?;
let table = self.tables.get_mut(table_id).ok_or_else(|| Error::Parse {
context: format!("string table update for non-existent table {table_id}"),
})?;
let is_baseline = table.name == INSTANCE_BASELINE_TABLE_NAME;
table.parse_update_with_limits(
&mut BitReader::new(&msg.string_data),
msg.num_changed_entries,
limits,
)?;
Ok(is_baseline)
}
pub fn do_full_update(
&mut self,
incoming_tables: impl IntoIterator<Item = (String, Vec<StringTableEntry>)>,
) -> Result<()> {
self.do_full_update_with_limits(incoming_tables, &DecodeLimits::default())
}
pub fn do_full_update_with_limits(
&mut self,
incoming_tables: impl IntoIterator<Item = (String, Vec<StringTableEntry>)>,
limits: &DecodeLimits,
) -> Result<()> {
for (table_name, items) in incoming_tables {
limits.ensure(
"full string-table entries",
items.len(),
limits.max_string_table_entries(),
)?;
let Some(table) = self
.tables
.iter_mut()
.find(|table| table.name == table_name)
else {
continue;
};
for (index, entry) in items.into_iter().enumerate() {
if index < table.entries.len() {
if entry.user_data.is_some() {
table.entries[index].user_data = entry.user_data;
table.dirty.push(index);
}
} else {
table.entries.resize_with(index, || StringTableEntry {
string: None,
user_data: None,
});
table.entries.push(entry);
table.dirty.push(index);
}
}
}
Ok(())
}
pub fn clear_dirty(&mut self) {
for table in &mut self.tables {
table.dirty.clear();
}
}
pub fn update_instance_baselines(&mut self) {
if let Some(table) = self
.tables
.iter()
.find(|t| t.name == INSTANCE_BASELINE_TABLE_NAME)
{
for entry in &table.entries {
if let (Some(s), Some(data)) = (&entry.string, &entry.user_data)
&& let Ok(class_id) = s.parse::<i32>()
{
if self.instance_baselines.get(&class_id) != Some(data) {
self.instance_baselines.insert(class_id, data.clone());
}
}
}
}
}
pub fn instance_baseline(&self, class_id: i32) -> Option<&[u8]> {
self.instance_baselines.get(&class_id).map(Vec::as_slice)
}
pub fn find_table(&self, name: &str) -> Option<&StringTable> {
self.tables.iter().find(|t| t.name == name)
}
pub fn tables(&self) -> &[StringTable] {
&self.tables
}
}
#[cfg(test)]
mod coverage_tests;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn container_starts_empty() {
let container = StringTableContainer::new();
assert!(container.tables().is_empty());
assert!(container.instance_baseline(0).is_none());
}
#[test]
fn rejects_unknown_table_update() {
let mut container = StringTableContainer::new();
let result = container.handle_update(UpdateStringTable {
table_id: 99,
num_changed_entries: 0,
string_data: Vec::new(),
});
assert!(result.is_err());
}
#[test]
fn creates_an_empty_table() {
let mut container = StringTableContainer::new();
container
.handle_create(CreateStringTable {
name: "test".to_owned(),
num_entries: 0,
user_data_fixed_size: false,
user_data_size: 0,
user_data_size_bits: 0,
flags: 0,
string_data: Vec::new(),
data_compressed: false,
using_varint_bitcounts: false,
})
.unwrap();
assert!(container.find_table("test").is_some());
}
}