glib/auto/
checksum.rs

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
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::{ffi, translate::*, ChecksumType};

crate::wrapper! {
    /// GLib provides a generic API for computing checksums (or ‘digests’)
    /// for a sequence of arbitrary bytes, using various hashing algorithms
    /// like MD5, SHA-1 and SHA-256. Checksums are commonly used in various
    /// environments and specifications.
    ///
    /// To create a new `GChecksum`, use `GLib::Checksum::new()`. To free
    /// a `GChecksum`, use `GLib::Checksum::free()`.
    ///
    /// GLib supports incremental checksums using the `GChecksum` data
    /// structure, by calling [`update()`][Self::update()] as long as there’s data
    /// available and then using [`string()`][Self::string()] or
    /// [`digest()`][Self::digest()] to compute the checksum and return it
    /// either as a string in hexadecimal form, or as a raw sequence of bytes. To
    /// compute the checksum for binary blobs and nul-terminated strings in
    /// one go, use the convenience functions [`compute_checksum_for_data()`][crate::compute_checksum_for_data()]
    /// and `compute_checksum_for_string()`, respectively.
    #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub struct Checksum(Boxed<ffi::GChecksum>);

    match fn {
        copy => |ptr| ffi::g_checksum_copy(ptr),
        free => |ptr| ffi::g_checksum_free(ptr),
        type_ => || ffi::g_checksum_get_type(),
    }
}

impl Checksum {
    /// Creates a new #GChecksum, using the checksum algorithm @checksum_type.
    /// If the @checksum_type is not known, [`None`] is returned.
    /// A #GChecksum can be used to compute the checksum, or digest, of an
    /// arbitrary binary blob, using different hashing algorithms.
    ///
    /// A #GChecksum works by feeding a binary blob through g_checksum_update()
    /// until there is data to be checked; the digest can then be extracted
    /// using g_checksum_get_string(), which will return the checksum as a
    /// hexadecimal string; or g_checksum_get_digest(), which will return a
    /// vector of raw bytes. Once either g_checksum_get_string() or
    /// g_checksum_get_digest() have been called on a #GChecksum, the checksum
    /// will be closed and it won't be possible to call g_checksum_update()
    /// on it anymore.
    /// ## `checksum_type`
    /// the desired type of checksum
    ///
    /// # Returns
    ///
    /// the newly created #GChecksum, or [`None`].
    ///   Use g_checksum_free() to free the memory allocated by it.
    #[doc(alias = "g_checksum_new")]
    pub fn new(checksum_type: ChecksumType) -> Option<Checksum> {
        unsafe { from_glib_full(ffi::g_checksum_new(checksum_type.into_glib())) }
    }

    /// Resets the state of the @self back to its initial state.
    #[doc(alias = "g_checksum_reset")]
    pub fn reset(&mut self) {
        unsafe {
            ffi::g_checksum_reset(self.to_glib_none_mut().0);
        }
    }

    /// Feeds @data into an existing #GChecksum. The checksum must still be
    /// open, that is g_checksum_get_string() or g_checksum_get_digest() must
    /// not have been called on @self.
    /// ## `data`
    /// buffer used to compute the checksum
    #[doc(alias = "g_checksum_update")]
    pub fn update(&mut self, data: &[u8]) {
        let length = data.len() as _;
        unsafe {
            ffi::g_checksum_update(self.to_glib_none_mut().0, data.to_glib_none().0, length);
        }
    }

    /// Gets the length in bytes of digests of type @checksum_type
    /// ## `checksum_type`
    /// a #GChecksumType
    ///
    /// # Returns
    ///
    /// the checksum length, or -1 if @checksum_type is
    /// not supported.
    #[doc(alias = "g_checksum_type_get_length")]
    pub fn type_get_length(checksum_type: ChecksumType) -> isize {
        unsafe { ffi::g_checksum_type_get_length(checksum_type.into_glib()) }
    }
}

unsafe impl Send for Checksum {}
unsafe impl Sync for Checksum {}