Skip to content

Commit 5a1dc27

Browse files
committed
Buffer[Field] -> Integer tests & fixes
1 parent 14ea326 commit 5a1dc27

1 file changed

Lines changed: 36 additions & 2 deletions

File tree

src/aml/object.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ impl Object {
217217
*/
218218
let length = usize::min(bytes.len(), allowed_bytes);
219219
let mut to_interpret = [0u8; 8];
220-
to_interpret[0..length].copy_from_slice(bytes);
220+
to_interpret[0..length].copy_from_slice(&bytes[0..length]);
221221
Ok(u64::from_le_bytes(to_interpret))
222222
}
223223
Object::String(value) => {
@@ -243,7 +243,7 @@ impl Object {
243243
Object::BufferField { .. } => {
244244
let mut buffer = [0u8; 8];
245245
self.read_buffer_field(&mut buffer)?;
246-
Ok(u64::from_le_bytes(buffer))
246+
Ok(u64::from_le_bytes(buffer) % (1 << (allowed_bytes as u64 * 8)))
247247
}
248248
_ => Err(AmlError::InvalidOperationOnObject { op: Operation::ToInteger, typ: self.typ() })?,
249249
}
@@ -544,4 +544,38 @@ mod tests {
544544
copy_bits(&src, 0, &mut dst, 2, 15);
545545
assert_eq!(dst, [0b1111_1101, 0b1101_1110, 0b0000_0001, 0b0000_0000, 0b0000_0000]);
546546
}
547+
548+
#[test]
549+
fn buffer_to_integer() {
550+
let buffer = Object::Buffer(Vec::from([0xab, 0xcd, 0xef, 0x01, 0xff]));
551+
assert_eq!(buffer.to_integer(4).unwrap(), 0x01efcdab);
552+
}
553+
554+
555+
#[test]
556+
fn buffer_field_to_integer() {
557+
const BUFFER: [u8; 5] = [0xffu8; 5];
558+
let buffer = Object::Buffer(Vec::from(BUFFER)).wrap();
559+
let buffer_field = Object::BufferField {
560+
buffer,
561+
offset: 5,
562+
length: 9,
563+
};
564+
565+
assert_eq!(buffer_field.to_integer(4).unwrap(), 0x1ff);
566+
}
567+
568+
#[test]
569+
fn buffer_field_to_4_byte_integer() {
570+
// The ones in this buffer are strategically chosen to not make it to the final integer.
571+
const BUFFER: [u8; 5] = [0x0f, 0x00, 0x00, 0x00, 0xf0];
572+
let buffer = Object::Buffer(Vec::from(BUFFER)).wrap();
573+
let buffer_field = Object::BufferField {
574+
buffer,
575+
offset: 4,
576+
length: 36, // This should be truncated to 32 bits in the conversion
577+
};
578+
579+
assert_eq!(buffer_field.to_integer(4).unwrap(), 0);
580+
}
547581
}

0 commit comments

Comments
 (0)