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
//! Capstone errors

use capstone_sys::{self, cs_strerror};
use capstone_sys::cs_err::*;

use std::ffi::CStr;
use std::fmt;
use std::error;
use std::result;

/// Create `RustFeatures` struct definition, `new()`, and a getter for each field
macro_rules! capstone_error_def {
    ( $( $( #[$attr:meta] )* => $rust_variant:ident = $cs_variant:ident; )* ) => {
        /// Error for Capstone
        #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
        pub enum CapstoneError {
            $(
                $(
                    #[$attr]
                )*
                $rust_variant,
            )*
        }

        #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
        /// An error enum for this library
        pub enum Error {
            /// An error emanating from the capstone framework library calls
            Capstone(CapstoneError),
            /// An unknown error not equal to a `CapstoneError`
            UnknownCapstoneError,
            /// Error with a custom message
            CustomError(&'static str),
        }

        impl From<capstone_sys::cs_err> for Error {
            fn from(err: capstone_sys::cs_err) -> Self {
                use self::Error::*;
                use self::CapstoneError::*;
                match err {
                    $(
                        $cs_variant => Capstone($rust_variant),
                    )*
                    _ => UnknownCapstoneError,
                }
            }
        }

        impl From<CapstoneError> for capstone_sys::cs_err {
            fn from(err: CapstoneError) -> Self {
                match err {
                    $(
                        CapstoneError::$rust_variant => $cs_variant,
                    )*
                }
            }
        }
    }
}

capstone_error_def!(
    /// Out-Of-Memory error: cs_open(), cs_disasm(), cs_disasm_iter()
    => OutOfMemory = CS_ERR_MEM;
    /// Unsupported Architecture: cs_open()
    => UnsupportedArch = CS_ERR_ARCH;
    /// Invalid Handle: cs_op_count(), cs_op_index()
    => InvalidHandle = CS_ERR_HANDLE;
    /// Invalid InvalidCsh argument: cs_close(), cs_errno(), cs_option()
    => InvalidCsh = CS_ERR_CSH;
    /// Invalid/unsupported mode: cs_open()
    => InvalidMode = CS_ERR_MODE;
    /// Invalid/unsupported option: cs_option()
    => InvalidOption = CS_ERR_OPTION;
    /// Information is unavailable because detail option is OFF
    => DetailOff = CS_ERR_DETAIL;
    /// Dynamic Memory management uninitialized (see CS_OPT_MEM)
    => UninitializedMemSetup = CS_ERR_MEMSETUP;
    /// Unsupported Version (bindings)
    => UnsupportedVersion = CS_ERR_VERSION;
    /// Access irrelevant data in "diet" engine
    => IrrelevantDataInDiet = CS_ERR_DIET;
    /// Access irrelevant data for "data" instruction in SKIPDATA Mode
    => IrrelevantDataInSkipData = CS_ERR_SKIPDATA;
    /// X86 AT&T syntax is unsupported (opt-out at compile time)
    => UnsupportedX86Att = CS_ERR_X86_ATT;
    /// X86 Intel syntax is unsupported (opt-out at compile time)
    => UnsupportedX86Intel = CS_ERR_X86_INTEL;
);

#[must_use]
pub type CsResult<T> = result::Result<T, Error>;

impl fmt::Display for Error {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        use self::Error::*;
        match *self {
            Capstone(err) => {
                let s = unsafe {
                    let err = cs_strerror(err.into());
                    CStr::from_ptr(err).to_string_lossy().into_owned()
                };
                write!(fmt, "{}", s)
            }
            UnknownCapstoneError => write!(fmt, "Encountered Unknown Capstone Return Error"),
            CustomError(msg) => write!(fmt, "{}", msg),
        }
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        use self::Error::*;
        use self::CapstoneError::*;
        match *self {
            Capstone(OutOfMemory) => "Out-Of-Memory error",
            Capstone(UnsupportedArch) => "Unsupported architecture",
            Capstone(InvalidHandle) => "Invalid handle",
            Capstone(InvalidCsh) => "Invalid csh argument",
            Capstone(InvalidMode) => "Invalid/unsupported mode",
            Capstone(InvalidOption) => "Invalid/unsupported option",
            Capstone(DetailOff) => "Information is unavailable because detail option is OFF",
            Capstone(UninitializedMemSetup) => {
                "Dynamic memory management uninitialized (see CS_OPT_MEM)"
            }
            Capstone(UnsupportedVersion) => "Unsupported version (bindings)",
            Capstone(IrrelevantDataInDiet) => "Access irrelevant data in \"diet\" engine",
            Capstone(IrrelevantDataInSkipData) => {
                "Access irrelevant data for \"data\" instruction in SKIPDATA mode"
            }
            Capstone(UnsupportedX86Att) => {
                "X86 AT&T syntax is unsupported (opt-out at compile time)"
            }
            Capstone(UnsupportedX86Intel) => {
                "X86 Intel syntax is unsupported (opt-out at compile time)"
            }
            UnknownCapstoneError => "Encountered Unknown Capstone Return Error",
            CustomError(msg) => msg,
        }
    }

    fn cause(&self) -> Option<&error::Error> {
        None
    }
}