Compare commits
2 Commits
be3fbe811a
...
5cf3918e74
Author | SHA1 | Date | |
---|---|---|---|
5cf3918e74 | |||
9437cc4cd9 |
@ -6,7 +6,3 @@ edition = "2021"
|
||||
[features]
|
||||
default = []
|
||||
internal_opt = []
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[profile.release]
|
||||
debug = true
|
@ -11,7 +11,3 @@ byte = "0.2.6"
|
||||
[features]
|
||||
default = []
|
||||
internal_opt = []
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[profile.release]
|
||||
debug = true
|
@ -1,23 +0,0 @@
|
||||
# cargo fmt config
|
||||
|
||||
# 最大行长
|
||||
max_width = 100
|
||||
# 链式调用的最大长度
|
||||
chain_width = 80
|
||||
# 数组的最大长度
|
||||
array_width = 80
|
||||
# 函数参数的最大长度
|
||||
attr_fn_like_width = 80
|
||||
# 函数调用参数的最大长度
|
||||
fn_call_width = 80
|
||||
# 简单函数格式化为单行
|
||||
fn_single_line = true
|
||||
|
||||
# 自动对齐最大长度
|
||||
enum_discrim_align_threshold = 5
|
||||
# 字段初始化使用简写
|
||||
use_field_init_shorthand = true
|
||||
# 是否使用彩色输出
|
||||
color = "Always"
|
||||
|
||||
edition = "2021"
|
@ -34,19 +34,6 @@ macro_rules! read_uncheck {
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! read {
|
||||
($name:ident, $ty:ty, $size:literal) => {
|
||||
#[doc = concat!("读取 ", stringify!($ty), " 类型 ", $size, " 长度的数据")]
|
||||
pub fn $name(&mut self) -> $ty {
|
||||
let value = self.data[self.cursor..self.cursor + $size]
|
||||
.iter()
|
||||
.fold(0, |acc, &x| (acc << 8) | x as $ty);
|
||||
self.cursor += $size;
|
||||
value
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl NbtReader<'_> {
|
||||
pub fn new(data: &mut [u8]) -> NbtReader {
|
||||
NbtReader {
|
||||
@ -71,6 +58,68 @@ impl NbtReader<'_> {
|
||||
read_uncheck!(read_u32_unchecked, u32, 4);
|
||||
read_uncheck!(read_i64_unchecked, i64, 8);
|
||||
read_uncheck!(read_u64_unchecked, u64, 8);
|
||||
/// 安全的读取 i16 类型的数据
|
||||
///
|
||||
/// 转换大小端(大端)
|
||||
///
|
||||
/// 会在超出长度时 panic
|
||||
pub fn read_i16(&mut self) -> i16 {
|
||||
let value = i16::from_be_bytes([self.data[self.cursor], self.data[self.cursor + 1]]);
|
||||
self.cursor += 2;
|
||||
value
|
||||
}
|
||||
/// 安全的读取 u16 类型的数据
|
||||
///
|
||||
/// 转换大小端(大端)
|
||||
///
|
||||
/// 会在超出长度时 panic
|
||||
pub fn read_u16(&mut self) -> u16 { self.read_i16() as u16 }
|
||||
/// 安全的读取 i32 类型的数据
|
||||
///
|
||||
/// 转换大小端(大端)
|
||||
///
|
||||
/// 会在超出长度时 panic
|
||||
pub fn read_i32(&mut self) -> i32 {
|
||||
let value = i32::from_be_bytes([
|
||||
self.data[self.cursor],
|
||||
self.data[self.cursor + 1],
|
||||
self.data[self.cursor + 2],
|
||||
self.data[self.cursor + 3],
|
||||
]);
|
||||
self.cursor += 4;
|
||||
value
|
||||
}
|
||||
/// 安全的读取 u32 类型的数据
|
||||
///
|
||||
/// 转换大小端(大端)
|
||||
///
|
||||
/// 会在超出长度时 panic
|
||||
pub fn read_u32(&mut self) -> u32 { self.read_i32() as u32 }
|
||||
/// 安全的读取 i64 类型的数据
|
||||
///
|
||||
/// 转换大小端(大端)
|
||||
///
|
||||
/// 会在超出长度时 panic
|
||||
pub fn read_i64(&mut self) -> i64 {
|
||||
let value = i64::from_be_bytes([
|
||||
self.data[self.cursor],
|
||||
self.data[self.cursor + 1],
|
||||
self.data[self.cursor + 2],
|
||||
self.data[self.cursor + 3],
|
||||
self.data[self.cursor + 4],
|
||||
self.data[self.cursor + 5],
|
||||
self.data[self.cursor + 6],
|
||||
self.data[self.cursor + 7],
|
||||
]);
|
||||
self.cursor += 8;
|
||||
value
|
||||
}
|
||||
/// 安全的读取 u64 类型的数据
|
||||
///
|
||||
/// 转换大小端(大端)
|
||||
///
|
||||
/// 会在超出长度时 panic
|
||||
pub fn read_u64(&mut self) -> u64 { self.read_i64() as u64 }
|
||||
/// 读取一个 f32 类型的数据
|
||||
///
|
||||
/// 转换大小端
|
||||
@ -81,7 +130,7 @@ impl NbtReader<'_> {
|
||||
pub unsafe fn read_f32_unchecked(&mut self) -> f32 {
|
||||
let value = std::ptr::read_unaligned(self.data[self.cursor..].as_ptr() as *const u32);
|
||||
self.cursor += 4;
|
||||
f32::from_bits(value.to_be())
|
||||
std::mem::transmute::<u32, f32>(value.to_be())
|
||||
}
|
||||
/// 读取一个 f64 类型的数据
|
||||
/// 转换大小端
|
||||
@ -92,14 +141,23 @@ impl NbtReader<'_> {
|
||||
pub unsafe fn read_f64_unchecked(&mut self) -> f64 {
|
||||
let value = std::ptr::read_unaligned(self.data[self.cursor..].as_ptr() as *const u64);
|
||||
self.cursor += 8;
|
||||
f64::from_bits(value.to_be())
|
||||
std::mem::transmute::<u64, f64>(value.to_be())
|
||||
}
|
||||
|
||||
/// 读取指定长度的 u8 数组
|
||||
///
|
||||
/// # 安全性
|
||||
///
|
||||
/// 长度溢出会导致 panic
|
||||
pub fn read_u8_array(&mut self, len: usize) -> &[u8] {
|
||||
let value = &self.data[self.cursor..self.cursor + len];
|
||||
self.cursor += len;
|
||||
value
|
||||
}
|
||||
/// 读取指定长度的 i8 数组
|
||||
///
|
||||
/// # 安全性
|
||||
///
|
||||
/// 长度溢出会导致 UB
|
||||
pub fn read_i8_array(&mut self, len: usize) -> &[i8] {
|
||||
let value = unsafe {
|
||||
std::slice::from_raw_parts(self.data[self.cursor..].as_ptr() as *const i8, len)
|
||||
@ -107,12 +165,17 @@ impl NbtReader<'_> {
|
||||
self.cursor += len;
|
||||
value
|
||||
}
|
||||
/// 读取指定长度的 utf-8 字符串
|
||||
///
|
||||
/// # 安全性
|
||||
///
|
||||
/// 长度溢出会导致 panic
|
||||
pub fn read_string(&mut self, len: usize) -> String {
|
||||
let value = String::from_utf8_lossy(&self.data[self.cursor..self.cursor + len]);
|
||||
self.cursor += len;
|
||||
value.into_owned()
|
||||
}
|
||||
pub fn read_int_array(&mut self, len: usize) -> &[i32] {
|
||||
pub fn read_i32_array_unchecked(&mut self, len: usize) -> &[i32] {
|
||||
unsafe {
|
||||
println!("data: {:?}", self.data);
|
||||
let value =
|
||||
@ -125,7 +188,7 @@ impl NbtReader<'_> {
|
||||
value
|
||||
}
|
||||
}
|
||||
pub fn read_long_array(&mut self, len: usize) -> &[i64] {
|
||||
pub fn read_i64_array(&mut self, len: usize) -> &[i64] {
|
||||
unsafe {
|
||||
println!("data: {:?}", self.data);
|
||||
let value =
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{NbtReader, NbtValue};
|
||||
use crate::NbtReader;
|
||||
|
||||
/// 生成测试数据
|
||||
pub fn gen_datas(len: usize) -> Vec<u8> {
|
||||
@ -19,21 +19,11 @@ fn basic_init() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn read_i8() {
|
||||
fn read_x8() {
|
||||
let mut data = vec![0x01, 0x02, 0x03, 0x04];
|
||||
let mut reader = NbtReader::new(&mut data);
|
||||
assert_eq!(reader.read_i8(), 0x01);
|
||||
assert_eq!(reader.cursor, 1);
|
||||
assert_eq!(reader.read_i8(), 0x02);
|
||||
assert_eq!(reader.cursor, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn read_one_bytes() {
|
||||
let mut data = vec![0x01, 0x02];
|
||||
let mut reader = NbtReader::new(&mut data);
|
||||
assert_eq!(reader.read_u8(), 0x01);
|
||||
assert_eq!(reader.cursor, 1);
|
||||
assert_eq!(reader.read_u8(), 0x02);
|
||||
assert_eq!(reader.cursor, 2);
|
||||
}
|
||||
@ -42,13 +32,14 @@ fn read_one_bytes() {
|
||||
fn read_data_safe() {
|
||||
let mut data = vec![0x01, 0x02, 0x03, 0x04];
|
||||
let mut reader = NbtReader::new(&mut data);
|
||||
assert_eq!(reader.read_u8(), 0x00);
|
||||
assert_eq!(reader.read_u8(), 0x01);
|
||||
assert_eq!(reader.cursor, 1);
|
||||
assert_eq!(reader.read_i8(), 0x01);
|
||||
assert_eq!(reader.read_i8(), 0x02);
|
||||
assert_eq!(reader.cursor, 2);
|
||||
assert_eq!(reader.read_u8(), 0x02);
|
||||
assert_eq!(reader.read_u8(), 0x03);
|
||||
assert_eq!(reader.cursor, 3);
|
||||
|
||||
assert_eq!(reader.read_i8(), 0x04);
|
||||
assert_eq!(reader.cursor, 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -65,7 +56,7 @@ fn read_array() {
|
||||
fn read_int_array() {
|
||||
let mut value = 1234567890_i32.to_be_bytes();
|
||||
let mut reader = NbtReader::new(&mut value);
|
||||
assert_eq!(reader.read_int_array(1), &[1234567890_i32]);
|
||||
assert_eq!(reader.read_i32_array_unchecked(1), &[1234567890_i32]);
|
||||
assert_eq!(reader.cursor, 4);
|
||||
}
|
||||
|
||||
@ -73,6 +64,6 @@ fn read_int_array() {
|
||||
fn read_long_array() {
|
||||
let mut value = 1234567890_i64.to_be_bytes();
|
||||
let mut reader = NbtReader::new(&mut value);
|
||||
assert_eq!(reader.read_long_array(1), &[1234567890_i64]);
|
||||
assert_eq!(reader.read_i64_array(1), &[1234567890_i64]);
|
||||
assert_eq!(reader.cursor, 8);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user