Skip to content

Commit

Permalink
Fixed another string-sizing bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
craftycodie committed Nov 12, 2024
1 parent ac682c7 commit 03adc8f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl SerializableBlfChunk for s_blf_chunk_hopper_configuration_table {
let category = &self.hopper_categories[i];
bitstream.write_integer(category.category_identifier as u32, 16);
bitstream.write_integer(category.category_image_index as u32, 6);
bitstream.write_string_utf8(&category.category_name.get_string(), 32);
bitstream.write_string_utf8(&category.category_name.get_string(), 16);
}

// Encode hopper_configuration_count
Expand All @@ -181,7 +181,7 @@ impl SerializableBlfChunk for s_blf_chunk_hopper_configuration_table {
// Encode each hopper configuration
for i in 0..self.hopper_configuration_count as usize {
let configuration = &self.hopper_configurations[i];
bitstream.write_string_utf8(&configuration.hopper_name.get_string(), 32);
bitstream.write_string_utf8(&configuration.hopper_name.get_string(), 16);
bitstream.write_raw_data(&configuration.game_set_hash.data, 0xA0);
bitstream.write_integer(configuration.hopper_identifier as u32, 16);
bitstream.write_integer(configuration.hopper_category as u32, 16);
Expand Down Expand Up @@ -302,15 +302,15 @@ impl SerializableBlfChunk for s_blf_chunk_hopper_configuration_table {
let category = &mut self.hopper_categories[i];
category.category_identifier = bitstream.read_u16(16);
category.category_image_index = bitstream.read_u8(6);
category.category_name.set_string(&bitstream.read_string_utf8(32)).unwrap();
category.category_name.set_string(&bitstream.read_string_utf8(16)).unwrap();
}

self.hopper_configuration_count = bitstream.read_u8(6);
self.hopper_configurations.resize(self.hopper_configuration_count as usize, c_hopper_configuration::default());

for i in 0..self.hopper_configuration_count as usize {
let configuration = &mut self.hopper_configurations[i];
configuration.hopper_name.set_string(&bitstream.read_string_utf8(32)).unwrap();
configuration.hopper_name.set_string(&bitstream.read_string_utf8(16)).unwrap();
configuration.game_set_hash = s_network_http_request_hash::try_from(bitstream.read_raw_data(0xA0)).unwrap();
configuration.hopper_identifier = bitstream.read_u16(16);
configuration.hopper_category = bitstream.read_u16(16);
Expand Down
11 changes: 8 additions & 3 deletions blf_lib/src/types/c_string.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ffi::{c_char, CStr};
use std::ffi::c_char;
use std::fmt::Write;
use blf_lib::types::array::StaticArray;
use serde::{Deserializer, Serialize, Serializer};
Expand Down Expand Up @@ -141,7 +141,11 @@ impl<const N: usize> StaticString<N> {
}

pub fn set_string(&mut self, value: &String) -> Result<(), String> {
let bytes = value.as_bytes();
let mut bytes = value.as_bytes();
// if a null termination was provided at the end, chop it off
if bytes.len() > 0 && bytes[bytes.len() - 1] == 0 {
bytes = &bytes[0..bytes.len() - 1];
}
if bytes.len() > N {
return Err(format!("String \"{value}\" too long ({} > {}) bytes", N, bytes.len()));
}
Expand All @@ -151,7 +155,8 @@ impl<const N: usize> StaticString<N> {
}

pub fn get_string(&self) -> String {
CStr::from_bytes_until_nul(self.buf.as_slice()).unwrap().to_str().unwrap().to_string()
let null_index = self.buf.iter().position(|c|c == &0u8).unwrap_or(N);
String::from_utf8(self.buf.as_slice()[0..null_index].to_vec()).unwrap()
}
}

Expand Down

0 comments on commit 03adc8f

Please sign in to comment.