fix stuff, add class annotation

This commit is contained in:
Jean-Marie Mineau 2023-11-28 19:06:28 +01:00
parent b8b4e28f2d
commit cf55766653
Signed by: histausse
GPG key ID: B66AEEDA9B645AD2
10 changed files with 164 additions and 117 deletions

View file

@ -87,6 +87,7 @@ mod test {
0xbe, 0x37, 0x01, 0xa8, 0x9f, 0x03, 0x1d, 0xf6, 0x33, 0x01, 0xd3, 0xe8, 0x02, 0x3f,
];
const ENCODED_ANNOTATION_RAW_2: &[u8] = &[0xf6, 0x33, 0x01, 0xd3, 0xe8, 0x02, 0x3f];
const ENCODED_ANNOTATION_RAW_3: &[u8] = &[0xbf, 0x37, 0x01, 0xa8, 0x9f, 0x03, 0x38, 0x38, 0x02];
const ANNOTATION_ELEMENT_RAW: &[u8] = &[
0xa8, 0x9f, 0x03, 0x1d, 0xf6, 0x33, 0x01, 0xd3, 0xe8, 0x02, 0x3f,
];
@ -94,6 +95,16 @@ mod test {
#[test]
fn deserialize_encoded_annotation() {
assert_eq!(
EncodedAnnotation::deserialize_from_slice(ENCODED_ANNOTATION_RAW_3).unwrap(),
EncodedAnnotation {
type_idx: Uleb128(7103),
elements: vec![AnnotationElement {
name_idx: Uleb128(53160),
value: EncodedValue::Type(0x0238)
}],
}
);
assert_eq!(
EncodedAnnotation::deserialize_from_slice(ENCODED_ANNOTATION_RAW_2).unwrap(),
EncodedAnnotation {

View file

@ -241,6 +241,7 @@ mod test {
const ANNOTATION_ITEM: &[u8] = &[
0x02, 0xbe, 0x37, 0x01, 0xa8, 0x9f, 0x03, 0x1d, 0xf6, 0x33, 0x01, 0xd3, 0xe8, 0x02, 0x3f,
];
const ANNOTATION_ITEM_2: &[u8] = &[0x02, 0xbf, 0x37, 0x01, 0xa8, 0x9f, 0x03, 0x38, 0x38, 0x02];
#[test]
fn deserialize_annotation_item() {
@ -252,6 +253,14 @@ mod test {
.unwrap(),
}
);
assert_eq!(
AnnotationItem::deserialize_from_slice(ANNOTATION_ITEM_2).unwrap(),
AnnotationItem {
visibility: AnnotationVisibility::System,
annotation: EncodedAnnotation::deserialize_from_slice(&ANNOTATION_ITEM_2[1..])
.unwrap(),
}
);
}
#[test]
@ -267,5 +276,16 @@ mod test {
.unwrap()
.as_slice()
);
assert_eq!(
ANNOTATION_ITEM_2,
AnnotationItem {
visibility: AnnotationVisibility::System,
annotation: EncodedAnnotation::deserialize_from_slice(&ANNOTATION_ITEM_2[1..])
.unwrap(),
}
.serialize_to_vec()
.unwrap()
.as_slice()
);
}
}

View file

@ -104,7 +104,7 @@ impl Serializable for EncodedValue {
}
Self::Short(val) => {
let size = self.size();
let bytes: [u8; 2] = val.to_be_bytes();
let bytes: [u8; 2] = val.to_le_bytes();
output
.write_all(&[((size as u8 - 2) << 5) | VALUE_SHORT.0])
.map_err(|err| {
@ -112,7 +112,7 @@ impl Serializable for EncodedValue {
"Failed to write serialized Short to output: {err}"
))
})?;
output.write_all(&bytes[(2 - size + 1)..]).map_err(|err| {
output.write_all(&bytes[..(size - 1)]).map_err(|err| {
Error::SerializationError(format!(
"Failed to write serialized Short to output: {err}"
))
@ -121,7 +121,7 @@ impl Serializable for EncodedValue {
}
Self::Char(val) => {
let size = self.size();
let bytes: [u8; 2] = val.to_be_bytes();
let bytes: [u8; 2] = val.to_le_bytes();
output
.write_all(&[((size as u8 - 2) << 5) | VALUE_CHAR.0])
.map_err(|err| {
@ -129,7 +129,7 @@ impl Serializable for EncodedValue {
"Failed to write serialized Char to output: {err}"
))
})?;
output.write_all(&bytes[(2 - size + 1)..]).map_err(|err| {
output.write_all(&bytes[..(size - 1)]).map_err(|err| {
Error::SerializationError(format!(
"Failed to write serialized Char to output: {err}"
))
@ -138,7 +138,7 @@ impl Serializable for EncodedValue {
}
Self::Int(val) => {
let size = self.size();
let bytes: [u8; 4] = val.to_be_bytes();
let bytes: [u8; 4] = val.to_le_bytes();
output
.write_all(&[((size as u8 - 2) << 5) | VALUE_INT.0])
.map_err(|err| {
@ -146,7 +146,7 @@ impl Serializable for EncodedValue {
"Failed to write serialized Int to output: {err}"
))
})?;
output.write_all(&bytes[(2 - size + 1)..]).map_err(|err| {
output.write_all(&bytes[..(size - 1)]).map_err(|err| {
Error::SerializationError(format!(
"Failed to write serialized Int to output: {err}"
))
@ -155,7 +155,7 @@ impl Serializable for EncodedValue {
}
Self::Long(val) => {
let size = self.size();
let bytes: [u8; 8] = val.to_be_bytes();
let bytes: [u8; 8] = val.to_le_bytes();
output
.write_all(&[((size as u8 - 2) << 5) | VALUE_LONG.0])
.map_err(|err| {
@ -163,7 +163,7 @@ impl Serializable for EncodedValue {
"Failed to write serialized Long to output: {err}"
))
})?;
output.write_all(&bytes[(2 - size + 1)..]).map_err(|err| {
output.write_all(&bytes[..(size - 1)]).map_err(|err| {
Error::SerializationError(format!(
"Failed to write serialized Long to output: {err}"
))
@ -172,7 +172,7 @@ impl Serializable for EncodedValue {
}
Self::Float(val) => {
let size = self.size();
let bytes = val.to_be_bytes();
let bytes = val.to_le_bytes(); // TODO: check LE/BE
output
.write_all(&[((size as u8 - 2) << 5) | VALUE_FLOAT.0])
.map_err(|err| {
@ -189,7 +189,7 @@ impl Serializable for EncodedValue {
}
Self::Double(val) => {
let size = self.size();
let bytes = val.to_be_bytes();
let bytes = val.to_le_bytes(); // TODO: check LE/BE
output
.write_all(&[((size as u8 - 2) << 5) | VALUE_DOUBLE.0])
.map_err(|err| {
@ -282,20 +282,19 @@ impl Serializable for EncodedValue {
} else {
let mut buffer = [0x00u8; 2];
let size = (arg + 1) as usize;
let first_byte = 2 - size;
input.read_exact(&mut buffer[first_byte..]).map_err(|_| {
input.read_exact(&mut buffer[..size]).map_err(|_| {
Error::InputTooSmall(
"Failed to read all bytes for the EncodedValue (Short) from the input"
.into(),
)
})?;
// Sign extention
if (buffer[first_byte] & 0b1000_0000) != 0 {
for b in buffer.iter_mut().take(first_byte - 1) {
if (buffer[size - 1] & 0b1000_0000) != 0 {
for b in buffer.iter_mut().take(size) {
*b = 0xff;
}
}
Ok(Self::Short(i16::from_be_bytes(buffer)))
Ok(Self::Short(i16::from_le_bytes(buffer)))
}
}
VALUE_CHAR => {
@ -306,14 +305,13 @@ impl Serializable for EncodedValue {
} else {
let mut buffer = [0x00u8; 2];
let size = (arg + 1) as usize;
let first_byte = 2 - size;
input.read_exact(&mut buffer[first_byte..]).map_err(|_| {
input.read_exact(&mut buffer[..size]).map_err(|_| {
Error::InputTooSmall(
"Failed to read all bytes for the EncodedValue (Char) from the input"
.into(),
)
})?;
Ok(Self::Char(u16::from_be_bytes(buffer)))
Ok(Self::Char(u16::from_le_bytes(buffer)))
}
}
VALUE_INT => {
@ -324,20 +322,19 @@ impl Serializable for EncodedValue {
} else {
let mut buffer = [0x00u8; 4];
let size = (arg + 1) as usize;
let first_byte = 4 - size;
input.read_exact(&mut buffer[first_byte..]).map_err(|_| {
input.read_exact(&mut buffer[..size]).map_err(|_| {
Error::InputTooSmall(
"Failed to read all bytes for the EncodedValue (Int) from the input"
.into(),
)
})?;
// Sign extention
if (buffer[first_byte] & 0b1000_0000) != 0 {
for b in buffer.iter_mut().take(first_byte - 1) {
if (buffer[size - 1] & 0b1000_0000) != 0 {
for b in buffer.iter_mut().take(size) {
*b = 0xff;
}
}
Ok(Self::Int(i32::from_be_bytes(buffer)))
Ok(Self::Int(i32::from_le_bytes(buffer)))
}
}
VALUE_LONG => {
@ -348,20 +345,19 @@ impl Serializable for EncodedValue {
} else {
let mut buffer = [0x00u8; 8];
let size = (arg + 1) as usize;
let first_byte = 8 - size;
input.read_exact(&mut buffer[first_byte..]).map_err(|_| {
input.read_exact(&mut buffer[..size]).map_err(|_| {
Error::InputTooSmall(
"Failed to read all bytes for the EncodedValue (Long) from the input"
.into(),
)
})?;
// Sign extention
if (buffer[first_byte] & 0b1000_0000) != 0 {
for b in buffer.iter_mut().take(first_byte - 1) {
if (buffer[size - 1] & 0b1000_0000) != 0 {
for b in buffer.iter_mut().take(size) {
*b = 0xff;
}
}
Ok(Self::Long(i64::from_be_bytes(buffer)))
Ok(Self::Long(i64::from_le_bytes(buffer)))
}
}
VALUE_FLOAT => {
@ -378,7 +374,7 @@ impl Serializable for EncodedValue {
.into(),
)
})?;
Ok(Self::Float(f32::from_be_bytes(buffer)))
Ok(Self::Float(f32::from_le_bytes(buffer)))
}
}
VALUE_DOUBLE => {
@ -395,7 +391,7 @@ impl Serializable for EncodedValue {
.into(),
)
})?;
Ok(Self::Double(f64::from_be_bytes(buffer)))
Ok(Self::Double(f64::from_le_bytes(buffer)))
}
}
VALUE_METHOD_TYPE => {
@ -406,14 +402,13 @@ impl Serializable for EncodedValue {
} else {
let mut buffer = [0x00u8; 4];
let size = (arg + 1) as usize;
let first_byte = 4 - size;
input.read_exact(&mut buffer[first_byte..]).map_err(|_| {
input.read_exact(&mut buffer[..size]).map_err(|_| {
Error::InputTooSmall(
"Failed to read all bytes for the EncodedValue (MethodType) from the input"
.into(),
)
})?;
Ok(Self::MethodType(u32::from_be_bytes(buffer)))
Ok(Self::MethodType(u32::from_le_bytes(buffer)))
}
}
VALUE_METHOD_HANDLE => {
@ -424,14 +419,13 @@ impl Serializable for EncodedValue {
} else {
let mut buffer = [0x00u8; 4];
let size = (arg + 1) as usize;
let first_byte = 4 - size;
input.read_exact(&mut buffer[first_byte..]).map_err(|_| {
input.read_exact(&mut buffer[..size]).map_err(|_| {
Error::InputTooSmall(
"Failed to read all bytes for the EncodedValue (MethodHandle) from the input"
.into(),
)
})?;
Ok(Self::MethodHandle(u32::from_be_bytes(buffer)))
Ok(Self::MethodHandle(u32::from_le_bytes(buffer)))
}
}
VALUE_STRING => {
@ -442,14 +436,13 @@ impl Serializable for EncodedValue {
} else {
let mut buffer = [0x00u8; 4];
let size = (arg + 1) as usize;
let first_byte = 4 - size;
input.read_exact(&mut buffer[first_byte..]).map_err(|_| {
input.read_exact(&mut buffer[..size]).map_err(|_| {
Error::InputTooSmall(
"Failed to read all bytes for the EncodedValue (String) from the input"
.into(),
)
})?;
Ok(Self::String(u32::from_be_bytes(buffer)))
Ok(Self::String(u32::from_le_bytes(buffer)))
}
}
VALUE_TYPE => {
@ -460,14 +453,13 @@ impl Serializable for EncodedValue {
} else {
let mut buffer = [0x00u8; 4];
let size = (arg + 1) as usize;
let first_byte = 4 - size;
input.read_exact(&mut buffer[first_byte..]).map_err(|_| {
input.read_exact(&mut buffer[..size]).map_err(|_| {
Error::InputTooSmall(
"Failed to read all bytes for the EncodedValue (Type) from the input"
.into(),
)
})?;
Ok(Self::Type(u32::from_be_bytes(buffer)))
Ok(Self::Type(u32::from_le_bytes(buffer)))
}
}
VALUE_FIELD => {
@ -478,14 +470,13 @@ impl Serializable for EncodedValue {
} else {
let mut buffer = [0x00u8; 4];
let size = (arg + 1) as usize;
let first_byte = 4 - size;
input.read_exact(&mut buffer[first_byte..]).map_err(|_| {
input.read_exact(&mut buffer[..size]).map_err(|_| {
Error::InputTooSmall(
"Failed to read all bytes for the EncodedValue (Field) from the input"
.into(),
)
})?;
Ok(Self::Field(u32::from_be_bytes(buffer)))
Ok(Self::Field(u32::from_le_bytes(buffer)))
}
}
VALUE_METHOD => {
@ -496,14 +487,13 @@ impl Serializable for EncodedValue {
} else {
let mut buffer = [0x00u8; 4];
let size = (arg + 1) as usize;
let first_byte = 4 - size;
input.read_exact(&mut buffer[first_byte..]).map_err(|_| {
input.read_exact(&mut buffer[..size]).map_err(|_| {
Error::InputTooSmall(
"Failed to read all bytes for the EncodedValue (Method) from the input"
.into(),
)
})?;
Ok(Self::Method(u32::from_be_bytes(buffer)))
Ok(Self::Method(u32::from_le_bytes(buffer)))
}
}
VALUE_ENUM => {
@ -514,14 +504,13 @@ impl Serializable for EncodedValue {
} else {
let mut buffer = [0x00u8; 4];
let size = (arg + 1) as usize;
let first_byte = 4 - size;
input.read_exact(&mut buffer[first_byte..]).map_err(|_| {
input.read_exact(&mut buffer[..size]).map_err(|_| {
Error::InputTooSmall(
"Failed to read all bytes for the EncodedValue (Enum) from the input"
.into(),
)
})?;
Ok(Self::Enum(u32::from_be_bytes(buffer)))
Ok(Self::Enum(u32::from_le_bytes(buffer)))
}
}
VALUE_ARRAY => {
@ -643,8 +632,8 @@ impl Serializable for Idx {
fn serialize(&self, output: &mut dyn Write) -> Result<()> {
let Idx(idx, ValueType(vt)) = self;
let size = self.size() - 2;
let bytes: [u8; 4] = idx.to_be_bytes();
let vec = &bytes[(4 - size + 1)..];
let bytes: [u8; 4] = idx.to_le_bytes();
let vec = &bytes[..(size + 1)];
output
.write_all(&[((size as u8) << 5) | vt])
.map_err(|err| {