libgpg_error_sys/
lib.rs

1#![allow(nonstandard_style)]
2#![no_std]
3pub use self::{consts::*, funcs::*, types::*};
4
5pub mod types {
6    use core::ffi::c_uint;
7
8    pub type gpg_error_t = c_uint;
9    pub type gpg_err_source_t = c_uint;
10    pub type gpg_err_code_t = c_uint;
11}
12
13pub mod consts {
14    use crate::types::{gpg_err_code_t, gpg_err_source_t, gpg_error_t};
15
16    pub const GPG_ERR_SOURCE_DIM: gpg_err_source_t = 128;
17    pub const GPG_ERR_SOURCE_MASK: gpg_error_t = (GPG_ERR_SOURCE_DIM as gpg_error_t) - 1;
18    pub const GPG_ERR_SOURCE_SHIFT: gpg_error_t = 24;
19
20    pub const GPG_ERR_SYSTEM_ERROR: gpg_err_code_t = 1 << 15;
21    pub const GPG_ERR_CODE_DIM: gpg_err_code_t = 65536;
22    pub const GPG_ERR_CODE_MASK: gpg_error_t = (GPG_ERR_CODE_DIM as gpg_error_t) - 1;
23
24    include!("consts.rs");
25}
26
27pub mod funcs {
28    use core::ffi::{c_char, c_int};
29
30    use crate::types::{gpg_err_code_t, gpg_err_source_t, gpg_error_t};
31
32    use crate::consts::*;
33
34    #[inline]
35    pub const fn gpg_err_make(source: gpg_err_source_t, code: gpg_err_code_t) -> gpg_error_t {
36        let code = code & GPG_ERR_CODE_MASK;
37        let source = source & GPG_ERR_SOURCE_MASK;
38        if code == GPG_ERR_NO_ERROR {
39            code
40        } else {
41            code | (source << GPG_ERR_SOURCE_SHIFT)
42        }
43    }
44
45    #[inline]
46    pub const fn gpg_err_code(err: gpg_error_t) -> gpg_err_code_t {
47        err & GPG_ERR_CODE_MASK
48    }
49
50    #[inline]
51    pub const fn gpg_err_source(err: gpg_error_t) -> gpg_err_source_t {
52        (err >> GPG_ERR_SOURCE_SHIFT) & GPG_ERR_SOURCE_MASK
53    }
54
55    #[inline]
56    pub unsafe fn gpg_err_make_from_errno(source: gpg_err_source_t, err: c_int) -> gpg_error_t {
57        gpg_err_make(source, gpg_err_code_from_errno(err))
58    }
59
60    #[inline]
61    pub unsafe fn gpg_error_from_errno(err: c_int) -> gpg_error_t {
62        gpg_err_make_from_errno(GPG_ERR_SOURCE_UNKNOWN, err)
63    }
64
65    #[inline]
66    pub unsafe fn gpg_error_from_syserror() -> gpg_error_t {
67        gpg_err_make(GPG_ERR_SOURCE_UNKNOWN, gpg_err_code_from_syserror())
68    }
69
70    #[cfg_attr(
71        all(windows, feature = "windows_raw_dylib"),
72        link(
73            name = "libgpg-error-0.dll",
74            kind = "raw-dylib",
75            modifiers = "+verbatim"
76        )
77    )]
78    extern "C" {
79        pub fn gpg_err_init() -> gpg_error_t;
80        pub fn gpg_err_deinit(mode: c_int);
81
82        pub fn gpg_strerror(err: gpg_error_t) -> *const c_char;
83        pub fn gpg_strerror_r(err: gpg_error_t, buf: *mut c_char, buflen: usize) -> c_int;
84
85        pub fn gpg_strsource(err: gpg_error_t) -> *const c_char;
86
87        pub fn gpg_err_code_from_errno(err: c_int) -> gpg_err_code_t;
88        pub fn gpg_err_code_to_errno(code: gpg_err_code_t) -> c_int;
89        pub fn gpg_err_code_from_syserror() -> gpg_err_code_t;
90
91        pub fn gpg_err_set_errno(err: c_int);
92
93        pub fn gpg_error_check_version(req_version: *const c_char) -> *const c_char;
94    }
95}