summaryrefslogtreecommitdiff
path: root/rust-bindings/src/checksum.rs
blob: d9e522f38247d8c4aa140dfb00ad208958cfa783 (plain)
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
use base64::{
    alphabet::Alphabet,
    engine::fast_portable::{FastPortable, FastPortableConfig},
    engine::DecodePaddingMode,
};
use glib::ffi::{g_free, g_malloc0, gpointer};
use glib::translate::{FromGlibPtrFull, FromGlibPtrNone};
use once_cell::sync::Lazy;
use std::ptr::copy_nonoverlapping;

const BYTES_LEN: usize = ffi::OSTREE_SHA256_DIGEST_LEN as usize;

static BASE64_ENGINE: Lazy<FastPortable> = Lazy::new(|| {
    // modified base64 alphabet used by ostree (uses _ instead of /)
    let alphabet =
        Alphabet::from_str("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+_")
            .expect("bad base64 alphabet");

    FastPortable::from(
        &alphabet,
        FastPortableConfig::new()
            .with_encode_padding(false)
            .with_decode_padding_mode(DecodePaddingMode::Indifferent),
    )
});

/// Error returned from parsing a checksum.
#[derive(Debug, thiserror::Error)]
pub enum ChecksumError {
    /// Invalid hex checksum string.
    #[error("invalid hex checksum string")]
    InvalidHexString,
    /// Invalid base64 checksum string
    #[error("invalid base64 checksum string")]
    InvalidBase64String,
}

/// A binary SHA256 checksum.
#[derive(Debug)]
pub struct Checksum {
    bytes: *mut [u8; BYTES_LEN],
}

// Safety: just a pointer to some memory owned by the type itself.
unsafe impl Send for Checksum {}

impl Checksum {
    /// Create a `Checksum` from a byte array.
    ///
    /// This copies the array.
    pub fn from_bytes(bytes: &[u8; BYTES_LEN]) -> Checksum {
        let mut checksum = Checksum::zeroed();
        checksum.as_mut().copy_from_slice(bytes);
        checksum
    }

    /// Create a `Checksum` from a hexadecimal SHA256 string.
    pub fn from_hex(hex_checksum: &str) -> Result<Checksum, ChecksumError> {
        let mut checksum = Checksum::zeroed();
        match hex::decode_to_slice(hex_checksum, checksum.as_mut()) {
            Ok(_) => Ok(checksum),
            Err(_) => Err(ChecksumError::InvalidHexString),
        }
    }

    /// Create a `Checksum` from a base64-encoded String.
    pub fn from_base64(b64_checksum: &str) -> Result<Checksum, ChecksumError> {
        let mut checksum = Checksum::zeroed();
        match base64::decode_engine_slice(b64_checksum, checksum.as_mut(), &*BASE64_ENGINE) {
            Ok(BYTES_LEN) => Ok(checksum),
            Ok(_) => Err(ChecksumError::InvalidBase64String),
            Err(_) => Err(ChecksumError::InvalidBase64String),
        }
    }

    /// Convert checksum to hex-encoded string.
    pub fn to_hex(&self) -> String {
        hex::encode(self.as_slice())
    }

    /// Convert checksum to base64 string.
    pub fn to_base64(&self) -> String {
        base64::encode_engine(self.as_slice(), &*BASE64_ENGINE)
    }

    /// Create a `Checksum` value, taking ownership of the given memory location.
    ///
    /// # Safety
    /// `bytes` must point to an initialized 32-byte memory location that is freeable with
    /// `g_free` (this is e.g. the case if the memory was allocated with `g_malloc`). The returned
    /// value takes ownership of the memory and frees it on drop.
    unsafe fn new(bytes: *mut [u8; BYTES_LEN]) -> Checksum {
        assert!(!bytes.is_null());
        Checksum { bytes }
    }

    /// Create a `Checksum` value initialized to 0.
    fn zeroed() -> Checksum {
        let bytes = unsafe { g_malloc0(BYTES_LEN) as *mut [u8; BYTES_LEN] };
        Checksum { bytes }
    }

    /// Get a shared reference to the inner array.
    fn as_slice(&self) -> &[u8; BYTES_LEN] {
        unsafe { &(*self.bytes) }
    }

    /// Get a mutable reference to the inner array.
    fn as_mut(&mut self) -> &mut [u8; BYTES_LEN] {
        unsafe { &mut (*self.bytes) }
    }
}

impl Drop for Checksum {
    fn drop(&mut self) {
        unsafe {
            g_free(self.bytes as gpointer);
        }
    }
}

impl Clone for Checksum {
    fn clone(&self) -> Self {
        unsafe { Checksum::from_glib_none(self.bytes) }
    }
}

