glib/auto/
checksum.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{ffi, translate::*, ChecksumType};
6
7crate::wrapper! {
8    /// GLib provides a generic API for computing checksums (or ‘digests’)
9    /// for a sequence of arbitrary bytes, using various hashing algorithms
10    /// like MD5, SHA-1 and SHA-256. Checksums are commonly used in various
11    /// environments and specifications.
12    ///
13    /// To create a new `GChecksum`, use `GLib::Checksum::new()`. To free
14    /// a `GChecksum`, use `GLib::Checksum::free()`.
15    ///
16    /// GLib supports incremental checksums using the `GChecksum` data
17    /// structure, by calling [`update()`][Self::update()] as long as there’s data
18    /// available and then using [`string()`][Self::string()] or
19    /// [`digest()`][Self::digest()] to compute the checksum and return it
20    /// either as a string in hexadecimal form, or as a raw sequence of bytes. To
21    /// compute the checksum for binary blobs and nul-terminated strings in
22    /// one go, use the convenience functions [`compute_checksum_for_data()`][crate::compute_checksum_for_data()]
23    /// and `compute_checksum_for_string()`, respectively.
24    // rustdoc-stripper-ignore-next-stop
25    /// GLib provides a generic API for computing checksums (or ‘digests’)
26    /// for a sequence of arbitrary bytes, using various hashing algorithms
27    /// like MD5, SHA-1 and SHA-256. Checksums are commonly used in various
28    /// environments and specifications.
29    ///
30    /// To create a new `GChecksum`, use `GLib::Checksum::new()`. To free
31    /// a `GChecksum`, use `GLib::Checksum::free()`.
32    ///
33    /// GLib supports incremental checksums using the `GChecksum` data
34    /// structure, by calling [`update()`][Self::update()] as long as there’s data
35    /// available and then using [`string()`][Self::string()] or
36    /// [`digest()`][Self::digest()] to compute the checksum and return it
37    /// either as a string in hexadecimal form, or as a raw sequence of bytes. To
38    /// compute the checksum for binary blobs and nul-terminated strings in
39    /// one go, use the convenience functions [`compute_checksum_for_data()`][crate::compute_checksum_for_data()]
40    /// and `compute_checksum_for_string()`, respectively.
41    #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
42    pub struct Checksum(Boxed<ffi::GChecksum>);
43
44    match fn {
45        copy => |ptr| ffi::g_checksum_copy(ptr),
46        free => |ptr| ffi::g_checksum_free(ptr),
47        type_ => || ffi::g_checksum_get_type(),
48    }
49}
50
51impl Checksum {
52    /// Creates a new #GChecksum, using the checksum algorithm @checksum_type.
53    /// If the @checksum_type is not known, [`None`] is returned.
54    /// A #GChecksum can be used to compute the checksum, or digest, of an
55    /// arbitrary binary blob, using different hashing algorithms.
56    ///
57    /// A #GChecksum works by feeding a binary blob through g_checksum_update()
58    /// until there is data to be checked; the digest can then be extracted
59    /// using g_checksum_get_string(), which will return the checksum as a
60    /// hexadecimal string; or g_checksum_get_digest(), which will return a
61    /// vector of raw bytes. Once either g_checksum_get_string() or
62    /// g_checksum_get_digest() have been called on a #GChecksum, the checksum
63    /// will be closed and it won't be possible to call g_checksum_update()
64    /// on it anymore.
65    /// ## `checksum_type`
66    /// the desired type of checksum
67    ///
68    /// # Returns
69    ///
70    /// the newly created #GChecksum, or [`None`].
71    ///   Use g_checksum_free() to free the memory allocated by it.
72    // rustdoc-stripper-ignore-next-stop
73    /// Creates a new #GChecksum, using the checksum algorithm @checksum_type.
74    /// If the @checksum_type is not known, [`None`] is returned.
75    /// A #GChecksum can be used to compute the checksum, or digest, of an
76    /// arbitrary binary blob, using different hashing algorithms.
77    ///
78    /// A #GChecksum works by feeding a binary blob through g_checksum_update()
79    /// until there is data to be checked; the digest can then be extracted
80    /// using g_checksum_get_string(), which will return the checksum as a
81    /// hexadecimal string; or g_checksum_get_digest(), which will return a
82    /// vector of raw bytes. Once either g_checksum_get_string() or
83    /// g_checksum_get_digest() have been called on a #GChecksum, the checksum
84    /// will be closed and it won't be possible to call g_checksum_update()
85    /// on it anymore.
86    /// ## `checksum_type`
87    /// the desired type of checksum
88    ///
89    /// # Returns
90    ///
91    /// the newly created #GChecksum, or [`None`].
92    ///   Use g_checksum_free() to free the memory allocated by it.
93    #[doc(alias = "g_checksum_new")]
94    pub fn new(checksum_type: ChecksumType) -> Option<Checksum> {
95        unsafe { from_glib_full(ffi::g_checksum_new(checksum_type.into_glib())) }
96    }
97
98    /// Resets the state of the @self back to its initial state.
99    // rustdoc-stripper-ignore-next-stop
100    /// Resets the state of the @self back to its initial state.
101    #[doc(alias = "g_checksum_reset")]
102    pub fn reset(&mut self) {
103        unsafe {
104            ffi::g_checksum_reset(self.to_glib_none_mut().0);
105        }
106    }
107
108    /// Feeds @data into an existing #GChecksum. The checksum must still be
109    /// open, that is g_checksum_get_string() or g_checksum_get_digest() must
110    /// not have been called on @self.
111    /// ## `data`
112    /// buffer used to compute the checksum
113    // rustdoc-stripper-ignore-next-stop
114    /// Feeds @data into an existing #GChecksum. The checksum must still be
115    /// open, that is g_checksum_get_string() or g_checksum_get_digest() must
116    /// not have been called on @self.
117    /// ## `data`
118    /// buffer used to compute the checksum
119    #[doc(alias = "g_checksum_update")]
120    pub fn update(&mut self, data: &[u8]) {
121        let length = data.len() as _;
122        unsafe {
123            ffi::g_checksum_update(self.to_glib_none_mut().0, data.to_glib_none().0, length);
124        }
125    }
126
127    /// Gets the length in bytes of digests of type @checksum_type
128    /// ## `checksum_type`
129    /// a #GChecksumType
130    ///
131    /// # Returns
132    ///
133    /// the checksum length, or -1 if @checksum_type is
134    /// not supported.
135    // rustdoc-stripper-ignore-next-stop
136    /// Gets the length in bytes of digests of type @checksum_type
137    /// ## `checksum_type`
138    /// a #GChecksumType
139    ///
140    /// # Returns
141    ///
142    /// the checksum length, or -1 if @checksum_type is
143    /// not supported.
144    #[doc(alias = "g_checksum_type_get_length")]
145    pub fn type_get_length(checksum_type: ChecksumType) -> isize {
146        unsafe { ffi::g_checksum_type_get_length(checksum_type.into_glib()) }
147    }
148}
149
150unsafe impl Send for Checksum {}
151unsafe impl Sync for Checksum {}