mirx 0.50.0

MIRX binary image format — flat & chunk-based container for ahead-of-time UI assets
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
use alloc::vec::Vec;
use core::{mem, mem::size_of};

use super::{COLOR_LEN, HEADER_LEN, PaletteDecodeError, PaletteView, validate_payload_layout};
use crate::{
    payload::envelope::{VERSION, checked_payload_len, write_crc_trailer},
    reader::PayloadLimits,
    types::Color,
};

/// Editable ordered PALETTE color table.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Palette {
    pub colors: Vec<Color>,
}

/// Failure from a positional in-memory PALETTE mutation.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum PaletteMutationError {
    IndexOutOfBounds { index: usize, len: usize },
    AllocationFailed,
}

/// Failure while validating or encoding an editable PALETTE value.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum PaletteEncodeError {
    InvalidPayload(PaletteDecodeError),
    ColorCountOverflow { actual: usize },
    BufferTooSmall { needed: usize, available: usize },
    AllocationFailed,
}

impl From<PaletteDecodeError> for PaletteEncodeError {
    fn from(value: PaletteDecodeError) -> Self {
        Self::InvalidPayload(value)
    }
}

impl Palette {
    pub const fn new() -> Self {
        Self { colors: Vec::new() }
    }

    pub fn from_colors(colors: Vec<Color>) -> Self {
        Self { colors }
    }

    /// Decodes one complete PALETTE payload with one bounded, fallible color
    /// allocation.
    ///
    /// Structural, resource, exact-end, and decoded-byte checks complete before
    /// CRC validation and the first reserve.
    pub fn decode_with_limits(
        payload: &[u8],
        limits: &PayloadLimits,
    ) -> Result<Self, PaletteDecodeError> {
        decode_payload_with_allocator(payload, limits, &mut CheckedDecodeAllocator)
    }

    pub(crate) fn decode_view_with_limits(
        view: PaletteView<'_>,
        limits: &PayloadLimits,
    ) -> Result<Self, PaletteDecodeError> {
        validate_decoded_budget(view.len(), limits)?;
        decode_view_with_allocator(view, &mut CheckedDecodeAllocator)
    }

    /// Appends a color after reserving its slot fallibly.
    pub fn push(&mut self, color: Color) -> Result<(), PaletteMutationError> {
        self.insert(self.colors.len(), color)
    }

    /// Inserts a color at an exact position, including `len()` for append.
    pub fn insert(&mut self, index: usize, color: Color) -> Result<(), PaletteMutationError> {
        self.insert_with(index, color, |colors| {
            colors
                .try_reserve(1)
                .map_err(|_| PaletteMutationError::AllocationFailed)
        })
    }

    /// Replaces one color and returns the previous value.
    pub fn replace(&mut self, index: usize, color: Color) -> Result<Color, PaletteMutationError> {
        let len = self.colors.len();
        let current = self
            .colors
            .get_mut(index)
            .ok_or(PaletteMutationError::IndexOutOfBounds { index, len })?;
        Ok(mem::replace(current, color))
    }

    /// Removes and returns one color at an exact position.
    pub fn remove(&mut self, index: usize) -> Result<Color, PaletteMutationError> {
        let len = self.colors.len();
        if index >= len {
            return Err(PaletteMutationError::IndexOutOfBounds { index, len });
        }
        Ok(self.colors.remove(index))
    }

    /// Moves one color to its final ordinal without allocating.
    pub fn move_color(&mut self, from: usize, to: usize) -> Result<(), PaletteMutationError> {
        let len = self.colors.len();
        if from >= len {
            return Err(PaletteMutationError::IndexOutOfBounds { index: from, len });
        }
        if to >= len {
            return Err(PaletteMutationError::IndexOutOfBounds { index: to, len });
        }
        if from < to {
            self.colors[from..=to].rotate_left(1);
        } else if to < from {
            self.colors[to..=from].rotate_right(1);
        }
        Ok(())
    }