impl PartialEq for Checksum {
    fn eq(&self, other: &Self) -> bool {
        unsafe {
            let ret =
                ffi::ostree_cmp_checksum_bytes(self.bytes as *const u8, other.bytes as *const u8);
            ret == 0
        }
    }
}

impl Eq for Checksum {}

impl std::fmt::Display for Checksum {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.to_hex())
    }
}

impl FromGlibPtrFull<*mut [u8; BYTES_LEN]> for Checksum {
    unsafe fn from_glib_full(ptr: *mut [u8; BYTES_LEN]) -> Self {
        Checksum::new(ptr)
    }
}

impl FromGlibPtrFull<*mut [*mut u8; BYTES_LEN]> for Checksum {
    unsafe fn from_glib_full(ptr: *mut [*mut u8; BYTES_LEN]) -> Self {
        Checksum::new(ptr as *mut u8 as *mut [u8; BYTES_LEN])
    }
}

impl FromGlibPtrFull<*mut u8> for Checksum {
    unsafe fn from_glib_full(ptr: *mut u8) -> Self {
        Checksum::new(ptr as *mut [u8; BYTES_LEN])
    }
}

impl FromGlibPtrNone<*mut [u8; BYTES_LEN]> for Checksum {
    unsafe fn from_glib_none(ptr: *mut [u8; BYTES_LEN]) -> Self {
        let checksum = Checksum::zeroed();
        // copy one array of BYTES_LEN elements
        copy_nonoverlapping(ptr, checksum.bytes, 1);
        checksum
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use glib::ffi::g_malloc0;
    use glib::translate::from_glib_full;

    const CHECKSUM_BYTES: &[u8; BYTES_LEN] = b"\xbf\x87S\x06x>\xfd\xc5\xbc\xab7\xea\x10\xb6\xcaN\x9bj\xea\x8b\x94X\r\x0c\xa9J\xf1 V\\\x0e\x8a";
    const CHECKSUM_HEX: &str = "bf875306783efdc5bcab37ea10b6ca4e9b6aea8b94580d0ca94af120565c0e8a";
    const CHECKSUM_BASE64: &str = "v4dTBng+_cW8qzfqELbKTptq6ouUWA0MqUrxIFZcDoo";

    #[test]
    fn should_create_checksum_from_bytes_taking_ownership() {
        let bytes = unsafe { g_malloc0(BYTES_LEN) } as *mut u8;
        let checksum: Checksum = unsafe { from_glib_full(bytes) };
        assert_eq!(checksum.to_string(), "00".repeat(BYTES_LEN));
    }

    #[test]
    fn should_create_checksum_from_bytes() {
        let checksum = Checksum::from_bytes(CHECKSUM_BYTES);
        assert_eq!(checksum.to_hex(), CHECKSUM_HEX);
    }

    #[test]
    fn should_parse_checksum_string_to_bytes() {
        let csum = Checksum::from_hex(CHECKSUM_HEX).unwrap();
        assert_eq!(csum.to_string(), CHECKSUM_HEX);
    }

    #[test]
    fn should_fail_for_too_short_hex_string() {
        let result = Checksum::from_hex(&"FF".repeat(31));
        assert!(result.is_err());
    }

    #[test]
    fn should_convert_checksum_to_base64() {
        let csum = Checksum::from_hex(CHECKSUM_HEX).unwrap();
        assert_eq!(csum.to_base64(), CHECKSUM_BASE64);
    }

    #[test]
    fn should_convert_base64_string_to_checksum() {
        let csum = Checksum::from_base64(CHECKSUM_BASE64).unwrap();
        assert_eq!(csum.to_base64(), CHECKSUM_BASE64);
        assert_eq!(csum.to_string(), CHECKSUM_HEX);
    }

    #[test]
    fn should_fail_for_too_short_b64_string() {
        let result = Checksum::from_base64("abcdefghi");
        assert!(result.is_err());
    }

    #[test]
    fn should_fail_for_invalid_base64_string() {
        let result = Checksum::from_base64(&"\n".repeat(43));
        assert!(result.is_err());
    }

    #[test]
    fn should_compare_checksums() {
        let csum = Checksum::from_hex(CHECKSUM_HEX).unwrap();
        assert_eq!(csum, csum);
        let csum2 = Checksum::from_hex(CHECKSUM_HEX).unwrap();
        assert_eq!(csum2, csum);
    }

    #[test]
    fn should_clone_value() {
        let csum = Checksum::from_hex(CHECKSUM_HEX).unwrap();
        let csum2 = csum.clone();
        assert_eq!(csum2, csum);
        let csum3 = csum2.clone();
        assert_eq!(csum3, csum);
        assert_eq!(csum3, csum2);
    }
}