less unsafe code

This commit is contained in:
Vinzenz Schroeter 2024-05-09 13:43:28 +02:00
parent 0b03c4d23a
commit 6bca63befd
2 changed files with 27 additions and 18 deletions

View file

@ -52,18 +52,14 @@ impl ApplicationHandler for App {
let mut i = 0; let mut i = 0;
for pixel in frame { for pixel in frame {
unsafe { let is_set = unsafe { DISPLAY[i] };
if i >= DISPLAY.len() { let color = if is_set {
break; [255u8, 255, 255, 255]
} } else {
[0u8, 0, 0, 255]
};
let color = if DISPLAY[i] { pixel.copy_from_slice(&color);
[255u8, 255, 255, 255]
} else {
[0u8, 0, 0, 255]
};
pixel.copy_from_slice(&color);
}
i += 1; i += 1;
} }

View file

@ -51,7 +51,6 @@ enum DisplayCommand {
CmdBitmapLinearXor = 0x0016, CmdBitmapLinearXor = 0x0016,
} }
#[repr(C)] #[repr(C)]
#[derive(Debug)] #[derive(Debug)]
struct HdrWindow { struct HdrWindow {
@ -123,7 +122,7 @@ fn read_hdr_window(buffer: &[u8]) -> Result<HdrWindow, String> {
return Err("received a packet that is too small".into()); return Err("received a packet that is too small".into());
} }
let command_u16 = u16::from_be(unsafe { std::ptr::read(buffer[0..=1].as_ptr() as *const u16) }); let command_u16 = read_beu16_from_buffer(&buffer[0..=1]);
let maybe_command = num::FromPrimitive::from_u16(command_u16); let maybe_command = num::FromPrimitive::from_u16(command_u16);
if maybe_command.is_none() { if maybe_command.is_none() {
return Err(format!("received invalid command {}", command_u16)); return Err(format!("received invalid command {}", command_u16));
@ -131,13 +130,26 @@ fn read_hdr_window(buffer: &[u8]) -> Result<HdrWindow, String> {
return Ok(HdrWindow { return Ok(HdrWindow {
command: maybe_command.unwrap(), command: maybe_command.unwrap(),
x: u16::from_be(unsafe { std::ptr::read(buffer[2..=3].as_ptr() as *const u16) }), x: read_beu16_from_buffer(&buffer[2..=3]),
y: u16::from_be(unsafe { std::ptr::read(buffer[4..=5].as_ptr() as *const u16) }), y: read_beu16_from_buffer(&buffer[4..=5]),
w: u16::from_be(unsafe { std::ptr::read(buffer[6..=7].as_ptr() as *const u16) }), w: read_beu16_from_buffer(&buffer[6..=7]),
h: u16::from_be(unsafe { std::ptr::read(buffer[8..=9].as_ptr() as *const u16) }), h: read_beu16_from_buffer(&buffer[8..=9]),
}); });
} }
fn read_beu16_from_buffer(buffer: &[u8]) -> u16 {
assert_eq!(
buffer.len(),
2,
"cannot read u16 from buffer with size != 2"
);
let ptr = buffer.as_ptr() as *const u16;
let u16 = unsafe { *ptr };
return u16::from_be(u16);
}
fn check_payload_size(buf: &[u8], expected: usize) -> bool { fn check_payload_size(buf: &[u8], expected: usize) -> bool {
let actual = buf.len(); let actual = buf.len();
if actual == expected { if actual == expected {
@ -179,9 +191,10 @@ fn print_bitmap_linear_win(header: &HdrWindow, payload: &[u8]) {
let translated_x = (x + header.x) as usize; let translated_x = (x + header.x) as usize;
let translated_y = (y + header.y) as usize; let translated_y = (y + header.y) as usize;
let index = translated_y * PIXEL_WIDTH as usize + translated_x;
unsafe { unsafe {
DISPLAY[translated_y * PIXEL_WIDTH as usize + translated_x] = is_set; DISPLAY[index] = is_set;
} }
} }
} }