Skip to content

Commit

Permalink
Merge pull request #109 from occanowey/master
Browse files Browse the repository at this point in the history
Add support for "network NBT"
  • Loading branch information
owengage authored Apr 9, 2024
2 parents 47c5fe5 + ba62c0f commit 2709cbe
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion fastnbt/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,11 @@ where
let peek = self.input.consume_tag()?;

match peek {
Tag::Compound => self.input.ignore_str()?,
Tag::Compound => {
if self.opts.expect_coumpound_names {
self.input.ignore_str()?
}
}
_ => return Err(Error::no_root_compound()),
}

Expand Down
15 changes: 15 additions & 0 deletions fastnbt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ where
pub struct DeOpts {
/// Maximum number of bytes a list or array can be.
max_seq_len: usize,
/// Whether compound tag names are expected to exist or not.
expect_coumpound_names: bool,
}

impl DeOpts {
Expand All @@ -362,19 +364,32 @@ impl DeOpts {
Default::default()
}

/// Creates a decoder for "network NBT" mode.
/// At the moment that consists compound tags not having names (or their length).
pub fn network_nbt() -> Self {
Self::new().expect_coumpound_names(false)
}

/// Set the maximum length any given sequence can be, eg lists. This does
/// not apply to NBT array types. This can help prevent panics on malformed
/// data.
pub fn max_seq_len(mut self, value: usize) -> Self {
self.max_seq_len = value;
self
}

/// Sets wheather the deserializer should expect compound tags to have names.
pub fn expect_coumpound_names(mut self, value: bool) -> Self {
self.expect_coumpound_names = value;
self
}
}

impl Default for DeOpts {
fn default() -> Self {
Self {
max_seq_len: 10_000_000, // arbitrary high limit.
expect_coumpound_names: true,
}
}
}
Expand Down

0 comments on commit 2709cbe

Please sign in to comment.