    /// Returns the exact size of this value's canonical PALETTE payload.
    pub fn encoded_payload_len(&self) -> Result<usize, PaletteEncodeError> {
        Ok(self.payload_plan()?.encoded_len())
    }

    /// Encodes a canonical PALETTE payload into the start of `out`.
    ///
    /// Complete sizing precedes the capacity check. Errors leave all of `out`
    /// unchanged, and success preserves its unused suffix.
    pub fn encode_payload_into(&self, out: &mut [u8]) -> Result<usize, PaletteEncodeError> {
        self.payload_plan()?.copy_payload_into(out)
    }

    /// Allocates and encodes one exact-length canonical PALETTE payload.
    pub fn encode_payload(&self) -> Result<Vec<u8>, PaletteEncodeError> {
        self.payload_plan()?.payload_to_vec()
    }

    pub(crate) fn payload_plan(&self) -> Result<PalettePayloadPlan<'_>, PaletteEncodeError> {
        PalettePayloadPlan::new(self)
    }

    fn insert_with(
        &mut self,
        index: usize,
        color: Color,
        reserve: impl FnOnce(&mut Vec<Color>) -> Result<(), PaletteMutationError>,
    ) -> Result<(), PaletteMutationError> {
        let len = self.colors.len();
        if index > len {
            return Err(PaletteMutationError::IndexOutOfBounds { index, len });
        }
        reserve(&mut self.colors)?;
        self.colors.insert(index, color);
        Ok(())
    }

    #[cfg(test)]
    fn encode_payload_with(
        &self,
        reserve: impl FnOnce(&mut Vec<u8>, usize) -> Result<(), PaletteEncodeError>,
    ) -> Result<Vec<u8>, PaletteEncodeError> {
        self.payload_plan()?.payload_to_vec_with(reserve)
    }
}

trait DecodeAllocator {
    fn reserve_colors(
        &mut self,
        colors: &mut Vec<Color>,
        count: usize,
    ) -> Result<(), PaletteDecodeError>;
}

struct CheckedDecodeAllocator;

impl DecodeAllocator for CheckedDecodeAllocator {
    fn reserve_colors(
        &mut self,
        colors: &mut Vec<Color>,
        count: usize,
    ) -> Result<(), PaletteDecodeError> {
        colors
            .try_reserve_exact(count)
            .map_err(|_| PaletteDecodeError::AllocationFailed)
    }
}

fn decode_payload_with_allocator<A: DecodeAllocator>(
    payload: &[u8],
    limits: &PayloadLimits,
    allocator: &mut A,
) -> Result<Palette, PaletteDecodeError> {
    let layout = validate_payload_layout(payload, limits)?;
    validate_decoded_budget(layout.color_count(), limits)?;
    let view = layout.validate_crc()?;
    decode_view_with_allocator(view, allocator)
}

fn decode_view_with_allocator<A: DecodeAllocator>(
    view: PaletteView<'_>,
    allocator: &mut A,
) -> Result<Palette, PaletteDecodeError> {
    let mut colors = Vec::new();
    allocator.reserve_colors(&mut colors, view.len())?;
    colors.extend(view.colors());
    Ok(Palette { colors })
}

fn validate_decoded_budget(
    color_count: usize,
    limits: &PayloadLimits,
) -> Result<(), PaletteDecodeError> {
    let needed = checked_decoded_bytes(color_count)?;
    let limit = limits.max_decoded_bytes();
    if needed > limit {
        return Err(PaletteDecodeError::DecodedBytesLimitExceeded { needed, limit });
    }
    Ok(())
}

fn checked_decoded_bytes(color_count: usize) -> Result<usize, PaletteDecodeError> {
    color_count
        .checked_mul(size_of::<Color>())
        .ok_or(PaletteDecodeError::SizeOverflow)
}

#[derive(Clone, Copy)]
pub(crate) struct PalettePayloadPlan<'a> {
    palette: &'a Palette,
    color_count: u32,
    covered_len: usize,
    payload_len: usize,
}

