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
/// ! Utility code related to OCP

pub fn compress<D: as_slice::AsSlice<Element = u8>>(data: D) -> Vec<u8> {
    eprintln!("[WARNING] OCP compression has never been tested");

    let data = data.as_slice();
    const MARKER: u8 = 1;

    let mut res = Vec::new();

    res.push(b'M');
    res.push(b'J');
    res.push(b'H');

    let length = data.len();
    let high = (length >> 8) as u8;
    let low = (length % 256) as u8;

    res.push(low);
    res.push(high);

    let mut previous = 0;
    let mut count = 0;

    for current in &data[1..] {
        let current = *current;

        if current == MARKER {
            if count != 0 {
                res.push(MARKER);
                res.push(count);
                res.push(previous);
            }

            res.push(MARKER);
            res.push(1);
            res.push(MARKER);
        }
        else if previous == current {
            if count == 255 {
                res.push(MARKER);
                res.push(0);
                res.push(current);
                count = 0;
            }
            else {
                count += 1;
            }
        }
        else {
            if count == 1 {
                debug_assert!(MARKER != current);
                res.push(current);
            }
            else {
                res.push(MARKER);
                res.push(count);
                res.push(current);
            }
            count = 0;
        }

        previous = current;
    }

    if count == 1 {
        res.push(previous);
    }
    else if count > 1 {
        res.push(MARKER);
        res.push(count);
        res.push(previous);
    }

    res
}