Skip to content

Commit

Permalink
Minior bytebuf improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Sep 16, 2024
1 parent 0da078a commit 2a9eff4
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions pumpkin-protocol/src/bytebuf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,19 @@ impl ByteBuffer {
}

pub fn get_string(&mut self) -> Result<String, DeserializerError> {
self.get_string_len(32767)
self.get_string_len(i16::MAX as i32)
}

pub fn get_string_len(&mut self, max_size: usize) -> Result<String, DeserializerError> {
pub fn get_string_len(&mut self, max_size: i32) -> Result<String, DeserializerError> {
let size = self.get_var_int()?.0;
if size as usize > max_size {
if size > max_size {
return Err(DeserializerError::Message(
"String length is bigger than max size".to_string(),
));
}

let data = self.copy_to_bytes(size as usize)?;
if data.len() > max_size {
if data.len() as i32 > max_size {
return Err(DeserializerError::Message(
"String is bigger than max size".to_string(),
));
Expand Down Expand Up @@ -125,6 +125,14 @@ impl ByteBuffer {
}

pub fn put_string(&mut self, val: &str) {
self.put_string_len(val, i16::MAX as i32);
}

pub fn put_string_len(&mut self, val: &str, max_size: i32) {
if val.len() as i32 > max_size {
// Should be panic?, I mean its our fault
panic!("String is too big");
}
self.put_var_int(&val.len().into());
self.buffer.put(val.as_bytes());
}
Expand Down

0 comments on commit 2a9eff4

Please sign in to comment.