impl<'a> PalettePayloadPlan<'a> {
    fn new(palette: &'a Palette) -> Result<Self, PaletteEncodeError> {
        let color_count = checked_color_count(palette.colors.len())?;
        let colors_len = palette
            .colors
            .len()
            .checked_mul(COLOR_LEN)
            .ok_or_else(size_overflow)?;
        let covered_len = HEADER_LEN
            .checked_add(colors_len)
            .ok_or_else(size_overflow)?;
        let payload_len = checked_payload_len(covered_len).map_err(|_| size_overflow())?;
        Ok(Self {
            palette,
            color_count,
            covered_len,
            payload_len,
        })
    }

    pub(crate) const fn encoded_len(self) -> usize {
        self.payload_len
    }

    pub(crate) fn validate_limits(self, limits: &PayloadLimits) -> Result<(), PaletteDecodeError> {
        if self.color_count > limits.max_palette_colors() {
            return Err(PaletteDecodeError::TooManyColors {
                count: self.color_count,
                limit: limits.max_palette_colors(),
            });
        }
        validate_decoded_budget(self.palette.colors.len(), limits)
    }

    pub(crate) fn equals_payload(self, candidate: &[u8]) -> bool {
        if candidate.len() != self.payload_len {
            return false;
        }
        let count = self.color_count.to_le_bytes();
        if candidate[..HEADER_LEN]
            != [
                VERSION,
                crate::ColorFormat::RGBA8888.to_u8(),
                0,
                0,
                count[0],
                count[1],
                count[2],
                count[3],
            ]
        {
            return false;
        }
        let mut offset = HEADER_LEN;
        for color in &self.palette.colors {
            let end = offset + COLOR_LEN;
            if candidate[offset..end] != [color.r, color.g, color.b, color.a] {
                return false;
            }
            offset = end;
        }
        let expected_crc = crate::crc32(&candidate[..self.covered_len]).to_le_bytes();
        candidate[self.covered_len..] == expected_crc
    }

    fn copy_payload_into(self, out: &mut [u8]) -> Result<usize, PaletteEncodeError> {
        if out.len() < self.payload_len {
            return Err(PaletteEncodeError::BufferTooSmall {
                needed: self.payload_len,
                available: out.len(),
            });
        }

        let (payload, _) = out.split_at_mut(self.payload_len);
        payload[0] = VERSION;
        payload[1] = crate::ColorFormat::RGBA8888.to_u8();
        payload[2..4].fill(0);
        payload[4..8].copy_from_slice(&self.color_count.to_le_bytes());

        let mut offset = HEADER_LEN;
        for color in &self.palette.colors {
            payload[offset..offset + COLOR_LEN]
                .copy_from_slice(&[color.r, color.g, color.b, color.a]);
            offset += COLOR_LEN;
        }
        debug_assert_eq!(offset, self.covered_len);
        let (covered, trailer) = payload.split_at_mut(self.covered_len);
        let trailer: &mut [u8; 4] = trailer
            .try_into()
            .expect("planned PALETTE payload has one CRC trailer");
        write_crc_trailer(covered, trailer);
        Ok(self.payload_len)
    }

    pub(crate) fn payload_to_vec(self) -> Result<Vec<u8>, PaletteEncodeError> {
        self.payload_to_vec_with(|bytes, needed| {
            bytes
                .try_reserve_exact(needed)
                .map_err(|_| PaletteEncodeError::AllocationFailed)
        })
    }

    fn payload_to_vec_with(
        self,
        reserve: impl FnOnce(&mut Vec<u8>, usize) -> Result<(), PaletteEncodeError>,
    ) -> Result<Vec<u8>, PaletteEncodeError> {
        let mut payload = Vec::new();
        reserve(&mut payload, self.payload_len)?;
        payload.resize(self.payload_len, 0);
        self.copy_payload_into(&mut payload)?;
        Ok(payload)
    }
}

