1 #![allow(nonstandard_style)]
2 pub use self::consts::*;
3 pub use self::funcs::*;
4 pub use self::types::*;
5 
6 #[cfg(not(ctest))]
7 include!(concat!(env!("OUT_DIR"), "/version.rs"));
8 
9 pub mod types {
10     use libc::c_uint;
11 
12     pub type gpg_error_t = c_uint;
13     pub type gpg_err_source_t = c_uint;
14     pub type gpg_err_code_t = c_uint;
15 }
16 
17 pub mod consts {
18     use crate::types::{gpg_err_code_t, gpg_err_source_t, gpg_error_t};
19 
20     pub const GPG_ERR_SOURCE_DIM: gpg_err_source_t = 128;
21     pub const GPG_ERR_SOURCE_MASK: gpg_error_t = (GPG_ERR_SOURCE_DIM as gpg_error_t) - 1;
22     pub const GPG_ERR_SOURCE_SHIFT: gpg_error_t = 24;
23 
24     pub const GPG_ERR_SYSTEM_ERROR: gpg_err_code_t = 1 << 15;
25     pub const GPG_ERR_CODE_DIM: gpg_err_code_t = 65536;
26     pub const GPG_ERR_CODE_MASK: gpg_error_t = (GPG_ERR_CODE_DIM as gpg_error_t) - 1;
27 
28     #[cfg(not(ctest))]
29     include!(concat!(env!("OUT_DIR"), "/constants.rs"));
30 }
31 
32 pub mod funcs {
33     use libc::{c_char, c_int};
34 
35     use crate::types::{gpg_err_code_t, gpg_err_source_t, gpg_error_t};
36 
37     use crate::consts::*;
38 
39     #[inline]
gpg_err_make(source: gpg_err_source_t, code: gpg_err_code_t) -> gpg_error_t40     pub fn gpg_err_make(source: gpg_err_source_t, code: gpg_err_code_t) -> gpg_error_t {
41         // TODO: make const function when conditionals in const functions are stable
42         let code = code & GPG_ERR_CODE_MASK;
43         let source = source & GPG_ERR_SOURCE_MASK;
44         if code == GPG_ERR_NO_ERROR {
45             code
46         } else {
47             code | (source << GPG_ERR_SOURCE_SHIFT)
48         }
49     }
50 
51     #[inline]
gpg_err_code(err: gpg_error_t) -> gpg_err_code_t52     pub const fn gpg_err_code(err: gpg_error_t) -> gpg_err_code_t {
53         err & GPG_ERR_CODE_MASK
54     }
55 
56     #[inline]
gpg_err_source(err: gpg_error_t) -> gpg_err_source_t57     pub const fn gpg_err_source(err: gpg_error_t) -> gpg_err_source_t {
58         (err >> GPG_ERR_SOURCE_SHIFT) & GPG_ERR_SOURCE_MASK
59     }
60 
61     #[inline]
gpg_err_make_from_errno(source: gpg_err_source_t, err: c_int) -> gpg_error_t62     pub unsafe fn gpg_err_make_from_errno(source: gpg_err_source_t, err: c_int) -> gpg_error_t {
63         gpg_err_make(source, gpg_err_code_from_errno(err))
64     }
65 
66     #[inline]
gpg_error_from_errno(err: c_int) -> gpg_error_t67     pub unsafe fn gpg_error_from_errno(err: c_int) -> gpg_error_t {
68         gpg_err_make_from_errno(GPG_ERR_SOURCE_UNKNOWN, err)
69     }
70 
71     #[inline]
gpg_error_from_syserror() -> gpg_error_t72     pub unsafe fn gpg_error_from_syserror() -> gpg_error_t {
73         gpg_err_make(GPG_ERR_SOURCE_UNKNOWN, gpg_err_code_from_syserror())
74     }
75 
76     extern "C" {
gpg_err_init() -> gpg_error_t77         pub fn gpg_err_init() -> gpg_error_t;
gpg_err_deinit(mode: c_int)78         pub fn gpg_err_deinit(mode: c_int);
79 
gpg_strerror(err: gpg_error_t) -> *const c_char80         pub fn gpg_strerror(err: gpg_error_t) -> *const c_char;
gpg_strerror_r(err: gpg_error_t, buf: *mut c_char, buflen: usize) -> c_int81         pub fn gpg_strerror_r(err: gpg_error_t, buf: *mut c_char, buflen: usize) -> c_int;
82 
gpg_strsource(err: gpg_error_t) -> *const c_char83         pub fn gpg_strsource(err: gpg_error_t) -> *const c_char;
84 
gpg_err_code_from_errno(err: c_int) -> gpg_err_code_t85         pub fn gpg_err_code_from_errno(err: c_int) -> gpg_err_code_t;
gpg_err_code_to_errno(code: gpg_err_code_t) -> c_int86         pub fn gpg_err_code_to_errno(code: gpg_err_code_t) -> c_int;
gpg_err_code_from_syserror() -> gpg_err_code_t87         pub fn gpg_err_code_from_syserror() -> gpg_err_code_t;
88 
gpg_err_set_errno(err: c_int)89         pub fn gpg_err_set_errno(err: c_int);
90 
gpg_error_check_version(req_version: *const c_char) -> *const c_char91         pub fn gpg_error_check_version(req_version: *const c_char) -> *const c_char;
92     }
93 }
94