// K2221-K2225: raster.pixels — Pixel format ve stride // Invariant: Çizim framebuffer byte aralığını aşmaz // Positive: RGBA ve padded stride // Counterexample: Son satırda overflow // Acceptance: Guard kanaryaları korunur pub const RASTER_PIXELS_SCHEMA: &str = "turkos.os.raster.pixels.v1"; pub const RASTER_PIXELS_INVARIANT: &str = "Çizim framebuffer byte aralığını aşmaz"; pub const RASTER_PIXELS_POSITIVE: &str = "RGBA ve padded stride"; pub const RASTER_PIXELS_COUNTEREXAMPLE: &str = "Son satırda overflow"; pub const RASTER_PIXELS_ACCEPTANCE: &str = "Guard kanaryaları korunur"; pub const EXPECTED_WIDTH_PX: u32 = 640; pub const EXPECTED_HEIGHT_PX: u32 = 480; pub const BYTES_PER_PIXEL_RGBA: u32 = 4; pub const EXPECTED_PADDED_STRIDE_BYTES: u32 = 2624; pub const EXPECTED_FRAMEBUFFER_BYTES: u32 = 1_259_520; pub const EXPECTED_GUARD_CANARY: u64 = 0xCA4A_7E00_2225_F00D; #[repr(u8)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum PixelsError { LastRowByteOverflow = 1, UnsupportedPixelFormat = 2, StrideSmallerThanRow = 3, InvalidPaddedStride = 4, InvalidDimensions = 5, InvalidFramebufferCapacity = 6, HeadGuardCanaryCorrupted = 7, TailGuardCanaryCorrupted = 8, ZeroPlottedPixels = 9, NonMonotonicPlotEpoch = 10, ZeroFramePixelDigest = 11, UnboundPixelPlotDigest = 12, AlreadyPlotted = 13, } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct PixelPlotRequest { pub format_rgba8888: bool, pub width_px: u32, pub height_px: u32, pub stride_bytes: u32, pub framebuffer_capacity_bytes: u32, pub last_row_end_byte: u32, pub head_guard_canary: u64, pub tail_guard_canary: u64, pub plotted_pixel_count: u32, pub plot_epoch: u64, pub previous_plot_epoch: u64, pub frame_pixel_digest: u64, pub pixel_binding_digest: u64, } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct PixelPlotReceipt { pub width_px: u32, pub height_px: u32, pub stride_bytes: u32, pub framebuffer_capacity_bytes: u32, pub last_row_end_byte: u32, pub plotted_pixel_count: u32, pub guard_canaries_intact: bool, pub plot_epoch: u64, pub frame_pixel_digest: u64, pub receipt_digest: u64, } pub const fn compute_pixel_binding_digest( width_px: u32, height_px: u32, stride_bytes: u32, framebuffer_capacity_bytes: u32, last_row_end_byte: u32, head_guard_canary: u64, tail_guard_canary: u64, plotted_pixel_count: u32, plot_epoch: u64, frame_pixel_digest: u64, ) -> u64 { let mut h: u64 = 0xcbf2_9ce4_8422_2325; h = (h ^ 0x2225_5049_5845_4C53).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (width_px as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (height_px as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (stride_bytes as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (framebuffer_capacity_bytes as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (last_row_end_byte as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ head_guard_canary).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ tail_guard_canary).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (plotted_pixel_count as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ plot_epoch).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ frame_pixel_digest).wrapping_mul(0x0000_0100_0000_01b3); h } pub struct RasterPixelsCoordinator { plotted: bool, plotted_total: u64, rejected_total: u64, bounds_refusals: u64, canary_refusals: u64, stride_refusals: u64, committed_receipt: Option, } impl RasterPixelsCoordinator { pub const fn new() -> Self { Self { plotted: false, plotted_total: 0, rejected_total: 0, bounds_refusals: 0, canary_refusals: 0, stride_refusals: 0, committed_receipt: None, } } pub fn is_plotted(&self) -> bool { self.plotted } pub fn plotted_total(&self) -> u64 { self.plotted_total } pub fn rejected_total(&self) -> u64 { self.rejected_total } pub fn bounds_refusals(&self) -> u64 { self.bounds_refusals } pub fn canary_refusals(&self) -> u64 { self.canary_refusals } pub fn stride_refusals(&self) -> u64 { self.stride_refusals } pub fn committed_receipt(&self) -> Option { self.committed_receipt } pub fn commit_pixel_plot( &mut self, req: &PixelPlotRequest, ) -> Result { if self.plotted { self.rejected_total += 1; return Err(PixelsError::AlreadyPlotted); } if !req.format_rgba8888 { self.rejected_total += 1; self.stride_refusals += 1; return Err(PixelsError::UnsupportedPixelFormat); } if req.width_px != EXPECTED_WIDTH_PX || req.height_px != EXPECTED_HEIGHT_PX { self.rejected_total += 1; self.stride_refusals += 1; return Err(PixelsError::InvalidDimensions); } if req.stride_bytes < req.width_px * BYTES_PER_PIXEL_RGBA { self.rejected_total += 1; self.stride_refusals += 1; return Err(PixelsError::StrideSmallerThanRow); } if req.stride_bytes != EXPECTED_PADDED_STRIDE_BYTES { self.rejected_total += 1; self.stride_refusals += 1; return Err(PixelsError::InvalidPaddedStride); } if req.framebuffer_capacity_bytes != EXPECTED_FRAMEBUFFER_BYTES { self.rejected_total += 1; self.bounds_refusals += 1; return Err(PixelsError::InvalidFramebufferCapacity); } if req.last_row_end_byte > req.framebuffer_capacity_bytes { self.rejected_total += 1; self.bounds_refusals += 1; return Err(PixelsError::LastRowByteOverflow); } if req.head_guard_canary != EXPECTED_GUARD_CANARY { self.rejected_total += 1; self.canary_refusals += 1; return Err(PixelsError::HeadGuardCanaryCorrupted); } if req.tail_guard_canary != EXPECTED_GUARD_CANARY { self.rejected_total += 1; self.canary_refusals += 1; return Err(PixelsError::TailGuardCanaryCorrupted); } if req.plotted_pixel_count == 0 { self.rejected_total += 1; return Err(PixelsError::ZeroPlottedPixels); } if req.plot_epoch <= req.previous_plot_epoch { self.rejected_total += 1; return Err(PixelsError::NonMonotonicPlotEpoch); } if req.frame_pixel_digest == 0 { self.rejected_total += 1; return Err(PixelsError::ZeroFramePixelDigest); } let expected_binding = compute_pixel_binding_digest( req.width_px, req.height_px, req.stride_bytes, req.framebuffer_capacity_bytes, req.last_row_end_byte, req.head_guard_canary, req.tail_guard_canary, req.plotted_pixel_count, req.plot_epoch, req.frame_pixel_digest, ); if req.pixel_binding_digest != expected_binding { self.rejected_total += 1; self.canary_refusals += 1; return Err(PixelsError::UnboundPixelPlotDigest); } self.plotted = true; self.plotted_total += 1; let receipt = PixelPlotReceipt { width_px: req.width_px, height_px: req.height_px, stride_bytes: req.stride_bytes, framebuffer_capacity_bytes: req.framebuffer_capacity_bytes, last_row_end_byte: req.last_row_end_byte, plotted_pixel_count: req.plotted_pixel_count, guard_canaries_intact: true, plot_epoch: req.plot_epoch, frame_pixel_digest: req.frame_pixel_digest, receipt_digest: expected_binding ^ 0x2225_ACE0_CA4A_7E01, }; self.committed_receipt = Some(receipt); Ok(receipt) } }