From 6bca63befd576552766d9f94cd3f820c0ac35fb8 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Thu, 9 May 2024 13:43:28 +0200 Subject: [PATCH] less unsafe code --- src/gui.rs | 18 +++++++----------- src/upd_loop.rs | 27 ++++++++++++++++++++------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/gui.rs b/src/gui.rs index 12b292b..e2f9cd4 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -52,18 +52,14 @@ impl ApplicationHandler for App { let mut i = 0; for pixel in frame { - unsafe { - if i >= DISPLAY.len() { - break; - } + let is_set = unsafe { DISPLAY[i] }; + let color = if is_set { + [255u8, 255, 255, 255] + } else { + [0u8, 0, 0, 255] + }; - let color = if DISPLAY[i] { - [255u8, 255, 255, 255] - } else { - [0u8, 0, 0, 255] - }; - pixel.copy_from_slice(&color); - } + pixel.copy_from_slice(&color); i += 1; } diff --git a/src/upd_loop.rs b/src/upd_loop.rs index 6f37c06..abced91 100644 --- a/src/upd_loop.rs +++ b/src/upd_loop.rs @@ -51,7 +51,6 @@ enum DisplayCommand { CmdBitmapLinearXor = 0x0016, } - #[repr(C)] #[derive(Debug)] struct HdrWindow { @@ -123,7 +122,7 @@ fn read_hdr_window(buffer: &[u8]) -> Result { 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); if maybe_command.is_none() { return Err(format!("received invalid command {}", command_u16)); @@ -131,13 +130,26 @@ fn read_hdr_window(buffer: &[u8]) -> Result { return Ok(HdrWindow { command: maybe_command.unwrap(), - x: u16::from_be(unsafe { std::ptr::read(buffer[2..=3].as_ptr() as *const u16) }), - y: u16::from_be(unsafe { std::ptr::read(buffer[4..=5].as_ptr() as *const u16) }), - w: u16::from_be(unsafe { std::ptr::read(buffer[6..=7].as_ptr() as *const u16) }), - h: u16::from_be(unsafe { std::ptr::read(buffer[8..=9].as_ptr() as *const u16) }), + x: read_beu16_from_buffer(&buffer[2..=3]), + y: read_beu16_from_buffer(&buffer[4..=5]), + w: read_beu16_from_buffer(&buffer[6..=7]), + 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 { let actual = buf.len(); 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_y = (y + header.y) as usize; + let index = translated_y * PIXEL_WIDTH as usize + translated_x; unsafe { - DISPLAY[translated_y * PIXEL_WIDTH as usize + translated_x] = is_set; + DISPLAY[index] = is_set; } } }