fn checked_color_count(actual: usize) -> Result<u32, PaletteEncodeError> {
    u32::try_from(actual).map_err(|_| PaletteEncodeError::ColorCountOverflow { actual })
}

const fn size_overflow() -> PaletteEncodeError {
    PaletteEncodeError::InvalidPayload(PaletteDecodeError::SizeOverflow)
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::vec;

    fn sample() -> Palette {
        Palette::from_colors(vec![
            Color::rgba(0x10, 0x20, 0x30, 0x40),
            Color::rgba(0xaa, 0xbb, 0xcc, 0xdd),
            Color::rgba(0x10, 0x20, 0x30, 0x40),
        ])
    }

    struct RejectDecodeReserve;

    impl DecodeAllocator for RejectDecodeReserve {
        fn reserve_colors(
            &mut self,
            _: &mut Vec<Color>,
            _: usize,
        ) -> Result<(), PaletteDecodeError> {
            Err(PaletteDecodeError::AllocationFailed)
        }
    }

    #[test]
    fn owned_decode_and_canonical_encode_preserve_order_and_duplicates() {
        let palette = sample();
        let payload = palette.encode_payload().unwrap();
        assert_eq!(payload.len(), 24);
        assert_eq!(payload[..8], [1, 0x61, 0, 0, 3, 0, 0, 0]);
        assert_eq!(
            &payload[8..20],
            &[
                0x10, 0x20, 0x30, 0x40, 0xaa, 0xbb, 0xcc, 0xdd, 0x10, 0x20, 0x30, 0x40
            ]
        );
        assert_eq!(
            Palette::decode_with_limits(&payload, &PayloadLimits::HOST),
            Ok(palette.clone())
        );
        assert!(palette.payload_plan().unwrap().equals_payload(&payload));

        let empty = Palette::new();
        let payload = empty.encode_payload().unwrap();
        assert_eq!(payload.len(), 12);
        assert_eq!(
            Palette::decode_with_limits(&payload, &PayloadLimits::EMBEDDED),
            Ok(empty)
        );
    }

    #[test]
    fn owned_budget_precedes_crc_and_decode_reserve() {
        let payload = sample().encode_payload().unwrap();
        let limit = size_of::<Color>() * 3 - 1;
        let limits = PayloadLimits::HOST.with_max_decoded_bytes(limit);
        assert_eq!(
            Palette::decode_with_limits(&payload, &limits),
            Err(PaletteDecodeError::DecodedBytesLimitExceeded {
                needed: size_of::<Color>() * 3,
                limit,
            })
        );

        let mut corrupt = payload.clone();
        let last = corrupt.len() - 1;
        corrupt[last] ^= 0x80;
        assert_eq!(
            Palette::decode_with_limits(&corrupt, &limits),
            Err(PaletteDecodeError::DecodedBytesLimitExceeded {
                needed: size_of::<Color>() * 3,
                limit,
            })
        );

        let layout = validate_payload_layout(&payload, &PayloadLimits::HOST).unwrap();
        let view = layout.validate_crc().unwrap();
        assert_eq!(
            decode_view_with_allocator(view, &mut RejectDecodeReserve),
            Err(PaletteDecodeError::AllocationFailed)
        );
    }

    #[test]
    fn positional_crud_and_move_are_exact_and_failure_atomic() {
        let a = Color::rgba(1, 2, 3, 4);
        let b = Color::rgba(5, 6, 7, 8);
        let c = Color::rgba(9, 10, 11, 12);
        let d = Color::rgba(13, 14, 15, 16);
        let mut palette = Palette::from_colors(vec![a, b, c]);

        palette.push(d).unwrap();
        palette.move_color(3, 1).unwrap();
        assert_eq!(palette.colors, [a, d, b, c]);
        assert_eq!(palette.replace(2, a), Ok(b));
        assert_eq!(palette.colors, [a, d, a, c]);
        assert_eq!(palette.remove(1), Ok(d));
        assert_eq!(palette.colors, [a, a, c]);
        palette.move_color(2, 0).unwrap();
        assert_eq!(palette.colors, [c, a, a]);
        palette.move_color(0, 2).unwrap();
        assert_eq!(palette.colors, [a, a, c]);
        palette.move_color(1, 1).unwrap();
        assert_eq!(palette.colors, [a, a, c]);

        let snapshot = palette.clone();
        assert_eq!(
            palette.insert(4, d),
            Err(PaletteMutationError::IndexOutOfBounds { index: 4, len: 3 })
        );
        for error in [
            palette.replace(3, d).unwrap_err(),
            palette.remove(3).unwrap_err(),
            palette.move_color(3, 0).unwrap_err(),
            palette.move_color(0, 3).unwrap_err(),
        ] {
            assert_eq!(
                error,
                PaletteMutationError::IndexOutOfBounds { index: 3, len: 3 }
            );
            assert_eq!(palette, snapshot);
        }

        assert_eq!(
            palette.insert_with(0, d, |_: &mut Vec<Color>| Err(
                PaletteMutationError::AllocationFailed
            )),
            Err(PaletteMutationError::AllocationFailed)
        );
        assert_eq!(palette, snapshot);
    }

    #[test]
    fn encode_into_is_atomic_suffix_safe_and_reserve_fallible() {
        let palette = sample();
        let needed = palette.encoded_payload_len().unwrap();
        let mut short = vec![0xa5; needed - 1];
        let before = short.clone();
        assert_eq!(
            palette.encode_payload_into(&mut short),
            Err(PaletteEncodeError::BufferTooSmall {
                needed,
                available: needed - 1,
            })
        );
        assert_eq!(short, before);

        let mut out = vec![0xa5; needed + 7];
        assert_eq!(palette.encode_payload_into(&mut out), Ok(needed));
        assert_eq!(&out[needed..], &[0xa5; 7]);
        assert_eq!(
            Palette::decode_with_limits(&out[..needed], &PayloadLimits::HOST),
            Ok(palette.clone())
        );

        assert_eq!(
            palette.encode_payload_with(|_, _| Err(PaletteEncodeError::AllocationFailed)),
            Err(PaletteEncodeError::AllocationFailed)
        );
    }

    #[test]
    fn sizing_and_limit_helpers_cover_wire_boundaries() {
        assert_eq!(checked_color_count(u32::MAX as usize), Ok(u32::MAX));
        if usize::BITS > 32 {
            let overflow = u32::MAX as usize + 1;
            assert_eq!(
                checked_color_count(overflow),
                Err(PaletteEncodeError::ColorCountOverflow { actual: overflow })
            );
        }
        assert_eq!(
            checked_decoded_bytes(usize::MAX),
            Err(PaletteDecodeError::SizeOverflow)
        );

        let palette = sample();
        let plan = palette.payload_plan().unwrap();
        let exact = PayloadLimits::HOST
            .with_max_palette_colors(3)
            .with_max_decoded_bytes(size_of::<Color>() * 3);
        assert_eq!(plan.validate_limits(&exact), Ok(()));
        assert_eq!(
            plan.validate_limits(&exact.with_max_palette_colors(2)),
            Err(PaletteDecodeError::TooManyColors { count: 3, limit: 2 })
        );
        assert!(matches!(
            plan.validate_limits(&exact.with_max_decoded_bytes(size_of::<Color>() * 3 - 1)),
            Err(PaletteDecodeError::DecodedBytesLimitExceeded { .. })
        ));

        let payload = palette.encode_payload().unwrap();
        let mut wrong_header = payload.clone();
        wrong_header[2] = 1;
        assert!(!plan.equals_payload(&wrong_header));
        let mut wrong_color = payload.clone();
        wrong_color[8] ^= 1;
        assert!(!plan.equals_payload(&wrong_color));
        let mut wrong_crc = payload.clone();
        *wrong_crc.last_mut().unwrap() ^= 1;
        assert!(!plan.equals_payload(&wrong_crc));
        assert!(!plan.equals_payload(&payload[..payload.len() - 1]));
    }
}